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