2010-07-31 [holger] 3.7.6cvs26
[claws.git] / src / gtk / gtkshruler.c
index 5a79123ce1014852d20afc3d2bf9e99514d3651f..66e548f6d7770206b3401bb9b6a0bb8743f70271 100644 (file)
@@ -1,11 +1,11 @@
 /* GtkSHRuler
  *
- *  Copyright (C) 2000 Alfons Hoogervorst
+ *  Copyright (C) 2000-2005 Alfons Hoogervorst & The Claws Mail Team
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 3 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,9 +13,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
  
 /* I derived this class from hruler. S in HRuler could be read as
  * last parameter has the distance between two ticks (which is
  * the width of the fixed font character!
  * 
- * -- Alfons (alfons@proteus.demon.nl)
+ * -- Alfons
  */
 
 #include <math.h>
 #include <stdio.h>
 #include <string.h>
-#include <gtk/gtkhruler.h>
+#include <gtk/gtk.h>
 #include "gtkshruler.h"
+#include "utils.h"
+#include "gtkutils.h"
 
 #define RULER_HEIGHT          14
 #define MINIMUM_INCR          5
@@ -44,9 +44,6 @@
 static void gtk_shruler_class_init     (GtkSHRulerClass *klass);
 static void gtk_shruler_init           (GtkSHRuler      *hruler);
 static void gtk_shruler_draw_ticks     (GtkRuler        *ruler);
-#if 0
-static void gtk_shruler_draw_pos       (GtkRuler        *ruler);
-#endif
 
 GType
 gtk_shruler_get_type(void)
@@ -80,8 +77,8 @@ gtk_shruler_class_init(GtkSHRulerClass * klass)
        GtkWidgetClass * widget_class;
        GtkRulerClass * hruler_class;
 
-       widget_class = (GtkWidgetClass*) klass;
-       hruler_class = (GtkRulerClass*) klass;
+       widget_class = GTK_WIDGET_CLASS(klass);
+       hruler_class = GTK_RULER_CLASS(klass);
 
        /* just neglect motion notify events */
        widget_class->motion_notify_event = NULL /* gtk_shruler_motion_notify */;
@@ -118,25 +115,25 @@ static void
 gtk_shruler_draw_ticks(GtkRuler *ruler)
 {
        GtkWidget *widget;
-       GdkGC *gc, *bg_gc;
-       GdkFont *font;
+       cairo_t *cr;
        gint i;
        gint width, height;
        gint xthickness;
        gint ythickness;
        gint pos;
 
-       g_return_if_fail (ruler != NULL);
-       g_return_if_fail (GTK_IS_HRULER (ruler));
+       cm_return_if_fail (ruler != NULL);
+       cm_return_if_fail (GTK_IS_HRULER (ruler));
 
-       if (!GTK_WIDGET_DRAWABLE (ruler)) 
+       if (!gtkut_widget_is_drawable (GTK_WIDGET(ruler))) 
                return;
 
        widget = GTK_WIDGET (ruler);
        
-       gc = widget->style->fg_gc[GTK_STATE_NORMAL];
-       bg_gc = widget->style->bg_gc[GTK_STATE_NORMAL];
-       font = gtk_style_get_font(widget->style);
+       cr = gdk_cairo_create(ruler->backing_store);
+       cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
+       cairo_set_line_width(cr, 1.);
+       gdk_cairo_set_source_color(cr, &gtk_widget_get_style(widget)->text[GTK_STATE_NORMAL]);
 
        xthickness = widget->style->xthickness;
        ythickness = widget->style->ythickness;
@@ -150,55 +147,35 @@ gtk_shruler_draw_ticks(GtkRuler *ruler)
                       0, 0, 
                       widget->allocation.width, widget->allocation.height);
 
-#if 0
-       gdk_draw_line (ruler->backing_store, gc,
-                      xthickness,
-                      height + ythickness,
-                      widget->allocation.width - xthickness,
-                      height + ythickness);
-#endif
-
-       /* assume ruler->max_size has the char width */    
-       /* i is increment of char_width,  pos is label number */
+       /* assume ruler->max_size has the char width */
+       /* i is increment of char_width,  pos is label number
+        * y position is based on height of widget itself */
        for ( i = 0, pos = 0; i < widget->allocation.width - xthickness; i += ruler->max_size, pos++ ) {        
-               int length = ythickness / 2;
+               gint length = height / 8;
        
-               if ( pos % 10 == 0 ) length = ( 4 * ythickness );
-               else if (pos % 5 == 0 ) length = ( 2 * ythickness );
+               if ( pos % 10 == 0 ) length = ( 2 * height / 3 );
+               else if ( pos % 5 == 0 ) length = ( height / 3 );
                
-               gdk_draw_line(ruler->backing_store, gc,
-                             i, height + ythickness, 
-                             i, height - length);                      
+               cairo_move_to(cr, i, height + ythickness);
+               cairo_line_to(cr, i, height - length);
+               cairo_stroke(cr);
                
                if ( pos % 10 == 0 ) {
-                       char buf[8];
+                       gchar buf[8];
+                       PangoLayout *layout;
+
                        /* draw label */
-                       sprintf(buf, "%d", (int) pos);
-                       gdk_draw_string(ruler->backing_store, font, gc,
-                                       i + 2, ythickness + font->ascent - 1,
-                                       buf);
-           }
+                       g_snprintf(buf, sizeof buf, "%d", pos);
+
+                       layout = gtk_widget_create_pango_layout
+                               (GTK_WIDGET(ruler), buf);
+
+                       cairo_move_to(cr, i+2, 0);
+                       pango_cairo_show_layout(cr, layout);
+
+                       g_object_unref(layout);
+               }
        }
-}
 
-/* gtk_ruler_set_pos() - does not work yet, need to reimplement 
- * gtk_ruler_draw_pos(). */
-void
-gtk_shruler_set_pos(GtkSHRuler * ruler, gfloat pos)
-{
-       GtkRuler * ruler_;
-       g_return_if_fail( ruler != NULL );
-       
-       ruler_ = GTK_RULER(ruler);
-       
-       if ( pos < ruler_->lower ) 
-               pos = ruler_->lower;
-       if ( pos > ruler_->upper )
-               pos = ruler_->upper;
-       
-       ruler_->position = pos; 
-       
-       /*  Make sure the ruler has been allocated already  */
-       if ( ruler_->backing_store != NULL )
-               gtk_ruler_draw_pos(ruler_);
+       cairo_destroy(cr);
 }