ui/gtk/button.c

changeset 1205
749a32e91513
parent 1185
32f10747902b
child 1207
833c37fc5475
equal deleted inserted replaced
1204:bc12e849a0fe 1205:749a32e91513
332 "destroy", 332 "destroy",
333 G_CALLBACK(ui_destroy_vardata), 333 G_CALLBACK(ui_destroy_vardata),
334 event); 334 event);
335 } 335 }
336 336
337 if(onchange) { 337 if(onchange || action) {
338 UiEventData *event = malloc(sizeof(UiEventData)); 338 UiEventData *event = malloc(sizeof(UiEventData));
339 event->obj = obj; 339 event->obj = obj;
340 event->userdata = onchangedata; 340 event->userdata = onchangedata;
341 event->callback = onchange; 341 event->callback = onchange;
342 event->action = action ? strdup(action) : NULL, 342 event->action = action ? strdup(action) : NULL,
574 UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs *args) { 574 UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs *args) {
575 return ui_checkbox_create(obj, args); 575 return ui_checkbox_create(obj, args);
576 } 576 }
577 577
578 #endif 578 #endif
579
580 static void content_toggle_button_changed(UiContentToggleButton *button) {
581 if(button->toggled) {
582 gtk_button_set_label(GTK_BUTTON(button->widget), button->label1);
583 ui_button_set_icon_name(button->widget, button->icon1);
584 gtk_widget_set_tooltip_text(button->widget, button->tooltip1);
585 } else {
586 gtk_button_set_label(GTK_BUTTON(button->widget), button->label0);
587 ui_button_set_icon_name(button->widget, button->icon0);
588 gtk_widget_set_tooltip_text(button->widget, button->tooltip0);
589 }
590
591 UiEvent e;
592 e.obj = button->obj;
593 e.window = button->obj->window;
594 e.document = button->obj->ctx->document;
595 e.eventdata = NULL;
596 e.eventdatatype = 0;
597 e.intval = button->toggled;
598 e.set = ui_get_setop();
599
600 if(button->onchange) {
601 button->onchange(&e, button->onchangedata);
602 }
603
604 if(button->onchange_action) {
605 uic_action_callback(&e, button->onchange_action);
606 }
607 }
608
609 static void ui_content_toggle_button_toggled(GtkWidget *widget, UiContentToggleButton *button) {
610 button->toggled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
611 if(ui_get_setop() == 0) {
612 content_toggle_button_changed(button);
613 }
614 }
615
616 static void ui_content_toggle_button_clicked(GtkWidget *widget, UiContentToggleButton *button) {
617 button->toggled = !button->toggled;
618 content_toggle_button_changed(button);
619 }
620
621 static void ui_destroy_content_togglebutton(GtkWidget *widget, UiContentToggleButton *button) {
622 free(button->label0);
623 free(button->icon0);
624 free(button->tooltip0);
625 free(button->label1);
626 free(button->icon1);
627 free(button->tooltip1);
628 free(button->onchange_action);
629 free(button);
630 }
631
632 UIWIDGET ui_create_content_togglebutton(UiObject *obj, UiContentToggleArgs *args) {
633 UiContentToggleButton *button = malloc(sizeof(UiContentToggleButton));
634 memset(button, 0, sizeof(UiContentToggleButton));
635 button->obj = obj;
636 button->label0 = args->label0 ? strdup(args->label0) : NULL;
637 button->icon0 = args->icon0 ? strdup(args->icon0) : NULL;
638 button->tooltip0 = args->tooltip0 ? strdup(args->tooltip0) : NULL;
639 button->label1 = args->label1 ? strdup(args->label1) : NULL;
640 button->icon1 = args->icon1 ? strdup(args->icon1) : NULL;
641 button->tooltip1 = args->tooltip1 ? strdup(args->tooltip1) : NULL;
642 button->enable_state = args->enable_state;
643 button->var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER);
644 button->onchange = args->onchange;
645 button->onchangedata = args->onchangedata;
646 button->onchange_action = args->action ? strdup(args->action) : NULL;
647
648 const char *label = args->label0;
649 const char *icon = args->icon0;
650 const char *tooltip = args->tooltip0;
651 if(button->var) {
652 UiInteger *i = button->var->value;
653 i->obj = button;
654 i->get = ui_ctntogglebutton_get;
655 i->set = ui_ctntogglebutton_set;
656
657 if(i->value) {
658 label = args->label1;
659 icon = args->label1;
660 tooltip = args->tooltip1;
661 button->toggled = 1;
662 }
663 }
664
665 GtkWidget *widget;
666 if(args->istogglebutton) {
667 widget = gtk_toggle_button_new_with_label(label);
668
669 g_signal_connect(
670 widget,
671 "toggled",
672 G_CALLBACK(ui_content_toggle_button_toggled),
673 button);
674 } else {
675 widget = gtk_button_new_with_label(label);
676
677 g_signal_connect(
678 widget,
679 "clicked",
680 G_CALLBACK(ui_content_toggle_button_clicked),
681 button);
682 }
683 ui_button_set_icon_name(widget, icon);
684 gtk_widget_set_tooltip_text(widget, tooltip);
685 button->widget = widget;
686
687 g_signal_connect(
688 widget,
689 "destroy",
690 G_CALLBACK(ui_destroy_content_togglebutton),
691 button);
692
693 g_object_set_data(G_OBJECT(widget), "ui_content_togglebutton", button);
694 return widget;
695 }
696
697 UIWIDGET ui_content_togglebutton_create(UiObject *obj, UiContentToggleArgs *args) {
698 GtkWidget *widget = ui_create_content_togglebutton(obj, args);
699
700 ui_set_name_and_style(widget, args->name, args->style_class);
701 ui_set_widget_states(obj->ctx, widget, args->states);
702 UiContainerPrivate *ct = (UiContainerPrivate*)obj->container_end;
703 UiLayout layout = UI_ARGS2LAYOUT(args);
704 ct->add(ct, widget, &layout);
705 uic_widget_set_visibility_states(obj->ctx, widget, args->visibility_states);
706 return widget;
707 }
708
709 int64_t ui_ctntogglebutton_get(UiInteger *value) {
710 UiContentToggleButton *button = value->obj;
711 value->value = button->toggled;
712 return value->value;
713 }
714
715 void ui_ctntogglebutton_set(UiInteger *value, int64_t i) {
716 UiContentToggleButton *button = value->obj;
717 button->toggled = i != 0;
718 value->value = button->toggled;
719 content_toggle_button_changed(button);
720 }
579 721
580 #if GTK_MAJOR_VERSION >= 4 722 #if GTK_MAJOR_VERSION >= 4
581 #define RADIOBUTTON_NEW(group, label) gtk_check_button_new_with_label(label) 723 #define RADIOBUTTON_NEW(group, label) gtk_check_button_new_with_label(label)
582 #define RADIOBUTTON_SET_GROUP(button, group) 724 #define RADIOBUTTON_SET_GROUP(button, group)
583 #define RADIOBUTTON_GET_GROUP(button) GTK_CHECK_BUTTON(button) 725 #define RADIOBUTTON_GET_GROUP(button) GTK_CHECK_BUTTON(button)

mercurial