|
25 | 25 | #include "develop/imageop.h" |
26 | 26 | #include "libs/lib.h" |
27 | 27 |
|
| 28 | +#include <glib-2.0/gio/gmenu.h> |
| 29 | +#include <glib-2.0/gio/gmenumodel.h> |
28 | 30 | #include <libxml/encoding.h> |
29 | 31 | #include <libxml/xmlwriter.h> |
30 | 32 | #include <libxml/parser.h> |
@@ -548,6 +550,84 @@ GtkWidget *dt_insert_preset_in_menu_hierarchy(const char *name, |
548 | 550 | return mi; |
549 | 551 | } |
550 | 552 |
|
| 553 | +static void _menu_shell_insert_sorted2(GMenu *menu, |
| 554 | + GMenuItem *item, |
| 555 | + const gchar *name) |
| 556 | +{ |
| 557 | + GMenuModel *model = G_MENU_MODEL(menu); |
| 558 | + |
| 559 | + int num = g_menu_model_get_n_items(model); |
| 560 | + gboolean found = FALSE; |
| 561 | + int i; |
| 562 | + for(i = 0; i < num; i++) |
| 563 | + { |
| 564 | + gchar *item_label = NULL; |
| 565 | + GVariant *attr = g_menu_model_get_item_attribute_value(model, i, "label", G_VARIANT_TYPE_STRING); |
| 566 | + if(attr) |
| 567 | + { |
| 568 | + item_label = g_variant_dup_string(attr, NULL); |
| 569 | + g_variant_unref(attr); |
| 570 | + if(g_utf8_collate(item_label, name) > 0) found = TRUE; |
| 571 | + g_free(item_label); |
| 572 | + if(found) break; |
| 573 | + } |
| 574 | + } |
| 575 | + |
| 576 | + g_menu_insert_item(menu, i, item); |
| 577 | +} |
| 578 | + |
| 579 | +void dt_insert_preset_in_menu_hierarchy2(const char *name, |
| 580 | + const char *action, |
| 581 | + GSList **menu_path, |
| 582 | + GMenu *mainmenu, |
| 583 | + GMenu **submenu, |
| 584 | + gchar ***prev_split, |
| 585 | + gboolean isdefault) |
| 586 | +{ |
| 587 | + gchar *local_name = dt_util_localize_segmented_name(name, FALSE); |
| 588 | + gchar **split = g_strsplit(local_name, "|", -1); |
| 589 | + gchar **s = split; |
| 590 | + gchar **p = *prev_split; |
| 591 | + GSList *mpath = *menu_path; |
| 592 | + GMenuItem *mi; |
| 593 | + g_free(local_name); |
| 594 | + |
| 595 | + for(; p && *(p+1) && *(s+1) && !g_strcmp0(*s, *p); p++, s++) |
| 596 | + ; |
| 597 | + |
| 598 | + for(; p && *(p+1); p++) |
| 599 | + { |
| 600 | + mpath = g_slist_delete_link(mpath, mpath); // pop |
| 601 | + *submenu = mpath ? mpath->data : mainmenu; |
| 602 | + } |
| 603 | + |
| 604 | + for(; *(s+1); s++) |
| 605 | + { |
| 606 | + GMenu *sm = g_menu_new(); |
| 607 | + GMenuItem *smi = g_menu_item_new_submenu(*s, G_MENU_MODEL(sm)); |
| 608 | + _menu_shell_insert_sorted2(*submenu, smi, *s); |
| 609 | + *submenu = sm; |
| 610 | + mpath = g_slist_prepend(mpath, sm); // push |
| 611 | + } |
| 612 | + |
| 613 | + *menu_path = mpath; |
| 614 | + g_strfreev(*prev_split); |
| 615 | + *prev_split = split; |
| 616 | + |
| 617 | + if(isdefault) |
| 618 | + { |
| 619 | + gchar *label = g_strdup_printf("%s %s", *s, _("(default)")); |
| 620 | + mi = g_menu_item_new(label, action); |
| 621 | + _menu_shell_insert_sorted2(*submenu, mi, label); |
| 622 | + g_free(label); |
| 623 | + } |
| 624 | + else |
| 625 | + { |
| 626 | + mi = g_menu_item_new(*s, action); |
| 627 | + _menu_shell_insert_sorted2(*submenu, mi, *s); |
| 628 | + } |
| 629 | +} |
| 630 | + |
551 | 631 | // clang-format off |
552 | 632 | // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py |
553 | 633 | // vim: shiftwidth=2 expandtab tabstop=2 cindent |
|
0 commit comments