Debian Codegraph @

examples/book/listbox_flowbox/listmodel/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2016 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 #include <ctime>
20 #include <cstdlib> // std::rand()
21 #include <algorithm> // std::max()
22
23 ExampleWindow::ExampleWindow()
24 :
25 m_store(Gio::ListStore<MyObject>::create()),
26 m_button_add_some("_Add some", true),
27 m_button_remove_some("_Remove some", true),
28 m_button_quit("_Quit", true)
29 {
30 set_title("ListBox and FlowBox with ListModel");
31 set_default_size(300, 300);
32
33 for (int i = 0; i < 20; ++i)
34 {
35 const auto label = Glib::ustring::format("Item ", i);
36 m_store->append(MyObject::create(i, label));
37 }
38
39 // Use current time as seed for random number generator.
40 std::srand(std::time(nullptr));
41
42 m_grid.set_margin(5);
43 set_child(m_grid);
44
45 // Select bind_model() or bind_list_store() randomly.
46 const bool use_bind_model = std::rand() & 2;
47 std::cout << "Using bind_" << (use_bind_model ? "model" : "list_store") << std::endl;
48
49 // A ListBox to the left.
50 m_scrolled_window_list_box.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
51 m_scrolled_window_list_box.set_hexpand(true);
52 m_scrolled_window_list_box.set_vexpand(true);
53 m_grid.attach(m_scrolled_window_list_box, 0, 0, 1 ,1);
54 m_scrolled_window_list_box.set_child(m_list_box);
55 if (use_bind_model)
56 m_list_box.bind_model(m_store,
57 [](const auto& item)
58 { return on_create_widget1(item); }
59 );
60 else
61 m_list_box.bind_list_store(m_store,
62 [](const auto& item)
63 { return on_create_widget2(item); }
64 );
65
66 // A FlowBox to the right.
67 m_scrolled_window_flow_box.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
68 m_scrolled_window_flow_box.set_hexpand(true);
69 m_scrolled_window_flow_box.set_vexpand(true);
70 m_grid.attach(m_scrolled_window_flow_box, 1, 0, 1 ,1);
71 m_scrolled_window_flow_box.set_child(m_flow_box);
72 if (use_bind_model)
73 m_flow_box.bind_model(m_store,
74 [](const auto& item)
75 { return on_create_widget1(item); }
76 );
77 else
78 m_flow_box.bind_list_store(m_store,
79 [](const auto& item)
80 { return on_create_widget2(item); }
81 );
82
83 // Buttons at the bottom.
84 m_button_add_some.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_add_some));
85 m_grid.attach(m_button_add_some, 0, 1, 2, 1);
86 m_button_remove_some.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_remove_some));
87 m_grid.attach(m_button_remove_some, 0, 2, 2, 1);
88 m_button_quit.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_quit));
89 m_grid.attach(m_button_quit, 0, 3, 2, 1);
90 }
91
92 // static
93 Gtk::Widget* ExampleWindow::on_create_widget1(const Glib::RefPtr<Glib::Object>& item)
94 {
95 auto obj = std::dynamic_pointer_cast<MyObject>(item);
96 if (!obj)
97 {
98 std::cout << "on_create_widget1(): item is not a MyObject" << std::endl;
99 return nullptr;
100 }
101 auto label = Gtk::make_managed<Gtk::Label>();
102 Glib::Binding::bind_property(obj->property_label(), label->property_label(),
103 Glib::Binding::Flags::SYNC_CREATE);
104 return label;
105 }
106
107 // static
108 Gtk::Widget* ExampleWindow::on_create_widget2(const Glib::RefPtr<MyObject>& item)
109 {
110 if (!item)
111 {
112 std::cout << "on_create_widget2(): item is empty" << std::endl;
113 return nullptr;
114 }
115 auto label = Gtk::make_managed<Gtk::Label>();
116 Glib::Binding::bind_property(item->property_label(), label->property_label(),
117 Glib::Binding::Flags::SYNC_CREATE);
118 return label;
119 }
120
121 void ExampleWindow::on_add_some()
122 {
123 for (int n = 0; n < 10; ++n)
124 {
125 const auto n_items = m_store->get_n_items();
126 const auto i = std::rand() % std::max(2*n_items, 4u);
127 const auto label = Glib::ustring::format("Added ", i);
128 m_store->insert_sorted(MyObject::create(i, label),
129 [](const auto& a, const auto& b)
130 { return MyObject::compare(a, b); }
131 );
132 }
133 }
134
135 void ExampleWindow::on_remove_some()
136 {
137 for (int n = 0; n < 10; ++n)
138 {
139 const auto n_items = m_store->get_n_items();
140 if (n_items == 0)
141 return;
142 const auto i = std::rand() % n_items;
143 m_store->remove(i);
144 }
145 }
146
147 void ExampleWindow::on_quit()
148 {
149 close();
150 }