This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.rs
100 lines (82 loc) · 3.1 KB
/
main.rs
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
use gtk::prelude::*;
use gtk::{
ApplicationWindow, CellRendererText, Label, ListStore, Orientation, TreeView, TreeViewColumn,
WindowPosition,
};
fn create_and_fill_model() -> ListStore {
// Creation of a model with two rows.
let model = ListStore::new(&[u32::static_type(), String::static_type()]);
// Filling up the tree view.
let entries = &["Michel", "Sara", "Liam", "Zelda", "Neo", "Octopus master"];
for (i, entry) in entries.iter().enumerate() {
model.insert_with_values(None, &[(0, &(i as u32 + 1)), (1, &entry)]);
}
model
}
fn append_column(tree: &TreeView, id: i32) {
let column = TreeViewColumn::new();
let cell = CellRendererText::new();
TreeViewColumnExt::pack_start(&column, &cell, true);
// Association of the view's column with the model's `id` column.
TreeViewColumnExt::add_attribute(&column, &cell, "text", id);
tree.append_column(&column);
}
fn create_and_setup_view() -> TreeView {
// Creating the tree view.
let tree = TreeView::new();
tree.set_headers_visible(false);
// Creating the two columns inside the view.
append_column(&tree, 0);
append_column(&tree, 1);
tree
}
fn build_ui(application: >k::Application) {
let window = ApplicationWindow::new(application);
window.set_title("Simple TreeView example");
window.set_position(WindowPosition::Center);
// Creating a vertical layout to place both tree view and label in the window.
let vertical_layout = gtk::Box::new(Orientation::Vertical, 0);
// Creation of the label.
let label = Label::new(None);
let tree = create_and_setup_view();
let model = create_and_fill_model();
// Setting the model into the view.
tree.set_model(Some(&model));
// Adding the view to the layout.
vertical_layout.add(&tree);
// Same goes for the label.
vertical_layout.add(&label);
// The closure responds to selection changes by connection to "::cursor-changed" signal,
// that gets emitted when the cursor moves (focus changes).
tree.connect_cursor_changed(move |tree_view| {
let selection = tree_view.selection();
if let Some((model, iter)) = selection.selected() {
// Now getting back the values from the row corresponding to the
// iterator `iter`.
//
// The `get_value` method do the conversion between the gtk type and Rust.
label.set_text(&format!(
"Hello '{}' from row {}",
model
.value(&iter, 1)
.get::<String>()
.expect("Treeview selection, column 1"),
model
.value(&iter, 0)
.get::<u32>()
.expect("Treeview selection, column 0"),
));
}
});
// Adding the layout to the window.
window.add(&vertical_layout);
window.show_all();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.simple_treeview"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}