FLTK 1.3.0
|
00001 // 00002 // "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z matt $" 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 int position_to_xy(int pos, int* x, int* y) const; 00146 00147 int in_selection(int x, int y) const; 00148 void show_insert_position(); 00149 00150 int move_right(); 00151 int move_left(); 00152 int move_up(); 00153 int move_down(); 00154 int count_lines(int start, int end, bool start_pos_is_line_start) const; 00155 int line_start(int pos) const; 00156 int line_end(int startPos, bool startPosIsLineStart) const; 00157 int skip_lines(int startPos, int nLines, bool startPosIsLineStart); 00158 int rewind_lines(int startPos, int nLines); 00159 void next_word(void); 00160 void previous_word(void); 00161 00162 void show_cursor(int b = 1); 00163 00167 void hide_cursor() { show_cursor(0); } 00168 00169 void cursor_style(int style); 00170 00175 Fl_Color cursor_color() const {return mCursor_color;} 00176 00181 void cursor_color(Fl_Color n) {mCursor_color = n;} 00182 00187 int scrollbar_width() const { return scrollbar_width_; } 00188 00193 void scrollbar_width(int W) { scrollbar_width_ = W; } 00194 00199 Fl_Align scrollbar_align() const { return scrollbar_align_; } 00200 00205 void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; } 00206 00212 int word_start(int pos) const { return buffer()->word_start(pos); } 00213 00219 int word_end(int pos) const { return buffer()->word_end(pos); } 00220 00221 00222 void highlight_data(Fl_Text_Buffer *styleBuffer, 00223 const Style_Table_Entry *styleTable, 00224 int nStyles, char unfinishedStyle, 00225 Unfinished_Style_Cb unfinishedHighlightCB, 00226 void *cbArg); 00227 00228 int position_style(int lineStartPos, int lineLen, int lineIndex) const; 00229 00235 int shortcut() const {return shortcut_;} 00236 00242 void shortcut(int s) {shortcut_ = s;} 00243 00248 Fl_Font textfont() const {return textfont_;} 00249 00254 void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;} 00255 00260 Fl_Fontsize textsize() const {return textsize_;} 00261 00266 void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;} 00267 00272 Fl_Color textcolor() const {return textcolor_;} 00273 00278 void textcolor(Fl_Color n) {textcolor_ = n;} 00279 00280 int wrapped_column(int row, int column) const; 00281 int wrapped_row(int row) const; 00282 void wrap_mode(int wrap, int wrap_margin); 00283 00284 virtual void resize(int X, int Y, int W, int H); 00285 00291 double x_to_col(double x) const; 00292 00299 double col_to_x(double col) const; 00300 00301 protected: 00302 // Most (all?) of this stuff should only be called from resize() or 00303 // draw(). 00304 // Anything with "vline" indicates thats it deals with currently 00305 // visible lines. 00306 00307 virtual void draw(); 00308 void draw_text(int X, int Y, int W, int H); 00309 void draw_range(int start, int end); 00310 void draw_cursor(int, int); 00311 00312 void draw_string(int style, int x, int y, int toX, const char *string, 00313 int nChars) const; 00314 00315 void draw_vline(int visLineNum, int leftClip, int rightClip, 00316 int leftCharIndex, int rightCharIndex); 00317 00318 int find_x(const char *s, int len, int style, int x) const; 00319 00320 enum { 00321 DRAW_LINE, 00322 FIND_INDEX, 00323 FIND_INDEX_FROM_ZERO, 00324 GET_WIDTH 00325 }; 00326 00327 int handle_vline(int mode, 00328 int lineStart, int lineLen, int leftChar, int rightChar, 00329 int topClip, int bottomClip, 00330 int leftClip, int rightClip) const; 00331 00332 void draw_line_numbers(bool clearAll); 00333 00334 void clear_rect(int style, int x, int y, int width, int height) const; 00335 void display_insert(); 00336 00337 void offset_line_starts(int newTopLineNum); 00338 00339 void calc_line_starts(int startLine, int endLine); 00340 00341 void update_line_starts(int pos, int charsInserted, int charsDeleted, 00342 int linesInserted, int linesDeleted, int *scrolled); 00343 00344 void calc_last_char(); 00345 00346 int position_to_line( int pos, int* lineNum ) const; 00347 double string_width(const char* string, int length, int style) const; 00348 00349 static void scroll_timer_cb(void*); 00350 00351 static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg); 00352 static void buffer_modified_cb(int pos, int nInserted, int nDeleted, 00353 int nRestyled, const char* deletedText, 00354 void* cbArg); 00355 00356 static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d); 00357 static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d); 00358 void update_v_scrollbar(); 00359 void update_h_scrollbar(); 00360 int measure_vline(int visLineNum) const; 00361 int longest_vline() const; 00362 int empty_vlines() const; 00363 int vline_length(int visLineNum) const; 00364 int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const; 00365 00366 void xy_to_rowcol(int x, int y, int* row, int* column, 00367 int PosType = CHARACTER_POS) const; 00368 void maintain_absolute_top_line_number(int state); 00369 int get_absolute_top_line_number() const; 00370 void absolute_top_line_number(int oldFirstChar); 00371 int maintaining_absolute_top_line_number() const; 00372 void reset_absolute_top_line_number(); 00373 int position_to_linecol(int pos, int* lineNum, int* column) const; 00374 int scroll_(int topLineNum, int horizOffset); 00375 00376 void extend_range_for_styles(int* start, int* end); 00377 00378 void find_wrap_range(const char *deletedText, int pos, int nInserted, 00379 int nDeleted, int *modRangeStart, int *modRangeEnd, 00380 int *linesInserted, int *linesDeleted); 00381 void measure_deleted_lines(int pos, int nDeleted); 00382 void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos, 00383 int maxLines, bool startPosIsLineStart, 00384 int styleBufOffset, int *retPos, int *retLines, 00385 int *retLineStart, int *retLineEnd, 00386 bool countLastLineMissingNewLine = true) const; 00387 void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd, 00388 int *nextLineStart) const; 00389 double measure_proportional_character(const char *s, int colNum, int pos) const; 00390 int wrap_uses_character(int lineEndPos) const; 00391 00392 int damage_range1_start, damage_range1_end; 00393 int damage_range2_start, damage_range2_end; 00394 int mCursorPos; 00395 int mCursorOn; 00396 int mCursorOldY; /* Y pos. of cursor for blanking */ 00397 int mCursorToHint; /* Tells the buffer modified callback 00398 where to move the cursor, to reduce 00399 the number of redraw calls */ 00400 int mCursorStyle; /* One of enum cursorStyles above */ 00401 int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */ 00402 int mNVisibleLines; /* # of visible (displayed) lines */ 00403 int mNBufferLines; /* # of newlines in the buffer */ 00404 Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */ 00405 Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing 00406 color and font information */ 00407 int mFirstChar, mLastChar; /* Buffer positions of first and last 00408 displayed character (lastChar points 00409 either to a newline or one character 00410 beyond the end of the buffer) */ 00411 int mContinuousWrap; /* Wrap long lines when displaying */ 00412 int mWrapMarginPix; /* Margin in # of pixels for 00413 wrapping in continuousWrap mode */ 00414 int* mLineStarts; 00415 int mTopLineNum; /* Line number of top displayed line 00416 of file (first line of file is 1) */ 00417 int mAbsTopLineNum; /* In continuous wrap mode, the line 00418 number of the top line if the text 00419 were not wrapped (note that this is 00420 only maintained as needed). */ 00421 int mNeedAbsTopLineNum; /* Externally settable flag to continue 00422 maintaining absTopLineNum even if 00423 it isn't needed for line # display */ 00424 int mHorizOffset; /* Horizontal scroll pos. in pixels */ 00425 int mTopLineNumHint; /* Line number of top displayed line 00426 of file (first line of file is 1) */ 00427 int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */ 00428 int mNStyles; /* Number of entries in styleTable */ 00429 const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for 00430 coloring/syntax-highlighting */ 00431 char mUnfinishedStyle; /* Style buffer entry which triggers 00432 on-the-fly reparsing of region */ 00433 Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */ 00434 /* regions */ 00435 void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */ 00436 00437 int mMaxsize; 00438 00439 int mSuppressResync; /* Suppress resynchronization of line 00440 starts during buffer updates */ 00441 int mNLinesDeleted; /* Number of lines deleted during 00442 buffer modification (only used 00443 when resynchronization is suppressed) */ 00444 int mModifyingTabDistance; /* Whether tab distance is being 00445 modified */ 00446 00447 mutable double mColumnScale; /* Width in pixels of an average character. This 00448 value is calculated as needed (lazy eval); it 00449 needs to be mutable so that it can be calculated 00450 within a method marked as "const" */ 00451 00452 Fl_Color mCursor_color; 00453 00454 Fl_Scrollbar* mHScrollBar; 00455 Fl_Scrollbar* mVScrollBar; 00456 int scrollbar_width_; 00457 Fl_Align scrollbar_align_; 00458 int dragPos, dragType, dragging; 00459 int display_insert_position_hint; 00460 struct { int x, y, w, h; } text_area; 00461 00462 int shortcut_; 00463 00464 Fl_Font textfont_; 00465 Fl_Fontsize textsize_; 00466 Fl_Color textcolor_; 00467 00468 // The following are not presently used from the original NEdit code, 00469 // but are being put here so that future versions of Fl_Text_Display 00470 // can implement line numbers without breaking binary compatibility. 00471 00472 /* Line number margin and width */ 00473 int mLineNumLeft, mLineNumWidth; 00474 }; 00475 00476 #endif 00477 00478 // 00479 // End of "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z matt $". 00480 //