Zarro warnings.
[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
21 #include "ui.h"
22 #include "callbacks.h"
23
24 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
25
26 static void set_widget_bg_black(GtkWidget* widget) {
27         GdkColor black;
28         black.red = 0x0000;
29         black.blue = 0x0000;
30         black.green = 0x0000;
31         gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
32 }
33
34 void load_images(AppData *appdata) {
35         g_print("Loading images\n");
36         int i = 0;
37         while(i < 10) {
38                 char* path = malloc(30);
39                 g_sprintf(path, IMAGE_PATH, i);
40                 appdata->image_array[i] = (GtkImage*) gtk_image_new_from_file(path);
41                 g_print(path);
42                 g_print("\n");
43                 free(path);
44                 i++;
45         }
46 }
47
48 void reset_speed(AppData* appdata) {
49
50         GdkPixbuf* zero = gtk_image_get_pixbuf(appdata->image_array[0]);
51
52         appdata->speed_array[0] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
53         appdata->speed_array[1] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
54         appdata->speed_array[2] = (GtkImage*) gtk_image_new_from_pixbuf(zero);
55 }
56
57 guint randomize(AppData* appdata) {
58
59         gint32 n = g_random_int_range(0, 3);
60         GtkImage* image = appdata->speed_array[n];
61
62         gint32 m = g_random_int_range(0, 10);
63         GdkPixbuf* buf = gtk_image_get_pixbuf(appdata->image_array[m]);
64
65         g_print("Setting number %d to %d.. ", n+1, m);
66
67         gtk_image_set_from_pixbuf(image, buf);
68         gtk_widget_queue_draw(GTK_WIDGET(appdata->speed_array[n]));
69
70         return TRUE;
71 }
72
73 void create_ui(AppData* appdata) {
74
75         GtkWidget *hbox;
76         GtkWidget *vbox;
77
78         vbox = gtk_vbox_new(FALSE, 0);
79         hbox = gtk_hbox_new(TRUE, 0);
80
81         GtkWidget* top_e = gtk_event_box_new();
82         GtkWidget* bottom_e = gtk_event_box_new();
83
84         g_signal_connect(G_OBJECT(top_e),
85                         "button_press_event",
86                         G_CALLBACK(top_event_box_button_press),
87                         appdata);
88
89         g_signal_connect(G_OBJECT(bottom_e),
90                         "button_press_event",
91                         G_CALLBACK(bottom_event_box_button_press),
92                         appdata);
93
94         g_signal_connect(G_OBJECT(appdata->window),
95                         "delete_event",
96                         G_CALLBACK(gtk_main_quit),
97                         NULL);
98
99     g_signal_connect(G_OBJECT(appdata->window),
100                 "key_press_event",
101                 G_CALLBACK(key_press_cb),
102                 appdata->window);
103
104         // add three digits to the hbox
105         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[0]), FALSE, FALSE, 0);
106         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[1]), FALSE, FALSE, 0);
107         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->speed_array[2]), FALSE, FALSE, 0);
108
109         gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e); // add event box on top
110         gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox); // numbers to the middle
111         gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e); // add event box on bottom
112
113         gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
114
115
116         // set backgrounds black
117         set_widget_bg_black(GTK_WIDGET(appdata->window));
118         set_widget_bg_black(bottom_e);
119         set_widget_bg_black(top_e);
120
121 }
122