]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
Remove implicit Window constructor
[xonotic/netradiant.git] / libs / uilib / uilib.h
1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
3
4 #include <string>
5
6 struct _GdkEventKey;
7 struct _GtkAccelGroup;
8 struct _GtkAdjustment;
9 struct _GtkAlignment;
10 struct _GtkBin;
11 struct _GtkBox;
12 struct _GtkButton;
13 struct _GtkCellEditable;
14 struct _GtkCellRenderer;
15 struct _GtkCellRendererText;
16 struct _GtkCheckButton;
17 struct _GtkCheckMenuItem;
18 struct _GtkComboBox;
19 struct _GtkComboBoxText;
20 struct _GtkContainer;
21 struct _GtkDialog;
22 struct _GtkEditable;
23 struct _GtkEntry;
24 struct _GtkEntryCompletion;
25 struct _GtkFrame;
26 struct _GtkHBox;
27 struct _GtkHPaned;
28 struct _GtkHScale;
29 struct _GtkImage;
30 struct _GtkItem;
31 struct _GtkLabel;
32 struct _GtkListStore;
33 struct _GtkMenu;
34 struct _GtkMenuBar;
35 struct _GtkMenuItem;
36 struct _GtkMenuShell;
37 struct _GtkMisc;
38 struct _GtkObject;
39 struct _GtkPaned;
40 struct _GtkRadioButton;
41 struct _GtkRadioMenuItem;
42 struct _GtkRadioToolButton;
43 struct _GtkRange;
44 struct _GtkScale;
45 struct _GtkScrolledWindow;
46 struct _GtkSpinButton;
47 struct _GtkTable;
48 struct _GtkTearoffMenuItem;
49 struct _GtkTextView;
50 struct _GtkToggleButton;
51 struct _GtkToggleToolButton;
52 struct _GtkToolbar;
53 struct _GtkToolButton;
54 struct _GtkToolItem;
55 struct _GtkTreeModel;
56 struct _GtkTreePath;
57 struct _GtkTreeView;
58 struct _GtkTreeViewColumn;
59 struct _GtkVBox;
60 struct _GtkVPaned;
61 struct _GtkWidget;
62 struct _GtkWindow;
63 struct _GTypeInstance;
64
65 struct ModalDialog;
66
67 namespace ui {
68
69     void init(int argc, char *argv[]);
70
71     void main();
72
73     void process();
74
75     extern class Widget root;
76
77     enum class alert_type {
78         OK,
79         OKCANCEL,
80         YESNO,
81         YESNOCANCEL,
82         NOYES,
83     };
84
85     enum class alert_icon {
86         Default,
87         Error,
88         Warning,
89         Question,
90         Asterisk,
91     };
92
93     enum class alert_response {
94         OK,
95         CANCEL,
96         YES,
97         NO,
98     };
99
100     enum class window_type {
101         TOP,
102         POPUP
103     };
104
105     namespace details {
106
107         enum class Convert {
108             Implicit, Explicit
109         };
110
111         template<class Self, class T, Convert mode>
112         struct Convertible;
113
114         template<class Self, class T>
115         struct Convertible<Self, T, Convert::Implicit> {
116             operator T() const
117             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
118         };
119
120         template<class Self, class T>
121         struct Convertible<Self, T, Convert::Explicit> {
122             explicit operator T() const
123             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
124         };
125
126         template<class Self, class... T>
127         struct All : T ... {
128             All()
129             {};
130         };
131
132         template<class Self, class Interfaces>
133         struct Mixin;
134         template<class Self>
135         struct Mixin<Self, void()> {
136             using type = All<Self>;
137         };
138         template<class Self, class... Interfaces>
139         struct Mixin<Self, void(Interfaces...)> {
140             using type = All<Self, Interfaces...>;
141         };
142     }
143
144     extern struct Null {} null;
145
146     class Object :
147             public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
148             public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
149     public:
150         using native = _GtkObject *;
151         native _handle;
152
153         Object(native h) : _handle(h)
154         {}
155
156         explicit operator bool() const
157         { return _handle != nullptr; }
158
159         explicit operator void *() const
160         { return _handle; }
161     };
162     static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
163
164 #define WRAP(name, super, T, interfaces, ctors, methods) \
165     class name; \
166     class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
167     public: \
168         using self = name *; \
169         methods \
170     }; \
171     class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
172     public: \
173         using self = name *; \
174         using native = T *; \
175         explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
176         explicit name(Null n) : name((native) nullptr) {} \
177         ctors \
178     }; \
179     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
180     static_assert(sizeof(name) == sizeof(super), "object slicing")
181
182     // https://developer.gnome.org/gtk2/stable/ch01.html
183
184     // GInterface
185
186     WRAP(CellEditable, Object, _GtkCellEditable, (),
187     ,
188     );
189
190     WRAP(Editable, Object, _GtkEditable, (),
191          Editable();
192     ,
193          void editable(bool value);
194     );
195
196     WRAP(TreeModel, Object, _GtkTreeModel, (),
197     ,
198     );
199
200     // GObject
201
202     WRAP(Widget, Object, _GtkWidget, (),
203          Widget();
204     ,
205          alert_response alert(
206                  std::string text,
207                  std::string title = "NetRadiant",
208                  alert_type type = alert_type::OK,
209                  alert_icon icon = alert_icon::Default
210          );
211          const char *file_dialog(
212                  bool open,
213                  const char *title,
214                  const char *path = nullptr,
215                  const char *pattern = nullptr,
216                  bool want_load = false,
217                  bool want_import = false,
218                  bool want_save = false
219          );
220          void show();
221     );
222
223     WRAP(Container, Widget, _GtkContainer, (),
224     ,
225          void add(Widget widget);
226     );
227
228     WRAP(Bin, Container, _GtkBin, (),
229     ,
230     );
231
232     class AccelGroup;
233     WRAP(Window, Bin, _GtkWindow, (),
234          Window(window_type type);
235     ,
236          Window create_dialog_window(
237                  const char *title,
238                  void func(),
239                  void *data,
240                  int default_w = -1,
241                  int default_h = -1
242          );
243
244          Window create_modal_dialog_window(
245                  const char *title,
246                  ModalDialog &dialog,
247                  int default_w = -1,
248                  int default_h = -1
249          );
250
251          Window create_floating_window(const char *title);
252
253          std::uint64_t on_key_press(
254                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
255                  void *extra = nullptr
256          );
257
258          void add_accel_group(AccelGroup group);
259     );
260
261     WRAP(Dialog, Window, _GtkDialog, (),
262     ,
263     );
264
265     WRAP(Alignment, Bin, _GtkAlignment, (),
266          Alignment(float xalign, float yalign, float xscale, float yscale);
267     ,
268     );
269
270     WRAP(Frame, Bin, _GtkFrame, (),
271          Frame(const char *label = nullptr);
272     ,
273     );
274
275     WRAP(Button, Bin, _GtkButton, (),
276          Button();
277          Button(const char *label);
278     ,
279     );
280
281     WRAP(ToggleButton, Button, _GtkToggleButton, (),
282     ,
283          bool active();
284     );
285
286     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
287          CheckButton(const char *label);
288     ,
289     );
290
291     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
292     ,
293     );
294
295     WRAP(Item, Bin, _GtkItem, (),
296     ,
297     );
298
299     WRAP(MenuItem, Item, _GtkMenuItem, (),
300          MenuItem();
301          MenuItem(const char *label, bool mnemonic = false);
302     ,
303     );
304
305     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
306     ,
307     );
308
309     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
310     ,
311     );
312
313     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
314          TearoffMenuItem();
315     ,
316     );
317
318     WRAP(ComboBox, Bin, _GtkComboBox, (),
319     ,
320     );
321
322     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
323          ComboBoxText();
324     ,
325     );
326
327     WRAP(ToolItem, Bin, _GtkToolItem, (),
328     ,
329     );
330
331     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
332     ,
333     );
334
335     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
336     ,
337     );
338
339     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
340     ,
341     );
342
343     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
344          ScrolledWindow();
345     ,
346     );
347
348     WRAP(Box, Container, _GtkBox, (),
349     ,
350     );
351
352     WRAP(VBox, Box, _GtkVBox, (),
353          VBox(bool homogenous, int spacing);
354     ,
355     );
356
357     WRAP(HBox, Box, _GtkHBox, (),
358          HBox(bool homogenous, int spacing);
359     ,
360     );
361
362     WRAP(Paned, Container, _GtkPaned, (),
363     ,
364     );
365
366     WRAP(HPaned, Paned, _GtkHPaned, (),
367          HPaned();
368     ,
369     );
370
371     WRAP(VPaned, Paned, _GtkVPaned, (),
372          VPaned();
373     ,
374     );
375
376     WRAP(MenuShell, Container, _GtkMenuShell, (),
377     ,
378     );
379
380     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
381     ,
382     );
383
384     WRAP(Menu, MenuShell, _GtkMenu, (),
385          Menu();
386     ,
387     );
388
389     WRAP(Table, Container, _GtkTable, (),
390          Table(std::size_t rows, std::size_t columns, bool homogenous);
391     ,
392     );
393
394     WRAP(TextView, Container, _GtkTextView, (),
395          TextView();
396     ,
397     );
398
399     WRAP(Toolbar, Container, _GtkToolbar, (),
400     ,
401     );
402
403     class TreeModel;
404     WRAP(TreeView, Widget, _GtkTreeView, (),
405          TreeView();
406          TreeView(TreeModel model);
407     ,
408     );
409
410     WRAP(Misc, Widget, _GtkMisc, (),
411     ,
412     );
413
414     WRAP(Label, Widget, _GtkLabel, (),
415          Label(const char *label);
416     ,
417     );
418
419     WRAP(Image, Widget, _GtkImage, (),
420          Image();
421     ,
422     );
423
424     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
425          Entry();
426          Entry(std::size_t max_length);
427     ,
428     );
429
430     class Adjustment;
431     WRAP(SpinButton, Entry, _GtkSpinButton, (),
432          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
433     ,
434     );
435
436     WRAP(Range, Widget, _GtkRange, (),
437     ,
438     );
439
440     WRAP(Scale, Range, _GtkScale, (),
441     ,
442     );
443
444     WRAP(HScale, Scale, _GtkHScale, (),
445          HScale(Adjustment adjustment);
446          HScale(double min, double max, double step);
447     ,
448     );
449
450     WRAP(Adjustment, Object, _GtkAdjustment, (),
451          Adjustment(double value,
452                     double lower, double upper,
453                     double step_increment, double page_increment,
454                     double page_size);
455     ,
456     );
457
458     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
459     ,
460     );
461
462     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
463          CellRendererText();
464     ,
465     );
466
467     struct TreeViewColumnAttribute {
468         const char *attribute;
469         int column;
470     };
471     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
472          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
473     ,
474     );
475
476     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
477          AccelGroup();
478     ,
479     );
480
481     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
482     ,
483     );
484
485     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
486     ,
487          void clear();
488     );
489
490     // GBoxed
491
492     WRAP(TreePath, Object, _GtkTreePath, (),
493          TreePath();
494          TreePath(const char *path);
495     ,
496     );
497
498 #undef WRAP
499
500 }
501
502 #endif