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