1 /* GTK - The GIMP Toolkit
2 * gtktextlayout.c - calculate the layout of the text
3 *
4 * Copyright (c) 1992-1994 The Regents of the University of California.
5 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
6 * Copyright (c) 2000 Red Hat, Inc.
7 * Tk->Gtk port by Havoc Pennington
8 * Pango support by Owen Taylor
9 *
10 * This file can be used under your choice of two licenses, the LGPL
11 * and the original Tk license.
12 *
13 * LGPL:
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 * Original Tk license:
30 *
31 * This software is copyrighted by the Regents of the University of
32 * California, Sun Microsystems, Inc., and other parties. The
33 * following terms apply to all files associated with the software
34 * unless explicitly disclaimed in individual files.
35 *
36 * The authors hereby grant permission to use, copy, modify,
37 * distribute, and license this software and its documentation for any
38 * purpose, provided that existing copyright notices are retained in
39 * all copies and that this notice is included verbatim in any
40 * distributions. No written agreement, license, or royalty fee is
41 * required for any of the authorized uses. Modifications to this
42 * software may be copyrighted by their authors and need not follow
43 * the licensing terms described here, provided that the new terms are
44 * clearly indicated on the first page of each file where they apply.
45 *
46 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
47 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
48 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
49 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 *
52 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
54 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
55 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
56 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
57 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
58 *
59 * GOVERNMENT USE: If you are acquiring this software on behalf of the
60 * U.S. government, the Government shall have only "Restricted Rights"
61 * in the software and related documentation as defined in the Federal
62 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
63 * are acquiring the software on behalf of the Department of Defense,
64 * the software shall be classified as "Commercial Computer Software"
65 * and the Government shall have only "Restricted Rights" as defined
66 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
67 * foregoing, the authors grant the U.S. Government and others acting
68 * in its behalf permission to use and distribute the software in
69 * accordance with the terms specified in this license.
70 *
71 */
72 /*
73 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
74 * file for a list of people on the GTK+ Team. See the ChangeLog
75 * files for a list of changes. These files are distributed with
76 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
77 */
78
79 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
80 #include "config.h"
81 #include "gtkmarshalers.h"
82 #include "gtktextlayout.h"
83 #include "gtktextbtree.h"
84 #include "gtktextiterprivate.h"
85 #include "gtktextutil.h"
86 #include "gtkintl.h"
87 #include "gtkalias.h"
88
89 #include <stdlib.h>
90 #include <string.h>
91
92 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
93
94 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
95
96 struct _GtkTextLayoutPrivate
97 {
98 /* Cache the line that the cursor is positioned on, as the keyboard
99 direction only influences the direction of the cursor line.
100 */
101 GtkTextLine *cursor_line;
102 };
103
104 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
105 GtkTextLine *line,
106 /* may be NULL */
107 GtkTextLineData *line_data);
108
109 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
110
111 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
112 const GtkTextIter *start,
113 const GtkTextIter *end);
114 static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout *layout,
115 const GtkTextIter *start,
116 const GtkTextIter *end);
117 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
118 GtkTextLine *line,
119 gboolean cursors_only);
120 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
121 gboolean cursors_only);
122 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
123 GtkTextLine *line,
124 GtkTextLineData *line_data);
125 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
126 gint y,
127 gint old_height,
128 gint new_height);
129
130 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
131
132 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
133
134 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
135 const GtkTextIter *location,
136 GtkTextMark *mark,
137 gpointer data);
138 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
139 GtkTextIter *iter,
140 gchar *str,
141 gint len,
142 gpointer data);
143 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
144 GtkTextIter *start,
145 GtkTextIter *end,
146 gpointer data);
147
148 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
149
150 static void line_display_index_to_iter (GtkTextLayout *layout,
151 GtkTextLineDisplay *display,
152 GtkTextIter *iter,
153 gint index,
154 gint trailing);
155
156 static gint line_display_iter_to_index (GtkTextLayout *layout,
157 GtkTextLineDisplay *display,
158 const GtkTextIter *iter);
159
160 enum {
161 INVALIDATED,
162 CHANGED,
163 ALLOCATE_CHILD,
164 LAST_SIGNAL
165 };
166
167 enum {
168 ARG_0,
169 LAST_ARG
170 };
171
172 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
173
174 static void gtk_text_layout_finalize (GObject *object);
175
176 static guint signals[LAST_SIGNAL] = { 0 };
177
178 PangoAttrType gtk_text_attr_appearance_type = 0;
179
G_DEFINE_TYPE(GtkTextLayout,gtk_text_layout,G_TYPE_OBJECT)180 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
181
182 static void
183 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
184 {
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
186
187 object_class->finalize = gtk_text_layout_finalize;
188
189 klass->wrap = gtk_text_layout_real_wrap;
190 klass->invalidate = gtk_text_layout_real_invalidate;
191 klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
192 klass->free_line_data = gtk_text_layout_real_free_line_data;
193
194 signals[INVALIDATED] =
195 g_signal_new (I_("invalidated"),
196 G_OBJECT_CLASS_TYPE (object_class),
197 G_SIGNAL_RUN_LAST,
198 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
199 NULL, NULL,
200 _gtk_marshal_VOID__VOID,
201 G_TYPE_NONE,
202 0);
203
204 signals[CHANGED] =
205 g_signal_new (I_("changed"),
206 G_OBJECT_CLASS_TYPE (object_class),
207 G_SIGNAL_RUN_LAST,
208 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
209 NULL, NULL,
210 _gtk_marshal_VOID__INT_INT_INT,
211 G_TYPE_NONE,
212 3,
213 G_TYPE_INT,
214 G_TYPE_INT,
215 G_TYPE_INT);
216
217 signals[ALLOCATE_CHILD] =
218 g_signal_new (I_("allocate-child"),
219 G_OBJECT_CLASS_TYPE (object_class),
220 G_SIGNAL_RUN_LAST,
221 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
222 NULL, NULL,
223 _gtk_marshal_VOID__OBJECT_INT_INT,
224 G_TYPE_NONE,
225 3,
226 GTK_TYPE_OBJECT,
227 G_TYPE_INT,
228 G_TYPE_INT);
229
230 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
231 }
232
233 static void
gtk_text_layout_init(GtkTextLayout * text_layout)234 gtk_text_layout_init (GtkTextLayout *text_layout)
235 {
236 text_layout->cursor_visible = TRUE;
237 }
238
239 GtkTextLayout*
gtk_text_layout_new(void)240 gtk_text_layout_new (void)
241 {
242 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
243 }
244
245 static void
free_style_cache(GtkTextLayout * text_layout)246 free_style_cache (GtkTextLayout *text_layout)
247 {
248 if (text_layout->one_style_cache)
249 {
250 gtk_text_attributes_unref (text_layout->one_style_cache);
251 text_layout->one_style_cache = NULL;
252 }
253 }
254
255 static void
gtk_text_layout_finalize(GObject * object)256 gtk_text_layout_finalize (GObject *object)
257 {
258 GtkTextLayout *layout;
259
260 layout = GTK_TEXT_LAYOUT (object);
261
262 gtk_text_layout_set_buffer (layout, NULL);
263
264 if (layout->default_style)
265 gtk_text_attributes_unref (layout->default_style);
266 layout->default_style = NULL;
267
268 if (layout->ltr_context)
269 {
270 g_object_unref (layout->ltr_context);
271 layout->ltr_context = NULL;
272 }
273 if (layout->rtl_context)
274 {
275 g_object_unref (layout->rtl_context);
276 layout->rtl_context = NULL;
277 }
278
279 if (layout->one_display_cache)
280 {
281 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
282 layout->one_display_cache = NULL;
283 gtk_text_layout_free_line_display (layout, tmp_display);
284 }
285
286 if (layout->preedit_string)
287 {
288 g_free (layout->preedit_string);
289 layout->preedit_string = NULL;
290 }
291
292 if (layout->preedit_attrs)
293 {
294 pango_attr_list_unref (layout->preedit_attrs);
295 layout->preedit_attrs = NULL;
296 }
297
298
299 G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
300 }
301
302 /**
303 * gtk_text_layout_set_buffer:
304 * @buffer: (allow-none):
305 */
306 void
gtk_text_layout_set_buffer(GtkTextLayout * layout,GtkTextBuffer * buffer)307 gtk_text_layout_set_buffer (GtkTextLayout *layout,
308 GtkTextBuffer *buffer)
309 {
310 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
311 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
312
313 if (layout->buffer == buffer)
314 return;
315
316 free_style_cache (layout);
317
318 if (layout->buffer)
319 {
320 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
321 layout);
322
323 g_signal_handlers_disconnect_by_func (layout->buffer,
324 G_CALLBACK (gtk_text_layout_mark_set_handler),
325 layout);
326 g_signal_handlers_disconnect_by_func (layout->buffer,
327 G_CALLBACK (gtk_text_layout_buffer_insert_text),
328 layout);
329 g_signal_handlers_disconnect_by_func (layout->buffer,
330 G_CALLBACK (gtk_text_layout_buffer_delete_range),
331 layout);
332
333 g_object_unref (layout->buffer);
334 layout->buffer = NULL;
335 }
336
337 if (buffer)
338 {
339 layout->buffer = buffer;
340
341 g_object_ref (buffer);
342
343 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
344
345 /* Bind to all signals that move the insert mark. */
346 g_signal_connect_after (layout->buffer, "mark-set",
347 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
348 g_signal_connect_after (layout->buffer, "insert-text",
349 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
350 g_signal_connect_after (layout->buffer, "delete-range",
351 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
352
353 gtk_text_layout_update_cursor_line (layout);
354 }
355 }
356
357 void
gtk_text_layout_default_style_changed(GtkTextLayout * layout)358 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
359 {
360 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
361
362 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
363 gtk_text_layout_invalidate_all (layout);
364 }
365
366 void
gtk_text_layout_set_default_style(GtkTextLayout * layout,GtkTextAttributes * values)367 gtk_text_layout_set_default_style (GtkTextLayout *layout,
368 GtkTextAttributes *values)
369 {
370 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
371 g_return_if_fail (values != NULL);
372
373 if (values == layout->default_style)
374 return;
375
376 gtk_text_attributes_ref (values);
377
378 if (layout->default_style)
379 gtk_text_attributes_unref (layout->default_style);
380
381 layout->default_style = values;
382
383 gtk_text_layout_default_style_changed (layout);
384 }
385
386 void
gtk_text_layout_set_contexts(GtkTextLayout * layout,PangoContext * ltr_context,PangoContext * rtl_context)387 gtk_text_layout_set_contexts (GtkTextLayout *layout,
388 PangoContext *ltr_context,
389 PangoContext *rtl_context)
390 {
391 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
392
393 if (layout->ltr_context != ltr_context)
394 {
395 if (layout->ltr_context)
396 g_object_unref (layout->ltr_context);
397
398 layout->ltr_context = ltr_context;
399 g_object_ref (layout->ltr_context);
400 }
401
402 if (layout->rtl_context != rtl_context)
403 {
404 if (layout->rtl_context)
405 g_object_unref (layout->rtl_context);
406
407 layout->rtl_context = rtl_context;
408 g_object_ref (layout->rtl_context);
409 }
410
411 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
412 gtk_text_layout_invalidate_all (layout);
413 }
414
415 /**
416 * gtk_text_layout_set_overwrite_mode:
417 * @layout: a #GtkTextLayout
418 * @overwrite: overwrite mode
419 *
420 * Sets overwrite mode
421 */
422 void
gtk_text_layout_set_overwrite_mode(GtkTextLayout * layout,gboolean overwrite)423 gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
424 gboolean overwrite)
425 {
426 overwrite = overwrite != 0;
427 if (overwrite != layout->overwrite_mode)
428 {
429 layout->overwrite_mode = overwrite;
430 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
431 }
432 }
433
434 /**
435 * gtk_text_layout_set_cursor_direction:
436 * @direction: the new direction(s) for which to draw cursors.
437 * %GTK_TEXT_DIR_NONE means draw cursors for both
438 * left-to-right insertion and right-to-left insertion.
439 * (The two cursors will be visually distinguished.)
440 *
441 * Sets which text directions (left-to-right and/or right-to-left) for
442 * which cursors will be drawn for the insertion point. The visual
443 * point at which new text is inserted depends on whether the new
444 * text is right-to-left or left-to-right, so it may be desired to
445 * make the drawn position of the cursor depend on the keyboard state.
446 */
447 void
gtk_text_layout_set_cursor_direction(GtkTextLayout * layout,GtkTextDirection direction)448 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
449 GtkTextDirection direction)
450 {
451 if (direction != layout->cursor_direction)
452 {
453 layout->cursor_direction = direction;
454 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
455 }
456 }
457
458 /**
459 * gtk_text_layout_set_keyboard_direction:
460 * @keyboard_dir: the current direction of the keyboard.
461 *
462 * Sets the keyboard direction; this is used as for the bidirectional
463 * base direction for the line with the cursor if the line contains
464 * only neutral characters.
465 */
466 void
gtk_text_layout_set_keyboard_direction(GtkTextLayout * layout,GtkTextDirection keyboard_dir)467 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
468 GtkTextDirection keyboard_dir)
469 {
470 if (keyboard_dir != layout->keyboard_direction)
471 {
472 layout->keyboard_direction = keyboard_dir;
473 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
474 }
475 }
476
477 /**
478 * gtk_text_layout_get_buffer:
479 * @layout: a #GtkTextLayout
480 *
481 * Gets the text buffer used by the layout. See
482 * gtk_text_layout_set_buffer().
483 *
484 * Return value: the text buffer used by the layout.
485 */
486 GtkTextBuffer *
gtk_text_layout_get_buffer(GtkTextLayout * layout)487 gtk_text_layout_get_buffer (GtkTextLayout *layout)
488 {
489 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
490
491 return layout->buffer;
492 }
493
494 void
gtk_text_layout_set_screen_width(GtkTextLayout * layout,gint width)495 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
496 {
497 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
498 g_return_if_fail (width >= 0);
499 g_return_if_fail (layout->wrap_loop_count == 0);
500
501 if (layout->screen_width == width)
502 return;
503
504 layout->screen_width = width;
505
506 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
507 gtk_text_layout_invalidate_all (layout);
508 }
509
510 /**
511 * gtk_text_layout_set_cursor_visible:
512 * @layout: a #GtkTextLayout
513 * @cursor_visible: If %FALSE, then the insertion cursor will not
514 * be shown, even if the text is editable.
515 *
516 * Sets whether the insertion cursor should be shown. Generally,
517 * widgets using #GtkTextLayout will hide the cursor when the
518 * widget does not have the input focus.
519 */
520 void
gtk_text_layout_set_cursor_visible(GtkTextLayout * layout,gboolean cursor_visible)521 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
522 gboolean cursor_visible)
523 {
524 cursor_visible = (cursor_visible != FALSE);
525
526 if (layout->cursor_visible != cursor_visible)
527 {
528 GtkTextIter iter;
529 gint y, height;
530
531 layout->cursor_visible = cursor_visible;
532
533 /* Now queue a redraw on the paragraph containing the cursor
534 */
535 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
536 gtk_text_buffer_get_insert (layout->buffer));
537
538 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
539 gtk_text_layout_emit_changed (layout, y, height, height);
540
541 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
542 }
543 }
544
545 /**
546 * gtk_text_layout_get_cursor_visible:
547 * @layout: a #GtkTextLayout
548 *
549 * Returns whether the insertion cursor will be shown.
550 *
551 * Return value: if %FALSE, the insertion cursor will not be
552 * shown, even if the text is editable.
553 */
554 gboolean
gtk_text_layout_get_cursor_visible(GtkTextLayout * layout)555 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
556 {
557 return layout->cursor_visible;
558 }
559
560 /**
561 * gtk_text_layout_set_preedit_string:
562 * @layout: a #PangoLayout
563 * @preedit_string: a string to display at the insertion point
564 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
565 * @cursor_pos: position of cursor within preedit string in chars
566 *
567 * Set the preedit string and attributes. The preedit string is a
568 * string showing text that is currently being edited and not
569 * yet committed into the buffer.
570 */
571 void
gtk_text_layout_set_preedit_string(GtkTextLayout * layout,const gchar * preedit_string,PangoAttrList * preedit_attrs,gint cursor_pos)572 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
573 const gchar *preedit_string,
574 PangoAttrList *preedit_attrs,
575 gint cursor_pos)
576 {
577 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
578 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
579
580 g_free (layout->preedit_string);
581
582 if (layout->preedit_attrs)
583 pango_attr_list_unref (layout->preedit_attrs);
584
585 if (preedit_string)
586 {
587 layout->preedit_string = g_strdup (preedit_string);
588 layout->preedit_len = strlen (layout->preedit_string);
589 pango_attr_list_ref (preedit_attrs);
590 layout->preedit_attrs = preedit_attrs;
591
592 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
593 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
594 }
595 else
596 {
597 layout->preedit_string = NULL;
598 layout->preedit_len = 0;
599 layout->preedit_attrs = NULL;
600 layout->preedit_cursor = 0;
601 }
602
603 gtk_text_layout_invalidate_cursor_line (layout, FALSE);
604 }
605
606 void
gtk_text_layout_get_size(GtkTextLayout * layout,gint * width,gint * height)607 gtk_text_layout_get_size (GtkTextLayout *layout,
608 gint *width,
609 gint *height)
610 {
611 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
612
613 if (width)
614 *width = layout->width;
615
616 if (height)
617 *height = layout->height;
618 }
619
620 static void
gtk_text_layout_invalidated(GtkTextLayout * layout)621 gtk_text_layout_invalidated (GtkTextLayout *layout)
622 {
623 g_signal_emit (layout, signals[INVALIDATED], 0);
624 }
625
626 static void
gtk_text_layout_emit_changed(GtkTextLayout * layout,gint y,gint old_height,gint new_height)627 gtk_text_layout_emit_changed (GtkTextLayout *layout,
628 gint y,
629 gint old_height,
630 gint new_height)
631 {
632 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
633 }
634
635 static void
text_layout_changed(GtkTextLayout * layout,gint y,gint old_height,gint new_height,gboolean cursors_only)636 text_layout_changed (GtkTextLayout *layout,
637 gint y,
638 gint old_height,
639 gint new_height,
640 gboolean cursors_only)
641 {
642 /* Check if the range intersects our cached line display,
643 * and invalidate the cached line if so.
644 */
645 if (layout->one_display_cache)
646 {
647 GtkTextLine *line = layout->one_display_cache->line;
648 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
649 line, layout);
650 gint cache_height = layout->one_display_cache->height;
651
652 if (cache_y + cache_height > y && cache_y < y + old_height)
653 gtk_text_layout_invalidate_cache (layout, line, cursors_only);
654 }
655
656 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
657 }
658
659 void
gtk_text_layout_changed(GtkTextLayout * layout,gint y,gint old_height,gint new_height)660 gtk_text_layout_changed (GtkTextLayout *layout,
661 gint y,
662 gint old_height,
663 gint new_height)
664 {
665 text_layout_changed (layout, y, old_height, new_height, FALSE);
666 }
667
668 void
gtk_text_layout_cursors_changed(GtkTextLayout * layout,gint y,gint old_height,gint new_height)669 gtk_text_layout_cursors_changed (GtkTextLayout *layout,
670 gint y,
671 gint old_height,
672 gint new_height)
673 {
674 text_layout_changed (layout, y, old_height, new_height, TRUE);
675 }
676
677 void
gtk_text_layout_free_line_data(GtkTextLayout * layout,GtkTextLine * line,GtkTextLineData * line_data)678 gtk_text_layout_free_line_data (GtkTextLayout *layout,
679 GtkTextLine *line,
680 GtkTextLineData *line_data)
681 {
682 GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
683 }
684
685 void
gtk_text_layout_invalidate(GtkTextLayout * layout,const GtkTextIter * start_index,const GtkTextIter * end_index)686 gtk_text_layout_invalidate (GtkTextLayout *layout,
687 const GtkTextIter *start_index,
688 const GtkTextIter *end_index)
689 {
690 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
691 }
692
693 void
gtk_text_layout_invalidate_cursors(GtkTextLayout * layout,const GtkTextIter * start_index,const GtkTextIter * end_index)694 gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
695 const GtkTextIter *start_index,
696 const GtkTextIter *end_index)
697 {
698 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
699 }
700
701 GtkTextLineData*
gtk_text_layout_wrap(GtkTextLayout * layout,GtkTextLine * line,GtkTextLineData * line_data)702 gtk_text_layout_wrap (GtkTextLayout *layout,
703 GtkTextLine *line,
704 /* may be NULL */
705 GtkTextLineData *line_data)
706 {
707 return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
708 }
709
710
711 /**
712 * gtk_text_layout_get_lines:
713 *
714 * Return value: (element-type GtkTextLine) (transfer container):
715 */
716 GSList*
gtk_text_layout_get_lines(GtkTextLayout * layout,gint top_y,gint bottom_y,gint * first_line_y)717 gtk_text_layout_get_lines (GtkTextLayout *layout,
718 /* [top_y, bottom_y) */
719 gint top_y,
720 gint bottom_y,
721 gint *first_line_y)
722 {
723 GtkTextLine *first_btree_line;
724 GtkTextLine *last_btree_line;
725 GtkTextLine *line;
726 GSList *retval;
727
728 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
729 g_return_val_if_fail (bottom_y > top_y, NULL);
730
731 retval = NULL;
732
733 first_btree_line =
734 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
735 layout, top_y, first_line_y);
736 if (first_btree_line == NULL)
737 {
738 /* off the bottom */
739 return NULL;
740 }
741
742 /* -1 since bottom_y is one past */
743 last_btree_line =
744 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
745 layout, bottom_y - 1, NULL);
746
747 if (!last_btree_line)
748 last_btree_line =
749 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
750
751 g_assert (last_btree_line != NULL);
752
753 line = first_btree_line;
754 while (TRUE)
755 {
756 retval = g_slist_prepend (retval, line);
757
758 if (line == last_btree_line)
759 break;
760
761 line = _gtk_text_line_next_excluding_last (line);
762 }
763
764 retval = g_slist_reverse (retval);
765
766 return retval;
767 }
768
769 static void
invalidate_cached_style(GtkTextLayout * layout)770 invalidate_cached_style (GtkTextLayout *layout)
771 {
772 free_style_cache (layout);
773 }
774
775 /* These should be called around a loop which wraps a CONTIGUOUS bunch
776 * of display lines. If the lines aren't contiguous you can't call
777 * these.
778 */
779 void
gtk_text_layout_wrap_loop_start(GtkTextLayout * layout)780 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
781 {
782 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
783 g_return_if_fail (layout->one_style_cache == NULL);
784
785 layout->wrap_loop_count += 1;
786 }
787
788 void
gtk_text_layout_wrap_loop_end(GtkTextLayout * layout)789 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
790 {
791 g_return_if_fail (layout->wrap_loop_count > 0);
792
793 layout->wrap_loop_count -= 1;
794
795 if (layout->wrap_loop_count == 0)
796 {
797 /* We cache a some stuff if we're iterating over some lines wrapping
798 * them. This cleans it up.
799 */
800 /* Nuke our cached style */
801 invalidate_cached_style (layout);
802 g_assert (layout->one_style_cache == NULL);
803 }
804 }
805
806 static void
gtk_text_layout_invalidate_all(GtkTextLayout * layout)807 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
808 {
809 GtkTextIter start;
810 GtkTextIter end;
811
812 if (layout->buffer == NULL)
813 return;
814
815 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
816
817 gtk_text_layout_invalidate (layout, &start, &end);
818 }
819
820 static void
gtk_text_layout_invalidate_cache(GtkTextLayout * layout,GtkTextLine * line,gboolean cursors_only)821 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
822 GtkTextLine *line,
823 gboolean cursors_only)
824 {
825 if (layout->one_display_cache && line == layout->one_display_cache->line)
826 {
827 GtkTextLineDisplay *display = layout->one_display_cache;
828
829 if (cursors_only)
830 {
831 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
832 g_slist_free (display->cursors);
833 display->cursors = NULL;
834 display->cursors_invalid = TRUE;
835 display->has_block_cursor = FALSE;
836 }
837 else
838 {
839 layout->one_display_cache = NULL;
840 gtk_text_layout_free_line_display (layout, display);
841 }
842 }
843 }
844
845 /* Now invalidate the paragraph containing the cursor
846 */
847 static void
gtk_text_layout_invalidate_cursor_line(GtkTextLayout * layout,gboolean cursors_only)848 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
849 gboolean cursors_only)
850 {
851 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
852 GtkTextLineData *line_data;
853
854 if (priv->cursor_line == NULL)
855 return;
856
857 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
858 if (line_data)
859 {
860 if (cursors_only)
861 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
862 else
863 {
864 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
865 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
866 }
867
868 gtk_text_layout_invalidated (layout);
869 }
870 }
871
872 static void
gtk_text_layout_update_cursor_line(GtkTextLayout * layout)873 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
874 {
875 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
876 GtkTextIter iter;
877
878 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
879 gtk_text_buffer_get_insert (layout->buffer));
880
881 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
882 }
883
884 static void
gtk_text_layout_real_invalidate(GtkTextLayout * layout,const GtkTextIter * start,const GtkTextIter * end)885 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
886 const GtkTextIter *start,
887 const GtkTextIter *end)
888 {
889 GtkTextLine *line;
890 GtkTextLine *last_line;
891
892 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
893 g_return_if_fail (layout->wrap_loop_count == 0);
894
895 /* Because we may be invalidating a mark, it's entirely possible
896 * that gtk_text_iter_equal (start, end) in which case we
897 * should still invalidate the line they are both on. i.e.
898 * we always invalidate the line with "start" even
899 * if there's an empty range.
900 */
901
902 #if 0
903 gtk_text_view_index_spew (start_index, "invalidate start");
904 gtk_text_view_index_spew (end_index, "invalidate end");
905 #endif
906
907 last_line = _gtk_text_iter_get_text_line (end);
908 line = _gtk_text_iter_get_text_line (start);
909
910 while (TRUE)
911 {
912 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
913
914 gtk_text_layout_invalidate_cache (layout, line, FALSE);
915
916 if (line_data)
917 _gtk_text_line_invalidate_wrap (line, line_data);
918
919 if (line == last_line)
920 break;
921
922 line = _gtk_text_line_next_excluding_last (line);
923 }
924
925 gtk_text_layout_invalidated (layout);
926 }
927
928 static void
gtk_text_layout_real_invalidate_cursors(GtkTextLayout * layout,const GtkTextIter * start,const GtkTextIter * end)929 gtk_text_layout_real_invalidate_cursors (GtkTextLayout *layout,
930 const GtkTextIter *start,
931 const GtkTextIter *end)
932 {
933 /* Check if the range intersects our cached line display,
934 * and invalidate the cached line if so.
935 */
936 if (layout->one_display_cache)
937 {
938 GtkTextIter line_start, line_end;
939 GtkTextLine *line = layout->one_display_cache->line;
940
941 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
942 &line_start, line, 0);
943 line_end = line_start;
944 if (!gtk_text_iter_ends_line (&line_end))
945 gtk_text_iter_forward_to_line_end (&line_end);
946
947 if (gtk_text_iter_compare (start, end) > 0)
948 {
949 const GtkTextIter *tmp = start;
950 start = end;
951 end = tmp;
952 }
953
954 if (gtk_text_iter_compare (&line_start, end) <= 0 &&
955 gtk_text_iter_compare (start, &line_end) <= 0)
956 {
957 gtk_text_layout_invalidate_cache (layout, line, TRUE);
958 }
959 }
960
961 gtk_text_layout_invalidated (layout);
962 }
963
964 static void
gtk_text_layout_real_free_line_data(GtkTextLayout * layout,GtkTextLine * line,GtkTextLineData * line_data)965 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
966 GtkTextLine *line,
967 GtkTextLineData *line_data)
968 {
969 gtk_text_layout_invalidate_cache (layout, line, FALSE);
970
971 g_free (line_data);
972 }
973
974 /**
975 * gtk_text_layout_is_valid:
976 * @layout: a #GtkTextLayout
977 *
978 * Check if there are any invalid regions in a #GtkTextLayout's buffer
979 *
980 * Return value: %TRUE if any invalid regions were found
981 */
982 gboolean
gtk_text_layout_is_valid(GtkTextLayout * layout)983 gtk_text_layout_is_valid (GtkTextLayout *layout)
984 {
985 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
986
987 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
988 layout);
989 }
990
991 static void
update_layout_size(GtkTextLayout * layout)992 update_layout_size (GtkTextLayout *layout)
993 {
994 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
995 layout,
996 &layout->width, &layout->height);
997 }
998
999 /**
1000 * gtk_text_layout_validate_yrange:
1001 * @layout: a #GtkTextLayout
1002 * @anchor: iter pointing into a line that will be used as the
1003 * coordinate origin
1004 * @y0_: offset from the top of the line pointed to by @anchor at
1005 * which to begin validation. (The offset here is in pixels
1006 * after validation.)
1007 * @y1_: offset from the top of the line pointed to by @anchor at
1008 * which to end validation. (The offset here is in pixels
1009 * after validation.)
1010 *
1011 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
1012 * signal will be emitted if any lines are validated.
1013 */
1014 void
gtk_text_layout_validate_yrange(GtkTextLayout * layout,GtkTextIter * anchor,gint y0,gint y1)1015 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1016 GtkTextIter *anchor,
1017 gint y0,
1018 gint y1)
1019 {
1020 GtkTextLine *line;
1021 GtkTextLine *first_line = NULL;
1022 GtkTextLine *last_line = NULL;
1023 gint seen;
1024 gint delta_height = 0;
1025 gint first_line_y = 0; /* Quiet GCC */
1026 gint last_line_y = 0; /* Quiet GCC */
1027
1028 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1029
1030 if (y0 > 0)
1031 y0 = 0;
1032 if (y1 < 0)
1033 y1 = 0;
1034
1035 /* Validate backwards from the anchor line to y0
1036 */
1037 line = _gtk_text_iter_get_text_line (anchor);
1038 line = _gtk_text_line_previous (line);
1039 seen = 0;
1040 while (line && seen < -y0)
1041 {
1042 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1043 if (!line_data || !line_data->valid)
1044 {
1045 gint old_height, new_height;
1046
1047 old_height = line_data ? line_data->height : 0;
1048
1049 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1050 line, layout);
1051 line_data = _gtk_text_line_get_data (line, layout);
1052
1053 new_height = line_data ? line_data->height : 0;
1054
1055 delta_height += new_height - old_height;
1056
1057 first_line = line;
1058 first_line_y = -seen - new_height;
1059 if (!last_line)
1060 {
1061 last_line = line;
1062 last_line_y = -seen;
1063 }
1064 }
1065
1066 seen += line_data ? line_data->height : 0;
1067 line = _gtk_text_line_previous (line);
1068 }
1069
1070 /* Validate forwards to y1 */
1071 line = _gtk_text_iter_get_text_line (anchor);
1072 seen = 0;
1073 while (line && seen < y1)
1074 {
1075 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1076 if (!line_data || !line_data->valid)
1077 {
1078 gint old_height, new_height;
1079
1080 old_height = line_data ? line_data->height : 0;
1081
1082 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1083 line, layout);
1084 line_data = _gtk_text_line_get_data (line, layout);
1085 new_height = line_data ? line_data->height : 0;
1086
1087 delta_height += new_height - old_height;
1088
1089 if (!first_line)
1090 {
1091 first_line = line;
1092 first_line_y = seen;
1093 }
1094 last_line = line;
1095 last_line_y = seen + new_height;
1096 }
1097
1098 seen += line_data ? line_data->height : 0;
1099 line = _gtk_text_line_next_excluding_last (line);
1100 }
1101
1102 /* If we found and validated any invalid lines, update size and
1103 * emit the changed signal
1104 */
1105 if (first_line)
1106 {
1107 gint line_top;
1108
1109 update_layout_size (layout);
1110
1111 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1112 first_line, layout);
1113
1114 gtk_text_layout_emit_changed (layout,
1115 line_top,
1116 last_line_y - first_line_y - delta_height,
1117 last_line_y - first_line_y);
1118 }
1119 }
1120
1121 /**
1122 * gtk_text_layout_validate:
1123 * @tree: a #GtkTextLayout
1124 * @max_pixels: the maximum number of pixels to validate. (No more
1125 * than one paragraph beyond this limit will be validated)
1126 *
1127 * Validate regions of a #GtkTextLayout. The ::changed signal will
1128 * be emitted for each region validated.
1129 **/
1130 void
gtk_text_layout_validate(GtkTextLayout * layout,gint max_pixels)1131 gtk_text_layout_validate (GtkTextLayout *layout,
1132 gint max_pixels)
1133 {
1134 gint y, old_height, new_height;
1135
1136 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1137
1138 while (max_pixels > 0 &&
1139 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1140 layout, max_pixels,
1141 &y, &old_height, &new_height))
1142 {
1143 max_pixels -= new_height;
1144
1145 update_layout_size (layout);
1146 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1147 }
1148 }
1149
1150 static GtkTextLineData*
gtk_text_layout_real_wrap(GtkTextLayout * layout,GtkTextLine * line,GtkTextLineData * line_data)1151 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1152 GtkTextLine *line,
1153 /* may be NULL */
1154 GtkTextLineData *line_data)
1155 {
1156 GtkTextLineDisplay *display;
1157
1158 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1159 g_return_val_if_fail (line != NULL, NULL);
1160
1161 if (line_data == NULL)
1162 {
1163 line_data = _gtk_text_line_data_new (layout, line);
1164 _gtk_text_line_add_data (line, line_data);
1165 }
1166
1167 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1168 line_data->width = display->width;
1169 line_data->height = display->height;
1170 line_data->valid = TRUE;
1171 gtk_text_layout_free_line_display (layout, display);
1172
1173 return line_data;
1174 }
1175
1176 /*
1177 * Layout utility functions
1178 */
1179
1180 /* If you get the style with get_style () you need to call
1181 release_style () to free it. */
1182 static GtkTextAttributes*
get_style(GtkTextLayout * layout,GPtrArray * tags)1183 get_style (GtkTextLayout *layout,
1184 GPtrArray *tags)
1185 {
1186 GtkTextAttributes *style;
1187
1188 /* If we have the one-style cache, then it means
1189 that we haven't seen a toggle since we filled in the
1190 one-style cache.
1191 */
1192 if (layout->one_style_cache != NULL)
1193 {
1194 gtk_text_attributes_ref (layout->one_style_cache);
1195 return layout->one_style_cache;
1196 }
1197
1198 g_assert (layout->one_style_cache == NULL);
1199
1200 /* No tags, use default style */
1201 if (tags == NULL || tags->len == 0)
1202 {
1203 /* One ref for the return value, one ref for the
1204 layout->one_style_cache reference */
1205 gtk_text_attributes_ref (layout->default_style);
1206 gtk_text_attributes_ref (layout->default_style);
1207 layout->one_style_cache = layout->default_style;
1208
1209 return layout->default_style;
1210 }
1211
1212 style = gtk_text_attributes_new ();
1213
1214 gtk_text_attributes_copy_values (layout->default_style,
1215 style);
1216
1217 _gtk_text_attributes_fill_from_tags (style,
1218 (GtkTextTag**) tags->pdata,
1219 tags->len);
1220
1221 g_assert (style->refcount == 1);
1222
1223 /* Leave this style as the last one seen */
1224 g_assert (layout->one_style_cache == NULL);
1225 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1226 layout->one_style_cache = style;
1227
1228 /* Returning yet another refcount */
1229 return style;
1230 }
1231
1232 static void
release_style(GtkTextLayout * layout,GtkTextAttributes * style)1233 release_style (GtkTextLayout *layout,
1234 GtkTextAttributes *style)
1235 {
1236 g_return_if_fail (style != NULL);
1237 g_return_if_fail (style->refcount > 0);
1238
1239 gtk_text_attributes_unref (style);
1240 }
1241
1242 /*
1243 * Lines
1244 */
1245
1246 /* This function tries to optimize the case where a line
1247 is completely invisible */
1248 static gboolean
totally_invisible_line(GtkTextLayout * layout,GtkTextLine * line,GtkTextIter * iter)1249 totally_invisible_line (GtkTextLayout *layout,
1250 GtkTextLine *line,
1251 GtkTextIter *iter)
1252 {
1253 GtkTextLineSegment *seg;
1254 int bytes = 0;
1255
1256 /* Check if the first char is visible, if so we are partially visible.
1257 * Note that we have to check this since we don't know the current
1258 * invisible/noninvisible toggle state; this function can use the whole btree
1259 * to get it right.
1260 */
1261 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1262 iter, line, 0);
1263
1264 if (!_gtk_text_btree_char_is_invisible (iter))
1265 return FALSE;
1266
1267 bytes = 0;
1268 seg = line->segments;
1269
1270 while (seg != NULL)
1271 {
1272 if (seg->byte_count > 0)
1273 bytes += seg->byte_count;
1274
1275 /* Note that these two tests can cause us to bail out
1276 * when we shouldn't, because a higher-priority tag
1277 * may override these settings. However the important
1278 * thing is to only invisible really-invisible lines, rather
1279 * than to invisible all really-invisible lines.
1280 */
1281
1282 else if (seg->type == >k_text_toggle_on_type)
1283 {
1284 invalidate_cached_style (layout);
1285
1286 /* Bail out if an elision-unsetting tag begins */
1287 if (seg->body.toggle.info->tag->invisible_set &&
1288 !seg->body.toggle.info->tag->values->invisible)
1289 break;
1290 }
1291 else if (seg->type == >k_text_toggle_off_type)
1292 {
1293 invalidate_cached_style (layout);
1294
1295 /* Bail out if an elision-setting tag ends */
1296 if (seg->body.toggle.info->tag->invisible_set &&
1297 seg->body.toggle.info->tag->values->invisible)
1298 break;
1299 }
1300
1301 seg = seg->next;
1302 }
1303
1304 if (seg != NULL) /* didn't reach line end */
1305 return FALSE;
1306
1307 return TRUE;
1308 }
1309
1310 static void
set_para_values(GtkTextLayout * layout,PangoDirection base_dir,GtkTextAttributes * style,GtkTextLineDisplay * display)1311 set_para_values (GtkTextLayout *layout,
1312 PangoDirection base_dir,
1313 GtkTextAttributes *style,
1314 GtkTextLineDisplay *display)
1315 {
1316 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1317 PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1318
1319 switch (base_dir)
1320 {
1321 /* If no base direction was found, then use the style direction */
1322 case PANGO_DIRECTION_NEUTRAL :
1323 display->direction = style->direction;
1324
1325 /* Override the base direction */
1326 if (display->direction == GTK_TEXT_DIR_RTL)
1327 base_dir = PANGO_DIRECTION_RTL;
1328 else
1329 base_dir = PANGO_DIRECTION_LTR;
1330
1331 break;
1332 case PANGO_DIRECTION_RTL :
1333 display->direction = GTK_TEXT_DIR_RTL;
1334 break;
1335 default:
1336 display->direction = GTK_TEXT_DIR_LTR;
1337 break;
1338 }
1339
1340 if (display->direction == GTK_TEXT_DIR_RTL)
1341 display->layout = pango_layout_new (layout->rtl_context);
1342 else
1343 display->layout = pango_layout_new (layout->ltr_context);
1344
1345 switch (style->justification)
1346 {
1347 case GTK_JUSTIFY_LEFT:
1348 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1349 break;
1350 case GTK_JUSTIFY_RIGHT:
1351 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1352 break;
1353 case GTK_JUSTIFY_CENTER:
1354 pango_align = PANGO_ALIGN_CENTER;
1355 break;
1356 case GTK_JUSTIFY_FILL:
1357 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1358 pango_layout_set_justify (display->layout, TRUE);
1359 break;
1360 default:
1361 g_assert_not_reached ();
1362 break;
1363 }
1364
1365 pango_layout_set_alignment (display->layout, pango_align);
1366 pango_layout_set_spacing (display->layout,
1367 style->pixels_inside_wrap * PANGO_SCALE);
1368
1369 if (style->tabs)
1370 pango_layout_set_tabs (display->layout, style->tabs);
1371
1372 display->top_margin = style->pixels_above_lines;
1373 display->height = style->pixels_above_lines + style->pixels_below_lines;
1374 display->bottom_margin = style->pixels_below_lines;
1375 display->left_margin = style->left_margin;
1376 display->right_margin = style->right_margin;
1377
1378 display->x_offset = display->left_margin;
1379
1380 pango_layout_set_indent (display->layout,
1381 style->indent * PANGO_SCALE);
1382
1383 switch (style->wrap_mode)
1384 {
1385 case GTK_WRAP_CHAR:
1386 pango_wrap = PANGO_WRAP_CHAR;
1387 break;
1388 case GTK_WRAP_WORD:
1389 pango_wrap = PANGO_WRAP_WORD;
1390 break;
1391
1392 case GTK_WRAP_WORD_CHAR:
1393 pango_wrap = PANGO_WRAP_WORD_CHAR;
1394 break;
1395
1396 case GTK_WRAP_NONE:
1397 break;
1398 }
1399
1400 if (style->wrap_mode != GTK_WRAP_NONE)
1401 {
1402 int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
1403 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1404 pango_layout_set_wrap (display->layout, pango_wrap);
1405 }
1406
1407 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1408
1409 if (style->pg_bg_color)
1410 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1411 else
1412 display->pg_bg_color = NULL;
1413 }
1414
1415 static PangoAttribute *
gtk_text_attr_appearance_copy(const PangoAttribute * attr)1416 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1417 {
1418 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1419
1420 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1421 }
1422
1423 static void
gtk_text_attr_appearance_destroy(PangoAttribute * attr)1424 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1425 {
1426 GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1427
1428 if (appearance_attr->appearance.bg_stipple)
1429 g_object_unref (appearance_attr->appearance.bg_stipple);
1430 if (appearance_attr->appearance.fg_stipple)
1431 g_object_unref (appearance_attr->appearance.fg_stipple);
1432
1433 g_slice_free (GtkTextAttrAppearance, appearance_attr);
1434 }
1435
1436 static gboolean
gtk_text_attr_appearance_compare(const PangoAttribute * attr1,const PangoAttribute * attr2)1437 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1438 const PangoAttribute *attr2)
1439 {
1440 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1441 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1442
1443 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1444 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1445 appearance1->fg_stipple == appearance2->fg_stipple &&
1446 appearance1->bg_stipple == appearance2->bg_stipple &&
1447 appearance1->underline == appearance2->underline &&
1448 appearance1->strikethrough == appearance2->strikethrough &&
1449 appearance1->draw_bg == appearance2->draw_bg);
1450 }
1451
1452 /*
1453 * gtk_text_attr_appearance_new:
1454 * @desc:
1455 *
1456 * Create a new font description attribute. (This attribute
1457 * allows setting family, style, weight, variant, stretch,
1458 * and size simultaneously.)
1459 *
1460 * Return value:
1461 */
1462 static PangoAttribute *
gtk_text_attr_appearance_new(const GtkTextAppearance * appearance)1463 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1464 {
1465 static PangoAttrClass klass = {
1466 0,
1467 gtk_text_attr_appearance_copy,
1468 gtk_text_attr_appearance_destroy,
1469 gtk_text_attr_appearance_compare
1470 };
1471
1472 GtkTextAttrAppearance *result;
1473
1474 if (!klass.type)
1475 klass.type = gtk_text_attr_appearance_type =
1476 pango_attr_type_register ("GtkTextAttrAppearance");
1477
1478 result = g_slice_new (GtkTextAttrAppearance);
1479 result->attr.klass = &klass;
1480
1481 result->appearance = *appearance;
1482
1483 if (appearance->bg_stipple)
1484 g_object_ref (appearance->bg_stipple);
1485 if (appearance->fg_stipple)
1486 g_object_ref (appearance->fg_stipple);
1487
1488 return (PangoAttribute *)result;
1489 }
1490
1491 static void
add_generic_attrs(GtkTextLayout * layout,GtkTextAppearance * appearance,gint byte_count,PangoAttrList * attrs,gint start,gboolean size_only,gboolean is_text)1492 add_generic_attrs (GtkTextLayout *layout,
1493 GtkTextAppearance *appearance,
1494 gint byte_count,
1495 PangoAttrList *attrs,
1496 gint start,
1497 gboolean size_only,
1498 gboolean is_text)
1499 {
1500 PangoAttribute *attr;
1501
1502 if (appearance->underline != PANGO_UNDERLINE_NONE)
1503 {
1504 attr = pango_attr_underline_new (appearance->underline);
1505
1506 attr->start_index = start;
1507 attr->end_index = start + byte_count;
1508
1509 pango_attr_list_insert (attrs, attr);
1510 }
1511
1512 if (appearance->strikethrough)
1513 {
1514 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1515
1516 attr->start_index = start;
1517 attr->end_index = start + byte_count;
1518
1519 pango_attr_list_insert (attrs, attr);
1520 }
1521
1522 if (appearance->rise != 0)
1523 {
1524 attr = pango_attr_rise_new (appearance->rise);
1525
1526 attr->start_index = start;
1527 attr->end_index = start + byte_count;
1528
1529 pango_attr_list_insert (attrs, attr);
1530 }
1531
1532 if (!size_only)
1533 {
1534 attr = gtk_text_attr_appearance_new (appearance);
1535
1536 attr->start_index = start;
1537 attr->end_index = start + byte_count;
1538
1539 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1540
1541 pango_attr_list_insert (attrs, attr);
1542 }
1543 }
1544
1545 static void
add_text_attrs(GtkTextLayout * layout,GtkTextAttributes * style,gint byte_count,PangoAttrList * attrs,gint start,gboolean size_only)1546 add_text_attrs (GtkTextLayout *layout,
1547 GtkTextAttributes *style,
1548 gint byte_count,
1549 PangoAttrList *attrs,
1550 gint start,
1551 gboolean size_only)
1552 {
1553 PangoAttribute *attr;
1554
1555 attr = pango_attr_font_desc_new (style->font);
1556 attr->start_index = start;
1557 attr->end_index = start + byte_count;
1558
1559 pango_attr_list_insert (attrs, attr);
1560
1561 if (style->font_scale != 1.0)
1562 {
1563 attr = pango_attr_scale_new (style->font_scale);
1564
1565 attr->start_index = start;
1566 attr->end_index = start + byte_count;
1567
1568 pango_attr_list_insert (attrs, attr);
1569 }
1570 }
1571
1572 static void
add_pixbuf_attrs(GtkTextLayout * layout,GtkTextLineDisplay * display,GtkTextAttributes * style,GtkTextLineSegment * seg,PangoAttrList * attrs,gint start)1573 add_pixbuf_attrs (GtkTextLayout *layout,
1574 GtkTextLineDisplay *display,
1575 GtkTextAttributes *style,
1576 GtkTextLineSegment *seg,
1577 PangoAttrList *attrs,
1578 gint start)
1579 {
1580 PangoAttribute *attr;
1581 PangoRectangle logical_rect;
1582 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1583 gint width, height;
1584
1585 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1586 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1587
1588 logical_rect.x = 0;
1589 logical_rect.y = -height * PANGO_SCALE;
1590 logical_rect.width = width * PANGO_SCALE;
1591 logical_rect.height = height * PANGO_SCALE;
1592
1593 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1594 pixbuf->pixbuf, NULL, NULL);
1595 attr->start_index = start;
1596 attr->end_index = start + seg->byte_count;
1597 pango_attr_list_insert (attrs, attr);
1598
1599 display->shaped_objects =
1600 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1601 }
1602
1603 static void
add_child_attrs(GtkTextLayout * layout,GtkTextLineDisplay * display,GtkTextAttributes * style,GtkTextLineSegment * seg,PangoAttrList * attrs,gint start)1604 add_child_attrs (GtkTextLayout *layout,
1605 GtkTextLineDisplay *display,
1606 GtkTextAttributes *style,
1607 GtkTextLineSegment *seg,
1608 PangoAttrList *attrs,
1609 gint start)
1610 {
1611 PangoAttribute *attr;
1612 PangoRectangle logical_rect;
1613 gint width, height;
1614 GSList *tmp_list;
1615 GtkWidget *widget;
1616
1617 width = 1;
1618 height = 1;
1619
1620 tmp_list = seg->body.child.widgets;
1621 while (tmp_list != NULL)
1622 {
1623 GtkWidget *child = tmp_list->data;
1624
1625 if (_gtk_anchored_child_get_layout (child) == layout)
1626 {
1627 /* Found it */
1628 GtkRequisition req;
1629
1630 gtk_widget_get_child_requisition (child, &req);
1631
1632 width = req.width;
1633 height = req.height;
1634
1635 widget = child;
1636
1637 break;
1638 }
1639
1640 tmp_list = g_slist_next (tmp_list);
1641 }
1642
1643 if (tmp_list == NULL)
1644 {
1645 /* If tmp_list == NULL then there is no widget at this anchor in
1646 * this display; not an error. We make up an arbitrary size
1647 * to use, just so the programmer can see the blank spot.
1648 * We also put a NULL in the shaped objects list, to keep
1649 * the correspondence between the list and the shaped chars in
1650 * the layout. A bad hack, yes.
1651 */
1652
1653 width = 30;
1654 height = 20;
1655
1656 widget = NULL;
1657 }
1658
1659 display->shaped_objects = g_slist_append (display->shaped_objects, widget);
1660
1661 logical_rect.x = 0;
1662 logical_rect.y = -height * PANGO_SCALE;
1663 logical_rect.width = width * PANGO_SCALE;
1664 logical_rect.height = height * PANGO_SCALE;
1665
1666 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1667 widget, NULL, NULL);
1668 attr->start_index = start;
1669 attr->end_index = start + seg->byte_count;
1670 pango_attr_list_insert (attrs, attr);
1671 }
1672
1673 /*
1674 * get_block_cursor:
1675 * @layout: a #GtkTextLayout
1676 * @display: a #GtkTextLineDisplay
1677 * @insert_iter: iter pointing to the cursor location
1678 * @insert_index: cursor offset in the @display's layout, it may
1679 * be different from @insert_iter's offset in case when preedit
1680 * string is present.
1681 * @pos: location to store cursor position
1682 * @cursor_at_line_end: whether cursor is at the end of line
1683 *
1684 * Checks whether layout should display block cursor at given position.
1685 * For this layout must be in overwrite mode and text at @insert_iter
1686 * must be editable.
1687 */
1688 static gboolean
get_block_cursor(GtkTextLayout * layout,GtkTextLineDisplay * display,const GtkTextIter * insert_iter,gint insert_index,GdkRectangle * pos,gboolean * cursor_at_line_end)1689 get_block_cursor (GtkTextLayout *layout,
1690 GtkTextLineDisplay *display,
1691 const GtkTextIter *insert_iter,
1692 gint insert_index,
1693 GdkRectangle *pos,
1694 gboolean *cursor_at_line_end)
1695 {
1696 PangoRectangle pango_pos;
1697
1698 if (layout->overwrite_mode &&
1699 gtk_text_iter_editable (insert_iter, TRUE) &&
1700 _gtk_text_util_get_block_cursor_location (display->layout,
1701 insert_index,
1702 &pango_pos,
1703 cursor_at_line_end))
1704 {
1705 if (pos)
1706 {
1707 pos->x = PANGO_PIXELS (pango_pos.x);
1708 pos->y = PANGO_PIXELS (pango_pos.y);
1709 pos->width = PANGO_PIXELS (pango_pos.width);
1710 pos->height = PANGO_PIXELS (pango_pos.height);
1711 }
1712
1713 return TRUE;
1714 }
1715 else
1716 return FALSE;
1717 }
1718
1719 static void
add_cursor(GtkTextLayout * layout,GtkTextLineDisplay * display,GtkTextLineSegment * seg,gint start)1720 add_cursor (GtkTextLayout *layout,
1721 GtkTextLineDisplay *display,
1722 GtkTextLineSegment *seg,
1723 gint start)
1724 {
1725 PangoRectangle strong_pos, weak_pos;
1726 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1727 gboolean add_weak = FALSE;
1728 gboolean add_strong = FALSE;
1729
1730 /* Hide insertion cursor when we have a selection or the layout
1731 * user has hidden the cursor.
1732 */
1733 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1734 seg->body.mark.obj) &&
1735 (!layout->cursor_visible ||
1736 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1737 return;
1738
1739 if (layout->overwrite_mode &&
1740 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1741 seg->body.mark.obj))
1742 {
1743 GtkTextIter iter;
1744 gboolean cursor_at_line_end;
1745
1746 _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
1747 &iter, seg->body.mark.obj);
1748
1749 if (get_block_cursor (layout, display, &iter, start,
1750 &display->block_cursor,
1751 &cursor_at_line_end))
1752 {
1753 display->has_block_cursor = TRUE;
1754 display->cursor_at_line_end = cursor_at_line_end;
1755 return;
1756 }
1757 }
1758
1759 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1760
1761 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1762 {
1763 add_strong = TRUE;
1764 add_weak = TRUE;
1765 }
1766 else if (display->direction == layout->cursor_direction)
1767 add_strong = TRUE;
1768 else
1769 add_weak = TRUE;
1770
1771 if (add_strong)
1772 {
1773 cursor = g_new (GtkTextCursorDisplay, 1);
1774
1775 cursor->x = PANGO_PIXELS (strong_pos.x);
1776 cursor->y = PANGO_PIXELS (strong_pos.y);
1777 cursor->height = PANGO_PIXELS (strong_pos.height);
1778 cursor->is_strong = TRUE;
1779 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1780 display->cursors = g_slist_prepend (display->cursors, cursor);
1781 }
1782
1783 if (add_weak)
1784 {
1785 if (weak_pos.x == strong_pos.x && add_strong)
1786 cursor->is_weak = TRUE;
1787 else
1788 {
1789 cursor = g_new (GtkTextCursorDisplay, 1);
1790
1791 cursor->x = PANGO_PIXELS (weak_pos.x);
1792 cursor->y = PANGO_PIXELS (weak_pos.y);
1793 cursor->height = PANGO_PIXELS (weak_pos.height);
1794 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1795 cursor->is_weak = TRUE;
1796 display->cursors = g_slist_prepend (display->cursors, cursor);
1797 }
1798 }
1799 }
1800
1801 static gboolean
is_shape(PangoLayoutRun * run)1802 is_shape (PangoLayoutRun *run)
1803 {
1804 GSList *tmp_list = run->item->analysis.extra_attrs;
1805
1806 while (tmp_list)
1807 {
1808 PangoAttribute *attr = tmp_list->data;
1809
1810 if (attr->klass->type == PANGO_ATTR_SHAPE)
1811 return TRUE;
1812
1813 tmp_list = tmp_list->next;
1814 }
1815
1816 return FALSE;
1817 }
1818
1819 static void
allocate_child_widgets(GtkTextLayout * text_layout,GtkTextLineDisplay * display)1820 allocate_child_widgets (GtkTextLayout *text_layout,
1821 GtkTextLineDisplay *display)
1822 {
1823 PangoLayout *layout = display->layout;
1824 PangoLayoutIter *run_iter;
1825
1826 run_iter = pango_layout_get_iter (layout);
1827 do
1828 {
1829 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (run_iter);
1830
1831 if (run && is_shape (run))
1832 {
1833 gint byte_index;
1834 GtkTextIter text_iter;
1835 GtkTextChildAnchor *anchor = NULL;
1836 GList *widgets = NULL;
1837 GList *l;
1838
1839 /* The pango iterator iterates in visual order.
1840 * We use the byte index to find the child widget.
1841 */
1842 byte_index = pango_layout_iter_get_index (run_iter);
1843 line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
1844 anchor = gtk_text_iter_get_child_anchor (&text_iter);
1845 if (anchor)
1846 widgets = gtk_text_child_anchor_get_widgets (anchor);
1847
1848 for (l = widgets; l; l = l->next)
1849 {
1850 PangoRectangle extents;
1851 GtkWidget *child = l->data;
1852
1853 if (_gtk_anchored_child_get_layout (child) == text_layout)
1854 {
1855
1856 /* We emit "allocate_child" with the x,y of
1857 * the widget with respect to the top of the line
1858 * and the left side of the buffer
1859 */
1860 pango_layout_iter_get_run_extents (run_iter,
1861 NULL,
1862 &extents);
1863
1864 g_signal_emit (text_layout,
1865 signals[ALLOCATE_CHILD],
1866 0,
1867 child,
1868 PANGO_PIXELS (extents.x) + display->x_offset,
1869 PANGO_PIXELS (extents.y) + display->top_margin);
1870 }
1871 }
1872
1873 g_list_free (widgets);
1874 }
1875 }
1876 while (pango_layout_iter_next_run (run_iter));
1877
1878 pango_layout_iter_free (run_iter);
1879 }
1880
1881 static void
convert_color(GdkColor * result,PangoAttrColor * attr)1882 convert_color (GdkColor *result,
1883 PangoAttrColor *attr)
1884 {
1885 result->red = attr->color.red;
1886 result->blue = attr->color.blue;
1887 result->green = attr->color.green;
1888 }
1889
1890 /* This function is used to convert the preedit string attributes, which are
1891 * standard PangoAttributes, into the custom attributes used by the text
1892 * widget and insert them into a attr list with a given offset.
1893 */
1894 static void
add_preedit_attrs(GtkTextLayout * layout,GtkTextAttributes * style,PangoAttrList * attrs,gint offset,gboolean size_only)1895 add_preedit_attrs (GtkTextLayout *layout,
1896 GtkTextAttributes *style,
1897 PangoAttrList *attrs,
1898 gint offset,
1899 gboolean size_only)
1900 {
1901 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1902
1903 do
1904 {
1905 GtkTextAppearance appearance = style->appearance;
1906 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1907 PangoAttribute *insert_attr;
1908 GSList *extra_attrs = NULL;
1909 GSList *tmp_list;
1910 PangoLanguage *language;
1911 gint start, end;
1912
1913 pango_attr_iterator_range (iter, &start, &end);
1914
1915 if (end == G_MAXINT)
1916 end = layout->preedit_len;
1917
1918 if (end == start)
1919 continue;
1920
1921 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1922
1923 tmp_list = extra_attrs;
1924 while (tmp_list)
1925 {
1926 PangoAttribute *attr = tmp_list->data;
1927
1928 switch (attr->klass->type)
1929 {
1930 case PANGO_ATTR_FOREGROUND:
1931 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1932 break;
1933 case PANGO_ATTR_BACKGROUND:
1934 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1935 appearance.draw_bg = TRUE;
1936 break;
1937 case PANGO_ATTR_UNDERLINE:
1938 appearance.underline = ((PangoAttrInt *)attr)->value;
1939 break;
1940 case PANGO_ATTR_STRIKETHROUGH:
1941 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1942 break;
1943 case PANGO_ATTR_RISE:
1944 appearance.rise = ((PangoAttrInt *)attr)->value;
1945 break;
1946 default:
1947 break;
1948 }
1949
1950 pango_attribute_destroy (attr);
1951 tmp_list = tmp_list->next;
1952 }
1953
1954 g_slist_free (extra_attrs);
1955
1956 insert_attr = pango_attr_font_desc_new (font_desc);
1957 insert_attr->start_index = start + offset;
1958 insert_attr->end_index = end + offset;
1959
1960 pango_attr_list_insert (attrs, insert_attr);
1961
1962 if (language)
1963 {
1964 insert_attr = pango_attr_language_new (language);
1965 insert_attr->start_index = start + offset;
1966 insert_attr->end_index = end + offset;
1967
1968 pango_attr_list_insert (attrs, insert_attr);
1969 }
1970
1971 add_generic_attrs (layout, &appearance, end - start,
1972 attrs, start + offset,
1973 size_only, TRUE);
1974
1975 pango_font_description_free (font_desc);
1976 }
1977 while (pango_attr_iterator_next (iter));
1978
1979 pango_attr_iterator_destroy (iter);
1980 }
1981
1982 /* Iterate over the line and fill in display->cursors.
1983 * It's a stripped copy of gtk_text_layout_get_line_display() */
1984 static void
update_text_display_cursors(GtkTextLayout * layout,GtkTextLine * line,GtkTextLineDisplay * display)1985 update_text_display_cursors (GtkTextLayout *layout,
1986 GtkTextLine *line,
1987 GtkTextLineDisplay *display)
1988 {
1989 GtkTextLineSegment *seg;
1990 GtkTextIter iter;
1991 gint layout_byte_offset, buffer_byte_offset;
1992 GSList *cursor_byte_offsets = NULL;
1993 GSList *cursor_segs = NULL;
1994 GSList *tmp_list1, *tmp_list2;
1995
1996 if (!display->cursors_invalid)
1997 return;
1998
1999 display->cursors_invalid = FALSE;
2000
2001 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2002 &iter, line, 0);
2003
2004 /* Special-case optimization for completely
2005 * invisible lines; makes it faster to deal
2006 * with sequences of invisible lines.
2007 */
2008 if (totally_invisible_line (layout, line, &iter))
2009 return;
2010
2011 /* Iterate over segments */
2012 layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
2013 buffer_byte_offset = 0; /* position in the buffer line */
2014 seg = _gtk_text_iter_get_any_segment (&iter);
2015 while (seg != NULL)
2016 {
2017 /* Displayable segments */
2018 if (seg->type == >k_text_char_type ||
2019 seg->type == >k_text_pixbuf_type ||
2020 seg->type == >k_text_child_type)
2021 {
2022 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2023 &iter, line,
2024 buffer_byte_offset);
2025
2026 if (!_gtk_text_btree_char_is_invisible (&iter))
2027 layout_byte_offset += seg->byte_count;
2028
2029 buffer_byte_offset += seg->byte_count;
2030 }
2031
2032 /* Marks */
2033 else if (seg->type == >k_text_right_mark_type ||
2034 seg->type == >k_text_left_mark_type)
2035 {
2036 gint cursor_offset = 0;
2037
2038 /* At the insertion point, add the preedit string, if any */
2039
2040 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2041 seg->body.mark.obj))
2042 {
2043 display->insert_index = layout_byte_offset;
2044
2045 if (layout->preedit_len > 0)
2046 {
2047 layout_byte_offset += layout->preedit_len;
2048 /* DO NOT increment the buffer byte offset for preedit */
2049 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2050 }
2051 }
2052
2053 /* Display visible marks */
2054
2055 if (seg->body.mark.visible)
2056 {
2057 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2058 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2059 cursor_segs = g_slist_prepend (cursor_segs, seg);
2060 }
2061 }
2062
2063 /* Toggles */
2064 else if (seg->type == >k_text_toggle_on_type ||
2065 seg->type == >k_text_toggle_off_type)
2066 {
2067 }
2068
2069 else
2070 g_error ("Unknown segment type: %s", seg->type->name);
2071
2072 seg = seg->next;
2073 }
2074
2075 tmp_list1 = cursor_byte_offsets;
2076 tmp_list2 = cursor_segs;
2077 while (tmp_list1)
2078 {
2079 add_cursor (layout, display, tmp_list2->data,
2080 GPOINTER_TO_INT (tmp_list1->data));
2081 tmp_list1 = tmp_list1->next;
2082 tmp_list2 = tmp_list2->next;
2083 }
2084 g_slist_free (cursor_byte_offsets);
2085 g_slist_free (cursor_segs);
2086 }
2087
2088 /* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
2089 * to be used in gtk_text_layout_get_line_display(). */
2090 static GPtrArray *
get_tags_array_at_iter(GtkTextIter * iter)2091 get_tags_array_at_iter (GtkTextIter *iter)
2092 {
2093 GtkTextTag **tags;
2094 GPtrArray *array = NULL;
2095 gint n_tags;
2096
2097 tags = _gtk_text_btree_get_tags (iter, &n_tags);
2098
2099 if (n_tags > 0)
2100 {
2101 array = g_ptr_array_sized_new (n_tags);
2102 g_ptr_array_set_size (array, n_tags);
2103 memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
2104 }
2105
2106 g_free (tags);
2107 return array;
2108 }
2109
2110 /* Add the tag to the array if it's not there already, and remove
2111 * it otherwise. It keeps the array sorted by tags priority. */
2112 static GPtrArray *
tags_array_toggle_tag(GPtrArray * array,GtkTextTag * tag)2113 tags_array_toggle_tag (GPtrArray *array,
2114 GtkTextTag *tag)
2115 {
2116 gint pos;
2117 GtkTextTag **tags;
2118
2119 if (array == NULL)
2120 array = g_ptr_array_new ();
2121
2122 tags = (GtkTextTag**) array->pdata;
2123
2124 for (pos = 0; pos < array->len && tags[pos]->priority < tag->priority; pos++) ;
2125
2126 if (pos < array->len && tags[pos] == tag)
2127 g_ptr_array_remove_index (array, pos);
2128 else
2129 {
2130 g_ptr_array_set_size (array, array->len + 1);
2131 if (pos < array->len - 1)
2132 memmove (array->pdata + pos + 1, array->pdata + pos,
2133 (array->len - pos - 1) * sizeof (GtkTextTag*));
2134 array->pdata[pos] = tag;
2135 }
2136
2137 return array;
2138 }
2139
2140 GtkTextLineDisplay *
gtk_text_layout_get_line_display(GtkTextLayout * layout,GtkTextLine * line,gboolean size_only)2141 gtk_text_layout_get_line_display (GtkTextLayout *layout,
2142 GtkTextLine *line,
2143 gboolean size_only)
2144 {
2145 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
2146 GtkTextLineDisplay *display;
2147 GtkTextLineSegment *seg;
2148 GtkTextIter iter;
2149 GtkTextAttributes *style;
2150 gchar *text;
2151 PangoAttrList *attrs;
2152 gint text_allocated, layout_byte_offset, buffer_byte_offset;
2153 PangoRectangle extents;
2154 gboolean para_values_set = FALSE;
2155 GSList *cursor_byte_offsets = NULL;
2156 GSList *cursor_segs = NULL;
2157 GSList *tmp_list1, *tmp_list2;
2158 gboolean saw_widget = FALSE;
2159 PangoDirection base_dir;
2160 GPtrArray *tags;
2161 gboolean initial_toggle_segments;
2162
2163 g_return_val_if_fail (line != NULL, NULL);
2164
2165 if (layout->one_display_cache)
2166 {
2167 if (line == layout->one_display_cache->line &&
2168 (size_only || !layout->one_display_cache->size_only))
2169 {
2170 if (!size_only)
2171 update_text_display_cursors (layout, line, layout->one_display_cache);
2172 return layout->one_display_cache;
2173 }
2174 else
2175 {
2176 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
2177 layout->one_display_cache = NULL;
2178 gtk_text_layout_free_line_display (layout, tmp_display);
2179 }
2180 }
2181
2182 DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
2183
2184 display = g_new0 (GtkTextLineDisplay, 1);
2185
2186 display->size_only = size_only;
2187 display->line = line;
2188 display->insert_index = -1;
2189
2190 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2191 &iter, line, 0);
2192
2193 /* Special-case optimization for completely
2194 * invisible lines; makes it faster to deal
2195 * with sequences of invisible lines.
2196 */
2197 if (totally_invisible_line (layout, line, &iter))
2198 {
2199 if (display->direction == GTK_TEXT_DIR_RTL)
2200 display->layout = pango_layout_new (layout->rtl_context);
2201 else
2202 display->layout = pango_layout_new (layout->ltr_context);
2203
2204 return display;
2205 }
2206
2207 /* Find the bidi base direction */
2208 base_dir = line->dir_propagated_forward;
2209 if (base_dir == PANGO_DIRECTION_NEUTRAL)
2210 base_dir = line->dir_propagated_back;
2211
2212 if (line == priv->cursor_line &&
2213 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
2214 {
2215 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
2216 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2217 }
2218
2219 /* Allocate space for flat text for buffer
2220 */
2221 text_allocated = _gtk_text_line_byte_count (line);
2222 text = g_malloc (text_allocated);
2223
2224 attrs = pango_attr_list_new ();
2225
2226 /* Iterate over segments, creating display chunks for them, and updating the tags array. */
2227 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
2228 buffer_byte_offset = 0; /* position in the buffer line */
2229 seg = _gtk_text_iter_get_any_segment (&iter);
2230 tags = get_tags_array_at_iter (&iter);
2231 initial_toggle_segments = TRUE;
2232 while (seg != NULL)
2233 {
2234 /* Displayable segments */
2235 if (seg->type == >k_text_char_type ||
2236 seg->type == >k_text_pixbuf_type ||
2237 seg->type == >k_text_child_type)
2238 {
2239 style = get_style (layout, tags);
2240 initial_toggle_segments = FALSE;
2241
2242 /* We have to delay setting the paragraph values until we
2243 * hit the first pixbuf or text segment because toggles at
2244 * the beginning of the paragraph should affect the
2245 * paragraph-global values
2246 */
2247 if (!para_values_set)
2248 {
2249 set_para_values (layout, base_dir, style, display);
2250 para_values_set = TRUE;
2251 }
2252
2253 /* First see if the chunk is invisible, and ignore it if so. Tk
2254 * looked at tabs, wrap mode, etc. before doing this, but
2255 * that made no sense to me, so I am just skipping the
2256 * invisible chunks
2257 */
2258 if (!style->invisible)
2259 {
2260 if (seg->type == >k_text_char_type)
2261 {
2262 /* We don't want to split segments because of marks,
2263 * so we scan forward for more segments only
2264 * separated from us by marks. In theory, we should
2265 * also merge segments with identical styles, even
2266 * if there are toggles in-between
2267 */
2268
2269 gint bytes = 0;
2270 GtkTextLineSegment *prev_seg = NULL;
2271
2272 while (seg)
2273 {
2274 if (seg->type == >k_text_char_type)
2275 {
2276 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
2277 layout_byte_offset += seg->byte_count;
2278 buffer_byte_offset += seg->byte_count;
2279 bytes += seg->byte_count;
2280 }
2281 else if (seg->type == >k_text_right_mark_type ||
2282 seg->type == >k_text_left_mark_type)
2283 {
2284 /* If we have preedit string, break out of this loop - we'll almost
2285 * certainly have different attributes on the preedit string
2286 */
2287
2288 if (layout->preedit_len > 0 &&
2289 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2290 seg->body.mark.obj))
2291 break;
2292
2293 if (seg->body.mark.visible)
2294 {
2295 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
2296 cursor_segs = g_slist_prepend (cursor_segs, seg);
2297 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2298 seg->body.mark.obj))
2299 display->insert_index = layout_byte_offset;
2300 }
2301 }
2302 else
2303 break;
2304
2305 prev_seg = seg;
2306 seg = seg->next;
2307 }
2308
2309 seg = prev_seg; /* Back up one */
2310 add_generic_attrs (layout, &style->appearance,
2311 bytes,
2312 attrs, layout_byte_offset - bytes,
2313 size_only, TRUE);
2314 add_text_attrs (layout, style, bytes, attrs,
2315 layout_byte_offset - bytes, size_only);
2316 }
2317 else if (seg->type == >k_text_pixbuf_type)
2318 {
2319 add_generic_attrs (layout,
2320 &style->appearance,
2321 seg->byte_count,
2322 attrs, layout_byte_offset,
2323 size_only, FALSE);
2324 add_pixbuf_attrs (layout, display, style,
2325 seg, attrs, layout_byte_offset);
2326 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2327 seg->byte_count);
2328 layout_byte_offset += seg->byte_count;
2329 buffer_byte_offset += seg->byte_count;
2330 }
2331 else if (seg->type == >k_text_child_type)
2332 {
2333 saw_widget = TRUE;
2334
2335 add_generic_attrs (layout, &style->appearance,
2336 seg->byte_count,
2337 attrs, layout_byte_offset,
2338 size_only, FALSE);
2339 add_child_attrs (layout, display, style,
2340 seg, attrs, layout_byte_offset);
2341 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2342 seg->byte_count);
2343 layout_byte_offset += seg->byte_count;
2344 buffer_byte_offset += seg->byte_count;
2345 }
2346 else
2347 {
2348 /* We don't know this segment type */
2349 g_assert_not_reached ();
2350 }
2351
2352 } /* if (segment was visible) */
2353 else
2354 {
2355 /* Invisible segment */
2356 buffer_byte_offset += seg->byte_count;
2357 }
2358
2359 release_style (layout, style);
2360 }
2361
2362 /* Toggles */
2363 else if (seg->type == >k_text_toggle_on_type ||
2364 seg->type == >k_text_toggle_off_type)
2365 {
2366 /* Style may have changed, drop our
2367 current cached style */
2368 invalidate_cached_style (layout);
2369 /* Add the tag only after we have seen some non-toggle non-mark segment,
2370 * otherwise the tag is already accounted for by _gtk_text_btree_get_tags(). */
2371 if (!initial_toggle_segments)
2372 tags = tags_array_toggle_tag (tags, seg->body.toggle.info->tag);
2373 }
2374
2375 /* Marks */
2376 else if (seg->type == >k_text_right_mark_type ||
2377 seg->type == >k_text_left_mark_type)
2378 {
2379 gint cursor_offset = 0;
2380
2381 /* At the insertion point, add the preedit string, if any */
2382
2383 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2384 seg->body.mark.obj))
2385 {
2386 display->insert_index = layout_byte_offset;
2387
2388 if (layout->preedit_len > 0)
2389 {
2390 text_allocated += layout->preedit_len;
2391 text = g_realloc (text, text_allocated);
2392
2393 style = get_style (layout, tags);
2394 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2395 release_style (layout, style);
2396
2397 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2398 layout_byte_offset += layout->preedit_len;
2399 /* DO NOT increment the buffer byte offset for preedit */
2400
2401 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2402 }
2403 }
2404
2405
2406 /* Display visible marks */
2407
2408 if (seg->body.mark.visible)
2409 {
2410 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2411 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2412 cursor_segs = g_slist_prepend (cursor_segs, seg);
2413 }
2414 }
2415
2416 else
2417 g_error ("Unknown segment type: %s", seg->type->name);
2418
2419 seg = seg->next;
2420 }
2421
2422 if (!para_values_set)
2423 {
2424 style = get_style (layout, tags);
2425 set_para_values (layout, base_dir, style, display);
2426 release_style (layout, style);
2427 }
2428
2429 /* Pango doesn't want the trailing paragraph delimiters */
2430
2431 {
2432 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2433 * Unicode 3.0; update this if that changes.
2434 */
2435 #define PARAGRAPH_SEPARATOR 0x2029
2436 gunichar ch = 0;
2437
2438 if (layout_byte_offset > 0)
2439 {
2440 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2441 ch = g_utf8_get_char (prev);
2442 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2443 layout_byte_offset = prev - text; /* chop off */
2444
2445 if (ch == '\n' && layout_byte_offset > 0)
2446 {
2447 /* Possibly chop a CR as well */
2448 prev = g_utf8_prev_char (text + layout_byte_offset);
2449 if (*prev == '\r')
2450 --layout_byte_offset;
2451 }
2452 }
2453 }
2454
2455 pango_layout_set_text (display->layout, text, layout_byte_offset);
2456 pango_layout_set_attributes (display->layout, attrs);
2457
2458 tmp_list1 = cursor_byte_offsets;
2459 tmp_list2 = cursor_segs;
2460 while (tmp_list1)
2461 {
2462 add_cursor (layout, display, tmp_list2->data,
2463 GPOINTER_TO_INT (tmp_list1->data));
2464 tmp_list1 = tmp_list1->next;
2465 tmp_list2 = tmp_list2->next;
2466 }
2467 g_slist_free (cursor_byte_offsets);
2468 g_slist_free (cursor_segs);
2469
2470 pango_layout_get_extents (display->layout, NULL, &extents);
2471
2472 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2473 display->height += PANGO_PIXELS (extents.height);
2474
2475 /* If we aren't wrapping, we need to do the alignment of each
2476 * paragraph ourselves.
2477 */
2478 if (pango_layout_get_width (display->layout) < 0)
2479 {
2480 gint excess = display->total_width - display->width;
2481
2482 switch (pango_layout_get_alignment (display->layout))
2483 {
2484 case PANGO_ALIGN_LEFT:
2485 break;
2486 case PANGO_ALIGN_CENTER:
2487 display->x_offset += excess / 2;
2488 break;
2489 case PANGO_ALIGN_RIGHT:
2490 display->x_offset += excess;
2491 break;
2492 }
2493 }
2494
2495 /* Free this if we aren't in a loop */
2496 if (layout->wrap_loop_count == 0)
2497 invalidate_cached_style (layout);
2498
2499 g_free (text);
2500 pango_attr_list_unref (attrs);
2501 if (tags != NULL)
2502 g_ptr_array_free (tags, TRUE);
2503
2504 layout->one_display_cache = display;
2505
2506 if (saw_widget)
2507 allocate_child_widgets (layout, display);
2508
2509 return display;
2510 }
2511
2512 void
gtk_text_layout_free_line_display(GtkTextLayout * layout,GtkTextLineDisplay * display)2513 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2514 GtkTextLineDisplay *display)
2515 {
2516 if (display != layout->one_display_cache)
2517 {
2518 if (display->layout)
2519 g_object_unref (display->layout);
2520
2521 if (display->cursors)
2522 {
2523 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2524 g_slist_free (display->cursors);
2525 }
2526 g_slist_free (display->shaped_objects);
2527
2528 if (display->pg_bg_color)
2529 gdk_color_free (display->pg_bg_color);
2530
2531 g_free (display);
2532 }
2533 }
2534
2535 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2536 * taking into account the preedit string and invisible text if necessary.
2537 */
2538 static gint
line_display_iter_to_index(GtkTextLayout * layout,GtkTextLineDisplay * display,const GtkTextIter * iter)2539 line_display_iter_to_index (GtkTextLayout *layout,
2540 GtkTextLineDisplay *display,
2541 const GtkTextIter *iter)
2542 {
2543 gint index;
2544
2545 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2546
2547 index = gtk_text_iter_get_visible_line_index (iter);
2548
2549 if (layout->preedit_len > 0 && display->insert_index >= 0)
2550 {
2551 if (index >= display->insert_index)
2552 index += layout->preedit_len;
2553 }
2554
2555 return index;
2556 }
2557
2558 static void
line_display_index_to_iter(GtkTextLayout * layout,GtkTextLineDisplay * display,GtkTextIter * iter,gint index,gint trailing)2559 line_display_index_to_iter (GtkTextLayout *layout,
2560 GtkTextLineDisplay *display,
2561 GtkTextIter *iter,
2562 gint index,
2563 gint trailing)
2564 {
2565 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2566 _gtk_text_buffer_get_btree (layout->buffer)));
2567
2568 if (layout->preedit_len > 0 && display->insert_index >= 0)
2569 {
2570 if (index >= display->insert_index + layout->preedit_len)
2571 index -= layout->preedit_len;
2572 else if (index > display->insert_index)
2573 {
2574 index = display->insert_index;
2575 trailing = 0;
2576 }
2577 }
2578
2579 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2580 iter, display->line, 0);
2581
2582 gtk_text_iter_set_visible_line_index (iter, index);
2583
2584 if (_gtk_text_iter_get_text_line (iter) != display->line)
2585 {
2586 /* Clamp to end of line - really this clamping should have been done
2587 * before here, maybe in Pango, this is a broken band-aid I think
2588 */
2589 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2590 iter, display->line, 0);
2591
2592 if (!gtk_text_iter_ends_line (iter))
2593 gtk_text_iter_forward_to_line_end (iter);
2594 }
2595
2596 gtk_text_iter_forward_chars (iter, trailing);
2597 }
2598
2599 static void
get_line_at_y(GtkTextLayout * layout,gint y,GtkTextLine ** line,gint * line_top)2600 get_line_at_y (GtkTextLayout *layout,
2601 gint y,
2602 GtkTextLine **line,
2603 gint *line_top)
2604 {
2605 if (y < 0)
2606 y = 0;
2607 if (y > layout->height)
2608 y = layout->height;
2609
2610 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2611 layout, y, line_top);
2612 if (*line == NULL)
2613 {
2614 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2615
2616 if (line_top)
2617 *line_top =
2618 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2619 *line, layout);
2620 }
2621 }
2622
2623 /**
2624 * gtk_text_layout_get_line_at_y:
2625 * @layout: a #GtkLayout
2626 * @target_iter: the iterator in which the result is stored
2627 * @y: the y positition
2628 * @line_top: location to store the y coordinate of the
2629 * top of the line. (Can by %NULL)
2630 *
2631 * Get the iter at the beginning of the line which is displayed
2632 * at the given y.
2633 */
2634 void
gtk_text_layout_get_line_at_y(GtkTextLayout * layout,GtkTextIter * target_iter,gint y,gint * line_top)2635 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2636 GtkTextIter *target_iter,
2637 gint y,
2638 gint *line_top)
2639 {
2640 GtkTextLine *line;
2641
2642 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2643 g_return_if_fail (target_iter != NULL);
2644
2645 get_line_at_y (layout, y, &line, line_top);
2646 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2647 target_iter, line, 0);
2648 }
2649
2650 void
gtk_text_layout_get_iter_at_pixel(GtkTextLayout * layout,GtkTextIter * target_iter,gint x,gint y)2651 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2652 GtkTextIter *target_iter,
2653 gint x,
2654 gint y)
2655 {
2656 gint trailing;
2657
2658 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2659
2660 gtk_text_iter_forward_chars (target_iter, trailing);
2661 }
2662
gtk_text_layout_get_iter_at_position(GtkTextLayout * layout,GtkTextIter * target_iter,gint * trailing,gint x,gint y)2663 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2664 GtkTextIter *target_iter,
2665 gint *trailing,
2666 gint x,
2667 gint y)
2668 {
2669 GtkTextLine *line;
2670 gint byte_index;
2671 gint line_top;
2672 GtkTextLineDisplay *display;
2673
2674 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2675 g_return_if_fail (target_iter != NULL);
2676
2677 get_line_at_y (layout, y, &line, &line_top);
2678
2679 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2680
2681 x -= display->x_offset;
2682 y -= line_top + display->top_margin;
2683
2684 /* If we are below the layout, position the cursor at the last character
2685 * of the line.
2686 */
2687 if (y > display->height - display->top_margin - display->bottom_margin)
2688 {
2689 byte_index = _gtk_text_line_byte_count (line);
2690 if (trailing)
2691 *trailing = 0;
2692 }
2693 else
2694 {
2695 /* Ignore the "outside" return value from pango. Pango is doing
2696 * the right thing even if we are outside the layout in the
2697 * x-direction.
2698 */
2699 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2700 &byte_index, trailing);
2701 }
2702
2703 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2704
2705 gtk_text_layout_free_line_display (layout, display);
2706 }
2707
2708
2709 /**
2710 * gtk_text_layout_get_cursor_locations:
2711 * @layout: a #GtkTextLayout
2712 * @iter: a #GtkTextIter
2713 * @strong_pos: (allow-none): location to store the strong cursor position (may be %NULL)
2714 * @weak_pos: (allow-none): location to store the weak cursor position (may be %NULL)
2715 *
2716 * Given an iterator within a text layout, determine the positions of the
2717 * strong and weak cursors if the insertion point is at that
2718 * iterator. The position of each cursor is stored as a zero-width
2719 * rectangle. The strong cursor location is the location where
2720 * characters of the directionality equal to the base direction of the
2721 * paragraph are inserted. The weak cursor location is the location
2722 * where characters of the directionality opposite to the base
2723 * direction of the paragraph are inserted.
2724 **/
2725 void
gtk_text_layout_get_cursor_locations(GtkTextLayout * layout,GtkTextIter * iter,GdkRectangle * strong_pos,GdkRectangle * weak_pos)2726 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2727 GtkTextIter *iter,
2728 GdkRectangle *strong_pos,
2729 GdkRectangle *weak_pos)
2730 {
2731 GtkTextLine *line;
2732 GtkTextLineDisplay *display;
2733 gint line_top;
2734 gint index;
2735 GtkTextIter insert_iter;
2736
2737 PangoRectangle pango_strong_pos;
2738 PangoRectangle pango_weak_pos;
2739
2740 g_return_if_fail (layout != NULL);
2741 g_return_if_fail (iter != NULL);
2742
2743 line = _gtk_text_iter_get_text_line (iter);
2744 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2745 index = line_display_iter_to_index (layout, display, iter);
2746
2747 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2748 line, layout);
2749
2750 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2751 gtk_text_buffer_get_insert (layout->buffer));
2752
2753 if (gtk_text_iter_equal (iter, &insert_iter))
2754 index += layout->preedit_cursor - layout->preedit_len;
2755
2756 pango_layout_get_cursor_pos (display->layout, index,
2757 strong_pos ? &pango_strong_pos : NULL,
2758 weak_pos ? &pango_weak_pos : NULL);
2759
2760 if (strong_pos)
2761 {
2762 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2763 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2764 strong_pos->width = 0;
2765 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2766 }
2767
2768 if (weak_pos)
2769 {
2770 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2771 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2772 weak_pos->width = 0;
2773 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2774 }
2775
2776 gtk_text_layout_free_line_display (layout, display);
2777 }
2778
2779 /**
2780 * _gtk_text_layout_get_block_cursor:
2781 * @layout: a #GtkTextLayout
2782 * @pos: a #GdkRectangle to store block cursor position
2783 *
2784 * If layout is to display a block cursor, calculates its position
2785 * and returns %TRUE. Otherwise it returns %FALSE. In case when
2786 * cursor is visible, it simply returns the position stored in
2787 * the line display, otherwise it has to compute the position
2788 * (see get_block_cursor()).
2789 **/
2790 gboolean
_gtk_text_layout_get_block_cursor(GtkTextLayout * layout,GdkRectangle * pos)2791 _gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
2792 GdkRectangle *pos)
2793 {
2794 GtkTextLine *line;
2795 GtkTextLineDisplay *display;
2796 GtkTextIter iter;
2797 GdkRectangle rect;
2798 gboolean block = FALSE;
2799
2800 g_return_val_if_fail (layout != NULL, FALSE);
2801
2802 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
2803 gtk_text_buffer_get_insert (layout->buffer));
2804 line = _gtk_text_iter_get_text_line (&iter);
2805 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2806
2807 if (display->has_block_cursor)
2808 {
2809 block = TRUE;
2810 rect = display->block_cursor;
2811 }
2812 else
2813 {
2814 gint index = display->insert_index;
2815
2816 if (index < 0)
2817 index = gtk_text_iter_get_line_index (&iter);
2818
2819 if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
2820 block = TRUE;
2821 }
2822
2823 if (block && pos)
2824 {
2825 gint line_top;
2826
2827 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2828 line, layout);
2829
2830 *pos = rect;
2831 pos->x += display->x_offset;
2832 pos->y += line_top + display->top_margin;
2833 }
2834
2835 gtk_text_layout_free_line_display (layout, display);
2836 return block;
2837 }
2838
2839 /**
2840 * gtk_text_layout_get_line_yrange:
2841 * @layout: a #GtkTextLayout
2842 * @iter: a #GtkTextIter
2843 * @y: location to store the top of the paragraph in pixels,
2844 * or %NULL.
2845 * @height location to store the height of the paragraph in pixels,
2846 * or %NULL.
2847 *
2848 * Find the range of y coordinates for the paragraph containing
2849 * the given iter.
2850 **/
2851 void
gtk_text_layout_get_line_yrange(GtkTextLayout * layout,const GtkTextIter * iter,gint * y,gint * height)2852 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2853 const GtkTextIter *iter,
2854 gint *y,
2855 gint *height)
2856 {
2857 GtkTextLine *line;
2858
2859 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2860 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2861
2862 line = _gtk_text_iter_get_text_line (iter);
2863
2864 if (y)
2865 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2866 line, layout);
2867 if (height)
2868 {
2869 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2870 if (line_data)
2871 *height = line_data->height;
2872 else
2873 *height = 0;
2874 }
2875 }
2876
2877 /**
2878 * _gtk_text_layout_get_line_xrange:
2879 * @layout: a #GtkTextLayout
2880 * @iter: a #GtkTextIter
2881 * @x: location to store the top of the paragraph in pixels,
2882 * or %NULL.
2883 * @width location to store the height of the paragraph in pixels,
2884 * or %NULL.
2885 *
2886 * Find the range of X coordinates for the paragraph containing
2887 * the given iter. Private for 2.0 due to API freeze, could
2888 * be made public for 2.2.
2889 **/
2890 void
_gtk_text_layout_get_line_xrange(GtkTextLayout * layout,const GtkTextIter * iter,gint * x,gint * width)2891 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2892 const GtkTextIter *iter,
2893 gint *x,
2894 gint *width)
2895 {
2896 GtkTextLine *line;
2897
2898 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2899 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2900
2901 line = _gtk_text_iter_get_text_line (iter);
2902
2903 if (x)
2904 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2905
2906 if (width)
2907 {
2908 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2909 if (line_data)
2910 *width = line_data->width;
2911 else
2912 *width = 0;
2913 }
2914 }
2915
2916 void
gtk_text_layout_get_iter_location(GtkTextLayout * layout,const GtkTextIter * iter,GdkRectangle * rect)2917 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2918 const GtkTextIter *iter,
2919 GdkRectangle *rect)
2920 {
2921 PangoRectangle pango_rect;
2922 GtkTextLine *line;
2923 GtkTextBTree *tree;
2924 GtkTextLineDisplay *display;
2925 gint byte_index;
2926 gint x_offset;
2927
2928 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2929 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2930 g_return_if_fail (rect != NULL);
2931
2932 tree = _gtk_text_iter_get_btree (iter);
2933 line = _gtk_text_iter_get_text_line (iter);
2934
2935 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2936
2937 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2938
2939 x_offset = display->x_offset * PANGO_SCALE;
2940
2941 byte_index = gtk_text_iter_get_line_index (iter);
2942
2943 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2944
2945 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2946 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2947 rect->width = PANGO_PIXELS (pango_rect.width);
2948 rect->height = PANGO_PIXELS (pango_rect.height);
2949
2950 gtk_text_layout_free_line_display (layout, display);
2951 }
2952
2953 /* FFIXX */
2954
2955 /* Find the iter for the logical beginning of the first display line whose
2956 * top y is >= y. If none exists, move the iter to the logical beginning
2957 * of the last line in the buffer.
2958 */
2959 static void
find_display_line_below(GtkTextLayout * layout,GtkTextIter * iter,gint y)2960 find_display_line_below (GtkTextLayout *layout,
2961 GtkTextIter *iter,
2962 gint y)
2963 {
2964 GtkTextLine *line, *next;
2965 GtkTextLine *found_line = NULL;
2966 gint line_top;
2967 gint found_byte = 0;
2968
2969 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2970 layout, y, &line_top);
2971 if (!line)
2972 {
2973 line =
2974 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2975
2976 line_top =
2977 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2978 line, layout);
2979 }
2980
2981 while (line && !found_line)
2982 {
2983 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2984 PangoLayoutIter *layout_iter;
2985
2986 layout_iter = pango_layout_get_iter (display->layout);
2987
2988 line_top += display->top_margin;
2989
2990 do
2991 {
2992 gint first_y, last_y;
2993 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2994
2995 found_byte = layout_line->start_index;
2996
2997 if (line_top >= y)
2998 {
2999 found_line = line;
3000 break;
3001 }
3002
3003 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3004 line_top += (last_y - first_y) / PANGO_SCALE;
3005 }
3006 while (pango_layout_iter_next_line (layout_iter));
3007
3008 pango_layout_iter_free (layout_iter);
3009
3010 line_top += display->bottom_margin;
3011 gtk_text_layout_free_line_display (layout, display);
3012
3013 next = _gtk_text_line_next_excluding_last (line);
3014 if (!next)
3015 found_line = line;
3016
3017 line = next;
3018 }
3019
3020 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3021 iter, found_line, found_byte);
3022 }
3023
3024 /* Find the iter for the logical beginning of the last display line whose
3025 * top y is >= y. If none exists, move the iter to the logical beginning
3026 * of the first line in the buffer.
3027 */
3028 static void
find_display_line_above(GtkTextLayout * layout,GtkTextIter * iter,gint y)3029 find_display_line_above (GtkTextLayout *layout,
3030 GtkTextIter *iter,
3031 gint y)
3032 {
3033 GtkTextLine *line;
3034 GtkTextLine *found_line = NULL;
3035 gint line_top;
3036 gint found_byte = 0;
3037
3038 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
3039 if (!line)
3040 {
3041 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
3042
3043 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
3044 }
3045
3046 while (line && !found_line)
3047 {
3048 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
3049 PangoRectangle logical_rect;
3050 PangoLayoutIter *layout_iter;
3051 gint tmp_top;
3052
3053 layout_iter = pango_layout_get_iter (display->layout);
3054
3055 line_top -= display->top_margin + display->bottom_margin;
3056 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
3057 line_top -= logical_rect.height / PANGO_SCALE;
3058
3059 tmp_top = line_top + display->top_margin;
3060
3061 do
3062 {
3063 gint first_y, last_y;
3064 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3065
3066 found_byte = layout_line->start_index;
3067
3068 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3069
3070 tmp_top -= (last_y - first_y) / PANGO_SCALE;
3071
3072 if (tmp_top < y)
3073 {
3074 found_line = line;
3075 pango_layout_iter_free (layout_iter);
3076 goto done;
3077 }
3078 }
3079 while (pango_layout_iter_next_line (layout_iter));
3080
3081 pango_layout_iter_free (layout_iter);
3082
3083 gtk_text_layout_free_line_display (layout, display);
3084
3085 line = _gtk_text_line_previous (line);
3086 }
3087
3088 done:
3089
3090 if (found_line)
3091 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3092 iter, found_line, found_byte);
3093 else
3094 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
3095 }
3096
3097 /**
3098 * gtk_text_layout_clamp_iter_to_vrange:
3099 * @layout: a #GtkTextLayout
3100 * @iter: a #GtkTextIter
3101 * @top: the top of the range
3102 * @bottom: the bottom the range
3103 *
3104 * If the iterator is not fully in the range @top <= y < @bottom,
3105 * then, if possible, move it the minimum distance so that the
3106 * iterator in this range.
3107 *
3108 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
3109 **/
3110 gboolean
gtk_text_layout_clamp_iter_to_vrange(GtkTextLayout * layout,GtkTextIter * iter,gint top,gint bottom)3111 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
3112 GtkTextIter *iter,
3113 gint top,
3114 gint bottom)
3115 {
3116 GdkRectangle iter_rect;
3117
3118 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
3119
3120 /* If the iter is at least partially above the range, put the iter
3121 * at the first fully visible line after the range.
3122 */
3123 if (iter_rect.y < top)
3124 {
3125 find_display_line_below (layout, iter, top);
3126
3127 return TRUE;
3128 }
3129 /* Otherwise, if the iter is at least partially below the screen, put the
3130 * iter on the last logical position of the last completely visible
3131 * line on screen
3132 */
3133 else if (iter_rect.y + iter_rect.height > bottom)
3134 {
3135 find_display_line_above (layout, iter, bottom);
3136
3137 return TRUE;
3138 }
3139 else
3140 return FALSE;
3141 }
3142
3143 /**
3144 * gtk_text_layout_move_iter_to_previous_line:
3145 * @layout: a #GtkLayout
3146 * @iter: a #GtkTextIter
3147 *
3148 * Move the iterator to the beginning of the previous line. The lines
3149 * of a wrapped paragraph are treated as distinct for this operation.
3150 **/
3151 gboolean
gtk_text_layout_move_iter_to_previous_line(GtkTextLayout * layout,GtkTextIter * iter)3152 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
3153 GtkTextIter *iter)
3154 {
3155 GtkTextLine *line;
3156 GtkTextLineDisplay *display;
3157 gint line_byte;
3158 GSList *tmp_list;
3159 PangoLayoutLine *layout_line;
3160 GtkTextIter orig;
3161 gboolean update_byte = FALSE;
3162
3163 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3164 g_return_val_if_fail (iter != NULL, FALSE);
3165
3166 orig = *iter;
3167
3168
3169 line = _gtk_text_iter_get_text_line (iter);
3170 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3171 line_byte = line_display_iter_to_index (layout, display, iter);
3172
3173 /* If display->height == 0 then the line is invisible, so don't
3174 * move onto it.
3175 */
3176 while (display->height == 0)
3177 {
3178 GtkTextLine *prev_line;
3179
3180 prev_line = _gtk_text_line_previous (line);
3181
3182 if (prev_line == NULL)
3183 {
3184 line_display_index_to_iter (layout, display, iter, 0, 0);
3185 goto out;
3186 }
3187
3188 gtk_text_layout_free_line_display (layout, display);
3189
3190 line = prev_line;
3191 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3192 update_byte = TRUE;
3193 }
3194
3195 tmp_list = pango_layout_get_lines_readonly (display->layout);
3196 layout_line = tmp_list->data;
3197
3198 if (update_byte)
3199 {
3200 line_byte = layout_line->start_index + layout_line->length;
3201 }
3202
3203 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
3204 {
3205 GtkTextLine *prev_line;
3206
3207 prev_line = _gtk_text_line_previous (line);
3208
3209 /* first line of the whole buffer, do not move the iter and return FALSE */
3210 if (prev_line == NULL)
3211 goto out;
3212
3213 while (prev_line)
3214 {
3215 gtk_text_layout_free_line_display (layout, display);
3216
3217 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3218
3219 if (display->height > 0)
3220 {
3221 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
3222 layout_line = tmp_list->data;
3223
3224 line_display_index_to_iter (layout, display, iter,
3225 layout_line->start_index + layout_line->length, 0);
3226 break;
3227 }
3228
3229 prev_line = _gtk_text_line_previous (prev_line);
3230 }
3231 }
3232 else
3233 {
3234 gint prev_offset = layout_line->start_index;
3235
3236 tmp_list = tmp_list->next;
3237 while (tmp_list)
3238 {
3239 layout_line = tmp_list->data;
3240
3241 if (line_byte < layout_line->start_index + layout_line->length ||
3242 !tmp_list->next)
3243 {
3244 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
3245 break;
3246 }
3247
3248 prev_offset = layout_line->start_index;
3249 tmp_list = tmp_list->next;
3250 }
3251 }
3252
3253 out:
3254
3255 gtk_text_layout_free_line_display (layout, display);
3256
3257 return
3258 !gtk_text_iter_equal (iter, &orig) &&
3259 !gtk_text_iter_is_end (iter);
3260 }
3261
3262 /**
3263 * gtk_text_layout_move_iter_to_next_line:
3264 * @layout: a #GtkLayout
3265 * @iter: a #GtkTextIter
3266 *
3267 * Move the iterator to the beginning of the next line. The
3268 * lines of a wrapped paragraph are treated as distinct for
3269 * this operation.
3270 **/
3271 gboolean
gtk_text_layout_move_iter_to_next_line(GtkTextLayout * layout,GtkTextIter * iter)3272 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
3273 GtkTextIter *iter)
3274 {
3275 GtkTextLine *line;
3276 GtkTextLineDisplay *display;
3277 gint line_byte;
3278 GtkTextIter orig;
3279 gboolean found = FALSE;
3280 gboolean found_after = FALSE;
3281 gboolean first = TRUE;
3282
3283 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3284 g_return_val_if_fail (iter != NULL, FALSE);
3285
3286 orig = *iter;
3287
3288 line = _gtk_text_iter_get_text_line (iter);
3289
3290 while (line && !found_after)
3291 {
3292 GSList *tmp_list;
3293
3294 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3295
3296 if (display->height == 0)
3297 goto next;
3298
3299 if (first)
3300 {
3301 line_byte = line_display_iter_to_index (layout, display, iter);
3302 first = FALSE;
3303 }
3304 else
3305 line_byte = 0;
3306
3307 tmp_list = pango_layout_get_lines_readonly (display->layout);
3308 while (tmp_list && !found_after)
3309 {
3310 PangoLayoutLine *layout_line = tmp_list->data;
3311
3312 if (found)
3313 {
3314 line_display_index_to_iter (layout, display, iter,
3315 layout_line->start_index, 0);
3316 found_after = TRUE;
3317 }
3318 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3319 found = TRUE;
3320
3321 tmp_list = tmp_list->next;
3322 }
3323
3324 next:
3325
3326 gtk_text_layout_free_line_display (layout, display);
3327
3328 line = _gtk_text_line_next_excluding_last (line);
3329 }
3330
3331 if (!found_after)
3332 gtk_text_buffer_get_end_iter (layout->buffer, iter);
3333
3334 return
3335 !gtk_text_iter_equal (iter, &orig) &&
3336 !gtk_text_iter_is_end (iter);
3337 }
3338
3339 /**
3340 * gtk_text_layout_move_iter_to_line_end:
3341 * @layout: a #GtkTextLayout
3342 * @direction: if negative, move to beginning of line, otherwise
3343 move to end of line.
3344 *
3345 * Move to the beginning or end of a display line.
3346 **/
3347 gboolean
gtk_text_layout_move_iter_to_line_end(GtkTextLayout * layout,GtkTextIter * iter,gint direction)3348 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
3349 GtkTextIter *iter,
3350 gint direction)
3351 {
3352 GtkTextLine *line;
3353 GtkTextLineDisplay *display;
3354 gint line_byte;
3355 GSList *tmp_list;
3356 GtkTextIter orig;
3357
3358 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3359 g_return_val_if_fail (iter != NULL, FALSE);
3360
3361 orig = *iter;
3362
3363 line = _gtk_text_iter_get_text_line (iter);
3364 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3365 line_byte = line_display_iter_to_index (layout, display, iter);
3366
3367 tmp_list = pango_layout_get_lines_readonly (display->layout);
3368 while (tmp_list)
3369 {
3370 PangoLayoutLine *layout_line = tmp_list->data;
3371
3372 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3373 {
3374 line_display_index_to_iter (layout, display, iter,
3375 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
3376 0);
3377
3378 /* FIXME: As a bad hack, we move back one position when we
3379 * are inside a paragraph to avoid going to next line on a
3380 * forced break not at whitespace. Real fix is to keep track
3381 * of whether marks are at leading or trailing edge? */
3382 if (direction > 0 && layout_line->length > 0 &&
3383 !gtk_text_iter_ends_line (iter) &&
3384 !_gtk_text_btree_char_is_invisible (iter))
3385 gtk_text_iter_backward_char (iter);
3386 break;
3387 }
3388
3389 tmp_list = tmp_list->next;
3390 }
3391
3392 gtk_text_layout_free_line_display (layout, display);
3393
3394 return
3395 !gtk_text_iter_equal (iter, &orig) &&
3396 !gtk_text_iter_is_end (iter);
3397 }
3398
3399
3400 /**
3401 * gtk_text_layout_iter_starts_line:
3402 * @layout: a #GtkTextLayout
3403 * @iter: iterator to test
3404 *
3405 * Tests whether an iterator is at the start of a display line.
3406 **/
3407 gboolean
gtk_text_layout_iter_starts_line(GtkTextLayout * layout,const GtkTextIter * iter)3408 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
3409 const GtkTextIter *iter)
3410 {
3411 GtkTextLine *line;
3412 GtkTextLineDisplay *display;
3413 gint line_byte;
3414 GSList *tmp_list;
3415
3416 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3417 g_return_val_if_fail (iter != NULL, FALSE);
3418
3419 line = _gtk_text_iter_get_text_line (iter);
3420 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3421 line_byte = line_display_iter_to_index (layout, display, iter);
3422
3423 tmp_list = pango_layout_get_lines_readonly (display->layout);
3424 while (tmp_list)
3425 {
3426 PangoLayoutLine *layout_line = tmp_list->data;
3427
3428 if (line_byte < layout_line->start_index + layout_line->length ||
3429 !tmp_list->next)
3430 {
3431 /* We're located on this line or the para delimiters before
3432 * it
3433 */
3434 gtk_text_layout_free_line_display (layout, display);
3435
3436 if (line_byte == layout_line->start_index)
3437 return TRUE;
3438 else
3439 return FALSE;
3440 }
3441
3442 tmp_list = tmp_list->next;
3443 }
3444
3445 g_assert_not_reached ();
3446 return FALSE;
3447 }
3448
3449 void
gtk_text_layout_get_iter_at_line(GtkTextLayout * layout,GtkTextIter * iter,GtkTextLine * line,gint byte_offset)3450 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3451 GtkTextIter *iter,
3452 GtkTextLine *line,
3453 gint byte_offset)
3454 {
3455 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3456 iter, line, byte_offset);
3457 }
3458
3459
3460 /**
3461 * gtk_text_layout_move_iter_to_x:
3462 * @layout: a #GtkTextLayout
3463 * @iter: a #GtkTextIter
3464 * @x: X coordinate
3465 *
3466 * Keeping the iterator on the same line of the layout, move it to the
3467 * specified X coordinate. The lines of a wrapped paragraph are
3468 * treated as distinct for this operation.
3469 **/
3470 void
gtk_text_layout_move_iter_to_x(GtkTextLayout * layout,GtkTextIter * iter,gint x)3471 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3472 GtkTextIter *iter,
3473 gint x)
3474 {
3475 GtkTextLine *line;
3476 GtkTextLineDisplay *display;
3477 gint line_byte;
3478 PangoLayoutIter *layout_iter;
3479
3480 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3481 g_return_if_fail (iter != NULL);
3482
3483 line = _gtk_text_iter_get_text_line (iter);
3484
3485 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3486 line_byte = line_display_iter_to_index (layout, display, iter);
3487
3488 layout_iter = pango_layout_get_iter (display->layout);
3489
3490 do
3491 {
3492 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3493
3494 if (line_byte < layout_line->start_index + layout_line->length ||
3495 pango_layout_iter_at_last_line (layout_iter))
3496 {
3497 PangoRectangle logical_rect;
3498 gint byte_index, trailing;
3499 gint x_offset = display->x_offset * PANGO_SCALE;
3500
3501 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3502
3503 pango_layout_line_x_to_index (layout_line,
3504 x * PANGO_SCALE - x_offset - logical_rect.x,
3505 &byte_index, &trailing);
3506
3507 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3508
3509 break;
3510 }
3511 }
3512 while (pango_layout_iter_next_line (layout_iter));
3513
3514 pango_layout_iter_free (layout_iter);
3515
3516 gtk_text_layout_free_line_display (layout, display);
3517 }
3518
3519 /**
3520 * gtk_text_layout_move_iter_visually:
3521 * @layout: a #GtkTextLayout
3522 * @iter: a #GtkTextIter
3523 * @count: number of characters to move (negative moves left, positive moves right)
3524 *
3525 * Move the iterator a given number of characters visually, treating
3526 * it as the strong cursor position. If @count is positive, then the
3527 * new strong cursor position will be @count positions to the right of
3528 * the old cursor position. If @count is negative then the new strong
3529 * cursor position will be @count positions to the left of the old
3530 * cursor position.
3531 *
3532 * In the presence of bidirection text, the correspondence
3533 * between logical and visual order will depend on the direction
3534 * of the current run, and there may be jumps when the cursor
3535 * is moved off of the end of a run.
3536 **/
3537
3538 gboolean
gtk_text_layout_move_iter_visually(GtkTextLayout * layout,GtkTextIter * iter,gint count)3539 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3540 GtkTextIter *iter,
3541 gint count)
3542 {
3543 GtkTextLineDisplay *display = NULL;
3544 GtkTextIter orig;
3545 GtkTextIter lineiter;
3546
3547 g_return_val_if_fail (layout != NULL, FALSE);
3548 g_return_val_if_fail (iter != NULL, FALSE);
3549
3550 orig = *iter;
3551
3552 while (count != 0)
3553 {
3554 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3555 gint line_byte;
3556 gint extra_back = 0;
3557 gboolean strong;
3558
3559 int byte_count = _gtk_text_line_byte_count (line);
3560
3561 int new_index;
3562 int new_trailing;
3563
3564 if (!display)
3565 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3566
3567 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3568 strong = TRUE;
3569 else
3570 strong = display->direction == layout->cursor_direction;
3571
3572 line_byte = line_display_iter_to_index (layout, display, iter);
3573
3574 if (count > 0)
3575 {
3576 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3577 count--;
3578 }
3579 else
3580 {
3581 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3582 count++;
3583 }
3584
3585 /* We need to handle the preedit string specially. Well, we don't really need to
3586 * handle it specially, since hopefully calling gtk_im_context_reset() will
3587 * remove the preedit string; but if we start off in front of the preedit
3588 * string (logically) and end up in or on the back edge of the preedit string,
3589 * we should move the iter one place farther.
3590 */
3591 if (layout->preedit_len > 0 && display->insert_index >= 0)
3592 {
3593 if (line_byte == display->insert_index + layout->preedit_len &&
3594 new_index < display->insert_index + layout->preedit_len)
3595 {
3596 line_byte = display->insert_index;
3597 extra_back = 1;
3598 }
3599 }
3600
3601 if (new_index < 0 || (new_index == 0 && extra_back))
3602 {
3603 do
3604 {
3605 line = _gtk_text_line_previous (line);
3606
3607 if (!line)
3608 goto done;
3609
3610 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3611 &lineiter, line, 0);
3612 }
3613 while (totally_invisible_line (layout, line, &lineiter));
3614
3615 gtk_text_layout_free_line_display (layout, display);
3616 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3617 gtk_text_iter_forward_to_line_end (&lineiter);
3618 new_index = gtk_text_iter_get_visible_line_index (&lineiter);
3619 }
3620 else if (new_index > byte_count)
3621 {
3622 do
3623 {
3624 line = _gtk_text_line_next_excluding_last (line);
3625 if (!line)
3626 goto done;
3627
3628 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3629 &lineiter, line, 0);
3630 }
3631 while (totally_invisible_line (layout, line, &lineiter));
3632
3633 gtk_text_layout_free_line_display (layout, display);
3634 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3635 new_index = 0;
3636 }
3637
3638 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3639 if (extra_back)
3640 gtk_text_iter_backward_char (iter);
3641 }
3642
3643 gtk_text_layout_free_line_display (layout, display);
3644
3645 done:
3646
3647 return
3648 !gtk_text_iter_equal (iter, &orig) &&
3649 !gtk_text_iter_is_end (iter);
3650 }
3651
3652 void
gtk_text_layout_spew(GtkTextLayout * layout)3653 gtk_text_layout_spew (GtkTextLayout *layout)
3654 {
3655 #if 0
3656 GtkTextDisplayLine *iter;
3657 guint wrapped = 0;
3658 guint paragraphs = 0;
3659 GtkTextLine *last_line = NULL;
3660
3661 iter = layout->line_list;
3662 while (iter != NULL)
3663 {
3664 if (iter->line != last_line)
3665 {
3666 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3667 ++paragraphs;
3668 last_line = iter->line;
3669 }
3670
3671 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3672 wrapped, iter->y, iter->length, iter->byte_offset,
3673 iter->byte_count);
3674
3675 ++wrapped;
3676 iter = iter->next;
3677 }
3678
3679 printf ("Layout %s recompute\n",
3680 layout->need_recompute ? "needs" : "doesn't need");
3681
3682 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3683 paragraphs, wrapped, layout->width,
3684 layout->height, layout->screen_width);
3685 #endif
3686 }
3687
3688 /* Catch all situations that move the insertion point.
3689 */
3690 static void
gtk_text_layout_mark_set_handler(GtkTextBuffer * buffer,const GtkTextIter * location,GtkTextMark * mark,gpointer data)3691 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3692 const GtkTextIter *location,
3693 GtkTextMark *mark,
3694 gpointer data)
3695 {
3696 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3697
3698 if (mark == gtk_text_buffer_get_insert (buffer))
3699 gtk_text_layout_update_cursor_line (layout);
3700 }
3701
3702 static void
gtk_text_layout_buffer_insert_text(GtkTextBuffer * textbuffer,GtkTextIter * iter,gchar * str,gint len,gpointer data)3703 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3704 GtkTextIter *iter,
3705 gchar *str,
3706 gint len,
3707 gpointer data)
3708 {
3709 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3710
3711 gtk_text_layout_update_cursor_line (layout);
3712 }
3713
3714 static void
gtk_text_layout_buffer_delete_range(GtkTextBuffer * textbuffer,GtkTextIter * start,GtkTextIter * end,gpointer data)3715 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3716 GtkTextIter *start,
3717 GtkTextIter *end,
3718 gpointer data)
3719 {
3720 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3721
3722 gtk_text_layout_update_cursor_line (layout);
3723 }
3724
3725 #define __GTK_TEXT_LAYOUT_C__
3726 #include "gtkaliasdef.c"
3727