| 1 |
/* gtkmm example Copyright (C) 2013 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 <array> |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
namespace |
| 22 |
{ |
| 23 |
struct SelectionModeStruct |
| 24 |
{ |
| 25 |
Gtk::SelectionMode mode; |
| 26 |
Glib::ustring text; |
| 27 |
}; |
| 28 |
|
| 29 |
const std::array<SelectionModeStruct, 4> selectionModes = |
| 30 |
{ |
| 31 |
SelectionModeStruct{Gtk::SelectionMode::NONE, "SelectionMode::NONE"}, |
| 32 |
SelectionModeStruct{Gtk::SelectionMode::SINGLE, "SelectionMode::SINGLE"}, |
| 33 |
SelectionModeStruct{Gtk::SelectionMode::BROWSE, "SelectionMode::BROWSE"}, |
| 34 |
SelectionModeStruct{Gtk::SelectionMode::MULTIPLE, "SelectionMode::MULTIPLE"} |
| 35 |
}; |
| 36 |
|
| 37 |
} // anonymous namespace |
| 38 |
|
| 39 |
ExampleWindow::ExampleWindow() : |
| 40 |
m_HBox(Gtk::Orientation::HORIZONTAL), |
| 41 |
m_VBox1(Gtk::Orientation::VERTICAL), |
| 42 |
m_VBox2(Gtk::Orientation::VERTICAL), |
| 43 |
m_ListBox(), |
| 44 |
m_DropDown(), |
| 45 |
m_CheckButton_SingleClick("single click mode", /* mnemonic= */ false), |
| 46 |
m_ScrolledWindow(), |
| 47 |
m_Row3("blah3", 3), |
| 48 |
m_VBox_Row(Gtk::Orientation::VERTICAL), |
| 49 |
m_HBox_Row(Gtk::Orientation::HORIZONTAL), |
| 50 |
m_Label_Row("a check box"), |
| 51 |
m_CheckButton_Row1(), |
| 52 |
m_CheckButton_Row2(), |
| 53 |
m_Button_Row1("hi!"), |
| 54 |
m_Button_Row2("focusable row"), |
| 55 |
m_Button_Sort("sort"), |
| 56 |
m_Button_ReverseSort("reverse sort"), |
| 57 |
m_Button_Change("change"), |
| 58 |
m_Button_Filter("filter"), |
| 59 |
m_Button_Unfilter("unfilter"), |
| 60 |
m_Button_Add("add"), |
| 61 |
m_Button_Separate("separate"), |
| 62 |
m_Button_Unseparate("unseparate"), |
| 63 |
m_Button_Visibility("visibility") |
| 64 |
{ |
| 65 |
set_title("ListBox example"); |
| 66 |
set_default_size(300, 300); |
| 67 |
|
| 68 |
m_HBox.set_margin(5); |
| 69 |
set_child(m_HBox); |
| 70 |
m_HBox.append(m_VBox1); |
| 71 |
|
| 72 |
// DropDown for selection mode. |
| 73 |
auto string_list = Gtk::StringList::create({}); |
| 74 |
for (const auto& selectionMode : selectionModes) |
| 75 |
string_list->append(selectionMode.text); |
| 76 |
|
| 77 |
m_DropDown.set_model(string_list); |
| 78 |
m_DropDown.property_selected().signal_changed().connect( |
| 79 |
sigc::mem_fun(*this, &ExampleWindow::on_selection_mode_changed)); |
| 80 |
m_VBox1.append(m_DropDown); |
| 81 |
|
| 82 |
const auto mode = m_ListBox.get_selection_mode(); |
| 83 |
int index = 0; |
| 84 |
for (std::size_t i = 0; i < selectionModes.size(); ++i) |
| 85 |
{ |
| 86 |
if (selectionModes[i].mode == mode) |
| 87 |
{ |
| 88 |
index = i; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
m_DropDown.set_selected(index); |
| 93 |
|
| 94 |
// Check button for single click. |
| 95 |
m_CheckButton_SingleClick.set_active(m_ListBox.get_activate_on_single_click()); |
| 96 |
m_CheckButton_SingleClick.signal_toggled().connect(sigc::mem_fun(*this, &ExampleWindow::on_single_click_toggled)); |
| 97 |
m_VBox1.append(m_CheckButton_SingleClick); |
| 98 |
|
| 99 |
// Put the ListBox in a ScrolledWindow. |
| 100 |
m_ScrolledWindow.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC); |
| 101 |
m_ScrolledWindow.set_child(m_ListBox); |
| 102 |
m_HBox.append(m_ScrolledWindow); |
| 103 |
|
| 104 |
m_ListBox.signal_row_selected().connect(sigc::mem_fun(*this, &ExampleWindow::on_row_selected)); |
| 105 |
m_ListBox.signal_row_activated().connect(sigc::mem_fun(*this, &ExampleWindow::on_row_activated)); |
| 106 |
|
| 107 |
// Add some rows to the ListBox. |
| 108 |
auto row = Gtk::make_managed<ExampleRow>("blah4", 4); |
| 109 |
m_ListBox.append(*row); |
| 110 |
m_ListBox.append(m_Row3); // blah3 |
| 111 |
row = Gtk::make_managed<ExampleRow>("blah1", 1); |
| 112 |
m_ListBox.append(*row); |
| 113 |
row = Gtk::make_managed<ExampleRow>("blah2", 2); |
| 114 |
m_ListBox.append(*row); |
| 115 |
|
| 116 |
row = Gtk::make_managed<ExampleRow>("", 0); |
| 117 |
m_HBox_Row.append(m_Label_Row); |
| 118 |
m_HBox_Row.append(m_CheckButton_Row1); |
| 119 |
m_HBox_Row.append(m_Button_Row1); |
| 120 |
m_VBox_Row.append(m_HBox_Row); |
| 121 |
m_VBox_Row.append(m_CheckButton_Row2); |
| 122 |
row->set_child(m_VBox_Row); |
| 123 |
m_ListBox.append(*row); |
| 124 |
|
| 125 |
row = Gtk::make_managed<ExampleRow>("", 0); |
| 126 |
m_Button_Row2.set_hexpand(false); |
| 127 |
m_Button_Row2.set_halign(Gtk::Align::START); |
| 128 |
row->set_child(m_Button_Row2); |
| 129 |
m_ListBox.append(*row); |
| 130 |
|
| 131 |
// Put buttons in a vertical box, and connect signal handlers. |
| 132 |
// There are several ways of connecting to a signal or any other function that |
| 133 |
// takes a sigc::slot. |
| 134 |
m_HBox.append(m_VBox2); |
| 135 |
m_VBox2.append(m_Button_Sort); |
| 136 |
// With sigc::mem_fun() or (for non-member functions and static member functions) |
| 137 |
// sigc::ptr_fun(). The only way before C++11 introduced lambda expressions. |
| 138 |
m_Button_Sort.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_sort_clicked)); |
| 139 |
m_VBox2.append(m_Button_ReverseSort); |
| 140 |
// With a lambda expression. Does not disconnect automatically when ExampleWindow |
| 141 |
// is deleted, like sigc::mem_fun() does. |
| 142 |
m_Button_ReverseSort.signal_clicked().connect([this] { on_reverse_sort_clicked(); }); |
| 143 |
m_VBox2.append(m_Button_Change); |
| 144 |
// With a lambda expression and sigc::track_obj() or sigc::track_object(). |
| 145 |
// Disconnects automatically like sigc::mem_fun(). |
| 146 |
#if SIGCXX_MINOR_VERSION >= 4 |
| 147 |
m_Button_Change.signal_clicked().connect( |
| 148 |
sigc::track_object([this] { on_change_clicked(); }, *this)); |
| 149 |
#else |
| 150 |
m_Button_Change.signal_clicked().connect( |
| 151 |
sigc::track_obj([this] { on_change_clicked(); }, *this)); |
| 152 |
#endif |
| 153 |
m_VBox2.append(m_Button_Filter); |
| 154 |
m_Button_Filter.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_filter_clicked)); |
| 155 |
m_VBox2.append(m_Button_Unfilter); |
| 156 |
// If the signal handler is very short, it might be easier to skip the on_xxx() |
| 157 |
// method and put its contents directly in the lambda expression. |
| 158 |
m_Button_Unfilter.signal_clicked().connect( |
| 159 |
[this] { m_ListBox.unset_filter_func(); /* on_unfilter_clicked() */}); |
| 160 |
m_VBox2.append(m_Button_Add); |
| 161 |
m_Button_Add.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_add_clicked)); |
| 162 |
m_VBox2.append(m_Button_Separate); |
| 163 |
m_Button_Separate.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_separate_clicked)); |
| 164 |
m_VBox2.append(m_Button_Unseparate); |
| 165 |
m_Button_Unseparate.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_unseparate_clicked)); |
| 166 |
m_VBox2.append(m_Button_Visibility); |
| 167 |
m_Button_Visibility.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_visibility_clicked)); |
| 168 |
} |
| 169 |
|
| 170 |
void ExampleWindow::on_selection_mode_changed() |
| 171 |
{ |
| 172 |
auto index = m_DropDown.get_selected(); |
| 173 |
if (index == GTK_INVALID_LIST_POSITION) |
| 174 |
index = 0; |
| 175 |
m_ListBox.set_selection_mode(selectionModes[index].mode); |
| 176 |
} |
| 177 |
|
| 178 |
void ExampleWindow::on_single_click_toggled() |
| 179 |
{ |
| 180 |
std::cout << "single: " << m_CheckButton_SingleClick.get_active() << std::endl; |
| 181 |
m_ListBox.set_activate_on_single_click(m_CheckButton_SingleClick.get_active()); |
| 182 |
} |
| 183 |
|
| 184 |
void ExampleWindow::on_row_selected(Gtk::ListBoxRow* row) |
| 185 |
{ |
| 186 |
std::cout << "selected row: " << row << std::endl; |
| 187 |
} |
| 188 |
|
| 189 |
void ExampleWindow::on_row_activated(Gtk::ListBoxRow* row) |
| 190 |
{ |
| 191 |
std::cout << "activated row: " << row << std::endl; |
| 192 |
} |
| 193 |
|
| 194 |
void ExampleWindow::on_sort_clicked() |
| 195 |
{ |
| 196 |
m_ListBox.set_sort_func( |
| 197 |
[] (Gtk::ListBoxRow* row1, Gtk::ListBoxRow* row2) |
| 198 |
{ return sort_func(row1, row2); } |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
void ExampleWindow::on_reverse_sort_clicked() |
| 203 |
{ |
| 204 |
m_ListBox.set_sort_func( |
| 205 |
[] (Gtk::ListBoxRow* row1, Gtk::ListBoxRow* row2) |
| 206 |
{ return sort_func(row2, row1); } |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
int ExampleWindow::sort_func(Gtk::ListBoxRow* row1, Gtk::ListBoxRow* row2) |
| 211 |
{ |
| 212 |
const auto xrow1 = dynamic_cast<ExampleRow*>(row1); |
| 213 |
const auto xrow2 = dynamic_cast<ExampleRow*>(row2); |
| 214 |
if (xrow1 && xrow2) |
| 215 |
return xrow1->get_sort_id() - xrow2->get_sort_id(); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
void ExampleWindow::on_change_clicked() |
| 221 |
{ |
| 222 |
if (m_Row3.get_text() == "blah3") |
| 223 |
{ |
| 224 |
m_Row3.set_text("blah5"); |
| 225 |
m_Row3.set_sort_id(5); |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
m_Row3.set_text("blah3"); |
| 230 |
m_Row3.set_sort_id(3); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void ExampleWindow::on_filter_clicked() |
| 235 |
{ |
| 236 |
m_ListBox.set_filter_func( |
| 237 |
[] (Gtk::ListBoxRow* row) |
| 238 |
{ |
| 239 |
auto xrow = dynamic_cast<ExampleRow*>(row); |
| 240 |
return xrow && xrow->get_text() != "blah3"; |
| 241 |
}); |
| 242 |
} |
| 243 |
|
| 244 |
//void ExampleWindow::on_unfilter_clicked() |
| 245 |
//{ |
| 246 |
// m_ListBox.unset_filter_func(); |
| 247 |
//} |
| 248 |
|
| 249 |
void ExampleWindow::on_add_clicked() |
| 250 |
{ |
| 251 |
static int new_button_nr = 1; |
| 252 |
const Glib::ustring text = "blah2 new " + Glib::ustring::format(new_button_nr); |
| 253 |
auto new_row = Gtk::make_managed<ExampleRow>(text, new_button_nr); |
| 254 |
m_ListBox.append(*new_row); |
| 255 |
++new_button_nr; |
| 256 |
} |
| 257 |
|
| 258 |
void ExampleWindow::on_separate_clicked() |
| 259 |
{ |
| 260 |
m_ListBox.set_header_func( |
| 261 |
[] (Gtk::ListBoxRow* row, Gtk::ListBoxRow* before) |
| 262 |
{ update_header_func(row, before); } |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
void ExampleWindow::on_unseparate_clicked() |
| 267 |
{ |
| 268 |
m_ListBox.unset_header_func(); |
| 269 |
} |
| 270 |
|
| 271 |
void ExampleWindow::update_header_func(Gtk::ListBoxRow* row, Gtk::ListBoxRow* before) |
| 272 |
{ |
| 273 |
auto xrow = dynamic_cast<ExampleRow*>(row); |
| 274 |
if (!before || (xrow && xrow->get_text() == "blah3")) |
| 275 |
{ |
| 276 |
// Create header if needed. |
| 277 |
if (!row->get_header()) |
| 278 |
{ |
| 279 |
auto hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL); |
| 280 |
auto label = Gtk::make_managed<Gtk::Label>("Header"); |
| 281 |
hbox->append(*label); |
| 282 |
auto button = Gtk::make_managed<Gtk::Button>("button"); |
| 283 |
hbox->append(*button); |
| 284 |
row->set_header(*hbox); |
| 285 |
} |
| 286 |
|
| 287 |
auto header = row->get_header(); |
| 288 |
if (header) |
| 289 |
{ |
| 290 |
auto first_child = header->get_first_child(); |
| 291 |
if (first_child) |
| 292 |
{ |
| 293 |
auto label = dynamic_cast<Gtk::Label*>(first_child); |
| 294 |
if (label && xrow) |
| 295 |
label->set_text("Header " + Glib::ustring::format(xrow->get_sort_id())); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
else |
| 300 |
row->unset_header(); |
| 301 |
} |
| 302 |
|
| 303 |
void ExampleWindow::on_visibility_clicked() |
| 304 |
{ |
| 305 |
m_Row3.set_visible(!m_Row3.get_visible()); |
| 306 |
} |