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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* nautilus-string-picker.c - A widget to pick a string from a list.
Copyright (C) 1999, 2000 Eazel, Inc.
The Gnome 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 of the
License, or (at your option) any later version.
The Gnome 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 the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: Ramiro Estrugo <ramiro@eazel.com>
*/
#include <config.h>
#include "nautilus-string-picker.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-glib-extensions.h"
#include <gtk/gtklabel.h>
#include <gtk/gtkoptionmenu.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtksignal.h>
static const gint STRING_PICKER_SPACING = 10;
/* Signals */
typedef enum
{
CHANGED,
LAST_SIGNAL
} NautilusStringPickerSignals;
struct _NautilusStringPickerDetail
{
GtkWidget *option_menu;
GtkWidget *menu;
NautilusStringList *string_list;
};
/* NautilusStringPickerClass methods */
static void nautilus_string_picker_initialize_class (NautilusStringPickerClass *klass);
static void nautilus_string_picker_initialize (NautilusStringPicker *string_picker);
/* GtkObjectClass methods */
static void nautilus_string_picker_destroy (GtkObject *object);
/* Option menu item callbacks */
static void option_menu_activate_callback (GtkWidget *menu_item,
gpointer callback_data);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusStringPicker, nautilus_string_picker, NAUTILUS_TYPE_CAPTION)
static guint string_picker_signals[LAST_SIGNAL] = { 0 };
/*
* NautilusStringPickerClass methods
*/
static void
nautilus_string_picker_initialize_class (NautilusStringPickerClass *string_picker_class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = GTK_OBJECT_CLASS (string_picker_class);
widget_class = GTK_WIDGET_CLASS (string_picker_class);
/* GtkObjectClass */
object_class->destroy = nautilus_string_picker_destroy;
/* Signals */
string_picker_signals[CHANGED] = gtk_signal_new ("changed",
GTK_RUN_LAST,
object_class->type,
0,
gtk_marshal_NONE__NONE,
GTK_TYPE_NONE,
0);
gtk_object_class_add_signals (object_class, string_picker_signals, LAST_SIGNAL);
}
static void
nautilus_string_picker_initialize (NautilusStringPicker *string_picker)
{
string_picker->detail = g_new (NautilusStringPickerDetail, 1);
gtk_box_set_homogeneous (GTK_BOX (string_picker), FALSE);
gtk_box_set_spacing (GTK_BOX (string_picker), STRING_PICKER_SPACING);
string_picker->detail->string_list = nautilus_string_list_new (TRUE);
string_picker->detail->menu = NULL;
string_picker->detail->option_menu = gtk_option_menu_new ();
nautilus_caption_set_child (NAUTILUS_CAPTION (string_picker),
string_picker->detail->option_menu);
gtk_widget_show (string_picker->detail->option_menu);
}
/*
* GtkObjectClass methods
*/
static void
nautilus_string_picker_destroy (GtkObject* object)
{
NautilusStringPicker * string_picker;
g_return_if_fail (object != NULL);
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (object));
string_picker = NAUTILUS_STRING_PICKER (object);
nautilus_string_list_free (string_picker->detail->string_list);
g_free (string_picker->detail);
/* Chain */
NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}
/* Option menu item callbacks */
static void
option_menu_activate_callback (GtkWidget *menu_item, gpointer callback_data)
{
NautilusStringPicker *string_picker;
g_return_if_fail (menu_item != NULL);
g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
g_return_if_fail (callback_data != NULL);
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (callback_data));
string_picker = NAUTILUS_STRING_PICKER (callback_data);
gtk_signal_emit (GTK_OBJECT (string_picker), string_picker_signals[CHANGED]);
}
/*
* NautilusStringPicker public methods
*/
GtkWidget *
nautilus_string_picker_new (void)
{
return gtk_widget_new (nautilus_string_picker_get_type (), NULL);
}
/**
* nautilus_string_picker_set_string_list:
* @string_picker: A NautilusStringPicker
* @string_list: A list of strings
*
* Returns: nope
*/
void
nautilus_string_picker_set_string_list (NautilusStringPicker *string_picker,
const NautilusStringList *string_list)
{
guint i;
g_return_if_fail (string_picker != NULL);
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker));
nautilus_string_list_assign_from_string_list (string_picker->detail->string_list, string_list);
/* Kill the old menu if alive */
if (string_picker->detail->menu != NULL) {
gtk_option_menu_remove_menu (GTK_OPTION_MENU (string_picker->detail->option_menu));
/* The widget gets unrefed in the above call */
string_picker->detail->menu = NULL;
}
/* Make a new menu */
string_picker->detail->menu = gtk_menu_new ();
if (nautilus_string_list_get_length (string_picker->detail->string_list) > 0) {
for (i = 0; i < nautilus_string_list_get_length (string_picker->detail->string_list); i++) {
GtkWidget *menu_item;
char *item_label = nautilus_string_list_nth (string_picker->detail->string_list, i);
g_assert (item_label != NULL);
menu_item = gtk_menu_item_new_with_label (item_label);
g_free (item_label);
/* Save the index so we can later use it to retrieve the nth label from the list */
gtk_object_set_data (GTK_OBJECT (menu_item), "index", GINT_TO_POINTER (i));
gtk_signal_connect (GTK_OBJECT (menu_item),
"activate",
GTK_SIGNAL_FUNC (option_menu_activate_callback),
string_picker);
gtk_widget_show (menu_item);
gtk_menu_append (GTK_MENU (string_picker->detail->menu), menu_item);
}
}
/* Allow the string picker to be sensitive only if there is a choice */
if (nautilus_string_list_get_length (string_picker->detail->string_list) > 1) {
gtk_widget_set_sensitive (GTK_WIDGET (string_picker), TRUE);
}
else {
gtk_widget_set_sensitive (GTK_WIDGET (string_picker), FALSE);
}
/* Attatch the menu to the option button */
gtk_option_menu_set_menu (GTK_OPTION_MENU (string_picker->detail->option_menu), string_picker->detail->menu);
}
/**
* nautilus_string_picker_get_string_list:
* @string_picker: A NautilusStringPicker
*
* Returns: A copy of the list of strings for the string picker. Need to free it.
*/
NautilusStringList*
nautilus_string_picker_get_string_list (const NautilusStringPicker *string_picker)
{
g_return_val_if_fail (string_picker != NULL, NULL);
g_return_val_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker), NULL);
return nautilus_string_list_new_from_string_list (string_picker->detail->string_list, TRUE);
}
/* FIXME bugzilla.eazel.com 1556:
* Rename confusing string picker get/set functions
*/
/**
* nautilus_string_picker_get_selected_string
* @string_picker: A NautilusStringPicker
*
* Returns: A copy of the currently selected text. Need to g_free() it.
*/
char *
nautilus_string_picker_get_selected_string (NautilusStringPicker *string_picker)
{
gint item_index;
GtkWidget *option_menu;
GtkWidget *menu_item;
g_return_val_if_fail (string_picker != NULL, NULL);
g_return_val_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker), NULL);
option_menu = string_picker->detail->option_menu;
menu_item = GTK_OPTION_MENU (option_menu)->menu_item;
item_index = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (menu_item), "index"));
return (item_index != -1) ? nautilus_string_list_nth (string_picker->detail->string_list, item_index) : NULL;
}
/**
* nautilus_string_picker_set_selected_string
* @string_picker: A NautilusStringPicker
*
* Set the active item corresponding to the given text.
*/
void
nautilus_string_picker_set_selected_string (NautilusStringPicker *string_picker,
const char *text)
{
gint item_index;
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker));
g_return_if_fail (nautilus_string_list_contains (string_picker->detail->string_list, text));
item_index = nautilus_string_list_get_index_for_string (string_picker->detail->string_list, text);
g_assert (item_index != NAUTILUS_STRING_LIST_NOT_FOUND);
gtk_option_menu_set_history (GTK_OPTION_MENU (string_picker->detail->option_menu), item_index);
}
/**
* nautilus_string_picker_set_selected_string_index
* @string_picker: A NautilusStringPicker
* @index: Index of selected string.
*
* Set the selected entry corresponding to the given index.
*/
void
nautilus_string_picker_set_selected_string_index (NautilusStringPicker *string_picker,
guint index)
{
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker));
g_return_if_fail (index < nautilus_string_list_get_length (string_picker->detail->string_list));
gtk_option_menu_set_history (GTK_OPTION_MENU (string_picker->detail->option_menu), index);
}
/**
* nautilus_string_picker_insert_string
* @string_picker: A NautilusStringPicker
* @string: The string to insert.
*
* Insert a new string into the string picker.
*/
void
nautilus_string_picker_insert_string (NautilusStringPicker *string_picker,
const char *string)
{
NautilusStringList *new_string_list;
g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker));
new_string_list = nautilus_string_list_new_from_string_list (string_picker->detail->string_list, TRUE);
nautilus_string_list_insert (new_string_list, string);
nautilus_string_picker_set_string_list (string_picker, new_string_list);
nautilus_string_list_free (new_string_list);
}
/**
* nautilus_string_picker_insert_string
* @string_picker: A NautilusStringPicker
* @string: The string to insert.
*
* Insert a new string into the string picker.
*/
gboolean
nautilus_string_picker_contains (const NautilusStringPicker *string_picker,
const char *string)
{
g_return_val_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker), FALSE);
return nautilus_string_list_contains (string_picker->detail->string_list, string);
}
/**
* nautilus_string_picker_get_index_for_string
* @string_picker: A NautilusStringPicker
* @string: String to find.
*
* Return the index for the given string.
* Return NAUTILUS_STRING_LIST_NOT_FOUND if the string is not found.
*/
int
nautilus_string_picker_get_index_for_string (const NautilusStringPicker *string_picker,
const char *string)
{
g_return_val_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker), NAUTILUS_STRING_LIST_NOT_FOUND);
return nautilus_string_list_get_index_for_string (string_picker->detail->string_list, string);
}
|