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