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, (),
398 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
399 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
402 WRAP(VBox, Box, _GtkVBox, (),
403 VBox(bool homogenous, int spacing);
407 WRAP(HBox, Box, _GtkHBox, (),
408 HBox(bool homogenous, int spacing);
412 WRAP(Paned, Container, _GtkPaned, (),
416 WRAP(HPaned, Paned, _GtkHPaned, (),
420 WRAP(VPaned, Paned, _GtkVPaned, (),
424 WRAP(MenuShell, Container, _GtkMenuShell, (),
428 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
432 WRAP(Menu, MenuShell, _GtkMenu, (),
436 WRAP(Table, Container, _GtkTable, (),
437 Table(std::size_t rows, std::size_t columns, bool homogenous);
441 WRAP(TextView, Container, _GtkTextView, (),
443 void text(char const *str);
446 WRAP(Toolbar, Container, _GtkToolbar, (),
451 WRAP(TreeView, Widget, _GtkTreeView, (),
452 TreeView(TreeModel model);
456 WRAP(Misc, Widget, _GtkMisc, (),
460 WRAP(Label, Widget, _GtkLabel, (),
461 explicit Label(const char *label);
463 void text(char const *str);
466 WRAP(Image, Widget, _GtkImage, (),
470 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
471 explicit Entry(std::size_t max_length);
474 void text(char const *str);
478 WRAP(SpinButton, Entry, _GtkSpinButton, (),
479 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
483 WRAP(Range, Widget, _GtkRange, (),
487 WRAP(Scale, Range, _GtkScale, (),
491 WRAP(HScale, Scale, _GtkHScale, (),
492 explicit HScale(Adjustment adjustment);
493 HScale(double min, double max, double step);
497 WRAP(Adjustment, Object, _GtkAdjustment, (),
498 Adjustment(double value,
499 double lower, double upper,
500 double step_increment, double page_increment,
505 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
509 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
513 struct TreeViewColumnAttribute {
514 const char *attribute;
517 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
518 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
522 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
526 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
530 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
535 void append(T... args);
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);
563 using GtkCallback = void (*)(_GtkWidget *, void *);
565 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
569 #define this (*static_cast<self>(this))
571 template<class Lambda>
572 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
574 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
577 template<class Lambda>
578 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
580 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
583 template<class Lambda>
584 void IContainer::foreach(Lambda &&lambda)
586 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
587 using Function = typename std::decay<Lambda>::type;
588 Function *f = static_cast<Function *>(data);
589 (*f)(Widget(widget));
591 gtk_container_foreach(this, cb, &lambda);
596 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
601 void IListStore::append(T... args) {
602 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
603 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);