1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
5 #include <glib-object.h>
14 struct _GtkCellEditable;
15 struct _GtkCellRenderer;
16 struct _GtkCellRendererText;
17 struct _GtkCheckButton;
18 struct _GtkCheckMenuItem;
20 struct _GtkComboBoxText;
25 struct _GtkEntryCompletion;
42 struct _GtkRadioButton;
43 struct _GtkRadioMenuItem;
44 struct _GtkRadioToolButton;
47 struct _GtkScrolledWindow;
48 struct _GtkSpinButton;
50 struct _GtkTearoffMenuItem;
52 struct _GtkToggleButton;
53 struct _GtkToggleToolButton;
55 struct _GtkToolButton;
59 struct _GtkTreeSelection;
61 struct _GtkTreeViewColumn;
66 struct _GTypeInstance;
73 using _GtkGLArea = struct _GtkDrawingArea;
80 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
86 extern class Widget root;
88 enum class alert_type {
96 enum class alert_icon {
104 enum class alert_response {
111 enum class window_type {
136 template<class Self, class T, Convert mode>
139 template<class Self, class T>
140 struct Convertible<Self, T, Convert::Implicit> {
142 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
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); }
151 template<class Self, class... T>
157 template<class Self, class Interfaces>
160 struct Mixin<Self, void()> {
161 using type = All<Self>;
163 template<class Self, class... Interfaces>
164 struct Mixin<Self, void(Interfaces...)> {
165 using type = All<Self, Interfaces...>;
169 extern struct Null {} null;
170 extern struct New_t {} New;
173 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
174 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
176 using self = Object *;
177 using native = _GtkObject *;
180 explicit Object(native h) : _handle(h)
183 explicit operator bool() const
184 { return _handle != nullptr; }
186 explicit operator void *() const
190 { g_object_unref(_handle); }
192 template<class Lambda>
193 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
195 template<class Lambda>
196 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
198 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
200 #define WRAP(name, super, T, interfaces, ctors, methods) \
202 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
204 using self = name *; \
207 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
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); } \
217 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
218 static_assert(sizeof(name) == sizeof(super), "object slicing")
220 // https://developer.gnome.org/gtk2/stable/ch01.html
224 WRAP(CellEditable, Object, _GtkCellEditable, (),
228 WRAP(Editable, Object, _GtkEditable, (),
230 void editable(bool value);
233 WRAP(TreeModel, Object, _GtkTreeModel, (),
244 WRAP(Widget, Object, _GtkWidget, (),
246 alert_response alert(
248 std::string title = "NetRadiant",
249 alert_type type = alert_type::OK,
250 alert_icon icon = alert_icon::Default
252 const char *file_dialog(
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
262 void visible(bool shown);
265 Dimensions dimensions();
266 void dimensions(int width, int height);
270 WRAP(Container, Widget, _GtkContainer, (),
272 void add(Widget widget);
274 void remove(Widget widget);
276 template<class Lambda>
277 void foreach(Lambda &&lambda);
280 WRAP(Bin, Container, _GtkBin, (),
285 WRAP(Window, Bin, _GtkWindow, (),
286 explicit Window(window_type type);
288 Window create_dialog_window(
296 Window create_modal_dialog_window(
303 Window create_floating_window(const char *title);
305 std::uint64_t on_key_press(
306 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
307 void *extra = nullptr
310 void add_accel_group(AccelGroup group);
313 WRAP(Dialog, Window, _GtkDialog, (),
317 WRAP(Alignment, Bin, _GtkAlignment, (),
318 Alignment(float xalign, float yalign, float xscale, float yscale);
322 WRAP(Frame, Bin, _GtkFrame, (),
323 explicit Frame(const char *label = nullptr);
327 WRAP(Button, Bin, _GtkButton, (),
328 explicit Button(const char *label);
332 WRAP(ToggleButton, Button, _GtkToggleButton, (),
337 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
338 explicit CheckButton(const char *label);
342 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
346 WRAP(Item, Bin, _GtkItem, (),
350 WRAP(MenuItem, Item, _GtkMenuItem, (),
351 explicit MenuItem(const char *label, bool mnemonic = false);
355 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
359 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
363 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
367 WRAP(ComboBox, Bin, _GtkComboBox, (),
371 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
375 WRAP(ToolItem, Bin, _GtkToolItem, (),
379 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
383 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
387 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
391 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
393 void overflow(Policy x, Policy y);
396 WRAP(Box, Container, _GtkBox, (),
400 WRAP(VBox, Box, _GtkVBox, (),
401 VBox(bool homogenous, int spacing);
405 WRAP(HBox, Box, _GtkHBox, (),
406 HBox(bool homogenous, int spacing);
410 WRAP(Paned, Container, _GtkPaned, (),
414 WRAP(HPaned, Paned, _GtkHPaned, (),
418 WRAP(VPaned, Paned, _GtkVPaned, (),
422 WRAP(MenuShell, Container, _GtkMenuShell, (),
426 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
430 WRAP(Menu, MenuShell, _GtkMenu, (),
434 WRAP(Table, Container, _GtkTable, (),
435 Table(std::size_t rows, std::size_t columns, bool homogenous);
439 WRAP(TextView, Container, _GtkTextView, (),
441 void text(char const *str);
444 WRAP(Toolbar, Container, _GtkToolbar, (),
449 WRAP(TreeView, Widget, _GtkTreeView, (),
450 TreeView(TreeModel model);
454 WRAP(Misc, Widget, _GtkMisc, (),
458 WRAP(Label, Widget, _GtkLabel, (),
459 explicit Label(const char *label);
461 void text(char const *str);
464 WRAP(Image, Widget, _GtkImage, (),
468 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
469 explicit Entry(std::size_t max_length);
472 void text(char const *str);
476 WRAP(SpinButton, Entry, _GtkSpinButton, (),
477 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
481 WRAP(Range, Widget, _GtkRange, (),
485 WRAP(Scale, Range, _GtkScale, (),
489 WRAP(HScale, Scale, _GtkHScale, (),
490 explicit HScale(Adjustment adjustment);
491 HScale(double min, double max, double step);
495 WRAP(Adjustment, Object, _GtkAdjustment, (),
496 Adjustment(double value,
497 double lower, double upper,
498 double step_increment, double page_increment,
503 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
507 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
511 struct TreeViewColumnAttribute {
512 const char *attribute;
515 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
516 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
520 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
524 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
528 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
533 void append(T... args);
538 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
544 WRAP(TreePath, Object, _GtkTreePath, (),
545 explicit TreePath(const char *path);
551 WRAP(GLArea, Widget, _GtkGLArea, (),
553 guint on_render(GCallback pFunction, void *data);
561 using GtkCallback = void (*)(_GtkWidget *, void *);
563 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
567 #define this (*static_cast<self>(this))
569 template<class Lambda>
570 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
572 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
575 template<class Lambda>
576 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
578 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
581 template<class Lambda>
582 void IContainer::foreach(Lambda &&lambda)
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));
589 gtk_container_foreach(this, cb, &lambda);
594 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
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);