1 //
2 // "$Id: Fl_Text_Buffer.H 6010 2008-01-04 20:31:52Z matt $"
3 //
4 // Header file for Fl_Text_Buffer class.
5 //
6 // Copyright 2001-2005 by Bill Spitzak and others.
7 // Original code Copyright Mark Edel.  Permission to distribute under
8 // the LGPL for the FLTK library granted by Mark Edel.
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23 // USA.
24 //
25 // Please report all bugs and problems on the following page:
26 //
27 //     http://www.fltk.org/str.php
28 //
29 
30 #ifndef FL_TEXT_BUFFER_H
31 #define FL_TEXT_BUFFER_H
32 
33 /* Maximum length in characters of a tab or control character expansion
34    of a single buffer character */
35 #define FL_TEXT_MAX_EXP_CHAR_LEN 20
36 
37 #include "Fl_Export.H"
38 
39 class FL_EXPORT Fl_Text_Selection {
40   friend class Fl_Text_Buffer;
41 
42   public:
43     void set(int start, int end);
44     void set_rectangular(int start, int end, int rectStart, int rectEnd);
45     void update(int pos, int nDeleted, int nInserted);
rectangular()46     char rectangular() { return mRectangular; }
start()47     int start() { return mStart; }
end()48     int end() { return mEnd; }
rect_start()49     int rect_start() { return mRectStart; }
rect_end()50     int rect_end() { return mRectEnd; }
selected()51     char selected() { return mSelected; }
selected(char b)52     void selected(char b) { mSelected = b; }
53     int includes(int pos, int lineStartPos, int dispIndex);
54     int position(int* start, int* end);
55     int position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd);
56 
57 
58   protected:
59     char mSelected;
60     char mRectangular;
61     int mStart;
62     int mEnd;
63     int mRectStart;
64     int mRectEnd;
65 };
66 
67 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
68                                   int nRestyled, const char* deletedText,
69                                   void* cbArg);
70 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
71 
72 class FL_EXPORT Fl_Text_Buffer {
73   public:
74     Fl_Text_Buffer(int requestedSize = 0);
75     ~Fl_Text_Buffer();
76 
length()77     int length() { return mLength; }
78     char* text();
79     void text(const char* text);
80     char* text_range(int start, int end);
81     char character(int pos);
82     char* text_in_rectangle(int start, int end, int rectStart, int rectEnd);
83     void insert(int pos, const char* text);
append(const char * t)84     void append(const char* t) { insert(length(), t); }
85     void remove(int start, int end);
86     void replace(int start, int end, const char *text);
87     void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
88     int undo(int *cp=0);
89     void canUndo(char flag=1);
90     int insertfile(const char *file, int pos, int buflen = 128*1024);
91     int appendfile(const char *file, int buflen = 128*1024)
92       { return insertfile(file, length(), buflen); }
93     int loadfile(const char *file, int buflen = 128*1024)
94       { select(0, length()); remove_selection(); return appendfile(file, buflen); }
95     int outputfile(const char *file, int start, int end, int buflen = 128*1024);
96     int savefile(const char *file, int buflen = 128*1024)
97       { return outputfile(file, 0, length(), buflen); }
98 
99     void insert_column(int column, int startPos, const char* text,
100                        int* charsInserted, int* charsDeleted);
101 
102     void replace_rectangular(int start, int end, int rectStart, int rectEnd,
103                              const char* text);
104 
105     void overlay_rectangular(int startPos, int rectStart, int rectEnd,
106                              const char* text, int* charsInserted,
107                              int* charsDeleted);
108 
109     void remove_rectangular(int start, int end, int rectStart, int rectEnd);
110     void clear_rectangular(int start, int end, int rectStart, int rectEnd);
tab_distance()111     int tab_distance() { return mTabDist; }
112     void tab_distance(int tabDist);
113     void select(int start, int end);
selected()114     int selected() { return mPrimary.selected(); }
115     void unselect();
116     void select_rectangular(int start, int end, int rectStart, int rectEnd);
117     int selection_position(int* start, int* end);
118 
119     int selection_position(int* start, int* end, int* isRect, int* rectStart,
120                            int* rectEnd);
121 
122     char* selection_text();
123     void remove_selection();
124     void replace_selection(const char* text);
125     void secondary_select(int start, int end);
secondary_selected()126     int secondary_selected() { return mSecondary.selected(); }
127     void secondary_unselect();
128 
129     void secondary_select_rectangular(int start, int end, int rectStart,
130                                       int rectEnd);
131 
132     int secondary_selection_position(int* start, int* end);
133     int secondary_selection_position(int* start, int* end, int* isRect,
134                                      int* rectStart, int* rectEnd);
135 
136     char* secondary_selection_text();
137     void remove_secondary_selection();
138     void replace_secondary_selection(const char* text);
139     void highlight(int start, int end);
highlight()140     int highlight() { return mHighlight.selected(); }
141     void unhighlight();
142     void highlight_rectangular(int start, int end, int rectStart, int rectEnd);
143 
144     int highlight_position(int* start, int* end);
145     int highlight_position(int* start, int* end, int* isRect, int* rectStart,
146                            int* rectEnd);
147 
148     char* highlight_text();
149     void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
150     void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
151 
call_modify_callbacks()152     void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
153 
154     void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
155     void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
156 
call_predelete_callbacks()157     void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
158 
159     char* line_text(int pos);
160     int line_start(int pos);
161     int line_end(int pos);
162     int word_start(int pos);
163     int word_end(int pos);
164     int expand_character(int pos, int indent, char *outStr);
165 
166     static int expand_character(char c, int indent, char* outStr, int tabDist,
167                                 char nullSubsChar);
168 
169     static int character_width(char c, int indent, int tabDist, char nullSubsChar);
170     int count_displayed_characters(int lineStartPos, int targetPos);
171     int skip_displayed_characters(int lineStartPos, int nChars);
172     int count_lines(int startPos, int endPos);
173     int skip_lines(int startPos, int nLines);
174     int rewind_lines(int startPos, int nLines);
175     int findchar_forward(int startPos, char searchChar, int* foundPos);
176     int findchar_backward(int startPos, char searchChar, int* foundPos);
177     int findchars_forward(int startPos, const char* searchChars, int* foundPos);
178     int findchars_backward(int startPos, const char* searchChars, int* foundPos);
179 
180     int search_forward(int startPos, const char* searchString, int* foundPos,
181                        int matchCase = 0);
182 
183     int search_backward(int startPos, const char* searchString, int* foundPos,
184                         int matchCase = 0);
185 
186     int substitute_null_characters(char* string, int length);
187     void unsubstitute_null_characters(char* string);
null_substitution_character()188     char null_substitution_character() { return mNullSubsChar; }
primary_selection()189     Fl_Text_Selection* primary_selection() { return &mPrimary; }
secondary_selection()190     Fl_Text_Selection* secondary_selection() { return &mSecondary; }
highlight_selection()191     Fl_Text_Selection* highlight_selection() { return &mHighlight; }
192 
193   protected:
194     void call_modify_callbacks(int pos, int nDeleted, int nInserted,
195                                int nRestyled, const char* deletedText);
196     void call_predelete_callbacks(int pos, int nDeleted);
197 
198     int insert_(int pos, const char* text);
199     void remove_(int start, int end);
200 
201     void remove_rectangular_(int start, int end, int rectStart, int rectEnd,
202                              int* replaceLen, int* endPos);
203 
204     void insert_column_(int column, int startPos, const char* insText,
205                         int* nDeleted, int* nInserted, int* endPos);
206 
207     void overlay_rectangular_(int startPos, int rectStart, int rectEnd,
208                               const char* insText, int* nDeleted,
209                               int* nInserted, int* endPos);
210 
211     void redisplay_selection(Fl_Text_Selection* oldSelection,
212                              Fl_Text_Selection* newSelection);
213 
214     void move_gap(int pos);
215     void reallocate_with_gap(int newGapStart, int newGapLen);
216     char* selection_text_(Fl_Text_Selection* sel);
217     void remove_selection_(Fl_Text_Selection* sel);
218     void replace_selection_(Fl_Text_Selection* sel, const char* text);
219 
220     void rectangular_selection_boundaries(int lineStartPos, int rectStart,
221                                           int rectEnd, int* selStart,
222                                           int* selEnd);
223 
224     void update_selections(int pos, int nDeleted, int nInserted);
225 
226     Fl_Text_Selection mPrimary; /* highlighted areas */
227     Fl_Text_Selection mSecondary;
228     Fl_Text_Selection mHighlight;
229     int mLength;                /* length of the text in the buffer (the length
230                                    of the buffer itself must be calculated:
231                                    gapEnd - gapStart + length) */
232     char* mBuf;                 /* allocated memory where the text is stored */
233     int mGapStart;              /* points to the first character of the gap */
234     int mGapEnd;                /* points to the first char after the gap */
235     // The hardware tab distance used by all displays for this buffer,
236     // and used in computing offsets for rectangular selection operations.
237     int mTabDist;               /* equiv. number of characters in a tab */
238     int mUseTabs;               /* True if buffer routines are allowed to use
239                                    tabs for padding in rectangular operations */
240     int mNModifyProcs;          /* number of modify-redisplay procs attached */
241     Fl_Text_Modify_Cb*          /* procedures to call when buffer is */
242     mNodifyProcs;               /* modified to redisplay contents */
243     void** mCbArgs;             /* caller arguments for modifyProcs above */
244     int mNPredeleteProcs;	/* number of pre-delete procs attached */
245     Fl_Text_Predelete_Cb*	/* procedure to call before text is deleted */
246 	 mPredeleteProcs;	/* from the buffer; at most one is supported. */
247     void **mPredeleteCbArgs;	/* caller argument for pre-delete proc above */
248     int mCursorPosHint;         /* hint for reasonable cursor position after
249                                    a buffer modification operation */
250     char mNullSubsChar;         /* NEdit is based on C null-terminated strings,
251                                    so ascii-nul characters must be substituted
252                                    with something else.  This is the else, but
253                                    of course, things get quite messy when you
254                                    use it */
255     char mCanUndo;		/* if this buffer is used for attributes, it must
256 				   not do any undo calls */
257 };
258 
259 #endif
260 
261 //
262 // End of "$Id: Fl_Text_Buffer.H 6010 2008-01-04 20:31:52Z matt $".
263 //
264