FLTK 1.3.0
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 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Library General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Library General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Library General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA.
00024 //
00025 // Please report all bugs and problems on the following page:
00026 //
00027 //     http://www.fltk.org/str.php
00028 //
00029 
00030 /* \file
00031  Fl_Text_Buffer, Fl_Text_Selection widget . */
00032 
00033 #ifndef FL_TEXT_BUFFER_H
00034 #define FL_TEXT_BUFFER_H
00035 
00036 
00037 #undef ASSERT_UTF8
00038 
00039 #ifdef ASSERT_UTF8
00040 # include <assert.h>
00041 # define IS_UTF8_ALIGNED(a) if (a && *a) assert(fl_utf8len(*(a))>0);
00042 # define IS_UTF8_ALIGNED2(a, b) if (b>=0 && b<a->length()) assert(fl_utf8len(a->byte_at(b))>0);
00043 #else
00044 # define IS_UTF8_ALIGNED(a)
00045 # define IS_UTF8_ALIGNED2(a, b)
00046 #endif
00047 
00048 
00049 /*
00050  "character size" is the size of a UTF-8 character in bytes
00051  "character width" is the width of a Unicode character in pixels 
00052  "column" was orginally defined as a character offset from the left margin. 
00053  It was identical to the byte offset. In UTF-8, we have neither a byte offset 
00054  nor truly fixed width fonts (*). Column could be a pixel value multiplied with
00055  an average character width (which is a bearable approximation).
00056  
00057  * in Unicode, there are no fixed width fonts! Even if the ASCII characters may 
00058    happen to be all the same width in pixels, chinese charcaters surely are not.
00059    There are plenty of exceptions, like ligatures, that make special handling of
00060    "fixed" character widths a nightmare. I decided to remove all references to
00061    fixed fonts and see "columns" as a multiple of the average width of a 
00062    character in the main font.
00063      - Matthias
00064  */
00065 
00066 
00067 /* Maximum length in characters of a tab or control character expansion
00068  of a single buffer character */
00069 #define FL_TEXT_MAX_EXP_CHAR_LEN 20
00070 
00071 #include "Fl_Export.H"
00072 
00073 
00080 class FL_EXPORT Fl_Text_Selection {
00081   friend class Fl_Text_Buffer;
00082   
00083 public:
00084   
00090   void set(int start, int end);
00091   
00099   void update(int pos, int nDeleted, int nInserted);
00100   
00105   int start() const { return mStart; }
00106   
00111   int end() const { return mEnd; }
00112   
00118   bool selected() const { return mSelected; }
00119   
00124   void selected(bool b) { mSelected = b; }
00125   
00130   int includes(int pos) const;
00131   
00138   int position(int* start, int* end) const;
00139   
00140 protected:
00141   
00142   int mStart;         
00143   int mEnd;           
00144   bool mSelected;     
00145 };
00146 
00147 
00148 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
00149                                   int nRestyled, const char* deletedText,
00150                                   void* cbArg);
00151 
00152 
00153 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
00154 
00155 
00168 class FL_EXPORT Fl_Text_Buffer {
00169 public:
00170 
00179   Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
00180   
00184   ~Fl_Text_Buffer();
00185   
00190   int length() const { return mLength; }
00191   
00198   char* text() const;
00199   
00204   void text(const char* text);
00205   
00216   char* text_range(int start, int end) const;
00217   
00224   unsigned int char_at(int pos) const;
00225   
00232   char byte_at(int pos) const;
00233   
00239   const char *address(int pos) const
00240   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00241 
00247   char *address(int pos)
00248   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00249   
00255   void insert(int pos, const char* text);
00256   
00261   void append(const char* t) { insert(length(), t); }
00262   
00268   void remove(int start, int end);
00269   
00276   void replace(int start, int end, const char *text);
00277   
00285   void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
00286   
00291   int undo(int *cp=0);
00292   
00296   void canUndo(char flag=1);
00297   
00309   int insertfile(const char *file, int pos, int buflen = 128*1024);
00310   
00314   int appendfile(const char *file, int buflen = 128*1024)
00315   { return insertfile(file, length(), buflen); }
00316   
00320   int loadfile(const char *file, int buflen = 128*1024)
00321   { select(0, length()); remove_selection(); return appendfile(file, buflen); }
00322   
00329   int outputfile(const char *file, int start, int end, int buflen = 128*1024);
00330   
00334   int savefile(const char *file, int buflen = 128*1024)
00335   { return outputfile(file, 0, length(), buflen); }
00336   
00340   int tab_distance() const { return mTabDist; }
00341   
00346   void tab_distance(int tabDist);
00347   
00351   void select(int start, int end);
00352   
00356   int selected() const { return mPrimary.selected(); }
00357   
00361   void unselect();
00362   
00366   int selection_position(int* start, int* end);
00367   
00372   char* selection_text();
00373 
00377   void remove_selection();
00378   
00382   void replace_selection(const char* text);
00383   
00387   void secondary_select(int start, int end);
00388   
00393   int secondary_selected() { return mSecondary.selected(); }
00394   
00398   void secondary_unselect();
00399   
00403   int secondary_selection_position(int* start, int* end);
00404   
00409   char* secondary_selection_text();
00410   
00414   void remove_secondary_selection();
00415   
00420   void replace_secondary_selection(const char* text);
00421   
00425   void highlight(int start, int end);
00426   
00431   int highlight() { return mHighlight.selected(); }
00432   
00436   void unhighlight();
00437 
00441   int highlight_position(int* start, int* end);
00442   
00447   char* highlight_text();
00448   
00459   void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00460   
00464   void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00465   
00471   void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
00472   
00476   void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
00477 
00482   void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
00483   
00488   void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
00489   
00497   char* line_text(int pos) const;
00498   
00504   int line_start(int pos) const;
00505   
00513   int line_end(int pos) const;
00514 
00520   int word_start(int pos) const;
00521 
00527   int word_end(int pos) const;
00528   
00535   int count_displayed_characters(int lineStartPos, int targetPos) const;
00536 
00545   int skip_displayed_characters(int lineStartPos, int nChars);
00546   
00551   int count_lines(int startPos, int endPos) const;
00552 
00557   int skip_lines(int startPos, int nLines);
00558   
00564   int rewind_lines(int startPos, int nLines);
00565 
00579   int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
00580   
00593   int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
00594   
00605   int search_forward(int startPos, const char* searchString, int* foundPos,
00606                      int matchCase = 0) const;
00607   
00618   int search_backward(int startPos, const char* searchString, int* foundPos,
00619                       int matchCase = 0) const;
00620   
00624   const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
00625   
00629   Fl_Text_Selection* primary_selection() { return &mPrimary; }
00630   
00634   const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
00635   
00639   const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
00640   
00645   int prev_char(int ix) const;
00646   int prev_char_clipped(int ix) const;
00647   
00652   int next_char(int ix) const;
00653   int next_char_clipped(int ix) const;
00654   
00658   int utf8_align(int) const;
00659   
00663   int input_file_was_transcoded;
00664 
00668   static const char* file_encoding_warning_message;
00669   
00679   void (*transcoding_warning_action)(Fl_Text_Buffer*);
00680   
00681 protected:
00682 
00687   void call_modify_callbacks(int pos, int nDeleted, int nInserted,
00688                              int nRestyled, const char* deletedText) const;
00689   
00694   void call_predelete_callbacks(int pos, int nDeleted) const;
00695   
00704   int insert_(int pos, const char* text);
00705   
00711   void remove_(int start, int end);
00712   
00717   void redisplay_selection(Fl_Text_Selection* oldSelection,
00718                            Fl_Text_Selection* newSelection) const;
00719   
00723   void move_gap(int pos);
00724   
00729   void reallocate_with_gap(int newGapStart, int newGapLen);
00730   
00731   char* selection_text_(Fl_Text_Selection* sel) const;
00732   
00736   void remove_selection_(Fl_Text_Selection* sel);
00737   
00741   void replace_selection_(Fl_Text_Selection* sel, const char* text);
00742   
00746   void update_selections(int pos, int nDeleted, int nInserted);
00747   
00748   Fl_Text_Selection mPrimary;     
00749   Fl_Text_Selection mSecondary;   
00750   Fl_Text_Selection mHighlight;   
00751   int mLength;                    
00754   char* mBuf;                     
00755   int mGapStart;                  
00756   int mGapEnd;                    
00757   // The hardware tab distance used by all displays for this buffer,
00758   // and used in computing offsets for rectangular selection operations.
00759   int mTabDist;                   
00760   int mNModifyProcs;              
00761   Fl_Text_Modify_Cb *mModifyProcs;
00763   void** mCbArgs;                 
00764   int mNPredeleteProcs;           
00765   Fl_Text_Predelete_Cb *mPredeleteProcs; 
00767   void **mPredeleteCbArgs;        
00768   int mCursorPosHint;             
00770   char mCanUndo;                  
00772   int mPreferredGapSize;          
00775 };
00776 
00777 #endif
00778 
00779 //
00780 // End of "$Id$".
00781 //