comparison audacious/textbox.c @ 0:cb178e5ad177 trunk

[svn] Import audacious source.
author nenolod
date Mon, 24 Oct 2005 03:06:47 -0700
parents
children 428d3865de3c
comparison
equal deleted inserted replaced
-1:000000000000 0:cb178e5ad177
1 /* BMP - Cross-platform multimedia player
2 * Copyright (C) 2003-2004 BMP development team.
3 *
4 * Based on XMMS:
5 * Copyright (C) 1998-2003 XMMS development team.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "textbox.h"
23
24 #include <glib.h>
25 #include <gtk/gtk.h>
26 #include <gdk/gdk.h>
27 #include <gdk/gdkprivate.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "main.h"
32 #include "util.h"
33
34 static void textbox_generate_pixmap(TextBox * tb);
35
36 static void
37 textbox_draw(Widget * w)
38 {
39 TextBox *tb = TEXT_BOX(w);
40 gint cw;
41 GdkPixmap *obj;
42 GdkPixmap *src;
43
44 g_return_if_fail(tb != NULL);
45
46 if (tb->tb_text &&
47 (!tb->tb_pixmap_text || strcmp(tb->tb_text, tb->tb_pixmap_text)))
48 textbox_generate_pixmap(tb);
49
50 if (tb->tb_pixmap) {
51 if (skin_get_id() != tb->tb_skin_id) {
52 tb->tb_skin_id = skin_get_id();
53 textbox_generate_pixmap(tb);
54 }
55 obj = tb->tb_widget.parent;
56 src = tb->tb_pixmap;
57
58 cw = tb->tb_pixmap_width - tb->tb_offset;
59 if (cw > tb->tb_widget.width)
60 cw = tb->tb_widget.width;
61 gdk_draw_pixmap(obj, tb->tb_widget.gc, src, tb->tb_offset, 0,
62 tb->tb_widget.x, tb->tb_widget.y, cw,
63 tb->tb_widget.height);
64 if (cw < tb->tb_widget.width)
65 gdk_draw_pixmap(obj, tb->tb_widget.gc, src, 0, 0,
66 tb->tb_widget.x + cw, tb->tb_widget.y,
67 tb->tb_widget.width - cw, tb->tb_widget.height);
68 }
69 }
70
71 static gboolean
72 textbox_scroll(gpointer data)
73 {
74 TextBox *tb = TEXT_BOX(data);
75
76 if (!tb->tb_is_dragging) {
77 tb->tb_offset++;
78 if (tb->tb_offset >= tb->tb_pixmap_width)
79 tb->tb_offset -= tb->tb_pixmap_width;
80 widget_draw(WIDGET(tb));
81 }
82
83 return TRUE;
84 }
85
86 static void
87 textbox_button_press(GtkWidget * w, GdkEventButton * event, gpointer data)
88 {
89 TextBox *tb = TEXT_BOX(data);
90
91 if (event->button != 1)
92 return;
93 if (widget_contains(&tb->tb_widget, event->x, event->y) &&
94 tb->tb_scroll_allowed &&
95 tb->tb_pixmap_width > tb->tb_widget.width && tb->tb_is_scrollable) {
96 tb->tb_is_dragging = TRUE;
97 tb->tb_drag_off = tb->tb_offset;
98 tb->tb_drag_x = event->x;
99 }
100 }
101
102 static void
103 textbox_motion(GtkWidget * w, GdkEventMotion * event, gpointer data)
104 {
105 TextBox *tb = TEXT_BOX(data);
106
107 if (tb->tb_is_dragging) {
108 if (tb->tb_scroll_allowed &&
109 tb->tb_pixmap_width > tb->tb_widget.width) {
110 tb->tb_offset = tb->tb_drag_off - (event->x - tb->tb_drag_x);
111
112 while (tb->tb_offset < 0)
113 tb->tb_offset += tb->tb_pixmap_width;
114
115 while (tb->tb_offset > tb->tb_pixmap_width)
116 tb->tb_offset -= tb->tb_pixmap_width;
117
118 widget_draw(WIDGET(tb));
119 }
120 }
121 }
122
123 static void
124 textbox_button_release(GtkWidget * w, GdkEventButton * event, gpointer data)
125 {
126 TextBox *tb = TEXT_BOX(data);
127
128 if (event->button == 1)
129 tb->tb_is_dragging = FALSE;
130 }
131
132 static gboolean
133 textbox_should_scroll(TextBox * tb)
134 {
135 g_return_val_if_fail(tb != NULL, FALSE);
136
137 if (!tb->tb_scroll_allowed)
138 return FALSE;
139
140 if (tb->tb_font) {
141 gint width;
142
143 text_get_extents(tb->tb_fontname, tb->tb_text, &width, NULL, NULL,
144 NULL);
145
146 if (width <= tb->tb_widget.width)
147 return FALSE;
148 else
149 return TRUE;
150 }
151
152 if (g_utf8_strlen(tb->tb_text, -1) * 5 > tb->tb_widget.width)
153 return TRUE;
154
155 return FALSE;
156 }
157
158 void
159 textbox_set_text(TextBox * tb, const gchar * text)
160 {
161 g_return_if_fail(tb != NULL);
162 g_return_if_fail(text != NULL);
163
164 widget_lock(WIDGET(tb));
165
166 if (tb->tb_text) {
167 if (!strcmp(text, tb->tb_text)) {
168 widget_unlock(WIDGET(tb));
169 return;
170 }
171 g_free(tb->tb_text);
172 }
173
174 tb->tb_text = str_to_utf8(text);
175
176 widget_unlock(WIDGET(tb));
177 widget_draw(WIDGET(tb));
178 }
179
180 static void
181 textbox_generate_xfont_pixmap(TextBox * tb, const gchar * pixmaptext)
182 {
183 gint length, i;
184 GdkGC *gc, *maskgc;
185 GdkColor *c, pattern;
186 GdkBitmap *mask;
187 PangoLayout *layout;
188 gint width;
189
190 g_return_if_fail(tb != NULL);
191 g_return_if_fail(pixmaptext != NULL);
192
193 length = g_utf8_strlen(pixmaptext, -1);
194
195 text_get_extents(tb->tb_fontname, pixmaptext, &width, NULL, NULL, NULL);
196
197 tb->tb_pixmap_width = MAX(width, tb->tb_widget.width);
198 tb->tb_pixmap = gdk_pixmap_new(mainwin->window, tb->tb_pixmap_width,
199 tb->tb_widget.height,
200 gdk_rgb_get_visual()->depth);
201 gc = tb->tb_widget.gc;
202 c = skin_get_color(bmp_active_skin, SKIN_TEXTBG);
203 for (i = 0; i < tb->tb_widget.height; i++) {
204 gdk_gc_set_foreground(gc, &c[6 * i / tb->tb_widget.height]);
205 gdk_draw_line(tb->tb_pixmap, gc, 0, i, tb->tb_pixmap_width, i);
206 }
207
208 mask = gdk_pixmap_new(mainwin->window, tb->tb_pixmap_width,
209 tb->tb_widget.height, 1);
210 maskgc = gdk_gc_new(mask);
211 pattern.pixel = 0;
212 gdk_gc_set_foreground(maskgc, &pattern);
213
214 gdk_draw_rectangle(mask, maskgc, TRUE, 0, 0,
215 tb->tb_pixmap_width, tb->tb_widget.height);
216 pattern.pixel = 1;
217 gdk_gc_set_foreground(maskgc, &pattern);
218
219 gdk_gc_set_foreground(gc, skin_get_color(bmp_active_skin, SKIN_TEXTFG));
220
221 layout = gtk_widget_create_pango_layout(mainwin, pixmaptext);
222 pango_layout_set_font_description(layout, tb->tb_font);
223
224 gdk_draw_layout(tb->tb_pixmap, gc, 0, (tb->tb_font_descent / 2), layout);
225 g_object_unref(layout);
226
227 g_object_unref(maskgc);
228
229 gdk_gc_set_clip_mask(gc, mask);
230 c = skin_get_color(bmp_active_skin, SKIN_TEXTFG);
231 for (i = 0; i < tb->tb_widget.height; i++) {
232 gdk_gc_set_foreground(gc, &c[6 * i / tb->tb_widget.height]);
233 gdk_draw_line(tb->tb_pixmap, gc, 0, i, tb->tb_pixmap_width, i);
234 }
235 g_object_unref(mask);
236 gdk_gc_set_clip_mask(gc, NULL);
237 }
238
239 static void
240 textbox_handle_special_char(gchar c, gint * x, gint * y)
241 {
242 switch (c) {
243 case '"':
244 *x = 130;
245 *y = 0;
246 break;
247 case ':':
248 *x = 60;
249 *y = 6;
250 break;
251 case '(':
252 *x = 65;
253 *y = 6;
254 break;
255 case ')':
256 *x = 70;
257 *y = 6;
258 break;
259 case '-':
260 *x = 75;
261 *y = 6;
262 break;
263 case '`':
264 case '\'':
265 *x = 80;
266 *y = 6;
267 break;
268 case '!':
269 *x = 85;
270 *y = 6;
271 break;
272 case '_':
273 *x = 90;
274 *y = 6;
275 break;
276 case '+':
277 *x = 95;
278 *y = 6;
279 break;
280 case '\\':
281 *x = 100;
282 *y = 6;
283 break;
284 case '/':
285 *x = 105;
286 *y = 6;
287 break;
288 case '[':
289 *x = 110;
290 *y = 6;
291 break;
292 case ']':
293 *x = 115;
294 *y = 6;
295 break;
296 case '^':
297 *x = 120;
298 *y = 6;
299 break;
300 case '&':
301 *x = 125;
302 *y = 6;
303 break;
304 case '%':
305 *x = 130;
306 *y = 6;
307 break;
308 case '.':
309 case ',':
310 *x = 135;
311 *y = 6;
312 break;
313 case '=':
314 *x = 140;
315 *y = 6;
316 break;
317 case '$':
318 *x = 145;
319 *y = 6;
320 break;
321 case '#':
322 *x = 150;
323 *y = 6;
324 break;
325 case 'å':
326 case 'Å':
327 *x = 0;
328 *y = 12;
329 break;
330 case 'ö':
331 case 'Ö':
332 *x = 5;
333 *y = 12;
334 break;
335 case 'ä':
336 case 'Ä':
337 *x = 10;
338 *y = 12;
339 break;
340 case 'ü':
341 case 'Ü':
342 *x = 100;
343 *y = 0;
344 break;
345 case '?':
346 *x = 15;
347 *y = 12;
348 break;
349 case '*':
350 *x = 20;
351 *y = 12;
352 break;
353 default:
354 *x = 145;
355 *y = 0;
356 break;
357 }
358 }
359
360 static void
361 textbox_generate_pixmap(TextBox * tb)
362 {
363 gint length, i, x, y, wl;
364 gchar *pixmaptext;
365 GdkGC *gc;
366
367 g_return_if_fail(tb != NULL);
368
369 if (tb->tb_pixmap) {
370 g_object_unref(tb->tb_pixmap);
371 tb->tb_pixmap = NULL;
372 }
373
374 /*
375 * Don't reset the offset if only text after the last '(' has
376 * changed. This is a hack to avoid visual noice on vbr files
377 * where we guess the length.
378 */
379 if (!(tb->tb_pixmap_text && strrchr(tb->tb_text, '(') &&
380 !strncmp(tb->tb_pixmap_text, tb->tb_text,
381 strrchr(tb->tb_text, '(') - tb->tb_text)))
382 tb->tb_offset = 0;
383
384 g_free(tb->tb_pixmap_text);
385 tb->tb_pixmap_text = g_strdup(tb->tb_text);
386
387 /*
388 * wl is the number of (partial) letters visible. Only makes
389 * sense when using skinned font.
390 */
391
392 wl = tb->tb_widget.width / 5;
393 if (wl * 5 != tb->tb_widget.width)
394 wl++;
395
396 length = g_utf8_strlen(tb->tb_text, -1);
397
398 tb->tb_is_scrollable = FALSE;
399
400 if (textbox_should_scroll(tb)) {
401 tb->tb_is_scrollable = TRUE;
402 pixmaptext = g_strconcat(tb->tb_pixmap_text, " *** ", NULL);
403 length += 7;
404 }
405 else if (!tb->tb_font && length <= wl) {
406 gint pad = wl - length;
407 gchar *padchars = g_strnfill(pad, ' ');
408
409 pixmaptext = g_strconcat(tb->tb_pixmap_text, padchars, NULL);
410 g_free(padchars);
411 length += pad;
412 }
413 else
414 pixmaptext = g_strdup(tb->tb_pixmap_text);
415
416
417 if (tb->tb_is_scrollable) {
418 if (tb->tb_scroll_enabled && !tb->tb_timeout_tag) {
419 gint tag;
420 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT;
421 tb->tb_timeout_tag = gtk_timeout_add(tag, textbox_scroll, tb);
422 }
423 }
424 else {
425 if (tb->tb_timeout_tag) {
426 gtk_timeout_remove(tb->tb_timeout_tag);
427 tb->tb_timeout_tag = 0;
428 }
429 tb->tb_offset = 0;
430 }
431
432 if (tb->tb_font) {
433 textbox_generate_xfont_pixmap(tb, pixmaptext);
434 g_free(pixmaptext);
435 return;
436 }
437
438 tb->tb_pixmap_width = length * 5;
439 tb->tb_pixmap = gdk_pixmap_new(mainwin->window,
440 tb->tb_pixmap_width, 6,
441 gdk_rgb_get_visual()->depth);
442 gc = tb->tb_widget.gc;
443
444 for (i = 0; i < length; i++) {
445 gchar c;
446 x = y = -1;
447 c = toupper(pixmaptext[i]);
448 if (c >= 'A' && c <= 'Z') {
449 x = 5 * (c - 'A');
450 y = 0;
451 }
452 else if (c >= '0' && c <= '9') {
453 x = 5 * (c - '0');
454 y = 6;
455 }
456 else
457 textbox_handle_special_char(c, &x, &y);
458
459 skin_draw_pixmap(bmp_active_skin,
460 tb->tb_pixmap, gc, tb->tb_skin_index,
461 x, y, i * 5, 0, 5, 6);
462 }
463 g_free(pixmaptext);
464 }
465
466 void
467 textbox_set_scroll(TextBox * tb, gboolean s)
468 {
469 g_return_if_fail(tb != NULL);
470
471 tb->tb_scroll_enabled = s;
472 if (tb->tb_scroll_enabled && tb->tb_is_scrollable
473 && tb->tb_scroll_allowed) {
474 gint tag;
475 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT;
476 tb->tb_timeout_tag = gtk_timeout_add(tag, textbox_scroll, tb);
477 }
478 else {
479 if (tb->tb_timeout_tag) {
480 gtk_timeout_remove(tb->tb_timeout_tag);
481 tb->tb_timeout_tag = 0;
482 }
483 tb->tb_offset = 0;
484 widget_draw(WIDGET(tb));
485 }
486
487 }
488
489 void
490 textbox_set_xfont(TextBox * tb, gboolean use_xfont, const gchar * fontname)
491 {
492 gint ascent, descent;
493
494 g_return_if_fail(tb != NULL);
495
496 if (tb->tb_font) {
497 pango_font_description_free(tb->tb_font);
498 tb->tb_font = NULL;
499 }
500
501 tb->tb_widget.y = tb->tb_nominal_y;
502 tb->tb_widget.height = tb->tb_nominal_height;
503
504 /* Make sure the pixmap is regenerated */
505 if (tb->tb_pixmap_text) {
506 g_free(tb->tb_pixmap_text);
507 tb->tb_pixmap_text = NULL;
508 }
509
510 if (!use_xfont || strlen(fontname) == 0)
511 return;
512
513 tb->tb_font = pango_font_description_from_string(fontname);
514 tb->tb_fontname = g_strdup(fontname);
515
516 text_get_extents(fontname,
517 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ",
518 NULL, NULL, &ascent, &descent);
519 tb->tb_font_ascent = ascent;
520 tb->tb_font_descent = descent;
521
522
523 if (tb->tb_font == NULL)
524 return;
525
526 tb->tb_widget.height = tb->tb_font_ascent;
527 if (tb->tb_widget.height > tb->tb_nominal_height)
528 tb->tb_widget.y -= (tb->tb_widget.height - tb->tb_nominal_height) / 2;
529 else
530 tb->tb_widget.height = tb->tb_nominal_height;
531 }
532
533 TextBox *
534 create_textbox(GList ** wlist, GdkPixmap * parent, GdkGC * gc,
535 gint x, gint y, gint w, gboolean allow_scroll, SkinPixmapId si)
536 {
537 TextBox *tb;
538
539 tb = g_new0(TextBox, 1);
540 widget_init(&tb->tb_widget, parent, gc, x, y, w, 6, 1);
541 tb->tb_widget.button_press_cb = textbox_button_press;
542 tb->tb_widget.button_release_cb = textbox_button_release;
543 tb->tb_widget.motion_cb = textbox_motion;
544 tb->tb_widget.draw = textbox_draw;
545 tb->tb_scroll_allowed = allow_scroll;
546 tb->tb_scroll_enabled = TRUE;
547 tb->tb_skin_index = si;
548 tb->tb_nominal_y = y;
549 tb->tb_nominal_height = tb->tb_widget.height;
550 widget_list_add(wlist, WIDGET(tb));
551 return tb;
552 }
553
554 void
555 textbox_free(TextBox * tb)
556 {
557 g_return_if_fail(tb != NULL);
558
559 if (tb->tb_pixmap)
560 g_object_unref(tb->tb_pixmap);
561 g_free(tb->tb_text);
562 g_free(tb);
563 }