File: GtkItemFactory.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 (421 lines) | stat: -rw-r--r-- 12,613 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
/*
 * Copyright (c) 2003 by the gtk2-perl team (see the file AUTHORS)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 *
 * $Id$
 */
#include "gtk2perl.h"

/* ------------------------------------------------------------------------- */

/*
 * a custom marshaler for the item factory callbacks
 */
static void
gtk2perl_item_factory_item_activate (gpointer    data,
				     guint       callback_action,
				     GtkWidget * widget)
{
	SV    * callback_sv;
	SV    * callback_data;

	dSP;

	/* the callback out of the widget */
	callback_sv = (SV *) g_object_get_data (
				G_OBJECT (widget),
				"_gtk2perl_item_factory_callback_sv");

	callback_data = (SV *) data;

	ENTER;
	SAVETMPS;

	PUSHMARK (SP);
	/* put the parameters on the stack (we're always type 1) */
	EXTEND (SP, 3);
	PUSHs (sv_2mortal (newSVsv (callback_data
	                            ? callback_data
	       	                    : &PL_sv_undef)));
	PUSHs (sv_2mortal (newSViv (callback_action)));
	PUSHs (sv_2mortal (newSVGtkWidget (widget)));
	PUTBACK;

	/* call the code in sv */
	call_sv (callback_sv, G_DISCARD);

	FREETMPS;
	LEAVE;
}

/* ------------------------------------------------------------------------- */

GPerlCallback *
gtk2perl_translate_func_create (SV * func, SV * data)
{
	GType param_types[1];
	param_types[0] = G_TYPE_STRING;
	return gperl_callback_new (func, data, G_N_ELEMENTS (param_types),
				   param_types, G_TYPE_STRING);
}

gchar *
gtk2perl_translate_func (const gchar *path,
			 gpointer data)
{
	GPerlCallback * callback = (GPerlCallback*)data;
	GValue value = {0,};
	SV * tempsv = NULL;
	gchar * retval;

	g_value_init (&value, callback->return_type);
	gperl_callback_invoke (callback, &value, path);
	retval = (gchar *) g_value_get_string (&value);
	/* g_value_unset() will free the string to which retval points.
	 * we will need to keep a copy; let's use a mortal scalar to keep
	 * it around long enough to be useful to the C callers. */
	if (retval)
		tempsv = sv_2mortal (newSVGChar (retval));
	g_value_unset (&value);

	/* using SvPV rather than SvGChar because we used newSVGChar to create
	 * it above, so we're assured that it's been upgraded to utf8 already.
	 * this avoids uncertainty about whether SvGChar may vivify the mortal
	 * value.  (i'm paranoid.) */
	return tempsv ? SvPV_nolen (tempsv) : NULL;
}

/* ------------------------------------------------------------------------- */

#define HV_FETCH_AND_CHECK(_member, _sv) \
	if (hv_exists (hv, #_member, strlen (#_member))) { \
		value = hv_fetch (hv, #_member, strlen (#_member), FALSE); \
		if (value && gperl_sv_is_defined (*value)) \
			entry->_member = _sv; \
	}

#define AV_FETCH_AND_CHECK(_index, _member, _sv) \
	value = av_fetch (av, _index, 0); \
	if (value && gperl_sv_is_defined (*value)) \
		entry->_member = _sv;

GtkItemFactoryEntry *
SvGtkItemFactoryEntry (SV *data, SV **callback)
{
	GtkItemFactoryEntry *entry = gperl_alloc_temp (sizeof (GtkItemFactoryEntry));
	memset (entry, 0, sizeof (GtkItemFactoryEntry));

	if (!gperl_sv_is_defined (data))
		return entry; /* fail silently if undef */

	if (gperl_sv_is_hash_ref (data)) {
		HV *hv = (HV *) SvRV (data);
		SV **value;

		HV_FETCH_AND_CHECK (path, SvGChar (*value));
		HV_FETCH_AND_CHECK (accelerator, SvGChar (*value));

		if (hv_exists (hv, "callback", 8)) {
			value = hv_fetch (hv, "callback", 8, FALSE);

			if (callback && value && gperl_sv_is_defined (*value)) {
				*callback = *value;
				entry->callback = gtk2perl_item_factory_item_activate;
			}
		}

		HV_FETCH_AND_CHECK (callback_action, SvIV (*value));
		HV_FETCH_AND_CHECK (item_type, SvGChar (*value));
		HV_FETCH_AND_CHECK (extra_data, SvPOK (*value) ? SvGChar (*value) : NULL);
	} else if (gperl_sv_is_array_ref (data)) {
		AV *av = (AV *) SvRV (data);
		SV **value;

		AV_FETCH_AND_CHECK (0, path, SvGChar (*value));
		AV_FETCH_AND_CHECK (1, accelerator, SvGChar (*value));

		value = av_fetch (av, 2, 0);

		if (callback && value && gperl_sv_is_defined (*value)) {
			*callback = *value;
			entry->callback = gtk2perl_item_factory_item_activate;
		}

		AV_FETCH_AND_CHECK (3, callback_action, SvIV (*value));
		AV_FETCH_AND_CHECK (4, item_type, SvGChar (*value));
		AV_FETCH_AND_CHECK (5, extra_data, SvPOK (*value) ? SvGChar (*value) : NULL);
	} else {
		croak ("badly formed GtkItemFactoryEntry; use either list or hash form:\n"
		       "    list form:\n"
		       "        [ path, accel, callback, action, type ]\n"
		       "    hash form:\n"
		       "        {\n"
		       "           path            => $path,\n"
		       "           accelerator     => $accel,   # optional\n"
		       "           callback        => $callback,\n"
		       "           callback_action => $action,\n"
		       "           item_type       => $type,    # optional\n"
		       "           extra_data      => $extra,   # optional\n"
		       "         }\n"
		       "  ");
	}

	return entry;
}

static void
gtk2perl_item_factory_create_item_helper (GtkItemFactory *ifactory,
                                          SV *entry_ref,
                                          SV *callback_data)
{
	GtkItemFactoryEntry *entry;
	gchar *clean_path;
	GtkWidget *widget = NULL;

	SV *callback_sv = NULL, *tmp_defsv;
	SV * real_data = callback_data ? gperl_sv_copy (callback_data) : NULL;

	entry = SvGtkItemFactoryEntry (entry_ref, &callback_sv);

	/* remove all those underscores that gtk+ turns into accelerators to
	 * get a clean path that can later be used for item retrieval */
	tmp_defsv = newSVsv (DEFSV);

	sv_setsv (DEFSV, sv_2mortal (newSVGChar (entry->path)));
	eval_pv ("s/_(?!_+)//g; s/_+/_/g;", 1);
	clean_path = SvGChar (sv_2mortal (newSVsv (DEFSV)));

	sv_setsv (DEFSV, tmp_defsv);

	/* create the item in the normal manner now */
	gtk_item_factory_create_item (ifactory, entry, real_data, 1);
	
	/* get the widget that was created by create_item (this is why
	 * we needed clean_path) */
	widget = gtk_item_factory_get_item (ifactory, clean_path);
	if (widget) {
		/* put the sv we need to call into the widget */
		g_object_set_data_full (G_OBJECT (widget),
		                        "_gtk2perl_item_factory_callback_sv",
		                        gperl_sv_copy (callback_sv),
		                        (GtkDestroyNotify) gperl_sv_free);
		if (real_data)
			g_object_set_data_full (G_OBJECT (widget),
			                        "_gtk2perl_item_factory_callback_data",
			                        real_data,
			                        (GtkDestroyNotify) gperl_sv_free);
	} else {
		if (real_data)
			gperl_sv_free (real_data);
		croak("ItemFactory couldn't retrieve widget it just created");
	}
}

/* ------------------------------------------------------------------------- */

MODULE = Gtk2::ItemFactory	PACKAGE = Gtk2::ItemFactory	PREFIX = gtk_item_factory_

=for deprecated_by Gtk2::UIManager
=cut

##  GtkItemFactory* gtk_item_factory_new (GType container_type, const gchar *path, GtkAccelGroup *accel_group) 
GtkItemFactory*
gtk_item_factory_new (class, container_type_package, path, accel_group=NULL)
	char * container_type_package
	const gchar *path
	GtkAccelGroup_ornull *accel_group
    PREINIT:
	GType container_type;
    CODE:
	container_type = gperl_type_from_package (container_type_package);
	RETVAL = gtk_item_factory_new (container_type, path, accel_group);
    OUTPUT:
	RETVAL

### deprecated
##  void gtk_item_factory_add_foreign (GtkWidget *accel_widget, const gchar *full_path, GtkAccelGroup *accel_group, guint keyval, GdkModifierType modifiers) 

GtkItemFactory_ornull*
gtk_item_factory_from_widget (class, widget)
	GtkWidget *widget
    C_ARGS:
	widget

const gchar*
gtk_item_factory_path_from_widget (class, widget)
	GtkWidget *widget
    C_ARGS:
	widget

GtkWidget_ornull*
gtk_item_factory_get_item (ifactory, path)
	GtkItemFactory *ifactory
	const gchar *path

GtkWidget_ornull*
gtk_item_factory_get_widget (ifactory, path)
	GtkItemFactory *ifactory
	const gchar *path

GtkWidget_ornull*
gtk_item_factory_get_widget_by_action (ifactory, action)
	GtkItemFactory *ifactory
	guint action

GtkWidget_ornull*
gtk_item_factory_get_item_by_action (ifactory, action)
	GtkItemFactory *ifactory
	guint action

=for apidoc

=for arg entry_ref GtkItemFactoryEntry

=cut
void
gtk_item_factory_create_item (ifactory, entry_ref, callback_data=NULL)
	GtkItemFactory *ifactory
	SV *entry_ref
	SV *callback_data
    CODE:
	gtk2perl_item_factory_create_item_helper (ifactory, entry_ref, callback_data);

=for apidoc

=for arg ... GtkItemFactoryEntry's

=cut
void
gtk_item_factory_create_items (ifactory, callback_data, ...)
	GtkItemFactory *ifactory
	SV *callback_data
    PREINIT:
	int i;
    CODE:
	for (i = 2; i < items; i++)
		gtk2perl_item_factory_create_item_helper (ifactory, ST (i), callback_data);

void
gtk_item_factory_delete_item (ifactory, path)
	GtkItemFactory *ifactory
	const gchar *path

=for apidoc

=for arg entry_ref GtkItemFactoryEntry

=cut
void
gtk_item_factory_delete_entry (ifactory, entry_ref)
	GtkItemFactory *ifactory
	SV *entry_ref
    PREINIT:
	GtkItemFactoryEntry *entry;
    CODE:
	entry = SvGtkItemFactoryEntry (entry_ref, NULL);
	gtk_item_factory_delete_entry (ifactory, entry);

=for apidoc

=for arg ... GtkItemFactoryEntry's

=cut
void gtk_item_factory_delete_entries (ifactory, ...)
	GtkItemFactory *ifactory
    PREINIT:
	int i;
	GtkItemFactoryEntry *entry;
    CODE:
	for (i = 1; i < items; i++) {
		entry = SvGtkItemFactoryEntry (ST (i), NULL);
		gtk_item_factory_delete_entry (ifactory, entry);
	}

##  void gtk_item_factory_popup (GtkItemFactory *ifactory, guint x, guint y, guint mouse_button, guint32 time_) 
##  void gtk_item_factory_popup_with_data(GtkItemFactory *ifactory, gpointer popup_data, GtkDestroyNotify destroy, guint x, guint y, guint mouse_button, guint32 time_) 

### combination of gtk_item_factory_popup and gtk_item_factory_popup_with_data
void
gtk_item_factory_popup (ifactory, x, y, mouse_button, time_, popup_data=NULL)
	GtkItemFactory *ifactory
	guint x
	guint y
	guint mouse_button
	guint32 time_
	SV * popup_data
    PREINIT:
	SV * real_popup_data = NULL;
    CODE:
	if (gperl_sv_is_defined (popup_data))
		real_popup_data = gperl_sv_copy (popup_data);
	gtk_item_factory_popup_with_data (ifactory,
	                                  real_popup_data, 
	                                  real_popup_data
	                                   ? (GDestroyNotify)gperl_sv_free
	                                   : NULL, 
	                                  x, y, mouse_button, time_);

SV *
gtk_item_factory_popup_data (ifactory)
	GtkItemFactory *ifactory
    CODE:
	RETVAL = (SV *) gtk_item_factory_popup_data (ifactory);

	if (RETVAL) {
		RETVAL = gperl_sv_copy (RETVAL);
	} else {
		RETVAL = &PL_sv_undef;
	}
    OUTPUT:
	RETVAL

SV *
gtk_item_factory_popup_data_from_widget (class, widget)
	GtkWidget *widget
    CODE:
	RETVAL = (SV *) gtk_item_factory_popup_data_from_widget (widget);

	if (RETVAL) {
		RETVAL = gperl_sv_copy (RETVAL);
	} else {
		RETVAL = &PL_sv_undef;
	}
    OUTPUT:
	RETVAL

void
gtk_item_factory_set_translate_func (ifactory, func, data=NULL)
	GtkItemFactory *ifactory
	SV *func
	SV *data
    PREINIT:
	GPerlCallback *callback;
    CODE:
	callback = gtk2perl_translate_func_create (func, data);
	gtk_item_factory_set_translate_func (ifactory,
	                                     gtk2perl_translate_func,
	                                     callback,
	                                     (GtkDestroyNotify)
	                                       gperl_callback_destroy);

##
## deprecated
##
##  void gtk_item_factory_create_items_ac (GtkItemFactory *ifactory, guint n_entries, GtkItemFactoryEntry *entries, gpointer callback_data, guint callback_type) 
##  GtkItemFactory* gtk_item_factory_from_path (const gchar *path) 
##  void gtk_item_factory_create_menu_entries (guint n_entries, GtkMenuEntry *entries) 
##  void gtk_item_factories_path_delete (const gchar *ifactory_path, const gchar *path)