summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-ellipsizing-label.c
blob: 1903f37744c01c8de8acc563cf3cff17b454b5a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-ellipsizing-label.c: Subclass of GtkLabel that ellipsizes the text.

   Copyright (C) 2001 Eazel, Inc.

   The Gnome Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: John Sullivan <sullivan@eazel.com>,
 */

#include <config.h>
#include "nautilus-ellipsizing-label.h"

#include "nautilus-gdk-font-extensions.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-string.h"

struct NautilusEllipsizingLabelDetails
{
	char *full_text;
};

static void nautilus_ellipsizing_label_initialize_class  (NautilusEllipsizingLabelClass *class);
static void nautilus_ellipsizing_label_initialize        (NautilusEllipsizingLabel      *label);
static void real_destroy				 (GtkObject			*object);
static void real_size_request      			 (GtkWidget                     *widget,
						    	  GtkRequisition                *requisition);
static void real_size_allocate 	 			 (GtkWidget 			*widget, 
							  GtkAllocation 		*allocation);
static void real_style_set				 (GtkWidget			*widget,
							  GtkStyle			*previous_style);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusEllipsizingLabel, nautilus_ellipsizing_label, GTK_TYPE_LABEL)

static void
nautilus_ellipsizing_label_initialize_class (NautilusEllipsizingLabelClass *klass)
{
	GtkWidgetClass *widget_class;
	GtkObjectClass *object_class;

	object_class = GTK_OBJECT_CLASS (klass);
	widget_class = GTK_WIDGET_CLASS (klass);

	object_class->destroy = real_destroy;

	widget_class->size_request = real_size_request;
	widget_class->size_allocate = real_size_allocate;
  	widget_class->style_set = real_style_set;
}

static void
nautilus_ellipsizing_label_initialize (NautilusEllipsizingLabel *label)
{
	label->details = g_new0 (NautilusEllipsizingLabelDetails, 1);
}

static void
real_destroy (GtkObject *object)
{
	NautilusEllipsizingLabel *label;

	g_return_if_fail (NAUTILUS_IS_ELLIPSIZING_LABEL (object));

	label = NAUTILUS_ELLIPSIZING_LABEL (object);

	g_free (label->details->full_text);
	g_free (label->details);
}

GtkWidget*
nautilus_ellipsizing_label_new (const char *string)
{
	NautilusEllipsizingLabel *label;
  
	label = gtk_type_new (NAUTILUS_TYPE_ELLIPSIZING_LABEL);
	nautilus_ellipsizing_label_set_text (label, string);
  
	return GTK_WIDGET (label);
}

static void
recompute_ellipsized_text (NautilusEllipsizingLabel *label, int width)
{
	char *ellipsized_text;

	if (label->details->full_text == NULL) {
		ellipsized_text = NULL;
	} else {
		ellipsized_text = nautilus_string_ellipsize_start (label->details->full_text, 
								   GTK_WIDGET (label)->style->font, 
								   width);
	}

	gtk_label_set_text (GTK_LABEL (label), ellipsized_text);
	g_free (ellipsized_text);
}

void
nautilus_ellipsizing_label_set_text (NautilusEllipsizingLabel *label, 
				     const char *string)
{
	g_return_if_fail (NAUTILUS_IS_ELLIPSIZING_LABEL (label));

	if (nautilus_str_is_equal (string, label->details->full_text)) {
		return;
	}

	g_free (label->details->full_text);
	label->details->full_text = g_strdup (string);

	recompute_ellipsized_text (label, GTK_WIDGET (label)->allocation.width);
}

static void
real_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
	NAUTILUS_CALL_PARENT (GTK_WIDGET_CLASS, size_request, (widget, requisition));

	/* Don't demand any particular width; will draw ellipsized into whatever size we're given */
	requisition->width = 0;
}

static void	
real_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
	recompute_ellipsized_text (NAUTILUS_ELLIPSIZING_LABEL (widget), allocation->width);
	
	NAUTILUS_CALL_PARENT (GTK_WIDGET_CLASS, size_allocate, (widget, allocation));
}

static void 
real_style_set (GtkWidget *widget, GtkStyle  *previous_style)
{
	recompute_ellipsized_text (NAUTILUS_ELLIPSIZING_LABEL (widget), widget->allocation.width);
	
	NAUTILUS_CALL_PARENT (GTK_WIDGET_CLASS, style_set, (widget, previous_style));
}