Back to index

gcompris  8.2.2
gcompris_confirm.c
Go to the documentation of this file.
00001 /* gcompris - gc_confirm_box.c
00002  *
00003  * Time-stamp: <2006/08/20 10:14:18 bruno>
00004  *
00005  * Copyright (C) 2000 Bruno Coudoin
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation; either version 2 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with this program; if not, write to the Free Software
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 #include <string.h>
00022 
00027 #include "gcompris.h"
00028 
00029 static GnomeCanvasItem *richtext_s, *richtext;
00030 
00031 static void              display_confirm(gchar *title,
00032                                     gchar *question_text,
00033                                     gchar *yes_text,
00034                                     gchar *no_text,
00035                                     ConfirmCallBack iscb);
00036 
00037 static gint
00038 button_event(GnomeCanvasItem *item, GdkEvent *event,  gchar *answer);
00039 
00040 static void              set_content(GnomeCanvasRichText *item_content, 
00041                                  GnomeCanvasRichText *item_content_s,
00042                                  gchar *text);
00043 
00044 static gboolean              confirm_displayed = FALSE;
00045 
00046 static GnomeCanvasItem      *rootitem = NULL;
00047 static GnomeCanvasItem      *no_button = NULL;
00048 static GnomeCanvasItem      *yes_button = NULL;
00049 static GnomeCanvasItem      *yes_stick = NULL;
00050 static GnomeCanvasItem      *no_cross = NULL;
00051 
00052 static ConfirmCallBack  confirmCallBack = NULL;
00053 
00054 static gdouble bg_x;
00055 static gdouble bg_y;
00056 
00057 static gdouble titre_x;
00058 static gdouble titre_w;
00059 static gdouble titre_y;
00060 static gdouble titre_h;
00061 
00062 
00063 static gdouble text_zone_x;
00064 static gdouble text_zone_w;
00065 static gdouble text_zone_y;
00066 static gdouble text_zone_h;
00067 
00068 
00069 static gdouble button_x;
00070 static gdouble button_w;
00071 static gdouble button_y;
00072 static gdouble button_h;
00073 static gdouble button_x_int;
00074 
00075 /* background */
00076 #define BG_X 400
00077 #define BG_Y 0
00078 
00079 /* TITLE */
00080 #define T_X 80
00081 #define T_Y 24
00082 #define T_W 620
00083 #define T_H 30
00084 
00085 /* Text */
00086 #define T_Z_X 80
00087 #define T_Z_Y 120
00088 #define T_Z_W 620
00089 #define T_Z_H 115
00090 
00091 /* Buttons */
00092 #define T_B_X 80 
00093 #define T_B_Y 250
00094 #define T_B_W 620 
00095 #define T_B_H 192
00096 #define T_B_X_INT 30
00097 
00098 
00099 /*
00100  * Main entry point 
00101  * ----------------
00102  *
00103  */
00104 
00105 /*
00106  * Do all the file_selector display and register the events
00107  * file_types is A Comma separated text explaining the different file types
00108  */
00109 
00110 void gc_confirm_box (gchar *title, 
00111                      gchar *question_text,
00112                      gchar *yes_text,
00113                      gchar *no_text,
00114                      ConfirmCallBack iscb)
00115 {
00116   display_confirm(title, 
00117                 question_text, 
00118                 yes_text,
00119                 no_text,
00120                 iscb);
00121 }
00122 
00123 /*
00124  * Remove the displayed file_selector.
00125  * Do nothing if none is currently being dislayed
00126  */
00127 
00128 void gc_confirm_box_stop ()
00129 {
00130   GcomprisBoard *gcomprisBoard = gc_board_get_current();
00131 
00132   // Destroy the box
00133   /* FIXME: Crashes randomly */
00134   if(rootitem!=NULL)
00135     {
00136       /* WORKAROUND: There is a bug in the richtex item and we need to remove it first */
00137       while (g_idle_remove_by_data (richtext));
00138       gtk_object_destroy (GTK_OBJECT(richtext));
00139       while (g_idle_remove_by_data (richtext_s));
00140       gtk_object_destroy (GTK_OBJECT(richtext_s));
00141       gtk_object_destroy(GTK_OBJECT(rootitem));
00142     }
00143 
00144   rootitem = NULL;     
00145 
00146   if(gcomprisBoard!=NULL && confirm_displayed)
00147     gc_board_pause(FALSE);
00148 
00149   gc_bar_hide(FALSE);
00150   confirm_displayed = FALSE;
00151 }
00152 
00153 
00154 
00155 /*-------------------------------------------------------------------------------*/
00156 /*-------------------------------------------------------------------------------*/
00157 /*-------------------------------------------------------------------------------*/
00158 /*-------------------------------------------------------------------------------*/
00159 
00160 
00161 static void
00162 display_confirm(gchar *title,
00163               gchar *question_text,
00164               gchar *yes_text,
00165               gchar *no_text,
00166               ConfirmCallBack iscb) {
00167 
00168   GnomeCanvasItem  *item, *item2;
00169   GdkPixbuf      *pixmap = NULL;
00170   GdkPixbuf      *pixmap_cross = NULL;
00171   GdkPixbuf      *pixmap_stick = NULL;
00172   gint            y_start = 0;
00173   gint            x_start = 0;
00174 
00175   if(rootitem)
00176     return;
00177 
00178   bg_x = BG_X;
00179   bg_y = BG_Y;
00180 
00181   titre_x = T_X;
00182   titre_w = T_W;
00183   titre_y = T_Y;;
00184   titre_h = T_H;
00185 
00186 
00187   text_zone_x = T_Z_X;
00188   text_zone_w = T_Z_W;
00189   text_zone_y = T_Z_Y;
00190   text_zone_h = T_Z_H;
00191 
00192 
00193   button_x = T_B_X;
00194   button_w = T_B_W;
00195   button_y = T_B_Y;
00196   button_h = T_B_Y;
00197   button_x_int = T_B_X_INT;
00198 
00199   gc_bar_hide(TRUE);
00200 
00201   gc_board_pause(TRUE);
00202 
00203   confirmCallBack=iscb;
00204 
00205   rootitem = \
00206     gnome_canvas_item_new (gnome_canvas_root(gc_get_canvas()),
00207                         gnome_canvas_group_get_type (),
00208                         "x", (double)0,
00209                         "y", (double)0,
00210                         NULL);
00211 
00212   pixmap = gc_skin_pixmap_load("help_bg.png");
00213   y_start = (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap))/2;
00214   if (y_start < 0)
00215     y_start = 0;
00216   x_start = (BOARDWIDTH)/2;
00217   item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00218                             gnome_canvas_pixbuf_get_type (),
00219                             "pixbuf", pixmap, 
00220                             "x", (double) x_start,
00221                             "y", (double) y_start,
00222                             "anchor", GTK_ANCHOR_NORTH,
00223                             NULL);
00224 
00225   gdk_pixbuf_unref(pixmap);
00226 
00227   /* Title */
00228   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00229                       gnome_canvas_text_get_type (),
00230                       "text", title,
00231                       "x", (gdouble) titre_x + titre_w/2 + 1.0,
00232                       "y", (gdouble) titre_y + titre_h/2 + 1.0,
00233                       "font", gc_skin_font_title,
00234                       "anchor", GTK_ANCHOR_CENTER,
00235                       "fill_color_rgba",  gc_skin_color_shadow,
00236                       NULL);
00237 
00238   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00239                       gnome_canvas_text_get_type (),
00240                       "text", title,
00241                       "x", (gdouble) titre_x + titre_w/2,
00242                       "y", (gdouble) titre_y + titre_h/2,
00243                       "font", gc_skin_font_title,
00244                       "anchor", GTK_ANCHOR_CENTER,
00245                       "fill_color_rgba",  gc_skin_color_title,
00246                       NULL);
00247 
00248   richtext_s = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00249                                  gnome_canvas_rich_text_get_type (),
00250                                  "x", (double) text_zone_x + 1.0,
00251                                  "y", (double)  text_zone_y + 1.0,
00252                                  "width",  text_zone_w,
00253                                  "height", text_zone_h,
00254                                  "anchor", GTK_ANCHOR_NW,
00255                                  "justification", GTK_JUSTIFY_CENTER,
00256                                  "grow_height", FALSE,
00257                                  "cursor_visible", FALSE,
00258                                  "cursor_blink", FALSE,
00259                                  "editable", FALSE,
00260                                  NULL);
00261 
00262 
00263   richtext = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00264                                  gnome_canvas_rich_text_get_type (),
00265                                  "x", (double) text_zone_x ,
00266                                  "y", (double)  text_zone_y,
00267                                  "width",  text_zone_w,
00268                                  "height", text_zone_h,
00269                                  "anchor", GTK_ANCHOR_NW,
00270                                  "justification", GTK_JUSTIFY_CENTER,
00271                                  "grow_height", FALSE,
00272                                  "cursor_visible", FALSE,
00273                                  "cursor_blink", FALSE,
00274                                  "editable", FALSE,
00275                                  NULL);
00276 
00277   set_content (GNOME_CANVAS_RICH_TEXT(richtext), 
00278               GNOME_CANVAS_RICH_TEXT(richtext_s), 
00279               question_text);
00280         
00281   
00282   /*
00283    * Buttons
00284    * -------
00285    */
00286 
00287   pixmap = gc_skin_pixmap_load("button_large.png");
00288   pixmap_stick = gc_skin_pixmap_load("button_checked.png");
00289   pixmap_cross = gc_skin_pixmap_load("bad.png");
00290 
00291   // CANCEL
00292   no_button = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00293                                  gnome_canvas_pixbuf_get_type (),
00294                                  "pixbuf", pixmap, 
00295                                  "x", (double) button_x ,
00296                                  "y", (double) button_y + 2*button_h/3,
00297                                  "anchor", GTK_ANCHOR_WEST,
00298                                  NULL);
00299 
00300   //gdk_pixbuf_unref(pixmap);
00301 
00302   gtk_signal_connect(GTK_OBJECT(no_button), "event",
00303                    (GtkSignalFunc) button_event,
00304                    "/no/");
00305 
00306   gtk_signal_connect(GTK_OBJECT(no_button), "event",
00307                    (GtkSignalFunc) gc_item_focus_event,
00308                    NULL);
00309 
00310   // CANCEL CROSS
00311   no_cross = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00312                                 gnome_canvas_pixbuf_get_type (),
00313                                 "pixbuf", pixmap_cross, 
00314                                 "x", (double) button_x  + gdk_pixbuf_get_width(pixmap)/2,
00315                                 "y", (double) button_y + 2*button_h/3,
00316                                 "anchor", GTK_ANCHOR_CENTER,
00317                                 NULL);
00318 
00319   gdk_pixbuf_unref(pixmap_cross);
00320 
00321   gtk_signal_connect(GTK_OBJECT(no_cross), "event",
00322                    (GtkSignalFunc) button_event,
00323                    "/no/");
00324   gtk_signal_connect(GTK_OBJECT(no_cross), "event",
00325                    (GtkSignalFunc) gc_item_focus_event,
00326                    NULL);
00327 
00328 
00329   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00330                       gnome_canvas_text_get_type (),
00331                       "text", no_text,
00332                       "font", gc_skin_font_subtitle,
00333                       "x", (double)  button_x + gdk_pixbuf_get_width(pixmap) + button_x_int ,
00334                       "y", (double)  button_y + 2*button_h/3,
00335                       "anchor", GTK_ANCHOR_WEST,
00336                       "fill_color_rgba", gc_skin_get_color("gcompris/helpfg"),
00337                       NULL);
00338 
00339   // OK
00340   yes_button = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00341                                   gnome_canvas_pixbuf_get_type (),
00342                                   "pixbuf", pixmap, 
00343                                   "x", (double) button_x ,
00344                                   "y", (double) button_y + button_h/3,
00345                                   "anchor", GTK_ANCHOR_WEST,
00346                                   NULL);
00347   
00348   gdk_pixbuf_unref(pixmap);
00349   
00350   gtk_signal_connect(GTK_OBJECT(yes_button), "event",
00351                    (GtkSignalFunc) button_event,
00352                    "/yes/");
00353 
00354   gtk_signal_connect(GTK_OBJECT(yes_button), "event",
00355                    (GtkSignalFunc) gc_item_focus_event,
00356                    NULL);
00357 
00358   // OK stick
00359   yes_stick = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00360                                  gnome_canvas_pixbuf_get_type (),
00361                                  "pixbuf", pixmap_stick, 
00362                                  "x", (double) button_x + gdk_pixbuf_get_width(pixmap)/2,
00363                                  "y", (double) button_y + button_h/3,
00364                                  "anchor", GTK_ANCHOR_CENTER,
00365                                  NULL);
00366 
00367   gdk_pixbuf_unref(pixmap_stick);
00368 
00369   gtk_signal_connect(GTK_OBJECT(yes_stick), "event",
00370                    (GtkSignalFunc) button_event,
00371                    "/yes/");
00372   gtk_signal_connect(GTK_OBJECT(yes_stick), "event",
00373                    (GtkSignalFunc) gc_item_focus_event,
00374                    NULL);
00375 
00376 
00377   item2 = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00378                              gnome_canvas_text_get_type (),
00379                              "text", yes_text,
00380                              "font", gc_skin_font_subtitle,
00381                              "x", (double)  button_x + gdk_pixbuf_get_width(pixmap) + button_x_int ,
00382                              "y", (double)  button_y + button_h/3,
00383                              "anchor", GTK_ANCHOR_WEST,
00384                              "fill_color_rgba", gc_skin_get_color("gcompris/helpfg"),
00385                              NULL);
00386 
00387   confirm_displayed = TRUE;
00388 
00389 }
00390 
00391 /* Apply the style to the given RichText item  */
00392 static void
00393 set_content(GnomeCanvasRichText *item_content, 
00394            GnomeCanvasRichText *item_content_s,
00395            gchar *text) {
00396 
00397   GtkTextIter    iter_start, iter_end;
00398   GtkTextBuffer *buffer;
00399   GtkTextTag    *txt_tag;
00400   gboolean success; 
00401   gchar *color_string;
00402   GdkColor *color_s = (GdkColor *)malloc(sizeof(GdkColor));
00403   GdkColor *color   = (GdkColor *)malloc(sizeof(GdkColor));
00404 
00405   /*
00406    * Set the new text in the 2 items
00407    */
00408   gnome_canvas_item_set(GNOME_CANVAS_ITEM(item_content),
00409                      "text", text,
00410                      NULL);
00411 
00412   gnome_canvas_item_set(GNOME_CANVAS_ITEM(item_content_s),
00413                      "text", text,
00414                      NULL);
00415 
00416   /*
00417    * Set the shadow
00418    */
00419 
00420   color_string = g_strdup_printf("#%x", gc_skin_color_shadow >> 8);
00421   gdk_color_parse(color_string, color_s);
00422   g_free(color_string);
00423   success = gdk_colormap_alloc_color(gdk_colormap_get_system(), 
00424                                  color_s,
00425                                  FALSE, TRUE); 
00426 
00427   buffer  = gnome_canvas_rich_text_get_buffer(GNOME_CANVAS_RICH_TEXT(item_content_s));
00428   txt_tag = gtk_text_buffer_create_tag(buffer, NULL, 
00429                                    "foreground-gdk", color_s,
00430                                    "font",       gc_skin_font_board_medium,
00431                                    NULL);
00432   gtk_text_buffer_get_end_iter(buffer, &iter_end);
00433   gtk_text_buffer_get_start_iter(buffer, &iter_start);
00434   gtk_text_buffer_apply_tag(buffer, txt_tag, &iter_start, &iter_end);
00435 
00436 
00437   /* 
00438    * Set the text
00439    */
00440   color_string = g_strdup_printf("#%x", gc_skin_get_color("gcompris/helpunselect") >> 8);
00441   gdk_color_parse(color_string, color);
00442   g_free(color_string);
00443   success = gdk_colormap_alloc_color(gdk_colormap_get_system(), 
00444                                  color,
00445                                  FALSE, TRUE); 
00446 
00447   buffer  = gnome_canvas_rich_text_get_buffer(GNOME_CANVAS_RICH_TEXT(item_content));
00448   txt_tag = gtk_text_buffer_create_tag(buffer, NULL, 
00449                                    "foreground-gdk", color,
00450                                    "font",        gc_skin_font_board_medium,
00451                                    NULL);
00452   gtk_text_buffer_get_end_iter(buffer, &iter_end);
00453   gtk_text_buffer_get_start_iter(buffer, &iter_start);
00454   gtk_text_buffer_apply_tag(buffer, txt_tag, &iter_start, &iter_end);
00455 
00456   g_free(color);
00457   g_free(color_s);
00458 
00459 }
00460 
00461 static gint
00462 button_event(GnomeCanvasItem *item, GdkEvent *event,  gchar *answer)
00463 {
00464   if (event->type != GDK_BUTTON_PRESS)
00465     return FALSE;
00466 
00467   /* Callback with the proper params */
00468   if (confirmCallBack){
00469     if (strcmp(answer, "/no/")==0)
00470       confirmCallBack(FALSE);
00471     else
00472       confirmCallBack(TRUE);
00473   }
00474   gc_confirm_box_stop();
00475 
00476   return TRUE;
00477   
00478 }