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