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