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