]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
5d8a48fb11a7ff6f88f63e4fb5ebd9800ec634ff
[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     );
399
400     WRAP(VBox, Box, _GtkVBox, (),
401          VBox(bool homogenous, int spacing);
402     ,
403     );
404
405     WRAP(HBox, Box, _GtkHBox, (),
406          HBox(bool homogenous, int spacing);
407     ,
408     );
409
410     WRAP(Paned, Container, _GtkPaned, (),
411     ,
412     );
413
414     WRAP(HPaned, Paned, _GtkHPaned, (),
415     ,
416     );
417
418     WRAP(VPaned, Paned, _GtkVPaned, (),
419     ,
420     );
421
422     WRAP(MenuShell, Container, _GtkMenuShell, (),
423     ,
424     );
425
426     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
427     ,
428     );
429
430     WRAP(Menu, MenuShell, _GtkMenu, (),
431     ,
432     );
433
434     WRAP(Table, Container, _GtkTable, (),
435          Table(std::size_t rows, std::size_t columns, bool homogenous);
436     ,
437     );
438
439     WRAP(TextView, Container, _GtkTextView, (),
440     ,
441          void text(char const *str);
442     );
443
444     WRAP(Toolbar, Container, _GtkToolbar, (),
445     ,
446     );
447
448     class TreeModel;
449     WRAP(TreeView, Widget, _GtkTreeView, (),
450          TreeView(TreeModel model);
451     ,
452     );
453
454     WRAP(Misc, Widget, _GtkMisc, (),
455     ,
456     );
457
458     WRAP(Label, Widget, _GtkLabel, (),
459          explicit Label(const char *label);
460     ,
461          void text(char const *str);
462     );
463
464     WRAP(Image, Widget, _GtkImage, (),
465     ,
466     );
467
468     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
469          explicit Entry(std::size_t max_length);
470     ,
471         char const *text();
472         void text(char const *str);
473     );
474
475     class Adjustment;
476     WRAP(SpinButton, Entry, _GtkSpinButton, (),
477          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
478     ,
479     );
480
481     WRAP(Range, Widget, _GtkRange, (),
482     ,
483     );
484
485     WRAP(Scale, Range, _GtkScale, (),
486     ,
487     );
488
489     WRAP(HScale, Scale, _GtkHScale, (),
490          explicit HScale(Adjustment adjustment);
491          HScale(double min, double max, double step);
492     ,
493     );
494
495     WRAP(Adjustment, Object, _GtkAdjustment, (),
496          Adjustment(double value,
497                     double lower, double upper,
498                     double step_increment, double page_increment,
499                     double page_size);
500     ,
501     );
502
503     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
504     ,
505     );
506
507     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
508     ,
509     );
510
511     struct TreeViewColumnAttribute {
512         const char *attribute;
513         int column;
514     };
515     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
516          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
517     ,
518     );
519
520     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
521     ,
522     );
523
524     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
525     ,
526     );
527
528     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
529     ,
530          void clear();
531
532          template<class... T>
533          void append(T... args);
534
535          void append();
536     );
537
538     WRAP(TreeSelection, Object, _GtkTreeSelection, (),
539     ,
540     );
541
542     // GBoxed
543
544     WRAP(TreePath, Object, _GtkTreePath, (),
545          explicit TreePath(const char *path);
546     ,
547     );
548
549     // Custom
550
551     WRAP(GLArea, Widget, _GtkGLArea, (),
552     ,
553          guint on_render(GCallback pFunction, void *data);
554     );
555
556 #undef WRAP
557
558     // callbacks
559
560     namespace {
561         using GtkCallback = void (*)(_GtkWidget *, void *);
562         extern "C" {
563         void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
564         }
565     }
566
567 #define this (*static_cast<self>(this))
568
569     template<class Lambda>
570     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
571     {
572         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
573     }
574
575     template<class Lambda>
576     gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
577     {
578         return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
579     }
580
581     template<class Lambda>
582     void IContainer::foreach(Lambda &&lambda)
583     {
584         GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
585             using Function = typename std::decay<Lambda>::type;
586             Function *f = static_cast<Function *>(data);
587             (*f)(Widget(widget));
588         };
589         gtk_container_foreach(this, cb, &lambda);
590     }
591
592     namespace {
593         extern "C" {
594         void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
595         }
596     }
597
598     template<class... T>
599     void IListStore::append(T... args) {
600         static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
601         gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);
602     }
603
604 #undef this
605
606 }
607
608 #endif