4
* Copyright (C) 2002 Robert Wilhelm
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.
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.
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.
21
* Authors: Bruno Coudoin
27
#include "money_widget.h"
29
struct _Money_WidgetPrivate {
30
GnomeCanvasGroup *rootItem; /* The canvas to display our euros in */
31
double x1; /* Coordinate of the widget */
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 */
42
GnomeCanvasItem *item_total; /* Item to display the total */
43
GList *moneyItemList; /* List of all the items */
47
typedef struct _MoneyList MoneyList;
54
// List of images to use in the game
55
static const MoneyList euroList[] =
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 },
73
Money_Widget *moneyWidget;
74
GnomeCanvasItem *item;
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);
88
money_widget_get_type ()
90
static guint money_widget_type = 0;
92
if (!money_widget_type) {
93
GtkTypeInfo money_widget_info = {
95
sizeof (Money_Widget),
96
sizeof (Money_WidgetClass),
97
(GtkClassInitFunc) class_init,
98
(GtkObjectInitFunc) init,
101
(GtkClassInitFunc) NULL
103
money_widget_type = gtk_type_unique (gtk_object_get_type (),
107
return money_widget_type;
112
finalize (GtkObject *object)
114
MoneyItem *moneyitem;
116
Money_Widget *moneyWidget = (Money_Widget *) object;
118
printf("ERROR : Finalize is NEVER CALLED\n");
119
/* FIXME: CLEANUP CODE UNTESTED */
120
while(g_list_length(moneyWidget->priv->moneyItemList)>0)
122
moneyitem = g_list_nth_data(moneyWidget->priv->moneyItemList, 0);
123
moneyWidget->priv->moneyItemList = g_list_remove (moneyWidget->priv->moneyItemList,
128
g_free (moneyWidget->priv);
130
moneyWidget->priv = NULL;
136
class_init (Money_WidgetClass *class)
138
GtkObjectClass *object_class;
140
object_class = (GtkObjectClass*) class;
142
//2 object_class->destroy = finalize;
146
init (Money_Widget *pos)
148
pos->priv = g_new0 (Money_WidgetPrivate, 1);
155
return GTK_OBJECT (gtk_type_new (money_widget_get_type ()));
159
money_widget_copy (Money_Widget *pos)
163
cpPos = MONEY_WIDGET (money_widget_new ());
165
memcpy (cpPos->priv, pos->priv, sizeof (Money_WidgetPrivate));
171
money_widget_set_target (Money_Widget *moneyWidget,
172
Money_Widget *targetWidget)
174
moneyWidget->priv->targetWidget = targetWidget;
178
money_widget_set_position (Money_Widget *moneyWidget,
179
GnomeCanvasGroup *rootItem,
186
gboolean display_total)
188
g_return_if_fail (moneyWidget != NULL);
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;
200
/* Uncomment to display the limits
201
gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootItem),
202
gnome_canvas_rect_get_type (),
207
"outline_color", "red",
212
moneyWidget->priv->item_total = gnome_canvas_item_new(rootItem,
213
gnome_canvas_text_get_type (),
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",
224
static void money_display_total(Money_Widget *moneyWidget)
227
g_return_if_fail (moneyWidget != NULL);
229
tmpstr = g_strdup_printf("%.2f €", moneyWidget->priv->total);
230
if(moneyWidget->priv->display_total)
231
gnome_canvas_item_set (moneyWidget->priv->item_total,
239
money_widget_add (Money_Widget *moneyWidget, MoneyEuroType value)
241
GnomeCanvasItem *item = NULL;
242
GdkPixbuf *pixmap = NULL;
243
double xratio, yratio, ratio;
244
double block_width, block_height;
245
MoneyItem *moneyitem;
248
g_return_if_fail (moneyWidget != NULL);
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++)
254
moneyitem = (MoneyItem *)g_list_nth_data(moneyWidget->priv->moneyItemList, i);
256
if(moneyitem && !moneyitem->inPocket && moneyitem->value == value)
258
gnome_canvas_item_show(moneyitem->item);
259
moneyitem->inPocket = TRUE;
260
moneyWidget->priv->total += euroList[value].value;
261
money_display_total(moneyWidget);
266
/* There is no already suitable item create, create a new one */
268
if(moneyWidget->priv->next_spot > moneyWidget->priv->columns * moneyWidget->priv->lines)
269
g_message("More money items requested than the pocket size\n");
271
block_width = (moneyWidget->priv->x2 - moneyWidget->priv->x1) / moneyWidget->priv->columns;
272
block_height = (moneyWidget->priv->y2 - moneyWidget->priv->y1) / moneyWidget->priv->lines;
274
pixmap = gc_pixmap_load((gchar *)euroList[value].image);
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);
280
item = gnome_canvas_item_new ( moneyWidget->priv->rootItem,
281
gnome_canvas_pixbuf_get_type (),
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)
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,
296
moneyitem = g_malloc(sizeof(MoneyItem));
297
moneyitem->moneyWidget = moneyWidget;
298
moneyitem->item = item;
299
moneyitem->value = value;
300
moneyitem->inPocket = TRUE;
302
moneyWidget->priv->moneyItemList = g_list_append (moneyWidget->priv->moneyItemList,
305
gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, moneyitem);
307
gdk_pixbuf_unref(pixmap);
309
moneyWidget->priv->next_spot++;
311
moneyWidget->priv->total += euroList[value].value;
313
money_display_total(moneyWidget);
317
money_widget_remove(Money_Widget *moneyWidget, MoneyEuroType value)
319
g_return_if_fail (moneyWidget != NULL);
321
moneyWidget->priv->total -= euroList[value].value;
323
money_display_total(moneyWidget);
327
money_widget_get_total (Money_Widget *moneyWidget)
329
if(moneyWidget == NULL)
332
return moneyWidget->priv->total;
337
item_event(GnomeCanvasItem *item, GdkEvent *event, MoneyItem *moneyItem)
342
case GDK_BUTTON_PRESS:
343
switch(event->button.button)
346
gnome_canvas_item_hide(item);
347
moneyItem->inPocket = FALSE;
348
money_widget_remove(moneyItem->moneyWidget, moneyItem->value);
350
if(moneyItem->moneyWidget->priv->targetWidget != NULL)
351
money_widget_add(moneyItem->moneyWidget->priv->targetWidget,