]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-kludge-label.c
Merge commit 'jtellier/video-call-button-sensitivity'
[empathy.git] / libempathy-gtk / empathy-kludge-label.c
1 /* vim: set ts=4 sts=4 sw=4 et: */
2 /*
3  * Copyright (C) 2009 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Davyd Madeley <davyd.madeley@collabora.co.uk>
20  */
21
22 #include "empathy-kludge-label.h"
23
24 G_DEFINE_TYPE (EmpathyKludgeLabel, empathy_kludge_label, GTK_TYPE_LABEL);
25
26 static void
27 empathy_kludge_label_size_allocate (GtkWidget     *self,
28                                     GtkAllocation *allocation)
29 {
30     PangoLayout *layout;
31
32     GTK_WIDGET_CLASS (empathy_kludge_label_parent_class)->size_allocate (
33             self, allocation);
34
35     /* force the width of the PangoLayout to be the width of the allocation */
36     layout = gtk_label_get_layout (GTK_LABEL (self));
37     pango_layout_set_width (layout, allocation->width * PANGO_SCALE);
38 }
39
40 static gboolean
41 empathy_kludge_label_expose_event (GtkWidget      *self,
42                                    GdkEventExpose *event)
43 {
44     PangoLayout *layout;
45     PangoRectangle rect;
46     GtkAllocation real_allocation;
47     gboolean r;
48
49     layout = gtk_label_get_layout (GTK_LABEL (self));
50     pango_layout_get_pixel_extents (layout, NULL, &rect);
51
52     /* this is mind-bendingly evil:
53      * get_layout_location() is going to remove rect.x from the position of the
54      * layout when painting it. This really sucks. We're going to compensate by
55      * adding this value to the allocation.
56      */
57     real_allocation = self->allocation;
58     self->allocation.x += rect.x;
59
60     r = GTK_WIDGET_CLASS (empathy_kludge_label_parent_class)->expose_event (
61             self, event);
62
63     self->allocation = real_allocation;
64
65     return r;
66 }
67
68 static void
69 empathy_kludge_label_class_init (EmpathyKludgeLabelClass *klass)
70 {
71     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
72
73     widget_class->size_allocate = empathy_kludge_label_size_allocate;
74     widget_class->expose_event = empathy_kludge_label_expose_event;
75 }
76
77 static void
78 empathy_kludge_label_init (EmpathyKludgeLabel *self)
79 {
80 }
81
82 GtkWidget *
83 empathy_kludge_label_new (const char *str)
84 {
85     return g_object_new (EMPATHY_TYPE_KLUDGE_LABEL,
86             "label", str,
87             "xalign", 0.,
88             NULL);
89 }