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
121 lines (97 loc) · 3.94 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use gtk::gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use gtk::{gio, glib};
use gtk::{
ApplicationWindow, ButtonsType, CellRendererPixbuf, CellRendererText, DialogFlags,
MessageDialog, MessageType, Orientation, TreeStore, TreeView, TreeViewColumn, WindowPosition,
};
fn append_text_column(tree: &TreeView) {
let column = TreeViewColumn::new();
let cell = CellRendererText::new();
TreeViewColumnExt::pack_start(&column, &cell, true);
TreeViewColumnExt::add_attribute(&column, &cell, "text", 0);
tree.append_column(&column);
}
fn build_ui(application: >k::Application) {
let window = ApplicationWindow::new(application);
window.set_title("TreeView example");
window.set_position(WindowPosition::Center);
// left pane
let left_tree = TreeView::new();
let left_store = TreeStore::new(&[String::static_type()]);
left_tree.set_model(Some(&left_store));
left_tree.set_headers_visible(false);
append_text_column(&left_tree);
for i in 0..10 {
// insert_with_values takes a slice of tuples: column index and ToValue
// trait objects. ToValue is implemented for strings, numeric types,
// bool and Object descendants
let iter = left_store.insert_with_values(None, None, &[(0, &format!("Hello {i}"))]);
for _ in 0..i {
left_store.insert_with_values(Some(&iter), None, &[(0, &"I'm a child node")]);
}
}
// right pane
let right_tree = TreeView::new();
let right_column_types = [Pixbuf::static_type(), String::static_type()];
let right_store = TreeStore::new(&right_column_types);
let renderer = CellRendererPixbuf::new();
let col = TreeViewColumn::new();
col.set_title("Picture");
TreeViewColumnExt::pack_start(&col, &renderer, false);
TreeViewColumnExt::add_attribute(&col, &renderer, "pixbuf", 0);
let renderer2 = CellRendererText::new();
TreeViewColumnExt::pack_start(&col, &renderer2, true);
TreeViewColumnExt::add_attribute(&col, &renderer2, "text", 1);
let image = Pixbuf::from_resource("/org/gtk-rs/examples/eye.png")
.map_err(|err| {
let msg = err.to_string();
glib::idle_add_local(
glib::clone!(@weak window => @default-return glib::ControlFlow::Break, move || {
let dialog = MessageDialog::new(Some(&window), DialogFlags::MODAL,
MessageType::Error, ButtonsType::Ok, &msg);
dialog.connect_response(|dialog, _| dialog.close());
dialog.show_all();
glib::ControlFlow::Break
}),
);
})
.ok();
right_tree.append_column(&col);
right_tree.set_model(Some(&right_store));
right_tree.set_headers_visible(true);
for _ in 0..10 {
right_store.insert_with_values(
None,
None,
&[(0, &image), (1, &"I'm a child node with an image")],
);
}
// selection and path manipulation
let left_selection = left_tree.selection();
left_selection.connect_changed(glib::clone!(@weak right_tree => move |tree_selection| {
let (left_model, iter) = tree_selection.selected().expect("Couldn't get selected");
let mut path = left_model.path(&iter).expect("Couldn't get path");
// get the top-level element path
while path.depth() > 1 {
path.up();
}
right_tree.selection().select_path(&path);
}));
// display the panes
let split_pane = gtk::Box::new(Orientation::Horizontal, 10);
split_pane.set_size_request(-1, -1);
split_pane.add(&left_tree);
split_pane.add(&right_tree);
window.add(&split_pane);
window.show_all();
}
fn main() {
gio::resources_register_include!("compiled.gresource").unwrap();
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.treeview"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}