ovccgtk-wrap-label.c 8.89 KB
Newer Older
1
/*
2
 * Copyright 2011-2013 Colomban Wendling <ban@herbesfolles.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

22 23
/* Initially written for GeanyGitUI, itself inspired from from GeanyWrapLabel.
 * GTK3 port is from GeanyWrapLabel */
24 25 26 27 28 29 30 31 32 33

#include "ovccgtk-wrap-label.h"

#include <glib.h>
#include <gtk/gtk.h>


struct _OVCCGtkWrapLabelPrivate
{
  gint wrap_width;
34
  gint wrap_height;
35 36 37
};


38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static gboolean           ovccgtk_wrap_label_draw                           (GtkWidget *widget,
                                                                             cairo_t   *cr);
static void               ovccgtk_wrap_label_get_preferred_width            (GtkWidget *widget,
                                                                             gint      *minimal_width,
                                                                             gint      *natural_width);
static void               ovccgtk_wrap_label_get_preferred_height           (GtkWidget *widget,
                                                                             gint      *minimal_height,
                                                                             gint      *natural_height);
static void               ovccgtk_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
                                                                             gint       height,
                                                                             gint      *minimal_width,
                                                                             gint      *natural_width);
static void               ovccgtk_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
                                                                             gint       width,
                                                                             gint      *minimal_height,
                                                                             gint      *natural_height);
static GtkSizeRequestMode ovccgtk_wrap_label_get_request_mode               (GtkWidget *widget);
static void               ovccgtk_wrap_label_size_allocate                  (GtkWidget     *widget,
                                                                             GtkAllocation *alloc);
static void               ovccgtk_wrap_label_notify_label_handler           (GObject     *object,
                                                                             GParamSpec  *pspec,
                                                                             gpointer     data);
60 61 62 63 64 65 66 67 68 69 70 71


G_DEFINE_TYPE (OVCCGtkWrapLabel,
               ovccgtk_wrap_label,
               GTK_TYPE_LABEL)


static void
ovccgtk_wrap_label_class_init (OVCCGtkWrapLabelClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  
72 73 74 75 76 77 78
  widget_class->draw                            = ovccgtk_wrap_label_draw;
  widget_class->get_preferred_width             = ovccgtk_wrap_label_get_preferred_width;
  widget_class->get_preferred_width_for_height  = ovccgtk_wrap_label_get_preferred_width_for_height;
  widget_class->get_preferred_height            = ovccgtk_wrap_label_get_preferred_height;
  widget_class->get_preferred_height_for_width  = ovccgtk_wrap_label_get_preferred_height_for_width;
  widget_class->get_request_mode                = ovccgtk_wrap_label_get_request_mode;
  widget_class->size_allocate                   = ovccgtk_wrap_label_size_allocate;
79 80 81 82 83 84 85 86 87 88 89
  
  g_type_class_add_private (klass, sizeof (OVCCGtkWrapLabelPrivate));
}

static void
ovccgtk_wrap_label_init (OVCCGtkWrapLabel *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, OVCCGTK_TYPE_WRAP_LABEL,
                                            OVCCGtkWrapLabelPrivate);
  
  self->priv->wrap_width = 0;
90
  self->priv->wrap_height = 0;
91 92 93 94 95 96 97 98
  
  gtk_misc_set_alignment (GTK_MISC (self), 0.0, 0.5);
  g_signal_connect (self, "notify::label",
                    G_CALLBACK (ovccgtk_wrap_label_notify_label_handler), NULL);
}

/* Sets the point at which the text should wrap. */
static void
99 100
ovccgtk_wrap_label_set_wrap_width (OVCCGtkWrapLabel  *self,
                                   gint               width)
101 102
{
  if (width > 0) {
103 104
    PangoLayout *layout = gtk_label_get_layout (GTK_LABEL (self));
    
105 106
    /* We may need to reset the wrap width, so do this regardless of whether
     * or not we've changed the width. */
107 108 109
    pango_layout_set_width (layout, width * PANGO_SCALE);
    pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
    pango_layout_get_pixel_size (layout, NULL, &self->priv->wrap_height);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    if (self->priv->wrap_width != width) {
      self->priv->wrap_width = width;
      gtk_widget_queue_resize (GTK_WIDGET (self));
    }
  }
}

/* updates the wrap width when the label text changes */
static void
ovccgtk_wrap_label_notify_label_handler (GObject     *object,
                                         GParamSpec  *pspec,
                                         gpointer     data)
{
  OVCCGtkWrapLabel *self = OVCCGTK_WRAP_LABEL (object);

  ovccgtk_wrap_label_set_wrap_width (self, self->priv->wrap_width);
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* makes sure the layout is setup for rendering and chains to parent renderer */
static gboolean
ovccgtk_wrap_label_draw (GtkWidget *widget,
                         cairo_t   *cr)
{
  OVCCGtkWrapLabel *self    = OVCCGTK_WRAP_LABEL (widget);
  PangoLayout      *layout  = gtk_label_get_layout (GTK_LABEL (widget));

  pango_layout_set_width (layout, self->priv->wrap_width * PANGO_SCALE);
  pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);

  return (* GTK_WIDGET_CLASS (ovccgtk_wrap_label_parent_class)->draw) (widget,
                                                                       cr);
}

static void
ovccgtk_wrap_label_get_preferred_width (GtkWidget  *widget,
                                        gint       *minimal_width,
                                        gint       *natural_width)
{
  *minimal_width = *natural_width = 0;
}

152
static void
153 154 155 156
ovccgtk_wrap_label_get_preferred_width_for_height (GtkWidget *widget,
                                                   gint       height,
                                                   gint      *minimal_width,
                                                   gint      *natural_width)
157
{
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  PangoLayout *layout = gtk_label_get_layout (GTK_LABEL(widget));;

  pango_layout_set_height (layout, height * PANGO_SCALE);
  pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
  pango_layout_get_pixel_size (layout, natural_width, NULL);

  *minimal_width = 0;
}

static void
ovccgtk_wrap_label_get_preferred_height (GtkWidget *widget,
                                         gint      *minimal_height,
                                         gint      *natural_height)
{
  *minimal_height = *natural_height = OVCCGTK_WRAP_LABEL (widget)->priv->wrap_height;
}

static void
ovccgtk_wrap_label_get_preferred_height_for_width (GtkWidget *widget,
                                                   gint       width,
                                                   gint      *minimal_height,
                                                   gint      *natural_height)
{
  PangoLayout *layout = gtk_label_get_layout (GTK_LABEL (widget));

  pango_layout_set_width (layout, width * PANGO_SCALE);
  pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
  pango_layout_get_pixel_size (layout, NULL, natural_height);

  *minimal_height = *natural_height;
}

static GtkSizeRequestMode
ovccgtk_wrap_label_get_request_mode (GtkWidget *widget)
{
  return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
194 195 196 197 198 199 200
}

/* Sets the wrap width to the width allocated to us. */
static void
ovccgtk_wrap_label_size_allocate (GtkWidget      *widget,
                                  GtkAllocation  *allocation)
{
201 202
  GtkWidget *parent = gtk_widget_get_parent (widget);
  
203 204 205 206 207
  GTK_WIDGET_CLASS (ovccgtk_wrap_label_parent_class)->size_allocate (widget,
                                                                     allocation);
  
  ovccgtk_wrap_label_set_wrap_width (OVCCGTK_WRAP_LABEL (widget),
                                     allocation->width);
208 209 210 211 212 213
  
  /* ask the parent to recompute our size, because it seems GTK3 size
   * caching is too aggressive */
  if (GTK_IS_CONTAINER (parent)) {
    gtk_container_check_resize (GTK_CONTAINER (parent));
  }
214 215 216 217 218 219 220 221
}


GtkWidget *
ovccgtk_wrap_label_new (const gchar *label)
{
  return g_object_new (OVCCGTK_TYPE_WRAP_LABEL, "label", label, NULL);
}