| 1 |
/* Copyright (C) 2014 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 <gtkmm/container.h> |
| 18 |
#include <gtkmm/orientable.h> |
| 19 |
#include <gtkmm/flowboxchild.h> |
| 20 |
#include <giomm/liststore.h> |
| 21 |
#include <glibmm/exceptionhandler.h> |
| 22 |
#include <vector> |
| 23 |
#include <utility> // std::forward |
| 24 |
|
| 25 |
_DEFS(gtkmm,gtk) |
| 26 |
_PINCLUDE(gtkmm/private/container_p.h) |
| 27 |
|
| 28 |
namespace Gtk |
| 29 |
{ |
| 30 |
|
| 31 |
/** A container that allows reflowing its children. |
| 32 |
* |
| 33 |
* A FlowBox positions child widgets in sequence according to its |
| 34 |
* orientation. |
| 35 |
* |
| 36 |
* For instance, with the horizontal orientation, the widgets will be |
| 37 |
* arranged from left to right, starting a new row under the previous |
| 38 |
* row when necessary. Reducing the width in this case will require more |
| 39 |
* rows, so a larger height will be requested. |
| 40 |
* |
| 41 |
* Likewise, with the vertical orientation, the widgets will be arranged |
| 42 |
* from top to bottom, starting a new column to the right when necessary. |
| 43 |
* Reducing the height will require more columns, so a larger width will |
| 44 |
* be requested. |
| 45 |
* |
| 46 |
* The children of a FlowBox can be dynamically sorted and filtered. |
| 47 |
* |
| 48 |
* Although a FlowBox must have only FlowBoxChild children, |
| 49 |
* you can add any kind of widget to it via Container::add(), and |
| 50 |
* a FlowBoxChild widget will automatically be inserted between |
| 51 |
* the box and the widget. |
| 52 |
* |
| 53 |
* Also see ListBox. |
| 54 |
* |
| 55 |
* @ingroup Widgets |
| 56 |
* @ingroup Containers |
| 57 |
* |
| 58 |
* @newin{3,12} |
| 59 |
*/ |
| 60 |
class GTKMM_API FlowBox |
| 61 |
: public Container, |
| 62 |
public Orientable |
| 63 |
{ |
| 64 |
_CLASS_GTKOBJECT(FlowBox, GtkFlowBox, GTK_FLOW_BOX, Gtk::Container, GtkContainer, , , GTKMM_API) |
| 65 |
_IMPLEMENTS_INTERFACE(Orientable) |
| 66 |
public: |
| 67 |
|
| 68 |
/** Creates a FlowBox. |
| 69 |
*/ |
| 70 |
_CTOR_DEFAULT |
| 71 |
|
| 72 |
/** For instance: void foreach_child(FlowBox* box, FlowBoxChild* child); |
| 73 |
* |
| 74 |
* A function used by FlowBox::selected_foreach(). |
| 75 |
* It will be called on every selected child of the @a box. |
| 76 |
* |
| 77 |
* @param box FlowBox containing the selected children |
| 78 |
* @param child Each selected FlowBoxChild |
| 79 |
* |
| 80 |
* @newin{3,12} |
| 81 |
*/ |
| 82 |
typedef sigc::slot<void, FlowBox*, FlowBoxChild*> SlotSelectedForeach; |
| 83 |
|
| 84 |
/** For instance: bool on_filter(FlowBoxChild* child); |
| 85 |
* |
| 86 |
* Will be called for each child after a call to FlowBox::set_filter_func(), |
| 87 |
* and it will continue to be called each time a child changes (via |
| 88 |
* FlowBoxChild::changed()) or when FlowBox::invalidate_filter() |
| 89 |
* is called. |
| 90 |
* |
| 91 |
* @param child A FlowBoxChild that may be filtered |
| 92 |
* @returns <tt>true</tt> if the child should be visible, <tt>false</tt> otherwise |
| 93 |
* |
| 94 |
* @newin{3,12} |
| 95 |
*/ |
| 96 |
typedef sigc::slot<bool, FlowBoxChild*> SlotFilter; |
| 97 |
|
| 98 |
/** For instance: int on_sort(FlowBoxChild* child1, FlowBoxChild* child2) |
| 99 |
* |
| 100 |
* Will be called for each child after a call to FlowBox::set_sort_func(), |
| 101 |
* and will continue to be called each time a child changes (via |
| 102 |
* FlowBoxChild::changed()) and when FlowBox::invalidate_sort() |
| 103 |
* is called. |
| 104 |
* |
| 105 |
* @param child1 The first child. |
| 106 |
* @param child2 The second child. |
| 107 |
* @returns < 0 if @a child1 should be before @a child2, 0 if |
| 108 |
* they are equal, and > 0 otherwise |
| 109 |
* |
| 110 |
* @newin{3,12} |
| 111 |
*/ |
| 112 |
typedef sigc::slot<int, FlowBoxChild*, FlowBoxChild*> SlotSort; |
| 113 |
|
| 114 |
_WRAP_METHOD(void set_homogeneous(bool homogeneous = true), gtk_flow_box_set_homogeneous) |
| 115 |
_WRAP_METHOD(bool get_homogeneous() const, gtk_flow_box_get_homogeneous) |
| 116 |
|
| 117 |
_WRAP_METHOD(void set_row_spacing(guint spacing), gtk_flow_box_set_row_spacing) |
| 118 |
_WRAP_METHOD(guint get_row_spacing() const, gtk_flow_box_get_row_spacing) |
| 119 |
|
| 120 |
_WRAP_METHOD(void set_column_spacing(guint spacing), gtk_flow_box_set_column_spacing) |
| 121 |
_WRAP_METHOD(guint get_column_spacing() const, gtk_flow_box_get_column_spacing) |
| 122 |
|
| 123 |
_WRAP_METHOD(void set_min_children_per_line(guint n_children), gtk_flow_box_set_min_children_per_line) |
| 124 |
_WRAP_METHOD(guint get_min_children_per_line() const, gtk_flow_box_get_min_children_per_line) |
| 125 |
|
| 126 |
_WRAP_METHOD(void set_max_children_per_line(guint n_children), gtk_flow_box_set_max_children_per_line) |
| 127 |
_WRAP_METHOD(guint get_max_children_per_line() const, gtk_flow_box_get_max_children_per_line) |
| 128 |
|
| 129 |
_WRAP_METHOD(void set_activate_on_single_click(bool single = true), gtk_flow_box_set_activate_on_single_click) |
| 130 |
_WRAP_METHOD(bool get_activate_on_single_click() const, gtk_flow_box_get_activate_on_single_click) |
| 131 |
|
| 132 |
_WRAP_METHOD(void insert(Widget& widget, int position), gtk_flow_box_insert) |
| 133 |
|
| 134 |
_WRAP_METHOD(FlowBoxChild* get_child_at_index(int idx), gtk_flow_box_get_child_at_index) |
| 135 |
_WRAP_METHOD(const FlowBoxChild* get_child_at_index(int idx) const, gtk_flow_box_get_child_at_index, constversion) |
| 136 |
|
| 137 |
/** Calls a function for each selected child. |
| 138 |
* |
| 139 |
* Note that the selection cannot be modified from within |
| 140 |
* this function. |
| 141 |
* |
| 142 |
* @param slot The function to call for each selected child |
| 143 |
* |
| 144 |
* @newin{3,12} |
| 145 |
*/ |
| 146 |
void selected_foreach(const SlotSelectedForeach& slot); |
| 147 |
_IGNORE(gtk_flow_box_selected_foreach) |
| 148 |
|
| 149 |
// transfer container |
| 150 |
#m4 _CONVERSION(`GList*',`std::vector< Gtk::FlowBoxChild*>',`Glib::ListHandler< Gtk::FlowBoxChild* >::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)') |
| 151 |
_WRAP_METHOD(std::vector< Gtk::FlowBoxChild*> get_selected_children(), gtk_flow_box_get_selected_children) |
| 152 |
#m4 _CONVERSION(`GList*',`std::vector< const Gtk::FlowBoxChild*>',`Glib::ListHandler< const Gtk::FlowBoxChild* >::list_to_vector($3, Glib::OWNERSHIP_SHALLOW)') |
| 153 |
_WRAP_METHOD(std::vector< const Gtk::FlowBoxChild*> get_selected_children() const, gtk_flow_box_get_selected_children) |
| 154 |
|
| 155 |
_WRAP_METHOD(void select_child(FlowBoxChild& child), gtk_flow_box_select_child) |
| 156 |
_WRAP_METHOD(void unselect_child(FlowBoxChild& child), gtk_flow_box_unselect_child) |
| 157 |
|
| 158 |
_WRAP_METHOD(void select_all(), gtk_flow_box_select_all) |
| 159 |
_WRAP_METHOD(void unselect_all(), gtk_flow_box_unselect_all) |
| 160 |
|
| 161 |
_WRAP_METHOD(void set_selection_mode(SelectionMode mode = SELECTION_NONE), gtk_flow_box_set_selection_mode) |
| 162 |
_WRAP_METHOD(SelectionMode get_selection_mode() const, gtk_flow_box_get_selection_mode) |
| 163 |
|
| 164 |
_WRAP_METHOD(void set_hadjustment(const Glib::RefPtr<Adjustment>& adjustment), gtk_flow_box_set_hadjustment) |
| 165 |
_WRAP_METHOD(void set_vadjustment(const Glib::RefPtr<Adjustment>& adjustment), gtk_flow_box_set_vadjustment) |
| 166 |
|
| 167 |
/** Sets a filter function. |
| 168 |
* |
| 169 |
* By setting a filter function on the FlowBox one can decide dynamically |
| 170 |
* which of the children to show. For instance, to implement a search |
| 171 |
* function that only shows the children matching the search terms. |
| 172 |
* |
| 173 |
* The @a slot will be called for each child after the call, and |
| 174 |
* it will continue to be called each time a child changes (via |
| 175 |
* FlowBoxChild::changed()) or when FlowBox::invalidate_filter() |
| 176 |
* is called. |
| 177 |
* |
| 178 |
* @param slot Callback that lets you filter which children to show |
| 179 |
* |
| 180 |
* @newin{3,12} |
| 181 |
*/ |
| 182 |
void set_filter_func(const SlotFilter& slot); |
| 183 |
_IGNORE(gtk_flow_box_set_filter_func) |
| 184 |
|
| 185 |
/** Removes the filter function, if any. |
| 186 |
* |
| 187 |
* @newin{3,12} |
| 188 |
*/ |
| 189 |
void unset_filter_func(); |
| 190 |
|
| 191 |
_WRAP_METHOD(void invalidate_filter(), gtk_flow_box_invalidate_filter) |
| 192 |
|
| 193 |
/** Sets a sort function. |
| 194 |
* |
| 195 |
* By setting a sort function on the FlowBox, one can dynamically |
| 196 |
* reorder the children of the box, based on the contents of |
| 197 |
* the children. |
| 198 |
* |
| 199 |
* The @a slot will be called for each child after the call, |
| 200 |
* and will continue to be called each time a child changes (via |
| 201 |
* FlowBoxChild::changed()) and when FlowBox::invalidate_sort() |
| 202 |
* is called. |
| 203 |
* |
| 204 |
* @param slot The sort function |
| 205 |
* |
| 206 |
* @newin{3,12} |
| 207 |
*/ |
| 208 |
void set_sort_func(const SlotSort& slot); |
| 209 |
_IGNORE(gtk_flow_box_set_sort_func) |
| 210 |
|
| 211 |
/** Removes the sort function, if any. |
| 212 |
* |
| 213 |
* @newin{3,12} |
| 214 |
*/ |
| 215 |
void unset_sort_func(); |
| 216 |
|
| 217 |
_WRAP_METHOD(void invalidate_sort(), gtk_flow_box_invalidate_sort) |
| 218 |
|
| 219 |
/** For instance: |
| 220 |
* Gtk::Widget* on_create_widget(const Glib::RefPtr<T_item>& item); |
| 221 |
* |
| 222 |
* Called for flow boxes that are bound to a Gio::ListModel with bind_model() |
| 223 |
* or bind_list_store() for each item that gets added to the model. |
| 224 |
* |
| 225 |
* @newin{3,22} |
| 226 |
* |
| 227 |
* @tparam T_item Base class of the items in the Gio::ListModel. All items must |
| 228 |
* be of type T_item or a type derived from T_item. |
| 229 |
* T_item must be Glib::Object or a type derived from Glib::Object. |
| 230 |
* @param item The item from the model for which to create a widget. |
| 231 |
* @eturn A Gtk::Widget that represents @a item. |
| 232 |
*/ |
| 233 |
template <typename T_item> |
| 234 |
using SlotCreateWidget = sigc::slot<Gtk::Widget*, const Glib::RefPtr<T_item>&>; |
| 235 |
|
| 236 |
/** Binds a Gio::ListModel. |
| 237 |
* |
| 238 |
* If this FlowBox was already bound to a model, that previous binding is |
| 239 |
* destroyed. |
| 240 |
* |
| 241 |
* The contents of the FlowBox are cleared and then filled with widgets that |
| 242 |
* represent items from @a model. The FlowBox is updated whenever @a model changes. |
| 243 |
* If @a model is an empty Glib::RefPtr, the FlowBox is left empty. |
| 244 |
* |
| 245 |
* It is undefined to add or remove widgets directly (for example, with |
| 246 |
* insert() or Gtk::Container::add()) while the FlowBox is bound to a model. |
| 247 |
* |
| 248 |
* Note that using a model is incompatible with the filtering and sorting |
| 249 |
* functionality in FlowBox. When using a model, filtering and sorting |
| 250 |
* should be implemented by the model. |
| 251 |
* |
| 252 |
* @newin{3,22} |
| 253 |
* |
| 254 |
* @param model The Gio::ListModel to be bound. |
| 255 |
* @param slot_create_widget A slot that creates widgets for items. |
| 256 |
* |
| 257 |
* @see bind_list_store() |
| 258 |
*/ |
| 259 |
void bind_model(const Glib::RefPtr<Gio::ListModel>& model, |
| 260 |
const SlotCreateWidget<Glib::Object>& slot_create_widget); |
| 261 |
_IGNORE(gtk_flow_box_bind_model) |
| 262 |
|
| 263 |
/** Binds a Gio::ListStore<>. |
| 264 |
* |
| 265 |
* If this FlowBox was already bound to a Gio::ListModel, that previous binding is |
| 266 |
* destroyed. (Gio::ListStore is a Gio::ListModel.) |
| 267 |
* |
| 268 |
* The contents of the FlowBox are cleared and then filled with widgets that |
| 269 |
* represent items from @a store. The FlowBox is updated whenever @a store changes. |
| 270 |
* If @a store is an empty Glib::RefPtr, the FlowBox is left empty. |
| 271 |
* |
| 272 |
* It is undefined to add or remove widgets directly (for example, with |
| 273 |
* insert() or Gtk::Container::add()) while the FlowBox is bound to a model. |
| 274 |
* |
| 275 |
* Note that using a model is incompatible with the filtering and sorting |
| 276 |
* functionality in FlowBox. When using a model, filtering and sorting |
| 277 |
* should be implemented by the model. |
| 278 |
* |
| 279 |
* @newin{3,22} |
| 280 |
* |
| 281 |
* @tparam T_item Base class of the items in the Gio::ListStore. All items must |
| 282 |
* be of type T_item or a type derived from T_item. |
| 283 |
* T_item must be Glib::Object or a type derived from Glib::Object. |
| 284 |
* @tparam T_slot SlotCreateWidget<T_item> or a type that can be converted |
| 285 |
* to SlotCreateWidget<T_item>. |
| 286 |
* @param store The Gio::ListStore<> to be bound. |
| 287 |
* @param slot_create_widget A slot that creates widgets for items. |
| 288 |
*/ |
| 289 |
template <typename T_item, typename T_slot> |
| 290 |
void bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store, |
| 291 |
T_slot&& slot_create_widget); |
| 292 |
|
| 293 |
_WRAP_PROPERTY("selection-mode", SelectionMode) |
| 294 |
_WRAP_PROPERTY("activate-on-single-click", bool) |
| 295 |
_WRAP_PROPERTY("homogeneous", bool) |
| 296 |
_WRAP_PROPERTY("min-children-per-line", guint) |
| 297 |
_WRAP_PROPERTY("max-children-per-line", guint) |
| 298 |
_WRAP_PROPERTY("row-spacing", guint) |
| 299 |
_WRAP_PROPERTY("column-spacing", guint) |
| 300 |
|
| 301 |
|
| 302 |
_WRAP_SIGNAL(void child_activated(FlowBoxChild* child), "child-activated") |
| 303 |
_WRAP_SIGNAL(void selected_children_changed(), "selected-children-changed") |
| 304 |
_IGNORE_SIGNAL(activate-cursor-child, toggle-cursor-child, move-cursor, select-all, unselect-all) // Action signals |
| 305 |
|
| 306 |
private: |
| 307 |
template <typename T_item> |
| 308 |
static GtkWidget* proxy_bind_list_store_create_widget_callback(void* item, void* data); |
| 309 |
}; |
| 310 |
|
| 311 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 312 |
|
| 313 |
template <typename T_item, typename T_slot> |
| 314 |
void FlowBox::bind_list_store(const Glib::RefPtr<Gio::ListStore<T_item>>& store, |
| 315 |
T_slot&& slot_create_widget) |
| 316 |
{ |
| 317 |
// Create a copy of the slot. |
| 318 |
// It will be deleted by Glib::destroy_notify_delete<SlotCreateWidget>. |
| 319 |
auto slot_copy = new SlotCreateWidget<T_item>(std::forward<T_slot>(slot_create_widget)); |
| 320 |
|
| 321 |
gtk_flow_box_bind_model(gobj(), |
| 322 |
Glib::unwrap(Glib::RefPtr<Gio::ListModel>::cast_static(store)), |
| 323 |
&proxy_bind_list_store_create_widget_callback<T_item>, |
| 324 |
slot_copy, &Glib::destroy_notify_delete<SlotCreateWidget<T_item>>); |
| 325 |
} |
| 326 |
|
| 327 |
template <typename T_item> |
| 328 |
GtkWidget* FlowBox::proxy_bind_list_store_create_widget_callback(void* item, void* data) |
| 329 |
{ |
| 330 |
auto& slot = *static_cast<SlotCreateWidget<T_item>*>(data); |
| 331 |
auto cobject = static_cast<typename T_item::BaseObjectType*>(item); |
| 332 |
|
| 333 |
try |
| 334 |
{ |
| 335 |
// cast_dynamic is necessary if T_item is a user-defined type, such as |
| 336 |
// class MyObject : public Glib::Object |
| 337 |
// take_copy is true here, because wrap() returns a Glib::RefPtr<>. |
| 338 |
// cobject will be unreferenced when the RefPtr is deleted. |
| 339 |
Gtk::Widget* widget = slot(Glib::RefPtr<T_item>::cast_dynamic(Glib::wrap(cobject, true))); |
| 340 |
if (widget) |
| 341 |
return widget->gobj(); |
| 342 |
} |
| 343 |
catch(...) |
| 344 |
{ |
| 345 |
Glib::exception_handlers_invoke(); |
| 346 |
} |
| 347 |
return nullptr; |
| 348 |
} |
| 349 |
|
| 350 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 351 |
|
| 352 |
} // namespace Gtk |