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
263 Dimensions dimensions();
264 void dimensions(int width, int height);
267 WRAP(Container, Widget, _GtkContainer, (),
269 void add(Widget widget);
271 void remove(Widget widget);
273 template<class Lambda>
274 void foreach(Lambda &&lambda);
277 WRAP(Bin, Container, _GtkBin, (),
282 WRAP(Window, Bin, _GtkWindow, (),
283 explicit Window(window_type type);
285 Window create_dialog_window(
293 Window create_modal_dialog_window(
300 Window create_floating_window(const char *title);
302 std::uint64_t on_key_press(
303 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
304 void *extra = nullptr
307 void add_accel_group(AccelGroup group);
310 WRAP(Dialog, Window, _GtkDialog, (),
314 WRAP(Alignment, Bin, _GtkAlignment, (),
315 Alignment(float xalign, float yalign, float xscale, float yscale);
319 WRAP(Frame, Bin, _GtkFrame, (),
320 explicit Frame(const char *label = nullptr);
324 WRAP(Button, Bin, _GtkButton, (),
325 explicit Button(const char *label);
329 WRAP(ToggleButton, Button, _GtkToggleButton, (),
334 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
335 explicit CheckButton(const char *label);
339 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
343 WRAP(Item, Bin, _GtkItem, (),
347 WRAP(MenuItem, Item, _GtkMenuItem, (),
348 explicit MenuItem(const char *label, bool mnemonic = false);
352 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
356 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
360 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
364 WRAP(ComboBox, Bin, _GtkComboBox, (),
368 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
372 WRAP(ToolItem, Bin, _GtkToolItem, (),
376 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
380 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
384 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
388 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
390 void overflow(Policy x, Policy y);
393 WRAP(Box, Container, _GtkBox, (),
397 WRAP(VBox, Box, _GtkVBox, (),
398 VBox(bool homogenous, int spacing);
402 WRAP(HBox, Box, _GtkHBox, (),
403 HBox(bool homogenous, int spacing);
407 WRAP(Paned, Container, _GtkPaned, (),
411 WRAP(HPaned, Paned, _GtkHPaned, (),
415 WRAP(VPaned, Paned, _GtkVPaned, (),
419 WRAP(MenuShell, Container, _GtkMenuShell, (),
423 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
427 WRAP(Menu, MenuShell, _GtkMenu, (),
431 WRAP(Table, Container, _GtkTable, (),
432 Table(std::size_t rows, std::size_t columns, bool homogenous);
436 WRAP(TextView, Container, _GtkTextView, (),
438 void text(char const *str);
441 WRAP(Toolbar, Container, _GtkToolbar, (),
446 WRAP(TreeView, Widget, _GtkTreeView, (),
447 TreeView(TreeModel model);
451 WRAP(Misc, Widget, _GtkMisc, (),
455 WRAP(Label, Widget, _GtkLabel, (),
456 explicit Label(const char *label);
458 void text(char const *str);
461 WRAP(Image, Widget, _GtkImage, (),
465 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
466 explicit Entry(std::size_t max_length);
469 void text(char const *str);
473 WRAP(SpinButton, Entry, _GtkSpinButton, (),
474 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
478 WRAP(Range, Widget, _GtkRange, (),
482 WRAP(Scale, Range, _GtkScale, (),
486 WRAP(HScale, Scale, _GtkHScale, (),
487 explicit HScale(Adjustment adjustment);
488 HScale(double min, double max, double step);
492 WRAP(Adjustment, Object, _GtkAdjustment, (),
493 Adjustment(double value,
494 double lower, double upper,
495 double step_increment, double page_increment,
500 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
504 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
508 struct TreeViewColumnAttribute {
509 const char *attribute;
512 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
513 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
517 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
521 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
525 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
530 void append(T... args);
535 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
541 WRAP(TreePath, Object, _GtkTreePath, (),
542 explicit TreePath(const char *path);
548 WRAP(GLArea, Widget, _GtkGLArea, (),
550 guint on_render(GCallback pFunction, void *data);
558 using GtkCallback = void (*)(_GtkWidget *, void *);
560 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
564 #define this (*static_cast<self>(this))
566 template<class Lambda>
567 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
569 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
572 template<class Lambda>
573 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
575 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
578 template<class Lambda>
579 void IContainer::foreach(Lambda &&lambda)
581 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
582 using Function = typename std::decay<Lambda>::type;
583 Function *f = static_cast<Function *>(data);
584 (*f)(Widget(widget));
586 gtk_container_foreach(this, cb, &lambda);
591 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
596 void IListStore::append(T... args) {
597 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
598 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);