changeset 27232:5dcee348bc22

Automatically enable and disable the Add/Save button in the PidginSmiley edit window. That should take care of that TODO comment and "provide a better user experience".
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 03 Jul 2009 01:18:02 +0000
parents 627d23bfdb05
children 0af18d18de0d
files pidgin/gtksmiley.c
diffstat 1 files changed, 44 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin/gtksmiley.c	Thu Jul 02 04:26:00 2009 +0000
+++ b/pidgin/gtksmiley.c	Fri Jul 03 01:18:02 2009 +0000
@@ -49,6 +49,7 @@
 	GdkPixbuf *custom_pixbuf;
 	gpointer data; /** @since 2.6.0 */
 	gsize datasize; /** @since 2.6.0 */
+	gint entry_len; /** @since 2.6.0 */
 };
 
 typedef struct
@@ -226,18 +227,6 @@
 	PurpleSmiley *emoticon;
 
 	entry = gtk_entry_get_text(GTK_ENTRY(s->smile));
-	if (!entry || !*entry) {
-		/*
-		 * TODO: We should enable/disable the add button based on
-		 *       whether the user has entered all required data.  That
-		 *       would eliminate the need for this check and provide a
-		 *       better user experience.
-		 */
-		purple_notify_error(s->parent, _("Custom Smiley"),
-				_("More Data needed"),
-				_("Please provide a shortcut to associate with the smiley."));
-		return;
-	}
 
 	emoticon = purple_smileys_find_by_shortcut(entry);
 	if (emoticon && emoticon != s->smiley) {
@@ -267,15 +256,6 @@
 		}
 		purple_smiley_set_shortcut(s->smiley, entry);
 	} else {
-		if ((s->filename == NULL && s->custom_pixbuf == NULL)
-				|| *entry == 0) {
-			purple_notify_error(s->parent, _("Custom Smiley"),
-					_("More Data needed"),
-					s->filename ? _("Please provide a shortcut to associate with the smiley.")
-					: _("Please select an image for the smiley."));
-			return;
-		}
-
 		purple_debug_info("gtksmiley", "adding a new smiley\n");
 
 		if (s->filename == NULL) {
@@ -355,6 +335,9 @@
 	if (pixbuf)
 		g_object_unref(G_OBJECT(pixbuf));
 	gtk_widget_grab_focus(s->smile);
+
+	if (s->entry_len > 0)
+		gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, TRUE);
 }
 
 static void
@@ -368,6 +351,36 @@
 	gtk_widget_show_all(file_chooser);
 }
 
+static void
+smiley_name_insert_cb(GtkEditable *editable,
+                      gchar       *new_text,
+                      gint         new_text_length,
+                      gint        *position,
+                      gpointer     user_data)
+{
+	PidginSmiley *s = user_data;
+	if (new_text_length != -1)
+		s->entry_len += new_text_length;
+	else
+		s->entry_len += strlen(new_text);
+
+	if (s->filename != NULL || s->custom_pixbuf != NULL || s->smiley != NULL)
+		gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, TRUE);
+}
+
+static void
+smiley_name_delete_cb(GtkEditable *editable,
+                      gint         start_pos,
+                      gint         end_pos,
+                      gpointer     user_data)
+{
+	PidginSmiley *s = user_data;
+	s->entry_len -= end_pos - start_pos;
+
+	if (s->entry_len <= 0)
+		gtk_dialog_set_response_sensitive(GTK_DIALOG(s->parent), GTK_RESPONSE_ACCEPT, FALSE);
+}
+
 PidginSmiley *
 pidgin_smiley_edit(GtkWidget *widget, PurpleSmiley *smiley)
 {
@@ -441,10 +454,18 @@
 	s->smile = gtk_entry_new();
 	gtk_entry_set_activates_default(GTK_ENTRY(s->smile), TRUE);
 	pidgin_set_accessible_label(s->smile, label);
-	if (smiley)
-		gtk_entry_set_text(GTK_ENTRY(s->smile), purple_smiley_get_shortcut(smiley));
+	if (smiley) {
+		const char *shortcut = purple_smiley_get_shortcut(smiley);
+		gtk_entry_set_text(GTK_ENTRY(s->smile), shortcut);
+		s->entry_len = strlen(shortcut);
+	}
+	else
+		gtk_dialog_set_response_sensitive(GTK_DIALOG(window), GTK_RESPONSE_ACCEPT, FALSE);
 
 	g_signal_connect(s->smile, "activate", G_CALLBACK(do_add), s);
+	/* gtk_entry_get_text_length is 2.14+, so we'll just keep track ourselves */
+	g_signal_connect(G_OBJECT(s->smile), "insert-text", G_CALLBACK(smiley_name_insert_cb), s);
+	g_signal_connect(G_OBJECT(s->smile), "delete-text", G_CALLBACK(smiley_name_delete_cb), s);
 
 	gtk_box_pack_end(GTK_BOX(hbox), s->smile, FALSE, FALSE, 0);
 	gtk_widget_show(s->smile);