1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
6 #include <glib-object.h>
10 struct _GtkAdjustment;
15 struct _GtkCellEditable;
16 struct _GtkCellRenderer;
17 struct _GtkCellRendererText;
18 struct _GtkCheckButton;
19 struct _GtkCheckMenuItem;
21 struct _GtkComboBoxText;
26 struct _GtkEntryCompletion;
43 struct _GtkRadioButton;
44 struct _GtkRadioMenuItem;
45 struct _GtkRadioToolButton;
48 struct _GtkScrolledWindow;
49 struct _GtkSpinButton;
51 struct _GtkTearoffMenuItem;
53 struct _GtkToggleButton;
54 struct _GtkToggleToolButton;
56 struct _GtkToolButton;
60 struct _GtkTreeSelection;
63 struct _GtkTreeViewColumn;
68 struct _GTypeInstance;
75 using _GtkGLArea = struct _GtkDrawingArea;
82 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
88 enum class window_type {
118 template<class Self, class T, Convert mode>
121 template<class Self, class T>
122 struct Convertible<Self, T, Convert::Implicit> {
124 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
127 template<class Self, class T>
128 struct Convertible<Self, T, Convert::Explicit> {
129 explicit operator T() const
130 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
133 template<class Self, class... T>
139 template<class Self, class Interfaces>
142 struct Mixin<Self, void()> {
143 using type = All<Self>;
145 template<class Self, class... Interfaces>
146 struct Mixin<Self, void(Interfaces...)> {
147 using type = All<Self, Interfaces...>;
151 const struct Null {} null = {};
152 const struct New_t {} New = {};
155 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
156 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
158 using self = Object *;
159 using native = _GtkObject *;
162 explicit Object(native h) : _handle(h)
165 explicit operator bool() const
166 { return _handle != nullptr; }
168 explicit operator void *() const
172 { g_object_unref(_handle); }
175 { g_object_ref(_handle); }
177 template<class Lambda>
178 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
180 template<class Lambda>
181 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
183 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
185 #define WRAP(name, super, T, interfaces, ctors, methods) \
187 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
189 using self = name *; \
192 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
194 using self = name *; \
195 using native = T *; \
197 explicit name(native h) noexcept : super(reinterpret_cast<super::native>(h)) {} \
199 explicit name(Null n) noexcept : name((native) nullptr) {} \
200 explicit name(New_t); \
201 static name from(native h) { return name(h); } \
202 static name from(void *ptr) { return name((native) ptr); } \
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, (),
218 void editable(bool value);
221 WRAP(TreeModel, Object, _GtkTreeModel, (),
233 WRAP(Widget, Object, _GtkWidget, (),
236 const char *file_dialog(
239 const char *path = nullptr,
240 const char *pattern = nullptr,
241 bool want_load = false,
242 bool want_import = false,
243 bool want_save = false
246 void visible(bool shown);
249 Dimensions dimensions();
250 void dimensions(int width, int height);
254 WRAP(Container, Widget, _GtkContainer, (),
256 void add(Widget widget);
258 void remove(Widget widget);
260 template<class Lambda>
261 void foreach(Lambda &&lambda);
264 WRAP(Bin, Container, _GtkBin, (),
269 WRAP(Window, Bin, _GtkWindow, (),
270 explicit Window(window_type type);
272 Window create_dialog_window(
280 Window create_modal_dialog_window(
287 Window create_floating_window(const char *title);
289 std::uint64_t on_key_press(
290 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
291 void *extra = nullptr
294 void add_accel_group(AccelGroup group);
297 WRAP(Dialog, Window, _GtkDialog, (),
301 WRAP(Alignment, Bin, _GtkAlignment, (),
302 Alignment(float xalign, float yalign, float xscale, float yscale);
306 WRAP(Frame, Bin, _GtkFrame, (),
307 explicit Frame(const char *label = nullptr);
311 WRAP(Button, Bin, _GtkButton, (),
312 explicit Button(const char *label);
316 WRAP(ToggleButton, Button, _GtkToggleButton, (),
319 void active(bool value);
322 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
323 explicit CheckButton(const char *label);
327 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
331 WRAP(Item, Bin, _GtkItem, (),
335 WRAP(MenuItem, Item, _GtkMenuItem, (),
336 explicit MenuItem(const char *label, bool mnemonic = false);
340 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
344 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
348 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
352 WRAP(ComboBox, Bin, _GtkComboBox, (),
356 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
360 WRAP(ToolItem, Bin, _GtkToolItem, (),
364 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
368 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
372 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
376 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
378 void overflow(Policy x, Policy y);
381 WRAP(Box, Container, _GtkBox, (),
383 void pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding);
384 void pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding);
385 void set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing);
388 WRAP(VBox, Box, _GtkVBox, (),
389 VBox(bool homogenous, int spacing);
393 WRAP(HBox, Box, _GtkHBox, (),
394 HBox(bool homogenous, int spacing);
398 WRAP(Paned, Container, _GtkPaned, (),
402 WRAP(HPaned, Paned, _GtkHPaned, (),
406 WRAP(VPaned, Paned, _GtkVPaned, (),
410 WRAP(MenuShell, Container, _GtkMenuShell, (),
414 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
418 WRAP(Menu, MenuShell, _GtkMenu, (),
423 unsigned int left, right, top, bottom;
426 struct TableAttachOptions {
431 struct TablePadding {
435 WRAP(Table, Container, _GtkTable, (),
436 Table(std::size_t rows, std::size_t columns, bool homogenous);
439 void attach(Widget child, TableAttach attach, TableAttachOptions options = {5, 5}, TablePadding padding = {0, 0});
442 WRAP(TextView, Container, _GtkTextView, (),
444 void text(char const *str);
447 WRAP(Toolbar, Container, _GtkToolbar, (),
452 WRAP(TreeView, Widget, _GtkTreeView, (),
453 TreeView(TreeModel model);
457 WRAP(Misc, Widget, _GtkMisc, (),
461 WRAP(Label, Widget, _GtkLabel, (),
462 explicit Label(const char *label);
464 void text(char const *str);
467 WRAP(Image, Widget, _GtkImage, (),
471 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
472 explicit Entry(std::size_t max_length);
475 void text(char const *str);
479 WRAP(SpinButton, Entry, _GtkSpinButton, (),
480 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
484 WRAP(Range, Widget, _GtkRange, (),
488 WRAP(Scale, Range, _GtkScale, (),
492 WRAP(HScale, Scale, _GtkHScale, (),
493 explicit HScale(Adjustment adjustment);
494 HScale(double min, double max, double step);
498 WRAP(Adjustment, Object, _GtkAdjustment, (),
499 Adjustment(double value,
500 double lower, double upper,
501 double step_increment, double page_increment,
506 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
510 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
514 struct TreeViewColumnAttribute {
515 const char *attribute;
518 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
519 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
523 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
527 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
531 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
536 void append(T... args);
541 WRAP(TreeStore, Object, _GtkTreeStore, (ITreeModel),
545 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
551 WRAP(TreePath, Object, _GtkTreePath, (),
552 explicit TreePath(const char *path);
558 WRAP(GLArea, Widget, _GtkGLArea, (),
560 guint on_render(GCallback pFunction, void *data);
567 enum class alert_response {
574 enum class alert_type {
582 enum class alert_icon {
590 extern class Window root;
592 alert_response alert(
595 std::string title = RADIANT_NAME,
596 alert_type type = alert_type::OK,
597 alert_icon icon = alert_icon::Default
603 using GtkCallback = void (*)(_GtkWidget *, void *);
605 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
609 #define this (*static_cast<self>(this))
611 template<class Lambda>
612 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
614 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
617 template<class Lambda>
618 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
620 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
623 template<class Lambda>
624 void IContainer::foreach(Lambda &&lambda)
626 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
627 using Function = typename std::decay<Lambda>::type;
628 Function *f = static_cast<Function *>(data);
629 (*f)(Widget::from(widget));
631 gtk_container_foreach(this, cb, &lambda);
636 void gtk_list_store_insert_with_values(_GtkListStore *, _GtkTreeIter *, gint position, ...);
641 void IListStore::append(T... args) {
642 static_assert(sizeof...(args) % 2 == 0, "received an odd number of arguments");
643 gtk_list_store_insert_with_values(this, NULL, -1, args..., -1);