]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
Wrap more GTK
[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();
342     );
343
344     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
345          explicit CheckButton(const char *label);
346     ,
347     );
348
349     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
350     ,
351     );
352
353     WRAP(Item, Bin, _GtkItem, (),
354     ,
355     );
356
357     WRAP(MenuItem, Item, _GtkMenuItem, (),
358          explicit MenuItem(const char *label, bool mnemonic = false);
359     ,
360     );
361
362     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
363     ,
364     );
365
366     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
367     ,
368     );
369
370     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
371     ,
372     );
373
374     WRAP(ComboBox, Bin, _GtkComboBox, (),
375     ,
376     );
377
378     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
379     ,
380     );
381
382     WRAP(ToolItem, Bin, _GtkToolItem, (),
383     ,
384     );
385
386     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
387     ,
388     );
389
390     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
391     ,
392     );
393
394     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
395     ,
396     );
397
398     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
399     ,
400          void overflow(Policy x, Policy y);
401     );
402
403     WRAP(Box, Container, _GtkBox, (),
404     ,
405          void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
406          void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
407     );
408
409     WRAP(VBox, Box, _GtkVBox, (),
410          VBox(bool homogenous, int spacing);
411     ,
412     );
413
414     WRAP(HBox, Box, _GtkHBox, (),
415          HBox(bool homogenous, int spacing);
416     ,
417     );
418
419     WRAP(Paned, Container, _GtkPaned, (),
420     ,
421     );
422
423     WRAP(HPaned, Paned, _GtkHPaned, (),
424     ,
425     );
426
427     WRAP(VPaned, Paned, _GtkVPaned, (),
428     ,
429     );
430
431     WRAP(MenuShell, Container, _GtkMenuShell, (),
432     ,
433     );
434
435     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
436     ,
437     );
438
439     WRAP(Menu, MenuShell, _GtkMenu, (),
440     ,
441     );
442
443     struct TableAttach {
444         unsigned int left, right, top, bottom;
445     };
446
447     struct TableAttachOptions {
448         // todo: type safety
449         unsigned int x, y;
450     };
451
452     struct TablePadding {
453         unsigned int x, y;
454     };
455
456     WRAP(Table, Container, _GtkTable, (),
457          Table(std::size_t rows, std::size_t columns, bool homogenous);
458     ,
459          // 5 = expand | fill
460          void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
461     );
462
463     WRAP(TextView, Container, _GtkTextView, (),
464     ,
465          void text(char const *str);
466     );
467
468     WRAP(Toolbar, Container, _GtkToolbar, (),
469     ,
470     );
471
472     class TreeModel;
473     WRAP(TreeView, Widget, _GtkTreeView, (),
474          TreeView(TreeModel model);
475     ,
476     );
477
478     WRAP(Misc, Widget, _GtkMisc, (),
479     ,
480     );
481
482     WRAP(Label, Widget, _GtkLabel, (),
483          explicit Label(const char *label);
484     ,
485          void text(char const *str);
486     );
487
488     WRAP(Image, Widget, _GtkImage, (),
489     ,
490     );
491
492     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
493          explicit Entry(std::size_t max_length);
494     ,
495         char const *text();
496         void text(char const *str);
497     );
498
499     class Adjustment;
500     WRAP(SpinButton, Entry, _GtkSpinButton, (),
501          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
502     ,
503     );
504
505     WRAP(Range, Widget, _GtkRange, (),
506     ,
507     );
508
509     WRAP(Scale, Range, _GtkScale, (),
510     ,
511     );
512
513     WRAP(HScale, Scale, _GtkHScale, (),
514          explicit HScale(Adjustment adjustment);
515          HScale(double min, double max, double step);
516     ,
517     );
518
519     WRAP(Adjustment, Object, _GtkAdjustment, (),
520          Adjustment(double value,
521                     double lower, double upper,
522                     double step_increment, double page_increment,
523                     double page_size);
524     ,
525     );
526
527     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
528     ,
529     );
530
531     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
532     ,
533     );
534
535     struct TreeViewColumnAttribute {
536         const char *attribute;
537         int column;
538     };
539     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
540          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
541     ,
542     );
543
544     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
545     ,
546     );
547
548     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
549     ,
550     );
551
552     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
553     ,
554          void clear();
555
556          template<class... T>
557          void append(T... args);
558
559          void append();
560     );
561
562     WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
563     ,
564     );
565
566     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
567     ,
568     );
569
570     // GBoxed
571
572     WRAP(TreePath, Object, _GtkTreePath, (),
573          explicit TreePath(const char *path);
574     ,
575     );
576
577     // Custom
578
579     WRAP(GLArea, Widget, _GtkGLArea, (),
580     ,
581          guint on_render(GCallback pFunction, void *data);
582     );
583
584 #undef WRAP
585
586     // callbacks
587
588     namespace {
589         using GtkCallback = void (*)(_GtkWidget *, void *);
590         extern "C" {
591         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
592         }
593     }
594
595 #define this (*static_cast<self>(this))
596
597     template<class Lambda>
598     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
599     {
600         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
601     }
602
603     template<class Lambda>
604     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
605     {
606         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
607     }
608
609     template<class Lambda>
610     void IContainer::foreach(Lambda &&lambda)
611     {
612         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
613             using Function = typename std::decay<Lambda>::type;
614             Function *f = static_cast<Function *>(data);
615             (*f)(Widget(widget));
616         };
617         gtk_container_foreach(this, cb, &lambda);
618     }
619
620     namespace {
621         extern "C" {
622         void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
623         }
624     }
625
626     template<class... T>
627     void IListStore::append(T... args) {
628         static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
629         gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
630     }
631
632 #undef this
633
634 }
635
636 #endif