Debian Codegraph @

demos/gtk-demo/example_treeview_editable_cells.cc

raw
1 /* Tree View/Editable Cells
2 *
3 * This demo demonstrates the use of editable cells in a Gtk::TreeView. If
4 * you're new to the Gtk::TreeView widgets and associates, look into
5 * the Gtk::ListStore example first.
6 *
7 */
8
9 #include <cstdlib>
10 #include <iostream>
11 #include <gtkmm.h>
12
13 class CellItem_Product
14 {
15 public:
16 CellItem_Product();
17 CellItem_Product(const CellItem_Product& src);
18 ~CellItem_Product();
19 CellItem_Product& operator=(const CellItem_Product& src);
20
21 int m_number;
22 Glib::ustring m_product;
23 };
24
25 class Example_TreeView_EditableCells : public Gtk::Window
26 {
27 public:
28 Example_TreeView_EditableCells();
29 ~Example_TreeView_EditableCells() override;
30
31 protected:
32 //signal handlers:
33 virtual void on_button_add_clicked();
34 virtual void on_button_remove_clicked();
35
36 virtual void create_model();
37 virtual void add_columns();
38 virtual void add_items();
39 virtual void liststore_add_item(const CellItem_Product& foo);
40
41 //We only have these signal handlers here, because the append_column_editable() convenience methods do not work with
42 //the IRIX MipsPro compiler.
43 virtual void on_column_number_edited(const Glib::ustring& path_string, const Glib::ustring& new_text);
44 virtual void on_column_product_edited(const Glib::ustring& path_string, const Glib::ustring& new_text);
45
46 //Member widgets:
47 Gtk::Box m_VBox;
48 Gtk::ScrolledWindow m_ScrolledWindow;
49 Gtk::Label m_Label;
50 Gtk::TreeView m_TreeView;
51 Glib::RefPtr<Gtk::ListStore> m_refListStore;
52 Gtk::Box m_HBox;
53 Gtk::Button m_Button_Add, m_Button_Remove;
54
55 typedef std::vector<CellItem_Product> type_vecItems;
56 type_vecItems m_vecItems;
57
58 struct ModelColumns : public Gtk::TreeModelColumnRecord
59 {
60 Gtk::TreeModelColumn<int> number;
61 Gtk::TreeModelColumn<Glib::ustring> product;
62
63 ModelColumns() { add(number); add(product); }
64 };
65
66 const ModelColumns m_columns;
67 };
68
69
70 CellItem_Product::CellItem_Product()
71 {
72 m_number = 0;
73 }
74
75 CellItem_Product::CellItem_Product(const CellItem_Product& src)
76 {
77 operator=(src);
78 }
79
80 CellItem_Product::~CellItem_Product()
81 {
82 }
83
84 CellItem_Product& CellItem_Product::operator=(const CellItem_Product& src)
85 {
86 m_number = src.m_number;
87 m_product = src.m_product;
88
89 return *this;
90 }
91
92
93 //Called by DemoWindow;
94 Gtk::Window* do_treeview_editable_cells()
95 {
96 return new Example_TreeView_EditableCells();
97 }
98
99
100 Example_TreeView_EditableCells::Example_TreeView_EditableCells()
101 : m_VBox(Gtk::ORIENTATION_VERTICAL, 5),
102 m_Label("Shopping list (you can edit the cells!)"),
103 m_HBox(Gtk::ORIENTATION_HORIZONTAL, 4),
104 m_Button_Add("Add item"),
105 m_Button_Remove("Remove item")
106 {
107 set_title("Shopping List");
108 set_border_width(5);
109 set_default_size(320, 200);
110
111 add(m_VBox);
112 m_VBox.pack_start(m_Label, Gtk::PACK_SHRINK);
113
114 m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_ETCHED_IN);
115 m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
116 m_VBox.pack_start(m_ScrolledWindow);
117
118 /* create model */
119 create_model();
120
121 /* create tree view */
122 m_TreeView.set_model(m_refListStore);
123 Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = m_TreeView.get_selection();
124 refTreeSelection->set_mode(Gtk::SELECTION_SINGLE);
125
126 add_columns();
127 m_ScrolledWindow.add(m_TreeView);
128
129 /* some buttons */
130 m_VBox.pack_start(m_HBox, Gtk::PACK_SHRINK);
131
132 m_HBox.pack_start(m_Button_Add);
133 m_Button_Add.signal_clicked().connect(
134 sigc::mem_fun(*this, &Example_TreeView_EditableCells::on_button_add_clicked));
135
136 m_HBox.pack_start(m_Button_Remove);
137 m_Button_Remove.signal_clicked().connect(
138 sigc::mem_fun(*this, &Example_TreeView_EditableCells::on_button_remove_clicked));
139
140 show_all();
141 }
142
143 Example_TreeView_EditableCells::~Example_TreeView_EditableCells()
144 {
145 }
146
147 void Example_TreeView_EditableCells::add_items()
148 {
149 CellItem_Product foo;
150
151 foo.m_number = 3;
152 foo.m_product = "bottles of coke";
153 m_vecItems.push_back(foo);
154
155 foo.m_number = 5;
156 foo.m_product = "packages of noodles";
157 m_vecItems.push_back(foo);
158
159 foo.m_number = 2;
160 foo.m_product = "packages of chocolate chip cookies";
161 m_vecItems.push_back(foo);
162
163 foo.m_number = 1;
164 foo.m_product = "can vanilla ice cream";
165 m_vecItems.push_back(foo);
166
167 foo.m_number = 6;
168 foo.m_product = "eggs";
169 m_vecItems.push_back(foo);
170 }
171
172 void Example_TreeView_EditableCells::create_model()
173 {
174 m_refListStore = Gtk::ListStore::create(m_columns);
175
176 /* add items */
177 add_items();
178
179 std::for_each(
180 m_vecItems.begin(), m_vecItems.end(),
181 sigc::mem_fun(*this, &Example_TreeView_EditableCells::liststore_add_item));
182 }
183
184 void Example_TreeView_EditableCells::liststore_add_item(const CellItem_Product& foo)
185 {
186 Gtk::TreeRow row = *(m_refListStore->append());
187
188 row[m_columns.number] = foo.m_number;
189 row[m_columns.product] = foo.m_product;
190 }
191
192 //We only have these signal handlers here, because the append_column_editable() convenience methods do not work with
193 //the IRIX MipsPro compiler.
194 void Example_TreeView_EditableCells::on_column_number_edited(const Glib::ustring& path_string, const Glib::ustring& new_text)
195 {
196 Gtk::TreePath path(path_string);
197
198 //Get the row from the path:
199 Glib::RefPtr<Gtk::TreeModel> refModel = m_TreeView.get_model();
200 if(refModel)
201 {
202 Gtk::TreeModel::iterator iter = refModel->get_iter(path);
203 if(iter)
204 {
205 //Convert the text to a number, using the same logic used by GtkCellRendererText when it stores numbers.
206 int new_value = 0;
207 try
208 {
209 new_value = std::stoi(new_text.raw());
210 }
211 catch (const std::exception& err)
212 {
213 std::cout << "Could not convert \"" << new_text << "\" to an integer. ("
214 << err.what() << ")" << std::endl;
215 }
216
217 //Store the user's new text in the model:
218 Gtk::TreeRow row = *iter;
219 row[m_columns.number] = new_value;
220 }
221 }
222 }
223
224 void Example_TreeView_EditableCells::on_column_product_edited(const Glib::ustring& path_string, const Glib::ustring& new_text)
225 {
226 Gtk::TreePath path(path_string);
227
228 //Get the row from the path:
229 Glib::RefPtr<Gtk::TreeModel> refModel = m_TreeView.get_model();
230 if(refModel)
231 {
232 Gtk::TreeModel::iterator iter = refModel->get_iter(path);
233 if(iter)
234 {
235 //Store the user's new text in the model:
236 Gtk::TreeRow row = *iter;
237 row[m_columns.product] = new_text;
238 }
239 }
240 }
241
242 void Example_TreeView_EditableCells::add_columns()
243 {
244 //This is the wasy way:
245 // number column:
246 //m_TreeView.append_column_editable("Number", m_columns.number);
247 //
248 // product column:
249 //m_TreeView.append_column_editable("Product", m_columns.product);
250
251 //And this is the way that works with the IRIX MipsPro compiler too:
252 {
253 Gtk::TreeView::Column* pViewColumn = Gtk::make_managed<Gtk::TreeView::Column>("Number", m_columns.number);
254
255 //connect signal handlers for auto-storing of edited cell data
256 Gtk::CellRenderer* pCellRenderer = pViewColumn->get_first_cell();
257 Gtk::CellRendererText* pCellRenderText = dynamic_cast<Gtk::CellRendererText*>(pCellRenderer);
258 if(pCellRenderText)
259 {
260 //Set the appropriate property,
261 pCellRenderText->property_editable() = true;
262
263 //Connect to the appropriate signal, sending the model_column too,
264 pCellRenderText->signal_edited().connect( sigc::mem_fun(*this, &Example_TreeView_EditableCells::on_column_number_edited) );
265 }
266
267 m_TreeView.append_column(*pViewColumn);
268 }
269
270 {
271 Gtk::TreeView::Column* pViewColumn = Gtk::make_managed<Gtk::TreeView::Column>("Product", m_columns.product);
272
273 //connect signal handlers for auto-storing of edited cell data
274 Gtk::CellRenderer* pCellRenderer = pViewColumn->get_first_cell();
275 Gtk::CellRendererText* pCellRenderText = dynamic_cast<Gtk::CellRendererText*>(pCellRenderer);
276 if(pCellRenderText)
277 {
278 //Set the appropriate property,
279 pCellRenderText->property_editable() = true;
280
281 //Connect to the appropriate signal, sending the model_column too,
282 pCellRenderText->signal_edited().connect( sigc::mem_fun(*this, &Example_TreeView_EditableCells::on_column_product_edited) );
283 }
284
285 m_TreeView.append_column(*pViewColumn);
286 }
287
288
289 }
290
291
292 void Example_TreeView_EditableCells::on_button_add_clicked()
293 {
294 CellItem_Product foo;
295 foo.m_number = 0;
296 foo.m_product = "Description here";
297 m_vecItems.push_back(foo);
298
299 liststore_add_item(foo);
300 }
301
302 void Example_TreeView_EditableCells::on_button_remove_clicked()
303 {
304 Glib::RefPtr<Gtk::TreeSelection> refSelection = m_TreeView.get_selection();
305
306 if(const Gtk::TreeModel::iterator iter = refSelection->get_selected())
307 {
308 const Gtk::TreeModel::Path path(iter);
309 const unsigned int index = path.front();
310
311 // Remove item from ListStore:
312 m_refListStore->erase(iter);
313
314 // Remove item from vecItems.
315 if(index < m_vecItems.size())
316 m_vecItems.erase(m_vecItems.begin() + index);
317 }
318 }