0.3 release with experimental layoyt.
[speedometer] / ui.c
diff --git a/ui.c b/ui.c
new file mode 100644 (file)
index 0000000..bb30df7
--- /dev/null
+++ b/ui.c
@@ -0,0 +1,122 @@
+/****
+       Speedometer, shows your current speed using GPS
+       Copyright (C) 2008 Wellu Mäkinen <wellu@wellu.org>
+
+       This program is free software: you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation, either version 3 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ ****/
+
+#include <gtk/gtk.h>
+
+#include "ui.h"
+#include "callbacks.h"
+
+#define IMAGE_PATH "/usr/share/speedometer/%d.png"
+
+static void set_widget_bg_black(GtkWidget* widget) {
+       GdkColor black;
+       black.red = 0x0000;
+       black.blue = 0x0000;
+       black.green = 0x0000;
+       gtk_widget_modify_bg(GTK_WIDGET(widget), GTK_STATE_NORMAL, &black);
+}
+
+void load_images(AppData *appdata) {
+       g_print("Loading images\n");
+       int i = 0;
+       while(i < 10) {
+               char* path = malloc(30);
+               g_sprintf(path, IMAGE_PATH, i);
+               appdata->image_array[i] = gtk_image_new_from_file(path);
+               g_print(path);
+               g_print("\n");
+               free(path);
+               i++;
+       }
+}
+
+void reset_speed(AppData* appdata) {
+
+       GdkPixbuf* zero = gtk_image_get_pixbuf(appdata->image_array[0]);
+
+       appdata->speed_array[0] = gtk_image_new_from_pixbuf(zero);
+       appdata->speed_array[1] = gtk_image_new_from_pixbuf(zero);
+       appdata->speed_array[2] = gtk_image_new_from_pixbuf(zero);
+}
+
+guint randomize(AppData* appdata) {
+
+       gint32 n = g_random_int_range(0, 3);
+       GtkImage* image = appdata->speed_array[n];
+
+       gint32 m = g_random_int_range(0, 10);
+       GdkPixbuf* buf = gtk_image_get_pixbuf(appdata->image_array[m]);
+
+       g_print("Setting number %d to %d.. ", n+1, m);
+
+       gtk_image_set_from_pixbuf(image, buf);
+       gtk_widget_queue_draw(appdata->speed_array[n]);
+
+       return TRUE;
+}
+
+void create_ui(AppData* appdata) {
+
+       GtkWidget *hbox;
+       GtkWidget *vbox;
+
+       vbox = gtk_vbox_new(FALSE, 0);
+       hbox = gtk_hbox_new(TRUE, 0);
+
+       GtkWidget* top_e = gtk_event_box_new();
+       GtkWidget* bottom_e = gtk_event_box_new();
+
+       g_signal_connect(G_OBJECT(top_e),
+                       "button_press_event",
+                       G_CALLBACK(top_event_box_button_press),
+                       appdata);
+
+       g_signal_connect(G_OBJECT(bottom_e),
+                       "button_press_event",
+                       G_CALLBACK(bottom_event_box_button_press),
+                       appdata);
+
+       g_signal_connect(G_OBJECT(appdata->window),
+                       "delete_event",
+                       G_CALLBACK(gtk_main_quit),
+                       NULL);
+
+    g_signal_connect(G_OBJECT(appdata->window),
+               "key_press_event",
+               G_CALLBACK(key_press_cb),
+               appdata->window);
+
+       // add three digits to the hbox
+       gtk_box_pack_start(hbox, appdata->speed_array[0], FALSE, FALSE, 0);
+       gtk_box_pack_start(hbox, appdata->speed_array[1], FALSE, FALSE, 0);
+       gtk_box_pack_start(hbox, appdata->speed_array[2], FALSE, FALSE, 0);
+
+       gtk_box_pack_start_defaults(vbox, top_e); // add event box on top
+       gtk_box_pack_start_defaults(vbox, hbox); // numbers to the middle
+       gtk_box_pack_start_defaults(vbox, bottom_e); // add event box on bottom
+
+       gtk_container_add(GTK_CONTAINER(appdata->window), GTK_WIDGET(vbox));
+
+
+       // set backgrounds black
+       set_widget_bg_black(appdata->window);
+       set_widget_bg_black(bottom_e);
+       set_widget_bg_black(top_e);
+
+}
+