gtktextlayout.c 107 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * gtktextlayout.c - calculate the layout of the text
3 4 5 6 7 8 9
 *
 * Copyright (c) 1992-1994 The Regents of the University of California.
 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
 * Copyright (c) 2000 Red Hat, Inc.
 * Tk->Gtk port by Havoc Pennington
 * Pango support by Owen Taylor
 *
10 11
 * This file can be used under your choice of two licenses, the LGPL
 * and the original Tk license.
12
 *
13
 * LGPL:
14
 *
15 16 17 18 19 20 21 22 23 24 25
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
26
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
27 28
 *
 * Original Tk license:
29 30 31 32 33
 *
 * This software is copyrighted by the Regents of the University of
 * California, Sun Microsystems, Inc., and other parties.  The
 * following terms apply to all files associated with the software
 * unless explicitly disclaimed in individual files.
34
 *
35 36 37 38 39 40 41 42 43
 * The authors hereby grant permission to use, copy, modify,
 * distribute, and license this software and its documentation for any
 * purpose, provided that existing copyright notices are retained in
 * all copies and that this notice is included verbatim in any
 * distributions. No written agreement, license, or royalty fee is
 * required for any of the authorized uses.  Modifications to this
 * software may be copyrighted by their authors and need not follow
 * the licensing terms described here, provided that the new terms are
 * clearly indicated on the first page of each file where they apply.
44
 *
45 46 47 48 49
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
50
 *
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
 * U.S. government, the Government shall have only "Restricted Rights"
 * in the software and related documentation as defined in the Federal
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
 * are acquiring the software on behalf of the Department of Defense,
 * the software shall be classified as "Commercial Computer Software"
 * and the Government shall have only "Restricted Rights" as defined
 * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
 * foregoing, the authors grant the U.S. Government and others acting
 * in its behalf permission to use and distribute the software in
 * accordance with the terms specified in this license.
69 70 71 72 73 74 75
 *
 */
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
76 77
 */

78
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
79
#include "config.h"
80
#include "gtkmarshalers.h"
81 82 83
#include "gtktextlayout.h"
#include "gtktextbtree.h"
#include "gtktextiterprivate.h"
84
#include "gtktextutil.h"
85
#include "gtkintl.h"
86 87

#include <stdlib.h>
88
#include <string.h>
89

90
#define GTK_TEXT_LAYOUT_GET_PRIVATE(o)  ((GtkTextLayoutPrivate *) gtk_text_layout_get_instance_private ((o)))
91 92 93 94 95 96 97 98 99 100 101

typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;

struct _GtkTextLayoutPrivate
{
  /* Cache the line that the cursor is positioned on, as the keyboard
     direction only influences the direction of the cursor line.
  */
  GtkTextLine *cursor_line;
};

102
static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
103 104 105
                                                   GtkTextLine *line,
                                                   /* may be NULL */
                                                   GtkTextLineData *line_data);
106 107 108

static void gtk_text_layout_invalidated     (GtkTextLayout     *layout);

109 110 111
static void gtk_text_layout_real_invalidate        (GtkTextLayout     *layout,
						    const GtkTextIter *start,
						    const GtkTextIter *end);
112 113 114
static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout     *layout,
						    const GtkTextIter *start,
						    const GtkTextIter *end);
115
static void gtk_text_layout_invalidate_cache       (GtkTextLayout     *layout,
116 117 118 119
						    GtkTextLine       *line,
						    gboolean           cursors_only);
static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout     *layout,
						    gboolean           cursors_only);
120 121 122
static void gtk_text_layout_real_free_line_data    (GtkTextLayout     *layout,
						    GtkTextLine       *line,
						    GtkTextLineData   *line_data);
Owen Taylor's avatar
Owen Taylor committed
123 124 125 126
static void gtk_text_layout_emit_changed           (GtkTextLayout     *layout,
						    gint               y,
						    gint               old_height,
						    gint               new_height);
127 128 129 130 131

static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);

static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static void gtk_text_layout_mark_set_handler    (GtkTextBuffer     *buffer,
						 const GtkTextIter *location,
						 GtkTextMark       *mark,
						 gpointer           data);
static void gtk_text_layout_buffer_insert_text  (GtkTextBuffer     *textbuffer,
						 GtkTextIter       *iter,
						 gchar             *str,
						 gint               len,
						 gpointer           data);
static void gtk_text_layout_buffer_delete_range (GtkTextBuffer     *textbuffer,
						 GtkTextIter       *start,
						 GtkTextIter       *end,
						 gpointer           data);

static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);

148 149 150 151 152 153 154 155 156 157
static void line_display_index_to_iter (GtkTextLayout      *layout,
	                                GtkTextLineDisplay *display,
			                GtkTextIter        *iter,
                                        gint                index,
                                        gint                trailing);

static gint line_display_iter_to_index (GtkTextLayout      *layout,
                                        GtkTextLineDisplay *display,
                                        const GtkTextIter  *iter);

158 159 160
enum {
  INVALIDATED,
  CHANGED,
161
  ALLOCATE_CHILD,
162 163 164 165 166 167 168 169
  LAST_SIGNAL
};

enum {
  ARG_0,
  LAST_ARG
};

170 171
#define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)

172 173 174 175
static guint signals[LAST_SIGNAL] = { 0 };

PangoAttrType gtk_text_attr_appearance_type = 0;

176
G_DEFINE_TYPE_WITH_PRIVATE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static void
gtk_text_layout_dispose (GObject *object)
{
  GtkTextLayout *layout;

  layout = GTK_TEXT_LAYOUT (object);

  gtk_text_layout_set_buffer (layout, NULL);

  if (layout->default_style != NULL)
    {
      gtk_text_attributes_unref (layout->default_style);
      layout->default_style = NULL;
    }

  g_clear_object (&layout->ltr_context);
  g_clear_object (&layout->rtl_context);

  if (layout->one_display_cache)
    {
      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
      layout->one_display_cache = NULL;
      gtk_text_layout_free_line_display (layout, tmp_display);
    }

  if (layout->preedit_attrs != NULL)
    {
      pango_attr_list_unref (layout->preedit_attrs);
      layout->preedit_attrs = NULL;
    }

  G_OBJECT_CLASS (gtk_text_layout_parent_class)->dispose (object);
}

static void
gtk_text_layout_finalize (GObject *object)
{
  GtkTextLayout *layout;

  layout = GTK_TEXT_LAYOUT (object);

  g_free (layout->preedit_string);

  G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
}

224 225 226
static void
gtk_text_layout_class_init (GtkTextLayoutClass *klass)
{
227
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
228

229
  object_class->dispose = gtk_text_layout_dispose;
230
  object_class->finalize = gtk_text_layout_finalize;
231 232 233

  klass->wrap = gtk_text_layout_real_wrap;
  klass->invalidate = gtk_text_layout_real_invalidate;
234
  klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
235 236
  klass->free_line_data = gtk_text_layout_real_free_line_data;

237
  signals[INVALIDATED] =
238
    g_signal_new (I_("invalidated"),
239 240 241 242
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
                  NULL, NULL,
243
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
244
                  G_TYPE_NONE,
245
                  0);
246 247

  signals[CHANGED] =
248
    g_signal_new (I_("changed"),
249 250 251 252
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
                  NULL, NULL,
253
                  _gtk_marshal_VOID__INT_INT_INT,
Manish Singh's avatar
Manish Singh committed
254
                  G_TYPE_NONE,
255
                  3,
Manish Singh's avatar
Manish Singh committed
256 257 258
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT);
259 260
  g_signal_set_va_marshaller (signals[CHANGED], G_TYPE_FROM_CLASS (klass),
                              _gtk_marshal_VOID__INT_INT_INTv);
261

262
  signals[ALLOCATE_CHILD] =
263
    g_signal_new (I_("allocate-child"),
264 265 266 267
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
                  NULL, NULL,
268
                  _gtk_marshal_VOID__OBJECT_INT_INT,
Manish Singh's avatar
Manish Singh committed
269
                  G_TYPE_NONE,
270
                  3,
271
                  G_TYPE_OBJECT,
Manish Singh's avatar
Manish Singh committed
272 273
                  G_TYPE_INT,
                  G_TYPE_INT);
274 275
}

276
static void
277 278
gtk_text_layout_init (GtkTextLayout *text_layout)
{
279
  text_layout->cursor_visible = TRUE;
280 281 282 283 284
}

GtkTextLayout*
gtk_text_layout_new (void)
{
Manish Singh's avatar
Manish Singh committed
285
  return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
286 287 288 289 290 291 292
}

static void
free_style_cache (GtkTextLayout *text_layout)
{
  if (text_layout->one_style_cache)
    {
293
      gtk_text_attributes_unref (text_layout->one_style_cache);
294 295 296 297
      text_layout->one_style_cache = NULL;
    }
}

298 299 300 301
/**
 * gtk_text_layout_set_buffer:
 * @buffer: (allow-none):
 */
302 303 304 305 306 307
void
gtk_text_layout_set_buffer (GtkTextLayout *layout,
                            GtkTextBuffer *buffer)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
308

309 310 311 312
  if (layout->buffer == buffer)
    return;

  free_style_cache (layout);
313

314 315
  if (layout->buffer)
    {
316
      _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
317
                                  layout);
318

319 320 321 322 323 324 325 326 327 328
      g_signal_handlers_disconnect_by_func (layout->buffer, 
                                            G_CALLBACK (gtk_text_layout_mark_set_handler), 
                                            layout);
      g_signal_handlers_disconnect_by_func (layout->buffer, 
                                            G_CALLBACK (gtk_text_layout_buffer_insert_text), 
                                            layout);
      g_signal_handlers_disconnect_by_func (layout->buffer, 
                                            G_CALLBACK (gtk_text_layout_buffer_delete_range), 
                                            layout);

Manish Singh's avatar
Manish Singh committed
329
      g_object_unref (layout->buffer);
330 331 332 333 334 335 336
      layout->buffer = NULL;
    }

  if (buffer)
    {
      layout->buffer = buffer;

Manish Singh's avatar
Manish Singh committed
337
      g_object_ref (buffer);
338

339
      _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
340 341

      /* Bind to all signals that move the insert mark. */
342
      g_signal_connect_after (layout->buffer, "mark-set",
343
                              G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
344
      g_signal_connect_after (layout->buffer, "insert-text",
345
                              G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
346
      g_signal_connect_after (layout->buffer, "delete-range",
347
                              G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
348 349

      gtk_text_layout_update_cursor_line (layout);
350 351 352 353 354 355 356
    }
}

void
gtk_text_layout_default_style_changed (GtkTextLayout *layout)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
357

358
  DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
359 360 361 362 363
  gtk_text_layout_invalidate_all (layout);
}

void
gtk_text_layout_set_default_style (GtkTextLayout *layout,
364
                                   GtkTextAttributes *values)
365 366 367 368 369 370 371
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (values != NULL);

  if (values == layout->default_style)
    return;

372
  gtk_text_attributes_ref (values);
373

374
  if (layout->default_style)
375
    gtk_text_attributes_unref (layout->default_style);
376 377 378 379 380 381 382 383

  layout->default_style = values;

  gtk_text_layout_default_style_changed (layout);
}

void
gtk_text_layout_set_contexts (GtkTextLayout *layout,
384 385
                              PangoContext  *ltr_context,
                              PangoContext  *rtl_context)
386 387 388
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));

389 390 391 392
  if (layout->ltr_context != ltr_context)
    {
      if (layout->ltr_context)
	g_object_unref (layout->ltr_context);
393

394 395 396
      layout->ltr_context = ltr_context;
      g_object_ref (layout->ltr_context);
    }
397

398 399 400 401
  if (layout->rtl_context != rtl_context)
    {
      if (layout->rtl_context)
	g_object_unref (layout->rtl_context);
402

403 404 405
      layout->rtl_context = rtl_context;
      g_object_ref (layout->rtl_context);
    }
406

407
  DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
408 409 410
  gtk_text_layout_invalidate_all (layout);
}

411 412 413 414 415 416
/**
 * gtk_text_layout_set_overwrite_mode:
 * @layout: a #GtkTextLayout
 * @overwrite: overwrite mode
 *
 * Sets overwrite mode
Matthias Clasen's avatar
Matthias Clasen committed
417
 */
418 419 420 421 422 423 424 425 426 427 428 429
void
gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
				    gboolean       overwrite)
{
  overwrite = overwrite != 0;
  if (overwrite != layout->overwrite_mode)
    {
      layout->overwrite_mode = overwrite;
      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
    }
}

430 431 432 433 434 435 436 437 438 439 440 441
/**
 * gtk_text_layout_set_cursor_direction:
 * @direction: the new direction(s) for which to draw cursors.
 *             %GTK_TEXT_DIR_NONE means draw cursors for both
 *             left-to-right insertion and right-to-left insertion.
 *             (The two cursors will be visually distinguished.)
 * 
 * Sets which text directions (left-to-right and/or right-to-left) for
 * which cursors will be drawn for the insertion point. The visual
 * point at which new text is inserted depends on whether the new
 * text is right-to-left or left-to-right, so it may be desired to
 * make the drawn position of the cursor depend on the keyboard state.
Matthias Clasen's avatar
Matthias Clasen committed
442
 */
443 444 445 446 447 448 449
void
gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
				      GtkTextDirection direction)
{
  if (direction != layout->cursor_direction)
    {
      layout->cursor_direction = direction;
450
      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
451 452 453
    }
}

454 455 456 457 458 459 460
/**
 * gtk_text_layout_set_keyboard_direction:
 * @keyboard_dir: the current direction of the keyboard.
 *
 * Sets the keyboard direction; this is used as for the bidirectional
 * base direction for the line with the cursor if the line contains
 * only neutral characters.
Matthias Clasen's avatar
Matthias Clasen committed
461
 */
462 463 464 465 466 467 468
void
gtk_text_layout_set_keyboard_direction (GtkTextLayout   *layout,
					GtkTextDirection keyboard_dir)
{
  if (keyboard_dir != layout->keyboard_direction)
    {
      layout->keyboard_direction = keyboard_dir;
469
      gtk_text_layout_invalidate_cursor_line (layout, TRUE);
470 471 472
    }
}

473 474 475 476 477 478 479 480
/**
 * gtk_text_layout_get_buffer:
 * @layout: a #GtkTextLayout
 *
 * Gets the text buffer used by the layout. See
 * gtk_text_layout_set_buffer().
 *
 * Return value: the text buffer used by the layout.
Matthias Clasen's avatar
Matthias Clasen committed
481
 */
482 483 484 485 486 487 488 489
GtkTextBuffer *
gtk_text_layout_get_buffer (GtkTextLayout *layout)
{
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);

  return layout->buffer;
}

490 491 492 493 494 495
void
gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (width >= 0);
  g_return_if_fail (layout->wrap_loop_count == 0);
496

497 498
  if (layout->screen_width == width)
    return;
499

500
  layout->screen_width = width;
501

502
  DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
503 504 505
  gtk_text_layout_invalidate_all (layout);
}

506 507 508 509
/**
 * gtk_text_layout_set_cursor_visible:
 * @layout: a #GtkTextLayout
 * @cursor_visible: If %FALSE, then the insertion cursor will not
510 511
 *   be shown, even if the text is editable.
 *
512 513 514
 * Sets whether the insertion cursor should be shown. Generally,
 * widgets using #GtkTextLayout will hide the cursor when the
 * widget does not have the input focus.
Matthias Clasen's avatar
Matthias Clasen committed
515
 */
516 517
void
gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
518
                                    gboolean       cursor_visible)
519 520 521 522 523 524 525
{
  cursor_visible = (cursor_visible != FALSE);

  if (layout->cursor_visible != cursor_visible)
    {
      GtkTextIter iter;
      gint y, height;
526

527 528 529 530 531
      layout->cursor_visible = cursor_visible;

      /* Now queue a redraw on the paragraph containing the cursor
       */
      gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
532
                                        gtk_text_buffer_get_insert (layout->buffer));
533 534

      gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
Owen Taylor's avatar
Owen Taylor committed
535
      gtk_text_layout_emit_changed (layout, y, height, height);
536

537
      gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
538 539 540 541 542 543
    }
}

/**
 * gtk_text_layout_get_cursor_visible:
 * @layout: a #GtkTextLayout
544
 *
545
 * Returns whether the insertion cursor will be shown.
546
 *
547
 * Return value: if %FALSE, the insertion cursor will not be
Matthias Clasen's avatar
Matthias Clasen committed
548 549
 *     shown, even if the text is editable.
 */
550 551 552 553 554 555
gboolean
gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
{
  return layout->cursor_visible;
}

Owen Taylor's avatar
Owen Taylor committed
556 557 558 559 560 561 562 563 564 565
/**
 * gtk_text_layout_set_preedit_string:
 * @layout: a #PangoLayout
 * @preedit_string: a string to display at the insertion point
 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
 * @cursor_pos: position of cursor within preedit string in chars
 * 
 * Set the preedit string and attributes. The preedit string is a
 * string showing text that is currently being edited and not
 * yet committed into the buffer.
Matthias Clasen's avatar
Matthias Clasen committed
566
 */
Owen Taylor's avatar
Owen Taylor committed
567 568 569 570 571 572 573 574 575
void
gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
				    const gchar   *preedit_string,
				    PangoAttrList *preedit_attrs,
				    gint           cursor_pos)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);

576
  g_free (layout->preedit_string);
Owen Taylor's avatar
Owen Taylor committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

  if (layout->preedit_attrs)
    pango_attr_list_unref (layout->preedit_attrs);

  if (preedit_string)
    {
      layout->preedit_string = g_strdup (preedit_string);
      layout->preedit_len = strlen (layout->preedit_string);
      pango_attr_list_ref (preedit_attrs);
      layout->preedit_attrs = preedit_attrs;

      cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
      layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
    }
  else
    {
      layout->preedit_string = NULL;
      layout->preedit_len = 0;
      layout->preedit_attrs = NULL;
      layout->preedit_cursor = 0;
    }

599
  gtk_text_layout_invalidate_cursor_line (layout, FALSE);
Owen Taylor's avatar
Owen Taylor committed
600 601
}

602 603 604 605 606 607
void
gtk_text_layout_get_size (GtkTextLayout *layout,
                          gint *width,
                          gint *height)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
608

609 610 611 612 613 614 615 616 617 618
  if (width)
    *width = layout->width;

  if (height)
    *height = layout->height;
}

static void
gtk_text_layout_invalidated (GtkTextLayout *layout)
{
Manish Singh's avatar
Manish Singh committed
619
  g_signal_emit (layout, signals[INVALIDATED], 0);
620 621
}

Owen Taylor's avatar
Owen Taylor committed
622 623 624 625 626 627 628 629 630
static void
gtk_text_layout_emit_changed (GtkTextLayout *layout,
			      gint           y,
			      gint           old_height,
			      gint           new_height)
{
  g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
}

631 632 633 634 635 636
static void
text_layout_changed (GtkTextLayout *layout,
                     gint           y,
                     gint           old_height,
                     gint           new_height,
                     gboolean       cursors_only)
637
{
Owen Taylor's avatar
Owen Taylor committed
638 639 640 641 642 643 644 645 646 647 648
  /* Check if the range intersects our cached line display,
   * and invalidate the cached line if so.
   */
  if (layout->one_display_cache)
    {
      GtkTextLine *line = layout->one_display_cache->line;
      gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
						    line, layout);
      gint cache_height = layout->one_display_cache->height;

      if (cache_y + cache_height > y && cache_y < y + old_height)
649
	gtk_text_layout_invalidate_cache (layout, line, cursors_only);
Owen Taylor's avatar
Owen Taylor committed
650
    }
651

Owen Taylor's avatar
Owen Taylor committed
652
  gtk_text_layout_emit_changed (layout, y, old_height, new_height);
653 654
}

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
void
gtk_text_layout_changed (GtkTextLayout *layout,
                         gint           y,
                         gint           old_height,
                         gint           new_height)
{
  text_layout_changed (layout, y, old_height, new_height, FALSE);
}

void
gtk_text_layout_cursors_changed (GtkTextLayout *layout,
                                 gint           y,
				 gint           old_height,
				 gint           new_height)
{
  text_layout_changed (layout, y, old_height, new_height, TRUE);
}

673 674
void
gtk_text_layout_free_line_data (GtkTextLayout     *layout,
675 676
                                GtkTextLine       *line,
                                GtkTextLineData   *line_data)
677
{
678
  GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
679 680 681 682 683 684 685
}

void
gtk_text_layout_invalidate (GtkTextLayout *layout,
                            const GtkTextIter *start_index,
                            const GtkTextIter *end_index)
{
686
  GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
687 688
}

689 690 691 692 693
void
gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
				    const GtkTextIter *start_index,
				    const GtkTextIter *end_index)
{
694
  GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
695 696
}

697 698
GtkTextLineData*
gtk_text_layout_wrap (GtkTextLayout *layout,
699 700 701
                      GtkTextLine  *line,
                      /* may be NULL */
                      GtkTextLineData *line_data)
702
{
703
  return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
704 705
}

706 707 708 709 710 711

/**
 * gtk_text_layout_get_lines:
 *
 * Return value: (element-type GtkTextLine) (transfer container):
 */
712 713 714
GSList*
gtk_text_layout_get_lines (GtkTextLayout *layout,
                           /* [top_y, bottom_y) */
715
                           gint top_y,
716 717 718 719 720 721 722
                           gint bottom_y,
                           gint *first_line_y)
{
  GtkTextLine *first_btree_line;
  GtkTextLine *last_btree_line;
  GtkTextLine *line;
  GSList *retval;
723

724 725 726 727
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
  g_return_val_if_fail (bottom_y > top_y, NULL);

  retval = NULL;
728

729
  first_btree_line =
730
    _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
731
                                   layout, top_y, first_line_y);
732 733 734 735 736
  if (first_btree_line == NULL)
    {
      /* off the bottom */
      return NULL;
    }
737

738
  /* -1 since bottom_y is one past */
739
  last_btree_line =
740
    _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
741
                                    layout, bottom_y - 1, NULL);
742 743

  if (!last_btree_line)
744
    last_btree_line =
745
      _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
746 747 748 749 750 751 752 753 754 755

  g_assert (last_btree_line != NULL);

  line = first_btree_line;
  while (TRUE)
    {
      retval = g_slist_prepend (retval, line);

      if (line == last_btree_line)
        break;
756

757
      line = _gtk_text_line_next_excluding_last (line);
758
    }
759

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  retval = g_slist_reverse (retval);

  return retval;
}

static void
invalidate_cached_style (GtkTextLayout *layout)
{
  free_style_cache (layout);
}

/* These should be called around a loop which wraps a CONTIGUOUS bunch
 * of display lines. If the lines aren't contiguous you can't call
 * these.
 */
void
gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->one_style_cache == NULL);
780

781 782 783 784 785 786 787
  layout->wrap_loop_count += 1;
}

void
gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
{
  g_return_if_fail (layout->wrap_loop_count > 0);
788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  layout->wrap_loop_count -= 1;

  if (layout->wrap_loop_count == 0)
    {
      /* We cache a some stuff if we're iterating over some lines wrapping
       * them. This cleans it up.
       */
      /* Nuke our cached style */
      invalidate_cached_style (layout);
      g_assert (layout->one_style_cache == NULL);
    }
}

static void
gtk_text_layout_invalidate_all (GtkTextLayout *layout)
{
  GtkTextIter start;
  GtkTextIter end;
807

808 809 810 811 812 813 814 815
  if (layout->buffer == NULL)
    return;

  gtk_text_buffer_get_bounds (layout->buffer, &start, &end);

  gtk_text_layout_invalidate (layout, &start, &end);
}

816 817
static void
gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
818 819
                                  GtkTextLine   *line,
				  gboolean       cursors_only)
820 821 822
{
  if (layout->one_display_cache && line == layout->one_display_cache->line)
    {
823 824 825 826
      GtkTextLineDisplay *display = layout->one_display_cache;

      if (cursors_only)
	{
827 828
          if (display->cursors)
            g_array_free (display->cursors, TRUE);
829 830
	  display->cursors = NULL;
	  display->cursors_invalid = TRUE;
831
	  display->has_block_cursor = FALSE;
832 833 834 835 836 837
	}
      else
	{
	  layout->one_display_cache = NULL;
	  gtk_text_layout_free_line_display (layout, display);
	}
838 839 840
    }
}

841 842 843
/* Now invalidate the paragraph containing the cursor
 */
static void
844 845
gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
					gboolean cursors_only)
846
{
847
  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
848 849
  GtkTextLineData *line_data;

850 851
  if (priv->cursor_line == NULL)
    return;
852

853
  line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
854 855
  if (line_data)
    {
856 857 858 859 860 861 862 863
      if (cursors_only)
	  gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
      else
	{
	  gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
	  _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
	}

864 865 866 867
      gtk_text_layout_invalidated (layout);
    }
}

868 869 870 871 872 873 874
static void
gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
{
  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
  GtkTextIter iter;

  gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
875 876
                                    gtk_text_buffer_get_insert (layout->buffer));

877 878 879
  priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
}

880 881 882 883 884 885 886
static void
gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                 const GtkTextIter *start,
                                 const GtkTextIter *end)
{
  GtkTextLine *line;
  GtkTextLine *last_line;
887

888 889 890
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->wrap_loop_count == 0);

891 892 893 894 895 896 897
  /* Because we may be invalidating a mark, it's entirely possible
   * that gtk_text_iter_equal (start, end) in which case we
   * should still invalidate the line they are both on. i.e.
   * we always invalidate the line with "start" even
   * if there's an empty range.
   */
  
898 899 900 901
#if 0
  gtk_text_view_index_spew (start_index, "invalidate start");
  gtk_text_view_index_spew (end_index, "invalidate end");
#endif
902

903 904
  last_line = _gtk_text_iter_get_text_line (end);
  line = _gtk_text_iter_get_text_line (start);
905 906 907

  while (TRUE)
    {
908
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
909

910
      gtk_text_layout_invalidate_cache (layout, line, FALSE);
911
      
912
      if (line_data)
913
        _gtk_text_line_invalidate_wrap (line, line_data);
914

915 916
      if (line == last_line)
        break;
917

918
      line = _gtk_text_line_next_excluding_last (line);
919
    }
920

921 922 923
  gtk_text_layout_invalidated (layout);
}

924 925 926 927 928 929 930 931 932 933 934 935 936
static void
gtk_text_layout_real_invalidate_cursors (GtkTextLayout     *layout,
					 const GtkTextIter *start,
					 const GtkTextIter *end)
{
  /* Check if the range intersects our cached line display,
   * and invalidate the cached line if so.
   */
  if (layout->one_display_cache)
    {
      GtkTextIter line_start, line_end;
      GtkTextLine *line = layout->one_display_cache->line;

937 938
      gtk_text_layout_get_iter_at_line (layout, &line_start, line, 0);

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
      line_end = line_start;
      if (!gtk_text_iter_ends_line (&line_end))
	gtk_text_iter_forward_to_line_end (&line_end);

      if (gtk_text_iter_compare (start, end) > 0)
	{
	  const GtkTextIter *tmp = start;
	  start = end;
	  end = tmp;
	}

      if (gtk_text_iter_compare (&line_start, end) <= 0 &&
	  gtk_text_iter_compare (start, &line_end) <= 0)
	{
	  gtk_text_layout_invalidate_cache (layout, line, TRUE);
	}
    }

  gtk_text_layout_invalidated (layout);
}

960 961
static void
gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
962 963
                                     GtkTextLine       *line,
                                     GtkTextLineData   *line_data)
964
{
965
  gtk_text_layout_invalidate_cache (layout, line, FALSE);
966

967 968 969 970 971 972
  g_free (line_data);
}

/**
 * gtk_text_layout_is_valid:
 * @layout: a #GtkTextLayout
973
 *
974
 * Check if there are any invalid regions in a #GtkTextLayout’s buffer
975
 *
976
 * Return value: %TRUE if any invalid regions were found
Matthias Clasen's avatar
Matthias Clasen committed
977
 */
978 979 980 981
gboolean
gtk_text_layout_is_valid (GtkTextLayout *layout)
{
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
982

983
  return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
984
                                  layout);
985 986
}

987 988 989
static void
update_layout_size (GtkTextLayout *layout)
{
990
  _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
991 992 993 994
				layout,
				&layout->width, &layout->height);
}

995 996 997 998 999
/**
 * gtk_text_layout_validate_yrange:
 * @layout: a #GtkTextLayout
 * @anchor: iter pointing into a line that will be used as the
 *          coordinate origin
1000 1001 1002 1003 1004 1005
 * @y0_: offset from the top of the line pointed to by @anchor at
 *       which to begin validation. (The offset here is in pixels
 *       after validation.)
 * @y1_: offset from the top of the line pointed to by @anchor at
 *       which to end validation. (The offset here is in pixels
 *       after validation.)
1006 1007
 *
 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
1008
 * signal will be emitted if any lines are validated.
Matthias Clasen's avatar
Matthias Clasen committed
1009
 */
1010 1011
void
gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1012 1013 1014
                                 GtkTextIter   *anchor,
                                 gint           y0,
                                 gint           y1)
1015 1016 1017 1018 1019 1020
{
  GtkTextLine *line;
  GtkTextLine *first_line = NULL;
  GtkTextLine *last_line = NULL;
  gint seen;
  gint delta_height = 0;
1021 1022 1023
  gint first_line_y = 0;        /* Quiet GCC */
  gint last_line_y = 0;         /* Quiet GCC */

1024
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1025

1026 1027 1028 1029
  if (y0 > 0)
    y0 = 0;
  if (y1 < 0)
    y1 = 0;
1030
  
1031 1032
  /* Validate backwards from the anchor line to y0
   */
1033
  line = _gtk_text_iter_get_text_line (anchor);
1034
  line = _gtk_text_line_previous (line);
1035 1036 1037
  seen = 0;
  while (line && seen < -y0)
    {
1038
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1039
      if (!line_data || !line_data->valid)
1040
        {
1041 1042 1043
          gint old_height, new_height;
	  
	  old_height = line_data ? line_data->height : 0;
1044

1045
          _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1046
                                         line, layout);
1047
          line_data = _gtk_text_line_get_data (line, layout);
1048

1049 1050 1051
	  new_height = line_data ? line_data->height : 0;

          delta_height += new_height - old_height;
1052
          
1053
          first_line = line;
1054
          first_line_y = -seen - new_height;
1055 1056 1057
          if (!last_line)
            {
              last_line = line;
1058
              last_line_y = -seen;
1059 1060 1061
            }
        }

1062
      seen += line_data ? line_data->height : 0;
1063
      line = _gtk_text_line_previous (line);
1064 1065 1066
    }

  /* Validate forwards to y1 */
1067
  line = _gtk_text_iter_get_text_line (anchor);
1068 1069 1070
  seen = 0;
  while (line && seen < y1)
    {
1071
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1072
      if (!line_data || !line_data->valid)
1073
        {
1074 1075 1076
          gint old_height, new_height;
	  
	  old_height = line_data ? line_data->height : 0;
1077

1078
          _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1079
                                         line, layout);
1080
          line_data = _gtk_text_line_get_data (line, layout);
1081
	  new_height = line_data ? line_data->height : 0;
1082

1083
          delta_height += new_height - old_height;
1084
          
1085 1086 1087 1088 1089 1090
          if (!first_line)
            {
              first_line = line;
              first_line_y = seen;
            }
          last_line = line;
1091
          last_line_y = seen + new_height;
1092 1093
        }

1094
      seen += line_data ? line_data->height : 0;
1095
      line = _gtk_text_line_next_excluding_last (line);
1096 1097
    }

1098 1099
  /* If we found and validated any invalid lines, update size and
   * emit the changed signal
1100 1101 1102
   */
  if (first_line)
    {
1103 1104 1105 1106
      gint line_top;

      update_layout_size (layout);

1107
      line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1108
                                                first_line, layout);
1109

Owen Taylor's avatar
Owen Taylor committed
1110 1111 1112 1113
      gtk_text_layout_emit_changed (layout,
				    line_top,
				    last_line_y - first_line_y - delta_height,
				    last_line_y - first_line_y);
1114 1115 1116 1117 1118 1119 1120 1121
    }
}

/**
 * gtk_text_layout_validate:
 * @tree: a #GtkTextLayout
 * @max_pixels: the maximum number of pixels to validate. (No more
 *              than one paragraph beyond this limit will be validated)
1122
 *
1123 1124 1125 1126 1127
 * Validate regions of a #GtkTextLayout. The ::changed signal will
 * be emitted for each region validated.
 **/
void
gtk_text_layout_validate (GtkTextLayout *layout,
1128
                          gint           max_pixels)
1129 1130 1131 1132
{
  gint y, old_height, new_height;

  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1133

1134
  while (max_pixels > 0 &&
1135
         _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1136 1137
                                   layout,  max_pixels,
                                   &y, &old_height, &new_height))
1138 1139
    {
      max_pixels -= new_height;
1140 1141

      update_layout_size (layout);
Owen Taylor's avatar
Owen Taylor committed
1142
      gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1143 1144 1145 1146 1147
    }
}

static GtkTextLineData*
gtk_text_layout_real_wrap (GtkTextLayout   *layout,
1148 1149 1150
                           GtkTextLine     *line,
                           /* may be NULL */
                           GtkTextLineData *line_data)
1151 1152
{
  GtkTextLineDisplay *display;
1153

1154
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1155 1156
  g_return_val_if_fail (line != NULL, NULL);
  
1157 1158
  if (line_data == NULL)
    {
1159
      line_data = _gtk_text_line_data_new (layout, line);
1160
      _gtk_text_line_add_data (line, line_data);
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    }

  display = gtk_text_layout_get_line_display (layout, line, TRUE);
  line_data->width = display->width;
  line_data->height = display->height;
  line_data->valid = TRUE;
  gtk_text_layout_free_line_display (layout, display);

  return line_data;
}

/*
 * Layout utility functions
 */

/* If you get the style with get_style () you need to call
   release_style () to free it. */
1178
static GtkTextAttributes*
1179
get_style (GtkTextLayout *layout,
1180
	   GPtrArray     *tags)
1181
{
1182
  GtkTextAttributes *style;
1183

1184 1185 1186 1187 1188 1189
  /* If we have the one-style cache, then it means
     that we haven't seen a toggle since we filled in the
     one-style cache.
  */
  if (layout->one_style_cache != NULL)
    {
1190
      gtk_text_attributes_ref (layout->one_style_cache);
1191 1192 1193 1194
      return layout->one_style_cache;
    }

  g_assert (layout->one_style_cache == NULL);
1195

1196
  /* No tags, use default style */
1197
  if (tags == NULL || tags->len == 0)
1198 1199 1200
    {
      /* One ref for the return value, one ref for the
         layout->one_style_cache reference */
1201 1202
      gtk_text_attributes_ref (layout->default_style);
      gtk_text_attributes_ref (layout->default_style);
1203 1204 1205 1206
      layout->one_style_cache = layout->default_style;

      return layout->default_style;
    }
1207

1208
  style = gtk_text_attributes_new ();
1209

1210 1211
  gtk_text_attributes_copy_values (layout->default_style,
                                   style);
1212

1213
  _gtk_text_attributes_fill_from_tags (style,
1214 1215
                                       (GtkTextTag**) tags->pdata,
                                       tags->len);
1216 1217 1218 1219 1220

  g_assert (style->refcount == 1);

  /* Leave this style as the last one seen */
  g_assert (layout->one_style_cache == NULL);
1221
  gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1222
  layout->one_style_cache = style;
1223

1224 1225 1226 1227 1228 1229
  /* Returning yet another refcount */
  return style;
}

static void
release_style (GtkTextLayout *layout,
1230
               GtkTextAttributes *style)
1231 1232 1233 1234
{
  g_return_if_fail (style != NULL);
  g_return_if_fail (style->refcount > 0);

1235
  gtk_text_attributes_unref (style);
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
}

/*
 * Lines
 */

/* This function tries to optimize the case where a line
   is completely invisible */
static gboolean
totally_invisible_line (GtkTextLayout *layout,
1246 1247
                        GtkTextLine   *line,
                        GtkTextIter   *iter)
1248 1249 1250
{
  GtkTextLineSegment *seg;
  int bytes = 0;
1251

1252 1253 1254 1255
  /* Check if the first char is visible, if so we are partially visible.  
   * Note that we have to check this since we don't know the current 
   * invisible/noninvisible toggle state; this function can use the whole btree 
   * to get it right.
1256
   */
1257
  gtk_text_layout_get_iter_at_line (layout, iter, line, 0);
1258
  if (!_gtk_text_btree_char_is_invisible (iter))
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    return FALSE;

  bytes = 0;
  seg = line->segments;

  while (seg != NULL)
    {
      if (seg->byte_count > 0)
        bytes += seg->byte_count;

      /* Note that these two tests can cause us to bail out
1270 1271 1272 1273 1274
       * when we shouldn't, because a higher-priority tag
       * may override these settings. However the important
       * thing is to only invisible really-invisible lines, rather
       * than to invisible all really-invisible lines.
       */
1275

1276 1277 1278
      else if (seg->type == &gtk_text_toggle_on_type)
        {
          invalidate_cached_style (layout);
1279

1280
          /* Bail out if an elision-unsetting tag begins */
1281 1282
          if (seg->body.toggle.info->tag->priv->invisible_set &&
              !seg->body.toggle.info->tag->priv->values->invisible)
1283 1284 1285 1286 1287
            break;
        }
      else if (seg->type == &gtk_text_toggle_off_type)
        {
          invalidate_cached_style (layout);
1288

1289
          /* Bail out if an elision-setting tag ends */
1290 1291
          if (seg->body.toggle.info->tag->priv->invisible_set &&
              seg->body.toggle.info->tag->priv->values->invisible)
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
            break;
        }

      seg = seg->next;
    }

  if (seg != NULL)       /* didn't reach line end */
    return FALSE;

  return TRUE;
}

static void
set_para_values (GtkTextLayout      *layout,
1306 1307
                 PangoDirection      base_dir,
                 GtkTextAttributes  *style,
1308
                 GtkTextLineDisplay *display)
1309 1310
{
  PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1311
  PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1312

1313 1314 1315 1316 1317
  switch (base_dir)
    {
    /* If no base direction was found, then use the style direction */
    case PANGO_DIRECTION_NEUTRAL :
      display->direction = style->direction;
1318

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
      /* Override the base direction */
      if (display->direction == GTK_TEXT_DIR_RTL)
        base_dir = PANGO_DIRECTION_RTL;
      else
        base_dir = PANGO_DIRECTION_LTR;
      
      break;
    case PANGO_DIRECTION_RTL :
      display->direction = GTK_TEXT_DIR_RTL;
      break;
    default:
      display->direction = GTK_TEXT_DIR_LTR;
      break;
    }
  
  if (display->direction == GTK_TEXT_DIR_RTL)
1335
    display->layout = pango_layout_new (layout->rtl_context);
1336 1337
  else
    display->layout = pango_layout_new (layout->ltr_context);
1338

1339
  switch (style->justification)
1340 1341
    {
    case GTK_JUSTIFY_LEFT:
1342
      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1343 1344
      break;
    case GTK_JUSTIFY_RIGHT:
1345
      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1346 1347 1348 1349 1350
      break;
    case GTK_JUSTIFY_CENTER:
      pango_align = PANGO_ALIGN_CENTER;
      break;
    case GTK_JUSTIFY_FILL:
1351 1352
      pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
      pango_layout_set_justify (display->layout, TRUE);
1353 1354 1355 1356 1357 1358 1359
      break;
    default:
      g_assert_not_reached ();
      break;
    }

  pango_layout_set_alignment (display->layout, pango_align);
1360 1361
  pango_layout_set_spacing (display->layout,
                            style->pixels_inside_wrap * PANGO_SCALE);
1362

1363 1364
  if (style->tabs)
    pango_layout_set_tabs (display->layout, style->tabs);
1365

1366 1367 1368
  display->top_margin = style->pixels_above_lines;
  display->height = style->pixels_above_lines + style->pixels_below_lines;
  display->bottom_margin = style->pixels_below_lines;
1369
  display->left_margin = style->left_margin;
1370
  display->right_margin = style->right_margin;
1371 1372 1373 1374 1375
  
  display->x_offset = display->left_margin;

  pango_layout_set_indent (display->layout,
                           style->indent * PANGO_SCALE);
1376 1377 1378

  switch (style->wrap_mode)
    {
1379
    case GTK_WRAP_CHAR:
1380
      pango_wrap = PANGO_WRAP_CHAR;
1381
      break;
1382
    case GTK_WRAP_WORD:
1383
      pango_wrap = PANGO_WRAP_WORD;
1384
      break;
1385

1386
    case GTK_WRAP_WORD_CHAR:
1387
      pango_wrap = PANGO_WRAP_WORD_CHAR;
1388 1389
      break;

1390
    case GTK_WRAP_NONE:
1391 1392
      break;
    }
1393

1394 1395 1396 1397 1398 1399 1400
  if (style->wrap_mode != GTK_WRAP_NONE)
    {
      int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
      pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
      pango_layout_set_wrap (display->layout, pango_wrap);
    }

1401
  display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1402 1403 1404 1405
  
  if (style->pg_bg_color)
    display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
  else
1406 1407 1408 1409 1410 1411
    display->pg_bg_color = NULL;

  if (style->pg_bg_rgba)
    display->pg_bg_rgba = gdk_rgba_copy (style->pg_bg_rgba);
  else
    display->pg_bg_rgba = NULL;
1412 1413 1414 1415 1416 1417
}

static PangoAttribute *
gtk_text_attr_appearance_copy (const PangoAttribute *attr)
{
  const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1418

1419 1420 1421 1422 1423 1424
  return gtk_text_attr_appearance_new (&appearance_attr->appearance);
}

static void
gtk_text_attr_appearance_destroy (PangoAttribute *attr)
{
Matthias Clasen's avatar
Matthias Clasen committed
1425
  GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1426

1427 1428 1429 1430 1431 1432
  if (appearance_attr->appearance.rgba[0])
    gdk_rgba_free (appearance_attr->appearance.rgba[0]);

  if (appearance_attr->appearance.rgba[1])
    gdk_rgba_free (appearance_attr->appearance.rgba[1]);

Matthias Clasen's avatar
Matthias Clasen committed
1433
  g_slice_free (GtkTextAttrAppearance, appearance_attr);
1434 1435
}

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
static gboolean 
rgba_equal (const GdkRGBA *rgba1, const GdkRGBA *rgba2)