FLTK
Fl_Text_Buffer.H
1 //
2 // Header file for Fl_Text_Buffer class.
3 //
4 // Copyright 2001-2017 by Bill Spitzak and others.
5 // Original code Copyright Mark Edel. Permission to distribute under
6 // the LGPL for the FLTK library granted by Mark Edel.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // https://www.fltk.org/COPYING.php
13 //
14 // Please see the following page on how to report bugs and issues:
15 //
16 // https://www.fltk.org/bugs.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 #include <stdarg.h> /* va_start/end */
26 
27 #undef ASSERT_UTF8
28 
29 #ifdef ASSERT_UTF8
30 # include <assert.h>
31 # define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
32 # define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
33 #else
34 # define IS_UTF8_ALIGNED(a)
35 # define IS_UTF8_ALIGNED2(a, b)
36 #endif
37 
38 
39 /*
40  "character size" is the size of a UTF-8 character in bytes
41  "character width" is the width of a Unicode character in pixels
42  "column" was orginally defined as a character offset from the left margin.
43  It was identical to the byte offset. In UTF-8, we have neither a byte offset
44  nor truly fixed width fonts (*). Column could be a pixel value multiplied with
45  an average character width (which is a bearable approximation).
46 
47  * in Unicode, there are no fixed width fonts! Even if the ASCII characters may
48  happen to be all the same width in pixels, Chinese characters surely are not.
49  There are plenty of exceptions, like ligatures, that make special handling of
50  "fixed" character widths a nightmare. I decided to remove all references to
51  fixed fonts and see "columns" as a multiple of the average width of a
52  character in the main font.
53  - Matthias
54  */
55 
56 
57 /* Maximum length in characters of a tab or control character expansion
58  of a single buffer character */
59 #define FL_TEXT_MAX_EXP_CHAR_LEN 20
60 
61 #include "Fl_Export.H"
62 
63 
97 class FL_EXPORT Fl_Text_Selection {
98  friend class Fl_Text_Buffer;
99 
100 public:
101 
102  // Sets the selection range and selected().
103  void set(int startpos, int endpos);
104 
105  // Updates a selection after text was modified.
106  void update(int pos, int nDeleted, int nInserted);
107 
119  int start() const { return mSelected ? mStart : 0; }
120 
132  int end() const { return mSelected ? mEnd : 0; }
133 
139  bool selected() const { return mSelected; }
140 
145  void selected(bool b) { mSelected = b; }
146 
160  int length() const { return mSelected ? mEnd - mStart : 0; }
161 
162  // Returns true if position \p pos is in this Fl_Text_Selection.
163  int includes(int pos) const;
164 
165  // Returns true if selected() and the positions of this selection.
166  int position(int *startpos, int *endpos) const;
167 
168 protected:
169 
170  int mStart;
171  int mEnd;
172  bool mSelected;
173 };
174 
175 
176 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
177  int nRestyled, const char* deletedText,
178  void* cbArg);
179 
180 
181 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
182 
183 
196 class FL_EXPORT Fl_Text_Buffer {
197 public:
198 
207  Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
208 
212  ~Fl_Text_Buffer();
213 
218  int length() const { return mLength; }
219 
226  char* text() const;
227 
232  void text(const char* text);
233 
244  char* text_range(int start, int end) const;
245 
252  unsigned int char_at(int pos) const;
253 
260  char byte_at(int pos) const;
261 
267  const char *address(int pos) const
268  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
269 
275  char *address(int pos)
276  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
277 
283  void insert(int pos, const char* text);
284 
289  void append(const char* t) { insert(length(), t); }
290 
291  void vprintf(const char *fmt, va_list ap);
292  void printf(const char* fmt, ...);
293 
299  void remove(int start, int end);
300 
308  void replace(int start, int end, const char *text);
309 
317  void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
318 
323  int undo(int *cp=0);
324 
328  void canUndo(char flag=1);
329 
345  int insertfile(const char *file, int pos, int buflen = 128*1024);
346 
350  int appendfile(const char *file, int buflen = 128*1024)
351  { return insertfile(file, length(), buflen); }
352 
356  int loadfile(const char *file, int buflen = 128*1024)
357  { select(0, length()); remove_selection(); return appendfile(file, buflen); }
358 
369  int outputfile(const char *file, int start, int end, int buflen = 128*1024);
370 
381  int savefile(const char *file, int buflen = 128*1024)
382  { return outputfile(file, 0, length(), buflen); }
383 
390  int tab_distance() const { return mTabDist; }
391 
396  void tab_distance(int tabDist);
397 
401  void select(int start, int end);
402 
406  int selected() const { return mPrimary.selected(); }
407 
411  void unselect();
412 
416  int selection_position(int* start, int* end);
417 
423  char* selection_text();
424 
428  void remove_selection();
429 
433  void replace_selection(const char* text);
434 
438  void secondary_select(int start, int end);
439 
444  int secondary_selected() { return mSecondary.selected(); }
445 
449  void secondary_unselect();
450 
454  int secondary_selection_position(int* start, int* end);
455 
461  char* secondary_selection_text();
462 
467  void remove_secondary_selection();
468 
473  void replace_secondary_selection(const char* text);
474 
478  void highlight(int start, int end);
479 
483  int highlight() { return mHighlight.selected(); }
484 
488  void unhighlight();
489 
493  int highlight_position(int* start, int* end);
494 
500  char* highlight_text();
501 
513  void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
514 
518  void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
519 
524  void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
525 
529  void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
530 
535  void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
536 
542 
551  char* line_text(int pos) const;
552 
558  int line_start(int pos) const;
559 
567  int line_end(int pos) const;
568 
574  int word_start(int pos) const;
575 
581  int word_end(int pos) const;
582 
590  int count_displayed_characters(int lineStartPos, int targetPos) const;
591 
601  int skip_displayed_characters(int lineStartPos, int nChars);
602 
607  int count_lines(int startPos, int endPos) const;
608 
613  int skip_lines(int startPos, int nLines);
614 
621  int rewind_lines(int startPos, int nLines);
622 
637  int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
638 
652  int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
653 
665  int search_forward(int startPos, const char* searchString, int* foundPos,
666  int matchCase = 0) const;
667 
679  int search_backward(int startPos, const char* searchString, int* foundPos,
680  int matchCase = 0) const;
681 
685  const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
686 
690  Fl_Text_Selection* primary_selection() { return &mPrimary; }
691 
695  const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
696 
700  const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
701 
706  int prev_char(int ix) const;
707  int prev_char_clipped(int ix) const;
708 
713  int next_char(int ix) const;
714  int next_char_clipped(int ix) const;
715 
719  int utf8_align(int) const;
720 
725 
729  static const char* file_encoding_warning_message;
730 
740  void (*transcoding_warning_action)(Fl_Text_Buffer*);
741  bool is_word_separator(int pos) const;
742 
743 protected:
744 
749  void call_modify_callbacks(int pos, int nDeleted, int nInserted,
750  int nRestyled, const char* deletedText) const;
751 
756  void call_predelete_callbacks(int pos, int nDeleted) const;
757 
767  int insert_(int pos, const char* text);
768 
775  void remove_(int start, int end);
776 
781  void redisplay_selection(Fl_Text_Selection* oldSelection,
782  Fl_Text_Selection* newSelection) const;
783 
787  void move_gap(int pos);
788 
793  void reallocate_with_gap(int newGapStart, int newGapLen);
794 
795  char* selection_text_(Fl_Text_Selection* sel) const;
796 
800  void remove_selection_(Fl_Text_Selection* sel);
801 
805  void replace_selection_(Fl_Text_Selection* sel, const char* text);
806 
810  void update_selections(int pos, int nDeleted, int nInserted);
811 
815  int mLength;
818  char* mBuf;
819  int mGapStart;
820  int mGapEnd;
821  // The hardware tab distance used by all displays for this buffer,
822  // and used in computing offsets for rectangular selection operations.
823  int mTabDist;
825  Fl_Text_Modify_Cb *mModifyProcs;
827  void** mCbArgs;
829  Fl_Text_Predelete_Cb *mPredeleteProcs;
834  char mCanUndo;
839 };
840 
841 #endif
const Fl_Text_Selection * primary_selection() const
Returns the primary selection.
Definition: Fl_Text_Buffer.H:685
int mNModifyProcs
number of modify-redisplay procs attached
Definition: Fl_Text_Buffer.H:824
bool selected() const
Returns true if any text is selected.
Definition: Fl_Text_Buffer.H:139
int length() const
Returns the number of bytes in the buffer.
Definition: Fl_Text_Buffer.H:218
Fl_Text_Modify_Cb * mModifyProcs
procedures to call when buffer is modified to redisplay contents
Definition: Fl_Text_Buffer.H:825
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:729
Fl_Text_Selection mPrimary
highlighted areas
Definition: Fl_Text_Buffer.H:812
Fl_Text_Selection mSecondary
highlighted areas
Definition: Fl_Text_Buffer.H:813
int tab_distance() const
Gets the tab width.
Definition: Fl_Text_Buffer.H:390
void selected(bool b)
Modifies the &#39;selected&#39; flag.
Definition: Fl_Text_Buffer.H:145
int mCursorPosHint
hint for reasonable cursor position after a buffer modification operation
Definition: Fl_Text_Buffer.H:832
This class manages Unicode text displayed in one or more Fl_Text_Display widgets. ...
Definition: Fl_Text_Buffer.H:196
int mStart
byte offset to the first selected character
Definition: Fl_Text_Buffer.H:170
bool mSelected
this flag is set if any text is selected
Definition: Fl_Text_Buffer.H:172
void append(const char *t)
Appends the text string to the end of the buffer.
Definition: Fl_Text_Buffer.H:289
int appendfile(const char *file, int buflen=128 *1024)
Appends the named file to the end of the buffer.
Definition: Fl_Text_Buffer.H:350
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:815
int mGapStart
points to the first character of the gap
Definition: Fl_Text_Buffer.H:819
int length() const
Returns the size in bytes of the selection.
Definition: Fl_Text_Buffer.H:160
int mTabDist
equiv.
Definition: Fl_Text_Buffer.H:823
int selected() const
Returns a non-zero value if text has been selected, 0 otherwise.
Definition: Fl_Text_Buffer.H:406
Fl_Text_Selection * primary_selection()
Returns the primary selection.
Definition: Fl_Text_Buffer.H:690
This is an internal class for Fl_Text_Buffer to manage text selections.
Definition: Fl_Text_Buffer.H:97
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:836
const Fl_Text_Selection * highlight_selection() const
Returns the current highlight selection.
Definition: Fl_Text_Buffer.H:700
int start() const
Returns the byte offset to the first selected character.
Definition: Fl_Text_Buffer.H:119
const char * address(int pos) const
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:267
char * address(int pos)
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:275
int end() const
Returns the byte offset to the character after the last selected character.
Definition: Fl_Text_Buffer.H:132
int highlight()
Returns a non-zero value if text has been highlighted, 0 otherwise.
Definition: Fl_Text_Buffer.H:483
int savefile(const char *file, int buflen=128 *1024)
Saves a text file from the current buffer.
Definition: Fl_Text_Buffer.H:381
void call_modify_callbacks()
Calls all modify callbacks that have been registered using the add_modify_callback() method...
Definition: Fl_Text_Buffer.H:524
char mCanUndo
if this buffer is used for attributes, it must not do any undo calls
Definition: Fl_Text_Buffer.H:834
void ** mCbArgs
caller arguments for modifyProcs above
Definition: Fl_Text_Buffer.H:827
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:829
Fl_Text_Selection mHighlight
highlighted areas
Definition: Fl_Text_Buffer.H:814
int mNPredeleteProcs
number of pre-delete procs attached
Definition: Fl_Text_Buffer.H:828
int loadfile(const char *file, int buflen=128 *1024)
Loads a text file into the buffer.
Definition: Fl_Text_Buffer.H:356
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:444
char * mBuf
allocated memory where the text is stored
Definition: Fl_Text_Buffer.H:818
int input_file_was_transcoded
true if the loaded file has been transcoded to UTF-8.
Definition: Fl_Text_Buffer.H:724
void ** mPredeleteCbArgs
caller argument for pre-delete proc above
Definition: Fl_Text_Buffer.H:831
int mGapEnd
points to the first character after the gap
Definition: Fl_Text_Buffer.H:820
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:541
const Fl_Text_Selection * secondary_selection() const
Returns the secondary selection.
Definition: Fl_Text_Buffer.H:695
int mEnd
byte offset to the character after the last selected character
Definition: Fl_Text_Buffer.H:171