Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/boards/money_widget.c
blob: 505fdb0f518887c282145fa38a81863efd40c0e5 (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
/*
 * money_widget.c
 *
 * Copyright (C) 2002  Robert Wilhelm
 *
 * 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.
 *
 * Authors: Bruno Coudoin
 *
 */

#include <stdlib.h>
#include <string.h>
#include "money_widget.h"

struct _Money_WidgetPrivate {
  GnomeCanvasGroup	*rootItem;	/* The canvas to display our euros in             */
  double		 x1;		/* Coordinate of the widget                       */
  double		 y1;
  double		 x2;
  double		 y2;
  guint			 columns;	/* Number of columns				  */
  guint			 lines;		/* Number of lines				  */
  guint			 next_spot;	/* Next spot to display a money item		  */
  double		 total;		/* The number of euro in this pocket              */
  Money_Widget		*targetWidget;	/* Target money widget to add when remove here	  */
  gboolean		 display_total;	/* Display or not the total of this pocket        */

  GnomeCanvasItem	*item_total;	/* Item to display the total                      */
  GList			*moneyItemList;	/* List of all the items			  */
};


typedef struct _MoneyList        MoneyList;

struct _MoneyList {
  const gchar		*image;
  const double		 value;
};

// List of images to use in the game
static const MoneyList euroList[] =
{
  { "money/euro/c1c.png",		0.01  },
  { "money/euro/c2c.png",		0.02  },
  { "money/euro/c5c.png",		0.05  },
  { "money/euro/c10c.png",		0.1   },
  { "money/euro/c20c.png",		0.20  },
  { "money/euro/c50c.png",		0.5   },
  { "money/euro/c1e.png",		1.0   },
  { "money/euro/c2e.png",		2.0   },
  { "money/euro/p5e.png",		5.0   },
  { "money/euro/p10e.png",		10.0  },
  { "money/euro/p20e.png",		20.0  },
  { "money/euro/p50e.png",		50.0  },
  { NULL,				-1.0  }
};

typedef struct {
  Money_Widget		*moneyWidget;
  GnomeCanvasItem	*item;
  MoneyEuroType		 value;
  gboolean		 inPocket;
} MoneyItem;

#define BORDER_GAP	6

/* Prototypes */
static void class_init (Money_WidgetClass *class);
static void init (Money_Widget *pos);
static void money_display_total(Money_Widget *moneyWidget);
static gint item_event(GnomeCanvasItem *item, GdkEvent *event, MoneyItem *moneyitem);

GtkType
money_widget_get_type ()
{
	static guint money_widget_type = 0;

	if (!money_widget_type) {
		GtkTypeInfo money_widget_info = {
			"Money_Widget",
			sizeof (Money_Widget),
			sizeof (Money_WidgetClass),
			(GtkClassInitFunc) class_init,
			(GtkObjectInitFunc) init,
			(gpointer) NULL,
			(gpointer) NULL,
			(GtkClassInitFunc) NULL
		};
		money_widget_type = gtk_type_unique (gtk_object_get_type (),
						 &money_widget_info);
	}

	return money_widget_type;
}

#if 0
static void
finalize (GtkObject *object)
{
  MoneyItem	  *moneyitem;

  Money_Widget *moneyWidget = (Money_Widget *) object;

  printf("ERROR : Finalize is NEVER CALLED\n");
  /* FIXME: CLEANUP CODE UNTESTED */
  while(g_list_length(moneyWidget->priv->moneyItemList)>0) 
    {
      moneyitem = g_list_nth_data(moneyWidget->priv->moneyItemList, 0);
      moneyWidget->priv->moneyItemList = g_list_remove (moneyWidget->priv->moneyItemList,
							moneyitem);
      g_free(moneyitem);
    }

  g_free (moneyWidget->priv);
  
  moneyWidget->priv = NULL;

}
#endif

static void
class_init (Money_WidgetClass *class)
{
  GtkObjectClass *object_class;
  
  object_class = (GtkObjectClass*) class;
  
  //2  object_class->destroy = finalize;
}

static void
init (Money_Widget *pos)
{
	pos->priv = g_new0 (Money_WidgetPrivate, 1);

}

GtkObject *
money_widget_new ()
{
	return GTK_OBJECT (gtk_type_new (money_widget_get_type ()));
}

Money_Widget *
money_widget_copy (Money_Widget *pos)
{
	Money_Widget *cpPos;

	cpPos = MONEY_WIDGET (money_widget_new ());

	memcpy (cpPos->priv, pos->priv, sizeof (Money_WidgetPrivate));

	return cpPos;
}

void
money_widget_set_target (Money_Widget *moneyWidget,
			 Money_Widget *targetWidget)
{
  moneyWidget->priv->targetWidget = targetWidget;
}

void
money_widget_set_position (Money_Widget *moneyWidget,
			   GnomeCanvasGroup *rootItem,
			   double x1,
			   double y1,
			   double x2,
			   double y2,
			   guint  columns,
			   guint  lines,
			   gboolean display_total)
{
  g_return_if_fail (moneyWidget != NULL);
  
  moneyWidget->priv->rootItem = rootItem;
  moneyWidget->priv->x1 = x1;
  moneyWidget->priv->y1 = y1;
  moneyWidget->priv->x2 = x2;
  moneyWidget->priv->y2 = y2;
  moneyWidget->priv->columns = columns;
  moneyWidget->priv->lines = lines;
  moneyWidget->priv->next_spot = 0;
  moneyWidget->priv->display_total = display_total;

  /* Uncomment to display the limits
    gnome_canvas_item_new (GNOME_CANVAS_GROUP(rootItem),
			 gnome_canvas_rect_get_type (),
			 "x1", (double) x1,
			 "y1", (double) y1,
			 "x2", (double) x2,
			 "y2", (double) y2,
			 "outline_color", "red",
			 "width_pixels", 2,
			 NULL);
  */

  moneyWidget->priv->item_total =  gnome_canvas_item_new(rootItem,
						       gnome_canvas_text_get_type (),
						       "text", "",
						       "font", gc_skin_font_board_big,
						       "x", (double) x1+(x2-x1)/2,
						       "y", (double) y2 + 10,
						       "anchor", GTK_ANCHOR_CENTER,
						       "fill_color", "white",
						       NULL);

}

static void money_display_total(Money_Widget *moneyWidget)
{
  gchar *tmpstr;
  g_return_if_fail (moneyWidget != NULL);

  tmpstr = g_strdup_printf("%.2f €", moneyWidget->priv->total);
  if(moneyWidget->priv->display_total)
    gnome_canvas_item_set (moneyWidget->priv->item_total,
			   "text", tmpstr,
			   NULL);
  g_free(tmpstr);
  
}

void
money_widget_add (Money_Widget *moneyWidget, MoneyEuroType value)
{
  GnomeCanvasItem *item   = NULL;
  GdkPixbuf       *pixmap = NULL;
  double	   xratio, yratio, ratio;
  double	   block_width, block_height;
  MoneyItem	  *moneyitem;
  guint		   i, length;

  g_return_if_fail (moneyWidget != NULL);

  /* Search for an hidden item with the same value */
  length = g_list_length(moneyWidget->priv->moneyItemList);
  for(i=0; i<length; i++)
    {
      moneyitem = (MoneyItem *)g_list_nth_data(moneyWidget->priv->moneyItemList, i);

      if(moneyitem && !moneyitem->inPocket && moneyitem->value == value)
	{
	  gnome_canvas_item_show(moneyitem->item);
	  moneyitem->inPocket = TRUE;
	  moneyWidget->priv->total += euroList[value].value;
	  money_display_total(moneyWidget);
	  return;
	}
    }
  
  /* There is no already suitable item create, create a new one */

  if(moneyWidget->priv->next_spot > moneyWidget->priv->columns * moneyWidget->priv->lines)
    g_message("More money items requested than the pocket size\n");

  block_width  = (moneyWidget->priv->x2 - moneyWidget->priv->x1) / moneyWidget->priv->columns;
  block_height = (moneyWidget->priv->y2 - moneyWidget->priv->y1) / moneyWidget->priv->lines;

  pixmap = gc_pixmap_load((gchar *)euroList[value].image);

  xratio =  block_width  / (gdk_pixbuf_get_width (pixmap) + BORDER_GAP);
  yratio =  block_height / (gdk_pixbuf_get_height(pixmap) + BORDER_GAP);
  ratio = yratio = MIN(xratio, yratio);

  item =  gnome_canvas_item_new ( moneyWidget->priv->rootItem,
				  gnome_canvas_pixbuf_get_type (),
				  "pixbuf", pixmap,
				  "x", (double) moneyWidget->priv->x1 + 
				  (moneyWidget->priv->next_spot % moneyWidget->priv->columns) * block_width
				  +  block_width/2 - (gdk_pixbuf_get_width(pixmap) * ratio)/2,
				  "y", (double)moneyWidget->priv->y1 +
				  (moneyWidget->priv->next_spot / moneyWidget->priv->columns)
				  * block_height
				  + block_height/2 - (gdk_pixbuf_get_height(pixmap) * ratio)/2,
				  "width",  (double) gdk_pixbuf_get_width(pixmap)  * ratio,
				  "height", (double) gdk_pixbuf_get_height(pixmap) * ratio,
				  "width_set", TRUE, 
				  "height_set", TRUE,
				  NULL);

  moneyitem = g_malloc(sizeof(MoneyItem));
  moneyitem->moneyWidget = moneyWidget;
  moneyitem->item   = item;
  moneyitem->value  = value;
  moneyitem->inPocket = TRUE;

  moneyWidget->priv->moneyItemList = g_list_append (moneyWidget->priv->moneyItemList,
						    moneyitem);

  gtk_signal_connect(GTK_OBJECT(item), "event", (GtkSignalFunc) item_event, moneyitem);

  gdk_pixbuf_unref(pixmap);

  moneyWidget->priv->next_spot++;

  moneyWidget->priv->total += euroList[value].value;

  money_display_total(moneyWidget);
}

void
money_widget_remove(Money_Widget *moneyWidget, MoneyEuroType value)
{
  g_return_if_fail (moneyWidget != NULL);

  moneyWidget->priv->total -= euroList[value].value;

  money_display_total(moneyWidget);
}

double
money_widget_get_total (Money_Widget *moneyWidget)
{
  if(moneyWidget == NULL)
    return 0;

  return moneyWidget->priv->total;
}


static gint
item_event(GnomeCanvasItem *item, GdkEvent *event, MoneyItem *moneyItem)
{

  switch (event->type) 
    {
    case GDK_BUTTON_PRESS:
      switch(event->button.button) 
	{
	case 1:
	  gnome_canvas_item_hide(item);
	  moneyItem->inPocket = FALSE;
	  money_widget_remove(moneyItem->moneyWidget, moneyItem->value);
	  
	  if(moneyItem->moneyWidget->priv->targetWidget != NULL)
	    money_widget_add(moneyItem->moneyWidget->priv->targetWidget,
			     moneyItem->value);
	  
	  break;
	default:
	  break;
	}
    default:
      break;
    }
  return FALSE;
}