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 {
125 template<class Self, class T, Convert mode>
128 template<class Self, class T>
129 struct Convertible<Self, T, Convert::Implicit> {
131 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
134 template<class Self, class T>
135 struct Convertible<Self, T, Convert::Explicit> {
136 explicit operator T() const
137 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
140 template<class Self, class... T>
146 template<class Self, class Interfaces>
149 struct Mixin<Self, void()> {
150 using type = All<Self>;
152 template<class Self, class... Interfaces>
153 struct Mixin<Self, void(Interfaces...)> {
154 using type = All<Self, Interfaces...>;
158 extern struct Null {} null;
161 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
162 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
164 using native = _GtkObject *;
167 Object(native h) : _handle(h)
170 explicit operator bool() const
171 { return _handle != nullptr; }
173 explicit operator void *() const
176 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
178 #define WRAP(name, super, T, interfaces, ctors, methods) \
180 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
182 using self = name *; \
185 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
187 using self = name *; \
188 using native = T *; \
189 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
190 explicit name(Null n) : name((native) nullptr) {} \
193 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
194 static_assert(sizeof(name) == sizeof(super), "object slicing")
196 // https://developer.gnome.org/gtk2/stable/ch01.html
200 WRAP(CellEditable, Object, _GtkCellEditable, (),
204 WRAP(Editable, Object, _GtkEditable, (),
207 void editable(bool value);
210 WRAP(TreeModel, Object, _GtkTreeModel, (),
216 WRAP(Widget, Object, _GtkWidget, (),
219 alert_response alert(
221 std::string title = "NetRadiant",
222 alert_type type = alert_type::OK,
223 alert_icon icon = alert_icon::Default
225 const char *file_dialog(
228 const char *path = nullptr,
229 const char *pattern = nullptr,
230 bool want_load = false,
231 bool want_import = false,
232 bool want_save = false
237 WRAP(Container, Widget, _GtkContainer, (),
239 void add(Widget widget);
241 void remove(Widget widget);
243 template<class Lambda>
244 void foreach(Lambda &&lambda);
247 WRAP(Bin, Container, _GtkBin, (),
252 WRAP(Window, Bin, _GtkWindow, (),
253 Window(window_type type);
255 Window create_dialog_window(
263 Window create_modal_dialog_window(
270 Window create_floating_window(const char *title);
272 std::uint64_t on_key_press(
273 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
274 void *extra = nullptr
277 void add_accel_group(AccelGroup group);
280 WRAP(Dialog, Window, _GtkDialog, (),
284 WRAP(Alignment, Bin, _GtkAlignment, (),
285 Alignment(float xalign, float yalign, float xscale, float yscale);
289 WRAP(Frame, Bin, _GtkFrame, (),
290 Frame(const char *label = nullptr);
294 WRAP(Button, Bin, _GtkButton, (),
296 Button(const char *label);
300 WRAP(ToggleButton, Button, _GtkToggleButton, (),
305 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
306 CheckButton(const char *label);
310 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
314 WRAP(Item, Bin, _GtkItem, (),
318 WRAP(MenuItem, Item, _GtkMenuItem, (),
320 MenuItem(const char *label, bool mnemonic = false);
324 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
328 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
332 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
337 WRAP(ComboBox, Bin, _GtkComboBox, (),
341 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
346 WRAP(ToolItem, Bin, _GtkToolItem, (),
350 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
354 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
358 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
362 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
367 WRAP(Box, Container, _GtkBox, (),
371 WRAP(VBox, Box, _GtkVBox, (),
372 VBox(bool homogenous, int spacing);
376 WRAP(HBox, Box, _GtkHBox, (),
377 HBox(bool homogenous, int spacing);
381 WRAP(Paned, Container, _GtkPaned, (),
385 WRAP(HPaned, Paned, _GtkHPaned, (),
390 WRAP(VPaned, Paned, _GtkVPaned, (),
395 WRAP(MenuShell, Container, _GtkMenuShell, (),
399 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
403 WRAP(Menu, MenuShell, _GtkMenu, (),
408 WRAP(Table, Container, _GtkTable, (),
409 Table(std::size_t rows, std::size_t columns, bool homogenous);
413 WRAP(TextView, Container, _GtkTextView, (),
418 WRAP(Toolbar, Container, _GtkToolbar, (),
423 WRAP(TreeView, Widget, _GtkTreeView, (),
425 TreeView(TreeModel model);
429 WRAP(Misc, Widget, _GtkMisc, (),
433 WRAP(Label, Widget, _GtkLabel, (),
434 Label(const char *label);
438 WRAP(Image, Widget, _GtkImage, (),
443 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
445 Entry(std::size_t max_length);
450 WRAP(SpinButton, Entry, _GtkSpinButton, (),
451 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
455 WRAP(Range, Widget, _GtkRange, (),
459 WRAP(Scale, Range, _GtkScale, (),
463 WRAP(HScale, Scale, _GtkHScale, (),
464 HScale(Adjustment adjustment);
465 HScale(double min, double max, double step);
469 WRAP(Adjustment, Object, _GtkAdjustment, (),
470 Adjustment(double value,
471 double lower, double upper,
472 double step_increment, double page_increment,
477 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
481 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
486 struct TreeViewColumnAttribute {
487 const char *attribute;
490 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
491 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
495 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
500 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
504 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
511 WRAP(TreePath, Object, _GtkTreePath, (),
513 TreePath(const char *path);
522 using GtkCallback = void (*)(_GtkWidget *, void *);
524 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
528 #define this (*static_cast<self>(this))
530 template<class Lambda>
531 void IContainer::foreach(Lambda &&lambda)
533 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
534 using Function = typename std::decay<Lambda>::type;
535 Function *f = static_cast<Function *>(data);
536 (*f)(Widget(widget));
538 gtk_container_foreach(this, cb, &lambda);