FLTK
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;
149  };
150 
151  Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
152  ~Fl_Text_Display();
153 
154  virtual int handle(int e);
155 
156  void buffer(Fl_Text_Buffer* buf);
157 
164  void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
165 
173  Fl_Text_Buffer* buffer() const { return mBuffer; }
174 
175  void redisplay_range(int start, int end);
176  void scroll(int topLineNum, int horizOffset);
177  void insert(const char* text);
178  void overstrike(const char* text);
179  void insert_position(int newPos);
180 
192  int insert_position() const { return mCursorPos; }
193  int position_to_xy(int pos, int* x, int* y) const;
194 
195  int in_selection(int x, int y) const;
196  void show_insert_position();
197 
198  int move_right();
199  int move_left();
200  int move_up();
201  int move_down();
202  int count_lines(int start, int end, bool start_pos_is_line_start) const;
203  int line_start(int pos) const;
204  int line_end(int startPos, bool startPosIsLineStart) const;
205  int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
206  int rewind_lines(int startPos, int nLines);
207  void next_word(void);
208  void previous_word(void);
209 
210  void show_cursor(int b = 1);
211 
215  void hide_cursor() { show_cursor(0); }
216 
217  void cursor_style(int style);
218  int cursor_style() const { return mCursorStyle; }
219 
224  Fl_Color cursor_color() const {return mCursor_color;}
225 
230  void cursor_color(Fl_Color n) {mCursor_color = n;}
231 
237  int scrollbar_width() const {
238  return scrollbar_width_ ? scrollbar_width_ : Fl::scrollbar_size();
239  }
240 
246  void scrollbar_width(int width) {
247  Fl::scrollbar_size(width);
248  scrollbar_width_ = 0;
249  }
250 
260  int scrollbar_size() const {
261  return(scrollbar_width_);
262  }
263 
283  void scrollbar_size(int newSize) {
284  scrollbar_width_ = newSize;
285  }
286 
291  Fl_Align scrollbar_align() const { return scrollbar_align_; }
292 
297  void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
298 
304  int word_start(int pos) const { return buffer()->word_start(pos); }
305 
311  int word_end(int pos) const { return buffer()->word_end(pos); }
312 
313 
314  void highlight_data(Fl_Text_Buffer *styleBuffer,
315  const Style_Table_Entry *styleTable,
316  int nStyles, char unfinishedStyle,
317  Unfinished_Style_Cb unfinishedHighlightCB,
318  void *cbArg);
319 
320  int position_style(int lineStartPos, int lineLen, int lineIndex) const;
321 
327  int shortcut() const {return shortcut_;}
328 
334  void shortcut(int s) {shortcut_ = s;}
335 
340  Fl_Font textfont() const {return textfont_;}
341 
346  void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0; }
347 
352  Fl_Fontsize textsize() const {return textsize_;}
353 
358  void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0; }
359 
364  Fl_Color textcolor() const {return textcolor_;}
365 
370  void textcolor(Fl_Color n) {textcolor_ = n;}
371 
372  int wrapped_column(int row, int column) const;
373  int wrapped_row(int row) const;
374  void wrap_mode(int wrap, int wrap_margin);
375 
376  virtual void recalc_display();
377  virtual void resize(int X, int Y, int W, int H);
378 
384  double x_to_col(double x) const;
385 
392  double col_to_x(double col) const;
393 
394  void linenumber_width(int width);
395  int linenumber_width() const;
396  void linenumber_font(Fl_Font val);
397  Fl_Font linenumber_font() const;
398  void linenumber_size(Fl_Fontsize val);
399  Fl_Fontsize linenumber_size() const;
400  void linenumber_fgcolor(Fl_Color val);
401  Fl_Color linenumber_fgcolor() const;
402  void linenumber_bgcolor(Fl_Color val);
403  Fl_Color linenumber_bgcolor() const;
404  void linenumber_align(Fl_Align val);
405  Fl_Align linenumber_align() const;
406  void linenumber_format(const char* val);
407  const char* linenumber_format() const;
408 
409 protected:
410  // Most (all?) of this stuff should only be called from resize() or
411  // draw().
412  // Anything with "vline" indicates thats it deals with currently
413  // visible lines.
414 
415  virtual void draw();
416  void draw_text(int X, int Y, int W, int H);
417  void draw_range(int start, int end);
418  void draw_cursor(int, int);
419 
420  void draw_string(int style, int x, int y, int toX, const char *string,
421  int nChars) const;
422 
423  void draw_vline(int visLineNum, int leftClip, int rightClip,
424  int leftCharIndex, int rightCharIndex);
425 
426  int find_x(const char *s, int len, int style, int x) const;
427 
428  enum {
429  DRAW_LINE,
430  FIND_INDEX,
431  FIND_INDEX_FROM_ZERO,
432  GET_WIDTH,
433  FIND_CURSOR_INDEX // STR #2788
434  };
435 
436  int handle_vline(int mode,
437  int lineStart, int lineLen, int leftChar, int rightChar,
438  int topClip, int bottomClip,
439  int leftClip, int rightClip) const;
440 
441  void draw_line_numbers(bool clearAll);
442 
443  void clear_rect(int style, int x, int y, int width, int height) const;
444  void display_insert();
445 
446  void offset_line_starts(int newTopLineNum);
447 
448  void calc_line_starts(int startLine, int endLine);
449 
450  void update_line_starts(int pos, int charsInserted, int charsDeleted,
451  int linesInserted, int linesDeleted, int *scrolled);
452 
453  void calc_last_char();
454 
455  int position_to_line( int pos, int* lineNum ) const;
456  double string_width(const char* string, int length, int style) const;
457 
458  static void scroll_timer_cb(void*);
459 
460  static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
461  static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
462  int nRestyled, const char* deletedText,
463  void* cbArg);
464 
465  static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
466  static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
467  void update_v_scrollbar();
468  void update_h_scrollbar();
469  int measure_vline(int visLineNum) const;
470  int longest_vline() const;
471  int empty_vlines() const;
472  int vline_length(int visLineNum) const;
473  int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
474 
475  void xy_to_rowcol(int x, int y, int* row, int* column,
476  int PosType = CHARACTER_POS) const;
477  void maintain_absolute_top_line_number(int state);
478  int get_absolute_top_line_number() const;
479  void absolute_top_line_number(int oldFirstChar);
480  int maintaining_absolute_top_line_number() const;
481  void reset_absolute_top_line_number();
482  int position_to_linecol(int pos, int* lineNum, int* column) const;
483  int scroll_(int topLineNum, int horizOffset);
484 
485  void extend_range_for_styles(int* start, int* end);
486 
487  void find_wrap_range(const char *deletedText, int pos, int nInserted,
488  int nDeleted, int *modRangeStart, int *modRangeEnd,
489  int *linesInserted, int *linesDeleted);
490  void measure_deleted_lines(int pos, int nDeleted);
491  void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
492  int maxLines, bool startPosIsLineStart,
493  int styleBufOffset, int *retPos, int *retLines,
494  int *retLineStart, int *retLineEnd,
495  bool countLastLineMissingNewLine = true) const;
496  void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
497  int *nextLineStart) const;
498  double measure_proportional_character(const char *s, int colNum, int pos) const;
499  int wrap_uses_character(int lineEndPos) const;
500 
501  int damage_range1_start, damage_range1_end;
502  int damage_range2_start, damage_range2_end;
503  int mCursorPos;
504  int mCursorOn;
505  int mCursorOldY; /* Y pos. of cursor for blanking */
506  int mCursorToHint; /* Tells the buffer modified callback
507  where to move the cursor, to reduce
508  the number of redraw calls */
509  int mCursorStyle; /* One of enum cursorStyles above */
510  int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
511  int mNVisibleLines; /* # of visible (displayed) lines. This is
512  also the size of the mLineStarts[] array. */
513  int mNBufferLines; /* # of newlines in the buffer */
514  Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
515  Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
516  color and font information */
517  int mFirstChar, mLastChar; /* Buffer positions of first and last
518  displayed character (lastChar points
519  either to a newline or one character
520  beyond the end of the buffer) */
521  int mContinuousWrap; /* Wrap long lines when displaying */
522  int mWrapMarginPix; /* Margin in # of pixels for
523  wrapping in continuousWrap mode */
524  int* mLineStarts; /* Array of the size mNVisibleLines.
525  This array only keeps track of lines
526  within the display area. Each entry
527  contains the starting character offset
528  (from the beginning of the text buffer)
529  for each /visible/ line.
530  If wrap enabled, points to the beginning
531  of each wrap. So a long line wrapping
532  into 3 separate lines in the display
533  will take up 3 separate array entries. */
534  int mTopLineNum; /* Line number of top displayed line
535  of file (first line of file is 1) */
536  int mAbsTopLineNum; /* In continuous wrap mode, the line
537  number of the top line if the text
538  were not wrapped (note that this is
539  only maintained as needed). */
540  int mNeedAbsTopLineNum; /* Externally settable flag to continue
541  maintaining absTopLineNum even if
542  it isn't needed for line # display */
543  int mHorizOffset; /* Horizontal scroll pos. in pixels */
544  int mTopLineNumHint; /* Line number of top displayed line
545  of file (first line of file is 1) */
546  int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
547  int mNStyles; /* Number of entries in styleTable */
548  const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
549  coloring/syntax-highlighting */
550  char mUnfinishedStyle; /* Style buffer entry which triggers
551  on-the-fly reparsing of region */
552  Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
553  /* regions */
554  void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
555 
556  int mMaxsize;
557 
558  int mSuppressResync; /* Suppress resynchronization of line
559  starts during buffer updates */
560  int mNLinesDeleted; /* Number of lines deleted during
561  buffer modification (only used
562  when resynchronization is suppressed) */
563  int mModifyingTabDistance; /* Whether tab distance is being modified XXX: UNUSED */
564 
565  mutable double mColumnScale; /* Width in pixels of an average character. This
566  value is calculated as needed (lazy eval); it
567  needs to be mutable so that it can be calculated
568  within a method marked as "const" */
569 
570  Fl_Color mCursor_color;
571 
572  Fl_Scrollbar* mHScrollBar;
573  Fl_Scrollbar* mVScrollBar;
574  int scrollbar_width_; // size of scrollbar trough (behavior changed in 1.4)
575  Fl_Align scrollbar_align_;
576  int dragPos, dragType, dragging;
577  int display_insert_position_hint;
578  struct { int x, y, w, h; } text_area;
579 
580  int shortcut_;
581 
582  Fl_Font textfont_;
583  Fl_Fontsize textsize_;
584  Fl_Color textcolor_;
585 
586  // Line number margin and width
587  int mLineNumLeft, mLineNumWidth;
588 
589  // Line number font/colors
590  Fl_Font linenumber_font_;
591  Fl_Fontsize linenumber_size_;
592  Fl_Color linenumber_fgcolor_;
593  Fl_Color linenumber_bgcolor_;
594  Fl_Align linenumber_align_;
595  const char* linenumber_format_;
596 };
597 
598 #endif
Fl_Fontsize size
text font size
Definition: Fl_Text_Display.H:147
Fl_Color cursor_color() const
Gets the text cursor color.
Definition: Fl_Text_Display.H:224
void hide_cursor()
Hides the text cursor.
Definition: Fl_Text_Display.H:215
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
void scrollbar_size(int newSize)
Sets the pixel size of the scrollbars&#39; troughs to newSize, in pixels.
Definition: Fl_Text_Display.H:283
Fl static class.
don&#39;t wrap text at all
Definition: Fl_Text_Display.H:121
wrap text at the given text column
Definition: Fl_Text_Display.H:122
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:340
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition: Fl_Text_Display.H:297
This class manages Unicode text displayed in one or more Fl_Text_Display widgets. ...
Definition: Fl_Text_Buffer.H:196
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:346
int shortcut() const
Definition: Fl_Text_Display.H:327
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:42
dim I-beam
Definition: Fl_Text_Display.H:88
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition: Fl_Text_Display.H:291
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition: Fl_Text_Display.H:364
int scrollbar_size() const
Gets the current size of the scrollbars&#39; troughs, in pixels.
Definition: Fl_Text_Display.H:260
wrap text at a pixel position
Definition: Fl_Text_Display.H:123
int scrollbar_width() const
Returns the global value Fl::scrollbar_size() unless a specific scrollbar_width_ has been set...
Definition: Fl_Text_Display.H:237
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition: Fl_Text_Display.H:192
unfille box under the current character
Definition: Fl_Text_Display.H:89
Fl_Widget, Fl_Label classes .
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:173
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition: Fl_Text_Display.H:358
I-beam.
Definition: Fl_Text_Display.H:86
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:164
unsigned attr
currently unused (this may be changed in the future)
Definition: Fl_Text_Display.H:148
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:1014
Fl_Color color
text color
Definition: Fl_Text_Display.H:145
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition: Fl_Text_Display.H:370
utility header to pull drawing functions together
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition: Enumerations.H:1042
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:222
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:985
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition: Fl_Text_Display.H:230
unsigned Fl_Align
FLTK type for alignment control.
Definition: Enumerations.H:908
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:246
thick I-beam
Definition: Fl_Text_Display.H:90
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition: Fl_Text_Display.H:311
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition: Fl_Text_Display.H:352
caret under the text
Definition: Fl_Text_Display.H:87
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:41
Rich text display widget.
Definition: Fl_Text_Display.H:78
Fl_Font font
text font
Definition: Fl_Text_Display.H:146
void shortcut(int s)
Definition: Fl_Text_Display.H:334
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition: Fl_Text_Display.H:304