FLTK 1.3.2
Fl_Text_Buffer.H
00001 //
00002 // "$Id$"
00003 //
00004 // Header file for Fl_Text_Buffer class.
00005 //
00006 // Copyright 2001-2010 by Bill Spitzak and others.
00007 // Original code Copyright Mark Edel.  Permission to distribute under
00008 // the LGPL for the FLTK library granted by Mark Edel.
00009 //
00010 // Please report all bugs and problems on the following page:
00011 //
00012 //     http://www.fltk.org/str.php
00013 //
00014 // Please report all bugs and problems on the following page:
00015 //
00016 //     http://www.fltk.org/str.php
00017 //
00018 
00019 /* \file
00020  Fl_Text_Buffer, Fl_Text_Selection widget . */
00021 
00022 #ifndef FL_TEXT_BUFFER_H
00023 #define FL_TEXT_BUFFER_H
00024 
00025 
00026 #undef ASSERT_UTF8
00027 
00028 #ifdef ASSERT_UTF8
00029 # include <assert.h>
00030 # define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
00031 # define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
00032 #else
00033 # define IS_UTF8_ALIGNED(a)
00034 # define IS_UTF8_ALIGNED2(a, b)
00035 #endif
00036 
00037 
00038 /*
00039  "character size" is the size of a UTF-8 character in bytes
00040  "character width" is the width of a Unicode character in pixels 
00041  "column" was orginally defined as a character offset from the left margin. 
00042  It was identical to the byte offset. In UTF-8, we have neither a byte offset 
00043  nor truly fixed width fonts (*). Column could be a pixel value multiplied with
00044  an average character width (which is a bearable approximation).
00045  
00046  * in Unicode, there are no fixed width fonts! Even if the ASCII characters may 
00047    happen to be all the same width in pixels, chinese charcaters surely are not.
00048    There are plenty of exceptions, like ligatures, that make special handling of
00049    "fixed" character widths a nightmare. I decided to remove all references to
00050    fixed fonts and see "columns" as a multiple of the average width of a 
00051    character in the main font.
00052      - Matthias
00053  */
00054 
00055 
00056 /* Maximum length in characters of a tab or control character expansion
00057  of a single buffer character */
00058 #define FL_TEXT_MAX_EXP_CHAR_LEN 20
00059 
00060 #include "Fl_Export.H"
00061 
00062 
00069 class FL_EXPORT Fl_Text_Selection {
00070   friend class Fl_Text_Buffer;
00071   
00072 public:
00073   
00079   void set(int start, int end);
00080   
00088   void update(int pos, int nDeleted, int nInserted);
00089   
00094   int start() const { return mStart; }
00095   
00100   int end() const { return mEnd; }
00101   
00107   bool selected() const { return mSelected; }
00108   
00113   void selected(bool b) { mSelected = b; }
00114   
00119   int includes(int pos) const;
00120   
00127   int position(int* start, int* end) const;
00128   
00129 protected:
00130   
00131   int mStart;         
00132   int mEnd;           
00133   bool mSelected;     
00134 };
00135 
00136 
00137 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
00138                                   int nRestyled, const char* deletedText,
00139                                   void* cbArg);
00140 
00141 
00142 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
00143 
00144 
00157 class FL_EXPORT Fl_Text_Buffer {
00158 public:
00159 
00168   Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
00169   
00173   ~Fl_Text_Buffer();
00174   
00179   int length() const { return mLength; }
00180   
00187   char* text() const;
00188   
00193   void text(const char* text);
00194   
00205   char* text_range(int start, int end) const;
00206   
00213   unsigned int char_at(int pos) const;
00214   
00221   char byte_at(int pos) const;
00222   
00228   const char *address(int pos) const
00229   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00230 
00236   char *address(int pos)
00237   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00238   
00244   void insert(int pos, const char* text);
00245   
00250   void append(const char* t) { insert(length(), t); }
00251   
00257   void remove(int start, int end);
00258   
00265   void replace(int start, int end, const char *text);
00266   
00274   void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
00275   
00280   int undo(int *cp=0);
00281   
00285   void canUndo(char flag=1);
00286   
00298   int insertfile(const char *file, int pos, int buflen = 128*1024);
00299   
00303   int appendfile(const char *file, int buflen = 128*1024)
00304   { return insertfile(file, length(), buflen); }
00305   
00309   int loadfile(const char *file, int buflen = 128*1024)
00310   { select(0, length()); remove_selection(); return appendfile(file, buflen); }
00311   
00318   int outputfile(const char *file, int start, int end, int buflen = 128*1024);
00319   
00323   int savefile(const char *file, int buflen = 128*1024)
00324   { return outputfile(file, 0, length(), buflen); }
00325   
00329   int tab_distance() const { return mTabDist; }
00330   
00335   void tab_distance(int tabDist);
00336   
00340   void select(int start, int end);
00341   
00345   int selected() const { return mPrimary.selected(); }
00346   
00350   void unselect();
00351   
00355   int selection_position(int* start, int* end);
00356   
00361   char* selection_text();
00362 
00366   void remove_selection();
00367   
00371   void replace_selection(const char* text);
00372   
00376   void secondary_select(int start, int end);
00377   
00382   int secondary_selected() { return mSecondary.selected(); }
00383   
00387   void secondary_unselect();
00388   
00392   int secondary_selection_position(int* start, int* end);
00393   
00398   char* secondary_selection_text();
00399   
00403   void remove_secondary_selection();
00404   
00409   void replace_secondary_selection(const char* text);
00410   
00414   void highlight(int start, int end);
00415   
00420   int highlight() { return mHighlight.selected(); }
00421   
00425   void unhighlight();
00426 
00430   int highlight_position(int* start, int* end);
00431   
00436   char* highlight_text();
00437   
00448   void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00449   
00453   void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00454   
00460   void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
00461   
00465   void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
00466 
00471   void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
00472   
00477   void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
00478   
00486   char* line_text(int pos) const;
00487   
00493   int line_start(int pos) const;
00494   
00502   int line_end(int pos) const;
00503 
00509   int word_start(int pos) const;
00510 
00516   int word_end(int pos) const;
00517   
00524   int count_displayed_characters(int lineStartPos, int targetPos) const;
00525 
00534   int skip_displayed_characters(int lineStartPos, int nChars);
00535   
00540   int count_lines(int startPos, int endPos) const;
00541 
00546   int skip_lines(int startPos, int nLines);
00547   
00553   int rewind_lines(int startPos, int nLines);
00554 
00568   int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
00569   
00582   int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
00583   
00594   int search_forward(int startPos, const char* searchString, int* foundPos,
00595                      int matchCase = 0) const;
00596   
00607   int search_backward(int startPos, const char* searchString, int* foundPos,
00608                       int matchCase = 0) const;
00609   
00613   const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
00614   
00618   Fl_Text_Selection* primary_selection() { return &mPrimary; }
00619   
00623   const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
00624   
00628   const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
00629   
00634   int prev_char(int ix) const;
00635   int prev_char_clipped(int ix) const;
00636   
00641   int next_char(int ix) const;
00642   int next_char_clipped(int ix) const;
00643   
00647   int utf8_align(int) const;
00648   
00652   int input_file_was_transcoded;
00653 
00657   static const char* file_encoding_warning_message;
00658   
00668   void (*transcoding_warning_action)(Fl_Text_Buffer*);
00669   
00670 protected:
00671 
00676   void call_modify_callbacks(int pos, int nDeleted, int nInserted,
00677                              int nRestyled, const char* deletedText) const;
00678   
00683   void call_predelete_callbacks(int pos, int nDeleted) const;
00684   
00693   int insert_(int pos, const char* text);
00694   
00700   void remove_(int start, int end);
00701   
00706   void redisplay_selection(Fl_Text_Selection* oldSelection,
00707                            Fl_Text_Selection* newSelection) const;
00708   
00712   void move_gap(int pos);
00713   
00718   void reallocate_with_gap(int newGapStart, int newGapLen);
00719   
00720   char* selection_text_(Fl_Text_Selection* sel) const;
00721   
00725   void remove_selection_(Fl_Text_Selection* sel);
00726   
00730   void replace_selection_(Fl_Text_Selection* sel, const char* text);
00731   
00735   void update_selections(int pos, int nDeleted, int nInserted);
00736   
00737   Fl_Text_Selection mPrimary;     
00738   Fl_Text_Selection mSecondary;   
00739   Fl_Text_Selection mHighlight;   
00740   int mLength;                    
00743   char* mBuf;                     
00744   int mGapStart;                  
00745   int mGapEnd;                    
00746   // The hardware tab distance used by all displays for this buffer,
00747   // and used in computing offsets for rectangular selection operations.
00748   int mTabDist;                   
00749   int mNModifyProcs;              
00750   Fl_Text_Modify_Cb *mModifyProcs;
00752   void** mCbArgs;                 
00753   int mNPredeleteProcs;           
00754   Fl_Text_Predelete_Cb *mPredeleteProcs; 
00756   void **mPredeleteCbArgs;        
00757   int mCursorPosHint;             
00759   char mCanUndo;                  
00761   int mPreferredGapSize;          
00764 };
00765 
00766 #endif
00767 
00768 //
00769 // End of "$Id$".
00770 //