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