[gcompris/gcomprixogoo] Completed the fix of missing letter configuration.



commit fbb5cd7d089e8a5dfc59c83b78035e5ee66cfcae
Author: Bruno Coudoin <bruno coudoin free fr>
Date:   Sun Nov 7 02:18:00 2010 +0100

    Completed the fix of missing letter configuration.
    
    There is now a cute dialog now explaining to the user the entry errors.

 src/missing_letter-activity/missingletter.c        |    1 -
 src/missing_letter-activity/missingletter_config.c |   97 ++++++++++++++------
 2 files changed, 69 insertions(+), 29 deletions(-)
---
diff --git a/src/missing_letter-activity/missingletter.c b/src/missing_letter-activity/missingletter.c
index 582d1bc..4891923 100644
--- a/src/missing_letter-activity/missingletter.c
+++ b/src/missing_letter-activity/missingletter.c
@@ -549,7 +549,6 @@ add_xml_data(xmlDocPtr doc, xmlNodePtr xmlnode, GList **list)
 	board->question = g_strdup(all_answer[i++]);
 	board->solution = 0;
 
-	printf("%s %s\n", board->answer, board->question);
 	while(all_answer[i] && text_index < MAX_PROPOSAL + 2)
 	  {
 	    board->text[text_index] = g_strdup(all_answer[i]);
diff --git a/src/missing_letter-activity/missingletter_config.c b/src/missing_letter-activity/missingletter_config.c
index 3af0853..fffbc8d 100644
--- a/src/missing_letter-activity/missingletter_config.c
+++ b/src/missing_letter-activity/missingletter_config.c
@@ -66,6 +66,7 @@ static gboolean valid_entry(const gchar *question, const gchar *answer,
   gboolean result=FALSE;
   gchar **split;
   gchar *error;
+  GtkWidget *dialog;
 
   g_assert(question);
   g_assert(answer);
@@ -97,7 +98,7 @@ static gboolean valid_entry(const gchar *question, const gchar *answer,
       goto error;
     }
 
-  if ( strlen(choice) < 3 )
+  if ( g_utf8_strlen(choice, -1) < 2 )
     {
       error = "There must be at least 2 choices";
       goto error;
@@ -113,14 +114,30 @@ static gboolean valid_entry(const gchar *question, const gchar *answer,
       goto error;
     }
 
-  //  if ( strchr(choice, answer[strlen(split[0])]) )
+  /* FIXME: Should manage UTF8 here */
+  if ( choice[0] != answer[strlen(split[0])] )
+    {
+      error = "The first choice must be the solution "
+	"that replaces the character '_'";
+      g_strfreev(split);
+      goto error;
+    }
   g_strfreev(split);
 
   return TRUE;
 
  error:
-    printf("Invalid entry: %s %s: %s\n", question, answer,
-	   error);
+  dialog = \
+    gtk_message_dialog_new (NULL,
+			    GTK_DIALOG_DESTROY_WITH_PARENT,
+			    GTK_MESSAGE_ERROR,
+			    GTK_BUTTONS_CLOSE,
+			    "Invalid entry:\n"
+			    "Question '%s' / Answer '%s'\n%s",
+			    question, answer,
+			    error);
+  gtk_dialog_run (GTK_DIALOG (dialog));
+  gtk_widget_destroy (dialog);
 
   return result;
 }
@@ -138,32 +155,51 @@ static void apply_clicked(gpointer data)
   answer = gtk_entry_get_text(u->answer);
   choice = gtk_entry_get_text(u->choice);
   pixmap = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(u->pixmap));
-  printf("pixmap=%s\n", pixmap);
-  if(valid_entry(question, answer, choice, pixmap))
+
+  if (gtk_tree_selection_get_selected (selection, &model, &iter))
     {
-      if (gtk_tree_selection_get_selected (selection, &model, &iter))
-        {
-          pixfile = gc_cache_import_pixmap(pixmap, "missingletter", 300, 300);
-	  printf("pixmap cache=%s\n", pixfile);
-          GdkPixbuf *pixbuf =
-	    gdk_pixbuf_new_from_file_at_size(pixmap, ICON_SIZE,
-					     ICON_SIZE, NULL);
-
-          gtk_list_store_set(GTK_LIST_STORE(model),&iter,
-                             QUESTION_COLUMN, question,
-                             ANSWER_COLUMN, answer,
-                             CHOICE_COLUMN, choice,
-                             PIXMAP_COLUMN, pixmap,
-                             PIXBUF_COLUMN, pixbuf,
-                             -1);
-          u->changed = TRUE;
-          g_free(pixfile);
-          g_object_unref(pixbuf);
-        }
+      pixfile = gc_cache_import_pixmap(pixmap, "missingletter", 300, 300);
+      GdkPixbuf *pixbuf =
+	gdk_pixbuf_new_from_file_at_size(pixmap, ICON_SIZE,
+					 ICON_SIZE, NULL);
+
+      gtk_list_store_set(GTK_LIST_STORE(model),&iter,
+			 QUESTION_COLUMN, question,
+			 ANSWER_COLUMN, answer,
+			 CHOICE_COLUMN, choice,
+			 PIXMAP_COLUMN, pixfile,
+			 PIXBUF_COLUMN, pixbuf,
+			 -1);
+      u->changed = TRUE;
+      g_free(pixfile);
+      g_object_unref(pixbuf);
     }
   g_free(pixmap);
 }
 
+static gboolean _check_errors(GtkTreeModel *model, GtkTreePath *path,
+			      GtkTreeIter *iter, gpointer data)
+{
+  gchar *question, *answer, *choice, *pixmap;
+  gboolean *has_error = (gboolean*)data;
+
+  gtk_tree_model_get (model, iter,
+                      QUESTION_COLUMN, &question,
+                      ANSWER_COLUMN, &answer,
+                      CHOICE_COLUMN, &choice,
+                      PIXMAP_COLUMN, &pixmap,
+                      -1);
+
+  if( ! valid_entry(question, answer, choice, pixmap))
+    {
+      *has_error = TRUE;
+      /* Don't check more errors */
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
 static gboolean _save(GtkTreeModel *model, GtkTreePath *path,
                       GtkTreeIter *iter, gpointer data)
 {
@@ -177,10 +213,9 @@ static gboolean _save(GtkTreeModel *model, GtkTreePath *path,
                       CHOICE_COLUMN, &choice,
                       PIXMAP_COLUMN, &pixmap,
                       -1);
-  printf("saving %s/%s/%s/%s\n", question, answer, choice, pixmap);
+
   if(valid_entry(question, answer, choice, pixmap))
     {
-      printf("saving2 %s\n", question);
       gchar *str = choice;
       gchar choices[(MAX_PROPOSAL * 2)+1];
       int i;
@@ -226,6 +261,12 @@ static void save_clicked(GtkButton *b, gpointer data)
   root = xmlNewNode(NULL, BAD_CAST "missing_letter");
   xmlDocSetRootElement(doc,root);
 
+  gboolean has_error = FALSE;
+  gtk_tree_model_foreach(model, _check_errors, &has_error);
+
+  if (has_error)
+    return;
+
   gtk_tree_model_foreach(model, _save, root);
 
   filename =
@@ -386,7 +427,7 @@ static void configure_colummns(GtkTreeView *treeview)
   column = gtk_tree_view_column_new_with_attributes(_("Choice"),
                                                     renderer, "text", CHOICE_COLUMN, NULL);
   gtk_tree_view_append_column(treeview, column);
-#if 1
+#if 0
   /* pixmap column (debug only)*/
   renderer = gtk_cell_renderer_text_new();
   column = gtk_tree_view_column_new_with_attributes("File",



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]