aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-emoticon-chooser-menu.c
blob: f2ed3376cf89e3593d5918640ede9a6b61872a8f (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * e-emoticon-chooser-menu.c
 *
 * Copyright (C) 2008 Novell, Inc.
 * Copyright (C) 2012 Dan Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-emoticon-chooser-menu.h"
#include "e-emoticon-chooser.h"

#include <glib/gi18n-lib.h>

enum {
    PROP_0,
    PROP_CURRENT_FACE
};

/* Forward Declarations */
static void e_emoticon_chooser_menu_interface_init
                    (EEmoticonChooserInterface *interface);

G_DEFINE_TYPE_WITH_CODE (
    EEmoticonChooserMenu,
    e_emoticon_chooser_menu,
    GTK_TYPE_MENU,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EMOTICON_CHOOSER,
        e_emoticon_chooser_menu_interface_init))

static void
emoticon_chooser_menu_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CURRENT_FACE:
            e_emoticon_chooser_set_current_emoticon (
                E_EMOTICON_CHOOSER (object),
                g_value_get_boxed (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
emoticon_chooser_menu_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_CURRENT_FACE:
            g_value_set_boxed (
                value,
                e_emoticon_chooser_get_current_emoticon (
                E_EMOTICON_CHOOSER (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static EEmoticon *
emoticon_chooser_menu_get_current_emoticon (EEmoticonChooser *chooser)
{
    GtkWidget *item;

    item = gtk_menu_get_active (GTK_MENU (chooser));
    if (item == NULL)
        return NULL;

    return g_object_get_data (G_OBJECT (item), "emoticon");
}

static void
emoticon_chooser_menu_set_current_emoticon (EEmoticonChooser *chooser,
                                            EEmoticon *emoticon)
{
    GList *list, *iter;

    list = gtk_container_get_children (GTK_CONTAINER (chooser));

    for (iter = list; iter != NULL; iter = iter->next) {
        GtkWidget *item = iter->data;
        EEmoticon *candidate;

        candidate = g_object_get_data (G_OBJECT (item), "emoticon");
        if (candidate == NULL)
            continue;

        if (e_emoticon_equal (emoticon, candidate)) {
            gtk_menu_shell_activate_item (
                GTK_MENU_SHELL (chooser), item, TRUE);
            break;
        }
    }

    g_list_free (list);
}

static void
e_emoticon_chooser_menu_class_init (EEmoticonChooserMenuClass *class)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = emoticon_chooser_menu_set_property;
    object_class->get_property = emoticon_chooser_menu_get_property;

    g_object_class_override_property (
        object_class, PROP_CURRENT_FACE, "current-emoticon");
}

static void
e_emoticon_chooser_menu_interface_init (EEmoticonChooserInterface *interface)
{
    interface->get_current_emoticon =
        emoticon_chooser_menu_get_current_emoticon;
    interface->set_current_emoticon =
        emoticon_chooser_menu_set_current_emoticon;
}

static void
e_emoticon_chooser_menu_init (EEmoticonChooserMenu *chooser_menu)
{
    EEmoticonChooser *chooser;
    GList *list, *iter;

    chooser = E_EMOTICON_CHOOSER (chooser_menu);
    list = e_emoticon_chooser_get_items ();

    for (iter = list; iter != NULL; iter = iter->next) {
        EEmoticon *emoticon = iter->data;
        GtkWidget *item;

        /* To keep translated strings in subclasses */
        item = gtk_image_menu_item_new_with_mnemonic (_(emoticon->label));
        gtk_image_menu_item_set_image (
            GTK_IMAGE_MENU_ITEM (item),
            gtk_image_new_from_icon_name (
            emoticon->icon_name, GTK_ICON_SIZE_MENU));
        gtk_widget_show (item);

        g_object_set_data_full (
            G_OBJECT (item), "emoticon",
            e_emoticon_copy (emoticon),
            (GDestroyNotify) e_emoticon_free);

        g_signal_connect_swapped (
            item, "activate",
            G_CALLBACK (e_emoticon_chooser_item_activated),
            chooser);

        gtk_menu_shell_append (GTK_MENU_SHELL (chooser_menu), item);
    }

    g_list_free (list);
}

GtkWidget *
e_emoticon_chooser_menu_new (void)
{
    return g_object_new (E_TYPE_EMOTICON_CHOOSER_MENU, NULL);
}