FLTK 1.3.2
Fl_Text_Buffer.H
1 //
2 // "$Id: Fl_Text_Buffer.H 9366 2012-04-21 15:05:00Z fabien $"
3 //
4 // Header file for Fl_Text_Buffer class.
5 //
6 // Copyright 2001-2010 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 charcaters 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 
88  void update(int pos, int nDeleted, int nInserted);
89 
94  int start() const { return mStart; }
95 
100  int end() const { return mEnd; }
101 
107  bool selected() const { return mSelected; }
108 
113  void selected(bool b) { mSelected = b; }
114 
119  int includes(int pos) const;
120 
127  int position(int* start, int* end) const;
128 
129 protected:
130 
131  int mStart;
132  int mEnd;
133  bool mSelected;
134 };
135 
136 
137 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
138  int nRestyled, const char* deletedText,
139  void* cbArg);
140 
141 
142 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
143 
144 
157 class FL_EXPORT Fl_Text_Buffer {
158 public:
159 
168  Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
169 
173  ~Fl_Text_Buffer();
174 
179  int length() const { return mLength; }
180 
187  char* text() const;
188 
193  void text(const char* text);
194 
205  char* text_range(int start, int end) const;
206 
213  unsigned int char_at(int pos) const;
214 
221  char byte_at(int pos) const;
222 
228  const char *address(int pos) const
229  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
230 
236  char *address(int pos)
237  { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
238 
244  void insert(int pos, const char* text);
245 
250  void append(const char* t) { insert(length(), t); }
251 
257  void remove(int start, int end);
258 
265  void replace(int start, int end, const char *text);
266 
274  void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
275 
280  int undo(int *cp=0);
281 
285  void canUndo(char flag=1);
286 
298  int insertfile(const char *file, int pos, int buflen = 128*1024);
299 
303  int appendfile(const char *file, int buflen = 128*1024)
304  { return insertfile(file, length(), buflen); }
305 
309  int loadfile(const char *file, int buflen = 128*1024)
310  { select(0, length()); remove_selection(); return appendfile(file, buflen); }
311 
318  int outputfile(const char *file, int start, int end, int buflen = 128*1024);
319 
323  int savefile(const char *file, int buflen = 128*1024)
324  { return outputfile(file, 0, length(), buflen); }
325 
329  int tab_distance() const { return mTabDist; }
330 
335  void tab_distance(int tabDist);
336 
340  void select(int start, int end);
341 
345  int selected() const { return mPrimary.selected(); }
346 
350  void unselect();
351 
355  int selection_position(int* start, int* end);
356 
361  char* selection_text();
362 
366  void remove_selection();
367 
371  void replace_selection(const char* text);
372 
376  void secondary_select(int start, int end);
377 
382  int secondary_selected() { return mSecondary.selected(); }
383 
387  void secondary_unselect();
388 
392  int secondary_selection_position(int* start, int* end);
393 
398  char* secondary_selection_text();
399 
403  void remove_secondary_selection();
404 
409  void replace_secondary_selection(const char* text);
410 
414  void highlight(int start, int end);
415 
420  int highlight() { return mHighlight.selected(); }
421 
425  void unhighlight();
426 
430  int highlight_position(int* start, int* end);
431 
436  char* highlight_text();
437 
448  void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
449 
453  void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
454 
460  void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
461 
465  void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
466 
471  void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
472 
478 
486  char* line_text(int pos) const;
487 
493  int line_start(int pos) const;
494 
502  int line_end(int pos) const;
503 
509  int word_start(int pos) const;
510 
516  int word_end(int pos) const;
517 
524  int count_displayed_characters(int lineStartPos, int targetPos) const;
525 
534  int skip_displayed_characters(int lineStartPos, int nChars);
535 
540  int count_lines(int startPos, int endPos) const;
541 
546  int skip_lines(int startPos, int nLines);
547 
553  int rewind_lines(int startPos, int nLines);
554 
568  int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
569 
582  int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
583 
594  int search_forward(int startPos, const char* searchString, int* foundPos,
595  int matchCase = 0) const;
596 
607  int search_backward(int startPos, const char* searchString, int* foundPos,
608  int matchCase = 0) const;
609 
613  const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
614 
618  Fl_Text_Selection* primary_selection() { return &mPrimary; }
619 
623  const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
624 
628  const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
629 
634  int prev_char(int ix) const;
635  int prev_char_clipped(int ix) const;
636 
641  int next_char(int ix) const;
642  int next_char_clipped(int ix) const;
643 
647  int utf8_align(int) const;
648 
653 
657  static const char* file_encoding_warning_message;
658 
668  void (*transcoding_warning_action)(Fl_Text_Buffer*);
669 
670 protected:
671 
676  void call_modify_callbacks(int pos, int nDeleted, int nInserted,
677  int nRestyled, const char* deletedText) const;
678 
683  void call_predelete_callbacks(int pos, int nDeleted) const;
684 
693  int insert_(int pos, const char* text);
694 
700  void remove_(int start, int end);
701 
706  void redisplay_selection(Fl_Text_Selection* oldSelection,
707  Fl_Text_Selection* newSelection) const;
708 
712  void move_gap(int pos);
713 
718  void reallocate_with_gap(int newGapStart, int newGapLen);
719 
720  char* selection_text_(Fl_Text_Selection* sel) const;
721 
725  void remove_selection_(Fl_Text_Selection* sel);
726 
730  void replace_selection_(Fl_Text_Selection* sel, const char* text);
731 
735  void update_selections(int pos, int nDeleted, int nInserted);
736 
740  int mLength;
743  char* mBuf;
744  int mGapStart;
745  int mGapEnd;
746  // The hardware tab distance used by all displays for this buffer,
747  // and used in computing offsets for rectangular selection operations.
748  int mTabDist;
750  Fl_Text_Modify_Cb *mModifyProcs;
752  void** mCbArgs;
754  Fl_Text_Predelete_Cb *mPredeleteProcs;
759  char mCanUndo;
764 };
765 
766 #endif
767 
768 //
769 // End of "$Id: Fl_Text_Buffer.H 9366 2012-04-21 15:05:00Z fabien $".
770 //
int mNModifyProcs
number of modify-redisplay procs attached
Definition: Fl_Text_Buffer.H:749
Fl_Text_Modify_Cb * mModifyProcs
procedures to call when buffer is modified to redisplay contents
Definition: Fl_Text_Buffer.H:750
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:657
Fl_Text_Selection mPrimary
highlighted areas
Definition: Fl_Text_Buffer.H:737
Fl_Text_Selection mSecondary
highlighted areas
Definition: Fl_Text_Buffer.H:738
const Fl_Text_Selection * highlight_selection() const
Returns the current highlight selection.
Definition: Fl_Text_Buffer.H:628
int length() const
Returns the number of bytes in the buffer.
Definition: Fl_Text_Buffer.H:179
void selected(bool b)
Modify the 'selected' flag.
Definition: Fl_Text_Buffer.H:113
int mCursorPosHint
hint for reasonable cursor position after a buffer modification operation
Definition: Fl_Text_Buffer.H:757
This class manages unicode displayed in one or more Fl_Text_Display widgets.
Definition: Fl_Text_Buffer.H:157
int end() const
Return the byte ofsset to the character after the last selected character.
Definition: Fl_Text_Buffer.H:100
int mStart
byte offset to the first selected character
Definition: Fl_Text_Buffer.H:131
bool mSelected
this flag is set if any text is selected
Definition: Fl_Text_Buffer.H:133
void append(const char *t)
Appends the text string to the end of the buffer.
Definition: Fl_Text_Buffer.H:250
int appendfile(const char *file, int buflen=128 *1024)
Appends the named file to the end of the buffer.
Definition: Fl_Text_Buffer.H:303
const char * address(int pos) const
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:228
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:740
int mGapStart
points to the first character of the gap
Definition: Fl_Text_Buffer.H:744
int mTabDist
equiv.
Definition: Fl_Text_Buffer.H:748
Fl_Text_Selection * primary_selection()
Returns the primary selection.
Definition: Fl_Text_Buffer.H:618
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:761
char * address(int pos)
Convert a byte offset in buffer into a memory address.
Definition: Fl_Text_Buffer.H:236
int start() const
Return the byte offset to the first selected character.
Definition: Fl_Text_Buffer.H:94
int highlight()
Returns the highlighted text.
Definition: Fl_Text_Buffer.H:420
int savefile(const char *file, int buflen=128 *1024)
Saves a text file from the current buffer.
Definition: Fl_Text_Buffer.H:323
void call_modify_callbacks()
Calls all modify callbacks that have been registered using the add_modify_callback() method...
Definition: Fl_Text_Buffer.H:460
char mCanUndo
if this buffer is used for attributes, it must not do any undo calls
Definition: Fl_Text_Buffer.H:759
void ** mCbArgs
caller arguments for modifyProcs above
Definition: Fl_Text_Buffer.H:752
const Fl_Text_Selection * primary_selection() const
Returns the primary selection.
Definition: Fl_Text_Buffer.H:613
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:754
const Fl_Text_Selection * secondary_selection() const
Returns the secondary selection.
Definition: Fl_Text_Buffer.H:623
Fl_Text_Selection mHighlight
highlighted areas
Definition: Fl_Text_Buffer.H:739
int mNPredeleteProcs
number of pre-delete procs attached
Definition: Fl_Text_Buffer.H:753
int loadfile(const char *file, int buflen=128 *1024)
Loads a text file into the buffer.
Definition: Fl_Text_Buffer.H:309
bool selected() const
Returns true if any text is selected.
Definition: Fl_Text_Buffer.H:107
int secondary_selected()
Returns a non 0 value if text has been selected in the secondary text selection, 0 otherwise...
Definition: Fl_Text_Buffer.H:382
char * mBuf
allocated memory where the text is stored
Definition: Fl_Text_Buffer.H:743
int input_file_was_transcoded
true iff the loaded file has been transcoded to UTF-8
Definition: Fl_Text_Buffer.H:652
int selected() const
Returns a non 0 value if text has been selected, 0 otherwise.
Definition: Fl_Text_Buffer.H:345
void ** mPredeleteCbArgs
caller argument for pre-delete proc above
Definition: Fl_Text_Buffer.H:756
int mGapEnd
points to the first char after the gap
Definition: Fl_Text_Buffer.H:745
int tab_distance() const
Gets the tab width.
Definition: Fl_Text_Buffer.H:329
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:477
int mEnd
byte offset to the character after the last selected character
Definition: Fl_Text_Buffer.H:132