]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
Purge gtk_container_add
[xonotic/netradiant.git] / libs / uilib / uilib.h
1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
3
4 #include <string>
5
6 struct _GdkEventKey;
7 struct _GtkAccelGroup;
8 struct _GtkAdjustment;
9 struct _GtkAlignment;
10 struct _GtkBin;
11 struct _GtkBox;
12 struct _GtkButton;
13 struct _GtkCellEditable;
14 struct _GtkCellRenderer;
15 struct _GtkCellRendererText;
16 struct _GtkCheckButton;
17 struct _GtkCheckMenuItem;
18 struct _GtkComboBox;
19 struct _GtkComboBoxText;
20 struct _GtkContainer;
21 struct _GtkDialog;
22 struct _GtkEditable;
23 struct _GtkEntry;
24 struct _GtkEntryCompletion;
25 struct _GtkFrame;
26 struct _GtkHBox;
27 struct _GtkHPaned;
28 struct _GtkHScale;
29 struct _GtkImage;
30 struct _GtkItem;
31 struct _GtkLabel;
32 struct _GtkListStore;
33 struct _GtkMenu;
34 struct _GtkMenuBar;
35 struct _GtkMenuItem;
36 struct _GtkMenuShell;
37 struct _GtkMisc;
38 struct _GtkObject;
39 struct _GtkPaned;
40 struct _GtkRadioButton;
41 struct _GtkRadioMenuItem;
42 struct _GtkRadioToolButton;
43 struct _GtkRange;
44 struct _GtkScale;
45 struct _GtkScrolledWindow;
46 struct _GtkSpinButton;
47 struct _GtkTable;
48 struct _GtkTearoffMenuItem;
49 struct _GtkTextView;
50 struct _GtkToggleButton;
51 struct _GtkToggleToolButton;
52 struct _GtkToolbar;
53 struct _GtkToolButton;
54 struct _GtkToolItem;
55 struct _GtkTreeModel;
56 struct _GtkTreePath;
57 struct _GtkTreeView;
58 struct _GtkTreeViewColumn;
59 struct _GtkVBox;
60 struct _GtkVPaned;
61 struct _GtkWidget;
62 struct _GtkWindow;
63 struct _GTypeInstance;
64
65 struct ModalDialog;
66
67 namespace ui {
68
69     void init(int argc, char *argv[]);
70
71     void main();
72
73     void process();
74
75     extern class Widget root;
76
77     enum class alert_type {
78         OK,
79         OKCANCEL,
80         YESNO,
81         YESNOCANCEL,
82         NOYES,
83     };
84
85     enum class alert_icon {
86         Default,
87         Error,
88         Warning,
89         Question,
90         Asterisk,
91     };
92
93     enum class alert_response {
94         OK,
95         CANCEL,
96         YES,
97         NO,
98     };
99
100     enum class window_type {
101         TOP,
102         POPUP
103     };
104
105     namespace details {
106
107         enum class Convert {
108             Implicit, Explicit
109         };
110
111         template<class Self, class T, Convert mode>
112         struct Convertible;
113
114         template<class Self, class T>
115         struct Convertible<Self, T, Convert::Implicit> {
116             operator T() const
117             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
118         };
119
120         template<class Self, class T>
121         struct Convertible<Self, T, Convert::Explicit> {
122             explicit operator T() const
123             { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
124         };
125
126         template<class Self, class... T>
127         struct All : T ... {
128             All()
129             {};
130         };
131
132         template<class Self, class Interfaces>
133         struct Mixin;
134         template<class Self>
135         struct Mixin<Self, void()> {
136             using type = All<Self>;
137         };
138         template<class Self, class... Interfaces>
139         struct Mixin<Self, void(Interfaces...)> {
140             using type = All<Self, Interfaces...>;
141         };
142     }
143
144     extern struct Null {} null;
145
146     class Object :
147             public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
148             public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
149     public:
150         using native = _GtkObject *;
151         native _handle;
152
153         Object(native h) : _handle(h)
154         {}
155
156         explicit operator bool() const
157         { return _handle != nullptr; }
158
159         explicit operator void *() const
160         { return _handle; }
161     };
162     static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
163
164 #define WRAP(name, super, T, interfaces, ctors, methods) \
165     class name; \
166     class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
167     public: \
168         using self = name *; \
169         methods \
170     }; \
171     class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
172     public: \
173         using self = name *; \
174         using native = T *; \
175         explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
176         explicit name(Null n) : name((native) nullptr) {} \
177         ctors \
178     }; \
179     inline bool operator<(name self, name other) { return self._handle < other._handle; } \
180     static_assert(sizeof(name) == sizeof(super), "object slicing")
181
182     // https://developer.gnome.org/gtk2/stable/ch01.html
183
184     // GInterface
185
186     WRAP(CellEditable, Object, _GtkCellEditable, (),
187     ,
188     );
189
190     WRAP(Editable, Object, _GtkEditable, (),
191          Editable();
192     ,
193          void editable(bool value);
194     );
195
196     WRAP(TreeModel, Object, _GtkTreeModel, (),
197     ,
198     );
199
200     // GObject
201
202     WRAP(Widget, Object, _GtkWidget, (),
203          Widget();
204     ,
205          alert_response alert(
206                  std::string text,
207                  std::string title = "NetRadiant",
208                  alert_type type = alert_type::OK,
209                  alert_icon icon = alert_icon::Default
210          );
211          const char *file_dialog(
212                  bool open,
213                  const char *title,
214                  const char *path = nullptr,
215                  const char *pattern = nullptr,
216                  bool want_load = false,
217                  bool want_import = false,
218                  bool want_save = false
219          );
220          void show();
221     );
222
223     WRAP(Container, Widget, _GtkContainer, (),
224     ,
225          void add(Widget widget);
226     );
227
228     WRAP(Bin, Container, _GtkBin, (),
229     ,
230     );
231
232     class AccelGroup;
233     WRAP(Window, Bin, _GtkWindow, (),
234          Window();
235          Window(window_type type);
236     ,
237          Window create_dialog_window(
238                  const char *title,
239                  void func(),
240                  void *data,
241                  int default_w = -1,
242                  int default_h = -1
243          );
244
245          Window create_modal_dialog_window(
246                  const char *title,
247                  ModalDialog &dialog,
248                  int default_w = -1,
249                  int default_h = -1
250          );
251
252          Window create_floating_window(const char *title);
253
254          std::uint64_t on_key_press(
255                  bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
256                  void *extra = nullptr
257          );
258
259          void add_accel_group(AccelGroup group);
260     );
261
262     WRAP(Dialog, Window, _GtkDialog, (),
263     ,
264     );
265
266     WRAP(Alignment, Bin, _GtkAlignment, (),
267          Alignment(float xalign, float yalign, float xscale, float yscale);
268     ,
269     );
270
271     WRAP(Frame, Bin, _GtkFrame, (),
272          Frame(const char *label = nullptr);
273     ,
274     );
275
276     WRAP(Button, Bin, _GtkButton, (),
277          Button();
278          Button(const char *label);
279     ,
280     );
281
282     WRAP(ToggleButton, Button, _GtkToggleButton, (),
283     ,
284          bool active();
285     );
286
287     WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
288          CheckButton(const char *label);
289     ,
290     );
291
292     WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
293     ,
294     );
295
296     WRAP(Item, Bin, _GtkItem, (),
297     ,
298     );
299
300     WRAP(MenuItem, Item, _GtkMenuItem, (),
301          MenuItem();
302          MenuItem(const char *label, bool mnemonic = false);
303     ,
304     );
305
306     WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
307     ,
308     );
309
310     WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
311     ,
312     );
313
314     WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
315          TearoffMenuItem();
316     ,
317     );
318
319     WRAP(ComboBox, Bin, _GtkComboBox, (),
320     ,
321     );
322
323     WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
324          ComboBoxText();
325     ,
326     );
327
328     WRAP(ToolItem, Bin, _GtkToolItem, (),
329     ,
330     );
331
332     WRAP(ToolButton, ToolItem, _GtkToolButton, (),
333     ,
334     );
335
336     WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
337     ,
338     );
339
340     WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
341     ,
342     );
343
344     WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
345          ScrolledWindow();
346     ,
347     );
348
349     WRAP(Box, Container, _GtkBox, (),
350     ,
351     );
352
353     WRAP(VBox, Box, _GtkVBox, (),
354          VBox(bool homogenous, int spacing);
355     ,
356     );
357
358     WRAP(HBox, Box, _GtkHBox, (),
359          HBox(bool homogenous, int spacing);
360     ,
361     );
362
363     WRAP(Paned, Container, _GtkPaned, (),
364     ,
365     );
366
367     WRAP(HPaned, Paned, _GtkHPaned, (),
368          HPaned();
369     ,
370     );
371
372     WRAP(VPaned, Paned, _GtkVPaned, (),
373          VPaned();
374     ,
375     );
376
377     WRAP(MenuShell, Container, _GtkMenuShell, (),
378     ,
379     );
380
381     WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
382     ,
383     );
384
385     WRAP(Menu, MenuShell, _GtkMenu, (),
386          Menu();
387     ,
388     );
389
390     WRAP(Table, Container, _GtkTable, (),
391          Table(std::size_t rows, std::size_t columns, bool homogenous);
392     ,
393     );
394
395     WRAP(TextView, Container, _GtkTextView, (),
396          TextView();
397     ,
398     );
399
400     WRAP(Toolbar, Container, _GtkToolbar, (),
401     ,
402     );
403
404     class TreeModel;
405     WRAP(TreeView, Widget, _GtkTreeView, (),
406          TreeView();
407          TreeView(TreeModel model);
408     ,
409     );
410
411     WRAP(Misc, Widget, _GtkMisc, (),
412     ,
413     );
414
415     WRAP(Label, Widget, _GtkLabel, (),
416          Label(const char *label);
417     ,
418     );
419
420     WRAP(Image, Widget, _GtkImage, (),
421          Image();
422     ,
423     );
424
425     WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
426          Entry();
427          Entry(std::size_t max_length);
428     ,
429     );
430
431     class Adjustment;
432     WRAP(SpinButton, Entry, _GtkSpinButton, (),
433          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
434     ,
435     );
436
437     WRAP(Range, Widget, _GtkRange, (),
438     ,
439     );
440
441     WRAP(Scale, Range, _GtkScale, (),
442     ,
443     );
444
445     WRAP(HScale, Scale, _GtkHScale, (),
446          HScale(Adjustment adjustment);
447          HScale(double min, double max, double step);
448     ,
449     );
450
451     WRAP(Adjustment, Object, _GtkAdjustment, (),
452          Adjustment(double value,
453                     double lower, double upper,
454                     double step_increment, double page_increment,
455                     double page_size);
456     ,
457     );
458
459     WRAP(CellRenderer, Object, _GtkCellRenderer, (),
460     ,
461     );
462
463     WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
464          CellRendererText();
465     ,
466     );
467
468     struct TreeViewColumnAttribute {
469         const char *attribute;
470         int column;
471     };
472     WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
473          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
474     ,
475     );
476
477     WRAP(AccelGroup, Object, _GtkAccelGroup, (),
478          AccelGroup();
479     ,
480     );
481
482     WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
483     ,
484     );
485
486     WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
487     ,
488          void clear();
489     );
490
491     // GBoxed
492
493     WRAP(TreePath, Object, _GtkTreePath, (),
494          TreePath();
495          TreePath(const char *path);
496     ,
497     );
498
499 #undef WRAP
500
501 }
502
503 #endif