Mercurial > pidgin
annotate src/gtkhtml.c @ 319:cea40691706c
[gaim-migrate @ 329]
A few good oscar changes before release.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 02 Jun 2000 19:45:16 +0000 |
parents | a8f964718837 |
children | f5b199e20d12 |
rev | line source |
---|---|
12 | 1 |
1 | 2 /* |
3 * gaim | |
4 * | |
5 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
22 | |
23 #include <stdio.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include <gtk/gtk.h> | |
27 #include <gdk/gdkprivate.h> | |
28 #include <gdk/gdkx.h> | |
29 #include <gdk/gdkkeysyms.h> | |
12 | 30 |
31 #ifndef _WIN32 | |
1 | 32 #include <X11/Xlib.h> |
33 #include <X11/Xatom.h> | |
12 | 34 #endif |
35 | |
69 | 36 #include "gaim.h" |
1 | 37 #include "gtkhtml.h" |
38 | |
39 #define MAX_SIZE 7 | |
40 #define MIN_HTML_WIDTH_LINES 20 | |
41 #define MIN_HTML_HEIGHT_LINES 10 | |
42 #define BORDER_WIDTH 2 | |
43 #define SCROLL_TIME 100 | |
44 #define SCROLL_PIXELS 5 | |
45 #define KEY_SCROLL_PIXELS 10 | |
46 | |
47 int font_sizes[] = { 80, 100, 120, 140, 200, 300, 400 }; | |
48 | |
12 | 49 /* |
1 | 50 GdkFont *fixed_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; |
51 GdkFont *fixed_bold_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
52 GdkFont *fixed_italic_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
12 | 53 GdkFont *fixed_bold_italic_font[] = |
54 { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
1 | 55 GdkFont *prop_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; |
56 GdkFont *prop_bold_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
57 GdkFont *prop_italic_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
58 GdkFont *prop_bold_italic_font[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; | |
12 | 59 */ |
60 | |
61 GData * font_cache; | |
62 static gboolean cache_init = FALSE; | |
63 | |
64 struct font_state | |
65 { | |
66 int size; | |
67 int owncolor; | |
68 int ownbg; | |
69 gchar font[1024]; | |
70 GdkColor *color; | |
71 GdkColor *bgcol; | |
72 struct font_state *next; | |
1 | 73 }; |
74 | |
75 struct font_state *push_state(struct font_state *current) | |
76 { | |
12 | 77 struct font_state *tmp; |
78 tmp = (struct font_state *) g_new0(struct font_state, 1); | |
1 | 79 tmp->next = current; |
80 tmp->color = current->color; | |
81 tmp->bgcol = current->bgcol; | |
82 tmp->size = current->size; | |
83 tmp->owncolor = 0; | |
84 tmp->ownbg = 0; | |
12 | 85 strcpy( tmp->font, current->font ); |
1 | 86 return tmp; |
87 } | |
88 | |
12 | 89 enum |
90 { | |
91 ARG_0, | |
92 ARG_HADJUSTMENT, | |
93 ARG_VADJUSTMENT, | |
1 | 94 }; |
95 | |
96 | |
12 | 97 enum |
98 { | |
99 TARGET_STRING, | |
100 TARGET_TEXT, | |
101 TARGET_COMPOUND_TEXT | |
1 | 102 }; |
103 | |
104 | |
12 | 105 static void gtk_html_class_init(GtkHtmlClass * klass); |
106 static void gtk_html_set_arg(GtkObject * object, GtkArg * arg, guint arg_id); | |
107 static void gtk_html_get_arg(GtkObject * object, GtkArg * arg, guint arg_id); | |
108 static void gtk_html_init(GtkHtml * html); | |
109 static void gtk_html_destroy(GtkObject * object); | |
110 static void gtk_html_finalize(GtkObject * object); | |
111 static void gtk_html_realize(GtkWidget * widget); | |
112 static void gtk_html_unrealize(GtkWidget * widget); | |
113 static void gtk_html_style_set(GtkWidget * widget, GtkStyle * previous_style); | |
114 static void gtk_html_draw_focus(GtkWidget * widget); | |
115 static void gtk_html_size_request(GtkWidget * widget, | |
116 GtkRequisition * requisition); | |
117 static void gtk_html_size_allocate(GtkWidget * widget, | |
118 GtkAllocation * allocation); | |
119 static void gtk_html_adjustment(GtkAdjustment * adjustment, GtkHtml * html); | |
120 static void gtk_html_disconnect(GtkAdjustment * adjustment, GtkHtml * html); | |
121 static void gtk_html_add_seperator(GtkHtml * html); | |
122 static void gtk_html_add_pixmap(GtkHtml * html, GdkPixmap * pm, gint fit); | |
123 static void gtk_html_add_text(GtkHtml * html, | |
124 GdkFont * font, | |
125 GdkColor * fore, | |
126 GdkColor * back, | |
127 gchar * chars, | |
128 gint length, | |
129 gint uline, gint strike, gchar * url); | |
130 static void gtk_html_draw_bit(GtkHtml * html, | |
131 GtkHtmlBit * htmlbit, gint redraw); | |
132 static void gtk_html_selection_get(GtkWidget * widget, | |
133 GtkSelectionData * selection_data, | |
134 guint sel_info, guint32 time); | |
135 static gint gtk_html_selection_clear(GtkWidget * widget, | |
136 GdkEventSelection * event); | |
137 static gint gtk_html_visibility_notify(GtkWidget * widget, | |
138 GdkEventVisibility * event); | |
1 | 139 |
140 | |
141 /* Event handlers */ | |
12 | 142 static void gtk_html_draw(GtkWidget * widget, GdkRectangle * area); |
143 static gint gtk_html_expose(GtkWidget * widget, GdkEventExpose * event); | |
144 static gint gtk_html_button_press(GtkWidget * widget, GdkEventButton * event); | |
145 static gint gtk_html_button_release(GtkWidget * widget, GdkEventButton * event); | |
146 static gint gtk_html_motion_notify(GtkWidget * widget, GdkEventMotion * event); | |
147 static gint gtk_html_key_press(GtkWidget * widget, GdkEventKey * event); | |
148 static gint gtk_html_leave_notify(GtkWidget * widget, GdkEventCrossing * event); | |
1 | 149 |
150 static gint gtk_html_tooltip_timeout(gpointer data); | |
151 | |
152 | |
12 | 153 static void clear_area(GtkHtml * html, GdkRectangle * area); |
154 static void expose_html(GtkHtml * html, GdkRectangle * area, gboolean cursor); | |
155 static void scroll_down(GtkHtml * html, gint diff0); | |
156 static void scroll_up(GtkHtml * html, gint diff0); | |
157 | |
158 static void adjust_adj(GtkHtml * html, GtkAdjustment * adj); | |
159 static void resize_html(GtkHtml * html); | |
160 static gint html_bit_is_onscreen(GtkHtml * html, GtkHtmlBit * hb); | |
161 static void draw_cursor(GtkHtml * html); | |
162 static void undraw_cursor(GtkHtml * html); | |
1 | 163 |
164 static GtkWidgetClass *parent_class = NULL; | |
165 | |
166 GtkType gtk_html_get_type(void) | |
167 { | |
12 | 168 static GtkType html_type = 0; |
169 | |
170 if (!html_type) | |
171 { | |
172 static const GtkTypeInfo html_info = { | |
173 "GtkHtml", | |
174 sizeof(GtkHtml), | |
175 sizeof(GtkHtmlClass), | |
176 (GtkClassInitFunc) gtk_html_class_init, | |
177 (GtkObjectInitFunc) gtk_html_init, | |
178 NULL, | |
179 NULL, | |
180 NULL, | |
181 }; | |
182 html_type = gtk_type_unique(GTK_TYPE_WIDGET, &html_info); | |
183 } | |
184 return html_type; | |
1 | 185 } |
186 | |
187 | |
12 | 188 static void gtk_html_class_init(GtkHtmlClass * class) |
1 | 189 { |
12 | 190 GtkObjectClass *object_class; |
191 GtkWidgetClass *widget_class; | |
192 | |
193 object_class = (GtkObjectClass *) class; | |
194 widget_class = (GtkWidgetClass *) class; | |
195 parent_class = gtk_type_class(GTK_TYPE_WIDGET); | |
196 | |
197 | |
198 gtk_object_add_arg_type("GtkHtml::hadjustment", | |
199 GTK_TYPE_ADJUSTMENT, | |
200 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT, | |
201 ARG_HADJUSTMENT); | |
202 | |
203 gtk_object_add_arg_type("GtkHtml::vadjustment", | |
204 GTK_TYPE_ADJUSTMENT, | |
205 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT, | |
206 ARG_VADJUSTMENT); | |
207 | |
208 object_class->set_arg = gtk_html_set_arg; | |
209 object_class->get_arg = gtk_html_get_arg; | |
210 object_class->destroy = gtk_html_destroy; | |
211 object_class->finalize = gtk_html_finalize; | |
212 | |
213 widget_class->realize = gtk_html_realize; | |
214 widget_class->unrealize = gtk_html_unrealize; | |
215 widget_class->style_set = gtk_html_style_set; | |
216 widget_class->draw_focus = gtk_html_draw_focus; | |
217 widget_class->size_request = gtk_html_size_request; | |
218 widget_class->size_allocate = gtk_html_size_allocate; | |
219 widget_class->draw = gtk_html_draw; | |
220 widget_class->expose_event = gtk_html_expose; | |
221 widget_class->button_press_event = gtk_html_button_press; | |
222 widget_class->button_release_event = gtk_html_button_release; | |
1 | 223 widget_class->motion_notify_event = gtk_html_motion_notify; |
224 widget_class->leave_notify_event = gtk_html_leave_notify; | |
12 | 225 widget_class->selection_get = gtk_html_selection_get; |
1 | 226 widget_class->selection_clear_event = gtk_html_selection_clear; |
227 widget_class->key_press_event = gtk_html_key_press; | |
228 widget_class->visibility_notify_event = gtk_html_visibility_notify; | |
12 | 229 |
230 | |
231 widget_class->set_scroll_adjustments_signal = | |
232 gtk_signal_new("set_scroll_adjustments", | |
233 GTK_RUN_LAST, | |
234 object_class->type, | |
235 GTK_SIGNAL_OFFSET(GtkHtmlClass, set_scroll_adjustments), | |
236 gtk_marshal_NONE__POINTER_POINTER, | |
237 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, | |
238 GTK_TYPE_ADJUSTMENT); | |
239 | |
240 | |
241 class->set_scroll_adjustments = gtk_html_set_adjustments; | |
1 | 242 |
243 } | |
244 | |
12 | 245 static void gtk_html_set_arg(GtkObject * object, GtkArg * arg, guint arg_id) |
1 | 246 { |
12 | 247 GtkHtml *html; |
248 | |
249 html = GTK_HTML(object); | |
250 | |
251 switch (arg_id) | |
252 { | |
253 case ARG_HADJUSTMENT: | |
254 gtk_html_set_adjustments(html, GTK_VALUE_POINTER(*arg), html->vadj); | |
255 break; | |
256 case ARG_VADJUSTMENT: | |
257 gtk_html_set_adjustments(html, html->hadj, GTK_VALUE_POINTER(*arg)); | |
258 break; | |
259 default: | |
260 break; | |
261 } | |
1 | 262 } |
263 | |
12 | 264 static void gtk_html_get_arg(GtkObject * object, GtkArg * arg, guint arg_id) |
1 | 265 { |
12 | 266 GtkHtml *html; |
267 | |
268 html = GTK_HTML(object); | |
269 | |
270 switch (arg_id) | |
271 { | |
272 case ARG_HADJUSTMENT: | |
273 GTK_VALUE_POINTER(*arg) = html->hadj; | |
274 break; | |
275 case ARG_VADJUSTMENT: | |
276 GTK_VALUE_POINTER(*arg) = html->vadj; | |
277 break; | |
278 default: | |
279 arg->type = GTK_TYPE_INVALID; | |
280 break; | |
281 } | |
1 | 282 } |
283 | |
12 | 284 static void gtk_html_init(GtkHtml * html) |
1 | 285 { |
12 | 286 static const GtkTargetEntry targets[] = { |
287 {"STRING", 0, TARGET_STRING}, | |
288 {"TEXT", 0, TARGET_TEXT}, | |
289 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT} | |
290 }; | |
291 | |
292 static const gint n_targets = sizeof(targets) / sizeof(targets[0]); | |
293 | |
294 GTK_WIDGET_SET_FLAGS(html, GTK_CAN_FOCUS); | |
295 | |
296 html->html_area = NULL; | |
297 html->hadj = NULL; | |
298 html->vadj = NULL; | |
1 | 299 html->current_x = 0; |
300 html->current_y = 0; | |
12 | 301 html->start_sel = html->end_sel = NULL; |
302 html->start_sel_x = html->start_sel_y = -1; | |
303 html->num_end = html->num_start = -1; | |
304 | |
1 | 305 html->html_bits = NULL; |
306 html->urls = NULL; | |
307 html->selected_text = NULL; | |
308 html->tooltip_hb = NULL; | |
309 html->tooltip_timer = -1; | |
310 html->tooltip_window = NULL; | |
12 | 311 html->cursor_hb = NULL; |
1 | 312 html->cursor_pos = 0; |
313 | |
314 html->pm = NULL; | |
315 | |
316 html->editable = 0; | |
317 html->transparent = 0; | |
318 | |
12 | 319 html->frozen = 0; |
320 | |
321 gtk_selection_add_targets(GTK_WIDGET(html), GDK_SELECTION_PRIMARY, | |
322 targets, n_targets); | |
323 | |
324 | |
325 | |
1 | 326 } |
327 | |
328 | |
12 | 329 GtkWidget *gtk_html_new(GtkAdjustment * hadj, GtkAdjustment * vadj) |
1 | 330 { |
12 | 331 GtkWidget *html; |
332 if(!cache_init) | |
333 { | |
334 g_datalist_init(&font_cache); | |
335 cache_init = TRUE; | |
336 } | |
337 | |
338 if (hadj) | |
339 g_return_val_if_fail(GTK_IS_ADJUSTMENT(hadj), NULL); | |
340 if (vadj) | |
341 g_return_val_if_fail(GTK_IS_ADJUSTMENT(vadj), NULL); | |
342 | |
343 html = gtk_widget_new(GTK_TYPE_HTML, | |
344 "hadjustment", hadj, "vadjustment", vadj, NULL); | |
345 | |
346 return html; | |
1 | 347 } |
348 | |
349 | |
12 | 350 void gtk_html_set_editable(GtkHtml * html, gboolean is_editable) |
1 | 351 { |
12 | 352 g_return_if_fail(html != NULL); |
353 g_return_if_fail(GTK_IS_HTML(html)); | |
354 | |
355 | |
356 html->editable = (is_editable != FALSE); | |
357 | |
358 if (is_editable) | |
359 draw_cursor(html); | |
360 else | |
361 undraw_cursor(html); | |
1 | 362 |
363 } | |
364 | |
12 | 365 void gtk_html_set_transparent(GtkHtml * html, gboolean is_transparent) |
1 | 366 { |
12 | 367 GdkRectangle rect; |
368 gint width, | |
369 height; | |
370 GtkWidget *widget; | |
371 | |
372 g_return_if_fail(html != NULL); | |
373 g_return_if_fail(GTK_IS_HTML(html)); | |
374 | |
375 | |
376 widget = GTK_WIDGET(html); | |
377 html->transparent = (is_transparent != FALSE); | |
378 | |
379 if (!GTK_WIDGET_REALIZED(widget)) | |
380 return; | |
381 | |
382 html->bg_gc = NULL; | |
383 gdk_window_get_size(widget->window, &width, &height); | |
384 rect.x = 0; | |
385 rect.y = 0; | |
386 rect.width = width; | |
387 rect.height = height; | |
388 gdk_window_clear_area(widget->window, rect.x, rect.y, rect.width, | |
389 rect.height); | |
390 | |
391 expose_html(html, &rect, FALSE); | |
392 gtk_html_draw_focus((GtkWidget *) html); | |
1 | 393 } |
394 | |
395 | |
12 | 396 void gtk_html_set_adjustments(GtkHtml * html, |
397 GtkAdjustment * hadj, GtkAdjustment * vadj) | |
1 | 398 { |
12 | 399 g_return_if_fail(html != NULL); |
400 g_return_if_fail(GTK_IS_HTML(html)); | |
401 if (hadj) | |
402 g_return_if_fail(GTK_IS_ADJUSTMENT(hadj)); | |
403 else | |
404 hadj = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)); | |
405 if (vadj) | |
406 g_return_if_fail(GTK_IS_ADJUSTMENT(vadj)); | |
407 else | |
408 vadj = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)); | |
409 | |
410 if (html->hadj && (html->hadj != hadj)) | |
411 { | |
412 gtk_signal_disconnect_by_data(GTK_OBJECT(html->hadj), html); | |
413 gtk_object_unref(GTK_OBJECT(html->hadj)); | |
414 } | |
415 | |
416 if (html->vadj && (html->vadj != vadj)) | |
417 { | |
418 gtk_signal_disconnect_by_data(GTK_OBJECT(html->vadj), html); | |
419 gtk_object_unref(GTK_OBJECT(html->vadj)); | |
420 } | |
421 | |
422 if (html->hadj != hadj) | |
423 { | |
424 html->hadj = hadj; | |
425 gtk_object_ref(GTK_OBJECT(html->hadj)); | |
426 gtk_object_sink(GTK_OBJECT(html->hadj)); | |
427 | |
428 gtk_signal_connect(GTK_OBJECT(html->hadj), "changed", | |
429 (GtkSignalFunc) gtk_html_adjustment, html); | |
430 gtk_signal_connect(GTK_OBJECT(html->hadj), "value_changed", | |
431 (GtkSignalFunc) gtk_html_adjustment, html); | |
432 gtk_signal_connect(GTK_OBJECT(html->hadj), "disconnect", | |
433 (GtkSignalFunc) gtk_html_disconnect, html); | |
434 gtk_html_adjustment(hadj, html); | |
435 } | |
436 | |
437 if (html->vadj != vadj) | |
438 { | |
439 html->vadj = vadj; | |
440 gtk_object_ref(GTK_OBJECT(html->vadj)); | |
441 gtk_object_sink(GTK_OBJECT(html->vadj)); | |
442 | |
443 gtk_signal_connect(GTK_OBJECT(html->vadj), "changed", | |
444 (GtkSignalFunc) gtk_html_adjustment, html); | |
445 gtk_signal_connect(GTK_OBJECT(html->vadj), "value_changed", | |
446 (GtkSignalFunc) gtk_html_adjustment, html); | |
447 gtk_signal_connect(GTK_OBJECT(html->vadj), "disconnect", | |
448 (GtkSignalFunc) gtk_html_disconnect, html); | |
449 gtk_html_adjustment(vadj, html); | |
450 } | |
1 | 451 } |
452 | |
453 | |
454 | |
12 | 455 GdkColor *get_color(int colorv, GdkColormap * map) |
1 | 456 { |
457 GdkColor *color; | |
12 | 458 #if 0 |
459 fprintf(stdout, "color is %x\n", colorv); | |
460 #endif | |
461 color = (GdkColor *) g_new0(GdkColor, 1); | |
1 | 462 color->red = ((colorv & 0xff0000) >> 16) * 256; |
463 color->green = ((colorv & 0xff00) >> 8) * 256; | |
464 color->blue = ((colorv & 0xff)) * 256; | |
465 #if 0 | |
12 | 466 fprintf(stdout, "Colors are %d, %d, %d\n", color->red, color->green, |
467 color->blue); | |
1 | 468 #endif |
469 gdk_color_alloc(map, color); | |
470 return color; | |
471 } | |
472 | |
473 | |
286 | 474 int load_font_with_cache(const char *name, const char *weight, char slant, |
475 int size, GdkFont **font_return) | |
1 | 476 { |
286 | 477 gchar font_spec[1024]; |
478 | |
306
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
479 if (size > 0) |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
480 g_snprintf(font_spec, sizeof font_spec, |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
481 "-*-%s-%s-%c-*-*-*-%d-*-*-*-*-*-*", |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
482 name, weight, slant, size); |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
483 else |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
484 g_snprintf(font_spec, sizeof font_spec, |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
485 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"); |
286 | 486 |
487 if((*font_return = g_datalist_id_get_data(&font_cache, | |
488 g_quark_from_string(font_spec)))) { | |
489 return TRUE; | |
490 } else if ((*font_return = gdk_font_load(font_spec))) { | |
491 g_datalist_id_set_data(&font_cache, | |
492 g_quark_from_string(font_spec), *font_return); | |
493 return TRUE; | |
494 } else { | |
495 return FALSE; | |
12 | 496 } |
286 | 497 } |
498 | |
499 | |
500 GdkFont *getfont(const char *font, int bold, int italic, int fixed, int size) | |
501 { | |
502 GdkFont *my_font = NULL; | |
503 gchar *weight, slant; | |
504 | |
505 if (!font || !strlen(font)) font = fixed ? "courier" : "helvetica"; | |
506 weight = bold ? "bold" : "medium"; | |
507 slant = italic ? 'i' : 'r'; | |
508 | |
509 if (size > MAX_SIZE) size = MAX_SIZE; | |
510 if (size < 1) size = 1; | |
511 size = font_sizes[size-1]; | |
512 | |
513 /* try both 'i'talic and 'o'blique for italic fonts, and keep | |
514 * increasing the size until we get one that works. */ | |
515 | |
305
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
516 while (size <= 720) { |
286 | 517 if (load_font_with_cache(font, weight, slant, size, &my_font)) |
518 return my_font; | |
519 if (italic && load_font_with_cache(font, weight, 'o', size, &my_font)) | |
520 return my_font; | |
521 size += 10; | |
12 | 522 } |
523 | |
286 | 524 /* since we couldn't get any size up to 72, fall back to the |
525 * default fonts. */ | |
526 | |
527 font = fixed ? "courier" : "helvetica"; | |
305
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
528 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
529 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
530 if (load_font_with_cache(font, weight, slant, size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
531 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
532 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
533 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
534 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
535 font = fixed ? "helvetica" : "courier"; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
536 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
537 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
538 if (load_font_with_cache(font, weight, slant, size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
539 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
540 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
541 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
542 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
543 /* whoops, couldn't do any of those. maybe they have a default outgoing |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
544 * font? maybe we can use that. */ |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
545 if (font_options & OPT_FONT_FACE) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
546 if (fontname != NULL) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
547 /* woohoo! */ |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
548 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
549 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
550 if (load_font_with_cache(fontname, "medium", 'r', size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
551 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
552 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
553 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
554 } |
12 | 555 } |
305
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
556 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
557 /* ok, now we're in a pickle. if we can't do any of the above, let's |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
558 * try doing the most boring font we can find. */ |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
559 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
560 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
561 if (load_font_with_cache("courier", "medium", 'r', size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
562 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
563 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
564 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
565 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
566 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
567 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
568 if (load_font_with_cache("helvetica", "medium", 'r', size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
569 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
570 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
571 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
572 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
573 size = 120; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
574 while (size <= 720) { |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
575 if (load_font_with_cache("times", "medium", 'r', size, &my_font)) |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
576 return my_font; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
577 size += 10; |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
578 } |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
579 |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
580 /* my god, how did we end up here. is there a 'generic font' function |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
581 * in gdk? that would be incredibly useful here. there's gotta be a |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
582 * better way to do this. */ |
77404a4692b1
[gaim-migrate @ 315]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
286
diff
changeset
|
583 |
306
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
584 /* well, if they can't do any of the fonts above, they'll take whatever |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
585 * they can get, and be happy about it, damn it. :) */ |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
586 load_font_with_cache("*", "*", '*', -1, &my_font); |
a8f964718837
[gaim-migrate @ 316]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
305
diff
changeset
|
587 return my_font; |
1 | 588 } |
589 | |
590 | |
591 /* 'Borrowed' from ETerm */ | |
12 | 592 GdkWindow *get_desktop_window(GtkWidget * widget) |
1 | 593 { |
12 | 594 #ifndef _WIN32 |
595 GdkAtom prop, | |
596 type, | |
597 prop2; | |
598 int format; | |
599 gint length; | |
600 guchar *data; | |
1 | 601 GtkWidget *w; |
602 | |
12 | 603 prop = gdk_atom_intern("_XROOTPMAP_ID", 1); |
604 prop2 = gdk_atom_intern("_XROOTCOLOR_PIXEL", 1); | |
605 | |
606 if (prop == None && prop2 == None) | |
607 { | |
608 return NULL; | |
609 } | |
610 | |
611 | |
612 | |
613 for (w = widget; w; w = w->parent) | |
614 { | |
615 | |
616 if (prop != None) | |
617 { | |
1 | 618 gdk_property_get(w->window, prop, AnyPropertyType, 0L, 1L, 0, |
12 | 619 &type, &format, &length, &data); |
620 } | |
621 else if (prop2 != None) | |
622 { | |
1 | 623 gdk_property_get(w->window, prop2, AnyPropertyType, 0L, 1L, 0, |
12 | 624 &type, &format, &length, &data); |
625 } | |
626 else | |
627 { | |
1 | 628 continue; |
629 } | |
12 | 630 if (type != None) |
631 { | |
1 | 632 return (w->window); |
633 } | |
634 } | |
12 | 635 #endif |
1 | 636 return NULL; |
637 | |
638 } | |
639 | |
640 | |
641 | |
12 | 642 GdkPixmap *get_desktop_pixmap(GtkWidget * widget) |
1 | 643 { |
12 | 644 #ifndef _WIN32 |
645 GdkPixmap *p; | |
646 GdkAtom prop, | |
647 type, | |
648 prop2; | |
649 int format; | |
650 gint length; | |
651 guint32 id; | |
652 guchar *data; | |
653 | |
654 prop = gdk_atom_intern("_XROOTPMAP_ID", 1); | |
655 prop2 = gdk_atom_intern("_XROOTCOLOR_PIXEL", 1); | |
656 | |
657 | |
658 if (prop == None && prop2 == None) | |
659 { | |
660 return NULL; | |
661 } | |
662 | |
663 if (prop != None) | |
664 { | |
665 gdk_property_get(get_desktop_window(widget), prop, AnyPropertyType, 0L, | |
666 1L, 0, &type, &format, &length, &data); | |
667 if (type == XA_PIXMAP) | |
668 { | |
1 | 669 id = data[0]; |
670 id += data[1] << 8; | |
671 id += data[2] << 16; | |
672 id += data[3] << 24; | |
12 | 673 p = gdk_pixmap_foreign_new(id); |
674 return p; | |
675 } | |
676 } | |
677 if (prop2 != None) | |
678 { | |
679 | |
1 | 680 /* XGetWindowProperty(Xdisplay, desktop_window, prop2, 0L, 1L, False, AnyPropertyType, |
681 &type, &format, &length, &after, &data);*/ | |
12 | 682 |
1 | 683 /* if (type == XA_CARDINAL) {*/ |
12 | 684 /* |
685 * D_PIXMAP((" Solid color not yet supported.\n")); | |
686 */ | |
687 | |
1 | 688 /* return NULL; |
689 }*/ | |
12 | 690 } |
691 /* | |
692 * D_PIXMAP(("No suitable attribute found.\n")); | |
693 */ | |
694 #endif | |
695 return NULL; | |
1 | 696 } |
697 | |
698 | |
12 | 699 static void clear_focus_area(GtkHtml * html, |
700 gint area_x, | |
701 gint area_y, gint area_width, gint area_height) | |
1 | 702 { |
12 | 703 GtkWidget *widget = GTK_WIDGET(html); |
704 gint x, | |
705 y; | |
706 | |
707 gint ythick = BORDER_WIDTH + widget->style->klass->ythickness; | |
708 gint xthick = BORDER_WIDTH + widget->style->klass->xthickness; | |
709 | |
710 gint width, | |
711 height; | |
712 | |
713 if (html->frozen > 0) | |
714 return; | |
715 | |
716 if (html->transparent) | |
717 { | |
1 | 718 if (html->pm == NULL) |
719 html->pm = get_desktop_pixmap(widget); | |
720 | |
12 | 721 if (html->pm == NULL) |
722 return; | |
723 | |
724 if (html->bg_gc == NULL) | |
725 { | |
1 | 726 GdkGCValues values; |
727 | |
12 | 728 values.tile = html->pm; |
729 values.fill = GDK_TILED; | |
730 | |
731 html->bg_gc = gdk_gc_new_with_values(html->html_area, &values, | |
732 GDK_GC_FILL | GDK_GC_TILE); | |
733 | |
734 } | |
735 | |
736 gdk_window_get_deskrelative_origin(widget->window, &x, &y); | |
1 | 737 |
738 gdk_draw_pixmap(widget->window, html->bg_gc, html->pm, | |
12 | 739 x + area_x, y + area_y, area_x, area_y, area_width, |
740 area_height); | |
741 | |
742 | |
743 } | |
744 else | |
745 { | |
746 gdk_window_get_size(widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, | |
747 &height); | |
748 | |
749 gdk_gc_set_ts_origin(html->bg_gc, | |
750 (-html->xoffset + xthick) % width, | |
751 (-html->yoffset + ythick) % height); | |
752 | |
753 gdk_draw_rectangle(widget->window, html->bg_gc, TRUE, | |
754 area_x, area_y, area_width, area_height); | |
755 } | |
1 | 756 } |
757 | |
12 | 758 static void gtk_html_draw_focus(GtkWidget * widget) |
1 | 759 { |
12 | 760 GtkHtml *html; |
761 gint width, | |
762 height; | |
763 gint x, | |
764 y; | |
765 | |
766 g_return_if_fail(widget != NULL); | |
767 g_return_if_fail(GTK_IS_HTML(widget)); | |
1 | 768 |
769 html = GTK_HTML(widget); | |
770 | |
12 | 771 if (GTK_WIDGET_DRAWABLE(widget)) |
772 { | |
773 gint ythick = widget->style->klass->ythickness; | |
774 gint xthick = widget->style->klass->xthickness; | |
775 gint xextra = BORDER_WIDTH; | |
776 gint yextra = BORDER_WIDTH; | |
777 | |
778 x = 0; | |
779 y = 0; | |
780 width = widget->allocation.width; | |
781 height = widget->allocation.height; | |
782 | |
783 if (GTK_WIDGET_HAS_FOCUS(widget)) | |
784 { | |
785 x += 1; | |
786 y += 1; | |
787 width -= 2; | |
788 height -= 2; | |
789 xextra -= 1; | |
790 yextra -= 1; | |
791 | |
792 gtk_paint_focus(widget->style, widget->window, | |
793 NULL, widget, "text", | |
794 0, 0, | |
795 widget->allocation.width - 1, | |
796 widget->allocation.height - 1); | |
797 } | |
798 | |
799 gtk_paint_shadow(widget->style, widget->window, | |
800 GTK_STATE_NORMAL, GTK_SHADOW_IN, | |
801 NULL, widget, "text", x, y, width, height); | |
802 | |
803 x += xthick; | |
804 y += ythick; | |
805 width -= 2 * xthick; | |
806 height -= 2 * ythick; | |
807 | |
808 | |
809 if (widget->style->bg_pixmap[GTK_STATE_NORMAL] || html->transparent) | |
810 { | |
811 /* | |
812 * top rect | |
813 */ | |
814 clear_focus_area(html, x, y, width, yextra); | |
815 /* | |
816 * left rect | |
817 */ | |
818 clear_focus_area(html, x, y + yextra, | |
819 xextra, y + height - 2 * yextra); | |
820 /* | |
821 * right rect | |
822 */ | |
823 clear_focus_area(html, x + width - xextra, y + yextra, | |
824 xextra, height - 2 * ythick); | |
825 /* | |
826 * bottom rect | |
827 */ | |
828 clear_focus_area(html, x, x + height - yextra, width, yextra); | |
829 } | |
830 } | |
1 | 831 } |
832 | |
12 | 833 static void gtk_html_size_request(GtkWidget * widget, |
834 GtkRequisition * requisition) | |
1 | 835 { |
12 | 836 gint xthickness; |
837 gint ythickness; | |
838 gint char_height; | |
839 gint char_width; | |
840 | |
841 g_return_if_fail(widget != NULL); | |
842 g_return_if_fail(GTK_IS_HTML(widget)); | |
843 g_return_if_fail(requisition != NULL); | |
844 | |
845 xthickness = widget->style->klass->xthickness + BORDER_WIDTH; | |
846 ythickness = widget->style->klass->ythickness + BORDER_WIDTH; | |
847 | |
848 char_height = MIN_HTML_HEIGHT_LINES * (widget->style->font->ascent + | |
849 widget->style->font->descent); | |
850 | |
851 char_width = MIN_HTML_WIDTH_LINES * (gdk_text_width(widget->style->font, | |
852 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | |
853 26) / 26); | |
854 | |
855 requisition->width = char_width + xthickness * 2; | |
856 requisition->height = char_height + ythickness * 2; | |
1 | 857 } |
858 | |
12 | 859 static void gtk_html_size_allocate(GtkWidget * widget, |
860 GtkAllocation * allocation) | |
1 | 861 { |
12 | 862 GtkHtml *html; |
863 | |
864 g_return_if_fail(widget != NULL); | |
865 g_return_if_fail(GTK_IS_HTML(widget)); | |
866 g_return_if_fail(allocation != NULL); | |
867 | |
868 html = GTK_HTML(widget); | |
869 | |
870 widget->allocation = *allocation; | |
871 if (GTK_WIDGET_REALIZED(widget)) | |
872 { | |
873 gdk_window_move_resize(widget->window, | |
874 allocation->x, allocation->y, | |
875 allocation->width, allocation->height); | |
876 | |
877 gdk_window_move_resize(html->html_area, | |
878 widget->style->klass->xthickness + BORDER_WIDTH, | |
879 widget->style->klass->ythickness + BORDER_WIDTH, | |
880 MAX(1, (gint) widget->allocation.width - | |
881 (gint) (widget->style->klass->xthickness + | |
882 (gint) BORDER_WIDTH) * 2), | |
883 MAX(1, (gint) widget->allocation.height - | |
884 (gint) (widget->style->klass->ythickness + | |
885 (gint) BORDER_WIDTH) * 2)); | |
886 | |
887 resize_html(html); | |
888 } | |
889 } | |
890 | |
891 static void gtk_html_draw(GtkWidget * widget, GdkRectangle * area) | |
892 { | |
893 g_return_if_fail(widget != NULL); | |
894 g_return_if_fail(GTK_IS_HTML(widget)); | |
895 g_return_if_fail(area != NULL); | |
896 | |
897 if (GTK_WIDGET_DRAWABLE(widget)) | |
898 { | |
899 expose_html(GTK_HTML(widget), area, TRUE); | |
900 gtk_widget_draw_focus(widget); | |
901 } | |
902 } | |
903 | |
904 | |
905 static gint gtk_html_expose(GtkWidget * widget, GdkEventExpose * event) | |
906 { | |
907 GtkHtml *html; | |
908 | |
909 g_return_val_if_fail(widget != NULL, FALSE); | |
910 g_return_val_if_fail(GTK_IS_HTML(widget), FALSE); | |
911 g_return_val_if_fail(event != NULL, FALSE); | |
912 | |
913 html = GTK_HTML(widget); | |
914 | |
915 if (event->window == html->html_area) | |
916 { | |
917 expose_html(html, &event->area, TRUE); | |
918 } | |
919 else if (event->count == 0) | |
920 { | |
921 gtk_widget_draw_focus(widget); | |
922 } | |
923 | |
924 return FALSE; | |
1 | 925 |
926 } | |
927 | |
928 | |
12 | 929 static gint gtk_html_selection_clear(GtkWidget * widget, |
930 GdkEventSelection * event) | |
1 | 931 { |
12 | 932 GtkHtml *html; |
933 | |
934 g_return_val_if_fail(widget != NULL, FALSE); | |
935 g_return_val_if_fail(GTK_IS_HTML(widget), FALSE); | |
936 g_return_val_if_fail(event != NULL, FALSE); | |
937 | |
938 /* | |
939 * Let the selection handling code know that the selection | |
940 * * has been changed, since we've overriden the default handler | |
941 */ | |
942 if (!gtk_selection_clear(widget, event)) | |
943 return FALSE; | |
944 | |
945 html = GTK_HTML(widget); | |
946 | |
947 if (event->selection == GDK_SELECTION_PRIMARY) | |
948 { | |
949 if (html->selected_text) | |
950 { | |
951 GList *hbits = html->html_bits; | |
952 GtkHtmlBit *hb; | |
953 | |
1 | 954 g_free(html->selected_text); |
955 html->selected_text = NULL; | |
956 html->start_sel = NULL; | |
957 html->end_sel = NULL; | |
958 html->num_start = 0; | |
959 html->num_end = 0; | |
12 | 960 while (hbits) |
961 { | |
962 hb = (GtkHtmlBit *) hbits->data; | |
1 | 963 if (hb->was_selected) |
964 gtk_html_draw_bit(html, hb, 1); | |
965 hbits = hbits->prev; | |
966 } | |
967 hbits = g_list_last(html->html_bits); | |
968 } | |
12 | 969 } |
970 | |
971 return TRUE; | |
1 | 972 } |
973 | |
974 | |
975 | |
12 | 976 static void gtk_html_selection_get(GtkWidget * widget, |
977 GtkSelectionData * selection_data, | |
978 guint sel_info, guint32 time) | |
1 | 979 { |
980 gchar *str; | |
12 | 981 gint len; |
982 GtkHtml *html; | |
983 | |
984 g_return_if_fail(widget != NULL); | |
985 g_return_if_fail(GTK_IS_HTML(widget)); | |
986 | |
987 html = GTK_HTML(widget); | |
988 | |
989 | |
1 | 990 if (selection_data->selection != GDK_SELECTION_PRIMARY) |
991 return; | |
992 | |
993 str = html->selected_text; | |
994 | |
995 if (!str) | |
996 return; | |
12 | 997 |
1 | 998 len = strlen(str); |
999 | |
12 | 1000 if (sel_info == TARGET_STRING) |
1001 { | |
1 | 1002 gtk_selection_data_set(selection_data, |
12 | 1003 GDK_SELECTION_TYPE_STRING, |
1004 8 * sizeof(gchar), (guchar *) str, len); | |
1005 } | |
1006 else if ((sel_info == TARGET_TEXT) || (sel_info == TARGET_COMPOUND_TEXT)) | |
1007 { | |
1 | 1008 guchar *text; |
1009 GdkAtom encoding; | |
1010 gint format; | |
1011 gint new_length; | |
1012 | |
12 | 1013 gdk_string_to_compound_text(str, &encoding, &format, &text, |
1014 &new_length); | |
1015 gtk_selection_data_set(selection_data, encoding, format, text, | |
1016 new_length); | |
1017 gdk_free_compound_text(text); | |
1 | 1018 } |
1019 | |
1020 | |
1021 | |
1022 } | |
1023 | |
12 | 1024 static void do_select(GtkHtml * html, int x, int y) |
1 | 1025 { |
1026 GList *hbits = g_list_last(html->html_bits); | |
12 | 1027 int epos, |
1028 spos; | |
1 | 1029 GtkHtmlBit *hb; |
12 | 1030 |
1 | 1031 if (!hbits) |
1032 return; | |
12 | 1033 |
1034 hb = (GtkHtmlBit *) hbits->data; | |
1035 | |
1036 while (hbits) | |
1037 { | |
1038 hb = (GtkHtmlBit *) hbits->data; | |
1039 if (hb->type == HTML_BIT_TEXT) | |
1 | 1040 break; |
1041 hbits = hbits->prev; | |
12 | 1042 } |
1043 | |
1 | 1044 if (!hb) |
12 | 1045 return; |
1046 | |
1047 | |
1048 if (y > hb->y) | |
1049 { | |
1 | 1050 html->num_end = strlen(hb->text) - 1; |
1051 html->end_sel = hb; | |
12 | 1052 } |
1053 else if (y < 0) | |
1054 { | |
1 | 1055 html->num_end = 0; |
12 | 1056 html->end_sel = (GtkHtmlBit *) html->html_bits->data; |
1057 } | |
1058 else | |
1059 while (hbits) | |
1060 { | |
1061 hb = (GtkHtmlBit *) hbits->data; | |
1062 if ((y < hb->y && y > (hb->y - hb->height)) && | |
1063 (x > hb->x + hb->width)) | |
1064 { | |
1065 if (hb->type != HTML_BIT_TEXT) | |
1066 { | |
1067 html->num_end = 0; | |
1068 html->end_sel = hb; | |
1069 break; | |
1070 } | |
1071 | |
1072 html->num_end = strlen(hb->text) - 1; | |
1 | 1073 html->end_sel = hb; |
1074 break; | |
1075 } | |
12 | 1076 else if ((x > hb->x && x < (hb->x + hb->width)) && |
1077 (y < hb->y && y > (hb->y - hb->height))) | |
1078 { | |
1079 int i, | |
1080 len; | |
1081 int w = x - hb->x; | |
1082 | |
1083 if (hb->type != HTML_BIT_TEXT) | |
1084 { | |
1085 html->num_end = 0; | |
1 | 1086 html->end_sel = hb; |
1087 break; | |
1088 } | |
12 | 1089 |
1090 len = strlen(hb->text); | |
1091 | |
1092 for (i = 1; i <= len; i++) | |
1093 { | |
1094 if (gdk_text_measure(hb->font, hb->text, i) > w) | |
1095 { | |
1096 html->num_end = i - 1; | |
1097 html->end_sel = hb; | |
1098 break; | |
1099 } | |
1100 } | |
1101 break; | |
1 | 1102 } |
12 | 1103 hbits = hbits->prev; |
1 | 1104 } |
1105 | |
1106 if (html->end_sel == NULL) | |
1107 return; | |
12 | 1108 if (html->start_sel == NULL) |
1109 { | |
1 | 1110 html->start_sel = html->end_sel; |
1111 html->num_start = html->num_end; | |
1112 } | |
12 | 1113 |
1 | 1114 epos = g_list_index(html->html_bits, html->end_sel); |
1115 spos = g_list_index(html->html_bits, html->start_sel); | |
1116 g_free(html->selected_text); | |
1117 html->selected_text = NULL; | |
1118 | |
12 | 1119 if (epos == spos) |
1120 { | |
1 | 1121 char *str; |
12 | 1122 if (html->start_sel->type != HTML_BIT_TEXT) |
1123 { | |
1 | 1124 html->selected_text = NULL; |
1125 return; | |
1126 } | |
12 | 1127 if (html->num_end == html->num_start) |
1128 { | |
1 | 1129 str = g_malloc(2); |
12 | 1130 if (strlen(html->start_sel->text)) |
1 | 1131 str[0] = html->start_sel->text[html->num_end]; |
1132 else | |
12 | 1133 str[0] = 0; |
1 | 1134 str[1] = 0; |
1135 gtk_html_draw_bit(html, html->start_sel, 0); | |
1136 html->selected_text = str; | |
12 | 1137 } |
1138 else | |
1139 { | |
79 | 1140 size_t st, |
12 | 1141 en; |
1 | 1142 char *str; |
12 | 1143 if (html->num_end > html->num_start) |
1144 { | |
1 | 1145 en = html->num_end; |
1146 st = html->num_start; | |
12 | 1147 } |
1148 else | |
1149 { | |
1 | 1150 en = html->num_start; |
1151 st = html->num_end; | |
1152 } | |
1153 | |
1154 str = g_malloc(en - st + 2); | |
1155 strncpy(str, html->start_sel->text + st, (en - st + 1)); | |
1156 str[en - st + 1] = 0; | |
12 | 1157 gtk_html_draw_bit(html, html->start_sel, 0); |
1 | 1158 html->selected_text = str; |
12 | 1159 |
1 | 1160 } |
12 | 1161 } |
1162 else | |
1163 { | |
1164 GtkHtmlBit *shb, | |
1165 *ehb; | |
79 | 1166 size_t en, |
12 | 1167 st; |
1168 int len, | |
1169 nlen; | |
1 | 1170 char *str; |
12 | 1171 if (epos > spos) |
1172 { | |
1 | 1173 shb = html->start_sel; |
1174 ehb = html->end_sel; | |
1175 en = html->num_end; | |
1176 st = html->num_start; | |
12 | 1177 } |
1178 else | |
1179 { | |
1 | 1180 shb = html->end_sel; |
1181 ehb = html->start_sel; | |
1182 en = html->num_start; | |
1183 st = html->num_end; | |
1184 } | |
12 | 1185 |
1 | 1186 hbits = g_list_find(html->html_bits, shb); |
1187 | |
1188 if (!hbits) | |
1189 return; | |
12 | 1190 |
1191 if (shb->type == HTML_BIT_TEXT) | |
1192 { | |
1 | 1193 len = strlen(shb->text) - st + 1; |
1194 str = g_malloc(len); | |
1195 strcpy(str, shb->text + st); | |
1196 str[len - 1] = 0; | |
1197 gtk_html_draw_bit(html, shb, 0); | |
12 | 1198 if (shb->newline) |
1199 { | |
1200 len += 1; | |
1 | 1201 str = g_realloc(str, len); |
1202 str[len - 2] = '\n'; | |
1203 str[len - 1] = 0; | |
1204 } | |
12 | 1205 } |
1206 else | |
1207 { | |
1 | 1208 len = 1; |
1209 str = g_malloc(1); | |
1210 str[0] = 0; | |
1211 } | |
12 | 1212 if (hbits->next == NULL) |
1213 { | |
1 | 1214 html->selected_text = str; |
1215 return; | |
1216 } | |
1217 | |
12 | 1218 |
1219 hbits = hbits->next; | |
1220 while (1) | |
1221 { /* | |
1222 * Yah I know is dangerous :P | |
1223 */ | |
1224 hb = (GtkHtmlBit *) hbits->data; | |
1225 if (hb->type != HTML_BIT_TEXT) | |
1226 { | |
1 | 1227 if (hb == ehb) |
1228 break; | |
1229 hbits = hbits->next; | |
1230 continue; | |
1231 } | |
12 | 1232 if (hb != ehb) |
1233 { | |
1 | 1234 nlen = len + strlen(hb->text); |
1235 str = g_realloc(str, nlen); | |
1236 strcpy(str + (len - 1), hb->text); | |
1237 len = nlen; | |
1238 str[len - 1] = 0; | |
1239 gtk_html_draw_bit(html, hb, 0); | |
12 | 1240 if (hb->newline) |
1241 { | |
1242 len += 1; | |
1 | 1243 str = g_realloc(str, len); |
1244 str[len - 2] = '\n'; | |
1245 str[len - 1] = 0; | |
1246 } | |
12 | 1247 } |
1248 else | |
1249 { | |
1 | 1250 nlen = len + en + 1; |
1251 str = g_realloc(str, nlen); | |
1252 strncpy(str + (len - 1), hb->text, en + 1); | |
1253 len = nlen; | |
1254 str[len - 1] = 0; | |
12 | 1255 |
1 | 1256 gtk_html_draw_bit(html, hb, 0); |
12 | 1257 if (hb->newline && en == strlen(hb->text)) |
1258 { | |
1259 len += 1; | |
1 | 1260 str = g_realloc(str, len); |
1261 str[len - 2] = '\n'; | |
1262 str[len - 1] = 0; | |
1263 } | |
1264 break; | |
1265 } | |
1266 hbits = hbits->next; | |
1267 } | |
1268 html->selected_text = str; | |
1269 } | |
1270 | |
1271 } | |
1272 | |
12 | 1273 static gint scroll_timeout(GtkHtml * html) |
1 | 1274 { |
12 | 1275 GdkEventMotion event; |
1276 gint x, | |
1277 y; | |
1278 GdkModifierType mask; | |
1 | 1279 |
1280 html->timer = 0; | |
12 | 1281 gdk_window_get_pointer(html->html_area, &x, &y, &mask); |
1282 | |
1 | 1283 if (mask & GDK_BUTTON1_MASK) |
1284 { | |
1285 event.is_hint = 0; | |
1286 event.x = x; | |
1287 event.y = y; | |
1288 event.state = mask; | |
1289 | |
12 | 1290 gtk_html_motion_notify(GTK_WIDGET(html), &event); |
1 | 1291 } |
1292 | |
1293 return FALSE; | |
1294 | |
1295 } | |
1296 | |
1297 | |
12 | 1298 static gint gtk_html_tooltip_paint_window(GtkHtml * html) |
1 | 1299 { |
1300 GtkStyle *style; | |
12 | 1301 gint y, |
1302 baseline_skip, | |
1303 gap; | |
1 | 1304 |
1305 style = html->tooltip_window->style; | |
1306 | |
1307 gap = (style->font->ascent + style->font->descent) / 4; | |
1308 if (gap < 2) | |
1309 gap = 2; | |
1310 baseline_skip = style->font->ascent + style->font->descent + gap; | |
1311 | |
1312 if (!html->tooltip_hb) | |
1313 return FALSE; | |
1314 | |
1315 gtk_paint_flat_box(style, html->tooltip_window->window, | |
12 | 1316 GTK_STATE_NORMAL, GTK_SHADOW_OUT, |
1317 NULL, GTK_WIDGET(html->tooltip_window), "tooltip", | |
1318 0, 0, -1, -1); | |
1 | 1319 |
1320 y = style->font->ascent + 4; | |
1321 | |
12 | 1322 gtk_paint_string(style, html->tooltip_window->window, |
1323 GTK_STATE_NORMAL, | |
1324 NULL, GTK_WIDGET(html->tooltip_window), "tooltip", | |
1325 4, y, "HTML Link:"); | |
1 | 1326 y += baseline_skip; |
12 | 1327 gtk_paint_string(style, html->tooltip_window->window, |
1328 GTK_STATE_NORMAL, | |
1329 NULL, GTK_WIDGET(html->tooltip_window), "tooltip", | |
1330 4, y, html->tooltip_hb->url); | |
1331 | |
1 | 1332 return FALSE; |
1333 | |
1334 | |
1335 } | |
1336 | |
1337 static gint gtk_html_tooltip_timeout(gpointer data) | |
1338 { | |
12 | 1339 GtkHtml *html = (GtkHtml *) data; |
1340 | |
1341 | |
1 | 1342 GDK_THREADS_ENTER(); |
1343 | |
12 | 1344 if (html->tooltip_hb && GTK_WIDGET_DRAWABLE(GTK_WIDGET(html))) |
1345 { | |
1 | 1346 GtkWidget *widget; |
1347 GtkStyle *style; | |
12 | 1348 gint gap, |
1349 x, | |
1350 y, | |
1351 w, | |
1352 h, | |
1353 scr_w, | |
1354 scr_h, | |
1355 baseline_skip; | |
1 | 1356 |
1357 if (html->tooltip_window) | |
1358 gtk_widget_destroy(html->tooltip_window); | |
12 | 1359 |
1360 html->tooltip_window = gtk_window_new(GTK_WINDOW_POPUP); | |
1361 gtk_widget_set_app_paintable(html->tooltip_window, TRUE); | |
1362 gtk_window_set_policy(GTK_WINDOW(html->tooltip_window), FALSE, FALSE, | |
1363 TRUE); | |
1364 gtk_widget_set_name(html->tooltip_window, "gtk-tooltips"); | |
1365 gtk_signal_connect_object(GTK_OBJECT(html->tooltip_window), | |
1366 "expose_event", | |
1367 GTK_SIGNAL_FUNC | |
1368 (gtk_html_tooltip_paint_window), | |
1369 GTK_OBJECT(html)); | |
1370 gtk_signal_connect_object(GTK_OBJECT(html->tooltip_window), "draw", | |
1371 GTK_SIGNAL_FUNC | |
1372 (gtk_html_tooltip_paint_window), | |
1373 GTK_OBJECT(html)); | |
1374 | |
1375 gtk_widget_ensure_style(html->tooltip_window); | |
1 | 1376 style = html->tooltip_window->style; |
12 | 1377 |
1 | 1378 widget = GTK_WIDGET(html); |
1379 | |
12 | 1380 scr_w = gdk_screen_width(); |
1381 scr_h = gdk_screen_height(); | |
1 | 1382 |
1383 gap = (style->font->ascent + style->font->descent) / 4; | |
1384 if (gap < 2) | |
1385 gap = 2; | |
1386 baseline_skip = style->font->ascent + style->font->descent + gap; | |
1387 | |
1388 w = 8 + MAX(gdk_string_width(style->font, "HTML Link:"), | |
12 | 1389 gdk_string_width(style->font, html->tooltip_hb->url)); |
1390 ; | |
1 | 1391 h = 8 - gap; |
12 | 1392 h += (baseline_skip * 2); |
1393 | |
1394 gdk_window_get_pointer(NULL, &x, &y, NULL); | |
1395 /* | |
1396 * gdk_window_get_origin (widget->window, NULL, &y); | |
1397 */ | |
1398 if (GTK_WIDGET_NO_WINDOW(widget)) | |
1 | 1399 y += widget->allocation.y; |
1400 | |
1401 x -= ((w >> 1) + 4); | |
1402 | |
1403 if ((x + w) > scr_w) | |
1404 x -= (x + w) - scr_w; | |
1405 else if (x < 0) | |
1406 x = 0; | |
1407 | |
1408 if ((y + h + 4) > scr_h) | |
12 | 1409 y = |
1410 y - html->tooltip_hb->font->ascent + | |
1411 html->tooltip_hb->font->descent; | |
1 | 1412 else |
12 | 1413 y = |
1414 y + html->tooltip_hb->font->ascent + | |
1415 html->tooltip_hb->font->descent; | |
1416 | |
1417 gtk_widget_set_usize(html->tooltip_window, w, h); | |
1418 gtk_widget_popup(html->tooltip_window, x, y); | |
1419 | |
1 | 1420 } |
1421 | |
1422 html->tooltip_timer = -1; | |
12 | 1423 |
1 | 1424 GDK_THREADS_LEAVE(); |
1425 | |
1426 return FALSE; | |
1427 } | |
1428 | |
1429 | |
12 | 1430 static gint gtk_html_leave_notify(GtkWidget * widget, GdkEventCrossing * event) |
1 | 1431 { |
12 | 1432 GtkHtml *html; |
1433 | |
1434 html = GTK_HTML(widget); | |
1435 | |
1436 if (html->tooltip_timer != -1) | |
1437 gtk_timeout_remove(html->tooltip_timer); | |
1438 if (html->tooltip_window) | |
1439 { | |
1440 gtk_widget_destroy(html->tooltip_window); | |
1441 html->tooltip_window = NULL; | |
1442 } | |
1443 | |
1444 | |
1445 html->tooltip_hb = NULL; | |
1446 return TRUE; | |
1 | 1447 } |
1448 | |
1449 | |
12 | 1450 static gint gtk_html_motion_notify(GtkWidget * widget, GdkEventMotion * event) |
1 | 1451 { |
12 | 1452 int x, |
1453 y; | |
1454 gint width, | |
1455 height; | |
1456 GdkModifierType state; | |
1457 int realx, | |
1458 realy; | |
1459 GtkHtml *html = GTK_HTML(widget); | |
1460 | |
1461 if (event->is_hint) | |
1462 gdk_window_get_pointer(event->window, &x, &y, &state); | |
1463 else | |
1464 { | |
1465 x = event->x; | |
1466 y = event->y; | |
1467 state = event->state; | |
1468 } | |
1469 | |
1470 gdk_window_get_size(html->html_area, &width, &height); | |
1471 | |
1 | 1472 realx = x; |
1473 realy = y + html->yoffset; | |
1474 | |
1475 | |
12 | 1476 if (state & GDK_BUTTON1_MASK) |
1477 { | |
1478 if (realx != html->start_sel_x || realy != html->start_sel_y) | |
1479 { | |
1 | 1480 char *tmp = NULL; |
1481 | |
12 | 1482 if (y < 0 || y > height) |
1483 { | |
1 | 1484 int diff; |
12 | 1485 if (html->timer == 0) |
1486 { | |
1 | 1487 html->timer = gtk_timeout_add(100, |
12 | 1488 (GtkFunction) scroll_timeout, |
1489 html); | |
1 | 1490 if (y < 0) |
1491 diff = y / 2; | |
1492 else | |
1493 diff = (y - height) / 2; | |
1494 | |
1495 if (html->vadj->value + diff > | |
12 | 1496 html->vadj->upper - height + 20) |
1 | 1497 gtk_adjustment_set_value(html->vadj, |
12 | 1498 html->vadj->upper - height + |
1499 20); | |
1 | 1500 else |
1501 gtk_adjustment_set_value(html->vadj, | |
12 | 1502 html->vadj->value + diff); |
1 | 1503 |
1504 } | |
1505 } | |
12 | 1506 |
1 | 1507 if (html->selected_text != NULL) |
1508 tmp = g_strdup(html->selected_text); | |
1509 do_select(html, realx, realy); | |
12 | 1510 if (tmp) |
1511 { | |
1512 if (!html->selected_text || strcmp(tmp, html->selected_text)) | |
1513 { | |
1 | 1514 GtkHtmlBit *hb; |
1515 GList *hbits = html->html_bits; | |
12 | 1516 while (hbits) |
1517 { | |
1518 hb = (GtkHtmlBit *) hbits->data; | |
1 | 1519 if (hb->was_selected) |
1520 gtk_html_draw_bit(html, hb, 0); | |
1521 hbits = hbits->next; | |
1522 } | |
1523 } | |
1524 g_free(tmp); | |
1525 } | |
1526 } | |
12 | 1527 } |
1528 else | |
1529 { | |
1 | 1530 GtkHtmlBit *hb; |
1531 GList *urls; | |
1532 | |
1533 urls = html->urls; | |
12 | 1534 while (urls) |
1535 { | |
1536 hb = (GtkHtmlBit *) urls->data; | |
1 | 1537 if ((realx > hb->x && realx < (hb->x + hb->width)) && |
12 | 1538 (realy < hb->y && realy > (hb->y - hb->height))) |
1539 { | |
26 | 1540 GdkCursor *cursor = NULL; |
1541 | |
12 | 1542 if (html->tooltip_hb != hb) |
1543 { | |
1 | 1544 html->tooltip_hb = hb; |
1545 if (html->tooltip_timer != -1) | |
1546 gtk_timeout_remove(html->tooltip_timer); | |
12 | 1547 if (html->tooltip_window) |
1548 { | |
1 | 1549 gtk_widget_destroy(html->tooltip_window); |
1550 html->tooltip_window = NULL; | |
1551 } | |
12 | 1552 html->tooltip_timer = |
1553 gtk_timeout_add(HTML_TOOLTIP_DELAY, | |
1554 gtk_html_tooltip_timeout, html); | |
1 | 1555 } |
26 | 1556 |
1557 cursor = gdk_cursor_new(GDK_HAND2); | |
1558 gdk_window_set_cursor(html->html_area, cursor); | |
1559 gdk_cursor_destroy(cursor); | |
1560 | |
1 | 1561 return TRUE; |
1562 } | |
12 | 1563 urls = urls->next; |
1 | 1564 } |
1565 if (html->tooltip_timer != -1) | |
1566 gtk_timeout_remove(html->tooltip_timer); | |
12 | 1567 if (html->tooltip_window) |
1568 { | |
1 | 1569 gtk_widget_destroy(html->tooltip_window); |
1570 html->tooltip_window = NULL; | |
1571 } | |
12 | 1572 |
1573 | |
1574 html->tooltip_hb = NULL; | |
1 | 1575 gdk_window_set_cursor(html->html_area, NULL); |
1576 | |
1577 | |
1578 } | |
1579 | |
1580 return TRUE; | |
1581 } | |
1582 | |
12 | 1583 static gint gtk_html_button_release(GtkWidget * widget, GdkEventButton * event) |
1 | 1584 { |
12 | 1585 GtkHtml *html; |
1586 | |
1587 html = GTK_HTML(widget); | |
1588 | |
1589 if (html->frozen > 0) | |
1590 return TRUE; | |
1591 | |
1592 if (event->button == 1) | |
1593 { | |
1594 int realx, | |
1595 realy; | |
1596 GtkHtmlBit *hb; | |
1597 GList *urls = html->urls; | |
1598 | |
1599 realx = event->x; | |
1600 realy = event->y + html->yoffset; | |
1601 if (realx != html->start_sel_x || realy != html->start_sel_y) | |
1602 { | |
1603 if (gtk_selection_owner_set(widget, | |
1604 GDK_SELECTION_PRIMARY, event->time)) | |
1605 { | |
1606 } | |
1607 else | |
1608 { | |
1609 } | |
1610 } | |
1611 else | |
1612 { | |
1613 if (gdk_selection_owner_get(GDK_SELECTION_PRIMARY) == | |
1614 widget->window) | |
1 | 1615 gtk_selection_owner_set(NULL, GDK_SELECTION_PRIMARY, |
12 | 1616 event->time); |
1617 | |
1618 | |
1619 while (urls) | |
1620 { | |
1621 void open_url_nw(GtkWidget * w, char *url); | |
1622 hb = (GtkHtmlBit *) urls->data; | |
1 | 1623 if ((realx > hb->x && realx < (hb->x + hb->width)) && |
12 | 1624 (realy < hb->y && realy > (hb->y - hb->height))) |
1625 { | |
1626 open_url_nw(NULL, hb->url); | |
1627 // else | |
1628 // open_url(NULL, hb->url); | |
1 | 1629 break; |
1630 } | |
1631 urls = urls->next; | |
1632 } | |
1633 } | |
1634 } | |
1635 return TRUE; | |
1636 } | |
1637 | |
1638 | |
1639 | |
12 | 1640 static gint gtk_html_button_press(GtkWidget * widget, GdkEventButton * event) |
1 | 1641 { |
12 | 1642 GtkHtml *html; |
1643 gfloat value; | |
1644 | |
1645 | |
1646 html = GTK_HTML(widget); | |
1647 value = html->vadj->value; | |
1648 | |
1649 if (html->frozen > 0) | |
1650 return TRUE; | |
1651 | |
1652 if (event->button == 4) | |
1653 { | |
1 | 1654 value -= html->vadj->step_increment; |
1655 if (value < html->vadj->lower) | |
1656 value = html->vadj->lower; | |
12 | 1657 gtk_adjustment_set_value(html->vadj, value); |
1658 } | |
1659 else if (event->button == 5) | |
1660 { | |
1 | 1661 value += html->vadj->step_increment; |
1662 if (value > html->vadj->upper) | |
1663 value = html->vadj->upper; | |
12 | 1664 gtk_adjustment_set_value(html->vadj, value); |
1665 | |
1666 } | |
1667 else if (event->button == 1) | |
1668 { | |
1669 GList *hbits = g_list_last(html->html_bits); | |
1670 int realx, | |
1671 realy; | |
1 | 1672 GtkHtmlBit *hb; |
1673 | |
1674 realx = event->x; | |
1675 realy = event->y + html->yoffset; | |
1676 | |
1677 html->start_sel_x = realx; | |
1678 html->start_sel_y = realy; | |
1679 | |
1680 if (!hbits) | |
1681 return TRUE; | |
1682 | |
12 | 1683 if (html->selected_text) |
1684 { | |
1 | 1685 g_free(html->selected_text); |
1686 html->selected_text = NULL; | |
1687 html->start_sel = NULL; | |
1688 html->end_sel = NULL; | |
1689 html->num_start = 0; | |
1690 html->num_end = 0; | |
12 | 1691 while (hbits) |
1692 { | |
1693 hb = (GtkHtmlBit *) hbits->data; | |
1 | 1694 if (hb->was_selected) |
1695 gtk_html_draw_bit(html, hb, 1); | |
1696 hbits = hbits->prev; | |
1697 } | |
1698 hbits = g_list_last(html->html_bits); | |
1699 } | |
1700 | |
12 | 1701 hb = (GtkHtmlBit *) hbits->data; |
1702 if (realy > hb->y) | |
1703 { | |
1 | 1704 if (hb->text) |
1705 html->num_start = strlen(hb->text) - 1; | |
1706 else | |
12 | 1707 html->num_start = 0; |
1 | 1708 html->start_sel = hb; |
12 | 1709 } |
1710 else | |
1711 while (hbits) | |
1712 { | |
1713 hb = (GtkHtmlBit *) hbits->data; | |
1714 if ((realy < hb->y && realy > (hb->y - hb->height)) && | |
1715 (realx > hb->x + hb->width)) | |
1716 { | |
1717 if (hb->type != HTML_BIT_TEXT) | |
1718 { | |
1719 html->num_end = 0; | |
1720 html->end_sel = hb; | |
1721 break; | |
1722 } | |
1723 | |
1724 if (hb->text) | |
1725 html->num_start = strlen(hb->text) - 1; | |
1726 else | |
1727 html->num_start = 0; | |
1728 | |
1729 html->start_sel = hb; | |
1 | 1730 break; |
1731 } | |
12 | 1732 else if ((realx > hb->x && realx < (hb->x + hb->width)) && |
1733 (realy < hb->y && realy > (hb->y - hb->height))) | |
1734 { | |
1735 int i, | |
1736 len; | |
1737 int w = realx - hb->x; | |
1738 | |
1739 if (hb->type != HTML_BIT_TEXT) | |
1740 { | |
1741 html->num_end = 0; | |
1742 html->end_sel = hb; | |
1743 break; | |
1744 } | |
1745 | |
1746 if (hb->text) | |
1747 len = strlen(hb->text); | |
1748 else | |
1749 len = 0; | |
1750 | |
1751 for (i = 1; i <= len; i++) | |
1752 { | |
1753 if (gdk_text_measure(hb->font, hb->text, i) > w) | |
1754 { | |
1755 html->num_start = i - 1; | |
1756 html->start_sel = hb; | |
1757 break; | |
1758 } | |
1759 } | |
1 | 1760 break; |
1761 } | |
12 | 1762 hbits = hbits->prev; |
1 | 1763 } |
12 | 1764 } |
1765 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS) | |
1766 { | |
1 | 1767 GtkHtmlBit *hb = NULL; |
12 | 1768 int realx, |
1769 realy; | |
1770 GList *urls; | |
1771 | |
1 | 1772 realx = event->x; |
1773 realy = event->y + html->yoffset; | |
12 | 1774 |
1 | 1775 urls = html->urls; |
12 | 1776 while (urls) |
1777 { | |
1778 hb = (GtkHtmlBit *) urls->data; | |
1 | 1779 if ((realx > hb->x && realx < (hb->x + hb->width)) && |
12 | 1780 (realy < hb->y && realy > (hb->y - hb->height))) |
1781 { | |
1 | 1782 break; |
1783 } | |
12 | 1784 urls = urls->next; |
1 | 1785 hb = NULL; |
1786 } | |
12 | 1787 |
1788 if (hb != NULL) | |
1789 { | |
69 | 1790 |
1791 GtkWidget *menu, *button; | |
1792 | |
1793 menu = gtk_menu_new(); | |
1794 | |
1795 if (web_browser == BROWSER_NETSCAPE) { | |
1796 | |
1797 button = gtk_menu_item_new_with_label("Open URL in existing window"); | |
1798 gtk_signal_connect(GTK_OBJECT(button), "activate", | |
1799 GTK_SIGNAL_FUNC(open_url), hb->url); | |
1800 gtk_menu_append(GTK_MENU(menu), button); | |
1801 gtk_widget_show(button); | |
1802 | |
1803 } | |
1804 | |
1805 | |
1806 button = gtk_menu_item_new_with_label("Open URL in new window"); | |
1807 gtk_signal_connect(GTK_OBJECT(button), "activate", | |
1808 GTK_SIGNAL_FUNC(open_url_nw), hb->url); | |
1809 gtk_menu_append(GTK_MENU(menu), button); | |
1810 gtk_widget_show(button); | |
1811 | |
1812 if (web_browser == BROWSER_NETSCAPE) { | |
1813 | |
1814 button = gtk_menu_item_new_with_label("Add URL as bookmark"); | |
1815 gtk_signal_connect(GTK_OBJECT(button), "activate", | |
1816 GTK_SIGNAL_FUNC(add_bookmark), hb->url); | |
1817 gtk_menu_append(GTK_MENU(menu), button); | |
1818 gtk_widget_show(button); | |
1819 | |
1820 } | |
1821 | |
1822 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | |
1823 event->button, event->time); | |
1824 | |
12 | 1825 } |
1 | 1826 } |
12 | 1827 |
1 | 1828 return TRUE; |
1829 } | |
1830 | |
1831 | |
12 | 1832 static void gtk_html_draw_bit(GtkHtml * html, GtkHtmlBit * hb, int redraw) |
1 | 1833 { |
12 | 1834 int mypos, |
1835 epos, | |
1836 spos; | |
1837 GdkGC *gc = html->gc; | |
1 | 1838 int shift; |
12 | 1839 GtkStateType selected_state; |
1840 GtkWidget *widget = GTK_WIDGET(html); | |
1 | 1841 GdkRectangle area; |
1842 | |
1843 if (html->frozen > 0) | |
1844 return; | |
1845 | |
12 | 1846 if (hb->type == HTML_BIT_TEXT) |
1847 { | |
1 | 1848 |
1849 if (!strlen(hb->text)) | |
1850 return; | |
12 | 1851 |
1 | 1852 mypos = g_list_index(html->html_bits, hb); |
1853 epos = g_list_index(html->html_bits, html->end_sel); | |
1854 spos = g_list_index(html->html_bits, html->start_sel); | |
1855 | |
12 | 1856 if (((html->end_sel == NULL) || (html->start_sel == NULL)) || |
1857 ((epos < mypos) && (spos < mypos)) || | |
1858 ((epos > mypos) && (spos > mypos))) | |
1859 { | |
1860 selected_state = GTK_STATE_NORMAL; | |
1861 } | |
1862 else | |
1863 { | |
1 | 1864 selected_state = GTK_STATE_SELECTED; |
1865 } | |
1866 | |
1867 | |
1868 gdk_text_extents(hb->font, hb->text, 1, &shift, NULL, NULL, NULL, NULL); | |
1869 | |
12 | 1870 if (selected_state == GTK_STATE_SELECTED) |
1871 { | |
1872 int schar = 0, | |
1873 echar = 0; | |
1874 int startx = 0, | |
1875 xwidth = 0; | |
1876 | |
1877 if (epos > spos || | |
1878 (epos == spos && html->num_end >= html->num_start)) | |
1879 { | |
1880 if (mypos == epos) | |
1881 { | |
1 | 1882 echar = html->num_end; |
12 | 1883 xwidth = |
1884 gdk_text_width(hb->font, hb->text, html->num_end + 1); | |
1885 } | |
1886 else | |
1887 { | |
1 | 1888 echar = strlen(hb->text); |
1889 xwidth = hb->width; | |
1890 } | |
12 | 1891 if (mypos == spos) |
1892 { | |
1 | 1893 schar = html->num_start; |
12 | 1894 startx = |
1895 gdk_text_width(hb->font, hb->text, html->num_start); | |
1 | 1896 xwidth -= startx; |
1897 } | |
12 | 1898 } |
1899 else | |
1900 { | |
1901 if (mypos == spos) | |
1902 { | |
1 | 1903 echar = html->num_start; |
12 | 1904 xwidth = |
1905 gdk_text_width(hb->font, hb->text, | |
1906 html->num_start + 1); | |
1907 } | |
1908 else | |
1909 { | |
1 | 1910 echar = strlen(hb->text); |
1911 xwidth = hb->width; | |
1912 } | |
12 | 1913 if (mypos == epos) |
1914 { | |
1 | 1915 schar = html->num_end; |
12 | 1916 startx = |
1917 gdk_text_width(hb->font, hb->text, html->num_end); | |
1 | 1918 xwidth -= startx; |
1919 } | |
1920 } | |
1921 | |
1922 if (!redraw && echar == hb->sel_e && schar == hb->sel_s) | |
1923 return; | |
12 | 1924 |
1 | 1925 hb->sel_e = echar; |
1926 hb->sel_s = schar; | |
12 | 1927 |
1 | 1928 startx += hb->x; |
1929 | |
1930 | |
12 | 1931 area.x = hb->x - html->xoffset; |
1932 area.y = hb->y - hb->height + 3 - html->yoffset; | |
1933 area.width = hb->width + 2; | |
1934 area.height = hb->height; | |
1935 clear_area(html, &area); | |
1936 | |
1937 gtk_paint_flat_box(widget->style, html->html_area, | |
1938 selected_state, GTK_SHADOW_NONE, | |
1939 NULL, widget, "text", | |
1940 startx, | |
1941 hb->y - hb->height + 3 - html->yoffset, | |
1942 xwidth + 2, hb->height); | |
1943 hb->was_selected = 1; | |
1944 } | |
1945 else if (hb->was_selected) | |
1946 { | |
1947 area.x = hb->x - html->xoffset; | |
1948 area.y = hb->y - hb->height + 3 - html->yoffset; | |
1949 area.width = hb->width + 2; | |
1950 area.height = hb->height; | |
1951 clear_area(html, &area); | |
1952 | |
1953 hb->sel_e = -1; | |
1954 hb->sel_s = -1; | |
1955 | |
1 | 1956 hb->was_selected = 0; |
1957 } | |
12 | 1958 |
1959 | |
1960 | |
1961 | |
1962 if (selected_state == GTK_STATE_SELECTED && (mypos == epos | |
1963 || mypos == spos)) | |
1964 { | |
1 | 1965 char *s = hb->text; |
12 | 1966 int num = 0, |
1967 width = 0, | |
1968 fsel = 0, | |
1969 esel = strlen(hb->text); | |
1970 int lbearing, | |
1971 rbearing, | |
1972 w; | |
1973 | |
1974 if (epos > spos || | |
1975 (epos == spos && html->num_end >= html->num_start)) | |
1976 { | |
1 | 1977 if (mypos == epos) |
1978 esel = html->num_end; | |
1979 if (mypos == spos) | |
1980 fsel = html->num_start; | |
12 | 1981 } |
1982 else | |
1983 { | |
1 | 1984 if (mypos == spos) |
1985 esel = html->num_start; | |
12 | 1986 if (mypos == epos) |
1987 fsel = html->num_end; | |
1 | 1988 } |
1989 | |
12 | 1990 while (*s) |
1991 { | |
1 | 1992 |
1993 if (num < fsel || num > esel) | |
1994 selected_state = GTK_STATE_NORMAL; | |
1995 else | |
1996 selected_state = GTK_STATE_SELECTED; | |
1997 if (hb->fore != NULL) | |
1998 gdk_gc_set_foreground(gc, hb->fore); | |
1999 else | |
12 | 2000 gdk_gc_set_foreground(gc, |
2001 &widget->style->fg[selected_state]); | |
1 | 2002 if (hb->back != NULL) |
2003 gdk_gc_set_background(gc, hb->back); | |
2004 else | |
12 | 2005 gdk_gc_set_background(gc, |
2006 &widget->style->bg[selected_state]); | |
1 | 2007 |
2008 | |
2009 gdk_gc_set_font(gc, hb->font); | |
2010 | |
12 | 2011 gdk_text_extents(hb->font, s, 1, &lbearing, &rbearing, &w, NULL, |
2012 NULL); | |
2013 | |
2014 gdk_draw_text(html->html_area, hb->font, gc, | |
2015 shift + hb->x + width, hb->y - html->yoffset, s, | |
2016 1); | |
2017 | |
2018 if (hb->uline) | |
2019 gdk_draw_line(html->html_area, gc, shift + hb->x + width, | |
2020 hb->y - html->yoffset, | |
2021 shift + hb->x + width + w, | |
2022 hb->y - html->yoffset); | |
1 | 2023 |
2024 if (hb->strike) | |
12 | 2025 gdk_draw_line(html->html_area, gc, shift + hb->x + width, |
2026 hb->y - html->yoffset - (hb->height / 3), | |
2027 shift + hb->x + width + w, | |
2028 hb->y - html->yoffset - (hb->height / 3)); | |
1 | 2029 |
2030 width += w; | |
12 | 2031 |
1 | 2032 s++; |
2033 num++; | |
2034 } | |
2035 | |
2036 | |
12 | 2037 } |
2038 else | |
2039 { | |
2040 /*my stuff here*/ | |
2041 | |
2042 if(!hb->was_selected) | |
2043 { | |
2044 area.x = hb->x - html->xoffset; | |
2045 area.y = hb->y - hb->height + 3 - html->yoffset; | |
2046 area.width = hb->width + 2; | |
2047 area.height = hb->height; | |
2048 clear_area(html, &area); | |
2049 } | |
2050 | |
2051 /*end my stuff*/ | |
2052 | |
1 | 2053 |
2054 if (hb->fore != NULL) | |
2055 gdk_gc_set_foreground(gc, hb->fore); | |
2056 else | |
12 | 2057 gdk_gc_set_foreground(gc, &widget->style->fg[selected_state]); |
1 | 2058 if (hb->back != NULL) |
2059 gdk_gc_set_background(gc, hb->back); | |
2060 else | |
2061 gdk_gc_set_background(gc, &widget->style->bg[selected_state]); | |
2062 | |
2063 | |
2064 gdk_gc_set_font(gc, hb->font); | |
2065 | |
12 | 2066 |
2067 gdk_draw_string(html->html_area, hb->font, gc, shift + hb->x, | |
2068 hb->y - html->yoffset, hb->text); | |
1 | 2069 if (hb->uline) |
12 | 2070 gdk_draw_line(html->html_area, gc, shift + hb->x, |
2071 hb->y - html->yoffset, | |
2072 hb->x + gdk_string_measure(hb->font, hb->text), | |
2073 hb->y - html->yoffset); | |
1 | 2074 |
2075 if (hb->strike) | |
12 | 2076 gdk_draw_line(html->html_area, gc, shift + hb->x, |
2077 hb->y - html->yoffset - (hb->height / 3), | |
2078 hb->x + gdk_string_measure(hb->font, hb->text), | |
2079 hb->y - html->yoffset - (hb->height / 3)); | |
1 | 2080 |
2081 } | |
12 | 2082 } |
2083 else if (hb->type == HTML_BIT_SEP) | |
2084 { | |
2085 | |
2086 gdk_draw_line(html->html_area, gc, hb->x + 2, | |
2087 hb->y - html->yoffset - (hb->height / 2 - 1), | |
2088 hb->x + hb->width, | |
2089 hb->y - html->yoffset - (hb->height / 2 - 1)); | |
2090 | |
2091 } | |
2092 else if (hb->type == HTML_BIT_PIXMAP) | |
2093 { | |
1 | 2094 gdk_gc_set_background(gc, &widget->style->base[GTK_STATE_NORMAL]); |
12 | 2095 gdk_draw_pixmap(html->html_area, gc, hb->pm, 0, 0, hb->x, |
2096 hb->y - html->yoffset - (hb->height) + 4, hb->width, | |
2097 hb->height - 2); | |
1 | 2098 } |
2099 } | |
2100 | |
2101 | |
2102 | |
12 | 2103 gint compare_types(GtkHtmlBit * hb, GtkHtmlBit * hb2) |
1 | 2104 { |
12 | 2105 /* |
2106 * In this function, it's OK to accidently return a | |
2107 * * 0, but will cause problems on an accidental 1 | |
2108 */ | |
1 | 2109 |
2110 if (!hb || !hb2) | |
2111 return 0; | |
12 | 2112 |
2113 | |
1 | 2114 if (hb->uline != hb2->uline) |
2115 return 0; | |
2116 if (hb->strike != hb2->strike) | |
12 | 2117 return 0; |
2118 if (hb->font && hb2->font) | |
2119 { | |
1 | 2120 if (!gdk_font_equal(hb->font, hb2->font)) |
2121 return 0; | |
12 | 2122 } |
2123 else if (hb->font && !hb2->font) | |
2124 { | |
1 | 2125 return 0; |
12 | 2126 } |
2127 else if (!hb->font && hb2->font) | |
2128 { | |
1 | 2129 return 0; |
2130 } | |
2131 if (hb->type != hb2->type) | |
2132 return 0; | |
12 | 2133 |
2134 if (hb->fore && hb2->fore) | |
2135 { | |
1 | 2136 if (!gdk_color_equal(hb->fore, hb2->fore)) |
2137 return 0; | |
12 | 2138 } |
2139 else if (hb->fore && !hb2->fore) | |
2140 { | |
1 | 2141 return 0; |
12 | 2142 } |
2143 else if (!hb->fore && hb2->fore) | |
2144 { | |
1 | 2145 return 0; |
2146 } | |
2147 | |
12 | 2148 if (hb->back && hb2->back) |
2149 { | |
1 | 2150 if (!gdk_color_equal(hb->back, hb2->back)) |
2151 return 0; | |
12 | 2152 } |
2153 else if (hb->back && !hb2->back) | |
2154 { | |
1 | 2155 return 0; |
12 | 2156 } |
2157 else if (!hb->back && hb2->back) | |
2158 { | |
1 | 2159 return 0; |
2160 } | |
2161 | |
2162 if ((hb->url != NULL && hb2->url == NULL) || | |
12 | 2163 (hb->url == NULL && hb2->url != NULL)) |
1 | 2164 return 0; |
12 | 2165 |
2166 if (hb->url != NULL && hb2->url != NULL) | |
1 | 2167 if (strcasecmp(hb->url, hb2->url)) |
2168 return 0; | |
12 | 2169 |
1 | 2170 return 1; |
2171 } | |
2172 | |
12 | 2173 static gint html_bit_is_onscreen(GtkHtml * html, GtkHtmlBit * hb) |
1 | 2174 { |
12 | 2175 gint width, |
2176 height; | |
1 | 2177 |
2178 gdk_window_get_size(html->html_area, &width, &height); | |
12 | 2179 |
2180 if (hb->y < html->yoffset) | |
2181 { | |
1 | 2182 return 0; |
2183 } | |
2184 | |
12 | 2185 if ((hb->y - hb->height) > (html->yoffset + height)) |
2186 { | |
1 | 2187 return 0; |
2188 } | |
2189 return 1; | |
2190 } | |
2191 | |
12 | 2192 static void draw_cursor(GtkHtml * html) |
1 | 2193 { |
12 | 2194 if (html->editable && |
2195 html->cursor_hb && | |
2196 GTK_WIDGET_DRAWABLE(html) && | |
2197 html_bit_is_onscreen(html, html->cursor_hb)) | |
2198 { | |
2199 gint x, | |
2200 y; | |
1 | 2201 gint width; |
2202 | |
2203 GdkFont *font = html->cursor_hb->font; | |
2204 | |
12 | 2205 gdk_text_extents(font, html->cursor_hb->text, html->cursor_pos, NULL, |
2206 NULL, &width, NULL, NULL); | |
2207 | |
2208 gdk_gc_set_foreground(html->gc, | |
2209 >K_WIDGET(html)->style->text[GTK_STATE_NORMAL]); | |
1 | 2210 |
2211 y = html->cursor_hb->y - html->yoffset; | |
2212 x = html->cursor_hb->x + width; | |
2213 | |
2214 | |
12 | 2215 gdk_draw_line(html->html_area, html->gc, x, y, x, y - font->ascent); |
1 | 2216 |
2217 } | |
2218 } | |
2219 | |
12 | 2220 static void undraw_cursor(GtkHtml * html) |
1 | 2221 { |
12 | 2222 if (html->editable && |
2223 html->cursor_hb && | |
2224 GTK_WIDGET_DRAWABLE(html) && | |
2225 html_bit_is_onscreen(html, html->cursor_hb)) | |
2226 { | |
2227 gint x, | |
2228 y; | |
1 | 2229 gint width; |
12 | 2230 GdkRectangle area; |
2231 | |
1 | 2232 GdkFont *font = html->cursor_hb->font; |
2233 | |
12 | 2234 gdk_text_extents(font, html->cursor_hb->text, html->cursor_pos, NULL, |
2235 NULL, &width, NULL, NULL); | |
1 | 2236 |
2237 y = html->cursor_hb->y - html->yoffset; | |
2238 x = html->cursor_hb->x + width; | |
2239 | |
2240 area.x = x; | |
2241 area.y = y - font->ascent; | |
2242 area.height = font->ascent + 1; | |
2243 area.width = 1; | |
2244 | |
2245 | |
12 | 2246 clear_area(html, &area); |
1 | 2247 |
2248 gtk_html_draw_bit(html, html->cursor_hb, 1); | |
12 | 2249 |
2250 | |
2251 } | |
1 | 2252 } |
2253 | |
2254 | |
12 | 2255 static void expose_html(GtkHtml * html, GdkRectangle * area, gboolean cursor) |
1 | 2256 { |
12 | 2257 GList *hbits; |
2258 GtkHtmlBit *hb; | |
2259 gint width, | |
2260 height; | |
2261 gint realy; | |
2262 | |
2263 | |
2264 if (html->frozen > 0) | |
2265 return; | |
2266 | |
2267 | |
2268 hbits = html->html_bits; | |
1 | 2269 |
2270 gdk_window_get_size(html->html_area, &width, &height); | |
2271 | |
12 | 2272 realy = area->y + html->yoffset; |
2273 | |
2274 clear_area(html, area); | |
2275 | |
2276 while (hbits) | |
2277 { | |
2278 | |
2279 hb = (GtkHtmlBit *) hbits->data; | |
1 | 2280 |
2281 if (html_bit_is_onscreen(html, hb)) | |
12 | 2282 gtk_html_draw_bit(html, hb, 1); |
2283 | |
2284 | |
2285 hbits = hbits->next; | |
2286 } | |
1 | 2287 } |
2288 | |
12 | 2289 static void resize_html(GtkHtml * html) |
1 | 2290 { |
2291 GList *hbits = html->html_bits; | |
2292 GList *html_bits = html->html_bits; | |
12 | 2293 GtkHtmlBit *hb, |
2294 *hb2; | |
1 | 2295 char *str; |
2296 gint height; | |
2297 | |
12 | 2298 if (!hbits) |
2299 return; | |
2300 | |
2301 | |
2302 html->html_bits = NULL; | |
2303 | |
2304 html->current_x = 0; | |
1 | 2305 html->current_y = 0; |
2306 | |
12 | 2307 html->vadj->upper = 0; |
2308 | |
2309 gtk_html_freeze(html); | |
2310 | |
2311 while (hbits) | |
2312 { | |
2313 hb = (GtkHtmlBit *) hbits->data; | |
2314 if (hb->type == HTML_BIT_SEP) | |
2315 { | |
2316 | |
1 | 2317 gtk_html_add_seperator(html); |
2318 | |
2319 g_free(hb); | |
2320 | |
2321 hbits = hbits->next; | |
2322 continue; | |
2323 } | |
12 | 2324 if (hb->type == HTML_BIT_PIXMAP) |
2325 { | |
1 | 2326 |
2327 gtk_html_add_pixmap(html, hb->pm, hb->fit); | |
2328 | |
2329 g_free(hb); | |
2330 | |
2331 hbits = hbits->next; | |
2332 continue; | |
2333 } | |
12 | 2334 |
2335 if (hb->newline) | |
2336 { | |
1 | 2337 int i; |
2338 | |
12 | 2339 if (!hb->text) |
2340 { | |
1 | 2341 hb->text = g_malloc(1); |
2342 hb->text[0] = 0; | |
2343 } | |
12 | 2344 for (i = 0; i < hb->newline; i++) |
2345 { | |
2346 str = hb->text; | |
1 | 2347 hb->text = g_strconcat(str, "\n", NULL); |
2348 g_free(str); | |
2349 } | |
2350 } | |
2351 | |
12 | 2352 if (hbits->next) |
2353 { | |
2354 hb2 = (GtkHtmlBit *) hbits->next->data; | |
2355 } | |
2356 else | |
2357 { | |
2358 hb2 = NULL; | |
2359 } | |
2360 | |
2361 | |
2362 | |
2363 if (!hb->newline && compare_types(hb, hb2)) | |
2364 { | |
1 | 2365 str = hb2->text; |
2366 hb2->text = g_strconcat(hb->text, hb2->text, NULL); | |
2367 g_free(str); | |
2368 hb2 = NULL; | |
12 | 2369 } |
2370 else if (hb->text) | |
2371 { | |
1 | 2372 gtk_html_add_text(html, hb->font, hb->fore, hb->back, |
12 | 2373 hb->text, strlen(hb->text), hb->uline, hb->strike, |
2374 hb->url); | |
1 | 2375 } |
2376 | |
12 | 2377 |
2378 | |
2379 /* | |
2380 * Font stays, so do colors (segfaults if I free) | |
2381 */ | |
1 | 2382 if (hb->fore) |
2383 gdk_color_free(hb->fore); | |
2384 if (hb->back) | |
2385 gdk_color_free(hb->back); | |
2386 if (hb->text) | |
2387 g_free(hb->text); | |
2388 if (hb->url) | |
2389 g_free(hb->url); | |
2390 | |
12 | 2391 g_free(hb); |
1 | 2392 |
2393 hbits = hbits->next; | |
2394 } | |
2395 | |
12 | 2396 g_list_free(html_bits); |
2397 | |
2398 | |
2399 gtk_html_thaw(html); | |
2400 | |
1 | 2401 gdk_window_get_size(html->html_area, NULL, &height); |
12 | 2402 gtk_adjustment_set_value(html->vadj, html->vadj->upper - height); |
1 | 2403 |
2404 } | |
2405 | |
12 | 2406 static GdkGC *create_bg_gc(GtkHtml * html) |
1 | 2407 { |
12 | 2408 GdkGCValues values; |
2409 | |
2410 values.tile = GTK_WIDGET(html)->style->bg_pixmap[GTK_STATE_NORMAL]; | |
2411 values.fill = GDK_TILED; | |
2412 | |
2413 return gdk_gc_new_with_values(html->html_area, &values, | |
2414 GDK_GC_FILL | GDK_GC_TILE); | |
1 | 2415 } |
2416 | |
12 | 2417 static void clear_area(GtkHtml * html, GdkRectangle * area) |
1 | 2418 { |
12 | 2419 GtkWidget *widget = GTK_WIDGET(html); |
2420 gint x, | |
2421 y; | |
2422 | |
2423 | |
2424 if (html->transparent) | |
2425 { | |
1 | 2426 if (html->pm == NULL) |
2427 html->pm = get_desktop_pixmap(widget); | |
2428 | |
12 | 2429 if (html->pm == NULL) |
2430 return; | |
2431 | |
2432 if (html->bg_gc == NULL) | |
2433 { | |
2434 GdkGCValues values; | |
2435 | |
2436 values.tile = html->pm; | |
2437 values.fill = GDK_TILED; | |
2438 | |
2439 html->bg_gc = gdk_gc_new_with_values(html->html_area, &values, | |
2440 GDK_GC_FILL | GDK_GC_TILE); | |
2441 | |
2442 } | |
2443 | |
1 | 2444 gdk_window_get_deskrelative_origin(html->html_area, &x, &y); |
2445 | |
12 | 2446 gdk_draw_pixmap(html->html_area, html->bg_gc, html->pm, |
2447 x + area->x, y + area->y, area->x, area->y, area->width, | |
2448 area->height); | |
1 | 2449 |
2450 return; | |
2451 | |
2452 } | |
12 | 2453 if (html->bg_gc) |
2454 { | |
2455 | |
2456 gint width, | |
2457 height; | |
2458 | |
2459 gdk_window_get_size(widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, | |
2460 &height); | |
2461 | |
2462 gdk_gc_set_ts_origin(html->bg_gc, | |
2463 (-html->xoffset) % width, | |
2464 (-html->yoffset) % height); | |
2465 | |
2466 gdk_draw_rectangle(html->html_area, html->bg_gc, TRUE, | |
2467 area->x, area->y, area->width, area->height); | |
2468 } | |
2469 else | |
2470 gdk_window_clear_area(html->html_area, area->x, area->y, area->width, | |
2471 area->height); | |
1 | 2472 } |
2473 | |
2474 | |
2475 | |
2476 | |
12 | 2477 static void gtk_html_destroy(GtkObject * object) |
1 | 2478 { |
12 | 2479 GtkHtml *html; |
2480 | |
2481 g_return_if_fail(object != NULL); | |
2482 g_return_if_fail(GTK_IS_HTML(object)); | |
2483 | |
2484 html = (GtkHtml *) object; | |
2485 | |
2486 | |
2487 gtk_signal_disconnect_by_data(GTK_OBJECT(html->hadj), html); | |
2488 gtk_signal_disconnect_by_data(GTK_OBJECT(html->vadj), html); | |
2489 | |
2490 if (html->timer) | |
2491 { | |
2492 gtk_timeout_remove(html->timer); | |
2493 html->timer = 0; | |
1 | 2494 } |
2495 | |
12 | 2496 if (html->tooltip_timer) |
2497 { | |
2498 gtk_timeout_remove(html->tooltip_timer); | |
1 | 2499 html->tooltip_timer = -1; |
2500 } | |
2501 | |
12 | 2502 |
2503 GTK_OBJECT_CLASS(parent_class)->destroy(object); | |
2504 | |
1 | 2505 } |
2506 | |
12 | 2507 static void gtk_html_finalize(GtkObject * object) |
1 | 2508 { |
12 | 2509 GList *hbits; |
2510 GtkHtml *html; | |
1 | 2511 GtkHtmlBit *hb; |
2512 | |
2513 | |
12 | 2514 g_return_if_fail(object != NULL); |
2515 g_return_if_fail(GTK_IS_HTML(object)); | |
2516 | |
2517 html = (GtkHtml *) object; | |
2518 | |
2519 gtk_object_unref(GTK_OBJECT(html->hadj)); | |
2520 gtk_object_unref(GTK_OBJECT(html->vadj)); | |
2521 | |
2522 hbits = html->html_bits; | |
2523 | |
2524 while (hbits) | |
2525 { | |
2526 hb = (GtkHtmlBit *) hbits->data; | |
2527 if (hb->fore) | |
2528 gdk_color_free(hb->fore); | |
2529 if (hb->back) | |
2530 gdk_color_free(hb->back); | |
2531 if (hb->text) | |
2532 g_free(hb->text); | |
2533 if (hb->url) | |
2534 g_free(hb->url); | |
2535 if (hb->pm) | |
2536 gdk_pixmap_unref(hb->pm); | |
2537 | |
2538 g_free(hb); | |
2539 hbits = hbits->next; | |
2540 } | |
2541 if (html->html_bits) | |
2542 g_list_free(html->html_bits); | |
2543 | |
2544 if (html->urls) | |
2545 g_list_free(html->urls); | |
2546 | |
2547 if (html->selected_text) | |
2548 g_free(html->selected_text); | |
2549 | |
2550 if (html->gc) | |
2551 gdk_gc_destroy(html->gc); | |
2552 | |
2553 if (html->bg_gc) | |
2554 gdk_gc_destroy(html->bg_gc); | |
1 | 2555 |
2556 if (html->tooltip_window) | |
2557 gtk_widget_destroy(html->tooltip_window); | |
12 | 2558 |
2559 GTK_OBJECT_CLASS(parent_class)->finalize(object); | |
1 | 2560 } |
2561 | |
12 | 2562 static void gtk_html_realize(GtkWidget * widget) |
1 | 2563 { |
12 | 2564 GtkHtml *html; |
2565 GdkWindowAttr attributes; | |
2566 gint attributes_mask; | |
2567 | |
2568 g_return_if_fail(widget != NULL); | |
2569 g_return_if_fail(GTK_IS_HTML(widget)); | |
2570 | |
2571 html = GTK_HTML(widget); | |
2572 GTK_WIDGET_SET_FLAGS(html, GTK_REALIZED); | |
2573 | |
2574 attributes.window_type = GDK_WINDOW_CHILD; | |
2575 attributes.x = widget->allocation.x; | |
2576 attributes.y = widget->allocation.y; | |
2577 attributes.width = widget->allocation.width; | |
2578 attributes.height = widget->allocation.height; | |
2579 attributes.wclass = GDK_INPUT_OUTPUT; | |
2580 attributes.visual = gtk_widget_get_visual(widget); | |
2581 attributes.colormap = gtk_widget_get_colormap(widget); | |
2582 attributes.event_mask = gtk_widget_get_events(widget); | |
2583 attributes.event_mask |= (GDK_EXPOSURE_MASK | | |
2584 GDK_BUTTON_PRESS_MASK | | |
2585 GDK_BUTTON_RELEASE_MASK | | |
2586 GDK_BUTTON_MOTION_MASK | | |
2587 GDK_ENTER_NOTIFY_MASK | | |
2588 GDK_LEAVE_NOTIFY_MASK | | |
2589 GDK_POINTER_MOTION_MASK | | |
2590 GDK_POINTER_MOTION_HINT_MASK | | |
2591 GDK_VISIBILITY_NOTIFY_MASK | GDK_KEY_PRESS_MASK); | |
2592 | |
2593 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; | |
2594 | |
2595 widget->window = | |
2596 gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, | |
2597 attributes_mask); | |
2598 gdk_window_set_user_data(widget->window, html); | |
2599 | |
2600 attributes.x = (widget->style->klass->xthickness + BORDER_WIDTH); | |
2601 attributes.y = (widget->style->klass->ythickness + BORDER_WIDTH); | |
2602 attributes.width = | |
2603 MAX(1, (gint) widget->allocation.width - (gint) attributes.x * 2); | |
2604 attributes.height = | |
2605 MAX(1, (gint) widget->allocation.height - (gint) attributes.y * 2); | |
2606 | |
2607 html->html_area = | |
2608 gdk_window_new(widget->window, &attributes, attributes_mask); | |
2609 gdk_window_set_user_data(html->html_area, html); | |
2610 | |
2611 widget->style = gtk_style_attach(widget->style, widget->window); | |
2612 | |
2613 /* | |
2614 * Can't call gtk_style_set_background here because it's handled specially | |
2615 */ | |
2616 gdk_window_set_background(widget->window, | |
2617 &widget->style->base[GTK_STATE_NORMAL]); | |
2618 gdk_window_set_background(html->html_area, | |
2619 &widget->style->base[GTK_STATE_NORMAL]); | |
2620 | |
2621 if (widget->style->bg_pixmap[GTK_STATE_NORMAL]) | |
2622 html->bg_gc = create_bg_gc(html); | |
2623 | |
2624 html->gc = gdk_gc_new(html->html_area); | |
2625 gdk_gc_set_exposures(html->gc, TRUE); | |
2626 gdk_gc_set_foreground(html->gc, &widget->style->text[GTK_STATE_NORMAL]); | |
2627 | |
2628 gdk_window_show(html->html_area); | |
1 | 2629 |
2630 } | |
2631 | |
12 | 2632 static void gtk_html_style_set(GtkWidget * widget, GtkStyle * previous_style) |
1 | 2633 { |
12 | 2634 GtkHtml *html; |
2635 | |
2636 g_return_if_fail(widget != NULL); | |
2637 g_return_if_fail(GTK_IS_HTML(widget)); | |
2638 | |
2639 html = GTK_HTML(widget); | |
2640 if (GTK_WIDGET_REALIZED(widget)) | |
2641 { | |
2642 gdk_window_set_background(widget->window, | |
2643 &widget->style->base[GTK_STATE_NORMAL]); | |
2644 gdk_window_set_background(html->html_area, | |
2645 &widget->style->base[GTK_STATE_NORMAL]); | |
2646 | |
2647 if (html->bg_gc) | |
2648 { | |
2649 gdk_gc_destroy(html->bg_gc); | |
2650 html->bg_gc = NULL; | |
2651 } | |
2652 | |
2653 if (widget->style->bg_pixmap[GTK_STATE_NORMAL]) | |
2654 { | |
2655 html->bg_gc = create_bg_gc(html); | |
2656 } | |
2657 | |
2658 } | |
1 | 2659 } |
2660 | |
12 | 2661 static void gtk_html_unrealize(GtkWidget * widget) |
1 | 2662 { |
12 | 2663 GtkHtml *html; |
2664 | |
2665 g_return_if_fail(widget != NULL); | |
2666 g_return_if_fail(GTK_IS_HTML(widget)); | |
2667 | |
2668 html = GTK_HTML(widget); | |
2669 | |
2670 gdk_window_set_user_data(html->html_area, NULL); | |
2671 gdk_window_destroy(html->html_area); | |
2672 html->html_area = NULL; | |
2673 | |
2674 gdk_gc_destroy(html->gc); | |
2675 html->gc = NULL; | |
2676 | |
2677 if (html->bg_gc) | |
2678 { | |
2679 gdk_gc_destroy(html->bg_gc); | |
2680 html->bg_gc = NULL; | |
2681 } | |
2682 | |
2683 if (GTK_WIDGET_CLASS(parent_class)->unrealize) | |
2684 (*GTK_WIDGET_CLASS(parent_class)->unrealize) (widget); | |
1 | 2685 } |
2686 | |
2687 | |
2688 | |
2689 | |
2690 | |
12 | 2691 static void gtk_html_add_pixmap(GtkHtml * html, GdkPixmap * pm, int fit) |
1 | 2692 { |
12 | 2693 GtkHtmlBit *last_hb; |
2694 GtkHtmlBit *hb = g_new0(GtkHtmlBit, 1); | |
2695 GdkWindowPrivate *private = (GdkWindowPrivate *) pm; | |
2696 | |
2697 last_hb = (GtkHtmlBit *) g_list_last(html->html_bits)->data; | |
1 | 2698 |
2699 hb->fit = fit; | |
2700 hb->x = html->current_x; | |
2701 hb->y = html->current_y; | |
12 | 2702 if (fit) |
1 | 2703 hb->height = last_hb->height; |
2704 else | |
2705 hb->height = private->height; | |
2706 hb->type = HTML_BIT_PIXMAP; | |
2707 hb->width = private->width; | |
2708 hb->text = NULL; | |
2709 hb->url = NULL; | |
2710 hb->fore = NULL; | |
2711 hb->back = NULL; | |
2712 hb->font = NULL; | |
12 | 2713 hb->uline = 0; |
2714 hb->strike = 0; | |
1 | 2715 hb->was_selected = 0; |
12 | 2716 hb->newline = 0; |
2717 hb->pm = pm; | |
2718 | |
2719 if (html->current_x == BORDER_WIDTH) | |
2720 { | |
1 | 2721 html->current_y += hb->height; |
2722 hb->y += hb->height; | |
2723 } | |
2724 | |
2725 | |
12 | 2726 html->current_x += hb->width; |
2727 | |
1 | 2728 gtk_html_draw_bit(html, hb, 1); |
2729 | |
2730 html->html_bits = g_list_append(html->html_bits, hb); | |
2731 | |
2732 | |
2733 } | |
2734 | |
12 | 2735 static void gtk_html_add_seperator(GtkHtml * html) |
1 | 2736 { |
12 | 2737 GtkHtmlBit *hb = g_new0(GtkHtmlBit, 1); |
2738 gint width, | |
2739 height; | |
2740 | |
1 | 2741 html->current_x = 0; |
2742 html->current_y += 5; | |
2743 | |
12 | 2744 gdk_window_get_size(html->html_area, &width, &height); |
2745 | |
1 | 2746 hb->x = html->current_x; |
2747 hb->y = html->current_y; | |
2748 hb->height = 5; | |
2749 hb->type = HTML_BIT_SEP; | |
12 | 2750 hb->width = |
2751 width - | |
2752 GTK_SCROLLED_WINDOW(GTK_WIDGET(html)->parent)->vscrollbar->allocation. | |
2753 width - 10; | |
1 | 2754 hb->text = NULL; |
2755 hb->url = NULL; | |
2756 hb->fore = NULL; | |
2757 hb->back = NULL; | |
2758 hb->font = NULL; | |
12 | 2759 hb->uline = 0; |
2760 hb->strike = 0; | |
1 | 2761 hb->was_selected = 0; |
12 | 2762 hb->newline = 0; |
2763 hb->pm = NULL; | |
1 | 2764 |
2765 gtk_html_draw_bit(html, hb, 1); | |
2766 | |
2767 html->html_bits = g_list_append(html->html_bits, hb); | |
2768 | |
2769 } | |
2770 | |
2771 | |
12 | 2772 static void gtk_html_add_text(GtkHtml * html, |
2773 GdkFont * cfont, | |
2774 GdkColor * fore, | |
2775 GdkColor * back, | |
2776 char *chars, | |
2777 gint length, gint uline, gint strike, char *url) | |
1 | 2778 { |
12 | 2779 char *nextline = NULL, |
2780 *c, | |
2781 *text, | |
2782 *tmp; | |
2783 GdkGC *gc; | |
2784 int nl = 0, | |
2785 nl2 = 0; | |
2786 int maxwidth; | |
2787 gint lb; | |
2788 GList *hbits; | |
79 | 2789 size_t num = 0; |
2790 int i, | |
12 | 2791 height; |
2792 GtkHtmlBit *hb; | |
2793 gint hwidth, | |
2794 hheight; | |
2795 | |
2796 if (length == 1 && chars[0] == '\n') | |
2797 { | |
2798 GtkHtmlBit *h; | |
2799 hbits = g_list_last(html->html_bits); | |
2800 if (!hbits) | |
2801 return; | |
2802 /* | |
2803 * I realize this loses a \n sometimes | |
2804 * * if it's the first thing in the widget. | |
2805 * * so fucking what. | |
2806 */ | |
2807 | |
2808 h = (GtkHtmlBit *) hbits->data; | |
2809 h->newline++; | |
2810 if (html->current_x > 0) | |
2811 html->current_x = 0; | |
2812 else | |
2813 html->current_y += cfont->ascent + cfont->descent + 2; | |
2814 return; | |
2815 } | |
2816 | |
2817 | |
2818 | |
2819 c = text = g_malloc(length + 2); | |
2820 strncpy(text, chars, length); | |
2821 text[length] = 0; | |
2822 | |
2823 | |
2824 gc = html->gc; | |
2825 | |
2826 if (gc == NULL) | |
2827 gc = html->gc = gdk_gc_new(html->html_area); | |
2828 | |
2829 gdk_gc_set_font(gc, cfont); | |
2830 | |
2831 | |
2832 while (*c) | |
2833 { | |
2834 if (*c == '\n') | |
2835 { | |
2836 if (*(c + 1) == '\0') | |
2837 { | |
2838 nl = 1; | |
2839 length--; | |
2840 c[0] = '\0'; | |
2841 break; | |
2842 } | |
2843 if (*c) | |
2844 { | |
2845 gtk_html_add_text(html, cfont, fore, back, text, num + 1, uline, | |
2846 strike, url); | |
2847 tmp = text; | |
2848 length -= (num + 1); | |
2849 text = g_malloc(length + 2); | |
2850 strncpy(text, (c + 1), length); | |
2851 text[length] = 0; | |
2852 c = text; | |
2853 num = 0; | |
2854 g_free(tmp); | |
1 | 2855 continue; |
12 | 2856 } |
2857 } | |
2858 | |
2859 num++; | |
2860 c++; | |
2861 } | |
2862 | |
2863 /* | |
2864 * Note, yG is chosen because G is damn high, and y is damn low, | |
2865 */ | |
2866 /* | |
2867 * it should be just fine. :) | |
2868 */ | |
2869 | |
2870 gdk_window_get_size(html->html_area, &hwidth, &hheight); | |
2871 | |
2872 num = strlen(text); | |
2873 | |
2874 while (GTK_WIDGET(html)->allocation.width < 20) | |
2875 { | |
2876 while (gtk_events_pending()) | |
1 | 2877 gtk_main_iteration(); |
2878 } | |
2879 | |
12 | 2880 maxwidth = (hwidth - html->current_x - 8); |
2881 /* | |
2882 * HTK_SCROLLED_WINDOW(GTK_WIDGET(layout)->parent)->vscrollbar->allocation.width) - 8; | |
2883 */ | |
2884 | |
2885 while (gdk_text_measure(cfont, text, num) > maxwidth) | |
2886 { | |
2887 if (num > 1) | |
2888 num--; | |
2889 else | |
2890 { | |
26 | 2891 if (html->current_x != 0) { |
2892 html->current_x = 0; | |
2893 if (nl) { | |
2894 text[length] = '\n'; | |
2895 length++; | |
2896 } | |
2897 gtk_html_add_text(html, cfont, fore, back, text, length, uline, strike, url); | |
2898 g_free(text); | |
2899 return; | |
2900 } else { | |
2901 num = strlen (text); | |
2902 break; | |
1 | 2903 } |
2904 } | |
2905 | |
2906 } | |
2907 | |
12 | 2908 height = cfont->ascent + cfont->descent + 2; |
2909 | |
2910 | |
2911 if ((int) (html->vadj->upper - html->current_y) < (int) (height * 2)) | |
2912 { | |
2913 int val; | |
2914 val = (height * 2) + html->current_y; | |
2915 html->vadj->upper = val; | |
2916 adjust_adj(html, html->vadj); | |
1 | 2917 } |
2918 | |
12 | 2919 |
2920 if (html->current_x == 0) | |
2921 { | |
2922 html->current_y += height; | |
2923 gdk_text_extents(cfont, text, 1, &lb, NULL, NULL, NULL, NULL); | |
2924 html->current_x += (2 - lb); | |
2925 } | |
2926 else if ((hbits = g_list_last(html->html_bits)) != NULL) | |
2927 { | |
2928 int diff, | |
2929 y; | |
2930 hb = (GtkHtmlBit *) hbits->data; | |
2931 if (height > hb->height) | |
2932 { | |
1 | 2933 diff = height - hb->height; |
2934 y = hb->y; | |
2935 html->current_y += diff; | |
12 | 2936 while (hbits) |
2937 { | |
2938 hb = (GtkHtmlBit *) hbits->data; | |
1 | 2939 if (hb->y != y) |
12 | 2940 break; |
2941 hb->height = height; | |
2942 hb->y += diff; ////////////my thing here ///////////////// | |
2943 gtk_html_draw_bit(html, hb, FALSE); | |
2944 | |
2945 hbits = hbits->prev; | |
1 | 2946 } |
2947 } | |
2948 } | |
2949 | |
2950 | |
2951 | |
2952 | |
12 | 2953 if (num != strlen(text)) |
2954 { | |
2955 /* | |
2956 * This is kinda cheesy but it may make things | |
2957 * * much better lookin | |
2958 */ | |
26 | 2959 |
2960 for (i=2; (num - i > 0); i++) { | |
2961 if (text[num - i] == ' ') { | |
12 | 2962 num = num - (i - 1); |
1 | 2963 nl2 = 1; |
2964 break; | |
2965 } | |
2966 } | |
2967 | |
2968 nextline = g_malloc(length - num + 2); | |
12 | 2969 strncpy(nextline, (char *) (text + num), length - num); |
1 | 2970 nextline[length - num] = 0; |
12 | 2971 if (nl) |
2972 { | |
1 | 2973 nextline[length - num] = '\n'; |
2974 nextline[length - num + 1] = 0; | |
2975 nl = 0; | |
2976 } | |
2977 | |
2978 | |
2979 text[num] = 0; | |
2980 } | |
2981 | |
2982 | |
52 | 2983 if (url != NULL) { |
53 | 2984 fore = get_color(3355647, gdk_window_get_colormap(html->html_area)); |
52 | 2985 } |
1 | 2986 |
2987 hb = g_new0(GtkHtmlBit, 1); | |
2988 | |
2989 hb->text = g_strdup(text); | |
2990 | |
52 | 2991 if (fore) |
2992 hb->fore = gdk_color_copy(fore); | |
2993 else | |
2994 hb->fore = NULL; | |
49 | 2995 |
1 | 2996 if (back) |
2997 hb->back = gdk_color_copy(back); | |
2998 else | |
2999 hb->back = NULL; | |
3000 hb->font = cfont; | |
3001 hb->uline = uline; | |
3002 hb->strike = strike; | |
3003 hb->height = height; | |
3004 gdk_text_extents(cfont, text, num, &lb, NULL, &hb->width, NULL, NULL); | |
3005 hb->x = html->current_x; | |
3006 hb->y = html->current_y; | |
12 | 3007 hb->type = HTML_BIT_TEXT; |
3008 hb->pm = NULL; | |
3009 if (url != NULL) | |
3010 { | |
1 | 3011 uline = 1; |
3012 hb->uline = 1; | |
3013 hb->url = g_strdup(url); | |
12 | 3014 } |
3015 else | |
3016 { | |
1 | 3017 hb->url = NULL; |
3018 } | |
3019 html->current_x += hb->width; | |
3020 | |
3021 html->html_bits = g_list_append(html->html_bits, hb); | |
12 | 3022 if (url != NULL) |
3023 { | |
3024 html->urls = g_list_append(html->urls, hb); | |
3025 } | |
3026 | |
3027 | |
3028 | |
3029 gtk_html_draw_bit(html, hb, 1); | |
3030 | |
3031 if (nl || nl2) | |
3032 { | |
3033 if (nl) | |
3034 hb->newline = 1; | |
3035 html->current_x = 0; | |
3036 } | |
3037 else | |
3038 hb->newline = 0; | |
3039 | |
3040 | |
3041 if (nextline != NULL) | |
3042 { | |
3043 gtk_html_add_text(html, cfont, fore, back, nextline, strlen(nextline), | |
3044 uline, strike, url); | |
3045 g_free(nextline); | |
3046 } | |
3047 | |
3048 g_free(text); | |
137 | 3049 if (url != NULL) |
3050 g_free(fore); | |
1 | 3051 } |
3052 | |
12 | 3053 static char * html_strtok( char * input, char delim ) |
1 | 3054 { |
12 | 3055 static char * end; |
3056 static char * curr_offset; | |
3057 int i; | |
3058 int num_quotes=0; | |
3059 | |
3060 if( input != NULL) | |
3061 { | |
3062 curr_offset = input; | |
3063 end = input+strlen(input); | |
3064 } | |
3065 else | |
3066 { | |
3067 if( curr_offset + strlen(curr_offset) < end ) | |
3068 { | |
3069 curr_offset += strlen(curr_offset) + 1; | |
3070 } | |
3071 else | |
3072 { | |
3073 return NULL; | |
3074 } | |
3075 } | |
3076 for( i=0; curr_offset+i < end && | |
3077 (curr_offset[i] != delim || num_quotes != 0) | |
3078 ; i++ ) | |
3079 { | |
3080 if( curr_offset[i] == '\"' ) | |
3081 { | |
3082 num_quotes = (num_quotes+1)%2; | |
3083 } | |
3084 } | |
3085 curr_offset[i] = '\0'; | |
3086 return curr_offset; | |
3087 } | |
3088 | |
3089 | |
3090 void gtk_html_append_text(GtkHtml * html, char *text, gint options) | |
3091 { | |
3092 GdkColormap *map; | |
1 | 3093 GdkFont *cfont; |
12 | 3094 GdkRectangle area; |
3095 char ws[BUF_LONG], | |
3096 tag[BUF_LONG], | |
3097 *c, | |
3098 *url = NULL; | |
3099 gint intag = 0, | |
3100 wpos = 0, | |
3101 tpos = 0, | |
3102 colorv, | |
3103 bold = 0, | |
3104 italic = 0, | |
3105 fixed = 0, | |
3106 uline = 0, | |
3107 strike = 0, | |
3108 title = 0; | |
3109 gint height; | |
3110 struct font_state *current, | |
3111 *tmp; | |
3112 struct font_state def_state = { 3, 0, 0, "", NULL, NULL, NULL }; | |
3113 | |
3114 current = &def_state; | |
3115 map = gdk_window_get_colormap(html->html_area); | |
3116 cfont = getfont(current->font, bold, italic, fixed, current->size); | |
1 | 3117 c = text; |
3118 | |
3119 | |
12 | 3120 while (*c) |
3121 { | |
3122 if (*c == '<') | |
3123 { | |
3124 if (!intag) | |
3125 { | |
3126 ws[wpos] = 0; | |
3127 if (wpos) | |
3128 { | |
3129 if (title) | |
3130 { | |
3131 if (html->title) | |
3132 g_free(html->title); | |
3133 html->title = g_strdup(ws); | |
3134 } | |
3135 else | |
3136 gtk_html_add_text(html, cfont, current->color, | |
3137 current->bgcol, ws, strlen(ws), uline, | |
3138 strike, url); | |
3139 } | |
3140 wpos = 0; | |
3141 intag = 1; | |
3142 } | |
3143 else | |
3144 { | |
3145 /* | |
3146 * Assuming you NEVER have nested tags | |
3147 * * (and I mean <tag <tag>> by this, not | |
3148 * * <tag><tag2></tag2><tag>.. | |
3149 */ | |
3150 tag[tpos] = 0; | |
3151 gtk_html_add_text(html, cfont, current->color, current->bgcol, | |
3152 "<", 1, 0, 0, NULL); | |
3153 gtk_html_add_text(html, cfont, current->color, current->bgcol, | |
3154 tag, strlen(tag), 0, 0, NULL); | |
1 | 3155 tpos = 0; |
12 | 3156 |
3157 tag[0] = *c; | |
1 | 3158 } |
12 | 3159 } |
3160 else if (*c == '>') | |
3161 { | |
3162 if (intag) | |
3163 { | |
3164 tag[tpos] = 0; | |
1 | 3165 if (!strcasecmp(tag, "B")) |
3166 bold = 1; | |
3167 else if (!strcasecmp(tag, "STRIKE")) | |
3168 strike = 1; | |
3169 else if (!strcasecmp(tag, "I")) | |
3170 italic = 1; | |
3171 else if (!strcasecmp(tag, "U")) | |
3172 uline = 1; | |
3173 else if (!strcasecmp(tag, "PRE")) | |
3174 fixed = 1; | |
3175 else if (!strcasecmp(tag, "HR")) | |
3176 gtk_html_add_seperator(html); | |
3177 else if (!strcasecmp(tag, "/B")) | |
3178 bold = 0; | |
3179 else if (!strcasecmp(tag, "/STRIKE")) | |
3180 strike = 0; | |
3181 else if (!strcasecmp(tag, "/I")) | |
3182 italic = 0; | |
3183 else if (!strcasecmp(tag, "/U")) | |
3184 uline = 0; | |
3185 else if (!strcasecmp(tag, "/PRE")) | |
3186 fixed = 0; | |
3187 else if (!strcasecmp(tag, "TITLE")) | |
3188 title = 1; | |
3189 else if (!strcasecmp(tag, "/TITLE")) | |
3190 title = 0; | |
12 | 3191 else if (!strncasecmp(tag, "IMG", 3)) |
3192 { | |
3193 | |
3194 } | |
3195 else if (!strcasecmp(tag, "H3")) | |
3196 { | |
1 | 3197 current = push_state(current); |
3198 current->size = 4; | |
12 | 3199 } |
3200 else if (!strcasecmp(tag, "/H3")) | |
3201 { | |
3202 gtk_html_add_text(html, cfont, current->color, | |
3203 current->bgcol, "\n", 1, 0, 0, NULL); | |
3204 | |
3205 if (current->next) | |
3206 { | |
1 | 3207 if (current->ownbg) |
3208 g_free(current->bgcol); | |
3209 if (current->owncolor) | |
3210 g_free(current->color); | |
12 | 3211 tmp = current; |
3212 current = current->next; | |
3213 g_free(tmp); | |
1 | 3214 } |
12 | 3215 } |
3216 else if (!strcasecmp(tag, "TABLE")) | |
3217 { | |
3218 } | |
3219 else if (!strcasecmp(tag, "/TABLE")) | |
3220 { | |
3221 } | |
3222 else if (!strcasecmp(tag, "TR")) | |
3223 { | |
3224 } | |
3225 else if (!strcasecmp(tag, "/TR")) | |
3226 { | |
3227 } | |
3228 else if (!strcasecmp(tag, "/TD")) | |
3229 { | |
3230 } | |
3231 else if (!strcasecmp(tag, "TD")) | |
3232 { | |
3233 gtk_html_add_text(html, cfont, current->color, | |
3234 current->bgcol, " ", 2, 0, 0, NULL); | |
3235 } | |
3236 else if (!strncasecmp(tag, "A ", 2)) | |
3237 { | |
1 | 3238 char *d; |
3239 char *temp = d = g_strdup(tag); | |
3240 int flag = 0; | |
12 | 3241 strtok(tag, " "); |
3242 while ((d = strtok(NULL, " "))) | |
3243 { | |
1 | 3244 if (strlen(d) < 7) |
3245 break; | |
12 | 3246 if (!strncasecmp(d, "HREF=\"", strlen("HREF=\""))) |
3247 { | |
3248 d += strlen("HREF=\""); | |
1 | 3249 d[strlen(d) - 1] = 0; |
3250 url = g_malloc(strlen(d) + 1); | |
3251 strcpy(url, d); | |
3252 flag = 1; | |
12 | 3253 } |
3254 } | |
1 | 3255 g_free(temp); |
12 | 3256 if (!flag) |
3257 { | |
3258 gtk_html_add_text(html, cfont, current->color, | |
3259 current->bgcol, "<", 1, 0, 0, NULL); | |
3260 gtk_html_add_text(html, cfont, current->color, | |
3261 current->bgcol, tag, strlen(tag), 0, | |
3262 0, NULL); | |
3263 gtk_html_add_text(html, cfont, current->color, | |
3264 current->bgcol, ">", 1, 0, 0, NULL); | |
1 | 3265 } |
12 | 3266 } |
3267 else if (!strcasecmp(tag, "/A")) | |
3268 { | |
3269 if (url) | |
3270 { | |
1 | 3271 g_free(url); |
3272 url = NULL; | |
3273 } | |
12 | 3274 } |
3275 else if (!strncasecmp(tag, "FONT", strlen("FONT"))) | |
3276 { | |
3277 char *d; | |
3278 /* | |
3279 * Push a new state onto the stack, based on the old state | |
3280 */ | |
3281 current = push_state(current); | |
3282 html_strtok(tag, ' '); | |
3283 while ((d = html_strtok(NULL, ' '))) | |
3284 { | |
3285 if (!strncasecmp(d, "COLOR=", strlen("COLOR="))) | |
3286 { | |
3287 d += strlen("COLOR="); | |
3288 if (*d == '\"') | |
3289 { | |
3290 d++; | |
3291 } | |
3292 if (*d == '#') | |
3293 d++; | |
3294 if (d[strlen(d) - 1] == '\"') | |
3295 d[strlen(d) - 1] = 0; | |
3296 if (sscanf(d, "%x", &colorv) | |
3297 && !(options & HTML_OPTION_NO_COLOURS)) | |
3298 { | |
1 | 3299 current->color = get_color(colorv, map); |
3300 current->owncolor = 1; | |
12 | 3301 } |
3302 else | |
3303 { | |
1 | 3304 } |
12 | 3305 } |
3306 if (!strncasecmp(d, "FACE=", strlen("FACE="))) | |
3307 { | |
3308 d += strlen("FACE="); | |
3309 if (*d == '\"') | |
3310 { | |
3311 d++; | |
3312 } | |
1 | 3313 if (d[strlen(d) - 1] == '\"') |
3314 d[strlen(d) - 1] = 0; | |
12 | 3315 strcpy(current->font, d); |
3316 } | |
3317 else if (!strncasecmp(d, "BACK=", strlen("BACK="))) | |
3318 { | |
3319 d += strlen("BACK="); | |
3320 if (*d == '\"') | |
3321 d++; | |
3322 if (*d == '#') | |
3323 d++; | |
3324 if (d[strlen(d) - 1] == '\"') | |
3325 d[strlen(d) - 1] = 0; | |
3326 if (sscanf(d, "%x", &colorv) | |
3327 && !(options & HTML_OPTION_NO_COLOURS)) | |
3328 { | |
1 | 3329 current->bgcol = get_color(colorv, map); |
3330 current->ownbg = 1; | |
12 | 3331 } |
3332 else | |
3333 { | |
3334 } | |
3335 } | |
3336 else if (!strncasecmp(d, "SIZE=", strlen("SIZE="))) | |
3337 { | |
3338 d += strlen("SIZE="); | |
3339 if (*d == '\"') | |
3340 d++; | |
3341 if (*d == '+') | |
3342 d++; | |
3343 if (sscanf(d, "%d", &colorv)) | |
3344 { | |
3345 current->size = colorv; | |
3346 } | |
3347 else | |
3348 { | |
1 | 3349 } |
12 | 3350 } |
3351 else if (strncasecmp(d, "PTSIZE=", strlen("PTSIZE="))) | |
3352 { | |
3353 } | |
1 | 3354 } |
12 | 3355 } |
3356 else | |
3357 if (!strncasecmp | |
3358 (tag, "BODY BGCOLOR", strlen("BODY BGCOLOR"))) | |
3359 { | |
3360 | |
3361 /* | |
3362 * Ditch trailing \" | |
3363 */ | |
3364 tag[strlen(tag) - 1] = 0; | |
3365 if (sscanf(tag + strlen("BODY BGCOLOR=\"#"), "%x", &colorv) | |
3366 && !(options & HTML_OPTION_NO_COLOURS)) | |
3367 { | |
3368 current->bgcol = get_color(colorv, map); | |
3369 current->ownbg = 1; | |
3370 } | |
3371 } | |
3372 else if (!strncasecmp(tag, "/FONT", strlen("/FONT"))) | |
3373 { | |
3374 /* | |
3375 * Pop a font state off the list if possible, freeing | |
3376 * any resources it used | |
3377 */ | |
3378 if (current->next) | |
3379 { | |
1 | 3380 if (current->ownbg) |
3381 g_free(current->bgcol); | |
3382 if (current->owncolor) | |
3383 g_free(current->color); | |
12 | 3384 tmp = current; |
3385 current = current->next; | |
3386 g_free(tmp); | |
3387 } | |
3388 | |
3389 } | |
3390 else if (!strcasecmp(tag, "/BODY")) | |
3391 { | |
3392 if (current->next) | |
3393 { | |
3394 if (current->ownbg) | |
3395 g_free(current->bgcol); | |
3396 if (current->owncolor) | |
3397 g_free(current->color); | |
3398 tmp = current; | |
3399 current = current->next; | |
1 | 3400 g_free(tmp); |
12 | 3401 } /* |
3402 * tags we ignore below | |
3403 */ | |
3404 } | |
3405 else if (!strncasecmp(tag, "BR", 2)) | |
3406 { | |
3407 gtk_html_add_text(html, cfont, current->color, | |
3408 current->bgcol, "\n", 1, 0, 0, NULL); | |
3409 } | |
3410 else if (strncasecmp(tag, "HTML", 4) | |
3411 && strncasecmp(tag, "/HTML", 5) | |
3412 && strncasecmp(tag, "BODY", 4) | |
3413 && strncasecmp(tag, "/BODY", 5) | |
3414 && strncasecmp(tag, "P", 1) | |
3415 && strncasecmp(tag, "/P", 2) | |
3416 && strncasecmp(tag, "HEAD", 4) | |
3417 && strncasecmp(tag, "/HEAD", 5)) | |
3418 { | |
3419 if (tpos) | |
3420 { | |
3421 gtk_html_add_text(html, cfont, current->color, | |
3422 current->bgcol, "<", 1, 0, 0, NULL); | |
3423 gtk_html_add_text(html, cfont, current->color, | |
3424 current->bgcol, tag, strlen(tag), 0, | |
3425 0, NULL); | |
3426 gtk_html_add_text(html, cfont, current->color, | |
3427 current->bgcol, ">", 1, 0, 0, NULL); | |
1 | 3428 |
3429 } | |
3430 } | |
12 | 3431 cfont = getfont(current->font, bold, italic, fixed, current->size); |
3432 tpos = 0; | |
1 | 3433 intag = 0; |
3434 } | |
12 | 3435 else |
3436 { | |
1 | 3437 ws[wpos++] = *c; |
3438 } | |
12 | 3439 } |
3440 else if (!intag && *c == '&') | |
3441 { | |
3442 if (!strncasecmp(c, "&", 5)) | |
3443 { | |
3444 ws[wpos++] = '&'; | |
3445 c += 4; | |
3446 } | |
3447 else if (!strncasecmp(c, "<", 4)) | |
3448 { | |
3449 ws[wpos++] = '<'; | |
3450 c += 3; | |
3451 } | |
3452 else if (!strncasecmp(c, ">", 4)) | |
3453 { | |
3454 ws[wpos++] = '>'; | |
3455 c += 3; | |
3456 } | |
3457 else if (!strncasecmp(c, " ", 6)) | |
3458 { | |
3459 ws[wpos++] = ' '; | |
3460 c += 5; | |
3461 } | |
3462 else | |
3463 { | |
3464 ws[wpos++] = *c; | |
3465 } | |
3466 } | |
3467 else | |
3468 { | |
3469 if (intag) | |
3470 { | |
3471 tag[tpos++] = *c; | |
3472 } | |
3473 else | |
3474 { | |
3475 ws[wpos++] = *c; | |
1 | 3476 } |
3477 } | |
3478 c++; | |
3479 } | |
12 | 3480 while (current->next) |
3481 { | |
1 | 3482 if (current->ownbg) |
3483 g_free(current->bgcol); | |
3484 if (current->owncolor) | |
3485 g_free(current->color); | |
3486 tmp = current; | |
3487 current = current->next; | |
3488 g_free(tmp); | |
3489 } | |
12 | 3490 ws[wpos] = 0; |
3491 tag[tpos] = 0; | |
3492 if (wpos) | |
3493 { | |
3494 gtk_html_add_text(html, cfont, current->color, current->bgcol, ws, | |
3495 strlen(ws), uline, strike, url); | |
1 | 3496 } |
12 | 3497 if (tpos) |
3498 { | |
3499 gtk_html_add_text(html, cfont, current->color, current->bgcol, "<", 1, | |
3500 0, 0, NULL); | |
3501 gtk_html_add_text(html, cfont, current->color, current->bgcol, tag, | |
3502 strlen(tag), 0, 0, NULL); | |
3503 gtk_html_add_text(html, cfont, current->color, current->bgcol, ">", 1, | |
3504 0, 0, NULL); | |
3505 } | |
3506 | |
3507 | |
3508 | |
3509 gdk_window_get_size(html->html_area, NULL, &height); | |
3510 area.height = height; | |
1 | 3511 gtk_adjustment_set_value(html->vadj, html->vadj->upper - area.height); |
3512 | |
12 | 3513 return; |
1 | 3514 } |
3515 | |
3516 | |
12 | 3517 static void adjust_adj(GtkHtml * html, GtkAdjustment * adj) |
1 | 3518 { |
12 | 3519 gint height; |
3520 | |
3521 gdk_window_get_size(html->html_area, NULL, &height); | |
3522 | |
3523 adj->step_increment = MIN(adj->upper, (float) SCROLL_PIXELS); | |
3524 adj->page_increment = MIN(adj->upper, height - (float) KEY_SCROLL_PIXELS); | |
3525 adj->page_size = MIN(adj->upper, height); | |
3526 adj->value = MIN(adj->value, adj->upper - adj->page_size); | |
3527 adj->value = MAX(adj->value, 0.0); | |
3528 | |
3529 gtk_signal_emit_by_name(GTK_OBJECT(adj), "changed"); | |
1 | 3530 } |
3531 | |
3532 | |
12 | 3533 static void scroll_down(GtkHtml * html, gint diff0) |
1 | 3534 { |
12 | 3535 GdkRectangle rect; |
3536 gint width, | |
3537 height; | |
3538 | |
3539 html->yoffset += diff0; | |
3540 | |
3541 gdk_window_get_size(html->html_area, &width, &height); | |
3542 | |
3543 if (html->transparent) | |
3544 { | |
1 | 3545 rect.x = 0; |
3546 rect.y = 0; | |
3547 rect.width = width; | |
3548 rect.height = height; | |
12 | 3549 } |
3550 else | |
3551 { | |
3552 | |
1 | 3553 |
3554 if (height > diff0 && !html->transparent) | |
12 | 3555 gdk_draw_pixmap(html->html_area, |
3556 html->gc, | |
3557 html->html_area, | |
3558 0, diff0, 0, 0, width, height - diff0); | |
3559 | |
3560 rect.x = 0; | |
3561 rect.y = MAX(0, height - diff0); | |
3562 rect.width = width; | |
3563 rect.height = MIN(height, diff0); | |
1 | 3564 } |
12 | 3565 |
3566 expose_html(html, &rect, FALSE); | |
3567 gtk_html_draw_focus((GtkWidget *) html); | |
1 | 3568 |
3569 } | |
3570 | |
12 | 3571 static void scroll_up(GtkHtml * html, gint diff0) |
1 | 3572 { |
12 | 3573 GdkRectangle rect; |
3574 gint width, | |
3575 height; | |
3576 | |
1 | 3577 html->yoffset -= diff0; |
3578 | |
3579 | |
12 | 3580 gdk_window_get_size(html->html_area, &width, &height); |
3581 | |
3582 if (html->transparent) | |
3583 { | |
1 | 3584 rect.x = 0; |
3585 rect.y = 0; | |
3586 rect.width = width; | |
3587 rect.height = height; | |
12 | 3588 } |
3589 else | |
3590 { | |
3591 | |
1 | 3592 if (height > diff0) |
12 | 3593 gdk_draw_pixmap(html->html_area, |
3594 html->gc, | |
3595 html->html_area, | |
3596 0, 0, 0, diff0, width, height - diff0); | |
3597 | |
3598 rect.x = 0; | |
3599 rect.y = 0; | |
3600 rect.width = width; | |
3601 rect.height = MIN(height, diff0); | |
1 | 3602 } |
3603 | |
12 | 3604 expose_html(html, &rect, FALSE); |
3605 gtk_html_draw_focus((GtkWidget *) html); | |
1 | 3606 |
3607 } | |
3608 | |
3609 | |
3610 | |
12 | 3611 static void gtk_html_adjustment(GtkAdjustment * adjustment, GtkHtml * html) |
1 | 3612 { |
12 | 3613 g_return_if_fail(adjustment != NULL); |
3614 g_return_if_fail(GTK_IS_ADJUSTMENT(adjustment)); | |
3615 g_return_if_fail(html != NULL); | |
3616 g_return_if_fail(GTK_IS_HTML(html)); | |
3617 | |
3618 /* | |
3619 * Just ignore it if we haven't been size-allocated and realized yet | |
3620 */ | |
3621 if (html->html_area == NULL) | |
3622 return; | |
3623 | |
3624 if (adjustment == html->hadj) | |
3625 { | |
3626 g_warning("horizontal scrolling not implemented"); | |
3627 } | |
3628 else | |
3629 { | |
3630 gint diff = ((gint) adjustment->value) - html->last_ver_value; | |
3631 | |
3632 if (diff != 0) | |
3633 { | |
3634 /* | |
3635 * undraw_cursor (text, FALSE); | |
3636 */ | |
3637 | |
3638 if (diff > 0) | |
3639 { | |
3640 scroll_down(html, diff); | |
3641 } | |
3642 else | |
3643 { /* | |
3644 * if (diff < 0) | |
3645 */ | |
3646 scroll_up(html, -diff); | |
3647 } | |
3648 /* | |
3649 * draw_cursor (text, FALSE); | |
3650 */ | |
3651 | |
3652 html->last_ver_value = adjustment->value; | |
3653 } | |
3654 } | |
1 | 3655 } |
12 | 3656 |
3657 static gint gtk_html_visibility_notify(GtkWidget * widget, | |
3658 GdkEventVisibility * event) | |
1 | 3659 { |
3660 GtkHtml *html; | |
3661 GdkRectangle rect; | |
12 | 3662 gint width, |
3663 height; | |
3664 | |
3665 g_return_val_if_fail(widget != NULL, FALSE); | |
3666 g_return_val_if_fail(GTK_IS_HTML(widget), FALSE); | |
3667 | |
3668 html = GTK_HTML(widget); | |
3669 | |
3670 if (GTK_WIDGET_REALIZED(widget) && html->transparent) | |
3671 { | |
3672 gdk_window_get_size(html->html_area, &width, &height); | |
3673 rect.x = 0; | |
3674 rect.y = 0; | |
3675 rect.width = width; | |
3676 rect.height = height; | |
3677 expose_html(html, &rect, FALSE); | |
3678 gtk_html_draw_focus((GtkWidget *) html); | |
3679 } | |
3680 else | |
3681 { | |
1 | 3682 } |
3683 | |
3684 | |
12 | 3685 return FALSE; |
1 | 3686 } |
3687 | |
3688 | |
3689 | |
12 | 3690 static void gtk_html_disconnect(GtkAdjustment * adjustment, GtkHtml * html) |
1 | 3691 { |
12 | 3692 g_return_if_fail(adjustment != NULL); |
3693 g_return_if_fail(GTK_IS_ADJUSTMENT(adjustment)); | |
3694 g_return_if_fail(html != NULL); | |
3695 g_return_if_fail(GTK_IS_HTML(html)); | |
3696 | |
3697 if (adjustment == html->hadj) | |
3698 gtk_html_set_adjustments(html, NULL, html->vadj); | |
3699 if (adjustment == html->vadj) | |
3700 gtk_html_set_adjustments(html, html->hadj, NULL); | |
1 | 3701 } |
3702 | |
12 | 3703 static void move_cursor_ver(GtkHtml * html, int count) |
1 | 3704 { |
3705 GList *hbits = g_list_find(html->html_bits, html->cursor_hb); | |
12 | 3706 GtkHtmlBit *hb = NULL, |
3707 *hb2 = NULL; | |
1 | 3708 gint y; |
79 | 3709 size_t len, |
12 | 3710 len2 = 0; |
3711 | |
1 | 3712 undraw_cursor(html); |
3713 | |
3714 if (!html->html_bits) | |
3715 return; | |
12 | 3716 |
1 | 3717 if (!html->cursor_hb) |
12 | 3718 html->cursor_hb = (GtkHtmlBit *) html->html_bits->data; |
1 | 3719 |
3720 hb = html->cursor_hb; | |
3721 | |
3722 len = html->cursor_pos; | |
3723 hbits = hbits->prev; | |
12 | 3724 while (hbits) |
3725 { | |
3726 hb2 = (GtkHtmlBit *) hbits->data; | |
1 | 3727 |
3728 if (hb2->y != hb->y) | |
3729 break; | |
3730 | |
12 | 3731 len += strlen(hb2->text); |
3732 | |
1 | 3733 hbits = hbits->prev; |
3734 } | |
3735 | |
12 | 3736 hbits = g_list_find(html->html_bits, html->cursor_hb); |
3737 | |
3738 if (count < 0) | |
3739 { | |
3740 while (hbits) | |
3741 { | |
3742 hb2 = (GtkHtmlBit *) hbits->data; | |
1 | 3743 |
3744 if (hb2->y != hb->y) | |
3745 break; | |
12 | 3746 |
1 | 3747 hbits = hbits->prev; |
3748 } | |
12 | 3749 if (!hbits) |
3750 { | |
1 | 3751 draw_cursor(html); |
3752 return; | |
3753 } | |
3754 y = hb2->y; | |
3755 hb = hb2; | |
12 | 3756 while (hbits) |
3757 { | |
3758 hb2 = (GtkHtmlBit *) hbits->data; | |
1 | 3759 |
3760 if (hb2->y != y) | |
3761 break; | |
3762 | |
3763 hb = hb2; | |
12 | 3764 |
1 | 3765 hbits = hbits->prev; |
3766 } | |
3767 hbits = g_list_find(html->html_bits, hb); | |
12 | 3768 while (hbits) |
3769 { | |
3770 hb2 = (GtkHtmlBit *) hbits->data; | |
3771 | |
3772 if (hb->y != hb2->y) | |
3773 { | |
1 | 3774 html->cursor_hb = hb; |
3775 html->cursor_pos = strlen(hb->text); | |
12 | 3776 break; |
1 | 3777 } |
3778 | |
3779 | |
12 | 3780 if (len < len2 + strlen(hb2->text)) |
3781 { | |
1 | 3782 html->cursor_hb = hb2; |
3783 html->cursor_pos = len - len2; | |
3784 break; | |
3785 } | |
3786 | |
3787 len2 += strlen(hb2->text); | |
3788 | |
3789 hb = hb2; | |
3790 | |
12 | 3791 hbits = hbits->next; |
1 | 3792 } |
12 | 3793 } |
3794 else | |
3795 { | |
3796 while (hbits) | |
3797 { | |
3798 hb2 = (GtkHtmlBit *) hbits->data; | |
1 | 3799 |
3800 if (hb2->y != hb->y) | |
3801 break; | |
12 | 3802 |
1 | 3803 hbits = hbits->next; |
3804 } | |
12 | 3805 if (!hbits) |
3806 { | |
1 | 3807 draw_cursor(html); |
3808 return; | |
3809 } | |
3810 hb = hb2; | |
12 | 3811 while (hbits) |
3812 { | |
3813 hb2 = (GtkHtmlBit *) hbits->data; | |
3814 | |
3815 if (hb->y != hb2->y) | |
3816 { | |
1 | 3817 html->cursor_hb = hb; |
3818 html->cursor_pos = strlen(hb->text); | |
12 | 3819 break; |
1 | 3820 } |
3821 | |
3822 | |
12 | 3823 if (len < len2 + strlen(hb2->text)) |
3824 { | |
1 | 3825 html->cursor_hb = hb2; |
3826 html->cursor_pos = len - len2; | |
3827 break; | |
3828 } | |
3829 | |
3830 len2 += strlen(hb2->text); | |
3831 | |
3832 hb = hb2; | |
3833 | |
12 | 3834 hbits = hbits->next; |
1 | 3835 } |
3836 } | |
3837 | |
3838 draw_cursor(html); | |
3839 | |
3840 } | |
3841 | |
12 | 3842 static void move_cursor_hor(GtkHtml * html, int count) |
1 | 3843 { |
3844 GList *hbits = g_list_find(html->html_bits, html->cursor_hb); | |
12 | 3845 GtkHtmlBit *hb, |
3846 *hb2; | |
1 | 3847 |
3848 undraw_cursor(html); | |
3849 | |
3850 if (!html->html_bits) | |
3851 return; | |
12 | 3852 |
1 | 3853 if (!html->cursor_hb) |
12 | 3854 html->cursor_hb = (GtkHtmlBit *) html->html_bits->data; |
3855 | |
3856 html->cursor_pos += count; | |
3857 | |
3858 if (html->cursor_pos < 0) | |
3859 { | |
3860 if (hbits->prev) | |
3861 { | |
1 | 3862 gint diff; |
3863 hb = html->cursor_hb; | |
12 | 3864 hb2 = (GtkHtmlBit *) hbits->prev->data; |
1 | 3865 diff = html->cursor_pos + strlen(hb2->text) + 1; |
3866 if (hb->y == hb2->y) | |
3867 --diff; | |
12 | 3868 |
1 | 3869 html->cursor_pos = diff; |
12 | 3870 |
3871 html->cursor_hb = (GtkHtmlBit *) hbits->prev->data; | |
3872 } | |
3873 else | |
3874 { | |
1 | 3875 html->cursor_pos = 0; |
3876 } | |
12 | 3877 } |
79 | 3878 else if ((unsigned) html->cursor_pos > strlen(html->cursor_hb->text)) |
12 | 3879 { |
3880 if (hbits->next) | |
3881 { | |
1 | 3882 gint diff; |
3883 hb = html->cursor_hb; | |
12 | 3884 hb2 = (GtkHtmlBit *) hbits->next->data; |
1 | 3885 |
3886 diff = html->cursor_pos - strlen(html->cursor_hb->text) - 1; | |
3887 if (hb->y == hb2->y) | |
12 | 3888 ++diff; |
1 | 3889 html->cursor_pos = diff; |
12 | 3890 html->cursor_hb = (GtkHtmlBit *) hbits->next->data; |
3891 } | |
3892 else | |
3893 { | |
1 | 3894 html->cursor_pos = strlen(html->cursor_hb->text); |
3895 } | |
3896 | |
3897 } | |
3898 | |
3899 draw_cursor(html); | |
3900 } | |
3901 | |
12 | 3902 static void move_beginning_of_line(GtkHtml * html) |
1 | 3903 { |
3904 GList *hbits = g_list_find(html->html_bits, html->cursor_hb); | |
3905 GtkHtmlBit *hb = NULL; | |
12 | 3906 gint y; |
3907 | |
1 | 3908 undraw_cursor(html); |
3909 | |
3910 if (!html->html_bits) | |
3911 return; | |
3912 | |
3913 if (!html->cursor_hb) | |
12 | 3914 html->cursor_hb = (GtkHtmlBit *) html->html_bits->data; |
1 | 3915 |
3916 y = html->cursor_hb->y; | |
12 | 3917 |
3918 while (hbits) | |
3919 { | |
3920 hb = (GtkHtmlBit *) hbits->data; | |
3921 | |
3922 if (y != hb->y) | |
3923 { | |
3924 hb = (GtkHtmlBit *) hbits->next->data; | |
1 | 3925 break; |
3926 } | |
12 | 3927 |
1 | 3928 hbits = hbits->prev; |
3929 } | |
3930 if (!hbits) | |
12 | 3931 html->cursor_hb = (GtkHtmlBit *) html->html_bits->data; |
1 | 3932 else |
3933 html->cursor_hb = hb; | |
3934 | |
3935 html->cursor_pos = 0; | |
3936 | |
3937 | |
3938 draw_cursor(html); | |
3939 | |
3940 | |
3941 } | |
3942 | |
12 | 3943 static void move_end_of_line(GtkHtml * html) |
1 | 3944 { |
3945 GList *hbits = g_list_find(html->html_bits, html->cursor_hb); | |
3946 GtkHtmlBit *hb = NULL; | |
12 | 3947 gint y; |
3948 | |
1 | 3949 undraw_cursor(html); |
3950 | |
3951 if (!html->html_bits) | |
3952 return; | |
3953 | |
3954 if (!html->cursor_hb) | |
12 | 3955 html->cursor_hb = (GtkHtmlBit *) html->html_bits->data; |
1 | 3956 |
3957 y = html->cursor_hb->y; | |
12 | 3958 |
3959 while (hbits) | |
3960 { | |
3961 hb = (GtkHtmlBit *) hbits->data; | |
3962 | |
3963 if (y != hb->y) | |
3964 { | |
3965 hb = (GtkHtmlBit *) hbits->prev->data; | |
1 | 3966 break; |
3967 } | |
12 | 3968 |
1 | 3969 hbits = hbits->next; |
3970 } | |
3971 if (!hbits) | |
12 | 3972 html->cursor_hb = (GtkHtmlBit *) g_list_last(html->html_bits)->data; |
1 | 3973 else |
3974 html->cursor_hb = hb; | |
3975 | |
3976 html->cursor_pos = strlen(html->cursor_hb->text); | |
3977 | |
3978 | |
3979 draw_cursor(html); | |
3980 | |
3981 | |
3982 } | |
3983 | |
3984 | |
3985 | |
12 | 3986 static gint gtk_html_key_press(GtkWidget * widget, GdkEventKey * event) |
1 | 3987 { |
3988 GtkHtml *html; | |
3989 gchar key; | |
3990 gint return_val; | |
12 | 3991 |
3992 g_return_val_if_fail(widget != NULL, FALSE); | |
3993 g_return_val_if_fail(GTK_IS_HTML(widget), FALSE); | |
3994 g_return_val_if_fail(event != NULL, FALSE); | |
3995 | |
1 | 3996 return_val = FALSE; |
12 | 3997 |
3998 html = GTK_HTML(widget); | |
3999 | |
1 | 4000 key = event->keyval; |
4001 return_val = TRUE; | |
4002 | |
4003 | |
12 | 4004 if (html->editable == FALSE) |
4005 { | |
4006 /* | |
4007 * switch (event->keyval) { | |
4008 * case GDK_Home: | |
4009 * if (event->state & GDK_CONTROL_MASK) | |
4010 * scroll_int (text, -text->vadj->value); | |
4011 * else | |
4012 * return_val = FALSE; | |
4013 * break; | |
4014 * case GDK_End: | |
4015 * if (event->state & GDK_CONTROL_MASK) | |
4016 * scroll_int (text, +text->vadj->upper); | |
4017 * else | |
4018 * return_val = FALSE; | |
4019 * break; | |
4020 * case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break; | |
4021 * case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break; | |
4022 * case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break; | |
4023 * case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break; | |
4024 * case GDK_Return: | |
4025 * if (event->state & GDK_CONTROL_MASK) | |
4026 * gtk_signal_emit_by_name (GTK_OBJECT (text), "activate"); | |
4027 * else | |
4028 * return_val = FALSE; | |
4029 * break; | |
4030 * default: | |
4031 * return_val = FALSE; | |
4032 * break; | |
4033 * } | |
4034 */ | |
4035 } | |
4036 else | |
4037 { | |
4038 | |
4039 switch (event->keyval) | |
4040 { | |
1 | 4041 case GDK_Home: |
12 | 4042 move_beginning_of_line(html); |
1 | 4043 break; |
4044 case GDK_End: | |
12 | 4045 move_end_of_line(html); |
1 | 4046 break; |
4047 /* | |
12 | 4048 * case GDK_Page_Up: |
4049 * move_cursor_page_ver (html, -1); | |
4050 * break; | |
4051 * case GDK_Page_Down: | |
4052 * move_cursor_page_ver (html, +1); | |
4053 * break; | |
4054 */ | |
4055 /* | |
4056 * CUA has Ctrl-Up/Ctrl-Down as paragraph up down | |
4057 */ | |
1 | 4058 case GDK_Up: |
12 | 4059 move_cursor_ver(html, -1); |
1 | 4060 break; |
4061 case GDK_Down: | |
12 | 4062 move_cursor_ver(html, +1); |
1 | 4063 break; |
4064 case GDK_Left: | |
12 | 4065 move_cursor_hor(html, -1); |
1 | 4066 break; |
4067 case GDK_Right: | |
12 | 4068 move_cursor_hor(html, +1); |
1 | 4069 break; |
4070 #if 0 | |
4071 case GDK_BackSpace: | |
4072 if (event->state & GDK_CONTROL_MASK) | |
12 | 4073 gtk_text_delete_backward_word(text); |
1 | 4074 else |
12 | 4075 gtk_text_delete_backward_character(text); |
1 | 4076 break; |
4077 case GDK_Clear: | |
12 | 4078 gtk_text_delete_line(text); |
1 | 4079 break; |
4080 case GDK_Insert: | |
4081 if (event->state & GDK_SHIFT_MASK) | |
4082 { | |
4083 extend_selection = FALSE; | |
12 | 4084 gtk_editable_paste_clipboard(editable); |
1 | 4085 } |
4086 else if (event->state & GDK_CONTROL_MASK) | |
4087 { | |
12 | 4088 gtk_editable_copy_clipboard(editable); |
1 | 4089 } |
4090 else | |
4091 { | |
12 | 4092 /* |
4093 * gtk_toggle_insert(text) -- IMPLEMENT | |
4094 */ | |
1 | 4095 } |
4096 break; | |
4097 case GDK_Delete: | |
4098 if (event->state & GDK_CONTROL_MASK) | |
12 | 4099 gtk_text_delete_forward_word(text); |
1 | 4100 else if (event->state & GDK_SHIFT_MASK) |
4101 { | |
4102 extend_selection = FALSE; | |
12 | 4103 gtk_editable_cut_clipboard(editable); |
1 | 4104 } |
4105 else | |
12 | 4106 gtk_text_delete_forward_character(text); |
1 | 4107 break; |
4108 case GDK_Tab: | |
4109 position = text->point.index; | |
12 | 4110 gtk_editable_insert_text(editable, "\t", 1, &position); |
1 | 4111 break; |
4112 case GDK_Return: | |
4113 if (event->state & GDK_CONTROL_MASK) | |
12 | 4114 gtk_signal_emit_by_name(GTK_OBJECT(text), "activate"); |
1 | 4115 else |
4116 { | |
4117 position = text->point.index; | |
12 | 4118 gtk_editable_insert_text(editable, "\n", 1, &position); |
1 | 4119 } |
4120 break; | |
4121 case GDK_Escape: | |
12 | 4122 /* |
4123 * Don't insert literally | |
4124 */ | |
1 | 4125 return_val = FALSE; |
4126 break; | |
4127 #endif | |
4128 default: | |
4129 return_val = FALSE; | |
4130 | |
4131 #if 0 | |
12 | 4132 if (event->state & GDK_CONTROL_MASK) |
4133 { | |
1 | 4134 if ((key >= 'A') && (key <= 'Z')) |
4135 key -= 'A' - 'a'; | |
4136 | |
12 | 4137 if ((key >= 'a') && (key <= 'z') |
4138 && control_keys[(int) (key - 'a')]) | |
1 | 4139 { |
12 | 4140 (*control_keys[(int) (key - 'a')]) (editable, event->time); |
1 | 4141 return_val = TRUE; |
4142 } | |
4143 | |
4144 break; | |
4145 } | |
4146 else if (event->state & GDK_MOD1_MASK) | |
4147 { | |
4148 if ((key >= 'A') && (key <= 'Z')) | |
4149 key -= 'A' - 'a'; | |
4150 | |
4151 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')]) | |
4152 { | |
12 | 4153 (*alt_keys[(int) (key - 'a')]) (editable, event->time); |
1 | 4154 return_val = TRUE; |
4155 } | |
4156 break; | |
4157 } | |
4158 #endif | |
4159 /* | |
12 | 4160 * if (event->length > 0) { |
4161 * html->cursor_pos++; | |
4162 * gtk_editable_insert_text (editable, event->string, event->length, &position); | |
4163 * | |
4164 * return_val = TRUE; | |
4165 * } | |
4166 * else | |
4167 * return_val = FALSE; | |
4168 */ | |
1 | 4169 } |
4170 | |
4171 } | |
4172 | |
4173 return return_val; | |
4174 } | |
12 | 4175 |
4176 void gtk_html_freeze(GtkHtml * html) | |
1 | 4177 { |
12 | 4178 g_return_if_fail(html != NULL); |
4179 g_return_if_fail(GTK_IS_HTML(html)); | |
1 | 4180 |
4181 html->frozen++; | |
4182 } | |
4183 | |
12 | 4184 void gtk_html_thaw(GtkHtml * html) |
1 | 4185 { |
4186 GdkRectangle area; | |
12 | 4187 |
4188 g_return_if_fail(html != NULL); | |
4189 g_return_if_fail(GTK_IS_HTML(html)); | |
1 | 4190 |
4191 html->frozen--; | |
4192 | |
4193 if (html->frozen < 0) | |
12 | 4194 html->frozen = 0; |
4195 | |
4196 if (html->frozen == 0) | |
4197 { | |
4198 if (html->html_area) | |
4199 { | |
4200 gint width, | |
4201 height; | |
1 | 4202 area.x = 0; |
4203 area.y = 0; | |
4204 | |
4205 gdk_window_get_size(html->html_area, &width, &height); | |
4206 | |
12 | 4207 area.width = width; |
4208 area.height = height; | |
4209 | |
1 | 4210 expose_html(html, &area, TRUE); |
4211 } | |
4212 } | |
4213 } |