1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
4 #include "globaldefs.h"
7 #include <glib-object.h>
10 struct _GtkAccelGroup;
11 struct _GtkAdjustment;
16 struct _GtkCellEditable;
17 struct _GtkCellRenderer;
18 struct _GtkCellRendererText;
19 struct _GtkCheckButton;
20 struct _GtkCheckMenuItem;
22 struct _GtkComboBoxText;
27 struct _GtkEntryCompletion;
44 struct _GtkRadioButton;
45 struct _GtkRadioMenuItem;
46 struct _GtkRadioToolButton;
49 struct _GtkScrolledWindow;
50 struct _GtkSpinButton;
52 struct _GtkTearoffMenuItem;
54 struct _GtkToggleButton;
55 struct _GtkToggleToolButton;
57 struct _GtkToolButton;
61 struct _GtkTreeSelection;
64 struct _GtkTreeViewColumn;
69 struct _GTypeInstance;
76 using _GtkGLArea = struct _GtkDrawingArea;
83 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
89 enum class window_type {
114 template<class Self, class T, Convert mode>
117 template<class Self, class T>
118 struct Convertible<Self, T, Convert::Implicit> {
120 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
123 template<class Self, class T>
124 struct Convertible<Self, T, Convert::Explicit> {
125 explicit operator T() const
126 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
129 template<class Self, class... T>
135 template<class Self, class Interfaces>
138 struct Mixin<Self, void()> {
139 using type = All<Self>;
141 template<class Self, class... Interfaces>
142 struct Mixin<Self, void(Interfaces...)> {
143 using type = All<Self, Interfaces...>;
147 const struct Null {} null = {};
148 const struct New_t {} New = {};
151 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
152 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
154 using self = Object *;
155 using native = _GtkObject *;
158 explicit Object(native h) : _handle(h)
161 explicit operator bool() const
162 { return _handle != nullptr; }
164 explicit operator void *() const
168 { g_object_unref(_handle); }
171 { g_object_ref(_handle); }
173 template<class Lambda>
174 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
176 template<class Lambda>
177 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
179 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
181 #define WRAP(name, super, T, interfaces, ctors, methods) \
183 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
185 using self = name *; \
188 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
190 using self = name *; \
191 using native = T *; \
193 explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
195 explicit name(Null n) noexcept : name((native) nullptr) {} \
196 explicit name(New_t); \
197 static name from(native h) { return name(h); } \
198 static name from(void *ptr) { return name((native) ptr); } \
201 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
202 static_assert(sizeof(name) == sizeof(super), "object slicing")
204 // https://developer.gnome.org/gtk2/stable/ch01.html
208 WRAP(CellEditable, Object, _GtkCellEditable, (),
212 WRAP(Editable, Object, _GtkEditable, (),
214 void editable(bool value);
217 WRAP(TreeModel, Object, _GtkTreeModel, (),
229 WRAP(Widget, Object, _GtkWidget, (),
232 const char *file_dialog(
235 const char *path = nullptr,
236 const char *pattern = nullptr,
237 bool want_load = false,
238 bool want_import = false,
239 bool want_save = false
242 void visible(bool shown);
245 Dimensions dimensions();
246 void dimensions(int width, int height);
250 WRAP(Container, Widget, _GtkContainer, (),
252 void add(Widget widget);
254 void remove(Widget widget);
256 template<class Lambda>
257 void foreach(Lambda &&lambda);
260 WRAP(Bin, Container, _GtkBin, (),
265 WRAP(Window, Bin, _GtkWindow, (),
266 explicit Window(window_type type);
268 Window create_dialog_window(
276 Window create_modal_dialog_window(
283 Window create_floating_window(const char *title);
285 std::uint64_t on_key_press(
286 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
287 void *extra = nullptr
290 void add_accel_group(AccelGroup group);
293 WRAP(Dialog, Window, _GtkDialog, (),
297 WRAP(Alignment, Bin, _GtkAlignment, (),
298 Alignment(float xalign, float yalign, float xscale, float yscale);
302 WRAP(Frame, Bin, _GtkFrame, (),
303 explicit Frame(const char *label = nullptr);
307 WRAP(Button, Bin, _GtkButton, (),
308 explicit Button(const char *label);
312 WRAP(ToggleButton, Button, _GtkToggleButton, (),
315 void active(bool value);
318 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
319 explicit CheckButton(const char *label);
323 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
327 WRAP(Item, Bin, _GtkItem, (),
331 WRAP(MenuItem, Item, _GtkMenuItem, (),
332 explicit MenuItem(const char *label, bool mnemonic = false);
336 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
340 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
344 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
348 WRAP(ComboBox, Bin, _GtkComboBox, (),
352 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
356 WRAP(ToolItem, Bin, _GtkToolItem, (),
360 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
364 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
368 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
372 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
374 void overflow(Policy x, Policy y);
377 WRAP(Box, Container, _GtkBox, (),
379 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
380 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
383 WRAP(VBox, Box, _GtkVBox, (),
384 VBox(bool homogenous, int spacing);
388 WRAP(HBox, Box, _GtkHBox, (),
389 HBox(bool homogenous, int spacing);
393 WRAP(Paned, Container, _GtkPaned, (),
397 WRAP(HPaned, Paned, _GtkHPaned, (),
401 WRAP(VPaned, Paned, _GtkVPaned, (),
405 WRAP(MenuShell, Container, _GtkMenuShell, (),
409 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
413 WRAP(Menu, MenuShell, _GtkMenu, (),
418 unsigned int left, right, top, bottom;
421 struct TableAttachOptions {
426 struct TablePadding {
430 WRAP(Table, Container, _GtkTable, (),
431 Table(std::size_t rows, std::size_t columns, bool homogenous);
434 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
437 WRAP(TextView, Container, _GtkTextView, (),
439 void text(char const *str);
442 WRAP(Toolbar, Container, _GtkToolbar, (),
447 WRAP(TreeView, Widget, _GtkTreeView, (),
448 TreeView(TreeModel model);
452 WRAP(Misc, Widget, _GtkMisc, (),
456 WRAP(Label, Widget, _GtkLabel, (),
457 explicit Label(const char *label);
459 void text(char const *str);
462 WRAP(Image, Widget, _GtkImage, (),
466 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
467 explicit Entry(std::size_t max_length);
470 void text(char const *str);
474 WRAP(SpinButton, Entry, _GtkSpinButton, (),
475 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
479 WRAP(Range, Widget, _GtkRange, (),
483 WRAP(Scale, Range, _GtkScale, (),
487 WRAP(HScale, Scale, _GtkHScale, (),
488 explicit HScale(Adjustment adjustment);
489 HScale(double min, double max, double step);
493 WRAP(Adjustment, Object, _GtkAdjustment, (),
494 Adjustment(double value,
495 double lower, double upper,
496 double step_increment, double page_increment,
501 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
505 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
509 struct TreeViewColumnAttribute {
510 const char *attribute;
513 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
514 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
518 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
522 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
526 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
531 void append(T... args);
536 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
540 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
546 WRAP(TreePath, Object, _GtkTreePath, (),
547 explicit TreePath(const char *path);
553 WRAP(GLArea, Widget, _GtkGLArea, (),
555 guint on_render(GCallback pFunction, void *data);
562 enum class alert_response {
569 enum class alert_type {
577 enum class alert_icon {
585 extern class Window root;
587 alert_response alert(
590 std::string title = "NetRadiant",
591 alert_type type = alert_type::OK,
592 alert_icon icon = alert_icon::Default
598 using GtkCallback = void (*)(_GtkWidget *, void *);
600 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
604 WARNING_SUPPRESS_CLANG(keyword-macro)
605 #define this (*static_cast<self>(this))
606 WARNING_RESTORE_CLANG(keyword-macro)
608 template<class Lambda>
609 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
611 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
614 template<class Lambda>
615 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
617 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
620 template<class Lambda>
621 void IContainer::foreach(Lambda &&lambda)
623 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
624 using Function = typename std::decay<Lambda>::type;
625 Function *f = static_cast<Function *>(data);
626 (*f)(Widget::from(widget));
628 gtk_container_foreach(this, cb, &lambda);
633 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
638 void IListStore::append(T... args) {
639 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
640 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);