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