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, see <http://www.gnu.org/licenses/>.
27  *
28  * Original Tk license:
29  *
30  * This software is copyrighted by the Regents of the University of
31  * California, Sun Microsystems, Inc., and other parties.  The
32  * following terms apply to all files associated with the software
33  * unless explicitly disclaimed in individual files.
34  *
35  * The authors hereby grant permission to use, copy, modify,
36  * distribute, and license this software and its documentation for any
37  * purpose, provided that existing copyright notices are retained in
38  * all copies and that this notice is included verbatim in any
39  * distributions. No written agreement, license, or royalty fee is
40  * required for any of the authorized uses.  Modifications to this
41  * software may be copyrighted by their authors and need not follow
42  * the licensing terms described here, provided that the new terms are
43  * clearly indicated on the first page of each file where they apply.
44  *
45  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
46  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
47  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
48  * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
52  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
54  * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
55  * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
56  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
57  *
58  * GOVERNMENT USE: If you are acquiring this software on behalf of the
59  * U.S. government, the Government shall have only "Restricted Rights"
60  * in the software and related documentation as defined in the Federal
61  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
62  * are acquiring the software on behalf of the Department of Defense,
63  * the software shall be classified as "Commercial Computer Software"
64  * and the Government shall have only "Restricted Rights" as defined
65  * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
66  * foregoing, the authors grant the U.S. Government and others acting
67  * in its behalf permission to use and distribute the software in
68  * accordance with the terms specified in this license.
69  *
70  */
71 /*
72  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
73  * file for a list of people on the GTK+ Team.  See the ChangeLog
74  * files for a list of changes.  These files are distributed with
75  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
76  */
77 
78 #ifndef __GTK_TEXT_LAYOUT_PRIVATE_H__
79 #define __GTK_TEXT_LAYOUT_PRIVATE_H__
80 
81 #include <gtk/gtk.h>
82 #include <gtk/gtktextattributes.h>
83 
84 G_BEGIN_DECLS
85 
86 /* forward declarations that have to be here to avoid including
87  * gtktextbtree.h
88  */
89 typedef struct _GtkTextLine     GtkTextLine;
90 typedef struct _GtkTextLineData GtkTextLineData;
91 
92 #define GTK_TYPE_TEXT_LAYOUT             (gtk_text_layout_get_type ())
93 #define GTK_TEXT_LAYOUT(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_TEXT_LAYOUT, GtkTextLayout))
94 #define GTK_TEXT_LAYOUT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutClass))
95 #define GTK_IS_TEXT_LAYOUT(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_TEXT_LAYOUT))
96 #define GTK_IS_TEXT_LAYOUT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_LAYOUT))
97 #define GTK_TEXT_LAYOUT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutClass))
98 
99 typedef struct _GtkTextLayout         GtkTextLayout;
100 typedef struct _GtkTextLayoutClass    GtkTextLayoutClass;
101 typedef struct _GtkTextLineDisplay    GtkTextLineDisplay;
102 typedef struct _GtkTextAttrAppearance GtkTextAttrAppearance;
103 
104 struct _GtkTextLayout
105 {
106   GObject parent_instance;
107 
108   /* width of the display area on-screen,
109    * i.e. pixels we should wrap to fit inside. */
110   int screen_width;
111 
112   /* width/height of the total logical area being laid out */
113   int width;
114   int height;
115 
116   int left_padding;
117   int right_padding;
118 
119   GtkTextBuffer *buffer;
120 
121   /* Default style used if no tags override it */
122   GtkTextAttributes *default_style;
123 
124   /* Pango contexts used for creating layouts */
125   PangoContext *ltr_context;
126   PangoContext *rtl_context;
127 
128   /* A cache of one style; this is used to ensure
129    * we don't constantly regenerate the style
130    * over long runs with the same style. */
131   GtkTextAttributes *one_style_cache;
132 
133   /* Whether we are allowed to wrap right now */
134   int wrap_loop_count;
135 
136   /* Whether to show the insertion cursor */
137   guint cursor_visible : 1;
138 
139   /* For what GtkTextDirection to draw cursor GTK_TEXT_DIR_NONE -
140    * means draw both cursors.
141    */
142   guint cursor_direction : 2;
143 
144   /* The keyboard direction is used to default the alignment when
145      there are no strong characters.
146   */
147   guint keyboard_direction : 2;
148 
149   guint overwrite_mode : 1;
150 
151   /* The preedit string and attributes, if any */
152 
153   char *preedit_string;
154   PangoAttrList *preedit_attrs;
155   int preedit_len;
156   int preedit_cursor;
157 };
158 
159 struct _GtkTextLayoutClass
160 {
161   GObjectClass parent_class;
162 };
163 
164 struct _GtkTextAttrAppearance
165 {
166   PangoAttribute attr;
167   GtkTextAppearance appearance;
168 };
169 
170 typedef struct _CursorPosition CursorPosition;
171 struct _CursorPosition {
172   int pos;
173   guint is_insert          : 1;
174   guint is_selection_bound : 1;
175 };
176 
177 struct _GtkTextLineDisplay
178 {
179   PangoLayout *layout;
180 
181   GskRenderNode *node;
182 
183   GArray *cursors;      /* indexes of cursors in the PangoLayout, and mark names */
184 
185   /* GSequenceIter backpointer for use within cache */
186   GSequenceIter *cache_iter;
187 
188   /* GQueue link for use in MRU to help cull cache */
189   GList          mru_link;
190 
191   GtkTextDirection direction;
192 
193   int width;                   /* Width of layout */
194   int total_width;             /* width - margins, if no width set on layout, if width set on layout, -1 */
195   int height;
196   /* Amount layout is shifted from left edge - this is the left margin
197    * plus any other factors, such as alignment or indentation.
198    */
199   int x_offset;
200   int left_margin;
201   int right_margin;
202   int top_margin;
203   int bottom_margin;
204   int insert_index;		/* Byte index of insert cursor within para or -1 */
205 
206   GtkTextLine *line;
207 
208   GdkRectangle block_cursor;
209 
210   guint cursors_invalid : 1;
211   guint has_block_cursor : 1;
212   guint cursor_at_line_end : 1;
213   guint size_only : 1;
214   guint pg_bg_rgba_set : 1;
215   guint has_children : 1;
216 
217   GdkRGBA pg_bg_rgba;
218 };
219 
220 #ifdef GTK_COMPILATION
221 extern G_GNUC_INTERNAL PangoAttrType gtk_text_attr_appearance_type;
222 #endif
223 
224 GType         gtk_text_layout_get_type    (void) G_GNUC_CONST;
225 
226 GtkTextLayout*     gtk_text_layout_new                   (void);
227 void               gtk_text_layout_set_buffer            (GtkTextLayout     *layout,
228 							  GtkTextBuffer     *buffer);
229 GtkTextBuffer     *gtk_text_layout_get_buffer            (GtkTextLayout     *layout);
230 void               gtk_text_layout_set_default_style     (GtkTextLayout     *layout,
231 							  GtkTextAttributes *values);
232 void               gtk_text_layout_set_contexts          (GtkTextLayout     *layout,
233 							  PangoContext      *ltr_context,
234 							  PangoContext      *rtl_context);
235 void               gtk_text_layout_set_cursor_direction  (GtkTextLayout     *layout,
236                                                           GtkTextDirection   direction);
237 void		   gtk_text_layout_set_overwrite_mode	 (GtkTextLayout     *layout,
238 							  gboolean           overwrite);
239 void               gtk_text_layout_set_keyboard_direction (GtkTextLayout     *layout,
240 							   GtkTextDirection keyboard_dir);
241 void               gtk_text_layout_default_style_changed (GtkTextLayout     *layout);
242 
243 void gtk_text_layout_set_screen_width       (GtkTextLayout     *layout,
244                                              int                width);
245 void gtk_text_layout_set_preedit_string     (GtkTextLayout     *layout,
246  					     const char        *preedit_string,
247  					     PangoAttrList     *preedit_attrs,
248  					     int                cursor_pos);
249 
250 void     gtk_text_layout_set_cursor_visible (GtkTextLayout     *layout,
251                                              gboolean           cursor_visible);
252 gboolean gtk_text_layout_get_cursor_visible (GtkTextLayout     *layout);
253 
254 void    gtk_text_layout_get_size  (GtkTextLayout  *layout,
255                                    int            *width,
256                                    int            *height);
257 
258 void gtk_text_layout_wrap_loop_start (GtkTextLayout *layout);
259 void gtk_text_layout_wrap_loop_end   (GtkTextLayout *layout);
260 
261 GtkTextLineDisplay* gtk_text_layout_get_line_display  (GtkTextLayout      *layout,
262                                                        GtkTextLine        *line,
263                                                        gboolean            size_only);
264 
265 GtkTextLineDisplay *gtk_text_line_display_ref         (GtkTextLineDisplay       *display);
266 void                gtk_text_line_display_unref       (GtkTextLineDisplay       *display);
267 int                 gtk_text_line_display_compare     (const GtkTextLineDisplay *display1,
268                                                        const GtkTextLineDisplay *display2,
269                                                        GtkTextLayout            *layout);
270 
271 void gtk_text_layout_get_line_at_y     (GtkTextLayout     *layout,
272                                         GtkTextIter       *target_iter,
273                                         int                y,
274                                         int               *line_top);
275 gboolean gtk_text_layout_get_iter_at_pixel (GtkTextLayout     *layout,
276                                             GtkTextIter       *iter,
277                                             int                x,
278                                             int                y);
279 gboolean gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
280                                                GtkTextIter       *iter,
281                                                int               *trailing,
282                                                int                x,
283                                                int                y);
284 void gtk_text_layout_invalidate        (GtkTextLayout     *layout,
285                                         const GtkTextIter *start,
286                                         const GtkTextIter *end);
287 void gtk_text_layout_invalidate_cursors(GtkTextLayout     *layout,
288                                         const GtkTextIter *start,
289                                         const GtkTextIter *end);
290 void gtk_text_layout_invalidate_selection (GtkTextLayout  *layout);
291 void gtk_text_layout_free_line_data    (GtkTextLayout     *layout,
292                                         GtkTextLine       *line,
293                                         GtkTextLineData   *line_data);
294 
295 gboolean gtk_text_layout_is_valid        (GtkTextLayout *layout);
296 void     gtk_text_layout_validate_yrange (GtkTextLayout *layout,
297                                           GtkTextIter   *anchor_line,
298                                           int            y0_,
299                                           int            y1_);
300 void     gtk_text_layout_validate        (GtkTextLayout *layout,
301                                           int            max_pixels);
302 
303 GtkTextLineData* gtk_text_layout_wrap  (GtkTextLayout   *layout,
304                                         GtkTextLine     *line,
305                                         GtkTextLineData *line_data);
306 void     gtk_text_layout_changed              (GtkTextLayout     *layout,
307                                                int                y,
308                                                int                old_height,
309                                                int                new_height);
310 void     gtk_text_layout_cursors_changed      (GtkTextLayout     *layout,
311                                                int                y,
312                                                int                old_height,
313                                                int                new_height);
314 void     gtk_text_layout_get_iter_location    (GtkTextLayout     *layout,
315                                                const GtkTextIter *iter,
316                                                GdkRectangle      *rect);
317 void     gtk_text_layout_get_line_yrange      (GtkTextLayout     *layout,
318                                                const GtkTextIter *iter,
319                                                int               *y,
320                                                int               *height);
321 void     gtk_text_layout_get_cursor_locations (GtkTextLayout     *layout,
322                                                GtkTextIter       *iter,
323                                                GdkRectangle      *strong_pos,
324                                                GdkRectangle      *weak_pos);
325 GtkTextLineDisplay *gtk_text_layout_create_display (GtkTextLayout *layout,
326                                                     GtkTextLine   *line,
327                                                     gboolean       size_only);
328 void     gtk_text_layout_update_display_cursors (GtkTextLayout      *layout,
329                                                  GtkTextLine        *line,
330                                                  GtkTextLineDisplay *display);
331 void     gtk_text_layout_update_children        (GtkTextLayout      *layout,
332                                                  GtkTextLineDisplay *display);
333 gboolean _gtk_text_layout_get_block_cursor    (GtkTextLayout     *layout,
334 					       GdkRectangle      *pos);
335 gboolean gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout     *layout,
336                                                GtkTextIter       *iter,
337                                                int                top,
338                                                int                bottom);
339 
340 gboolean gtk_text_layout_move_iter_to_line_end      (GtkTextLayout *layout,
341                                                      GtkTextIter   *iter,
342                                                      int            direction);
343 gboolean gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
344                                                      GtkTextIter   *iter);
345 gboolean gtk_text_layout_move_iter_to_next_line     (GtkTextLayout *layout,
346                                                      GtkTextIter   *iter);
347 void     gtk_text_layout_move_iter_to_x             (GtkTextLayout *layout,
348                                                      GtkTextIter   *iter,
349                                                      int            x);
350 gboolean gtk_text_layout_move_iter_visually         (GtkTextLayout *layout,
351                                                      GtkTextIter   *iter,
352                                                      int            count);
353 
354 gboolean gtk_text_layout_iter_starts_line           (GtkTextLayout       *layout,
355                                                      const GtkTextIter   *iter);
356 
357 void     gtk_text_layout_get_iter_at_line           (GtkTextLayout *layout,
358                                                      GtkTextIter    *iter,
359                                                      GtkTextLine    *line,
360                                                      int             byte_offset);
361 
362 void gtk_text_child_anchor_register_child   (GtkTextChildAnchor *anchor,
363                                              GtkWidget          *child,
364                                              GtkTextLayout      *layout);
365 void gtk_text_child_anchor_unregister_child (GtkTextChildAnchor *anchor,
366                                              GtkWidget          *child);
367 
368 void gtk_text_child_anchor_queue_resize     (GtkTextChildAnchor *anchor,
369                                              GtkTextLayout      *layout);
370 
371 void gtk_text_anchored_child_set_layout     (GtkWidget          *child,
372                                              GtkTextLayout      *layout);
373 
374 void gtk_text_layout_spew (GtkTextLayout *layout);
375 
376 void gtk_text_layout_snapshot (GtkTextLayout        *layout,
377                                GtkWidget            *widget,
378                                GtkSnapshot          *snapshot,
379                                const GdkRectangle   *clip,
380                                float                 cursor_alpha);
381 
382 void gtk_text_layout_set_mru_size (GtkTextLayout *layout,
383                                    guint          mru_size);
384 
385 G_END_DECLS
386 
387 #endif  /* __GTK_TEXT_LAYOUT_PRIVATE_H__ */
388