]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/uilib/uilib.cpp
Wrap GtkTreeView
[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         auto pass = std::make_tuple(f, extra);
77         auto func = [](ui_widget *widget, GdkEventKey *event, void *pass_) -> bool {
78             using pass_t = decltype(pass);
79             auto &args = *(pass_t *) pass_;
80             auto func = std::get<0>(args);
81             auto pass = std::get<1>(args);
82             return func(Widget(widget), event, pass);
83         };
84         return g_signal_connect(G_OBJECT(*this), "key-press-event", (GCallback) +func, &pass);
85     }
86
87     Alignment::Alignment(float xalign, float yalign, float xscale, float yscale)
88             : Alignment(GTK_ALIGNMENT(gtk_alignment_new(xalign, yalign, xscale, yscale)))
89     { }
90
91     Button::Button(const char *label) : Button(GTK_BUTTON(gtk_button_new_with_label(label)))
92     { }
93
94     Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label)))
95     { }
96
97     TreeView::TreeView(TreeModel model) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new_with_model(model)))
98     { }
99
100 }