595 ls->index = index; |
595 ls->index = index; |
596 ls->oldcount = 0; |
596 ls->oldcount = 0; |
597 ls->getvalue = il->getvalue; |
597 ls->getvalue = il->getvalue; |
598 |
598 |
599 UiVar* var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST); |
599 UiVar* var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST); |
600 ls->list = var->value; |
600 ls->var = var; |
|
601 UiList *list = var->value; |
601 |
602 |
602 ls->callback = il->callback; |
603 ls->callback = il->callback; |
603 ls->userdata = il->userdata; |
604 ls->userdata = il->userdata; |
604 |
605 |
605 ls->list->observers = ui_add_observer( |
606 list->observers = ui_add_observer( |
606 ls->list->observers, |
607 list->observers, |
607 (ui_callback)ui_update_gmenu_item_list, |
608 (ui_callback)ui_update_gmenu_item_list, |
608 ls); |
609 ls); |
609 |
610 |
610 GSimpleAction *action = g_simple_action_new(item->id, g_variant_type_new("i")); |
611 GSimpleAction *action = g_simple_action_new(item->id, g_variant_type_new("i")); |
611 g_action_map_add_action(obj->ctx->action_map, G_ACTION(action)); |
612 g_action_map_add_action(obj->ctx->action_map, G_ACTION(action)); |
614 |
615 |
615 UiEventData *event = malloc(sizeof(UiEventData)); |
616 UiEventData *event = malloc(sizeof(UiEventData)); |
616 event->obj = obj; |
617 event->obj = obj; |
617 event->userdata = il->userdata; |
618 event->userdata = il->userdata; |
618 event->callback = il->callback; |
619 event->callback = il->callback; |
619 event->customdata = NULL; |
620 event->customdata = var; |
620 event->value = 0; |
621 event->value = 0; |
621 |
622 |
622 g_signal_connect( |
623 g_signal_connect( |
623 action, |
624 action, |
624 "activate", |
625 "activate", |
625 G_CALLBACK(ui_activate_event_wrapper), |
626 G_CALLBACK(ui_menu_list_item_activate_event_wrapper), |
626 event); |
627 event); |
627 g_signal_connect( |
628 g_signal_connect( |
628 obj->widget, |
629 obj->widget, |
629 "destroy", |
630 "destroy", |
630 G_CALLBACK(ui_destroy_userdata), |
631 G_CALLBACK(ui_destroy_userdata), |
646 evt.eventdata = event->customdata; |
647 evt.eventdata = event->customdata; |
647 evt.intval = intval; |
648 evt.intval = intval; |
648 event->callback(&evt, event->userdata); |
649 event->callback(&evt, event->userdata); |
649 } |
650 } |
650 |
651 |
|
652 void ui_menu_list_item_activate_event_wrapper(GSimpleAction* self, GVariant* parameter, UiEventData *event) { |
|
653 int index = g_variant_get_int32(parameter); |
|
654 UiVar *var = event->customdata; |
|
655 UiList *list = var->value; |
|
656 |
|
657 UiEvent evt; |
|
658 evt.obj = event->obj; |
|
659 evt.window = event->obj->window; |
|
660 evt.document = event->obj->ctx->document; |
|
661 evt.eventdata = ui_list_get(list, index); |
|
662 evt.intval = index; |
|
663 event->callback(&evt, event->userdata); |
|
664 |
|
665 } |
|
666 |
651 void ui_update_gmenu_item_list(UiEvent *event, UiActiveGMenuItemList *list) { |
667 void ui_update_gmenu_item_list(UiEvent *event, UiActiveGMenuItemList *list) { |
652 // remove old items |
668 // remove old items |
653 for(int i=0;i<list->oldcount;i++) { |
669 for(int i=0;i<list->oldcount;i++) { |
654 g_menu_remove(list->menu, list->index); |
670 g_menu_remove(list->menu, list->index); |
655 } |
671 } |
|
672 UiList *ls = list->var->value; |
656 |
673 |
657 // add list items |
674 // add list items |
658 ui_getvaluefunc getvalue = list->getvalue; |
675 ui_getvaluefunc getvalue = list->getvalue; |
659 int i = 0; |
676 int i = 0; |
660 void* elm = ui_list_first(list->list); |
677 void* elm = ui_list_first(ls); |
661 while(elm) { |
678 while(elm) { |
662 char *label = (char*) (getvalue ? getvalue(elm, 0) : elm); |
679 char *label = (char*) (getvalue ? getvalue(elm, 0) : elm); |
663 |
680 |
664 GMenuItem *item = g_menu_item_new(label, NULL); |
681 GMenuItem *item = g_menu_item_new(label, NULL); |
665 GVariant *v = g_variant_new("i", i); |
682 GVariant *v = g_variant_new("i", i); |
666 g_menu_item_set_action_and_target_value(item, list->action, v); |
683 g_menu_item_set_action_and_target_value(item, list->action, v); |
667 g_menu_insert_item(list->menu, list->index+i, item); |
684 g_menu_insert_item(list->menu, list->index+i, item); |
668 |
685 |
669 elm = ui_list_next(list->list); |
686 elm = ui_list_next(ls); |
670 i++; |
687 i++; |
671 } |
688 } |
672 |
689 |
673 list->oldcount = i; |
690 list->oldcount = i; |
674 } |
691 } |