1 #include <gtk/gtkspinbutton.h>
2 #include <gtk/gtksignal.h>
3 #include <gtk/gtkcalendar.h>
5 #include "MyTotalWidget.h"
7 #include "MyTouchscreen.h"
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 };
26 mytotalwidget_get_type (void)
28 static GType mtw_type
= 0;
32 static const GTypeInfo mtw_info
=
34 sizeof (MyTotalWidgetClass
),
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);
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
),
68 g_cclosure_marshal_VOID__VOID
,
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
);
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
) {
108 MyTotalWidgetPrivate
*priv
= MYTOTALWIDGET_GET_PRIVATE(mtw
);
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
);
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
]);
136 mc
= mytouchscreen_get_counter(mts
, linkto
[i
]);
137 if (mc
== NULL
) mc
= mytotal_get_counter(MYTOTAL(mt
), linkto
[i
]);
140 priv
->linkto
= g_list_prepend(priv
->linkto
, mc
);
141 g_signal_connect (G_OBJECT (mc
), "recalculate", G_CALLBACK (mytotalwidget_recalculate
), mtw
);
143 g_warning("%s was not found to be linked.", linkto
[i
]);
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
);
158 GList
*walker
= g_list_first(priv
->linkto
);
160 if (walker
!= NULL
) {
161 if (priv
->start
&& priv
->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
);
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
);
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
));