Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/dialog.c
blob: 2a059e9da62fd763bdee4c84f538146fc7cbd368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* gcompris - dialog.c
 *
 * Copyright (C) 2000, 2008 Bruno Coudoin
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "gcompris.h"

static GooCanvasItem *rootDialogItem = NULL;
static GooCanvasItem *itemDialogText = NULL;
static gboolean item_event_ok (GooCanvasItem  *item,
			       GooCanvasItem  *target,
			       GdkEventButton *event,
			       DialogBoxCallBack dbcb);

/*
 * Close the dialog box if it was open. It not, do nothing.
 */
void gc_dialog_close() {

  /* If we already running delete the previous one */
  if(rootDialogItem) {
    goo_canvas_item_remove(rootDialogItem);
  }

  rootDialogItem = NULL;

}

/*
 * Display a dialog box and an OK button
 * When the box is closed, the given callback is called if any
 */
void gc_dialog(gchar *str, DialogBoxCallBack dbcb)
{
  gint y_start;
  gint x_start;

  g_warning("Dialog=%s\n", str);

  /* If we are already running do nothing */
  if(rootDialogItem) {
    g_warning("Cannot run a dialog box, one is already running. Message = %s\n", str);
    return;
  }

  if(gc_get_canvas() == NULL)
    {
      g_warning("Cannot run a dialog box, canvas not initialized yet = %s\n", str);
      return;
    }

  /* First pause the board */
  gc_board_pause(TRUE);

  gc_bar_hide(TRUE);

  rootDialogItem = goo_canvas_group_new (goo_canvas_get_root_item(gc_get_canvas()),
					 NULL);

  itemDialogText = goo_canvas_svg_new (rootDialogItem,
				       gc_skin_rsvg_get(),
				       "svg-id", "#DIALOG",
				       NULL);
  g_signal_connect(itemDialogText, "button_press_event",
		   (GtkSignalFunc) item_event_ok,
		   dbcb);

  GooCanvasBounds bounds;
  goo_canvas_item_get_bounds(itemDialogText, &bounds);
  x_start = bounds.x1;
  y_start = bounds.y1;

  goo_canvas_text_new (rootDialogItem,
		       str,
		       BOARDWIDTH / 2,
		       (bounds.y2 - bounds.y1)/2 - 100,
		       BOARDWIDTH / 2,
		       GTK_ANCHOR_CENTER,
		       "alignment", PANGO_ALIGN_CENTER,
		       NULL);


  /* OK Button */
  gc_util_button_text_svg(rootDialogItem,
			  BOARDWIDTH * 0.5,
			  bounds.y2 - 30,
			  "#BUTTON_TEXT",
			  _("OK"),
			  (GtkSignalFunc) item_event_ok,
			  dbcb);
}

/* Callback for the bar operations */
static gboolean
item_event_ok (GooCanvasItem  *item,
	       GooCanvasItem  *target,
	       GdkEventButton *event,
	       DialogBoxCallBack dbcb)
{
  if(rootDialogItem)
    gc_dialog_close();

  gc_sound_play_ogg ("sounds/bleep.wav", NULL);

  /* restart the board */
  gc_board_pause(FALSE);

  gc_bar_hide(FALSE);

  if(dbcb != NULL)
    dbcb();

  return TRUE;
}