Back to index

gcompris  8.2.2
dialog.c
Go to the documentation of this file.
00001 /* gcompris - dialog.c
00002  *
00003  * Time-stamp: <2006/08/20 09:52:47 bruno>
00004  *
00005  * Copyright (C) 2000-2006 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 
00022 #include <string.h>
00023 
00024 #include "gcompris.h"
00025 
00026 static GnomeCanvasGroup *rootDialogItem = NULL;
00027 static GnomeCanvasItem *itemDialogText = NULL;
00028 static gint item_event_ok(GnomeCanvasItem *item, GdkEvent *event, DialogBoxCallBack dbcb);
00029 
00030 typedef void (*sighandler_t)(int);
00031 
00032 
00033 /*
00034  * Close the dialog box if it was open. It not, do nothing.
00035  */
00036 void gc_dialog_close() {
00037 
00038   /* If we already running delete the previous one */
00039   if(rootDialogItem) {
00040     /* WORKAROUND: There is a bug in the richtex item and we need to remove it first */
00041     while (g_idle_remove_by_data (itemDialogText));
00042     gtk_object_destroy (GTK_OBJECT(itemDialogText));
00043     itemDialogText = NULL;
00044 
00045     gtk_object_destroy(GTK_OBJECT(rootDialogItem));
00046   }
00047 
00048   rootDialogItem = NULL;
00049 
00050 }
00051 
00052 /*
00053  * Display a dialog box and an OK button
00054  * When the box is closed, the given callback is called if any
00055  */
00056 void gc_dialog(gchar *str, DialogBoxCallBack dbcb)
00057 {
00058   GnomeCanvasItem *item_text   = NULL;
00059   GnomeCanvasItem *item_text_ok   = NULL;
00060   GdkPixbuf       *pixmap_dialog = NULL;
00061   GtkTextIter      iter_start, iter_end;
00062   GtkTextBuffer   *buffer;
00063   GtkTextTag      *txt_tag;
00064 
00065   g_warning("Dialog=%s\n", str);
00066 
00067   /* If we are already running do nothing */
00068   if(rootDialogItem) {
00069     g_warning("Cannot run a dialog box, one is already running. Message = %s\n", str);
00070     return;
00071   }
00072 
00073   /* First pause the board */
00074   gc_board_pause(TRUE);
00075 
00076   gc_bar_hide(TRUE);
00077 
00078   rootDialogItem = \
00079     GNOME_CANVAS_GROUP(gnome_canvas_item_new (gnome_canvas_root(gc_get_canvas()),
00080                                          gnome_canvas_group_get_type (),
00081                                          "x", (double) 0,
00082                                          "y", (double) 0,
00083                                          NULL));
00084 
00085   pixmap_dialog = gc_skin_pixmap_load("dialogbox.png");
00086 
00087   itemDialogText = gnome_canvas_item_new (rootDialogItem,
00088                                    gnome_canvas_pixbuf_get_type (),
00089                                    "pixbuf", pixmap_dialog,
00090                                    "x", (double) (BOARDWIDTH - gdk_pixbuf_get_width(pixmap_dialog))/2,
00091                                    "y", (double) (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap_dialog))/2,
00092                                   NULL);
00093 
00094   /* OK Text */
00095   item_text_ok = gnome_canvas_item_new (rootDialogItem,
00096                                    gnome_canvas_text_get_type (),
00097                                    "text", _("OK"),
00098                                    "font", gc_skin_font_title,
00099                                    "x", (double)  BOARDWIDTH*0.5,
00100                                    "y", (double)  (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap_dialog))/2 +
00101                                    gdk_pixbuf_get_height(pixmap_dialog) - 35,
00102                                    "anchor", GTK_ANCHOR_CENTER,
00103                                    "fill_color_rgba", gc_skin_color_text_button,
00104                                    "weight", PANGO_WEIGHT_HEAVY,
00105                             NULL);
00106 
00107   gdk_pixbuf_unref(pixmap_dialog);
00108 
00109   gtk_signal_connect(GTK_OBJECT(itemDialogText), "event",
00110                    (GtkSignalFunc) item_event_ok,
00111                    dbcb);
00112 
00113   item_text = gnome_canvas_item_new (rootDialogItem,
00114                                  gnome_canvas_rich_text_get_type (),
00115                                  "x", (double) BOARDWIDTH/2,
00116                                  "y", (double) 100.0,
00117                                  "width", (double)BOARDWIDTH-260.0,
00118                                  "height", 400.0,
00119                                  "anchor", GTK_ANCHOR_NORTH,
00120                                  "justification", GTK_JUSTIFY_CENTER,
00121                                  "grow_height", FALSE,
00122                                  "cursor_visible", FALSE,
00123                                  "cursor_blink", FALSE,
00124                                  "editable", FALSE,
00125                                  NULL);
00126 
00127   gnome_canvas_item_set (item_text,
00128                       "text", str,
00129                       NULL);
00130 
00131   buffer  = gnome_canvas_rich_text_get_buffer(GNOME_CANVAS_RICH_TEXT(item_text));
00132   txt_tag = gtk_text_buffer_create_tag(buffer, NULL,
00133                                    "font",       gc_skin_font_board_medium,
00134                                    "foreground", "blue",
00135                                    "family-set", TRUE,
00136                                    NULL);
00137   gtk_text_buffer_get_end_iter(buffer, &iter_end);
00138   gtk_text_buffer_get_start_iter(buffer, &iter_start);
00139   gtk_text_buffer_apply_tag(buffer, txt_tag, &iter_start, &iter_end);
00140 
00141   gtk_signal_connect(GTK_OBJECT(item_text), "event",
00142                    (GtkSignalFunc) item_event_ok,
00143                    dbcb);
00144   gtk_signal_connect(GTK_OBJECT(item_text_ok), "event",
00145                    (GtkSignalFunc) item_event_ok,
00146                    dbcb);
00147 
00148 }
00149 
00150 /* Callback for the bar operations */
00151 static gint
00152 item_event_ok(GnomeCanvasItem *item, GdkEvent *event, DialogBoxCallBack dbcb)
00153 {
00154   switch (event->type)
00155     {
00156     case GDK_ENTER_NOTIFY:
00157       break;
00158     case GDK_LEAVE_NOTIFY:
00159       break;
00160     case GDK_BUTTON_PRESS:
00161       if(rootDialogItem)
00162        gc_dialog_close();
00163 
00164       /* restart the board */
00165       gc_board_pause(FALSE);
00166 
00167       gc_bar_hide(FALSE);
00168 
00169       if(dbcb != NULL)
00170        dbcb();
00171 
00172     default:
00173       break;
00174     }
00175   return TRUE;
00176 }