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;
41 struct _GtkRadioButton;
42 struct _GtkRadioMenuItem;
43 struct _GtkRadioToolButton;
46 struct _GtkScrolledWindow;
47 struct _GtkSpinButton;
49 struct _GtkTearoffMenuItem;
51 struct _GtkToggleButton;
52 struct _GtkToggleToolButton;
54 struct _GtkToolButton;
58 struct _GtkTreeSelection;
60 struct _GtkTreeViewColumn;
65 struct _GTypeInstance;
71 void init(int argc, char *argv[]);
77 extern class Widget root;
79 enum class alert_type {
87 enum class alert_icon {
95 enum class alert_response {
102 enum class window_type {
127 template<class Self, class T, Convert mode>
130 template<class Self, class T>
131 struct Convertible<Self, T, Convert::Implicit> {
133 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
136 template<class Self, class T>
137 struct Convertible<Self, T, Convert::Explicit> {
138 explicit operator T() const
139 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
142 template<class Self, class... T>
148 template<class Self, class Interfaces>
151 struct Mixin<Self, void()> {
152 using type = All<Self>;
154 template<class Self, class... Interfaces>
155 struct Mixin<Self, void(Interfaces...)> {
156 using type = All<Self, Interfaces...>;
160 extern struct Null {} null;
163 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
164 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
166 using self = Object *;
167 using native = _GtkObject *;
170 Object(native h) : _handle(h)
173 explicit operator bool() const
174 { return _handle != nullptr; }
176 explicit operator void *() const
180 { g_object_unref(_handle); }
182 template<class Lambda>
183 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
185 template<class Lambda>
186 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
188 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
190 #define WRAP(name, super, T, interfaces, ctors, methods) \
192 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
194 using self = name *; \
197 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
199 using self = name *; \
200 using native = T *; \
201 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
202 explicit name(Null n) : name((native) nullptr) {} \
205 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
206 static_assert(sizeof(name) == sizeof(super), "object slicing")
208 // https://developer.gnome.org/gtk2/stable/ch01.html
212 WRAP(CellEditable, Object, _GtkCellEditable, (),
216 WRAP(Editable, Object, _GtkEditable, (),
219 void editable(bool value);
222 WRAP(TreeModel, Object, _GtkTreeModel, (),
228 WRAP(Widget, Object, _GtkWidget, (),
231 alert_response alert(
233 std::string title = "NetRadiant",
234 alert_type type = alert_type::OK,
235 alert_icon icon = alert_icon::Default
237 const char *file_dialog(
240 const char *path = nullptr,
241 const char *pattern = nullptr,
242 bool want_load = false,
243 bool want_import = false,
244 bool want_save = false
249 WRAP(Container, Widget, _GtkContainer, (),
251 void add(Widget widget);
253 void remove(Widget widget);
255 template<class Lambda>
256 void foreach(Lambda &&lambda);
259 WRAP(Bin, Container, _GtkBin, (),
264 WRAP(Window, Bin, _GtkWindow, (),
265 Window(window_type type);
267 Window create_dialog_window(
275 Window create_modal_dialog_window(
282 Window create_floating_window(const char *title);
284 std::uint64_t on_key_press(
285 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
286 void *extra = nullptr
289 void add_accel_group(AccelGroup group);
292 WRAP(Dialog, Window, _GtkDialog, (),
296 WRAP(Alignment, Bin, _GtkAlignment, (),
297 Alignment(float xalign, float yalign, float xscale, float yscale);
301 WRAP(Frame, Bin, _GtkFrame, (),
302 Frame(const char *label = nullptr);
306 WRAP(Button, Bin, _GtkButton, (),
308 Button(const char *label);
312 WRAP(ToggleButton, Button, _GtkToggleButton, (),
317 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
318 CheckButton(const char *label);
322 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
326 WRAP(Item, Bin, _GtkItem, (),
330 WRAP(MenuItem, Item, _GtkMenuItem, (),
332 MenuItem(const char *label, bool mnemonic = false);
336 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
340 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
344 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
349 WRAP(ComboBox, Bin, _GtkComboBox, (),
353 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
358 WRAP(ToolItem, Bin, _GtkToolItem, (),
362 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
366 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
370 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
374 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
379 WRAP(Box, Container, _GtkBox, (),
383 WRAP(VBox, Box, _GtkVBox, (),
384 VBox(bool homogenous, int spacing);
388 WRAP(HBox, Box, _GtkHBox, (),
389 HBox(bool homogenous, int spacing);
393 WRAP(Paned, Container, _GtkPaned, (),
397 WRAP(HPaned, Paned, _GtkHPaned, (),
402 WRAP(VPaned, Paned, _GtkVPaned, (),
407 WRAP(MenuShell, Container, _GtkMenuShell, (),
411 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
415 WRAP(Menu, MenuShell, _GtkMenu, (),
420 WRAP(Table, Container, _GtkTable, (),
421 Table(std::size_t rows, std::size_t columns, bool homogenous);
425 WRAP(TextView, Container, _GtkTextView, (),
430 WRAP(Toolbar, Container, _GtkToolbar, (),
435 WRAP(TreeView, Widget, _GtkTreeView, (),
437 TreeView(TreeModel model);
441 WRAP(Misc, Widget, _GtkMisc, (),
445 WRAP(Label, Widget, _GtkLabel, (),
446 Label(const char *label);
450 WRAP(Image, Widget, _GtkImage, (),
455 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
457 Entry(std::size_t max_length);
462 WRAP(SpinButton, Entry, _GtkSpinButton, (),
463 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
467 WRAP(Range, Widget, _GtkRange, (),
471 WRAP(Scale, Range, _GtkScale, (),
475 WRAP(HScale, Scale, _GtkHScale, (),
476 HScale(Adjustment adjustment);
477 HScale(double min, double max, double step);
481 WRAP(Adjustment, Object, _GtkAdjustment, (),
482 Adjustment(double value,
483 double lower, double upper,
484 double step_increment, double page_increment,
489 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
493 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
498 struct TreeViewColumnAttribute {
499 const char *attribute;
502 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
503 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
507 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
512 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
516 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
521 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
527 WRAP(TreePath, Object, _GtkTreePath, (),
529 TreePath(const char *path);
538 using GtkCallback = void (*)(_GtkWidget *, void *);
540 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
544 #define this (*static_cast<self>(this))
546 template<class Lambda>
547 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
549 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
552 template<class Lambda>
553 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
555 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
558 template<class Lambda>
559 void IContainer::foreach(Lambda &&lambda)
561 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
562 using Function = typename std::decay<Lambda>::type;
563 Function *f = static_cast<Function *>(data);
564 (*f)(Widget(widget));
566 gtk_container_foreach(this, cb, &lambda);