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;
62 struct _GtkTreeViewColumn;
67 struct _GTypeInstance;
74 using _GtkGLArea = struct _GtkDrawingArea;
81 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
87 enum class window_type {
117 template<class Self, class T, Convert mode>
120 template<class Self, class T>
121 struct Convertible<Self, T, Convert::Implicit> {
123 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
126 template<class Self, class T>
127 struct Convertible<Self, T, Convert::Explicit> {
128 explicit operator T() const
129 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
132 template<class Self, class... T>
138 template<class Self, class Interfaces>
141 struct Mixin<Self, void()> {
142 using type = All<Self>;
144 template<class Self, class... Interfaces>
145 struct Mixin<Self, void(Interfaces...)> {
146 using type = All<Self, Interfaces...>;
150 const struct Null {} null = {};
151 const struct New_t {} New = {};
154 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
155 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
157 using self = Object *;
158 using native = _GtkObject *;
161 explicit Object(native h) : _handle(h)
164 explicit operator bool() const
165 { return _handle != nullptr; }
167 explicit operator void *() const
171 { g_object_unref(_handle); }
174 { g_object_ref(_handle); }
176 template<class Lambda>
177 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
179 template<class Lambda>
180 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
182 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
184 #define WRAP(name, super, T, interfaces, ctors, methods) \
186 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
188 using self = name *; \
191 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
193 using self = name *; \
194 using native = T *; \
196 explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
198 explicit name(Null n) noexcept : name((native) nullptr) {} \
199 explicit name(New_t); \
200 static name from(native h) { return name(h); } \
201 static name from(void *ptr) { return name((native) ptr); } \
204 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
205 static_assert(sizeof(name) == sizeof(super), "object slicing")
207 // https://developer.gnome.org/gtk2/stable/ch01.html
211 WRAP(CellEditable, Object, _GtkCellEditable, (),
215 WRAP(Editable, Object, _GtkEditable, (),
217 void editable(bool value);
220 WRAP(TreeModel, Object, _GtkTreeModel, (),
232 WRAP(Widget, Object, _GtkWidget, (),
235 const char *file_dialog(
238 const char *path = nullptr,
239 const char *pattern = nullptr,
240 bool want_load = false,
241 bool want_import = false,
242 bool want_save = false
245 void visible(bool shown);
248 Dimensions dimensions();
249 void dimensions(int width, int height);
253 WRAP(Container, Widget, _GtkContainer, (),
255 void add(Widget widget);
257 void remove(Widget widget);
259 template<class Lambda>
260 void foreach(Lambda &&lambda);
263 WRAP(Bin, Container, _GtkBin, (),
268 WRAP(Window, Bin, _GtkWindow, (),
269 explicit Window(window_type type);
271 Window create_dialog_window(
279 Window create_modal_dialog_window(
286 Window create_floating_window(const char *title);
288 std::uint64_t on_key_press(
289 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
290 void *extra = nullptr
293 void add_accel_group(AccelGroup group);
296 WRAP(Dialog, Window, _GtkDialog, (),
300 WRAP(Alignment, Bin, _GtkAlignment, (),
301 Alignment(float xalign, float yalign, float xscale, float yscale);
305 WRAP(Frame, Bin, _GtkFrame, (),
306 explicit Frame(const char *label = nullptr);
310 WRAP(Button, Bin, _GtkButton, (),
311 explicit Button(const char *label);
315 WRAP(ToggleButton, Button, _GtkToggleButton, (),
318 void active(bool value);
321 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
322 explicit CheckButton(const char *label);
326 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
330 WRAP(Item, Bin, _GtkItem, (),
334 WRAP(MenuItem, Item, _GtkMenuItem, (),
335 explicit MenuItem(const char *label, bool mnemonic = false);
339 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
343 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
347 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
351 WRAP(ComboBox, Bin, _GtkComboBox, (),
355 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
359 WRAP(ToolItem, Bin, _GtkToolItem, (),
363 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
367 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
371 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
375 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
377 void overflow(Policy x, Policy y);
380 WRAP(Box, Container, _GtkBox, (),
382 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
383 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
384 void set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing);
387 WRAP(VBox, Box, _GtkVBox, (),
388 VBox(bool homogenous, int spacing);
392 WRAP(HBox, Box, _GtkHBox, (),
393 HBox(bool homogenous, int spacing);
397 WRAP(Paned, Container, _GtkPaned, (),
401 WRAP(HPaned, Paned, _GtkHPaned, (),
405 WRAP(VPaned, Paned, _GtkVPaned, (),
409 WRAP(MenuShell, Container, _GtkMenuShell, (),
413 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
417 WRAP(Menu, MenuShell, _GtkMenu, (),
422 unsigned int left, right, top, bottom;
425 struct TableAttachOptions {
430 struct TablePadding {
434 WRAP(Table, Container, _GtkTable, (),
435 Table(std::size_t rows, std::size_t columns, bool homogenous);
438 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
441 WRAP(TextView, Container, _GtkTextView, (),
443 void text(char const *str);
446 WRAP(Toolbar, Container, _GtkToolbar, (),
451 WRAP(TreeView, Widget, _GtkTreeView, (),
452 TreeView(TreeModel model);
456 WRAP(Misc, Widget, _GtkMisc, (),
460 WRAP(Label, Widget, _GtkLabel, (),
461 explicit Label(const char *label);
463 void text(char const *str);
466 WRAP(Image, Widget, _GtkImage, (),
470 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
471 explicit Entry(std::size_t max_length);
474 void text(char const *str);
478 WRAP(SpinButton, Entry, _GtkSpinButton, (),
479 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
483 WRAP(Range, Widget, _GtkRange, (),
487 WRAP(Scale, Range, _GtkScale, (),
491 WRAP(HScale, Scale, _GtkHScale, (),
492 explicit HScale(Adjustment adjustment);
493 HScale(double min, double max, double step);
497 WRAP(Adjustment, Object, _GtkAdjustment, (),
498 Adjustment(double value,
499 double lower, double upper,
500 double step_increment, double page_increment,
505 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
509 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
513 struct TreeViewColumnAttribute {
514 const char *attribute;
517 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
518 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
522 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
526 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
530 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
535 void append(T... args);
540 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
544 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
550 WRAP(TreePath, Object, _GtkTreePath, (),
551 explicit TreePath(const char *path);
557 WRAP(GLArea, Widget, _GtkGLArea, (),
559 guint on_render(GCallback pFunction, void *data);
566 enum class alert_response {
573 enum class alert_type {
581 enum class alert_icon {
589 extern class Window root;
591 alert_response alert(
594 std::string title = RADIANT_NAME,
595 alert_type type = alert_type::OK,
596 alert_icon icon = alert_icon::Default
602 using GtkCallback = void (*)(_GtkWidget *, void *);
604 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
608 #define this (*static_cast<self>(this))
610 template<class Lambda>
611 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
613 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
616 template<class Lambda>
617 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
619 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
622 template<class Lambda>
623 void IContainer::foreach(Lambda &&lambda)
625 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
626 using Function = typename std::decay<Lambda>::type;
627 Function *f = static_cast<Function *>(data);
628 (*f)(Widget::from(widget));
630 gtk_container_foreach(this, cb, &lambda);
635 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
640 void IListStore::append(T... args) {
641 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
642 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);