]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
GTK: wrap gtk_box_pack_start
[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         template<class Lambda>
193         gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
194
195         template<class Lambda>
196         gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
197     };
198     static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
199
200 #define WRAP(name, super, T, interfaces, ctors, methods) \
201     class name; \
202     class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
203     public: \
204         using self = name *; \
205         methods \
206     }; \
207     class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
208     public: \
209         using self = name *; \
210         using native = T *; \
211         explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
212         explicit name(Null n) : name((native) nullptr) {} \
213         explicit name(New_t); \
214         static name from(void *ptr) { return name((native) ptr); } \
215         ctors \
216     }; \
217     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
218     static_assert(sizeof(name) == sizeof(super), "object slicing")
219
220     // https://developer.gnome.org/gtk2/stable/ch01.html
221
222     // GInterface
223
224     WRAP(CellEditable, Object, _GtkCellEditable, (),
225     ,
226     );
227
228     WRAP(Editable, Object, _GtkEditable, (),
229     ,
230          void editable(bool value);
231     );
232
233     WRAP(TreeModel, Object, _GtkTreeModel, (),
234     ,
235     );
236
237     // GObject
238
239     struct Dimensions {
240         int width;
241         int height;
242     };
243
244     WRAP(Widget, Object, _GtkWidget, (),
245     ,
246          alert_response alert(
247                  std::string text,
248                  std::string title = "NetRadiant",
249                  alert_type type = alert_type::OK,
250                  alert_icon icon = alert_icon::Default
251          );
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          Window create_dialog_window(
289                  const char *title,
290                  void func(),
291                  void *data,
292                  int default_w = -1,
293                  int default_h = -1
294          );
295
296          Window create_modal_dialog_window(
297                  const char *title,
298                  ModalDialog &dialog,
299                  int default_w = -1,
300                  int default_h = -1
301          );
302
303          Window create_floating_window(const char *title);
304
305          std::uint64_t on_key_press(
306                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
307                  void *extra = nullptr
308          );
309
310          void add_accel_group(AccelGroup group);
311     );
312
313     WRAP(Dialog, Window, _GtkDialog, (),
314     ,
315     );
316
317     WRAP(Alignment, Bin, _GtkAlignment, (),
318          Alignment(float xalign, float yalign, float xscale, float yscale);
319     ,
320     );
321
322     WRAP(Frame, Bin, _GtkFrame, (),
323          explicit Frame(const char *label = nullptr);
324     ,
325     );
326
327     WRAP(Button, Bin, _GtkButton, (),
328          explicit Button(const char *label);
329     ,
330     );
331
332     WRAP(ToggleButton, Button, _GtkToggleButton, (),
333     ,
334          bool active();
335     );
336
337     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
338          explicit CheckButton(const char *label);
339     ,
340     );
341
342     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
343     ,
344     );
345
346     WRAP(Item, Bin, _GtkItem, (),
347     ,
348     );
349
350     WRAP(MenuItem, Item, _GtkMenuItem, (),
351          explicit MenuItem(const char *label, bool mnemonic = false);
352     ,
353     );
354
355     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
356     ,
357     );
358
359     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
360     ,
361     );
362
363     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
364     ,
365     );
366
367     WRAP(ComboBox, Bin, _GtkComboBox, (),
368     ,
369     );
370
371     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
372     ,
373     );
374
375     WRAP(ToolItem, Bin, _GtkToolItem, (),
376     ,
377     );
378
379     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
380     ,
381     );
382
383     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
384     ,
385     );
386
387     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
388     ,
389     );
390
391     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
392     ,
393          void overflow(Policy x, Policy y);
394     );
395
396     WRAP(Box, Container, _GtkBox, (),
397     ,
398          void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
399          void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
400     );
401
402     WRAP(VBox, Box, _GtkVBox, (),
403          VBox(bool homogenous, int spacing);
404     ,
405     );
406
407     WRAP(HBox, Box, _GtkHBox, (),
408          HBox(bool homogenous, int spacing);
409     ,
410     );
411
412     WRAP(Paned, Container, _GtkPaned, (),
413     ,
414     );
415
416     WRAP(HPaned, Paned, _GtkHPaned, (),
417     ,
418     );
419
420     WRAP(VPaned, Paned, _GtkVPaned, (),
421     ,
422     );
423
424     WRAP(MenuShell, Container, _GtkMenuShell, (),
425     ,
426     );
427
428     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
429     ,
430     );
431
432     WRAP(Menu, MenuShell, _GtkMenu, (),
433     ,
434     );
435
436     WRAP(Table, Container, _GtkTable, (),
437          Table(std::size_t rows, std::size_t columns, bool homogenous);
438     ,
439     );
440
441     WRAP(TextView, Container, _GtkTextView, (),
442     ,
443          void text(char const *str);
444     );
445
446     WRAP(Toolbar, Container, _GtkToolbar, (),
447     ,
448     );
449
450     class TreeModel;
451     WRAP(TreeView, Widget, _GtkTreeView, (),
452          TreeView(TreeModel model);
453     ,
454     );
455
456     WRAP(Misc, Widget, _GtkMisc, (),
457     ,
458     );
459
460     WRAP(Label, Widget, _GtkLabel, (),
461          explicit Label(const char *label);
462     ,
463          void text(char const *str);
464     );
465
466     WRAP(Image, Widget, _GtkImage, (),
467     ,
468     );
469
470     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
471          explicit Entry(std::size_t max_length);
472     ,
473         char const *text();
474         void text(char const *str);
475     );
476
477     class Adjustment;
478     WRAP(SpinButton, Entry, _GtkSpinButton, (),
479          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
480     ,
481     );
482
483     WRAP(Range, Widget, _GtkRange, (),
484     ,
485     );
486
487     WRAP(Scale, Range, _GtkScale, (),
488     ,
489     );
490
491     WRAP(HScale, Scale, _GtkHScale, (),
492          explicit HScale(Adjustment adjustment);
493          HScale(double min, double max, double step);
494     ,
495     );
496
497     WRAP(Adjustment, Object, _GtkAdjustment, (),
498          Adjustment(double value,
499                     double lower, double upper,
500                     double step_increment, double page_increment,
501                     double page_size);
502     ,
503     );
504
505     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
506     ,
507     );
508
509     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
510     ,
511     );
512
513     struct TreeViewColumnAttribute {
514         const char *attribute;
515         int column;
516     };
517     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
518          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
519     ,
520     );
521
522     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
523     ,
524     );
525
526     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
527     ,
528     );
529
530     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
531     ,
532          void clear();
533
534          template<class... T>
535          void append(T... args);
536
537          void append();
538     );
539
540     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
541     ,
542     );
543
544     // GBoxed
545
546     WRAP(TreePath, Object, _GtkTreePath, (),
547          explicit TreePath(const char *path);
548     ,
549     );
550
551     // Custom
552
553     WRAP(GLArea, Widget, _GtkGLArea, (),
554     ,
555          guint on_render(GCallback pFunction, void *data);
556     );
557
558 #undef WRAP
559
560     // callbacks
561
562     namespace {
563         using GtkCallback = void (*)(_GtkWidget *, void *);
564         extern "C" {
565         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
566         }
567     }
568
569 #define this (*static_cast<self>(this))
570
571     template<class Lambda>
572     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
573     {
574         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
575     }
576
577     template<class Lambda>
578     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
579     {
580         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
581     }
582
583     template<class Lambda>
584     void IContainer::foreach(Lambda &&lambda)
585     {
586         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
587             using Function = typename std::decay<Lambda>::type;
588             Function *f = static_cast<Function *>(data);
589             (*f)(Widget(widget));
590         };
591         gtk_container_foreach(this, cb, &lambda);
592     }
593
594     namespace {
595         extern "C" {
596         void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
597         }
598     }
599
600     template<class... T>
601     void IListStore::append(T... args) {
602         static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
603         gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
604     }
605
606 #undef this
607
608 }
609
610 #endif