]> Pileus Git - ~andy/gtk/blob - gtk/gtktextlayout.h
Patch from Dov Grobgeld to implement auto-bidi-direction for GtkTextView
[~andy/gtk] / gtk / gtktextlayout.h
1 /* GTK - The GIMP Toolkit
2  * gtktextlayout.h
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 #ifndef GTK_TEXT_LAYOUT_H
80 #define GTK_TEXT_LAYOUT_H
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif /* __cplusplus */
85
86 /* This is a "semi-private" header; it is intended for
87  * use by the text widget, and the text canvas item,
88  * but that's all. We may have to install it so the
89  * canvas item can use it, but users are not supposed
90  * to use it.
91  */
92 #ifndef GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
93 #error "You are not supposed to be including this file; the equivalent public API is in gtktextview.h"
94 #endif
95
96 #include <gtk/gtktextbuffer.h>
97 #include <gtk/gtktextiter.h>
98
99 /* forward declarations that have to be here to avoid including
100  * gtktextbtree.h
101  */
102 typedef struct _GtkTextLine     GtkTextLine;
103 typedef struct _GtkTextLineData GtkTextLineData;
104
105 #define GTK_TYPE_TEXT_LAYOUT             (gtk_text_layout_get_type ())
106 #define GTK_TEXT_LAYOUT(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_TEXT_LAYOUT, GtkTextLayout))
107 #define GTK_TEXT_LAYOUT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutClass))
108 #define GTK_IS_TEXT_LAYOUT(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_TEXT_LAYOUT))
109 #define GTK_IS_TEXT_LAYOUT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_LAYOUT))
110 #define GTK_TEXT_LAYOUT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutClass))
111
112 typedef struct _GtkTextLayout         GtkTextLayout;
113 typedef struct _GtkTextLayoutClass    GtkTextLayoutClass;
114 typedef struct _GtkTextLineDisplay    GtkTextLineDisplay;
115 typedef struct _GtkTextCursorDisplay  GtkTextCursorDisplay;
116 typedef struct _GtkTextAttrAppearance GtkTextAttrAppearance;
117
118 struct _GtkTextLayout
119 {
120   GObject parent_instance;
121
122   /* width of the display area on-screen,
123    * i.e. pixels we should wrap to fit inside. */
124   gint screen_width;
125
126   /* width/height of the total logical area being layed out */
127   gint width;
128   gint height;
129
130   /* Pixel offsets from the left and from the top to be used when we
131    * draw; these allow us to create left/top margins. We don't need
132    * anything special for bottom/right margins, because those don't
133    * affect drawing.
134    */
135   /* gint left_edge; */
136   /* gint top_edge; */
137
138   GtkTextBuffer *buffer;
139
140   /* Default style used if no tags override it */
141   GtkTextAttributes *default_style;
142
143   /* Pango contexts used for creating layouts */
144   PangoContext *ltr_context;
145   PangoContext *rtl_context;
146
147   /* A cache of one style; this is used to ensure
148    * we don't constantly regenerate the style
149    * over long runs with the same style. */
150   GtkTextAttributes *one_style_cache;
151
152   /* A cache of one line display. Getting the same line
153    * many times in a row is the most common case.
154    */
155   GtkTextLineDisplay *one_display_cache;
156
157   /* Whether we are allowed to wrap right now */
158   gint wrap_loop_count;
159   
160   /* Whether to show the insertion cursor */
161   guint cursor_visible : 1;
162
163   /* For what GtkTextDirection to draw cursor GTK_TEXT_DIR_NONE -
164    * means draw both cursors.
165    */
166   guint cursor_direction : 2;
167
168   /* The keyboard direction is used to default the alignment when
169      there are no strong characters.
170   */
171   guint keyboard_direction : 2;
172
173   /* The preedit string and attributes, if any */
174
175   gchar *preedit_string;
176   PangoAttrList *preedit_attrs;
177   gint preedit_len;
178   gint preedit_cursor;
179 };
180
181 struct _GtkTextLayoutClass
182 {
183   GObjectClass parent_class;
184
185   /* Some portion of the layout was invalidated
186    */
187   void  (*invalidated)  (GtkTextLayout *layout);
188
189   /* A range of the layout changed appearance and possibly height
190    */
191   void  (*changed)              (GtkTextLayout     *layout,
192                                  gint               y,
193                                  gint               old_height,
194                                  gint               new_height);
195   GtkTextLineData* (*wrap)      (GtkTextLayout     *layout,
196                                  GtkTextLine       *line,
197                                  GtkTextLineData   *line_data); /* may be NULL */
198   void  (*get_log_attrs)        (GtkTextLayout     *layout,
199                                  GtkTextLine       *line,
200                                  PangoLogAttr     **attrs,
201                                  gint              *n_attrs);
202   void  (*invalidate)           (GtkTextLayout     *layout,
203                                  const GtkTextIter *start,
204                                  const GtkTextIter *end);
205   void  (*free_line_data)       (GtkTextLayout     *layout,
206                                  GtkTextLine       *line,
207                                  GtkTextLineData   *line_data);
208
209   void (*allocate_child)        (GtkTextLayout     *layout,
210                                  GtkWidget         *child,
211                                  gint               x,
212                                  gint               y);
213
214   /* Padding for future expansion */
215   void (*_gtk_reserved1) (void);
216   void (*_gtk_reserved2) (void);
217   void (*_gtk_reserved3) (void);
218   void (*_gtk_reserved4) (void);
219 };
220
221 struct _GtkTextAttrAppearance
222 {
223   PangoAttribute attr;
224   GtkTextAppearance appearance;
225 };
226 struct _GtkTextCursorDisplay
227 {
228   gint x;
229   gint y;
230   gint height;
231   guint is_strong : 1;
232   guint is_weak : 1;
233 };
234 struct _GtkTextLineDisplay
235 {
236   PangoLayout *layout;
237   GSList *cursors;
238   GSList *shaped_objects;
239   
240   GtkTextDirection direction;
241
242   gint width;                   /* Width of layout */
243   gint total_width;             /* width - margins, if no width set on layout, if width set on layout, -1 */
244   gint height;
245   /* Amount layout is shifted from left edge - this is the left margin
246    * plus any other factors, such as alignment or indentation.
247    */
248   gint x_offset;
249   gint left_margin;
250   gint right_margin;
251   gint top_margin;
252   gint bottom_margin;
253   gint insert_index;            /* Byte index of insert cursor within para or -1 */
254
255   gboolean size_only;
256   GtkTextLine *line;
257 };
258
259 extern PangoAttrType gtk_text_attr_appearance_type;
260
261 GType         gtk_text_layout_get_type    (void) G_GNUC_CONST;
262
263 GtkTextLayout*     gtk_text_layout_new                   (void);
264 void               gtk_text_layout_set_buffer            (GtkTextLayout     *layout,
265                                                           GtkTextBuffer     *buffer);
266 GtkTextBuffer     *gtk_text_layout_get_buffer            (GtkTextLayout     *layout);
267 void               gtk_text_layout_set_default_style     (GtkTextLayout     *layout,
268                                                           GtkTextAttributes *values);
269 void               gtk_text_layout_set_contexts          (GtkTextLayout     *layout,
270                                                           PangoContext      *ltr_context,
271                                                           PangoContext      *rtl_context);
272 void               gtk_text_layout_set_cursor_direction  (GtkTextLayout     *layout,
273                                                           GtkTextDirection   direction);
274 void               gtk_text_layout_set_keyboard_direction (GtkTextLayout     *layout,
275                                                            GtkTextDirection keyboard_dir);
276 void               gtk_text_layout_default_style_changed (GtkTextLayout     *layout);
277
278 void gtk_text_layout_set_screen_width       (GtkTextLayout     *layout,
279                                              gint               width);
280 void gtk_text_layout_set_preedit_string     (GtkTextLayout     *layout,
281                                              const gchar       *preedit_string,
282                                              PangoAttrList     *preedit_attrs,
283                                              gint               cursor_pos);
284
285 void     gtk_text_layout_set_cursor_visible (GtkTextLayout     *layout,
286                                              gboolean           cursor_visible);
287 gboolean gtk_text_layout_get_cursor_visible (GtkTextLayout     *layout);
288
289 /* Getting the size or the lines potentially results in a call to
290  * recompute, which is pretty massively expensive. Thus it should
291  * basically only be done in an idle handler.
292  *
293  * Long-term, we would really like to be able to do these without
294  * a full recompute so they may get cheaper over time.
295  */
296 void    gtk_text_layout_get_size  (GtkTextLayout  *layout,
297                                    gint           *width,
298                                    gint           *height);
299 GSList* gtk_text_layout_get_lines (GtkTextLayout  *layout,
300                                    /* [top_y, bottom_y) */
301                                    gint            top_y,
302                                    gint            bottom_y,
303                                    gint           *first_line_y);
304
305 void gtk_text_layout_wrap_loop_start (GtkTextLayout *layout);
306 void gtk_text_layout_wrap_loop_end   (GtkTextLayout *layout);
307
308 GtkTextLineDisplay* gtk_text_layout_get_line_display  (GtkTextLayout      *layout,
309                                                        GtkTextLine        *line,
310                                                        gboolean            size_only);
311 void                gtk_text_layout_free_line_display (GtkTextLayout      *layout,
312                                                        GtkTextLineDisplay *display);
313
314 void gtk_text_layout_get_line_at_y     (GtkTextLayout     *layout,
315                                         GtkTextIter       *target_iter,
316                                         gint               y,
317                                         gint              *line_top);
318 void gtk_text_layout_get_iter_at_pixel (GtkTextLayout     *layout,
319                                         GtkTextIter       *iter,
320                                         gint               x,
321                                         gint               y);
322 void gtk_text_layout_invalidate        (GtkTextLayout     *layout,
323                                         const GtkTextIter *start,
324                                         const GtkTextIter *end);
325 void gtk_text_layout_free_line_data    (GtkTextLayout     *layout,
326                                         GtkTextLine       *line,
327                                         GtkTextLineData   *line_data);
328
329 gboolean gtk_text_layout_is_valid        (GtkTextLayout *layout);
330 void     gtk_text_layout_validate_yrange (GtkTextLayout *layout,
331                                           GtkTextIter   *anchor_line,
332                                           gint           y0_,
333                                           gint           y1_);
334 void     gtk_text_layout_validate        (GtkTextLayout *layout,
335                                           gint           max_pixels);
336
337 /* This function should return the passed-in line data,
338  * OR remove the existing line data from the line, and
339  * return a NEW line data after adding it to the line.
340  * That is, invariant after calling the callback is that
341  * there should be exactly one line data for this view
342  * stored on the btree line.
343  */
344 GtkTextLineData* gtk_text_layout_wrap  (GtkTextLayout   *layout,
345                                         GtkTextLine     *line,
346                                         GtkTextLineData *line_data); /* may be NULL */
347 void     gtk_text_layout_changed              (GtkTextLayout     *layout,
348                                                gint               y,
349                                                gint               old_height,
350                                                gint               new_height);
351 void     gtk_text_layout_get_iter_location    (GtkTextLayout     *layout,
352                                                const GtkTextIter *iter,
353                                                GdkRectangle      *rect);
354 void     gtk_text_layout_get_line_yrange      (GtkTextLayout     *layout,
355                                                const GtkTextIter *iter,
356                                                gint              *y,
357                                                gint              *height);
358 void     _gtk_text_layout_get_line_xrange     (GtkTextLayout     *layout,
359                                                const GtkTextIter *iter,
360                                                gint              *x,
361                                                gint              *width);
362 void     gtk_text_layout_get_cursor_locations (GtkTextLayout     *layout,
363                                                GtkTextIter       *iter,
364                                                GdkRectangle      *strong_pos,
365                                                GdkRectangle      *weak_pos);
366 gboolean gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout     *layout,
367                                                GtkTextIter       *iter,
368                                                gint               top,
369                                                gint               bottom);
370
371 gboolean gtk_text_layout_move_iter_to_line_end      (GtkTextLayout *layout,
372                                                      GtkTextIter   *iter,
373                                                      gint           direction);
374 gboolean gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
375                                                      GtkTextIter   *iter);
376 gboolean gtk_text_layout_move_iter_to_next_line     (GtkTextLayout *layout,
377                                                      GtkTextIter   *iter);
378 void     gtk_text_layout_move_iter_to_x             (GtkTextLayout *layout,
379                                                      GtkTextIter   *iter,
380                                                      gint           x);
381 gboolean gtk_text_layout_move_iter_visually         (GtkTextLayout *layout,
382                                                      GtkTextIter   *iter,
383                                                      gint           count);
384
385 gboolean gtk_text_layout_iter_starts_line           (GtkTextLayout       *layout,
386                                                      const GtkTextIter   *iter);
387
388 void     gtk_text_layout_get_iter_at_line           (GtkTextLayout *layout,
389                                                      GtkTextIter    *iter,
390                                                      GtkTextLine    *line,
391                                                      gint            byte_offset);
392
393 /* Don't use these. Use gtk_text_view_add_child_at_anchor().
394  * These functions are defined in gtktextchild.c, but here
395  * since they are semi-public and require GtkTextLayout to
396  * be declared.
397  */
398 void gtk_text_child_anchor_register_child   (GtkTextChildAnchor *anchor,
399                                              GtkWidget          *child,
400                                              GtkTextLayout      *layout);
401 void gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
402                                              GtkWidget          *child);
403
404 void gtk_text_child_anchor_queue_resize     (GtkTextChildAnchor *anchor,
405                                              GtkTextLayout      *layout);
406
407 void gtk_text_anchored_child_set_layout     (GtkWidget          *child,
408                                              GtkTextLayout      *layout);
409
410 void gtk_text_layout_spew (GtkTextLayout *layout);
411
412 #ifdef __cplusplus
413 }
414 #endif /* __cplusplus */
415
416 #endif  /* GTK_TEXT_LAYOUT_H */