File: ctklabel.c

package info (click to toggle)
libctk 3.0.24.6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,316 kB
  • ctags: 1,201
  • sloc: ansic: 10,623; sh: 10,438; makefile: 102
file content (299 lines) | stat: -rw-r--r-- 6,620 bytes parent folder | download | duplicates (2)
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
153
154
155
156
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* 
 * $Id: ctklabel.c,v 1.22 2000/08/07 23:19:18 terpstra Exp $
 *
 * CTK - Console Toolkit
 *
 * Copyright (C) 1998-2000 Stormix Technologies Inc.
 *
 * License: LGPL
 *
 * Authors: Kevin Lindsay, Wesley Terpstra
 *  
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2 of the License, or (at your option) any later version.
 *    
 *    This 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
 *    Lesser General Public License for more details.
 *    
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>

#include "ctk.h"
#include "ctkcolor.h"
#include "ctkutil.h"

gboolean ctk_label_wrap_hack_report_vertible;

gpointer ctk_label_destroy(CtkObject *object);

/* Initialize the Label */
void ctk_label_init(CtkLabel *label)
{
	CtkWidget* widget;
	
	ctk_misc_init(&label->misc);
	CTK_OBJECT(label)->type = CtkTypeLabel;

	widget  = CTK_WIDGET(label);
	
	label->text = NULL;
	label->text_split = NULL;
	label->wrap = FALSE;
	label->first = TRUE;

	((CtkObject *)label)->destroy_func = ctk_label_destroy;

	widget->width       = 0;
	widget->orig_width  = 0;
	widget->height      = 0;
	widget->orig_height = 0;
	
	widget->main_col = ctk_calculate_palette(CTK_COLOR_WHITE,CTK_COLOR_BLUE);
	widget->inverse_col = ctk_calculate_palette(CTK_COLOR_RED,CTK_COLOR_BLUE);

	widget->set_min_size = &ctk_label_min_size;
}

/* Create a Label Widget */
CtkWidget* ctk_label_new(const gchar* text)
{
	CtkLabel* label;

	label = g_malloc(sizeof(CtkLabel));

	ctk_label_init(label);
	ctk_label_set_text(label, text);

	return CTK_WIDGET(label);
}

CtkWidget* ctk_label_newf(const gchar* format, ...)
{
	gchar*     tmp;
	va_list    ap;
	CtkWidget* out;
	
	va_start(ap, format);
	tmp = g_strdup_vprintf(format, ap);
	out = ctk_label_new(tmp);
	g_free(tmp);
	va_end(ap);
	
	return out;
}

/* Set the text in a label */
void ctk_label_set_text(CtkLabel* label, const gchar* text)
{
	CtkWidget* widget = CTK_WIDGET(label);

	if (!text)
	    return;

	if (label->text)
	    free(label->text);

	label->text = g_strdup(text);
	ctk_size_mark_changed(widget);
}

/* Set Line Wrap */
void ctk_label_set_line_wrap(CtkLabel* label, gboolean wrap)
{
	if (!label)
	    return;
	
	label->wrap = wrap;
	ctk_size_mark_changed(CTK_WIDGET(label));
}

/* Set Justify */
void ctk_label_set_justify(CtkLabel *label, CtkJustification jtype)
{
	switch (jtype)
	{
	case CTK_JUSTIFY_LEFT:
		ctk_misc_set_alignment(CTK_MISC(label), 
			0, CTK_MISC(label)->yalign);
		break;
	case CTK_JUSTIFY_RIGHT:
		ctk_misc_set_alignment(CTK_MISC(label), 
			1, CTK_MISC(label)->yalign);
		break;
	case CTK_JUSTIFY_CENTER:
		ctk_misc_set_alignment(CTK_MISC(label), 
			0.5, CTK_MISC(label)->yalign);
		break;
	case CTK_JUSTIFY_FILL:
		// FIXME
		break;
	}
}

/* Destroy label data */
gpointer ctk_label_destroy(CtkObject* object)
{
      CtkLabel* label = CTK_LABEL(object);
      
      if (label->text)
	    g_free(label->text);
      if (label->text_split)
	    g_strfreev(label->text_split);
      
      return NULL;
}

void ctk_label_min_size(CtkWidget* widget)
{
      CtkLabel* label = CTK_LABEL(widget);
      gint      i;
      
      if (!label->text)
      {
      	widget->min_width  = 1;
      	widget->min_height = 1;
      	return;
      }

      if (label->wrap)
      {
	    GSList* cuts;
	    GSList* node;
	    gchar*  scan;
	    gchar*  start;
	    gchar*  lastgood;
	    gint    delta;
	    gint    width;
	    gint    count;

	    if (ctk_label_wrap_hack_report_vertible)
	    {
		  /* Here we scan through and split stuff up based on width */

		  /* Fail if we were set to 0x0 (invisible) */
		  if (widget->width < widget->min_width)
			return;

		  if (label->text_split)
			g_strfreev(label->text_split);

		  cuts = NULL;
		  count = 0;
		  width = widget->width - CTK_MISC(widget)->xpad*2;
		  
		  for (start = label->text; *start; start = lastgood)
		  {
			lastgood = start;

			for (scan = start; *scan; scan++)
			{
			      delta = (int)scan - (int)start;

			      if (delta >= width) 
				    break;

			      if (isspace(*scan)) 
				    lastgood = scan;
			      if (*scan == '\n')
				    break;
			}

			if (!*scan || *scan == '\n')
			{
			      lastgood = scan;
			}
			else if (lastgood == start)
			{    /* Couldn't cut on nice place */
			      lastgood = start + width;
			}

			delta = (int)lastgood - (int)start;
			cuts = g_slist_prepend(cuts, g_strndup(start, delta));
			count++;

			while (*lastgood && isspace(*lastgood)) lastgood++;
		  }

		  label->text_split = (gchar**)malloc(sizeof(gchar*) * (count+1));
		  label->text_split[count] = NULL;
		  if (count == 0)
		  	count++;
		  widget->min_height = count;

		  for (node = cuts; node; node = node->next)
		  {
			label->text_split[--count] = node->data;
		  }

		  g_slist_free(cuts);
	    }
	    else
	    {
		  /* Here we just report <max word length>x1 */
		  widget->min_width = 0;

		  for (start = label->text; *start; start = scan)
		  {
			for (scan = start; *scan; scan++)
			{
			      if (isspace(*scan)) break;
			}
			
			if (*scan) scan++;
			delta = (int)scan - (int)start;
			if (delta > widget->min_width) 
			      widget->min_width = delta;
		  }
		  
		  /* Always eat one col */
		  if (widget->min_width == 0)
		  	widget->min_width++;

		  widget->min_width += CTK_MISC(widget)->xpad*2;

		  widget->min_height = 1 + CTK_MISC(widget)->ypad*2;
	    }
      }
      else
      {
	    if (label->text_split)
		  g_strfreev(label->text_split);

	    label->text_split = g_strsplit(label->text, "\n", 0);
	    
	    widget->min_width  = 0;
	    widget->min_height = 0;

	    for (i = 0; label->text_split[i]; i++)
	    {
		  gint len = strlen(label->text_split[i]);

		  if (len > widget->min_width)
			widget->min_width = len;
	    }
	    
	    /* Always eat up one row at least */
	    if (i == 0)
	    	i++;
	    
	    /* Always eat up one col at least */
	    if (widget->min_width == 0)
	    	widget->min_width++;

	    widget->min_height = i + CTK_MISC(widget)->ypad*2;
	    widget->min_width += CTK_MISC(widget)->xpad*2;
      }
}