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