comparison audacious/widgets/textbox.c @ 1653:a6e6d3500c13 trunk

[svn] - revert back to r2216
author nenolod
date Thu, 07 Sep 2006 22:26:54 -0700
parents
children 837983bac90f
comparison
equal deleted inserted replaced
1652:62c5bff8a05b 1653:a6e6d3500c13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include "widgetcore.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 += 1;
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 '\r':
248 *x = 50;
249 *y = 6;
250 break;
251 case ':':
252 case ';':
253 *x = 60;
254 *y = 6;
255 break;
256 case '(':
257 *x = 65;
258 *y = 6;
259 break;
260 case ')':
261 *x = 70;
262 *y = 6;
263 break;
264 case '-':
265 *x = 75;
266 *y = 6;
267 break;
268 case '`':
269 case '\'':
270 *x = 80;
271 *y = 6;
272 break;
273 case '!':
274 *x = 85;
275 *y = 6;
276 break;
277 case '_':
278 *x = 90;
279 *y = 6;
280 break;
281 case '+':
282 *x = 95;
283 *y = 6;
284 break;
285 case '\\':
286 *x = 100;
287 *y = 6;
288 break;
289 case '/':
290 *x = 105;
291 *y = 6;
292 break;
293 case '[':
294 *x = 110;
295 *y = 6;
296 break;
297 case ']':
298 *x = 115;
299 *y = 6;
300 break;
301 case '^':
302 *x = 120;
303 *y = 6;
304 break;
305 case '&':
306 *x = 125;
307 *y = 6;
308 break;
309 case '%':
310 *x = 130;
311 *y = 6;
312 break;
313 case '.':
314 case ',':
315 *x = 135;
316 *y = 6;
317 break;
318 case '=':
319 *x = 140;
320 *y = 6;
321 break;
322 case '$':
323 *x = 145;
324 *y = 6;
325 break;
326 case '#':
327 *x = 150;
328 *y = 6;
329 break;
330 case 'å':
331 case 'Å':
332 *x = 0;
333 *y = 12;
334 break;
335 case 'ö':
336 case 'Ö':
337 *x = 5;
338 *y = 12;
339 break;
340 case 'ä':
341 case 'Ä':
342 *x = 10;
343 *y = 12;
344 break;
345 case 'ü':
346 case 'Ü':
347 *x = 100;
348 *y = 0;
349 break;
350 case '?':
351 *x = 15;
352 *y = 12;
353 break;
354 case '*':
355 *x = 20;
356 *y = 12;
357 break;
358 default:
359 *x = 145;
360 *y = 0;
361 break;
362 }
363 }
364
365 static void
366 textbox_generate_pixmap(TextBox * tb)
367 {
368 gint length, i, x, y, wl;
369 gchar *pixmaptext;
370 GdkGC *gc;
371
372 g_return_if_fail(tb != NULL);
373
374 if (tb->tb_pixmap) {
375 g_object_unref(tb->tb_pixmap);
376 tb->tb_pixmap = NULL;
377 }
378
379 /*
380 * Don't reset the offset if only text after the last '(' has
381 * changed. This is a hack to avoid visual noice on vbr files
382 * where we guess the length.
383 */
384 if (!(tb->tb_pixmap_text && strrchr(tb->tb_text, '(') &&
385 !strncmp(tb->tb_pixmap_text, tb->tb_text,
386 strrchr(tb->tb_text, '(') - tb->tb_text)))
387 tb->tb_offset = 0;
388
389 g_free(tb->tb_pixmap_text);
390 tb->tb_pixmap_text = g_strdup(tb->tb_text);
391
392 /*
393 * wl is the number of (partial) letters visible. Only makes
394 * sense when using skinned font.
395 */
396
397 wl = tb->tb_widget.width / 5;
398 if (wl * 5 != tb->tb_widget.width)
399 wl++;
400
401 length = g_utf8_strlen(tb->tb_text, -1);
402
403 tb->tb_is_scrollable = FALSE;
404
405 if (textbox_should_scroll(tb)) {
406 tb->tb_is_scrollable = TRUE;
407 pixmaptext = g_strconcat(tb->tb_pixmap_text, " *** ", NULL);
408 length += 7;
409 }
410 else if (!tb->tb_font && length <= wl) {
411 gint pad = wl - length;
412 gchar *padchars = g_strnfill(pad, ' ');
413
414 pixmaptext = g_strconcat(tb->tb_pixmap_text, padchars, NULL);
415 g_free(padchars);
416 length += pad;
417 }
418 else
419 pixmaptext = g_strdup(tb->tb_pixmap_text);
420
421
422 if (tb->tb_is_scrollable) {
423 if (tb->tb_scroll_enabled && !tb->tb_timeout_tag) {
424 gint tag;
425 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT;
426 tb->tb_timeout_tag = gtk_timeout_add(tag, textbox_scroll, tb);
427 }
428 }
429 else {
430 if (tb->tb_timeout_tag) {
431 gtk_timeout_remove(tb->tb_timeout_tag);
432 tb->tb_timeout_tag = 0;
433 }
434 tb->tb_offset = 0;
435 }
436
437 if (tb->tb_font) {
438 textbox_generate_xfont_pixmap(tb, pixmaptext);
439 g_free(pixmaptext);
440 return;
441 }
442
443 tb->tb_pixmap_width = length * 5;
444 tb->tb_pixmap = gdk_pixmap_new(mainwin->window,
445 tb->tb_pixmap_width, 6,
446 gdk_rgb_get_visual()->depth);
447 gc = tb->tb_widget.gc;
448
449 for (i = 0; i < length; i++) {
450 gchar c;
451 x = y = -1;
452 c = toupper(pixmaptext[i]);
453 if (c >= 'A' && c <= 'Z') {
454 x = 5 * (c - 'A');
455 y = 0;
456 }
457 else if (c >= '0' && c <= '9') {
458 x = 5 * (c - '0');
459 y = 6;
460 }
461 else
462 textbox_handle_special_char(c, &x, &y);
463
464 skin_draw_pixmap(bmp_active_skin,
465 tb->tb_pixmap, gc, tb->tb_skin_index,
466 x, y, i * 5, 0, 5, 6);
467 }
468 g_free(pixmaptext);
469 }
470
471 void
472 textbox_set_scroll(TextBox * tb, gboolean s)
473 {
474 g_return_if_fail(tb != NULL);
475
476 tb->tb_scroll_enabled = s;
477 if (tb->tb_scroll_enabled && tb->tb_is_scrollable
478 && tb->tb_scroll_allowed) {
479 gint tag;
480 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT;
481
482 if (tb->tb_timeout_tag)
483 {
484 gtk_timeout_remove(tb->tb_timeout_tag);
485 tb->tb_timeout_tag = 0;
486 }
487
488 tb->tb_timeout_tag = gtk_timeout_add(tag, textbox_scroll, tb);
489 }
490 else
491 {
492 if (tb->tb_timeout_tag)
493 {
494 gtk_timeout_remove(tb->tb_timeout_tag);
495 tb->tb_timeout_tag = 0;
496 }
497
498 tb->tb_offset = 0;
499 widget_draw(WIDGET(tb));
500 }
501
502 }
503
504 void
505 textbox_set_xfont(TextBox * tb, gboolean use_xfont, const gchar * fontname)
506 {
507 gint ascent, descent;
508
509 g_return_if_fail(tb != NULL);
510
511 if (tb->tb_font) {
512 pango_font_description_free(tb->tb_font);
513 tb->tb_font = NULL;
514 }
515
516 tb->tb_widget.y = tb->tb_nominal_y;
517 tb->tb_widget.height = tb->tb_nominal_height;
518
519 /* Make sure the pixmap is regenerated */
520 if (tb->tb_pixmap_text) {
521 g_free(tb->tb_pixmap_text);
522 tb->tb_pixmap_text = NULL;
523 }
524
525 if (!use_xfont || strlen(fontname) == 0)
526 return;
527
528 tb->tb_font = pango_font_description_from_string(fontname);
529 tb->tb_fontname = g_strdup(fontname);
530
531 text_get_extents(fontname,
532 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ",
533 NULL, NULL, &ascent, &descent);
534 tb->tb_font_ascent = ascent;
535 tb->tb_font_descent = descent;
536
537
538 if (tb->tb_font == NULL)
539 return;
540
541 tb->tb_widget.height = tb->tb_font_ascent;
542 if (tb->tb_widget.height > tb->tb_nominal_height)
543 tb->tb_widget.y -= (tb->tb_widget.height - tb->tb_nominal_height) / 2;
544 else
545 tb->tb_widget.height = tb->tb_nominal_height;
546 }
547
548 TextBox *
549 create_textbox(GList ** wlist, GdkPixmap * parent, GdkGC * gc,
550 gint x, gint y, gint w, gboolean allow_scroll, SkinPixmapId si)
551 {
552 TextBox *tb;
553
554 tb = g_new0(TextBox, 1);
555 widget_init(&tb->tb_widget, parent, gc, x, y, w, 6, 1);
556 tb->tb_widget.button_press_cb = textbox_button_press;
557 tb->tb_widget.button_release_cb = textbox_button_release;
558 tb->tb_widget.motion_cb = textbox_motion;
559 tb->tb_widget.draw = textbox_draw;
560 tb->tb_scroll_allowed = allow_scroll;
561 tb->tb_scroll_enabled = TRUE;
562 tb->tb_skin_index = si;
563 tb->tb_nominal_y = y;
564 tb->tb_nominal_height = tb->tb_widget.height;
565 widget_list_add(wlist, WIDGET(tb));
566 tb->tb_timeout_tag = 0;
567 return tb;
568 }
569
570 void
571 textbox_free(TextBox * tb)
572 {
573 g_return_if_fail(tb != NULL);
574
575 if (tb->tb_pixmap)
576 g_object_unref(tb->tb_pixmap);
577 g_free(tb->tb_text);
578 g_free(tb);
579 }