File: bonobo-selector.c

package info (click to toggle)
libbonoboui 2.24.5-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,808 kB
  • ctags: 3,596
  • sloc: ansic: 30,221; sh: 10,256; xml: 599; makefile: 475
file content (375 lines) | stat: -rw-r--r-- 10,898 bytes parent folder | download | duplicates (4)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * bonobo-selector.c: Bonobo Component Selector
 *
 * Authors:
 *   Richard Hestilow (hestgray@ionet.net)
 *   Miguel de Icaza  (miguel@kernel.org)
 *   Martin Baulig    (martin@home-of-linux.org)
 *   Anders Carlsson  (andersca@gnu.org)
 *   Havoc Pennington (hp@redhat.com)
 *   Dietmar Maurer   (dietmar@ximian.com)
 *   Michael Meeks    (michael@ximian.com)
 *
 * Copyright 1999, 2000 Richard Hestilow, Ximian, Inc,
 *                      Martin Baulig, Anders Carlsson,
 *                      Havoc Pennigton, Dietmar Maurer
 */
#include <config.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <bonobo/bonobo-selector.h>

G_DEFINE_TYPE (BonoboSelector, bonobo_selector, GTK_TYPE_DIALOG)

#define DEFAULT_INTERFACE "IDL:Bonobo/Control:1.0"
#define BONOBO_PAD_SMALL 4

struct _BonoboSelectorPrivate {
	BonoboSelectorWidget *selector;
};

enum {
	OK,
	CANCEL,
	LAST_SIGNAL
};

enum {
	PROP_0,
	PROP_INTERFACES
};

static guint bonobo_selector_signals [LAST_SIGNAL] = { 0, 0 };

static void       
bonobo_selector_finalize (GObject *object)
{
	g_return_if_fail (BONOBO_IS_SELECTOR (object));

	g_free (BONOBO_SELECTOR (object)->priv);

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

/**
 * bonobo_selector_get_selected_id:
 * @sel: A BonoboSelector widget.
 *
 * Returns: A newly-allocated string containing the ID of the
 * currently-selected CORBA server (i.e., the corba server whose name
 * is highlighted in the list).  The user of this function is
 * responsible for freeing this. It will give an oaf iid back.
 */
gchar *
bonobo_selector_get_selected_id (BonoboSelector *sel)
{
	g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL);

	return bonobo_selector_widget_get_id (sel->priv->selector);
}

/**
 * bonobo_selector_get_selected_name:
 * @sel: A BonoboSelector widget.
 *
 * Returns: A newly-allocated string containing the name of the
 * currently-selected CORBA server (i.e., the corba server whose name
 * is highlighted in the list).  The user of this function is
 * responsible for freeing this.
 */
gchar *
bonobo_selector_get_selected_name (BonoboSelector *sel)
{
	g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL);

	return bonobo_selector_widget_get_name (sel->priv->selector);
}

/**
 * bonobo_selector_get_selected_description:
 * @sel: A BonoboSelector widget.
 *
 * Returns: A newly-allocated string containing the description of the
 * currently-selected CORBA server (i.e., the corba server whose name
 * is highlighted in the list).  The user of this function is
 * responsible for freeing this.
 */
gchar *
bonobo_selector_get_selected_description (BonoboSelector *sel)
{
	g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL);

	return bonobo_selector_widget_get_description (sel->priv->selector);
}

static void
ok_callback (GtkWidget *widget, gpointer data)
{
	char *text = bonobo_selector_get_selected_id (
		BONOBO_SELECTOR (widget));

	g_object_set_data (G_OBJECT (widget), "UserData", text);
}

/**
 * bonobo_selector_select_id:
 * @title: The title to be used for the dialog.
 * @interfaces_required: A list of required interfaces.  See
 * bonobo_selector_new().
 *
 * Calls bonobo_selector_new() to create a new
 * BonoboSelector widget with the specified paramters, @title and
 * @interfaces_required.  Then runs the dialog modally and allows
 * the user to make a selection.
 *
 * Returns: The Oaf IID of the selected server, or NULL if no server is
 * selected.  The ID string has been allocated with g_strdup.
 */
gchar *
bonobo_selector_select_id (const gchar  *title,
			   const gchar **interfaces_required)
{
	GtkWidget *sel = bonobo_selector_new (title, interfaces_required);
	gchar     *name = NULL;
	int        n;

	g_return_val_if_fail (sel != NULL, NULL);

	g_signal_connect (sel, "ok",
			  G_CALLBACK (ok_callback), NULL);

	g_object_set_data (G_OBJECT (sel), "UserData", NULL);
	
	gtk_widget_show (sel);
		
	n = gtk_dialog_run (GTK_DIALOG (sel));

	switch (n) {
	case GTK_RESPONSE_CANCEL:
		name = NULL;
		break;
	case GTK_RESPONSE_APPLY:
	case GTK_RESPONSE_OK:
		name = g_object_get_data (G_OBJECT (sel), "UserData");
		break;
	default:
		break;
	}
		
	gtk_widget_destroy (sel);

	return name;
}

static void
response_callback (GtkWidget *widget,
		   gint       response_id,
		   gpointer   data) 
{
	switch (response_id) {
		case GTK_RESPONSE_OK:
			g_signal_emit (data, bonobo_selector_signals [OK], 0);
			break;
		case GTK_RESPONSE_CANCEL:
			g_signal_emit (data, bonobo_selector_signals [CANCEL], 0);
		default:
			break;
	}
}

static void
final_select_cb (GtkWidget *widget, BonoboSelector *sel)
{
	gtk_dialog_response (GTK_DIALOG (sel), GTK_RESPONSE_OK);
}

/**
 * bonobo_selector_construct:
 * @sel: the selector to construct
 * @title: the title for the window
 * @selector: the component view widget to put inside it.
 *
 * Don't use this ever - use construct-time properties instead.
 * TODO: Remove from header when we are allowed to change the API.
 * Constructs the innards of a bonobo selector window.
 *
 * Return value: the constructed widget.
 **/
GtkWidget *
bonobo_selector_construct       (BonoboSelector       *sel,
					    const gchar          *title,
					    BonoboSelectorWidget *selector)
{	
 	g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL);
 	g_return_val_if_fail (BONOBO_IS_SELECTOR_WIDGET (selector), NULL);
 
	sel->priv->selector = selector;

 	g_signal_connect (selector, "final_select",
 			  G_CALLBACK (final_select_cb), sel);
	
	gtk_window_set_title (GTK_WINDOW (sel), title ? title : "");
 
 	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (sel)->vbox),
 			    GTK_WIDGET (selector),
			    TRUE, TRUE, BONOBO_PAD_SMALL);

	gtk_dialog_add_button (GTK_DIALOG (sel), GTK_STOCK_OK,
			       GTK_RESPONSE_OK);
	gtk_dialog_add_button (GTK_DIALOG (sel), GTK_STOCK_CANCEL,
			       GTK_RESPONSE_CANCEL);
	gtk_dialog_set_default_response (GTK_DIALOG (sel), GTK_RESPONSE_OK);

	g_signal_connect (sel, "response",
			  G_CALLBACK (response_callback), sel);

	gtk_window_set_default_size (GTK_WINDOW (sel), 400, 300);
	gtk_widget_show_all (GTK_DIALOG (sel)->vbox);
	return GTK_WIDGET (sel);
}

static void
bonobo_selector_internal_construct (BonoboSelector       *sel)
{
	BonoboSelectorWidget* selector = sel->priv->selector;

	g_signal_connect (selector, "final_select",
			  G_CALLBACK (final_select_cb), sel);

	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (sel)->vbox),
			    GTK_WIDGET (selector),
			    TRUE, TRUE, BONOBO_PAD_SMALL);
	
	gtk_dialog_add_button (GTK_DIALOG (sel), GTK_STOCK_OK,
			       GTK_RESPONSE_OK);
	gtk_dialog_add_button (GTK_DIALOG (sel), GTK_STOCK_CANCEL,
			       GTK_RESPONSE_CANCEL);
	gtk_dialog_set_default_response (GTK_DIALOG (sel), GTK_RESPONSE_OK);
	
	g_signal_connect (sel, "response",
			  G_CALLBACK (response_callback), sel);
	
	gtk_window_set_default_size (GTK_WINDOW (sel), 400, 300);
	gtk_widget_show_all  (GTK_DIALOG (sel)->vbox);
}

static void
bonobo_selector_init (BonoboSelector *sel)
{
	BonoboSelectorWidget *selectorwidget = NULL;
	
	sel->priv = g_new0 (BonoboSelectorPrivate, 1);
	
	selectorwidget = BONOBO_SELECTOR_WIDGET (bonobo_selector_widget_new ());
	sel->priv->selector = selectorwidget;
	
	bonobo_selector_internal_construct (sel);
}

static void
bonobo_selector_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	BonoboSelector *selector = BONOBO_SELECTOR (object);

	switch (prop_id) {
	case PROP_INTERFACES:
	{
		const gchar *query [2] = { DEFAULT_INTERFACE, NULL }; /* the default interfaces_required. */
		BonoboSelectorWidget *selectorwidget = NULL;
		
		/* Get the supplied array or interfaces, replacing it with a default if none have been provided: */
		const gchar **interfaces_required = NULL;
		if (!interfaces_required)
			interfaces_required = query;
		
		/* Set the interfaces_required in the child SelectorWidget: */
		selectorwidget = selector->priv->selector;;
		if (selectorwidget)
			bonobo_selector_widget_set_interfaces (selectorwidget, interfaces_required);
			
		break;
	}
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
bonobo_selector_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	switch (prop_id) {
	/* PROP_INTERFACES is read-only, because there is a virtual BonoboSelectorWidget::set_interfaces(), but no get_interfaces(). */
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
bonobo_selector_class_init (BonoboSelectorClass *klass)
{
	GObjectClass *object_class;
	
	object_class = (GObjectClass *) klass;
	object_class->finalize = bonobo_selector_finalize;
	
	object_class->set_property = bonobo_selector_set_property;
	object_class->get_property = bonobo_selector_get_property;

	bonobo_selector_signals [OK] =
		g_signal_new ("ok",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (BonoboSelectorClass, ok),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
	
	bonobo_selector_signals [CANCEL] =
		g_signal_new ("cancel",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (BonoboSelectorClass, cancel),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
		
	/* properties: */	
   	g_object_class_install_property (object_class,
				 PROP_INTERFACES,
				 g_param_spec_value_array ("interfaces_required",
							   _("Interfaces required"),
							   _("A NULL-terminated array of interfaces which a server must support in order to be listed in the selector. Defaults to \"IDL:Bonobo/Embeddable:1.0\" if no interfaces are listed"),
							   g_param_spec_string ("interface-required-entry",
										_("Interface required entry"),
										_("One of the interfaces that's required"),
										NULL,
										G_PARAM_READWRITE),
							   G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

/**
 * bonobo_selector_new:
 * @title: A string which should go in the title of the
 * BonoboSelector window.
 * @interfaces_required: A NULL-terminated array of interfaces which a
 * server must support in order to be listed in the selector.  Defaults
 * to "IDL:Bonobo/Embeddable:1.0" if no interfaces are listed.
 *
 * Creates a new BonoboSelector widget.  The title of the dialog
 * is set to @title, and the list of selectable servers is populated
 * with those servers which support the interfaces specified in
 * @interfaces_required.
 *
 * Returns: A pointer to the newly-created BonoboSelector widget.
 */
GtkWidget *
bonobo_selector_new (const gchar *title,
		     const gchar **interfaces_required)
{
	BonoboSelector *sel =  g_object_new (bonobo_selector_get_type (), "title", title, "interfaces_required", interfaces_required, NULL);

	return GTK_WIDGET (sel);
}