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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Spinbox widget - displays a range with up/down buttons. 
//!
//! User can select between either a range of numbers or a list of values.
//!
//! * also see the Tk [manual](https://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_spinbox.htm)

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

/// Refers to a spinbox widget for numbers
#[derive(Clone, Debug, PartialEq)]
pub struct TkSpinboxRange {
    pub id: String,
}

/// Creates an instance of a numeric-spinbox widget in given parent.
///
/// This spinbox is used to select between a range of numbers, defined as [from, to].
/// The increment specifies how much the number changes as the up/down arrow is clicked.
pub fn make_spinbox_range(
    parent: &impl widget::TkWidget,
    from: f64,
    to: f64,
    increment: f64,
) -> TkSpinboxRange {
    let id = wish::next_wid(parent.id());
    let msg = format!(
        "ttk::spinbox {} -from {} -to {} -increment {} ",
        id, from, to, increment
    );
    wish::tell_wish(&msg);

    TkSpinboxRange { id }
}

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

impl grid::TkGridLayout for TkSpinboxRange {}
impl pack::TkPackLayout for TkSpinboxRange {}

impl TkSpinboxRange {
    /// Sets the state of the widget; Readonly means user cannot enter
    /// their own value, but must pick from the given selection.
    pub fn state(&self, value: widget::State) {
        widget::configure(&self.id, "state", &value.to_string());
    }

    /// Retrieves the spinbox's value.
    pub fn value_get(&self) -> f64 {
        let msg = format!("puts [{} get] ; flush stdout", self.id);
        let result = wish::ask_wish(&msg);
        if let Ok(value) = result.parse::<f64>() {
            value
        } else {
            0.0
        }
    }

    /// Set to true so spinbox 'wraps' around at top/bottom.
    pub fn wrap(&self, value: bool) {
        widget::configure(&self.id, "wrap", if value { "1" } else { "0" });
    }
}

/// Refers to a spinbox widget for discrete values
#[derive(Clone, Debug, PartialEq)]
pub struct TkSpinboxValues {
    pub id: String,
}

/// Creates an instance of a values-spinbox widget in given parent.
///
/// This spinbox is used to select between a given list of string values.
pub fn make_spinbox_values(parent: &impl widget::TkWidget, values: &[&str]) -> TkSpinboxValues {
    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::spinbox {} -values {{{}}} ", id, values_str);
    wish::tell_wish(&msg);

    TkSpinboxValues { id }
}

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

impl grid::TkGridLayout for TkSpinboxValues {}
impl pack::TkPackLayout for TkSpinboxValues {}

impl TkSpinboxValues {
    /// Sets the state of the widget; Readonly means user cannot enter
    /// their own value, but must pick from the given selection.
    pub fn state(&self, value: widget::State) {
        widget::configure(&self.id, "state", &value.to_string());
    }

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

    /// Set to true so spinbox 'wraps' around at top/bottom.
    pub fn wrap(&self, value: bool) {
        widget::configure(&self.id, "wrap", if value { "1" } else { "0" });
    }
}