Mercurial > audlegacy-plugins
annotate src/skins/ui_skinned_textbox.c @ 2836:fbb17b57229a
fix possible segfault on cleanup
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Mon, 14 Jul 2008 11:54:07 +0200 |
parents | 32e99af83a3e |
children | 2e081c64a529 |
rev | line source |
---|---|
2586 | 1 /* |
2 * Audacious - a cross-platform multimedia player | |
3 * Copyright (c) 2007 Tomasz Moń | |
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; under version 3 of the License. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses>. | |
22 * | |
23 * The Audacious team does not consider modular code linking to | |
24 * Audacious or using our public API to be a derived work. | |
25 */ | |
26 | |
27 #include "ui_skinned_textbox.h" | |
28 #include "skins_cfg.h" | |
29 #include "plugin.h" | |
30 #include <string.h> | |
31 | |
32 #include "util.h" | |
33 | |
34 #define UI_SKINNED_TEXTBOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), ui_skinned_textbox_get_type(), UiSkinnedTextboxPrivate)) | |
35 typedef struct _UiSkinnedTextboxPrivate UiSkinnedTextboxPrivate; | |
36 | |
37 #define TEXTBOX_SCROLL_SMOOTH_TIMEOUT 30 | |
38 #define TEXTBOX_SCROLL_WAIT 80 | |
39 | |
40 enum { | |
41 CLICKED, | |
42 DOUBLE_CLICKED, | |
43 RIGHT_CLICKED, | |
44 DOUBLED, | |
45 LAST_SIGNAL | |
46 }; | |
47 | |
48 struct _UiSkinnedTextboxPrivate { | |
49 SkinPixmapId skin_index; | |
50 gboolean scaled; | |
51 gboolean scroll_back; | |
52 gint nominal_y, nominal_height; | |
53 gint scroll_timeout; | |
54 gint font_ascent, font_descent; | |
55 PangoFontDescription *font; | |
56 gchar *fontname; | |
57 gchar *pixbuf_text; | |
58 gint skin_id; | |
59 gint drag_x, drag_off, offset; | |
60 gboolean is_scrollable, is_dragging; | |
61 gint pixbuf_width; | |
62 GdkPixbuf *pixbuf; | |
63 gboolean scroll_allowed, scroll_enabled; | |
64 gint scroll_dummy; | |
65 gint move_x, move_y; | |
66 }; | |
67 | |
68 static void ui_skinned_textbox_class_init (UiSkinnedTextboxClass *klass); | |
69 static void ui_skinned_textbox_init (UiSkinnedTextbox *textbox); | |
70 static void ui_skinned_textbox_destroy (GtkObject *object); | |
71 static void ui_skinned_textbox_realize (GtkWidget *widget); | |
72 static void ui_skinned_textbox_size_request (GtkWidget *widget, GtkRequisition *requisition); | |
73 static void ui_skinned_textbox_size_allocate (GtkWidget *widget, GtkAllocation *allocation); | |
74 static gboolean ui_skinned_textbox_expose (GtkWidget *widget, GdkEventExpose *event); | |
75 static gboolean ui_skinned_textbox_button_press (GtkWidget *widget, GdkEventButton *event); | |
76 static gboolean ui_skinned_textbox_button_release (GtkWidget *widget, GdkEventButton *event); | |
77 static gboolean ui_skinned_textbox_motion_notify (GtkWidget *widget, GdkEventMotion *event); | |
78 static void ui_skinned_textbox_toggle_scaled (UiSkinnedTextbox *textbox); | |
79 static gboolean ui_skinned_textbox_should_scroll (UiSkinnedTextbox *textbox); | |
80 static void textbox_generate_xfont_pixmap (UiSkinnedTextbox *textbox, const gchar *pixmaptext); | |
81 static gboolean textbox_scroll (gpointer data); | |
82 static void textbox_generate_pixmap (UiSkinnedTextbox *textbox); | |
83 static void textbox_handle_special_char (gchar *c, gint * x, gint * y); | |
84 | |
85 static GtkWidgetClass *parent_class = NULL; | |
86 static guint textbox_signals[LAST_SIGNAL] = { 0 }; | |
87 | |
88 GType ui_skinned_textbox_get_type() { | |
89 static GType textbox_type = 0; | |
90 if (!textbox_type) { | |
91 static const GTypeInfo textbox_info = { | |
92 sizeof (UiSkinnedTextboxClass), | |
93 NULL, | |
94 NULL, | |
95 (GClassInitFunc) ui_skinned_textbox_class_init, | |
96 NULL, | |
97 NULL, | |
98 sizeof (UiSkinnedTextbox), | |
99 0, | |
100 (GInstanceInitFunc) ui_skinned_textbox_init, | |
101 }; | |
2590 | 102 textbox_type = g_type_register_static (GTK_TYPE_WIDGET, "UiSkinnedTextbox", &textbox_info, 0); |
2586 | 103 } |
104 | |
105 return textbox_type; | |
106 } | |
107 | |
108 static void ui_skinned_textbox_class_init(UiSkinnedTextboxClass *klass) { | |
109 GObjectClass *gobject_class; | |
110 GtkObjectClass *object_class; | |
111 GtkWidgetClass *widget_class; | |
112 | |
113 gobject_class = G_OBJECT_CLASS(klass); | |
114 object_class = (GtkObjectClass*) klass; | |
115 widget_class = (GtkWidgetClass*) klass; | |
116 parent_class = gtk_type_class (gtk_widget_get_type ()); | |
117 | |
118 object_class->destroy = ui_skinned_textbox_destroy; | |
119 | |
120 widget_class->realize = ui_skinned_textbox_realize; | |
121 widget_class->expose_event = ui_skinned_textbox_expose; | |
122 widget_class->size_request = ui_skinned_textbox_size_request; | |
123 widget_class->size_allocate = ui_skinned_textbox_size_allocate; | |
124 widget_class->button_press_event = ui_skinned_textbox_button_press; | |
125 widget_class->button_release_event = ui_skinned_textbox_button_release; | |
126 widget_class->motion_notify_event = ui_skinned_textbox_motion_notify; | |
127 | |
128 klass->clicked = NULL; | |
129 klass->double_clicked = NULL; | |
130 klass->right_clicked = NULL; | |
131 klass->scaled = ui_skinned_textbox_toggle_scaled; | |
132 | |
133 textbox_signals[CLICKED] = | |
134 g_signal_new ("clicked", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
135 G_STRUCT_OFFSET (UiSkinnedTextboxClass, clicked), NULL, NULL, | |
136 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
137 | |
138 textbox_signals[DOUBLE_CLICKED] = | |
139 g_signal_new ("double-clicked", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
140 G_STRUCT_OFFSET (UiSkinnedTextboxClass, double_clicked), NULL, NULL, | |
141 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
142 | |
143 textbox_signals[RIGHT_CLICKED] = | |
144 g_signal_new ("right-clicked", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
145 G_STRUCT_OFFSET (UiSkinnedTextboxClass, right_clicked), NULL, NULL, | |
146 gtk_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER); | |
147 | |
148 textbox_signals[DOUBLED] = | |
149 g_signal_new ("toggle-scaled", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, | |
150 G_STRUCT_OFFSET (UiSkinnedTextboxClass, scaled), NULL, NULL, | |
151 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0); | |
152 | |
153 g_type_class_add_private (gobject_class, sizeof (UiSkinnedTextboxPrivate)); | |
154 } | |
155 | |
156 static void ui_skinned_textbox_init(UiSkinnedTextbox *textbox) { | |
157 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
158 priv->move_x = 0; | |
159 priv->move_y = 0; | |
160 } | |
161 | |
162 GtkWidget* ui_skinned_textbox_new(GtkWidget *fixed, gint x, gint y, gint w, gboolean allow_scroll, SkinPixmapId si) { | |
163 UiSkinnedTextbox *textbox = g_object_new (ui_skinned_textbox_get_type (), NULL); | |
164 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
165 | |
166 textbox->height = aud_active_skin->properties.textbox_bitmap_font_height; | |
167 textbox->x = x; | |
168 textbox->y = y; | |
169 textbox->text = g_strdup(""); | |
170 textbox->width = w; | |
171 priv->scroll_allowed = allow_scroll; | |
172 priv->scroll_enabled = TRUE; | |
173 priv->skin_index = si; | |
174 priv->nominal_y = y; | |
175 priv->nominal_height = textbox->height; | |
176 priv->scroll_timeout = 0; | |
177 priv->scroll_dummy = 0; | |
178 | |
179 priv->scaled = FALSE; | |
180 | |
181 gtk_fixed_put(GTK_FIXED(fixed), GTK_WIDGET(textbox), textbox->x, textbox->y); | |
182 | |
183 return GTK_WIDGET(textbox); | |
184 } | |
185 | |
186 static void ui_skinned_textbox_destroy(GtkObject *object) { | |
187 UiSkinnedTextbox *textbox; | |
2836
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
188 UiSkinnedTextboxPrivate *priv; |
2586 | 189 |
190 g_return_if_fail (object != NULL); | |
191 g_return_if_fail (UI_SKINNED_IS_TEXTBOX (object)); | |
192 | |
193 textbox = UI_SKINNED_TEXTBOX (object); | |
2836
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
194 priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(object); |
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
195 |
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
196 if (priv->scroll_timeout) { |
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
197 g_source_remove(priv->scroll_timeout); |
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
198 priv->scroll_timeout = 0; |
fbb17b57229a
fix possible segfault on cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2698
diff
changeset
|
199 } |
2586 | 200 |
201 if (GTK_OBJECT_CLASS (parent_class)->destroy) | |
202 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); | |
203 } | |
204 | |
205 static void ui_skinned_textbox_realize(GtkWidget *widget) { | |
206 UiSkinnedTextbox *textbox; | |
207 GdkWindowAttr attributes; | |
208 gint attributes_mask; | |
209 | |
210 g_return_if_fail (widget != NULL); | |
211 g_return_if_fail (UI_SKINNED_IS_TEXTBOX(widget)); | |
212 | |
213 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED); | |
214 textbox = UI_SKINNED_TEXTBOX(widget); | |
215 | |
216 attributes.x = widget->allocation.x; | |
217 attributes.y = widget->allocation.y; | |
218 attributes.width = widget->allocation.width; | |
219 attributes.height = widget->allocation.height; | |
220 attributes.wclass = GDK_INPUT_OUTPUT; | |
221 attributes.window_type = GDK_WINDOW_CHILD; | |
222 attributes.event_mask = gtk_widget_get_events(widget); | |
223 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | | |
224 GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | | |
225 GDK_POINTER_MOTION_HINT_MASK; | |
226 attributes.visual = gtk_widget_get_visual(widget); | |
227 attributes.colormap = gtk_widget_get_colormap(widget); | |
228 | |
229 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; | |
230 widget->window = gdk_window_new(widget->parent->window, &attributes, attributes_mask); | |
231 | |
232 widget->style = gtk_style_attach(widget->style, widget->window); | |
233 | |
234 gdk_window_set_user_data(widget->window, widget); | |
235 } | |
236 | |
237 static void ui_skinned_textbox_size_request(GtkWidget *widget, GtkRequisition *requisition) { | |
238 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
239 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
240 | |
241 requisition->width = textbox->width*(priv->scaled ? config.scale_factor : 1); | |
242 requisition->height = textbox->height*(priv->scaled ? config.scale_factor : 1 ); | |
243 } | |
244 | |
245 static void ui_skinned_textbox_size_allocate(GtkWidget *widget, GtkAllocation *allocation) { | |
246 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
247 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
248 | |
249 widget->allocation = *allocation; | |
250 widget->allocation.x *= (priv->scaled ? config.scale_factor : 1); | |
251 widget->allocation.y *= (priv->scaled ? config.scale_factor : 1); | |
252 if (GTK_WIDGET_REALIZED (widget)) | |
253 gdk_window_move_resize(widget->window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height); | |
254 | |
255 if (textbox->x + priv->move_x - widget->allocation.x/(priv->scaled ? config.scale_factor : 1) <3); | |
256 priv->move_x = 0; | |
257 if (textbox->y + priv->move_y - widget->allocation.y/(priv->scaled ? config.scale_factor : 1) <3); | |
258 priv->move_y = 0; | |
259 textbox->x = widget->allocation.x/(priv->scaled ? config.scale_factor : 1); | |
260 textbox->y = widget->allocation.y/(priv->scaled ? config.scale_factor : 1); | |
261 | |
262 if (textbox->width - (guint) (widget->allocation.width / (priv->scaled ? config.scale_factor : 1)) > 2) { | |
263 textbox->width = (guint) (widget->allocation.width / (priv->scaled ? config.scale_factor : 1)); | |
264 if (priv->pixbuf_text) g_free(priv->pixbuf_text); | |
265 priv->pixbuf_text = NULL; | |
266 priv->offset = 0; | |
267 gtk_widget_set_size_request(widget, textbox->width, textbox->height); | |
268 gtk_widget_queue_draw(GTK_WIDGET(textbox)); | |
269 } | |
270 } | |
271 | |
272 static gboolean ui_skinned_textbox_expose(GtkWidget *widget, GdkEventExpose *event) { | |
273 g_return_val_if_fail (widget != NULL, FALSE); | |
274 g_return_val_if_fail (UI_SKINNED_IS_TEXTBOX (widget), FALSE); | |
275 g_return_val_if_fail (event != NULL, FALSE); | |
276 | |
277 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
278 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
279 g_return_val_if_fail (textbox->width > 0 && textbox->height > 0, FALSE); | |
280 | |
281 GdkPixbuf *obj = NULL; | |
282 gint cw; | |
283 | |
284 if (textbox->text && (!priv->pixbuf_text || strcmp(textbox->text, priv->pixbuf_text))) | |
285 textbox_generate_pixmap(textbox); | |
286 | |
287 if (priv->pixbuf) { | |
288 if (skin_get_id() != priv->skin_id) { | |
289 priv->skin_id = skin_get_id(); | |
290 textbox_generate_pixmap(textbox); | |
291 } | |
292 obj = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, textbox->width, textbox->height); | |
293 | |
294 if (config.twoway_scroll) { // twoway scroll | |
295 cw = priv->pixbuf_width - priv->offset; | |
296 if (cw > textbox->width) | |
297 cw = textbox->width; | |
298 gdk_pixbuf_copy_area(priv->pixbuf, priv->offset, 0, cw, textbox->height, obj, 0, 0); | |
299 if (cw < textbox->width) | |
300 gdk_pixbuf_copy_area(priv->pixbuf, 0, 0, textbox->width - cw, textbox->height, | |
301 obj, textbox->width - cw, textbox->height); | |
302 } else { // oneway scroll | |
303 int cw1, cw2; | |
304 | |
305 if (priv->offset >= priv->pixbuf_width) | |
306 priv->offset = 0; | |
307 | |
308 if (priv->pixbuf_width - priv->offset > textbox->width) { // case1 | |
309 cw1 = textbox->width; | |
310 gdk_pixbuf_copy_area(priv->pixbuf, priv->offset, 0, cw1, textbox->height, | |
311 obj, 0, 0); | |
312 } else { // case 2 | |
313 cw1 = priv->pixbuf_width - priv->offset; | |
314 gdk_pixbuf_copy_area(priv->pixbuf, priv->offset, 0, cw1, textbox->height, obj, 0, 0); | |
315 cw2 = textbox->width - cw1; | |
316 gdk_pixbuf_copy_area(priv->pixbuf, 0, 0, cw2, textbox->height, obj, cw1, 0); | |
317 } | |
318 } | |
319 | |
320 ui_skinned_widget_draw(widget, obj, textbox->width, textbox->height, priv->scaled); | |
321 | |
322 g_object_unref(obj); | |
323 } | |
324 | |
325 return FALSE; | |
326 } | |
327 | |
328 static gboolean ui_skinned_textbox_button_press(GtkWidget *widget, GdkEventButton *event) { | |
329 g_return_val_if_fail (widget != NULL, FALSE); | |
330 g_return_val_if_fail (UI_SKINNED_IS_TEXTBOX (widget), FALSE); | |
331 g_return_val_if_fail (event != NULL, FALSE); | |
332 | |
333 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
334 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
335 | |
336 if (event->type == GDK_BUTTON_PRESS) { | |
337 textbox = UI_SKINNED_TEXTBOX(widget); | |
338 if (event->button == 3 && !g_signal_has_handler_pending(widget, textbox_signals[RIGHT_CLICKED], 0, TRUE)) | |
339 return FALSE; | |
340 else if (event->button == 1) { | |
341 if (priv->scroll_allowed) { | |
342 if ((priv->pixbuf_width > textbox->width) && priv->is_scrollable) { | |
343 priv->is_dragging = TRUE; | |
344 priv->drag_off = priv->offset; | |
345 priv->drag_x = event->x; | |
346 } | |
347 } else | |
348 g_signal_emit(widget, textbox_signals[CLICKED], 0); | |
349 | |
350 } else if (event->button == 3) { | |
351 g_signal_emit(widget, textbox_signals[RIGHT_CLICKED], 0, event); | |
352 } else | |
353 priv->is_dragging = FALSE; | |
354 } else if (event->type == GDK_2BUTTON_PRESS) { | |
355 if (event->button == 1) { | |
356 if (g_signal_has_handler_pending(widget, textbox_signals[DOUBLE_CLICKED], 0, TRUE)) | |
357 g_signal_emit(widget, textbox_signals[DOUBLE_CLICKED], 0); | |
358 else | |
359 return FALSE; | |
360 } | |
361 } | |
362 | |
363 return TRUE; | |
364 } | |
365 | |
366 static gboolean ui_skinned_textbox_button_release(GtkWidget *widget, GdkEventButton *event) { | |
367 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(widget); | |
368 | |
369 if (event->button == 1) { | |
370 priv->is_dragging = FALSE; | |
371 } | |
372 | |
373 return TRUE; | |
374 } | |
375 | |
376 static gboolean ui_skinned_textbox_motion_notify(GtkWidget *widget, GdkEventMotion *event) { | |
377 g_return_val_if_fail (widget != NULL, FALSE); | |
378 g_return_val_if_fail (UI_SKINNED_IS_TEXTBOX (widget), FALSE); | |
379 g_return_val_if_fail (event != NULL, FALSE); | |
380 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); | |
381 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(widget); | |
382 | |
383 if (priv->is_dragging) { | |
384 if (priv->scroll_allowed && | |
385 priv->pixbuf_width > textbox->width) { | |
386 priv->offset = priv->drag_off - (event->x - priv->drag_x); | |
387 | |
388 while (priv->offset < 0) | |
389 priv->offset = 0; | |
390 | |
391 while (priv->offset > (priv->pixbuf_width - textbox->width)) | |
392 priv->offset = (priv->pixbuf_width - textbox->width); | |
393 | |
394 gtk_widget_queue_draw(widget); | |
395 } | |
396 } | |
397 | |
398 return TRUE; | |
399 } | |
400 | |
401 static void ui_skinned_textbox_toggle_scaled(UiSkinnedTextbox *textbox) { | |
402 GtkWidget *widget = GTK_WIDGET (textbox); | |
403 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
404 | |
405 priv->scaled = !priv->scaled; | |
406 | |
407 gtk_widget_set_size_request(widget, textbox->width*(priv->scaled ? config.scale_factor : 1 ), | |
408 textbox->height*(priv->scaled ? config.scale_factor : 1 )); | |
409 | |
410 gtk_widget_queue_draw(GTK_WIDGET(textbox)); | |
411 } | |
412 | |
413 static gboolean ui_skinned_textbox_should_scroll(UiSkinnedTextbox *textbox) { | |
414 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
415 | |
416 if (!priv->scroll_allowed) | |
417 return FALSE; | |
418 | |
419 if (priv->font) { | |
420 gint width; | |
421 text_get_extents(priv->fontname, textbox->text, &width, NULL, NULL, NULL); | |
422 | |
423 if (width <= textbox->width) | |
424 return FALSE; | |
425 else | |
426 return TRUE; | |
427 } | |
428 | |
429 if (g_utf8_strlen(textbox->text, -1) * aud_active_skin->properties.textbox_bitmap_font_width > textbox->width) | |
430 return TRUE; | |
431 | |
432 return FALSE; | |
433 } | |
434 | |
435 void ui_skinned_textbox_set_xfont(GtkWidget *widget, gboolean use_xfont, const gchar * fontname) { | |
436 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
437 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
438 | |
439 gint ascent, descent; | |
440 | |
441 g_return_if_fail(textbox != NULL); | |
442 | |
443 if (priv->font) { | |
444 pango_font_description_free(priv->font); | |
445 priv->font = NULL; | |
446 } | |
447 | |
448 textbox->y = priv->nominal_y; | |
449 textbox->height = priv->nominal_height; | |
450 | |
451 /* Make sure the pixmap is regenerated */ | |
452 if (priv->pixbuf_text) { | |
453 g_free(priv->pixbuf_text); | |
454 priv->pixbuf_text = NULL; | |
455 } | |
456 | |
457 if (!use_xfont || strlen(fontname) == 0) | |
458 return; | |
459 | |
460 priv->font = pango_font_description_from_string(fontname); | |
461 priv->fontname = g_strdup(fontname); | |
462 | |
463 text_get_extents(fontname, | |
464 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz ", | |
465 NULL, NULL, &ascent, &descent); | |
466 priv->font_ascent = ascent; | |
467 priv->font_descent = descent; | |
468 | |
469 | |
470 if (priv->font == NULL) | |
471 return; | |
472 | |
473 textbox->height = priv->font_ascent; | |
474 if (textbox->height > priv->nominal_height) | |
475 textbox->y -= (textbox->height - priv->nominal_height) / 2; | |
476 else | |
477 textbox->height = priv->nominal_height; | |
478 } | |
479 | |
480 void ui_skinned_textbox_set_text(GtkWidget *widget, const gchar *text) { | |
481 g_return_if_fail(text != NULL); | |
482 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX (widget); | |
483 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
484 | |
485 if (!strcmp(textbox->text, text)) | |
486 return; | |
487 if (textbox->text) | |
488 g_free(textbox->text); | |
489 | |
490 textbox->text = aud_str_to_utf8(text); | |
491 priv->scroll_back = FALSE; | |
492 gtk_widget_queue_draw(GTK_WIDGET(textbox)); | |
493 } | |
494 | |
495 static void textbox_generate_xfont_pixmap(UiSkinnedTextbox *textbox, const gchar *pixmaptext) { | |
496 /* FIXME: should operate directly on priv->pixbuf, it shouldn't use pixmap */ | |
497 gint length, i; | |
498 GdkGC *gc, *maskgc; | |
499 GdkColor *c, pattern; | |
500 GdkBitmap *mask; | |
501 PangoLayout *layout; | |
502 gint width; | |
503 GdkPixmap *pixmap; | |
504 | |
505 g_return_if_fail(textbox != NULL); | |
506 g_return_if_fail(pixmaptext != NULL); | |
507 g_return_if_fail(textbox->height > 0); | |
508 | |
509 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
510 | |
511 length = g_utf8_strlen(pixmaptext, -1); | |
512 | |
513 text_get_extents(priv->fontname, pixmaptext, &width, NULL, NULL, NULL); | |
514 | |
515 priv->pixbuf_width = MAX(width, textbox->width); | |
516 pixmap = gdk_pixmap_new(mainwin->window, priv->pixbuf_width, | |
517 textbox->height, | |
518 gdk_rgb_get_visual()->depth); | |
519 gc = gdk_gc_new(pixmap); | |
520 c = skin_get_color(aud_active_skin, SKIN_TEXTBG); | |
521 for (i = 0; i < textbox->height; i++) { | |
522 gdk_gc_set_foreground(gc, &c[6 * i / textbox->height]); | |
523 gdk_draw_line(pixmap, gc, 0, i, priv->pixbuf_width, i); | |
524 } | |
525 | |
526 mask = gdk_pixmap_new(mainwin->window, priv->pixbuf_width, textbox->height, 1); | |
527 maskgc = gdk_gc_new(mask); | |
528 pattern.pixel = 0; | |
529 gdk_gc_set_foreground(maskgc, &pattern); | |
530 | |
531 gdk_draw_rectangle(mask, maskgc, TRUE, 0, 0, priv->pixbuf_width, textbox->height); | |
532 pattern.pixel = 1; | |
533 gdk_gc_set_foreground(maskgc, &pattern); | |
534 | |
535 gdk_gc_set_foreground(gc, skin_get_color(aud_active_skin, SKIN_TEXTFG)); | |
536 | |
537 layout = gtk_widget_create_pango_layout(mainwin, pixmaptext); | |
538 pango_layout_set_font_description(layout, priv->font); | |
539 | |
540 gdk_draw_layout(pixmap, gc, 0, (priv->font_descent / 2), layout); | |
541 g_object_unref(layout); | |
542 | |
543 g_object_unref(maskgc); | |
544 | |
545 gdk_gc_set_clip_mask(gc, mask); | |
546 c = skin_get_color(aud_active_skin, SKIN_TEXTFG); | |
547 for (i = 0; i < textbox->height; i++) { | |
548 gdk_gc_set_foreground(gc, &c[6 * i / textbox->height]); | |
549 gdk_draw_line(pixmap, gc, 0, i, priv->pixbuf_width, i); | |
550 } | |
551 priv->pixbuf = gdk_pixbuf_get_from_drawable(NULL, pixmap, gdk_colormap_get_system(), 0, 0, 0, 0, priv->pixbuf_width, textbox->height); | |
552 g_object_unref(mask); | |
553 g_object_unref(gc); | |
554 } | |
555 | |
556 static gboolean textbox_scroll(gpointer data) { | |
557 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(data); | |
558 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
559 | |
560 if (!priv->is_dragging) { | |
561 if (priv->scroll_dummy < TEXTBOX_SCROLL_WAIT) | |
562 priv->scroll_dummy++; | |
563 else { | |
564 if(config.twoway_scroll) { | |
565 if (priv->scroll_back) | |
566 priv->offset -= 1; | |
567 else | |
568 priv->offset += 1; | |
569 | |
570 if (priv->offset >= (priv->pixbuf_width - textbox->width)) { | |
571 priv->scroll_back = TRUE; | |
572 priv->scroll_dummy = 0; | |
573 priv->offset = priv->pixbuf_width - textbox->width; | |
574 } | |
575 if (priv->offset <= 0) { | |
576 priv->scroll_back = FALSE; | |
577 priv->scroll_dummy = 0; | |
578 priv->offset = 0; | |
579 } | |
580 } | |
581 else { // oneway scroll | |
582 priv->scroll_back = FALSE; | |
583 priv->offset += 1; | |
584 } | |
585 gtk_widget_queue_draw(GTK_WIDGET(textbox)); | |
586 } | |
587 } | |
588 return TRUE; | |
589 } | |
590 | |
591 static void textbox_generate_pixmap(UiSkinnedTextbox *textbox) { | |
592 gint length, i, x, y, wl; | |
593 gchar *pixmaptext; | |
594 gchar *tmp, *stxt; | |
595 | |
596 g_return_if_fail(textbox != NULL); | |
597 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
598 | |
599 if (priv->pixbuf) { | |
600 g_object_unref(priv->pixbuf); | |
601 priv->pixbuf = NULL; | |
602 } | |
603 | |
604 /* | |
605 * Don't reset the offset if only text after the last '(' has | |
606 * changed. This is a hack to avoid visual noice on vbr files | |
607 * where we guess the length. | |
608 */ | |
609 if (!(priv->pixbuf_text && strrchr(textbox->text, '(') && | |
610 !strncmp(priv->pixbuf_text, textbox->text, | |
611 strrchr(textbox->text, '(') - textbox->text))) | |
612 priv->offset = 0; | |
613 | |
614 g_free(priv->pixbuf_text); | |
615 priv->pixbuf_text = g_strdup(textbox->text); | |
616 | |
617 /* | |
618 * wl is the number of (partial) letters visible. Only makes | |
619 * sense when using skinned font. | |
620 */ | |
621 wl = textbox->width / 5; | |
622 if (wl * 5 != textbox->width) | |
623 wl++; | |
624 | |
625 length = g_utf8_strlen(textbox->text, -1); | |
626 | |
627 priv->is_scrollable = FALSE; | |
628 | |
629 priv->is_scrollable = ui_skinned_textbox_should_scroll(textbox); | |
630 | |
631 if (priv->is_scrollable) { | |
632 if(!config.twoway_scroll) { | |
633 pixmaptext = g_strdup_printf("%s *** ", priv->pixbuf_text); | |
634 length += 5; | |
635 } else | |
636 pixmaptext = g_strdup(priv->pixbuf_text); | |
637 } else | |
638 if (!priv->font && length <= wl) { | |
639 gint pad = wl - length; | |
640 gchar *padchars = g_strnfill(pad, ' '); | |
641 | |
642 pixmaptext = g_strconcat(priv->pixbuf_text, padchars, NULL); | |
643 g_free(padchars); | |
644 length += pad; | |
645 } else | |
646 pixmaptext = g_strdup(priv->pixbuf_text); | |
647 | |
648 if (priv->is_scrollable) { | |
649 if (priv->scroll_enabled && !priv->scroll_timeout) { | |
650 gint tag; | |
651 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT; | |
652 priv->scroll_timeout = g_timeout_add(tag, textbox_scroll, textbox); | |
653 } | |
654 } else { | |
655 if (priv->scroll_timeout) { | |
656 g_source_remove(priv->scroll_timeout); | |
657 priv->scroll_timeout = 0; | |
658 } | |
659 priv->offset = 0; | |
660 } | |
661 | |
662 if (priv->font) { | |
663 textbox_generate_xfont_pixmap(textbox, pixmaptext); | |
664 g_free(pixmaptext); | |
665 return; | |
666 } | |
667 | |
668 priv->pixbuf_width = length * aud_active_skin->properties.textbox_bitmap_font_width; | |
669 priv->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, | |
670 priv->pixbuf_width, aud_active_skin->properties.textbox_bitmap_font_height); | |
671 | |
672 for (tmp = stxt = g_utf8_strup(pixmaptext, -1), i = 0; | |
673 tmp != NULL && i < length; i++, tmp = g_utf8_next_char(tmp)) { | |
674 gchar c = *tmp; | |
675 x = y = -1; | |
676 if (c >= 'A' && c <= 'Z') { | |
677 x = aud_active_skin->properties.textbox_bitmap_font_width * (c - 'A'); | |
678 y = 0; | |
679 } | |
680 else if (c >= '0' && c <= '9') { | |
681 x = aud_active_skin->properties.textbox_bitmap_font_width * (c - '0'); | |
682 y = aud_active_skin->properties.textbox_bitmap_font_height; | |
683 } | |
684 else | |
685 textbox_handle_special_char(tmp, &x, &y); | |
686 | |
687 skin_draw_pixbuf(GTK_WIDGET(textbox), aud_active_skin, | |
688 priv->pixbuf, priv->skin_index, | |
689 x, y, i * aud_active_skin->properties.textbox_bitmap_font_width, 0, | |
690 aud_active_skin->properties.textbox_bitmap_font_width, | |
691 aud_active_skin->properties.textbox_bitmap_font_height); | |
692 } | |
693 g_free(stxt); | |
694 g_free(pixmaptext); | |
695 } | |
696 | |
697 void ui_skinned_textbox_set_scroll(GtkWidget *widget, gboolean scroll) { | |
698 g_return_if_fail(widget != NULL); | |
2635
b990e7eb0c25
save config on plugin cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2590
diff
changeset
|
699 g_return_if_fail(UI_SKINNED_IS_TEXTBOX(widget)); |
b990e7eb0c25
save config on plugin cleanup
Tomasz Mon <desowin@gmail.com>
parents:
2590
diff
changeset
|
700 |
2586 | 701 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); |
702 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(textbox); | |
703 | |
704 priv->scroll_enabled = scroll; | |
705 if (priv->scroll_enabled && priv->is_scrollable && priv->scroll_allowed) { | |
706 gint tag; | |
707 tag = TEXTBOX_SCROLL_SMOOTH_TIMEOUT; | |
708 if (priv->scroll_timeout) { | |
709 g_source_remove(priv->scroll_timeout); | |
710 priv->scroll_timeout = 0; | |
711 } | |
712 priv->scroll_timeout = g_timeout_add(tag, textbox_scroll, textbox); | |
713 | |
714 } else { | |
715 | |
716 if (priv->scroll_timeout) { | |
717 g_source_remove(priv->scroll_timeout); | |
718 priv->scroll_timeout = 0; | |
719 } | |
720 | |
721 priv->offset = 0; | |
722 gtk_widget_queue_draw(GTK_WIDGET(textbox)); | |
723 } | |
724 } | |
725 | |
726 static void textbox_handle_special_char(gchar *c, gint * x, gint * y) { | |
727 gint tx, ty; | |
728 | |
729 switch (*c) { | |
730 case '"': | |
731 tx = 26; | |
732 ty = 0; | |
733 break; | |
734 case '\r': | |
735 tx = 10; | |
736 ty = 1; | |
737 break; | |
738 case ':': | |
739 case ';': | |
740 tx = 12; | |
741 ty = 1; | |
742 break; | |
743 case '(': | |
744 tx = 13; | |
745 ty = 1; | |
746 break; | |
747 case ')': | |
748 tx = 14; | |
749 ty = 1; | |
750 break; | |
751 case '-': | |
752 tx = 15; | |
753 ty = 1; | |
754 break; | |
755 case '`': | |
756 case '\'': | |
757 tx = 16; | |
758 ty = 1; | |
759 break; | |
760 case '!': | |
761 tx = 17; | |
762 ty = 1; | |
763 break; | |
764 case '_': | |
765 tx = 18; | |
766 ty = 1; | |
767 break; | |
768 case '+': | |
769 tx = 19; | |
770 ty = 1; | |
771 break; | |
772 case '\\': | |
773 tx = 20; | |
774 ty = 1; | |
775 break; | |
776 case '/': | |
777 tx = 21; | |
778 ty = 1; | |
779 break; | |
780 case '[': | |
781 tx = 22; | |
782 ty = 1; | |
783 break; | |
784 case ']': | |
785 tx = 23; | |
786 ty = 1; | |
787 break; | |
788 case '^': | |
789 tx = 24; | |
790 ty = 1; | |
791 break; | |
792 case '&': | |
793 tx = 25; | |
794 ty = 1; | |
795 break; | |
796 case '%': | |
797 tx = 26; | |
798 ty = 1; | |
799 break; | |
800 case '.': | |
801 case ',': | |
802 tx = 27; | |
803 ty = 1; | |
804 break; | |
805 case '=': | |
806 tx = 28; | |
807 ty = 1; | |
808 break; | |
809 case '$': | |
810 tx = 29; | |
811 ty = 1; | |
812 break; | |
813 case '#': | |
814 tx = 30; | |
815 ty = 1; | |
816 break; | |
817 case '?': | |
818 tx = 3; | |
819 ty = 2; | |
820 break; | |
821 case '*': | |
822 tx = 4; | |
823 ty = 2; | |
824 break; | |
825 default: | |
826 tx = 29; | |
827 ty = 0; | |
828 break; | |
829 } | |
830 | |
831 const gchar *change[] = {"Ą", "A", "Ę", "E", "Ć", "C", "Ł", "L", "Ó", "O", "Ś", "S", "Ż", "Z", "Ź", "Z", | |
832 "Ń", "N", "Ü", "U", NULL}; | |
833 int i; | |
834 for (i = 0; change[i]; i+=2) { | |
835 if (!strncmp(c, change[i], strlen(change[i]))) { | |
836 tx = (*change[i+1] - 'A'); | |
837 break; | |
838 } | |
839 } | |
840 | |
841 /* those are commonly included into skins */ | |
842 if (!strncmp(c, "Å", strlen("Å"))) { | |
843 tx = 0; | |
844 ty = 2; | |
845 } else if (!strncmp(c, "Ö", strlen("Ö"))) { | |
846 tx = 1; | |
847 ty = 2; | |
848 } else if (!strncmp(c, "Ä", strlen("Ä"))) { | |
849 tx = 2; | |
850 ty = 2; | |
851 } | |
852 | |
853 *x = tx * aud_active_skin->properties.textbox_bitmap_font_width; | |
854 *y = ty * aud_active_skin->properties.textbox_bitmap_font_height; | |
855 } | |
856 | |
857 void ui_skinned_textbox_move_relative(GtkWidget *widget, gint x, gint y) { | |
2698
32e99af83a3e
I don't think redraw signal is needed anymore
Tomasz Mon <desowin@gmail.com>
parents:
2635
diff
changeset
|
858 UiSkinnedTextbox *textbox = UI_SKINNED_TEXTBOX(widget); |
2586 | 859 UiSkinnedTextboxPrivate *priv = UI_SKINNED_TEXTBOX_GET_PRIVATE(widget); |
860 priv->move_x += x; | |
861 priv->move_y += y; | |
2698
32e99af83a3e
I don't think redraw signal is needed anymore
Tomasz Mon <desowin@gmail.com>
parents:
2635
diff
changeset
|
862 gtk_fixed_move(GTK_FIXED(gtk_widget_get_parent(widget)), widget, |
32e99af83a3e
I don't think redraw signal is needed anymore
Tomasz Mon <desowin@gmail.com>
parents:
2635
diff
changeset
|
863 textbox->x+priv->move_x, textbox->y+priv->move_y); |
2586 | 864 } |