fixes
[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
22 #include "ui.h"
23 #include "callbacks.h"
24
25 static GtkWidget* graphix[10];          // contains all the graphics
26 static GtkWidget* big_digits[3];        // big digits that are shown as speed
27 static GtkWidget* small_digits[2];      // small digits that are shown on the screen
28
29 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
30
31 static void set_widget_bg_black(GtkWidget* widget) {
32         g_assert(widget);
33         GdkColor black;
34         black.red = 0x0000;
35         black.blue = 0x0000;
36         black.green = 0x0000;
37         gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
38 }
39
40 void load_graphix(AppData *appdata) {
41         g_assert(appdata);
42         g_print("Loading images\n");
43         guint i = 0;
44         while(i < 10) {
45                 char* path = g_malloc(30);
46                 g_sprintf(path, IMAGE_PATH, i);
47                 graphix[i] = gtk_image_new_from_file(path);
48                 g_print(path);
49                 g_print("\n");
50                 g_free(path);
51                 i++;
52         }
53 }
54
55 void set_digits_to_zero(AppData* appdata) {
56         g_assert(appdata);
57
58         GdkPixbuf* zero = gtk_image_get_pixbuf(GTK_IMAGE(graphix[0]));
59
60         big_digits[0] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
61         big_digits[1] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
62         big_digits[2] = GTK_WIDGET(gtk_image_new_from_pixbuf(zero));
63 }
64
65 void set_nth_digit(AppData* appdata, guint n, guint value) {
66
67         g_assert(value < 10);
68         g_assert(n < 3);
69
70         GtkWidget* image = big_digits[n];
71         GdkPixbuf* buf = gtk_image_get_pixbuf(GTK_IMAGE(graphix[value]));
72         gtk_image_set_from_pixbuf(GTK_IMAGE(image), buf);
73 }
74
75 void repaint_all_digits(AppData* appdata) {
76
77         gtk_widget_queue_draw(GTK_WIDGET(big_digits[0]));
78         gtk_widget_queue_draw(GTK_WIDGET(big_digits[1]));
79         gtk_widget_queue_draw(GTK_WIDGET(big_digits[2]));
80 }
81
82 void create_ui(AppData* appdata) {
83         g_assert(appdata);
84
85         appdata->unit = 1; // by default use km/h display
86
87         GtkWidget *hbox;
88         GtkWidget *vbox;
89
90         vbox = gtk_vbox_new(FALSE, 0);
91         hbox = gtk_hbox_new(TRUE, 0);
92
93         GtkWidget* top_e = gtk_event_box_new();
94         GtkWidget* bottom_e = gtk_event_box_new();
95
96         g_signal_connect(G_OBJECT(top_e),
97                         "button_press_event",
98                         G_CALLBACK(top_event_box_button_press),
99                         appdata);
100
101         g_signal_connect(G_OBJECT(bottom_e),
102                         "button_press_event",
103                         G_CALLBACK(bottom_event_box_button_press),
104                         appdata);
105
106         g_signal_connect(G_OBJECT(appdata->window),
107                         "delete_event",
108                         G_CALLBACK(gtk_main_quit),
109                         NULL);
110
111     g_signal_connect(G_OBJECT(appdata->window),
112                 "key_press_event",
113                 G_CALLBACK(key_press_cb),
114                 appdata->window);
115
116         // add three digits to the hbox
117         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[0]), FALSE, FALSE, 0);
118         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[1]), FALSE, FALSE, 0);
119         gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(big_digits[2]), FALSE, FALSE, 0);
120
121         gtk_box_pack_start_defaults(GTK_BOX(vbox), top_e); // add event box on top
122         gtk_box_pack_start_defaults(GTK_BOX(vbox), hbox); // numbers to the middle
123         gtk_box_pack_start_defaults(GTK_BOX(vbox), bottom_e); // add event box on bottom
124
125         gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
126
127         // set backgrounds black
128         set_widget_bg_black(GTK_WIDGET(appdata->window));
129         set_widget_bg_black(bottom_e);
130         set_widget_bg_black(top_e);
131
132         gtk_window_fullscreen(GTK_WINDOW(appdata->window));
133         gtk_widget_show_all(GTK_WIDGET(appdata->window));
134 }
135