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 enum class window_type {
112 template<class Self, class T, Convert mode>
115 template<class Self, class T>
116 struct Convertible<Self, T, Convert::Implicit> {
118 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
121 template<class Self, class T>
122 struct Convertible<Self, T, Convert::Explicit> {
123 explicit operator T() const
124 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
127 template<class Self, class... T>
133 template<class Self, class Interfaces>
136 struct Mixin<Self, void()> {
137 using type = All<Self>;
139 template<class Self, class... Interfaces>
140 struct Mixin<Self, void(Interfaces...)> {
141 using type = All<Self, Interfaces...>;
145 const struct Null {} null = {};
146 const struct New_t {} New = {};
149 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
150 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
152 using self = Object *;
153 using native = _GtkObject *;
156 explicit Object(native h) : _handle(h)
159 explicit operator bool() const
160 { return _handle != nullptr; }
162 explicit operator void *() const
166 { g_object_unref(_handle); }
169 { g_object_ref(_handle); }
171 template<class Lambda>
172 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
174 template<class Lambda>
175 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
177 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
179 #define WRAP(name, super, T, interfaces, ctors, methods) \
181 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
183 using self = name *; \
186 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
188 using self = name *; \
189 using native = T *; \
191 explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
193 explicit name(Null n) noexcept : name((native) nullptr) {} \
194 explicit name(New_t); \
195 static name from(native h) { return name(h); } \
196 static name from(void *ptr) { return name((native) ptr); } \
199 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
200 static_assert(sizeof(name) == sizeof(super), "object slicing")
202 // https://developer.gnome.org/gtk2/stable/ch01.html
206 WRAP(CellEditable, Object, _GtkCellEditable, (),
210 WRAP(Editable, Object, _GtkEditable, (),
212 void editable(bool value);
215 WRAP(TreeModel, Object, _GtkTreeModel, (),
227 WRAP(Widget, Object, _GtkWidget, (),
230 const char *file_dialog(
233 const char *path = nullptr,
234 const char *pattern = nullptr,
235 bool want_load = false,
236 bool want_import = false,
237 bool want_save = false
240 void visible(bool shown);
243 Dimensions dimensions();
244 void dimensions(int width, int height);
248 WRAP(Container, Widget, _GtkContainer, (),
250 void add(Widget widget);
252 void remove(Widget widget);
254 template<class Lambda>
255 void foreach(Lambda &&lambda);
258 WRAP(Bin, Container, _GtkBin, (),
263 WRAP(Window, Bin, _GtkWindow, (),
264 explicit Window(window_type type);
266 Window create_dialog_window(
274 Window create_modal_dialog_window(
281 Window create_floating_window(const char *title);
283 std::uint64_t on_key_press(
284 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
285 void *extra = nullptr
288 void add_accel_group(AccelGroup group);
291 WRAP(Dialog, Window, _GtkDialog, (),
295 WRAP(Alignment, Bin, _GtkAlignment, (),
296 Alignment(float xalign, float yalign, float xscale, float yscale);
300 WRAP(Frame, Bin, _GtkFrame, (),
301 explicit Frame(const char *label = nullptr);
305 WRAP(Button, Bin, _GtkButton, (),
306 explicit Button(const char *label);
310 WRAP(ToggleButton, Button, _GtkToggleButton, (),
313 void active(bool value);
316 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
317 explicit CheckButton(const char *label);
321 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
325 WRAP(Item, Bin, _GtkItem, (),
329 WRAP(MenuItem, Item, _GtkMenuItem, (),
330 explicit MenuItem(const char *label, bool mnemonic = false);
334 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
338 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
342 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
346 WRAP(ComboBox, Bin, _GtkComboBox, (),
350 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
354 WRAP(ToolItem, Bin, _GtkToolItem, (),
358 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
362 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
366 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
370 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
372 void overflow(Policy x, Policy y);
375 WRAP(Box, Container, _GtkBox, (),
377 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
378 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
381 WRAP(VBox, Box, _GtkVBox, (),
382 VBox(bool homogenous, int spacing);
386 WRAP(HBox, Box, _GtkHBox, (),
387 HBox(bool homogenous, int spacing);
391 WRAP(Paned, Container, _GtkPaned, (),
395 WRAP(HPaned, Paned, _GtkHPaned, (),
399 WRAP(VPaned, Paned, _GtkVPaned, (),
403 WRAP(MenuShell, Container, _GtkMenuShell, (),
407 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
411 WRAP(Menu, MenuShell, _GtkMenu, (),
416 unsigned int left, right, top, bottom;
419 struct TableAttachOptions {
424 struct TablePadding {
428 WRAP(Table, Container, _GtkTable, (),
429 Table(std::size_t rows, std::size_t columns, bool homogenous);
432 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
435 WRAP(TextView, Container, _GtkTextView, (),
437 void text(char const *str);
440 WRAP(Toolbar, Container, _GtkToolbar, (),
445 WRAP(TreeView, Widget, _GtkTreeView, (),
446 TreeView(TreeModel model);
450 WRAP(Misc, Widget, _GtkMisc, (),
454 WRAP(Label, Widget, _GtkLabel, (),
455 explicit Label(const char *label);
457 void text(char const *str);
460 WRAP(Image, Widget, _GtkImage, (),
464 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
465 explicit Entry(std::size_t max_length);
468 void text(char const *str);
472 WRAP(SpinButton, Entry, _GtkSpinButton, (),
473 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
477 WRAP(Range, Widget, _GtkRange, (),
481 WRAP(Scale, Range, _GtkScale, (),
485 WRAP(HScale, Scale, _GtkHScale, (),
486 explicit HScale(Adjustment adjustment);
487 HScale(double min, double max, double step);
491 WRAP(Adjustment, Object, _GtkAdjustment, (),
492 Adjustment(double value,
493 double lower, double upper,
494 double step_increment, double page_increment,
499 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
503 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
507 struct TreeViewColumnAttribute {
508 const char *attribute;
511 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
512 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
516 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
520 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
524 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
529 void append(T... args);
534 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
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);
560 enum class alert_response {
567 enum class alert_type {
575 enum class alert_icon {
583 extern class Window root;
585 alert_response alert(
588 std::string title = "NetRadiant",
589 alert_type type = alert_type::OK,
590 alert_icon icon = alert_icon::Default
596 using GtkCallback = void (*)(_GtkWidget *, void *);
598 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
602 #define this (*static_cast<self>(this))
604 template<class Lambda>
605 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
607 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
610 template<class Lambda>
611 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
613 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
616 template<class Lambda>
617 void IContainer::foreach(Lambda &&lambda)
619 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
620 using Function = typename std::decay<Lambda>::type;
621 Function *f = static_cast<Function *>(data);
622 (*f)(Widget::from(widget));
624 gtk_container_foreach(this, cb, &lambda);
629 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
634 void IListStore::append(T... args) {
635 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
636 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);