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, (),
344 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
345 explicit CheckButton(const char *label);
349 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
353 WRAP(Item, Bin, _GtkItem, (),
357 WRAP(MenuItem, Item, _GtkMenuItem, (),
358 explicit MenuItem(const char *label, bool mnemonic = false);
362 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
366 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
370 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
374 WRAP(ComboBox, Bin, _GtkComboBox, (),
378 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
382 WRAP(ToolItem, Bin, _GtkToolItem, (),
386 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
390 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
394 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
398 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
400 void overflow(Policy x, Policy y);
403 WRAP(Box, Container, _GtkBox, (),
405 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
406 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
409 WRAP(VBox, Box, _GtkVBox, (),
410 VBox(bool homogenous, int spacing);
414 WRAP(HBox, Box, _GtkHBox, (),
415 HBox(bool homogenous, int spacing);
419 WRAP(Paned, Container, _GtkPaned, (),
423 WRAP(HPaned, Paned, _GtkHPaned, (),
427 WRAP(VPaned, Paned, _GtkVPaned, (),
431 WRAP(MenuShell, Container, _GtkMenuShell, (),
435 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
439 WRAP(Menu, MenuShell, _GtkMenu, (),
444 unsigned int left, right, top, bottom;
447 struct TableAttachOptions {
452 struct TablePadding {
456 WRAP(Table, Container, _GtkTable, (),
457 Table(std::size_t rows, std::size_t columns, bool homogenous);
460 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
463 WRAP(TextView, Container, _GtkTextView, (),
465 void text(char const *str);
468 WRAP(Toolbar, Container, _GtkToolbar, (),
473 WRAP(TreeView, Widget, _GtkTreeView, (),
474 TreeView(TreeModel model);
478 WRAP(Misc, Widget, _GtkMisc, (),
482 WRAP(Label, Widget, _GtkLabel, (),
483 explicit Label(const char *label);
485 void text(char const *str);
488 WRAP(Image, Widget, _GtkImage, (),
492 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
493 explicit Entry(std::size_t max_length);
496 void text(char const *str);
500 WRAP(SpinButton, Entry, _GtkSpinButton, (),
501 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
505 WRAP(Range, Widget, _GtkRange, (),
509 WRAP(Scale, Range, _GtkScale, (),
513 WRAP(HScale, Scale, _GtkHScale, (),
514 explicit HScale(Adjustment adjustment);
515 HScale(double min, double max, double step);
519 WRAP(Adjustment, Object, _GtkAdjustment, (),
520 Adjustment(double value,
521 double lower, double upper,
522 double step_increment, double page_increment,
527 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
531 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
535 struct TreeViewColumnAttribute {
536 const char *attribute;
539 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
540 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
544 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
548 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
552 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
557 void append(T... args);
562 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
566 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
572 WRAP(TreePath, Object, _GtkTreePath, (),
573 explicit TreePath(const char *path);
579 WRAP(GLArea, Widget, _GtkGLArea, (),
581 guint on_render(GCallback pFunction, void *data);
589 using GtkCallback = void (*)(_GtkWidget *, void *);
591 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
595 #define this (*static_cast<self>(this))
597 template<class Lambda>
598 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
600 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
603 template<class Lambda>
604 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
606 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
609 template<class Lambda>
610 void IContainer::foreach(Lambda &&lambda)
612 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
613 using Function = typename std::decay<Lambda>::type;
614 Function *f = static_cast<Function *>(data);
615 (*f)(Widget(widget));
617 gtk_container_foreach(this, cb, &lambda);
622 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
627 void IListStore::append(T... args) {
628 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
629 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);