[773] | 1 | // |
---|
| 2 | // "$Id: TextBuffer.h 4467 2005-08-05 19:17:34Z dejan $" |
---|
| 3 | // |
---|
| 4 | // Header file for TextBuffer 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
| 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 TEXT_BUFFER_H |
---|
| 31 | #define TEXT_BUFFER_H |
---|
| 32 | |
---|
| 33 | #include "FL_API.h" |
---|
| 34 | |
---|
| 35 | namespace fltk { |
---|
| 36 | |
---|
| 37 | /* Maximum length in characters of a tab or control character expansion |
---|
| 38 | of a single buffer character */ |
---|
| 39 | #define TEXT_MAX_EXP_CHAR_LEN 20 |
---|
| 40 | |
---|
| 41 | class FL_API TextSelection { |
---|
| 42 | friend class TextBuffer; |
---|
| 43 | |
---|
| 44 | public: |
---|
| 45 | void set(int start, int end); |
---|
| 46 | void set_rectangular(int start, int end, int rectStart, int rectEnd); |
---|
| 47 | void update(int pos, int nDeleted, int nInserted); |
---|
| 48 | char rectangular() { return mRectangular; } |
---|
| 49 | int start() { return mStart; } |
---|
| 50 | int end() { return mEnd; } |
---|
| 51 | int rect_start() { return mRectStart; } |
---|
| 52 | int rect_end() { return mRectEnd; } |
---|
| 53 | char selected() { return mSelected; } |
---|
| 54 | void selected(char b) { mSelected = b; } |
---|
| 55 | int includes(int pos, int lineStartPos, int dispIndex); |
---|
| 56 | int position(int* start, int* end); |
---|
| 57 | int position(int* start, int* end, int* isRect, int* rectStart, int* rectEnd); |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | protected: |
---|
| 61 | char mSelected; |
---|
| 62 | char mRectangular; |
---|
| 63 | int mStart; |
---|
| 64 | int mEnd; |
---|
| 65 | int mRectStart; |
---|
| 66 | int mRectEnd; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | typedef void (*Text_Modify_Cb)(int pos, int nInserted, int nDeleted, |
---|
| 70 | int nRestyled, const char* deletedText, |
---|
| 71 | void* cbArg); |
---|
| 72 | typedef void (*Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg); |
---|
| 73 | |
---|
| 74 | class FL_API TextBuffer { |
---|
| 75 | public: |
---|
| 76 | TextBuffer(int requestedSize = 0); |
---|
| 77 | ~TextBuffer(); |
---|
| 78 | |
---|
| 79 | int length() { return mLength; } |
---|
| 80 | char* text(); |
---|
| 81 | void text(const char* text); |
---|
| 82 | char* text_range(int start, int end); |
---|
| 83 | char character(int pos); |
---|
| 84 | char* text_in_rectangle(int start, int end, int rectStart, int rectEnd); |
---|
| 85 | void insert(int pos, const char* text); |
---|
| 86 | void append(const char* t) { insert(length(), t); } |
---|
| 87 | void remove(int start, int end); |
---|
| 88 | void replace(int start, int end, const char *text); |
---|
| 89 | void copy(TextBuffer* fromBuf, int fromStart, int fromEnd, int toPos); |
---|
| 90 | int undo(int *cp=0); |
---|
| 91 | void canUndo(char flag=1); |
---|
| 92 | int insertfile(const char *file, int pos, int buflen = 128*1024); |
---|
| 93 | int appendfile(const char *file, int buflen = 128*1024) |
---|
| 94 | { return insertfile(file, length(), buflen); } |
---|
| 95 | int loadfile(const char *file, int buflen = 128*1024) |
---|
| 96 | { select(0, length()); remove_selection(); return appendfile(file, buflen); } |
---|
| 97 | int outputfile(const char *file, int start, int end, int buflen = 128*1024); |
---|
| 98 | int savefile(const char *file, int buflen = 128*1024) |
---|
| 99 | { return outputfile(file, 0, length(), buflen); } |
---|
| 100 | |
---|
| 101 | void insert_column(int column, int startPos, const char* text, |
---|
| 102 | int* charsInserted, int* charsDeleted); |
---|
| 103 | |
---|
| 104 | void replace_rectangular(int start, int end, int rectStart, int rectEnd, |
---|
| 105 | const char* text); |
---|
| 106 | |
---|
| 107 | void overlay_rectangular(int startPos, int rectStart, int rectEnd, |
---|
| 108 | const char* text, int* charsInserted, |
---|
| 109 | int* charsDeleted); |
---|
| 110 | |
---|
| 111 | void remove_rectangular(int start, int end, int rectStart, int rectEnd); |
---|
| 112 | void clear_rectangular(int start, int end, int rectStart, int rectEnd); |
---|
| 113 | int tab_distance() { return mTabDist; } |
---|
| 114 | void tab_distance(int tabDist); |
---|
| 115 | void select(int start, int end); |
---|
| 116 | int selected() { return mPrimary.selected(); } |
---|
| 117 | void unselect(); |
---|
| 118 | void select_rectangular(int start, int end, int rectStart, int rectEnd); |
---|
| 119 | int selection_position(int* start, int* end); |
---|
| 120 | |
---|
| 121 | int selection_position(int* start, int* end, int* isRect, int* rectStart, |
---|
| 122 | int* rectEnd); |
---|
| 123 | |
---|
| 124 | char* selection_text(); |
---|
| 125 | void remove_selection(); |
---|
| 126 | void replace_selection(const char* text); |
---|
| 127 | void secondary_select(int start, int end); |
---|
| 128 | void secondary_unselect(); |
---|
| 129 | |
---|
| 130 | void secondary_select_rectangular(int start, int end, int rectStart, |
---|
| 131 | int rectEnd); |
---|
| 132 | |
---|
| 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); |
---|
| 140 | void unhighlight(); |
---|
| 141 | void highlight_rectangular(int start, int end, int rectStart, int rectEnd); |
---|
| 142 | |
---|
| 143 | int highlight_position(int* start, int* end, int* isRect, int* rectStart, |
---|
| 144 | int* rectEnd); |
---|
| 145 | |
---|
| 146 | char* highlight_text(); |
---|
| 147 | void add_modify_callback(Text_Modify_Cb bufModifiedCB, void* cbArg); |
---|
| 148 | void remove_modify_callback(Text_Modify_Cb bufModifiedCB, void* cbArg); |
---|
| 149 | |
---|
| 150 | void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); } |
---|
| 151 | |
---|
| 152 | void add_predelete_callback(Text_Predelete_Cb bufPredelCB, void* cbArg); |
---|
| 153 | void remove_predelete_callback(Text_Predelete_Cb predelCB, void* cbArg); |
---|
| 154 | |
---|
| 155 | void call_predelete_callbacks() { call_predelete_callbacks(0, 0); } |
---|
| 156 | |
---|
| 157 | char* line_text(int pos); |
---|
| 158 | int line_start(int pos); |
---|
| 159 | int line_end(int pos); |
---|
| 160 | int word_start(int pos); |
---|
| 161 | int word_end(int pos); |
---|
| 162 | int expand_character(int pos, int indent, char *outStr); |
---|
| 163 | |
---|
| 164 | static int expand_character(char c, int indent, char* outStr, int tabDist, |
---|
| 165 | char nullSubsChar); |
---|
| 166 | |
---|
| 167 | static int character_width(char c, int indent, int tabDist, char nullSubsChar); |
---|
| 168 | int count_displayed_characters(int lineStartPos, int targetPos); |
---|
| 169 | int skip_displayed_characters(int lineStartPos, int nChars); |
---|
| 170 | int count_lines(int startPos, int endPos); |
---|
| 171 | int skip_lines(int startPos, int nLines); |
---|
| 172 | int rewind_lines(int startPos, int nLines); |
---|
| 173 | int findchar_forward(int startPos, char searchChar, int* foundPos); |
---|
| 174 | int findchar_backward(int startPos, char searchChar, int* foundPos); |
---|
| 175 | int findchars_forward(int startPos, const char* searchChars, int* foundPos); |
---|
| 176 | int findchars_backward(int startPos, const char* searchChars, int* foundPos); |
---|
| 177 | |
---|
| 178 | int search_forward(int startPos, const char* searchString, int* foundPos, |
---|
| 179 | int matchCase = 0); |
---|
| 180 | |
---|
| 181 | int search_backward(int startPos, const char* searchString, int* foundPos, |
---|
| 182 | int matchCase = 0); |
---|
| 183 | |
---|
| 184 | int substitute_null_characters(char* string, int length); |
---|
| 185 | void unsubstitute_null_characters(char* string); |
---|
| 186 | char null_substitution_character() { return mNullSubsChar; } |
---|
| 187 | TextSelection* primary_selection() { return &mPrimary; } |
---|
| 188 | TextSelection* secondary_selection() { return &mSecondary; } |
---|
| 189 | TextSelection* highlight_selection() { return &mHighlight; } |
---|
| 190 | |
---|
| 191 | protected: |
---|
| 192 | void call_modify_callbacks(int pos, int nDeleted, int nInserted, |
---|
| 193 | int nRestyled, const char* deletedText); |
---|
| 194 | void call_predelete_callbacks(int pos, int nDeleted); |
---|
| 195 | |
---|
| 196 | int insert_(int pos, const char* text); |
---|
| 197 | void remove_(int start, int end); |
---|
| 198 | |
---|
| 199 | void remove_rectangular_(int start, int end, int rectStart, int rectEnd, |
---|
| 200 | int* replaceLen, int* endPos); |
---|
| 201 | |
---|
| 202 | void insert_column_(int column, int startPos, const char* insText, |
---|
| 203 | int* nDeleted, int* nInserted, int* endPos); |
---|
| 204 | |
---|
| 205 | void overlay_rectangular_(int startPos, int rectStart, int rectEnd, |
---|
| 206 | const char* insText, int* nDeleted, |
---|
| 207 | int* nInserted, int* endPos); |
---|
| 208 | |
---|
| 209 | void redisplay_selection(TextSelection* oldSelection, |
---|
| 210 | TextSelection* newSelection); |
---|
| 211 | |
---|
| 212 | void move_gap(int pos); |
---|
| 213 | void reallocate_with_gap(int newGapStart, int newGapLen); |
---|
| 214 | char* selection_text_(TextSelection* sel); |
---|
| 215 | void remove_selection_(TextSelection* sel); |
---|
| 216 | void replace_selection_(TextSelection* sel, const char* text); |
---|
| 217 | |
---|
| 218 | void rectangular_selection_boundaries(int lineStartPos, int rectStart, |
---|
| 219 | int rectEnd, int* selStart, |
---|
| 220 | int* selEnd); |
---|
| 221 | |
---|
| 222 | void update_selections(int pos, int nDeleted, int nInserted); |
---|
| 223 | |
---|
| 224 | TextSelection mPrimary; /* highlighted areas */ |
---|
| 225 | TextSelection mSecondary; |
---|
| 226 | TextSelection mHighlight; |
---|
| 227 | int mLength; /* length of the text in the buffer (the length |
---|
| 228 | of the buffer itself must be calculated: |
---|
| 229 | gapEnd - gapStart + length) */ |
---|
| 230 | char* mBuf; /* allocated memory where the text is stored */ |
---|
| 231 | int mGapStart; /* points to the first character of the gap */ |
---|
| 232 | int mGapEnd; /* points to the first char after the gap */ |
---|
| 233 | // The hardware tab distance used by all displays for this buffer, |
---|
| 234 | // and used in computing offsets for rectangular selection operations. |
---|
| 235 | int mTabDist; /* equiv. number of characters in a tab */ |
---|
| 236 | int mUseTabs; /* True if buffer routines are allowed to use |
---|
| 237 | tabs for padding in rectangular operations */ |
---|
| 238 | int mNModifyProcs; /* number of modify-redisplay procs attached */ |
---|
| 239 | Text_Modify_Cb* /* procedures to call when buffer is */ |
---|
| 240 | mNodifyProcs; /* modified to redisplay contents */ |
---|
| 241 | void** mCbArgs; /* caller arguments for modifyProcs above */ |
---|
| 242 | int mNPredeleteProcs; /* number of pre-delete procs attached */ |
---|
| 243 | Text_Predelete_Cb* /* procedure to call before text is deleted */ |
---|
| 244 | mPredeleteProcs; /* from the buffer; at most one is supported. */ |
---|
| 245 | void **mPredeleteCbArgs; /* caller argument for pre-delete proc above */ |
---|
| 246 | int mCursorPosHint; /* hint for reasonable cursor position after |
---|
| 247 | a buffer modification operation */ |
---|
| 248 | char mNullSubsChar; /* NEdit is based on C null-terminated strings, |
---|
| 249 | so ascii-nul characters must be substituted |
---|
| 250 | with something else. This is the else, but |
---|
| 251 | of course, things get quite messy when you |
---|
| 252 | use it */ |
---|
| 253 | char mCanUndo; /* if this buffer is used for attributes, it must |
---|
| 254 | not do any undo calls */ |
---|
| 255 | }; |
---|
| 256 | |
---|
| 257 | } |
---|
| 258 | #endif |
---|
| 259 | |
---|
| 260 | // |
---|
| 261 | // End of "$Id: TextBuffer.h 4467 2005-08-05 19:17:34Z dejan $". |
---|
| 262 | // |
---|
| 263 | |
---|