• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

Fl_Text_Buffer.H

00001 //
00002 // "$Id$"
00003 //
00004 // Header file for Fl_Text_Buffer class.
00005 //
00006 // Copyright 2001-2016 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 characters 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 
00089   void update(int pos, int nDeleted, int nInserted);
00090 
00095   int start() const { return mStart; }
00096 
00101   int end() const { return mEnd; }
00102 
00108   bool selected() const { return mSelected; }
00109 
00114   void selected(bool b) { mSelected = b; }
00115 
00120   int includes(int pos) const;
00121 
00128   int position(int* start, int* end) const;
00129 
00130 protected:
00131 
00132   int mStart;         
00133   int mEnd;           
00134   bool mSelected;     
00135 };
00136 
00137 
00138 typedef void (*Fl_Text_Modify_Cb)(int pos, int nInserted, int nDeleted,
00139                                   int nRestyled, const char* deletedText,
00140                                   void* cbArg);
00141 
00142 
00143 typedef void (*Fl_Text_Predelete_Cb)(int pos, int nDeleted, void* cbArg);
00144 
00145 
00158 class FL_EXPORT Fl_Text_Buffer {
00159 public:
00160 
00169   Fl_Text_Buffer(int requestedSize = 0, int preferredGapSize = 1024);
00170 
00174   ~Fl_Text_Buffer();
00175 
00180   int length() const { return mLength; }
00181 
00188   char* text() const;
00189 
00194   void text(const char* text);
00195 
00206   char* text_range(int start, int end) const;
00207 
00214   unsigned int char_at(int pos) const;
00215 
00222   char byte_at(int pos) const;
00223 
00229   const char *address(int pos) const
00230   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00231 
00237   char *address(int pos)
00238   { return (pos < mGapStart) ? mBuf+pos : mBuf+pos+mGapEnd-mGapStart; }
00239 
00245   void insert(int pos, const char* text);
00246 
00251   void append(const char* t) { insert(length(), t); }
00252 
00258   void remove(int start, int end);
00259 
00267   void replace(int start, int end, const char *text);
00268 
00276   void copy(Fl_Text_Buffer* fromBuf, int fromStart, int fromEnd, int toPos);
00277 
00282   int undo(int *cp=0);
00283 
00287   void canUndo(char flag=1);
00288 
00304   int insertfile(const char *file, int pos, int buflen = 128*1024);
00305 
00309   int appendfile(const char *file, int buflen = 128*1024)
00310   { return insertfile(file, length(), buflen); }
00311 
00315   int loadfile(const char *file, int buflen = 128*1024)
00316   { select(0, length()); remove_selection(); return appendfile(file, buflen); }
00317 
00328   int outputfile(const char *file, int start, int end, int buflen = 128*1024);
00329 
00340   int savefile(const char *file, int buflen = 128*1024)
00341   { return outputfile(file, 0, length(), buflen); }
00342 
00349   int tab_distance() const { return mTabDist; }
00350 
00355   void tab_distance(int tabDist);
00356 
00360   void select(int start, int end);
00361 
00365   int selected() const { return mPrimary.selected(); }
00366 
00370   void unselect();
00371 
00375   int selection_position(int* start, int* end);
00376 
00382   char* selection_text();
00383 
00387   void remove_selection();
00388 
00392   void replace_selection(const char* text);
00393 
00397   void secondary_select(int start, int end);
00398 
00403   int secondary_selected() { return mSecondary.selected(); }
00404 
00408   void secondary_unselect();
00409 
00413   int secondary_selection_position(int* start, int* end);
00414 
00420   char* secondary_selection_text();
00421 
00426   void remove_secondary_selection();
00427 
00432   void replace_secondary_selection(const char* text);
00433 
00437   void highlight(int start, int end);
00438 
00444   int highlight() { return mHighlight.selected(); }
00445 
00449   void unhighlight();
00450 
00454   int highlight_position(int* start, int* end);
00455 
00461   char* highlight_text();
00462 
00474   void add_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00475 
00479   void remove_modify_callback(Fl_Text_Modify_Cb bufModifiedCB, void* cbArg);
00480 
00485   void call_modify_callbacks() { call_modify_callbacks(0, 0, 0, 0, 0); }
00486 
00490   void add_predelete_callback(Fl_Text_Predelete_Cb bufPredelCB, void* cbArg);
00491 
00496   void remove_predelete_callback(Fl_Text_Predelete_Cb predelCB, void* cbArg);
00497 
00502   void call_predelete_callbacks() { call_predelete_callbacks(0, 0); }
00503 
00512   char* line_text(int pos) const;
00513 
00519   int line_start(int pos) const;
00520 
00528   int line_end(int pos) const;
00529 
00535   int word_start(int pos) const;
00536 
00542   int word_end(int pos) const;
00543 
00551   int count_displayed_characters(int lineStartPos, int targetPos) const;
00552 
00562   int skip_displayed_characters(int lineStartPos, int nChars);
00563 
00568   int count_lines(int startPos, int endPos) const;
00569 
00574   int skip_lines(int startPos, int nLines);
00575 
00582   int rewind_lines(int startPos, int nLines);
00583 
00598   int findchar_forward(int startPos, unsigned searchChar, int* foundPos) const;
00599 
00613   int findchar_backward(int startPos, unsigned int searchChar, int* foundPos) const;
00614 
00626   int search_forward(int startPos, const char* searchString, int* foundPos,
00627                      int matchCase = 0) const;
00628 
00640   int search_backward(int startPos, const char* searchString, int* foundPos,
00641                       int matchCase = 0) const;
00642 
00646   const Fl_Text_Selection* primary_selection() const { return &mPrimary; }
00647 
00651   Fl_Text_Selection* primary_selection() { return &mPrimary; }
00652 
00656   const Fl_Text_Selection* secondary_selection() const { return &mSecondary; }
00657 
00661   const Fl_Text_Selection* highlight_selection() const { return &mHighlight; }
00662 
00667   int prev_char(int ix) const;
00668   int prev_char_clipped(int ix) const;
00669 
00674   int next_char(int ix) const;
00675   int next_char_clipped(int ix) const;
00676 
00680   int utf8_align(int) const;
00681 
00685   int input_file_was_transcoded;
00686 
00690   static const char* file_encoding_warning_message;
00691 
00701   void (*transcoding_warning_action)(Fl_Text_Buffer*);
00702 
00703 protected:
00704 
00709   void call_modify_callbacks(int pos, int nDeleted, int nInserted,
00710                              int nRestyled, const char* deletedText) const;
00711 
00716   void call_predelete_callbacks(int pos, int nDeleted) const;
00717 
00727   int insert_(int pos, const char* text);
00728 
00735   void remove_(int start, int end);
00736 
00741   void redisplay_selection(Fl_Text_Selection* oldSelection,
00742                            Fl_Text_Selection* newSelection) const;
00743 
00747   void move_gap(int pos);
00748 
00753   void reallocate_with_gap(int newGapStart, int newGapLen);
00754 
00755   char* selection_text_(Fl_Text_Selection* sel) const;
00756 
00760   void remove_selection_(Fl_Text_Selection* sel);
00761 
00765   void replace_selection_(Fl_Text_Selection* sel, const char* text);
00766 
00770   void update_selections(int pos, int nDeleted, int nInserted);
00771 
00772   Fl_Text_Selection mPrimary;     
00773   Fl_Text_Selection mSecondary;   
00774   Fl_Text_Selection mHighlight;   
00775   int mLength;                    
00778   char* mBuf;                     
00779   int mGapStart;                  
00780   int mGapEnd;                    
00781   // The hardware tab distance used by all displays for this buffer,
00782   // and used in computing offsets for rectangular selection operations.
00783   int mTabDist;                   
00784   int mNModifyProcs;              
00785   Fl_Text_Modify_Cb *mModifyProcs;
00787   void** mCbArgs;                 
00788   int mNPredeleteProcs;           
00789   Fl_Text_Predelete_Cb *mPredeleteProcs; 
00791   void **mPredeleteCbArgs;        
00792   int mCursorPosHint;             
00794   char mCanUndo;                  
00796   int mPreferredGapSize;          
00799 };
00800 
00801 #endif
00802 
00803 //
00804 // End of "$Id$".
00805 //
  • © 1998-2016 by Bill Spitzak and others.     FLTK

  • © 1998-2016 by Bill Spitzak and others.     FLTK

    Permission is granted to reproduce this manual or any portion for any purpose, provided this copyright and permission notice are preserved.