]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/uilib/uilib.cpp
macos: introduce ugly packing hack to attempt to reduce the size of the residual...
[xonotic/netradiant.git] / libs / uilib / uilib.cpp
index cb44edb24f992e62462592535524947987fb3abb..5caf150d2c9bb16c2218235610c1bce346725f89 100644 (file)
@@ -36,25 +36,32 @@ namespace ui {
         }
     }
 
-    Widget root{ui::null};
-
 #define IMPL(T, F) template<> _IMPL(T, F)
-#define _IMPL(T, F) struct verify<T *> { using self = T; static self test(self it) { return self(F(it)); } }
+#define _IMPL(T, F) struct verify<T *> { using self = T; static self test(self it) { return self::from(F(it)); } }
 
     template<class T>
     struct verify;
 
     template<class T> _IMPL(T,);
 
-#define this (verify<self>::test(*static_cast<self>(this)))
+    template<class T>
+    using pointer_remove_const = std::add_pointer<
+            typename std::remove_const<
+                    typename std::remove_pointer<T>::type
+            >::type
+    >;
+
+#define this (verify<self>::test(*static_cast<self>(const_cast<pointer_remove_const<decltype(this)>::type>(this))))
 
     IMPL(Editable, GTK_EDITABLE);
 
     void IEditable::editable(bool value)
     {
-        gtk_editable_set_editable(GTK_EDITABLE(this), value);
+        gtk_editable_set_editable(this, value);
     }
 
+    IMPL(TreeModel, GTK_TREE_MODEL);
+
     IMPL(Widget, GTK_WIDGET);
 
     Widget::Widget(ui::New_t) : Widget(nullptr)
@@ -80,6 +87,16 @@ namespace ui {
     void IWidget::visible(bool shown)
     {
         if (shown) {
+
+#ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
+            /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
+            GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( this ), "glwidget" ) );
+            if ( glwidget ){
+                gtk_widget_hide( glwidget );
+                gtk_widget_show( glwidget );
+            }
+#endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
+
             this.show();
         } else {
             this.hide();
@@ -136,31 +153,6 @@ namespace ui {
     )))
     {}
 
-    alert_response IWindow::alert(std::string text, std::string title, alert_type type, alert_icon icon)
-    {
-        auto ret = gtk_MessageBox(this, text.c_str(),
-                                  title.c_str(),
-                                  type == alert_type::OK ? eMB_OK :
-                                  type == alert_type::OKCANCEL ? eMB_OKCANCEL :
-                                  type == alert_type::YESNO ? eMB_YESNO :
-                                  type == alert_type::YESNOCANCEL ? eMB_YESNOCANCEL :
-                                  type == alert_type::NOYES ? eMB_NOYES :
-                                  eMB_OK,
-                                  icon == alert_icon::Default ? eMB_ICONDEFAULT :
-                                  icon == alert_icon::Error ? eMB_ICONERROR :
-                                  icon == alert_icon::Warning ? eMB_ICONWARNING :
-                                  icon == alert_icon::Question ? eMB_ICONQUESTION :
-                                  icon == alert_icon::Asterisk ? eMB_ICONASTERISK :
-                                  eMB_ICONDEFAULT
-        );
-        return
-                ret == eIDOK ? alert_response::OK :
-                ret == eIDCANCEL ? alert_response::CANCEL :
-                ret == eIDYES ? alert_response::YES :
-                ret == eIDNO ? alert_response::NO :
-                alert_response::OK;
-    }
-
     Window IWindow::create_dialog_window(const char *title, void func(), void *data, int default_w, int default_h)
     {
         return Window(::create_dialog_window(this, title, func, data, default_w, default_h));
@@ -187,7 +179,7 @@ namespace ui {
             delete data;
         };
         auto func = [](_GtkWidget *widget, GdkEventKey *event, user_data *args) -> bool {
-            return args->f(Widget(widget), event, args->extra);
+            return args->f(Widget::from(widget), event, args->extra);
         };
         auto clos = g_cclosure_new(G_CALLBACK(+func), pass, reinterpret_cast<GClosureNotify>(+dtor));
         return g_signal_connect_closure(G_OBJECT(this), "key-press-event", clos, false);
@@ -219,9 +211,14 @@ namespace ui {
 
     IMPL(ToggleButton, GTK_TOGGLE_BUTTON);
 
-    bool IToggleButton::active()
+    bool IToggleButton::active() const
+    {
+        return gtk_toggle_button_get_active(this) != 0;
+    }
+
+    void IToggleButton::active(bool value)
     {
-        return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(this)) != 0;
+        gtk_toggle_button_set_active(this, value);
     }
 
     IMPL(CheckButton, GTK_CHECK_BUTTON);
@@ -273,6 +270,11 @@ namespace ui {
         gtk_box_pack_end(this, child, expand, fill, padding);
     }
 
+    void IBox::set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing)
+    {
+        gtk_box_set_child_packing(this, child, expand, fill, padding, (GtkPackType) packing);
+    }
+
     IMPL(VBox, GTK_VBOX);
 
     VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing)))
@@ -305,6 +307,14 @@ namespace ui {
     )
     {}
 
+    void ITable::attach(Widget child, TableAttach attach, TableAttachOptions options, TablePadding padding) {
+        gtk_table_attach(this, child,
+                         attach.left, attach.right, attach.top, attach.bottom,
+                         static_cast<GtkAttachOptions>(options.x), static_cast<GtkAttachOptions>(options.y),
+                         padding.x, padding.y
+        );
+    }
+
     IMPL(TextView, GTK_TEXT_VIEW);
 
     TextView::TextView(ui::New_t) : TextView(GTK_TEXT_VIEW(gtk_text_view_new()))
@@ -414,6 +424,8 @@ namespace ui {
         gtk_list_store_append(this, nullptr);
     }
 
+    IMPL(TreeStore, GTK_TREE_STORE);
+
     // IMPL(TreePath, GTK_TREE_PATH);
 
     TreePath::TreePath(ui::New_t) : TreePath(gtk_tree_path_new())
@@ -424,6 +436,16 @@ namespace ui {
 
     // Custom
 
+#if GTK_TARGET == 3
+
+    IMPL(GLArea, (void *));
+
+#elif GTK_TARGET == 2
+
+    IMPL(GLArea, GTK_DRAWING_AREA);
+
+#endif
+
     guint IGLArea::on_render(GCallback pFunction, void *data)
     {
 #if GTK_TARGET == 3
@@ -434,4 +456,33 @@ namespace ui {
 #endif
     }
 
+    // global
+
+    Window root{ui::null};
+
+    alert_response alert(Window parent, std::string text, std::string title, alert_type type, alert_icon icon)
+    {
+        auto ret = gtk_MessageBox(parent, text.c_str(),
+                                  title.c_str(),
+                                  type == alert_type::OK ? eMB_OK :
+                                  type == alert_type::OKCANCEL ? eMB_OKCANCEL :
+                                  type == alert_type::YESNO ? eMB_YESNO :
+                                  type == alert_type::YESNOCANCEL ? eMB_YESNOCANCEL :
+                                  type == alert_type::NOYES ? eMB_NOYES :
+                                  eMB_OK,
+                                  icon == alert_icon::Default ? eMB_ICONDEFAULT :
+                                  icon == alert_icon::Error ? eMB_ICONERROR :
+                                  icon == alert_icon::Warning ? eMB_ICONWARNING :
+                                  icon == alert_icon::Question ? eMB_ICONQUESTION :
+                                  icon == alert_icon::Asterisk ? eMB_ICONASTERISK :
+                                  eMB_ICONDEFAULT
+        );
+        return
+                ret == eIDOK ? alert_response::OK :
+                ret == eIDCANCEL ? alert_response::CANCEL :
+                ret == eIDYES ? alert_response::YES :
+                ret == eIDNO ? alert_response::NO :
+                alert_response::OK;
+    }
+
 }