| 
1 | 1 | // Variation on ListBox model example to show use of StringList and StringSorter.  | 
2 | 2 | 
 
  | 
3 |  | -// TODO: add a dialog window to allow adding rows.  | 
4 |  | -// use plain gtk::Window for this per current practice.  | 
5 |  | - | 
6 | 3 | use gtk::{  | 
7 | 4 |     glib::{self, clone},  | 
8 | 5 |     prelude::*,  | 
@@ -79,62 +76,6 @@ fn build_ui(application: >k::Application) {  | 
79 | 76 | 
 
  | 
80 | 77 |     scrolled_window.set_child(Some(&listbox));  | 
81 | 78 | 
 
  | 
82 |  | -    let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);  | 
83 |  | - | 
84 |  | -    // The add button opens a new dialog which is basically the same as the edit  | 
85 |  | -    // dialog, except that we don't have a corresponding item yet at that point  | 
86 |  | -    // and only create it once the Ok button in the dialog is clicked, and only  | 
87 |  | -    // then add it to the model. Once added to the model, it will immediately  | 
88 |  | -    // appear in the listbox UI  | 
89 |  | -    let add_button = gtk::Button::with_label("Add");  | 
90 |  | -    add_button.set_sensitive(false);  | 
91 |  | - | 
92 |  | -    hbox.append(&add_button);  | 
93 |  | - | 
94 |  | -    // Via the delete button we delete the item from the model that  | 
95 |  | -    // is at the index of the selected row. Also deleting from the  | 
96 |  | -    // model is immediately reflected in the listbox.  | 
97 |  | -    let delete_button = gtk::Button::with_label("Delete");  | 
98 |  | -    delete_button.connect_clicked(clone!(  | 
99 |  | -        #[weak]  | 
100 |  | -        model,  | 
101 |  | -        #[weak]  | 
102 |  | -        listbox,  | 
103 |  | -        move |_| {  | 
104 |  | -            let selected = listbox.selected_row();  | 
105 |  | -            //let sorted_model = listbox.model();  | 
106 |  | - | 
107 |  | -            if let Some(selected) = selected {  | 
108 |  | -                // Find the selected text.  | 
109 |  | -                let selected = selected.child().expect("Listrow child should be a widget");  | 
110 |  | -                let selected = selected  | 
111 |  | -                    .downcast_ref::<gtk::Inscription>()  | 
112 |  | -                    .expect("The object should be of type `Inscription`.");  | 
113 |  | -                if let Some(selected) = selected.text() {  | 
114 |  | -                    let mut selected_index = None;  | 
115 |  | -                    // Find the position in the StringList model of the selected string  | 
116 |  | -                    for ind in 0..model.n_items() {  | 
117 |  | -                        if let Some(item) = model.item(ind) {  | 
118 |  | -                            let item = item  | 
119 |  | -                                .downcast_ref::<gtk::StringObject>()  | 
120 |  | -                                .expect("Object should be a stringobject");  | 
121 |  | -                            if item.string() == selected {  | 
122 |  | -                                selected_index = Some(ind);  | 
123 |  | -                                break;  | 
124 |  | -                            }  | 
125 |  | -                        }  | 
126 |  | -                    }  | 
127 |  | -                    // If the selected string is found in the stringlist model, delete it  | 
128 |  | -                    if let Some(index) = selected_index {  | 
129 |  | -                        model.remove(index);  | 
130 |  | -                    }  | 
131 |  | -                }  | 
132 |  | -            }  | 
133 |  | -        }  | 
134 |  | -    ));  | 
135 |  | -    hbox.append(&delete_button);  | 
136 |  | - | 
137 |  | -    vbox.append(&hbox);  | 
138 | 79 |     vbox.append(&scrolled_window);  | 
139 | 80 | 
 
  | 
140 | 81 |     window.set_child(Some(&vbox));  | 
 | 
0 commit comments