#if !defined( INCLUDED_GTKUTIL_CONTAINER_H )
#define INCLUDED_GTKUTIL_CONTAINER_H
-#include <gtk/gtk.h>
-
-inline ui::Widget container_add_widget( ui::Container container, ui::Widget widget ){
- container.add(widget);
- return widget;
-}
-
-inline void container_remove( ui::Widget item, gpointer data ){
- auto container = ui::Container(GTK_CONTAINER( data ));
- container.add(item);
-}
-
-inline void container_remove_all( ui::Container container ){
- gtk_container_foreach(container, (GtkCallback) container_remove, container );
+inline void container_remove_all(ui::Container container)
+{
+ container.foreach([=](ui::Widget it) mutable {
+ container.remove(it);
+ });
}
#endif
ui::MenuItem menu_separator( ui::Menu menu ){
auto menu_item = ui::MenuItem(GTK_MENU_ITEM( gtk_menu_item_new() ));
- container_add_widget( menu, menu_item );
+ menu.add(menu_item);
gtk_widget_set_sensitive( GTK_WIDGET( menu_item ), FALSE );
menu_item.show();
return menu_item;
ui::TearoffMenuItem menu_tearoff( ui::Menu menu ){
auto menu_item = ui::TearoffMenuItem(GTK_TEAROFF_MENU_ITEM( gtk_tearoff_menu_item_new() ));
- container_add_widget( menu, menu_item );
+ menu.add(menu_item);
// gtk_widget_set_sensitive(GTK_WIDGET(menu_item), FALSE); -- controls whether menu is detachable
menu_item.show();
return menu_item;
ui::Menu create_sub_menu_with_mnemonic( ui::MenuShell parent, const char* mnemonic ){
auto item = new_sub_menu_item_with_mnemonic( mnemonic );
- container_add_widget( parent, item );
+ parent.add(item);
return ui::Menu(GTK_MENU( gtk_menu_item_get_submenu( item ) ));
}
ui::MenuItem create_menu_item_with_mnemonic( ui::Menu menu, const char *mnemonic, const Callback& callback ){
auto item = new_menu_item_with_mnemonic( mnemonic, callback );
- container_add_widget( menu, item );
+ menu.add(item);
return item;
}
ui::CheckMenuItem create_check_menu_item_with_mnemonic( ui::Menu menu, const char* mnemonic, const Callback& callback ){
auto item = new_check_menu_item_with_mnemonic( mnemonic, callback );
- container_add_widget( menu, item );
+ menu.add(item);
return item;
}
ui::RadioMenuItem create_radio_menu_item_with_mnemonic( ui::Menu menu, GSList** group, const char* mnemonic, const Callback& callback ){
auto item = new_radio_menu_item_with_mnemonic( group, mnemonic, callback );
- container_add_widget( menu, item );
+ menu.add(item);
return item;
}
template<class T> _IMPL(T,);
-#define this verify<self>::test(*static_cast<self>(this))
+#define this (verify<self>::test(*static_cast<self>(this)))
IMPL(Editable, GTK_EDITABLE);
gtk_container_add(this, widget);
}
+ void IContainer::remove(Widget widget)
+ {
+ gtk_container_remove(this, widget);
+ }
+
IMPL(Bin, GTK_BIN);
IMPL(Window, GTK_WINDOW);
WRAP(Container, Widget, _GtkContainer, (),
,
void add(Widget widget);
+
+ void remove(Widget widget);
+
+ template<class Lambda>
+ void foreach(Lambda &&lambda);
);
WRAP(Bin, Container, _GtkBin, (),
#undef WRAP
+ // callbacks
+
+ namespace {
+ using GtkCallback = void (*)(_GtkWidget *, void *);
+ extern "C" {
+ void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
+ }
+ }
+
+#define this (*static_cast<self>(this))
+
+ template<class Lambda>
+ void IContainer::foreach(Lambda &&lambda)
+ {
+ GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
+ using Function = typename std::decay<Lambda>::type;
+ Function *f = static_cast<Function *>(data);
+ (*f)(Widget(widget));
+ };
+ gtk_container_foreach(this, cb, &lambda);
+ }
+
+#undef this
+
}
#endif
ui::Widget item(ui::MenuItem( "Clear" ));
g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( console_clear ), 0 );
item.show();
- container_add_widget( menu, item );
+ menu.add(item);
}
gboolean destroy_set_null( ui::Window widget, ui::Widget* p ){
void pushMenu( const CopiedString& name ){
auto item = ui::MenuItem( name.c_str() );
item.show();
- container_add_widget( m_stack.back().first, item );
+ m_stack.back().first.add(item);
auto submenu = ui::Menu();
gtk_menu_item_set_submenu( item, GTK_WIDGET( submenu ) );