|
| 1 | +use glib::clone; |
| 2 | +use gtk::{ |
| 3 | + prelude::*, CellRendererText, CellRendererToggle, ComboBox, Entry, ListStore, TreeModel, |
| 4 | +}; |
| 5 | +use std::{cell::RefCell, rc::Rc}; |
| 6 | + |
| 7 | +fn build_ui(application: >k::Application) { |
| 8 | + let window = gtk::ApplicationWindow::new(application); |
| 9 | + window.set_title("First GTK+ Program"); |
| 10 | + window.set_border_width(10); |
| 11 | + window.set_position(gtk::WindowPosition::Center); |
| 12 | + window.set_default_size(350, 70); |
| 13 | + |
| 14 | + let combobox = gtk::ComboBox::new(); |
| 15 | + let renderer = CellRendererToggle::new(); |
| 16 | + combobox.pack_start(&renderer, true); |
| 17 | + combobox.add_attribute(&renderer, "active", 0); |
| 18 | + |
| 19 | + let renderer = CellRendererText::new(); |
| 20 | + combobox.pack_start(&renderer, true); |
| 21 | + combobox.add_attribute(&renderer, "text", 1); |
| 22 | + combobox.set_entry_text_column(1); |
| 23 | + |
| 24 | + let model = gtk::ListStore::new(&[glib::Type::BOOL, glib::Type::STRING]); |
| 25 | + let data: [(u32, &dyn ToValue); 2] = [(0, &true), (1, &"data1")]; |
| 26 | + model.insert_with_values(Some(0), &data); |
| 27 | + let data: [(u32, &dyn ToValue); 2] = [(0, &false), (1, &"date2")]; |
| 28 | + model.insert_with_values(Some(1), &data); |
| 29 | + let data: [(u32, &dyn ToValue); 2] = [(0, &false), (1, &"date3")]; |
| 30 | + model.insert_with_values(Some(2), &data); |
| 31 | + |
| 32 | + combobox.set_model(Some(&model)); |
| 33 | + window.add(&combobox); |
| 34 | + let entry = gtk::Entry::new(); |
| 35 | + entry.set_editable(false); |
| 36 | + combobox.add(&entry); |
| 37 | + |
| 38 | + combobox.connect_changed(clone!( |
| 39 | + @weak entry => move |cb| { |
| 40 | + let model = cb.property::<TreeModel>("model"); |
| 41 | + let store = model.dynamic_cast_ref::<ListStore>().unwrap(); |
| 42 | + if let Some(iter) = cb.active_iter() { |
| 43 | + let value = store.value(&iter, 0).get::<bool>().unwrap(); |
| 44 | + store.set(&iter, &[(0, &!value)]); |
| 45 | + } |
| 46 | + update_entry(&entry, cb); |
| 47 | + cb.set_active_iter(None); |
| 48 | + })); |
| 49 | + combobox.connect_map(move |cb| update_entry(&entry, cb)); |
| 50 | + window.show_all(); |
| 51 | +} |
| 52 | + |
| 53 | +fn for_each<F: Fn(>k::ComboBox, >k::TreeIter, &ListStore)>(cb: >k::ComboBox, f: F) { |
| 54 | + let model = cb.property::<TreeModel>("model"); |
| 55 | + if let Some(iter) = model.iter_first() { |
| 56 | + let store = model.dynamic_cast_ref::<ListStore>().unwrap(); |
| 57 | + |
| 58 | + loop { |
| 59 | + f(cb, &iter, store); |
| 60 | + #[cfg(debug_assertions)] |
| 61 | + { |
| 62 | + if !store.iter_is_valid(&iter) { |
| 63 | + unreachable!("iter is not valid: Don't remove iter in for_each.") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if !model.iter_next(&iter) { |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn update_entry(entry: &Entry, cb: &ComboBox) { |
| 75 | + entry.set_text(""); |
| 76 | + let vec_string: Vec<String> = Default::default(); |
| 77 | + let vec_string = Rc::new(RefCell::new(vec_string)); |
| 78 | + for_each( |
| 79 | + cb, |
| 80 | + clone!( |
| 81 | + @weak vec_string, => move |_, iter, store| { |
| 82 | + if store.value(iter, 0).get::<bool>().unwrap() { |
| 83 | + let value = store.value(iter, 1).get::<String>().unwrap(); |
| 84 | + vec_string.borrow_mut().push(value); |
| 85 | + } |
| 86 | + }), |
| 87 | + ); |
| 88 | + entry.set_text(vec_string.borrow().join(", ").as_str()); |
| 89 | +} |
| 90 | + |
| 91 | +fn main() { |
| 92 | + let application = gtk::Application::new( |
| 93 | + Some("com.github.gtk-rs.examples.combobox_liststore"), |
| 94 | + Default::default(), |
| 95 | + ); |
| 96 | + |
| 97 | + application.connect_activate(build_ui); |
| 98 | + |
| 99 | + application.run(); |
| 100 | +} |
0 commit comments