From d4db92b3acf67aeaa4642042b056def20db01681 Mon Sep 17 00:00:00 2001 From: Shalom Date: Wed, 10 Aug 2022 13:19:50 +0900 Subject: [PATCH] Add example: Combobox with list store --- examples/Cargo.toml | 4 + examples/combobox_liststore/README.md | 11 +++ examples/combobox_liststore/main.rs | 100 +++++++++++++++++++++ examples/combobox_liststore/screenshot.png | Bin 0 -> 7352 bytes 4 files changed, 115 insertions(+) create mode 100644 examples/combobox_liststore/README.md create mode 100644 examples/combobox_liststore/main.rs create mode 100644 examples/combobox_liststore/screenshot.png diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 5364ec77f571..eec3493389b1 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -74,6 +74,10 @@ path = "clock/main.rs" name = "clone_macro" path = "clone_macro/main.rs" +[[bin]] +name = "combobox_liststore" +path = "combobox_liststore/main.rs" + [[bin]] name = "communication_thread" path = "communication_thread/main.rs" diff --git a/examples/combobox_liststore/README.md b/examples/combobox_liststore/README.md new file mode 100644 index 000000000000..75bde137cde7 --- /dev/null +++ b/examples/combobox_liststore/README.md @@ -0,0 +1,11 @@ +# Combobox liststore + +Example on how to use combobox and liststore. + +Run it by executing: + +```bash +cargo run --bin combobox_liststore +``` + +![screenshot](screenshot.png) \ No newline at end of file diff --git a/examples/combobox_liststore/main.rs b/examples/combobox_liststore/main.rs new file mode 100644 index 000000000000..cb5331146518 --- /dev/null +++ b/examples/combobox_liststore/main.rs @@ -0,0 +1,100 @@ +use glib::clone; +use gtk::{ + prelude::*, CellRendererText, CellRendererToggle, ComboBox, Entry, ListStore, TreeModel, +}; +use std::{cell::RefCell, rc::Rc}; + +fn build_ui(application: >k::Application) { + let window = gtk::ApplicationWindow::new(application); + window.set_title("First GTK+ Program"); + window.set_border_width(10); + window.set_position(gtk::WindowPosition::Center); + window.set_default_size(350, 70); + + let combobox = gtk::ComboBox::new(); + let renderer = CellRendererToggle::new(); + combobox.pack_start(&renderer, true); + combobox.add_attribute(&renderer, "active", 0); + + let renderer = CellRendererText::new(); + combobox.pack_start(&renderer, true); + combobox.add_attribute(&renderer, "text", 1); + combobox.set_entry_text_column(1); + + let model = gtk::ListStore::new(&[glib::Type::BOOL, glib::Type::STRING]); + let data: [(u32, &dyn ToValue); 2] = [(0, &true), (1, &"data1")]; + model.insert_with_values(Some(0), &data); + let data: [(u32, &dyn ToValue); 2] = [(0, &false), (1, &"date2")]; + model.insert_with_values(Some(1), &data); + let data: [(u32, &dyn ToValue); 2] = [(0, &false), (1, &"date3")]; + model.insert_with_values(Some(2), &data); + + combobox.set_model(Some(&model)); + window.add(&combobox); + let entry = gtk::Entry::new(); + entry.set_editable(false); + combobox.add(&entry); + + combobox.connect_changed(clone!( + @weak entry => move |cb| { + let model = cb.property::("model"); + let store = model.dynamic_cast_ref::().unwrap(); + if let Some(iter) = cb.active_iter() { + let value = store.value(&iter, 0).get::().unwrap(); + store.set(&iter, &[(0, &!value)]); + } + update_entry(&entry, cb); + cb.set_active_iter(None); + })); + combobox.connect_map(move |cb| update_entry(&entry, cb)); + window.show_all(); +} + +fn for_each(cb: >k::ComboBox, f: F) { + let model = cb.property::("model"); + if let Some(iter) = model.iter_first() { + let store = model.dynamic_cast_ref::().unwrap(); + + loop { + f(cb, &iter, store); + #[cfg(debug_assertions)] + { + if !store.iter_is_valid(&iter) { + unreachable!("iter is not valid: Don't remove iter in for_each.") + } + } + + if !model.iter_next(&iter) { + break; + } + } + } +} + +fn update_entry(entry: &Entry, cb: &ComboBox) { + entry.set_text(""); + let vec_string: Vec = Default::default(); + let vec_string = Rc::new(RefCell::new(vec_string)); + for_each( + cb, + clone!( + @weak vec_string, => move |_, iter, store| { + if store.value(iter, 0).get::().unwrap() { + let value = store.value(iter, 1).get::().unwrap(); + vec_string.borrow_mut().push(value); + } + }), + ); + entry.set_text(vec_string.borrow().join(", ").as_str()); +} + +fn main() { + let application = gtk::Application::new( + Some("com.github.gtk-rs.examples.combobox_liststore"), + Default::default(), + ); + + application.connect_activate(build_ui); + + application.run(); +} diff --git a/examples/combobox_liststore/screenshot.png b/examples/combobox_liststore/screenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..8275f87101ad13bba64901a0ffcf5568b148c8d1 GIT binary patch literal 7352 zcmaiZXIN8Pw=RN0q{Sc|1PuZrO`3Ef7(hU(fb?ENlOnxrKp=DjQluA=-U&5;^b)F6 zgMf6T_YOI6fBQSmIu6TIl9e+Nz zdYlR@@bDOw6`<0Zo<`fVB)*gr%|v^~6ck2*d;O6To~*Y}07@#Kmrj0$vO(hyjYu@2 zDW%(B_sSO|gYM-0x)V)J83@*+^sXFHB2z4#(EeU@=g~tMz^%AFfC9iP<>Qp3_3D7t z{pY6f_Ey62^TL^<0@haSsioK35bVH^=dA)N5)cUKI=3+og^myLjfBIwf&x{b79dhL zFgVgA>pn>LHbFYP7fibjg;KY@Uy_+AdFlobwLgm`0rhrC92c}TyatQ{b zr>Siuqiw~O$ZH&+<9Qk$+`Ks;R4&S;67G31S{QL6NeuwEhzSp)oOX?31THt9}}84$VZop&_j!o9u36zsJ}7Hn}ov)xV^kC zO}=G4K~B2F5L`nW0Y@;T92Dx%*V97;a-dpgkImm7hl0WJy>IA=$ivm`xYzb#b4Xpm z;KTZ-t`D@=*}umYfIy(nrfTEQgkYjQf&Ni{w)d;_XerRmD6N=f2CM&5blbn=NBLxU zfuXBTwsnO}T{3~EIY!a9WKxh1(!mg4&1ZdXP22kG!2n(9YP#D=E^Bf%2Pw%sRuB!y)V$BQ^H(B!#ZsF(rfhKR%oWkY zKCJ!bO|D#wmrvd9gV{TK7Cp#4QM zbDehd+3~hCOhY0`Ruow3Hg? zJsc~6AU$mQI;>_Fv+WrWPme4;v%O0z#YH$Qar4CcWUFwHD&dn!7iam>$N~$uKT~jS zSg|^~a^*Xs?;|DFxg{HEU@^`5(Er*I^ks2A(tW99Rti&f^H-X>ssHOJsSXN0?2-H$ z^8BK6UlVT%h*nP?+ltV6z$y2kDRS)(8dzPjQ+iq16Vy0FU|pQ)u( zMfWJ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