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
//! Scrollbar widget - displays a scrollbar to control position within a range.
//!
//! * also see the Tk [manual](https://www.tcl-lang.org/man/tcl8.6/TkCmd/ttk_scrollbar.htm)
//!

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

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

/// Creates an instance of an horizontal scrollbar in given parent item,
/// connected to associated widget.
///
pub fn make_horizontal_scrollbar(
    parent: &impl widget::TkWidget,
    widget: &impl widget::TkWidget,
) -> TkScrollbar {
    let id = wish::next_wid(parent.id());
    let msg = format!(
        "ttk::scrollbar {} -orient horizontal -command {{{} xview}}",
        id,
        widget.id()
    );
    wish::tell_wish(&msg);
    let msg = format!("{} configure -xscrollcommand {{{} set}}", widget.id(), id);
    wish::tell_wish(&msg);

    TkScrollbar { id }
}

/// Creates an instance of a vertical scrollbar in given parent item,
/// connected to associated widget.
///
pub fn make_vertical_scrollbar(
    parent: &impl widget::TkWidget,
    widget: &impl widget::TkWidget,
) -> TkScrollbar {
    let id = wish::next_wid(parent.id());
    let msg = format!(
        "ttk::scrollbar {} -orient vertical -command {{{} yview}}",
        id,
        widget.id()
    );
    wish::tell_wish(&msg);
    let msg = format!("{} configure -yscrollcommand {{{} set}}", widget.id(), id);
    wish::tell_wish(&msg);

    TkScrollbar { id }
}

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

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

impl TkScrollbar {}