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