gtktextlayout.c 98.3 KB
Newer Older
1 2
/* GTK - The GIMP Toolkit
 * 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 26 27 28 29
 * 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
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Original Tk license:
30 31 32 33 34
 *
 * 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.
35
 *
36 37 38 39 40 41 42 43 44
 * 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.
45
 *
46 47 48 49 50
 * 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.
51
 *
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 * 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.
70 71 72 73 74 75 76
 *
 */
/*
 * 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/.
77 78
 */

79
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
80
#include <config.h>
81
#include "gtkmarshalers.h"
82 83 84
#include "gtktextlayout.h"
#include "gtktextbtree.h"
#include "gtktextiterprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
85
#include "gtkintl.h"
86
#include "gtkalias.h"
87 88

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

91 92 93 94 95 96 97 98 99 100 101 102
#define GTK_TEXT_LAYOUT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))

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;
};

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

static void gtk_text_layout_invalidated     (GtkTextLayout     *layout);

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

static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);

static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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);

144 145 146
enum {
  INVALIDATED,
  CHANGED,
147
  ALLOCATE_CHILD,
148 149 150 151 152 153 154 155
  LAST_SIGNAL
};

enum {
  ARG_0,
  LAST_ARG
};

156 157
#define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)

158
static void gtk_text_layout_init       (GtkTextLayout      *text_layout);
159
static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
160
static void gtk_text_layout_finalize   (GObject            *object);
161 162 163 164 165 166 167


static GtkObjectClass *parent_class = NULL;
static guint signals[LAST_SIGNAL] = { 0 };

PangoAttrType gtk_text_attr_appearance_type = 0;

168
GType
169 170
gtk_text_layout_get_type (void)
{
171
  static GType our_type = 0;
172 173 174

  if (our_type == 0)
    {
175
      static const GTypeInfo our_info =
176 177
      {
        sizeof (GtkTextLayoutClass),
178 179 180 181 182 183 184 185
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_text_layout_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkTextLayout),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_text_layout_init
186 187
      };

Matthias Clasen's avatar
Matthias Clasen committed
188
      our_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkTextLayout"),
Manish Singh's avatar
Manish Singh committed
189
                                         &our_info, 0);
190
    }
191 192 193 194 195 196 197

  return our_type;
}

static void
gtk_text_layout_class_init (GtkTextLayoutClass *klass)
{
198
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
199

200 201 202
  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = gtk_text_layout_finalize;
203 204 205 206 207

  klass->wrap = gtk_text_layout_real_wrap;
  klass->invalidate = gtk_text_layout_real_invalidate;
  klass->free_line_data = gtk_text_layout_real_free_line_data;

208
  signals[INVALIDATED] =
Matthias Clasen's avatar
Matthias Clasen committed
209
    g_signal_new (I_("invalidated"),
210 211 212 213
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
                  NULL, NULL,
214
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
215
                  G_TYPE_NONE,
216
                  0);
217 218

  signals[CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
219
    g_signal_new (I_("changed"),
220 221 222 223
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
                  NULL, NULL,
224
                  _gtk_marshal_VOID__INT_INT_INT,
Manish Singh's avatar
Manish Singh committed
225
                  G_TYPE_NONE,
226
                  3,
Manish Singh's avatar
Manish Singh committed
227 228 229
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT);
230

231
  signals[ALLOCATE_CHILD] =
Matthias Clasen's avatar
Matthias Clasen committed
232
    g_signal_new (I_("allocate_child"),
233 234 235 236
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
                  NULL, NULL,
237
                  _gtk_marshal_VOID__OBJECT_INT_INT,
Manish Singh's avatar
Manish Singh committed
238
                  G_TYPE_NONE,
239 240
                  3,
                  GTK_TYPE_OBJECT,
Manish Singh's avatar
Manish Singh committed
241 242
                  G_TYPE_INT,
                  G_TYPE_INT);
243 244
  
  g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
245 246
}

247
static void
248 249
gtk_text_layout_init (GtkTextLayout *text_layout)
{
250
  text_layout->cursor_visible = TRUE;
251 252 253 254 255
}

GtkTextLayout*
gtk_text_layout_new (void)
{
Manish Singh's avatar
Manish Singh committed
256
  return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
257 258 259 260 261 262 263
}

static void
free_style_cache (GtkTextLayout *text_layout)
{
  if (text_layout->one_style_cache)
    {
264
      gtk_text_attributes_unref (text_layout->one_style_cache);
265 266 267 268 269
      text_layout->one_style_cache = NULL;
    }
}

static void
270
gtk_text_layout_finalize (GObject *object)
271 272 273 274 275
{
  GtkTextLayout *layout;

  layout = GTK_TEXT_LAYOUT (object);

276
  gtk_text_layout_set_buffer (layout, NULL);
277 278

  if (layout->default_style)
279
    gtk_text_attributes_unref (layout->default_style);
280 281 282 283
  layout->default_style = NULL;

  if (layout->ltr_context)
    {
Manish Singh's avatar
Manish Singh committed
284
      g_object_unref (layout->ltr_context);
285 286 287 288
      layout->ltr_context = NULL;
    }
  if (layout->rtl_context)
    {
Manish Singh's avatar
Manish Singh committed
289
      g_object_unref (layout->rtl_context);
290 291
      layout->rtl_context = NULL;
    }
292
  
293 294 295 296 297 298 299
  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);
    }

300 301 302 303 304 305 306 307 308 309 310 311 312
  if (layout->preedit_string)
    {
      g_free (layout->preedit_string);
      layout->preedit_string = NULL;
    }

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


313 314 315 316 317 318 319 320 321
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

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));
322

323 324 325 326
  if (layout->buffer == buffer)
    return;

  free_style_cache (layout);
327

328 329
  if (layout->buffer)
    {
330
      _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
331
                                  layout);
332

333 334 335 336 337 338 339 340 341 342
      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
343
      g_object_unref (layout->buffer);
344 345 346 347 348 349 350
      layout->buffer = NULL;
    }

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

Manish Singh's avatar
Manish Singh committed
351
      g_object_ref (buffer);
352

353
      _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
354 355 356 357 358 359 360 361

      /* Bind to all signals that move the insert mark. */
      g_signal_connect_after (layout->buffer, "mark_set",
                              G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
      g_signal_connect_after (layout->buffer, "insert_text",
                              G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
      g_signal_connect_after (layout->buffer, "delete_range",
                              G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
362 363

      gtk_text_layout_update_cursor_line (layout);
364 365 366 367 368 369 370
    }
}

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

372
  DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
373 374 375 376 377
  gtk_text_layout_invalidate_all (layout);
}

void
gtk_text_layout_set_default_style (GtkTextLayout *layout,
378
                                   GtkTextAttributes *values)
379 380 381 382 383 384 385
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (values != NULL);

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

386
  gtk_text_attributes_ref (values);
387

388
  if (layout->default_style)
389
    gtk_text_attributes_unref (layout->default_style);
390 391 392 393 394 395 396 397

  layout->default_style = values;

  gtk_text_layout_default_style_changed (layout);
}

void
gtk_text_layout_set_contexts (GtkTextLayout *layout,
398 399
                              PangoContext  *ltr_context,
                              PangoContext  *rtl_context)
400 401 402 403
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));

  if (layout->ltr_context)
Manish Singh's avatar
Manish Singh committed
404
    g_object_unref (ltr_context);
405 406

  layout->ltr_context = ltr_context;
Manish Singh's avatar
Manish Singh committed
407
  g_object_ref (ltr_context);
408

409
  if (layout->rtl_context)
Manish Singh's avatar
Manish Singh committed
410
    g_object_unref (rtl_context);
411 412

  layout->rtl_context = rtl_context;
Manish Singh's avatar
Manish Singh committed
413
  g_object_ref (rtl_context);
414

415
  DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
416 417 418
  gtk_text_layout_invalidate_all (layout);
}

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/**
 * 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.
 **/
void
gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
				      GtkTextDirection direction)
{
  if (direction != layout->cursor_direction)
    {
      layout->cursor_direction = direction;
      gtk_text_layout_invalidate_cursor_line (layout);
    }
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
/**
 * 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.
 **/
void
gtk_text_layout_set_keyboard_direction (GtkTextLayout   *layout,
					GtkTextDirection keyboard_dir)
{
  if (keyboard_dir != layout->keyboard_direction)
    {
      layout->keyboard_direction = keyboard_dir;
      gtk_text_layout_invalidate_cursor_line (layout);
    }
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
/**
 * 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.
 **/
GtkTextBuffer *
gtk_text_layout_get_buffer (GtkTextLayout *layout)
{
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);

  return layout->buffer;
}

479 480 481 482 483 484
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);
485

486 487
  if (layout->screen_width == width)
    return;
488

489
  layout->screen_width = width;
490

491
  DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
492 493 494
  gtk_text_layout_invalidate_all (layout);
}

495 496 497 498
/**
 * gtk_text_layout_set_cursor_visible:
 * @layout: a #GtkTextLayout
 * @cursor_visible: If %FALSE, then the insertion cursor will not
499 500
 *   be shown, even if the text is editable.
 *
501 502 503 504 505 506
 * 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.
 **/
void
gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
507
                                    gboolean       cursor_visible)
508 509 510 511 512 513 514
{
  cursor_visible = (cursor_visible != FALSE);

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

516 517 518 519 520
      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,
521
                                        gtk_text_buffer_get_mark (layout->buffer, "insert"));
522 523

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

526
      gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
527 528 529 530 531 532
    }
}

/**
 * gtk_text_layout_get_cursor_visible:
 * @layout: a #GtkTextLayout
533
 *
534
 * Returns whether the insertion cursor will be shown.
535
 *
536 537 538 539 540 541 542 543 544
 * Return value: if %FALSE, the insertion cursor will not be
    shown, even if the text is editable.
 **/
gboolean
gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
{
  return layout->cursor_visible;
}

Owen Taylor's avatar
Owen Taylor committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/**
 * 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.
 **/
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);

  if (layout->preedit_string)
    g_free (layout->preedit_string);

  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;
    }

589
  gtk_text_layout_invalidate_cursor_line (layout);
Owen Taylor's avatar
Owen Taylor committed
590 591
}

592 593 594 595 596 597
void
gtk_text_layout_get_size (GtkTextLayout *layout,
                          gint *width,
                          gint *height)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
598

599 600 601 602 603 604 605 606 607 608
  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
609
  g_signal_emit (layout, signals[INVALIDATED], 0);
610 611
}

Owen Taylor's avatar
Owen Taylor committed
612 613 614 615 616 617 618 619 620
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);
}

621 622
void
gtk_text_layout_changed (GtkTextLayout *layout,
623 624 625
                         gint           y,
                         gint           old_height,
                         gint           new_height)
626
{
Owen Taylor's avatar
Owen Taylor committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
  /* 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)
	gtk_text_layout_invalidate_cache (layout, line);
    }
  
  gtk_text_layout_emit_changed (layout, y, old_height, new_height);
642 643 644 645
}

void
gtk_text_layout_free_line_data (GtkTextLayout     *layout,
646 647
                                GtkTextLine       *line,
                                GtkTextLineData   *line_data)
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
{
  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
    (layout, line, line_data);
}

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

GtkTextLineData*
gtk_text_layout_wrap (GtkTextLayout *layout,
664 665 666
                      GtkTextLine  *line,
                      /* may be NULL */
                      GtkTextLineData *line_data)
667 668 669 670 671 672 673
{
  return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
}

GSList*
gtk_text_layout_get_lines (GtkTextLayout *layout,
                           /* [top_y, bottom_y) */
674
                           gint top_y,
675 676 677 678 679 680 681
                           gint bottom_y,
                           gint *first_line_y)
{
  GtkTextLine *first_btree_line;
  GtkTextLine *last_btree_line;
  GtkTextLine *line;
  GSList *retval;
682

683 684 685 686
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
  g_return_val_if_fail (bottom_y > top_y, NULL);

  retval = NULL;
687

688
  first_btree_line =
689
    _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
690
                                   layout, top_y, first_line_y);
691 692 693 694 695
  if (first_btree_line == NULL)
    {
      /* off the bottom */
      return NULL;
    }
696

697
  /* -1 since bottom_y is one past */
698
  last_btree_line =
699
    _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
700
                                    layout, bottom_y - 1, NULL);
701 702

  if (!last_btree_line)
703
    last_btree_line =
704
      _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
705 706 707 708 709 710 711 712 713 714

  g_assert (last_btree_line != NULL);

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

      if (line == last_btree_line)
        break;
715

716
      line = _gtk_text_line_next_excluding_last (line);
717
    }
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  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);
739

740 741 742 743 744 745 746
  layout->wrap_loop_count += 1;
}

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

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  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;
766

767 768 769 770 771 772 773 774
  if (layout->buffer == NULL)
    return;

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

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

775 776
static void
gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
777
                                  GtkTextLine   *line)
778 779 780 781 782 783 784 785 786
{
  if (layout->one_display_cache && line == layout->one_display_cache->line)
    {
      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
      layout->one_display_cache = NULL;
      gtk_text_layout_free_line_display (layout, tmp_display);
    }
}

787 788 789 790 791
/* Now invalidate the paragraph containing the cursor
 */
static void
gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
{
792
  GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
793 794
  GtkTextLineData *line_data;

795 796
  if (priv->cursor_line == NULL)
    return;
797

798
  line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
799 800
  if (line_data)
    {
801 802
      gtk_text_layout_invalidate_cache (layout, priv->cursor_line);
      _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
803 804 805 806
      gtk_text_layout_invalidated (layout);
    }
}

807 808 809 810 811 812 813 814 815 816 817 818
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,
                                    gtk_text_buffer_get_mark (layout->buffer, "insert"));
  
  priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
}

819 820 821 822 823 824 825
static void
gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                 const GtkTextIter *start,
                                 const GtkTextIter *end)
{
  GtkTextLine *line;
  GtkTextLine *last_line;
826

827 828 829
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->wrap_loop_count == 0);

830 831 832 833 834 835 836
  /* 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.
   */
  
837 838 839 840
#if 0
  gtk_text_view_index_spew (start_index, "invalidate start");
  gtk_text_view_index_spew (end_index, "invalidate end");
#endif
841

842 843
  last_line = _gtk_text_iter_get_text_line (end);
  line = _gtk_text_iter_get_text_line (start);
844 845 846

  while (TRUE)
    {
847
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
848

849 850
      gtk_text_layout_invalidate_cache (layout, line);
      
851
      if (line_data)
852
        _gtk_text_line_invalidate_wrap (line, line_data);
853

854 855
      if (line == last_line)
        break;
856

857
      line = _gtk_text_line_next_excluding_last (line);
858
    }
859

860 861 862 863 864
  gtk_text_layout_invalidated (layout);
}

static void
gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
865 866
                                     GtkTextLine       *line,
                                     GtkTextLineData   *line_data)
867
{
868
  gtk_text_layout_invalidate_cache (layout, line);
869

870 871 872 873 874 875 876 877
  g_free (line_data);
}



/**
 * gtk_text_layout_is_valid:
 * @layout: a #GtkTextLayout
878
 *
879
 * Check if there are any invalid regions in a #GtkTextLayout's buffer
880
 *
881
 * Return value: %TRUE if any invalid regions were found
882 883 884 885 886
 **/
gboolean
gtk_text_layout_is_valid (GtkTextLayout *layout)
{
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
887

888
  return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
889
                                  layout);
890 891
}

892 893 894
static void
update_layout_size (GtkTextLayout *layout)
{
895
  _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
896 897 898 899
				layout,
				&layout->width, &layout->height);
}

900 901 902 903 904
/**
 * gtk_text_layout_validate_yrange:
 * @layout: a #GtkTextLayout
 * @anchor: iter pointing into a line that will be used as the
 *          coordinate origin
905 906 907 908 909 910
 * @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.)
911 912
 *
 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
913 914 915 916
 * signal will be emitted if any lines are validated.
 **/
void
gtk_text_layout_validate_yrange (GtkTextLayout *layout,
917 918 919
                                 GtkTextIter   *anchor,
                                 gint           y0,
                                 gint           y1)
920 921 922 923 924 925
{
  GtkTextLine *line;
  GtkTextLine *first_line = NULL;
  GtkTextLine *last_line = NULL;
  gint seen;
  gint delta_height = 0;
926 927 928
  gint first_line_y = 0;        /* Quiet GCC */
  gint last_line_y = 0;         /* Quiet GCC */

929
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
930

931 932 933 934
  if (y0 > 0)
    y0 = 0;
  if (y1 < 0)
    y1 = 0;
935
  
936 937
  /* Validate backwards from the anchor line to y0
   */
938
  line = _gtk_text_iter_get_text_line (anchor);
939
  line = _gtk_text_line_previous (line);
940 941 942
  seen = 0;
  while (line && seen < -y0)
    {
943
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
944
      if (!line_data || !line_data->valid)
945 946 947
        {
          gint old_height = line_data ? line_data->height : 0;

948
          _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
949
                                         line, layout);
950
          line_data = _gtk_text_line_get_data (line, layout);
951 952

          delta_height += line_data->height - old_height;
953
          
954
          first_line = line;
955
          first_line_y = -seen - line_data->height;
956 957 958
          if (!last_line)
            {
              last_line = line;
959
              last_line_y = -seen;
960 961 962
            }
        }

963
      seen += line_data->height;
964
      line = _gtk_text_line_previous (line);
965 966 967
    }

  /* Validate forwards to y1 */
968
  line = _gtk_text_iter_get_text_line (anchor);
969 970 971
  seen = 0;
  while (line && seen < y1)
    {
972
      GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
973
      if (!line_data || !line_data->valid)
974 975 976
        {
          gint old_height = line_data ? line_data->height : 0;

977
          _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
978
                                         line, layout);
979
          line_data = _gtk_text_line_get_data (line, layout);
980 981

          delta_height += line_data->height - old_height;
982
          
983 984 985 986 987 988 989 990 991
          if (!first_line)
            {
              first_line = line;
              first_line_y = seen;
            }
          last_line = line;
          last_line_y = seen + line_data->height;
        }

992
      seen += line_data->height;
993
      line = _gtk_text_line_next_excluding_last (line);
994 995
    }

996 997
  /* If we found and validated any invalid lines, update size and
   * emit the changed signal
998 999 1000
   */
  if (first_line)
    {
1001 1002 1003 1004
      gint line_top;

      update_layout_size (layout);

1005
      line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1006
                                                first_line, layout);
1007

Owen Taylor's avatar
Owen Taylor committed
1008 1009 1010 1011
      gtk_text_layout_emit_changed (layout,
				    line_top,
				    last_line_y - first_line_y - delta_height,
				    last_line_y - first_line_y);
1012 1013 1014 1015 1016 1017 1018 1019
    }
}

/**
 * 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)
1020
 *
1021 1022 1023 1024 1025
 * Validate regions of a #GtkTextLayout. The ::changed signal will
 * be emitted for each region validated.
 **/
void
gtk_text_layout_validate (GtkTextLayout *layout,
1026
                          gint           max_pixels)
1027 1028 1029 1030
{
  gint y, old_height, new_height;

  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1031

1032
  while (max_pixels > 0 &&
1033
         _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1034 1035
                                   layout,  max_pixels,
                                   &y, &old_height, &new_height))
1036 1037
    {
      max_pixels -= new_height;
1038 1039

      update_layout_size (layout);
Owen Taylor's avatar
Owen Taylor committed
1040
      gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1041 1042 1043 1044 1045
    }
}

static GtkTextLineData*
gtk_text_layout_real_wrap (GtkTextLayout   *layout,
1046 1047 1048
                           GtkTextLine     *line,
                           /* may be NULL */
                           GtkTextLineData *line_data)
1049 1050
{
  GtkTextLineDisplay *display;
1051

1052
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1053 1054
  g_return_val_if_fail (line != NULL, NULL);
  
1055 1056
  if (line_data == NULL)
    {
1057
      line_data = _gtk_text_line_data_new (layout, line);
1058
      _gtk_text_line_add_data (line, line_data);
1059 1060