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 {
119 template<class Self, class T, Convert mode>
122 template<class Self, class T>
123 struct Convertible<Self, T, Convert::Implicit> {
125 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
128 template<class Self, class T>
129 struct Convertible<Self, T, Convert::Explicit> {
130 explicit operator T() const
131 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
134 template<class Self, class... T>
140 template<class Self, class Interfaces>
143 struct Mixin<Self, void()> {
144 using type = All<Self>;
146 template<class Self, class... Interfaces>
147 struct Mixin<Self, void(Interfaces...)> {
148 using type = All<Self, Interfaces...>;
152 extern struct Null {} null;
155 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
156 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
158 using native = _GtkObject *;
161 Object(native h) : _handle(h)
164 explicit operator bool() const
165 { return _handle != nullptr; }
167 explicit operator void *() const
170 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
172 #define WRAP(name, super, T, interfaces, ctors, methods) \
174 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
176 using self = name *; \
179 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
181 using self = name *; \
182 using native = T *; \
183 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
184 explicit name(Null n) : name((native) nullptr) {} \
187 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
188 static_assert(sizeof(name) == sizeof(super), "object slicing")
190 // https://developer.gnome.org/gtk2/stable/ch01.html
194 WRAP(CellEditable, Object, _GtkCellEditable, (),
198 WRAP(Editable, Object, _GtkEditable, (),
201 void editable(bool value);
204 WRAP(TreeModel, Object, _GtkTreeModel, (),
210 WRAP(Widget, Object, _GtkWidget, (),
213 alert_response alert(
215 std::string title = "NetRadiant",
216 alert_type type = alert_type::OK,
217 alert_icon icon = alert_icon::Default
219 const char *file_dialog(
222 const char *path = nullptr,
223 const char *pattern = nullptr,
224 bool want_load = false,
225 bool want_import = false,
226 bool want_save = false
231 WRAP(Container, Widget, _GtkContainer, (),
233 void add(Widget widget);
235 void remove(Widget widget);
237 template<class Lambda>
238 void foreach(Lambda &&lambda);
241 WRAP(Bin, Container, _GtkBin, (),
246 WRAP(Window, Bin, _GtkWindow, (),
247 Window(window_type type);
249 Window create_dialog_window(
257 Window create_modal_dialog_window(
264 Window create_floating_window(const char *title);
266 std::uint64_t on_key_press(
267 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
268 void *extra = nullptr
271 void add_accel_group(AccelGroup group);
274 WRAP(Dialog, Window, _GtkDialog, (),
278 WRAP(Alignment, Bin, _GtkAlignment, (),
279 Alignment(float xalign, float yalign, float xscale, float yscale);
283 WRAP(Frame, Bin, _GtkFrame, (),
284 Frame(const char *label = nullptr);
288 WRAP(Button, Bin, _GtkButton, (),
290 Button(const char *label);
294 WRAP(ToggleButton, Button, _GtkToggleButton, (),
299 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
300 CheckButton(const char *label);
304 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
308 WRAP(Item, Bin, _GtkItem, (),
312 WRAP(MenuItem, Item, _GtkMenuItem, (),
314 MenuItem(const char *label, bool mnemonic = false);
318 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
322 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
326 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
331 WRAP(ComboBox, Bin, _GtkComboBox, (),
335 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
340 WRAP(ToolItem, Bin, _GtkToolItem, (),
344 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
348 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
352 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
356 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
361 WRAP(Box, Container, _GtkBox, (),
365 WRAP(VBox, Box, _GtkVBox, (),
366 VBox(bool homogenous, int spacing);
370 WRAP(HBox, Box, _GtkHBox, (),
371 HBox(bool homogenous, int spacing);
375 WRAP(Paned, Container, _GtkPaned, (),
379 WRAP(HPaned, Paned, _GtkHPaned, (),
384 WRAP(VPaned, Paned, _GtkVPaned, (),
389 WRAP(MenuShell, Container, _GtkMenuShell, (),
393 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
397 WRAP(Menu, MenuShell, _GtkMenu, (),
402 WRAP(Table, Container, _GtkTable, (),
403 Table(std::size_t rows, std::size_t columns, bool homogenous);
407 WRAP(TextView, Container, _GtkTextView, (),
412 WRAP(Toolbar, Container, _GtkToolbar, (),
417 WRAP(TreeView, Widget, _GtkTreeView, (),
419 TreeView(TreeModel model);
423 WRAP(Misc, Widget, _GtkMisc, (),
427 WRAP(Label, Widget, _GtkLabel, (),
428 Label(const char *label);
432 WRAP(Image, Widget, _GtkImage, (),
437 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
439 Entry(std::size_t max_length);
444 WRAP(SpinButton, Entry, _GtkSpinButton, (),
445 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
449 WRAP(Range, Widget, _GtkRange, (),
453 WRAP(Scale, Range, _GtkScale, (),
457 WRAP(HScale, Scale, _GtkHScale, (),
458 HScale(Adjustment adjustment);
459 HScale(double min, double max, double step);
463 WRAP(Adjustment, Object, _GtkAdjustment, (),
464 Adjustment(double value,
465 double lower, double upper,
466 double step_increment, double page_increment,
471 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
475 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
480 struct TreeViewColumnAttribute {
481 const char *attribute;
484 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
485 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
489 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
494 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
498 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
505 WRAP(TreePath, Object, _GtkTreePath, (),
507 TreePath(const char *path);
516 using GtkCallback = void (*)(_GtkWidget *, void *);
518 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
522 #define this (*static_cast<self>(this))
524 template<class Lambda>
525 void IContainer::foreach(Lambda &&lambda)
527 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
528 using Function = typename std::decay<Lambda>::type;
529 Function *f = static_cast<Function *>(data);
530 (*f)(Widget(widget));
532 gtk_container_foreach(this, cb, &lambda);