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