| 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 | bool init(int *argc, char **argv[], char const *parameter_string, char const **error) |
| 15 | { |
| 16 | gtk_disable_setlocale(); |
| 17 | static GOptionEntry entries[] = {{}}; |
| 18 | char const *translation_domain = NULL; |
| 19 | GError *gerror = NULL; |
| 20 | bool ret = gtk_init_with_args(argc, argv, parameter_string, entries, translation_domain, &gerror) != 0; |
| 21 | if (!ret) { |
| 22 | *error = gerror->message; |
| 23 | } |
| 24 | return ret; |
| 25 | } |
| 26 | |
| 27 | void main() |
| 28 | { |
| 29 | gtk_main(); |
| 30 | } |
| 31 | |
| 32 | void process() |
| 33 | { |
| 34 | while (gtk_events_pending()) { |
| 35 | gtk_main_iteration(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #define IMPL(T, F) template<> _IMPL(T, F) |
| 40 | #define _IMPL(T, F) struct verify<T *> { using self = T; static self test(self it) { return self::from(F(it)); } } |
| 41 | |
| 42 | template<class T> |
| 43 | struct verify; |
| 44 | |
| 45 | template<class T> _IMPL(T,); |
| 46 | |
| 47 | template<class T> |
| 48 | using pointer_remove_const = std::add_pointer< |
| 49 | typename std::remove_const< |
| 50 | typename std::remove_pointer<T>::type |
| 51 | >::type |
| 52 | >; |
| 53 | |
| 54 | #define this (verify<self>::test(*static_cast<self>(const_cast<pointer_remove_const<decltype(this)>::type>(this)))) |
| 55 | |
| 56 | IMPL(Editable, GTK_EDITABLE); |
| 57 | |
| 58 | void IEditable::editable(bool value) |
| 59 | { |
| 60 | gtk_editable_set_editable(this, value); |
| 61 | } |
| 62 | |
| 63 | IMPL(TreeModel, GTK_TREE_MODEL); |
| 64 | |
| 65 | IMPL(Widget, GTK_WIDGET); |
| 66 | |
| 67 | Widget::Widget(ui::New_t) : Widget(nullptr) |
| 68 | {} |
| 69 | |
| 70 | Window IWidget::window() |
| 71 | { |
| 72 | return Window::from(gtk_widget_get_toplevel(this)); |
| 73 | } |
| 74 | |
| 75 | const char * |
| 76 | IWidget::file_dialog(bool open, const char *title, const char *path, const char *pattern, bool want_load, |
| 77 | bool want_import, bool want_save) |
| 78 | { |
| 79 | return ::file_dialog(this.window(), open, title, path, pattern, want_load, want_import, want_save); |
| 80 | } |
| 81 | |
| 82 | bool IWidget::visible() |
| 83 | { |
| 84 | return gtk_widget_get_visible(this) != 0; |
| 85 | } |
| 86 | |
| 87 | void IWidget::visible(bool shown) |
| 88 | { |
| 89 | if (shown) { |
| 90 | |
| 91 | #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET |
| 92 | /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */ |
| 93 | GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( this ), "glwidget" ) ); |
| 94 | if ( glwidget ){ |
| 95 | gtk_widget_hide( glwidget ); |
| 96 | gtk_widget_show( glwidget ); |
| 97 | } |
| 98 | #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET |
| 99 | |
| 100 | this.show(); |
| 101 | } else { |
| 102 | this.hide(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void IWidget::show() |
| 107 | { |
| 108 | gtk_widget_show(this); |
| 109 | } |
| 110 | |
| 111 | void IWidget::hide() |
| 112 | { |
| 113 | gtk_widget_hide(this); |
| 114 | } |
| 115 | |
| 116 | Dimensions IWidget::dimensions() |
| 117 | { |
| 118 | GtkAllocation allocation; |
| 119 | gtk_widget_get_allocation(this, &allocation); |
| 120 | return Dimensions{allocation.width, allocation.height}; |
| 121 | } |
| 122 | |
| 123 | void IWidget::dimensions(int width, int height) |
| 124 | { |
| 125 | gtk_widget_set_size_request(this, width, height); |
| 126 | } |
| 127 | |
| 128 | void IWidget::destroy() |
| 129 | { |
| 130 | gtk_widget_destroy(this); |
| 131 | } |
| 132 | |
| 133 | IMPL(Container, GTK_CONTAINER); |
| 134 | |
| 135 | void IContainer::add(Widget widget) |
| 136 | { |
| 137 | gtk_container_add(this, widget); |
| 138 | } |
| 139 | |
| 140 | void IContainer::remove(Widget widget) |
| 141 | { |
| 142 | gtk_container_remove(this, widget); |
| 143 | } |
| 144 | |
| 145 | IMPL(Bin, GTK_BIN); |
| 146 | |
| 147 | IMPL(Window, GTK_WINDOW); |
| 148 | |
| 149 | Window::Window(window_type type) : Window(GTK_WINDOW(gtk_window_new( |
| 150 | type == window_type::TOP ? GTK_WINDOW_TOPLEVEL : |
| 151 | type == window_type::POPUP ? GTK_WINDOW_POPUP : |
| 152 | GTK_WINDOW_TOPLEVEL |
| 153 | ))) |
| 154 | {} |
| 155 | |
| 156 | Window IWindow::create_dialog_window(const char *title, void func(), void *data, int default_w, int default_h) |
| 157 | { |
| 158 | return Window(::create_dialog_window(this, title, func, data, default_w, default_h)); |
| 159 | } |
| 160 | |
| 161 | Window IWindow::create_modal_dialog_window(const char *title, ModalDialog &dialog, int default_w, int default_h) |
| 162 | { |
| 163 | return Window(::create_modal_dialog_window(this, title, dialog, default_w, default_h)); |
| 164 | } |
| 165 | |
| 166 | Window IWindow::create_floating_window(const char *title) |
| 167 | { |
| 168 | return Window(::create_floating_window(title, this)); |
| 169 | } |
| 170 | |
| 171 | std::uint64_t IWindow::on_key_press(bool (*f)(Widget widget, _GdkEventKey *event, void *extra), void *extra) |
| 172 | { |
| 173 | using f_t = decltype(f); |
| 174 | struct user_data { |
| 175 | f_t f; |
| 176 | void *extra; |
| 177 | } *pass = new user_data{f, extra}; |
| 178 | auto dtor = [](user_data *data, GClosure *) { |
| 179 | delete data; |
| 180 | }; |
| 181 | auto func = [](_GtkWidget *widget, GdkEventKey *event, user_data *args) -> bool { |
| 182 | return args->f(Widget::from(widget), event, args->extra); |
| 183 | }; |
| 184 | auto clos = g_cclosure_new(G_CALLBACK(+func), pass, reinterpret_cast<GClosureNotify>(+dtor)); |
| 185 | return g_signal_connect_closure(G_OBJECT(this), "key-press-event", clos, false); |
| 186 | } |
| 187 | |
| 188 | void IWindow::add_accel_group(AccelGroup group) |
| 189 | { |
| 190 | gtk_window_add_accel_group(this, group); |
| 191 | } |
| 192 | |
| 193 | IMPL(Alignment, GTK_ALIGNMENT); |
| 194 | |
| 195 | Alignment::Alignment(float xalign, float yalign, float xscale, float yscale) |
| 196 | : Alignment(GTK_ALIGNMENT(gtk_alignment_new(xalign, yalign, xscale, yscale))) |
| 197 | {} |
| 198 | |
| 199 | IMPL(Frame, GTK_FRAME); |
| 200 | |
| 201 | Frame::Frame(const char *label) : Frame(GTK_FRAME(gtk_frame_new(label))) |
| 202 | {} |
| 203 | |
| 204 | IMPL(Button, GTK_BUTTON); |
| 205 | |
| 206 | Button::Button(ui::New_t) : Button(GTK_BUTTON(gtk_button_new())) |
| 207 | {} |
| 208 | |
| 209 | Button::Button(const char *label) : Button(GTK_BUTTON(gtk_button_new_with_label(label))) |
| 210 | {} |
| 211 | |
| 212 | IMPL(ToggleButton, GTK_TOGGLE_BUTTON); |
| 213 | |
| 214 | bool IToggleButton::active() const |
| 215 | { |
| 216 | return gtk_toggle_button_get_active(this) != 0; |
| 217 | } |
| 218 | |
| 219 | void IToggleButton::active(bool value) |
| 220 | { |
| 221 | gtk_toggle_button_set_active(this, value); |
| 222 | } |
| 223 | |
| 224 | IMPL(CheckButton, GTK_CHECK_BUTTON); |
| 225 | |
| 226 | CheckButton::CheckButton(ui::New_t) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new())) |
| 227 | {} |
| 228 | |
| 229 | CheckButton::CheckButton(const char *label) : CheckButton(GTK_CHECK_BUTTON(gtk_check_button_new_with_label(label))) |
| 230 | {} |
| 231 | |
| 232 | IMPL(MenuItem, GTK_MENU_ITEM); |
| 233 | |
| 234 | MenuItem::MenuItem(ui::New_t) : MenuItem(GTK_MENU_ITEM(gtk_menu_item_new())) |
| 235 | {} |
| 236 | |
| 237 | MenuItem::MenuItem(const char *label, bool mnemonic) : MenuItem( |
| 238 | GTK_MENU_ITEM((mnemonic ? gtk_menu_item_new_with_mnemonic : gtk_menu_item_new_with_label)(label))) |
| 239 | {} |
| 240 | |
| 241 | IMPL(TearoffMenuItem, GTK_TEAROFF_MENU_ITEM); |
| 242 | |
| 243 | TearoffMenuItem::TearoffMenuItem(ui::New_t) : TearoffMenuItem(GTK_TEAROFF_MENU_ITEM(gtk_tearoff_menu_item_new())) |
| 244 | {} |
| 245 | |
| 246 | IMPL(ComboBoxText, GTK_COMBO_BOX_TEXT); |
| 247 | |
| 248 | ComboBoxText::ComboBoxText(ui::New_t) : ComboBoxText(GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new())) |
| 249 | {} |
| 250 | |
| 251 | IMPL(ScrolledWindow, GTK_SCROLLED_WINDOW); |
| 252 | |
| 253 | ScrolledWindow::ScrolledWindow(ui::New_t) : ScrolledWindow(GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(nullptr, nullptr))) |
| 254 | {} |
| 255 | |
| 256 | void IScrolledWindow::overflow(Policy x, Policy y) |
| 257 | { |
| 258 | gtk_scrolled_window_set_policy(this, static_cast<GtkPolicyType>(x), static_cast<GtkPolicyType>(y)); |
| 259 | } |
| 260 | |
| 261 | IMPL(Box, GTK_BOX); |
| 262 | |
| 263 | void IBox::pack_start(ui::Widget child, bool expand, bool fill, unsigned int padding) |
| 264 | { |
| 265 | gtk_box_pack_start(this, child, expand, fill, padding); |
| 266 | } |
| 267 | |
| 268 | void IBox::pack_end(ui::Widget child, bool expand, bool fill, unsigned int padding) |
| 269 | { |
| 270 | gtk_box_pack_end(this, child, expand, fill, padding); |
| 271 | } |
| 272 | |
| 273 | void IBox::set_child_packing(ui::Widget child, bool expand, bool fill, unsigned int padding, ui::Packing packing) |
| 274 | { |
| 275 | gtk_box_set_child_packing(this, child, expand, fill, padding, (GtkPackType) packing); |
| 276 | } |
| 277 | |
| 278 | IMPL(VBox, GTK_VBOX); |
| 279 | |
| 280 | VBox::VBox(bool homogenous, int spacing) : VBox(GTK_VBOX(gtk_vbox_new(homogenous, spacing))) |
| 281 | {} |
| 282 | |
| 283 | IMPL(HBox, GTK_HBOX); |
| 284 | |
| 285 | HBox::HBox(bool homogenous, int spacing) : HBox(GTK_HBOX(gtk_hbox_new(homogenous, spacing))) |
| 286 | {} |
| 287 | |
| 288 | IMPL(HPaned, GTK_HPANED); |
| 289 | |
| 290 | HPaned::HPaned(ui::New_t) : HPaned(GTK_HPANED(gtk_hpaned_new())) |
| 291 | {} |
| 292 | |
| 293 | IMPL(VPaned, GTK_VPANED); |
| 294 | |
| 295 | VPaned::VPaned(ui::New_t) : VPaned(GTK_VPANED(gtk_vpaned_new())) |
| 296 | {} |
| 297 | |
| 298 | IMPL(Menu, GTK_MENU); |
| 299 | |
| 300 | Menu::Menu(ui::New_t) : Menu(GTK_MENU(gtk_menu_new())) |
| 301 | {} |
| 302 | |
| 303 | IMPL(Table, GTK_TABLE); |
| 304 | |
| 305 | Table::Table(std::size_t rows, std::size_t columns, bool homogenous) : Table( |
| 306 | GTK_TABLE(gtk_table_new(rows, columns, homogenous)) |
| 307 | ) |
| 308 | {} |
| 309 | |
| 310 | void ITable::attach(Widget child, TableAttach attach, TableAttachOptions options, TablePadding padding) { |
| 311 | gtk_table_attach(this, child, |
| 312 | attach.left, attach.right, attach.top, attach.bottom, |
| 313 | static_cast<GtkAttachOptions>(options.x), static_cast<GtkAttachOptions>(options.y), |
| 314 | padding.x, padding.y |
| 315 | ); |
| 316 | } |
| 317 | |
| 318 | IMPL(TextView, GTK_TEXT_VIEW); |
| 319 | |
| 320 | TextView::TextView(ui::New_t) : TextView(GTK_TEXT_VIEW(gtk_text_view_new())) |
| 321 | {} |
| 322 | |
| 323 | void ITextView::text(char const *str) |
| 324 | { |
| 325 | GtkTextBuffer *buffer = gtk_text_view_get_buffer(this); |
| 326 | gtk_text_buffer_set_text(buffer, str, -1); |
| 327 | } |
| 328 | |
| 329 | IMPL(TreeView, GTK_TREE_VIEW); |
| 330 | |
| 331 | TreeView::TreeView(ui::New_t) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new())) |
| 332 | {} |
| 333 | |
| 334 | TreeView::TreeView(TreeModel model) : TreeView(GTK_TREE_VIEW(gtk_tree_view_new_with_model(model))) |
| 335 | {} |
| 336 | |
| 337 | IMPL(Label, GTK_LABEL); |
| 338 | |
| 339 | Label::Label(const char *label) : Label(GTK_LABEL(gtk_label_new(label))) |
| 340 | {} |
| 341 | |
| 342 | void ILabel::text(char const *str) |
| 343 | { |
| 344 | gtk_label_set_text(this, str); |
| 345 | } |
| 346 | |
| 347 | IMPL(Image, GTK_IMAGE); |
| 348 | |
| 349 | Image::Image(ui::New_t) : Image(GTK_IMAGE(gtk_image_new())) |
| 350 | {} |
| 351 | |
| 352 | IMPL(Entry, GTK_ENTRY); |
| 353 | |
| 354 | Entry::Entry(ui::New_t) : Entry(GTK_ENTRY(gtk_entry_new())) |
| 355 | {} |
| 356 | |
| 357 | Entry::Entry(std::size_t max_length) : Entry(ui::New) |
| 358 | { |
| 359 | gtk_entry_set_max_length(this, static_cast<gint>(max_length)); |
| 360 | } |
| 361 | |
| 362 | char const *IEntry::text() |
| 363 | { |
| 364 | return gtk_entry_get_text(this); |
| 365 | } |
| 366 | |
| 367 | void IEntry::text(char const *str) |
| 368 | { |
| 369 | return gtk_entry_set_text(this, str); |
| 370 | } |
| 371 | |
| 372 | IMPL(SpinButton, GTK_SPIN_BUTTON); |
| 373 | |
| 374 | SpinButton::SpinButton(Adjustment adjustment, double climb_rate, std::size_t digits) : SpinButton( |
| 375 | GTK_SPIN_BUTTON(gtk_spin_button_new(adjustment, climb_rate, digits))) |
| 376 | {} |
| 377 | |
| 378 | IMPL(HScale, GTK_HSCALE); |
| 379 | |
| 380 | HScale::HScale(Adjustment adjustment) : HScale(GTK_HSCALE(gtk_hscale_new(adjustment))) |
| 381 | {} |
| 382 | |
| 383 | HScale::HScale(double min, double max, double step) : HScale(GTK_HSCALE(gtk_hscale_new_with_range(min, max, step))) |
| 384 | {} |
| 385 | |
| 386 | IMPL(Adjustment, GTK_ADJUSTMENT); |
| 387 | |
| 388 | Adjustment::Adjustment(double value, |
| 389 | double lower, double upper, |
| 390 | double step_increment, double page_increment, |
| 391 | double page_size) |
| 392 | : Adjustment( |
| 393 | GTK_ADJUSTMENT(gtk_adjustment_new(value, lower, upper, step_increment, page_increment, page_size))) |
| 394 | {} |
| 395 | |
| 396 | IMPL(CellRendererText, GTK_CELL_RENDERER_TEXT); |
| 397 | |
| 398 | CellRendererText::CellRendererText(ui::New_t) : CellRendererText(GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new())) |
| 399 | {} |
| 400 | |
| 401 | IMPL(TreeViewColumn, GTK_TREE_VIEW_COLUMN); |
| 402 | |
| 403 | TreeViewColumn::TreeViewColumn(const char *title, CellRenderer renderer, |
| 404 | std::initializer_list<TreeViewColumnAttribute> attributes) |
| 405 | : TreeViewColumn(gtk_tree_view_column_new_with_attributes(title, renderer, nullptr)) |
| 406 | { |
| 407 | for (auto &it : attributes) { |
| 408 | gtk_tree_view_column_add_attribute(this, renderer, it.attribute, it.column); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | IMPL(AccelGroup, GTK_ACCEL_GROUP); |
| 413 | |
| 414 | AccelGroup::AccelGroup(ui::New_t) : AccelGroup(GTK_ACCEL_GROUP(gtk_accel_group_new())) |
| 415 | {} |
| 416 | |
| 417 | IMPL(ListStore, GTK_LIST_STORE); |
| 418 | |
| 419 | void IListStore::clear() |
| 420 | { |
| 421 | gtk_list_store_clear(this); |
| 422 | } |
| 423 | |
| 424 | void IListStore::append() |
| 425 | { |
| 426 | gtk_list_store_append(this, nullptr); |
| 427 | } |
| 428 | |
| 429 | IMPL(TreeStore, GTK_TREE_STORE); |
| 430 | |
| 431 | // IMPL(TreePath, GTK_TREE_PATH); |
| 432 | |
| 433 | TreePath::TreePath(ui::New_t) : TreePath(gtk_tree_path_new()) |
| 434 | {} |
| 435 | |
| 436 | TreePath::TreePath(const char *path) : TreePath(gtk_tree_path_new_from_string(path)) |
| 437 | {} |
| 438 | |
| 439 | // Custom |
| 440 | |
| 441 | #if GTK_TARGET == 3 |
| 442 | |
| 443 | IMPL(GLArea, GTK_GL_AREA); |
| 444 | |
| 445 | #elif GTK_TARGET == 2 |
| 446 | |
| 447 | IMPL(GLArea, GTK_DRAWING_AREA); |
| 448 | |
| 449 | #endif |
| 450 | |
| 451 | guint IGLArea::on_render(GCallback pFunction, void *data) |
| 452 | { |
| 453 | #if GTK_TARGET == 3 |
| 454 | return this.connect("render", pFunction, data); |
| 455 | #endif |
| 456 | #if GTK_TARGET == 2 |
| 457 | return this.connect("expose_event", pFunction, data); |
| 458 | #endif |
| 459 | } |
| 460 | |
| 461 | // global |
| 462 | |
| 463 | Window root{ui::null}; |
| 464 | |
| 465 | alert_response alert(Window parent, std::string text, std::string title, alert_type type, alert_icon icon) |
| 466 | { |
| 467 | auto ret = gtk_MessageBox(parent, text.c_str(), |
| 468 | title.c_str(), |
| 469 | type == alert_type::OK ? eMB_OK : |
| 470 | type == alert_type::OKCANCEL ? eMB_OKCANCEL : |
| 471 | type == alert_type::YESNO ? eMB_YESNO : |
| 472 | type == alert_type::YESNOCANCEL ? eMB_YESNOCANCEL : |
| 473 | type == alert_type::NOYES ? eMB_NOYES : |
| 474 | eMB_OK, |
| 475 | icon == alert_icon::Default ? eMB_ICONDEFAULT : |
| 476 | icon == alert_icon::Error ? eMB_ICONERROR : |
| 477 | icon == alert_icon::Warning ? eMB_ICONWARNING : |
| 478 | icon == alert_icon::Question ? eMB_ICONQUESTION : |
| 479 | icon == alert_icon::Asterisk ? eMB_ICONASTERISK : |
| 480 | eMB_ICONDEFAULT |
| 481 | ); |
| 482 | return |
| 483 | ret == eIDOK ? alert_response::OK : |
| 484 | ret == eIDCANCEL ? alert_response::CANCEL : |
| 485 | ret == eIDYES ? alert_response::YES : |
| 486 | ret == eIDNO ? alert_response::NO : |
| 487 | alert_response::OK; |
| 488 | } |
| 489 | |
| 490 | } |