FLTK 1.3.4
Fl_Text_Display.H
1 //
2 // "$Id: Fl_Text_Display.H 11756 2016-05-26 16:39:09Z greg.ercolano $"
3 //
4 // Header file for Fl_Text_Display class.
5 //
6 // Copyright 2001-2016 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. Distribution and use rights are outlined in
11 // the file "COPYING" which should have been included with this file. If this
12 // file is missing or damaged, see the license at:
13 //
14 // http://www.fltk.org/COPYING.php
15 //
16 // Please report all bugs and problems on the following page:
17 //
18 // http://www.fltk.org/str.php
19 //
20 
21 /* \file
22  Fl_Text_Display widget . */
23 
24 #ifndef FL_TEXT_DISPLAY_H
25 #define FL_TEXT_DISPLAY_H
26 
27 #include "fl_draw.H"
28 #include "Fl_Group.H"
29 #include "Fl_Widget.H"
30 #include "Fl_Scrollbar.H"
31 #include "Fl_Text_Buffer.H"
32 
82 class FL_EXPORT Fl_Text_Display: public Fl_Group {
83 
84 public:
85 
89  enum {
95  SIMPLE_CURSOR
96  };
97 
103  enum {
104  CURSOR_POS,
105  CHARACTER_POS
106  };
107 
113  enum {
114  DRAG_NONE = -2,
115  DRAG_START_DND = -1,
116  DRAG_CHAR = 0,
117  DRAG_WORD = 1,
118  DRAG_LINE = 2
119  };
120 
124  enum {
128  WRAP_AT_BOUNDS
129  };
130 
131  friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
132 
133  typedef void (*Unfinished_Style_Cb)(int, void *);
134 
152  unsigned attr;
153  };
154 
155  Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
156  ~Fl_Text_Display();
157 
158  virtual int handle(int e);
159 
160  void buffer(Fl_Text_Buffer* buf);
161 
167  void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
168 
174  Fl_Text_Buffer* buffer() const { return mBuffer; }
175 
176  void redisplay_range(int start, int end);
177  void scroll(int topLineNum, int horizOffset);
178  void insert(const char* text);
179  void overstrike(const char* text);
180  void insert_position(int newPos);
181 
186  int insert_position() const { return mCursorPos; }
187  int position_to_xy(int pos, int* x, int* y) const;
188 
189  int in_selection(int x, int y) const;
190  void show_insert_position();
191 
192  int move_right();
193  int move_left();
194  int move_up();
195  int move_down();
196  int count_lines(int start, int end, bool start_pos_is_line_start) const;
197  int line_start(int pos) const;
198  int line_end(int startPos, bool startPosIsLineStart) const;
199  int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
200  int rewind_lines(int startPos, int nLines);
201  void next_word(void);
202  void previous_word(void);
203 
204  void show_cursor(int b = 1);
205 
209  void hide_cursor() { show_cursor(0); }
210 
211  void cursor_style(int style);
212 
217  Fl_Color cursor_color() const {return mCursor_color;}
218 
223  void cursor_color(Fl_Color n) {mCursor_color = n;}
224 
229  int scrollbar_width() const { return scrollbar_width_; }
230 
235  void scrollbar_width(int W) { scrollbar_width_ = W; }
236 
241  Fl_Align scrollbar_align() const { return scrollbar_align_; }
242 
247  void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
248 
254  int word_start(int pos) const { return buffer()->word_start(pos); }
255 
261  int word_end(int pos) const { return buffer()->word_end(pos); }
262 
263 
264  void highlight_data(Fl_Text_Buffer *styleBuffer,
265  const Style_Table_Entry *styleTable,
266  int nStyles, char unfinishedStyle,
267  Unfinished_Style_Cb unfinishedHighlightCB,
268  void *cbArg);
269 
270  int position_style(int lineStartPos, int lineLen, int lineIndex) const;
271 
277  int shortcut() const {return shortcut_;}
278 
284  void shortcut(int s) {shortcut_ = s;}
285 
290  Fl_Font textfont() const {return textfont_;}
291 
296  void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;}
297 
302  Fl_Fontsize textsize() const {return textsize_;}
303 
308  void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;}
309 
314  Fl_Color textcolor() const {return textcolor_;}
315 
320  void textcolor(Fl_Color n) {textcolor_ = n;}
321 
322  int wrapped_column(int row, int column) const;
323  int wrapped_row(int row) const;
324  void wrap_mode(int wrap, int wrap_margin);
325 
326  virtual void resize(int X, int Y, int W, int H);
327 
333  double x_to_col(double x) const;
334 
341  double col_to_x(double col) const;
342 
343  void linenumber_width(int width);
344  int linenumber_width() const;
345  void linenumber_font(Fl_Font val);
346  Fl_Font linenumber_font() const;
347  void linenumber_size(Fl_Fontsize val);
348  Fl_Fontsize linenumber_size() const;
349  void linenumber_fgcolor(Fl_Color val);
350  Fl_Color linenumber_fgcolor() const;
351  void linenumber_bgcolor(Fl_Color val);
352  Fl_Color linenumber_bgcolor() const;
353  void linenumber_align(Fl_Align val);
354  Fl_Align linenumber_align() const;
355  void linenumber_format(const char* val);
356  const char* linenumber_format() const;
357 
358 protected:
359  // Most (all?) of this stuff should only be called from resize() or
360  // draw().
361  // Anything with "vline" indicates thats it deals with currently
362  // visible lines.
363 
364  virtual void draw();
365  void draw_text(int X, int Y, int W, int H);
366  void draw_range(int start, int end);
367  void draw_cursor(int, int);
368 
369  void draw_string(int style, int x, int y, int toX, const char *string,
370  int nChars) const;
371 
372  void draw_vline(int visLineNum, int leftClip, int rightClip,
373  int leftCharIndex, int rightCharIndex);
374 
375  int find_x(const char *s, int len, int style, int x) const;
376 
377  enum {
378  DRAW_LINE,
379  FIND_INDEX,
380  FIND_INDEX_FROM_ZERO,
381  GET_WIDTH
382  };
383 
384  int handle_vline(int mode,
385  int lineStart, int lineLen, int leftChar, int rightChar,
386  int topClip, int bottomClip,
387  int leftClip, int rightClip) const;
388 
389  void draw_line_numbers(bool clearAll);
390 
391  void clear_rect(int style, int x, int y, int width, int height) const;
392  void display_insert();
393 
394  void offset_line_starts(int newTopLineNum);
395 
396  void calc_line_starts(int startLine, int endLine);
397 
398  void update_line_starts(int pos, int charsInserted, int charsDeleted,
399  int linesInserted, int linesDeleted, int *scrolled);
400 
401  void calc_last_char();
402 
403  int position_to_line( int pos, int* lineNum ) const;
404  double string_width(const char* string, int length, int style) const;
405 
406  static void scroll_timer_cb(void*);
407 
408  static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
409  static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
410  int nRestyled, const char* deletedText,
411  void* cbArg);
412 
413  static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
414  static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
415  void update_v_scrollbar();
416  void update_h_scrollbar();
417  int measure_vline(int visLineNum) const;
418  int longest_vline() const;
419  int empty_vlines() const;
420  int vline_length(int visLineNum) const;
421  int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
422 
423  void xy_to_rowcol(int x, int y, int* row, int* column,
424  int PosType = CHARACTER_POS) const;
425  void maintain_absolute_top_line_number(int state);
426  int get_absolute_top_line_number() const;
427  void absolute_top_line_number(int oldFirstChar);
428  int maintaining_absolute_top_line_number() const;
429  void reset_absolute_top_line_number();
430  int position_to_linecol(int pos, int* lineNum, int* column) const;
431  int scroll_(int topLineNum, int horizOffset);
432 
433  void extend_range_for_styles(int* start, int* end);
434 
435  void find_wrap_range(const char *deletedText, int pos, int nInserted,
436  int nDeleted, int *modRangeStart, int *modRangeEnd,
437  int *linesInserted, int *linesDeleted);
438  void measure_deleted_lines(int pos, int nDeleted);
439  void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
440  int maxLines, bool startPosIsLineStart,
441  int styleBufOffset, int *retPos, int *retLines,
442  int *retLineStart, int *retLineEnd,
443  bool countLastLineMissingNewLine = true) const;
444  void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
445  int *nextLineStart) const;
446  double measure_proportional_character(const char *s, int colNum, int pos) const;
447  int wrap_uses_character(int lineEndPos) const;
448 
449  int damage_range1_start, damage_range1_end;
450  int damage_range2_start, damage_range2_end;
451  int mCursorPos;
452  int mCursorOn;
453  int mCursorOldY; /* Y pos. of cursor for blanking */
454  int mCursorToHint; /* Tells the buffer modified callback
455  where to move the cursor, to reduce
456  the number of redraw calls */
457  int mCursorStyle; /* One of enum cursorStyles above */
458  int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
459  int mNVisibleLines; /* # of visible (displayed) lines */
460  int mNBufferLines; /* # of newlines in the buffer */
461  Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
462  Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
463  color and font information */
464  int mFirstChar, mLastChar; /* Buffer positions of first and last
465  displayed character (lastChar points
466  either to a newline or one character
467  beyond the end of the buffer) */
468  int mContinuousWrap; /* Wrap long lines when displaying */
469  int mWrapMarginPix; /* Margin in # of pixels for
470  wrapping in continuousWrap mode */
471  int* mLineStarts;
472  int mTopLineNum; /* Line number of top displayed line
473  of file (first line of file is 1) */
474  int mAbsTopLineNum; /* In continuous wrap mode, the line
475  number of the top line if the text
476  were not wrapped (note that this is
477  only maintained as needed). */
478  int mNeedAbsTopLineNum; /* Externally settable flag to continue
479  maintaining absTopLineNum even if
480  it isn't needed for line # display */
481  int mHorizOffset; /* Horizontal scroll pos. in pixels */
482  int mTopLineNumHint; /* Line number of top displayed line
483  of file (first line of file is 1) */
484  int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
485  int mNStyles; /* Number of entries in styleTable */
486  const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
487  coloring/syntax-highlighting */
488  char mUnfinishedStyle; /* Style buffer entry which triggers
489  on-the-fly reparsing of region */
490  Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
491  /* regions */
492  void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
493 
494  int mMaxsize;
495 
496  int mSuppressResync; /* Suppress resynchronization of line
497  starts during buffer updates */
498  int mNLinesDeleted; /* Number of lines deleted during
499  buffer modification (only used
500  when resynchronization is suppressed) */
501  int mModifyingTabDistance; /* Whether tab distance is being
502  modified */
503 
504  mutable double mColumnScale; /* Width in pixels of an average character. This
505  value is calculated as needed (lazy eval); it
506  needs to be mutable so that it can be calculated
507  within a method marked as "const" */
508 
509  Fl_Color mCursor_color;
510 
511  Fl_Scrollbar* mHScrollBar;
512  Fl_Scrollbar* mVScrollBar;
513  int scrollbar_width_;
514  Fl_Align scrollbar_align_;
515  int dragPos, dragType, dragging;
516  int display_insert_position_hint;
517  struct { int x, y, w, h; } text_area;
518 
519  int shortcut_;
520 
521  Fl_Font textfont_;
522  Fl_Fontsize textsize_;
523  Fl_Color textcolor_;
524 
525  // Line number margin and width
526  int mLineNumLeft, mLineNumWidth;
527 
528  // Line number font/colors
529 #if FLTK_ABI_VERSION >= 10303
530  Fl_Font linenumber_font_;
531  Fl_Fontsize linenumber_size_;
532  Fl_Color linenumber_fgcolor_;
533  Fl_Color linenumber_bgcolor_;
534  Fl_Align linenumber_align_;
535  const char* linenumber_format_;
536 #endif
537 };
538 
539 #endif
540 
541 //
542 // End of "$Id: Fl_Text_Display.H 11756 2016-05-26 16:39:09Z greg.ercolano $".
543 //
Fl_Fontsize size
text font size
Definition: Fl_Text_Display.H:151
void hide_cursor()
Hides the text cursor.
Definition: Fl_Text_Display.H:209
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:290
int scrollbar_width() const
Gets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:229
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:284
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition: Fl_Text_Display.H:241
This structure associates the color, font, and font size of a string to draw with an attribute mask m...
Definition: Fl_Text_Display.H:148
don't wrap text at all
Definition: Fl_Text_Display.H:125
wrap text at the given text column
Definition: Fl_Text_Display.H:126
int h() const
Gets the widget height.
Definition: Fl_Widget.H:299
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition: Fl_Text_Display.H:186
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition: Fl_Text_Display.H:247
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition: Fl_Text_Display.H:261
This class manages Unicode text displayed in one or more Fl_Text_Display widgets. ...
Definition: Fl_Text_Buffer.H:158
int w() const
Gets the widget width.
Definition: Fl_Widget.H:294
Fl_Color cursor_color() const
Gets the text cursor color.
Definition: Fl_Text_Display.H:217
void draw()
Draws the widget.
Definition: Fl_Group.cxx:738
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:296
int y() const
Gets the widget position in its window.
Definition: Fl_Widget.H:289
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:458
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:41
dim I-beam
Definition: Fl_Text_Display.H:92
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:147
wrap text at a pixel position
Definition: Fl_Text_Display.H:127
unfille box under the current character
Definition: Fl_Text_Display.H:93
Fl_Widget, Fl_Label classes .
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition: Fl_Text_Display.H:308
I-beam.
Definition: Fl_Text_Display.H:90
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:167
unsigned attr
currently unused (this may be change in the future)
Definition: Fl_Text_Display.H:152
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:906
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:634
Fl_Color color
text color
Definition: Fl_Text_Display.H:149
int shortcut() const
Definition: Fl_Text_Display.H:277
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition: Fl_Text_Display.H:302
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition: Fl_Text_Display.H:320
utility header to pull drawing functions together
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition: Enumerations.H:934
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:877
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition: Fl_Text_Display.H:223
unsigned Fl_Align
FLTK type for alignment control.
Definition: Enumerations.H:828
thick I-beam
Definition: Fl_Text_Display.H:94
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition: Fl_Text_Display.H:254
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:174
caret under the text
Definition: Fl_Text_Display.H:91
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition: Fl_Text_Display.H:314
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:43
Rich text display widget.
Definition: Fl_Text_Display.H:82
Fl_Font font
text font
Definition: Fl_Text_Display.H:150
void shortcut(int s)
Definition: Fl_Text_Display.H:284
void scrollbar_width(int W)
Sets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:235