]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/uilib/uilib.h
macos: introduce ugly packing hack to attempt to reduce the size of the residual...
[xonotic/netradiant.git] / libs / uilib / uilib.h
index 72470990ea8cf7c5e4804a377ae8038de97c7c58..d8f41c85d88559371a36f86c2ed90a33235537c3 100644 (file)
@@ -31,6 +31,7 @@ struct _GtkImage;
 struct _GtkItem;
 struct _GtkLabel;
 struct _GtkListStore;
+struct _GtkTreeIter;
 struct _GtkMenu;
 struct _GtkMenuBar;
 struct _GtkMenuItem;
@@ -56,6 +57,7 @@ struct _GtkToolItem;
 struct _GtkTreeModel;
 struct _GtkTreePath;
 struct _GtkTreeSelection;
+struct _GtkTreeStore;
 struct _GtkTreeView;
 struct _GtkTreeViewColumn;
 struct _GtkVBox;
@@ -76,37 +78,12 @@ struct ModalDialog;
 
 namespace ui {
 
-    void init(int argc, char *argv[]);
+    bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
 
     void main();
 
     void process();
 
-    extern class Widget root;
-
-    enum class alert_type {
-        OK,
-        OKCANCEL,
-        YESNO,
-        YESNOCANCEL,
-        NOYES,
-    };
-
-    enum class alert_icon {
-        Default,
-        Error,
-        Warning,
-        Question,
-        Asterisk,
-    };
-
-    enum class alert_response {
-        OK,
-        CANCEL,
-        YES,
-        NO,
-    };
-
     enum class window_type {
         TOP,
         POPUP
@@ -126,6 +103,11 @@ namespace ui {
         NEVER
     };
 
+       enum class Packing {
+               START,
+               END
+       };
+
     namespace details {
 
         enum class Convert {
@@ -165,7 +147,8 @@ namespace ui {
         };
     }
 
-    extern struct Null {} null;
+    const struct Null {} null = {};
+    const struct New_t {} New = {};
 
     class Object :
             public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
@@ -175,7 +158,7 @@ namespace ui {
         using native = _GtkObject *;
         native _handle;
 
-        Object(native h) : _handle(h)
+        explicit Object(native h) : _handle(h)
         {}
 
         explicit operator bool() const
@@ -187,6 +170,9 @@ namespace ui {
         void unref()
         { g_object_unref(_handle); }
 
+        void ref()
+        { g_object_ref(_handle); }
+
         template<class Lambda>
         gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
 
@@ -206,8 +192,13 @@ namespace ui {
     public: \
         using self = name *; \
         using native = T *; \
-        explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
-        explicit name(Null n) : name((native) nullptr) {} \
+    protected: \
+        explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
+    public: \
+        explicit name(Null n) noexcept : name((native) nullptr) {} \
+        explicit name(New_t); \
+        static name from(native h) { return name(h); } \
+        static name from(void *ptr) { return name((native) ptr); } \
         ctors \
     }; \
     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
@@ -222,7 +213,6 @@ namespace ui {
     );
 
     WRAP(Editable, Object, _GtkEditable, (),
-         Editable();
     ,
          void editable(bool value);
     );
@@ -233,15 +223,15 @@ namespace ui {
 
     // GObject
 
+    struct Dimensions {
+        int width;
+        int height;
+    };
+
+    class Window;
     WRAP(Widget, Object, _GtkWidget, (),
-         Widget();
     ,
-         alert_response alert(
-                 std::string text,
-                 std::string title = "NetRadiant",
-                 alert_type type = alert_type::OK,
-                 alert_icon icon = alert_icon::Default
-         );
+         Window window();
          const char *file_dialog(
                  bool open,
                  const char *title,
@@ -251,7 +241,13 @@ namespace ui {
                  bool want_import = false,
                  bool want_save = false
          );
+         bool visible();
+         void visible(bool shown);
          void show();
+         void hide();
+         Dimensions dimensions();
+         void dimensions(int width, int height);
+         void destroy();
     );
 
     WRAP(Container, Widget, _GtkContainer, (),
@@ -270,7 +266,7 @@ namespace ui {
 
     class AccelGroup;
     WRAP(Window, Bin, _GtkWindow, (),
-         Window(window_type type);
+         explicit Window(window_type type);
     ,
          Window create_dialog_window(
                  const char *title,
@@ -307,23 +303,23 @@ namespace ui {
     );
 
     WRAP(Frame, Bin, _GtkFrame, (),
-         Frame(const char *label = nullptr);
+         explicit Frame(const char *label = nullptr);
     ,
     );
 
     WRAP(Button, Bin, _GtkButton, (),
-         Button();
-         Button(const char *label);
+         explicit Button(const char *label);
     ,
     );
 
     WRAP(ToggleButton, Button, _GtkToggleButton, (),
     ,
-         bool active();
+         bool active() const;
+         void active(bool value);
     );
 
     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
-         CheckButton(const char *label);
+         explicit CheckButton(const char *label);
     ,
     );
 
@@ -336,8 +332,7 @@ namespace ui {
     );
 
     WRAP(MenuItem, Item, _GtkMenuItem, (),
-         MenuItem();
-         MenuItem(const char *label, bool mnemonic = false);
+         explicit MenuItem(const char *label, bool mnemonic = false);
     ,
     );
 
@@ -350,7 +345,6 @@ namespace ui {
     );
 
     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
-         TearoffMenuItem();
     ,
     );
 
@@ -359,7 +353,6 @@ namespace ui {
     );
 
     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
-         ComboBoxText();
     ,
     );
 
@@ -380,12 +373,15 @@ namespace ui {
     );
 
     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
-         ScrolledWindow();
     ,
+         void overflow(Policy x, Policy y);
     );
 
     WRAP(Box, Container, _GtkBox, (),
     ,
+         void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
+         void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
+         void set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing);
     );
 
     WRAP(VBox, Box, _GtkVBox, (),
@@ -403,12 +399,10 @@ namespace ui {
     );
 
     WRAP(HPaned, Paned, _GtkHPaned, (),
-         HPaned();
     ,
     );
 
     WRAP(VPaned, Paned, _GtkVPaned, (),
-         VPaned();
     ,
     );
 
@@ -421,18 +415,32 @@ namespace ui {
     );
 
     WRAP(Menu, MenuShell, _GtkMenu, (),
-         Menu();
     ,
     );
 
+    struct TableAttach {
+        unsigned int left, right, top, bottom;
+    };
+
+    struct TableAttachOptions {
+        // todo: type safety
+        unsigned int x, y;
+    };
+
+    struct TablePadding {
+        unsigned int x, y;
+    };
+
     WRAP(Table, Container, _GtkTable, (),
          Table(std::size_t rows, std::size_t columns, bool homogenous);
     ,
+         // 5 = expand | fill
+         void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
     );
 
     WRAP(TextView, Container, _GtkTextView, (),
-         TextView();
     ,
+         void text(char const *str);
     );
 
     WRAP(Toolbar, Container, _GtkToolbar, (),
@@ -441,7 +449,6 @@ namespace ui {
 
     class TreeModel;
     WRAP(TreeView, Widget, _GtkTreeView, (),
-         TreeView();
          TreeView(TreeModel model);
     ,
     );
@@ -451,19 +458,20 @@ namespace ui {
     );
 
     WRAP(Label, Widget, _GtkLabel, (),
-         Label(const char *label);
+         explicit Label(const char *label);
     ,
+         void text(char const *str);
     );
 
     WRAP(Image, Widget, _GtkImage, (),
-         Image();
     ,
     );
 
     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
-         Entry();
-         Entry(std::size_t max_length);
+         explicit Entry(std::size_t max_length);
     ,
+        char const *text();
+        void text(char const *str);
     );
 
     class Adjustment;
@@ -481,7 +489,7 @@ namespace ui {
     );
 
     WRAP(HScale, Scale, _GtkHScale, (),
-         HScale(Adjustment adjustment);
+         explicit HScale(Adjustment adjustment);
          HScale(double min, double max, double step);
     ,
     );
@@ -499,7 +507,6 @@ namespace ui {
     );
 
     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
-         CellRendererText();
     ,
     );
 
@@ -513,7 +520,6 @@ namespace ui {
     );
 
     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
-         AccelGroup();
     ,
     );
 
@@ -524,6 +530,15 @@ namespace ui {
     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
     ,
          void clear();
+
+         template<class... T>
+         void append(T... args);
+
+         void append();
+    );
+
+    WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
+    ,
     );
 
     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
@@ -533,8 +548,7 @@ namespace ui {
     // GBoxed
 
     WRAP(TreePath, Object, _GtkTreePath, (),
-         TreePath();
-         TreePath(const char *path);
+         explicit TreePath(const char *path);
     ,
     );
 
@@ -547,6 +561,41 @@ namespace ui {
 
 #undef WRAP
 
+    // global
+
+    enum class alert_response {
+        OK,
+        CANCEL,
+        YES,
+        NO,
+    };
+
+    enum class alert_type {
+        OK,
+        OKCANCEL,
+        YESNO,
+        YESNOCANCEL,
+        NOYES,
+    };
+
+    enum class alert_icon {
+        Default,
+        Error,
+        Warning,
+        Question,
+        Asterisk,
+    };
+
+    extern class Window root;
+
+    alert_response alert(
+            Window parent,
+            std::string text,
+            std::string title = RADIANT_NAME,
+            alert_type type = alert_type::OK,
+            alert_icon icon = alert_icon::Default
+    );
+
     // callbacks
 
     namespace {
@@ -576,11 +625,23 @@ namespace ui {
         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
             using Function = typename std::decay<Lambda>::type;
             Function *f = static_cast<Function *>(data);
-            (*f)(Widget(widget));
+            (*f)(Widget::from(widget));
         };
         gtk_container_foreach(this, cb, &lambda);
     }
 
+    namespace {
+        extern "C" {
+        void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
+        }
+    }
+
+    template<class... T>
+    void IListStore::append(T... args) {
+        static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
+        gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
+    }
+
 #undef this
 
 }