FLTK 1.3.0
Fl_Text_Display.H
1 //
2 // "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z matt $"
3 //
4 // Header file for Fl_Text_Display class.
5 //
6 // Copyright 2001-2010 by Bill Spitzak and others.
7 // Original code Copyright Mark Edel. Permission to distribute under
8 // the LGPL for the FLTK library granted by Mark Edel.
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA.
24 //
25 // Please report all bugs and problems on the following page:
26 //
27 // http://www.fltk.org/str.php
28 //
29 
30 /* \file
31  Fl_Text_Display widget . */
32 
33 #ifndef FL_TEXT_DISPLAY_H
34 #define FL_TEXT_DISPLAY_H
35 
36 #include "fl_draw.H"
37 #include "Fl_Group.H"
38 #include "Fl_Widget.H"
39 #include "Fl_Scrollbar.H"
40 #include "Fl_Text_Buffer.H"
41 
50 class FL_EXPORT Fl_Text_Display: public Fl_Group {
51 
52 public:
53 
57  enum {
62  HEAVY_CURSOR
63  };
64 
70  enum {
71  CURSOR_POS,
72  CHARACTER_POS
73  };
74 
80  enum {
81  DRAG_NONE = -2,
82  DRAG_START_DND = -1,
83  DRAG_CHAR = 0,
84  DRAG_WORD = 1,
85  DRAG_LINE = 2
86  };
87 
91  enum {
95  WRAP_AT_BOUNDS
96  };
97 
98  friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
99 
100  typedef void (*Unfinished_Style_Cb)(int, void *);
101 
107  Fl_Color color;
108  Fl_Font font;
109  Fl_Fontsize size;
110  unsigned attr;
111  };
112 
113  Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
114  ~Fl_Text_Display();
115 
116  virtual int handle(int e);
117 
118  void buffer(Fl_Text_Buffer* buf);
119 
125  void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
126 
132  Fl_Text_Buffer* buffer() const { return mBuffer; }
133 
134  void redisplay_range(int start, int end);
135  void scroll(int topLineNum, int horizOffset);
136  void insert(const char* text);
137  void overstrike(const char* text);
138  void insert_position(int newPos);
139 
144  int insert_position() const { return mCursorPos; }
145  int position_to_xy(int pos, int* x, int* y) const;
146 
147  int in_selection(int x, int y) const;
148  void show_insert_position();
149 
150  int move_right();
151  int move_left();
152  int move_up();
153  int move_down();
154  int count_lines(int start, int end, bool start_pos_is_line_start) const;
155  int line_start(int pos) const;
156  int line_end(int startPos, bool startPosIsLineStart) const;
157  int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
158  int rewind_lines(int startPos, int nLines);
159  void next_word(void);
160  void previous_word(void);
161 
162  void show_cursor(int b = 1);
163 
167  void hide_cursor() { show_cursor(0); }
168 
169  void cursor_style(int style);
170 
175  Fl_Color cursor_color() const {return mCursor_color;}
176 
181  void cursor_color(Fl_Color n) {mCursor_color = n;}
182 
187  int scrollbar_width() const { return scrollbar_width_; }
188 
193  void scrollbar_width(int W) { scrollbar_width_ = W; }
194 
199  Fl_Align scrollbar_align() const { return scrollbar_align_; }
200 
205  void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
206 
212  int word_start(int pos) const { return buffer()->word_start(pos); }
213 
219  int word_end(int pos) const { return buffer()->word_end(pos); }
220 
221 
222  void highlight_data(Fl_Text_Buffer *styleBuffer,
223  const Style_Table_Entry *styleTable,
224  int nStyles, char unfinishedStyle,
225  Unfinished_Style_Cb unfinishedHighlightCB,
226  void *cbArg);
227 
228  int position_style(int lineStartPos, int lineLen, int lineIndex) const;
229 
235  int shortcut() const {return shortcut_;}
236 
242  void shortcut(int s) {shortcut_ = s;}
243 
248  Fl_Font textfont() const {return textfont_;}
249 
254  void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;}
255 
260  Fl_Fontsize textsize() const {return textsize_;}
261 
266  void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;}
267 
272  Fl_Color textcolor() const {return textcolor_;}
273 
278  void textcolor(Fl_Color n) {textcolor_ = n;}
279 
280  int wrapped_column(int row, int column) const;
281  int wrapped_row(int row) const;
282  void wrap_mode(int wrap, int wrap_margin);
283 
284  virtual void resize(int X, int Y, int W, int H);
285 
291  double x_to_col(double x) const;
292 
299  double col_to_x(double col) const;
300 
301 protected:
302  // Most (all?) of this stuff should only be called from resize() or
303  // draw().
304  // Anything with "vline" indicates thats it deals with currently
305  // visible lines.
306 
307  virtual void draw();
308  void draw_text(int X, int Y, int W, int H);
309  void draw_range(int start, int end);
310  void draw_cursor(int, int);
311 
312  void draw_string(int style, int x, int y, int toX, const char *string,
313  int nChars) const;
314 
315  void draw_vline(int visLineNum, int leftClip, int rightClip,
316  int leftCharIndex, int rightCharIndex);
317 
318  int find_x(const char *s, int len, int style, int x) const;
319 
320  enum {
321  DRAW_LINE,
322  FIND_INDEX,
323  FIND_INDEX_FROM_ZERO,
324  GET_WIDTH
325  };
326 
327  int handle_vline(int mode,
328  int lineStart, int lineLen, int leftChar, int rightChar,
329  int topClip, int bottomClip,
330  int leftClip, int rightClip) const;
331 
332  void draw_line_numbers(bool clearAll);
333 
334  void clear_rect(int style, int x, int y, int width, int height) const;
335  void display_insert();
336 
337  void offset_line_starts(int newTopLineNum);
338 
339  void calc_line_starts(int startLine, int endLine);
340 
341  void update_line_starts(int pos, int charsInserted, int charsDeleted,
342  int linesInserted, int linesDeleted, int *scrolled);
343 
344  void calc_last_char();
345 
346  int position_to_line( int pos, int* lineNum ) const;
347  double string_width(const char* string, int length, int style) const;
348 
349  static void scroll_timer_cb(void*);
350 
351  static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
352  static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
353  int nRestyled, const char* deletedText,
354  void* cbArg);
355 
356  static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
357  static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
358  void update_v_scrollbar();
359  void update_h_scrollbar();
360  int measure_vline(int visLineNum) const;
361  int longest_vline() const;
362  int empty_vlines() const;
363  int vline_length(int visLineNum) const;
364  int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
365 
366  void xy_to_rowcol(int x, int y, int* row, int* column,
367  int PosType = CHARACTER_POS) const;
368  void maintain_absolute_top_line_number(int state);
369  int get_absolute_top_line_number() const;
370  void absolute_top_line_number(int oldFirstChar);
371  int maintaining_absolute_top_line_number() const;
372  void reset_absolute_top_line_number();
373  int position_to_linecol(int pos, int* lineNum, int* column) const;
374  int scroll_(int topLineNum, int horizOffset);
375 
376  void extend_range_for_styles(int* start, int* end);
377 
378  void find_wrap_range(const char *deletedText, int pos, int nInserted,
379  int nDeleted, int *modRangeStart, int *modRangeEnd,
380  int *linesInserted, int *linesDeleted);
381  void measure_deleted_lines(int pos, int nDeleted);
382  void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
383  int maxLines, bool startPosIsLineStart,
384  int styleBufOffset, int *retPos, int *retLines,
385  int *retLineStart, int *retLineEnd,
386  bool countLastLineMissingNewLine = true) const;
387  void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
388  int *nextLineStart) const;
389  double measure_proportional_character(const char *s, int colNum, int pos) const;
390  int wrap_uses_character(int lineEndPos) const;
391 
392  int damage_range1_start, damage_range1_end;
393  int damage_range2_start, damage_range2_end;
394  int mCursorPos;
395  int mCursorOn;
396  int mCursorOldY; /* Y pos. of cursor for blanking */
397  int mCursorToHint; /* Tells the buffer modified callback
398  where to move the cursor, to reduce
399  the number of redraw calls */
400  int mCursorStyle; /* One of enum cursorStyles above */
401  int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
402  int mNVisibleLines; /* # of visible (displayed) lines */
403  int mNBufferLines; /* # of newlines in the buffer */
404  Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
405  Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
406  color and font information */
407  int mFirstChar, mLastChar; /* Buffer positions of first and last
408  displayed character (lastChar points
409  either to a newline or one character
410  beyond the end of the buffer) */
411  int mContinuousWrap; /* Wrap long lines when displaying */
412  int mWrapMarginPix; /* Margin in # of pixels for
413  wrapping in continuousWrap mode */
414  int* mLineStarts;
415  int mTopLineNum; /* Line number of top displayed line
416  of file (first line of file is 1) */
417  int mAbsTopLineNum; /* In continuous wrap mode, the line
418  number of the top line if the text
419  were not wrapped (note that this is
420  only maintained as needed). */
421  int mNeedAbsTopLineNum; /* Externally settable flag to continue
422  maintaining absTopLineNum even if
423  it isn't needed for line # display */
424  int mHorizOffset; /* Horizontal scroll pos. in pixels */
425  int mTopLineNumHint; /* Line number of top displayed line
426  of file (first line of file is 1) */
427  int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
428  int mNStyles; /* Number of entries in styleTable */
429  const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
430  coloring/syntax-highlighting */
431  char mUnfinishedStyle; /* Style buffer entry which triggers
432  on-the-fly reparsing of region */
433  Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
434  /* regions */
435  void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
436 
437  int mMaxsize;
438 
439  int mSuppressResync; /* Suppress resynchronization of line
440  starts during buffer updates */
441  int mNLinesDeleted; /* Number of lines deleted during
442  buffer modification (only used
443  when resynchronization is suppressed) */
444  int mModifyingTabDistance; /* Whether tab distance is being
445  modified */
446 
447  mutable double mColumnScale; /* Width in pixels of an average character. This
448  value is calculated as needed (lazy eval); it
449  needs to be mutable so that it can be calculated
450  within a method marked as "const" */
451 
452  Fl_Color mCursor_color;
453 
454  Fl_Scrollbar* mHScrollBar;
455  Fl_Scrollbar* mVScrollBar;
456  int scrollbar_width_;
457  Fl_Align scrollbar_align_;
458  int dragPos, dragType, dragging;
459  int display_insert_position_hint;
460  struct { int x, y, w, h; } text_area;
461 
462  int shortcut_;
463 
464  Fl_Font textfont_;
465  Fl_Fontsize textsize_;
466  Fl_Color textcolor_;
467 
468  // The following are not presently used from the original NEdit code,
469  // but are being put here so that future versions of Fl_Text_Display
470  // can implement line numbers without breaking binary compatibility.
471 
472  /* Line number margin and width */
473  int mLineNumLeft, mLineNumWidth;
474 };
475 
476 #endif
477 
478 //
479 // End of "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z matt $".
480 //
void hide_cursor()
Hides the text cursor.
Definition: Fl_Text_Display.H:167
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:248
int scrollbar_width() const
Gets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:187
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:280
wrap text at a pixel position
Definition: Fl_Text_Display.H:94
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition: Fl_Text_Display.H:199
This structure associates the color, font, andsize of a string to draw with an attribute mask matchin...
Definition: Fl_Text_Display.H:106
int h() const
Gets the widget height.
Definition: Fl_Widget.H:295
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition: Fl_Text_Display.H:144
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition: Fl_Text_Display.H:205
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition: Fl_Text_Display.H:219
This class manages unicode displayed in one or more Fl_Text_Display widgets.
Definition: Fl_Text_Buffer.H:168
int w() const
Gets the widget width.
Definition: Fl_Widget.H:290
Fl_Color cursor_color() const
Gets the text cursor color.
Definition: Fl_Text_Display.H:175
I-beam.
Definition: Fl_Text_Display.H:58
void draw()
Draws the widget.
Definition: Fl_Group.cxx:741
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:254
int y() const
Gets the widget position in its window.
Definition: Fl_Widget.H:285
void insert(Fl_Widget &, int i)
The widget is removed from its current group (if any) and then inserted into this group...
Definition: Fl_Group.cxx:461
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:45
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:150
caret under the text
Definition: Fl_Text_Display.H:59
Fl_Widget, Fl_Label classes .
don't wrap text at all
Definition: Fl_Text_Display.H:92
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition: Fl_Text_Display.H:266
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:125
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:741
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:637
int shortcut() const
Definition: Fl_Text_Display.H:235
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition: Fl_Text_Display.H:260
unfille box under the current character
Definition: Fl_Text_Display.H:61
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition: Fl_Text_Display.H:278
utility header to pull drawing functions together
unsigned int Fl_Color
an FLTK color value
Definition: Enumerations.H:769
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:712
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition: Fl_Text_Display.H:181
unsigned Fl_Align
FLTK type for alignment control.
Definition: Enumerations.H:663
wrap text at the given text column
Definition: Fl_Text_Display.H:93
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition: Fl_Text_Display.H:212
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:132
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition: Fl_Text_Display.H:272
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:52
Rich text display widget.
Definition: Fl_Text_Display.H:50
void shortcut(int s)
Definition: Fl_Text_Display.H:242
dim I-beam
Definition: Fl_Text_Display.H:60
void scrollbar_width(int W)
Sets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:193