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;
71 void init(int argc, char *argv[]);
77 extern class Widget root;
79 enum class alert_type {
87 enum class alert_icon {
95 enum class alert_response {
102 enum class window_type {
127 template<class Self, class T, Convert mode>
130 template<class Self, class T>
131 struct Convertible<Self, T, Convert::Implicit> {
133 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
136 template<class Self, class T>
137 struct Convertible<Self, T, Convert::Explicit> {
138 explicit operator T() const
139 { return reinterpret_cast<T>(static_cast<Self const *>(this)->_handle); }
142 template<class Self, class... T>
148 template<class Self, class Interfaces>
151 struct Mixin<Self, void()> {
152 using type = All<Self>;
154 template<class Self, class... Interfaces>
155 struct Mixin<Self, void(Interfaces...)> {
156 using type = All<Self, Interfaces...>;
160 extern struct Null {} null;
163 public details::Convertible<Object, _GtkObject *, details::Convert::Explicit>,
164 public details::Convertible<Object, _GTypeInstance *, details::Convert::Explicit> {
166 using self = Object *;
167 using native = _GtkObject *;
170 Object(native h) : _handle(h)
173 explicit operator bool() const
174 { return _handle != nullptr; }
176 explicit operator void *() const
179 template<class Lambda>
180 gulong connect(char const *detailed_signal, Lambda &&c_handler, void *data);
182 static_assert(sizeof(Object) == sizeof(Object::native), "object slicing");
184 #define WRAP(name, super, T, interfaces, ctors, methods) \
186 class I##name : public details::Convertible<name, T *, details::Convert::Implicit> { \
188 using self = name *; \
191 class name : public super, public I##name, public details::Mixin<name, void interfaces>::type { \
193 using self = name *; \
194 using native = T *; \
195 explicit name(native h) : super(reinterpret_cast<super::native>(h)) {} \
196 explicit name(Null n) : name((native) nullptr) {} \
199 inline bool operator<(name self, name other) { return self._handle < other._handle; } \
200 static_assert(sizeof(name) == sizeof(super), "object slicing")
202 // https://developer.gnome.org/gtk2/stable/ch01.html
206 WRAP(CellEditable, Object, _GtkCellEditable, (),
210 WRAP(Editable, Object, _GtkEditable, (),
213 void editable(bool value);
216 WRAP(TreeModel, Object, _GtkTreeModel, (),
222 WRAP(Widget, Object, _GtkWidget, (),
225 alert_response alert(
227 std::string title = "NetRadiant",
228 alert_type type = alert_type::OK,
229 alert_icon icon = alert_icon::Default
231 const char *file_dialog(
234 const char *path = nullptr,
235 const char *pattern = nullptr,
236 bool want_load = false,
237 bool want_import = false,
238 bool want_save = false
243 WRAP(Container, Widget, _GtkContainer, (),
245 void add(Widget widget);
247 void remove(Widget widget);
249 template<class Lambda>
250 void foreach(Lambda &&lambda);
253 WRAP(Bin, Container, _GtkBin, (),
258 WRAP(Window, Bin, _GtkWindow, (),
259 Window(window_type type);
261 Window create_dialog_window(
269 Window create_modal_dialog_window(
276 Window create_floating_window(const char *title);
278 std::uint64_t on_key_press(
279 bool (*f)(Widget widget, _GdkEventKey *event, void *extra),
280 void *extra = nullptr
283 void add_accel_group(AccelGroup group);
286 WRAP(Dialog, Window, _GtkDialog, (),
290 WRAP(Alignment, Bin, _GtkAlignment, (),
291 Alignment(float xalign, float yalign, float xscale, float yscale);
295 WRAP(Frame, Bin, _GtkFrame, (),
296 Frame(const char *label = nullptr);
300 WRAP(Button, Bin, _GtkButton, (),
302 Button(const char *label);
306 WRAP(ToggleButton, Button, _GtkToggleButton, (),
311 WRAP(CheckButton, ToggleButton, _GtkCheckButton, (),
312 CheckButton(const char *label);
316 WRAP(RadioButton, CheckButton, _GtkRadioButton, (),
320 WRAP(Item, Bin, _GtkItem, (),
324 WRAP(MenuItem, Item, _GtkMenuItem, (),
326 MenuItem(const char *label, bool mnemonic = false);
330 WRAP(CheckMenuItem, MenuItem, _GtkCheckMenuItem, (),
334 WRAP(RadioMenuItem, CheckMenuItem, _GtkRadioMenuItem, (),
338 WRAP(TearoffMenuItem, MenuItem, _GtkTearoffMenuItem, (),
343 WRAP(ComboBox, Bin, _GtkComboBox, (),
347 WRAP(ComboBoxText, ComboBox, _GtkComboBoxText, (),
352 WRAP(ToolItem, Bin, _GtkToolItem, (),
356 WRAP(ToolButton, ToolItem, _GtkToolButton, (),
360 WRAP(ToggleToolButton, ToolButton, _GtkToggleToolButton, (),
364 WRAP(RadioToolButton, ToggleToolButton, _GtkRadioToolButton, (),
368 WRAP(ScrolledWindow, Bin, _GtkScrolledWindow, (),
373 WRAP(Box, Container, _GtkBox, (),
377 WRAP(VBox, Box, _GtkVBox, (),
378 VBox(bool homogenous, int spacing);
382 WRAP(HBox, Box, _GtkHBox, (),
383 HBox(bool homogenous, int spacing);
387 WRAP(Paned, Container, _GtkPaned, (),
391 WRAP(HPaned, Paned, _GtkHPaned, (),
396 WRAP(VPaned, Paned, _GtkVPaned, (),
401 WRAP(MenuShell, Container, _GtkMenuShell, (),
405 WRAP(MenuBar, MenuShell, _GtkMenuBar, (),
409 WRAP(Menu, MenuShell, _GtkMenu, (),
414 WRAP(Table, Container, _GtkTable, (),
415 Table(std::size_t rows, std::size_t columns, bool homogenous);
419 WRAP(TextView, Container, _GtkTextView, (),
424 WRAP(Toolbar, Container, _GtkToolbar, (),
429 WRAP(TreeView, Widget, _GtkTreeView, (),
431 TreeView(TreeModel model);
435 WRAP(Misc, Widget, _GtkMisc, (),
439 WRAP(Label, Widget, _GtkLabel, (),
440 Label(const char *label);
444 WRAP(Image, Widget, _GtkImage, (),
449 WRAP(Entry, Widget, _GtkEntry, (IEditable, ICellEditable),
451 Entry(std::size_t max_length);
456 WRAP(SpinButton, Entry, _GtkSpinButton, (),
457 SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits);
461 WRAP(Range, Widget, _GtkRange, (),
465 WRAP(Scale, Range, _GtkScale, (),
469 WRAP(HScale, Scale, _GtkHScale, (),
470 HScale(Adjustment adjustment);
471 HScale(double min, double max, double step);
475 WRAP(Adjustment, Object, _GtkAdjustment, (),
476 Adjustment(double value,
477 double lower, double upper,
478 double step_increment, double page_increment,
483 WRAP(CellRenderer, Object, _GtkCellRenderer, (),
487 WRAP(CellRendererText, CellRenderer, _GtkCellRendererText, (),
492 struct TreeViewColumnAttribute {
493 const char *attribute;
496 WRAP(TreeViewColumn, Object, _GtkTreeViewColumn, (),
497 TreeViewColumn(const char *title, CellRenderer renderer, std::initializer_list<TreeViewColumnAttribute> attributes);
501 WRAP(AccelGroup, Object, _GtkAccelGroup, (),
506 WRAP(EntryCompletion, Object, _GtkEntryCompletion, (),
510 WRAP(ListStore, Object, _GtkListStore, (ITreeModel),
515 WRAP(TreeSelection, Object, _GtkTreeSelection, (),
521 WRAP(TreePath, Object, _GtkTreePath, (),
523 TreePath(const char *path);
532 using GtkCallback = void (*)(_GtkWidget *, void *);
534 void gtk_container_foreach(_GtkContainer *, GtkCallback, void *);
538 #define this (*static_cast<self>(this))
540 template<class Lambda>
541 gulong Object::connect(char const *detailed_signal, Lambda &&c_handler, void *data)
543 return g_signal_connect(G_OBJECT(this), detailed_signal, c_handler, data);
546 template<class Lambda>
547 void IContainer::foreach(Lambda &&lambda)
549 GtkCallback cb = [](_GtkWidget *widget, void *data) -> void {
550 using Function = typename std::decay<Lambda>::type;
551 Function *f = static_cast<Function *>(data);
552 (*f)(Widget(widget));
554 gtk_container_foreach(this, cb, &lambda);