dzl-bolding-label.c 5.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* dzl-bolding-label.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

19 20
#include "config.h"

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <glib/gi18n.h>

#include "dzl-bolding-label.h"

/**
 * SECTION:dzl-bolding-label
 *
 * This is a GtkLabel widget that will allocate extra space if necessary
 * so that the size request will not change when the contents of the
 * label are bolded.
 *
 * This might be useful when you want to change a label based on some
 * selection state without it affecting the size request or layout.
 */

struct _DzlBoldingLabel
{
  GtkLabel parent_instance;
};

G_DEFINE_TYPE (DzlBoldingLabel, dzl_bolding_label, GTK_TYPE_LABEL)

43 44 45 46 47 48 49 50
enum {
  PROP_0,
  PROP_BOLD,
  N_PROPS
};

static GParamSpec *properties [N_PROPS];

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static void
dzl_bolding_label_get_preferred_width (GtkWidget *widget,
                                       gint      *min_width,
                                       gint      *nat_width)
{
  const PangoFontDescription *font_desc;
  PangoContext *context;

  g_assert (DZL_IS_BOLDING_LABEL (widget));
  g_assert (min_width);
  g_assert (nat_width);

  GTK_WIDGET_CLASS (dzl_bolding_label_parent_class)->get_preferred_width (widget, min_width, nat_width);

  if (NULL == (context = gtk_widget_get_pango_context (widget)) ||
      NULL == (font_desc = pango_context_get_font_description (context)))
    return;

  if (pango_font_description_get_weight (font_desc) != PANGO_WEIGHT_BOLD)
    {
      PangoFontDescription *font_desc_copy;
      PangoLayout *layout;
      const gchar *text;
74
      PangoEllipsizeMode ellipsize;
75 76 77 78 79
      gint height;
      gint width;

      text = gtk_label_get_text (GTK_LABEL (widget));
      layout = gtk_widget_create_pango_layout (widget, text);
80
      font_desc_copy = pango_font_description_copy (font_desc);
81 82
      ellipsize = gtk_label_get_ellipsize (GTK_LABEL (widget));

83 84
      pango_font_description_set_weight (font_desc_copy, PANGO_WEIGHT_BOLD);
      pango_layout_set_font_description (layout, font_desc_copy);
85
      pango_layout_set_ellipsize (layout, ellipsize);
86 87
      pango_layout_get_pixel_size (layout, &width, &height);

88 89 90 91 92 93 94 95 96
      if (ellipsize == PANGO_ELLIPSIZE_NONE)
        {
          /*
           * Only apply min_width if we cannot ellipsize, if we can, that
           * effects things differently.
           */
          if (width > *min_width)
            *min_width = width;
        }
97 98 99 100 101 102 103 104 105

      if (width > *nat_width)
        *nat_width = width;

      pango_font_description_free (font_desc_copy);
      g_object_unref (layout);
    }
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void
dzl_bolding_label_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  DzlBoldingLabel *self = DZL_BOLDING_LABEL (object);

  switch (prop_id)
    {
    case PROP_BOLD:
      dzl_bolding_label_set_bold (self, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

125 126 127 128
static void
dzl_bolding_label_class_init (DzlBoldingLabelClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
129 130 131
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = dzl_bolding_label_set_property;
132 133

  widget_class->get_preferred_width = dzl_bolding_label_get_preferred_width;
134 135 136 137 138 139 140 141 142

  properties [PROP_BOLD] =
    g_param_spec_boolean ("bold",
                          "Bold",
                          "Set the bold weight for the label",
                          FALSE,
                          (G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, N_PROPS, properties);
143 144 145 146 147 148
}

static void
dzl_bolding_label_init (DzlBoldingLabel *self)
{
}
149 150 151 152 153 154 155 156 157 158 159 160

static gboolean
remove_weights (PangoAttribute *attr,
                gpointer        user_data)
{
  return attr->klass->type == ((PangoAttribute *)user_data)->klass->type;
}

void
dzl_bolding_label_set_weight (DzlBoldingLabel *self,
                              PangoWeight      weight)
{
161
  PangoAttrList *filtered;
162 163 164 165 166 167 168 169 170 171 172 173
  PangoAttrList *attrs;
  PangoAttrList *copy;
  PangoAttribute *attr;

  g_return_if_fail (DZL_IS_BOLDING_LABEL (self));

  attrs = gtk_label_get_attributes (GTK_LABEL (self));
  if (attrs)
    copy = pango_attr_list_copy (attrs);
  else
    copy = pango_attr_list_new ();
  attr = pango_attr_weight_new (weight);
174
  filtered = pango_attr_list_filter (copy, remove_weights, attr);
175 176 177
  pango_attr_list_insert (copy, attr);
  gtk_label_set_attributes (GTK_LABEL (self), copy);
  gtk_widget_queue_draw (GTK_WIDGET (self));
178
  pango_attr_list_unref (filtered);
179 180
  pango_attr_list_unref (copy);
}
181 182 183 184 185 186 187 188 189

void
dzl_bolding_label_set_bold (DzlBoldingLabel *self,
                            gboolean         bold)
{
  g_return_if_fail (DZL_IS_BOLDING_LABEL (self));

  dzl_bolding_label_set_weight (self, bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
}