summaryrefslogtreecommitdiff
path: root/vcl/unx/gtk/window/gloactiongroup.cxx
blob: a409015c7ac51563a4a0fe496d1a5656d4a803c3 (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <unx/gtk/gloactiongroup.h>

#include <unx/gtk/gtkinst.hxx>
#include <unx/gtk/gtkframe.hxx>
#include <unx/gtk/gtksalmenu.hxx>
#include <vcl/menu.hxx>

#include <stdio.h>
#include <iostream>

using namespace std;

struct _GLOActionGroupPrivate
{
    GHashTable *table;  /* string -> GtkSalMenuItem* */
};

static void g_lo_action_group_iface_init (GActionGroupInterface *);

G_DEFINE_TYPE_WITH_CODE (GLOActionGroup,
    g_lo_action_group, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
                           g_lo_action_group_iface_init));

static gchar **
g_lo_action_group_list_actions (GActionGroup *group)
{
    GLOActionGroup *loGroup = G_LO_ACTION_GROUP (group);
    GHashTableIter iter;
    gint n, i = 0;
    gchar **keys;
    gpointer key;

    n = g_hash_table_size (loGroup->priv->table);
    keys = g_new (gchar *, n + 1);

    g_hash_table_iter_init (&iter, loGroup->priv->table);
    while (g_hash_table_iter_next (&iter, &key, NULL))
        keys[i++] = g_strdup ((gchar*) key);
    g_assert_cmpint (i, ==, n);
    keys[n] = NULL;

    return keys;
}

static gboolean
g_lo_action_group_query_action (GActionGroup        *group,
                                const gchar         *action_name,
                                gboolean            *enabled,
                                const GVariantType **parameter_type,
                                const GVariantType **state_type,
                                GVariant           **state_hint,
                                GVariant           **state)
{
    GLOActionGroup *loGroup = G_LO_ACTION_GROUP (group);
    GtkSalMenuItem* item_info;

    item_info = static_cast< GtkSalMenuItem* >( g_hash_table_lookup (loGroup->priv->table, action_name) );

    if (item_info == NULL)
        return FALSE;

    if (enabled) {
        sal_Bool bEnabled = item_info->mpVCLMenu->IsItemEnabled( item_info->mnId );
        *enabled = (bEnabled) ? TRUE : FALSE;
    }

    if (parameter_type)
        *parameter_type = NULL;

    if (state_type)
        *state_type = item_info->mpStateType;

    if (state_hint)
        *state_hint = NULL;

    if (state) {
        if (item_info->mpState) {
            g_variant_ref( item_info->mpState );
            *state = item_info->mpState;
        } else {
            *state = NULL;
        }
    }

    return TRUE;
}

static void
g_lo_action_group_change_state (GActionGroup *group,
                                const gchar  *action_name,
                                GVariant     *value)
{
    if (!action_name || !value)
        return;

    GLOActionGroup* lo_group = G_LO_ACTION_GROUP (group);
    GtkSalMenuItem* item_info;

    item_info = static_cast<GtkSalMenuItem*>( g_hash_table_lookup (lo_group->priv->table, action_name) );

    if (!item_info)
        return;

    if (!item_info->mpStateType) {
        item_info->mpStateType = g_variant_type_copy(g_variant_get_type(value));
    }

    if (g_variant_is_of_type(value, item_info->mpStateType)) {
        if (item_info->mpState)
            g_variant_unref(item_info->mpState);

        item_info->mpState = g_variant_new_variant(value);
        g_action_group_action_state_changed(group, action_name, value);
    }
}

static void
g_lo_action_group_activate (GActionGroup *group,
                            const gchar  *action_name,
                            GVariant     *parameter)
{
    GLOActionGroup *loGroup = G_LO_ACTION_GROUP (group);
    GtkSalMenuItem *pSalMenuItem;

    pSalMenuItem = static_cast< GtkSalMenuItem* >( g_hash_table_lookup (loGroup->priv->table, action_name) );

    if (pSalMenuItem == NULL || pSalMenuItem->mpSubMenu )
        return;

    GTK_YIELD_GRAB();

    const GtkSalFrame *pFrame = pSalMenuItem->mpParentMenu ? pSalMenuItem->mpParentMenu->GetFrame() : NULL;

    if ( pFrame && !pFrame->GetParent() ) {
        ((PopupMenu*) pSalMenuItem->mpVCLMenu)->SetSelectedEntry( pSalMenuItem->mnId );
        SalMenuEvent aMenuEvt( pSalMenuItem->mnId, pSalMenuItem->mpVCLMenu );
        pFrame->CallCallback( SALEVENT_MENUCOMMAND, &aMenuEvt );
    }
    else if ( pSalMenuItem->mpVCLMenu )
    {
        // if an item from submenu was selected. the corresponding Window does not exist because
        // we use native popup menus, so we have to set the selected menuitem directly
        // incidentally this of course works for top level popup menus, too
        PopupMenu * pPopupMenu = dynamic_cast<PopupMenu *>(pSalMenuItem->mpVCLMenu);
        if( pPopupMenu )
        {
            // FIXME: revise this ugly code

            // select handlers in vcl are dispatch on the original menu
            // if not consumed by the select handler of the current menu
            // however since only the starting menu ever came into Execute
            // the hierarchy is not build up. Workaround this by getting
            // the menu it should have been

            // get started from hierarchy in vcl menus
            GtkSalMenu* pParentMenu = pSalMenuItem->mpParentMenu;
            Menu* pCurMenu = pSalMenuItem->mpVCLMenu;
            while( pParentMenu && pParentMenu->GetMenu() )
            {
                pCurMenu = pParentMenu->GetMenu();
                pParentMenu = pParentMenu->GetParentSalMenu();
            }

            pPopupMenu->SetSelectedEntry( pSalMenuItem->mnId );
            pPopupMenu->ImplSelectWithStart( pCurMenu );
        }
        else
        {
            OSL_FAIL( "menubar item without frame !" );
        }
    }
}

void
g_lo_action_group_insert (GLOActionGroup *group,
                          const gchar    *action_name,
                          gpointer        action_info)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (group));

    gpointer old_action;

    old_action = g_hash_table_lookup (group->priv->table, action_name);

    if (old_action != action_info)
    {
        if (old_action != NULL)
            g_action_group_action_removed (G_ACTION_GROUP (group), action_name);

        g_hash_table_insert (group->priv->table, g_strdup (action_name), action_info);

        g_action_group_action_added (G_ACTION_GROUP (group), action_name);
    }
}

static void
g_lo_action_group_finalize (GObject *object)
{
    GLOActionGroup *loGroup = G_LO_ACTION_GROUP (object);

    g_hash_table_unref (loGroup->priv->table);

    G_OBJECT_CLASS (g_lo_action_group_parent_class)->finalize (object);
}

static void
g_lo_action_group_init (GLOActionGroup *group)
{
    group->priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
                                                 G_TYPE_LO_ACTION_GROUP,
                                                 GLOActionGroupPrivate);
    group->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  g_free, NULL );
}

static void
g_lo_action_group_class_init (GLOActionGroupClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = g_lo_action_group_finalize;

    g_type_class_add_private (klass, sizeof (GLOActionGroupPrivate));
}

static void
g_lo_action_group_iface_init (GActionGroupInterface *iface)
{
    iface->list_actions = g_lo_action_group_list_actions;
    iface->query_action = g_lo_action_group_query_action;
    iface->change_action_state = g_lo_action_group_change_state;
    iface->activate_action = g_lo_action_group_activate;
}

GLOActionGroup *
g_lo_action_group_new (void)
{
    return G_LO_ACTION_GROUP( g_object_new (G_TYPE_LO_ACTION_GROUP, NULL) );
}

void
g_lo_action_group_set_action_enabled (GLOActionGroup *group,
                                      const gchar    *action_name,
                                      gboolean        enabled)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (group));

    g_action_group_action_enabled_changed(G_ACTION_GROUP(group),
                                          action_name,
                                          enabled);

}

void
g_lo_action_group_remove (GLOActionGroup *group,
                          const gchar    *action_name)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (group));

    if (action_name != NULL)
    {
        g_action_group_action_removed (G_ACTION_GROUP (group), action_name);
        g_hash_table_remove (group->priv->table, action_name);
    }
}

void
g_lo_action_group_clear (GLOActionGroup  *group)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (group));

    g_hash_table_remove_all(group->priv->table);
}

void
g_lo_action_group_merge (GLOActionGroup *input_group,
                         GLOActionGroup *output_group)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (input_group));
    g_return_if_fail (G_IS_LO_ACTION_GROUP (output_group));
    g_return_if_fail (input_group != NULL);
    g_return_if_fail (output_group != NULL);

    GHashTableIter iter;
    gpointer key, value;

    g_hash_table_iter_init (&iter, input_group->priv->table);

    while (g_hash_table_iter_next (&iter, &key, &value))
    {
        g_lo_action_group_insert(output_group, (gchar*) key, value);
    }
}