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