Friendly email address to contact me.
[bityugov.git] / ccs / MyTotalWidget / MyTotalWidget.c
blobe095cb501c45a0dc42a273921cc811ebe5d13eea
1 #include <gtk/gtkspinbutton.h>
2 #include <gtk/gtksignal.h>
3 #include <gtk/gtkcalendar.h>
4 #include "MyTotal.h"
5 #include "MyTotalWidget.h"
6 #include "MyCounter.h"
7 #include "MyTouchscreen.h"
9 #define DEBUG 1
11 enum {
12 RECALCULATE_SIGNAL,
13 LAST_SIGNAL
17 static void mytotalwidget_class_init (MyTotalWidgetClass *klass);
18 static void mytotalwidget_init (MyTotalWidget *mtw);
19 static void mytotalwidget_destroy (GtkObject *object);
20 static void mytotalwidget_recalculate(gpointer *widget, MyTotalWidget *mtw);
22 static gint mytotalwidget_signals[LAST_SIGNAL] = { 0 };
25 GType
26 mytotalwidget_get_type (void)
28 static GType mtw_type = 0;
30 if (!mtw_type)
32 static const GTypeInfo mtw_info =
34 sizeof (MyTotalWidgetClass),
35 NULL, /* base_init */
36 NULL, /* base_finalize */
37 (GClassInitFunc) mytotalwidget_class_init,
38 NULL, /* class_finalize */
39 NULL, /* class_data */
40 sizeof (MyTotalWidget),
42 (GInstanceInitFunc) mytotalwidget_init,
45 mtw_type = g_type_register_static (GTK_TYPE_SPIN_BUTTON, "MyTotalWidget", &mtw_info, 0);
48 return mtw_type;
51 static void
52 mytotalwidget_class_init (MyTotalWidgetClass *klass)
55 GtkObjectClass *object_class;
56 GtkWidgetClass *widget_class;
58 object_class = (GtkObjectClass*) klass;
59 widget_class = (GtkWidgetClass*) klass;
61 object_class->destroy = mytotalwidget_destroy;
62 mytotalwidget_signals[RECALCULATE_SIGNAL] = g_signal_new ("recalculate",
63 G_TYPE_FROM_CLASS (klass),
64 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
65 G_STRUCT_OFFSET (MyTotalWidgetClass, mytotalwidget),
66 NULL,
67 NULL,
68 g_cclosure_marshal_VOID__VOID,
69 G_TYPE_NONE, 0);
71 g_type_class_add_private (object_class, sizeof (MyTotalWidgetPrivate));
74 static void mytotalwidget_destroy (GtkObject *object)
76 MyTotalWidget *mtw = MYTOTALWIDGET(object);
77 MyTotalWidgetPrivate *priv = MYTOTALWIDGET_GET_PRIVATE(mtw);
79 g_list_free(priv->linkto);
82 static void mytotalwidget_init (MyTotalWidget *mtw)
84 MyTotalWidgetPrivate *priv = MYTOTALWIDGET_GET_PRIVATE(mtw);
85 gtk_spin_button_set_adjustment(GTK_SPIN_BUTTON(mtw), (GtkAdjustment*)gtk_adjustment_new (0.0, 0.0, 10000.0, 0.0, 0.0, 0.0));
86 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(mtw), 2);
87 gtk_entry_set_alignment(GTK_ENTRY(mtw), 1.0);
89 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(mtw), TRUE);
90 gtk_entry_set_width_chars(GTK_ENTRY(mtw), 6);
91 gtk_editable_set_editable(GTK_EDITABLE(mtw), FALSE);
93 priv->linkto = NULL;
95 priv->start = NULL;
96 priv->end = NULL;
98 priv->calendar_multiply = NULL;
101 GtkWidget* mytotalwidget_new ()
103 return GTK_WIDGET (g_object_new (mytotalwidget_get_type (), NULL));
106 void mytotalwidget_calendar_multiplyer(MyTotalWidget *mtw, GtkCalendar *start, GtkCalendar *end, MyCounter *mc) {
107 if (start && end) {
108 MyTotalWidgetPrivate *priv = MYTOTALWIDGET_GET_PRIVATE(mtw);
109 priv->start = start;
110 priv->end = end;
112 g_object_ref(start);
113 g_object_ref(end);
115 g_signal_connect (G_OBJECT (priv->start), "day-selected", G_CALLBACK (mytotalwidget_recalculate), mtw);
116 g_signal_connect (G_OBJECT (priv->end), "day-selected", G_CALLBACK (mytotalwidget_recalculate), mtw);
117 g_signal_connect (G_OBJECT (priv->start), "month-changed", G_CALLBACK (mytotalwidget_recalculate), mtw);
118 g_signal_connect (G_OBJECT (priv->end), "month-changed", G_CALLBACK (mytotalwidget_recalculate), mtw);
120 priv->calendar_multiply = g_list_prepend(priv->calendar_multiply, mc);
124 void mytotalwidget_add_links(MyTotalWidget *mtw, GtkWidget *mt, MyTouchscreen *mts, gchar **linkto, GtkWidget *start, GtkWidget *end) {
125 guint amount = g_strv_length(linkto);
126 MyTotalWidgetPrivate *priv = MYTOTALWIDGET_GET_PRIVATE(mtw);
127 if (amount > 0) {
128 guint i;
129 GtkWidget *mc = NULL;
130 for (i = 0; i < amount; i++) {
131 if (mc && i > 0 && g_ascii_strcasecmp (linkto[i], "Calendar" ) == 0) {
132 mytotalwidget_calendar_multiplyer(MYTOTALWIDGET(mtw), GTK_CALENDAR(start), GTK_CALENDAR(end), MYCOUNTER(mc));
134 g_debug("%s", linkto[i]);
135 } else {
136 mc = mytouchscreen_get_counter(mts, linkto[i]);
137 if (mc == NULL) mc = mytotal_get_counter(MYTOTAL(mt), linkto[i]);
139 if (mc != NULL) {
140 priv->linkto = g_list_prepend(priv->linkto, mc);
141 g_signal_connect (G_OBJECT (mc), "recalculate", G_CALLBACK (mytotalwidget_recalculate), mtw);
142 } else {
143 g_warning("%s was not found to be linked.", linkto[i]);
147 } else {
148 gtk_editable_set_editable(GTK_EDITABLE(mtw), TRUE);
149 g_signal_connect(G_OBJECT(mtw), "value-changed", G_CALLBACK (mytotalwidget_recalculate), mtw);
151 g_debug("%d", g_list_length(priv->linkto));
154 static void mytotalwidget_recalculate(gpointer *widget, MyTotalWidget *mtw) {
155 MyTotalWidgetPrivate *priv = MYTOTALWIDGET_GET_PRIVATE(mtw);
156 gdouble total = 0.0;
157 gint multiplyer = 1;
158 GList *walker = g_list_first(priv->linkto);
160 if (walker != NULL) {
161 if (priv->start && priv->end) {
162 GDate *start, *end;
163 guint year, month, day;
164 gtk_calendar_get_date(priv->start, &year, &month, &day);
165 month++; /* Nice ;) */
166 start = g_date_new_dmy(day, month, year);
167 gtk_calendar_get_date(priv->end, &year, &month, &day);
168 month++; /* Nice ;) */
169 end = g_date_new_dmy(day, month, year);
170 multiplyer = g_date_days_between(start, end);
174 do {
175 gdouble element = 0.0;
176 if (IS_MYCOUNTER(walker->data)) element = mycounter_get_total(MYCOUNTER(walker->data));
177 if (IS_MYTOTALWIDGET(walker->data)) element = mytotalwidget_get_total(MYTOTALWIDGET(walker->data));
179 if (g_list_find(priv->calendar_multiply, walker->data) != NULL) {
180 total += (element * multiplyer);
181 } else {
182 total += element;
184 walker = g_list_next(walker);
185 } while(walker != NULL);
187 gtk_spin_button_set_value(GTK_SPIN_BUTTON(mtw), total);
191 g_signal_emit (G_OBJECT (mtw), mytotalwidget_signals[RECALCULATE_SIGNAL], 0);
194 gdouble mytotalwidget_get_total(MyTotalWidget *mtw) {
195 return gtk_spin_button_get_value(GTK_SPIN_BUTTON(mtw));