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