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

« back to all changes in this revision

Viewing changes to src/boards/draw.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 - draw.c
 
2
 *
 
3
 * Time-stamp: <2001/08/20 00:54:45 bruno>
 
4
 *
 
5
 * Copyright (C) 2000 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
/* libxml includes */
 
23
#include <libxml/tree.h>
 
24
#include <libxml/parser.h>
 
25
 
 
26
#include <math.h>
 
27
 
 
28
#include "gcompris/gcompris.h"
 
29
 
 
30
#define SOUNDLISTFILE PACKAGE
 
31
 
 
32
/* Represent the drawing area */
 
33
static gint drawing_area_x1 = 0;
 
34
static gint drawing_area_y1 = 0;
 
35
static gint drawing_area_x2 = 0;
 
36
static gint drawing_area_y2 = 0;
 
37
 
 
38
/* Set the grid size. If 0 then no grid */
 
39
static gint grid_step = 0;
 
40
static GnomeCanvasItem  *gridItem = NULL;
 
41
static GnomeCanvasItem  *grid_root_item = NULL;
 
42
#define DEFAULT_GRID_STEP 20
 
43
 
 
44
static GcomprisBoard    *gcomprisBoard = NULL;
 
45
static GnomeCanvasItem  *shape_root_item = NULL;
 
46
static GnomeCanvasItem  *draw_root_item = NULL;
 
47
static GnomeCanvasItem  *current_color_item = NULL;
 
48
static gchar            *currentColor = NULL;
 
49
 
 
50
typedef enum
 
51
  {
 
52
    TOOL_RECT           = 0,
 
53
    TOOL_FILLED_RECT    = 1,
 
54
    TOOL_CIRCLE         = 2,
 
55
    TOOL_FILLED_CIRCLE  = 3,
 
56
    TOOL_LINE           = 4,
 
57
    TOOL_POINT          = 5,
 
58
    TOOL_DELETE         = 6,
 
59
    TOOL_FILL           = 7,
 
60
    TOOL_SELECT         = 8,
 
61
    TOOL_GRID           = 9,
 
62
  } ToolList;
 
63
 
 
64
#define NUMBER_Of_TOOL  TOOL_GRID + 1
 
65
 
 
66
static ToolList          currentTool = TOOL_RECT;
 
67
static GnomeCanvasItem  *currentToolItem = NULL;
 
68
 
 
69
// Used to cross reference pixmap for the tools
 
70
static char *tool_pixmap_name[] =
 
71
  {
 
72
    "draw/tool-rectangle.png", "draw/tool-rectangle_on.png", 
 
73
    "draw/tool-filledrectangle.png", "draw/tool-filledrectangle_on.png", 
 
74
    "draw/tool-circle.png", "draw/tool-circle_on.png", 
 
75
    "draw/tool-filledcircle.png", "draw/tool-filledcircle_on.png", 
 
76
    "draw/tool-line.png", "draw/tool-line_on.png", 
 
77
    "draw/tool-point.png", "draw/tool-point_on.png", 
 
78
    "draw/tool-del.png", "draw/tool-del_on.png", 
 
79
    "draw/tool-fill.png", "draw/tool-fill_on.png",
 
80
    "draw/tool-select.png", "draw/tool-select_on.png",
 
81
    "draw/tool-grid.png", "draw/tool-grid_on.png"
 
82
  };
 
83
 
 
84
#define PIXMAP_OFF 0
 
85
#define PIXMAP_ON  1
 
86
 
 
87
/*
 
88
 * A Single Point
 
89
 */
 
90
typedef struct {
 
91
  double           x;
 
92
  double           y;
 
93
} Point;
 
94
 
 
95
/*
 
96
 * Contains the points that represents the anchors
 
97
 */
 
98
typedef struct {
 
99
  GnomeCanvasItem *rootitem;
 
100
  ToolList        tool;
 
101
  GnomeCanvasItem *item;
 
102
  GnomeCanvasItem *nw;
 
103
  GnomeCanvasItem *n;
 
104
  GnomeCanvasItem *ne;
 
105
  GnomeCanvasItem *e;
 
106
  GnomeCanvasItem *w;
 
107
  GnomeCanvasItem *sw;
 
108
  GnomeCanvasItem *s;
 
109
  GnomeCanvasItem *se;
 
110
  double           ref_x1;
 
111
  double           ref_y1;
 
112
  double           ref_x2;
 
113
  double           ref_y2;
 
114
} AnchorsItem;
 
115
 
 
116
typedef enum
 
117
  {
 
118
    ANCHOR_NW           = 1,
 
119
    ANCHOR_N            = 2,
 
120
    ANCHOR_NE           = 3,
 
121
    ANCHOR_E            = 4,
 
122
    ANCHOR_W            = 5,
 
123
    ANCHOR_SW           = 6,
 
124
    ANCHOR_S            = 7,
 
125
    ANCHOR_SE           = 8,
 
126
  } AnchorType;
 
127
 
 
128
#define ANCHOR_COLOR            0x36ede400
 
129
#define DEFAULT_ITEM_SIZE       40
 
130
#define DEFAULT_ANCHOR_SIZE     8
 
131
static AnchorsItem *selected_anchors_item = NULL;
 
132
 
 
133
#define DRAW_WIDTH_PIXELS       6
 
134
 
 
135
#define GRID_COLOR              0x267da400
 
136
 
 
137
static gchar *colorlist [] = 
 
138
  {
 
139
    "black",
 
140
    "brown",
 
141
    "red",
 
142
    "orange",
 
143
    "yellow",
 
144
    "green",
 
145
    "blue",
 
146
    "purple",
 
147
    "grey",
 
148
    "white",
 
149
  };
 
150
 
 
151
static void      start_board (GcomprisBoard *agcomprisBoard);
 
152
static void      pause_board (gboolean pause);
 
153
static void      end_board (void);
 
154
static gboolean  is_our_board (GcomprisBoard *gcomprisBoard);
 
155
static void      config(void);
 
156
 
 
157
static void      draw_destroy_all_items(void);
 
158
static void      draw_next_level(void);
 
159
static void      display_color_selector(GnomeCanvasGroup *parent);
 
160
static void      display_tool_selector(GnomeCanvasGroup *parent);
 
161
static void      display_drawing_area(GnomeCanvasGroup *parent);
 
162
static void      display_grid(gboolean status);
 
163
static gint      color_event(GnomeCanvasItem *item, GdkEvent *event, gchar *color);
 
164
static gint      tool_event(GnomeCanvasItem *item, GdkEvent *event, gint tool);
 
165
static gint      item_event(GnomeCanvasItem *item, GdkEvent *event, void *shape);
 
166
static gint      item_event_resize(GnomeCanvasItem *item, GdkEvent *event, AnchorsItem *anchorsItem);
 
167
static gint      item_event_move(GnomeCanvasItem *item, GdkEvent *event, AnchorsItem *anchorsItem);
 
168
static void      highlight_color_item(GnomeCanvasItem *item);
 
169
static guint     get_tool_cursor(ToolList tool);
 
170
static guint     get_resize_cursor(AnchorType anchor);
 
171
static void      realign_to_grid(GnomeCanvasItem *item);
 
172
static void      snap_to_grid(double *x, double *y);
 
173
 
 
174
/* Description of this plugin */
 
175
BoardPlugin menu_bp =
 
176
  {
 
177
    NULL,
 
178
    NULL,
 
179
    N_("A simple vector drawing tool"),
 
180
    N_("Creative board where you can freely draw"),
 
181
    "Bruno Coudoin <bruno.coudoin@free.fr>",
 
182
    NULL,
 
183
    NULL,
 
184
    NULL,
 
185
    NULL,
 
186
    start_board,
 
187
    pause_board,
 
188
    end_board,
 
189
    is_our_board,
 
190
    NULL,
 
191
    NULL,
 
192
    NULL,
 
193
    config,
 
194
    NULL
 
195
  };
 
196
 
 
197
/*
 
198
 * Main entry point mandatory for each Gcompris's game
 
199
 * ---------------------------------------------------
 
200
 *
 
201
 */
 
202
 
 
203
BoardPlugin 
 
204
*get_bplugin_info(void)
 
205
{
 
206
  return &menu_bp;
 
207
}
 
208
 
 
209
/*
 
210
 * in : boolean TRUE = PAUSE : FALSE = CONTINUE
 
211
 *
 
212
 */
 
213
static void pause_board (gboolean pause)
 
214
{
 
215
 
 
216
}
 
217
 
 
218
/*
 
219
 */
 
220
static void start_board (GcomprisBoard *agcomprisBoard)
 
221
{
 
222
 
 
223
  if(agcomprisBoard!=NULL)
 
224
    {
 
225
      gcomprisBoard=agcomprisBoard;
 
226
 
 
227
      gcompris_set_background(gnome_canvas_root(gcomprisBoard->canvas), "draw/draw-bg.jpg");
 
228
 
 
229
 
 
230
      /* set initial values for this level */
 
231
      gcomprisBoard->level = 1;
 
232
      gcomprisBoard->maxlevel=1;
 
233
      gcompris_bar_set(0);
 
234
 
 
235
      gcomprisBoard->number_of_sublevel=0;
 
236
      gcomprisBoard->sublevel = 0;
 
237
 
 
238
      drawing_area_x1 = 124;
 
239
      drawing_area_y1 = 20;
 
240
      drawing_area_x2 = gcomprisBoard->width  - 19;
 
241
      drawing_area_y2 = gcomprisBoard->height - 76;
 
242
 
 
243
      gcompris_bar_set(0);
 
244
 
 
245
      draw_next_level();
 
246
 
 
247
      /* Special Init for this board */
 
248
      selected_anchors_item = NULL;
 
249
 
 
250
      pause_board(FALSE);
 
251
 
 
252
    }
 
253
 
 
254
}
 
255
 
 
256
static void
 
257
end_board ()
 
258
{
 
259
 
 
260
  if(gcomprisBoard!=NULL)
 
261
    {
 
262
      gcompris_set_cursor(GCOMPRIS_DEFAULT_CURSOR);
 
263
      pause_board(TRUE);
 
264
      draw_destroy_all_items();
 
265
      gcomprisBoard->level = 1;       // Restart this game to zero
 
266
    }
 
267
  gcomprisBoard = NULL;
 
268
}
 
269
 
 
270
 
 
271
gboolean
 
272
is_our_board (GcomprisBoard *gcomprisBoard)
 
273
{
 
274
  if (gcomprisBoard)
 
275
    {
 
276
      if(g_strcasecmp(gcomprisBoard->type, "draw")==0)
 
277
        {
 
278
          /* Set the plugin entry */
 
279
          gcomprisBoard->plugin=&menu_bp;
 
280
 
 
281
          return TRUE;
 
282
        }
 
283
    }
 
284
  return FALSE;
 
285
}
 
286
 
 
287
static void
 
288
config ()
 
289
{
 
290
  if(gcomprisBoard!=NULL)
 
291
    {
 
292
      pause_board(TRUE);
 
293
    }
 
294
}
 
295
 
 
296
/*-------------------------------------------------------------------------------*/
 
297
/*-------------------------------------------------------------------------------*/
 
298
/*-------------------------------------------------------------------------------*/
 
299
/*-------------------------------------------------------------------------------*/
 
300
 
 
301
/* set initial values for the next level */
 
302
static void draw_next_level() 
 
303
{
 
304
 
 
305
  gcompris_bar_set_level(gcomprisBoard);
 
306
 
 
307
  draw_destroy_all_items();
 
308
 
 
309
  shape_root_item = \
 
310
    gnome_canvas_item_new (gnome_canvas_root(gcomprisBoard->canvas),
 
311
                           gnome_canvas_group_get_type (),
 
312
                           "x", (double)0,
 
313
                           "y", (double)0,
 
314
                           NULL);
 
315
 
 
316
  display_color_selector(GNOME_CANVAS_GROUP(shape_root_item));
 
317
  display_tool_selector(GNOME_CANVAS_GROUP(shape_root_item));
 
318
  display_drawing_area(GNOME_CANVAS_GROUP(shape_root_item));
 
319
 
 
320
  display_grid(TRUE);
 
321
}
 
322
 
 
323
 
 
324
static void display_drawing_area(GnomeCanvasGroup *parent)
 
325
{
 
326
  draw_root_item = gnome_canvas_item_new (parent,
 
327
                                          gnome_canvas_rect_get_type (),
 
328
                                          "x1", (double) drawing_area_x1,
 
329
                                          "y1", (double) drawing_area_y1,
 
330
                                          "x2", (double) drawing_area_x2,
 
331
                                          "y2", (double) drawing_area_y2,
 
332
                                          "fill_color", "white",
 
333
                                          NULL);
 
334
  gtk_signal_connect(GTK_OBJECT(draw_root_item), "event",
 
335
                     (GtkSignalFunc) item_event,
 
336
                     NULL);
 
337
  
 
338
}
 
339
 
 
340
static void display_color_selector(GnomeCanvasGroup *parent)
 
341
{
 
342
  GdkPixbuf *pixmap;
 
343
  GnomeCanvasItem *item = NULL;
 
344
  gint x  = 0;
 
345
  gint y  = 0;
 
346
  gint x1 = 0;
 
347
  gint c  = 0;
 
348
 
 
349
  pixmap = gcompris_load_pixmap("draw/color-selector.jpg");
 
350
  if(pixmap)
 
351
    {
 
352
      x = (drawing_area_x2 - drawing_area_x1
 
353
           - gdk_pixbuf_get_width(pixmap))/2 
 
354
        + drawing_area_x1;
 
355
      
 
356
      y = gcomprisBoard->height - gdk_pixbuf_get_height(pixmap) - 5;
 
357
 
 
358
      item = gnome_canvas_item_new (parent,
 
359
                                    gnome_canvas_pixbuf_get_type (),
 
360
                                    "pixbuf", pixmap, 
 
361
                                    "x", (double) x,
 
362
                                    "y", (double) y,
 
363
                                    NULL);
 
364
      gdk_pixbuf_unref(pixmap);
 
365
    }
 
366
 
 
367
  for(x1=x+26; x1<(x+26)+55*10; x1+=55)
 
368
    {
 
369
      item = gnome_canvas_item_new (parent,
 
370
                                    gnome_canvas_rect_get_type (),
 
371
                                    "x1", (double) x1,
 
372
                                    "y1", (double) y + 8,
 
373
                                    "x2", (double) x1 + 50,
 
374
                                    "y2", (double) y + gdk_pixbuf_get_height(pixmap) - 8,
 
375
                                    "fill_color", colorlist[c],
 
376
                                    NULL);
 
377
 
 
378
      gtk_signal_connect(GTK_OBJECT(item), "event",
 
379
                         (GtkSignalFunc) color_event,
 
380
                         colorlist[c]);
 
381
 
 
382
      if(c==0)
 
383
        highlight_color_item(item);
 
384
 
 
385
      c++;
 
386
    }
 
387
 
 
388
  currentColor = colorlist[0];
 
389
}
 
390
 
 
391
#define SELECTOR_VERTICAL_SPACING 60
 
392
 
 
393
static void display_tool_selector(GnomeCanvasGroup *parent)
 
394
{
 
395
  GdkPixbuf *pixmap;
 
396
  GnomeCanvasItem *item = NULL;
 
397
  gint x   = 0;
 
398
  gint x2  = 0;
 
399
  gint y   = 0;
 
400
  guint toolIndex = 0;
 
401
 
 
402
  pixmap = gcompris_load_pixmap("draw/tool-selector.jpg");
 
403
  if(pixmap)
 
404
    {
 
405
      x = 3;
 
406
      y = (drawing_area_y2 - drawing_area_y1 - gdk_pixbuf_get_height(pixmap)) / 2
 
407
        +  drawing_area_y1;
 
408
      item = gnome_canvas_item_new (parent,
 
409
                                    gnome_canvas_pixbuf_get_type (),
 
410
                                    "pixbuf", pixmap, 
 
411
                                    "x", (double) x,
 
412
                                    "y", (double) y,
 
413
                                    NULL);
 
414
      gdk_pixbuf_unref(pixmap);
 
415
    }
 
416
 
 
417
  y += 15;
 
418
  x  = 10;
 
419
  x2 = 55;
 
420
  pixmap = gcompris_load_pixmap(tool_pixmap_name[0 + PIXMAP_ON]);
 
421
  if(pixmap)
 
422
    {
 
423
      item = gnome_canvas_item_new (parent,
 
424
                                    gnome_canvas_pixbuf_get_type (),
 
425
                                    "pixbuf", pixmap, 
 
426
                                    "x", (double) x,
 
427
                                    "y", (double) y,
 
428
                                    NULL);
 
429
      gdk_pixbuf_unref(pixmap);
 
430
 
 
431
      gtk_signal_connect(GTK_OBJECT(item), "event",
 
432
                         (GtkSignalFunc) tool_event,
 
433
                         (void *)TOOL_RECT);
 
434
 
 
435
    }
 
436
  currentTool = TOOL_RECT;
 
437
  currentToolItem = item;
 
438
 
 
439
  for( toolIndex = 1 ; toolIndex < NUMBER_Of_TOOL ; toolIndex++)
 
440
    {
 
441
      y += (toolIndex%2 == 0 ? SELECTOR_VERTICAL_SPACING : 0);
 
442
      pixmap = gcompris_load_pixmap(tool_pixmap_name[(2*toolIndex) + PIXMAP_OFF]);
 
443
 
 
444
      if(pixmap)
 
445
        {
 
446
          item = gnome_canvas_item_new (parent,
 
447
                                        gnome_canvas_pixbuf_get_type (),
 
448
                                        "pixbuf", pixmap, 
 
449
                                        "x", (double) (toolIndex%2 == 0 ? x : x2),
 
450
                                        "y", (double) y,
 
451
                                        NULL);
 
452
          gdk_pixbuf_unref(pixmap);
 
453
 
 
454
          if(toolIndex == TOOL_GRID)
 
455
            gridItem = item;
 
456
 
 
457
          gtk_signal_connect(GTK_OBJECT(item), "event",
 
458
                             (GtkSignalFunc) tool_event,
 
459
                             (void *)toolIndex);
 
460
        }
 
461
    }
 
462
}
 
463
 
 
464
/*
 
465
 * Request the display of the grid if status is true
 
466
 *
 
467
 */
 
468
static void display_grid(gboolean status)
 
469
{
 
470
  guint x, y;
 
471
  GdkPixbuf *pixmap = NULL;
 
472
 
 
473
  pixmap = gcompris_load_pixmap(tool_pixmap_name[(TOOL_GRID*2) + 
 
474
                                                 (status == TRUE ? PIXMAP_ON : PIXMAP_OFF)]);
 
475
  if(pixmap)
 
476
    {
 
477
      gnome_canvas_item_set (gridItem,
 
478
                             "pixbuf", pixmap,
 
479
                             NULL);
 
480
      gdk_pixbuf_unref(pixmap);
 
481
    }
 
482
 
 
483
  if(!status)
 
484
    {
 
485
      grid_step = 0;
 
486
 
 
487
      if(grid_root_item!=NULL)
 
488
        gnome_canvas_item_hide(grid_root_item);
 
489
      return;
 
490
    }
 
491
 
 
492
  grid_step = DEFAULT_GRID_STEP;
 
493
 
 
494
  if(grid_root_item!=NULL)
 
495
    {
 
496
      gnome_canvas_item_show(grid_root_item);
 
497
      return;
 
498
    }
 
499
 
 
500
  /* Creation of the grid */
 
501
 
 
502
  grid_root_item = \
 
503
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(shape_root_item),
 
504
                           gnome_canvas_group_get_type (),
 
505
                           "x", (double)0,
 
506
                           "y", (double)0,
 
507
                           NULL);
 
508
  gnome_canvas_item_raise_to_top(grid_root_item);
 
509
  //  gnome_canvas_item_raise(grid_root_item, 50);
 
510
 
 
511
  for( x = drawing_area_x1 ; x < drawing_area_x2 ; x += grid_step)
 
512
    {
 
513
      GnomeCanvasPoints *points;
 
514
      GnomeCanvasItem   *item;
 
515
 
 
516
      points = gnome_canvas_points_new(2);
 
517
      points->coords[0] = (double) x;
 
518
      points->coords[1] = (double) drawing_area_y1;
 
519
      points->coords[2] = (double) x;
 
520
      points->coords[3] = (double) drawing_area_y2;
 
521
      
 
522
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(grid_root_item),
 
523
                                    gnome_canvas_line_get_type (),
 
524
                                    "points", points,
 
525
                                    "fill_color_rgba", GRID_COLOR,
 
526
                                    "width_pixels", 1,
 
527
                                    NULL);
 
528
 
 
529
      gtk_signal_connect(GTK_OBJECT(item), "event",
 
530
                         (GtkSignalFunc) item_event,
 
531
                         NULL);
 
532
 
 
533
      gnome_canvas_points_unref(points);
 
534
 
 
535
    }
 
536
 
 
537
 
 
538
  for( y = drawing_area_y1 ; y < drawing_area_y2 ; y += grid_step)
 
539
    {
 
540
      GnomeCanvasPoints *points;
 
541
      GnomeCanvasItem   *item;
 
542
 
 
543
      points = gnome_canvas_points_new(2);
 
544
      points->coords[0] = (double) drawing_area_x1;
 
545
      points->coords[1] = (double) y;
 
546
      points->coords[2] = (double) drawing_area_x2;
 
547
      points->coords[3] = (double) y;
 
548
      
 
549
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(grid_root_item),
 
550
                                    gnome_canvas_line_get_type (),
 
551
                                    "points", points,
 
552
                                    "fill_color_rgba", GRID_COLOR,
 
553
                                    "width_pixels", 1,
 
554
                                    NULL);
 
555
 
 
556
      gtk_signal_connect(GTK_OBJECT(item), "event",
 
557
                         (GtkSignalFunc) item_event,
 
558
                         NULL);
 
559
 
 
560
      gnome_canvas_points_unref(points);
 
561
 
 
562
    }
 
563
}
 
564
 
 
565
/*
 
566
 * Given an object, realign it to the grid
 
567
 * if the grid is on
 
568
 * FIXME: Does not work yet as expected
 
569
 */
 
570
static void realign_to_grid(GnomeCanvasItem *item)
 
571
{
 
572
 
 
573
  if(grid_step!=0)
 
574
    {
 
575
      double x1, y1, x2, y2;
 
576
      double xsnap1, ysnap1;
 
577
      
 
578
      gnome_canvas_item_get_bounds  (item,
 
579
                                     &x1,
 
580
                                     &y1,
 
581
                                     &x2,
 
582
                                     &y2);
 
583
      
 
584
      xsnap1 = x1;
 
585
      ysnap1 = y1;
 
586
      snap_to_grid(&xsnap1, &ysnap1);
 
587
      
 
588
      // Realign our object on the grid
 
589
      gnome_canvas_item_move(item, 
 
590
                             x1 - xsnap1,
 
591
                             y1 - ysnap1);
 
592
    }
 
593
}
 
594
 
 
595
/*
 
596
 * Given a pair (x,y) rewrite them so that (x,y) is on a grid node
 
597
 * Do nothing if grid_step is 0
 
598
 */
 
599
static void snap_to_grid(double *x, double *y)
 
600
{
 
601
  guint tmp;
 
602
 
 
603
  if(grid_step==0)
 
604
    return;
 
605
 
 
606
  tmp = (guint)((*x+(grid_step/2)) - drawing_area_x1)/grid_step;
 
607
  *x = (double)drawing_area_x1 + tmp*grid_step;
 
608
 
 
609
  tmp = (guint)((*y+(grid_step/2)) - drawing_area_y1)/grid_step;
 
610
  *y = (double)drawing_area_y1 + tmp*grid_step;
 
611
}
 
612
 
 
613
/* Destroy all the items */
 
614
static void draw_destroy_all_items()
 
615
{
 
616
  
 
617
  /* Deleting the root item automatically deletes children items */
 
618
  if(shape_root_item!=NULL)
 
619
    gtk_object_destroy (GTK_OBJECT(shape_root_item));
 
620
  shape_root_item = NULL;
 
621
 
 
622
  if(grid_root_item!=NULL)
 
623
    gtk_object_destroy (GTK_OBJECT(grid_root_item));
 
624
  grid_root_item = NULL;
 
625
 
 
626
  current_color_item = NULL;
 
627
}
 
628
 
 
629
/*
 
630
 * Return the proper GDK cursor based on the given tool
 
631
 *
 
632
 */
 
633
static guint get_tool_cursor(ToolList tool)
 
634
{
 
635
  switch(tool)
 
636
    {
 
637
    case TOOL_RECT:
 
638
      return(GCOMPRIS_FILLRECT_CURSOR);
 
639
      break;
 
640
    case TOOL_FILLED_RECT:
 
641
      return(GCOMPRIS_RECT_CURSOR);
 
642
      break;
 
643
    case TOOL_CIRCLE:
 
644
      return(GCOMPRIS_CIRCLE_CURSOR);
 
645
      break;
 
646
    case TOOL_FILLED_CIRCLE:
 
647
      return(GCOMPRIS_FILLCIRCLE_CURSOR);
 
648
      break;
 
649
    case TOOL_LINE:
 
650
      return(GCOMPRIS_LINE_CURSOR);
 
651
      break;
 
652
    case TOOL_POINT:
 
653
      return(GCOMPRIS_CIRCLE_CURSOR);
 
654
      break;
 
655
    case TOOL_FILL:
 
656
      return(GCOMPRIS_FILL_CURSOR);
 
657
      break;
 
658
    case TOOL_DELETE:
 
659
      return(GCOMPRIS_DEL_CURSOR);
 
660
      break;
 
661
    case TOOL_SELECT:
 
662
      return(GCOMPRIS_SELECT_CURSOR);
 
663
      break;
 
664
    default:
 
665
      return(GCOMPRIS_DEFAULT_CURSOR);
 
666
      break;
 
667
    }
 
668
  return(0);
 
669
}
 
670
 
 
671
static gint
 
672
tool_event(GnomeCanvasItem *item, GdkEvent *event, gint tool)
 
673
{
 
674
  GdkPixbuf *pixmap = NULL;
 
675
 
 
676
  switch (event->type) 
 
677
    {
 
678
    case GDK_BUTTON_PRESS:
 
679
      switch(event->button.button) 
 
680
        {
 
681
        case 1:
 
682
 
 
683
          if(tool == TOOL_GRID)
 
684
            {
 
685
              display_grid((grid_step==0 ? TRUE : FALSE));
 
686
              return TRUE;
 
687
            }
 
688
 
 
689
          if(currentToolItem)
 
690
            {
 
691
              pixmap = gcompris_load_pixmap(tool_pixmap_name[(currentTool*2) + PIXMAP_OFF]);
 
692
              if(pixmap)
 
693
                {
 
694
                  gnome_canvas_item_set (currentToolItem,
 
695
                                         "pixbuf", pixmap,
 
696
                                         NULL);
 
697
                  gdk_pixbuf_unref(pixmap);
 
698
                }
 
699
            }
 
700
 
 
701
          currentTool = tool;
 
702
          currentToolItem = item;
 
703
 
 
704
          pixmap = gcompris_load_pixmap(tool_pixmap_name[(currentTool*2) + PIXMAP_ON]);
 
705
          if(pixmap)
 
706
            {
 
707
              gnome_canvas_item_set (item,
 
708
                                     "pixbuf", pixmap,
 
709
                                     NULL);
 
710
              gdk_pixbuf_unref(pixmap);
 
711
            }
 
712
        default:
 
713
          break;
 
714
        }
 
715
    default:
 
716
      break;
 
717
    }
 
718
  return FALSE;
 
719
}
 
720
 
 
721
static void highlight_color_item(GnomeCanvasItem *item)
 
722
{
 
723
  // Highligh the selected color
 
724
  if(current_color_item)
 
725
    gnome_canvas_item_set (current_color_item,
 
726
                           "outline_color_rgba", 0x07a3e080,
 
727
                           "width_pixels", 0,
 
728
                           NULL);
 
729
 
 
730
  gnome_canvas_item_set (item,
 
731
                         "outline_color_rgba", 0xaef45880,
 
732
                         "width_pixels", 3,
 
733
                         NULL);
 
734
  current_color_item=item;
 
735
}
 
736
 
 
737
static gint
 
738
color_event(GnomeCanvasItem *item, GdkEvent *event, gchar *color)
 
739
{
 
740
  if(color==NULL)
 
741
    return FALSE;
 
742
 
 
743
  switch (event->type) 
 
744
    {
 
745
    case GDK_BUTTON_PRESS:
 
746
      switch(event->button.button) 
 
747
        {
 
748
        case 1:
 
749
          currentColor = color;
 
750
          highlight_color_item(item);
 
751
          gcompris_play_ogg(color, NULL);
 
752
          break;
 
753
        default:
 
754
          break;
 
755
        }
 
756
    default:
 
757
      break;
 
758
    }
 
759
  return FALSE;
 
760
}
 
761
 
 
762
static void display_anchors(AnchorsItem *anchorsItem, gboolean visible)
 
763
{
 
764
  if(visible)
 
765
    {
 
766
      if(anchorsItem->nw)
 
767
          gnome_canvas_item_show(anchorsItem->nw);
 
768
 
 
769
      if(anchorsItem->n)
 
770
          gnome_canvas_item_show(anchorsItem->n);
 
771
 
 
772
      if(anchorsItem->ne)
 
773
          gnome_canvas_item_show(anchorsItem->ne);
 
774
 
 
775
      if(anchorsItem->w)
 
776
          gnome_canvas_item_show(anchorsItem->w);
 
777
 
 
778
      if(anchorsItem->e)
 
779
          gnome_canvas_item_show(anchorsItem->e);
 
780
 
 
781
      if(anchorsItem->sw)
 
782
          gnome_canvas_item_show(anchorsItem->sw);
 
783
 
 
784
      if(anchorsItem->s)
 
785
          gnome_canvas_item_show(anchorsItem->s);
 
786
 
 
787
      if(anchorsItem->se)
 
788
          gnome_canvas_item_show(anchorsItem->se);
 
789
 
 
790
    }
 
791
  else
 
792
    {
 
793
      if(anchorsItem->nw)
 
794
      gnome_canvas_item_hide(anchorsItem->nw);
 
795
 
 
796
      if(anchorsItem->n)
 
797
      gnome_canvas_item_hide(anchorsItem->n);
 
798
 
 
799
      if(anchorsItem->ne)
 
800
      gnome_canvas_item_hide(anchorsItem->ne);
 
801
 
 
802
      if(anchorsItem->w)
 
803
      gnome_canvas_item_hide(anchorsItem->w);
 
804
 
 
805
      if(anchorsItem->e)
 
806
      gnome_canvas_item_hide(anchorsItem->e);
 
807
 
 
808
      if(anchorsItem->sw)
 
809
      gnome_canvas_item_hide(anchorsItem->sw);
 
810
 
 
811
      if(anchorsItem->s)
 
812
      gnome_canvas_item_hide(anchorsItem->s);
 
813
 
 
814
      if(anchorsItem->se)
 
815
      gnome_canvas_item_hide(anchorsItem->se);
 
816
    }
 
817
}
 
818
 
 
819
static void reset_anchors_line(AnchorsItem *anchorsItem)
 
820
{
 
821
 
 
822
  if(anchorsItem->nw)
 
823
  gnome_canvas_item_set (anchorsItem->nw,
 
824
                         "x1", (double) anchorsItem->ref_x1 - DEFAULT_ANCHOR_SIZE,
 
825
                         "y1", (double) anchorsItem->ref_y1 - DEFAULT_ANCHOR_SIZE,
 
826
                         "x2", (double) anchorsItem->ref_x1 + DEFAULT_ANCHOR_SIZE,
 
827
                         "y2", (double) anchorsItem->ref_y1 + DEFAULT_ANCHOR_SIZE,
 
828
                         NULL);
 
829
 
 
830
  if(anchorsItem->se)
 
831
  gnome_canvas_item_set (anchorsItem->se,
 
832
                         "x1", (double) anchorsItem->ref_x2 - DEFAULT_ANCHOR_SIZE,
 
833
                         "y1", (double) anchorsItem->ref_y2 - DEFAULT_ANCHOR_SIZE,
 
834
                         "x2", (double) anchorsItem->ref_x2 + DEFAULT_ANCHOR_SIZE,
 
835
                         "y2", (double) anchorsItem->ref_y2 + DEFAULT_ANCHOR_SIZE,
 
836
                         NULL);
 
837
 
 
838
}
 
839
 
 
840
 
 
841
static void reset_anchors_bounded(AnchorsItem *anchorsItem)
 
842
{
 
843
  double x1, x2, y1, y2;
 
844
  
 
845
  gnome_canvas_item_get_bounds(anchorsItem->item,  &x1, &y1, &x2, &y2); 
 
846
 
 
847
  if(anchorsItem->nw)
 
848
  gnome_canvas_item_set (anchorsItem->nw,
 
849
                         "x1", (double) x1 - DEFAULT_ANCHOR_SIZE,
 
850
                         "y1", (double) y1 - DEFAULT_ANCHOR_SIZE,
 
851
                         "x2", (double) x1 + DEFAULT_ANCHOR_SIZE,
 
852
                         "y2", (double) y1 + DEFAULT_ANCHOR_SIZE,
 
853
                         NULL);
 
854
 
 
855
  if(anchorsItem->n)
 
856
  gnome_canvas_item_set (anchorsItem->n,
 
857
                         "x1", (double) x1+(x2-x1)/2 - DEFAULT_ANCHOR_SIZE,
 
858
                         "y1", (double) y1 - DEFAULT_ANCHOR_SIZE,
 
859
                         "x2", (double) x1+(x2-x1)/2 + DEFAULT_ANCHOR_SIZE,
 
860
                         "y2", (double) y1 + DEFAULT_ANCHOR_SIZE,
 
861
                         NULL);
 
862
 
 
863
  if(anchorsItem->ne)
 
864
  gnome_canvas_item_set (anchorsItem->ne,
 
865
                         "x1", (double) x2 - DEFAULT_ANCHOR_SIZE,
 
866
                         "y1", (double) y1 - DEFAULT_ANCHOR_SIZE,
 
867
                         "x2", (double) x2 + DEFAULT_ANCHOR_SIZE,
 
868
                         "y2", (double) y1 + DEFAULT_ANCHOR_SIZE,
 
869
                         NULL);
 
870
 
 
871
  if(anchorsItem->e)
 
872
  gnome_canvas_item_set (anchorsItem->e,
 
873
                         "x1", (double) x2 - DEFAULT_ANCHOR_SIZE,
 
874
                         "y1", (double) y1+(y2-y1)/2 - DEFAULT_ANCHOR_SIZE,
 
875
                         "x2", (double) x2 + DEFAULT_ANCHOR_SIZE,
 
876
                         "y2", (double) y1+(y2-y1)/2 + DEFAULT_ANCHOR_SIZE,
 
877
                         NULL);
 
878
 
 
879
  if(anchorsItem->w)
 
880
  gnome_canvas_item_set (anchorsItem->w,
 
881
                         "x1", (double) x1 - DEFAULT_ANCHOR_SIZE,
 
882
                         "y1", (double) y1+(y2-y1)/2 - DEFAULT_ANCHOR_SIZE,
 
883
                         "x2", (double) x1 + DEFAULT_ANCHOR_SIZE,
 
884
                         "y2", (double) y1+(y2-y1)/2 + DEFAULT_ANCHOR_SIZE,
 
885
                         NULL);
 
886
 
 
887
  if(anchorsItem->sw)
 
888
  gnome_canvas_item_set (anchorsItem->sw,
 
889
                         "x1", (double) x1 - DEFAULT_ANCHOR_SIZE,
 
890
                         "y1", (double) y2 - DEFAULT_ANCHOR_SIZE,
 
891
                         "x2", (double) x1 + DEFAULT_ANCHOR_SIZE,
 
892
                         "y2", (double) y2 + DEFAULT_ANCHOR_SIZE,
 
893
                         NULL);
 
894
 
 
895
  if(anchorsItem->se)
 
896
  gnome_canvas_item_set (anchorsItem->se,
 
897
                         "x1", (double) x2 - DEFAULT_ANCHOR_SIZE,
 
898
                         "y1", (double) y2 - DEFAULT_ANCHOR_SIZE,
 
899
                         "x2", (double) x2 + DEFAULT_ANCHOR_SIZE,
 
900
                         "y2", (double) y2 + DEFAULT_ANCHOR_SIZE,
 
901
                         NULL);
 
902
 
 
903
  if(anchorsItem->s)
 
904
  gnome_canvas_item_set (anchorsItem->s,
 
905
                         "x1", (double) x1+(x2-x1)/2 - DEFAULT_ANCHOR_SIZE,
 
906
                         "y1", (double) y2 - DEFAULT_ANCHOR_SIZE,
 
907
                         "x2", (double) x1+(x2-x1)/2 + DEFAULT_ANCHOR_SIZE,
 
908
                         "y2", (double) y2 + DEFAULT_ANCHOR_SIZE,
 
909
                         NULL);
 
910
 
 
911
}
 
912
 
 
913
static void resize_item(AnchorsItem *anchorsItem, AnchorType anchor, double x, double y)
 
914
{
 
915
  double                 x1, y1, x2, y2;
 
916
  GnomeCanvasPoints     *points = NULL;
 
917
  GnomeCanvasItem       *item = NULL;
 
918
 
 
919
  item = anchorsItem->item;
 
920
  gnome_canvas_item_get_bounds  (item,
 
921
                                 &x1,
 
922
                                 &y1,
 
923
                                 &x2,
 
924
                                 &y2);
 
925
  
 
926
 
 
927
  if(GNOME_IS_CANVAS_RECT(item) || GNOME_IS_CANVAS_ELLIPSE(item))
 
928
    {
 
929
      switch(anchor)
 
930
        {
 
931
        case ANCHOR_E:
 
932
          if(x>x1+1)
 
933
            gnome_canvas_item_set (item,
 
934
                                   "x2", (double) x,
 
935
                                   NULL);
 
936
          break;
 
937
        case ANCHOR_W:
 
938
          if(x<x2-1)
 
939
            gnome_canvas_item_set (item,
 
940
                                   "x1", (double) x,
 
941
                                   NULL);
 
942
          break;
 
943
        case ANCHOR_N:
 
944
          if(y<y2-1)
 
945
            gnome_canvas_item_set (item,
 
946
                                   "y1", (double) y,
 
947
                                   NULL);
 
948
          break;
 
949
        case ANCHOR_S:
 
950
          if(y>y1+1)
 
951
            gnome_canvas_item_set (item,
 
952
                                   "y2", (double) y,
 
953
                                   NULL);
 
954
          break;
 
955
        case ANCHOR_NW:
 
956
          if(y<y2-1)
 
957
            gnome_canvas_item_set (item,
 
958
                                   "y1", (double) y,
 
959
                                   NULL);
 
960
          if(x<x2-1)
 
961
            gnome_canvas_item_set (item,
 
962
                                   "x1", (double) x,
 
963
                                   NULL);
 
964
          break;
 
965
        case ANCHOR_NE:
 
966
          if(y<y2-1)
 
967
            gnome_canvas_item_set (item,
 
968
                                   "y1", (double) y,
 
969
                                   NULL);
 
970
          if(x>x1+1)
 
971
            gnome_canvas_item_set (item,
 
972
                                   "x2", (double) x,
 
973
                                   NULL);
 
974
          break;
 
975
        case ANCHOR_SW:
 
976
          if(y>y1+1)
 
977
            gnome_canvas_item_set (item,
 
978
                                   "y2", (double) y,
 
979
                                   NULL);
 
980
          if(x<x2-1)
 
981
            gnome_canvas_item_set (item,
 
982
                                   "x1", (double) x,
 
983
                                   NULL);
 
984
          break;
 
985
        case ANCHOR_SE:
 
986
          if(y>y1+1)
 
987
            gnome_canvas_item_set (item,
 
988
                                   "y2", (double) y,
 
989
                                   NULL);
 
990
          if(x>x1+1)
 
991
            gnome_canvas_item_set (item,
 
992
                                   "x2", (double) x,
 
993
                                   NULL);
 
994
          break;
 
995
        }
 
996
 
 
997
      reset_anchors_bounded(anchorsItem);
 
998
    }
 
999
  else if(GNOME_IS_CANVAS_LINE(item))
 
1000
    {
 
1001
 
 
1002
      /* I don't know why, I need to shrink the bounding box */
 
1003
      x1 += DRAW_WIDTH_PIXELS;
 
1004
      y1 += DRAW_WIDTH_PIXELS;
 
1005
      x2 -= DRAW_WIDTH_PIXELS;
 
1006
      y2 -= DRAW_WIDTH_PIXELS;
 
1007
 
 
1008
      switch(anchor)
 
1009
        {
 
1010
        case ANCHOR_NW:
 
1011
          points = gnome_canvas_points_new(2);
 
1012
          points->coords[0] = (double) x;
 
1013
          points->coords[1] = (double) y;
 
1014
          points->coords[2] = (double) anchorsItem->ref_x2;
 
1015
          points->coords[3] = (double) anchorsItem->ref_y2;
 
1016
          break;
 
1017
        case ANCHOR_SE:
 
1018
          points = gnome_canvas_points_new(2);
 
1019
          points->coords[0] = (double) anchorsItem->ref_x1;
 
1020
          points->coords[1] = (double) anchorsItem->ref_y1;
 
1021
          points->coords[2] = (double) x;
 
1022
          points->coords[3] = (double) y;
 
1023
          break;
 
1024
        default:
 
1025
          break;
 
1026
        }
 
1027
  
 
1028
      if(points)
 
1029
        {
 
1030
          anchorsItem->ref_x1 = points->coords[0];
 
1031
          anchorsItem->ref_y1 = points->coords[1];
 
1032
          anchorsItem->ref_x2 = points->coords[2];
 
1033
          anchorsItem->ref_y2 = points->coords[3];
 
1034
          
 
1035
          gnome_canvas_item_set (item,
 
1036
                                 "points", points,
 
1037
                                 NULL);
 
1038
          
 
1039
          gnome_canvas_points_unref(points);
 
1040
          
 
1041
          reset_anchors_line(anchorsItem);
 
1042
        }
 
1043
    }
 
1044
}
 
1045
 
 
1046
/*
 
1047
 * Set the color of the item based on the tool with with it has been
 
1048
 * created
 
1049
 * 
 
1050
 */
 
1051
static void set_item_color(AnchorsItem *anchorsItem, gchar *color)
 
1052
{
 
1053
  GnomeCanvasItem *item = anchorsItem->item;
 
1054
 
 
1055
  switch(anchorsItem->tool)
 
1056
    {
 
1057
    case TOOL_RECT:
 
1058
    case TOOL_CIRCLE:
 
1059
      gnome_canvas_item_set (GNOME_CANVAS_ITEM(item),
 
1060
                             "outline_color", currentColor,
 
1061
                             NULL);
 
1062
      break;
 
1063
    case TOOL_FILLED_RECT:
 
1064
    case TOOL_FILLED_CIRCLE:
 
1065
    case TOOL_POINT:
 
1066
      gnome_canvas_item_set (GNOME_CANVAS_ITEM(item),
 
1067
                             "fill_color", currentColor,
 
1068
                             NULL);
 
1069
      break;
 
1070
    case TOOL_LINE:
 
1071
      gnome_canvas_item_set (GNOME_CANVAS_ITEM(item),
 
1072
                             "fill_color", currentColor,
 
1073
                             NULL);
 
1074
      break;
 
1075
    default:
 
1076
      break;
 
1077
    }
 
1078
}
 
1079
 
 
1080
static GnomeCanvasItem *create_item(double x, double y)
 
1081
{
 
1082
  GnomeCanvasItem *item = NULL;
 
1083
  GnomeCanvasPoints* points = NULL;
 
1084
  GnomeCanvasItem *item_root_item = NULL;
 
1085
  guint item_size_x = 0;
 
1086
  guint item_size_y = 0;
 
1087
 
 
1088
  item_root_item = \
 
1089
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(shape_root_item),
 
1090
                           gnome_canvas_group_get_type (),
 
1091
                           "x", (double)0,
 
1092
                           "y", (double)0,
 
1093
                           NULL);
 
1094
 
 
1095
  item_size_x = MIN(DEFAULT_ITEM_SIZE, drawing_area_x2 - x);
 
1096
  item_size_y = MIN(DEFAULT_ITEM_SIZE, drawing_area_y2 - y);
 
1097
 
 
1098
  switch(currentTool)
 
1099
    {
 
1100
    case TOOL_RECT:
 
1101
      // This is a rectangle
 
1102
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1103
                                    gnome_canvas_rect_get_type (),
 
1104
                                    "x1", (double) x,
 
1105
                                    "y1", (double) y,
 
1106
                                    "x2", (double) x + item_size_x,
 
1107
                                    "y2", (double) y + item_size_y,
 
1108
                                    "outline_color", currentColor,
 
1109
                                    "width_pixels", DRAW_WIDTH_PIXELS,
 
1110
                                    NULL);
 
1111
      break;
 
1112
    case TOOL_FILLED_RECT:
 
1113
      // This is a filled rectangle
 
1114
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1115
                                    gnome_canvas_rect_get_type (),
 
1116
                                    "x1", (double) x,
 
1117
                                    "y1", (double) y,
 
1118
                                    "x2", (double) x + item_size_x,
 
1119
                                    "y2", (double) y + item_size_y,
 
1120
                                    "fill_color", currentColor,
 
1121
                                    NULL);
 
1122
      break;      
 
1123
    case TOOL_POINT:
 
1124
      // This is a point
 
1125
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1126
                                    gnome_canvas_ellipse_get_type (),
 
1127
                                    "x1", (double) x - DRAW_WIDTH_PIXELS,
 
1128
                                    "y1", (double) y - DRAW_WIDTH_PIXELS,
 
1129
                                    "x2", (double) x + DRAW_WIDTH_PIXELS,
 
1130
                                    "y2", (double) y + DRAW_WIDTH_PIXELS,
 
1131
                                    "fill_color", currentColor,
 
1132
                                    NULL);
 
1133
      break;      
 
1134
    case TOOL_CIRCLE:
 
1135
      // This is an ellipse
 
1136
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1137
                                    gnome_canvas_ellipse_get_type (),
 
1138
                                    "x1", (double) x,
 
1139
                                    "y1", (double) y,
 
1140
                                    "x2", (double) x + item_size_x,
 
1141
                                    "y2", (double) y + item_size_y,
 
1142
                                    "outline_color", currentColor,
 
1143
                                    "width_pixels", DRAW_WIDTH_PIXELS,
 
1144
                                    NULL);
 
1145
      break;
 
1146
    case TOOL_FILLED_CIRCLE:
 
1147
      // This is a filled ellipse
 
1148
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1149
                                    gnome_canvas_ellipse_get_type (),
 
1150
                                    "x1", (double) x,
 
1151
                                    "y1", (double) y,
 
1152
                                    "x2", (double) x + item_size_x,
 
1153
                                    "y2", (double) y + item_size_y,
 
1154
                                    "fill_color", currentColor,
 
1155
                                    NULL);
 
1156
      break;      
 
1157
    case TOOL_LINE:
 
1158
      // This is a line
 
1159
      points = gnome_canvas_points_new(2);
 
1160
      points->coords[0] = (double) x;
 
1161
      points->coords[1] = (double) y;
 
1162
      points->coords[2] = (double) x + item_size_x;
 
1163
      points->coords[3] = (double) y + item_size_y;
 
1164
 
 
1165
      snap_to_grid(&points->coords[0], &points->coords[1]);
 
1166
      snap_to_grid(&points->coords[2], &points->coords[3]);
 
1167
 
 
1168
      item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1169
                                    gnome_canvas_line_get_type (),
 
1170
                                    "points", points,
 
1171
                                    "fill_color", currentColor,
 
1172
                                    "width_pixels", DRAW_WIDTH_PIXELS,
 
1173
                                    NULL);
 
1174
      break;      
 
1175
    default:
 
1176
      break;
 
1177
    }
 
1178
 
 
1179
  if(item)
 
1180
    {
 
1181
      GnomeCanvasItem           *anchorItem = NULL;
 
1182
      AnchorsItem               *anchorsItem = NULL;
 
1183
 
 
1184
      /* Let the new item be on top */
 
1185
      gnome_canvas_item_raise_to_top(item);
 
1186
 
 
1187
      /* Create the Anchors */
 
1188
      anchorsItem = g_new(AnchorsItem, 1);
 
1189
 
 
1190
      anchorsItem->rootitem = item_root_item;
 
1191
      anchorsItem->item = item;
 
1192
      anchorsItem->tool = currentTool;
 
1193
 
 
1194
      // Keep track of the original size. It helps the resize operation for the line
 
1195
      if(points)
 
1196
        {
 
1197
          anchorsItem->ref_x1 = points->coords[0];
 
1198
          anchorsItem->ref_y1 = points->coords[1];
 
1199
          anchorsItem->ref_x2 = points->coords[2];
 
1200
          anchorsItem->ref_y2 = points->coords[3];
 
1201
          gnome_canvas_points_unref(points);
 
1202
        }
 
1203
 
 
1204
      /*----------------------------------------*/
 
1205
      switch(currentTool)
 
1206
        {
 
1207
        case TOOL_LINE:
 
1208
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1209
                                              gnome_canvas_rect_get_type (),
 
1210
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1211
                                              "outline_color", "black",
 
1212
                                              "width_pixels", 1,
 
1213
                                              NULL);
 
1214
          anchorsItem->nw = anchorItem;
 
1215
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_NW);
 
1216
 
 
1217
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1218
                             (GtkSignalFunc) item_event_resize,
 
1219
                             anchorsItem);
 
1220
          
 
1221
          /*----------------------------------------*/
 
1222
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1223
                                              gnome_canvas_rect_get_type (),
 
1224
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1225
                                              "outline_color", "black",
 
1226
                                              "width_pixels", 1,
 
1227
                                              NULL);
 
1228
          anchorsItem->se = anchorItem;
 
1229
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_SE);
 
1230
 
 
1231
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1232
                             (GtkSignalFunc) item_event_resize,
 
1233
                             anchorsItem);
 
1234
 
 
1235
          anchorsItem->n  = NULL;
 
1236
          anchorsItem->s  = NULL;
 
1237
          anchorsItem->e  = NULL;
 
1238
          anchorsItem->w  = NULL;
 
1239
          anchorsItem->ne = NULL;
 
1240
          anchorsItem->sw = NULL;
 
1241
 
 
1242
          reset_anchors_line(anchorsItem);
 
1243
          break;
 
1244
 
 
1245
        case TOOL_RECT:
 
1246
        case TOOL_CIRCLE:
 
1247
        case TOOL_FILLED_RECT:
 
1248
        case TOOL_FILLED_CIRCLE:
 
1249
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1250
                                              gnome_canvas_rect_get_type (),
 
1251
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1252
                                              "outline_color", "black",
 
1253
                                              "width_pixels", 1,
 
1254
                                              NULL);
 
1255
          anchorsItem->nw = anchorItem;
 
1256
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_NW);
 
1257
 
 
1258
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1259
                             (GtkSignalFunc) item_event_resize,
 
1260
                             anchorsItem);
 
1261
 
 
1262
          /*----------------------------------------*/
 
1263
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1264
                                              gnome_canvas_rect_get_type (),
 
1265
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1266
                                              "outline_color", "black",
 
1267
                                              "width_pixels", 1,
 
1268
                                              NULL);
 
1269
          anchorsItem->n = anchorItem;
 
1270
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_N);
 
1271
 
 
1272
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1273
                             (GtkSignalFunc) item_event_resize,
 
1274
                             anchorsItem);
 
1275
 
 
1276
          /*----------------------------------------*/
 
1277
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1278
                                              gnome_canvas_rect_get_type (),
 
1279
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1280
                                              "outline_color", "black",
 
1281
                                              "width_pixels", 1,
 
1282
                                              NULL);
 
1283
          anchorsItem->ne = anchorItem;
 
1284
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_NE);
 
1285
 
 
1286
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1287
                             (GtkSignalFunc) item_event_resize,
 
1288
                             anchorsItem);
 
1289
 
 
1290
          /*----------------------------------------*/
 
1291
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1292
                                              gnome_canvas_rect_get_type (),
 
1293
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1294
                                              "outline_color", "black",
 
1295
                                              "width_pixels", 1,
 
1296
                                              NULL);
 
1297
          anchorsItem->sw = anchorItem;
 
1298
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_SW);
 
1299
 
 
1300
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1301
                             (GtkSignalFunc) item_event_resize,
 
1302
                             anchorsItem);
 
1303
 
 
1304
          /*----------------------------------------*/
 
1305
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1306
                                              gnome_canvas_rect_get_type (),
 
1307
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1308
                                              "outline_color", "black",
 
1309
                                              "width_pixels", 1,
 
1310
                                              NULL);
 
1311
          anchorsItem->s = anchorItem;
 
1312
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_S);
 
1313
 
 
1314
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1315
                             (GtkSignalFunc) item_event_resize,
 
1316
                             anchorsItem);
 
1317
 
 
1318
          /*----------------------------------------*/
 
1319
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1320
                                              gnome_canvas_rect_get_type (),
 
1321
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1322
                                              "outline_color", "black",
 
1323
                                              "width_pixels", 1,
 
1324
                                              NULL);
 
1325
          anchorsItem->se = anchorItem;
 
1326
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_SE);
 
1327
 
 
1328
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1329
                             (GtkSignalFunc) item_event_resize,
 
1330
                             anchorsItem);
 
1331
 
 
1332
          /*----------------------------------------*/
 
1333
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1334
                                              gnome_canvas_rect_get_type (),
 
1335
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1336
                                              "outline_color", "black",
 
1337
                                              "width_pixels", 1,
 
1338
                                              NULL);
 
1339
          anchorsItem->w = anchorItem;
 
1340
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_W);
 
1341
 
 
1342
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1343
                             (GtkSignalFunc) item_event_resize,
 
1344
                             anchorsItem);
 
1345
 
 
1346
          /*----------------------------------------*/
 
1347
          anchorItem = gnome_canvas_item_new (GNOME_CANVAS_GROUP(item_root_item),
 
1348
                                              gnome_canvas_rect_get_type (),
 
1349
                                              "fill_color_rgba", ANCHOR_COLOR,
 
1350
                                              "outline_color", "black",
 
1351
                                              "width_pixels", 1,
 
1352
                                              NULL);
 
1353
          anchorsItem->e = anchorItem;
 
1354
          gtk_object_set_user_data(GTK_OBJECT(anchorItem), (void *)ANCHOR_E);
 
1355
 
 
1356
          gtk_signal_connect(GTK_OBJECT(anchorItem), "event",
 
1357
                             (GtkSignalFunc) item_event_resize,
 
1358
                             anchorsItem);
 
1359
      
 
1360
          reset_anchors_bounded(anchorsItem);
 
1361
          break;
 
1362
 
 
1363
        default:
 
1364
          /* No anchors in these cases */
 
1365
          anchorsItem->n  = NULL;
 
1366
          anchorsItem->s  = NULL;
 
1367
          anchorsItem->e  = NULL;
 
1368
          anchorsItem->w  = NULL;
 
1369
          anchorsItem->ne = NULL;
 
1370
          anchorsItem->sw = NULL;
 
1371
          anchorsItem->nw = NULL;
 
1372
          anchorsItem->se = NULL;
 
1373
          break;
 
1374
        }
 
1375
 
 
1376
      if(selected_anchors_item)
 
1377
        display_anchors(selected_anchors_item, FALSE);
 
1378
      
 
1379
      selected_anchors_item = anchorsItem;
 
1380
      display_anchors(anchorsItem, FALSE);
 
1381
      
 
1382
      /* Move is performed on the item itself */
 
1383
      gtk_signal_connect(GTK_OBJECT(anchorsItem->rootitem), "event",
 
1384
                         (GtkSignalFunc) item_event_move,
 
1385
                         anchorsItem);
 
1386
    }
 
1387
 
 
1388
  return item;
 
1389
}
 
1390
 
 
1391
/*
 
1392
 * Return the proper GDK cursor based on the given anchor
 
1393
 *
 
1394
 */
 
1395
static guint get_resize_cursor(AnchorType anchor)
 
1396
{
 
1397
  switch(anchor) 
 
1398
    {
 
1399
    case  ANCHOR_NW:
 
1400
      return(GDK_TOP_LEFT_CORNER);
 
1401
      break;
 
1402
    case  ANCHOR_N:
 
1403
      return(GDK_TOP_SIDE);
 
1404
      break;
 
1405
    case  ANCHOR_NE:
 
1406
      return(GDK_TOP_RIGHT_CORNER);
 
1407
      break;
 
1408
    case  ANCHOR_E:
 
1409
      return(GDK_RIGHT_SIDE);
 
1410
      break;
 
1411
    case  ANCHOR_W:
 
1412
      return(GDK_LEFT_SIDE);
 
1413
      break;
 
1414
    case  ANCHOR_SW:
 
1415
      return(GDK_BOTTOM_LEFT_CORNER);
 
1416
      break;
 
1417
    case  ANCHOR_S:
 
1418
      return(GDK_BOTTOM_SIDE);
 
1419
      break;
 
1420
    case  ANCHOR_SE:
 
1421
      return(GDK_BOTTOM_RIGHT_CORNER);
 
1422
      break;
 
1423
    }
 
1424
 
 
1425
  return 0;
 
1426
}
 
1427
 
 
1428
/*
 
1429
 * Special event callback for the resize operation
 
1430
 */
 
1431
static gint
 
1432
item_event_resize(GnomeCanvasItem *item, GdkEvent *event, AnchorsItem *anchorsItem)
 
1433
{
 
1434
  static double                  x, y;
 
1435
  static GnomeCanvasItem        *draggingItem = NULL;
 
1436
  static int                     dragging;
 
1437
  double                         item_x, item_y;
 
1438
  GdkCursor                     *fleur = NULL;
 
1439
  AnchorType                     anchor;
 
1440
 
 
1441
  if(!gcomprisBoard)
 
1442
    return FALSE;
 
1443
 
 
1444
  anchor = (AnchorType)gtk_object_get_user_data(GTK_OBJECT(item));
 
1445
 
 
1446
  switch (event->type) 
 
1447
    {
 
1448
    case GDK_BUTTON_PRESS:
 
1449
      switch(event->button.button) 
 
1450
        {
 
1451
        case 1:
 
1452
          switch(currentTool) {
 
1453
          case TOOL_RECT:
 
1454
          case TOOL_FILLED_RECT:
 
1455
          case TOOL_CIRCLE: 
 
1456
          case TOOL_FILLED_CIRCLE:
 
1457
          case TOOL_LINE:
 
1458
          case TOOL_POINT:
 
1459
            /* In this case, we simply redirect to the item creation */
 
1460
            item_event(item, event, NULL);
 
1461
            break;
 
1462
          case TOOL_SELECT:
 
1463
            fleur = gdk_cursor_new(get_resize_cursor(anchor));
 
1464
            
 
1465
            gnome_canvas_item_grab(item,
 
1466
                                   GDK_POINTER_MOTION_MASK | 
 
1467
                                   GDK_BUTTON_RELEASE_MASK,
 
1468
                                   fleur,
 
1469
                                   event->button.time);
 
1470
            gdk_cursor_destroy(fleur);
 
1471
            draggingItem = item;
 
1472
            dragging = TRUE;
 
1473
            
 
1474
            item_x = event->button.x;
 
1475
            item_y = event->button.y;
 
1476
            gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
1477
            snap_to_grid(&item_x, &item_y);
 
1478
            x = item_x;
 
1479
            y = item_y;
 
1480
            break;
 
1481
 
 
1482
          default:
 
1483
            break;
 
1484
          }
 
1485
          break;
 
1486
        default:
 
1487
          break;
 
1488
        }
 
1489
      break;
 
1490
    case GDK_BUTTON_RELEASE:
 
1491
      if(dragging) 
 
1492
        {
 
1493
          gnome_canvas_item_ungrab(item, event->button.time);
 
1494
          dragging = FALSE;
 
1495
          draggingItem = NULL;
 
1496
        }
 
1497
      break;
 
1498
    case GDK_ENTER_NOTIFY:
 
1499
      gcompris_set_cursor(get_resize_cursor(anchor));
 
1500
      break;
 
1501
 
 
1502
    case GDK_LEAVE_NOTIFY:
 
1503
      gcompris_set_cursor(get_tool_cursor(currentTool));
 
1504
      break;
 
1505
 
 
1506
    case GDK_MOTION_NOTIFY:
 
1507
      if (dragging && (event->motion.state & GDK_BUTTON1_MASK))
 
1508
        {
 
1509
          double                         parent_x, parent_y;
 
1510
          
 
1511
          item_x = event->button.x;
 
1512
          item_y = event->button.y;
 
1513
          gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
1514
 
 
1515
          parent_x = event->button.x;
 
1516
          parent_y = event->button.y;
 
1517
          gnome_canvas_item_w2i(anchorsItem->rootitem->parent, &parent_x, &parent_y);
 
1518
 
 
1519
          snap_to_grid(&x, &y);
 
1520
          resize_item(anchorsItem, anchor, x, y);
 
1521
 
 
1522
          /* Manage border limits */                   
 
1523
          if((parent_x > drawing_area_x1) && (parent_x < drawing_area_x2))
 
1524
            x = item_x;
 
1525
 
 
1526
          if((parent_y > drawing_area_y1) && (parent_y < drawing_area_y2))
 
1527
            y = item_y;
 
1528
 
 
1529
        }
 
1530
      break;
 
1531
    default:
 
1532
      break;
 
1533
    }
 
1534
 
 
1535
  return(TRUE);
 
1536
}
 
1537
 
 
1538
/*
 
1539
 * Special event callback for the move operation
 
1540
 */
 
1541
static gint
 
1542
item_event_move(GnomeCanvasItem *item, GdkEvent *event, AnchorsItem *anchorsItem)
 
1543
{
 
1544
  static double x, y;
 
1545
  static double start_x, start_y;
 
1546
  static GnomeCanvasItem *draggingItem = NULL;
 
1547
  static int dragging;
 
1548
  double new_x, new_y;
 
1549
  double item_x, item_y;
 
1550
  GdkCursor *fleur = NULL;
 
1551
 
 
1552
  if(!gcomprisBoard)
 
1553
    return FALSE;
 
1554
 
 
1555
  switch (event->type) 
 
1556
    {
 
1557
    case GDK_BUTTON_PRESS:
 
1558
      switch(event->button.button) 
 
1559
        {
 
1560
        case 1:
 
1561
          switch(currentTool) {
 
1562
          case TOOL_RECT:
 
1563
          case TOOL_FILLED_RECT:
 
1564
          case TOOL_CIRCLE: 
 
1565
          case TOOL_FILLED_CIRCLE:
 
1566
          case TOOL_LINE:
 
1567
          case TOOL_POINT:
 
1568
            /* In this case, we simply redirect to the item creation */
 
1569
            item_event(item, event, NULL);
 
1570
            break;
 
1571
          case TOOL_SELECT:
 
1572
            // Move an item
 
1573
            if(selected_anchors_item!=anchorsItem)
 
1574
              {
 
1575
                if(selected_anchors_item)
 
1576
                  display_anchors(selected_anchors_item, FALSE);
 
1577
 
 
1578
                display_anchors(anchorsItem, TRUE);
 
1579
                selected_anchors_item = anchorsItem;
 
1580
              }
 
1581
             
 
1582
            fleur = gdk_cursor_new(GDK_FLEUR);
 
1583
             
 
1584
            gnome_canvas_item_grab(item,
 
1585
                                   GDK_POINTER_MOTION_MASK | 
 
1586
                                   GDK_BUTTON_RELEASE_MASK,
 
1587
                                   fleur,
 
1588
                                   event->button.time);
 
1589
            gdk_cursor_destroy(fleur);
 
1590
            draggingItem = item;
 
1591
            dragging = TRUE;
 
1592
             
 
1593
            item_x = event->button.x;
 
1594
            item_y = event->button.y;
 
1595
            gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
1596
            snap_to_grid(&item_x, &item_y);
 
1597
            
 
1598
            x = item_x;
 
1599
            y = item_y;
 
1600
            start_x = item_x;
 
1601
            start_y = item_y;
 
1602
 
 
1603
            display_anchors(anchorsItem, FALSE);
 
1604
            break;
 
1605
 
 
1606
          case TOOL_DELETE:
 
1607
            if(selected_anchors_item == anchorsItem)
 
1608
              selected_anchors_item = NULL;
 
1609
 
 
1610
            gtk_object_destroy (GTK_OBJECT(anchorsItem->rootitem));
 
1611
            g_free(anchorsItem);
 
1612
            return FALSE;
 
1613
            break;
 
1614
 
 
1615
          case TOOL_FILL:
 
1616
            set_item_color(anchorsItem, currentColor);
 
1617
            break;
 
1618
 
 
1619
          default:
 
1620
            break;
 
1621
          }
 
1622
          break;
 
1623
 
 
1624
        case 2:
 
1625
          // Shortcut for the Delete operation
 
1626
          if(selected_anchors_item == anchorsItem)
 
1627
            selected_anchors_item = NULL;
 
1628
           
 
1629
          gtk_object_destroy (GTK_OBJECT(anchorsItem->rootitem));
 
1630
          g_free(anchorsItem);
 
1631
          break;
 
1632
 
 
1633
        case 3:
 
1634
          // Shortcut for the Raise operation
 
1635
          gnome_canvas_item_raise_to_top(item); 
 
1636
          break;
 
1637
 
 
1638
        default:
 
1639
          break;
 
1640
        }
 
1641
      break;
 
1642
    case GDK_BUTTON_RELEASE:
 
1643
      if(dragging) 
 
1644
        {
 
1645
          gnome_canvas_item_ungrab(item, event->button.time);
 
1646
          dragging = FALSE;
 
1647
          draggingItem = NULL;     
 
1648
 
 
1649
          /* Moving back the anchors around their shape */
 
1650
          display_anchors(anchorsItem, TRUE);
 
1651
 
 
1652
        }
 
1653
      break;
 
1654
    case GDK_ENTER_NOTIFY:
 
1655
      switch(currentTool) {
 
1656
      case TOOL_RECT:
 
1657
      case TOOL_FILLED_RECT:
 
1658
      case TOOL_CIRCLE: 
 
1659
      case TOOL_FILLED_CIRCLE:
 
1660
      case TOOL_LINE:
 
1661
      case TOOL_POINT:
 
1662
      case TOOL_DELETE:
 
1663
      case TOOL_FILL:
 
1664
        gcompris_set_cursor(get_tool_cursor(currentTool));
 
1665
        break;
 
1666
      case TOOL_SELECT:
 
1667
        gcompris_set_cursor(GDK_FLEUR);
 
1668
        break;
 
1669
      default:
 
1670
        break;
 
1671
      }
 
1672
      break;
 
1673
    case GDK_LEAVE_NOTIFY:
 
1674
      gcompris_set_cursor(get_tool_cursor(currentTool));
 
1675
      break;
 
1676
 
 
1677
    case GDK_MOTION_NOTIFY:
 
1678
      if (dragging && (event->motion.state & GDK_BUTTON1_MASK))
 
1679
        {
 
1680
          double x1, y1, x2, y2;
 
1681
 
 
1682
          item_x = event->button.x;
 
1683
          item_y = event->button.y;
 
1684
          gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
1685
 
 
1686
          new_x = item_x;
 
1687
          new_y = item_y;
 
1688
 
 
1689
          gnome_canvas_item_get_bounds  (item,
 
1690
                                         &x1,
 
1691
                                         &y1,
 
1692
                                         &x2,
 
1693
                                         &y2);
 
1694
 
 
1695
          /* Manage border limits */                   
 
1696
          if((x1 + new_x - x) < drawing_area_x1 && (new_x - x)<0)
 
1697
            new_x = x;
 
1698
          else if((x2 + new_x -x) > drawing_area_x2 && (new_x - x)>0)
 
1699
            new_x = x;
 
1700
           
 
1701
          if((y1 + new_y - y) < drawing_area_y1 && (new_y - y)<0)
 
1702
            new_y = y;
 
1703
          else if((y2 + new_y - y) > drawing_area_y2 && (new_y - y)>0)
 
1704
            new_y = y;
 
1705
          
 
1706
          snap_to_grid(&new_x, &new_y);
 
1707
          gnome_canvas_item_move(item, new_x - x, new_y - y);
 
1708
 
 
1709
          x = new_x;
 
1710
          y = new_y;
 
1711
        }
 
1712
      break;
 
1713
    default:
 
1714
      break;
 
1715
    }
 
1716
 
 
1717
  return(TRUE);
 
1718
}
 
1719
 
 
1720
/*
 
1721
 * Event that occur when a creation of object is requested
 
1722
 *
 
1723
 */
 
1724
static gint
 
1725
item_event(GnomeCanvasItem *item, GdkEvent *event, void *shape)
 
1726
{
 
1727
  static double x, y;
 
1728
  double item_x, item_y;
 
1729
  GnomeCanvasItem *newItem = NULL;
 
1730
 
 
1731
  if(!gcomprisBoard)
 
1732
    return FALSE;
 
1733
 
 
1734
  switch (event->type) 
 
1735
    {
 
1736
    case GDK_BUTTON_PRESS:
 
1737
      switch(event->button.button) 
 
1738
        {
 
1739
        case 1:
 
1740
        case 2:
 
1741
        case 3:
 
1742
          item_x = event->button.x;
 
1743
          item_y = event->button.y;
 
1744
          gnome_canvas_item_w2i(item->parent, &item_x, &item_y);
 
1745
 
 
1746
          x = item_x;
 
1747
          y = item_y;
 
1748
           
 
1749
          switch(currentTool) {
 
1750
          case TOOL_RECT:
 
1751
          case TOOL_FILLED_RECT:
 
1752
          case TOOL_CIRCLE: 
 
1753
          case TOOL_FILLED_CIRCLE:
 
1754
          case TOOL_LINE:
 
1755
          case TOOL_POINT:
 
1756
            // Create a new item
 
1757
            if(event->button.button==1)
 
1758
              {
 
1759
                snap_to_grid(&x, &y);
 
1760
                newItem = create_item(x, y);
 
1761
 
 
1762
                if(newItem==NULL)
 
1763
                  return FALSE;
 
1764
              }
 
1765
            break;
 
1766
 
 
1767
          default:
 
1768
            break;
 
1769
          }
 
1770
 
 
1771
        default:
 
1772
          break;
 
1773
        }
 
1774
      break;
 
1775
 
 
1776
    case GDK_ENTER_NOTIFY:
 
1777
      gcompris_set_cursor(get_tool_cursor(currentTool));
 
1778
      break;
 
1779
 
 
1780
    case GDK_LEAVE_NOTIFY:
 
1781
      gcompris_set_cursor(GCOMPRIS_DEFAULT_CURSOR);
 
1782
      break;
 
1783
 
 
1784
    case GDK_MOTION_NOTIFY:
 
1785
      break;
 
1786
           
 
1787
    case GDK_BUTTON_RELEASE:
 
1788
      break;
 
1789
 
 
1790
    default:
 
1791
      break;
 
1792
    }
 
1793
         
 
1794
  return TRUE;
 
1795
}