File: dialog.c

package info (click to toggle)
gcompris 12.01-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 259,320 kB
  • sloc: ansic: 86,075; python: 18,474; xml: 12,933; sh: 12,759; makefile: 6,316
file content (125 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download | duplicates (3)
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
/* 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)
{
  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",
		   (GCallback) item_event_ok,
		   dbcb);

  GooCanvasBounds bounds;
  goo_canvas_item_get_bounds(itemDialogText, &bounds);

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


  /* OK Button */
  gc_util_button_text_svg(rootDialogItem,
			  BOARDWIDTH * 0.5,
			  bounds.y2 - 30,
			  "#BUTTON_TEXT",
			  _("OK"),
			  (GCallback) 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;
}