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, (),
247 WRAP(Widget, Object, _GtkWidget, (),
249 alert_response alert(
251 std::string title = "NetRadiant",
252 alert_type type = alert_type::OK,
253 alert_icon icon = alert_icon::Default
255 const char *file_dialog(
258 const char *path = nullptr,
259 const char *pattern = nullptr,
260 bool want_load = false,
261 bool want_import = false,
262 bool want_save = false
265 void visible(bool shown);
268 Dimensions dimensions();
269 void dimensions(int width, int height);
273 WRAP(Container, Widget, _GtkContainer, (),
275 void add(Widget widget);
277 void remove(Widget widget);
279 template<class Lambda>
280 void foreach(Lambda &&lambda);
283 WRAP(Bin, Container, _GtkBin, (),
288 WRAP(Window, Bin, _GtkWindow, (),
289 explicit Window(window_type type);
291 Window create_dialog_window(
299 Window create_modal_dialog_window(
306 Window create_floating_window(const char *title);
308 std::uint64_t on_key_press(
309 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
310 void *extra = nullptr
313 void add_accel_group(AccelGroup group);
316 WRAP(Dialog, Window, _GtkDialog, (),
320 WRAP(Alignment, Bin, _GtkAlignment, (),
321 Alignment(float xalign, float yalign, float xscale, float yscale);
325 WRAP(Frame, Bin, _GtkFrame, (),
326 explicit Frame(const char *label = nullptr);
330 WRAP(Button, Bin, _GtkButton, (),
331 explicit Button(const char *label);
335 WRAP(ToggleButton, Button, _GtkToggleButton, (),
340 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
341 explicit CheckButton(const char *label);
345 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
349 WRAP(Item, Bin, _GtkItem, (),
353 WRAP(MenuItem, Item, _GtkMenuItem, (),
354 explicit MenuItem(const char *label, bool mnemonic = false);
358 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
362 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
366 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
370 WRAP(ComboBox, Bin, _GtkComboBox, (),
374 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
378 WRAP(ToolItem, Bin, _GtkToolItem, (),
382 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
386 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
390 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
394 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
396 void overflow(Policy x, Policy y);
399 WRAP(Box, Container, _GtkBox, (),
401 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
402 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
405 WRAP(VBox, Box, _GtkVBox, (),
406 VBox(bool homogenous, int spacing);
410 WRAP(HBox, Box, _GtkHBox, (),
411 HBox(bool homogenous, int spacing);
415 WRAP(Paned, Container, _GtkPaned, (),
419 WRAP(HPaned, Paned, _GtkHPaned, (),
423 WRAP(VPaned, Paned, _GtkVPaned, (),
427 WRAP(MenuShell, Container, _GtkMenuShell, (),
431 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
435 WRAP(Menu, MenuShell, _GtkMenu, (),
439 WRAP(Table, Container, _GtkTable, (),
440 Table(std::size_t rows, std::size_t columns, bool homogenous);
444 WRAP(TextView, Container, _GtkTextView, (),
446 void text(char const *str);
449 WRAP(Toolbar, Container, _GtkToolbar, (),
454 WRAP(TreeView, Widget, _GtkTreeView, (),
455 TreeView(TreeModel model);
459 WRAP(Misc, Widget, _GtkMisc, (),
463 WRAP(Label, Widget, _GtkLabel, (),
464 explicit Label(const char *label);
466 void text(char const *str);
469 WRAP(Image, Widget, _GtkImage, (),
473 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
474 explicit Entry(std::size_t max_length);
477 void text(char const *str);
481 WRAP(SpinButton, Entry, _GtkSpinButton, (),
482 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
486 WRAP(Range, Widget, _GtkRange, (),
490 WRAP(Scale, Range, _GtkScale, (),
494 WRAP(HScale, Scale, _GtkHScale, (),
495 explicit HScale(Adjustment adjustment);
496 HScale(double min, double max, double step);
500 WRAP(Adjustment, Object, _GtkAdjustment, (),
501 Adjustment(double value,
502 double lower, double upper,
503 double step_increment, double page_increment,
508 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
512 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
516 struct TreeViewColumnAttribute {
517 const char *attribute;
520 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
521 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
525 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
529 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
533 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
538 void append(T... args);
543 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
549 WRAP(TreePath, Object, _GtkTreePath, (),
550 explicit TreePath(const char *path);
556 WRAP(GLArea, Widget, _GtkGLArea, (),
558 guint on_render(GCallback pFunction, void *data);
566 using GtkCallback = void (*)(_GtkWidget *, void *);
568 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
572 #define this (*static_cast<self>(this))
574 template<class Lambda>
575 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
577 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
580 template<class Lambda>
581 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
583 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
586 template<class Lambda>
587 void IContainer::foreach(Lambda &&lambda)
589 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
590 using Function = typename std::decay<Lambda>::type;
591 Function *f = static_cast<Function *>(data);
592 (*f)(Widget(widget));
594 gtk_container_foreach(this, cb, &lambda);
599 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
604 void IListStore::append(T... args) {
605 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
606 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);