[Xfce4-commits] <xfce4-panel:devel> * Clock improvents and bug fix.

Nick Schermer nick at xfce.org
Tue Aug 11 20:22:54 CEST 2009


Updating branch refs/heads/devel
         to 649126e0b9085b0fefc6c214c3c0a05058f21376 (commit)
       from 0696fee17c91fb782b9bdf0f0f1d99e0ccb2e05c (commit)

commit 649126e0b9085b0fefc6c214c3c0a05058f21376
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Aug 10 18:45:00 2008 +0200

    * Clock improvents and bug fix.

 TODO                          |    2 -
 plugins/clock/clock-analog.c  |   25 +-------
 plugins/clock/clock-binary.c  |   25 +-------
 plugins/clock/clock-digital.c |   25 +-------
 plugins/clock/clock-lcd.c     |  139 ++++++++++++++++++++++------------------
 plugins/clock/clock.c         |   13 ++++-
 6 files changed, 91 insertions(+), 138 deletions(-)

diff --git a/TODO b/TODO
index acc6798..3c22d95 100644
--- a/TODO
+++ b/TODO
@@ -3,7 +3,6 @@ Things to do for Xfce Panel
 
 Pick From Trunk
 ---------------
- - Clock +1 px width, bug 3960.
  - DND to launcher, bug 3958.
 
 General
@@ -30,4 +29,3 @@ Plugins
    changes we edit the origional launcher (copy in
    ~/.local/share/applications/).
  - Transparent tray icons.
-
diff --git a/plugins/clock/clock-analog.c b/plugins/clock/clock-analog.c
index 984f7bd..5502fab 100644
--- a/plugins/clock/clock-analog.c
+++ b/plugins/clock/clock-analog.c
@@ -95,28 +95,7 @@ struct _XfceClockAnalog
 
 
 
-static GObjectClass *xfce_clock_analog_parent_class;
-
-
-
-GType
-xfce_clock_analog_get_type (void)
-{
-    static GType type = G_TYPE_INVALID;
-
-    if (G_UNLIKELY (type == G_TYPE_INVALID))
-    {
-        type = g_type_register_static_simple (GTK_TYPE_IMAGE,
-                                              I_("XfceClockAnalog"),
-                                              sizeof (XfceClockAnalogClass),
-                                              (GClassInitFunc) xfce_clock_analog_class_init,
-                                              sizeof (XfceClockAnalog),
-                                              (GInstanceInitFunc) xfce_clock_analog_init,
-                                              0);
-    }
-
-    return type;
-}
+G_DEFINE_TYPE (XfceClockAnalog, xfce_clock_analog, GTK_TYPE_IMAGE);
 
 
 
@@ -126,8 +105,6 @@ xfce_clock_analog_class_init (XfceClockAnalogClass *klass)
     GObjectClass   *gobject_class;
     GtkWidgetClass *gtkwidget_class;
 
-    xfce_clock_analog_parent_class = g_type_class_peek_parent (klass);
-
     gobject_class = G_OBJECT_CLASS (klass);
     gobject_class->finalize = xfce_clock_analog_finalize;
     gobject_class->set_property = xfce_clock_analog_set_property;
diff --git a/plugins/clock/clock-binary.c b/plugins/clock/clock-binary.c
index fd92abf..e71ed93 100644
--- a/plugins/clock/clock-binary.c
+++ b/plugins/clock/clock-binary.c
@@ -83,28 +83,7 @@ struct _XfceClockBinary
 
 
 
-static GObjectClass *xfce_clock_binary_parent_class;
-
-
-
-GType
-xfce_clock_binary_get_type (void)
-{
-    static GType type = G_TYPE_INVALID;
-
-    if (G_UNLIKELY (type == G_TYPE_INVALID))
-    {
-        type = g_type_register_static_simple (GTK_TYPE_IMAGE,
-                                              I_("XfceClockBinary"),
-                                              sizeof (XfceClockBinaryClass),
-                                              (GClassInitFunc) xfce_clock_binary_class_init,
-                                              sizeof (XfceClockBinary),
-                                              (GInstanceInitFunc) xfce_clock_binary_init,
-                                              0);
-    }
-
-    return type;
-}
+G_DEFINE_TYPE (XfceClockBinary, xfce_clock_binary, GTK_TYPE_IMAGE);
 
 
 
@@ -114,8 +93,6 @@ xfce_clock_binary_class_init (XfceClockBinaryClass *klass)
     GObjectClass   *gobject_class;
     GtkWidgetClass *gtkwidget_class;
 
-    xfce_clock_binary_parent_class = g_type_class_peek_parent (klass);
-
     gobject_class = G_OBJECT_CLASS (klass);
     gobject_class->finalize = xfce_clock_binary_finalize;
     gobject_class->set_property = xfce_clock_binary_set_property;
diff --git a/plugins/clock/clock-digital.c b/plugins/clock/clock-digital.c
index 6e82017..a92c482 100644
--- a/plugins/clock/clock-digital.c
+++ b/plugins/clock/clock-digital.c
@@ -68,28 +68,7 @@ struct _XfceClockDigital
 
 
 
-static GObjectClass *xfce_clock_digital_parent_class;
-
-
-
-GType
-xfce_clock_digital_get_type (void)
-{
-    static GType type = G_TYPE_INVALID;
-
-    if (G_UNLIKELY (type == G_TYPE_INVALID))
-    {
-        type = g_type_register_static_simple (GTK_TYPE_LABEL,
-                                              I_("XfceClockDigital"),
-                                              sizeof (XfceClockDigitalClass),
-                                              (GClassInitFunc) xfce_clock_digital_class_init,
-                                              sizeof (XfceClockDigital),
-                                              (GInstanceInitFunc) xfce_clock_digital_init,
-                                              0);
-    }
-
-    return type;
-}
+G_DEFINE_TYPE (XfceClockDigital, xfce_clock_digital, GTK_TYPE_LABEL);
 
 
 
@@ -98,8 +77,6 @@ xfce_clock_digital_class_init (XfceClockDigitalClass *klass)
 {
     GObjectClass *gobject_class;
 
-    xfce_clock_digital_parent_class = g_type_class_peek_parent (klass);
-
     gobject_class = G_OBJECT_CLASS (klass);
     gobject_class->finalize = xfce_clock_digital_finalize;
     gobject_class->set_property = xfce_clock_digital_set_property;
diff --git a/plugins/clock/clock-lcd.c b/plugins/clock/clock-lcd.c
index d164933..5c3f3ae 100644
--- a/plugins/clock/clock-lcd.c
+++ b/plugins/clock/clock-lcd.c
@@ -21,7 +21,9 @@
 #include <config.h>
 #endif
 
-
+#ifdef HAVE_MATH_H
+#include <math.h>
+#endif
 
 #include <gtk/gtk.h>
 #include <cairo/cairo.h>
@@ -29,8 +31,9 @@
 #include "clock.h"
 #include "clock-lcd.h"
 
-#define RELATIVE_SPACE 0.10
-
+#define RELATIVE_SPACE (0.10)
+#define RELATIVE_DIGIT (0.50)
+#define RELATIVE_DOTS  (3 * RELATIVE_SPACE)
 
 
 /* prototypes */
@@ -95,28 +98,7 @@ struct _XfceClockLcd
 
 
 
-static GObjectClass *xfce_clock_lcd_parent_class;
-
-
-
-GType
-xfce_clock_lcd_get_type (void)
-{
-    static GType type = G_TYPE_INVALID;
-
-    if (G_UNLIKELY (type == G_TYPE_INVALID))
-    {
-        type = g_type_register_static_simple (GTK_TYPE_IMAGE,
-                                              I_("XfceClockLcd"),
-                                              sizeof (XfceClockLcdClass),
-                                              (GClassInitFunc) xfce_clock_lcd_class_init,
-                                              sizeof (XfceClockLcd),
-                                              (GInstanceInitFunc) xfce_clock_lcd_init,
-                                              0);
-    }
-
-    return type;
-}
+G_DEFINE_TYPE (XfceClockLcd, xfce_clock_lcd, GTK_TYPE_IMAGE);
 
 
 
@@ -126,8 +108,6 @@ xfce_clock_lcd_class_init (XfceClockLcdClass *klass)
     GObjectClass   *gobject_class;
     GtkWidgetClass *gtkwidget_class;
 
-    xfce_clock_lcd_parent_class = g_type_class_peek_parent (klass);
-
     gobject_class = G_OBJECT_CLASS (klass);
     gobject_class->finalize = xfce_clock_lcd_finalize;
     gobject_class->set_property = xfce_clock_lcd_set_property;
@@ -291,9 +271,6 @@ xfce_clock_lcd_size_request (GtkWidget      *widget,
         requisition->height = height;
         requisition->width = height * ratio;
     }
-    
-    /* increase the width with 1 px for rounding errors */
-    requisition->width++;
 }
 
 
@@ -312,15 +289,22 @@ xfce_clock_lcd_expose_event (GtkWidget      *widget,
 
     g_return_val_if_fail (XFCE_CLOCK_IS_LCD (clock), FALSE);
 
+    /* get the width:height ratio */
+    ratio = xfce_clock_lcd_get_ratio (XFCE_CLOCK_LCD (widget));
+
     /* size of a digit should be a fraction of 10 */
     size = widget->allocation.height - widget->allocation.height % 10;
 
-    /* get the width:height ratio */
-    ratio = xfce_clock_lcd_get_ratio (XFCE_CLOCK_LCD (widget));
+    /* make sure we also fit on small vertical panels */
+    size = MIN (rint ((gdouble) widget->allocation.width / ratio), size);
 
     /* begin offsets */
-    offset_x = widget->allocation.x + (widget->allocation.width - (size * ratio)) / 2;
-    offset_y = widget->allocation.y + (widget->allocation.height - size) / 2;
+    offset_x = rint ((widget->allocation.width - (size * ratio)) / 2.00);
+    offset_y = rint ((widget->allocation.height - size) / 2.00);
+
+    /* only allow positive values from the base point */
+    offset_x = widget->allocation.x + MAX (0.00, offset_x);
+    offset_y = widget->allocation.y + MAX (0.00, offset_y);
 
     /* get the cairo context */
     cr = gdk_cairo_create (widget->window);
@@ -409,8 +393,8 @@ xfce_clock_lcd_get_ratio (XfceClockLcd *clock)
     /* get the local time */
     xfce_clock_util_get_localtime (&tm);
 
-    /* hour + minutes */
-    ratio = (3 * 0.5 + 6 * RELATIVE_SPACE);
+    /* 8:88 */
+    ratio = (3 * RELATIVE_DIGIT) + RELATIVE_DOTS + RELATIVE_SPACE;
 
     ticks = tm.tm_hour;
 
@@ -418,13 +402,13 @@ xfce_clock_lcd_get_ratio (XfceClockLcd *clock)
         ticks -= 12;
 
     if (ticks >= 10)
-        ratio += (0.5 + RELATIVE_SPACE);
+        ratio += RELATIVE_DIGIT + RELATIVE_SPACE;
 
     if (clock->show_seconds)
-        ratio += (2 * 0.5 + 4 * RELATIVE_SPACE);
+        ratio += (2 * RELATIVE_DIGIT) + RELATIVE_SPACE + RELATIVE_DOTS;
 
     if (clock->show_meridiem)
-        ratio += (0.5 + RELATIVE_SPACE);
+        ratio += RELATIVE_DIGIT + RELATIVE_SPACE;
 
     return ratio;
 }
@@ -439,10 +423,20 @@ xfce_clock_lcd_draw_dots (cairo_t *cr,
 {
     gint i;
 
-    /* draw the dots */
-    for (i = 1; i < 3; i++)
-        cairo_rectangle (cr, offset_x, offset_y + size * 0.30 * i,
-                         size * 0.10, size * 0.10);
+    if (size >= 10)
+    {
+        /* draw the dots (with rounding) */
+        for (i = 1; i < 3; i++)
+            cairo_rectangle (cr, rint (offset_x), rint (offset_y + size * RELATIVE_DOTS * i),
+                             rint (size * RELATIVE_SPACE), rint (size * RELATIVE_SPACE));
+    }
+    else
+    {
+        /* draw the dots */
+        for (i = 1; i < 3; i++)
+            cairo_rectangle (cr, offset_x, offset_y + size * RELATIVE_DOTS * i,
+                             size * RELATIVE_SPACE, size * RELATIVE_SPACE);
+    }
 
     /* fill the dots */
     cairo_fill (cr);
@@ -462,24 +456,32 @@ xfce_clock_lcd_draw_digit (cairo_t *cr,
     gint    i, j;
     gint    segment;
     gdouble x, y;
+    gdouble rel_x, rel_y;
 
     g_return_val_if_fail (number >= 0 || number <= 11, offset_x);
 
+    /* ##1##
+     * 6   2
+     * ##7##
+     * 5   3
+     * ##4##
+     */
+
     /* coordicates to draw for each segment */
-    gdouble segments_x[][6] = { { 0.02, 0.48, 0.38, 0.12, -1.0, 0.00 },
-                                { 0.40, 0.50, 0.50, 0.40, -1.0, 0.00 },
-                                { 0.40, 0.50, 0.50, 0.40, -1.0, 0.00 },
-                                { 0.12, 0.38, 0.48, 0.02, -1.0, 0.00 },
-                                { 0.00, 0.10, 0.10, 0.00, -1.0, 0.00 },
-                                { 0.00, 0.10, 0.10, 0.00, -1.0, 0.00 },
-                                { 0.00, 0.10, 0.40, 0.50, 0.40, 0.10 } };
-    gdouble segments_y[][6] = { { 0.00, 0.00, 0.10, 0.10, -1.0, 0.00 },
-                                { 0.12, 0.02, 0.48, 0.43, -1.0, 0.00 },
-                                { 0.57, 0.52, 0.98, 0.88, -1.0, 0.00 },
-                                { 0.90, 0.90, 1.00, 1.00, -1.0, 0.00 },
-                                { 0.52, 0.57, 0.88, 0.98, -1.0, 0.00 },
-                                { 0.02, 0.12, 0.43, 0.48, -1.0, 0.00 },
-                                { 0.50, 0.45, 0.45, 0.50, 0.55, 0.55 } };
+    gdouble segments_x[][6] = { { 0.02, 0.48, 0.38, 0.12, -1.0, 0.00 },     /* 1x */
+                                { 0.40, 0.505, 0.505, 0.40, -1.0, 0.00 },   /* 2x */
+                                { 0.40, 0.505, 0.505, 0.40, -1.0, 0.00 },   /* 3x */
+                                { 0.12, 0.38, 0.48, 0.02, -1.0, 0.00 },     /* 4x */
+                                { 0.00, 0.105, 0.105, 0.00, -1.0, 0.00 },   /* 5x */
+                                { 0.00, 0.105, 0.105, 0.00, -1.0, 0.00 },   /* 6x */
+                                { 0.00, 0.10, 0.40, 0.50, 0.40, 0.10 } };   /* 7x */
+    gdouble segments_y[][6] = { { 0.00, 0.00, 0.105, 0.105, -1.0, 0.00 },   /* 1y */
+                                { 0.12, 0.02, 0.48, 0.43, -1.0, 0.00 },     /* 2y */
+                                { 0.57, 0.52, 0.98, 0.88, -1.0, 0.00 },     /* 3y */
+                                { 0.90, 0.90, 1.00, 1.00, -1.0, 0.00 },     /* 4y */
+                                { 0.52, 0.57, 0.88, 0.98, -1.0, 0.00 },     /* 5y */
+                                { 0.02, 0.12, 0.43, 0.48, -1.0, 0.00 },     /* 6y */
+                                { 0.50, 0.445, 0.445, 0.50, 0.55, 0.55 } }; /* 7y */
 
     /* segment to draw for each number: 0, 1, ..., 9, A, P */
     gint    numbers[][8] = { { 0, 1, 2, 3, 4, 5, -1 },
@@ -507,15 +509,26 @@ xfce_clock_lcd_draw_digit (cairo_t *cr,
         /* walk through the coordinate points */
         for (j = 0; j < 6; j++)
         {
-            /* get x and y coordinates for this point */
-            x = segments_x[segment][j] * size + offset_x;
-            y = segments_y[segment][j] * size + offset_y;
+            /* get the relative sizes */
+            rel_x = segments_x[segment][j];
+            rel_y = segments_y[segment][j];
 
             /* leave when there are no valid coordinates */
-            if (x < 0 || y < 0)
+            if (rel_x == -1.00 || rel_y == -1.00)
                 break;
 
-            if (j == 0)
+            /* get x and y coordinates for this point */
+            x = rel_x * size + offset_x;
+            y = rel_y * size + offset_y;
+
+            /* when 0.01 * size is larger then 1, round the numbers */
+            if (size >= 10)
+            {
+                x = rint (x);
+                y = rint (y);
+            }
+
+            if (G_UNLIKELY (j == 0))
                 cairo_move_to (cr, x, y);
             else
                 cairo_line_to (cr, x, y);
@@ -528,7 +541,7 @@ xfce_clock_lcd_draw_digit (cairo_t *cr,
     /* fill the segments */
     cairo_fill (cr);
 
-    return (offset_x + size * (0.50 + RELATIVE_SPACE));
+    return (offset_x + size * (RELATIVE_DIGIT + RELATIVE_SPACE));
 }
 
 
diff --git a/plugins/clock/clock.c b/plugins/clock/clock.c
index 601ed41..2ec3889 100644
--- a/plugins/clock/clock.c
+++ b/plugins/clock/clock.c
@@ -510,12 +510,13 @@ xfce_clock_plugin_set_size (ClockPlugin *clock,
                             guint        size)
 {
     GtkOrientation orientation;
+    gint           clock_size;
 
     /* set the frame border */
     gtk_container_set_border_width (GTK_CONTAINER (clock->frame), size > 26 ? 1 : 0);
 
     /* get the clock size */
-    size -= size > 26 ? 6 : 4;
+    clock_size = CLAMP (size - (size > 26 ? 6 : 4), 1, G_MAXINT);
 
     /* get plugin orientation */
     orientation = xfce_panel_plugin_get_orientation (clock->plugin);
@@ -532,6 +533,15 @@ xfce_clock_plugin_set_size (ClockPlugin *clock,
 
 
 static void
+xfce_clock_plugin_set_orientation (ClockPlugin *clock)
+{
+    /* do a size request */
+    xfce_clock_plugin_set_size (clock, xfce_panel_plugin_get_size (clock->plugin));
+}
+
+
+
+static void
 xfce_clock_plugin_read (ClockPlugin *clock)
 {
     gchar       *filename;
@@ -658,5 +668,6 @@ xfce_clock_plugin_construct (XfcePanelPlugin *plugin)
     g_signal_connect_swapped (G_OBJECT (plugin), "save", G_CALLBACK (xfce_clock_plugin_write), clock);
     g_signal_connect_swapped (G_OBJECT (plugin), "free-data", G_CALLBACK (xfce_clock_plugin_free), clock);
     g_signal_connect_swapped (G_OBJECT (plugin), "configure-plugin", G_CALLBACK (xfce_clock_dialog_show), clock);
+    g_signal_connect_swapped (G_OBJECT (plugin), "orientation-changed", G_CALLBACK (xfce_clock_plugin_set_orientation), clock);
 }
 



More information about the Xfce4-commits mailing list