FLTK 1.3.3
Fl_Text_Buffer.H
1 //
2 // "$Id: Fl_Text_Buffer.H 9956 2013-08-12 15:46:57Z greg.ercolano $"
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;
757  int mCursorPosHint;
759  char mCanUndo;
761  int mPreferredGapSize;
764 };
765 
766 #endif
767 
768 //
769 // End of "$Id: Fl_Text_Buffer.H 9956 2013-08-12 15:46:57Z greg.ercolano $".
770 //