]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - 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
1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
3
4 #include <string>
5 #include <glib-object.h>
6
7 struct _GdkEventKey;
8 struct _GtkAccelGroup;
9 struct _GtkAdjustment;
10 struct _GtkAlignment;
11 struct _GtkBin;
12 struct _GtkBox;
13 struct _GtkButton;
14 struct _GtkCellEditable;
15 struct _GtkCellRenderer;
16 struct _GtkCellRendererText;
17 struct _GtkCheckButton;
18 struct _GtkCheckMenuItem;
19 struct _GtkComboBox;
20 struct _GtkComboBoxText;
21 struct _GtkContainer;
22 struct _GtkDialog;
23 struct _GtkEditable;
24 struct _GtkEntry;
25 struct _GtkEntryCompletion;
26 struct _GtkFrame;
27 struct _GtkHBox;
28 struct _GtkHPaned;
29 struct _GtkHScale;
30 struct _GtkImage;
31 struct _GtkItem;
32 struct _GtkLabel;
33 struct _GtkListStore;
34 struct _GtkTreeIter;
35 struct _GtkMenu;
36 struct _GtkMenuBar;
37 struct _GtkMenuItem;
38 struct _GtkMenuShell;
39 struct _GtkMisc;
40 struct _GtkObject;
41 struct _GtkPaned;
42 struct _GtkRadioButton;
43 struct _GtkRadioMenuItem;
44 struct _GtkRadioToolButton;
45 struct _GtkRange;
46 struct _GtkScale;
47 struct _GtkScrolledWindow;
48 struct _GtkSpinButton;
49 struct _GtkTable;
50 struct _GtkTearoffMenuItem;
51 struct _GtkTextView;
52 struct _GtkToggleButton;
53 struct _GtkToggleToolButton;
54 struct _GtkToolbar;
55 struct _GtkToolButton;
56 struct _GtkToolItem;
57 struct _GtkTreeModel;
58 struct _GtkTreePath;
59 struct _GtkTreeSelection;
60 struct _GtkTreeStore;
61 struct _GtkTreeView;
62 struct _GtkTreeViewColumn;
63 struct _GtkVBox;
64 struct _GtkVPaned;
65 struct _GtkWidget;
66 struct _GtkWindow;
67 struct _GTypeInstance;
68
69 #if GTK_TARGET == 3
70 struct _GtkGLArea;
71 #endif
72
73 #if GTK_TARGET == 2
74 using _GtkGLArea = struct _GtkDrawingArea;
75 #endif
76
77 struct ModalDialog;
78
79 namespace ui {
80
81     bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
82
83     void main();
84
85     void process();
86
87     enum class window_type {
88         TOP,
89         POPUP
90     };
91
92     enum class Shadow {
93         NONE,
94         IN,
95         OUT,
96         ETCHED_IN,
97         ETCHED_OUT
98     };
99
100     enum class Policy {
101         ALWAYS,
102         AUTOMATIC,
103         NEVER
104     };
105
106         enum class Packing {
107                 START,
108                 END
109         };
110
111     namespace details {
112
113         enum class Convert {
114             Implicit, Explicit
115         };
116
117         template<class Self, class T, Convert mode>
118         struct Convertible;
119
120         template<class Self, class T>
121         struct Convertible<Self, T, Convert::Implicit> {
122             operator T() const
123             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
124         };
125
126         template<class Self, class T>
127         struct Convertible<Self, T, Convert::Explicit> {
128             explicit operator T() const
129             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
130         };
131
132         template<class Self, class... T>
133         struct All : T ... {
134             All()
135             {};
136         };
137
138         template<class Self, class Interfaces>
139         struct Mixin;
140         template<class Self>
141         struct Mixin<Self, void()> {
142             using type = All<Self>;
143         };
144         template<class Self, class... Interfaces>
145         struct Mixin<Self, void(Interfaces...)> {
146             using type = All<Self, Interfaces...>;
147         };
148     }
149
150     const struct Null {} null = {};
151     const struct New_t {} New = {};
152
153     class Object :
154             public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
155             public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
156     public:
157         using self = Object *;
158         using native = _GtkObject *;
159         native _handle;
160
161         explicit Object(native h) : _handle(h)
162         {}
163
164         explicit operator bool() const
165         { return _handle != nullptr; }
166
167         explicit operator void *() const
168         { return _handle; }
169
170         void unref()
171         { g_object_unref(_handle); }
172
173         void ref()
174         { g_object_ref(_handle); }
175
176         template<class Lambda>
177         gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
178
179         template<class Lambda>
180         gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
181     };
182     static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
183
184 #define WRAP(name, super, T, interfaces, ctors, methods) \
185     class name; \
186     class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
187     public: \
188         using self = name *; \
189         methods \
190     }; \
191     class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
192     public: \
193         using self = name *; \
194         using native = T *; \
195     protected: \
196         explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
197     public: \
198         explicit name(Null n) noexcept : name((native) nullptr) {} \
199         explicit name(New_t); \
200         static name from(native h) { return name(h); } \
201         static name from(void *ptr) { return name((native) ptr); } \
202         ctors \
203     }; \
204     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
205     static_assert(sizeof(name) == sizeof(super), "object slicing")
206
207     // https://developer.gnome.org/gtk2/stable/ch01.html
208
209     // GInterface
210
211     WRAP(CellEditable, Object, _GtkCellEditable, (),
212     ,
213     );
214
215     WRAP(Editable, Object, _GtkEditable, (),
216     ,
217          void editable(bool value);
218     );
219
220     WRAP(TreeModel, Object, _GtkTreeModel, (),
221     ,
222     );
223
224     // GObject
225
226     struct Dimensions {
227         int width;
228         int height;
229     };
230
231     class Window;
232     WRAP(Widget, Object, _GtkWidget, (),
233     ,
234          Window window();
235          const char *file_dialog(
236                  bool open,
237                  const char *title,
238                  const char *path = nullptr,
239                  const char *pattern = nullptr,
240                  bool want_load = false,
241                  bool want_import = false,
242                  bool want_save = false
243          );
244          bool visible();
245          void visible(bool shown);
246          void show();
247          void hide();
248          Dimensions dimensions();
249          void dimensions(int width, int height);
250          void destroy();
251     );
252
253     WRAP(Container, Widget, _GtkContainer, (),
254     ,
255          void add(Widget widget);
256
257          void remove(Widget widget);
258
259          template<class Lambda>
260          void foreach(Lambda &&lambda);
261     );
262
263     WRAP(Bin, Container, _GtkBin, (),
264     ,
265     );
266
267     class AccelGroup;
268     WRAP(Window, Bin, _GtkWindow, (),
269          explicit Window(window_type type);
270     ,
271          Window create_dialog_window(
272                  const char *title,
273                  void func(),
274                  void *data,
275                  int default_w = -1,
276                  int default_h = -1
277          );
278
279          Window create_modal_dialog_window(
280                  const char *title,
281                  ModalDialog &dialog,
282                  int default_w = -1,
283                  int default_h = -1
284          );
285
286          Window create_floating_window(const char *title);
287
288          std::uint64_t on_key_press(
289                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
290                  void *extra = nullptr
291          );
292
293          void add_accel_group(AccelGroup group);
294     );
295
296     WRAP(Dialog, Window, _GtkDialog, (),
297     ,
298     );
299
300     WRAP(Alignment, Bin, _GtkAlignment, (),
301          Alignment(float xalign, float yalign, float xscale, float yscale);
302     ,
303     );
304
305     WRAP(Frame, Bin, _GtkFrame, (),
306          explicit Frame(const char *label = nullptr);
307     ,
308     );
309
310     WRAP(Button, Bin, _GtkButton, (),
311          explicit Button(const char *label);
312     ,
313     );
314
315     WRAP(ToggleButton, Button, _GtkToggleButton, (),
316     ,
317          bool active() const;
318          void active(bool value);
319     );
320
321     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
322          explicit CheckButton(const char *label);
323     ,
324     );
325
326     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
327     ,
328     );
329
330     WRAP(Item, Bin, _GtkItem, (),
331     ,
332     );
333
334     WRAP(MenuItem, Item, _GtkMenuItem, (),
335          explicit MenuItem(const char *label, bool mnemonic = false);
336     ,
337     );
338
339     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
340     ,
341     );
342
343     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
344     ,
345     );
346
347     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
348     ,
349     );
350
351     WRAP(ComboBox, Bin, _GtkComboBox, (),
352     ,
353     );
354
355     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
356     ,
357     );
358
359     WRAP(ToolItem, Bin, _GtkToolItem, (),
360     ,
361     );
362
363     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
364     ,
365     );
366
367     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
368     ,
369     );
370
371     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
372     ,
373     );
374
375     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
376     ,
377          void overflow(Policy x, Policy y);
378     );
379
380     WRAP(Box, Container, _GtkBox, (),
381     ,
382          void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
383          void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
384          void set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing);
385     );
386
387     WRAP(VBox, Box, _GtkVBox, (),
388          VBox(bool homogenous, int spacing);
389     ,
390     );
391
392     WRAP(HBox, Box, _GtkHBox, (),
393          HBox(bool homogenous, int spacing);
394     ,
395     );
396
397     WRAP(Paned, Container, _GtkPaned, (),
398     ,
399     );
400
401     WRAP(HPaned, Paned, _GtkHPaned, (),
402     ,
403     );
404
405     WRAP(VPaned, Paned, _GtkVPaned, (),
406     ,
407     );
408
409     WRAP(MenuShell, Container, _GtkMenuShell, (),
410     ,
411     );
412
413     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
414     ,
415     );
416
417     WRAP(Menu, MenuShell, _GtkMenu, (),
418     ,
419     );
420
421     struct TableAttach {
422         unsigned int left, right, top, bottom;
423     };
424
425     struct TableAttachOptions {
426         // todo: type safety
427         unsigned int x, y;
428     };
429
430     struct TablePadding {
431         unsigned int x, y;
432     };
433
434     WRAP(Table, Container, _GtkTable, (),
435          Table(std::size_t rows, std::size_t columns, bool homogenous);
436     ,
437          // 5 = expand | fill
438          void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
439     );
440
441     WRAP(TextView, Container, _GtkTextView, (),
442     ,
443          void text(char const *str);
444     );
445
446     WRAP(Toolbar, Container, _GtkToolbar, (),
447     ,
448     );
449
450     class TreeModel;
451     WRAP(TreeView, Widget, _GtkTreeView, (),
452          TreeView(TreeModel model);
453     ,
454     );
455
456     WRAP(Misc, Widget, _GtkMisc, (),
457     ,
458     );
459
460     WRAP(Label, Widget, _GtkLabel, (),
461          explicit Label(const char *label);
462     ,
463          void text(char const *str);
464     );
465
466     WRAP(Image, Widget, _GtkImage, (),
467     ,
468     );
469
470     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
471          explicit Entry(std::size_t max_length);
472     ,
473         char const *text();
474         void text(char const *str);
475     );
476
477     class Adjustment;
478     WRAP(SpinButton, Entry, _GtkSpinButton, (),
479          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
480     ,
481     );
482
483     WRAP(Range, Widget, _GtkRange, (),
484     ,
485     );
486
487     WRAP(Scale, Range, _GtkScale, (),
488     ,
489     );
490
491     WRAP(HScale, Scale, _GtkHScale, (),
492          explicit HScale(Adjustment adjustment);
493          HScale(double min, double max, double step);
494     ,
495     );
496
497     WRAP(Adjustment, Object, _GtkAdjustment, (),
498          Adjustment(double value,
499                     double lower, double upper,
500                     double step_increment, double page_increment,
501                     double page_size);
502     ,
503     );
504
505     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
506     ,
507     );
508
509     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
510     ,
511     );
512
513     struct TreeViewColumnAttribute {
514         const char *attribute;
515         int column;
516     };
517     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
518          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
519     ,
520     );
521
522     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
523     ,
524     );
525
526     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
527     ,
528     );
529
530     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
531     ,
532          void clear();
533
534          template<class... T>
535          void append(T... args);
536
537          void append();
538     );
539
540     WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
541     ,
542     );
543
544     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
545     ,
546     );
547
548     // GBoxed
549
550     WRAP(TreePath, Object, _GtkTreePath, (),
551          explicit TreePath(const char *path);
552     ,
553     );
554
555     // Custom
556
557     WRAP(GLArea, Widget, _GtkGLArea, (),
558     ,
559          guint on_render(GCallback pFunction, void *data);
560     );
561
562 #undef WRAP
563
564     // global
565
566     enum class alert_response {
567         OK,
568         CANCEL,
569         YES,
570         NO,
571     };
572
573     enum class alert_type {
574         OK,
575         OKCANCEL,
576         YESNO,
577         YESNOCANCEL,
578         NOYES,
579     };
580
581     enum class alert_icon {
582         Default,
583         Error,
584         Warning,
585         Question,
586         Asterisk,
587     };
588
589     extern class Window root;
590
591     alert_response alert(
592             Window parent,
593             std::string text,
594             std::string title = RADIANT_NAME,
595             alert_type type = alert_type::OK,
596             alert_icon icon = alert_icon::Default
597     );
598
599     // callbacks
600
601     namespace {
602         using GtkCallback = void (*)(_GtkWidget *, void *);
603         extern "C" {
604         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
605         }
606     }
607
608 #define this (*static_cast<self>(this))
609
610     template<class Lambda>
611     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
612     {
613         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
614     }
615
616     template<class Lambda>
617     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
618     {
619         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
620     }
621
622     template<class Lambda>
623     void IContainer::foreach(Lambda &&lambda)
624     {
625         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
626             using Function = typename std::decay<Lambda>::type;
627             Function *f = static_cast<Function *>(data);
628             (*f)(Widget::from(widget));
629         };
630         gtk_container_foreach(this, cb, &lambda);
631     }
632
633     namespace {
634         extern "C" {
635         void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
636         }
637     }
638
639     template<class... T>
640     void IListStore::append(T... args) {
641         static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
642         gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
643     }
644
645 #undef this
646
647 }
648
649 #endif