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