git-svn-id: file:///svnroot/speedometer/trunk@44 df364472-da61-43ef-8a67-511c89aa921b
[speedometer] / ui.c
diff --git a/ui.c b/ui.c
index d5394c7..70a7cfe 100644 (file)
--- a/ui.c
+++ b/ui.c
  * Base unit is m/s thus the first multiplier is
  * one. Units are in following order:
  *
- * m/s, km/h, mph
+ * m/s, km/h, mph, f/s, knot
  */
-gdouble conversion[] = { 1, 3.6, 2.237 };
+gdouble conversion[] = { 1, 3.6, 2.237, 3.281, 1.944 };
+guint unit = 1;
+#define UNIT_COUNT 4
+// TODO: wrap this mess up somewhere
+// TODO: knot graphics are missing
 
-static GdkPixbuf* big_graphics[10];                    // contains all the big graphics
-static GdkPixbuf* small_graphics[10];  // contains small graphics
-static GtkWidget* digits[5];           //
+static GdkPixbuf* big_graphics[14];
+static GdkPixbuf* small_graphics[14];
+static GtkWidget* digits[5];
+static GtkWidget* unit_graphic;
 
 #define IMAGE_PATH "/usr/share/speedometer/%d.png"
 
@@ -66,7 +71,7 @@ void load_graphics() {
         * and store the pixbufs to the array. Pixbufs are
         * correct size to be used in the big digits.
         */
-       while(i < 10) {
+       while(i < 14) {
                char* path = g_malloc(30);
                g_sprintf(path, IMAGE_PATH, i);
                g_print(path);
@@ -82,8 +87,8 @@ void load_graphics() {
 
                small_graphics[i] = gdk_pixbuf_scale_simple(
                                big_graphics[i],
-                               60,
-                               60,
+                               150,
+                               150,
                                GDK_INTERP_BILINEAR);
 
                i++;
@@ -97,6 +102,8 @@ void set_digits_to_zero() {
        digits[2] = gtk_image_new_from_pixbuf(big_graphics[0]);
        digits[3] = gtk_image_new_from_pixbuf(small_graphics[0]);
        digits[4] = gtk_image_new_from_pixbuf(small_graphics[0]);
+       // TODO: load this from GConf
+       unit_graphic = gtk_image_new_from_pixbuf(small_graphics[11]);
 }
 
 static void set_nth_digit(guint n, guint value) {
@@ -167,6 +174,7 @@ void create_ui(AppData* appdata) {
        gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(digits[2]), FALSE, FALSE, 0);
 
        // add small digits to another hbox
+       gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(unit_graphic), FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[3]), FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(bhbox), GTK_WIDGET(digits[4]), FALSE, FALSE, 0);
 
@@ -192,17 +200,29 @@ void create_ui(AppData* appdata) {
        gtk_widget_show_all(GTK_WIDGET(appdata->window));
 }
 
+void change_unit() {
+       unit++;
+
+       if(unit >= UNIT_COUNT) {
+               unit = 0;
+       }
+       g_print("Unit used in conversion: %f\n", conversion[unit]);
+
+       GdkPixbuf* buf = small_graphics[10 + unit];
+       gtk_image_set_from_pixbuf(GTK_IMAGE(unit_graphic), buf);
+}
+
 void interpret_and_set_speed(gdouble speed) {
        g_assert(!isnan(speed));
 
        /* speed is in m/s so let's convert
         * it to the unit that we are using
         */
-       speed *= conversion[1];
+       speed *= conversion[unit];
 
        /* Convert float to a 6 digits (including dot) wide
         * string with leading zeros. After conversion
-        * the speed might look like:
+        * the speed looks like:
         *
         * 009.20 (9,20 km/h) or
         * 010.90 (10,90 km/h) or