FLTK 1.3.2
|
00001 // 00002 // "$Id$" 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. Distribution and use rights are outlined in 00011 // the file "COPYING" which should have been included with this file. If this 00012 // file is missing or damaged, see the license at: 00013 // 00014 // http://www.fltk.org/COPYING.php 00015 // 00016 // Please report all bugs and problems on the following page: 00017 // 00018 // http://www.fltk.org/str.php 00019 // 00020 00021 /* \file 00022 Fl_Text_Display widget . */ 00023 00024 #ifndef FL_TEXT_DISPLAY_H 00025 #define FL_TEXT_DISPLAY_H 00026 00027 #include "fl_draw.H" 00028 #include "Fl_Group.H" 00029 #include "Fl_Widget.H" 00030 #include "Fl_Scrollbar.H" 00031 #include "Fl_Text_Buffer.H" 00032 00041 class FL_EXPORT Fl_Text_Display: public Fl_Group { 00042 00043 public: 00044 00048 enum { 00049 NORMAL_CURSOR, 00050 CARET_CURSOR, 00051 DIM_CURSOR, 00052 BLOCK_CURSOR, 00053 HEAVY_CURSOR, 00054 SIMPLE_CURSOR 00055 }; 00056 00062 enum { 00063 CURSOR_POS, 00064 CHARACTER_POS 00065 }; 00066 00072 enum { 00073 DRAG_NONE = -2, 00074 DRAG_START_DND = -1, 00075 DRAG_CHAR = 0, 00076 DRAG_WORD = 1, 00077 DRAG_LINE = 2 00078 }; 00079 00083 enum { 00084 WRAP_NONE, 00085 WRAP_AT_COLUMN, 00086 WRAP_AT_PIXEL, 00087 WRAP_AT_BOUNDS 00088 }; 00089 00090 friend void fl_text_drag_me(int pos, Fl_Text_Display* d); 00091 00092 typedef void (*Unfinished_Style_Cb)(int, void *); 00093 00098 struct Style_Table_Entry { 00099 Fl_Color color; 00100 Fl_Font font; 00101 Fl_Fontsize size; 00102 unsigned attr; 00103 }; 00104 00105 Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0); 00106 ~Fl_Text_Display(); 00107 00108 virtual int handle(int e); 00109 00110 void buffer(Fl_Text_Buffer* buf); 00111 00117 void buffer(Fl_Text_Buffer& buf) { buffer(&buf); } 00118 00124 Fl_Text_Buffer* buffer() const { return mBuffer; } 00125 00126 void redisplay_range(int start, int end); 00127 void scroll(int topLineNum, int horizOffset); 00128 void insert(const char* text); 00129 void overstrike(const char* text); 00130 void insert_position(int newPos); 00131 00136 int insert_position() const { return mCursorPos; } 00137 int position_to_xy(int pos, int* x, int* y) const; 00138 00139 int in_selection(int x, int y) const; 00140 void show_insert_position(); 00141 00142 int move_right(); 00143 int move_left(); 00144 int move_up(); 00145 int move_down(); 00146 int count_lines(int start, int end, bool start_pos_is_line_start) const; 00147 int line_start(int pos) const; 00148 int line_end(int startPos, bool startPosIsLineStart) const; 00149 int skip_lines(int startPos, int nLines, bool startPosIsLineStart); 00150 int rewind_lines(int startPos, int nLines); 00151 void next_word(void); 00152 void previous_word(void); 00153 00154 void show_cursor(int b = 1); 00155 00159 void hide_cursor() { show_cursor(0); } 00160 00161 void cursor_style(int style); 00162 00167 Fl_Color cursor_color() const {return mCursor_color;} 00168 00173 void cursor_color(Fl_Color n) {mCursor_color = n;} 00174 00179 int scrollbar_width() const { return scrollbar_width_; } 00180 00185 void scrollbar_width(int W) { scrollbar_width_ = W; } 00186 00191 Fl_Align scrollbar_align() const { return scrollbar_align_; } 00192 00197 void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } 00198 00204 int word_start(int pos) const { return buffer()->word_start(pos); } 00205 00211 int word_end(int pos) const { return buffer()->word_end(pos); } 00212 00213 00214 void highlight_data(Fl_Text_Buffer *styleBuffer, 00215 const Style_Table_Entry *styleTable, 00216 int nStyles, char unfinishedStyle, 00217 Unfinished_Style_Cb unfinishedHighlightCB, 00218 void *cbArg); 00219 00220 int position_style(int lineStartPos, int lineLen, int lineIndex) const; 00221 00227 int shortcut() const {return shortcut_;} 00228 00234 void shortcut(int s) {shortcut_ = s;} 00235 00240 Fl_Font textfont() const {return textfont_;} 00241 00246 void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;} 00247 00252 Fl_Fontsize textsize() const {return textsize_;} 00253 00258 void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;} 00259 00264 Fl_Color textcolor() const {return textcolor_;} 00265 00270 void textcolor(Fl_Color n) {textcolor_ = n;} 00271 00272 int wrapped_column(int row, int column) const; 00273 int wrapped_row(int row) const; 00274 void wrap_mode(int wrap, int wrap_margin); 00275 00276 virtual void resize(int X, int Y, int W, int H); 00277 00283 double x_to_col(double x) const; 00284 00291 double col_to_x(double col) const; 00292 00293 protected: 00294 // Most (all?) of this stuff should only be called from resize() or 00295 // draw(). 00296 // Anything with "vline" indicates thats it deals with currently 00297 // visible lines. 00298 00299 virtual void draw(); 00300 void draw_text(int X, int Y, int W, int H); 00301 void draw_range(int start, int end); 00302 void draw_cursor(int, int); 00303 00304 void draw_string(int style, int x, int y, int toX, const char *string, 00305 int nChars) const; 00306 00307 void draw_vline(int visLineNum, int leftClip, int rightClip, 00308 int leftCharIndex, int rightCharIndex); 00309 00310 int find_x(const char *s, int len, int style, int x) const; 00311 00312 enum { 00313 DRAW_LINE, 00314 FIND_INDEX, 00315 FIND_INDEX_FROM_ZERO, 00316 GET_WIDTH 00317 }; 00318 00319 int handle_vline(int mode, 00320 int lineStart, int lineLen, int leftChar, int rightChar, 00321 int topClip, int bottomClip, 00322 int leftClip, int rightClip) const; 00323 00324 void draw_line_numbers(bool clearAll); 00325 00326 void clear_rect(int style, int x, int y, int width, int height) const; 00327 void display_insert(); 00328 00329 void offset_line_starts(int newTopLineNum); 00330 00331 void calc_line_starts(int startLine, int endLine); 00332 00333 void update_line_starts(int pos, int charsInserted, int charsDeleted, 00334 int linesInserted, int linesDeleted, int *scrolled); 00335 00336 void calc_last_char(); 00337 00338 int position_to_line( int pos, int* lineNum ) const; 00339 double string_width(const char* string, int length, int style) const; 00340 00341 static void scroll_timer_cb(void*); 00342 00343 static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg); 00344 static void buffer_modified_cb(int pos, int nInserted, int nDeleted, 00345 int nRestyled, const char* deletedText, 00346 void* cbArg); 00347 00348 static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d); 00349 static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d); 00350 void update_v_scrollbar(); 00351 void update_h_scrollbar(); 00352 int measure_vline(int visLineNum) const; 00353 int longest_vline() const; 00354 int empty_vlines() const; 00355 int vline_length(int visLineNum) const; 00356 int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const; 00357 00358 void xy_to_rowcol(int x, int y, int* row, int* column, 00359 int PosType = CHARACTER_POS) const; 00360 void maintain_absolute_top_line_number(int state); 00361 int get_absolute_top_line_number() const; 00362 void absolute_top_line_number(int oldFirstChar); 00363 int maintaining_absolute_top_line_number() const; 00364 void reset_absolute_top_line_number(); 00365 int position_to_linecol(int pos, int* lineNum, int* column) const; 00366 int scroll_(int topLineNum, int horizOffset); 00367 00368 void extend_range_for_styles(int* start, int* end); 00369 00370 void find_wrap_range(const char *deletedText, int pos, int nInserted, 00371 int nDeleted, int *modRangeStart, int *modRangeEnd, 00372 int *linesInserted, int *linesDeleted); 00373 void measure_deleted_lines(int pos, int nDeleted); 00374 void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos, 00375 int maxLines, bool startPosIsLineStart, 00376 int styleBufOffset, int *retPos, int *retLines, 00377 int *retLineStart, int *retLineEnd, 00378 bool countLastLineMissingNewLine = true) const; 00379 void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd, 00380 int *nextLineStart) const; 00381 double measure_proportional_character(const char *s, int colNum, int pos) const; 00382 int wrap_uses_character(int lineEndPos) const; 00383 00384 int damage_range1_start, damage_range1_end; 00385 int damage_range2_start, damage_range2_end; 00386 int mCursorPos; 00387 int mCursorOn; 00388 int mCursorOldY; /* Y pos. of cursor for blanking */ 00389 int mCursorToHint; /* Tells the buffer modified callback 00390 where to move the cursor, to reduce 00391 the number of redraw calls */ 00392 int mCursorStyle; /* One of enum cursorStyles above */ 00393 int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */ 00394 int mNVisibleLines; /* # of visible (displayed) lines */ 00395 int mNBufferLines; /* # of newlines in the buffer */ 00396 Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */ 00397 Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing 00398 color and font information */ 00399 int mFirstChar, mLastChar; /* Buffer positions of first and last 00400 displayed character (lastChar points 00401 either to a newline or one character 00402 beyond the end of the buffer) */ 00403 int mContinuousWrap; /* Wrap long lines when displaying */ 00404 int mWrapMarginPix; /* Margin in # of pixels for 00405 wrapping in continuousWrap mode */ 00406 int* mLineStarts; 00407 int mTopLineNum; /* Line number of top displayed line 00408 of file (first line of file is 1) */ 00409 int mAbsTopLineNum; /* In continuous wrap mode, the line 00410 number of the top line if the text 00411 were not wrapped (note that this is 00412 only maintained as needed). */ 00413 int mNeedAbsTopLineNum; /* Externally settable flag to continue 00414 maintaining absTopLineNum even if 00415 it isn't needed for line # display */ 00416 int mHorizOffset; /* Horizontal scroll pos. in pixels */ 00417 int mTopLineNumHint; /* Line number of top displayed line 00418 of file (first line of file is 1) */ 00419 int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */ 00420 int mNStyles; /* Number of entries in styleTable */ 00421 const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for 00422 coloring/syntax-highlighting */ 00423 char mUnfinishedStyle; /* Style buffer entry which triggers 00424 on-the-fly reparsing of region */ 00425 Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */ 00426 /* regions */ 00427 void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */ 00428 00429 int mMaxsize; 00430 00431 int mSuppressResync; /* Suppress resynchronization of line 00432 starts during buffer updates */ 00433 int mNLinesDeleted; /* Number of lines deleted during 00434 buffer modification (only used 00435 when resynchronization is suppressed) */ 00436 int mModifyingTabDistance; /* Whether tab distance is being 00437 modified */ 00438 00439 mutable double mColumnScale; /* Width in pixels of an average character. This 00440 value is calculated as needed (lazy eval); it 00441 needs to be mutable so that it can be calculated 00442 within a method marked as "const" */ 00443 00444 Fl_Color mCursor_color; 00445 00446 Fl_Scrollbar* mHScrollBar; 00447 Fl_Scrollbar* mVScrollBar; 00448 int scrollbar_width_; 00449 Fl_Align scrollbar_align_; 00450 int dragPos, dragType, dragging; 00451 int display_insert_position_hint; 00452 struct { int x, y, w, h; } text_area; 00453 00454 int shortcut_; 00455 00456 Fl_Font textfont_; 00457 Fl_Fontsize textsize_; 00458 Fl_Color textcolor_; 00459 00460 // The following are not presently used from the original NEdit code, 00461 // but are being put here so that future versions of Fl_Text_Display 00462 // can implement line numbers without breaking binary compatibility. 00463 00464 /* Line number margin and width */ 00465 int mLineNumLeft, mLineNumWidth; 00466 }; 00467 00468 #endif 00469 00470 // 00471 // End of "$Id$". 00472 //