diff options
-rw-r--r-- | ChangeLog | 17 | ||||
-rw-r--r-- | libnautilus-extensions/nautilus-string-picker.c | 165 | ||||
-rw-r--r-- | libnautilus-extensions/nautilus-string-picker.h | 2 | ||||
-rw-r--r-- | libnautilus-private/nautilus-string-picker.c | 165 | ||||
-rw-r--r-- | libnautilus-private/nautilus-string-picker.h | 2 | ||||
-rw-r--r-- | test/test-nautilus-widgets.c | 4 |
6 files changed, 200 insertions, 155 deletions
@@ -1,5 +1,22 @@ 2000-07-05 Ramiro Estrugo <ramiro@eazel.com> + * libnautilus-extensions/nautilus-string-picker.c: + (nautilus_string_picker_initialize_class), + (nautilus_string_picker_initialize), + (nautilus_string_picker_destroy), (option_menu_activate_callback), + (nautilus_string_picker_set_string_list), + (nautilus_string_picker_get_text), + (nautilus_string_picker_set_text): + * libnautilus-extensions/nautilus-string-picker.h: + Changed string picker to use option menus instead of combo boxes. + + * test/test-nautilus-widgets.c: (test_string_picker), + (string_picker_changed_callback): + Changed widget test program to exercise more string picker + functions. + +2000-07-05 Ramiro Estrugo <ramiro@eazel.com> + * components/services/install/command-line/.cvsignore: Ignore generated files. diff --git a/libnautilus-extensions/nautilus-string-picker.c b/libnautilus-extensions/nautilus-string-picker.c index f1dd36385..31ea966ec 100644 --- a/libnautilus-extensions/nautilus-string-picker.c +++ b/libnautilus-extensions/nautilus-string-picker.c @@ -29,11 +29,12 @@ #include "nautilus-glib-extensions.h" #include <gtk/gtklabel.h> -#include <gtk/gtkcombo.h> +#include <gtk/gtkoptionmenu.h> +#include <gtk/gtkmenu.h> +#include <gtk/gtkmenuitem.h> #include <gtk/gtksignal.h> -static const gint STRING_PICKER_INVALID = -1; static const gint STRING_PICKER_SPACING = 10; /* Signals */ @@ -45,24 +46,23 @@ typedef enum struct _NautilusStringPickerDetail { -// GtkWidget *title_label; - GtkWidget *combo_box; + 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); +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); +static void nautilus_string_picker_destroy (GtkObject *object); -/* Private stuff */ -static GtkEntry *string_picker_get_entry_widget (NautilusStringPicker *string_picker); -/* Editable (entry) callbacks */ -static void entry_changed_callback (GtkWidget *entry, - gpointer user_data); +/* 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) @@ -84,14 +84,13 @@ nautilus_string_picker_initialize_class (NautilusStringPickerClass *string_picke 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); + 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); } @@ -105,41 +104,21 @@ nautilus_string_picker_initialize (NautilusStringPicker *string_picker) gtk_box_set_spacing (GTK_BOX (string_picker), STRING_PICKER_SPACING); string_picker->detail->string_list = NULL; + string_picker->detail->menu = NULL; -// string_picker->detail->title_label = gtk_label_new ("Title Label:"); - string_picker->detail->combo_box = gtk_combo_new (); - - gtk_entry_set_editable (string_picker_get_entry_widget (string_picker), FALSE); - -// gtk_box_pack_start (GTK_BOX (string_picker), -// string_picker->detail->title_label, -// FALSE, /* expand */ -// TRUE, /* fill */ -// 0); /* padding */ - -// gtk_box_pack_end (GTK_BOX (string_picker), -// string_picker->detail->combo_box, -// TRUE, /* expand */ -// TRUE, /* fill */ -// 0); /* padding */ - + string_picker->detail->option_menu = gtk_option_menu_new (); nautilus_caption_set_child (NAUTILUS_CAPTION (string_picker), - string_picker->detail->combo_box); + string_picker->detail->option_menu); - gtk_signal_connect (GTK_OBJECT (string_picker_get_entry_widget (string_picker)), - "changed", - GTK_SIGNAL_FUNC (entry_changed_callback), - (gpointer) string_picker); - - gtk_widget_show (string_picker->detail->combo_box); + gtk_widget_show (string_picker->detail->option_menu); } /* * GtkObjectClass methods */ static void -nautilus_string_picker_destroy(GtkObject* object) +nautilus_string_picker_destroy (GtkObject* object) { NautilusStringPicker * string_picker; @@ -158,30 +137,19 @@ nautilus_string_picker_destroy(GtkObject* object) NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object)); } -/* - * Private stuff - */ -static GtkEntry * -string_picker_get_entry_widget (NautilusStringPicker *string_picker) -{ - g_assert (string_picker != NULL); - g_assert (NAUTILUS_IS_STRING_PICKER (string_picker)); - - return GTK_ENTRY (GTK_COMBO (string_picker->detail->combo_box)->entry); -} - -/* - * Editable (entry) callbacks - */ +/* Option menu item callbacks */ static void -entry_changed_callback (GtkWidget *entry, gpointer user_data) +option_menu_activate_callback (GtkWidget *menu_item, gpointer callback_data) { NautilusStringPicker *string_picker; - g_assert (user_data != NULL); - g_assert (NAUTILUS_IS_STRING_PICKER (user_data)); + 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 (user_data); + string_picker = NAUTILUS_STRING_PICKER (callback_data); gtk_signal_emit (GTK_OBJECT (string_picker), string_picker_signals[CHANGED]); } @@ -209,20 +177,52 @@ void nautilus_string_picker_set_string_list (NautilusStringPicker *string_picker, const NautilusStringList *string_list) { - GList *strings; + guint i; g_return_if_fail (string_picker != NULL); g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker)); string_picker->detail->string_list = nautilus_string_list_new_from_string_list (string_list); - strings = nautilus_string_list_as_g_list (string_picker->detail->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)); + gtk_widget_destroy (string_picker->detail->menu); + string_picker->detail->menu = NULL; + } - gtk_combo_set_popdown_strings (GTK_COMBO (string_picker->detail->combo_box), strings); + /* Make a new menu */ + string_picker->detail->menu = gtk_menu_new (); + + 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); + + } - nautilus_g_list_free_deep (strings); + gtk_option_menu_set_menu (GTK_OPTION_MENU (string_picker->detail->option_menu), string_picker->detail->menu); } +/* FIXME bugzilla.eazel.com 1556: + * Rename confusing string picker get/set functions + */ + /** * nautilus_string_picker_get_text * @string_picker: A NautilusStringPicker @@ -232,29 +232,42 @@ nautilus_string_picker_set_string_list (NautilusStringPicker *string_picker, char * nautilus_string_picker_get_text (NautilusStringPicker *string_picker) { - const char *entry_text; + 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); - /* WATCHOUT: - * As of gtk1.2, gtk_entry_get_text() returns a non const reference to - * the internal string. - */ - entry_text = (const char *) gtk_entry_get_text (string_picker_get_entry_widget (string_picker)); + 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 g_strdup (entry_text); + return (item_index != -1) ? nautilus_string_list_nth (string_picker->detail->string_list, item_index) : NULL; } +/** + * nautilus_string_picker_set_text + * @string_picker: A NautilusStringPicker + * + * Set the active item corresponding to the given text. + */ void nautilus_string_picker_set_text (NautilusStringPicker *string_picker, const char *text) { + gint item_index; + g_return_if_fail (string_picker != NULL); g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker)); g_return_if_fail (string_picker->detail->string_list != NULL); g_return_if_fail (nautilus_string_list_contains (string_picker->detail->string_list, text)); - gtk_entry_set_text (string_picker_get_entry_widget (string_picker), 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); } diff --git a/libnautilus-extensions/nautilus-string-picker.h b/libnautilus-extensions/nautilus-string-picker.h index dcf02d29c..218da549e 100644 --- a/libnautilus-extensions/nautilus-string-picker.h +++ b/libnautilus-extensions/nautilus-string-picker.h @@ -31,7 +31,7 @@ /* * NautilusStringPicker is made up of 2 widgets. * - * [title label] [string combo box] + * [title label] [string list] * * The user can select a string from the list. */ diff --git a/libnautilus-private/nautilus-string-picker.c b/libnautilus-private/nautilus-string-picker.c index f1dd36385..31ea966ec 100644 --- a/libnautilus-private/nautilus-string-picker.c +++ b/libnautilus-private/nautilus-string-picker.c @@ -29,11 +29,12 @@ #include "nautilus-glib-extensions.h" #include <gtk/gtklabel.h> -#include <gtk/gtkcombo.h> +#include <gtk/gtkoptionmenu.h> +#include <gtk/gtkmenu.h> +#include <gtk/gtkmenuitem.h> #include <gtk/gtksignal.h> -static const gint STRING_PICKER_INVALID = -1; static const gint STRING_PICKER_SPACING = 10; /* Signals */ @@ -45,24 +46,23 @@ typedef enum struct _NautilusStringPickerDetail { -// GtkWidget *title_label; - GtkWidget *combo_box; + 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); +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); +static void nautilus_string_picker_destroy (GtkObject *object); -/* Private stuff */ -static GtkEntry *string_picker_get_entry_widget (NautilusStringPicker *string_picker); -/* Editable (entry) callbacks */ -static void entry_changed_callback (GtkWidget *entry, - gpointer user_data); +/* 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) @@ -84,14 +84,13 @@ nautilus_string_picker_initialize_class (NautilusStringPickerClass *string_picke 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); + 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); } @@ -105,41 +104,21 @@ nautilus_string_picker_initialize (NautilusStringPicker *string_picker) gtk_box_set_spacing (GTK_BOX (string_picker), STRING_PICKER_SPACING); string_picker->detail->string_list = NULL; + string_picker->detail->menu = NULL; -// string_picker->detail->title_label = gtk_label_new ("Title Label:"); - string_picker->detail->combo_box = gtk_combo_new (); - - gtk_entry_set_editable (string_picker_get_entry_widget (string_picker), FALSE); - -// gtk_box_pack_start (GTK_BOX (string_picker), -// string_picker->detail->title_label, -// FALSE, /* expand */ -// TRUE, /* fill */ -// 0); /* padding */ - -// gtk_box_pack_end (GTK_BOX (string_picker), -// string_picker->detail->combo_box, -// TRUE, /* expand */ -// TRUE, /* fill */ -// 0); /* padding */ - + string_picker->detail->option_menu = gtk_option_menu_new (); nautilus_caption_set_child (NAUTILUS_CAPTION (string_picker), - string_picker->detail->combo_box); + string_picker->detail->option_menu); - gtk_signal_connect (GTK_OBJECT (string_picker_get_entry_widget (string_picker)), - "changed", - GTK_SIGNAL_FUNC (entry_changed_callback), - (gpointer) string_picker); - - gtk_widget_show (string_picker->detail->combo_box); + gtk_widget_show (string_picker->detail->option_menu); } /* * GtkObjectClass methods */ static void -nautilus_string_picker_destroy(GtkObject* object) +nautilus_string_picker_destroy (GtkObject* object) { NautilusStringPicker * string_picker; @@ -158,30 +137,19 @@ nautilus_string_picker_destroy(GtkObject* object) NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object)); } -/* - * Private stuff - */ -static GtkEntry * -string_picker_get_entry_widget (NautilusStringPicker *string_picker) -{ - g_assert (string_picker != NULL); - g_assert (NAUTILUS_IS_STRING_PICKER (string_picker)); - - return GTK_ENTRY (GTK_COMBO (string_picker->detail->combo_box)->entry); -} - -/* - * Editable (entry) callbacks - */ +/* Option menu item callbacks */ static void -entry_changed_callback (GtkWidget *entry, gpointer user_data) +option_menu_activate_callback (GtkWidget *menu_item, gpointer callback_data) { NautilusStringPicker *string_picker; - g_assert (user_data != NULL); - g_assert (NAUTILUS_IS_STRING_PICKER (user_data)); + 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 (user_data); + string_picker = NAUTILUS_STRING_PICKER (callback_data); gtk_signal_emit (GTK_OBJECT (string_picker), string_picker_signals[CHANGED]); } @@ -209,20 +177,52 @@ void nautilus_string_picker_set_string_list (NautilusStringPicker *string_picker, const NautilusStringList *string_list) { - GList *strings; + guint i; g_return_if_fail (string_picker != NULL); g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker)); string_picker->detail->string_list = nautilus_string_list_new_from_string_list (string_list); - strings = nautilus_string_list_as_g_list (string_picker->detail->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)); + gtk_widget_destroy (string_picker->detail->menu); + string_picker->detail->menu = NULL; + } - gtk_combo_set_popdown_strings (GTK_COMBO (string_picker->detail->combo_box), strings); + /* Make a new menu */ + string_picker->detail->menu = gtk_menu_new (); + + 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); + + } - nautilus_g_list_free_deep (strings); + gtk_option_menu_set_menu (GTK_OPTION_MENU (string_picker->detail->option_menu), string_picker->detail->menu); } +/* FIXME bugzilla.eazel.com 1556: + * Rename confusing string picker get/set functions + */ + /** * nautilus_string_picker_get_text * @string_picker: A NautilusStringPicker @@ -232,29 +232,42 @@ nautilus_string_picker_set_string_list (NautilusStringPicker *string_picker, char * nautilus_string_picker_get_text (NautilusStringPicker *string_picker) { - const char *entry_text; + 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); - /* WATCHOUT: - * As of gtk1.2, gtk_entry_get_text() returns a non const reference to - * the internal string. - */ - entry_text = (const char *) gtk_entry_get_text (string_picker_get_entry_widget (string_picker)); + 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 g_strdup (entry_text); + return (item_index != -1) ? nautilus_string_list_nth (string_picker->detail->string_list, item_index) : NULL; } +/** + * nautilus_string_picker_set_text + * @string_picker: A NautilusStringPicker + * + * Set the active item corresponding to the given text. + */ void nautilus_string_picker_set_text (NautilusStringPicker *string_picker, const char *text) { + gint item_index; + g_return_if_fail (string_picker != NULL); g_return_if_fail (NAUTILUS_IS_STRING_PICKER (string_picker)); g_return_if_fail (string_picker->detail->string_list != NULL); g_return_if_fail (nautilus_string_list_contains (string_picker->detail->string_list, text)); - gtk_entry_set_text (string_picker_get_entry_widget (string_picker), 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); } diff --git a/libnautilus-private/nautilus-string-picker.h b/libnautilus-private/nautilus-string-picker.h index dcf02d29c..218da549e 100644 --- a/libnautilus-private/nautilus-string-picker.h +++ b/libnautilus-private/nautilus-string-picker.h @@ -31,7 +31,7 @@ /* * NautilusStringPicker is made up of 2 widgets. * - * [title label] [string combo box] + * [title label] [string list] * * The user can select a string from the list. */ diff --git a/test/test-nautilus-widgets.c b/test/test-nautilus-widgets.c index 5a472e8cb..9b462390a 100644 --- a/test/test-nautilus-widgets.c +++ b/test/test-nautilus-widgets.c @@ -153,6 +153,8 @@ test_string_picker (void) GTK_SIGNAL_FUNC (string_picker_changed_callback), (gpointer) NULL); + nautilus_string_picker_set_text (NAUTILUS_STRING_PICKER (picker), "Fixed"); + gtk_widget_show_all (window); } @@ -221,7 +223,7 @@ string_picker_changed_callback (GtkWidget *string_picker, gpointer user_data) text = nautilus_string_picker_get_text (NAUTILUS_STRING_PICKER (string_picker)); g_print ("string_picker_changed_callback(%s)\n", text); - + g_free (text); } |