]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.cpp
Propagate ui::Window
[xonotic/netradiant.git] / libs / uilib / uilib.cpp
1 #include "uilib.h"
2
3 #include <tuple>
4
5 #include <gtk/gtk.h>
6
7 #include "gtkutil/dialog.h"
8 #include "gtkutil/filechooser.h"
9 #include "gtkutil/messagebox.h"
10 #include "gtkutil/window.h"
11
12 namespace ui {
13
14     void init(int argc, char *argv[])
15     {
16         gtk_disable_setlocale();
17         gtk_init(&argc, &argv);
18     }
19
20     void main()
21     {
22         gtk_main();
23     }
24
25     Widget root;
26
27     alert_response Widget::alert(std::string text, std::string title, alert_type type, alert_icon icon)
28     {
29         auto ret = gtk_MessageBox(*this, text.c_str(),
30                                   title.c_str(),
31                                   type == alert_type::OK ? eMB_OK :
32                                   type == alert_type::OKCANCEL ? eMB_OKCANCEL :
33                                   type == alert_type::YESNO ? eMB_YESNO :
34                                   type == alert_type::YESNOCANCEL ? eMB_YESNOCANCEL :
35                                   type == alert_type::NOYES ? eMB_NOYES :
36                                   eMB_OK,
37                                   icon == alert_icon::Default ? eMB_ICONDEFAULT :
38                                   icon == alert_icon::Error ? eMB_ICONERROR :
39                                   icon == alert_icon::Warning ? eMB_ICONWARNING :
40                                   icon == alert_icon::Question ? eMB_ICONQUESTION :
41                                   icon == alert_icon::Asterisk ? eMB_ICONASTERISK :
42                                   eMB_ICONDEFAULT
43         );
44         return
45                 ret == eIDOK ? alert_response::OK :
46                 ret == eIDCANCEL ? alert_response::CANCEL :
47                 ret == eIDYES ? alert_response::YES :
48                 ret == eIDNO ? alert_response::NO :
49                 alert_response::OK;
50     }
51
52     const char *Widget::file_dialog(bool open, const char *title, const char *path,
53                                     const char *pattern, bool want_load, bool want_import,
54                                     bool want_save)
55     {
56         return ::file_dialog(*this, open, title, path, pattern, want_load, want_import, want_save);
57     }
58
59     Window::Window(window_type type)
60             : Window(GTK_WINDOW(gtk_window_new(
61             type == window_type::TOP ? GTK_WINDOW_TOPLEVEL :
62             type == window_type::POPUP ? GTK_WINDOW_POPUP :
63             GTK_WINDOW_TOPLEVEL)))
64     {};
65
66     Window Window::create_dialog_window(const char *title, void func(), void *data, int default_w, int default_h)
67     {
68         return Window(::create_dialog_window(*this, title, func, data, default_w, default_h));
69     }
70
71     Window Window::create_modal_dialog_window(const char *title, ui_modal &dialog, int default_w, int default_h)
72     {
73         return Window(::create_modal_dialog_window(*this, title, dialog, default_w, default_h));
74     }
75
76     Window Window::create_floating_window(const char *title)
77     {
78         return Window(::create_floating_window(title, *this));
79     }
80
81     std::uint64_t Window::on_key_press(bool (*f)(Widget widget, ui_evkey *event, void *extra), void *extra)
82     {
83         using f_t = decltype(f);
84         struct user_data {
85             f_t f;
86             void *extra;
87         } *pass = new user_data{f, extra};
88         auto dtor = [](user_data *data, GClosure *) {
89             delete data;
90         };
91         auto func = [](ui_widget *widget, GdkEventKey *event, user_data *args) -> bool {
92             return args->f(Widget(widget), event, args->extra);
93         };
94         auto clos = g_cclosure_new(G_CALLBACK(+func), pass, reinterpret_cast<GClosureNotify>(+dtor));
95         return g_signal_connect_closure(G_OBJECT(*this), "key-press-event", clos, false);
96     }
97
98     void Window::add_accel_group(AccelGroup group)
99     {
100         gtk_window_add_accel_group(*this, group);
101     }
102
103     AccelGroup::AccelGroup() : AccelGroup(GTK_ACCEL_GROUP(gtk_accel_group_new()))
104     {}
105
106     Adjustment::Adjustment(double value,
107                            double lower, double upper,
108                            double step_increment, double page_increment,
109                            double page_size)
110             : Adjustment(
111             GTK_ADJUSTMENT(gtk_adjustment_new(value, lower, upper, step_increment, page_increment, page_size)))
112     {}
113
114     Alignment::Alignment(float xalign, float yalign, float xscale, float yscale)
115             : Alignment(GTK_ALIGNMENT(gtk_alignment_new(xalign, yalign, xscale, yscale)))
116     {}
117
118     Button::Button() : Button(GTK_BUTTON(gtk_button_new()))
119     {}
120
121     Button::Button(const char *label) : Button(GTK_BUTTON(gtk_button_new_with_label(label)))
122     {}
123
124     CellRendererText::CellRendererText() : CellRendererText(GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()))
125     {}
126
127     ComboBoxText::ComboBoxText() : ComboBoxText(GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new()))
128     {}
129
130     CheckButton::CheckButton(const char *label) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new_with_label(label)))
131     {}
132
133     Entry::Entry() : Entry(GTK_ENTRY(gtk_entry_new()))
134     {}
135
136     Entry::Entry(std::size_t max_length) : Entry()
137     {
138         gtk_entry_set_max_length(*this, static_cast<gint>(max_length));
139     }
140
141     Frame::Frame(const char *label) : Frame(GTK_FRAME(gtk_frame_new(label)))
142     {}
143
144     HBox::HBox(bool homogenous, int spacing) : HBox(GTK_HBOX(gtk_hbox_new(homogenous, spacing)))
145     {}
146
147     HScale::HScale(Adjustment adjustment) : HScale(GTK_HSCALE(gtk_hscale_new(adjustment)))
148     {}
149
150     HScale::HScale(double min, double max, double step) : HScale(GTK_HSCALE(gtk_hscale_new_with_range(min, max, step)))
151     {}
152
153     Image::Image() : Image(GTK_IMAGE(gtk_image_new()))
154     {}
155
156     Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label)))
157     {}
158
159     Menu::Menu() : Menu(GTK_MENU(gtk_menu_new()))
160     {}
161
162     MenuItem::MenuItem(const char *label, bool mnemonic) : MenuItem(
163             GTK_MENU_ITEM((mnemonic ? gtk_menu_item_new_with_mnemonic : gtk_menu_item_new_with_label)(label)))
164     {}
165
166     HPaned::HPaned() : HPaned(GTK_HPANED(gtk_hpaned_new()))
167     {}
168
169     VPaned::VPaned() : VPaned(GTK_VPANED(gtk_vpaned_new()))
170     {}
171
172     ScrolledWindow::ScrolledWindow() : ScrolledWindow(GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(nullptr, nullptr)))
173     {}
174
175     SpinButton::SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits) : SpinButton(
176             GTK_SPIN_BUTTON(gtk_spin_button_new(adjustment, climb_rate, digits)))
177     {}
178
179     Table::Table(std::size_t rows, std::size_t columns, bool homogenous) : Table(
180             GTK_TABLE(gtk_table_new(rows, columns, homogenous)))
181     {}
182
183     TextView::TextView() : TextView(GTK_TEXT_VIEW(gtk_text_view_new()))
184     {}
185
186     TreePath::TreePath() : TreePath(gtk_tree_path_new())
187     {}
188
189     TreePath::TreePath(const char *path) : TreePath(gtk_tree_path_new_from_string(path))
190     {}
191
192     TreeView::TreeView() : TreeView(GTK_TREE_VIEW(gtk_tree_view_new()))
193     {}
194
195     TreeView::TreeView(TreeModel model) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new_with_model(model)))
196     {}
197
198     TreeViewColumn::TreeViewColumn(const char *title, CellRenderer renderer,
199                                    std::initializer_list<TreeViewColumnAttribute> attributes)
200             : TreeViewColumn(gtk_tree_view_column_new_with_attributes(title, renderer, nullptr))
201     {
202         for (auto &it : attributes) {
203             gtk_tree_view_column_add_attribute(*this, renderer, it.attribute, it.column);
204         }
205     };
206
207     VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing)))
208     {}
209
210 }