FLTK 1.3.x

Fl_Text_Display.H

00001 //
00002 // "$Id: Fl_Text_Display.H 7938 2010-12-02 09:46:01Z AlbrechtS $"
00003 //
00004 // Header file for Fl_Text_Display 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_Display widget . */
00032 
00033 #ifndef FL_TEXT_DISPLAY_H
00034 #define FL_TEXT_DISPLAY_H
00035 
00036 #include "fl_draw.H"
00037 #include "Fl_Group.H"
00038 #include "Fl_Widget.H"
00039 #include "Fl_Scrollbar.H"
00040 #include "Fl_Text_Buffer.H"
00041 
00050 class FL_EXPORT Fl_Text_Display: public Fl_Group 
00051 {
00052 public:
00053   
00057   enum {
00058     NORMAL_CURSOR,    
00059     CARET_CURSOR,     
00060     DIM_CURSOR,       
00061     BLOCK_CURSOR,     
00062     HEAVY_CURSOR      
00063   };
00064   
00070   enum {
00071     CURSOR_POS, 
00072     CHARACTER_POS
00073   };
00074   
00080   enum {
00081     DRAG_NONE = -2,
00082     DRAG_START_DND = -1,
00083     DRAG_CHAR = 0, 
00084     DRAG_WORD = 1, 
00085     DRAG_LINE = 2
00086   };
00087   
00091   enum {
00092     WRAP_NONE,      
00093     WRAP_AT_COLUMN, 
00094     WRAP_AT_PIXEL,  
00095     WRAP_AT_BOUNDS  
00096   };    
00097   
00098   friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
00099   
00100   typedef void (*Unfinished_Style_Cb)(int, void *);
00101   
00106   struct Style_Table_Entry {
00107     Fl_Color    color;
00108     Fl_Font     font;
00109     Fl_Fontsize size;
00110     unsigned    attr;
00111   };
00112   
00113   Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
00114   ~Fl_Text_Display();
00115   
00116   virtual int handle(int e);
00117   
00118   void buffer(Fl_Text_Buffer* buf);
00119   
00125   void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
00126   
00132   Fl_Text_Buffer* buffer() const { return mBuffer; }
00133   
00134   void redisplay_range(int start, int end);
00135   void scroll(int topLineNum, int horizOffset);
00136   void insert(const char* text);
00137   void overstrike(const char* text);
00138   void insert_position(int newPos);
00139   
00144   int insert_position() const { return mCursorPos; }
00145   
00146   int in_selection(int x, int y) const;
00147   void show_insert_position();
00148   
00149   int move_right();
00150   int move_left();
00151   int move_up();  
00152   int move_down();
00153   int count_lines(int start, int end, bool start_pos_is_line_start) const;
00154   int line_start(int pos) const;
00155   int line_end(int startPos, bool startPosIsLineStart) const;
00156   int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
00157   int rewind_lines(int startPos, int nLines);
00158   void next_word(void);
00159   void previous_word(void);
00160   
00161   void show_cursor(int b = 1);
00162   
00166   void hide_cursor() { show_cursor(0); }
00167   
00168   void cursor_style(int style);
00169   
00174   Fl_Color cursor_color() const {return mCursor_color;}
00175   
00180   void cursor_color(Fl_Color n) {mCursor_color = n;}
00181   
00186   int scrollbar_width() const { return scrollbar_width_; }
00187   
00192   void scrollbar_width(int W) { scrollbar_width_ = W; }
00193   
00198   Fl_Align scrollbar_align() const { return scrollbar_align_; }
00199   
00204   void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
00205   
00211   int word_start(int pos) const { return buffer()->word_start(pos); }
00212   
00218   int word_end(int pos) const { return buffer()->word_end(pos); }
00219   
00220   
00221   void highlight_data(Fl_Text_Buffer *styleBuffer,
00222                       const Style_Table_Entry *styleTable,
00223                       int nStyles, char unfinishedStyle,
00224                       Unfinished_Style_Cb unfinishedHighlightCB,
00225                       void *cbArg);
00226   
00227   int position_style(int lineStartPos, int lineLen, int lineIndex) const;
00228   
00234   int shortcut() const {return shortcut_;}
00235   
00241   void shortcut(int s) {shortcut_ = s;}
00242   
00247   Fl_Font textfont() const {return textfont_;}
00248   
00253   void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;}
00254   
00259   Fl_Fontsize textsize() const {return textsize_;}
00260   
00265   void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;}
00266   
00271   Fl_Color textcolor() const {return textcolor_;}
00272   
00277   void textcolor(Fl_Color n) {textcolor_ = n;}
00278   
00279   int wrapped_column(int row, int column) const;
00280   int wrapped_row(int row) const;
00281   void wrap_mode(int wrap, int wrap_margin);
00282   
00283   virtual void resize(int X, int Y, int W, int H);
00284 
00290   double x_to_col(double x) const;
00291   
00298   double col_to_x(double col) const;
00299   
00300 protected:
00301   // Most (all?) of this stuff should only be called from resize() or
00302   // draw().
00303   // Anything with "vline" indicates thats it deals with currently
00304   // visible lines.
00305   
00306   virtual void draw();
00307   void draw_text(int X, int Y, int W, int H);
00308   void draw_range(int start, int end);
00309   void draw_cursor(int, int);
00310   
00311   void draw_string(int style, int x, int y, int toX, const char *string,
00312                    int nChars) const;
00313   
00314   void draw_vline(int visLineNum, int leftClip, int rightClip,
00315                   int leftCharIndex, int rightCharIndex);
00316   
00317   int find_x(const char *s, int len, int style, int x) const;
00318   
00319   enum { 
00320     DRAW_LINE, 
00321     FIND_INDEX,
00322     FIND_INDEX_FROM_ZERO,
00323     GET_WIDTH 
00324   };
00325   
00326   int handle_vline(int mode, 
00327                    int lineStart, int lineLen, int leftChar, int rightChar,
00328                    int topClip, int bottomClip,
00329                    int leftClip, int rightClip) const;
00330   
00331   void draw_line_numbers(bool clearAll);
00332   
00333   void clear_rect(int style, int x, int y, int width, int height) const;
00334   void display_insert();
00335   
00336   void offset_line_starts(int newTopLineNum);
00337   
00338   void calc_line_starts(int startLine, int endLine);
00339   
00340   void update_line_starts(int pos, int charsInserted, int charsDeleted,
00341                           int linesInserted, int linesDeleted, int *scrolled);
00342   
00343   void calc_last_char();
00344   
00345   int position_to_line( int pos, int* lineNum ) const;
00346   double string_width(const char* string, int length, int style) const;
00347   
00348   static void scroll_timer_cb(void*);
00349   
00350   static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
00351   static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
00352                                  int nRestyled, const char* deletedText,
00353                                  void* cbArg);
00354   
00355   static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
00356   static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
00357   void update_v_scrollbar();
00358   void update_h_scrollbar();
00359   int measure_vline(int visLineNum) const;
00360   int longest_vline() const;
00361   int empty_vlines() const;
00362   int vline_length(int visLineNum) const;
00363   int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
00364   
00365   void xy_to_rowcol(int x, int y, int* row, int* column,
00366                     int PosType = CHARACTER_POS) const;
00367   
00368   int position_to_xy(int pos, int* x, int* y) const;
00369   void maintain_absolute_top_line_number(int state);
00370   int get_absolute_top_line_number() const;
00371   void absolute_top_line_number(int oldFirstChar);
00372   int maintaining_absolute_top_line_number() const;
00373   void reset_absolute_top_line_number();
00374   int position_to_linecol(int pos, int* lineNum, int* column) const;
00375   int scroll_(int topLineNum, int horizOffset);
00376   
00377   void extend_range_for_styles(int* start, int* end);
00378   
00379   void find_wrap_range(const char *deletedText, int pos, int nInserted,
00380                        int nDeleted, int *modRangeStart, int *modRangeEnd,
00381                        int *linesInserted, int *linesDeleted);
00382   void measure_deleted_lines(int pos, int nDeleted);
00383   void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
00384                             int maxLines, bool startPosIsLineStart,
00385                             int styleBufOffset, int *retPos, int *retLines,
00386                             int *retLineStart, int *retLineEnd,
00387                             bool countLastLineMissingNewLine = true) const;
00388   void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
00389                      int *nextLineStart) const;
00390   double measure_proportional_character(const char *s, int colNum, int pos) const;
00391   int wrap_uses_character(int lineEndPos) const;
00392   
00393   int damage_range1_start, damage_range1_end;
00394   int damage_range2_start, damage_range2_end;
00395   int mCursorPos;
00396   int mCursorOn;
00397   int mCursorOldY;              /* Y pos. of cursor for blanking */
00398   int mCursorToHint;            /* Tells the buffer modified callback
00399                                  where to move the cursor, to reduce
00400                                  the number of redraw calls */
00401   int mCursorStyle;             /* One of enum cursorStyles above */
00402   int mCursorPreferredXPos;     /* Pixel position for vert. cursor movement */
00403   int mNVisibleLines;           /* # of visible (displayed) lines */
00404   int mNBufferLines;            /* # of newlines in the buffer */
00405   Fl_Text_Buffer* mBuffer;      /* Contains text to be displayed */
00406   Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
00407                                  color and font information */
00408   int mFirstChar, mLastChar;    /* Buffer positions of first and last
00409                                  displayed character (lastChar points
00410                                  either to a newline or one character
00411                                  beyond the end of the buffer) */
00412   int mContinuousWrap;          /* Wrap long lines when displaying */
00413   int mWrapMarginPix;           /* Margin in # of pixels for
00414                                  wrapping in continuousWrap mode */
00415   int* mLineStarts;
00416   int mTopLineNum;              /* Line number of top displayed line
00417                                  of file (first line of file is 1) */
00418   int mAbsTopLineNum;           /* In continuous wrap mode, the line
00419                                   number of the top line if the text
00420                                   were not wrapped (note that this is
00421                                   only maintained as needed). */
00422   int mNeedAbsTopLineNum;       /* Externally settable flag to continue
00423                                  maintaining absTopLineNum even if
00424                                  it isn't needed for line # display */
00425   int mHorizOffset;             /* Horizontal scroll pos. in pixels */
00426   int mTopLineNumHint;          /* Line number of top displayed line
00427                                  of file (first line of file is 1) */
00428   int mHorizOffsetHint;         /* Horizontal scroll pos. in pixels */
00429   int mNStyles;                 /* Number of entries in styleTable */
00430   const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
00431                                          coloring/syntax-highlighting */
00432   char mUnfinishedStyle;        /* Style buffer entry which triggers
00433                                  on-the-fly reparsing of region */
00434   Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
00435   /* regions */
00436   void* mHighlightCBArg;        /* Arg to unfinishedHighlightCB */
00437   
00438   int mMaxsize;
00439   
00440   int mSuppressResync;          /* Suppress resynchronization of line
00441                                  starts during buffer updates */
00442   int mNLinesDeleted;           /* Number of lines deleted during
00443                                  buffer modification (only used
00444                                  when resynchronization is suppressed) */
00445   int mModifyingTabDistance;    /* Whether tab distance is being
00446                                  modified */
00447   
00448   mutable double mColumnScale; /* Width in pixels of an average character. This
00449                                  value is calculated as needed (lazy eval); it 
00450                                  needs to be mutable so that it can be calculated
00451                                  within a method marked as "const" */
00452   
00453   Fl_Color mCursor_color;
00454   
00455   Fl_Scrollbar* mHScrollBar;
00456   Fl_Scrollbar* mVScrollBar;
00457   int scrollbar_width_;
00458   Fl_Align scrollbar_align_;
00459   int dragPos, dragType, dragging;
00460   int display_insert_position_hint;
00461   struct { int x, y, w, h; } text_area;
00462   
00463   int shortcut_;
00464   
00465   Fl_Font textfont_;
00466   Fl_Fontsize textsize_;
00467   Fl_Color textcolor_;
00468   
00469   // The following are not presently used from the original NEdit code,
00470   // but are being put here so that future versions of Fl_Text_Display
00471   // can implement line numbers without breaking binary compatibility.
00472   
00473   /* Line number margin and width */
00474   int mLineNumLeft, mLineNumWidth;
00475 };
00476 
00477 #endif
00478 
00479 //
00480 // End of "$Id: Fl_Text_Display.H 7938 2010-12-02 09:46:01Z AlbrechtS $".
00481 //