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