[gtksourceview/wip/centertext] Prototype to center the text view



commit 75430d88489975d8466ad7a4ed3927f54bbeb428
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Sep 1 15:30:06 2013 +0200

    Prototype to center the text view
    
    We use the right margin as an indication of the natural size of the
    text and add a gutter margin to the left to ensure the text is centered.
    (We do not add margin to the right since there the text should be
    allowed to exceed the right margin).
    In this prototype this feature is always on, if we do it this way we
    need to add a boolean property to control it.

 gtksourceview/gtksourcegutter.c |   85 ++++++++++++++++++++++++++++++++++++++-
 gtksourceview/gtksourcegutter.h |    8 ++++
 gtksourceview/gtksourceview.c   |   24 +++++++----
 3 files changed, 108 insertions(+), 9 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutter.c b/gtksourceview/gtksourcegutter.c
index 6585b83..bc4af21 100644
--- a/gtksourceview/gtksourcegutter.c
+++ b/gtksourceview/gtksourcegutter.c
@@ -57,7 +57,9 @@ enum
        PROP_VIEW,
        PROP_WINDOW_TYPE,
        PROP_XPAD,
-       PROP_YPAD
+       PROP_YPAD,
+       PROP_LEFT_MARGIN,
+       PROP_RIGHT_MARGIN
 };
 
 typedef struct
@@ -97,6 +99,8 @@ struct _GtkSourceGutterPrivate
 
        gint xpad;
        gint ypad;
+       gint left_margin;
+       gint right_margin;
 
        guint signals[LAST_EXTERNAL_SIGNAL];
 
@@ -420,6 +424,8 @@ calculate_gutter_size (GtkSourceGutter  *gutter,
                total_width += gutter->priv->xpad;
        }
 
+       total_width += gutter->priv->left_margin + gutter->priv->right_margin;
+
        return total_width;
 }
 
@@ -497,6 +503,14 @@ gtk_source_gutter_set_property (GObject       *object,
                case PROP_YPAD:
                        set_ypad (self, g_value_get_int (value), TRUE);
                        break;
+               case PROP_LEFT_MARGIN:
+                       self->priv->left_margin = g_value_get_int (value);
+                       update_gutter_size (self);
+                       break;
+               case PROP_RIGHT_MARGIN:
+                       self->priv->right_margin = g_value_get_int (value);
+                       update_gutter_size (self);
+                       break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -585,6 +599,26 @@ gtk_source_gutter_class_init (GtkSourceGutterClass *klass)
                                                           G_MAXINT,
                                                           0,
                                                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
+       g_object_class_install_property (object_class,
+                                        PROP_LEFT_MARGIN,
+                                        g_param_spec_int ("left-margin",
+                                                          _("Left Marging"),
+                                                          _("The left margin before the renderers"),
+                                                          -1,
+                                                          G_MAXINT,
+                                                          0,
+                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
+       g_object_class_install_property (object_class,
+                                        PROP_RIGHT_MARGIN,
+                                        g_param_spec_int ("right-margin",
+                                                          _("Right Marging"),
+                                                          _("The right margin before the renderers"),
+                                                          -1,
+                                                          G_MAXINT,
+                                                          0,
+                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
 }
 
 static void
@@ -997,6 +1031,8 @@ on_view_draw (GtkSourceView   *view,
        i = 0;
        x = 0;
 
+       cairo_translate (cr, gutter->priv->left_margin, 0);
+
        background_area.x = 0;
        background_area.height = get_lines (text_view,
                                            y1,
@@ -1613,6 +1649,53 @@ gtk_source_gutter_get_padding (GtkSourceGutter *gutter,
        }
 }
 
+void
+gtk_source_gutter_set_margin (GtkSourceGutter *gutter,
+                              gint             left,
+                              gint             right)
+{
+       gboolean update = FALSE;
+
+       g_return_if_fail (GTK_SOURCE_IS_GUTTER (gutter));
+
+       if (left != gutter->priv->left_margin)
+       {
+               gutter->priv->left_margin = left;
+               g_object_notify (G_OBJECT (gutter), "left-margin");
+               update = TRUE;
+       }
+
+       if (right != gutter->priv->right_margin)
+       {
+               gutter->priv->right_margin = right;
+               g_object_notify (G_OBJECT (gutter), "right-margin");
+               update = TRUE;
+       }
+
+       if (update)
+       {
+               update_gutter_size (gutter);
+       }
+}
+
+void
+gtk_source_gutter_get_margin (GtkSourceGutter *gutter,
+                              gint            *left,
+                              gint            *right)
+{
+       g_return_if_fail (GTK_SOURCE_IS_GUTTER (gutter));
+
+       if (left)
+       {
+               *left = gutter->priv->left_margin;
+       }
+
+       if (right)
+       {
+               *right = gutter->priv->right_margin;
+       }
+}
+
 /**
  * gtk_source_gutter_get_renderer_at_pos:
  * @gutter: A #GtkSourceGutter.
diff --git a/gtksourceview/gtksourcegutter.h b/gtksourceview/gtksourcegutter.h
index f0973cd..aa081a2 100644
--- a/gtksourceview/gtksourcegutter.h
+++ b/gtksourceview/gtksourcegutter.h
@@ -75,6 +75,14 @@ void gtk_source_gutter_get_padding              (GtkSourceGutter         *gutter
                                                  gint                    *xpad,
                                                  gint                    *ypad);
 
+void gtk_source_gutter_set_margin               (GtkSourceGutter         *gutter,
+                                                 gint                     left,
+                                                 gint                     right);
+
+void gtk_source_gutter_get_margin               (GtkSourceGutter         *gutter,
+                                                 gint                    *left,
+                                                 gint                    *right);
+
 GtkSourceGutterRenderer *
      gtk_source_gutter_get_renderer_at_pos      (GtkSourceGutter         *gutter,
                                                  gint                     x,
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 381a381..583ed4d 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -2225,14 +2225,6 @@ gtk_source_view_paint_right_margin (GtkSourceView *view,
 
        g_return_if_fail (view->priv->right_margin_line_color != NULL);
 
-       if (view->priv->cached_right_margin_pos < 0)
-       {
-               view->priv->cached_right_margin_pos =
-                       calculate_real_tab_width (view,
-                                                 view->priv->right_margin_pos,
-                                                 '_');
-       }
-
 #ifdef ENABLE_PROFILE
        if (timer == NULL)
                timer = g_timer_new ();
@@ -2305,6 +2297,22 @@ gtk_source_view_draw (GtkWidget *widget,
        view = GTK_SOURCE_VIEW (widget);
        text_view = GTK_TEXT_VIEW (widget);
 
+       if (view->priv->cached_right_margin_pos < 0)
+       {
+               view->priv->cached_right_margin_pos =
+                       calculate_real_tab_width (view,
+                                                 view->priv->right_margin_pos,
+                                                 '_');
+       }
+
+       gint alloc_w = gtk_widget_get_allocated_width (GTK_WIDGET (view));
+       gint left_margin = (alloc_w - view->priv->cached_right_margin_pos) / 2;
+       if (left_margin > 0)
+       {
+               /* FIXME: stop this draw and queua a new one after resize is done? */
+               gtk_source_gutter_set_margin (view->priv->left_gutter, left_margin, 0);
+       }
+
        window = gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_TEXT);
 
        cairo_save (cr);


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