[planner: 41/61] gantt-header: Port to cairo drawing




commit 9166daa7b35f9947a68770da03eb7599b55bf587
Author: Mart Raudsepp <leio gentoo org>
Date:   Tue Dec 29 01:26:56 2020 +0200

    gantt-header: Port to cairo drawing

 src/planner-gantt-header.c | 79 +++++++++++++++++++++-------------------------
 1 file changed, 36 insertions(+), 43 deletions(-)
---
diff --git a/src/planner-gantt-header.c b/src/planner-gantt-header.c
index eda7cf7c..15bd58fe 100644
--- a/src/planner-gantt-header.c
+++ b/src/planner-gantt-header.c
@@ -487,7 +487,8 @@ gantt_header_expose_event (GtkWidget      *widget,
 {
        PlannerGanttHeader     *header;
        PlannerGanttHeaderPriv *priv;
-       gint                    width, height;
+       cairo_t                *cr;
+       gint                    height;
        gdouble                 hscale;
        gint                    x;
        mrptime                 t0;
@@ -496,8 +497,10 @@ gantt_header_expose_event (GtkWidget      *widget,
        gchar                  *str;
        gint                    minor_width;
        gint                    major_width;
-       GdkGC                  *gc;
-       GdkRectangle            rect;
+
+       cr = gdk_cairo_create (event->window);
+       cairo_set_line_width (cr, 1.0);
+       cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
 
        header = PLANNER_GANTT_HEADER (widget);
        priv = header->priv;
@@ -506,7 +509,6 @@ gantt_header_expose_event (GtkWidget      *widget,
        t0 = floor ((priv->x1 + event->area.x) / hscale + 0.5);
        t1 = floor ((priv->x1 + event->area.x + event->area.width) / hscale + 0.5);
 
-       width = gdk_window_get_width (event->window);
        height = gdk_window_get_height (event->window);
 
        /* Draw background. We only draw over the exposed area, padding with +/-
@@ -524,12 +526,11 @@ gantt_header_expose_event (GtkWidget      *widget,
                       event->area.width + 10,
                       height);
 
-       gdk_draw_line (event->window,
-                      widget->style->fg_gc[GTK_STATE_INSENSITIVE],
-                      event->area.x,
-                      height / 2,
-                      event->area.x + event->area.width,
-                      height / 2);
+       // Horizontal full width line between week number and date rows
+       gdk_cairo_set_source_color (cr, &widget->style->fg[GTK_STATE_INSENSITIVE]);
+       cairo_move_to (cr, event->area.x + 0.5, height / 2 + 0.5);
+       cairo_line_to (cr, event->area.x + event->area.width + 0.5, height / 2 + 0.5);
+       cairo_stroke (cr);
 
        /* Get the widths of major/minor ticks so that we know how wide to make
         * the clip region.
@@ -540,12 +541,6 @@ gantt_header_expose_event (GtkWidget      *widget,
        minor_width = hscale * (mrp_time_align_next (t0, priv->minor_unit) -
                                mrp_time_align_prev (t0, priv->minor_unit));
 
-       gc = gdk_gc_new (widget->window);
-       gdk_gc_copy (gc, widget->style->text_gc[GTK_STATE_NORMAL]);
-
-       rect.y = 0;
-       rect.height = height;
-
        /* Draw the major scale. */
        if (major_width < 2 || priv->major_unit == MRP_TIME_UNIT_NONE) {
                /* Unless it's too thin to make sense. */
@@ -557,10 +552,11 @@ gantt_header_expose_event (GtkWidget      *widget,
        while (t <= t1) {
                x = floor (t * hscale - priv->x1 + 0.5);
 
-               gdk_draw_line (event->window,
-                              widget->style->fg_gc[GTK_STATE_INSENSITIVE],
-                              x, 0,
-                              x, height / 2);
+               // Vertical lines between different weeks
+               gdk_cairo_set_source_color (cr, &widget->style->fg[GTK_STATE_INSENSITIVE]);
+               cairo_move_to (cr, x + 0.5, 0.5);
+               cairo_line_to (cr, x + 0.5, height / 2 + 1);
+               cairo_stroke (cr);
 
                str = planner_scale_format_time (t,
                                            priv->major_unit,
@@ -570,15 +566,13 @@ gantt_header_expose_event (GtkWidget      *widget,
                                       -1);
                g_free (str);
 
-               rect.x = x;
-               rect.width = major_width;
-               gdk_gc_set_clip_rectangle (gc, &rect);
-
-               gdk_draw_layout (event->window,
-                                gc,
-                                x + 3,
-                                2,
-                                priv->layout);
+               cairo_save (cr);
+               cairo_rectangle (cr, x, 0, major_width, height);
+               cairo_clip (cr);
+               gdk_cairo_set_source_color (cr, &widget->style->text[GTK_STATE_NORMAL]);
+               cairo_move_to (cr, x + 3, 2);
+               pango_cairo_show_layout (cr, priv->layout);
+               cairo_restore (cr);
 
                t = mrp_time_align_next (t, priv->major_unit);
        }
@@ -596,10 +590,11 @@ gantt_header_expose_event (GtkWidget      *widget,
        while (t <= t1) {
                x = floor (t * hscale - priv->x1 + 0.5);
 
-               gdk_draw_line (event->window,
-                              widget->style->fg_gc[GTK_STATE_INSENSITIVE],
-                              x, height / 2,
-                              x, height);
+               // NOTE: Vertical lines between dates
+               gdk_cairo_set_source_color (cr, &widget->style->fg[GTK_STATE_INSENSITIVE]);
+               cairo_move_to (cr, x + 0.5, height / 2 + 0.5);
+               cairo_line_to (cr, x + 0.5, height + 0.5);
+               cairo_stroke (cr);
 
                str = planner_scale_format_time (t,
                                            priv->minor_unit,
@@ -609,21 +604,19 @@ gantt_header_expose_event (GtkWidget      *widget,
                                       -1);
                g_free (str);
 
-               rect.x = x;
-               rect.width = minor_width;
-               gdk_gc_set_clip_rectangle (gc, &rect);
-
-               gdk_draw_layout (event->window,
-                                gc,
-                                x + 3,
-                                height / 2 + 2,
-                                priv->layout);
+               cairo_save (cr);
+               cairo_rectangle (cr, x, 0, minor_width, height);
+               cairo_clip (cr);
+               gdk_cairo_set_source_color (cr, &widget->style->text[GTK_STATE_NORMAL]);
+               cairo_move_to (cr, x + 3, height / 2 + 2);
+               pango_cairo_show_layout (cr, priv->layout);
+               cairo_restore (cr);
 
                t = mrp_time_align_next (t, priv->minor_unit);
        }
 
  done:
-       g_object_unref(gc);
+       cairo_destroy (cr);
 
        return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]