~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/readingh-activity/reading.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* gcompris - reading.c
 
2
 *
 
3
 * Copyright (C) 2000, 2008 Bruno Coudoin
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU General Public License as published by
 
7
 *   the Free Software Foundation; either version 3 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <string.h>
 
20
 
 
21
#include "gcompris/gcompris.h"
 
22
 
 
23
 
 
24
#define SOUNDLISTFILE PACKAGE
 
25
#define MAXWORDSLENGTH 50
 
26
#define MAX_WORDS 100
 
27
 
 
28
 
 
29
static GcomprisWordlist *gc_wordlist = NULL;
 
30
 
 
31
static GcomprisBoard *gcomprisBoard = NULL;
 
32
 
 
33
static gint drop_items_id    = 0;
 
34
static gint next_level_timer = 0;
 
35
 
 
36
static gchar *textToFind = NULL;
 
37
static gint textToFindIndex = 0;
 
38
#define NOT_THERE -1000
 
39
 
 
40
static GooCanvasItem *boardRootItem = NULL;
 
41
 
 
42
typedef enum
 
43
{
 
44
  MODE_HORIZONTAL               = 0,
 
45
  MODE_VERTICAL                 = 1,
 
46
  MODE_HORIZONTAL_RTL           = 2
 
47
} Mode;
 
48
static Mode currentMode = MODE_VERTICAL;
 
49
 
 
50
/* Store the moving words */
 
51
typedef struct {
 
52
  GooCanvasItem *rootItem;
 
53
  GooCanvasItem *overwriteItem;
 
54
  GooCanvasItem *item;
 
55
} LettersItem;
 
56
 
 
57
static LettersItem previousFocus;
 
58
static LettersItem toDeleteFocus;
 
59
 
 
60
 
 
61
/* Define the page area where text can be displayed */
 
62
#define BASE_X1 70
 
63
#define BASE_Y1 120
 
64
#define BASE_X2 350
 
65
#define BASE_Y2 520
 
66
#define BASE_CX  BASE_X1+(BASE_X2-BASE_X1)/2
 
67
 
 
68
static gint current_x;
 
69
static gint current_y;
 
70
static gint numberOfLine;
 
71
static gint font_size;
 
72
static gint interline;
 
73
 
 
74
 
 
75
static void              start_board (GcomprisBoard *agcomprisBoard);
 
76
static void              pause_board (gboolean pause);
 
77
static void              end_board (void);
 
78
static gboolean          is_our_board (GcomprisBoard *gcomprisBoard);
 
79
static void              set_level (guint level);
 
80
static int               wait_for_ready;
 
81
static int               gamewon;
 
82
 
 
83
static gboolean          reading_create_item(GooCanvasItem *parent);
 
84
static gint              reading_drop_items (void);
 
85
static void              reading_destroy_all_items(void);
 
86
static gint              reading_next_level(void);
 
87
static void              reading_config_start(GcomprisBoard *agcomprisBoard,
 
88
                                             GcomprisProfile *aProfile);
 
89
static void              reading_config_stop(void);
 
90
 
 
91
static void              player_win(void);
 
92
static void              player_loose(void);
 
93
static gchar            *get_random_word(const gchar *except);
 
94
static GooCanvasItem    *display_what_to_do(GooCanvasItem *parent);
 
95
static void              ask_ready(gboolean status);
 
96
static void              ask_yes_no(void);
 
97
static gboolean          item_event_valid (GooCanvasItem  *item,
 
98
                                           GooCanvasItem  *target,
 
99
                                           GdkEventButton *event,
 
100
                                           gpointer data);
 
101
 
 
102
static  guint32          fallSpeed = 0;
 
103
 
 
104
/* Description of this plugin */
 
105
static BoardPlugin menu_bp =
 
106
{
 
107
   NULL,
 
108
   NULL,
 
109
   "Reading",
 
110
   "Read a list of words and then work out if the given word is in it",
 
111
   "Bruno Coudoin <bruno.coudoin@free.fr>",
 
112
   NULL,
 
113
   NULL,
 
114
   NULL,
 
115
   NULL,
 
116
   start_board,
 
117
   pause_board,
 
118
   end_board,
 
119
   is_our_board,
 
120
   NULL,
 
121
   NULL,
 
122
   set_level,
 
123
   NULL,
 
124
   NULL,
 
125
   reading_config_start,
 
126
   reading_config_stop
 
127
};
 
128
 
 
129
/*
 
130
 * Main entry point mandatory for each Gcompris's game
 
131
 * ---------------------------------------------------
 
132
 *
 
133
 */
 
134
 
 
135
GET_BPLUGIN_INFO(reading)
 
136
 
 
137
/*
 
138
 * in : boolean TRUE = PAUSE : FALSE = UNPAUSE
 
139
 *
 
140
 */
 
141
static void pause_board (gboolean pause)
 
142
{
 
143
  // after the bonus is ended, the board is unpaused, but we must wait for
 
144
  // the player to be ready (this board does not use the same framework as others)
 
145
  if (wait_for_ready)
 
146
    return;
 
147
 
 
148
  if(gcomprisBoard==NULL)
 
149
    return;
 
150
 
 
151
  if(pause)
 
152
    {
 
153
      if (drop_items_id) {
 
154
        gtk_timeout_remove (drop_items_id);
 
155
        drop_items_id = 0;
 
156
      }
 
157
    }
 
158
  else
 
159
    {
 
160
      if(!drop_items_id) {
 
161
        reading_drop_items();
 
162
      }
 
163
    }
 
164
}
 
165
 
 
166
/*
 
167
 */
 
168
static void start_board (GcomprisBoard *agcomprisBoard)
 
169
{
 
170
  GHashTable *config = gc_db_get_board_conf();
 
171
 
 
172
  gc_locale_set(g_hash_table_lookup( config, "locale"));
 
173
 
 
174
  g_hash_table_destroy(config);
 
175
 
 
176
  if(agcomprisBoard!=NULL)
 
177
    {
 
178
      gcomprisBoard=agcomprisBoard;
 
179
 
 
180
      gc_set_background(goo_canvas_get_root_item(gcomprisBoard->canvas),
 
181
                        "readingh/reading-bg.svgz");
 
182
      wait_for_ready = TRUE;
 
183
      gamewon = FALSE;
 
184
 
 
185
      gcomprisBoard->level = 1;
 
186
      gcomprisBoard->maxlevel = 9;
 
187
      gc_bar_set(GC_BAR_CONFIG|GC_BAR_LEVEL);
 
188
      gc_bar_location(BOARDWIDTH-240, -1, 0.7);
 
189
      PangoFontDescription *font_medium = pango_font_description_from_string(gc_skin_font_board_medium);
 
190
      font_size = PANGO_PIXELS(pango_font_description_get_size (font_medium));
 
191
      interline = (int) (1.5*font_size);
 
192
 
 
193
      PangoContext *pango_context = gtk_widget_get_pango_context  (GTK_WIDGET(agcomprisBoard->canvas));
 
194
 
 
195
      PangoFontMetrics* pango_metrics =  pango_context_get_metrics (pango_context,
 
196
                                                                    font_medium,
 
197
                                                                    pango_language_from_string   (gc_locale_get()));
 
198
      pango_font_description_free(font_medium);
 
199
 
 
200
      int ascent = PANGO_PIXELS(pango_font_metrics_get_ascent (pango_metrics));
 
201
      int descent = PANGO_PIXELS(pango_font_metrics_get_descent (pango_metrics));
 
202
 
 
203
      pango_font_metrics_unref(pango_metrics);
 
204
 
 
205
      interline = ascent + descent;
 
206
 
 
207
      g_warning ("Font to display words have size %d  ascent : %d, descent : %d.\n Set inerline to %d",
 
208
                 font_size, ascent, descent, interline);
 
209
 
 
210
      gc_wordlist = gc_wordlist_get_from_file("wordsgame/default-$LOCALE.xml");
 
211
 
 
212
      if(!gc_wordlist)
 
213
        {
 
214
          /* Fallback to english */
 
215
          gc_wordlist = gc_wordlist_get_from_file("wordsgame/default-en.xml");
 
216
 
 
217
          if(!gc_wordlist)
 
218
            {
 
219
              gcomprisBoard = NULL;
 
220
              gc_dialog(_("Error: We can't find\na list of words to play this game.\n"), gc_board_end);
 
221
              return;
 
222
            }
 
223
        }
 
224
 
 
225
 
 
226
      currentMode=MODE_VERTICAL; // Default mode
 
227
      if(gcomprisBoard->mode && g_strcasecmp(gcomprisBoard->mode, "horizontal")==0)
 
228
        {
 
229
          if (pango_unichar_direction(g_utf8_get_char(gc_wordlist_random_word_get(gc_wordlist, gcomprisBoard->level))) == PANGO_DIRECTION_RTL)
 
230
              currentMode=MODE_HORIZONTAL_RTL;
 
231
          else
 
232
               currentMode=MODE_HORIZONTAL;
 
233
        }
 
234
 
 
235
       reading_next_level();
 
236
    }
 
237
}
 
238
 
 
239
static void
 
240
end_board ()
 
241
{
 
242
 
 
243
  if(gcomprisBoard!=NULL)
 
244
    {
 
245
      pause_board(TRUE);
 
246
      reading_destroy_all_items();
 
247
    }
 
248
 
 
249
  if (gc_wordlist != NULL){
 
250
    gc_wordlist_free(gc_wordlist);
 
251
    gc_wordlist = NULL;
 
252
  }
 
253
 
 
254
  gc_locale_reset();
 
255
 
 
256
  gcomprisBoard = NULL;
 
257
}
 
258
 
 
259
static void
 
260
set_level (guint level)
 
261
{
 
262
 
 
263
  if(gcomprisBoard!=NULL)
 
264
    {
 
265
      gcomprisBoard->level=level;
 
266
      reading_next_level();
 
267
    }
 
268
}
 
269
 
 
270
gboolean
 
271
is_our_board (GcomprisBoard *gcomprisBoard)
 
272
{
 
273
  if (gcomprisBoard)
 
274
    {
 
275
      if(g_strcasecmp(gcomprisBoard->type, "reading")==0)
 
276
        {
 
277
          /* Set the plugin entry */
 
278
          gcomprisBoard->plugin=&menu_bp;
 
279
 
 
280
          return TRUE;
 
281
        }
 
282
    }
 
283
  return FALSE;
 
284
}
 
285
 
 
286
 
 
287
/*-------------------------------------------------------------------------------*/
 
288
/*-------------------------------------------------------------------------------*/
 
289
/*-------------------------------------------------------------------------------*/
 
290
/*-------------------------------------------------------------------------------*/
 
291
 
 
292
/* set initial values for the next level */
 
293
static gint reading_next_level()
 
294
{
 
295
 
 
296
  gc_bar_set_level(gcomprisBoard);
 
297
 
 
298
  gamewon = FALSE;
 
299
 
 
300
  reading_destroy_all_items();
 
301
 
 
302
  boardRootItem = goo_canvas_group_new (goo_canvas_get_root_item(gcomprisBoard->canvas),
 
303
                                        NULL);
 
304
 
 
305
 
 
306
  /* Default speed */
 
307
  fallSpeed = 1400-gcomprisBoard->level*120;
 
308
 
 
309
  if(currentMode==MODE_VERTICAL)
 
310
    {
 
311
      current_x = BASE_CX;
 
312
      numberOfLine = 7 + gcomprisBoard->level;
 
313
    }
 
314
  else
 
315
    {
 
316
      current_x = BASE_X2;
 
317
      numberOfLine = 2 + gcomprisBoard->level;
 
318
    }
 
319
 
 
320
  current_y = BASE_Y1 - 2 * interline;
 
321
 
 
322
  gcomprisBoard->number_of_sublevel = 1;
 
323
  gcomprisBoard->sublevel = 1;
 
324
 
 
325
  display_what_to_do(boardRootItem);
 
326
  ask_ready(TRUE);
 
327
  return (FALSE);
 
328
}
 
329
 
 
330
/* Destroy all the items */
 
331
static void
 
332
reading_destroy_all_items()
 
333
{
 
334
 
 
335
  if (drop_items_id) {
 
336
    gtk_timeout_remove (drop_items_id);
 
337
    drop_items_id = 0;
 
338
  }
 
339
 
 
340
  if (next_level_timer) {
 
341
    gtk_timeout_remove (next_level_timer);
 
342
    drop_items_id = 0;
 
343
  }
 
344
 
 
345
  if(boardRootItem!=NULL)
 
346
      goo_canvas_item_remove(boardRootItem);
 
347
 
 
348
  boardRootItem = NULL;
 
349
  previousFocus.rootItem = NULL;
 
350
  toDeleteFocus.rootItem = NULL;
 
351
 
 
352
  if (textToFind!=NULL)
 
353
    {
 
354
    g_free(textToFind);
 
355
    textToFind=NULL;
 
356
    }
 
357
}
 
358
 
 
359
static GooCanvasItem *
 
360
display_what_to_do(GooCanvasItem *parent)
 
361
{
 
362
 
 
363
  gint base_Y = 90;
 
364
  gint base_X = 570;
 
365
 
 
366
  /* Load the text to find */
 
367
 
 
368
  textToFind = get_random_word(NULL);
 
369
 
 
370
  g_assert(textToFind != NULL);
 
371
 
 
372
  /* Decide now if this time we will display the text to find */
 
373
  /* Use this formula to have a better random number see 'man 3 rand' */
 
374
  if(g_random_boolean())
 
375
      textToFindIndex = g_random_int_range(0, numberOfLine);
 
376
  else
 
377
    textToFindIndex = NOT_THERE;
 
378
 
 
379
  goo_canvas_text_new (parent,
 
380
                       _("Please, check if the word"),
 
381
                       (double) base_X,
 
382
                       (double) base_Y,
 
383
                       -1,
 
384
                       GTK_ANCHOR_CENTER,
 
385
                       "font", gc_skin_font_board_medium,
 
386
                       "fill-color", "black",
 
387
                       NULL);
 
388
 
 
389
  goo_canvas_text_new (parent,
 
390
                       textToFind,
 
391
                       (double) base_X,
 
392
                       (double) base_Y + 30,
 
393
                       -1,
 
394
                       GTK_ANCHOR_CENTER,
 
395
                       "font", gc_skin_font_board_big,
 
396
                       "fill-color", "blue",
 
397
                       NULL);
 
398
 
 
399
  goo_canvas_text_new (parent,
 
400
                       _("is being displayed"),
 
401
                       (double) base_X,
 
402
                       (double) base_Y + 60,
 
403
                       -1,
 
404
                       GTK_ANCHOR_CENTER,
 
405
                       "font", gc_skin_font_board_medium,
 
406
                       "fill-color", "black",
 
407
                       NULL);
 
408
 
 
409
 
 
410
  return NULL;
 
411
}
 
412
 
 
413
static gboolean
 
414
reading_create_item(GooCanvasItem *parent)
 
415
{
 
416
  gint   anchor = GTK_ANCHOR_CENTER;
 
417
  gchar *word;
 
418
 
 
419
  g_assert(textToFind!=NULL);
 
420
 
 
421
  if(toDeleteFocus.rootItem)
 
422
    {
 
423
      goo_canvas_item_remove(toDeleteFocus.rootItem);
 
424
      toDeleteFocus.rootItem = NULL;
 
425
    }
 
426
 
 
427
  if(previousFocus.rootItem)
 
428
    {
 
429
      g_object_set (previousFocus.overwriteItem,
 
430
                    "visibility", GOO_CANVAS_ITEM_VISIBLE,
 
431
                    NULL);
 
432
      toDeleteFocus.rootItem = previousFocus.rootItem;
 
433
    }
 
434
 
 
435
  if(numberOfLine<=0)
 
436
    {
 
437
      goo_canvas_item_remove(toDeleteFocus.rootItem);
 
438
      toDeleteFocus.rootItem = NULL;
 
439
 
 
440
      ask_yes_no();
 
441
      return FALSE;
 
442
    }
 
443
 
 
444
 
 
445
  if(textToFindIndex!=0)
 
446
    {
 
447
      word = get_random_word(textToFind);
 
448
    }
 
449
  else
 
450
    {
 
451
      word = g_strdup(textToFind);
 
452
    }
 
453
 
 
454
  if(word==NULL)
 
455
  {
 
456
        gc_dialog(_("We skip this level because there are not enough word in the list!"),
 
457
                (DialogBoxCallBack)reading_next_level);
 
458
        gcomprisBoard->level++;
 
459
        if(gcomprisBoard->level>gcomprisBoard->maxlevel)
 
460
          gcomprisBoard->level = gcomprisBoard->maxlevel;
 
461
        return FALSE;
 
462
  }
 
463
 
 
464
  if(textToFindIndex>=0)
 
465
    textToFindIndex--;
 
466
 
 
467
  previousFocus.rootItem = \
 
468
    goo_canvas_group_new (parent,
 
469
                          NULL);
 
470
 
 
471
  goo_canvas_item_translate(previousFocus.rootItem,
 
472
                            current_x,
 
473
                            current_y);
 
474
 
 
475
  if(currentMode==MODE_HORIZONTAL)
 
476
    anchor=GTK_ANCHOR_WEST;
 
477
  else if (currentMode==MODE_HORIZONTAL_RTL)
 
478
    anchor=GTK_ANCHOR_EAST;
 
479
 
 
480
  previousFocus.item = \
 
481
    goo_canvas_text_new (previousFocus.rootItem,
 
482
                         word,
 
483
                         (double) 0,
 
484
                         (double) 0,
 
485
                         -1,
 
486
                         anchor,
 
487
                         "font", gc_skin_font_board_medium,
 
488
                         "fill-color", "black",
 
489
                         NULL);
 
490
 
 
491
 
 
492
  gchar *oldword = g_strdup_printf("<span foreground=\"black\" background=\"black\">%s</span>", word);
 
493
 
 
494
  g_free(word);
 
495
 
 
496
  previousFocus.overwriteItem = \
 
497
    goo_canvas_text_new (previousFocus.rootItem,
 
498
                         oldword,
 
499
                         0,
 
500
                         0,
 
501
                         -1,
 
502
                         anchor,
 
503
                         "font", gc_skin_font_board_medium,
 
504
                         "use-markup", TRUE,
 
505
                         NULL);
 
506
 
 
507
  g_free(oldword);
 
508
  g_object_set (previousFocus.overwriteItem,
 
509
                "visibility", GOO_CANVAS_ITEM_INVISIBLE, NULL);
 
510
 
 
511
  // Calculate the next spot
 
512
  if(currentMode==MODE_VERTICAL)
 
513
    {
 
514
      current_y += interline;
 
515
      numberOfLine--;
 
516
    }
 
517
  else if (currentMode==MODE_HORIZONTAL_RTL)
 
518
    {
 
519
      GooCanvasBounds bounds;
 
520
      goo_canvas_item_get_bounds(previousFocus.rootItem, &bounds);
 
521
 
 
522
      // Are we out of bound
 
523
      if(bounds.x1<BASE_X1)
 
524
        {
 
525
          // Do the line Wrapping
 
526
          goo_canvas_item_translate(previousFocus.rootItem, BASE_X2-bounds.x2, interline);
 
527
          current_y += interline;
 
528
          current_x = BASE_X2;
 
529
          numberOfLine--;
 
530
        }
 
531
      current_x -= bounds.x2-bounds.x1 + font_size;
 
532
     }
 
533
 
 
534
  else
 
535
    {
 
536
      GooCanvasBounds bounds;
 
537
 
 
538
      goo_canvas_item_get_bounds(previousFocus.rootItem, &bounds);
 
539
 
 
540
      // Are we out of bound
 
541
      if(bounds.x2 > BASE_X2)
 
542
        {
 
543
          // Do the line Wrapping
 
544
          goo_canvas_item_translate(previousFocus.rootItem,
 
545
                                    BASE_X1-bounds.x1, interline);
 
546
          current_y += interline;
 
547
          current_x = BASE_X1;
 
548
          numberOfLine--;
 
549
        }
 
550
      current_x += bounds.x2 - bounds.x1 + font_size;
 
551
    }
 
552
 
 
553
  return (TRUE);
 
554
}
 
555
 
 
556
/*
 
557
 * This is called on a low frequency and is used to display new items
 
558
 *
 
559
 */
 
560
static gint
 
561
reading_drop_items ()
 
562
{
 
563
 
 
564
  if(reading_create_item(boardRootItem))
 
565
    drop_items_id = gtk_timeout_add (fallSpeed,
 
566
                                     (GtkFunction) reading_drop_items, NULL);
 
567
  return (FALSE);
 
568
}
 
569
 
 
570
static GooCanvasItem *
 
571
addBackground(GooCanvasItem *parent,
 
572
              GooCanvasItem *item)
 
573
{
 
574
  GooCanvasBounds bounds;
 
575
  int gap = 8;
 
576
 
 
577
  goo_canvas_item_get_bounds (item, &bounds);
 
578
 
 
579
  return(goo_canvas_rect_new (parent,
 
580
                              bounds.x1 - gap,
 
581
                              bounds.y1 - gap,
 
582
                              bounds.x2 - bounds.x1 + gap*2,
 
583
                              bounds.y2 - bounds.y1 + gap*2,
 
584
                              "stroke_color_rgba", 0xFFFFFFFFL,
 
585
                              "fill_color_rgba", 0XD5C393FFL,
 
586
                              "line-width", (double) 2,
 
587
                              "radius-x", (double) 10,
 
588
                              "radius-y", (double) 10,
 
589
                              NULL) );
 
590
}
 
591
 
 
592
static void
 
593
ask_ready(gboolean status)
 
594
{
 
595
  static GooCanvasItem *item1 = NULL;
 
596
  static GooCanvasItem *item2 = NULL;
 
597
  double x_offset = 560;
 
598
  double y_offset = 260;
 
599
 
 
600
  if(textToFind==NULL)
 
601
    return;
 
602
 
 
603
  if(status==FALSE)
 
604
    {
 
605
      gc_item_focus_remove(item1, NULL);
 
606
      gc_item_focus_remove(item2, item1);
 
607
 
 
608
      if(item1!=NULL)
 
609
        goo_canvas_item_remove(item1);
 
610
 
 
611
      if(item2!=NULL)
 
612
        goo_canvas_item_remove(item2);
 
613
 
 
614
      item1 = NULL;
 
615
      item2 = NULL;
 
616
      return;
 
617
    }
 
618
 
 
619
  /*----- READY -----*/
 
620
  item2 = goo_canvas_text_new (boardRootItem,
 
621
                               _("I am Ready"),
 
622
                               x_offset,
 
623
                               y_offset,
 
624
                               -1,
 
625
                               GTK_ANCHOR_CENTER,
 
626
                               "font", gc_skin_font_board_big,
 
627
                               "fill-color", "white",
 
628
                                NULL);
 
629
 
 
630
  g_signal_connect(item2, "button-press-event",
 
631
                   (GtkSignalFunc) item_event_valid,
 
632
                   "R");
 
633
 
 
634
  item1 = addBackground(boardRootItem, item2);
 
635
 
 
636
  g_signal_connect(item1, "button-press-event",
 
637
                   (GtkSignalFunc) item_event_valid,
 
638
                   "R");
 
639
  gc_item_focus_init(item1, NULL);
 
640
  gc_item_focus_init(item2, item1);
 
641
  goo_canvas_item_raise(item2, NULL);
 
642
}
 
643
 
 
644
static void
 
645
ask_yes_no()
 
646
{
 
647
  GooCanvasItem *item1;
 
648
  GooCanvasItem *item2;
 
649
  double x_offset = 560;
 
650
  double y_offset = 260;
 
651
 
 
652
  if(textToFind==NULL)
 
653
    return;
 
654
 
 
655
  /*----- YES -----*/
 
656
 
 
657
  item2 =
 
658
    goo_canvas_text_new (boardRootItem,
 
659
                         ("Yes, I saw it"),
 
660
                         x_offset,
 
661
                         y_offset,
 
662
                         -1,
 
663
                         GTK_ANCHOR_CENTER,
 
664
                         "font", gc_skin_font_board_big,
 
665
                         "fill-color", "white",
 
666
                         NULL);
 
667
 
 
668
  item1 = addBackground(boardRootItem, item2);
 
669
 
 
670
  g_signal_connect(item2, "button-press-event",
 
671
                   (GtkSignalFunc) item_event_valid,
 
672
                   "Y");
 
673
  g_signal_connect(item1, "button-press-event",
 
674
                   (GtkSignalFunc) item_event_valid,
 
675
                   "Y");
 
676
 
 
677
  gc_item_focus_init(item1, NULL);
 
678
  gc_item_focus_init(item2, item1);
 
679
  goo_canvas_item_raise(item2, NULL);
 
680
 
 
681
  /*----- NO -----*/
 
682
  y_offset += 100;
 
683
 
 
684
  item2 =
 
685
    goo_canvas_text_new (boardRootItem,
 
686
                         _("No, it was not there"),
 
687
                         x_offset,
 
688
                         y_offset,
 
689
                         -1,
 
690
                         GTK_ANCHOR_CENTER,
 
691
                         "font", gc_skin_font_board_big,
 
692
                         "fill-color", "white",
 
693
                         NULL);
 
694
 
 
695
  item1 = addBackground(boardRootItem, item2);
 
696
 
 
697
  g_signal_connect(item2, "button-press-event",
 
698
                   (GtkSignalFunc) item_event_valid,
 
699
                   "N");
 
700
  g_signal_connect(item1, "button-press-event",
 
701
                   (GtkSignalFunc) item_event_valid,
 
702
                   "N");
 
703
 
 
704
  gc_item_focus_init(item1, NULL);
 
705
  gc_item_focus_init(item2, item1);
 
706
  goo_canvas_item_raise(item2, NULL);
 
707
}
 
708
 
 
709
 
 
710
static void
 
711
player_win()
 
712
{
 
713
  gamewon = TRUE;
 
714
  wait_for_ready = TRUE;
 
715
  gc_bonus_display(gamewon, GC_BONUS_FLOWER);
 
716
  /* Try the next level */
 
717
  gcomprisBoard->level++;
 
718
  if(gcomprisBoard->level>gcomprisBoard->maxlevel)
 
719
    gcomprisBoard->level = gcomprisBoard->maxlevel;
 
720
 
 
721
  next_level_timer = g_timeout_add(3000, (GtkFunction)reading_next_level, NULL);
 
722
}
 
723
 
 
724
static void
 
725
player_loose()
 
726
{
 
727
  gchar *expected;
 
728
  gchar *got;
 
729
  gamewon = FALSE;
 
730
  wait_for_ready = TRUE;
 
731
 
 
732
  /* Report what was wrong in the log */
 
733
  expected = g_strdup_printf(_("The word to find was '%s'"), textToFind);
 
734
 
 
735
  if(textToFindIndex == NOT_THERE)
 
736
    got    = g_strdup_printf(_("But it was not displayed"));
 
737
  else
 
738
    got    = g_strdup_printf(_("And it was displayed"));
 
739
 
 
740
  gc_log_set_comment (gcomprisBoard, expected, got);
 
741
 
 
742
  g_free(expected);
 
743
  g_free(got);
 
744
 
 
745
  gc_bonus_display(gamewon, GC_BONUS_FLOWER);
 
746
 
 
747
  next_level_timer = g_timeout_add(3000, (GtkFunction)reading_next_level, NULL);
 
748
}
 
749
 
 
750
/* Callback for the yes and no buttons */
 
751
static gboolean
 
752
item_event_valid (GooCanvasItem  *item,
 
753
                  GooCanvasItem  *target,
 
754
                  GdkEventButton *event,
 
755
                  gpointer data)
 
756
{
 
757
 
 
758
  if (((char *)data)[0]=='R')
 
759
    {
 
760
      // The user is Ready
 
761
      wait_for_ready = FALSE;
 
762
      ask_ready(FALSE);
 
763
      pause_board(FALSE);
 
764
    }
 
765
  else if(!wait_for_ready) {
 
766
    if ((((char *)data)[0]=='Y' && textToFindIndex == -1)
 
767
        || (((char *)data)[0]=='N' && textToFindIndex == NOT_THERE))
 
768
      {
 
769
        player_win();
 
770
      }
 
771
    else
 
772
      {
 
773
        player_loose();
 
774
      }
 
775
  }
 
776
  return TRUE;
 
777
}
 
778
 
 
779
 
 
780
/** Return a random word from a set of text file depending on
 
781
 *  the current level and language
 
782
 *
 
783
 * \param except: if non NULL, never return this value
 
784
 *
 
785
 * \return a random word from. must be freed by the caller
 
786
 */
 
787
static gchar *
 
788
get_random_word(const gchar* except)
 
789
{
 
790
  gchar *word;
 
791
  int count=0;
 
792
 
 
793
  word = gc_wordlist_random_word_get(gc_wordlist, gcomprisBoard->level);
 
794
 
 
795
  if(except)
 
796
    while(strcmp(except, word)==0)
 
797
      {
 
798
        g_free(word);
 
799
 
 
800
        if(count++>100)
 
801
        {
 
802
                word = NULL;
 
803
                break;
 
804
        }
 
805
        word = gc_wordlist_random_word_get(gc_wordlist, gcomprisBoard->level);
 
806
      }
 
807
 
 
808
  return(word);
 
809
}
 
810
 
 
811
 
 
812
/* ************************************* */
 
813
/* *            Configuration          * */
 
814
/* ************************************* */
 
815
 
 
816
 
 
817
/* ======================= */
 
818
/* = config_start        = */
 
819
/* ======================= */
 
820
 
 
821
static GcomprisProfile *profile_conf;
 
822
static GcomprisBoard   *board_conf;
 
823
 
 
824
static void save_table (gpointer key,
 
825
                        gpointer value,
 
826
                        gpointer user_data)
 
827
{
 
828
  gc_db_set_board_conf ( profile_conf,
 
829
                            board_conf,
 
830
                            (gchar *) key,
 
831
                            (gchar *) value);
 
832
}
 
833
 
 
834
static void conf_ok(GHashTable *table)
 
835
{
 
836
  if (!table){
 
837
    if (gcomprisBoard)
 
838
      pause_board(FALSE);
 
839
    return;
 
840
  }
 
841
 
 
842
  g_hash_table_foreach(table, (GHFunc) save_table, NULL);
 
843
 
 
844
  if (gcomprisBoard){
 
845
    gc_locale_reset();
 
846
 
 
847
    GHashTable *config;
 
848
 
 
849
    if (profile_conf)
 
850
      config = gc_db_get_board_conf();
 
851
    else
 
852
      config = table;
 
853
 
 
854
    gc_locale_set(g_hash_table_lookup( config, "locale"));
 
855
 
 
856
    if (profile_conf)
 
857
      g_hash_table_destroy(config);
 
858
 
 
859
    reading_next_level();
 
860
 
 
861
    pause_board(FALSE);
 
862
  }
 
863
 
 
864
  board_conf = NULL;
 
865
  profile_conf = NULL;
 
866
 
 
867
}
 
868
 
 
869
static void
 
870
reading_config_start(GcomprisBoard *agcomprisBoard,
 
871
                    GcomprisProfile *aProfile)
 
872
{
 
873
  GcomprisBoardConf *conf;
 
874
  board_conf = agcomprisBoard;
 
875
  profile_conf = aProfile;
 
876
 
 
877
  if (gcomprisBoard)
 
878
    pause_board(TRUE);
 
879
 
 
880
  gchar *label = g_strdup_printf(_("<b>%s</b> configuration\n for profile <b>%s</b>"),
 
881
                                 agcomprisBoard->name,
 
882
                                 aProfile? aProfile->name: "");
 
883
 
 
884
  conf = gc_board_config_window_display( label,
 
885
                                 (GcomprisConfCallback )conf_ok);
 
886
 
 
887
  g_free(label);
 
888
 
 
889
  /* init the combo to previously saved value */
 
890
  GHashTable *config = gc_db_get_conf( profile_conf, board_conf);
 
891
 
 
892
  gchar *locale = g_hash_table_lookup( config, "locale");
 
893
 
 
894
  gc_board_config_combo_locales(conf, locale);
 
895
  gc_board_config_wordlist(conf, "wordsgame/default-$LOCALE.xml");
 
896
}
 
897
 
 
898
 
 
899
/* ======================= */
 
900
/* = config_stop        = */
 
901
/* ======================= */
 
902
static void
 
903
reading_config_stop()
 
904
{
 
905
}