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