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