1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Combobox widgets - text field with a drop-down list of options.
//!
//! * also see the Tk [manual](https://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_combobox.htm)
//! # Events
//!
//! Use [bind](widget::TkWidget::bind) to call a function on following event:
//!
//! * `<<ComboboxSelected>>` - when an element is selected

use super::grid;
use super::pack;
use super::widget;
use super::wish;

/// Refers to a combobox widget
#[derive(Clone, Debug, PartialEq)]
pub struct TkCombobox {
    pub id: String,
}

/// Creates an instance of a combo-box widget in given parent,
/// populating the drop-down list with the given set of values.
pub fn make_combobox(parent: &impl widget::TkWidget, values: &[&str]) -> TkCombobox {
    let id = wish::next_wid(parent.id());
    let mut values_str = String::new();
    for value in values {
        values_str.push('{');
        values_str.push_str(value);
        values_str.push('}');
        values_str.push(' ');
    }

    let msg = format!("ttk::combobox {} -values {{{}}}", id, values_str);
    wish::tell_wish(&msg);

    TkCombobox { id }
}

impl widget::TkWidget for TkCombobox {
    /// Returns the widget's id reference - used within tk
    fn id(&self) -> &str {
        &self.id
    }
}
impl grid::TkGridLayout for TkCombobox {}
impl pack::TkPackLayout for TkCombobox {}

impl TkCombobox {
    /// Sets the height of the widget, in rows
    pub fn height(&self, value: u64) {
        let msg = format!("{} configure -height {{{}}}", self.id, value);
        wish::tell_wish(&msg);
    }

    /// Alignment of text within widget
    pub fn justify(&self, value: widget::Justify) {
        widget::configure(&self.id, "justify", &value.to_string());
    }

    /// Sets the state of the widget (readonly, normal or disabled).
    pub fn state(&self, value: widget::State) {
        widget::configure(&self.id, "state", &value.to_string());
    }

    /// Sets the current value
    pub fn value(&self, value: &str) {
        let msg = format!("puts [{} set {{{}}}] ; flush stdout", self.id, value);
        wish::tell_wish(&msg);
    }

    /// Returns the current value
    pub fn value_get(&self) -> String {
        let msg = format!("puts [{} get] ; flush stdout", self.id);
        wish::ask_wish(&msg)
    }

    /// Sets the width of the widget, in characters
    pub fn width(&self, value: u64) {
        let msg = format!("{} configure -width {{{}}}", self.id, value);
        wish::tell_wish(&msg);
    }
}