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;
72 using _GtkGLArea = struct _GtkDrawingArea;
79 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
85 extern class Widget root;
87 enum class alert_type {
95 enum class alert_icon {
103 enum class alert_response {
110 enum class window_type {
135 template<class Self, class T, Convert mode>
138 template<class Self, class T>
139 struct Convertible<Self, T, Convert::Implicit> {
141 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
144 template<class Self, class T>
145 struct Convertible<Self, T, Convert::Explicit> {
146 explicit operator T() const
147 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
150 template<class Self, class... T>
156 template<class Self, class Interfaces>
159 struct Mixin<Self, void()> {
160 using type = All<Self>;
162 template<class Self, class... Interfaces>
163 struct Mixin<Self, void(Interfaces...)> {
164 using type = All<Self, Interfaces...>;
168 extern struct Null {} null;
169 extern struct New_t {} New;
172 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
173 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
175 using self = Object *;
176 using native = _GtkObject *;
179 explicit Object(native h) : _handle(h)
182 explicit operator bool() const
183 { return _handle != nullptr; }
185 explicit operator void *() const
189 { g_object_unref(_handle); }
191 template<class Lambda>
192 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
194 template<class Lambda>
195 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
197 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
199 #define WRAP(name, super, T, interfaces, ctors, methods) \
201 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
203 using self = name *; \
206 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
208 using self = name *; \
209 using native = T *; \
210 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
211 explicit name(Null n) : name((native) nullptr) {} \
212 explicit name(New_t); \
213 static name from(void *ptr) { return name((native) ptr); } \
216 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
217 static_assert(sizeof(name) == sizeof(super), "object slicing")
219 // https://developer.gnome.org/gtk2/stable/ch01.html
223 WRAP(CellEditable, Object, _GtkCellEditable, (),
227 WRAP(Editable, Object, _GtkEditable, (),
229 void editable(bool value);
232 WRAP(TreeModel, Object, _GtkTreeModel, (),
243 WRAP(Widget, Object, _GtkWidget, (),
245 alert_response alert(
247 std::string title = "NetRadiant",
248 alert_type type = alert_type::OK,
249 alert_icon icon = alert_icon::Default
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
262 Dimensions dimensions();
263 void dimensions(int width, int height);
266 WRAP(Container, Widget, _GtkContainer, (),
268 void add(Widget widget);
270 void remove(Widget widget);
272 template<class Lambda>
273 void foreach(Lambda &&lambda);
276 WRAP(Bin, Container, _GtkBin, (),
281 WRAP(Window, Bin, _GtkWindow, (),
282 explicit Window(window_type type);
284 Window create_dialog_window(
292 Window create_modal_dialog_window(
299 Window create_floating_window(const char *title);
301 std::uint64_t on_key_press(
302 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
303 void *extra = nullptr
306 void add_accel_group(AccelGroup group);
309 WRAP(Dialog, Window, _GtkDialog, (),
313 WRAP(Alignment, Bin, _GtkAlignment, (),
314 Alignment(float xalign, float yalign, float xscale, float yscale);
318 WRAP(Frame, Bin, _GtkFrame, (),
319 explicit Frame(const char *label = nullptr);
323 WRAP(Button, Bin, _GtkButton, (),
324 explicit Button(const char *label);
328 WRAP(ToggleButton, Button, _GtkToggleButton, (),
333 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
334 explicit CheckButton(const char *label);
338 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
342 WRAP(Item, Bin, _GtkItem, (),
346 WRAP(MenuItem, Item, _GtkMenuItem, (),
347 explicit MenuItem(const char *label, bool mnemonic = false);
351 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
355 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
359 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
363 WRAP(ComboBox, Bin, _GtkComboBox, (),
367 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
371 WRAP(ToolItem, Bin, _GtkToolItem, (),
375 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
379 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
383 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
387 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
389 void overflow(Policy x, Policy y);
392 WRAP(Box, Container, _GtkBox, (),
396 WRAP(VBox, Box, _GtkVBox, (),
397 VBox(bool homogenous, int spacing);
401 WRAP(HBox, Box, _GtkHBox, (),
402 HBox(bool homogenous, int spacing);
406 WRAP(Paned, Container, _GtkPaned, (),
410 WRAP(HPaned, Paned, _GtkHPaned, (),
414 WRAP(VPaned, Paned, _GtkVPaned, (),
418 WRAP(MenuShell, Container, _GtkMenuShell, (),
422 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
426 WRAP(Menu, MenuShell, _GtkMenu, (),
430 WRAP(Table, Container, _GtkTable, (),
431 Table(std::size_t rows, std::size_t columns, bool homogenous);
435 WRAP(TextView, Container, _GtkTextView, (),
437 void text(char const *str);
440 WRAP(Toolbar, Container, _GtkToolbar, (),
445 WRAP(TreeView, Widget, _GtkTreeView, (),
446 TreeView(TreeModel model);
450 WRAP(Misc, Widget, _GtkMisc, (),
454 WRAP(Label, Widget, _GtkLabel, (),
455 explicit Label(const char *label);
457 void text(char const *str);
460 WRAP(Image, Widget, _GtkImage, (),
464 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
465 explicit Entry(std::size_t max_length);
468 void text(char const *str);
472 WRAP(SpinButton, Entry, _GtkSpinButton, (),
473 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
477 WRAP(Range, Widget, _GtkRange, (),
481 WRAP(Scale, Range, _GtkScale, (),
485 WRAP(HScale, Scale, _GtkHScale, (),
486 explicit HScale(Adjustment adjustment);
487 HScale(double min, double max, double step);
491 WRAP(Adjustment, Object, _GtkAdjustment, (),
492 Adjustment(double value,
493 double lower, double upper,
494 double step_increment, double page_increment,
499 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
503 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
507 struct TreeViewColumnAttribute {
508 const char *attribute;
511 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
512 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
516 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
520 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
524 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
529 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
535 WRAP(TreePath, Object, _GtkTreePath, (),
536 explicit TreePath(const char *path);
542 WRAP(GLArea, Widget, _GtkGLArea, (),
544 guint on_render(GCallback pFunction, void *data);
552 using GtkCallback = void (*)(_GtkWidget *, void *);
554 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
558 #define this (*static_cast<self>(this))
560 template<class Lambda>
561 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
563 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
566 template<class Lambda>
567 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
569 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
572 template<class Lambda>
573 void IContainer::foreach(Lambda &&lambda)
575 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
576 using Function = typename std::decay<Lambda>::type;
577 Function *f = static_cast<Function *>(data);
578 (*f)(Widget(widget));
580 gtk_container_foreach(this, cb, &lambda);