1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * bonobo-selector.c: Bonobo Component Selector |
---|
4 | * |
---|
5 | * Authors: |
---|
6 | * Richard Hestilow (hestgray@ionet.net) |
---|
7 | * Miguel de Icaza (miguel@kernel.org) |
---|
8 | * Martin Baulig (martin@ |
---|
9 | * Anders Carlsson (andersca@gnu.org) |
---|
10 | * Havoc Pennington (hp@redhat.com) |
---|
11 | * Dietmar Maurer (dietmar@maurer-it.com) |
---|
12 | * Michael Meeks (michael@helixcode.com) |
---|
13 | * |
---|
14 | * Copyright 1999, 2000 Richard Hestilow, Helix Code, Inc, |
---|
15 | * Martin Baulig, Anders Carlsson, |
---|
16 | * Havoc Pennigton, Dietmar Maurer |
---|
17 | */ |
---|
18 | #include <config.h> |
---|
19 | #include <string.h> /* strcmp */ |
---|
20 | #include <libgnomeui/gnome-uidefs.h> |
---|
21 | #include <libgnomeui/gnome-stock.h> |
---|
22 | #include <libgnomeui/gnome-dialog.h> |
---|
23 | #include <bonobo/bonobo-object-directory.h> |
---|
24 | #include <bonobo/bonobo-selector.h> |
---|
25 | |
---|
26 | #define DEFAULT_INTERFACE "IDL:Bonobo/Embeddable:1.0" |
---|
27 | |
---|
28 | static GtkDialogClass *parent_class; |
---|
29 | |
---|
30 | struct _BonoboSelectorPrivate { |
---|
31 | BonoboSelectorWidget *selector; |
---|
32 | }; |
---|
33 | |
---|
34 | enum { |
---|
35 | OK, |
---|
36 | CANCEL, |
---|
37 | LAST_SIGNAL |
---|
38 | }; |
---|
39 | |
---|
40 | guint bonobo_selector_signals [LAST_SIGNAL] = { 0, 0 }; |
---|
41 | |
---|
42 | /* FIXME: remove this as soon it is included in gnome-dialog */ |
---|
43 | static void |
---|
44 | gnome_dialog_clicked (GnomeDialog *dialog, gint button_num) |
---|
45 | { |
---|
46 | gtk_signal_emit_by_name (GTK_OBJECT (dialog), "clicked", |
---|
47 | button_num); |
---|
48 | } |
---|
49 | |
---|
50 | static void |
---|
51 | bonobo_selector_finalize (GtkObject *object) |
---|
52 | { |
---|
53 | g_return_if_fail (BONOBO_IS_SELECTOR (object)); |
---|
54 | |
---|
55 | g_free (BONOBO_SELECTOR (object)->priv); |
---|
56 | |
---|
57 | if (GTK_OBJECT_CLASS (parent_class)->finalize) |
---|
58 | GTK_OBJECT_CLASS (parent_class)->finalize (object); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * bonobo_selector_get_selected_id: |
---|
63 | * @sel: A BonoboSelector widget. |
---|
64 | * |
---|
65 | * Returns: A newly-allocated string containing the ID of the |
---|
66 | * currently-selected CORBA server (i.e., the corba server whose name |
---|
67 | * is highlighted in the list). The user of this function is |
---|
68 | * responsible for freeing this. It will give an oaf iid back. |
---|
69 | */ |
---|
70 | gchar * |
---|
71 | bonobo_selector_get_selected_id (BonoboSelector *sel) |
---|
72 | { |
---|
73 | g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL); |
---|
74 | |
---|
75 | return bonobo_selector_widget_get_id (sel->priv->selector); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * bonobo_selector_get_selected_name: |
---|
80 | * @sel: A BonoboSelector widget. |
---|
81 | * |
---|
82 | * Returns: A newly-allocated string containing the name of the |
---|
83 | * currently-selected CORBA server (i.e., the corba server whose name |
---|
84 | * is highlighted in the list). The user of this function is |
---|
85 | * responsible for freeing this. |
---|
86 | */ |
---|
87 | gchar * |
---|
88 | bonobo_selector_get_selected_name (BonoboSelector *sel) |
---|
89 | { |
---|
90 | g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL); |
---|
91 | |
---|
92 | return bonobo_selector_widget_get_name (sel->priv->selector); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * bonobo_selector_get_selected_description: |
---|
97 | * @sel: A BonoboSelector widget. |
---|
98 | * |
---|
99 | * Returns: A newly-allocated string containing the description of the |
---|
100 | * currently-selected CORBA server (i.e., the corba server whose name |
---|
101 | * is highlighted in the list). The user of this function is |
---|
102 | * responsible for freeing this. |
---|
103 | */ |
---|
104 | gchar * |
---|
105 | bonobo_selector_get_selected_description (BonoboSelector *sel) |
---|
106 | { |
---|
107 | g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL); |
---|
108 | |
---|
109 | return bonobo_selector_widget_get_description (sel->priv->selector); |
---|
110 | } |
---|
111 | |
---|
112 | static void |
---|
113 | ok_callback (GtkWidget *widget, gpointer data) |
---|
114 | { |
---|
115 | char *text = bonobo_selector_get_selected_id ( |
---|
116 | BONOBO_SELECTOR (widget)); |
---|
117 | |
---|
118 | gtk_object_set_user_data (GTK_OBJECT (widget), text); |
---|
119 | gtk_main_quit (); |
---|
120 | } |
---|
121 | |
---|
122 | static void |
---|
123 | cancel_callback (GtkWidget *widget, gpointer data) |
---|
124 | { |
---|
125 | gtk_main_quit (); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * bonobo_selector_select_id: |
---|
130 | * @title: The title to be used for the dialog. |
---|
131 | * @interfaces_required: A list of required interfaces. See |
---|
132 | * bonobo_selector_new(). |
---|
133 | * |
---|
134 | * Calls bonobo_selector_new() to create a new |
---|
135 | * BonoboSelector widget with the specified paramters, @title and |
---|
136 | * @interfaces_required. Then runs the dialog modally and allows |
---|
137 | * the user to make a selection. |
---|
138 | * |
---|
139 | * Returns: The Oaf IID of the selected server, or NULL if no server is |
---|
140 | * selected. The ID string has been allocated with g_strdup. |
---|
141 | */ |
---|
142 | gchar * |
---|
143 | bonobo_selector_select_id (const gchar *title, |
---|
144 | const gchar **interfaces_required) |
---|
145 | { |
---|
146 | GtkWidget *sel = bonobo_selector_new (title, interfaces_required); |
---|
147 | gchar *name = NULL; |
---|
148 | int n; |
---|
149 | |
---|
150 | g_return_val_if_fail (sel != NULL, NULL); |
---|
151 | |
---|
152 | gtk_signal_connect (GTK_OBJECT (sel), "ok", |
---|
153 | GTK_SIGNAL_FUNC (ok_callback), NULL); |
---|
154 | |
---|
155 | gtk_signal_connect (GTK_OBJECT (sel), "cancel", |
---|
156 | GTK_SIGNAL_FUNC (cancel_callback), NULL); |
---|
157 | |
---|
158 | gtk_object_set_user_data (GTK_OBJECT (sel), NULL); |
---|
159 | |
---|
160 | gtk_widget_show (sel); |
---|
161 | |
---|
162 | n = gnome_dialog_run (GNOME_DIALOG(sel)); |
---|
163 | if (n == -1) |
---|
164 | return NULL; |
---|
165 | |
---|
166 | if (n == 0) |
---|
167 | name = gtk_object_get_user_data (GTK_OBJECT (sel)); |
---|
168 | |
---|
169 | gtk_widget_destroy (sel); |
---|
170 | |
---|
171 | return name; |
---|
172 | } |
---|
173 | |
---|
174 | static void |
---|
175 | button_callback (GtkWidget *widget, |
---|
176 | gint button_number, |
---|
177 | gpointer data) |
---|
178 | { |
---|
179 | switch (button_number) { |
---|
180 | case 0: |
---|
181 | gtk_signal_emit (GTK_OBJECT (data), |
---|
182 | bonobo_selector_signals [OK]); |
---|
183 | break; |
---|
184 | case 1: |
---|
185 | gtk_signal_emit (GTK_OBJECT (data), |
---|
186 | bonobo_selector_signals [CANCEL]); |
---|
187 | default: |
---|
188 | break; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | static void |
---|
193 | final_select_cb (GtkWidget *widget, BonoboSelector *sel) |
---|
194 | { |
---|
195 | gnome_dialog_clicked (GNOME_DIALOG (sel), 0); |
---|
196 | } |
---|
197 | |
---|
198 | static void |
---|
199 | bonobo_selector_init (GtkWidget *widget) |
---|
200 | { |
---|
201 | BonoboSelector *sel = BONOBO_SELECTOR (widget); |
---|
202 | |
---|
203 | g_return_if_fail (widget != NULL); |
---|
204 | |
---|
205 | sel->priv = g_new0 (BonoboSelectorPrivate, 1); |
---|
206 | } |
---|
207 | |
---|
208 | static void |
---|
209 | bonobo_selector_class_init (BonoboSelectorClass *klass) |
---|
210 | { |
---|
211 | GtkObjectClass *object_class; |
---|
212 | |
---|
213 | g_return_if_fail (klass != NULL); |
---|
214 | |
---|
215 | object_class = (GtkObjectClass *) klass; |
---|
216 | object_class->finalize = bonobo_selector_finalize; |
---|
217 | |
---|
218 | parent_class = gtk_type_class (gnome_dialog_get_type ()); |
---|
219 | |
---|
220 | bonobo_selector_signals [OK] = |
---|
221 | gtk_signal_new ("ok", GTK_RUN_LAST, object_class->type, |
---|
222 | GTK_SIGNAL_OFFSET (BonoboSelectorClass, ok), |
---|
223 | gtk_signal_default_marshaller, GTK_TYPE_NONE, 0); |
---|
224 | |
---|
225 | bonobo_selector_signals [CANCEL] = |
---|
226 | gtk_signal_new ("cancel", GTK_RUN_LAST, object_class->type, |
---|
227 | GTK_SIGNAL_OFFSET (BonoboSelectorClass, cancel), |
---|
228 | gtk_signal_default_marshaller, GTK_TYPE_NONE, 0); |
---|
229 | |
---|
230 | gtk_object_class_add_signals (object_class, bonobo_selector_signals, |
---|
231 | LAST_SIGNAL); |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * bonobo_selector_get_type: |
---|
236 | * |
---|
237 | * Returns: The GtkType for the BonoboSelector object class. |
---|
238 | */ |
---|
239 | GtkType |
---|
240 | bonobo_selector_get_type (void) |
---|
241 | { |
---|
242 | static guint bonobo_selector_type = 0; |
---|
243 | |
---|
244 | if (!bonobo_selector_type) { |
---|
245 | GtkTypeInfo bonobo_selector_info = { |
---|
246 | "BonoboSelector", |
---|
247 | sizeof (BonoboSelector), |
---|
248 | sizeof (BonoboSelectorClass), |
---|
249 | (GtkClassInitFunc) bonobo_selector_class_init, |
---|
250 | (GtkObjectInitFunc) bonobo_selector_init, |
---|
251 | (GtkArgSetFunc) NULL, |
---|
252 | (GtkArgGetFunc) NULL |
---|
253 | }; |
---|
254 | |
---|
255 | bonobo_selector_type = gtk_type_unique ( |
---|
256 | gnome_dialog_get_type (), |
---|
257 | &bonobo_selector_info); |
---|
258 | } |
---|
259 | |
---|
260 | return bonobo_selector_type; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * bonobo_selector_construct: |
---|
265 | * @sel: the selector to construct |
---|
266 | * @title: the title for the window |
---|
267 | * @selector: the component view widget to put inside it. |
---|
268 | * |
---|
269 | * Constructs the innards of a bonobo selector window. |
---|
270 | * |
---|
271 | * Return value: the constructed widget. |
---|
272 | **/ |
---|
273 | GtkWidget * |
---|
274 | bonobo_selector_construct (BonoboSelector *sel, |
---|
275 | const gchar *title, |
---|
276 | BonoboSelectorWidget *selector) |
---|
277 | { |
---|
278 | g_return_val_if_fail (BONOBO_IS_SELECTOR (sel), NULL); |
---|
279 | g_return_val_if_fail (BONOBO_IS_SELECTOR_WIDGET (selector), NULL); |
---|
280 | |
---|
281 | sel->priv->selector = selector; |
---|
282 | |
---|
283 | gtk_signal_connect (GTK_OBJECT (selector), "final_select", |
---|
284 | final_select_cb, sel); |
---|
285 | |
---|
286 | gtk_window_set_title (GTK_WINDOW (sel), title ? title : ""); |
---|
287 | |
---|
288 | gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (sel)->vbox), |
---|
289 | GTK_WIDGET (selector), |
---|
290 | TRUE, TRUE, GNOME_PAD_SMALL); |
---|
291 | |
---|
292 | gnome_dialog_append_button (GNOME_DIALOG (sel), GNOME_STOCK_BUTTON_OK); |
---|
293 | gnome_dialog_append_button (GNOME_DIALOG (sel), |
---|
294 | GNOME_STOCK_BUTTON_CANCEL); |
---|
295 | gnome_dialog_set_default (GNOME_DIALOG (sel), 0); |
---|
296 | |
---|
297 | gtk_signal_connect (GTK_OBJECT (sel), |
---|
298 | "clicked", GTK_SIGNAL_FUNC (button_callback), sel); |
---|
299 | gtk_signal_connect (GTK_OBJECT (sel), "close", |
---|
300 | GTK_SIGNAL_FUNC (button_callback), sel); |
---|
301 | |
---|
302 | gtk_widget_set_usize (GTK_WIDGET (sel), 400, 300); |
---|
303 | gtk_widget_show_all (GNOME_DIALOG (sel)->vbox); |
---|
304 | |
---|
305 | return GTK_WIDGET (sel); |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * bonobo_selector_new: |
---|
310 | * @title: A string which should go in the title of the |
---|
311 | * BonoboSelector window. |
---|
312 | * @interfaces_required: A NULL_terminated array of interfaces which a |
---|
313 | * server must support in order to be listed in the selector. Defaults |
---|
314 | * to "IDL:Bonobo/Embeddable:1.0" if no interfaces are listed. |
---|
315 | * |
---|
316 | * Creates a new BonoboSelector widget. The title of the dialog |
---|
317 | * is set to @title, and the list of selectable servers is populated |
---|
318 | * with those servers which support the interfaces specified in |
---|
319 | * @interfaces_required. |
---|
320 | * |
---|
321 | * Returns: A pointer to the newly-created BonoboSelector widget. |
---|
322 | */ |
---|
323 | GtkWidget * |
---|
324 | bonobo_selector_new (const gchar *title, |
---|
325 | const gchar **interfaces_required) |
---|
326 | { |
---|
327 | const gchar *query [2] = { DEFAULT_INTERFACE, NULL }; |
---|
328 | BonoboSelector *sel; |
---|
329 | BonoboSelectorWidget *selector; |
---|
330 | |
---|
331 | selector = BONOBO_SELECTOR_WIDGET (bonobo_selector_widget_new ()); |
---|
332 | |
---|
333 | if (!interfaces_required) |
---|
334 | interfaces_required = query; |
---|
335 | |
---|
336 | bonobo_selector_widget_set_interfaces (selector, interfaces_required); |
---|
337 | |
---|
338 | sel = gtk_type_new (bonobo_selector_get_type ()); |
---|
339 | |
---|
340 | return bonobo_selector_construct (sel, title, selector); |
---|
341 | } |
---|