Moved lots of UI stuff to ui.c.
[speedometer] / ui.c
1 /****
2         Speedometer, shows your current speed using GPS
3         Copyright (C) 2008 Wellu Mäkinen <wellu@wellu.org>
4
5         This program is free software: you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation, either version 3 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  ****/
18
19 #include <gtk/gtk.h>
20 #include <glib/gprintf.h>
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <math.h>
23 #include <gconf/gconf-client.h>
24
25 #include "ui.h"
26 #include "callbacks.h"
27
28 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
29
30 /* This is used when converting to other units
31  * number represents a multiplier which is used
32  * when converting the base unit to other units.
33  * Base unit is m/s thus the first multiplier is
34  * one. Units are in following order:
35  *
36  * m/s, km/h, mph
37  */
38 gdouble conversion[] = { 1, 3.6, 2.237 };
39
40 static GdkPixbuf* big_graphics[10];                     // contains all the big graphics
41 static GdkPixbuf* small_graphics[10];   // contains small graphics
42 static GtkWidget* digits[5];            //
43
44 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
45
46 static void set_widget_bg_black(GtkWidget* widget) {
47         g_assert(widget);
48         GdkColor black;
49         black.red = 0x0000;
50         black.blue = 0x0000;
51         black.green = 0x0000;
52         gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
53 }
54
55
56 /* Loads all the graphics from the disk and
57  * scales the images down to be used in several
58  * places. Might block for few seconds depending
59  * on the load.
60  */
61 void load_graphics() {
62         g_print("Loading and scaling images\n");
63         guint i = 0;
64
65         /* This loop will load all the images from the disk
66          * and store the pixbufs to the array. Pixbufs are
67          * correct size to be used in the big digits.
68          */
69         while(i < 10) {
70                 char* path = g_malloc(30);
71                 g_sprintf(path, IMAGE_PATH, i);
72                 g_print(path);
73                 g_print("\n");
74                 GError* error = NULL;
75                 big_graphics[i] = gdk_pixbuf_new_from_file(path, &error);
76                 if(error) {
77                         g_print("Error loading graphics: %s\n", error->message);
78                         g_error_free(error);
79                         error = NULL;
80                 }
81                 g_free(path);
82
83                 small_graphics[i] = gdk_pixbuf_scale_simple(
84                                 big_graphics[i],
85                                 60,
86                                 60,
87                                 GDK_INTERP_BILINEAR);
88
89                 i++;
90         }
91         g_print("Done\n");
92 }
93
94 void set_digits_to_zero() {
95         digits[0] = gtk_image_new_from_pixbuf(big_graphics[0]);
96         digits[1] = gtk_image_new_from_pixbuf(big_graphics[0]);
97         digits[2] = gtk_image_new_from_pixbuf(big_graphics[0]);
98         digits[3] = gtk_image_new_from_pixbuf(small_graphics[0]);
99         digits[4] = gtk_image_new_from_pixbuf(small_graphics[0]);
100 }
101
102 static void set_nth_digit(guint n, guint value) {
103         g_assert(value < 10);
104         g_assert(n < 5);
105
106         if(n < 3) {
107                 GtkWidget* image = digits[n];
108                 GdkPixbuf* buf = big_graphics[value];
109                 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
110         }
111         else {
112                 GtkWidget* image = digits[n];
113                 GdkPixbuf* buf = small_graphics[value];
114                 gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
115         }
116 }
117
118 static void repaint_all_digits() {
119         gtk_widget_queue_draw(GTK_WIDGET(digits[0]));
120         gtk_widget_queue_draw(GTK_WIDGET(digits[1]));
121         gtk_widget_queue_draw(GTK_WIDGET(digits[2]));
122 }
123
124 void create_ui(AppData* appdata) {
125         g_assert(appdata);
126
127         GtkWidget *hbox;
128         GtkWidget *bhbox;
129         GtkWidget *vbox;
130
131         vbox = gtk_vbox_new(FALSE, 0);
132         hbox = gtk_hbox_new(TRUE, 0);
133         bhbox = gtk_hbox_new(TRUE, 0);
134
135         GtkWidget* top_e = gtk_event_box_new();
136         GtkWidget* middle_e = gtk_event_box_new();
137         GtkWidget* bottom_e = gtk_event_box_new();
138
139         g_signal_connect(G_OBJECT(top_e),
140                         "button_press_event",
141                         G_CALLBACK(top_event_box_button_press),
142                         appdata);
143
144         g_signal_connect(G_OBJECT(middle_e),
145                         "button_press_event",
146                         G_CALLBACK(middle_event_box_button_press),
147                         appdata);
148
149         g_signal_connect(G_OBJECT(bottom_e),
150                         "button_press_event",
151                         G_CALLBACK(bottom_event_box_button_press),
152                         appdata);
153
154         g_signal_connect(G_OBJECT(appdata->window),
155                         "delete_event",
156                         G_CALLBACK(gtk_main_quit),
157                         NULL);
158
159         g_signal_connect(G_OBJECT(appdata->window),
160                         "key_press_event",
161                         G_CALLBACK(key_press_cb),
162                         appdata->window);
163
164         // add three big digits to the hbox
165         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[0]), FALSE, FALSE, 0);
166         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[1]), FALSE, FALSE, 0);
167         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
168
169         // add small digits to another hbox
170         gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
171         gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
172
173         // add hboxes to the event boxes
174         gtk_container_add(GTK_CONTAINER(middle_e), hbox);
175         gtk_container_add(GTK_CONTAINER(bottom_e), bhbox);
176
177         // add event boxes to the vbox
178         gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e);
179         gtk_box_pack_start_defaults(GTK_BOX(vbox), middle_e);
180         gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e);
181
182         // finally add the vertical box with all the children to the window
183         gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
184
185         // set backgrounds black
186         set_widget_bg_black(GTK_WIDGET(appdata->window));
187         set_widget_bg_black(bottom_e);
188         set_widget_bg_black(top_e);
189         set_widget_bg_black(middle_e);
190
191         gtk_window_fullscreen(GTK_WINDOW(appdata->window));
192         gtk_widget_show_all(GTK_WIDGET(appdata->window));
193 }
194
195 void interpret_and_set_speed(gdouble speed) {
196         g_assert(!isnan(speed));
197
198         /* speed is in m/s so let's convert
199          * it to the unit that we are using
200          */
201         speed *= conversion[1];
202
203         /* Convert float to a 6 digits (including dot) wide
204          * string with leading zeros. After conversion
205          * the speed might look like:
206          *
207          * 009.20 (9,20 km/h) or
208          * 010.90 (10,90 km/h) or
209          * 120.10 (120,10 km/h)
210          *
211          * Now, regardless of speed we know the position
212          * of the digits and can access the array directly.
213          */
214         gchar* charspeed = g_malloc(10); // alloc
215         g_sprintf(charspeed, "%0*.2f", 6, speed);
216
217         g_print("Speed is %s km/h\n", charspeed);
218
219         // these three lines will set the big digits
220         set_nth_digit(0, g_ascii_digit_value(charspeed[0]));
221         set_nth_digit(1, g_ascii_digit_value(charspeed[1]));
222         set_nth_digit(2, g_ascii_digit_value(charspeed[2]));
223
224         // these two lines will set the small digits
225         set_nth_digit(3, g_ascii_digit_value(charspeed[4]));
226         set_nth_digit(4, g_ascii_digit_value(charspeed[5]));
227
228         repaint_all_digits();
229
230         g_free(charspeed);
231 }
232
233 static void show_dialog() {
234         GtkWidget *dialog = gtk_message_dialog_new(
235                         NULL,
236                         GTK_DIALOG_MODAL,
237                         GTK_MESSAGE_INFO,
238                         GTK_BUTTONS_OK,
239                         "This program is licensed under GNU General Public License, "
240                         "which means (among other things) that you don't have to pay "
241                         "a dime for it. "
242                         "If you think, however, that this software is worth it, you "
243                         "can always drop me a postcard.\n\n"
244                         "Wellu Mäkinen\n"
245                         "PO BOX\n"
246                         "33580 Tampere\n"
247                         "FINLAND");
248         gtk_dialog_run(GTK_DIALOG(dialog));
249         gtk_widget_destroy(dialog);
250 }
251
252 void show_cardware_dialog() {
253         GConfClient* client = gconf_client_get_default();
254         g_assert(GCONF_IS_CLIENT(client));
255
256         GConfValue* gcvalue = NULL;
257         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
258
259         if(gcvalue == NULL) {
260                 g_print("GConf key not found so show dialog.\n");
261                 show_dialog();
262                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
263         }
264         g_object_unref(client);
265 }
266