File: GtkActionGroup.xs

package info (click to toggle)
libgtk2-perl 2:1.2499-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,668 kB
  • ctags: 594
  • sloc: perl: 22,100; ansic: 122; makefile: 17
file content (574 lines) | stat: -rw-r--r-- 18,175 bytes parent folder | download | duplicates (5)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * Copyright (c) 2003-2005, 2010 by the gtk2-perl team (see the file AUTHORS)
 *
 * Licensed under the LGPL, see LICENSE file for more information.
 *
 * $Id$
 */

#include "gtk2perl.h"
#include "gtk2perl-private.h" /* For the translate callback. */

/* helper for using gperl_signal_connect when you don't have the SV of
 * the instance... */
#define WRAPINSTANCE(object)	(sv_2mortal (newSVGObject (G_OBJECT (object))))


/* these macros expect there to exist an SV** named svp */

#define HFETCHPV(hv, key)	\
	(((svp = hv_fetch ((hv), (key), strlen ((key)), FALSE))	\
	  && gperl_sv_is_defined (*svp))					\
	  ? SvPV_nolen (*svp)					\
	  : NULL)

#define HFETCHCV(hv, key)	\
	(((svp = hv_fetch ((hv), (key), strlen ((key)), FALSE))	\
	  && gperl_sv_is_defined (*svp))					\
	  ? (gpointer)(*svp)					\
	  : NULL)

#define HFETCHIV(hv, key)	\
	(((svp = hv_fetch ((hv), (key), strlen ((key)), FALSE))	\
	  && gperl_sv_is_defined (*svp))					\
	  ? SvIV (*svp)						\
	  : 0)

#define AFETCHPV(av, index)	\
	(((svp = av_fetch ((av), (index), FALSE)) && gperl_sv_is_defined (*svp))	\
	 ? SvPV_nolen (*svp)						\
	 : NULL)

#define AFETCHCV(av, index)	\
	(((svp = av_fetch ((av), (index), FALSE)) && gperl_sv_is_defined (*svp))	\
	 ? (gpointer)(*svp)						\
	 : NULL)

#define AFETCHIV(av, index)	\
	(((svp = av_fetch ((av), (index), FALSE)) && gperl_sv_is_defined (*svp))	\
	 ? SvIV (*svp)							\
	 : 0)


/*
struct _GtkActionEntry
{
  gchar     *name;
  gchar     *stock_id;
  gchar     *label;
  gchar     *accelerator;
  gchar     *tooltip;
  GCallback  callback;
};
*/

static void
read_action_entry_from_sv (SV * sv,
                           GtkActionEntry * action)
{
	SV ** svp;
	if (!gperl_sv_is_defined (sv) || !SvROK (sv))
		croak ("invalid action entry");

	switch (SvTYPE (SvRV (sv))) {
	    case SVt_PVHV:
		{
		HV * hv = (HV*) SvRV (sv);
		action->name        = HFETCHPV (hv, "name");
		action->stock_id    = HFETCHPV (hv, "stock_id");
		action->label       = HFETCHPV (hv, "label");
		action->accelerator = HFETCHPV (hv, "accelerator");
		action->tooltip     = HFETCHPV (hv, "tooltip");
		action->callback    = HFETCHCV (hv, "callback");
		}
		break;
	    case SVt_PVAV:
		{
		AV * av = (AV*) SvRV (sv);
		action->name        = AFETCHPV (av, 0);
		action->stock_id    = AFETCHPV (av, 1);
		action->label       = AFETCHPV (av, 2);
		action->accelerator = AFETCHPV (av, 3);
		action->tooltip     = AFETCHPV (av, 4);
		action->callback    = AFETCHCV (av, 5);
		}
		break;
	    default:
		croak ("action entry must be a hash or an array");
	}
}


/*
struct _GtkToggleActionEntry
{
  gchar     *name;
  gchar     *stock_id;
  gchar     *label;
  gchar     *accelerator;
  gchar     *tooltip;
  GCallback  callback;
  gboolean   is_active;
};
*/

static void
read_toggle_action_entry_from_sv (SV * sv,
                                  GtkToggleActionEntry * action)
{
	SV ** svp;
	if (!gperl_sv_is_defined (sv) || !SvROK (sv))
		croak ("invalid toggle action entry");

	switch (SvTYPE (SvRV (sv))) {
	    case SVt_PVHV:
		{
		HV * hv = (HV*) SvRV (sv);
		action->name        = HFETCHPV (hv, "name");
		action->stock_id    = HFETCHPV (hv, "stock_id");
		action->label       = HFETCHPV (hv, "label");
		action->accelerator = HFETCHPV (hv, "accelerator");
		action->tooltip     = HFETCHPV (hv, "tooltip");
		action->callback    = HFETCHCV (hv, "callback");
		action->is_active   = HFETCHIV (hv, "is_active");
		}
		break;
	    case SVt_PVAV:
		{
		AV * av = (AV*) SvRV (sv);
		if (av_len (av) < 5)
			croak ("not enough items in array form of toggle action entry; expecting:\n"
			       "     [ name, stock_id, label, accelerator, tooltip, value]\n"
			       "  ");
		action->name        = AFETCHPV (av, 0);
		action->stock_id    = AFETCHPV (av, 1);
		action->label       = AFETCHPV (av, 2);
		action->accelerator = AFETCHPV (av, 3);
		action->tooltip     = AFETCHPV (av, 4);
		action->callback    = AFETCHCV (av, 5);
		action->is_active   = AFETCHIV (av, 6);
		}
		break;
	    default:
		croak ("action entry must be a hash or an array");
	}
}


/*
struct _GtkRadioActionEntry
{
  gchar *name;
  gchar *stock_id;
  gchar *label;
  gchar *accelerator;
  gchar *tooltip;
  gint   value;
};
*/

static void
read_radio_action_entry_from_sv (SV * sv,
                                 GtkRadioActionEntry * action)
{
	SV ** svp;
	if (!gperl_sv_is_defined (sv) || !SvROK (sv))
		croak ("invalid radio action entry");

	switch (SvTYPE (SvRV (sv))) {
	    case SVt_PVHV:
		{
		HV * hv = (HV*) SvRV (sv);
		action->name        = HFETCHPV (hv, "name");
		action->stock_id    = HFETCHPV (hv, "stock_id");
		action->label       = HFETCHPV (hv, "label");
		action->accelerator = HFETCHPV (hv, "accelerator");
		action->tooltip     = HFETCHPV (hv, "tooltip");
		action->value       = HFETCHIV (hv, "value");
		}
		break;
	    case SVt_PVAV:
		{
		AV * av = (AV*) SvRV (sv);
		if (av_len (av) < 5)
			croak ("not enough items in array form of radio action entry; expecting:\n"
			       "     [ name, stock_id, label, accelerator, tooltip, value]\n"
			       "  ");
		action->name        = AFETCHPV (av, 0);
		action->stock_id    = AFETCHPV (av, 1);
		action->label       = AFETCHPV (av, 2);
		action->accelerator = AFETCHPV (av, 3);
		action->tooltip     = AFETCHPV (av, 4);
		action->value       = AFETCHIV (av, 5);
		}
		break;
	    default:
		croak ("action entry must be a hash or an array");
	}
}

MODULE = Gtk2::ActionGroup	PACKAGE = Gtk2::ActionGroup	PREFIX = gtk_action_group_


=for position DESCRIPTION

=head2 NOTE: Translation

In C, gtk+'s action groups can use the translation domain to ensure that action
labels and tooltips are translated along with the rest of the app.  However,
the translation function was not available for calling B<by> the Perl bindings
until gtk+ 2.6; that is, setting the translation domain had no effect.
Translation of action groups is supported in Perl as of Gtk2 1.080 using
gtk+ 2.6.0 or later.

=cut


GtkActionGroup_noinc *gtk_action_group_new (class, const gchar *name);
    C_ARGS:
	name

const gchar *gtk_action_group_get_name (GtkActionGroup *action_group);

void gtk_action_group_set_sensitive (GtkActionGroup *action_group, gboolean sensitive);

gboolean gtk_action_group_get_sensitive (GtkActionGroup *action_group);

void gtk_action_group_set_visible (GtkActionGroup *action_group, gboolean sensitive);

gboolean gtk_action_group_get_visible (GtkActionGroup *action_group);

GtkAction *gtk_action_group_get_action (GtkActionGroup *action_group, const gchar *action_name);

void gtk_action_group_list_actions (GtkActionGroup *action_group);
    PREINIT:
	GList * actions, * i;
    PPCODE:
	actions = gtk_action_group_list_actions (action_group);
	for (i = actions ; i != NULL ; i = i->next)
		XPUSHs (sv_2mortal (newSVGtkAction (i->data)));
	g_list_free (actions);

void gtk_action_group_add_action (GtkActionGroup *action_group, GtkAction *action);

void gtk_action_group_add_action_with_accel (GtkActionGroup *action_group, GtkAction *action, const gchar_ornull *accelerator);

void gtk_action_group_remove_action (GtkActionGroup *action_group, GtkAction *action);

##void gtk_action_group_add_actions (GtkActionGroup *action_group, GtkActionEntry *entries, guint n_entries, gpointer user_data);
##void gtk_action_group_add_actions_full (GtkActionGroup *action_group, GtkActionEntry *entries, guint n_entries, gpointer user_data, GDestroyNotify destroy);
void
gtk_action_group_add_actions (action_group, action_entries, user_data=NULL)
	GtkActionGroup * action_group
	SV * action_entries
	SV * user_data
    PREINIT:
	AV * av;
	GtkActionEntry * entries;
	gint n_actions, i;
    CODE:
	if (!gperl_sv_is_array_ref (action_entries))
		croak ("actions must be a reference to an array of action entries");
	av = (AV*) SvRV (action_entries);
	n_actions = av_len (av) + 1;
	if (n_actions < 1)
		croak ("action array is empty");
	entries = gperl_alloc_temp (sizeof (GtkActionEntry) * n_actions);
	for (i = 0 ; i < n_actions ; i++) {
		SV ** svp = av_fetch (av, i, 0);
		read_action_entry_from_sv (*svp, entries+i);
	}

	for (i = 0 ; i < n_actions ; i++) {
		GtkAction * action;
		gchar * accel_path;
		const gchar * label;
		const gchar * tooltip;
#if GTK_CHECK_VERSION (2, 6, 0)
		label = gtk_action_group_translate_string (action_group,
							   entries[i].label);
		tooltip =
			gtk_action_group_translate_string (action_group,
							   entries[i].tooltip);
#else
		label = entries[i].label;
		tooltip = entries[i].tooltip;
#endif

		action = gtk_action_new (entries[i].name,
		                         label,
		                         tooltip,
		                         entries[i].stock_id);
		if (entries[i].callback)
			gperl_signal_connect (WRAPINSTANCE (action),
			                      "activate",
					      (SV*)(entries[i].callback),
					      user_data, 0);

		/* set the accel path for the menu item */
		accel_path = g_strconcat
				("<Actions>/",
				 gtk_action_group_get_name (action_group),
				 "/", entries[i].name, NULL);

		if (entries[i].accelerator) {
			guint accel_key = 0;
			GdkModifierType accel_mods;

			gtk_accelerator_parse (entries[i].accelerator,
			                       &accel_key, &accel_mods);
			if (accel_key)
				gtk_accel_map_add_entry (accel_path,
				                         accel_key,
				                         accel_mods);
		}

		gtk_action_set_accel_path (action, accel_path);
		g_free (accel_path);

		gtk_action_group_add_action (action_group, action);
		g_object_unref (action);
	}

##void gtk_action_group_add_toggle_actions (GtkActionGroup *action_group, GtkToggleActionEntry *entries, guint n_entries, gpointer user_data);
##void gtk_action_group_add_toggle_actions_full (GtkActionGroup *action_group, GtkToggleActionEntry *entries, guint n_entries, gpointer user_data, GDestroyNotify destroy);
void
gtk_action_group_add_toggle_actions (action_group, toggle_action_entries, user_data=NULL)
	GtkActionGroup * action_group
	SV * toggle_action_entries
	SV * user_data
    PREINIT:
	AV * av;
	GtkToggleActionEntry * entries;
	gint n_actions, i;
    CODE:
	if (!gperl_sv_is_array_ref (toggle_action_entries))
		croak ("entries must be a reference to an array of toggle action entries");
	av = (AV*) SvRV (toggle_action_entries);
	n_actions = av_len (av) + 1;
	if (n_actions < 1)
		croak ("toggle action array is empty");
	entries = gperl_alloc_temp (sizeof (GtkToggleActionEntry) * n_actions);
	for (i = 0 ; i < n_actions ; i++) {
		SV ** svp = av_fetch (av, i, 0);
		read_toggle_action_entry_from_sv (*svp, entries+i);
	}

	for (i = 0 ; i < n_actions ; i++) {
		GtkAction * action;
		gchar * accel_path;
		const gchar * label;
		const gchar * tooltip;
#if GTK_CHECK_VERSION (2, 6, 0)
		label = gtk_action_group_translate_string (action_group,
							   entries[i].label);
		tooltip =
			gtk_action_group_translate_string (action_group,
							   entries[i].tooltip);
#else
		label = entries[i].label;
		tooltip = entries[i].tooltip;
#endif

		action = g_object_new (GTK_TYPE_TOGGLE_ACTION,
		                       "name", entries[i].name,
		                       "label", label,
		                       "tooltip", tooltip,
		                       "stock_id", entries[i].stock_id,
		                       NULL);
		gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
		                              entries[i].is_active);
		if (entries[i].callback)
			gperl_signal_connect (WRAPINSTANCE (action),
			                      "activate",
					      (SV*)(entries[i].callback),
					      user_data, 0);

		/* set the accel path for the menu item */
		accel_path = g_strconcat
				("<Actions>/",
				 gtk_action_group_get_name (action_group),
				 "/", entries[i].name, NULL);

		if (entries[i].accelerator) {
			guint accel_key = 0;
			GdkModifierType accel_mods;

			gtk_accelerator_parse (entries[i].accelerator,
			                       &accel_key, &accel_mods);
			if (accel_key)
				gtk_accel_map_add_entry (accel_path,
				                         accel_key,
				                         accel_mods);
		}

		gtk_action_set_accel_path (action, accel_path);
		g_free (accel_path);

		gtk_action_group_add_action (action_group, action);
		g_object_unref (action);
	}

=for apidoc
Create and add a set of C<Gtk2::RadioAction> actions to
C<$action_group>.  For example

    $action_group->add_radio_actions
      ([ [ "Red",   undef, "_Red",   "<Control>R", "Blood", 1 ],
         [ "Green", undef, "_Green", "<Control>G", "Grass", 2 ],
         [ "Blue",  undef, "_Blue",  "<Control>B", "Sky",   3 ],
       ],
       2,    # initial, or -1 for no initial
       sub {
         my ($first_action, $selected_action, $userdata) = @_;
         print "now: ", $selected_action->get_name, "\n";
       },
       $userdata);

C<radio_action_entries> is an arrayref, each element of which is either a
ref to a 6-element array

    [ $name,          # string
      $stock_id,      # string, or undef
      $label,         # string, or undef to use stock label
      $accelerator,   # string key name, or undef for no accel
      $tooltip,       # string, or undef for no tooltip
      $value          # integer, for $action->set_current_value etc
    ]

or a ref to a hash of named fields similarly.  A C<name> is mandatory, the
rest are optional.  C<value> defaults to 0 if absent or C<undef>.

    { name        => $name,
      stock_id    => $stock_id,
      label       => $label,
      accelerator => $accelerator,
      tooltip     => $tooltip,
      value       => $value }

If C<$on_change> is not C<undef> then it's a signal handler function
which is connected to the C<changed> signal on the first action
created.  See L<Gtk2::RadioAction> for that signal.
=cut
##void gtk_action_group_add_radio_actions (GtkActionGroup *action_group, GtkRadioActionEntry *entries, guint n_entries, gint value, GCallback on_change, gpointer user_data);
##void gtk_action_group_add_radio_actions_full (GtkActionGroup *action_group, GtkRadioActionEntry *entries, guint n_entries, gint value, GCallback on_change, gpointer user_data, GDestroyNotify destroy);
void
gtk_action_group_add_radio_actions (action_group, radio_action_entries, value, on_change, user_data=NULL)
	GtkActionGroup * action_group
	SV * radio_action_entries
	gint value
	SV * on_change
	SV * user_data
    PREINIT:
	AV * av;
	GtkRadioActionEntry * entries;
	GtkAction * first_action = NULL;
	GSList * group = NULL;
	gint n_actions, i;
    CODE:
	if (!gperl_sv_is_array_ref (radio_action_entries))
		croak ("radio_action_entries must be a reference to an array of action entries");
	av = (AV*) SvRV (radio_action_entries);
	n_actions = av_len (av) + 1;
	if (n_actions < 1)
		croak ("radio action array is empty");
	entries = gperl_alloc_temp (sizeof (GtkRadioActionEntry) * n_actions);
	for (i = 0 ; i < n_actions ; i++) {
		SV ** svp = av_fetch (av, i, 0);
		read_radio_action_entry_from_sv (*svp, entries+i);
	}

	for (i = 0 ; i < n_actions ; i++) {
		GtkAction * action;
		gchar * accel_path;
		const gchar * label;
		const gchar * tooltip;
#if GTK_CHECK_VERSION (2, 6, 0)
		label = gtk_action_group_translate_string (action_group,
							   entries[i].label);
		tooltip =
			gtk_action_group_translate_string (action_group,
							   entries[i].tooltip);
#else
		label = entries[i].label;
		tooltip = entries[i].tooltip;
#endif

		action = g_object_new (GTK_TYPE_RADIO_ACTION,
		                       "name", entries[i].name,
		                       "label", label,
		                       "tooltip", tooltip,
		                       "stock_id", entries[i].stock_id,
		                       "value", entries[i].value,
		                       NULL);

		if (i == 0)
			first_action = action;
		gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
		group = gtk_radio_action_get_group (GTK_RADIO_ACTION (action));
		if (value == entries[i].value)
			gtk_toggle_action_set_active
					(GTK_TOGGLE_ACTION (action), TRUE);

		/* set the accel path for the menu item */
		accel_path = g_strconcat
				("<Actions>/",
				 gtk_action_group_get_name (action_group),
				 "/", entries[i].name, NULL);

		if (entries[i].accelerator) {
			guint accel_key = 0;
			GdkModifierType accel_mods;

			gtk_accelerator_parse (entries[i].accelerator,
			                       &accel_key, &accel_mods);
			if (accel_key)
				gtk_accel_map_add_entry (accel_path,
				                         accel_key,
				                         accel_mods);
		}

		gtk_action_set_accel_path (action, accel_path);
		g_free (accel_path);

		gtk_action_group_add_action (action_group, action);
		g_object_unref (action);
	}

	if (gperl_sv_is_defined (on_change))
		gperl_signal_connect (WRAPINSTANCE (first_action),
		                      "changed", on_change, user_data, 0);

void gtk_action_group_set_translation_domain (GtkActionGroup *action_group, const gchar *domain);

## NOTE: we had to implement the group adding API in xs so that we can
##       properly destroy the user data and callbacks and such.  since we
##       reimplement, we can't get to the translation function, its data,
##       or the translation domain, which are held in the opaque private
##       data object of the action group.  not the end of the world, but
##       not great, either.  see #135740

##       as of gtk+ 2.6.0, there is new API that allows one to call the
##       translate func, so we can enable the whole translation API.

#if GTK_CHECK_VERSION (2, 6, 0)

##void gtk_action_group_set_translate_func (GtkActionGroup *action_group, GtkTranslateFunc func, gpointer data, GtkDestroyNotify notify);
void
gtk_action_group_set_translate_func (action_group, func, data=NULL)
	GtkActionGroup *action_group
	SV *func
	SV *data
    PREINIT:
	GPerlCallback *callback;
    CODE:
	callback = gtk2perl_translate_func_create (func, data);
	gtk_action_group_set_translate_func (action_group,
	                                     gtk2perl_translate_func,
	                                     callback,
	                                     (GtkDestroyNotify)
	                                       gperl_callback_destroy);

const gchar * gtk_action_group_translate_string (GtkActionGroup *action_group, const gchar *string);

#endif