]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
Wrap g_object_unref
[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         void unref()
180         { g_object_unref(_handle); }
181
182         template<class Lambda>
183         gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
184
185         template<class Lambda>
186         gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
187     };
188     static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
189
190 #define WRAP(name, super, T, interfaces, ctors, methods) \
191     class name; \
192     class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
193     public: \
194         using self = name *; \
195         methods \
196     }; \
197     class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
198     public: \
199         using self = name *; \
200         using native = T *; \
201         explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
202         explicit name(Null n) : name((native) nullptr) {} \
203         ctors \
204     }; \
205     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
206     static_assert(sizeof(name) == sizeof(super), "object slicing")
207
208     // https://developer.gnome.org/gtk2/stable/ch01.html
209
210     // GInterface
211
212     WRAP(CellEditable, Object, _GtkCellEditable, (),
213     ,
214     );
215
216     WRAP(Editable, Object, _GtkEditable, (),
217          Editable();
218     ,
219          void editable(bool value);
220     );
221
222     WRAP(TreeModel, Object, _GtkTreeModel, (),
223     ,
224     );
225
226     // GObject
227
228     WRAP(Widget, Object, _GtkWidget, (),
229          Widget();
230     ,
231          alert_response alert(
232                  std::string text,
233                  std::string title = "NetRadiant",
234                  alert_type type = alert_type::OK,
235                  alert_icon icon = alert_icon::Default
236          );
237          const char *file_dialog(
238                  bool open,
239                  const char *title,
240                  const char *path = nullptr,
241                  const char *pattern = nullptr,
242                  bool want_load = false,
243                  bool want_import = false,
244                  bool want_save = false
245          );
246          void show();
247     );
248
249     WRAP(Container, Widget, _GtkContainer, (),
250     ,
251          void add(Widget widget);
252
253          void remove(Widget widget);
254
255          template<class Lambda>
256          void foreach(Lambda &&lambda);
257     );
258
259     WRAP(Bin, Container, _GtkBin, (),
260     ,
261     );
262
263     class AccelGroup;
264     WRAP(Window, Bin, _GtkWindow, (),
265          Window(window_type type);
266     ,
267          Window create_dialog_window(
268                  const char *title,
269                  void func(),
270                  void *data,
271                  int default_w = -1,
272                  int default_h = -1
273          );
274
275          Window create_modal_dialog_window(
276                  const char *title,
277                  ModalDialog &dialog,
278                  int default_w = -1,
279                  int default_h = -1
280          );
281
282          Window create_floating_window(const char *title);
283
284          std::uint64_t on_key_press(
285                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
286                  void *extra = nullptr
287          );
288
289          void add_accel_group(AccelGroup group);
290     );
291
292     WRAP(Dialog, Window, _GtkDialog, (),
293     ,
294     );
295
296     WRAP(Alignment, Bin, _GtkAlignment, (),
297          Alignment(float xalign, float yalign, float xscale, float yscale);
298     ,
299     );
300
301     WRAP(Frame, Bin, _GtkFrame, (),
302          Frame(const char *label = nullptr);
303     ,
304     );
305
306     WRAP(Button, Bin, _GtkButton, (),
307          Button();
308          Button(const char *label);
309     ,
310     );
311
312     WRAP(ToggleButton, Button, _GtkToggleButton, (),
313     ,
314          bool active();
315     );
316
317     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
318          CheckButton(const char *label);
319     ,
320     );
321
322     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
323     ,
324     );
325
326     WRAP(Item, Bin, _GtkItem, (),
327     ,
328     );
329
330     WRAP(MenuItem, Item, _GtkMenuItem, (),
331          MenuItem();
332          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          TearoffMenuItem();
346     ,
347     );
348
349     WRAP(ComboBox, Bin, _GtkComboBox, (),
350     ,
351     );
352
353     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
354          ComboBoxText();
355     ,
356     );
357
358     WRAP(ToolItem, Bin, _GtkToolItem, (),
359     ,
360     );
361
362     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
363     ,
364     );
365
366     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
367     ,
368     );
369
370     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
371     ,
372     );
373
374     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
375          ScrolledWindow();
376     ,
377     );
378
379     WRAP(Box, Container, _GtkBox, (),
380     ,
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          HPaned();
399     ,
400     );
401
402     WRAP(VPaned, Paned, _GtkVPaned, (),
403          VPaned();
404     ,
405     );
406
407     WRAP(MenuShell, Container, _GtkMenuShell, (),
408     ,
409     );
410
411     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
412     ,
413     );
414
415     WRAP(Menu, MenuShell, _GtkMenu, (),
416          Menu();
417     ,
418     );
419
420     WRAP(Table, Container, _GtkTable, (),
421          Table(std::size_t rows, std::size_t columns, bool homogenous);
422     ,
423     );
424
425     WRAP(TextView, Container, _GtkTextView, (),
426          TextView();
427     ,
428     );
429
430     WRAP(Toolbar, Container, _GtkToolbar, (),
431     ,
432     );
433
434     class TreeModel;
435     WRAP(TreeView, Widget, _GtkTreeView, (),
436          TreeView();
437          TreeView(TreeModel model);
438     ,
439     );
440
441     WRAP(Misc, Widget, _GtkMisc, (),
442     ,
443     );
444
445     WRAP(Label, Widget, _GtkLabel, (),
446          Label(const char *label);
447     ,
448     );
449
450     WRAP(Image, Widget, _GtkImage, (),
451          Image();
452     ,
453     );
454
455     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
456          Entry();
457          Entry(std::size_t max_length);
458     ,
459     );
460
461     class Adjustment;
462     WRAP(SpinButton, Entry, _GtkSpinButton, (),
463          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
464     ,
465     );
466
467     WRAP(Range, Widget, _GtkRange, (),
468     ,
469     );
470
471     WRAP(Scale, Range, _GtkScale, (),
472     ,
473     );
474
475     WRAP(HScale, Scale, _GtkHScale, (),
476          HScale(Adjustment adjustment);
477          HScale(double min, double max, double step);
478     ,
479     );
480
481     WRAP(Adjustment, Object, _GtkAdjustment, (),
482          Adjustment(double value,
483                     double lower, double upper,
484                     double step_increment, double page_increment,
485                     double page_size);
486     ,
487     );
488
489     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
490     ,
491     );
492
493     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
494          CellRendererText();
495     ,
496     );
497
498     struct TreeViewColumnAttribute {
499         const char *attribute;
500         int column;
501     };
502     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
503          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
504     ,
505     );
506
507     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
508          AccelGroup();
509     ,
510     );
511
512     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
513     ,
514     );
515
516     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
517     ,
518          void clear();
519     );
520
521     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
522     ,
523     );
524
525     // GBoxed
526
527     WRAP(TreePath, Object, _GtkTreePath, (),
528          TreePath();
529          TreePath(const char *path);
530     ,
531     );
532
533 #undef WRAP
534
535     // callbacks
536
537     namespace {
538         using GtkCallback = void (*)(_GtkWidget *, void *);
539         extern "C" {
540         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
541         }
542     }
543
544 #define this (*static_cast<self>(this))
545
546     template<class Lambda>
547     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
548     {
549         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
550     }
551
552     template<class Lambda>
553     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
554     {
555         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
556     }
557
558     template<class Lambda>
559     void IContainer::foreach(Lambda &&lambda)
560     {
561         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
562             using Function = typename std::decay<Lambda>::type;
563             Function *f = static_cast<Function *>(data);
564             (*f)(Widget(widget));
565         };
566         gtk_container_foreach(this, cb, &lambda);
567     }
568
569 #undef this
570
571 }
572
573 #endif