Debian Codegraph @

gtk/src/listbox.ccg

raw
1 /* Copyright (C) 2013 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library 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 GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <gtk/gtk.h>
18 #include <gtkmm/adjustment.h>
19
20 namespace
21 {
22
23 gboolean SignalProxy_Filter_gtk_callback(GtkListBoxRow* row, void* data)
24 {
25 auto the_slot = static_cast<Gtk::ListBox::SlotFilter*>(data);
26
27 try
28 {
29 return (*the_slot)(Glib::wrap(row));
30 }
31 catch(...)
32 {
33 Glib::exception_handlers_invoke();
34 return FALSE;
35 }
36 }
37
38 void SignalProxy_Filter_gtk_callback_destroy(void* data)
39 {
40 delete static_cast<Gtk::ListBox::SlotFilter*>(data);
41 }
42
43 gint SignalProxy_Sort_gtk_callback(GtkListBoxRow* row1, GtkListBoxRow* row2, void* data)
44 {
45 auto the_slot = static_cast<Gtk::ListBox::SlotSort*>(data);
46
47 try
48 {
49 return (*the_slot)(Glib::wrap(row1), Glib::wrap(row2));
50 }
51 catch(...)
52 {
53 Glib::exception_handlers_invoke();
54 return 0;
55 }
56 }
57
58 void SignalProxy_Sort_gtk_callback_destroy(void* data)
59 {
60 delete static_cast<Gtk::ListBox::SlotSort*>(data);
61 }
62
63 void SignalProxy_UpdateHeader_gtk_callback(GtkListBoxRow* row, GtkListBoxRow* before, void* data)
64 {
65 auto the_slot = static_cast<Gtk::ListBox::SlotUpdateHeader*>(data);
66
67 try
68 {
69 (*the_slot)(Glib::wrap(row), Glib::wrap(before));
70 }
71 catch(...)
72 {
73 Glib::exception_handlers_invoke();
74 }
75 }
76
77 void SignalProxy_UpdateHeader_gtk_callback_destroy(void* data)
78 {
79 delete static_cast<Gtk::ListBox::SlotUpdateHeader*>(data);
80 }
81
82
83 static void proxy_foreach_callback(GtkListBox* /* list_box */, GtkListBoxRow* row, void* data)
84 {
85 typedef Gtk::ListBox::SlotForeach SlotType;
86 auto& slot = *static_cast<SlotType*>(data);
87
88 try
89 {
90 slot(Glib::wrap(row, true));
91 }
92 catch(...)
93 {
94 Glib::exception_handlers_invoke();
95 }
96 }
97
98 static GtkWidget* proxy_bind_model_create_widget_callback(void* item, void* data)
99 {
100 auto& slot = *static_cast<Gtk::ListBox::SlotCreateWidget<Glib::Object>*>(data);
101 auto cobject = static_cast<GObject*>(item);
102
103 try
104 {
105 // take_copy is true here, because wrap() returns a Glib::RefPtr<Glib::Object>.
106 // cobject will be unreferenced when the RefPtr is deleted.
107 Gtk::Widget* widget = slot(Glib::wrap(cobject, true));
108 if (widget)
109 return widget->gobj();
110 }
111 catch(...)
112 {
113 Glib::exception_handlers_invoke();
114 }
115 return nullptr;
116 }
117
118 } // anonymous namespace
119
120 namespace Gtk
121 {
122
123 void ListBox::unselect_row()
124 {
125 gtk_list_box_select_row(gobj(), nullptr);
126 }
127
128 void ListBox::unset_placeholder()
129 {
130 gtk_list_box_set_placeholder(gobj(), nullptr);
131 }
132
133 void ListBox::set_filter_func(const SlotFilter& slot)
134 {
135 // Create a copy of the slot object. A pointer to this will be passed
136 // through the callback's data parameter. It will be deleted
137 // when SignalProxy_Filter_gtk_callback_destroy() is called.
138 auto slot_copy = new SlotFilter(slot);
139
140 gtk_list_box_set_filter_func(gobj(),
141 &SignalProxy_Filter_gtk_callback, slot_copy,
142 &SignalProxy_Filter_gtk_callback_destroy);
143 }
144
145 void ListBox::unset_filter_func()
146 {
147 gtk_list_box_set_filter_func(gobj(), nullptr, nullptr, nullptr);
148 }
149
150 void ListBox::set_sort_func(const SlotSort& slot)
151 {
152 // Create a copy of the slot object. A pointer to this will be passed
153 // through the callback's data parameter. It will be deleted
154 // when SignalProxy_Sort_gtk_callback_destroy() is called.
155 auto slot_copy = new SlotSort(slot);
156
157 gtk_list_box_set_sort_func(gobj(),
158 &SignalProxy_Sort_gtk_callback, slot_copy,
159 &SignalProxy_Sort_gtk_callback_destroy);
160 }
161
162 void ListBox::unset_sort_func()
163 {
164 gtk_list_box_set_sort_func(gobj(), nullptr, nullptr, nullptr);
165 }
166
167 void ListBox::set_header_func(const SlotUpdateHeader& slot)
168 {
169 // Create a copy of the slot object. A pointer to this will be passed
170 // through the callback's data parameter. It will be deleted
171 // when SignalProxy_Sort_gtk_callback_destroy() is called.
172 auto slot_copy = new SlotUpdateHeader(slot);
173
174 gtk_list_box_set_header_func(gobj(),
175 &SignalProxy_UpdateHeader_gtk_callback, slot_copy,
176 &SignalProxy_UpdateHeader_gtk_callback_destroy);
177 }
178
179 void ListBox::unset_header_func()
180 {
181 gtk_list_box_set_header_func(gobj(), nullptr, nullptr, nullptr);
182 }
183
184 void ListBox::append(Widget& child)
185 {
186 insert(child, -1);
187 }
188
189 void ListBox::selected_foreach(const SlotForeach& slot)
190 {
191 SlotForeach slot_copy(slot); //TODO: Is this necessary?
192 gtk_list_box_selected_foreach(gobj(), &proxy_foreach_callback, &slot_copy);
193 }
194
195 void ListBox::bind_model(const Glib::RefPtr<Gio::ListModel>& model,
196 const SlotCreateWidget<Glib::Object>& slot_create_widget)
197 {
198 // Create a copy of the slot.
199 // It will be deleted by Glib::destroy_notify_delete<SlotCreateWidget>.
200 auto slot_copy = new SlotCreateWidget<Glib::Object>(slot_create_widget);
201
202 gtk_list_box_bind_model(gobj(), Glib::unwrap(model), &proxy_bind_model_create_widget_callback,
203 slot_copy, &Glib::destroy_notify_delete<SlotCreateWidget<Glib::Object>>);
204 }
205
206 } //namespace Gtk