Back to index

gcompris  8.2.2
help.c
Go to the documentation of this file.
00001 /* gcompris - help.c
00002  *
00003  * Time-stamp: <2006/08/21 23:26:30 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 
00026 #include <string.h>
00027 
00028 #include "gcompris.h"
00029 #include "gc_core.h"
00030 
00031 #define SOUNDLISTFILE PACKAGE
00032 
00033 #define GAP_TO_BUTTON -20
00034 
00035 static gint    item_event_help(GnomeCanvasItem *item, GdkEvent *event, gpointer data);
00036 static void    select_item(GnomeCanvasItem *item, GnomeCanvasItem *item_text);
00037 static void    set_content(char *text);
00038 
00039 static gboolean help_displayed                   = FALSE;
00040 
00041 static GnomeCanvasItem *rootitem          = NULL;
00042 
00043 static gchar *prerequisite                = NULL;
00044 static gchar *goal                        = NULL;
00045 static gchar *manual                      = NULL;
00046 static gchar *credit                      = NULL;
00047 
00048 static GnomeCanvasItem *item_prerequisite = NULL;
00049 static GnomeCanvasItem *item_goal         = NULL;
00050 static GnomeCanvasItem *item_manual              = NULL;
00051 static GnomeCanvasItem *item_credit              = NULL;
00052 
00053 static GnomeCanvasItem *item_prerequisite_text  = NULL;
00054 static GnomeCanvasItem *item_goal_text          = NULL;
00055 static GnomeCanvasItem *item_manual_text        = NULL;
00056 static GnomeCanvasItem *item_credit_text        = NULL;
00057 
00058 static GnomeCanvasItem *item_selected            = NULL;
00059 static GnomeCanvasItem *item_selected_text       = NULL;
00060 
00061 static GtkTextBuffer   *buffer_content;
00062 /*
00063  * Main entry point
00064  * ----------------
00065  *
00066  */
00067 
00072 gboolean gc_help_has_board (GcomprisBoard *gcomprisBoard)
00073 {
00074 
00075   prerequisite       = gcomprisBoard->prerequisite;
00076   goal        = gcomprisBoard->goal;
00077   manual      = gcomprisBoard->manual;
00078   credit      = gcomprisBoard->credit;
00079   return(prerequisite!=NULL || goal!=NULL || manual!=NULL || credit!=NULL);
00080 }
00081 
00082 /*
00083  * Do all the help display and register the events
00084  */
00085 
00086 void gc_help_start (GcomprisBoard *gcomprisBoard)
00087 {
00088 
00089   GdkPixbuf   *pixmap = NULL;
00090   GnomeCanvasItem *item, *item2;
00091   gint y = 0;
00092   gint y_start = 0;
00093   gint x_start = 0;
00094   gchar   *name = NULL;
00095   gchar   *text_to_display = NULL;
00096 
00097   if(rootitem)
00098     return;
00099 
00100   gc_board_pause(TRUE);
00101 
00102   item_selected = NULL;
00103   item_selected_text = NULL;
00104 
00105   name = gcomprisBoard->title;
00106   gc_help_has_board(gcomprisBoard);
00107 
00108   rootitem = \
00109     gnome_canvas_item_new (gnome_canvas_root(gc_get_canvas()),
00110                         gnome_canvas_group_get_type (),
00111                         "x", (double)0,
00112                         "y", (double)0,
00113                         NULL);
00114 
00115   pixmap = gc_skin_pixmap_load("help_bg.png");
00116   y_start = (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap))/2;
00117   x_start = (BOARDWIDTH - gdk_pixbuf_get_width(pixmap))/2;
00118   item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00119                             gnome_canvas_pixbuf_get_type (),
00120                             "pixbuf", pixmap,
00121                             "x", (double) x_start,
00122                             "y", (double) y_start,
00123                             NULL);
00124   y = BOARDHEIGHT - (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap))/2;
00125   gdk_pixbuf_unref(pixmap);
00126 
00127   y_start += 14;
00128   if(gcomprisBoard->section && gcomprisBoard->name) {
00129     text_to_display = g_strdup_printf("%s/%s", gcomprisBoard->section, gcomprisBoard->name);
00130      gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00131                          gnome_canvas_text_get_type (),
00132                          "text", text_to_display,
00133                          "font", gc_skin_font_board_tiny,
00134                          "x", (double) BOARDWIDTH*0.10 + 1.0,
00135                          "y", (double) y_start + 1.0,
00136                          "anchor", GTK_ANCHOR_NW,
00137                          "fill_color_rgba", gc_skin_color_shadow,
00138                          NULL);
00139      gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00140                          gnome_canvas_text_get_type (),
00141                          "text", text_to_display,
00142                          "font", gc_skin_font_board_tiny,
00143                          "x", (double) BOARDWIDTH*0.10,
00144                          "y", (double) y_start,
00145                          "anchor", GTK_ANCHOR_NW,
00146                          "fill_color_rgba", gc_skin_color_title,
00147                          NULL);
00148     g_free(text_to_display);
00149   }
00150 
00151   y_start += 35;
00152   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00153                       gnome_canvas_text_get_type (),
00154                       "text", name,
00155                       "font", gc_skin_font_title,
00156                       "x", (double) BOARDWIDTH/2 + 1.0,
00157                       "y", (double) y_start + 1.0,
00158                       "anchor", GTK_ANCHOR_CENTER,
00159                       "fill_color_rgba", gc_skin_color_shadow,
00160                       "weight", PANGO_WEIGHT_HEAVY,
00161                       NULL);
00162   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00163                       gnome_canvas_text_get_type (),
00164                       "text", name,
00165                       "font", gc_skin_font_title,
00166                       "x", (double) BOARDWIDTH/2,
00167                       "y", (double) y_start,
00168                       "anchor", GTK_ANCHOR_CENTER,
00169                       "fill_color_rgba", gc_skin_color_title,
00170                       "weight", PANGO_WEIGHT_HEAVY,
00171                       NULL);
00172 
00173 
00174   y_start += 120;
00175 
00176   pixmap = gc_skin_pixmap_load("button_up.png");
00177 
00178   // Prerequisite Button
00179   if(prerequisite)
00180     {
00181       item_prerequisite = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00182                                            gnome_canvas_pixbuf_get_type (),
00183                                            "pixbuf", pixmap,
00184                                            "x", (double) (BOARDWIDTH*0.2) - gdk_pixbuf_get_width(pixmap)/2,
00185                                            "y", (double) y_start - gdk_pixbuf_get_height(pixmap) - 10,
00186                                            NULL);
00187 
00188       gtk_signal_connect(GTK_OBJECT(item_prerequisite), "event",
00189                       (GtkSignalFunc) item_event_help,
00190                       "prerequisite");
00191 
00192       item_prerequisite_text = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00193                                 gnome_canvas_text_get_type (),
00194                                 "text", _("Prerequisite"),
00195                                 "font", gc_skin_font_content,
00196                                 "x", (double)  BOARDWIDTH*0.20,
00197                                 "y", (double)  y_start - gdk_pixbuf_get_height(pixmap)  + GAP_TO_BUTTON,
00198                                 "anchor", GTK_ANCHOR_CENTER,
00199                                 "fill_color_rgba", gc_skin_get_color("gcompris/helpunselect"),
00200                                 NULL);
00201       gtk_signal_connect(GTK_OBJECT(item_prerequisite_text), "event",
00202                       (GtkSignalFunc) item_event_help,
00203                       "prerequisite");
00204     }
00205 
00206   // Goal Button
00207   if(goal)
00208     {
00209       item_goal = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00210                                     gnome_canvas_pixbuf_get_type (),
00211                                     "pixbuf", pixmap,
00212                                     "x", (double) (BOARDWIDTH*0.4) - gdk_pixbuf_get_width(pixmap)/2,
00213                                     "y", (double) y_start - gdk_pixbuf_get_height(pixmap) - 10,
00214                                     NULL);
00215 
00216       gtk_signal_connect(GTK_OBJECT(item_goal), "event",
00217                       (GtkSignalFunc) item_event_help,
00218                       "goal");
00219 
00220       item_goal_text = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00221                                 gnome_canvas_text_get_type (),
00222                                 "text", _("Goal"),
00223                                 "font", gc_skin_font_content,
00224                                 "x", (double)  BOARDWIDTH*0.4,
00225                                 "y", (double)  y_start - gdk_pixbuf_get_height(pixmap)  + GAP_TO_BUTTON,
00226                                 "anchor", GTK_ANCHOR_CENTER,
00227                                 "fill_color_rgba", gc_skin_get_color("gcompris/helpunselect"),
00228                                 NULL);
00229       gtk_signal_connect(GTK_OBJECT(item_goal_text), "event",
00230                       (GtkSignalFunc) item_event_help,
00231                       "goal");
00232     }
00233 
00234   // Manual Button
00235   if(manual)
00236     {
00237       item_manual = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00238                                       gnome_canvas_pixbuf_get_type (),
00239                                       "pixbuf", pixmap,
00240                                       "x", (double) (BOARDWIDTH*0.6) - gdk_pixbuf_get_width(pixmap)/2,
00241                                       "y", (double) y_start - gdk_pixbuf_get_height(pixmap) - 10,
00242                                       NULL);
00243 
00244       gtk_signal_connect(GTK_OBJECT(item_manual), "event",
00245                       (GtkSignalFunc) item_event_help,
00246                       "manual");
00247 
00248       item_manual_text = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00249                                 gnome_canvas_text_get_type (),
00250                                 "text", _("Manual"),
00251                                 "font", gc_skin_font_content,
00252                                 "x", (double)  BOARDWIDTH*0.6,
00253                                 "y", (double)  y_start - gdk_pixbuf_get_height(pixmap)  + GAP_TO_BUTTON,
00254                                 "anchor", GTK_ANCHOR_CENTER,
00255                                 "fill_color_rgba", gc_skin_get_color("gcompris/helpunselect"),
00256                                 NULL);
00257       gtk_signal_connect(GTK_OBJECT(item_manual_text), "event",
00258                       (GtkSignalFunc) item_event_help,
00259                       "manual");
00260     }
00261 
00262   // Credit Button
00263   if(credit)
00264     {
00265       item_credit = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00266                                       gnome_canvas_pixbuf_get_type (),
00267                                       "pixbuf", pixmap,
00268                                       "x", (double) (BOARDWIDTH*0.8) - gdk_pixbuf_get_width(pixmap)/2,
00269                                       "y", (double) y_start - gdk_pixbuf_get_height(pixmap) - 10,
00270                                       NULL);
00271 
00272       gtk_signal_connect(GTK_OBJECT(item_credit), "event",
00273                       (GtkSignalFunc) item_event_help,
00274                       "credit");
00275 
00276       item_credit_text = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00277                                 gnome_canvas_text_get_type (),
00278                                 "text", _("Credit"),
00279                                 "font", gc_skin_font_content,
00280                                 "x", (double)  BOARDWIDTH*0.8,
00281                                 "y", (double)  y_start - gdk_pixbuf_get_height(pixmap)  + GAP_TO_BUTTON,
00282                                 "anchor", GTK_ANCHOR_CENTER,
00283                                 "fill_color_rgba", gc_skin_get_color("gcompris/helpunselect"),
00284                                 NULL);
00285       gtk_signal_connect(GTK_OBJECT(item_credit_text), "event",
00286                       (GtkSignalFunc) item_event_help,
00287                       "credit");
00288     }
00289 
00290   gdk_pixbuf_unref(pixmap);
00291 
00292   // CONTENT
00293 
00294   // default text to display
00295 
00296   if(prerequisite)
00297     {
00298       text_to_display = prerequisite;
00299       select_item(item_prerequisite, item_prerequisite_text);
00300     }
00301   else if(goal)
00302     {
00303       text_to_display = goal;
00304       select_item(item_goal, item_goal_text);
00305     }
00306   else if(manual)
00307     {
00308       text_to_display = manual;
00309       select_item(item_manual, item_manual_text);
00310     }
00311   else if(credit)
00312     {
00313       text_to_display = credit;
00314       select_item(item_credit, item_credit_text);
00315     }
00316 
00317   y_start += 5;
00318 
00319   /* Create a scrolled area for the text content */
00320   GtkWidget *view;
00321   GtkWidget *sw;
00322   view = gtk_text_view_new ();
00323   gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
00324   gtk_text_view_set_editable(GTK_TEXT_VIEW (view), FALSE);
00325   gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW (view), FALSE);
00326 
00327   PangoFontDescription *font_desc;
00328   font_desc = pango_font_description_from_string (gc_skin_font_content);
00329   gtk_widget_modify_font (view, font_desc);
00330   pango_font_description_free (font_desc);
00331 
00332   GdkColor fg_color;
00333   GdkColor bg_color;
00334   gc_skin_get_gdkcolor("gcompris/helpfg", &fg_color);
00335   gc_skin_get_gdkcolor("gcompris/helpbg", &bg_color);
00336   gtk_widget_modify_base(view, GTK_STATE_NORMAL, &bg_color);
00337   gtk_widget_modify_text(view, GTK_STATE_NORMAL, &fg_color);
00338 
00339   buffer_content = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
00340   sw = gtk_scrolled_window_new (NULL, NULL);
00341   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
00342                               GTK_POLICY_AUTOMATIC,
00343                               GTK_POLICY_AUTOMATIC);
00344   gtk_container_add (GTK_CONTAINER (sw), view);
00345 
00346   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00347                       gnome_canvas_widget_get_type (),
00348                       "widget", GTK_WIDGET(sw),
00349                       "x", (double)  x_start + 40,
00350                       "y", (double)  y_start,
00351                       "width",  618.0,
00352                       "height", 280.0,
00353                       NULL);
00354   gtk_widget_show_all (sw);
00355 
00356   set_content(text_to_display);
00357   // OK
00358   pixmap = gc_skin_pixmap_load("button_large.png");
00359 
00360   item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00361                             gnome_canvas_pixbuf_get_type (),
00362                             "pixbuf", pixmap,
00363                             "x", (double) (BOARDWIDTH*0.5) - gdk_pixbuf_get_width(pixmap)/2,
00364                             "y", (double) y - gdk_pixbuf_get_height(pixmap) - 5,
00365                             NULL);
00366 
00367   gtk_signal_connect(GTK_OBJECT(item), "event",
00368                    (GtkSignalFunc) item_event_help,
00369                    "ok");
00370   gtk_signal_connect(GTK_OBJECT(item), "event",
00371                    (GtkSignalFunc) gc_item_focus_event,
00372                    NULL);
00373 
00374   gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00375                       gnome_canvas_text_get_type (),
00376                       "text", _("OK"),
00377                       "font", gc_skin_font_title,
00378                       "x", (double)  BOARDWIDTH*0.5 + 1.0,
00379                       "y", (double)  y - gdk_pixbuf_get_height(pixmap) + 20 + 1.0,
00380                       "anchor", GTK_ANCHOR_CENTER,
00381                       "fill_color_rgba", gc_skin_color_shadow,
00382                       "weight", PANGO_WEIGHT_HEAVY,
00383                       NULL);
00384   item2 = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
00385                              gnome_canvas_text_get_type (),
00386                              "text", _("OK"),
00387                              "font", gc_skin_font_title,
00388                              "x", (double)  BOARDWIDTH*0.5,
00389                              "y", (double)  y - gdk_pixbuf_get_height(pixmap) + 20,
00390                              "anchor", GTK_ANCHOR_CENTER,
00391                              "fill_color_rgba", gc_skin_color_text_button,
00392                              "weight", PANGO_WEIGHT_HEAVY,
00393                             NULL);
00394   gtk_signal_connect(GTK_OBJECT(item2), "event",
00395                    (GtkSignalFunc) item_event_help,
00396                    "ok");
00397   gtk_signal_connect(GTK_OBJECT(item2), "event",
00398                    (GtkSignalFunc) gc_item_focus_event,
00399                    item);
00400   gdk_pixbuf_unref(pixmap);
00401 
00402   gc_bar_hide(TRUE);
00403 
00404   help_displayed = TRUE;
00405 }
00406 
00407 /*
00408  * Remove the displayed help.
00409  * Do nothing if none is currently being dislayed
00410  */
00411 void gc_help_stop ()
00412 {
00413   if(help_displayed)
00414     {
00415       // Destroy the help box
00416       if(rootitem!=NULL)
00417        {
00418          gtk_object_destroy(GTK_OBJECT(rootitem));
00419          rootitem = NULL;
00420        }
00421       gc_board_pause(FALSE);
00422     }
00423 
00424   gc_bar_hide(FALSE);
00425   help_displayed = FALSE;
00426 }
00427 
00428 
00429 
00430 /*-------------------------------------------------------------------------------*/
00431 /*-------------------------------------------------------------------------------*/
00432 /*-------------------------------------------------------------------------------*/
00433 /*-------------------------------------------------------------------------------*/
00434 
00435 static void select_item(GnomeCanvasItem *item, GnomeCanvasItem *item_text)
00436 {
00437   GdkPixbuf   *pixmap = NULL;
00438 
00439   if(item_selected)
00440     {
00441       pixmap = gc_skin_pixmap_load("button_up.png");
00442       /* Warning changing the image needs to update pixbuf_ref for the focus usage */
00443       gc_item_focus_free(item_selected, NULL);
00444       gnome_canvas_item_set(item_selected,
00445                          "pixbuf", pixmap,
00446                          NULL);
00447       gnome_canvas_item_set(item_selected_text,
00448                          "fill_color_rgba", gc_skin_get_color("gcompris/helpunselect"),
00449                          NULL);
00450 
00451       gdk_pixbuf_unref(pixmap);
00452     }
00453 
00454   pixmap = gc_skin_pixmap_load("button_up_selected.png");
00455   /* Warning changing the image needs to update pixbuf_ref for the focus usage */
00456   gc_item_focus_free(item, NULL);
00457   gnome_canvas_item_set(item,
00458                      "pixbuf", pixmap,
00459                      NULL);
00460   gdk_pixbuf_unref(pixmap);
00461   gnome_canvas_item_set(item_text,
00462                      "fill_color_rgba", gc_skin_get_color("gcompris/helpselect"),
00463                      NULL);
00464   item_selected = item;
00465   item_selected_text = item_text;
00466 }
00467 
00468 /* Apply the style to the given RichText item  */
00469 static void set_content(gchar *text)
00470 {
00471   gtk_text_buffer_set_text (buffer_content, "", 0);
00472   gtk_text_buffer_insert_at_cursor(buffer_content, text, -1);
00473 }
00474 
00475 /* Callback for the bar operations */
00476 static gint
00477 item_event_help(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
00478 {
00479 
00480   switch (event->type)
00481     {
00482     case GDK_ENTER_NOTIFY:
00483       break;
00484     case GDK_LEAVE_NOTIFY:
00485       break;
00486     case GDK_BUTTON_PRESS:
00487       if(!strcmp((char *)data, "ok"))
00488        {
00489          gc_help_stop();
00490        }
00491       else if(!strcmp((char *)data, "prerequisite"))
00492        {
00493          select_item(item_prerequisite, item_prerequisite_text);
00494          set_content(prerequisite);
00495        }
00496       else if(!strcmp((char *)data, "goal"))
00497        {
00498          select_item(item_goal, item_goal_text);
00499          set_content(goal);
00500        }
00501       else if(!strcmp((char *)data, "manual"))
00502        {
00503          select_item(item_manual, item_manual_text);
00504          set_content(manual);
00505        }
00506       else if(!strcmp((char *)data, "credit"))
00507        {
00508          select_item(item_credit, item_credit_text);
00509          set_content(credit);
00510        }
00511     default:
00512       break;
00513     }
00514   return FALSE;
00515 
00516 }