summaryrefslogtreecommitdiff
path: root/vcl/unx/gtk/window/gloactiongroup.cxx
blob: 1a0e88fbd107ef661935725a1b81b0769ffbc629 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 * Copyright © 2011 Canonical Ltd.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * licence, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 *
 * Author: Antonio Fern√°ndez <antonio.fernandez@aentos.es>
 */

#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;

/*
 * GLOAction
 */

#define G_TYPE_LO_ACTION                                (g_lo_action_get_type ())
#define G_LO_ACTION(inst)                               (G_TYPE_CHECK_INSTANCE_CAST ((inst),                     \
                                                         G_TYPE_LO_ACTION, GLOAction))
#define G_IS_LO_ACTION(inst)                            (G_TYPE_CHECK_INSTANCE_TYPE ((inst),                     \
                                                         G_TYPE_LO_ACTION))

struct _GLOAction
{
    GObject         parent_instance;

    GtkSalMenuItem* item;               // A pointer to the menu item.
    gboolean        enabled;            // TRUE if action is enabled, FALSE otherwise.
    GVariantType*   parameter_type;     // A GVariantType with the action parameter type.
    GVariantType*   state_type;         // A GVariantType with item state type
    GVariant*       state_hint;         // A GVariant with state hints.
    GVariant*       state;              // A GVariant with current item state
};

typedef GObjectClass GLOActionClass;
typedef struct _GLOAction GLOAction;

G_DEFINE_TYPE (GLOAction, g_lo_action, G_TYPE_OBJECT);

GLOAction*
g_lo_action_new (void)
{
    return G_LO_ACTION (g_object_new (G_TYPE_LO_ACTION, NULL));
}

static void
g_lo_action_init (GLOAction *action)
{
    action->item = NULL;
    action->enabled = TRUE;
    action->parameter_type = NULL;
    action->state_type = NULL;
    action->state_hint = NULL;
    action->state = NULL;
}

static void
g_lo_action_finalize (GObject *object)
{
    GLOAction* action = G_LO_ACTION(object);

    action->item = NULL;

    if (action->parameter_type)
        g_variant_type_free (action->parameter_type);

    if (action->state_type)
        g_variant_type_free (action->state_type);

    if (action->state_hint)
        g_variant_unref (action->state_hint);

    if (action->state)
        g_variant_unref (action->state);

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

static void
g_lo_action_class_init (GLOActionClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    object_class->finalize = g_lo_action_finalize;
}

/*
 * GLOActionGroup
 */

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 *lo_group = G_LO_ACTION_GROUP (group);
    GLOAction* action;

    action = G_LO_ACTION (g_hash_table_lookup (lo_group->priv->table, action_name));

    if (action == NULL)
        return FALSE;

    if (enabled)
        *enabled = action->enabled;

    if (parameter_type) {
        *parameter_type = action->parameter_type;
    }

    if (state_type) {
        *state_type = action->state_type;
    }

    if (state_hint) {
        *state_hint = (action->state_hint) ? g_variant_ref(action->state_hint) : NULL;
    }

    if (state) {
        *state = (action->state) ? g_variant_ref(action->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);

    GLOAction* action = G_LO_ACTION (g_hash_table_lookup (lo_group->priv->table, action_name));

    if (action == NULL)
        return;

    if (action->state_type == NULL)
        action->state_type = g_variant_type_copy(g_variant_get_type(value));

    g_return_if_fail (g_variant_is_of_type(value, action->state_type) == TRUE);

    if (action->state)
        g_variant_unref(action->state);

    action->state = g_variant_take_ref(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)
{
    GTK_YIELD_GRAB();

    GLOActionGroup *loGroup = G_LO_ACTION_GROUP (group);
    GLOAction* action = G_LO_ACTION (g_hash_table_lookup (loGroup->priv->table, action_name));
    GtkSalMenuItem *pSalMenuItem = action->item;

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

    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_lo_action_group_insert_stateful (group, action_name, action_info, NULL, NULL, NULL, NULL);
}

void
g_lo_action_group_insert_stateful (GLOActionGroup     *group,
                                   const gchar        *action_name,
                                   gpointer            action_info,
                                   const GVariantType *parameter_type,
                                   const GVariantType *state_type,
                                   GVariant           *state_hint,
                                   GVariant           *state)
{
    g_return_if_fail (G_IS_LO_ACTION_GROUP (group));

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

    if (old_action == NULL || old_action->item != action_info)
    {
        if (old_action != NULL)
            g_action_group_action_removed (G_ACTION_GROUP (group), action_name);

        GLOAction* action = g_lo_action_new();

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

        action->item = static_cast< GtkSalMenuItem* >( action_info );

        if (parameter_type)
            action->parameter_type = (GVariantType*) parameter_type;

        if (state_type)
            action->state_type = (GVariantType*) state_type;

        if (state_hint)
            action->state_hint = g_variant_take_ref (state_hint);

        if (state)
            action->state = g_variant_take_ref (state);

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

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

    g_hash_table_unref (lo_group->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, g_object_unref);
}

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_return_if_fail (action_name != NULL);

    GLOAction* action = G_LO_ACTION (g_hash_table_lookup (group->priv->table, action_name));

    if (action == NULL)
        return;

    action->enabled = enabled;

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

gpointer
g_lo_action_group_get_action_item (GLOActionGroup *group,
                                   const gchar    *action_name)
{
    g_return_val_if_fail (G_IS_LO_ACTION_GROUP (group), NULL);
    g_return_val_if_fail (action_name != NULL, NULL);

    GLOAction* action = G_LO_ACTION (g_hash_table_lookup (group->priv->table, action_name));

    return (action != NULL) ? action->item : NULL;
}

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));

    GList* keys = g_hash_table_get_keys (group->priv->table);

    for (GList* element = g_list_first (keys); element != NULL; element = g_list_next (element))
    {
        g_lo_action_group_remove (group, (gchar*) element->data);
    }
}

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);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */