FLTK 1.4.4

Loading...
Searching...
No Matches
Fl_Text_Buffer.H
1//
2// Header file for Fl_Text_Buffer class.
3//
4// Copyright 2001-2023 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_list */
26#include "fl_attr.h" /* Doxygen can't find <FL/fl_attr.h> */
27
28#undef ASSERT_UTF8
29
30#ifdef ASSERT_UTF8
31# include <assert.h>
32# define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
33# define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
34#else
35# define IS_UTF8_ALIGNED(a)
36# define IS_UTF8_ALIGNED2(a, b)
37#endif
38
39
40/*
41 "character size" is the size of a UTF-8 character in bytes
42 "character width" is the width of a Unicode character in pixels
43 "column" was originally defined as a character offset from the left margin.
44 It was identical to the byte offset. In UTF-8, we have neither a byte offset
45 nor truly fixed width fonts (*). Column could be a pixel value multiplied with
46 an average character width (which is a bearable approximation).
47
48 * in Unicode, there are no fixed width fonts! Even if the ASCII characters may
49 happen to be all the same width in pixels, Chinese characters surely are not.
50 There are plenty of exceptions, like ligatures, that make special handling of
51 "fixed" character widths a nightmare. I decided to remove all references to
52 fixed fonts and see "columns" as a multiple of the average width of a
53 character in the main font.
54 - Matthias
55 */
56
57
58/* Maximum length in characters of a tab or control character expansion
59 of a single buffer character */
60#define FL_TEXT_MAX_EXP_CHAR_LEN 20
61
62#include "Fl_Export.H"
63
64 class Fl_Text_Undo_Action_List;
65 class Fl_Text_Undo_Action;
66
100 class FL_EXPORT Fl_Text_Selection {
101 friend class Fl_Text_Buffer;
102
103 public:
104
105 // Sets the selection range and selected().
106 void set(int startpos, int endpos);
107
108 // Updates a selection after text was modified.
109 void update(int pos, int nDeleted, int nInserted);
110
122 int start() const { return mSelected ? mStart : 0; }
123
135 int end() const { return mSelected ? mEnd : 0; }
136
142 bool selected() const { return mSelected; }
143
148 void selected(bool b) { mSelected = b; }
149
163 int length() const { return mSelected ? mEnd - mStart : 0; }
164
165 // Returns true if position \p pos is in this Fl_Text_Selection.
166 int includes(int pos) const;
167
168 // Returns true if selected() and the positions of this selection.
169 int selected(int *startpos, int *endpos) const;
170 FL_DEPRECATED("since 1.4.0 - use selected(startpos, endpos) instead",
171 int position(int *startpos, int *endpos) const) { return selected(startpos, endpos); }
172
173 protected:
174
175 int mStart;
176 int mEnd;
177 bool mSelected;
178};
179
180
181 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
182 int nRestyled, const char* deletedText,
183 void* cbArg);
184
185
186 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
187
188
201 class FL_EXPORT Fl_Text_Buffer {
202 public:
203
212 Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
213
217 ~Fl_Text_Buffer();
218
223 int length() const { return mLength; }
224
231 char* text() const;
232
237 void text(const char* text);
238
249 char* text_range(int start, int end) const;
250
257 unsigned int char_at(int pos) const;
258
265 char byte_at(int pos) const;
266
272 const char *address(int pos) const
273 { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
274
280 char *address(int pos)
281 { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
282
289 void insert(int pos, const char* text, int insertedLength = -1);
290
296 void append(const char* t, int addedLength = -1) { insert(length(), t, addedLength); }
297
298 void vprintf(const char *fmt, va_list ap);
299 void printf(const char* fmt, ...);
300
306 void remove(int start, int end);
307
316 void replace(int start, int end, const char *text, int insertedLength = -1);
317
325 void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
326
331 int undo(int *cp=0);
332
337 bool can_undo() const;
338
342 int redo(int *cp=0);
343
348 bool can_redo() const;
349
357 void canUndo(char flag=1);
358
374 int insertfile(const char *file, int pos, int buflen = 128*1024);
375
379 int appendfile(const char *file, int buflen = 128*1024)
380 { return insertfile(file, length(), buflen); }
381
385 int loadfile(const char *file, int buflen = 128*1024)
386 { select(0, length()); remove_selection(); return appendfile(file, buflen); }
387
398 int outputfile(const char *file, int start, int end, int buflen = 128*1024);
399
410 int savefile(const char *file, int buflen = 128*1024)
411 { return outputfile(file, 0, length(), buflen); }
412
419 int tab_distance() const { return mTabDist; }
420
425 void tab_distance(int tabDist);
426
430 void select(int start, int end);
431
435 int selected() const { return mPrimary.selected(); }
436
440 void unselect();
441
445 int selection_position(int* start, int* end);
446
452 char* selection_text();
453
457 void remove_selection();
458
462 void replace_selection(const char* text);
463
467 void secondary_select(int start, int end);
468
473 int secondary_selected() { return mSecondary.selected(); }
474
478 void secondary_unselect();
479
483 int secondary_selection_position(int* start, int* end);
484
490 char* secondary_selection_text();
491
496 void remove_secondary_selection();
497
502 void replace_secondary_selection(const char* text);
503
507 void highlight(int start, int end);
508
512 int highlight() { return mHighlight.selected(); }
513
517 void unhighlight();
518
522 int highlight_position(int* start, int* end);
523
529 char* highlight_text();
530
542 void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
543
547 void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
548
553 void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
554
558 void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
559
564 void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
565
570 void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
571
580 char* line_text(int pos) const;
581
587 int line_start(int pos) const;
588
596 int line_end(int pos) const;
597
603 int word_start(int pos) const;
604
610 int word_end(int pos) const;
611
619 int count_displayed_characters(int lineStartPos, int targetPos) const;
620
630 int skip_displayed_characters(int lineStartPos, int nChars);
631
636 int count_lines(int startPos, int endPos) const;
637
643 int estimate_lines(int startPos, int endPos, int lineLen) const;
644
649 int skip_lines(int startPos, int nLines);
650
657 int rewind_lines(int startPos, int nLines);
658
673 int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
674
688 int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
689
701 int search_forward(int startPos, const char* searchString, int* foundPos,
702 int matchCase = 0) const;
703
715 int search_backward(int startPos, const char* searchString, int* foundPos,
716 int matchCase = 0) const;
717
721 const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
722
726 Fl_Text_Selection* primary_selection() { return &mPrimary; }
727
731 const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
732
736 const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
737
742 int prev_char(int ix) const;
743 int prev_char_clipped(int ix) const;
744
749 int next_char(int ix) const;
750 int next_char_clipped(int ix) const;
751
755 int utf8_align(int) const;
756
760 int input_file_was_transcoded;
761
765 static const char* file_encoding_warning_message;
766
776 void (*transcoding_warning_action)(Fl_Text_Buffer*);
777 bool is_word_separator(int pos) const;
778
779 protected:
780
785 void call_modify_callbacks(int pos, int nDeleted, int nInserted,
786 int nRestyled, const char* deletedText) const;
787
792 void call_predelete_callbacks(int pos, int nDeleted) const;
793
804 int insert_(int pos, const char* text, int insertedLength = -1);
805
812 void remove_(int start, int end);
813
818 void redisplay_selection(Fl_Text_Selection* oldSelection,
819 Fl_Text_Selection* newSelection) const;
820
824 void move_gap(int pos);
825
830 void reallocate_with_gap(int newGapStart, int newGapLen);
831
832 char* selection_text_(Fl_Text_Selection* sel) const;
833
837 void remove_selection_(Fl_Text_Selection* sel);
838
842 void replace_selection_(Fl_Text_Selection* sel, const char* text);
843
847 void update_selections(int pos, int nDeleted, int nInserted);
848
852 int apply_undo(Fl_Text_Undo_Action* action, int* cursorPos);
853
854 Fl_Text_Selection mPrimary;
855 Fl_Text_Selection mSecondary;
856 Fl_Text_Selection mHighlight;
857 int mLength;
860 char* mBuf;
861 int mGapStart;
862 int mGapEnd;
863 // The hardware tab distance used by all displays for this buffer,
864 // and used in computing offsets for rectangular selection operations.
865 int mTabDist;
866 int mNModifyProcs;
867 Fl_Text_Modify_Cb *mModifyProcs;
869 void** mCbArgs;
870 int mNPredeleteProcs;
871 Fl_Text_Predelete_Cb *mPredeleteProcs;
873 void **mPredeleteCbArgs;
874 int mCursorPosHint;
876 char mCanUndo;
878 int mPreferredGapSize;
881 Fl_Text_Undo_Action* mUndo;
882 Fl_Text_Undo_Action_List* mUndoList;
883 Fl_Text_Undo_Action_List* mRedoList;
884};
885
886#endif
Fl_Text_Buffer
This class manages Unicode text displayed in one or more Fl_Text_Display widgets.
Definition Fl_Text_Buffer.H:201
Fl_Text_Buffer::replace_selection_
void replace_selection_(Fl_Text_Selection *sel, const char *text)
Replaces the text in selection sel.
Definition Fl_Text_Buffer.cxx:1608
Fl_Text_Buffer::mSecondary
Fl_Text_Selection mSecondary
highlighted areas
Definition Fl_Text_Buffer.H:855
Fl_Text_Buffer::mCbArgs
void ** mCbArgs
caller arguments for modifyProcs above
Definition Fl_Text_Buffer.H:869
Fl_Text_Buffer::savefile
int savefile(const char *file, int buflen=128 *1024)
Saves a text file from the current buffer.
Definition Fl_Text_Buffer.H:410
Fl_Text_Buffer::text
char * text() const
Get a copy of the entire contents of the text buffer.
Definition Fl_Text_Buffer.cxx:261
Fl_Text_Buffer::mBuf
char * mBuf
allocated memory where the text is stored
Definition Fl_Text_Buffer.H:860
Fl_Text_Buffer::insertfile
int insertfile(const char *file, int pos, int buflen=128 *1024)
Inserts a file at the specified position.
Definition Fl_Text_Buffer.cxx:2007
Fl_Text_Buffer::remove_
void remove_(int start, int end)
Internal (non-redisplaying) version of remove().
Definition Fl_Text_Buffer.cxx:1453
Fl_Text_Buffer::length
int length() const
Returns the number of bytes in the buffer.
Definition Fl_Text_Buffer.H:223
Fl_Text_Buffer::primary_selection
const Fl_Text_Selection * primary_selection() const
Returns the primary selection.
Definition Fl_Text_Buffer.H:721
Fl_Text_Buffer::secondary_selected
int secondary_selected()
Returns a non-zero value if text has been selected in the secondary text selection,...
Definition Fl_Text_Buffer.H:473
Fl_Text_Buffer::secondary_selection
const Fl_Text_Selection * secondary_selection() const
Returns the secondary selection.
Definition Fl_Text_Buffer.H:731
Fl_Text_Buffer::select
void select(int start, int end)
Selects a range of characters in the buffer.
Definition Fl_Text_Buffer.cxx:717
Fl_Text_Buffer::selected
int selected() const
Returns a non-zero value if text has been selected, 0 otherwise.
Definition Fl_Text_Buffer.H:435
Fl_Text_Buffer::insert
void insert(int pos, const char *text, int insertedLength=-1)
Inserts null-terminated string text at position pos.
Definition Fl_Text_Buffer.cxx:383
Fl_Text_Buffer::append
void append(const char *t, int addedLength=-1)
Appends the text string to the end of the buffer.
Definition Fl_Text_Buffer.H:296
Fl_Text_Buffer::mRedoList
Fl_Text_Undo_Action_List * mRedoList
List of redo event.
Definition Fl_Text_Buffer.H:883
Fl_Text_Buffer::input_file_was_transcoded
int input_file_was_transcoded
true if the loaded file has been transcoded to UTF-8.
Definition Fl_Text_Buffer.H:760
Fl_Text_Buffer::remove_selection
void remove_selection()
Removes the text in the primary selection.
Definition Fl_Text_Buffer.cxx:762
Fl_Text_Buffer::address
const char * address(int pos) const
Convert a byte offset in buffer into a memory address.
Definition Fl_Text_Buffer.H:272
Fl_Text_Buffer::file_encoding_warning_message
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:765
Fl_Text_Buffer::move_gap
void move_gap(int pos)
Move the gap to start at a new position.
Definition Fl_Text_Buffer.cxx:1712
Fl_Text_Buffer::mNModifyProcs
int mNModifyProcs
number of modify-redisplay procs attached
Definition Fl_Text_Buffer.H:866
Fl_Text_Buffer::mUndoList
Fl_Text_Undo_Action_List * mUndoList
List of undo event.
Definition Fl_Text_Buffer.H:882
Fl_Text_Buffer::highlight_selection
const Fl_Text_Selection * highlight_selection() const
Returns the current highlight selection.
Definition Fl_Text_Buffer.H:736
Fl_Text_Buffer::Fl_Text_Buffer
Fl_Text_Buffer(int requestedSize=0, int preferredGapSize=1024)
Create an empty text buffer of a pre-determined size.
Definition Fl_Text_Buffer.cxx:207
Fl_Text_Buffer::mGapStart
int mGapStart
points to the first character of the gap
Definition Fl_Text_Buffer.H:861
Fl_Text_Buffer::mCursorPosHint
int mCursorPosHint
hint for reasonable cursor position after a buffer modification operation
Definition Fl_Text_Buffer.H:874
Fl_Text_Buffer::apply_undo
int apply_undo(Fl_Text_Undo_Action *action, int *cursorPos)
Apply the current undo/redo operation, called from undo() or redo().
Definition Fl_Text_Buffer.cxx:563
Fl_Text_Buffer::mUndo
Fl_Text_Undo_Action * mUndo
local undo event
Definition Fl_Text_Buffer.H:881
Fl_Text_Buffer::appendfile
int appendfile(const char *file, int buflen=128 *1024)
Appends the named file to the end of the buffer.
Definition Fl_Text_Buffer.H:379
Fl_Text_Buffer::call_predelete_callbacks
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:570
Fl_Text_Buffer::call_modify_callbacks
void call_modify_callbacks()
Calls all modify callbacks that have been registered using the add_modify_callback() method.
Definition Fl_Text_Buffer.H:553
Fl_Text_Buffer::mHighlight
Fl_Text_Selection mHighlight
highlighted areas
Definition Fl_Text_Buffer.H:856
Fl_Text_Buffer::highlight
int highlight()
Returns a non-zero value if text has been highlighted, 0 otherwise.
Definition Fl_Text_Buffer.H:512
Fl_Text_Buffer::redisplay_selection
void redisplay_selection(Fl_Text_Selection *oldSelection, Fl_Text_Selection *newSelection) const
Calls the stored redisplay procedure(s) for this buffer to update the screen for a change in a select...
Definition Fl_Text_Buffer.cxx:1656
Fl_Text_Buffer::mGapEnd
int mGapEnd
points to the first character after the gap
Definition Fl_Text_Buffer.H:862
Fl_Text_Buffer::outputfile
int outputfile(const char *file, int start, int end, int buflen=128 *1024)
Writes the specified portions of the text buffer to a file.
Definition Fl_Text_Buffer.cxx:2048
Fl_Text_Buffer::insert_
int insert_(int pos, const char *text, int insertedLength=-1)
Internal (non-redisplaying) version of insert().
Definition Fl_Text_Buffer.cxx:1400
Fl_Text_Buffer::mPredeleteProcs
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:871
Fl_Text_Buffer::mPredeleteCbArgs
void ** mPredeleteCbArgs
caller argument for pre-delete proc above
Definition Fl_Text_Buffer.H:873
Fl_Text_Buffer::address
char * address(int pos)
Convert a byte offset in buffer into a memory address.
Definition Fl_Text_Buffer.H:280
Fl_Text_Buffer::mPrimary
Fl_Text_Selection mPrimary
highlighted areas
Definition Fl_Text_Buffer.H:854
Fl_Text_Buffer::primary_selection
Fl_Text_Selection * primary_selection()
Returns the primary selection.
Definition Fl_Text_Buffer.H:726
Fl_Text_Buffer::is_word_separator
bool is_word_separator(int pos) const
Returns whether character at position pos is a word separator.
Definition Fl_Text_Buffer.cxx:1063
Fl_Text_Buffer::mNPredeleteProcs
int mNPredeleteProcs
number of pre-delete procs attached
Definition Fl_Text_Buffer.H:870
Fl_Text_Buffer::mPreferredGapSize
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:878
Fl_Text_Buffer::mModifyProcs
Fl_Text_Modify_Cb * mModifyProcs
procedures to call when buffer is modified to redisplay contents
Definition Fl_Text_Buffer.H:867
Fl_Text_Buffer::transcoding_warning_action
void(* transcoding_warning_action)(Fl_Text_Buffer *)
Pointer to a function called after reading a non UTF-8 encoded file.
Definition Fl_Text_Buffer.H:776
Fl_Text_Buffer::mCanUndo
char mCanUndo
if this buffer is used for attributes, it must not do any undo calls
Definition Fl_Text_Buffer.H:876
Fl_Text_Buffer::remove_selection_
void remove_selection_(Fl_Text_Selection *sel)
Removes the text from the buffer corresponding to sel.
Definition Fl_Text_Buffer.cxx:1593
Fl_Text_Buffer::loadfile
int loadfile(const char *file, int buflen=128 *1024)
Loads a text file into the buffer.
Definition Fl_Text_Buffer.H:385
Fl_Text_Buffer::mTabDist
int mTabDist
equiv.
Definition Fl_Text_Buffer.H:865
Fl_Text_Buffer::update_selections
void update_selections(int pos, int nDeleted, int nInserted)
Updates all of the selections in the buffer for changes in the buffer's text.
Definition Fl_Text_Buffer.cxx:1758
Fl_Text_Buffer::tab_distance
int tab_distance() const
Gets the tab width.
Definition Fl_Text_Buffer.H:419
Fl_Text_Buffer::reallocate_with_gap
void reallocate_with_gap(int newGapStart, int newGapLen)
Reallocates the text storage in the buffer to have a gap starting at newGapStart and a gap size of ne...
Definition Fl_Text_Buffer.cxx:1729
Fl_Text_Buffer::mLength
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:857
Fl_Text_Selection
This is an internal class for Fl_Text_Buffer to manage text selections.
Definition Fl_Text_Buffer.H:100
Fl_Text_Selection::position
int position(int *startpos, int *endpos) const
Definition Fl_Text_Buffer.H:171
Fl_Text_Selection::end
int end() const
Returns the byte offset to the character after the last selected character.
Definition Fl_Text_Buffer.H:135
Fl_Text_Selection::selected
bool selected() const
Returns true if any text is selected.
Definition Fl_Text_Buffer.H:142
Fl_Text_Selection::selected
void selected(bool b)
Modifies the 'selected' flag.
Definition Fl_Text_Buffer.H:148
Fl_Text_Selection::mSelected
bool mSelected
this flag is set if any text is selected
Definition Fl_Text_Buffer.H:177
Fl_Text_Selection::mEnd
int mEnd
byte offset to the character after the last selected character
Definition Fl_Text_Buffer.H:176
Fl_Text_Selection::start
int start() const
Returns the byte offset to the first selected character.
Definition Fl_Text_Buffer.H:122
Fl_Text_Selection::update
void update(int pos, int nDeleted, int nInserted)
Updates a selection after text was modified.
Definition Fl_Text_Buffer.cxx:1778
Fl_Text_Selection::mStart
int mStart
byte offset to the first selected character
Definition Fl_Text_Buffer.H:175
Fl_Text_Selection::length
int length() const
Returns the size in bytes of the selection.
Definition Fl_Text_Buffer.H:163
Fl_Text_Selection::set
void set(int startpos, int endpos)
Sets the selection range.
Definition Fl_Text_Buffer.cxx:1517
fl_attr.h
This file defines compiler-specific macros.
FL_DEPRECATED
#define FL_DEPRECATED(msg, func)
Enclosing a function or method in FL_DEPRECATED marks it as no longer recommended.
Definition fl_attr.h:57