Mercurial > audlegacy
annotate src/audacious/ui_skinned_textbox.c @ 2917:e968134f11ed trunk
fix dragging textbox
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Fri, 29 Jun 2007 10:16:01 +0200 |
parents | 3ef909ed3056 |
children | 4e71092ab29b |
rev | line source |
---|---|
2911 | 1 /* |
2 * Audacious - a cross-platform multimedia player | |
3 * Copyright (c) 2007 Audacious development team. | |
4 * | |
5 * Based on: | |
6 * BMP - Cross-platform multimedia player | |
7 * Copyright (C) 2003-2004 BMP development team. | |
8 * XMMS: | |
9 * Copyright (C) 1998-2003 XMMS development team. | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2 of the License, or | |
14 * (at your option) any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this program; if not, write to the Free Software | |
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
24 */ | |
25 | |
26 #include "widgets/widgetcore.h" | |
27 #include "ui_skinned_textbox.h" | |
28 #include "main.h" | |
29 #include "util.h" | |
30 #include "strings.h" | |
31 #include <string.h> | |
32 #include <ctype.h> | |
33 #include <gtk/gtkmain.h> | |
34 #include <gtk/gtkmarshal.h> | |
35 #include <gtk/gtkimage.h> | |
36 | |
37 #define UI_SKINNED_TEXTBOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UI_TYPE_SKINNED_TEXTBOX, UiSkinnedTextboxPrivate)) | |
38 typedef struct _UiSkinnedTextboxPrivate UiSkinnedTextboxPrivate; | |
39 | |
40 enum { | |
41 DOUBLE_CLICKED, | |
42 RIGHT_CLICKED, | |
43 DOUBLED, | |
44 REDRAW, | |
45 LAST_SIGNAL | |
46 }; | |
47 | |
48 struct _UiSkinnedTextboxPrivate { | |
49 GtkWidget *image; | |
50 GdkGC *gc; | |
51 gint w; | |
52 SkinPixmapId skin_index; | |
53 GtkWidget *fixed; | |
54 gboolean double_size; | |
55 gboolean scroll_back; | |
56 gint nominal_y, nominal_height; | |
57 gint scroll_timeout; | |
58 gint font_ascent, font_descent; | |
59 PangoFontDescription *font; | |
60 gchar *fontname; | |
61 gchar *pixmap_text; | |
62 gint skin_id; | |
63 gint drag_x, drag_off, offset; | |
64 gboolean is_scrollable, is_dragging; | |
65 gint pixmap_width; | |
66 GdkPixmap *pixmap; | |
67 gboolean scroll_allowed, scroll_enabled; | |
68 }; | |
69 | |
70 | |
71 static GtkBinClass *parent_class = NULL; | |
72 static void ui_skinned_textbox_class_init(UiSkinnedTextboxClass *klass); | |
73 static void ui_skinned_textbox_init(UiSkinnedTextbox *textbox); | |
74 static GObject* ui_skinned_textbox_constructor(GType type, guint n_construct_properties, GObjectConstructParam *construct_params); | |
75 static void ui_skinned_textbox_destroy(GtkObject *object); | |
76 static void ui_skinned_textbox_realize(GtkWidget *widget); | |
77 static void ui_skinned_textbox_unrealize(GtkWidget *widget); | |
78 static void ui_skinned_textbox_map(GtkWidget *widget); | |
79 static void ui_skinned_textbox_unmap(GtkWidget *widget); | |
80 static gint ui_skinned_textbox_expose(GtkWidget *widget,GdkEventExpose *event); | |
81 | |
82 static void ui_skinned_textbox_size_allocate(GtkWidget *widget, GtkAllocation *allocation); | |
83 | |
84 static guint textbox_signals[LAST_SIGNAL] = { 0 }; | |
85 static gint ui_skinned_textbox_textbox_press(GtkWidget *widget, GdkEventButton *event); | |
86 static gint ui_skinned_textbox_textbox_release(GtkWidget *widget, GdkEventButton *event); | |
87 | |
88 static void ui_skinned_textbox_add(GtkContainer *container, GtkWidget *widget); | |
89 static void ui_skinned_textbox_toggle_doublesize(UiSkinnedTextbox *textbox); | |
90 | |
91 static gint ui_skinned_textbox_motion_notify(GtkWidget *widget, GdkEventMotion *event); | |
92 static void ui_skinned_textbox_paint(UiSkinnedTextbox *textbox); | |
93 static void ui_skinned_textbox_redraw(UiSkinnedTextbox *textbox); | |
94 static gboolean ui_skinned_textbox_should_scroll(UiSkinnedTextbox *textbox); | |
95 static void textbox_generate_xfont_pixmap(UiSkinnedTextbox *textbox, const gchar *pixmaptext); | |
96 static void textbox_generate_pixmap(UiSkinnedTextbox *textbox); | |
97 static void textbox_handle_special_char(gchar c, gint * x, gint * y); | |
98 | |
99 GType ui_skinned_textbox_get_type (void) { | |
100 static GType textbox_type = 0; | |
101 | |
102 if (!textbox_type) { | |
103 static const GTypeInfo textbox_info = { | |
104 sizeof (UiSkinnedTextboxClass), | |
105 NULL, /* base_init */ | |
106 NULL, /* base_finalize */ | |
107 (GClassInitFunc) ui_skinned_textbox_class_init, | |
108 NULL, /* class_finalize */ | |
109 NULL, /* class_data */ | |
110 sizeof (UiSkinnedTextbox), | |
111 16, /* n_preallocs */ | |
112 (GInstanceInitFunc) ui_skinned_textbox_init, | |
113 }; | |
114 | |
115 textbox_type = g_type_register_static (GTK_TYPE_BIN, "UiSkinnedTextbox", &textbox_info, 0); | |
116 } | |
117 return textbox_type; | |
118 } | |
119 | |
120 static void ui_skinned_textbox_class_init (UiSkinnedTextboxClass *klass) { | |
121 GObjectClass *gobject_class; | |
122 GtkObjectClass *object_class; | |
123 GtkWidgetClass *widget_class; | |
124 GtkContainerClass *container_class; | |
125 | |
126 gobject_class = G_OBJECT_CLASS(klass); | |
127 object_class = (GtkObjectClass*)klass; | |
128 widget_class = (GtkWidgetClass*)klass; | |
129 container_class = (GtkContainerClass*)klass; | |
130 | |
131 parent_class = g_type_class_peek_parent(klass); | |
132 gobject_class->constructor = ui_skinned_textbox_constructor; | |
133 object_class->destroy = ui_skinned_textbox_destroy; | |
134 | |
135 widget_class->realize = ui_skinned_textbox_realize; | |
136 widget_class->unrealize = ui_skinned_textbox_unrealize; | |
137 widget_class->map = ui_skinned_textbox_map; | |
138 widget_class->unmap = ui_skinned_textbox_unmap; | |
139 widget_class->size_allocate = ui_skinned_textbox_size_allocate; | |
140 widget_class->expose_event = ui_skinned_textbox_expose; | |
141 widget_class->button_press_event = ui_skinned_textbox_textbox_press; | |
142 widget_class->button_release_event = ui_skinned_textbox_textbox_release; | |
143 widget_class->motion_notify_event = ui_skinned_textbox_motion_notify; | |
144 | |
145 container_class->add = ui_skinned_textbox_add; | |
146 | |
147 klass->double_clicked = NULL; | |
148 klass->right_clicked = NULL; | |
149 klass->doubled = ui_skinned_textbox_toggle_doublesize; | |
150 klass->redraw = ui_skinned_textbox_redraw; | |
151 | |
152 textbox_signals[DOUBLE_CLICKED] = | |
153 g_signal_new ("double-clicked", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
154 G_STRUCT_OFFSET (UiSkinnedTextboxClass, double_clicked), NULL, NULL, | |
155 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
156 | |
157 textbox_signals[RIGHT_CLICKED] = | |
158 g_signal_new ("right-clicked", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
159 G_STRUCT_OFFSET (UiSkinnedTextboxClass, right_clicked), NULL, NULL, | |
160 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
161 | |
162 textbox_signals[DOUBLED] = | |
163 g_signal_new ("toggle-double-size", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
164 G_STRUCT_OFFSET (UiSkinnedTextboxClass, doubled), NULL, NULL, | |
165 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
166 | |
167 textbox_signals[REDRAW] = | |
168 g_signal_new ("redraw", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
169 G_STRUCT_OFFSET (UiSkinnedTextboxClass, redraw), NULL, NULL, | |
170 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
171 | |
172 g_type_class_add_private (gobject_class, sizeof (UiSkinnedTextboxPrivate)); | |
173 } | |
174 | |
175 static void ui_skinned_textbox_init (UiSkinnedTextbox *textbox) { | |
176 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
177 priv->image = gtk_image_new(); | |
178 textbox->redraw = TRUE; | |
179 | |
180 g_object_set (priv->image, "visible", TRUE, NULL); | |
181 gtk_container_add(GTK_CONTAINER(GTK_WIDGET(textbox)), priv->image); | |
182 | |
183 GTK_WIDGET_SET_FLAGS (textbox, GTK_NO_WINDOW); | |
184 } | |
185 | |
186 static void ui_skinned_textbox_destroy (GtkObject *object) { | |
187 (*GTK_OBJECT_CLASS (parent_class)->destroy) (object); | |
188 } | |
189 | |
190 static GObject* ui_skinned_textbox_constructor(GType type, guint n_construct_properties, GObjectConstructParam *construct_params) { | |
191 GObject *object = (*G_OBJECT_CLASS (parent_class)->constructor)(type, n_construct_properties, construct_params); | |
192 | |
193 return object; | |
194 } | |
195 | |
196 static void ui_skinned_textbox_realize (GtkWidget *widget) { | |
197 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
198 GdkWindowAttr attrib; | |
199 | |
200 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); | |
201 | |
202 attrib.window_type = GDK_WINDOW_CHILD; | |
203 attrib.x = widget->allocation.x; | |
204 attrib.y = widget->allocation.y; | |
205 attrib.width = widget->allocation.width; | |
206 attrib.height = widget->allocation.height; | |
207 attrib.wclass = GDK_INPUT_ONLY; | |
208 attrib.event_mask = gtk_widget_get_events (widget); | |
209 attrib.event_mask |= (GDK_BUTTON_PRESS_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_RELEASE_MASK); | |
210 | |
211 widget->window = gtk_widget_get_parent_window(widget); | |
212 g_object_ref(widget->window); | |
213 | |
214 textbox->event_window = gdk_window_new(gtk_widget_get_parent_window(widget), &attrib, GDK_WA_X | GDK_WA_Y); | |
215 gdk_window_set_user_data (textbox->event_window, textbox); | |
216 } | |
217 | |
218 static void ui_skinned_textbox_unrealize(GtkWidget *widget) { | |
219 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
220 | |
221 if (textbox->event_window) { | |
222 gdk_window_set_user_data (textbox->event_window, NULL); | |
223 gdk_window_destroy (textbox->event_window); | |
224 textbox->event_window = NULL; | |
225 } | |
226 | |
227 GTK_WIDGET_CLASS (parent_class)->unrealize (widget); | |
228 } | |
229 | |
230 static void ui_skinned_textbox_map(GtkWidget *widget) { | |
231 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
232 g_return_if_fail (UI_IS_SKINNED_TEXTBOX (widget)); | |
233 GTK_WIDGET_CLASS (parent_class)->map(widget); | |
234 gdk_window_show (textbox->event_window); | |
235 } | |
236 | |
237 static void ui_skinned_textbox_unmap (GtkWidget *widget) { | |
238 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
239 g_return_if_fail (UI_IS_SKINNED_TEXTBOX(widget)); | |
240 | |
241 if (textbox->event_window) | |
242 gdk_window_hide (textbox->event_window); | |
243 | |
244 GTK_WIDGET_CLASS (parent_class)->unmap (widget); | |
245 } | |
246 | |
247 static gboolean ui_skinned_textbox_expose(GtkWidget *widget, GdkEventExpose *event) { | |
248 if (GTK_WIDGET_DRAWABLE (widget)) | |
249 (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event); | |
250 | |
251 return FALSE; | |
252 } | |
253 | |
254 GtkWidget* ui_skinned_textbox_new () { | |
255 return g_object_new (UI_TYPE_SKINNED_TEXTBOX, NULL); | |
256 } | |
257 | |
258 void ui_skinned_textbox_setup(GtkWidget *widget, GtkWidget *fixed,GdkPixmap * parent, GdkGC * gc, gint x, gint y, gint w, gboolean allow_scroll, SkinPixmapId si) { | |
259 | |
260 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
261 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
262 textbox->height = bmp_active_skin->properties.textbox_bitmap_font_height; | |
263 textbox->x = x; | |
264 textbox->y = y; | |
265 priv->gc = gc; | |
266 priv->w = w; | |
267 priv->scroll_allowed = allow_scroll; | |
268 priv->scroll_enabled = TRUE; | |
269 priv->skin_index = si; | |
270 priv->nominal_y = y; | |
271 priv->nominal_height = textbox->height; | |
272 priv->scroll_timeout = 0; | |
273 | |
274 priv->fixed = fixed; | |
275 priv->double_size = FALSE; | |
276 | |
277 gtk_widget_set_size_request(widget, priv->w, textbox->height); | |
278 gtk_fixed_put(GTK_FIXED(priv->fixed), widget, textbox->x, textbox->y); | |
279 } | |
280 | |
281 static void ui_skinned_textbox_size_allocate(GtkWidget *widget, GtkAllocation *allocation) { | |
282 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
283 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
284 GtkAllocation child_alloc; | |
285 | |
286 widget->allocation = *allocation; | |
287 if (GTK_BIN (textbox)->child) { //image should fill whole widget | |
288 child_alloc.x = widget->allocation.x; | |
289 child_alloc.y = widget->allocation.y; | |
290 child_alloc.width = MAX (1, widget->allocation.width); | |
291 child_alloc.height = MAX (1, widget->allocation.height); | |
292 | |
293 gtk_widget_size_allocate (GTK_BIN (textbox)->child, &child_alloc); | |
294 } | |
295 | |
296 if (GDK_IS_WINDOW(textbox->event_window)) | |
297 gdk_window_move_resize (textbox->event_window, widget->allocation.x, widget->allocation.y, | |
298 widget->allocation.width, widget->allocation.height); | |
299 | |
300 textbox->x = widget->allocation.x/(priv->double_size ? 2 : 1); | |
301 textbox->y = widget->allocation.y/(priv->double_size ? 2 : 1); | |
302 } | |
303 | |
304 static gboolean ui_skinned_textbox_textbox_press(GtkWidget *widget, GdkEventButton *event) { | |
305 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
306 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
307 | |
308 if (event->type == GDK_BUTTON_PRESS) { | |
309 textbox = UI_SKINNED_TEXTBOX(widget); | |
310 | |
311 if (event->button == 1) { | |
312 if (priv->scroll_allowed && (priv->pixmap_width > priv->w) && priv->is_scrollable) { | |
313 priv->is_dragging = TRUE; | |
314 textbox->redraw = TRUE; | |
315 priv->drag_off = priv->offset; | |
316 priv->drag_x = event->x; | |
317 } | |
318 } else if (event->button == 3) { | |
319 g_signal_emit(widget, textbox_signals[RIGHT_CLICKED], 0); | |
320 } | |
321 } else if (event->type == GDK_2BUTTON_PRESS) { | |
322 if (event->button == 1) { | |
323 g_signal_emit(widget, textbox_signals[DOUBLE_CLICKED], 0); | |
324 } | |
325 } | |
326 return TRUE; | |
327 } | |
328 | |
329 static gboolean ui_skinned_textbox_textbox_release(GtkWidget *widget, GdkEventButton *event) { | |
330 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
331 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
332 if (event->button == 1) { | |
333 priv->is_dragging = FALSE; | |
334 textbox->redraw = TRUE; | |
335 } | |
336 | |
337 return TRUE; | |
338 } | |
339 | |
340 static gboolean ui_skinned_textbox_motion_notify(GtkWidget *widget, GdkEventMotion *event) { | |
341 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
342 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
343 | |
344 if (priv->is_dragging) { | |
345 if (priv->scroll_allowed && | |
346 priv->pixmap_width > priv->w) { | |
347 priv->offset = priv->drag_off - (event->x - priv->drag_x); | |
348 | |
349 while (priv->offset < 0) | |
2917 | 350 priv->offset = 0; |
2911 | 351 |
352 while (priv->offset > (priv->pixmap_width - priv->w)) | |
2917 | 353 priv->offset = (priv->pixmap_width - priv->w); |
2911 | 354 |
355 ui_skinned_textbox_redraw(textbox); | |
356 } | |
357 } | |
358 | |
359 return FALSE; | |
360 } | |
361 | |
362 static void ui_skinned_textbox_add(GtkContainer *container, GtkWidget *widget) { | |
363 GTK_CONTAINER_CLASS (parent_class)->add(container, widget); | |
364 } | |
365 | |
366 static void ui_skinned_textbox_toggle_doublesize(UiSkinnedTextbox *textbox) { | |
367 GtkWidget *widget = GTK_WIDGET (textbox); | |
368 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
369 priv->double_size = !priv->double_size; | |
370 | |
371 gtk_widget_set_size_request(widget, priv->w*(1+priv->double_size), textbox->height*(1+priv->double_size)); | |
372 gtk_widget_set_uposition(widget, textbox->x*(1+priv->double_size), textbox->y*(1+priv->double_size)); | |
373 | |
374 textbox->redraw = TRUE; | |
375 } | |
376 | |
377 static void ui_skinned_textbox_paint(UiSkinnedTextbox *textbox) { | |
378 GtkWidget *widget = GTK_WIDGET (textbox); | |
379 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
380 | |
381 if (textbox->redraw == TRUE) { | |
382 textbox->redraw = FALSE; | |
383 gint cw; | |
384 GdkPixmap *obj; | |
385 GdkPixmap *src; | |
386 | |
387 if (textbox->text && (!priv->pixmap_text || strcmp(textbox->text, priv->pixmap_text))) | |
388 textbox_generate_pixmap(textbox); | |
389 | |
390 if (priv->pixmap) { | |
391 if (skin_get_id() != priv->skin_id) { | |
392 priv->skin_id = skin_get_id(); | |
393 textbox_generate_pixmap(textbox); | |
394 } | |
395 obj = gdk_pixmap_new(NULL, priv->w, textbox->height, gdk_rgb_get_visual()->depth); | |
396 src = priv->pixmap; | |
397 | |
398 cw = priv->pixmap_width - priv->offset; | |
399 if (cw > priv->w) | |
400 cw = priv->w; | |
401 gdk_draw_drawable(obj, priv->gc, src, priv->offset, 0, 0, 0, cw, textbox->height); | |
402 if (cw < priv->w) | |
403 gdk_draw_drawable(obj, priv->gc, src, 0, 0, | |
404 textbox->x + cw, textbox->y, | |
405 priv->w - cw, textbox->height); | |
406 | |
407 if (priv->double_size) { | |
408 GdkImage *img, *img2x; | |
409 img = gdk_drawable_get_image(obj, 0, 0, priv->w, textbox->height); | |
410 img2x = create_dblsize_image(img); | |
411 gtk_image_set(GTK_IMAGE(priv->image), img2x, NULL); | |
412 g_object_unref(img2x); | |
413 g_object_unref(img); | |
414 } else | |
415 gtk_image_set_from_pixmap(GTK_IMAGE(priv->image), obj, NULL); | |
416 | |
417 g_object_unref(obj); | |
418 gtk_widget_queue_resize(widget); | |
419 } | |
420 } | |
421 } | |
422 | |
423 static void ui_skinned_textbox_redraw(UiSkinnedTextbox *textbox) { | |
424 textbox->redraw = TRUE; | |
425 ui_skinned_textbox_paint(textbox); | |
426 } | |
427 | |
428 static gboolean ui_skinned_textbox_should_scroll(UiSkinnedTextbox *textbox) { | |
429 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
430 if (!priv->scroll_allowed) | |
431 return FALSE; | |
432 | |
433 if (priv->font) { | |
434 gint width; | |
435 text_get_extents(priv->fontname, textbox->text, &width, NULL, NULL, NULL); | |
436 | |
437 if (width <= priv->w) | |
438 return FALSE; | |
439 else | |
440 return TRUE; | |
441 } | |
442 | |
443 if (g_utf8_strlen(textbox->text, -1) * bmp_active_skin->properties.textbox_bitmap_font_width > priv->w) | |
444 return TRUE; | |
445 | |
446 return FALSE; | |
447 } | |
448 | |
449 void ui_skinned_textbox_set_xfont(GtkWidget *widget, gboolean use_xfont, const gchar * fontname) { | |
450 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
451 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
452 gint ascent, descent; | |
453 | |
454 g_return_if_fail(textbox != NULL); | |
455 | |
456 if (priv->font) { | |
457 pango_font_description_free(priv->font); | |
458 priv->font = NULL; | |
459 } | |
460 | |
461 textbox->y = priv->nominal_y; | |
462 textbox->height = priv->nominal_height; | |
463 | |
464 /* Make sure the pixmap is regenerated */ | |
465 if (priv->pixmap_text) { | |
466 g_free(priv->pixmap_text); | |
467 priv->pixmap_text = NULL; | |
468 } | |
469 | |
470 if (!use_xfont || strlen(fontname) == 0) | |
471 return; | |
472 | |
473 priv->font = pango_font_description_from_string(fontname); | |
474 priv->fontname = g_strdup(fontname); | |
475 | |
476 text_get_extents(fontname, | |
477 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ", | |
478 NULL, NULL, &ascent, &descent); | |
479 priv->font_ascent = ascent; | |
480 priv->font_descent = descent; | |
481 | |
482 | |
483 if (priv->font == NULL) | |
484 return; | |
485 | |
486 textbox->height = priv->font_ascent; | |
487 if (textbox->height > priv->nominal_height) | |
488 textbox->y -= (textbox->height - priv->nominal_height) / 2; | |
489 else | |
490 textbox->height = priv->nominal_height; | |
491 } | |
492 | |
493 void ui_skinned_textbox_set_text(GtkWidget *widget, const gchar *text) { | |
494 g_return_if_fail(text != NULL); | |
495 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
496 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
497 | |
498 if (textbox->text) { | |
499 if (!strcmp(text, textbox->text)) | |
500 return; | |
501 g_free(textbox->text); | |
502 } | |
503 | |
504 textbox->text = str_to_utf8(text); | |
505 priv->scroll_back = FALSE; | |
506 ui_skinned_textbox_redraw(textbox); | |
507 } | |
508 | |
509 static void textbox_generate_xfont_pixmap(UiSkinnedTextbox *textbox, const gchar *pixmaptext) { | |
510 gint length, i; | |
511 GdkGC *gc, *maskgc; | |
512 GdkColor *c, pattern; | |
513 GdkBitmap *mask; | |
514 PangoLayout *layout; | |
515 gint width; | |
516 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
517 | |
518 g_return_if_fail(textbox != NULL); | |
519 g_return_if_fail(pixmaptext != NULL); | |
520 | |
521 length = g_utf8_strlen(pixmaptext, -1); | |
522 | |
523 text_get_extents(priv->fontname, pixmaptext, &width, NULL, NULL, NULL); | |
524 | |
525 priv->pixmap_width = MAX(width, priv->w); | |
526 priv->pixmap = gdk_pixmap_new(mainwin->window, priv->pixmap_width, | |
527 textbox->height, | |
528 gdk_rgb_get_visual()->depth); | |
529 gc = priv->gc; | |
530 c = skin_get_color(bmp_active_skin, SKIN_TEXTBG); | |
531 for (i = 0; i < textbox->height; i++) { | |
532 gdk_gc_set_foreground(gc, &c[6 * i / textbox->height]); | |
533 gdk_draw_line(priv->pixmap, gc, 0, i, priv->w, i); | |
534 } | |
535 | |
536 mask = gdk_pixmap_new(mainwin->window, priv->pixmap_width, textbox->height, 1); | |
537 maskgc = gdk_gc_new(mask); | |
538 pattern.pixel = 0; | |
539 gdk_gc_set_foreground(maskgc, &pattern); | |
540 | |
541 gdk_draw_rectangle(mask, maskgc, TRUE, 0, 0, priv->pixmap_width, textbox->height); | |
542 pattern.pixel = 1; | |
543 gdk_gc_set_foreground(maskgc, &pattern); | |
544 | |
545 gdk_gc_set_foreground(gc, skin_get_color(bmp_active_skin, SKIN_TEXTFG)); | |
546 | |
547 layout = gtk_widget_create_pango_layout(mainwin, pixmaptext); | |
548 pango_layout_set_font_description(layout, priv->font); | |
549 | |
550 gdk_draw_layout(priv->pixmap, gc, 0, (priv->font_descent / 2), layout); | |
551 g_object_unref(layout); | |
552 | |
553 g_object_unref(maskgc); | |
554 | |
555 gdk_gc_set_clip_mask(gc, mask); | |
556 c = skin_get_color(bmp_active_skin, SKIN_TEXTFG); | |
557 for (i = 0; i < textbox->height; i++) { | |
558 gdk_gc_set_foreground(gc, &c[6 * i / textbox->height]); | |
559 gdk_draw_line(priv->pixmap, gc, 0, i, priv->pixmap_width, i); | |
560 } | |
561 g_object_unref(mask); | |
562 gdk_gc_set_clip_mask(gc, NULL); | |
563 } | |
564 | |
565 static gboolean textbox_scroll(gpointer data) { | |
566 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(data); | |
567 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (data); | |
568 | |
569 if (!priv->is_dragging) { | |
570 if (priv->scroll_back) priv->offset -= 1; | |
571 else priv->offset += 1; | |
572 | |
573 if (priv->offset >= (priv->pixmap_width - priv->w)) priv->scroll_back = TRUE; | |
574 if (priv->offset <= 0) priv->scroll_back = FALSE; | |
575 ui_skinned_textbox_redraw(textbox); | |
576 } | |
577 | |
578 return TRUE; | |
579 } | |
580 | |
581 static void textbox_generate_pixmap(UiSkinnedTextbox *textbox) { | |
582 gint length, i, x, y, wl; | |
583 gchar *pixmaptext; | |
584 GdkGC *gc; | |
585 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
586 g_return_if_fail(textbox != NULL); | |
587 | |
588 if (priv->pixmap) { | |
589 g_object_unref(priv->pixmap); | |
590 priv->pixmap = NULL; | |
591 } | |
592 | |
593 /* | |
594 * Don't reset the offset if only text after the last '(' has | |
595 * changed. This is a hack to avoid visual noice on vbr files | |
596 * where we guess the length. | |
597 */ | |
598 if (!(priv->pixmap_text && strrchr(textbox->text, '(') && | |
599 !strncmp(priv->pixmap_text, textbox->text, | |
600 strrchr(textbox->text, '(') - textbox->text))) | |
601 priv->offset = 0; | |
602 | |
603 g_free(priv->pixmap_text); | |
604 priv->pixmap_text = g_strdup(textbox->text); | |
605 | |
606 /* | |
607 * wl is the number of (partial) letters visible. Only makes | |
608 * sense when using skinned font. | |
609 */ | |
610 wl = priv->w / 5; | |
611 if (wl * 5 != priv->w) | |
612 wl++; | |
613 | |
614 length = g_utf8_strlen(textbox->text, -1); | |
615 | |
616 priv->is_scrollable = FALSE; | |
617 | |
618 priv->is_scrollable = ui_skinned_textbox_should_scroll(textbox); | |
2916
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
619 |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
620 if (!priv->is_scrollable && !priv->font && length <= wl) { |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
621 gint pad = wl - length; |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
622 gchar *padchars = g_strnfill(pad, ' '); |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
623 |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
624 pixmaptext = g_strconcat(priv->pixmap_text, padchars, NULL); |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
625 g_free(padchars); |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
626 length += pad; |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
627 } else |
3ef909ed3056
fix clearing textbox when short text is displayed
Tomasz Mon <desowin@gmail.com>
parents:
2913
diff
changeset
|
628 pixmaptext = g_strdup(priv->pixmap_text); |
2911 | 629 |
630 if (priv->is_scrollable) { | |
631 if (priv->scroll_enabled && !priv->scroll_timeout) { | |
632 gint tag; | |
633 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT; | |
634 priv->scroll_timeout = g_timeout_add(tag, textbox_scroll, textbox); | |
635 } | |
636 } else { | |
637 if (priv->scroll_timeout) { | |
638 g_source_remove(priv->scroll_timeout); | |
639 priv->scroll_timeout = 0; | |
640 } | |
641 priv->offset = 0; | |
642 } | |
643 | |
644 if (priv->font) { | |
645 textbox_generate_xfont_pixmap(textbox, pixmaptext); | |
646 g_free(pixmaptext); | |
647 return; | |
648 } | |
649 | |
650 priv->pixmap_width = length * bmp_active_skin->properties.textbox_bitmap_font_width; | |
651 priv->pixmap = gdk_pixmap_new(mainwin->window, | |
652 priv->pixmap_width, bmp_active_skin->properties.textbox_bitmap_font_height, | |
653 gdk_rgb_get_visual()->depth); | |
654 gc = priv->gc; | |
655 | |
656 for (i = 0; i < length; i++) { | |
657 gchar c; | |
658 x = y = -1; | |
659 c = toupper((int) pixmaptext[i]); | |
660 if (c >= 'A' && c <= 'Z') { | |
661 x = bmp_active_skin->properties.textbox_bitmap_font_width * (c - 'A'); | |
662 y = 0; | |
663 } | |
664 else if (c >= '0' && c <= '9') { | |
665 x = bmp_active_skin->properties.textbox_bitmap_font_width * (c - '0'); | |
666 y = bmp_active_skin->properties.textbox_bitmap_font_height; | |
667 } | |
668 else | |
669 textbox_handle_special_char(c, &x, &y); | |
670 | |
671 skin_draw_pixmap(bmp_active_skin, | |
672 priv->pixmap, gc, priv->skin_index, | |
673 x, y, i * bmp_active_skin->properties.textbox_bitmap_font_width, 0, | |
674 bmp_active_skin->properties.textbox_bitmap_font_width, | |
675 bmp_active_skin->properties.textbox_bitmap_font_height); | |
676 } | |
677 g_free(pixmaptext); | |
678 } | |
679 | |
680 void ui_skinned_textbox_set_scroll(GtkWidget *widget, gboolean scroll) { | |
681 g_return_if_fail(widget != NULL); | |
682 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
683 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE (textbox); | |
684 | |
685 priv->scroll_enabled = scroll; | |
686 if (priv->scroll_enabled && priv->is_scrollable && priv->scroll_allowed) { | |
687 gint tag; | |
688 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT; | |
689 if (priv->scroll_timeout) { | |
690 g_source_remove(priv->scroll_timeout); | |
691 priv->scroll_timeout = 0; | |
692 } | |
693 priv->scroll_timeout = g_timeout_add(tag, textbox_scroll, textbox); | |
694 | |
695 } else { | |
696 | |
697 if (priv->scroll_timeout) { | |
698 g_source_remove(priv->scroll_timeout); | |
699 priv->scroll_timeout = 0; | |
700 } | |
701 | |
702 priv->offset = 0; | |
703 ui_skinned_textbox_redraw(textbox); | |
704 } | |
705 } | |
706 | |
707 static void | |
708 textbox_handle_special_char(gchar c, gint * x, gint * y) | |
709 { | |
710 gint tx, ty; | |
711 | |
712 switch (c) { | |
713 case '"': | |
714 tx = 26; | |
715 ty = 0; | |
716 break; | |
717 case '\r': | |
718 tx = 10; | |
719 ty = 1; | |
720 break; | |
721 case ':': | |
722 case ';': | |
723 tx = 12; | |
724 ty = 1; | |
725 break; | |
726 case '(': | |
727 tx = 13; | |
728 ty = 1; | |
729 break; | |
730 case ')': | |
731 tx = 14; | |
732 ty = 1; | |
733 break; | |
734 case '-': | |
735 tx = 15; | |
736 ty = 1; | |
737 break; | |
738 case '`': | |
739 case '\'': | |
740 tx = 16; | |
741 ty = 1; | |
742 break; | |
743 case '!': | |
744 tx = 17; | |
745 ty = 1; | |
746 break; | |
747 case '_': | |
748 tx = 18; | |
749 ty = 1; | |
750 break; | |
751 case '+': | |
752 tx = 19; | |
753 ty = 1; | |
754 break; | |
755 case '\\': | |
756 tx = 20; | |
757 ty = 1; | |
758 break; | |
759 case '/': | |
760 tx = 21; | |
761 ty = 1; | |
762 break; | |
763 case '[': | |
764 tx = 22; | |
765 ty = 1; | |
766 break; | |
767 case ']': | |
768 tx = 23; | |
769 ty = 1; | |
770 break; | |
771 case '^': | |
772 tx = 24; | |
773 ty = 1; | |
774 break; | |
775 case '&': | |
776 tx = 25; | |
777 ty = 1; | |
778 break; | |
779 case '%': | |
780 tx = 26; | |
781 ty = 1; | |
782 break; | |
783 case '.': | |
784 case ',': | |
785 tx = 27; | |
786 ty = 1; | |
787 break; | |
788 case '=': | |
789 tx = 28; | |
790 ty = 1; | |
791 break; | |
792 case '$': | |
793 tx = 29; | |
794 ty = 1; | |
795 break; | |
796 case '#': | |
797 tx = 30; | |
798 ty = 1; | |
799 break; | |
800 case '?': | |
801 tx = 3; | |
802 ty = 2; | |
803 break; | |
804 case '*': | |
805 tx = 4; | |
806 ty = 2; | |
807 break; | |
808 default: | |
809 tx = 29; | |
810 ty = 0; | |
811 break; | |
812 } | |
813 | |
814 *x = tx * bmp_active_skin->properties.textbox_bitmap_font_width; | |
815 *y = ty * bmp_active_skin->properties.textbox_bitmap_font_height; | |
816 } |