summaryrefslogtreecommitdiff
path: root/src/libaudgui/menu.cc
blob: 3069d8bf3d520055463653882e0ede19725da758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * menu.c
 * Copyright 2011-2014 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "menu.h"

#include <libaudcore/hook.h>
#include <libaudcore/i18n.h>
#include <libaudcore/runtime.h>

static GtkWidget * image_menu_item_new (const char * text, const char * icon)
{
    GtkWidget * widget = gtk_image_menu_item_new_with_mnemonic (text);

    if (icon)
    {
        GtkWidget * image = gtk_image_new_from_icon_name (icon, GTK_ICON_SIZE_MENU);
        gtk_image_menu_item_set_image ((GtkImageMenuItem *) widget, image);
    }

    return widget;
}

static void toggled_cb (GtkCheckMenuItem * check, const AudguiMenuItem * item)
{
    gboolean on = gtk_check_menu_item_get_active (check);

    if (aud_get_bool (item->csect, item->cname) == on)
        return;

    aud_set_bool (item->csect, item->cname, on);

    if (item->func)
        item->func ();
}

static void hook_cb (void * data, GtkWidget * check)
{
    const AudguiMenuItem * item = (const AudguiMenuItem *) g_object_get_data
     ((GObject *) check, "item");
    gtk_check_menu_item_set_active ((GtkCheckMenuItem *) check, aud_get_bool
     (item->csect, item->cname));
}

static void unhook_cb (GtkCheckMenuItem * check, const AudguiMenuItem * item)
{
    hook_dissociate (item->hook, (HookFunction) hook_cb, check);
}

EXPORT GtkWidget * audgui_menu_item_new_with_domain
 (const AudguiMenuItem * item, GtkAccelGroup * accel, const char * domain)
{
    const char * name = domain ? dgettext (domain, item->name) : item->name;
    GtkWidget * widget = nullptr;

    if (name && item->func && ! item->cname) /* normal widget */
    {
        widget = image_menu_item_new (name, item->icon);
        g_signal_connect (widget, "activate", item->func, nullptr);
    }
    else if (name && item->cname) /* toggle widget */
    {
        widget = gtk_check_menu_item_new_with_mnemonic (name);
        gtk_check_menu_item_set_active ((GtkCheckMenuItem *) widget,
         aud_get_bool (item->csect, item->cname));
        g_signal_connect (widget, "toggled", (GCallback) toggled_cb, (void *) item);

        if (item->hook)
        {
            g_object_set_data ((GObject *) widget, "item", (void *) item);
            hook_associate (item->hook, (HookFunction) hook_cb, widget);
            g_signal_connect (widget, "destroy", (GCallback) unhook_cb, (void *) item);
        }
    }
    else if (name && (item->items.len || item->get_sub)) /* submenu */
    {
        widget = image_menu_item_new (name, item->icon);

        GtkWidget * sub;

        if (item->get_sub)
            sub = item->get_sub ();
        else
        {
            sub = gtk_menu_new ();
            audgui_menu_init_with_domain (sub, item->items, accel, domain);
        }

        gtk_menu_item_set_submenu ((GtkMenuItem *) widget, sub);
    }
    else if (item->sep) /* separator */
        widget = gtk_separator_menu_item_new ();

    if (widget && accel && item->key)
        gtk_widget_add_accelerator (widget, "activate", accel, item->key,
         item->mod, GTK_ACCEL_VISIBLE);

    return widget;
}

EXPORT void audgui_menu_init_with_domain (GtkWidget * shell,
 ArrayRef<AudguiMenuItem> items, GtkAccelGroup * accel,
 const char * domain)
{
    for (const AudguiMenuItem & item : items)
    {
        GtkWidget * widget = audgui_menu_item_new_with_domain (& item, accel, domain);
        if (! widget)
            continue;

        gtk_widget_show (widget);
        gtk_menu_shell_append ((GtkMenuShell *) shell, widget);
    }
}