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
|
/*
* Copyright (c) 2003-2005 by the gtk2-perl team (see the file AUTHORS)
*
* Licensed under the LGPL, see LICENSE file for more information.
*
* $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/xs/GtkActionGroup.xs,v 1.10 2005/07/10 12:22:20 kaffeetisch Exp $
*/
#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)) \
&& SvOK (*svp)) \
? SvPV_nolen (*svp) \
: NULL)
#define HFETCHCV(hv, key) \
(((svp = hv_fetch ((hv), (key), strlen ((key)), FALSE)) \
&& SvOK (*svp)) \
? (gpointer)(*svp) \
: NULL)
#define HFETCHIV(hv, key) \
(((svp = hv_fetch ((hv), (key), strlen ((key)), FALSE)) \
&& SvOK (*svp)) \
? SvIV (*svp) \
: 0)
#define AFETCHPV(av, index) \
(((svp = av_fetch ((av), (index), FALSE)) && SvOK (*svp)) \
? SvPV_nolen (*svp) \
: NULL)
#define AFETCHCV(av, index) \
(((svp = av_fetch ((av), (index), FALSE)) && SvOK (*svp)) \
? (gpointer)(*svp) \
: NULL)
#define AFETCHIV(av, index) \
(((svp = av_fetch ((av), (index), FALSE)) && SvOK (*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 (!sv || !SvOK (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 (!sv || !SvOK (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 (!sv || !SvOK (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 (!action_entries || !SvOK (action_entries) || !SvROK (action_entries)
|| SvTYPE (SvRV (action_entries)) != SVt_PVAV)
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 (!toggle_action_entries ||
!SvOK (toggle_action_entries) || !SvROK (toggle_action_entries) ||
SvTYPE (SvRV (toggle_action_entries)) != SVt_PVAV)
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);
}
##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 (!radio_action_entries ||
!SvOK (radio_action_entries) || !SvROK (radio_action_entries) ||
SvTYPE (SvRV (radio_action_entries)) != SVt_PVAV)
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 (on_change && SvOK (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
|