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); }
193 { g_object_ref(_handle); }
195 template<class Lambda>
196 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
198 template<class Lambda>
199 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
201 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
203 #define WRAP(name, super, T, interfaces, ctors, methods) \
205 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
207 using self = name *; \
210 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
212 using self = name *; \
213 using native = T *; \
214 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
215 explicit name(Null n) : name((native) nullptr) {} \
216 explicit name(New_t); \
217 static name from(void *ptr) { return name((native) ptr); } \
220 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
221 static_assert(sizeof(name) == sizeof(super), "object slicing")
223 // https://developer.gnome.org/gtk2/stable/ch01.html
227 WRAP(CellEditable, Object, _GtkCellEditable, (),
231 WRAP(Editable, Object, _GtkEditable, (),
233 void editable(bool value);
236 WRAP(TreeModel, Object, _GtkTreeModel, (),
248 WRAP(Widget, Object, _GtkWidget, (),
251 const char *file_dialog(
254 const char *path = nullptr,
255 const char *pattern = nullptr,
256 bool want_load = false,
257 bool want_import = false,
258 bool want_save = false
261 void visible(bool shown);
264 Dimensions dimensions();
265 void dimensions(int width, int height);
269 WRAP(Container, Widget, _GtkContainer, (),
271 void add(Widget widget);
273 void remove(Widget widget);
275 template<class Lambda>
276 void foreach(Lambda &&lambda);
279 WRAP(Bin, Container, _GtkBin, (),
284 WRAP(Window, Bin, _GtkWindow, (),
285 explicit Window(window_type type);
287 alert_response alert(
289 std::string title = "NetRadiant",
290 alert_type type = alert_type::OK,
291 alert_icon icon = alert_icon::Default
294 Window create_dialog_window(
302 Window create_modal_dialog_window(
309 Window create_floating_window(const char *title);
311 std::uint64_t on_key_press(
312 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
313 void *extra = nullptr
316 void add_accel_group(AccelGroup group);
319 WRAP(Dialog, Window, _GtkDialog, (),
323 WRAP(Alignment, Bin, _GtkAlignment, (),
324 Alignment(float xalign, float yalign, float xscale, float yscale);
328 WRAP(Frame, Bin, _GtkFrame, (),
329 explicit Frame(const char *label = nullptr);
333 WRAP(Button, Bin, _GtkButton, (),
334 explicit Button(const char *label);
338 WRAP(ToggleButton, Button, _GtkToggleButton, (),
343 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
344 explicit CheckButton(const char *label);
348 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
352 WRAP(Item, Bin, _GtkItem, (),
356 WRAP(MenuItem, Item, _GtkMenuItem, (),
357 explicit MenuItem(const char *label, bool mnemonic = false);
361 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
365 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
369 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
373 WRAP(ComboBox, Bin, _GtkComboBox, (),
377 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
381 WRAP(ToolItem, Bin, _GtkToolItem, (),
385 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
389 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
393 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
397 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
399 void overflow(Policy x, Policy y);
402 WRAP(Box, Container, _GtkBox, (),
404 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
405 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
408 WRAP(VBox, Box, _GtkVBox, (),
409 VBox(bool homogenous, int spacing);
413 WRAP(HBox, Box, _GtkHBox, (),
414 HBox(bool homogenous, int spacing);
418 WRAP(Paned, Container, _GtkPaned, (),
422 WRAP(HPaned, Paned, _GtkHPaned, (),
426 WRAP(VPaned, Paned, _GtkVPaned, (),
430 WRAP(MenuShell, Container, _GtkMenuShell, (),
434 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
438 WRAP(Menu, MenuShell, _GtkMenu, (),
442 WRAP(Table, Container, _GtkTable, (),
443 Table(std::size_t rows, std::size_t columns, bool homogenous);
447 WRAP(TextView, Container, _GtkTextView, (),
449 void text(char const *str);
452 WRAP(Toolbar, Container, _GtkToolbar, (),
457 WRAP(TreeView, Widget, _GtkTreeView, (),
458 TreeView(TreeModel model);
462 WRAP(Misc, Widget, _GtkMisc, (),
466 WRAP(Label, Widget, _GtkLabel, (),
467 explicit Label(const char *label);
469 void text(char const *str);
472 WRAP(Image, Widget, _GtkImage, (),
476 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
477 explicit Entry(std::size_t max_length);
480 void text(char const *str);
484 WRAP(SpinButton, Entry, _GtkSpinButton, (),
485 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
489 WRAP(Range, Widget, _GtkRange, (),
493 WRAP(Scale, Range, _GtkScale, (),
497 WRAP(HScale, Scale, _GtkHScale, (),
498 explicit HScale(Adjustment adjustment);
499 HScale(double min, double max, double step);
503 WRAP(Adjustment, Object, _GtkAdjustment, (),
504 Adjustment(double value,
505 double lower, double upper,
506 double step_increment, double page_increment,
511 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
515 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
519 struct TreeViewColumnAttribute {
520 const char *attribute;
523 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
524 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
528 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
532 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
536 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
541 void append(T... args);
546 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
552 WRAP(TreePath, Object, _GtkTreePath, (),
553 explicit TreePath(const char *path);
559 WRAP(GLArea, Widget, _GtkGLArea, (),
561 guint on_render(GCallback pFunction, void *data);
569 using GtkCallback = void (*)(_GtkWidget *, void *);
571 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
575 #define this (*static_cast<self>(this))
577 template<class Lambda>
578 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
580 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
583 template<class Lambda>
584 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
586 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
589 template<class Lambda>
590 void IContainer::foreach(Lambda &&lambda)
592 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
593 using Function = typename std::decay<Lambda>::type;
594 Function *f = static_cast<Function *>(data);
595 (*f)(Widget(widget));
597 gtk_container_foreach(this, cb, &lambda);
602 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
607 void IListStore::append(T... args) {
608 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
609 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);