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