]> Pileus Git - ~andy/gtk/blob - gtk/gtktextbuffer.h
9a0eb9d28e4886cc5550c7b0f16b47896878e990
[~andy/gtk] / gtk / gtktextbuffer.h
1 /* GTK - The GIMP Toolkit
2  * gtktextbuffer.h Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #ifndef __GTK_TEXT_BUFFER_H__
28 #define __GTK_TEXT_BUFFER_H__
29
30 #include <gtk/gtkwidget.h>
31 #include <gtk/gtkclipboard.h>
32 #include <gtk/gtktexttagtable.h>
33 #include <gtk/gtktextiter.h>
34 #include <gtk/gtktextmark.h>
35 #include <gtk/gtktextchild.h>
36
37 G_BEGIN_DECLS
38
39 /*
40  * This is the PUBLIC representation of a text buffer.
41  * GtkTextBTree is the PRIVATE internal representation of it.
42  */
43
44 /* these values are used as "info" for the targets contained in the
45  * lists returned by gtk_text_buffer_get_copy,paste_target_list()
46  *
47  * the enum counts down from G_MAXUINT to avoid clashes with application
48  * added drag destinations which usually start at 0.
49  */
50 typedef enum
51 {
52   GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS = - 1,
53   GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT       = - 2,
54   GTK_TEXT_BUFFER_TARGET_INFO_TEXT            = - 3
55 } GtkTextBufferTargetInfo;
56
57 typedef struct _GtkTextBTree GtkTextBTree;
58
59 typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
60
61 #define GTK_TYPE_TEXT_BUFFER            (gtk_text_buffer_get_type ())
62 #define GTK_TEXT_BUFFER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_TEXT_BUFFER, GtkTextBuffer))
63 #define GTK_TEXT_BUFFER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_BUFFER, GtkTextBufferClass))
64 #define GTK_IS_TEXT_BUFFER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_TEXT_BUFFER))
65 #define GTK_IS_TEXT_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_BUFFER))
66 #define GTK_TEXT_BUFFER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_BUFFER, GtkTextBufferClass))
67
68 typedef struct _GtkTextBufferClass GtkTextBufferClass;
69
70 struct _GtkTextBuffer
71 {
72   GObject parent_instance;
73
74   GtkTextTagTable *tag_table;
75   GtkTextBTree *btree;
76
77   GSList *clipboard_contents_buffers;
78   GSList *selection_clipboards;
79
80   GtkTextLogAttrCache *log_attr_cache;
81
82   guint user_action_count;
83   
84   /* Whether the buffer has been modified since last save */
85   guint modified : 1;
86
87   guint has_selection : 1;
88 };
89
90 struct _GtkTextBufferClass
91 {
92   GObjectClass parent_class;
93
94   void (* insert_text)     (GtkTextBuffer *buffer,
95                             GtkTextIter *pos,
96                             const gchar *text,
97                             gint length);
98
99   void (* insert_pixbuf)   (GtkTextBuffer *buffer,
100                             GtkTextIter   *pos,
101                             GdkPixbuf     *pixbuf);
102
103   void (* insert_child_anchor)   (GtkTextBuffer      *buffer,
104                                   GtkTextIter        *pos,
105                                   GtkTextChildAnchor *anchor);
106   
107   void (* delete_range)     (GtkTextBuffer *buffer,
108                              GtkTextIter   *start,
109                              GtkTextIter   *end);
110
111   /* Only for text/widgets/pixbuf changed, marks/tags don't cause this
112    * to be emitted
113    */
114   void (* changed)         (GtkTextBuffer *buffer);
115
116
117   /* New value for the modified flag */
118   void (* modified_changed)   (GtkTextBuffer *buffer);
119
120   /* Mark moved or created */
121   void (* mark_set)           (GtkTextBuffer *buffer,
122                                const GtkTextIter *location,
123                                GtkTextMark *mark);
124
125   void (* mark_deleted)       (GtkTextBuffer *buffer,
126                                GtkTextMark *mark);
127
128   void (* apply_tag)          (GtkTextBuffer *buffer,
129                                GtkTextTag *tag,
130                                const GtkTextIter *start_char,
131                                const GtkTextIter *end_char);
132
133   void (* remove_tag)         (GtkTextBuffer *buffer,
134                                GtkTextTag *tag,
135                                const GtkTextIter *start_char,
136                                const GtkTextIter *end_char);
137
138   /* Called at the start and end of an atomic user action */
139   void (* begin_user_action)  (GtkTextBuffer *buffer);
140   void (* end_user_action)    (GtkTextBuffer *buffer);
141
142   /* Padding for future expansion */
143   void (*_gtk_reserved1) (void);
144   void (*_gtk_reserved2) (void);
145   void (*_gtk_reserved3) (void);
146   void (*_gtk_reserved4) (void);
147   void (*_gtk_reserved5) (void);
148   void (*_gtk_reserved6) (void);
149 };
150
151 GType        gtk_text_buffer_get_type       (void) G_GNUC_CONST;
152
153
154
155 /* table is NULL to create a new one */
156 GtkTextBuffer *gtk_text_buffer_new            (GtkTextTagTable *table);
157 gint           gtk_text_buffer_get_line_count (GtkTextBuffer   *buffer);
158 gint           gtk_text_buffer_get_char_count (GtkTextBuffer   *buffer);
159
160
161 GtkTextTagTable* gtk_text_buffer_get_tag_table (GtkTextBuffer  *buffer);
162
163 /* Delete whole buffer, then insert */
164 void gtk_text_buffer_set_text          (GtkTextBuffer *buffer,
165                                         const gchar   *text,
166                                         gint           len);
167
168 /* Insert into the buffer */
169 void gtk_text_buffer_insert            (GtkTextBuffer *buffer,
170                                         GtkTextIter   *iter,
171                                         const gchar   *text,
172                                         gint           len);
173 void gtk_text_buffer_insert_at_cursor  (GtkTextBuffer *buffer,
174                                         const gchar   *text,
175                                         gint           len);
176
177 gboolean gtk_text_buffer_insert_interactive           (GtkTextBuffer *buffer,
178                                                        GtkTextIter   *iter,
179                                                        const gchar   *text,
180                                                        gint           len,
181                                                        gboolean       default_editable);
182 gboolean gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
183                                                        const gchar   *text,
184                                                        gint           len,
185                                                        gboolean       default_editable);
186
187 void     gtk_text_buffer_insert_range             (GtkTextBuffer     *buffer,
188                                                    GtkTextIter       *iter,
189                                                    const GtkTextIter *start,
190                                                    const GtkTextIter *end);
191 gboolean gtk_text_buffer_insert_range_interactive (GtkTextBuffer     *buffer,
192                                                    GtkTextIter       *iter,
193                                                    const GtkTextIter *start,
194                                                    const GtkTextIter *end,
195                                                    gboolean           default_editable);
196
197 void    gtk_text_buffer_insert_with_tags          (GtkTextBuffer     *buffer,
198                                                    GtkTextIter       *iter,
199                                                    const gchar       *text,
200                                                    gint               len,
201                                                    GtkTextTag        *first_tag,
202                                                    ...) G_GNUC_NULL_TERMINATED;
203
204 void    gtk_text_buffer_insert_with_tags_by_name  (GtkTextBuffer     *buffer,
205                                                    GtkTextIter       *iter,
206                                                    const gchar       *text,
207                                                    gint               len,
208                                                    const gchar       *first_tag_name,
209                                                    ...) G_GNUC_NULL_TERMINATED;
210
211 /* Delete from the buffer */
212 void     gtk_text_buffer_delete             (GtkTextBuffer *buffer,
213                                              GtkTextIter   *start,
214                                              GtkTextIter   *end);
215 gboolean gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
216                                              GtkTextIter   *start_iter,
217                                              GtkTextIter   *end_iter,
218                                              gboolean       default_editable);
219 gboolean gtk_text_buffer_backspace          (GtkTextBuffer *buffer,
220                                              GtkTextIter   *iter,
221                                              gboolean       interactive,
222                                              gboolean       default_editable);
223
224 /* Obtain strings from the buffer */
225 gchar          *gtk_text_buffer_get_text            (GtkTextBuffer     *buffer,
226                                                      const GtkTextIter *start,
227                                                      const GtkTextIter *end,
228                                                      gboolean           include_hidden_chars);
229
230 gchar          *gtk_text_buffer_get_slice           (GtkTextBuffer     *buffer,
231                                                      const GtkTextIter *start,
232                                                      const GtkTextIter *end,
233                                                      gboolean           include_hidden_chars);
234
235 /* Insert a pixbuf */
236 void gtk_text_buffer_insert_pixbuf         (GtkTextBuffer *buffer,
237                                             GtkTextIter   *iter,
238                                             GdkPixbuf     *pixbuf);
239
240 /* Insert a child anchor */
241 void               gtk_text_buffer_insert_child_anchor (GtkTextBuffer      *buffer,
242                                                         GtkTextIter        *iter,
243                                                         GtkTextChildAnchor *anchor);
244
245 /* Convenience, create and insert a child anchor */
246 GtkTextChildAnchor *gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
247                                                          GtkTextIter   *iter);
248
249 /* Mark manipulation */
250 void           gtk_text_buffer_add_mark    (GtkTextBuffer     *buffer,
251                                             GtkTextMark       *mark,
252                                             const GtkTextIter *where);
253 GtkTextMark   *gtk_text_buffer_create_mark (GtkTextBuffer     *buffer,
254                                             const gchar       *mark_name,
255                                             const GtkTextIter *where,
256                                             gboolean           left_gravity);
257 void           gtk_text_buffer_move_mark   (GtkTextBuffer     *buffer,
258                                             GtkTextMark       *mark,
259                                             const GtkTextIter *where);
260 void           gtk_text_buffer_delete_mark (GtkTextBuffer     *buffer,
261                                             GtkTextMark       *mark);
262 GtkTextMark*   gtk_text_buffer_get_mark    (GtkTextBuffer     *buffer,
263                                             const gchar       *name);
264
265 void gtk_text_buffer_move_mark_by_name   (GtkTextBuffer     *buffer,
266                                           const gchar       *name,
267                                           const GtkTextIter *where);
268 void gtk_text_buffer_delete_mark_by_name (GtkTextBuffer     *buffer,
269                                           const gchar       *name);
270
271 GtkTextMark* gtk_text_buffer_get_insert          (GtkTextBuffer *buffer);
272 GtkTextMark* gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer);
273
274 /* efficiently move insert and selection_bound at the same time */
275 void gtk_text_buffer_place_cursor (GtkTextBuffer     *buffer,
276                                    const GtkTextIter *where);
277 void gtk_text_buffer_select_range (GtkTextBuffer     *buffer,
278                                    const GtkTextIter *ins,
279                                    const GtkTextIter *bound);
280
281
282
283 /* Tag manipulation */
284 void gtk_text_buffer_apply_tag             (GtkTextBuffer     *buffer,
285                                             GtkTextTag        *tag,
286                                             const GtkTextIter *start,
287                                             const GtkTextIter *end);
288 void gtk_text_buffer_remove_tag            (GtkTextBuffer     *buffer,
289                                             GtkTextTag        *tag,
290                                             const GtkTextIter *start,
291                                             const GtkTextIter *end);
292 void gtk_text_buffer_apply_tag_by_name     (GtkTextBuffer     *buffer,
293                                             const gchar       *name,
294                                             const GtkTextIter *start,
295                                             const GtkTextIter *end);
296 void gtk_text_buffer_remove_tag_by_name    (GtkTextBuffer     *buffer,
297                                             const gchar       *name,
298                                             const GtkTextIter *start,
299                                             const GtkTextIter *end);
300 void gtk_text_buffer_remove_all_tags       (GtkTextBuffer     *buffer,
301                                             const GtkTextIter *start,
302                                             const GtkTextIter *end);
303
304
305 /* You can either ignore the return value, or use it to
306  * set the attributes of the tag. tag_name can be NULL
307  */
308 GtkTextTag    *gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
309                                            const gchar   *tag_name,
310                                            const gchar   *first_property_name,
311                                            ...);
312
313 /* Obtain iterators pointed at various places, then you can move the
314  * iterator around using the GtkTextIter operators
315  */
316 void gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
317                                               GtkTextIter   *iter,
318                                               gint           line_number,
319                                               gint           char_offset);
320 void gtk_text_buffer_get_iter_at_line_index  (GtkTextBuffer *buffer,
321                                               GtkTextIter   *iter,
322                                               gint           line_number,
323                                               gint           byte_index);
324 void gtk_text_buffer_get_iter_at_offset      (GtkTextBuffer *buffer,
325                                               GtkTextIter   *iter,
326                                               gint           char_offset);
327 void gtk_text_buffer_get_iter_at_line        (GtkTextBuffer *buffer,
328                                               GtkTextIter   *iter,
329                                               gint           line_number);
330 void gtk_text_buffer_get_start_iter          (GtkTextBuffer *buffer,
331                                               GtkTextIter   *iter);
332 void gtk_text_buffer_get_end_iter            (GtkTextBuffer *buffer,
333                                               GtkTextIter   *iter);
334 void gtk_text_buffer_get_bounds              (GtkTextBuffer *buffer,
335                                               GtkTextIter   *start,
336                                               GtkTextIter   *end);
337 void gtk_text_buffer_get_iter_at_mark        (GtkTextBuffer *buffer,
338                                               GtkTextIter   *iter,
339                                               GtkTextMark   *mark);
340
341 void gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer      *buffer,
342                                                GtkTextIter        *iter,
343                                                GtkTextChildAnchor *anchor);
344
345 /* There's no get_first_iter because you just get the iter for
346    line or char 0 */
347
348 /* Used to keep track of whether the buffer needs saving; anytime the
349    buffer contents change, the modified flag is turned on. Whenever
350    you save, turn it off. Tags and marks do not affect the modified
351    flag, but if you would like them to you can connect a handler to
352    the tag/mark signals and call set_modified in your handler */
353
354 gboolean        gtk_text_buffer_get_modified            (GtkTextBuffer *buffer);
355 void            gtk_text_buffer_set_modified            (GtkTextBuffer *buffer,
356                                                          gboolean       setting);
357
358 gboolean        gtk_text_buffer_get_has_selection       (GtkTextBuffer *buffer);
359
360 void gtk_text_buffer_add_selection_clipboard    (GtkTextBuffer     *buffer,
361                                                  GtkClipboard      *clipboard);
362 void gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer     *buffer,
363                                                  GtkClipboard      *clipboard);
364
365 void            gtk_text_buffer_cut_clipboard           (GtkTextBuffer *buffer,
366                                                          GtkClipboard  *clipboard,
367                                                          gboolean       default_editable);
368 void            gtk_text_buffer_copy_clipboard          (GtkTextBuffer *buffer,
369                                                          GtkClipboard  *clipboard);
370 void            gtk_text_buffer_paste_clipboard         (GtkTextBuffer *buffer,
371                                                          GtkClipboard  *clipboard,
372                                                          GtkTextIter   *override_location,
373                                                          gboolean       default_editable);
374
375 gboolean        gtk_text_buffer_get_selection_bounds    (GtkTextBuffer *buffer,
376                                                          GtkTextIter   *start,
377                                                          GtkTextIter   *end);
378 gboolean        gtk_text_buffer_delete_selection        (GtkTextBuffer *buffer,
379                                                          gboolean       interactive,
380                                                          gboolean       default_editable);                                                    
381
382 /* Called to specify atomic user actions, used to implement undo */
383 void            gtk_text_buffer_begin_user_action       (GtkTextBuffer *buffer);
384 void            gtk_text_buffer_end_user_action         (GtkTextBuffer *buffer);
385
386 GtkTargetList * gtk_text_buffer_get_copy_target_list    (GtkTextBuffer *buffer);
387 GtkTargetList * gtk_text_buffer_get_paste_target_list   (GtkTextBuffer *buffer);
388
389 /* INTERNAL private stuff */
390 void            _gtk_text_buffer_spew                  (GtkTextBuffer      *buffer);
391
392 GtkTextBTree*   _gtk_text_buffer_get_btree             (GtkTextBuffer      *buffer);
393
394 const PangoLogAttr* _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer     *buffer,
395                                                          const GtkTextIter *anywhere_in_line,
396                                                          gint              *char_len);
397
398 void _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
399                                               GtkTextTag    *tag);
400
401 G_END_DECLS
402
403 #endif