]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.cpp
191990e05e1472efac561c09f8ad6e3ac7bc42f4
[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::create_dialog_window(const char *title, void func(), void *data, int default_w, int default_h)
60     {
61         return Window(::create_dialog_window(*this, title, func, data, default_w, default_h));
62     }
63
64     Window Window::create_modal_dialog_window(const char *title, ui_modal &dialog, int default_w, int default_h)
65     {
66         return Window(::create_modal_dialog_window(*this, title, dialog, default_w, default_h));
67     }
68
69     Window Window::create_floating_window(const char *title)
70     {
71         return Window(::create_floating_window(title, *this));
72     }
73
74     std::uint64_t Window::on_key_press(bool (*f)(Widget widget, ui_evkey *event, void *extra), void *extra)
75     {
76         using f_t = decltype(f);
77         struct user_data {
78             f_t f;
79             void *extra;
80         } *pass = new user_data{f, extra};
81         auto dtor = [](user_data *data, GClosure *) {
82             delete data;
83         };
84         auto func = [](ui_widget *widget, GdkEventKey *event, user_data *args) -> bool {
85             return args->f(Widget(widget), event, args->extra);
86         };
87         auto clos = g_cclosure_new(G_CALLBACK(+func), pass, reinterpret_cast<GClosureNotify>(+dtor));
88         return g_signal_connect_closure(G_OBJECT(*this), "key-press-event", clos, false);
89     }
90
91     AccelGroup::AccelGroup() : AccelGroup(GTK_ACCEL_GROUP(gtk_accel_group_new()))
92     { }
93
94     Adjustment::Adjustment(double value,
95                            double lower, double upper,
96                            double step_increment, double page_increment,
97                            double page_size)
98             : Adjustment(
99             GTK_ADJUSTMENT(gtk_adjustment_new(value, lower, upper, step_increment, page_increment, page_size)))
100     { }
101
102     Alignment::Alignment(float xalign, float yalign, float xscale, float yscale)
103             : Alignment(GTK_ALIGNMENT(gtk_alignment_new(xalign, yalign, xscale, yscale)))
104     { }
105
106     Button::Button() : Button(GTK_BUTTON(gtk_button_new()))
107     { }
108
109     Button::Button(const char *label) : Button(GTK_BUTTON(gtk_button_new_with_label(label)))
110     { }
111
112     CellRendererText::CellRendererText() : CellRendererText(GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()))
113     { }
114
115     ComboBox ComboBoxText()
116     { return ComboBox(GTK_COMBO_BOX(gtk_combo_box_new_text())); }
117
118     CheckButton::CheckButton(const char *label) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new_with_label(label)))
119     { }
120
121     Entry::Entry() : Entry(GTK_ENTRY(gtk_entry_new()))
122     { }
123
124     Entry::Entry(std::size_t max_length) : Entry(GTK_ENTRY(gtk_entry_new_with_max_length(max_length)))
125     { }
126
127     Frame::Frame(const char *label) : Frame(GTK_FRAME(gtk_frame_new(label)))
128     { }
129
130     HBox::HBox(bool homogenous, int spacing) : HBox(GTK_HBOX(gtk_hbox_new(homogenous, spacing)))
131     { }
132
133     Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label)))
134     { }
135
136     Menu::Menu() : Menu(GTK_MENU(gtk_menu_new()))
137     { }
138
139     MenuItem::MenuItem(const char *label, bool mnemonic) : MenuItem(
140             GTK_MENU_ITEM((mnemonic ? gtk_menu_item_new_with_mnemonic : gtk_menu_item_new_with_label)(label)))
141     { }
142
143     HPaned::HPaned() : HPaned(GTK_HPANED(gtk_hpaned_new()))
144     { }
145
146     VPaned::VPaned() : VPaned(GTK_VPANED(gtk_vpaned_new()))
147     { }
148
149     ScrolledWindow::ScrolledWindow() : ScrolledWindow(GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(nullptr, nullptr)))
150     { }
151
152     SpinButton::SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits) : SpinButton(
153             GTK_SPIN_BUTTON(gtk_spin_button_new(adjustment, climb_rate, digits)))
154     { }
155
156     Table::Table(std::size_t rows, std::size_t columns, bool homogenous) : Table(
157             GTK_TABLE(gtk_table_new(rows, columns, homogenous)))
158     { }
159
160     TreePath::TreePath() : TreePath(gtk_tree_path_new())
161     { }
162
163     TreePath::TreePath(const char *path) : TreePath(gtk_tree_path_new_from_string(path))
164     { }
165
166     TreeView::TreeView(TreeModel model) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new_with_model(model)))
167     { }
168
169     TreeViewColumn::TreeViewColumn(const char *title, CellRenderer renderer,
170                                    std::initializer_list<TreeViewColumnAttribute> attributes)
171             : TreeViewColumn(gtk_tree_view_column_new_with_attributes(title, renderer, nullptr))
172     {
173         for (auto &it : attributes) {
174             gtk_tree_view_column_add_attribute(*this, renderer, it.attribute, it.column);
175         }
176     };
177
178     VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing)))
179     { }
180
181 }