Debian Codegraph @

examples/book/combobox/complex/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2002 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "examplewindow.h"
18 #include <iostream>
19
20 ExampleWindow::ExampleWindow()
21 {
22 set_title("ComboBox example");
23
24 //Create the Tree model:
25 //m_refTreeModel = Gtk::TreeStore::create(m_Columns);
26 m_refTreeModel = Gtk::ListStore::create(m_Columns);
27 m_Combo.set_model(m_refTreeModel);
28
29 //Fill the ComboBox's Tree Model:
30 auto iter = m_refTreeModel->append();
31 auto row = *iter;
32 row[m_Columns.m_col_id] = 1;
33 row[m_Columns.m_col_name] = "Billy Bob";
34 row[m_Columns.m_col_extra] = "something";
35 m_Combo.set_active(iter);
36 /*
37 auto childrow = *(m_refTreeModel->append(row.children()));
38 childrow[m_Columns.m_col_id] = 11;
39 childrow[m_Columns.m_col_name] = "Billy Bob Junior";
40
41 childrow = *(m_refTreeModel->append(row.children()));
42 childrow[m_Columns.m_col_id] = 12;
43 childrow[m_Columns.m_col_name] = "Sue Bob";
44 */
45
46 row = *(m_refTreeModel->append());
47 row[m_Columns.m_col_id] = 2;
48 row[m_Columns.m_col_name] = "Joey Jojo";
49 row[m_Columns.m_col_extra] = "yadda";
50
51
52 row = *(m_refTreeModel->append());
53 row[m_Columns.m_col_id] = 3;
54 row[m_Columns.m_col_name] = "Rob McRoberts";
55 row[m_Columns.m_col_extra] = "";
56
57 /*
58 childrow = *(m_refTreeModel->append(row.children()));
59 childrow[m_Columns.m_col_id] = 31;
60 childrow[m_Columns.m_col_name] = "Xavier McRoberts";
61 */
62
63 //Add the model columns to the Combo (which is a kind of view),
64 //rendering them in the default way:
65 m_Combo.pack_start(m_Columns.m_col_id);
66 m_Combo.pack_start(m_Columns.m_col_name);
67
68 //An example of adding a cell renderer manually,
69 //instead of using pack_start(model_column, Gtk::PackOptions::EXPAND_WIDGET)
70 //so we have more control:
71 m_Combo.set_cell_data_func(m_cell,
72 sigc::mem_fun(*this, &ExampleWindow::on_cell_data_extra));
73 m_Combo.pack_start(m_cell);
74
75 //Add the ComboBox to the window.
76 set_child(m_Combo);
77
78 //Connect signal handler:
79 m_Combo.signal_changed().connect( sigc::mem_fun(*this, &ExampleWindow::on_combo_changed) );
80 }
81
82 ExampleWindow::~ExampleWindow()
83 {
84 }
85
86 void ExampleWindow::on_cell_data_extra(const Gtk::TreeModel::const_iterator& iter)
87 {
88 auto row = *iter;
89 const Glib::ustring extra = row[m_Columns.m_col_extra];
90
91 //Some arbitrary logic just to show that this is where you can do such things:
92
93 //Transform the value, deciding how to represent it as text:
94 if(extra.empty())
95 m_cell.property_text() = "(none)";
96 else
97 m_cell.property_text() = "-" + extra + "-";
98
99 //Change other cell renderer properties too:
100 m_cell.property_foreground() = (extra == "yadda" ? "red" : "green");
101 }
102
103 void ExampleWindow::on_combo_changed()
104 {
105 const auto iter = m_Combo.get_active();
106 if(iter)
107 {
108 const auto row = *iter;
109 if(row)
110 {
111 //Get the data for the selected row, using our knowledge of the tree
112 //model:
113 int id = row[m_Columns.m_col_id];
114 Glib::ustring name = row[m_Columns.m_col_name];
115
116 std::cout << " ID=" << id << ", name=" << name << std::endl;
117 }
118 }
119 else
120 std::cout << "invalid iter" << std::endl;
121 }
122