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;
41 struct _GtkRadioButton;
42 struct _GtkRadioMenuItem;
43 struct _GtkRadioToolButton;
46 struct _GtkScrolledWindow;
47 struct _GtkSpinButton;
49 struct _GtkTearoffMenuItem;
51 struct _GtkToggleButton;
52 struct _GtkToggleToolButton;
54 struct _GtkToolButton;
58 struct _GtkTreeSelection;
60 struct _GtkTreeViewColumn;
65 struct _GTypeInstance;
72 using _GtkGLArea = struct _GtkDrawingArea;
79 void init(int argc, char *argv[]);
85 extern class Widget root;
87 enum class alert_type {
95 enum class alert_icon {
103 enum class alert_response {
110 enum class window_type {
135 template<class Self, class T, Convert mode>
138 template<class Self, class T>
139 struct Convertible<Self, T, Convert::Implicit> {
141 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
144 template<class Self, class T>
145 struct Convertible<Self, T, Convert::Explicit> {
146 explicit operator T() const
147 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
150 template<class Self, class... T>
156 template<class Self, class Interfaces>
159 struct Mixin<Self, void()> {
160 using type = All<Self>;
162 template<class Self, class... Interfaces>
163 struct Mixin<Self, void(Interfaces...)> {
164 using type = All<Self, Interfaces...>;
168 extern struct Null {} null;
171 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
172 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
174 using self = Object *;
175 using native = _GtkObject *;
178 Object(native h) : _handle(h)
181 explicit operator bool() const
182 { return _handle != nullptr; }
184 explicit operator void *() const
188 { g_object_unref(_handle); }
190 template<class Lambda>
191 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
193 template<class Lambda>
194 gulong connect(char const *detailed_signal, Lambda &&c_handler, Object data);
196 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
198 #define WRAP(name, super, T, interfaces, ctors, methods) \
200 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
202 using self = name *; \
205 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
207 using self = name *; \
208 using native = T *; \
209 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
210 explicit name(Null n) : name((native) nullptr) {} \
213 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
214 static_assert(sizeof(name) == sizeof(super), "object slicing")
216 // https://developer.gnome.org/gtk2/stable/ch01.html
220 WRAP(CellEditable, Object, _GtkCellEditable, (),
224 WRAP(Editable, Object, _GtkEditable, (),
227 void editable(bool value);
230 WRAP(TreeModel, Object, _GtkTreeModel, (),
236 WRAP(Widget, Object, _GtkWidget, (),
239 alert_response alert(
241 std::string title = "NetRadiant",
242 alert_type type = alert_type::OK,
243 alert_icon icon = alert_icon::Default
245 const char *file_dialog(
248 const char *path = nullptr,
249 const char *pattern = nullptr,
250 bool want_load = false,
251 bool want_import = false,
252 bool want_save = false
257 WRAP(Container, Widget, _GtkContainer, (),
259 void add(Widget widget);
261 void remove(Widget widget);
263 template<class Lambda>
264 void foreach(Lambda &&lambda);
267 WRAP(Bin, Container, _GtkBin, (),
272 WRAP(Window, Bin, _GtkWindow, (),
273 Window(window_type type);
275 Window create_dialog_window(
283 Window create_modal_dialog_window(
290 Window create_floating_window(const char *title);
292 std::uint64_t on_key_press(
293 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
294 void *extra = nullptr
297 void add_accel_group(AccelGroup group);
300 WRAP(Dialog, Window, _GtkDialog, (),
304 WRAP(Alignment, Bin, _GtkAlignment, (),
305 Alignment(float xalign, float yalign, float xscale, float yscale);
309 WRAP(Frame, Bin, _GtkFrame, (),
310 Frame(const char *label = nullptr);
314 WRAP(Button, Bin, _GtkButton, (),
316 Button(const char *label);
320 WRAP(ToggleButton, Button, _GtkToggleButton, (),
325 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
326 CheckButton(const char *label);
330 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
334 WRAP(Item, Bin, _GtkItem, (),
338 WRAP(MenuItem, Item, _GtkMenuItem, (),
340 MenuItem(const char *label, bool mnemonic = false);
344 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
348 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
352 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
357 WRAP(ComboBox, Bin, _GtkComboBox, (),
361 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
366 WRAP(ToolItem, Bin, _GtkToolItem, (),
370 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
374 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
378 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
382 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
387 WRAP(Box, Container, _GtkBox, (),
391 WRAP(VBox, Box, _GtkVBox, (),
392 VBox(bool homogenous, int spacing);
396 WRAP(HBox, Box, _GtkHBox, (),
397 HBox(bool homogenous, int spacing);
401 WRAP(Paned, Container, _GtkPaned, (),
405 WRAP(HPaned, Paned, _GtkHPaned, (),
410 WRAP(VPaned, Paned, _GtkVPaned, (),
415 WRAP(MenuShell, Container, _GtkMenuShell, (),
419 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
423 WRAP(Menu, MenuShell, _GtkMenu, (),
428 WRAP(Table, Container, _GtkTable, (),
429 Table(std::size_t rows, std::size_t columns, bool homogenous);
433 WRAP(TextView, Container, _GtkTextView, (),
438 WRAP(Toolbar, Container, _GtkToolbar, (),
443 WRAP(TreeView, Widget, _GtkTreeView, (),
445 TreeView(TreeModel model);
449 WRAP(Misc, Widget, _GtkMisc, (),
453 WRAP(Label, Widget, _GtkLabel, (),
454 Label(const char *label);
458 WRAP(Image, Widget, _GtkImage, (),
463 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
465 Entry(std::size_t max_length);
470 WRAP(SpinButton, Entry, _GtkSpinButton, (),
471 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
475 WRAP(Range, Widget, _GtkRange, (),
479 WRAP(Scale, Range, _GtkScale, (),
483 WRAP(HScale, Scale, _GtkHScale, (),
484 HScale(Adjustment adjustment);
485 HScale(double min, double max, double step);
489 WRAP(Adjustment, Object, _GtkAdjustment, (),
490 Adjustment(double value,
491 double lower, double upper,
492 double step_increment, double page_increment,
497 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
501 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
506 struct TreeViewColumnAttribute {
507 const char *attribute;
510 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
511 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
515 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
520 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
524 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
529 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
535 WRAP(TreePath, Object, _GtkTreePath, (),
537 TreePath(const char *path);
543 WRAP(GLArea, Widget, _GtkGLArea, (),
545 guint on_render(GCallback pFunction, void *data);
553 using GtkCallback = void (*)(_GtkWidget *, void *);
555 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
559 #define this (*static_cast<self>(this))
561 template<class Lambda>
562 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
564 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
567 template<class Lambda>
568 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
570 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
573 template<class Lambda>
574 void IContainer::foreach(Lambda &&lambda)
576 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
577 using Function = typename std::decay<Lambda>::type;
578 Function *f = static_cast<Function *>(data);
579 (*f)(Widget(widget));
581 gtk_container_foreach(this, cb, &lambda);