]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
GTK: wrap gtk_table_attach
[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     class Window;
248     WRAP(Widget, Object, _GtkWidget, (),
249     ,
250          Window window();
251          const char *file_dialog(
252                  bool open,
253                  const char *title,
254                  const char *path = nullptr,
255                  const char *pattern = nullptr,
256                  bool want_load = false,
257                  bool want_import = false,
258                  bool want_save = false
259          );
260          bool visible();
261          void visible(bool shown);
262          void show();
263          void hide();
264          Dimensions dimensions();
265          void dimensions(int width, int height);
266          void destroy();
267     );
268
269     WRAP(Container, Widget, _GtkContainer, (),
270     ,
271          void add(Widget widget);
272
273          void remove(Widget widget);
274
275          template<class Lambda>
276          void foreach(Lambda &&lambda);
277     );
278
279     WRAP(Bin, Container, _GtkBin, (),
280     ,
281     );
282
283     class AccelGroup;
284     WRAP(Window, Bin, _GtkWindow, (),
285          explicit Window(window_type type);
286     ,
287          alert_response alert(
288                  std::string text,
289                  std::string title = "NetRadiant",
290                  alert_type type = alert_type::OK,
291                  alert_icon icon = alert_icon::Default
292          );
293
294          Window create_dialog_window(
295                  const char *title,
296                  void func(),
297                  void *data,
298                  int default_w = -1,
299                  int default_h = -1
300          );
301
302          Window create_modal_dialog_window(
303                  const char *title,
304                  ModalDialog &dialog,
305                  int default_w = -1,
306                  int default_h = -1
307          );
308
309          Window create_floating_window(const char *title);
310
311          std::uint64_t on_key_press(
312                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
313                  void *extra = nullptr
314          );
315
316          void add_accel_group(AccelGroup group);
317     );
318
319     WRAP(Dialog, Window, _GtkDialog, (),
320     ,
321     );
322
323     WRAP(Alignment, Bin, _GtkAlignment, (),
324          Alignment(float xalign, float yalign, float xscale, float yscale);
325     ,
326     );
327
328     WRAP(Frame, Bin, _GtkFrame, (),
329          explicit Frame(const char *label = nullptr);
330     ,
331     );
332
333     WRAP(Button, Bin, _GtkButton, (),
334          explicit Button(const char *label);
335     ,
336     );
337
338     WRAP(ToggleButton, Button, _GtkToggleButton, (),
339     ,
340          bool active();
341     );
342
343     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
344          explicit CheckButton(const char *label);
345     ,
346     );
347
348     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
349     ,
350     );
351
352     WRAP(Item, Bin, _GtkItem, (),
353     ,
354     );
355
356     WRAP(MenuItem, Item, _GtkMenuItem, (),
357          explicit MenuItem(const char *label, bool mnemonic = false);
358     ,
359     );
360
361     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
362     ,
363     );
364
365     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
366     ,
367     );
368
369     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
370     ,
371     );
372
373     WRAP(ComboBox, Bin, _GtkComboBox, (),
374     ,
375     );
376
377     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
378     ,
379     );
380
381     WRAP(ToolItem, Bin, _GtkToolItem, (),
382     ,
383     );
384
385     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
386     ,
387     );
388
389     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
390     ,
391     );
392
393     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
394     ,
395     );
396
397     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
398     ,
399          void overflow(Policy x, Policy y);
400     );
401
402     WRAP(Box, Container, _GtkBox, (),
403     ,
404          void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
405          void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
406     );
407
408     WRAP(VBox, Box, _GtkVBox, (),
409          VBox(bool homogenous, int spacing);
410     ,
411     );
412
413     WRAP(HBox, Box, _GtkHBox, (),
414          HBox(bool homogenous, int spacing);
415     ,
416     );
417
418     WRAP(Paned, Container, _GtkPaned, (),
419     ,
420     );
421
422     WRAP(HPaned, Paned, _GtkHPaned, (),
423     ,
424     );
425
426     WRAP(VPaned, Paned, _GtkVPaned, (),
427     ,
428     );
429
430     WRAP(MenuShell, Container, _GtkMenuShell, (),
431     ,
432     );
433
434     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
435     ,
436     );
437
438     WRAP(Menu, MenuShell, _GtkMenu, (),
439     ,
440     );
441
442     struct TableAttach {
443         unsigned int left, right, top, bottom;
444     };
445
446     struct TableAttachOptions {
447         // todo: type safety
448         unsigned int x, y;
449     };
450
451     struct TablePadding {
452         unsigned int x, y;
453     };
454
455     WRAP(Table, Container, _GtkTable, (),
456          Table(std::size_t rows, std::size_t columns, bool homogenous);
457     ,
458          // 5 = expand | fill
459          void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
460     );
461
462     WRAP(TextView, Container, _GtkTextView, (),
463     ,
464          void text(char const *str);
465     );
466
467     WRAP(Toolbar, Container, _GtkToolbar, (),
468     ,
469     );
470
471     class TreeModel;
472     WRAP(TreeView, Widget, _GtkTreeView, (),
473          TreeView(TreeModel model);
474     ,
475     );
476
477     WRAP(Misc, Widget, _GtkMisc, (),
478     ,
479     );
480
481     WRAP(Label, Widget, _GtkLabel, (),
482          explicit Label(const char *label);
483     ,
484          void text(char const *str);
485     );
486
487     WRAP(Image, Widget, _GtkImage, (),
488     ,
489     );
490
491     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
492          explicit Entry(std::size_t max_length);
493     ,
494         char const *text();
495         void text(char const *str);
496     );
497
498     class Adjustment;
499     WRAP(SpinButton, Entry, _GtkSpinButton, (),
500          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
501     ,
502     );
503
504     WRAP(Range, Widget, _GtkRange, (),
505     ,
506     );
507
508     WRAP(Scale, Range, _GtkScale, (),
509     ,
510     );
511
512     WRAP(HScale, Scale, _GtkHScale, (),
513          explicit HScale(Adjustment adjustment);
514          HScale(double min, double max, double step);
515     ,
516     );
517
518     WRAP(Adjustment, Object, _GtkAdjustment, (),
519          Adjustment(double value,
520                     double lower, double upper,
521                     double step_increment, double page_increment,
522                     double page_size);
523     ,
524     );
525
526     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
527     ,
528     );
529
530     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
531     ,
532     );
533
534     struct TreeViewColumnAttribute {
535         const char *attribute;
536         int column;
537     };
538     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
539          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
540     ,
541     );
542
543     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
544     ,
545     );
546
547     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
548     ,
549     );
550
551     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
552     ,
553          void clear();
554
555          template<class... T>
556          void append(T... args);
557
558          void append();
559     );
560
561     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
562     ,
563     );
564
565     // GBoxed
566
567     WRAP(TreePath, Object, _GtkTreePath, (),
568          explicit TreePath(const char *path);
569     ,
570     );
571
572     // Custom
573
574     WRAP(GLArea, Widget, _GtkGLArea, (),
575     ,
576          guint on_render(GCallback pFunction, void *data);
577     );
578
579 #undef WRAP
580
581     // callbacks
582
583     namespace {
584         using GtkCallback = void (*)(_GtkWidget *, void *);
585         extern "C" {
586         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
587         }
588     }
589
590 #define this (*static_cast<self>(this))
591
592     template<class Lambda>
593     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
594     {
595         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
596     }
597
598     template<class Lambda>
599     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
600     {
601         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
602     }
603
604     template<class Lambda>
605     void IContainer::foreach(Lambda &&lambda)
606     {
607         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
608             using Function = typename std::decay<Lambda>::type;
609             Function *f = static_cast<Function *>(data);
610             (*f)(Widget(widget));
611         };
612         gtk_container_foreach(this, cb, &lambda);
613     }
614
615     namespace {
616         extern "C" {
617         void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
618         }
619     }
620
621     template<class... T>
622     void IListStore::append(T... args) {
623         static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
624         gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
625     }
626
627 #undef this
628
629 }
630
631 #endif