1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
13 struct _GtkCellEditable;
14 struct _GtkCellRenderer;
15 struct _GtkCellRendererText;
16 struct _GtkCheckButton;
17 struct _GtkCheckMenuItem;
19 struct _GtkComboBoxText;
24 struct _GtkEntryCompletion;
40 struct _GtkRadioButton;
41 struct _GtkRadioMenuItem;
42 struct _GtkRadioToolButton;
45 struct _GtkScrolledWindow;
46 struct _GtkSpinButton;
48 struct _GtkTearoffMenuItem;
50 struct _GtkToggleButton;
51 struct _GtkToggleToolButton;
53 struct _GtkToolButton;
58 struct _GtkTreeViewColumn;
63 struct _GTypeInstance;
69 void init(int argc, char *argv[]);
75 extern class Widget root;
77 enum class alert_type {
85 enum class alert_icon {
93 enum class alert_response {
100 enum class window_type {
111 template<class Self, class T, Convert mode>
114 template<class Self, class T>
115 struct Convertible<Self, T, Convert::Implicit> {
117 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
120 template<class Self, class T>
121 struct Convertible<Self, T, Convert::Explicit> {
122 explicit operator T() const
123 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
126 template<class Self, class... T>
132 template<class Self, class Interfaces>
135 struct Mixin<Self, void()> {
136 using type = All<Self>;
138 template<class Self, class... Interfaces>
139 struct Mixin<Self, void(Interfaces...)> {
140 using type = All<Self, Interfaces...>;
144 extern struct Null {} null;
147 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
148 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
150 using native = _GtkObject *;
153 Object(native h) : _handle(h)
156 explicit operator bool() const
157 { return _handle != nullptr; }
159 explicit operator void *() const
162 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
164 #define WRAP(name, super, T, interfaces, ctors, methods) \
166 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
168 using self = name *; \
171 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
173 using self = name *; \
174 using native = T *; \
175 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
176 explicit name(Null n) : name((native) nullptr) {} \
179 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
180 static_assert(sizeof(name) == sizeof(super), "object slicing")
182 // https://developer.gnome.org/gtk2/stable/ch01.html
186 WRAP(CellEditable, Object, _GtkCellEditable, (),
190 WRAP(Editable, Object, _GtkEditable, (),
193 void editable(bool value);
196 WRAP(TreeModel, Object, _GtkTreeModel, (),
202 WRAP(Widget, Object, _GtkWidget, (),
205 alert_response alert(
207 std::string title = "NetRadiant",
208 alert_type type = alert_type::OK,
209 alert_icon icon = alert_icon::Default
211 const char *file_dialog(
214 const char *path = nullptr,
215 const char *pattern = nullptr,
216 bool want_load = false,
217 bool want_import = false,
218 bool want_save = false
223 WRAP(Container, Widget, _GtkContainer, (),
225 void add(Widget widget);
227 void remove(Widget widget);
229 template<class Lambda>
230 void foreach(Lambda &&lambda);
233 WRAP(Bin, Container, _GtkBin, (),
238 WRAP(Window, Bin, _GtkWindow, (),
239 Window(window_type type);
241 Window create_dialog_window(
249 Window create_modal_dialog_window(
256 Window create_floating_window(const char *title);
258 std::uint64_t on_key_press(
259 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
260 void *extra = nullptr
263 void add_accel_group(AccelGroup group);
266 WRAP(Dialog, Window, _GtkDialog, (),
270 WRAP(Alignment, Bin, _GtkAlignment, (),
271 Alignment(float xalign, float yalign, float xscale, float yscale);
275 WRAP(Frame, Bin, _GtkFrame, (),
276 Frame(const char *label = nullptr);
280 WRAP(Button, Bin, _GtkButton, (),
282 Button(const char *label);
286 WRAP(ToggleButton, Button, _GtkToggleButton, (),
291 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
292 CheckButton(const char *label);
296 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
300 WRAP(Item, Bin, _GtkItem, (),
304 WRAP(MenuItem, Item, _GtkMenuItem, (),
306 MenuItem(const char *label, bool mnemonic = false);
310 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
314 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
318 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
323 WRAP(ComboBox, Bin, _GtkComboBox, (),
327 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
332 WRAP(ToolItem, Bin, _GtkToolItem, (),
336 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
340 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
344 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
348 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
353 WRAP(Box, Container, _GtkBox, (),
357 WRAP(VBox, Box, _GtkVBox, (),
358 VBox(bool homogenous, int spacing);
362 WRAP(HBox, Box, _GtkHBox, (),
363 HBox(bool homogenous, int spacing);
367 WRAP(Paned, Container, _GtkPaned, (),
371 WRAP(HPaned, Paned, _GtkHPaned, (),
376 WRAP(VPaned, Paned, _GtkVPaned, (),
381 WRAP(MenuShell, Container, _GtkMenuShell, (),
385 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
389 WRAP(Menu, MenuShell, _GtkMenu, (),
394 WRAP(Table, Container, _GtkTable, (),
395 Table(std::size_t rows, std::size_t columns, bool homogenous);
399 WRAP(TextView, Container, _GtkTextView, (),
404 WRAP(Toolbar, Container, _GtkToolbar, (),
409 WRAP(TreeView, Widget, _GtkTreeView, (),
411 TreeView(TreeModel model);
415 WRAP(Misc, Widget, _GtkMisc, (),
419 WRAP(Label, Widget, _GtkLabel, (),
420 Label(const char *label);
424 WRAP(Image, Widget, _GtkImage, (),
429 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
431 Entry(std::size_t max_length);
436 WRAP(SpinButton, Entry, _GtkSpinButton, (),
437 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
441 WRAP(Range, Widget, _GtkRange, (),
445 WRAP(Scale, Range, _GtkScale, (),
449 WRAP(HScale, Scale, _GtkHScale, (),
450 HScale(Adjustment adjustment);
451 HScale(double min, double max, double step);
455 WRAP(Adjustment, Object, _GtkAdjustment, (),
456 Adjustment(double value,
457 double lower, double upper,
458 double step_increment, double page_increment,
463 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
467 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
472 struct TreeViewColumnAttribute {
473 const char *attribute;
476 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
477 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
481 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
486 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
490 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
497 WRAP(TreePath, Object, _GtkTreePath, (),
499 TreePath(const char *path);
508 using GtkCallback = void (*)(_GtkWidget *, void *);
510 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
514 #define this (*static_cast<self>(this))
516 template<class Lambda>
517 void IContainer::foreach(Lambda &&lambda)
519 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
520 using Function = typename std::decay<Lambda>::type;
521 Function *f = static_cast<Function *>(data);
522 (*f)(Widget(widget));
524 gtk_container_foreach(this, cb, &lambda);