Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/bar.c
blob: bb5ac5b1ae68d62cc5883bba5632aacbddc57768 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* gcompris - bar.c
 *
 * Time-stamp: <2006/08/21 23:27:39 bruno>
 *
 * Copyright (C) 2000-2003 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 2 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, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * The bar button common to each games
 *
 */

#include <string.h>

#include "gcompris.h"
#include "gc_core.h"
#include "gcompris_config.h"
#include "about.h"

#define SOUNDLISTFILE PACKAGE

#define BAR_GAP		15	/* Value used to fill space above and under icons in the bar */
#define NUMBER_OF_ITEMS 10	/* Number of buttons in the bar                              */

static void	 update_exit_button();
static gint	 item_event_bar(GnomeCanvasItem *item, GdkEvent *event, gchar *data);
static void	 bar_reset_sound_id (void);

static gint current_level = -1;
static gint current_flags = -1;
static GnomeCanvasItem *bar_item  = NULL;
static GnomeCanvasItem *exit_item = NULL;
static GnomeCanvasItem *home_item = NULL;
static GnomeCanvasItem *level_item = NULL;
static GnomeCanvasItem *ok_item = NULL;
static GnomeCanvasItem *help_item = NULL;
static GnomeCanvasItem *repeat_item = NULL;
static GnomeCanvasItem *config_item = NULL;
static GnomeCanvasItem *about_item = NULL;
static guint level_handler_id;

static gint sound_play_id = 0;

static void  confirm_quit(gboolean answer);

/*
 * Main entry point
 * ----------------
 *
 */


/*
 * Do all the bar display and register the events
 */
void gc_bar_start (GnomeCanvas *theCanvas)
{
  GcomprisProperties *properties = gc_prop_get();
  GdkPixbuf   *pixmap = NULL;
  GnomeCanvasItem *rootitem;
  gint16           width, height;
  double           zoom;

  width  = BOARDWIDTH;
  height = BARHEIGHT-2;

  bar_reset_sound_id();

  rootitem = \
    gnome_canvas_item_new (gnome_canvas_root(theCanvas),
			   gnome_canvas_group_get_type (),
			   "x", (double)0,
			   "y", (double)0,
			   NULL);

  pixmap = gc_skin_pixmap_load("bar_bg.jpg");
  bar_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				gnome_canvas_pixbuf_get_type (),
				"pixbuf", pixmap,
				"x", (double) 0,
				"y", (double) 0,
				NULL);
  gdk_pixbuf_unref(pixmap);

  // EXIT
  if(properties->disable_quit == 0)
    {
      pixmap = gc_skin_pixmap_load("button_exit.png");
      zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
      exit_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
					 gnome_canvas_pixbuf_get_type (),
					 "pixbuf", pixmap,
					 "x", (double) (width/NUMBER_OF_ITEMS) * 1 -
					 gdk_pixbuf_get_width(pixmap)/2,
					 "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
					 NULL);
      gdk_pixbuf_unref(pixmap);

      gtk_signal_connect(GTK_OBJECT(exit_item), "event",
			 (GtkSignalFunc) item_event_bar,
			 "quit");

      gtk_signal_connect(GTK_OBJECT(exit_item), "event",
			 (GtkSignalFunc) gc_item_focus_event,
			 NULL);
    }

  // HOME
  pixmap = gc_skin_pixmap_load("home.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  home_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				     gnome_canvas_pixbuf_get_type (),
				     "pixbuf", pixmap,
				     "x", (double) (width/NUMBER_OF_ITEMS) * 9 -
				     gdk_pixbuf_get_width(pixmap)/2,
				     "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				     NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(home_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "back");

  gtk_signal_connect(GTK_OBJECT(home_item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);


  // OK
  pixmap = gc_skin_pixmap_load("ok.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  ok_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				   gnome_canvas_pixbuf_get_type (),
				   "pixbuf", pixmap,
				   "x", (double) (width/NUMBER_OF_ITEMS) * 7 -
				   gdk_pixbuf_get_width(pixmap)/2,
				   "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				   NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(ok_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "ok");

  gtk_signal_connect(GTK_OBJECT(ok_item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);


  // LEVEL
  pixmap = gc_skin_pixmap_load("level1.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  level_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				      gnome_canvas_pixbuf_get_type (),
				      "pixbuf", pixmap,
				      "x", (double) (width/NUMBER_OF_ITEMS) * 5 -
				      gdk_pixbuf_get_width(pixmap)/2,
				      "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				      NULL);
  gdk_pixbuf_unref(pixmap);

  current_level = 1;
  gtk_signal_connect(GTK_OBJECT(level_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "level");

  level_handler_id =   gtk_signal_connect(GTK_OBJECT(level_item), "event",
					  (GtkSignalFunc) gc_item_focus_event,
					  NULL);

  // REPEAT
  pixmap = gc_skin_pixmap_load("repeat.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  repeat_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				       gnome_canvas_pixbuf_get_type (),
				       "pixbuf", pixmap,
				       "x", (double) (width/NUMBER_OF_ITEMS) * 6 -
				       gdk_pixbuf_get_width(pixmap)/2,
				       "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				       NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(repeat_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "repeat");

  gtk_signal_connect(GTK_OBJECT(repeat_item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  // HELP
  pixmap = gc_skin_pixmap_load("help.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  help_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				     gnome_canvas_pixbuf_get_type (),
				     "pixbuf", pixmap,
				     "x", (double) (width/NUMBER_OF_ITEMS) * 4 -
				     gdk_pixbuf_get_width(pixmap)/2,
				     "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				     NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(help_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "help");

  gtk_signal_connect(GTK_OBJECT(help_item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  // CONFIG
  if(properties->disable_config == 0)
    {
      pixmap = gc_skin_pixmap_load("config.png");
      zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
      config_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
					   gnome_canvas_pixbuf_get_type (),
					   "pixbuf", pixmap,
					   "x", (double) (width/NUMBER_OF_ITEMS) * 3 -
					   gdk_pixbuf_get_width(pixmap)/2,
					   "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
					   NULL);
      gdk_pixbuf_unref(pixmap);

      gtk_signal_connect(GTK_OBJECT(config_item), "event",
			 (GtkSignalFunc) item_event_bar,
			 "configuration");

      gtk_signal_connect(GTK_OBJECT(config_item), "event",
			 (GtkSignalFunc) gc_item_focus_event,
			 NULL);
    }

  // ABOUT
  pixmap = gc_skin_pixmap_load("about.png");
  zoom = (double)(height-BAR_GAP)/(double)gdk_pixbuf_get_height(pixmap);
  about_item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				     gnome_canvas_pixbuf_get_type (),
				     "pixbuf", pixmap,
				     "x", (double) (width/NUMBER_OF_ITEMS) * 2 -
				     gdk_pixbuf_get_width(pixmap)/2,
				     "y", (double) (height-gdk_pixbuf_get_height(pixmap)*zoom)/2,
				     NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(about_item), "event",
		     (GtkSignalFunc) item_event_bar,
		     "about");

  gtk_signal_connect(GTK_OBJECT(about_item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  // Show them all
  update_exit_button();
  gnome_canvas_item_hide(level_item);
  gnome_canvas_item_hide(ok_item);
  gnome_canvas_item_hide(help_item);
  gnome_canvas_item_hide(repeat_item);
  if(config_item)
    gnome_canvas_item_hide(config_item);
  gnome_canvas_item_hide(about_item);
}


void gc_bar_set_level(GcomprisBoard *gcomprisBoard)
{
  char *str = NULL;
  GdkPixbuf *pixmap = NULL;

  /* Non yet initialized : Something Wrong */
  if(level_item==NULL)
    {
      g_message("in bar_set_level, level_item uninitialized : should not happen\n");
      return;
    }

  if(gcomprisBoard!=NULL)
    {

      str = g_strdup_printf("level%d.png", gcomprisBoard->level);
      pixmap = gc_skin_pixmap_load(str);

      g_free(str);
      /* Warning changing the image needs to free the focus first */
      gc_item_focus_free(level_item, NULL);

      gnome_canvas_item_set (level_item,
      			     "pixbuf", pixmap,
      			     NULL);
      gdk_pixbuf_unref(pixmap);
    }

  current_level=gcomprisBoard->level;
}



/* gc_bar_set_repeat_icon
 *
 * Override the repeat icon to a new one specific to your current board.
 * This must be called before calling gc_bar_set with GC_BAR_REPEAT_ICON
 * the given pixmap is not freed.
 *
 * Next call to gc_bar_set with GC_BAR_REPEAT will restore the default icon.
 *
 */
void
gc_bar_set_repeat_icon (GdkPixbuf *pixmap)
{
  /* Warning changing the image needs to update pixbuf_ref for the focus usage */
  gc_item_focus_free(repeat_item, NULL);
  gnome_canvas_item_set (repeat_item,
			 "pixbuf", pixmap,
			 NULL);
}

/* Setting list of available icons in the control bar */
void
gc_bar_set (const GComprisBarFlags flags)
{

  current_flags = flags;

  update_exit_button();

  if(flags&GC_BAR_LEVEL)
    gnome_canvas_item_show(level_item);
  else
    gnome_canvas_item_hide(level_item);

  if(flags&GC_BAR_OK)
    gnome_canvas_item_show(ok_item);
  else
    gnome_canvas_item_hide(ok_item);

  if(gc_help_has_board(gc_board_get_current()))
    gnome_canvas_item_show(help_item);
  else
    gnome_canvas_item_hide(help_item);

  if(flags&GC_BAR_REPEAT) {
    GdkPixbuf *pixmap;

    /* Set the repeat icon to the original one */
    pixmap = gc_skin_pixmap_load("repeat.png");
    gnome_canvas_item_set (repeat_item,
			   "pixbuf", pixmap,
			   NULL);
    gdk_pixbuf_unref(pixmap);

    gnome_canvas_item_show(repeat_item);
  } else {

    if(flags&GC_BAR_REPEAT_ICON)
      gnome_canvas_item_show(repeat_item);
    else
      gnome_canvas_item_hide(repeat_item);
  }

  if(flags&GC_BAR_CONFIG && config_item)
    gnome_canvas_item_show(config_item);
  else
    gnome_canvas_item_hide(config_item);

  if(flags&GC_BAR_ABOUT)
    gnome_canvas_item_show(about_item);
  else
    gnome_canvas_item_hide(about_item);

  /* FIXME : Workaround for bugged canvas */
  //  gnome_canvas_update_now(gc_board_get_current()->canvas);

}

/* Hide all icons in the control bar
 * or retore the icons to the previous value
 */
void
gc_bar_hide (gboolean hide)
{
  if(hide)
    {
      if(exit_item)
	gnome_canvas_item_hide(exit_item);
      gnome_canvas_item_hide(home_item);
      gnome_canvas_item_hide(level_item);
      gnome_canvas_item_hide(ok_item);
      gnome_canvas_item_hide(help_item);
      gnome_canvas_item_hide(repeat_item);
      if(config_item)
	gnome_canvas_item_hide(config_item);
      gnome_canvas_item_hide(about_item);
    }
  else
    {
      gnome_canvas_item_show(home_item);
      gc_bar_set(current_flags);
    }
}

/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/

/*
 * Display or not the exit button
 */
static void update_exit_button()
{

  if(gc_board_get_current() == NULL)
    return;

  if (gc_board_get_current()->previous_board == NULL)
    {
      /* We are in the upper menu: show it */
      if(exit_item)
	gnome_canvas_item_show(exit_item);
      gnome_canvas_item_hide(home_item);
    }
  else
    {
      if(exit_item)
	gnome_canvas_item_hide(exit_item);
      gnome_canvas_item_show(home_item);
    }
}

/*
 * This is called to play sound
 *
 */
static gint bar_play_sound (gchar *sound)
{
  int policy = gc_sound_policy_get();
  gchar *str;
  gc_sound_policy_set(PLAY_ONLY_IF_IDLE);

  str = g_strdup_printf("sounds/$LOCALE/misc/%s.ogg", sound);

  gc_sound_play_ogg(str, NULL);

  g_free(str);

  gc_sound_policy_set(policy);
  sound_play_id = 0;
  return (FALSE);
}

static void bar_reset_sound_id ()
{
  if(sound_play_id)
    gtk_timeout_remove (sound_play_id);

  sound_play_id=0;
}

/* Callback for the bar operations */
static gint
item_event_bar(GnomeCanvasItem *item, GdkEvent *event, gchar *data)
{
  GcomprisBoard *gcomprisBoard = gc_board_get_current();

  switch (event->type)
    {
    case GDK_ENTER_NOTIFY:
      bar_reset_sound_id();
      sound_play_id = gtk_timeout_add (1000, (GtkFunction) bar_play_sound, data);
      break;
    case GDK_LEAVE_NOTIFY:
      bar_reset_sound_id();
      break;
    case GDK_BUTTON_PRESS:
      bar_reset_sound_id();
      gc_sound_play_ogg ("sounds/bleep.wav", NULL);

      /* This is not perfect clean but it makes it easy to remove the help window
	 by clicking on any button in the bar */
      if(strcmp((char *)data, "help"))
	gc_help_stop ();

      if(!strcmp((char *)data, "ok"))
	{
	  if(gcomprisBoard!=NULL)
	    {
	      if(gcomprisBoard->plugin->ok != NULL)
		{
		  gcomprisBoard->plugin->ok();
		}
	    }
	}
      else if(!strcmp((char *)data, "level"))
	{
	  gint tmp = current_level;

	  current_level++;
	  if(current_level>gcomprisBoard->maxlevel)
	    current_level=1;

	  if(tmp!=current_level)
	    {
	      gchar *str_number;

	      gchar *number_str = g_strdup_printf("%d", current_level);
	      gchar *current_level_str = gc_sound_alphabet(number_str);
	      g_free(number_str);

	      str_number = g_strdup_printf("sounds/$LOCALE/alphabet/%s", current_level_str);

	      gc_sound_play_ogg("sounds/$LOCALE/misc/level.ogg", str_number, NULL);

	      g_free(str_number);
	      g_free(current_level_str);
	    }

	  if(gcomprisBoard->plugin->set_level != NULL)
	    {
	      gcomprisBoard->plugin->set_level(current_level);
	    }
	}
      else if(!strcmp((char *)data, "back"))
	{
	  gc_sound_play_ogg ("gobble", NULL);
	  gc_bar_hide (TRUE);
	  gc_board_stop();
	}
      else if(!strcmp((char *)data, "help"))
	{
	  gc_sound_play_ogg ("gobble", NULL);
	  gc_help_start(gcomprisBoard);
	}
      else if(!strcmp((char *)data, "repeat"))
	{
	  if(gcomprisBoard->plugin->repeat != NULL)
	    {
	      gcomprisBoard->plugin->repeat();
	    }
	}
      else if(!strcmp((char *)data, "configuration"))
	{
	  if(gcomprisBoard->plugin->config_start != NULL)
	    {
	      gcomprisBoard->plugin->config_start(gcomprisBoard,
						  gc_profile_get_current());
	    }
	}
      else if(!strcmp((char *)data, "about"))
	{
	  gc_about_start();
	}
      else if(!strcmp((char *)data, "quit"))
	{
	  gc_confirm_box( _("GCompris confirmation"),
			    _("Are you sure you want to quit?"),
			    _("Yes, I am sure!"),
			    _("No, I want to keep going"),
			    (ConfirmCallBack) confirm_quit);
	}
      break;

    default:
      break;
    }
  return FALSE;

}

static void
confirm_quit(gboolean answer)
{
  if (answer)
    gc_exit();
}