Skip to content

Commit 3109847

Browse files
jimbrankellybilelmoussaoui
authored andcommitted
examples: Add a new ListBox based one
Modified version of the ListBox-Model example using a StringList that is sorted. The StringSorter object uses a PropertyExpression to specify the sorted parameter.
1 parent 8725108 commit 3109847

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

examples/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ name = "list_box_model"
174174
path = "list_box_model/main.rs"
175175
required-features = ["im-rc"]
176176

177+
[[bin]]
178+
name = "list_box_sort_stringlist"
179+
path = "list_box_sort_stringlist/main.rs"
180+
required-features = ["v4_14"]
181+
177182
[[bin]]
178183
name = "list_view_apps_launcher"
179184
path = "list_view_apps_launcher/main.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ cargo run --bin basics
4545
- [Grid Packing](./grid_packing)
4646
- [GtkBuilder](./gtk_builder/)
4747
- [ListBox and ListModel](./list_box_model/)
48+
- [ListBox: StringList with Sorter](./listbox_sort_stringlist/)
4849
- [ListView: Applications Launcher](./list_view_apps_launcher/)
4950
- [Menubar](./menubar/)
5051
- [Rotation Bin](./rotation_bin/)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ListBox and StringListModel with Sorter example
2+
3+
This example demonstrates how to use `gtk::ListBox` in combination with
4+
a StringList model and StringSorter with a PropertyExpression.
5+
6+
It sets up a `gtk::ListBox` containing an Inscription on each row.
7+
The rows are sorted alphabetically.
8+
9+
Run it by executing:
10+
11+
```bash
12+
cargo run --bin list_box_sort_stringlist --features v4_14
13+
``
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Variation on ListBox model example to show use of StringList and StringSorter.
2+
3+
use gtk::{glib, prelude::*};
4+
5+
fn main() -> glib::ExitCode {
6+
let application = gtk::Application::builder()
7+
.application_id("com.github.gtk-rs.examples.SortedStringList")
8+
.build();
9+
10+
application.connect_activate(build_ui);
11+
12+
application.run()
13+
}
14+
15+
fn build_ui(application: &gtk::Application) {
16+
let window = gtk::ApplicationWindow::builder()
17+
.default_width(320)
18+
.default_height(200)
19+
.application(application)
20+
.title("Sorted StringList")
21+
.build();
22+
23+
// Create a StringSorter with a property expression to sort
24+
// StringObjects in a StringList. StringObject has a "string" property.
25+
let expression = gtk::PropertyExpression::new(
26+
gtk::StringObject::static_type(),
27+
None::<gtk::Expression>,
28+
"string",
29+
);
30+
let sorter = gtk::StringSorter::new(Some(expression));
31+
sorter.set_ignore_case(true);
32+
33+
// Create our list store as a StringList and populate with some strings.
34+
let model = gtk::StringList::new(&["zoo", "abba", "donkey", "sunrise", "river", "phoenix"]);
35+
36+
// Create a sort model and bind it to the ListStore and the sorter.
37+
let sort_model = gtk::SortListModel::new(Some(model.clone()), Some(sorter));
38+
39+
// And then create the UI part, the listbox and bind the sort
40+
// model to it. Whenever the UI needs to show a new row, e.g. because
41+
// it was notified that the model changed, it will call the callback
42+
// with the corresponding item from the model and will ask for a new
43+
// gtk::ListBoxRow that should be displayed.
44+
//
45+
// The gtk::ListBoxRow can contain any possible widgets.
46+
// Here we use an Inscription.
47+
48+
let listbox = gtk::ListBox::new();
49+
listbox.bind_model(Some(&sort_model), move |obj| {
50+
let list_object = obj
51+
.downcast_ref::<gtk::StringObject>()
52+
.expect("The object should be of type `StringObject`.");
53+
gtk::Inscription::builder()
54+
.xalign(0.0)
55+
.text(list_object.string())
56+
.build()
57+
.upcast()
58+
});
59+
60+
let scrolled_window = gtk::ScrolledWindow::builder()
61+
.hscrollbar_policy(gtk::PolicyType::Never) // Disable horizontal scrolling
62+
.min_content_height(200)
63+
.min_content_width(360)
64+
.build();
65+
66+
scrolled_window.set_child(Some(&listbox));
67+
68+
window.set_child(Some(&scrolled_window));
69+
70+
for i in 0..10 {
71+
model.append(&format!("Name {i}"));
72+
}
73+
74+
window.present();
75+
}

0 commit comments

Comments
 (0)