Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/images_selector.c
blob: 0bbd9ae4f329826a970ee40c6a19b793be03d725 (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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/* gcompris - images_selector.c
 *
 * Time-stamp: <2006/08/21 23:26:43 bruno>
 *
 * Copyright (C) 2000 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
 */

/**
 * An image selector for gcompris
 *
 */

#include "string.h"

/* libxml includes */
#include <libxml/tree.h>
#include <libxml/parser.h>

#include "gcompris.h"

#define SOUNDLISTFILE PACKAGE

static gint		 item_event_images_selector(GnomeCanvasItem *item,
						    GdkEvent *event,
						    gpointer data);
static gint		 item_event_imageset_selector(GnomeCanvasItem *item,
						      GdkEvent *event,
						      gpointer data);
static gint		 item_event_scroll(GnomeCanvasItem *item,
					   GdkEvent *event,
					   GnomeCanvas *canvas);
static gboolean		 read_xml_file(gchar *fname);
static gboolean		 read_dataset_directory(gchar *dataset_dir);
static void		 display_image(gchar *imagename, GnomeCanvasItem *rootitem);
static void		 free_stuff (GtkObject *obj, GSList *data);

static gboolean		 images_selector_displayed = FALSE;

static GnomeCanvasItem	*rootitem = NULL;
static GnomeCanvasItem	*current_root_set = NULL;

static GnomeCanvas	*canvas_list_selector; /* The scrolled left part */
static GnomeCanvasItem  *list_bg_item;

static GnomeCanvas	*canvas_image_selector; /* The scrolled right part */
static GnomeCanvasItem  *image_bg_item;

static ImageSelectorCallBack imageSelectorCallBack = NULL;

static gboolean		 display_in_progress;

/* Represent the limits of the image area */
#define	DRAWING_AREA_X1	111.0
#define DRAWING_AREA_Y1	14.0
#define DRAWING_AREA_X2	774.0
#define DRAWING_AREA_Y2	500.0

/* Represent the limits of list area */
#define	LIST_AREA_X1	18.0
#define	LIST_AREA_Y1	16.0
#define	LIST_AREA_X2	80.0
#define	LIST_AREA_Y2	500.0
#define VERTICAL_NUMBER_OF_LIST_IMAGE	6

#define HORIZONTAL_NUMBER_OF_IMAGE	8
#define VERTICAL_NUMBER_OF_IMAGE	4
#define IMAGE_GAP			10

#define IMAGE_WIDTH  (DRAWING_AREA_X2-DRAWING_AREA_X1)/HORIZONTAL_NUMBER_OF_IMAGE-IMAGE_GAP
#define IMAGE_HEIGHT (DRAWING_AREA_Y2-DRAWING_AREA_Y1)/VERTICAL_NUMBER_OF_IMAGE-IMAGE_GAP

#define LIST_IMAGE_WIDTH  LIST_AREA_X2-LIST_AREA_X1-IMAGE_GAP
#define LIST_IMAGE_HEIGHT (LIST_AREA_Y2-LIST_AREA_Y1)/VERTICAL_NUMBER_OF_LIST_IMAGE-IMAGE_GAP

static guint		 ix;
static guint		 iy;
static guint		 isy;

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

/*
 * Do all the images_selector display and register the events
 */

void
gc_selector_images_start (GcomprisBoard *gcomprisBoard, gchar *dataset,
				ImageSelectorCallBack iscb)
{

  GnomeCanvasItem *item, *item2;
  GdkPixbuf	*pixmap = NULL;
  gint		 y = 0;
  gint		 y_start = 0;
  gint		 x_start = 0;
  gchar		*dataseturl = NULL;

  GtkWidget	  *w;

  if(rootitem)
    return;

  gc_bar_hide(TRUE);

  gc_board_pause(TRUE);

  imageSelectorCallBack=iscb;

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

  images_selector_displayed = TRUE;

  pixmap = gc_skin_pixmap_load("images_selector_bg.png");
  y_start = (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap))/2;
  x_start = (BOARDWIDTH - gdk_pixbuf_get_width(pixmap))/2;
  item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				gnome_canvas_pixbuf_get_type (),
				"pixbuf", pixmap,
				"x", (double) x_start,
				"y", (double) y_start,
				NULL);
  y = BOARDHEIGHT - (BOARDHEIGHT - gdk_pixbuf_get_height(pixmap))/2;
  gdk_pixbuf_unref(pixmap);

  y_start += 110;

  pixmap = gc_skin_pixmap_load("button_large.png");

  /*
   * Create the list scrollbar
   * -------------------------
   */
  canvas_list_selector     = GNOME_CANVAS(gnome_canvas_new ());

  gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
			 gnome_canvas_widget_get_type (),
			 "widget", GTK_WIDGET(canvas_list_selector),
			 "x", (double) LIST_AREA_X1,
			 "y", (double) LIST_AREA_Y1,
			 "width",  LIST_AREA_X2 - LIST_AREA_X1,
			 "height", LIST_AREA_Y2 - LIST_AREA_Y1 - 35.0,
			 NULL);

  gtk_widget_show (GTK_WIDGET(canvas_list_selector));

  /* Set the new canvas to the background color or it's white */
  list_bg_item = gnome_canvas_item_new (gnome_canvas_root(canvas_list_selector),
					gnome_canvas_rect_get_type (),
					"x1", (double) 0,
					"y1", (double) 0,
					"x2", (double) LIST_AREA_X2 - LIST_AREA_X1,
					"y2", (double) LIST_AREA_Y2 - LIST_AREA_Y1,
					"fill_color_rgba", gc_skin_get_color("gcompris/imageselectbg"),
					NULL);


  w = gtk_vscrollbar_new (GTK_LAYOUT(canvas_list_selector)->vadjustment);

  gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
			 gnome_canvas_widget_get_type (),
			 "widget", GTK_WIDGET(w),
			 "x", (double) LIST_AREA_X2 - 5.0,
			 "y", (double) LIST_AREA_Y1,
			 "width", 30.0,
			 "height", LIST_AREA_Y2 - LIST_AREA_Y1 - 20.0,
			 NULL);
  gtk_widget_show (w);
  gnome_canvas_set_center_scroll_region (GNOME_CANVAS (canvas_list_selector), FALSE);

  /* Set the scrollwheel event */
  gtk_signal_connect(GTK_OBJECT(canvas_list_selector), "event",
		     (GtkSignalFunc) item_event_scroll,
		     GNOME_CANVAS(canvas_list_selector));

  /*
   * Create the image scrollbar
   * --------------------------
   */
  canvas_image_selector     = GNOME_CANVAS(gnome_canvas_new ());

  gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
			 gnome_canvas_widget_get_type (),
			 "widget", GTK_WIDGET(canvas_image_selector),
			 "x", (double) DRAWING_AREA_X1,
			 "y", (double) DRAWING_AREA_Y1,
			 "width",  DRAWING_AREA_X2 - DRAWING_AREA_X1,
			 "height", DRAWING_AREA_Y2 - DRAWING_AREA_Y1 - 35.0,
			 NULL);

  gtk_widget_show (GTK_WIDGET(canvas_image_selector));

  /* Set the new canvas to the background color or it's white */
  image_bg_item = gnome_canvas_item_new (gnome_canvas_root(canvas_image_selector),
					gnome_canvas_rect_get_type (),
					"x1", (double) 0,
					"y1", (double) 0,
					"x2", (double) DRAWING_AREA_X2 - DRAWING_AREA_X1,
					"y2", (double) DRAWING_AREA_Y2 - DRAWING_AREA_Y1,
					 "fill_color_rgba", gc_skin_get_color("gcompris/imageselectbg"),
					NULL);


  w = gtk_vscrollbar_new (GTK_LAYOUT(canvas_image_selector)->vadjustment);

  gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
			 gnome_canvas_widget_get_type (),
			 "widget", GTK_WIDGET(w),
			 "x", (double) DRAWING_AREA_X2 - 5.0,
			 "y", (double) DRAWING_AREA_Y1,
			 "width", 30.0,
			 "height", DRAWING_AREA_Y2 - DRAWING_AREA_Y1 - 20.0,
			 NULL);
  gtk_widget_show (w);
  gnome_canvas_set_center_scroll_region (GNOME_CANVAS (canvas_image_selector), FALSE);

  /* Set the scrollwheel event */
  gtk_signal_connect(GTK_OBJECT(canvas_image_selector), "event",
		     (GtkSignalFunc) item_event_scroll,
		     GNOME_CANVAS(canvas_image_selector));

  /*
   * DISPLAY IMAGES
   */

  /* Initial image position */
  ix  = 0;
  iy  = 0;
  isy = 0;

  /* I need  the following :
     -> if dataset is a file read it.
     -> if dataset is a directory, read all xml file in it.
  */
  dataseturl = \
    gc_file_find_absolute(dataset,
				    NULL);

  /* if the file doesn't exist */
  if(g_file_test ((dataseturl), G_FILE_TEST_IS_DIR) )
    {
      g_warning("dataset %s is a directory. Trying to read xml", dataseturl);

      read_dataset_directory(dataseturl);
    }
  else if(dataseturl)
    {
      /* Read the given data set file, local or net */
      read_xml_file(dataseturl);
    }
  else
    {
      /* Network code for dataset directory */
      GSList *filelist = NULL;
      GSList *i = NULL;

      g_free(dataseturl);
      dataseturl = g_strconcat("boards/", dataset, NULL);

      filelist = gc_net_dir_read_name(dataseturl, ".xml");

      for (i = filelist; i != NULL; i = g_slist_next (i))
	{
	  gchar *url = gc_file_find_absolute(i->data,
					     NULL);
	  g_warning("processing dataset=%s\n", (char *)i->data);
	  read_xml_file(url);
	  g_free(url);
	}

      g_slist_free(filelist);

    }
  g_free(dataseturl);

  /*
   * OK Button
   * ---------
   */
  item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				gnome_canvas_pixbuf_get_type (),
				"pixbuf", pixmap,
				"x", (double) (BOARDWIDTH*0.5) - gdk_pixbuf_get_width(pixmap)/2,
				"y", (double) y - gdk_pixbuf_get_height(pixmap) - 10,
				NULL);

  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) item_event_images_selector,
		     "/ok/");
  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  item2 = gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootitem),
				gnome_canvas_text_get_type (),
				"text", _("OK"),
				"font", gc_skin_font_title,
				"x", (double)  BOARDWIDTH*0.5,
				"y", (double)  y - gdk_pixbuf_get_height(pixmap) + 15,
				"anchor", GTK_ANCHOR_CENTER,
				"fill_color_rgba", gc_skin_color_text_button,
				NULL);
  gtk_signal_connect(GTK_OBJECT(item2), "event",
		     (GtkSignalFunc) item_event_images_selector,
		     "/ok/");
  gtk_signal_connect(GTK_OBJECT(item2), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     item);
  gdk_pixbuf_unref(pixmap);

}

/*
 * Remove the displayed images_selector.
 * Do nothing if none is currently being dislayed
 */
void gc_selector_images_stop ()
{
  GcomprisBoard *gcomprisBoard = gc_board_get_current();

  if(gcomprisBoard!=NULL && images_selector_displayed)
    {
      gc_board_pause(FALSE);
    }

  // Destroy the images_selector box
  if(rootitem!=NULL)
    {
      gtk_object_destroy(GTK_OBJECT(rootitem));
    }
  rootitem = NULL;
  current_root_set = NULL;

  gc_bar_hide(FALSE);
  images_selector_displayed = FALSE;
}



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

static void
display_image(gchar *imagename, GnomeCanvasItem *root_item)
{

  GdkPixbuf *pixmap = NULL;
  GnomeCanvasItem *item;
  double xratio, yratio;
  double iw, ih;

  if (imagename==NULL || !images_selector_displayed)
    return;

  pixmap = gc_pixmap_load(imagename);

  /* Sad, the image is not found */
  if(!pixmap)
    return;

  iw = IMAGE_WIDTH;
  ih = IMAGE_HEIGHT;

  /* Calc the max to resize width or height */
  xratio = (double) (((double)gdk_pixbuf_get_width(pixmap))/iw);
  yratio = (double) (((double)gdk_pixbuf_get_height(pixmap))/ih);
  xratio = MAX(yratio,xratio);

  item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(root_item),
				gnome_canvas_pixbuf_get_type (),
				"pixbuf", pixmap,
				"x", (double)ix,
				"y", (double)iy,
				"width", (double) gdk_pixbuf_get_width(pixmap)/xratio,
				"height", (double) gdk_pixbuf_get_height(pixmap)/xratio,
				"width_set", TRUE,
				"height_set", TRUE,
				NULL);
  gdk_pixbuf_unref(pixmap);

  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) item_event_images_selector,
		     imagename);
  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  ix+=IMAGE_WIDTH + IMAGE_GAP;
  if(ix>=DRAWING_AREA_X2-DRAWING_AREA_X1-IMAGE_GAP)
    {
      guint iy_calc;
      ix=0;
      iy+=IMAGE_HEIGHT + IMAGE_GAP;

      /* Cannot use GINT_TO_POINTER with a constant calculation */
      iy_calc = iy + IMAGE_HEIGHT + IMAGE_GAP;
      g_object_set_data (G_OBJECT (root_item), "iy", GINT_TO_POINTER (iy_calc));

      if(iy>=DRAWING_AREA_Y2-DRAWING_AREA_Y1) {
	gnome_canvas_item_set(image_bg_item,
			      "y2", (double) iy + IMAGE_HEIGHT + IMAGE_GAP,
			      NULL);
      }
    }
}

/*
 * Same as display_image but for the dataset
 * The imagelist contains the list of images to be displayed when this dataset is selected
 */
static void
display_image_set(gchar *imagename, GSList *imagelist)
{
  GdkPixbuf *pixmap = NULL;
  GnomeCanvasItem *item;
  GnomeCanvasItem *rootitem_set;
  double xratio, yratio;
  double iw, ih;

  if (imagename==NULL || !images_selector_displayed)
    return;

  pixmap = gc_pixmap_load(imagename);

  iw = LIST_IMAGE_WIDTH;
  ih = LIST_IMAGE_HEIGHT;

  /* Calc the max to resize width or height */
  xratio = (double) (((double)gdk_pixbuf_get_width(pixmap))/iw);
  yratio = (double) (((double)gdk_pixbuf_get_height(pixmap))/ih);
  xratio = MAX(yratio,xratio);

  item = gnome_canvas_item_new (gnome_canvas_root(canvas_list_selector),
				gnome_canvas_pixbuf_get_type (),
				"pixbuf", pixmap,
				"x", (double)5,
				"y", (double)isy,
				"width", (double) gdk_pixbuf_get_width(pixmap)/xratio,
				"height", (double) gdk_pixbuf_get_height(pixmap)/xratio,
				"width_set", TRUE,
				"height_set", TRUE,
				NULL);
  gdk_pixbuf_unref(pixmap);
  g_object_set_data (G_OBJECT (item), "imagelist", imagelist);

  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) item_event_imageset_selector,
		     imagename);
  gtk_signal_connect(GTK_OBJECT(item), "event",
		     (GtkSignalFunc) gc_item_focus_event,
		     NULL);

  isy+=LIST_IMAGE_HEIGHT + IMAGE_GAP;

  gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas_list_selector), 0, 0,
				  LIST_AREA_X2- LIST_AREA_X1,
				  isy - IMAGE_GAP);

  if(isy>=LIST_AREA_Y2-LIST_AREA_Y1) {
    gnome_canvas_item_set(list_bg_item,
			  "y2", (double)isy + LIST_IMAGE_HEIGHT + IMAGE_GAP,
			  NULL);
  }

  /* Create a root item to put the image list in it */
  rootitem_set = \
    gnome_canvas_item_new (gnome_canvas_root(canvas_image_selector),
			   gnome_canvas_group_get_type (),
			   "x", (double)0,
			   "y", (double)0,
			   NULL);

  g_object_set_data (G_OBJECT (item), "rootitem", rootitem_set);
  g_object_set_data (G_OBJECT (item), "imageset_done", GINT_TO_POINTER (0));
  g_signal_connect (item, "destroy",
		    G_CALLBACK (free_stuff),
		    imagelist);

}

static void
free_stuff (GtkObject *obj, GSList *list)
{
  while (g_slist_length(list) > 0) {
    g_free(g_slist_nth_data(list,0));
    list = g_slist_remove(list, g_slist_nth_data(list,0));
  }
  g_slist_free(list);
}


/* Callback when an image set is selected */
static gint
item_event_imageset_selector(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
{
  GSList *image_list;
  GnomeCanvasItem *rootitem_set;
  guint imageset_done;

  if(display_in_progress)
    return TRUE;

  switch (event->type)
    {
    case GDK_ENTER_NOTIFY:
      break;
    case GDK_LEAVE_NOTIFY:
      break;
    case GDK_BUTTON_PRESS:
	{
	  guint last_iy;

	  display_in_progress = TRUE;

	  /* We must display the list of images for this set */
	  image_list = (GSList *)g_object_get_data (G_OBJECT (item), "imagelist");
	  g_return_val_if_fail (image_list != NULL, FALSE);

	  /* We must display the list of images for this set */
	  rootitem_set = (GnomeCanvasItem *)g_object_get_data (G_OBJECT (item), "rootitem");
	  g_return_val_if_fail (rootitem_set != NULL, FALSE);

	  imageset_done = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), "imageset_done"));

	  /* Hide the previous image set if any */
	  if (current_root_set != NULL) {
	    gnome_canvas_item_hide(current_root_set);
	  }
	  /* Not yet displayed this set */
	  if(!imageset_done) {
	    guint iy_calc;
	    /* Cannot use GINT_TO_POINTER with a constant calculation */
	    /* Set the initial iy value                               */
	    iy_calc = IMAGE_HEIGHT + IMAGE_GAP;
	    g_object_set_data (G_OBJECT (rootitem_set), "iy", GINT_TO_POINTER (iy_calc));

	    g_slist_foreach (image_list, (GFunc) display_image, rootitem_set);
	    g_object_set_data (G_OBJECT (item), "imageset_done", GINT_TO_POINTER (1));
	  }

	  /* Set the image scrollbar back to its max position */
	  last_iy = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (rootitem_set), "iy"));
	  gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas_image_selector), 0, 0,
					  DRAWING_AREA_X2- DRAWING_AREA_X1,
					  last_iy - IMAGE_GAP);
	  if(last_iy>=DRAWING_AREA_Y2-DRAWING_AREA_Y1) {
	    gnome_canvas_item_set(image_bg_item,
				  "y2", (double) last_iy + IMAGE_HEIGHT + IMAGE_GAP,
				  NULL);
	  }

	  gnome_canvas_item_show(rootitem_set);
	  current_root_set = rootitem_set;

	  /* Back to the initial image position */
	  ix  = 0;
	  iy  = 0;

	  display_in_progress = FALSE;
	}
    default:
      break;
    }
  return FALSE;

}

/* Callback when an image is selected */
static gint
item_event_images_selector(GnomeCanvasItem *item, GdkEvent *event, gpointer data)
{

  if(display_in_progress)
    return TRUE;

  switch (event->type)
    {
    case GDK_ENTER_NOTIFY:
      break;
    case GDK_LEAVE_NOTIFY:
      break;
    case GDK_BUTTON_PRESS:
      if(!strcmp((char *)data, "/ok/"))
	{
	  gc_selector_images_stop();
	}
      else
	{
	  if(imageSelectorCallBack!=NULL)
	    imageSelectorCallBack(data);

	  gc_selector_images_stop();
	}
    default:
      break;
    }
  return FALSE;

}

/* Callback when a scroll event happens */
static gint
item_event_scroll(GnomeCanvasItem *item, GdkEvent *event, GnomeCanvas *canvas)
{
  int x, y;
  if(!rootitem)
    return FALSE;

  switch (event->type)
    {
    case GDK_SCROLL:
      gnome_canvas_get_scroll_offsets (canvas, &x, &y);
      if ( event->scroll.direction == GDK_SCROLL_UP )
	gnome_canvas_scroll_to (canvas, x, y - 20);
      else if ( event->scroll.direction == GDK_SCROLL_DOWN )
	gnome_canvas_scroll_to (canvas, x, y + 20);

      break;
    default:
      break;
    }
  return FALSE;
}

/*
 * Images PARSING
 * --------------
 */

static void
parseImage (xmlDocPtr doc, xmlNodePtr cur) {
  GcomprisProperties *properties = gc_prop_get();
  gchar  *imageSetName = NULL;
  gchar  *filename;
  gchar	 *pathname = NULL;
  gchar  *absolutepath;
  GSList *imageList = NULL;	/* List of Images */
  gboolean havePathName = FALSE;
  gchar  *type = NULL;
  GDir   *imageset_directory;

  /* get the filename of this ImageSet */
  imageSetName = (gchar *)xmlGetProp(cur, BAD_CAST "filename");
  if (xmlHasProp(cur, BAD_CAST "PathName")){
    pathname = (gchar *)xmlGetProp(cur, BAD_CAST "PathName");
    havePathName = TRUE;
  }
  if (havePathName && pathname[0] == '~'){
    /* replace '~' by home dir */
    pathname = g_strdup_printf("%s%s",g_get_home_dir(), pathname+1);
    if (!g_file_test ((pathname), G_FILE_TEST_IS_DIR)){
       g_warning("In ImageSet %s, the pathname for the home directory '%s' is not found. Skipping the whole ImageSet.",
		 imageSetName, pathname);
      return;
    }
  }

  if (xmlHasProp(cur, BAD_CAST "type")){
    /* lsdir means try all file of directory */
    /* list means just keep old behaviour */
    /* others are extensions to look for */
    type = (gchar *)xmlGetProp(cur, BAD_CAST "type");
  }

  /* Looking for imageSetName */
  /* absolute path -> we check it's here */
  /* relative path -> we check for pathname/imagesetname */
  /*               -> and else for PACKAGE_DATA_DIR/imagesetname */
  if (havePathName) {
    if (!g_path_is_absolute (imageSetName)){
      absolutepath = gc_file_find_absolute(imageSetName, NULL);
    }
    else
      absolutepath = g_strdup(imageSetName);
  }
  else
    absolutepath = gc_file_find_absolute(imageSetName, NULL);

  if(!absolutepath)
    {
      g_warning("In ImageSet %s, an image is not found. Skipping ImageSet...", imageSetName);
      return;
    }

  if ((type == NULL) || (g_ascii_strcasecmp (type,"list")==0)) {
    /* old behaviour : we read the filenames from xml files */

    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
      if ((!xmlStrcmp(cur->name, (const xmlChar *)"Image"))) {
	/* get the filename of this ImageSet */
	filename = (gchar *)xmlGetProp(cur, BAD_CAST "filename");
	if (havePathName){
	  filename = g_strdup_printf("%s/%s",pathname,filename);
	}
	imageList = g_slist_prepend (imageList, filename);
      }
      cur = cur->next;
    }

  }
  else {
    /* new behaviour : we read all file of a directory */
    /* or all files with a given suffix */

    if (!g_file_test ((pathname), G_FILE_TEST_IS_DIR)){
      char *tmpdir;
      tmpdir = g_strdup_printf("%s/%s", properties->package_data_dir, pathname);
      g_free(pathname);
      pathname = tmpdir;
      if (!g_file_test ((pathname), G_FILE_TEST_IS_DIR)){
        g_warning("In ImageSet %s, directory %s is not found. Skipping all the ImageSet...", absolutepath, pathname);
        return;
      }
    }
    imageset_directory = g_dir_open (pathname, 0, NULL);
    const gchar * onefile;
    while ((onefile = g_dir_read_name(imageset_directory))){
      if ((g_ascii_strcasecmp (type,"lsdir") != 0) &&
	  (!g_str_has_suffix (onefile, type))){
	continue;
      }
      filename = g_strdup_printf("%s/%s", pathname, onefile);
      if (!g_file_test ((filename), G_FILE_TEST_IS_REGULAR)){
	continue;
      }

      {
	/* Add a % before a % to avoid it being used as a format arg */
	gchar **tmp;
	gchar *filename2;
	if((tmp = g_strsplit(filename, "%", -1)))
	  {
	    filename2 = g_strjoinv("%%", tmp);
	    g_strfreev(tmp);
	    g_free(filename);
	    filename = filename2;
	  }
      }
      imageList = g_slist_prepend (imageList, filename);
    }
    g_dir_close(imageset_directory);
  }

  /* do not display if there is nothing to display */
  if (imageList != NULL) /* g_slist is not empty */
    display_image_set(imageSetName, imageList);

  g_free(absolutepath);
  if(pathname)
    g_free(pathname);
  if(type)
    g_free(type);

  return;
}

static void
parse_doc(xmlDocPtr doc) {
  xmlNodePtr cur;

  cur = xmlDocGetRootElement(doc);
  if (cur == NULL) {
    fprintf(stderr,"empty document\n");
    xmlFreeDoc(doc);
    return;
  }
  cur = cur->xmlChildrenNode;

  while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"ImageSet"))){
      parseImage (doc, cur);
    }

    cur = cur->next;
  }

  return;
}


/* read an xml file into our memory structures and update our view,
 * dump any old data we have in memory if we can load a new set
 *
 * \param fname is an absolute file name
 *
 */
static gboolean
read_xml_file(gchar *fname)
{
  /* pointer to the new doc */
  xmlDocPtr doc;

  g_return_val_if_fail(fname!=NULL, FALSE);

  doc = gc_net_load_xml(fname);

  /* in case something went wrong */
  if(!doc)
    return FALSE;

  if(/* if there is no root element */
     !doc->children ||
     /* if it doesn't have a name */
     !doc->children->name ||
     /* if it isn't the good node */
     g_strcasecmp((gchar *)doc->children->name, "ImageSetRoot")!=0) {
    xmlFreeDoc(doc);
    return FALSE;
  }

  /* parse our document and replace old data */
  parse_doc(doc);

  xmlFreeDoc(doc);

  return TRUE;
}


/** read an xml file into our memory structures and update our view,
 *   dump any old data we have in memory if we can load a new set
 *
 * \return TRUE is the parsing occurs, FALSE instead
 *
 */
static gboolean
read_dataset_directory(gchar *dataset_dir)
{
  GDir *dataset_directory = g_dir_open (dataset_dir, 0, NULL);
  const gchar *fname;
  gchar *absolute_fname;

  if(!dataset_directory)
    return FALSE;

  while ((fname = g_dir_read_name(dataset_directory))) {
    /* skip files without ".xml" */
    if (!g_str_has_suffix (fname,".xml")){
      g_warning("skipping file not in .xml : %s", fname);
      continue;
    }

    absolute_fname = g_strdup_printf("%s/%s", dataset_dir, fname);
    g_warning("Reading dataset file %s", absolute_fname);

    if (!g_file_test ((absolute_fname), G_FILE_TEST_EXISTS))
    {
      g_free(absolute_fname);
      continue;
    }

    /* parse the new file and put the result into newdoc */

    /* pointer to the new doc */
    xmlDocPtr doc;

    doc = xmlParseFile(absolute_fname);

    /* in case something went wrong */
    if(!doc)
    {
      g_free(absolute_fname);
      continue;
    }
    
    if(/* if there is no root element */
       !doc->children ||
       /* if it doesn't have a name */
       !doc->children->name ||
       /* if it isn't the good node */
       g_strcasecmp((gchar *)doc->children->name, "ImageSetRoot")!=0) {
      xmlFreeDoc(doc);
      g_free(absolute_fname);
      continue;
    }

    /* parse our document and replace old data */
    g_warning("Parsing dataset : %s \n", absolute_fname);
    g_free(absolute_fname);
    parse_doc(doc);

    xmlFreeDoc(doc);
  }

  g_dir_close(dataset_directory);

  return TRUE;
}