| 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 <gtkmm/eventcontrollerfocus.h> |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
ExampleWindow::ExampleWindow() |
| 22 |
: m_Combo(true /* has_entry */) |
| 23 |
{ |
| 24 |
set_title("ComboBox example"); |
| 25 |
|
| 26 |
//Create the Tree model: |
| 27 |
//m_refTreeModel = Gtk::TreeStore::create(m_Columns); |
| 28 |
m_refTreeModel = Gtk::ListStore::create(m_Columns); |
| 29 |
m_Combo.set_model(m_refTreeModel); |
| 30 |
|
| 31 |
//Fill the ComboBox's Tree Model: |
| 32 |
auto row = *(m_refTreeModel->append()); |
| 33 |
row[m_Columns.m_col_id] = "1"; |
| 34 |
row[m_Columns.m_col_name] = "Billy Bob"; |
| 35 |
/* |
| 36 |
auto childrow = *(m_refTreeModel->append(row.children())); |
| 37 |
childrow[m_Columns.m_col_id] = 11; |
| 38 |
childrow[m_Columns.m_col_name] = "Billy Bob Junior"; |
| 39 |
|
| 40 |
childrow = *(m_refTreeModel->append(row.children())); |
| 41 |
childrow[m_Columns.m_col_id] = 12; |
| 42 |
childrow[m_Columns.m_col_name] = "Sue Bob"; |
| 43 |
*/ |
| 44 |
|
| 45 |
row = *(m_refTreeModel->append()); |
| 46 |
row[m_Columns.m_col_id] = "2"; |
| 47 |
row[m_Columns.m_col_name] = "Joey Jojo"; |
| 48 |
|
| 49 |
row = *(m_refTreeModel->append()); |
| 50 |
row[m_Columns.m_col_id] = "3"; |
| 51 |
row[m_Columns.m_col_name] = "Rob McRoberts"; |
| 52 |
|
| 53 |
/* |
| 54 |
childrow = *(m_refTreeModel->append(row.children())); |
| 55 |
childrow[m_Columns.m_col_id] = 31; |
| 56 |
childrow[m_Columns.m_col_name] = "Xavier McRoberts"; |
| 57 |
*/ |
| 58 |
|
| 59 |
//Add the model columns to the Combo (which is a kind of view), |
| 60 |
//rendering them in the default way: |
| 61 |
//This is automatically rendered when we use set_entry_text_column(). |
| 62 |
//m_Combo.pack_start(m_Columns.m_col_id, Gtk::PackOptions::EXPAND_WIDGET); |
| 63 |
m_Combo.pack_start(m_Columns.m_col_name); |
| 64 |
|
| 65 |
m_Combo.set_entry_text_column(m_Columns.m_col_id); |
| 66 |
m_Combo.set_active(1); |
| 67 |
|
| 68 |
//Add the ComboBox to the window. |
| 69 |
set_child(m_Combo); |
| 70 |
|
| 71 |
//Connect signal handlers: |
| 72 |
auto entry = m_Combo.get_entry(); |
| 73 |
if (entry) |
| 74 |
{ |
| 75 |
// Alternatively you can connect to m_Combo.signal_changed(). |
| 76 |
entry->signal_changed().connect(sigc::mem_fun(*this, |
| 77 |
&ExampleWindow::on_entry_changed)); |
| 78 |
#if HAS_SIGNAL_ACTIVATE |
| 79 |
entry->signal_activate().connect(sigc::mem_fun(*this, |
| 80 |
&ExampleWindow::on_entry_activate)); |
| 81 |
#endif |
| 82 |
// The Entry shall receive focus-leave events. |
| 83 |
auto controller = Gtk::EventControllerFocus::create(); |
| 84 |
m_ConnectionFocusLeave = controller->signal_leave().connect( |
| 85 |
sigc::mem_fun(*this, &ExampleWindow::on_entry_focus_leave)); |
| 86 |
entry->add_controller(controller); |
| 87 |
} |
| 88 |
else |
| 89 |
std::cout << "No Entry ???" << std::endl; |
| 90 |
} |
| 91 |
|
| 92 |
ExampleWindow::~ExampleWindow() |
| 93 |
{ |
| 94 |
// The focus-leave signal may be emitted while m_Combo is being destructed. |
| 95 |
// The signal handler can generate critical messages, if it's called when |
| 96 |
// m_Combo has been partly destructed. |
| 97 |
m_ConnectionFocusLeave.disconnect(); |
| 98 |
} |
| 99 |
|
| 100 |
void ExampleWindow::on_entry_changed() |
| 101 |
{ |
| 102 |
auto entry = m_Combo.get_entry(); |
| 103 |
if (entry) |
| 104 |
{ |
| 105 |
std::cout << "on_entry_changed(): Row=" << m_Combo.get_active_row_number() |
| 106 |
<< ", ID=" << entry->get_text() << std::endl; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#if HAS_SIGNAL_ACTIVATE |
| 111 |
void ExampleWindow::on_entry_activate() |
| 112 |
{ |
| 113 |
auto entry = m_Combo.get_entry(); |
| 114 |
if (entry) |
| 115 |
{ |
| 116 |
std::cout << "on_entry_activate(): Row=" << m_Combo.get_active_row_number() |
| 117 |
<< ", ID=" << entry->get_text() << std::endl; |
| 118 |
} |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
void ExampleWindow::on_entry_focus_leave() |
| 123 |
{ |
| 124 |
auto entry = m_Combo.get_entry(); |
| 125 |
if (entry) |
| 126 |
{ |
| 127 |
std::cout << "on_entry_focus_leave(): Row=" << m_Combo.get_active_row_number() |
| 128 |
<< ", ID=" << entry->get_text() << std::endl; |
| 129 |
} |
| 130 |
} |