Minor thingies..
[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         g_assert(widget);
28         GdkColor black;
29         black.red = 0x0000;
30         black.blue = 0x0000;
31         black.green = 0x0000;
32         gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
33 }
34
35 void load_images(AppData *appdata) {
36         g_assert(appdata);
37         g_print("Loading images\n");
38         guint i = 0;
39         while(i < 10) {
40                 char* path = g_malloc(30);
41                 g_sprintf(path, IMAGE_PATH, i);
42                 appdata->image_array[i] = gtk_image_new_from_file(path);
43                 g_print(path);
44                 g_print("\n");
45                 g_free(path);
46                 i++;
47         }
48 }
49
50 void load_initial_pixbufs(AppData* appdata) {
51         g_assert(appdata);
52
53         GdkPixbuf* zero = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[0]));
54
55         appdata->digit_array[0] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
56         appdata->digit_array[1] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
57         appdata->digit_array[2] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
58 }
59
60 void set_nth_digit(AppData* appdata, guint n, guint value) {
61         g_assert(appdata);
62         g_assert(value < 10);
63         g_assert(n < 3);
64
65         GtkWidget* image = appdata->digit_array[n];
66         GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[value]));
67         gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
68 }
69
70 void repaint_all_digits(AppData* appdata) {
71         g_assert(appdata);
72         gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[0]));
73         gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[1]));
74         gtk_widget_queue_draw(GTK_WIDGET(appdata->digit_array[2]));
75 }
76
77 // used only in testing...
78 void randomize(AppData* appdata) {
79         g_assert(appdata);
80         gint32 n = g_random_int_range(0, 3);
81         GtkWidget* image = appdata->digit_array[n];
82
83         gint32 m = g_random_int_range(0, 10);
84         GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(appdata->image_array[m]));
85
86         g_print("Setting number %d to %d.. ", n+1, m);
87
88         gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
89         gtk_widget_queue_draw(appdata->digit_array[n]);
90 }
91
92 void create_ui(AppData* appdata) {
93         g_assert(appdata);
94         GtkWidget *hbox;
95         GtkWidget *vbox;
96
97         vbox = gtk_vbox_new(FALSE, 0);
98         hbox = gtk_hbox_new(TRUE, 0);
99
100         GtkWidget* top_e = gtk_event_box_new();
101         GtkWidget* bottom_e = gtk_event_box_new();
102
103         g_signal_connect(G_OBJECT(top_e),
104                         "button_press_event",
105                         G_CALLBACK(top_event_box_button_press),
106                         appdata);
107
108         g_signal_connect(G_OBJECT(bottom_e),
109                         "button_press_event",
110                         G_CALLBACK(bottom_event_box_button_press),
111                         appdata);
112
113         g_signal_connect(G_OBJECT(appdata->window),
114                         "delete_event",
115                         G_CALLBACK(gtk_main_quit),
116                         NULL);
117
118     g_signal_connect(G_OBJECT(appdata->window),
119                 "key_press_event",
120                 G_CALLBACK(key_press_cb),
121                 appdata->window);
122
123         // add three digits to the hbox
124         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[0]), FALSE, FALSE, 0);
125         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[1]), FALSE, FALSE, 0);
126         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(appdata->digit_array[2]), FALSE, FALSE, 0);
127
128         gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e); // add event box on top
129         gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox); // numbers to the middle
130         gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e); // add event box on bottom
131
132         gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
133
134         // set backgrounds black
135         set_widget_bg_black(GTK_WIDGET(appdata->window));
136         set_widget_bg_black(bottom_e);
137         set_widget_bg_black(top_e);
138
139 }
140