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 bool init(int *argc, char **argv[], char const *parameter_string, char const **error);
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) {} \
211 static name from(void *ptr) { return name((native) ptr); } \
214 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
215 static_assert(sizeof(name) == sizeof(super), "object slicing")
217 // https://developer.gnome.org/gtk2/stable/ch01.html
221 WRAP(CellEditable, Object, _GtkCellEditable, (),
225 WRAP(Editable, Object, _GtkEditable, (),
228 void editable(bool value);
231 WRAP(TreeModel, Object, _GtkTreeModel, (),
237 WRAP(Widget, Object, _GtkWidget, (),
240 alert_response alert(
242 std::string title = "NetRadiant",
243 alert_type type = alert_type::OK,
244 alert_icon icon = alert_icon::Default
246 const char *file_dialog(
249 const char *path = nullptr,
250 const char *pattern = nullptr,
251 bool want_load = false,
252 bool want_import = false,
253 bool want_save = false
258 WRAP(Container, Widget, _GtkContainer, (),
260 void add(Widget widget);
262 void remove(Widget widget);
264 template<class Lambda>
265 void foreach(Lambda &&lambda);
268 WRAP(Bin, Container, _GtkBin, (),
273 WRAP(Window, Bin, _GtkWindow, (),
274 Window(window_type type);
276 Window create_dialog_window(
284 Window create_modal_dialog_window(
291 Window create_floating_window(const char *title);
293 std::uint64_t on_key_press(
294 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
295 void *extra = nullptr
298 void add_accel_group(AccelGroup group);
301 WRAP(Dialog, Window, _GtkDialog, (),
305 WRAP(Alignment, Bin, _GtkAlignment, (),
306 Alignment(float xalign, float yalign, float xscale, float yscale);
310 WRAP(Frame, Bin, _GtkFrame, (),
311 Frame(const char *label = nullptr);
315 WRAP(Button, Bin, _GtkButton, (),
317 Button(const char *label);
321 WRAP(ToggleButton, Button, _GtkToggleButton, (),
326 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
327 CheckButton(const char *label);
331 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
335 WRAP(Item, Bin, _GtkItem, (),
339 WRAP(MenuItem, Item, _GtkMenuItem, (),
341 MenuItem(const char *label, bool mnemonic = false);
345 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
349 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
353 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
358 WRAP(ComboBox, Bin, _GtkComboBox, (),
362 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
367 WRAP(ToolItem, Bin, _GtkToolItem, (),
371 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
375 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
379 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
383 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
388 WRAP(Box, Container, _GtkBox, (),
392 WRAP(VBox, Box, _GtkVBox, (),
393 VBox(bool homogenous, int spacing);
397 WRAP(HBox, Box, _GtkHBox, (),
398 HBox(bool homogenous, int spacing);
402 WRAP(Paned, Container, _GtkPaned, (),
406 WRAP(HPaned, Paned, _GtkHPaned, (),
411 WRAP(VPaned, Paned, _GtkVPaned, (),
416 WRAP(MenuShell, Container, _GtkMenuShell, (),
420 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
424 WRAP(Menu, MenuShell, _GtkMenu, (),
429 WRAP(Table, Container, _GtkTable, (),
430 Table(std::size_t rows, std::size_t columns, bool homogenous);
434 WRAP(TextView, Container, _GtkTextView, (),
439 WRAP(Toolbar, Container, _GtkToolbar, (),
444 WRAP(TreeView, Widget, _GtkTreeView, (),
446 TreeView(TreeModel model);
450 WRAP(Misc, Widget, _GtkMisc, (),
454 WRAP(Label, Widget, _GtkLabel, (),
455 Label(const char *label);
459 WRAP(Image, Widget, _GtkImage, (),
464 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
466 Entry(std::size_t max_length);
471 WRAP(SpinButton, Entry, _GtkSpinButton, (),
472 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
476 WRAP(Range, Widget, _GtkRange, (),
480 WRAP(Scale, Range, _GtkScale, (),
484 WRAP(HScale, Scale, _GtkHScale, (),
485 HScale(Adjustment adjustment);
486 HScale(double min, double max, double step);
490 WRAP(Adjustment, Object, _GtkAdjustment, (),
491 Adjustment(double value,
492 double lower, double upper,
493 double step_increment, double page_increment,
498 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
502 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
507 struct TreeViewColumnAttribute {
508 const char *attribute;
511 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
512 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
516 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
521 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
525 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
530 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
536 WRAP(TreePath, Object, _GtkTreePath, (),
538 TreePath(const char *path);
544 WRAP(GLArea, Widget, _GtkGLArea, (),
546 guint on_render(GCallback pFunction, void *data);
554 using GtkCallback = void (*)(_GtkWidget *, void *);
556 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
560 #define this (*static_cast<self>(this))
562 template<class Lambda>
563 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
565 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
568 template<class Lambda>
569 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, Object data)
571 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, (_GtkObject *) data);
574 template<class Lambda>
575 void IContainer::foreach(Lambda &&lambda)
577 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
578 using Function = typename std::decay<Lambda>::type;
579 Function *f = static_cast<Function *>(data);
580 (*f)(Widget(widget));
582 gtk_container_foreach(this, cb, &lambda);