]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.cpp
Wrap GTK
[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     AccelGroup::AccelGroup() : AccelGroup(GTK_ACCEL_GROUP(gtk_accel_group_new()))
99     { }
100
101     Adjustment::Adjustment(double value,
102                            double lower, double upper,
103                            double step_increment, double page_increment,
104                            double page_size)
105             : Adjustment(
106             GTK_ADJUSTMENT(gtk_adjustment_new(value, lower, upper, step_increment, page_increment, page_size)))
107     { }
108
109     Alignment::Alignment(float xalign, float yalign, float xscale, float yscale)
110             : Alignment(GTK_ALIGNMENT(gtk_alignment_new(xalign, yalign, xscale, yscale)))
111     { }
112
113     Button::Button() : Button(GTK_BUTTON(gtk_button_new()))
114     { }
115
116     Button::Button(const char *label) : Button(GTK_BUTTON(gtk_button_new_with_label(label)))
117     { }
118
119     CellRendererText::CellRendererText() : CellRendererText(GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()))
120     { }
121
122     ComboBoxText::ComboBoxText() : ComboBoxText(GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new()))
123     { }
124
125     CheckButton::CheckButton(const char *label) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new_with_label(label)))
126     { }
127
128     Entry::Entry() : Entry(GTK_ENTRY(gtk_entry_new()))
129     { }
130
131     Entry::Entry(std::size_t max_length) : Entry()
132     {
133         gtk_entry_set_max_length(*this, static_cast<gint>(max_length));
134     }
135
136     Frame::Frame(const char *label) : Frame(GTK_FRAME(gtk_frame_new(label)))
137     { }
138
139     HBox::HBox(bool homogenous, int spacing) : HBox(GTK_HBOX(gtk_hbox_new(homogenous, spacing)))
140     { }
141
142     HScale::HScale(Adjustment adjustment) : HScale(GTK_HSCALE(gtk_hscale_new(adjustment)))
143     { }
144
145     HScale::HScale(double min, double max, double step) : HScale(GTK_HSCALE(gtk_hscale_new_with_range(min, max, step)))
146     { }
147
148     Image::Image() : Image(GTK_IMAGE(gtk_image_new()))
149     { }
150
151     Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label)))
152     { }
153
154     Menu::Menu() : Menu(GTK_MENU(gtk_menu_new()))
155     { }
156
157     MenuItem::MenuItem(const char *label, bool mnemonic) : MenuItem(
158             GTK_MENU_ITEM((mnemonic ? gtk_menu_item_new_with_mnemonic : gtk_menu_item_new_with_label)(label)))
159     { }
160
161     HPaned::HPaned() : HPaned(GTK_HPANED(gtk_hpaned_new()))
162     { }
163
164     VPaned::VPaned() : VPaned(GTK_VPANED(gtk_vpaned_new()))
165     { }
166
167     ScrolledWindow::ScrolledWindow() : ScrolledWindow(GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(nullptr, nullptr)))
168     { }
169
170     SpinButton::SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits) : SpinButton(
171             GTK_SPIN_BUTTON(gtk_spin_button_new(adjustment, climb_rate, digits)))
172     { }
173
174     Table::Table(std::size_t rows, std::size_t columns, bool homogenous) : Table(
175             GTK_TABLE(gtk_table_new(rows, columns, homogenous)))
176     { }
177
178     TextView::TextView() : TextView(GTK_TEXT_VIEW(gtk_text_view_new()))
179     { }
180
181     TreePath::TreePath() : TreePath(gtk_tree_path_new())
182     { }
183
184     TreePath::TreePath(const char *path) : TreePath(gtk_tree_path_new_from_string(path))
185     { }
186
187     TreeView::TreeView() : TreeView(GTK_TREE_VIEW(gtk_tree_view_new()))
188     { }
189
190     TreeView::TreeView(TreeModel model) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new_with_model(model)))
191     { }
192
193     TreeViewColumn::TreeViewColumn(const char *title, CellRenderer renderer,
194                                    std::initializer_list<TreeViewColumnAttribute> attributes)
195             : TreeViewColumn(gtk_tree_view_column_new_with_attributes(title, renderer, nullptr))
196     {
197         for (auto &it : attributes) {
198             gtk_tree_view_column_add_attribute(*this, renderer, it.attribute, it.column);
199         }
200     };
201
202     VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing)))
203     { }
204
205 }