FLTK 1.3.4
Fl_Text_Buffer.H
1 //
2 // "$Id: Fl_Text_Buffer.H 11799 2016-06-25 00:54:24Z greg.ercolano $"
3 //
4 // Header file for Fl_Text_Buffer class.
5 //
6 // Copyright 2001-2016 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 // Please report all bugs and problems on the following page:
11 //
12 // http://www.fltk.org/str.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 /* \file
20  Fl_Text_Buffer, Fl_Text_Selection widget . */
21 
22 #ifndef FL_TEXT_BUFFER_H
23 #define FL_TEXT_BUFFER_H
24 
25 
26 #undef ASSERT_UTF8
27 
28 #ifdef ASSERT_UTF8
29 # include <assert.h>
30 # define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
31 # define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
32 #else
33 # define IS_UTF8_ALIGNED(a)
34 # define IS_UTF8_ALIGNED2(a, b)
35 #endif
36 
37 
38 /*
39  "character size" is the size of a UTF-8 character in bytes
40  "character width" is the width of a Unicode character in pixels
41  "column" was orginally defined as a character offset from the left margin.
42  It was identical to the byte offset. In UTF-8, we have neither a byte offset
43  nor truly fixed width fonts (*). Column could be a pixel value multiplied with
44  an average character width (which is a bearable approximation).
45 
46  * in Unicode, there are no fixed width fonts! Even if the ASCII characters may
47  happen to be all the same width in pixels, Chinese characters surely are not.
48  There are plenty of exceptions, like ligatures, that make special handling of
49  "fixed" character widths a nightmare. I decided to remove all references to
50  fixed fonts and see "columns" as a multiple of the average width of a
51  character in the main font.
52  - Matthias
53  */
54 
55 
56 /* Maximum length in characters of a tab or control character expansion
57  of a single buffer character */
58 #define FL_TEXT_MAX_EXP_CHAR_LEN 20
59 
60 #include "Fl_Export.H"
61 
62 
69 class FL_EXPORT Fl_Text_Selection {
70  friend class Fl_Text_Buffer;
71 
72 public:
73 
79  void set(int start, int end);
80 
89  void update(int pos, int nDeleted, int nInserted);
90 
95  int start() const { return mStart; }
96 
101  int end() const { return mEnd; }
102 
108  bool selected() const { return mSelected; }
109 
114  void selected(bool b) { mSelected = b; }
115 
120  int includes(int pos) const;
121 
128  int position(int* start, int* end) const;
129 
130 protected:
131 
132  int mStart;
133  int mEnd;
134  bool mSelected;
135 };
136 
137 
138 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
139  int nRestyled, const char* deletedText,
140  void* cbArg);
141 
142 
143 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
144 
145 
158 class FL_EXPORT Fl_Text_Buffer {
159 public:
160 
169  Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
170 
174  ~Fl_Text_Buffer();
175 
180  int length() const { return mLength; }
181 
188  char* text() const;
189 
194  void text(const char* text);
195 
206  char* text_range(int start, int end) const;
207 
214  unsigned int char_at(int pos) const;
215 
222  char byte_at(int pos) const;
223 
229  const char *address(int pos) const
230  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
231 
237  char *address(int pos)
238  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
239 
245  void insert(int pos, const char* text);
246 
251  void append(const char* t) { insert(length(), t); }
252 
258  void remove(int start, int end);
259 
267  void replace(int start, int end, const char *text);
268 
276  void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
277 
282  int undo(int *cp=0);
283 
287  void canUndo(char flag=1);
288 
304  int insertfile(const char *file, int pos, int buflen = 128*1024);
305 
309  int appendfile(const char *file, int buflen = 128*1024)
310  { return insertfile(file, length(), buflen); }
311 
315  int loadfile(const char *file, int buflen = 128*1024)
316  { select(0, length()); remove_selection(); return appendfile(file, buflen); }
317 
328  int outputfile(const char *file, int start, int end, int buflen = 128*1024);
329 
340  int savefile(const char *file, int buflen = 128*1024)
341  { return outputfile(file, 0, length(), buflen); }
342 
349  int tab_distance() const { return mTabDist; }
350 
355  void tab_distance(int tabDist);
356 
360  void select(int start, int end);
361 
365  int selected() const { return mPrimary.selected(); }
366 
370  void unselect();
371 
375  int selection_position(int* start, int* end);
376 
382  char* selection_text();
383 
387  void remove_selection();
388 
392  void replace_selection(const char* text);
393 
397  void secondary_select(int start, int end);
398 
403  int secondary_selected() { return mSecondary.selected(); }
404 
408  void secondary_unselect();
409 
413  int secondary_selection_position(int* start, int* end);
414 
420  char* secondary_selection_text();
421 
426  void remove_secondary_selection();
427 
432  void replace_secondary_selection(const char* text);
433 
437  void highlight(int start, int end);
438 
444  int highlight() { return mHighlight.selected(); }
445 
449  void unhighlight();
450 
454  int highlight_position(int* start, int* end);
455 
461  char* highlight_text();
462 
474  void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
475 
479  void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
480 
485  void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
486 
490  void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
491 
496  void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
497 
503 
512  char* line_text(int pos) const;
513 
519  int line_start(int pos) const;
520 
528  int line_end(int pos) const;
529 
535  int word_start(int pos) const;
536 
542  int word_end(int pos) const;
543 
551  int count_displayed_characters(int lineStartPos, int targetPos) const;
552 
562  int skip_displayed_characters(int lineStartPos, int nChars);
563 
568  int count_lines(int startPos, int endPos) const;
569 
574  int skip_lines(int startPos, int nLines);
575 
582  int rewind_lines(int startPos, int nLines);
583 
598  int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
599 
613  int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
614 
626  int search_forward(int startPos, const char* searchString, int* foundPos,
627  int matchCase = 0) const;
628 
640  int search_backward(int startPos, const char* searchString, int* foundPos,
641  int matchCase = 0) const;
642 
646  const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
647 
651  Fl_Text_Selection* primary_selection() { return &mPrimary; }
652 
656  const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
657 
661  const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
662 
667  int prev_char(int ix) const;
668  int prev_char_clipped(int ix) const;
669 
674  int next_char(int ix) const;
675  int next_char_clipped(int ix) const;
676 
680  int utf8_align(int) const;
681 
686 
690  static const char* file_encoding_warning_message;
691 
701  void (*transcoding_warning_action)(Fl_Text_Buffer*);
702 
703 protected:
704 
709  void call_modify_callbacks(int pos, int nDeleted, int nInserted,
710  int nRestyled, const char* deletedText) const;
711 
716  void call_predelete_callbacks(int pos, int nDeleted) const;
717 
727  int insert_(int pos, const char* text);
728 
735  void remove_(int start, int end);
736 
741  void redisplay_selection(Fl_Text_Selection* oldSelection,
742  Fl_Text_Selection* newSelection) const;
743 
747  void move_gap(int pos);
748 
753  void reallocate_with_gap(int newGapStart, int newGapLen);
754 
755  char* selection_text_(Fl_Text_Selection* sel) const;
756 
760  void remove_selection_(Fl_Text_Selection* sel);
761 
765  void replace_selection_(Fl_Text_Selection* sel, const char* text);
766 
770  void update_selections(int pos, int nDeleted, int nInserted);
771 
775  int mLength;
778  char* mBuf;
779  int mGapStart;
780  int mGapEnd;
781  // The hardware tab distance used by all displays for this buffer,
782  // and used in computing offsets for rectangular selection operations.
783  int mTabDist;
785  Fl_Text_Modify_Cb *mModifyProcs;
787  void** mCbArgs;
789  Fl_Text_Predelete_Cb *mPredeleteProcs;
794  char mCanUndo;
799 };
800 
801 #endif
802 
803 //
804 // End of "$Id: Fl_Text_Buffer.H 11799 2016-06-25 00:54:24Z greg.ercolano $".
805 //
int mNModifyProcs
number of modify-redisplay procs attached
Definition: Fl_Text_Buffer.H:784
Fl_Text_Modify_Cb * mModifyProcs
procedures to call when buffer is modified to redisplay contents
Definition: Fl_Text_Buffer.H:785
static const char * file_encoding_warning_message
This message may be displayed using the fl_alert() function when a file which was not UTF-8 encoded i...
Definition: Fl_Text_Buffer.H:690
Fl_Text_Selection mPrimary
highlighted areas
Definition: Fl_Text_Buffer.H:772
Fl_Text_Selection mSecondary
highlighted areas
Definition: Fl_Text_Buffer.H:773
const Fl_Text_Selection * highlight_selection() const
Returns the current highlight selection.
Definition: Fl_Text_Buffer.H:661
int length() const
Returns the number of bytes in the buffer.
Definition: Fl_Text_Buffer.H:180
void selected(bool b)
Modify the 'selected' flag.
Definition: Fl_Text_Buffer.H:114
int mCursorPosHint
hint for reasonable cursor position after a buffer modification operation
Definition: Fl_Text_Buffer.H:792
This class manages Unicode text displayed in one or more Fl_Text_Display widgets. ...
Definition: Fl_Text_Buffer.H:158
int end() const
Return the byte offset to the character after the last selected character.
Definition: Fl_Text_Buffer.H:101
int mStart
byte offset to the first selected character
Definition: Fl_Text_Buffer.H:132
bool mSelected
this flag is set if any text is selected
Definition: Fl_Text_Buffer.H:134
void append(const char *t)
Appends the text string to the end of the buffer.
Definition: Fl_Text_Buffer.H:251
int appendfile(const char *file, int buflen=128 *1024)
Appends the named file to the end of the buffer.
Definition: Fl_Text_Buffer.H:309
const char * address(int pos) const
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:229
int mLength
length of the text in the buffer (the length of the buffer itself must be calculated: gapEnd - gapSta...
Definition: Fl_Text_Buffer.H:775
int mGapStart
points to the first character of the gap
Definition: Fl_Text_Buffer.H:779
int mTabDist
equiv.
Definition: Fl_Text_Buffer.H:783
Fl_Text_Selection * primary_selection()
Returns the primary selection.
Definition: Fl_Text_Buffer.H:651
This is an internal class for Fl_Text_Buffer to manage text selections.
Definition: Fl_Text_Buffer.H:69
int mPreferredGapSize
the default allocation for the text gap is 1024 bytes and should only be increased if frequent and la...
Definition: Fl_Text_Buffer.H:796
char * address(int pos)
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:237
int start() const
Return the byte offset to the first selected character.
Definition: Fl_Text_Buffer.H:95
int highlight()
Returns the highlighted text.
Definition: Fl_Text_Buffer.H:444
int savefile(const char *file, int buflen=128 *1024)
Saves a text file from the current buffer.
Definition: Fl_Text_Buffer.H:340
void call_modify_callbacks()
Calls all modify callbacks that have been registered using the add_modify_callback() method...
Definition: Fl_Text_Buffer.H:485
char mCanUndo
if this buffer is used for attributes, it must not do any undo calls
Definition: Fl_Text_Buffer.H:794
void ** mCbArgs
caller arguments for modifyProcs above
Definition: Fl_Text_Buffer.H:787
const Fl_Text_Selection * primary_selection() const
Returns the primary selection.
Definition: Fl_Text_Buffer.H:646
Fl_Text_Predelete_Cb * mPredeleteProcs
procedure to call before text is deleted from the buffer; at most one is supported.
Definition: Fl_Text_Buffer.H:789
const Fl_Text_Selection * secondary_selection() const
Returns the secondary selection.
Definition: Fl_Text_Buffer.H:656
Fl_Text_Selection mHighlight
highlighted areas
Definition: Fl_Text_Buffer.H:774
int mNPredeleteProcs
number of pre-delete procs attached
Definition: Fl_Text_Buffer.H:788
int loadfile(const char *file, int buflen=128 *1024)
Loads a text file into the buffer.
Definition: Fl_Text_Buffer.H:315
bool selected() const
Returns true if any text is selected.
Definition: Fl_Text_Buffer.H:108
int secondary_selected()
Returns a non-zero value if text has been selected in the secondary text selection, 0 otherwise.
Definition: Fl_Text_Buffer.H:403
char * mBuf
allocated memory where the text is stored
Definition: Fl_Text_Buffer.H:778
int input_file_was_transcoded
true if the loaded file has been transcoded to UTF-8.
Definition: Fl_Text_Buffer.H:685
int selected() const
Returns a non-zero value if text has been selected, 0 otherwise.
Definition: Fl_Text_Buffer.H:365
void ** mPredeleteCbArgs
caller argument for pre-delete proc above
Definition: Fl_Text_Buffer.H:791
int mGapEnd
points to the first character after the gap
Definition: Fl_Text_Buffer.H:780
int tab_distance() const
Gets the tab width.
Definition: Fl_Text_Buffer.H:349
void call_predelete_callbacks()
Calls the stored pre-delete callback procedure(s) for this buffer to update the changed area(s) on th...
Definition: Fl_Text_Buffer.H:502
int mEnd
byte offset to the character after the last selected character
Definition: Fl_Text_Buffer.H:133