]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.h
Wrap GtkScrolledWindow
[xonotic/netradiant.git] / libs / uilib / uilib.h
1 #ifndef INCLUDED_UILIB_H
2 #define INCLUDED_UILIB_H
3
4 #include <string>
5
6 using ui_alignment = struct _GtkAlignment;
7 using ui_button = struct _GtkButton;
8 using ui_checkbutton = struct _GtkCheckButton;
9 using ui_evkey = struct _GdkEventKey;
10 using ui_label = struct _GtkLabel;
11 using ui_menuitem = struct _GtkMenuItem;
12 using ui_modal = struct ModalDialog;
13 using ui_scrolledwindow = struct _GtkScrolledWindow;
14 using ui_treemodel = struct _GtkTreeModel;
15 using ui_treeview = struct _GtkTreeView;
16 using ui_typeinst = struct _GTypeInstance;
17 using ui_vbox = struct _GtkVBox;
18 using ui_widget = struct _GtkWidget;
19 using ui_window = struct _GtkWindow;
20
21 namespace ui {
22
23     void init(int argc, char *argv[]);
24
25     void main();
26
27     enum class alert_type {
28         OK,
29         OKCANCEL,
30         YESNO,
31         YESNOCANCEL,
32         NOYES,
33     };
34
35     enum class alert_icon {
36         DEFAULT,
37         ERROR,
38         WARNING,
39         QUESTION,
40         ASTERISK,
41     };
42
43     enum class alert_response {
44         OK,
45         CANCEL,
46         YES,
47         NO,
48     };
49
50     template<class Self, class T>
51     class Convertible {
52     public:
53         T *handle() const
54         { return (T *) static_cast<const Self *>(this)->_handle; }
55
56         operator T *() const
57         { return handle(); }
58     };
59
60     class Base {
61     public:
62         void *_handle;
63
64         Base(void *h) : _handle(h)
65         { }
66
67         explicit operator bool() const
68         { return _handle != nullptr; }
69
70         explicit operator ui_typeinst *() const
71         { return (ui_typeinst *) _handle; }
72
73         explicit operator void *() const
74         { return _handle; }
75     };
76
77     static_assert(sizeof(Base) == sizeof(ui_widget *), "object slicing");
78
79     class Widget : public Base, public Convertible<Widget, ui_widget> {
80     public:
81         explicit Widget(ui_widget *h = nullptr) : Base((void *) h)
82         { }
83
84         alert_response alert(std::string text, std::string title = "NetRadiant",
85                              alert_type type = alert_type::OK, alert_icon icon = alert_icon::DEFAULT);
86
87         const char *file_dialog(bool open, const char *title, const char *path = nullptr,
88                                 const char *pattern = nullptr, bool want_load = false, bool want_import = false,
89                                 bool want_save = false);
90     };
91
92     static_assert(sizeof(Widget) == sizeof(Base), "object slicing");
93
94     extern Widget root;
95
96 #define WRAP(name, impl, methods) \
97     class name : public Widget, public Convertible<name, impl> { \
98         public: \
99             explicit name(impl *h) : Widget(reinterpret_cast<ui_widget *>(h)) {} \
100         methods \
101     }; \
102     static_assert(sizeof(name) == sizeof(Widget), "object slicing")
103
104     WRAP(Alignment, ui_alignment,
105          Alignment(float xalign, float yalign, float xscale, float yscale);
106     );
107
108     WRAP(Button, ui_button,
109          Button(const char *label);
110     );
111
112     WRAP(CheckButton, ui_checkbutton,
113          CheckButton(const char *label);
114     );
115
116     WRAP(Label, ui_label,
117          Label(const char *label);
118     );
119
120     WRAP(MenuItem, ui_menuitem,);
121
122     WRAP(ScrolledWindow, ui_scrolledwindow,
123          ScrolledWindow();
124     );
125
126     WRAP(SpinButton, ui_widget,);
127
128     WRAP(TreeModel, ui_treemodel,);
129
130     WRAP(TreeView, ui_treeview,
131          TreeView(TreeModel model);
132     );
133
134     WRAP(VBox, ui_vbox,
135          VBox(bool homogenous, int spacing);
136     );
137
138     WRAP(Window, ui_window,
139          Window() : Window(nullptr) {};
140
141          Window create_dialog_window(const char *title, void func(), void *data, int default_w = -1,
142                                      int default_h = -1);
143
144          Window create_modal_dialog_window(const char *title, ui_modal &dialog, int default_w = -1,
145                                            int default_h = -1);
146
147          Window create_floating_window(const char *title);
148
149          std::uint64_t on_key_press(bool (*f)(Widget widget, ui_evkey *event, void *extra),
150                                     void *extra = nullptr);
151     );
152
153 #undef WRAP
154
155 }
156
157 #endif