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;
62 struct _GtkTreeViewColumn;
67 struct _GTypeInstance;
74 using _GtkGLArea = struct _GtkDrawingArea;
81 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
87 extern class Widget root;
89 enum class alert_type {
97 enum class alert_icon {
105 enum class alert_response {
112 enum class window_type {
137 template<class Self, class T, Convert mode>
140 template<class Self, class T>
141 struct Convertible<Self, T, Convert::Implicit> {
143 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
146 template<class Self, class T>
147 struct Convertible<Self, T, Convert::Explicit> {
148 explicit operator T() const
149 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
152 template<class Self, class... T>
158 template<class Self, class Interfaces>
161 struct Mixin<Self, void()> {
162 using type = All<Self>;
164 template<class Self, class... Interfaces>
165 struct Mixin<Self, void(Interfaces...)> {
166 using type = All<Self, Interfaces...>;
170 extern struct Null {} null;
171 extern struct New_t {} New;
174 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
175 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
177 using self = Object *;
178 using native = _GtkObject *;
181 explicit Object(native h) : _handle(h)
184 explicit operator bool() const
185 { return _handle != nullptr; }
187 explicit operator void *() const
191 { g_object_unref(_handle); }
194 { g_object_ref(_handle); }
196 template<class Lambda>
197 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
199 template<class Lambda>
200 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
202 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
204 #define WRAP(name, super, T, interfaces, ctors, methods) \
206 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
208 using self = name *; \
211 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
213 using self = name *; \
214 using native = T *; \
215 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
216 explicit name(Null n) : name((native) nullptr) {} \
217 explicit name(New_t); \
218 static name from(void *ptr) { return name((native) ptr); } \
221 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
222 static_assert(sizeof(name) == sizeof(super), "object slicing")
224 // https://developer.gnome.org/gtk2/stable/ch01.html
228 WRAP(CellEditable, Object, _GtkCellEditable, (),
232 WRAP(Editable, Object, _GtkEditable, (),
234 void editable(bool value);
237 WRAP(TreeModel, Object, _GtkTreeModel, (),
249 WRAP(Widget, Object, _GtkWidget, (),
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 alert_response alert(
290 std::string title = "NetRadiant",
291 alert_type type = alert_type::OK,
292 alert_icon icon = alert_icon::Default
295 Window create_dialog_window(
303 Window create_modal_dialog_window(
310 Window create_floating_window(const char *title);
312 std::uint64_t on_key_press(
313 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
314 void *extra = nullptr
317 void add_accel_group(AccelGroup group);
320 WRAP(Dialog, Window, _GtkDialog, (),
324 WRAP(Alignment, Bin, _GtkAlignment, (),
325 Alignment(float xalign, float yalign, float xscale, float yscale);
329 WRAP(Frame, Bin, _GtkFrame, (),
330 explicit Frame(const char *label = nullptr);
334 WRAP(Button, Bin, _GtkButton, (),
335 explicit Button(const char *label);
339 WRAP(ToggleButton, Button, _GtkToggleButton, (),
342 void active(bool value);
345 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
346 explicit CheckButton(const char *label);
350 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
354 WRAP(Item, Bin, _GtkItem, (),
358 WRAP(MenuItem, Item, _GtkMenuItem, (),
359 explicit MenuItem(const char *label, bool mnemonic = false);
363 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
367 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
371 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
375 WRAP(ComboBox, Bin, _GtkComboBox, (),
379 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
383 WRAP(ToolItem, Bin, _GtkToolItem, (),
387 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
391 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
395 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
399 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
401 void overflow(Policy x, Policy y);
404 WRAP(Box, Container, _GtkBox, (),
406 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
407 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
410 WRAP(VBox, Box, _GtkVBox, (),
411 VBox(bool homogenous, int spacing);
415 WRAP(HBox, Box, _GtkHBox, (),
416 HBox(bool homogenous, int spacing);
420 WRAP(Paned, Container, _GtkPaned, (),
424 WRAP(HPaned, Paned, _GtkHPaned, (),
428 WRAP(VPaned, Paned, _GtkVPaned, (),
432 WRAP(MenuShell, Container, _GtkMenuShell, (),
436 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
440 WRAP(Menu, MenuShell, _GtkMenu, (),
445 unsigned int left, right, top, bottom;
448 struct TableAttachOptions {
453 struct TablePadding {
457 WRAP(Table, Container, _GtkTable, (),
458 Table(std::size_t rows, std::size_t columns, bool homogenous);
461 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
464 WRAP(TextView, Container, _GtkTextView, (),
466 void text(char const *str);
469 WRAP(Toolbar, Container, _GtkToolbar, (),
474 WRAP(TreeView, Widget, _GtkTreeView, (),
475 TreeView(TreeModel model);
479 WRAP(Misc, Widget, _GtkMisc, (),
483 WRAP(Label, Widget, _GtkLabel, (),
484 explicit Label(const char *label);
486 void text(char const *str);
489 WRAP(Image, Widget, _GtkImage, (),
493 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
494 explicit Entry(std::size_t max_length);
497 void text(char const *str);
501 WRAP(SpinButton, Entry, _GtkSpinButton, (),
502 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
506 WRAP(Range, Widget, _GtkRange, (),
510 WRAP(Scale, Range, _GtkScale, (),
514 WRAP(HScale, Scale, _GtkHScale, (),
515 explicit HScale(Adjustment adjustment);
516 HScale(double min, double max, double step);
520 WRAP(Adjustment, Object, _GtkAdjustment, (),
521 Adjustment(double value,
522 double lower, double upper,
523 double step_increment, double page_increment,
528 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
532 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
536 struct TreeViewColumnAttribute {
537 const char *attribute;
540 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
541 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
545 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
549 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
553 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
558 void append(T... args);
563 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
567 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
573 WRAP(TreePath, Object, _GtkTreePath, (),
574 explicit TreePath(const char *path);
580 WRAP(GLArea, Widget, _GtkGLArea, (),
582 guint on_render(GCallback pFunction, void *data);
590 using GtkCallback = void (*)(_GtkWidget *, void *);
592 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
596 #define this (*static_cast<self>(this))
598 template<class Lambda>
599 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
601 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
604 template<class Lambda>
605 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
607 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
610 template<class Lambda>
611 void IContainer::foreach(Lambda &&lambda)
613 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
614 using Function = typename std::decay<Lambda>::type;
615 Function *f = static_cast<Function *>(data);
616 (*f)(Widget(widget));
618 gtk_container_foreach(this, cb, &lambda);
623 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
628 void IListStore::append(T... args) {
629 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
630 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);