cell-draw.c 8.1 KB
Newer Older
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
/*
 * cell-draw.c: Cell drawing on screen
 *
 * Author:
 *    Miguel de Icaza 1998, 1999 (miguel@kernel.org)
 */
#include <config.h>
#include <gnome.h>
#include <locale.h>
#include "gnumeric.h"
#include "gnumeric-sheet.h"
#include "eval.h"
#include "format.h"
#include "color.h"
#include "utils.h"

static void
draw_overflow (GdkDrawable *drawable, GdkGC *gc, GdkFont *font, int x1, int y1, int text_base, int width, int height)
{
	GdkRectangle rect;
	int len = gdk_string_width (font, "#");
	int total, offset;
	
	rect.x = x1;
	rect.y = y1;
	rect.width = width;
	rect.height = height;
	gdk_gc_set_clip_rectangle (gc, &rect);
	
	offset = x1 + width - len;
	for (total = len;  offset > len; total += len){
		gdk_draw_text (drawable, font, gc, x1 + offset, text_base, "#", 1);
		offset -= len;
	} 
}

37
38
39
40
41
/*
 * WARNING : This code is an almost exact duplicate of
 *          print-cell.c:cell_split_text
 * Try to keep it that way.
 */
42
static GList *
43
cell_split_text (GdkFont *font, char const *text, int const width)
44
{
45
46
47
48
49
50
	char const *p, *line_begin;
	char const *first_whitespace = NULL;
	char const *last_whitespace = NULL;
	gboolean prev_was_space = FALSE;
	GList *list = NULL;
	int used = 0, used_last_space = 0;
51
52

	for (line_begin = p = text; *p; p++){
53
		int const len_current = gdk_text_width (font, p, 1);
54

55
56
		/* Wrap if there is an embeded newline, or we have overflowed */
		if (*p == '\n' || used + len_current > width){
57
			char const *begin = line_begin;
58
59
60
61
62
63
64
65
66
67
68
69
			int len;

			if (*p == '\n'){
				/* start after newline, preserve whitespace */
				line_begin = p+1;
				len = p - begin;
				used = 0;
			} else if (last_whitespace != NULL){
				/* Split at the run of whitespace */
				line_begin = last_whitespace + 1;
				len = first_whitespace - begin;
				used = len_current + used - used_last_space;
70
			} else {
71
72
73
74
				/* Split before the current character */
				line_begin = p; /* next line starts here */
				len = p - begin;
				used = len_current;
75
76
			}

77
78
79
80
81
			list = g_list_append (list, g_strndup (begin, len));
			first_whitespace = last_whitespace = NULL;
			prev_was_space = FALSE;
			continue;
		}
82

83
84
85
86
87
88
89
90
91
		used += len_current;
		if (*p == ' '){
			used_last_space = used;
			last_whitespace = p;
			if (!prev_was_space)
				first_whitespace = p;
			prev_was_space = TRUE;
		} else
			prev_was_space = FALSE;
92
93
	}

94
95
96
97
98
	/* Catch the final bit that did not wrap */
	if (*line_begin)
		list = g_list_append (list,
				      g_strndup (line_begin, p - line_begin));

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
	return list;
}

static GdkFont *
sheet_view_font (SheetView *sheet_view, Cell *cell)
{
	return style_font_gdk_font (cell->style->font);
}

/*
 * Returns the number of columns used for the draw
 */
int 
cell_draw (Cell *cell, SheetView *sheet_view, GdkGC *gc, GdkDrawable *drawable, int x1, int y1)
{
	Style        *style = cell->style;
	GdkFont      *font;
	GdkRectangle rect;
	
	int start_col, end_col;
	int width, height;
	int text_base;
	int font_height;
	int halign;
	int do_multi_line;
	char *text;

	font = sheet_view_font (sheet_view, cell);
	text_base = y1 + cell->row->pixels - cell->row->margin_b - font->descent;
	
	cell_get_span (cell, &start_col, &end_col);

	width  = COL_INTERNAL_WIDTH (cell->col);
	height = ROW_INTERNAL_HEIGHT (cell->row);

	font_height = style_font_get_height (cell->style->font);
	
	halign = cell_get_horizontal_align (cell);

	/* if a number overflows, do special drawing */
	if (width < cell->width && cell_is_number (cell)){
		draw_overflow (drawable, gc, font, x1 + cell->col->margin_a, y1, text_base,
			       width, height);
		return 1;
	}

	if (halign == HALIGN_JUSTIFY || style->valign == VALIGN_JUSTIFY || style->fit_in_cell)
		do_multi_line = TRUE;
	else
		do_multi_line = FALSE;

150
151
152
153
154
155
156
	if (cell && cell->text && cell->text->str)
		text = cell->text->str;
	else {
		printf ("Serious cell error at '%s'\n", cell_name (cell->col->pos,
								   cell->row->pos));
		text = "FATAL ERROR";
	} 
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	
	/*
	 * To draw the background of the cell, we need to
	 * change the gc's drawing color before calling the
	 * gdk_draw_rectangle function
	 */
	gdk_gc_set_foreground (gc, &(style->back_color->color));

	if (do_multi_line){
		GList *lines, *l;
		int cell_pixel_height = ROW_INTERNAL_HEIGHT (cell->row);
		int line_count, x_offset, y_offset, inter_space;
		
		lines = cell_split_text (font, text, width);
		line_count = g_list_length (lines);

173
174
175
176
		rect.x = x1 + 1;
		rect.y = y1 + 1;
		rect.height = cell->row->pixels - 1;
		rect.width = cell->col->pixels  - 1;
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
		gdk_gc_set_clip_rectangle (gc, &rect);
		
		gdk_draw_rectangle (drawable, gc, TRUE,
                                    rect.x, rect.y, rect.width, rect.height);

		if (cell->render_color)
			gdk_gc_set_foreground (gc, &cell->render_color->color);
		else
			gdk_gc_set_foreground (gc, &(style->fore_color->color));

		switch (style->valign){
		case VALIGN_TOP:
			y_offset = 0;
			inter_space = font_height;
			break;
			
		case VALIGN_CENTER:
			y_offset = (cell_pixel_height - (line_count * font_height))/2;
			inter_space = font_height;
			break;
			
		case VALIGN_JUSTIFY:
			if (line_count > 1){
				y_offset = 0;
				inter_space = font_height + 
					(cell_pixel_height - (line_count * font_height))
					/ (line_count-1);
				break;
			} 
			/* Else, we become a VALIGN_BOTTOM line */
			
		case VALIGN_BOTTOM:
			y_offset = cell_pixel_height - (line_count * font_height);
			inter_space = font_height;
			break;
			
		default:
			g_warning ("Unhandled cell vertical alignment\n");
			y_offset = 0;
			inter_space = font_height;
		}

		y_offset += font_height - 1;
		for (l = lines; l; l = l->next){
221
			char const * const str = l->data;
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

			switch (halign){
			case HALIGN_LEFT:
			case HALIGN_JUSTIFY:
				x_offset = cell->col->margin_a;
				break;
				
			case HALIGN_RIGHT:
				x_offset = cell->col->pixels - cell->col->margin_b -
					gdk_string_width (font, str);
				break;

			case HALIGN_CENTER:
				x_offset = (cell->col->pixels - gdk_string_width (font, str)) / 2;
				break;
			default:
				g_warning ("Multi-line justification style not supported\n");
				x_offset = cell->col->margin_a;
			}
			/* Advance one pixel for the border */
			x_offset++;
			gc = GTK_WIDGET (sheet_view->sheet_view)->style->black_gc;
			gdk_draw_text (drawable, font, gc, x1 + x_offset, y1 + y_offset, str, strlen (str));
			y_offset += inter_space;
			
			g_free (l->data);
		}
		g_list_free (lines);
		
	} else {
		int x, diff, total, len;

		/*
		 * x1, y1 are relative to this cell origin, but the cell might be using
		 * columns to the left (if it is set to right justify or center justify)
		 * compute the pixel difference 
		 */
		if (start_col != cell->col->pos)
			diff = -sheet_col_get_distance (cell->sheet, start_col, cell->col->pos);
		else
			diff = 0;

		/* This rectangle has the whole area used by this cell */
		rect.x = x1 + 1 + diff;
		rect.y = y1 + 1;
		rect.width  = sheet_col_get_distance (cell->sheet, start_col, end_col+1) - 1;
		rect.height = cell->row->pixels - 1;
		
		/* Set the clip rectangle */
		gdk_gc_set_clip_rectangle (gc, &rect);

		/*
		 * To draw the background of the cell, we need to change the
		 * gc's drawing color before calling the gdk_draw_rectangle function
		 */
		gdk_gc_set_foreground (gc, &(style->back_color->color));
		gdk_draw_rectangle (drawable, gc, TRUE,
                                    rect.x, rect.y, rect.width, rect.height);
		
		/*
		 * And now reset the previous foreground color
		 */
		if (cell->render_color)
			gdk_gc_set_foreground (gc, &cell->render_color->color);
		else
			gdk_gc_set_foreground (gc, &(style->fore_color->color));

		len = 0;
		switch (halign){
		case HALIGN_FILL:
			printf ("FILL!\n");
			len = gdk_string_width (font, text);
			/* fall down */
			
		case HALIGN_LEFT:
			x = cell->col->margin_a;
			break;

		case HALIGN_RIGHT:
			x = cell->col->pixels - cell->col->margin_b - cell->width;
			break;

		case HALIGN_CENTER:
			x = (cell->col->pixels - cell->width)/2;
			break;
			
		default:
			g_warning ("Single-line justification style not supported\n");
			x = cell->col->margin_a;
			break;
		}

		total = 0;
		do {
			gdk_draw_text (drawable, font, gc, 1 + x1 + x, text_base, text, strlen (text));
			x1 += len;
			total += len;
319
		} while (halign == HALIGN_FILL && total < rect.width && len >0);
320
321
322
323
324
	}

	return end_col - start_col + 1;
}