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

« back to all changes in this revision

Viewing changes to src/boards/money_widget.c

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

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * money_widget.c
3
 
 *
4
 
 * Copyright (C) 2002  Robert Wilhelm
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU General Public License as
8
 
 * published by the Free Software Foundation; either version 2 of the
9
 
 * License, or (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public
17
 
 * License along with this program; if not, write to the
18
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 
 * Boston, MA 02111-1307, USA.
20
 
 *
21
 
 * Authors: Bruno Coudoin
22
 
 *
23
 
 */
24
 
 
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
#include "money_widget.h"
28
 
 
29
 
struct _Money_WidgetPrivate {
30
 
  GnomeCanvasGroup      *rootItem;      /* The canvas to display our euros in             */
31
 
  double                 x1;            /* Coordinate of the widget                       */
32
 
  double                 y1;
33
 
  double                 x2;
34
 
  double                 y2;
35
 
  guint                  columns;       /* Number of columns                              */
36
 
  guint                  lines;         /* Number of lines                                */
37
 
  guint                  next_spot;     /* Next spot to display a money item              */
38
 
  double                 total;         /* The number of euro in this pocket              */
39
 
  Money_Widget          *targetWidget;  /* Target money widget to add when remove here    */
40
 
  gboolean               display_total; /* Display or not the total of this pocket        */
41
 
 
42
 
  GnomeCanvasItem       *item_total;    /* Item to display the total                      */
43
 
  GList                 *moneyItemList; /* List of all the items                          */
44
 
};
45
 
 
46
 
 
47
 
typedef struct _MoneyList        MoneyList;
48
 
 
49
 
struct _MoneyList {
50
 
  const gchar           *image;
51
 
  const double           value;
52
 
};
53
 
 
54
 
// List of images to use in the game
55
 
static const MoneyList euroList[] =
56
 
{
57
 
  { "money/euro/c1c.png",               0.01  },
58
 
  { "money/euro/c2c.png",               0.02  },
59
 
  { "money/euro/c5c.png",               0.05  },
60
 
  { "money/euro/c10c.png",              0.1   },
61
 
  { "money/euro/c20c.png",              0.20  },
62
 
  { "money/euro/c50c.png",              0.5   },
63
 
  { "money/euro/c1e.png",               1.0   },
64
 
  { "money/euro/c2e.png",               2.0   },
65
 
  { "money/euro/p5e.png",               5.0   },
66
 
  { "money/euro/p10e.png",              10.0  },
67
 
  { "money/euro/p20e.png",              20.0  },
68
 
  { "money/euro/p50e.png",              50.0  },
69
 
  { NULL,                               -1.0  }
70
 
};
71
 
 
72
 
typedef struct {
73
 
  Money_Widget          *moneyWidget;
74
 
  GnomeCanvasItem       *item;
75
 
  MoneyEuroType          value;
76
 
  gboolean               inPocket;
77
 
} MoneyItem;
78
 
 
79
 
#define BORDER_GAP      6
80
 
 
81
 
/* Prototypes */
82
 
static void class_init (Money_WidgetClass *class);
83
 
static void init (Money_Widget *pos);
84
 
static void money_display_total(Money_Widget *moneyWidget);
85
 
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, MoneyItem *moneyitem);
86
 
 
87
 
GtkType
88
 
money_widget_get_type ()
89
 
{
90
 
        static guint money_widget_type = 0;
91
 
 
92
 
        if (!money_widget_type) {
93
 
                GtkTypeInfo money_widget_info = {
94
 
                        "Money_Widget",
95
 
                        sizeof (Money_Widget),
96
 
                        sizeof (Money_WidgetClass),
97
 
                        (GtkClassInitFunc) class_init,
98
 
                        (GtkObjectInitFunc) init,
99
 
                        (gpointer) NULL,
100
 
                        (gpointer) NULL,
101
 
                        (GtkClassInitFunc) NULL
102
 
                };
103
 
                money_widget_type = gtk_type_unique (gtk_object_get_type (),
104
 
                                                 &money_widget_info);
105
 
        }
106
 
 
107
 
        return money_widget_type;
108
 
}
109
 
 
110
 
#if 0
111
 
static void
112
 
finalize (GtkObject *object)
113
 
{
114
 
  MoneyItem       *moneyitem;
115
 
 
116
 
  Money_Widget *moneyWidget = (Money_Widget *) object;
117
 
 
118
 
  printf("ERROR : Finalize is NEVER CALLED\n");
119
 
  /* FIXME: CLEANUP CODE UNTESTED */
120
 
  while(g_list_length(moneyWidget->priv->moneyItemList)>0) 
121
 
    {
122
 
      moneyitem = g_list_nth_data(moneyWidget->priv->moneyItemList, 0);
123
 
      moneyWidget->priv->moneyItemList = g_list_remove (moneyWidget->priv->moneyItemList,
124
 
                                                        moneyitem);
125
 
      g_free(moneyitem);
126
 
    }
127
 
 
128
 
  g_free (moneyWidget->priv);
129
 
  
130
 
  moneyWidget->priv = NULL;
131
 
 
132
 
}
133
 
#endif
134
 
 
135
 
static void
136
 
class_init (Money_WidgetClass *class)
137
 
{
138
 
  GtkObjectClass *object_class;
139
 
  
140
 
  object_class = (GtkObjectClass*) class;
141
 
  
142
 
  //2  object_class->destroy = finalize;
143
 
}
144
 
 
145
 
static void
146
 
init (Money_Widget *pos)
147
 
{
148
 
        pos->priv = g_new0 (Money_WidgetPrivate, 1);
149
 
 
150
 
}
151
 
 
152
 
GtkObject *
153
 
money_widget_new ()
154
 
{
155
 
        return GTK_OBJECT (gtk_type_new (money_widget_get_type ()));
156
 
}
157
 
 
158
 
Money_Widget *
159
 
money_widget_copy (Money_Widget *pos)
160
 
{
161
 
        Money_Widget *cpPos;
162
 
 
163
 
        cpPos = MONEY_WIDGET (money_widget_new ());
164
 
 
165
 
        memcpy (cpPos->priv, pos->priv, sizeof (Money_WidgetPrivate));
166
 
 
167
 
        return cpPos;
168
 
}
169
 
 
170
 
void
171
 
money_widget_set_target (Money_Widget *moneyWidget,
172
 
                         Money_Widget *targetWidget)
173
 
{
174
 
  moneyWidget->priv->targetWidget = targetWidget;
175
 
}
176
 
 
177
 
void
178
 
money_widget_set_position (Money_Widget *moneyWidget,
179
 
                           GnomeCanvasGroup *rootItem,
180
 
                           double x1,
181
 
                           double y1,
182
 
                           double x2,
183
 
                           double y2,
184
 
                           guint  columns,
185
 
                           guint  lines,
186
 
                           gboolean display_total)
187
 
{
188
 
  g_return_if_fail (moneyWidget != NULL);
189
 
  
190
 
  moneyWidget->priv->rootItem = rootItem;
191
 
  moneyWidget->priv->x1 = x1;
192
 
  moneyWidget->priv->y1 = y1;
193
 
  moneyWidget->priv->x2 = x2;
194
 
  moneyWidget->priv->y2 = y2;
195
 
  moneyWidget->priv->columns = columns;
196
 
  moneyWidget->priv->lines = lines;
197
 
  moneyWidget->priv->next_spot = 0;
198
 
  moneyWidget->priv->display_total = display_total;
199
 
 
200
 
  /* Uncomment to display the limits
201
 
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootItem),
202
 
                         gnome_canvas_rect_get_type (),
203
 
                         "x1", (double) x1,
204
 
                         "y1", (double) y1,
205
 
                         "x2", (double) x2,
206
 
                         "y2", (double) y2,
207
 
                         "outline_color", "red",
208
 
                         "width_pixels", 2,
209
 
                         NULL);
210
 
  */
211
 
 
212
 
  moneyWidget->priv->item_total =  gnome_canvas_item_new(rootItem,
213
 
                                                       gnome_canvas_text_get_type (),
214
 
                                                       "text", "",
215
 
                                                       "font", gc_skin_font_board_big,
216
 
                                                       "x", (double) x1+(x2-x1)/2,
217
 
                                                       "y", (double) y2 + 10,
218
 
                                                       "anchor", GTK_ANCHOR_CENTER,
219
 
                                                       "fill_color", "white",
220
 
                                                       NULL);
221
 
 
222
 
}
223
 
 
224
 
static void money_display_total(Money_Widget *moneyWidget)
225
 
{
226
 
  gchar *tmpstr;
227
 
  g_return_if_fail (moneyWidget != NULL);
228
 
 
229
 
  tmpstr = g_strdup_printf("%.2f €", moneyWidget->priv->total);
230
 
  if(moneyWidget->priv->display_total)
231
 
    gnome_canvas_item_set (moneyWidget->priv->item_total,
232
 
                           "text", tmpstr,
233
 
                           NULL);
234
 
  g_free(tmpstr);
235
 
  
236
 
}
237
 
 
238
 
void
239
 
money_widget_add (Money_Widget *moneyWidget, MoneyEuroType value)
240
 
{
241
 
  GnomeCanvasItem *item   = NULL;
242
 
  GdkPixbuf       *pixmap = NULL;
243
 
  double           xratio, yratio, ratio;
244
 
  double           block_width, block_height;
245
 
  MoneyItem       *moneyitem;
246
 
  guint            i, length;
247
 
 
248
 
  g_return_if_fail (moneyWidget != NULL);
249
 
 
250
 
  /* Search for an hidden item with the same value */
251
 
  length = g_list_length(moneyWidget->priv->moneyItemList);
252
 
  for(i=0; i<length; i++)
253
 
    {
254
 
      moneyitem = (MoneyItem *)g_list_nth_data(moneyWidget->priv->moneyItemList, i);
255
 
 
256
 
      if(moneyitem && !moneyitem->inPocket && moneyitem->value == value)
257
 
        {
258
 
          gnome_canvas_item_show(moneyitem->item);
259
 
          moneyitem->inPocket = TRUE;
260
 
          moneyWidget->priv->total += euroList[value].value;
261
 
          money_display_total(moneyWidget);
262
 
          return;
263
 
        }
264
 
    }
265
 
  
266
 
  /* There is no already suitable item create, create a new one */
267
 
 
268
 
  if(moneyWidget->priv->next_spot > moneyWidget->priv->columns * moneyWidget->priv->lines)
269
 
    g_message("More money items requested than the pocket size\n");
270
 
 
271
 
  block_width  = (moneyWidget->priv->x2 - moneyWidget->priv->x1) / moneyWidget->priv->columns;
272
 
  block_height = (moneyWidget->priv->y2 - moneyWidget->priv->y1) / moneyWidget->priv->lines;
273
 
 
274
 
  pixmap = gc_pixmap_load((gchar *)euroList[value].image);
275
 
 
276
 
  xratio =  block_width  / (gdk_pixbuf_get_width (pixmap) + BORDER_GAP);
277
 
  yratio =  block_height / (gdk_pixbuf_get_height(pixmap) + BORDER_GAP);
278
 
  ratio = yratio = MIN(xratio, yratio);
279
 
 
280
 
  item =  gnome_canvas_item_new ( moneyWidget->priv->rootItem,
281
 
                                  gnome_canvas_pixbuf_get_type (),
282
 
                                  "pixbuf", pixmap,
283
 
                                  "x", (double) moneyWidget->priv->x1 + 
284
 
                                  (moneyWidget->priv->next_spot % moneyWidget->priv->columns) * block_width
285
 
                                  +  block_width/2 - (gdk_pixbuf_get_width(pixmap) * ratio)/2,
286
 
                                  "y", (double)moneyWidget->priv->y1 +
287
 
                                  (moneyWidget->priv->next_spot / moneyWidget->priv->columns)
288
 
                                  * block_height
289
 
                                  + block_height/2 - (gdk_pixbuf_get_height(pixmap) * ratio)/2,
290
 
                                  "width",  (double) gdk_pixbuf_get_width(pixmap)  * ratio,
291
 
                                  "height", (double) gdk_pixbuf_get_height(pixmap) * ratio,
292
 
                                  "width_set", TRUE, 
293
 
                                  "height_set", TRUE,
294
 
                                  NULL);
295
 
 
296
 
  moneyitem = g_malloc(sizeof(MoneyItem));
297
 
  moneyitem->moneyWidget = moneyWidget;
298
 
  moneyitem->item   = item;
299
 
  moneyitem->value  = value;
300
 
  moneyitem->inPocket = TRUE;
301
 
 
302
 
  moneyWidget->priv->moneyItemList = g_list_append (moneyWidget->priv->moneyItemList,
303
 
                                                    moneyitem);
304
 
 
305
 
  gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, moneyitem);
306
 
 
307
 
  gdk_pixbuf_unref(pixmap);
308
 
 
309
 
  moneyWidget->priv->next_spot++;
310
 
 
311
 
  moneyWidget->priv->total += euroList[value].value;
312
 
 
313
 
  money_display_total(moneyWidget);
314
 
}
315
 
 
316
 
void
317
 
money_widget_remove(Money_Widget *moneyWidget, MoneyEuroType value)
318
 
{
319
 
  g_return_if_fail (moneyWidget != NULL);
320
 
 
321
 
  moneyWidget->priv->total -= euroList[value].value;
322
 
 
323
 
  money_display_total(moneyWidget);
324
 
}
325
 
 
326
 
double
327
 
money_widget_get_total (Money_Widget *moneyWidget)
328
 
{
329
 
  if(moneyWidget == NULL)
330
 
    return 0;
331
 
 
332
 
  return moneyWidget->priv->total;
333
 
}
334
 
 
335
 
 
336
 
static gint
337
 
item_event(GnomeCanvasItem *item, GdkEvent *event, MoneyItem *moneyItem)
338
 
{
339
 
 
340
 
  switch (event->type) 
341
 
    {
342
 
    case GDK_BUTTON_PRESS:
343
 
      switch(event->button.button) 
344
 
        {
345
 
        case 1:
346
 
          gnome_canvas_item_hide(item);
347
 
          moneyItem->inPocket = FALSE;
348
 
          money_widget_remove(moneyItem->moneyWidget, moneyItem->value);
349
 
          
350
 
          if(moneyItem->moneyWidget->priv->targetWidget != NULL)
351
 
            money_widget_add(moneyItem->moneyWidget->priv->targetWidget,
352
 
                             moneyItem->value);
353
 
          
354
 
          break;
355
 
        default:
356
 
          break;
357
 
        }
358
 
    default:
359
 
      break;
360
 
    }
361
 
  return FALSE;
362
 
}