FLTK 1.4.0
Fl_Text_Display.H
1 //
2 // Header file for Fl_Text_Display class.
3 //
4 // Copyright 2001-2020 by Bill Spitzak and others.
5 // Original code Copyright Mark Edel. Permission to distribute under
6 // the LGPL for the FLTK library granted by Mark Edel.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // https://www.fltk.org/COPYING.php
13 //
14 // Please see the following page on how to report bugs and issues:
15 //
16 // https://www.fltk.org/bugs.php
17 //
18 
19 /* \file
20  Fl_Text_Display widget . */
21 
22 #ifndef FL_TEXT_DISPLAY_H
23 #define FL_TEXT_DISPLAY_H
24 
25 #include <FL/Fl.H> // Fl::scrollbar_size()
26 #include "fl_draw.H"
27 #include "Fl_Group.H"
28 #include "Fl_Widget.H"
29 #include "Fl_Scrollbar.H"
30 #include "Fl_Text_Buffer.H"
31 
78 class FL_EXPORT Fl_Text_Display: public Fl_Group {
79 
80 public:
81 
85  enum {
91  SIMPLE_CURSOR
92  };
93 
99  enum {
100  CURSOR_POS,
101  CHARACTER_POS
102  };
103 
109  enum {
110  DRAG_NONE = -2,
111  DRAG_START_DND = -1,
112  DRAG_CHAR = 0,
113  DRAG_WORD = 1,
114  DRAG_LINE = 2
115  };
116 
120  enum {
124  WRAP_AT_BOUNDS
125  };
126 
127  friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
128 
129  typedef void (*Unfinished_Style_Cb)(int, void *);
130 
148  unsigned attr;
150  };
151 
155  enum {
156  ATTR_BGCOLOR = 0x0001,
157  ATTR_BGCOLOR_EXT_ = 0x0002,
158  ATTR_BGCOLOR_EXT = 0x0003,
159  ATTR_UNDERLINE = 0x0004,
160  ATTR_GRAMMAR = 0x0008,
161  ATTR_SPELLING = 0x000C,
162  ATTR_STRIKE_THROUGH = 0x0010,
163  ATTR_LINES_MASK = 0x001C,
164  };
165 
166  Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
167  ~Fl_Text_Display();
168 
169  virtual int handle(int e);
170 
171  void buffer(Fl_Text_Buffer* buf);
172 
179  void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
180 
188  Fl_Text_Buffer* buffer() const { return mBuffer; }
195  Fl_Text_Buffer* style_buffer() const { return mStyleBuffer; }
196 
197  void redisplay_range(int start, int end);
198  void scroll(int topLineNum, int horizOffset);
199  void insert(const char* text);
200  void overstrike(const char* text);
201  void insert_position(int newPos);
202 
214  int insert_position() const { return mCursorPos; }
215  int position_to_xy(int pos, int* x, int* y) const;
216 
217  int in_selection(int x, int y) const;
218  void show_insert_position();
219 
220  int move_right();
221  int move_left();
222  int move_up();
223  int move_down();
224  int count_lines(int start, int end, bool start_pos_is_line_start) const;
225  int line_start(int pos) const;
226  int line_end(int startPos, bool startPosIsLineStart) const;
227  int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
228  int rewind_lines(int startPos, int nLines);
229  void next_word(void);
230  void previous_word(void);
231 
232  void show_cursor(int b = 1);
233 
237  void hide_cursor() { show_cursor(0); }
238 
239  void cursor_style(int style);
240  int cursor_style() const { return mCursorStyle; }
241 
246  Fl_Color cursor_color() const {return mCursor_color;}
247 
252  void cursor_color(Fl_Color n) {mCursor_color = n;}
253 
259  int scrollbar_width() const {
260  return scrollbar_width_ ? scrollbar_width_ : Fl::scrollbar_size();
261  }
262 
268  void scrollbar_width(int width) {
269  Fl::scrollbar_size(width);
270  scrollbar_width_ = 0;
271  }
272 
282  int scrollbar_size() const {
283  return(scrollbar_width_);
284  }
285 
305  void scrollbar_size(int newSize) {
306  scrollbar_width_ = newSize;
307  }
308 
313  Fl_Align scrollbar_align() const { return scrollbar_align_; }
314 
319  void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
320 
326  int word_start(int pos) const { return buffer()->word_start(pos); }
327 
333  int word_end(int pos) const { return buffer()->word_end(pos); }
334 
335 
336  void highlight_data(Fl_Text_Buffer *styleBuffer,
337  const Style_Table_Entry *styleTable,
338  int nStyles, char unfinishedStyle,
339  Unfinished_Style_Cb unfinishedHighlightCB,
340  void *cbArg);
341 
342  int position_style(int lineStartPos, int lineLen, int lineIndex) const;
343 
349  int shortcut() const {return shortcut_;}
350 
356  void shortcut(int s) {shortcut_ = s;}
357 
362  Fl_Font textfont() const {return textfont_;}
363 
368  void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0; }
369 
374  Fl_Fontsize textsize() const {return textsize_;}
375 
380  void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0; }
381 
386  Fl_Color textcolor() const {return textcolor_;}
387 
392  void textcolor(Fl_Color n) {textcolor_ = n;}
393 
398  void grammar_underline_color(Fl_Color color) { grammar_underline_color_ = color; }
399 
404  Fl_Color grammar_underline_color() const { return grammar_underline_color_;}
405 
410  void spelling_underline_color(Fl_Color color) { spelling_underline_color_ = color; }
411 
416  Fl_Color spelling_underline_color() const { return spelling_underline_color_;}
417 
422  void secondary_selection_color(Fl_Color color) { secondary_selection_color_ = color; }
423 
428  Fl_Color secondary_selection_color() const { return secondary_selection_color_;}
429 
430  int wrapped_column(int row, int column) const;
431  int wrapped_row(int row) const;
432  void wrap_mode(int wrap, int wrap_margin);
433 
434  virtual void recalc_display();
435  virtual void resize(int X, int Y, int W, int H);
436 
442  double x_to_col(double x) const;
443 
450  double col_to_x(double col) const;
451 
452  void linenumber_width(int width);
453  int linenumber_width() const;
454  void linenumber_font(Fl_Font val);
455  Fl_Font linenumber_font() const;
456  void linenumber_size(Fl_Fontsize val);
457  Fl_Fontsize linenumber_size() const;
458  void linenumber_fgcolor(Fl_Color val);
459  Fl_Color linenumber_fgcolor() const;
460  void linenumber_bgcolor(Fl_Color val);
461  Fl_Color linenumber_bgcolor() const;
462  void linenumber_align(Fl_Align val);
463  Fl_Align linenumber_align() const;
464  void linenumber_format(const char* val);
465  const char* linenumber_format() const;
466 
467 protected:
468  // Most (all?) of this stuff should only be called from resize() or
469  // draw().
470  // Anything with "vline" indicates thats it deals with currently
471  // visible lines.
472 
473  virtual void draw();
474  void draw_text(int X, int Y, int W, int H);
475  void draw_range(int start, int end);
476  void draw_cursor(int, int);
477 
478  void draw_string(int style, int x, int y, int toX, const char *string,
479  int nChars) const;
480 
481  void draw_vline(int visLineNum, int leftClip, int rightClip,
482  int leftCharIndex, int rightCharIndex);
483 
484  int find_x(const char *s, int len, int style, int x) const;
485 
486  enum {
487  DRAW_LINE,
488  FIND_INDEX,
489  FIND_INDEX_FROM_ZERO,
490  GET_WIDTH,
491  FIND_CURSOR_INDEX // STR #2788
492  };
493 
494  int handle_vline(int mode,
495  int lineStart, int lineLen, int leftChar, int rightChar,
496  int topClip, int bottomClip,
497  int leftClip, int rightClip) const;
498 
499  void draw_line_numbers(bool clearAll);
500 
501  void clear_rect(int style, int x, int y, int width, int height) const;
502  void display_insert();
503 
504  void offset_line_starts(int newTopLineNum);
505 
506  void calc_line_starts(int startLine, int endLine);
507 
508  void update_line_starts(int pos, int charsInserted, int charsDeleted,
509  int linesInserted, int linesDeleted, int *scrolled);
510 
511  void calc_last_char();
512 
513  int position_to_line( int pos, int* lineNum ) const;
514  double string_width(const char* string, int length, int style) const;
515 
516  static void scroll_timer_cb(void*);
517 
518  static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
519  static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
520  int nRestyled, const char* deletedText,
521  void* cbArg);
522 
523  static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
524  static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
525  void update_v_scrollbar();
526  void update_h_scrollbar();
527  int measure_vline(int visLineNum) const;
528  int longest_vline() const;
529  int empty_vlines() const;
530  int vline_length(int visLineNum) const;
531  int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
532 
533  void xy_to_rowcol(int x, int y, int* row, int* column,
534  int PosType = CHARACTER_POS) const;
535  void maintain_absolute_top_line_number(int state);
536  int get_absolute_top_line_number() const;
537  void absolute_top_line_number(int oldFirstChar);
538  int maintaining_absolute_top_line_number() const;
539  void reset_absolute_top_line_number();
540  int position_to_linecol(int pos, int* lineNum, int* column) const;
541  int scroll_(int topLineNum, int horizOffset);
542 
543  void extend_range_for_styles(int* start, int* end);
544 
545  void find_wrap_range(const char *deletedText, int pos, int nInserted,
546  int nDeleted, int *modRangeStart, int *modRangeEnd,
547  int *linesInserted, int *linesDeleted);
548  void measure_deleted_lines(int pos, int nDeleted);
549  void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
550  int maxLines, bool startPosIsLineStart,
551  int styleBufOffset, int *retPos, int *retLines,
552  int *retLineStart, int *retLineEnd,
553  bool countLastLineMissingNewLine = true) const;
554  void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
555  int *nextLineStart) const;
556  double measure_proportional_character(const char *s, int colNum, int pos) const;
557  int wrap_uses_character(int lineEndPos) const;
558 
559  int damage_range1_start, damage_range1_end;
560  int damage_range2_start, damage_range2_end;
561  int mCursorPos;
562  int mCursorOn;
563  int mCursorOldY; /* Y pos. of cursor for blanking */
564  int mCursorToHint; /* Tells the buffer modified callback
565  where to move the cursor, to reduce
566  the number of redraw calls */
567  int mCursorStyle; /* One of enum cursorStyles above */
568  int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
569  int mNVisibleLines; /* # of visible (displayed) lines. This is
570  also the size of the mLineStarts[] array. */
571  int mNBufferLines; /* # of newlines in the buffer */
572  Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
573  Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
574  color and font information */
575  int mFirstChar, mLastChar; /* Buffer positions of first and last
576  displayed character (lastChar points
577  either to a newline or one character
578  beyond the end of the buffer) */
579  int mContinuousWrap; /* Wrap long lines when displaying */
580  int mWrapMarginPix; /* Margin in # of pixels for
581  wrapping in continuousWrap mode */
582  int* mLineStarts; /* Array of the size mNVisibleLines.
583  This array only keeps track of lines
584  within the display area. Each entry
585  contains the starting character offset
586  (from the beginning of the text buffer)
587  for each /visible/ line.
588  If wrap enabled, points to the beginning
589  of each wrap. So a long line wrapping
590  into 3 separate lines in the display
591  will take up 3 separate array entries. */
592  int mTopLineNum; /* Line number of top displayed line
593  of file (first line of file is 1) */
594  int mAbsTopLineNum; /* In continuous wrap mode, the line
595  number of the top line if the text
596  were not wrapped (note that this is
597  only maintained as needed). */
598  int mNeedAbsTopLineNum; /* Externally settable flag to continue
599  maintaining absTopLineNum even if
600  it isn't needed for line # display */
601  int mHorizOffset; /* Horizontal scroll pos. in pixels */
602  int mTopLineNumHint; /* Line number of top displayed line
603  of file (first line of file is 1) */
604  int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
605  int mNStyles; /* Number of entries in styleTable */
606  const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
607  coloring/syntax-highlighting */
608  char mUnfinishedStyle; /* Style buffer entry which triggers
609  on-the-fly reparsing of region */
610  Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
611  /* regions */
612  void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
613 
614  int mMaxsize;
615 
616  int mSuppressResync; /* Suppress resynchronization of line
617  starts during buffer updates */
618  int mNLinesDeleted; /* Number of lines deleted during
619  buffer modification (only used
620  when resynchronization is suppressed) */
621  int mModifyingTabDistance; /* Whether tab distance is being modified XXX: UNUSED */
622 
623  mutable double mColumnScale; /* Width in pixels of an average character. This
624  value is calculated as needed (lazy eval); it
625  needs to be mutable so that it can be calculated
626  within a method marked as "const" */
627 
628  Fl_Color mCursor_color;
629 
630  Fl_Scrollbar* mHScrollBar;
631  Fl_Scrollbar* mVScrollBar;
632  int scrollbar_width_; // size of scrollbar trough (behavior changed in 1.4)
633  Fl_Align scrollbar_align_;
634  int dragPos, dragType, dragging;
635  int display_insert_position_hint;
636  struct { int x, y, w, h; } text_area;
637 
638  int shortcut_;
639 
640  Fl_Font textfont_;
641  Fl_Fontsize textsize_;
642  Fl_Color textcolor_;
643  Fl_Color grammar_underline_color_;
644  Fl_Color spelling_underline_color_;
645  Fl_Color secondary_selection_color_;
646 
647  // Line number margin and width
648  int mLineNumLeft, mLineNumWidth;
649 
650  // Line number font/colors
651  Fl_Font linenumber_font_;
652  Fl_Fontsize linenumber_size_;
653  Fl_Color linenumber_fgcolor_;
654  Fl_Color linenumber_bgcolor_;
655  Fl_Align linenumber_align_;
656  const char* linenumber_format_;
657 };
658 
659 #endif
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:985
unsigned Fl_Align
FLTK type for alignment control.
Definition: Enumerations.H:908
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition: Enumerations.H:1042
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:1014
Fl static class.
Fl_Widget, Fl_Label classes .
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:42
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:145
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:455
void draw()
Draws the widget.
Definition: Fl_Group.cxx:840
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:737
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:41
This class manages Unicode text displayed in one or more Fl_Text_Display widgets.
Definition: Fl_Text_Buffer.H:196
Rich text display widget.
Definition: Fl_Text_Display.H:78
int scrollbar_width() const
Returns the global value Fl::scrollbar_size() unless a specific scrollbar_width_ has been set.
Definition: Fl_Text_Display.H:259
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition: Fl_Text_Display.H:380
void hide_cursor()
Hides the text cursor.
Definition: Fl_Text_Display.H:237
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition: Fl_Text_Display.H:252
void scrollbar_size(int newSize)
Sets the pixel size of the scrollbars' troughs to newSize, in pixels.
Definition: Fl_Text_Display.H:305
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition: Fl_Text_Display.H:392
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition: Fl_Text_Display.H:313
@ WRAP_AT_COLUMN
wrap text at the given text column
Definition: Fl_Text_Display.H:122
@ WRAP_NONE
don't wrap text at all
Definition: Fl_Text_Display.H:121
@ WRAP_AT_PIXEL
wrap text at a pixel position
Definition: Fl_Text_Display.H:123
void shortcut(int s)
Definition: Fl_Text_Display.H:356
void scrollbar_width(int width)
Sets the global Fl::scrollbar_size(), and forces this instance of the widget to use it.
Definition: Fl_Text_Display.H:268
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition: Fl_Text_Display.H:319
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition: Fl_Text_Display.H:326
int scrollbar_size() const
Gets the current size of the scrollbars' troughs, in pixels.
Definition: Fl_Text_Display.H:282
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition: Fl_Text_Display.H:333
Fl_Color cursor_color() const
Gets the text cursor color.
Definition: Fl_Text_Display.H:246
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition: Fl_Text_Display.H:386
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:368
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition: Fl_Text_Display.H:214
Fl_Color grammar_underline_color() const
Gets the underline color for style attribute ATTR_GRAMMAR.
Definition: Fl_Text_Display.H:404
int shortcut() const
Definition: Fl_Text_Display.H:349
Fl_Color secondary_selection_color() const
Gets the background color for the secondary selection block.
Definition: Fl_Text_Display.H:428
Fl_Color spelling_underline_color() const
Gets the underline color for style attribute ATTR_SPELLING.
Definition: Fl_Text_Display.H:416
Fl_Text_Buffer * style_buffer() const
Gets the current style buffer associated with the text widget.
Definition: Fl_Text_Display.H:195
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition: Fl_Text_Display.H:374
void grammar_underline_color(Fl_Color color)
Sets the underline color for style attribute ATTR_GRAMMAR.
Definition: Fl_Text_Display.H:398
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:188
void spelling_underline_color(Fl_Color color)
Sets the underline color for style attribute ATTR_SPELLING.
Definition: Fl_Text_Display.H:410
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:179
@ BLOCK_CURSOR
unfille box under the current character
Definition: Fl_Text_Display.H:89
@ DIM_CURSOR
dim I-beam
Definition: Fl_Text_Display.H:88
@ CARET_CURSOR
caret under the text
Definition: Fl_Text_Display.H:87
@ NORMAL_CURSOR
I-beam.
Definition: Fl_Text_Display.H:86
@ HEAVY_CURSOR
thick I-beam
Definition: Fl_Text_Display.H:90
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:362
void secondary_selection_color(Fl_Color color)
Sets the background color for the secondary selection block.
Definition: Fl_Text_Display.H:422
Fl_Color color() const
Gets the background color of the widget.
Definition: Fl_Widget.H:384
int y() const
Gets the widget position in its window.
Definition: Fl_Widget.H:295
int h() const
Gets the widget height.
Definition: Fl_Widget.H:305
int w() const
Gets the widget width.
Definition: Fl_Widget.H:300
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:290
static int scrollbar_size()
Gets the default scrollbar size used by Fl_Browser_, Fl_Help_View, Fl_Scroll, and Fl_Text_Display wid...
Definition: Fl.cxx:146
utility header to pull drawing functions together
This structure associates the color, font, and font size of a string to draw with an attribute mask m...
Definition: Fl_Text_Display.H:144
Fl_Color color
text color
Definition: Fl_Text_Display.H:145
Fl_Fontsize size
text font size
Definition: Fl_Text_Display.H:147
Fl_Font font
text font
Definition: Fl_Text_Display.H:146
unsigned attr
further attributes for the text style (see ATTR_BGCOLOR, etc.)
Definition: Fl_Text_Display.H:148
Fl_Color bgcolor
text background color if ATTR_BGCOLOR or ATTR_BGCOLOR_EXT is set
Definition: Fl_Text_Display.H:149