X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fnetradiant.git;a=blobdiff_plain;f=libs%2Fuilib%2Fuilib.cpp;h=7aaccfa2ef04aa1f24d07500655caceb54897ca5;hp=1e37a1e05287b73f53b3d366d4370777b5299340;hb=265c42d;hpb=57f4ad12206f19d639260384220a5507e7b20086 diff --git a/libs/uilib/uilib.cpp b/libs/uilib/uilib.cpp index 1e37a1e0..7aaccfa2 100644 --- a/libs/uilib/uilib.cpp +++ b/libs/uilib/uilib.cpp @@ -11,10 +11,17 @@ namespace ui { - void init(int argc, char *argv[]) + bool init(int *argc, char **argv[], char const *parameter_string, char const **error) { gtk_disable_setlocale(); - gtk_init(&argc, &argv); + static GOptionEntry entries[] = {{NULL}}; + char const *translation_domain = NULL; + GError *gerror = NULL; + bool ret = gtk_init_with_args(argc, argv, parameter_string, entries, translation_domain, &gerror) != 0; + if (!ret) { + *error = gerror->message; + } + return ret; } void main() @@ -22,7 +29,14 @@ namespace ui { gtk_main(); } - Widget root{nullptr}; + void process() + { + while (gtk_events_pending()) { + gtk_main_iteration(); + } + } + + Widget root; #define IMPL(T, F) template<> _IMPL(T, F) #define _IMPL(T, F) struct verify { using self = T; static self test(self it) { return self(F(it)); } } @@ -32,7 +46,7 @@ namespace ui { template _IMPL(T,); -#define this verify::test(*static_cast(this)) +#define this (verify::test(*static_cast(this))) IMPL(Editable, GTK_EDITABLE); @@ -78,15 +92,43 @@ namespace ui { return ::file_dialog(this, open, title, path, pattern, want_load, want_import, want_save); } + bool IWidget::visible() + { + return gtk_widget_get_visible(this) != 0; + } + void IWidget::show() { gtk_widget_show(this); } - IMPL(Window, GTK_WINDOW); + Dimensions IWidget::dimensions() + { + GtkAllocation allocation; + gtk_widget_get_allocation(this, &allocation); + return Dimensions{allocation.width, allocation.height}; + } - Window::Window() : Window(nullptr) - {} + void IWidget::dimensions(int width, int height) + { + gtk_widget_set_size_request(this, width, height); + } + + IMPL(Container, GTK_CONTAINER); + + void IContainer::add(Widget widget) + { + gtk_container_add(this, widget); + } + + void IContainer::remove(Widget widget) + { + gtk_container_remove(this, widget); + } + + IMPL(Bin, GTK_BIN); + + IMPL(Window, GTK_WINDOW); Window::Window(window_type type) : Window(GTK_WINDOW(gtk_window_new( type == window_type::TOP ? GTK_WINDOW_TOPLEVEL : @@ -160,6 +202,9 @@ namespace ui { IMPL(CheckButton, GTK_CHECK_BUTTON); + CheckButton::CheckButton() : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new())) + {} + CheckButton::CheckButton(const char *label) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new_with_label(label))) {} @@ -187,6 +232,11 @@ namespace ui { ScrolledWindow::ScrolledWindow() : ScrolledWindow(GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(nullptr, nullptr))) {} + void IScrolledWindow::overflow(Policy x, Policy y) + { + gtk_scrolled_window_set_policy(this, static_cast(x), static_cast(y)); + } + IMPL(VBox, GTK_VBOX); VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing))) @@ -224,6 +274,12 @@ namespace ui { TextView::TextView() : TextView(GTK_TEXT_VIEW(gtk_text_view_new())) {} + void ITextView::text(char const *str) + { + GtkTextBuffer *buffer = gtk_text_view_get_buffer(this); + gtk_text_buffer_set_text(buffer, str, -1); + } + TreeView::TreeView() : TreeView(GTK_TREE_VIEW(gtk_tree_view_new())) {} @@ -235,6 +291,11 @@ namespace ui { Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label))) {} + void ILabel::text(char const *str) + { + gtk_label_set_text(this, str); + } + IMPL(Image, GTK_IMAGE); Image::Image() : Image(GTK_IMAGE(gtk_image_new())) @@ -250,6 +311,16 @@ namespace ui { gtk_entry_set_max_length(this, static_cast(max_length)); } + char const *IEntry::text() + { + return gtk_entry_get_text(this); + } + + void IEntry::text(char const *str) + { + return gtk_entry_set_text(this, str); + } + IMPL(SpinButton, GTK_SPIN_BUTTON); SpinButton::SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits) : SpinButton( @@ -288,7 +359,7 @@ namespace ui { for (auto &it : attributes) { gtk_tree_view_column_add_attribute(this, renderer, it.attribute, it.column); } - }; + } IMPL(AccelGroup, GTK_ACCEL_GROUP); @@ -310,4 +381,16 @@ namespace ui { TreePath::TreePath(const char *path) : TreePath(gtk_tree_path_new_from_string(path)) {} + // Custom + + guint IGLArea::on_render(GCallback pFunction, void *data) + { +#if GTK_TARGET == 3 + return this.connect("render", pFunction, data); +#endif +#if GTK_TARGET == 2 + return this.connect("expose_event", pFunction, data); +#endif + } + }