]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
24d3145025107fbaf91605a87902eca34d0977e3
[xonotic/netradiant.git] / libs / uilib / uilib.h
1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
3
4 #include <string>
5
6 using ui_accelgroup = struct _GtkAccelGroup;
7 using ui_adjustment = struct _GtkAdjustment;
8 using ui_alignment = struct _GtkAlignment;
9 using ui_box = struct _GtkBox;
10 using ui_button = struct _GtkButton;
11 using ui_checkbutton = struct _GtkCheckButton;
12 using ui_combobox = struct _GtkComboBox;
13 using ui_cellrenderer = struct _GtkCellRenderer;
14 using ui_cellrenderertext = struct _GtkCellRendererText;
15 using ui_entry = struct _GtkEntry;
16 using ui_evkey = struct _GdkEventKey;
17 using ui_frame = struct _GtkFrame;
18 using ui_hbox = struct _GtkHBox;
19 using ui_hpaned = struct _GtkHPaned;
20 using ui_label = struct _GtkLabel;
21 using ui_menu = struct _GtkMenu;
22 using ui_menuitem = struct _GtkMenuItem;
23 using ui_modal = struct ModalDialog;
24 using ui_object = struct _GtkObject;
25 using ui_paned = struct _GtkPaned;
26 using ui_scrolledwindow = struct _GtkScrolledWindow;
27 using ui_spinbutton = struct _GtkSpinButton;
28 using ui_table = struct _GtkTable;
29 using ui_treemodel = struct _GtkTreeModel;
30 using ui_treepath = struct _GtkTreePath;
31 using ui_treeview = struct _GtkTreeView;
32 using ui_treeviewcolumn = struct _GtkTreeViewColumn;
33 using ui_typeinst = struct _GTypeInstance;
34 using ui_vbox = struct _GtkVBox;
35 using ui_vpaned = struct _GtkVPaned;
36 using ui_widget = struct _GtkWidget;
37 using ui_window = struct _GtkWindow;
38
39 namespace ui {
40
41     void init(int argc, char *argv[]);
42
43     void main();
44
45     enum class alert_type {
46         OK,
47         OKCANCEL,
48         YESNO,
49         YESNOCANCEL,
50         NOYES,
51     };
52
53     enum class alert_icon {
54         DEFAULT,
55         ERROR,
56         WARNING,
57         QUESTION,
58         ASTERISK,
59     };
60
61     enum class alert_response {
62         OK,
63         CANCEL,
64         YES,
65         NO,
66     };
67
68     template<class Self, class T, bool implicit = true>
69     struct Convertible;
70
71     template<class Self, class T>
72     struct Convertible<Self, T, true> {
73         operator T *() const
74         { return reinterpret_cast<T *>(static_cast<const Self *>(this)->_handle); }
75     };
76
77     template<class Self, class T>
78     struct Convertible<Self, T, false> {
79         explicit operator T *() const
80         { return reinterpret_cast<T *>(static_cast<const Self *>(this)->_handle); }
81     };
82
83     class Object : public Convertible<Object, ui_object, false> {
84     public:
85         using native = ui_object;
86         void *_handle;
87
88         Object(void *h) : _handle(h)
89         { }
90
91         explicit operator bool() const
92         { return _handle != nullptr; }
93
94         explicit operator ui_typeinst *() const
95         { return (ui_typeinst *) _handle; }
96
97         explicit operator void *() const
98         { return _handle; }
99     };
100
101     static_assert(sizeof(Object) == sizeof(ui_widget *), "object slicing");
102
103     class Widget : public Object, public Convertible<Widget, ui_widget> {
104     public:
105         using native = ui_widget;
106         explicit Widget(ui_widget *h = nullptr) : Object((void *) h)
107         { }
108
109         alert_response alert(std::string text, std::string title = "NetRadiant",
110                              alert_type type = alert_type::OK, alert_icon icon = alert_icon::DEFAULT);
111
112         const char *file_dialog(bool open, const char *title, const char *path = nullptr,
113                                 const char *pattern = nullptr, bool want_load = false, bool want_import = false,
114                                 bool want_save = false);
115     };
116
117     static_assert(sizeof(Widget) == sizeof(Object), "object slicing");
118
119     extern Widget root;
120
121 #define WRAP(name, super, impl, methods) \
122     class name : public super, public Convertible<name, impl> { \
123         public: \
124             using native = impl; \
125             explicit name(impl *h) : super(reinterpret_cast<super::native *>(h)) {} \
126         methods \
127     }; \
128     static_assert(sizeof(name) == sizeof(super), "object slicing")
129
130     WRAP(AccelGroup, Object, ui_accelgroup,
131          AccelGroup();
132     );
133
134     WRAP(Adjustment, Widget, ui_adjustment,
135          Adjustment(double value,
136                     double lower, double upper,
137                     double step_increment, double page_increment,
138                     double page_size);
139     );
140
141     WRAP(Alignment, Widget, ui_alignment,
142          Alignment(float xalign, float yalign, float xscale, float yscale);
143     );
144
145     WRAP(Box, Widget, ui_box,);
146
147     WRAP(Button, Widget, ui_button,
148          Button();
149          Button(const char *label);
150     );
151
152     WRAP(CellRenderer, Object, ui_cellrenderer,);
153
154     WRAP(CellRendererText, CellRenderer, ui_cellrenderertext,
155          CellRendererText();
156     );
157
158     WRAP(CheckButton, Widget, ui_checkbutton,
159          CheckButton(const char *label);
160     );
161
162     WRAP(ComboBox, Widget, ui_combobox,);
163
164     ComboBox ComboBoxText();
165
166     WRAP(Entry, Widget, ui_entry,
167          Entry();
168          Entry(std::size_t max_length);
169     );
170
171     WRAP(Frame, Widget, ui_frame,
172          Frame(const char *label = nullptr);
173     );
174
175     WRAP(HBox, Box, ui_hbox,
176          HBox(bool homogenous, int spacing);
177     );
178
179     WRAP(Label, Widget, ui_label,
180          Label(const char *label);
181     );
182
183     WRAP(Menu, Widget, ui_menu,
184          Menu();
185     );
186
187     WRAP(MenuItem, Widget, ui_menuitem,
188          MenuItem(const char *label, bool mnemonic = false);
189     );
190
191     WRAP(Paned, Widget, ui_paned,);
192
193         WRAP(HPaned, Paned, ui_hpaned,
194              HPaned();
195         );
196
197         WRAP(VPaned, Paned, ui_vpaned,
198              VPaned();
199         );
200
201     WRAP(ScrolledWindow, Widget, ui_scrolledwindow,
202          ScrolledWindow();
203     );
204
205     WRAP(SpinButton, Widget, ui_spinbutton,
206          SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
207     );
208
209     WRAP(Table, Widget, ui_table,
210          Table(std::size_t rows, std::size_t columns, bool homogenous);
211     );
212
213     WRAP(TreeModel, Widget, ui_treemodel,);
214
215     WRAP(TreePath, Object, ui_treepath,
216          TreePath();
217          TreePath(const char *path);
218     );
219
220     WRAP(TreeView, Widget, ui_treeview,
221          TreeView(TreeModel model);
222     );
223
224     struct TreeViewColumnAttribute {
225         const char *attribute;
226         int column;
227     };
228     WRAP(TreeViewColumn, Widget, ui_treeviewcolumn,
229          TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
230     );
231
232     WRAP(VBox, Box, ui_vbox,
233          VBox(bool homogenous, int spacing);
234     );
235
236     WRAP(Window, Widget, ui_window,
237          Window() : Window(nullptr) {};
238
239          Window create_dialog_window(const char *title, void func(), void *data, int default_w = -1,
240                                      int default_h = -1);
241
242          Window create_modal_dialog_window(const char *title, ui_modal &dialog, int default_w = -1,
243                                            int default_h = -1);
244
245          Window create_floating_window(const char *title);
246
247          std::uint64_t on_key_press(bool (*f)(Widget widget, ui_evkey *event, void *extra),
248                                     void *extra = nullptr);
249     );
250
251 #undef WRAP
252
253 }
254
255 #endif