~ubuntu-branches/ubuntu/trusty/gcompris/trusty

« back to all changes in this revision

Viewing changes to src/gcompris/gcompris.c

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-21 16:16:27 UTC
  • Revision ID: james.westby@ubuntu.com-20020421161627-s07yahahm817qxs6
Tags: upstream-1.0.3
ImportĀ upstreamĀ versionĀ 1.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gcompris - gcompris.c
 
2
 *
 
3
 * Time-stamp: <2002/04/13 23:54:29 bruno>
 
4
 *
 
5
 * Copyright (C) 2000,2001 Bruno Coudoin
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
#include <signal.h>
 
23
 
 
24
#include "gcompris.h"
 
25
#include <popt-gnome.h>
 
26
 
 
27
#include "cursor.h"
 
28
 
 
29
GtkWidget *window;
 
30
GtkWidget *drawing_area;
 
31
GnomeCanvas *canvas;
 
32
GnomeCanvas *canvas_bar;
 
33
GnomeCanvas *canvas_bg;
 
34
 
 
35
//static gint pause_board_cb (GtkWidget *widget, gpointer data);
 
36
static void quit_cb (GtkWidget *widget, gpointer data);
 
37
static gint board_widget_key_press_callback (GtkWidget   *widget,
 
38
                                            GdkEventKey *event,
 
39
                                            gpointer     client_data);
 
40
 
 
41
GcomprisProperties      *properties = NULL;
 
42
GcomprisBoard           *gcomprisBoardMenu = NULL;
 
43
 
 
44
/****************************************************************************/
 
45
/* Some constants.  */
 
46
 
 
47
static GnomeCanvasItem *backgroundimg = NULL;
 
48
static gchar           *gcompris_locale = NULL;
 
49
 
 
50
/****************************************************************************/
 
51
/* Command line params */
 
52
 
 
53
#define P_FULLSCREEN    101
 
54
#define P_WINDOW        102
 
55
#define P_SOUND         103
 
56
#define P_MUTE          104
 
57
#define P_VERSION       105
 
58
#define P_CURSOR        106
 
59
 
 
60
struct poptOption command_line[] = {
 
61
  {"fullscreen", 'f', POPT_ARGFLAG_ONEDASH, NULL, P_FULLSCREEN,
 
62
   N_("run gcompris in fullscreen mode.")},
 
63
  {"window", 'w', POPT_ARGFLAG_ONEDASH, NULL, P_WINDOW,
 
64
   N_("run gcompris in window mode.")},
 
65
  {"sound", 's', POPT_ARGFLAG_ONEDASH, NULL, P_SOUND,
 
66
   N_("run gcompris with sound enabled.")},
 
67
  {"mute", 'm', POPT_ARGFLAG_ONEDASH, NULL, P_MUTE,
 
68
   N_("run gcompris without sound.")},
 
69
  {"cursor", 'c', POPT_ARGFLAG_ONEDASH, NULL, P_CURSOR,
 
70
   N_("run gcompris with the default gnome cursor.")},
 
71
  {"version", 'v', POPT_ARGFLAG_ONEDASH, NULL, P_VERSION,
 
72
   N_("Prints the version of " PACKAGE)},
 
73
  POPT_AUTOHELP {NULL, 0, 0, NULL, 0}
 
74
};
 
75
 
 
76
 
 
77
 
 
78
/****************************************************************************/
 
79
 
 
80
static gint
 
81
board_widget_key_press_callback (GtkWidget   *widget,
 
82
                                GdkEventKey *event,
 
83
                                gpointer     client_data)
 
84
{
 
85
 
 
86
  switch (event->keyval)
 
87
    {
 
88
    case GDK_Escape:
 
89
      gcompris_help_stop();
 
90
      board_stop();
 
91
      //      quit_cb(NULL, NULL);
 
92
      return TRUE;
 
93
    case GDK_KP_Enter:
 
94
    case GDK_Return:
 
95
      if (get_current_board_plugin()!=NULL && get_current_board_plugin()->ok)
 
96
        {
 
97
          get_current_board_plugin()->ok ();
 
98
        }
 
99
      return TRUE;
 
100
    default:
 
101
      /* If the board needs to receive key pressed */
 
102
      if (get_current_board_plugin()!=NULL && get_current_board_plugin()->key_press)
 
103
        {
 
104
          return(get_current_board_plugin()->key_press (event->keyval));
 
105
        }
 
106
    }
 
107
 
 
108
  return FALSE;
 
109
};
 
110
 
 
111
/**
 
112
 * Return the main canvas we run in
 
113
 */
 
114
GnomeCanvas *gcompris_get_canvas()
 
115
{
 
116
  return canvas;
 
117
}
 
118
 
 
119
GtkWidget *gcompris_get_window()
 
120
{
 
121
  return window;
 
122
}
 
123
 
 
124
 
 
125
GnomeCanvasItem *gcompris_set_background(GnomeCanvasGroup *parent, gchar *file)
 
126
{
 
127
  GdkPixbuf *background_pixmap = NULL;
 
128
 
 
129
  background_pixmap = gcompris_load_pixmap (file);
 
130
 
 
131
  if(backgroundimg)
 
132
    gtk_object_destroy (GTK_OBJECT(backgroundimg));
 
133
 
 
134
  backgroundimg=gnome_canvas_item_new (parent,
 
135
                                       gnome_canvas_pixbuf_get_type (),
 
136
                                       "pixbuf", background_pixmap, 
 
137
                                       "x", 0.0,
 
138
                                       "y", 0.0,
 
139
                                       "width", (double) BOARDWIDTH,
 
140
                                       "height", (double) BOARDHEIGHT,
 
141
                                       NULL);
 
142
  gnome_canvas_item_lower_to_bottom(backgroundimg);
 
143
 
 
144
  gdk_pixbuf_unref(background_pixmap);
 
145
 
 
146
  return (backgroundimg);
 
147
}
 
148
 
 
149
static void init_background()
 
150
{
 
151
  double xratio, yratio;
 
152
  GtkWidget *vbox;
 
153
 
 
154
  yratio=gdk_screen_height()/(float)(BOARDHEIGHT+BARHEIGHT);
 
155
  xratio=gdk_screen_width()/(float)BOARDWIDTH;
 
156
    printf("The gdk_screen_width()=%f gdk_screen_height()=%f\n",
 
157
         (double)gdk_screen_width(), (double)gdk_screen_height());
 
158
    printf("The xratio=%f yratio=%f\n", xratio, yratio);
 
159
 
 
160
  yratio=xratio=MIN(xratio, yratio);
 
161
 
 
162
  /* The canvas does not look pretty when resized above 1 ratio. Avoid that */
 
163
  xratio=MIN(1.0, xratio);
 
164
 
 
165
  printf("Calculated x ratio xratio=%f\n", xratio);
 
166
  
 
167
  /* Background area if ratio above 1 */
 
168
  if(xratio>=1.0 && properties->fullscreen)
 
169
    {
 
170
      /* First, Remove the gnome crash dialog because it locks the user when in full screen */
 
171
      signal(SIGSEGV, SIG_DFL);
 
172
 
 
173
      gnome_canvas_set_scroll_region (canvas_bg,
 
174
                                      0, 0,
 
175
                                      gdk_screen_width(),
 
176
                                      gdk_screen_height());
 
177
      
 
178
      gtk_widget_set_usize (GTK_WIDGET(canvas_bg), gdk_screen_width(), gdk_screen_height());
 
179
      
 
180
      /* Create a black box for the background */
 
181
      gnome_canvas_item_new (gnome_canvas_root(canvas_bg),
 
182
                             gnome_canvas_rect_get_type (),
 
183
                             "x1", (double) 0,
 
184
                             "y1", (double) 0,
 
185
                             "x2", (double) gdk_screen_width(),
 
186
                             "y2", (double) gdk_screen_height(),
 
187
                             "fill_color", "black",
 
188
                             NULL);
 
189
    }
 
190
 
 
191
  /* Create a vertical box in which I put first the play board area, then the button bar */
 
192
  vbox = gtk_vbox_new (FALSE, 0);
 
193
 
 
194
  if(xratio<1.0 || properties->fullscreen==FALSE)
 
195
    gnome_app_set_contents (GNOME_APP (window), GTK_WIDGET(vbox));
 
196
 
 
197
  gtk_widget_show (GTK_WIDGET(vbox));
 
198
  gtk_widget_show (GTK_WIDGET(canvas));
 
199
  gtk_widget_show (GTK_WIDGET(canvas_bar));
 
200
 
 
201
  gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET(canvas), TRUE, TRUE, 0);
 
202
  gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET(canvas_bar), TRUE, TRUE, 0);
 
203
 
 
204
  if(xratio>=1.0 && properties->fullscreen)
 
205
    {
 
206
      gnome_canvas_item_new (gnome_canvas_root(canvas_bg),
 
207
                             gnome_canvas_widget_get_type (),
 
208
                             "widget", vbox, 
 
209
                             "x", (double) (gdk_screen_width()-BOARDWIDTH)/2,
 
210
                             "y", (double) (gdk_screen_height()-BOARDHEIGHT-BARHEIGHT)/2,
 
211
                             "size_pixels", TRUE,
 
212
                             NULL);
 
213
    }
 
214
 
 
215
  /* Create the drawing area */
 
216
  gnome_canvas_set_pixels_per_unit (canvas, xratio);
 
217
 
 
218
  gnome_canvas_set_scroll_region (canvas,
 
219
                                  0, 0,
 
220
                                  BOARDWIDTH,
 
221
                                  BOARDHEIGHT);
 
222
 
 
223
  gtk_widget_set_usize (GTK_WIDGET(canvas), BOARDWIDTH*xratio, BOARDHEIGHT*xratio);
 
224
 
 
225
  /* Create the spot for the bar */
 
226
  gnome_canvas_set_pixels_per_unit (canvas_bar, xratio);
 
227
  gnome_canvas_set_scroll_region (canvas_bar,
 
228
                                  0, 0,
 
229
                                  BOARDWIDTH,
 
230
                                  BARHEIGHT);
 
231
  gtk_widget_set_usize (GTK_WIDGET(canvas_bar),  BOARDWIDTH*xratio,  BARHEIGHT*xratio);
 
232
 
 
233
}
 
234
 
 
235
void gcompris_set_cursor(guint gdk_cursor_type)
 
236
{
 
237
  GdkCursor *cursor;
 
238
 
 
239
  // Little hack to force gcompris to use the default gnome cursor
 
240
  if(gdk_cursor_type==GCOMPRIS_DEFAULT_CURSOR)
 
241
    gdk_cursor_type=properties->defaultcursor;
 
242
 
 
243
  // I suppose there is less than GCOMPRIS_FIRST_CUSTOM_CURSOR cursors defined in gdkcursors.h !
 
244
  if (gdk_cursor_type < GCOMPRIS_FIRST_CUSTOM_CURSOR) {
 
245
    cursor = gdk_cursor_new(gdk_cursor_type);
 
246
    gdk_window_set_cursor        (window->window, cursor);
 
247
    gdk_cursor_destroy(cursor);
 
248
  } else { // we use a custom cursor
 
249
    GdkColor fg, bg;
 
250
    //    static const gchar * cursor;
 
251
    static const gchar ** bits;
 
252
    
 
253
    switch (gdk_cursor_type) {
 
254
    case GCOMPRIS_BIG_RED_ARROW_CURSOR : 
 
255
      bits = big_red_arrow_cursor_bits; 
 
256
      break;
 
257
    case GCOMPRIS_BIRD_CURSOR : 
 
258
      bits = bird_cursor_bits; 
 
259
      break;
 
260
    case GCOMPRIS_LINE_CURSOR : 
 
261
      bits = big_red_line_cursor_bits; 
 
262
      break;
 
263
    case GCOMPRIS_RECT_CURSOR : 
 
264
      bits = big_red_rectangle_cursor_bits; 
 
265
      break;
 
266
    case GCOMPRIS_FILLRECT_CURSOR : 
 
267
      bits = big_red_filledrectangle_cursor_bits; 
 
268
      break;
 
269
    case GCOMPRIS_CIRCLE_CURSOR : 
 
270
      bits = big_red_circle_cursor_bits; 
 
271
      break;
 
272
    case GCOMPRIS_FILLCIRCLE_CURSOR : 
 
273
      bits = big_red_filledcircle_cursor_bits; 
 
274
      break;
 
275
    case GCOMPRIS_FILL_CURSOR : 
 
276
      bits = big_red_fill_cursor_bits; 
 
277
      break;
 
278
    case GCOMPRIS_DEL_CURSOR : 
 
279
      bits = big_red_del_cursor_bits; 
 
280
      break;
 
281
    case GCOMPRIS_SELECT_CURSOR : 
 
282
      bits = big_red_select_cursor_bits; 
 
283
      break;
 
284
    default : bits = big_red_arrow_cursor_bits;
 
285
    }
 
286
    
 
287
    gdk_color_parse("rgb:FFFF/FFFF/FFFF",&fg);
 
288
    gdk_color_parse("rgb:FFFF/3FFF/0000",&bg);
 
289
    cursor = gdk_cursor_new_from_data(bits, 40 , 40, &fg, &bg, 0, 0);
 
290
    gdk_window_set_cursor(window->window, cursor);
 
291
    gdk_cursor_destroy(cursor);
 
292
  }
 
293
}
 
294
 
 
295
static void setup_window ()
 
296
{
 
297
  window = gnome_app_new (PACKAGE, _("GCompris I Have Understood"));
 
298
 
 
299
  gtk_window_set_policy (GTK_WINDOW (window), FALSE, FALSE, TRUE);
 
300
 
 
301
  gtk_widget_realize (window);
 
302
 
 
303
  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
 
304
                      GTK_SIGNAL_FUNC (quit_cb), NULL);
 
305
  gtk_signal_connect (GTK_OBJECT (window), "key_press_event",
 
306
                      GTK_SIGNAL_FUNC (board_widget_key_press_callback), 0);
 
307
 
 
308
 
 
309
  // Full screen
 
310
  if(properties->fullscreen)
 
311
    gnome_win_hints_set_layer (GTK_WIDGET (window),  WIN_LAYER_ABOVE_DOCK);
 
312
  //  gnome_win_hints_set_state(GTK_WIDGET (window),  WIN_STATE_FIXED_POSITION);
 
313
 
 
314
  // Set the cursor
 
315
  gcompris_set_cursor(GCOMPRIS_DEFAULT_CURSOR);
 
316
 
 
317
  /* -------------- */
 
318
 
 
319
  /* For non anti alias canvas */
 
320
  gtk_widget_push_visual (gdk_rgb_get_visual ());
 
321
  gtk_widget_push_colormap (gdk_rgb_get_cmap ());
 
322
 
 
323
  /* For anti alias canvas */
 
324
  /*
 
325
  gtk_widget_push_visual(gdk_rgb_get_visual());
 
326
  gtk_widget_push_colormap(gdk_rgb_get_cmap());
 
327
  */
 
328
 
 
329
  /* For non anti alias canvas */
 
330
  canvas     = GNOME_CANVAS(gnome_canvas_new ());
 
331
  canvas_bar = GNOME_CANVAS(gnome_canvas_new ());
 
332
  canvas_bg  = GNOME_CANVAS(gnome_canvas_new ());
 
333
 
 
334
  /* For anti alias canvas */
 
335
  /*
 
336
  canvas     = GNOME_CANVAS(gnome_canvas_new_aa ());
 
337
  canvas_bar = GNOME_CANVAS(gnome_canvas_new_aa ());
 
338
  canvas_bg = GNOME_CANVAS(gnome_canvas_new_aa ());
 
339
  */
 
340
 
 
341
 
 
342
  gnome_app_set_contents (GNOME_APP (window), GTK_WIDGET(canvas_bg));
 
343
 
 
344
  gtk_widget_pop_colormap ();
 
345
  gtk_widget_pop_visual ();
 
346
 
 
347
 
 
348
  gtk_widget_show (GTK_WIDGET(canvas_bg));
 
349
 
 
350
  if(properties->fullscreen)
 
351
    {
 
352
      gdk_window_set_decorations (window->window, 0);
 
353
      gdk_window_set_functions (window->window, 0);
 
354
      gtk_widget_set_uposition (window, 0, 0);
 
355
    }
 
356
 
 
357
  init_plugins();
 
358
 
 
359
  /* Load and Run the menu */
 
360
  gcomprisBoardMenu = gcompris_read_xml_file(PACKAGE_DATA_DIR "/menu.xml");
 
361
  if(!board_check_file(gcomprisBoardMenu))
 
362
    g_error("Cant't find the menu board or plugin execution error");
 
363
 
 
364
  /* Run the bar */
 
365
  gcompris_bar_start(canvas_bar);
 
366
 
 
367
  board_play (gcomprisBoardMenu);
 
368
 
 
369
  init_background();
 
370
}
 
371
 
 
372
void gcompris_end_board()
 
373
{
 
374
  if (get_current_gcompris_board()->previous_board == NULL)
 
375
    {
 
376
      /* We are in the upper menu: leave GCompris? */
 
377
      quit_cb(NULL, NULL);
 
378
 
 
379
      /* Oups, the user changed his mind : restart the menu */
 
380
      board_play (get_current_gcompris_board());
 
381
      return;
 
382
    }
 
383
 
 
384
  /* Run the previous board */
 
385
  board_play (get_current_gcompris_board()->previous_board);
 
386
}
 
387
 
 
388
static void quit_cb (GtkWidget *widget, gpointer data)
 
389
{
 
390
  // FIXME: Should be implemented as a canvas dialog window
 
391
  //  if (end_board_box ())
 
392
  //    return;
 
393
 
 
394
  /*      cleanup_plugins(); */
 
395
  gcompris_properties_save(properties);
 
396
 
 
397
  gtk_main_quit ();
 
398
 
 
399
}
 
400
 
 
401
static void load_properties ()
 
402
{
 
403
  properties = gcompris_properties_new ();
 
404
}
 
405
 
 
406
/*
 
407
 * This returns the locale for which text must be displayed
 
408
 *
 
409
 */
 
410
gchar *gcompris_get_locale()
 
411
{
 
412
  char *locale;
 
413
 
 
414
  /* First check locale got overrided by the user */
 
415
  if(gcompris_locale != NULL)
 
416
    return(gcompris_locale);
 
417
 
 
418
  locale = getenv("LC_ALL");
 
419
  if(locale == NULL)
 
420
    locale = getenv("LANG");
 
421
 
 
422
  if(locale!=NULL)
 
423
    return(locale);
 
424
 
 
425
  return("en");
 
426
}
 
427
 
 
428
/*
 
429
 * This set the locale for which text must be displayed
 
430
 *
 
431
 */
 
432
void gcompris_set_locale(gchar *locale)
 
433
{
 
434
 
 
435
  gcompris_locale = g_strdup(setlocale(LC_ALL, locale));
 
436
  printf("gcompris_set_locale requested %s got %s\n", locale, gcompris_locale);
 
437
  /* WARNING: This does not update gettext translation */
 
438
}
 
439
 
 
440
/*****************************************
 
441
 * Main
 
442
 *
 
443
 */
 
444
 
 
445
int
 
446
main (int argc, char *argv[])
 
447
{
 
448
  int c;
 
449
  poptContext optCon;
 
450
 
 
451
  /* To have some real random behaviour */
 
452
  srand (time (NULL));
 
453
 
 
454
  bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
 
455
  textdomain (PACKAGE);
 
456
 
 
457
  load_properties ();
 
458
 
 
459
  // Set the default gcompris cursor
 
460
  properties->defaultcursor = GCOMPRIS_DEFAULT_CURSOR;
 
461
 
 
462
  // Set the user's choice locale
 
463
  gcompris_set_locale(properties->locale);
 
464
  
 
465
  initSound();
 
466
  
 
467
  gnome_init_with_popt_table (PACKAGE, VERSION, argc, argv, command_line, 0, &optCon);
 
468
  
 
469
  optCon = poptGetContext (NULL, argc, argv, command_line, 0);
 
470
 
 
471
  /*------------------------------------------------------------*/
 
472
  while ((c = poptGetNextOpt (optCon)) != -1)
 
473
    {
 
474
      switch (c)
 
475
        {
 
476
        case P_VERSION:
 
477
          printf (_("GCompris\nVersion: %s\nLicence: GPL\n"
 
478
                    "More infos on http://ofset.sourceforge.net/gcompris\n"),
 
479
                  VERSION);
 
480
          exit (0);
 
481
          break;
 
482
 
 
483
        case P_FULLSCREEN:
 
484
          properties->fullscreen = TRUE;
 
485
          break;
 
486
 
 
487
        case P_WINDOW:
 
488
          properties->fullscreen = FALSE;
 
489
          break;
 
490
 
 
491
        case P_MUTE:
 
492
          g_warning("Sound disabled");
 
493
          properties->music = FALSE;
 
494
          properties->fx = FALSE;
 
495
          break;
 
496
 
 
497
        case P_SOUND:
 
498
          g_warning("Sound enabled");
 
499
          properties->music = TRUE;
 
500
          properties->fx = TRUE;
 
501
          break;
 
502
 
 
503
        case P_CURSOR:
 
504
          g_warning("Default gnome cursor enabled");
 
505
          properties->defaultcursor = GDK_LEFT_PTR;
 
506
          break;
 
507
        }
 
508
    }
 
509
 
 
510
  if (c < -1)
 
511
    {
 
512
      /* an error occurred during option processing */
 
513
      fprintf (stderr, "%s: %s\n",
 
514
               poptBadOption (optCon, POPT_BADOPTION_NOALIAS),
 
515
               poptStrerror (c));
 
516
      return (-1);
 
517
    }
 
518
 
 
519
  /*------------------------------------------------------------*/
 
520
 
 
521
 
 
522
  gnome_sound_init(NULL);
 
523
 
 
524
  /* Gdk-Pixbuf */
 
525
  gdk_rgb_init();
 
526
 
 
527
  setup_window ();
 
528
 
 
529
  gtk_widget_show (window);
 
530
 
 
531
  if (properties->music)
 
532
    {
 
533
      gcompris_play_ogg("intro", "welcome", NULL);
 
534
    }
 
535
  else
 
536
    gcompris_play_ogg("welcome", NULL);
 
537
 
 
538
  gtk_main ();
 
539
  return 0;
 
540
}
 
541
 
 
542
 
 
543
/* Local Variables: */
 
544
/* mode:c */
 
545
/* eval:(load-library "time-stamp") */
 
546
/* eval:(make-local-variable 'write-file-hooks) */
 
547
/* eval:(add-hook 'write-file-hooks 'time-stamp) */
 
548
/* eval:(setq time-stamp-format '(time-stamp-yyyy/mm/dd time-stamp-hh:mm:ss user-login-name)) */
 
549
/* End: */