FLTK 1.5.0
Loading...
Searching...
No Matches
Fl_Text_Display.H
1//
2// Header file for Fl_Text_Display class.
3//
4// Copyright 2001-2023 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
78class FL_EXPORT Fl_Text_Display: public Fl_Group {
79
80public:
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 int fl_text_drag_prepare(int pos, int key, Fl_Text_Display* d);
128 friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
129
130 typedef void (*Unfinished_Style_Cb)(int, void *);
131
152
156 enum {
157 ATTR_BGCOLOR = 0x0001,
158 ATTR_BGCOLOR_EXT_ = 0x0002,
159 ATTR_BGCOLOR_EXT = 0x0003,
160 ATTR_UNDERLINE = 0x0004,
161 ATTR_GRAMMAR = 0x0008,
162 ATTR_SPELLING = 0x000C,
163 ATTR_STRIKE_THROUGH = 0x0010,
164 ATTR_LINES_MASK = 0x001C
165 };
166
167 Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
169
170 int handle(int e) FL_OVERRIDE;
171
172 void buffer(Fl_Text_Buffer* buf);
173
180 void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
181
189 Fl_Text_Buffer* buffer() const { return mBuffer; }
196 Fl_Text_Buffer* style_buffer() const { return mStyleBuffer; }
197
198 void redisplay_range(int start, int end);
199 void scroll(int topLineNum, int horizOffset);
200 void insert(const char* text);
201 void overstrike(const char* text);
202 void insert_position(int newPos);
203
215 int insert_position() const { return mCursorPos; }
216 int position_to_xy(int pos, int* x, int* y) const;
217
218 int in_selection(int x, int y) const;
219 void show_insert_position();
220
221 int move_right();
222 int move_left();
223 int move_up();
224 int move_down();
225 int count_lines(int start, int end, bool start_pos_is_line_start) const;
226 int line_start(int pos) const;
227 int line_end(int startPos, bool startPosIsLineStart) const;
228 int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
229 int rewind_lines(int startPos, int nLines);
230 void next_word(void);
231 void previous_word(void);
232
233 void show_cursor(int b = 1);
234
238 void hide_cursor() { show_cursor(0); }
239
240 void cursor_style(int style);
241 int cursor_style() const { return mCursorStyle; }
242
247 Fl_Color cursor_color() const {return mCursor_color;}
248
253 void cursor_color(Fl_Color n) {mCursor_color = n;}
254
260 int scrollbar_width() const {
261 return scrollbar_width_ ? scrollbar_width_ : Fl::scrollbar_size();
262 }
263
269 void scrollbar_width(int width) {
270 Fl::scrollbar_size(width);
271 scrollbar_width_ = 0;
272 }
273
283 int scrollbar_size() const {
284 return(scrollbar_width_);
285 }
286
306 void scrollbar_size(int newSize) {
307 scrollbar_width_ = newSize;
308 }
309
314 Fl_Align scrollbar_align() const { return scrollbar_align_; }
315
320 void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
321
327 int word_start(int pos) const { return buffer()->word_start(pos); }
328
334 int word_end(int pos) const { return buffer()->word_end(pos); }
335
336
337 void highlight_data(Fl_Text_Buffer *styleBuffer,
338 const Style_Table_Entry *styleTable,
339 int nStyles, char unfinishedStyle,
340 Unfinished_Style_Cb unfinishedHighlightCB,
341 void *cbArg);
342
343 int position_style(int lineStartPos, int lineLen, int lineIndex) const;
344
350 int shortcut() const {return shortcut_;}
351
357 void shortcut(int s) {shortcut_ = s;}
358
363 Fl_Font textfont() const {return textfont_;}
364
369 void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0; }
370
375 Fl_Fontsize textsize() const {return textsize_;}
376
381 void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0; }
382
387 Fl_Color textcolor() const {return textcolor_;}
388
393 void textcolor(Fl_Color n) {textcolor_ = n;}
394
399 void grammar_underline_color(Fl_Color color) { grammar_underline_color_ = color; }
400
405 Fl_Color grammar_underline_color() const { return grammar_underline_color_;}
406
411 void spelling_underline_color(Fl_Color color) { spelling_underline_color_ = color; }
412
417 Fl_Color spelling_underline_color() const { return spelling_underline_color_;}
418
423 void secondary_selection_color(Fl_Color color) { secondary_selection_color_ = color; }
424
429 Fl_Color secondary_selection_color() const { return secondary_selection_color_;}
430
431 int wrapped_column(int row, int column) const;
432 int wrapped_row(int row) const;
433 void wrap_mode(int wrap, int wrap_margin);
434
435 virtual void recalc_display();
436 virtual void display_needs_recalc();
437 void resize(int X, int Y, int W, int H) FL_OVERRIDE;
438
444 double x_to_col(double x) const;
445
452 double col_to_x(double col) const;
453
454 void linenumber_width(int width);
455 int linenumber_width() const;
456 void linenumber_font(Fl_Font val);
457 Fl_Font linenumber_font() const;
458 void linenumber_size(Fl_Fontsize val);
459 Fl_Fontsize linenumber_size() const;
460 void linenumber_fgcolor(Fl_Color val);
461 Fl_Color linenumber_fgcolor() const;
462 void linenumber_bgcolor(Fl_Color val);
463 Fl_Color linenumber_bgcolor() const;
464 void linenumber_align(Fl_Align val);
465 Fl_Align linenumber_align() const;
466 void linenumber_format(const char* val);
467 const char* linenumber_format() const;
468
469protected:
470 // Most (all?) of this stuff should only be called from resize() or
471 // draw().
472 // Anything with "vline" indicates thats it deals with currently
473 // visible lines.
474
475 void draw() FL_OVERRIDE;
476 void draw_text(int X, int Y, int W, int H);
477 void draw_range(int start, int end);
478 void draw_cursor(int, int);
479
480 void draw_string(int style, int x, int y, int toX, const char *string,
481 int nChars) const;
482
483 void draw_vline(int visLineNum, int leftClip, int rightClip,
484 int leftCharIndex, int rightCharIndex);
485
486 int find_x(const char *s, int len, int style, int x) const;
487
488 enum {
489 DRAW_LINE,
490 FIND_INDEX,
491 FIND_INDEX_FROM_ZERO,
492 GET_WIDTH,
493 FIND_CURSOR_INDEX // STR #2788
494 };
495
496 int handle_vline(int mode,
497 int lineStart, int lineLen, int leftChar, int rightChar,
498 int topClip, int bottomClip,
499 int leftClip, int rightClip) const;
500
501 int handle_rmb(int readonly);
502
503 void draw_line_numbers(bool clearAll);
504
505 void clear_rect(int style, int x, int y, int width, int height) const;
506 void display_insert();
507
508 void offset_line_starts(int newTopLineNum);
509
510 void calc_line_starts(int startLine, int endLine);
511
512 void update_line_starts(int pos, int charsInserted, int charsDeleted,
513 int linesInserted, int linesDeleted, int *scrolled);
514
515 void calc_last_char();
516
517 int position_to_line( int pos, int* lineNum ) const;
518 double string_width(const char* string, int length, int style) const;
519
520 static void scroll_timer_cb(void*);
521
522 static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
523 static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
524 int nRestyled, const char* deletedText,
525 void* cbArg);
526
527 static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
528 static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
529 void update_v_scrollbar();
530 void update_h_scrollbar();
531 int measure_vline(int visLineNum) const;
532 int longest_vline() const;
533 int empty_vlines() const;
534 int vline_length(int visLineNum) const;
535 int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
536
537 void xy_to_rowcol(int x, int y, int* row, int* column,
538 int PosType = CHARACTER_POS) const;
539 void maintain_absolute_top_line_number(int state);
540public:
541 int get_absolute_top_line_number() const;
542 int scroll_row() { return mTopLineNum; }
543 int scroll_col() { return mHorizOffset; }
544protected:
545 void absolute_top_line_number(int oldFirstChar);
546 int maintaining_absolute_top_line_number() const;
547 void reset_absolute_top_line_number();
548 int position_to_linecol(int pos, int* lineNum, int* column) const;
549 int scroll_(int topLineNum, int horizOffset);
550
551 void extend_range_for_styles(int* start, int* end);
552
553 void find_wrap_range(const char *deletedText, int pos, int nInserted,
554 int nDeleted, int *modRangeStart, int *modRangeEnd,
555 int *linesInserted, int *linesDeleted);
556 void measure_deleted_lines(int pos, int nDeleted);
557 void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
558 int maxLines, bool startPosIsLineStart,
559 int styleBufOffset, int *retPos, int *retLines,
560 int *retLineStart, int *retLineEnd,
561 bool countLastLineMissingNewLine = true) const;
562 void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
563 int *nextLineStart) const;
564 double measure_proportional_character(const char *s, int colNum, int pos) const;
565 int wrap_uses_character(int lineEndPos) const;
566
567 int damage_range1_start, damage_range1_end;
568 int damage_range2_start, damage_range2_end;
569 int mCursorPos;
570 int mCursorOn;
571 int mCursorOldY; /* Y pos. of cursor for blanking */
572 int mCursorToHint; /* Tells the buffer modified callback
573 where to move the cursor, to reduce
574 the number of redraw calls */
575 int mCursorStyle; /* One of enum cursorStyles above */
576 int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
577 int mNVisibleLines; /* # of visible (displayed) lines. This is
578 also the size of the mLineStarts[] array. */
579 int mNBufferLines; /* # of newlines in the buffer, or number of
580 wraps if line wrapping is enabled. Note that
581 partial lines at the end of the buffer are
582 not counted, so you may want to add 1. */
583 Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
584 Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
585 color and font information */
586 int mFirstChar, mLastChar; /* Buffer positions of first and last
587 displayed character (lastChar points
588 either to a newline or one character
589 beyond the end of the buffer) */
590 int mContinuousWrap; /* Wrap long lines when displaying */
591 int mWrapMarginPix; /* Margin in # of pixels for
592 wrapping in continuousWrap mode */
593 int* mLineStarts; /* Array of the size mNVisibleLines.
594 This array only keeps track of lines
595 within the display area. Each entry
596 contains the starting character offset
597 (from the beginning of the text buffer)
598 for each /visible/ line.
599 If wrap enabled, points to the beginning
600 of each wrap. So a long line wrapping
601 into 3 separate lines in the display
602 will take up 3 separate array entries. */
603 int mTopLineNum; /* Line number of top displayed line
604 of file (first line of file is 1) */
605 int mAbsTopLineNum; /* In continuous wrap mode, the line
606 number of the top line if the text
607 were not wrapped (note that this is
608 only maintained as needed). */
609 int mNeedAbsTopLineNum; /* Externally settable flag to continue
610 maintaining absTopLineNum even if
611 it isn't needed for line # display */
612 int mHorizOffset; /* Horizontal scroll pos. in pixels */
613 int mTopLineNumHint; /* Line number of top displayed line
614 of file (first line of file is 1) */
615 int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
616 int mNStyles; /* Number of entries in styleTable */
617 const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
618 coloring/syntax-highlighting */
619 char mUnfinishedStyle; /* Style buffer entry which triggers
620 on-the-fly reparsing of region */
621 Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
622 /* regions */
623 void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
624
625 int mMaxsize;
626
627 int mSuppressResync; /* Suppress resynchronization of line
628 starts during buffer updates */
629 int mNLinesDeleted; /* Number of lines deleted during
630 buffer modification (only used
631 when resynchronization is suppressed) */
632 int mModifyingTabDistance; /* Whether tab distance is being modified XXX: UNUSED */
633
634 mutable double mColumnScale; /* Width in pixels of an average character. This
635 value is calculated as needed (lazy eval); it
636 needs to be mutable so that it can be calculated
637 within a method marked as "const" */
638
639 bool display_needs_recalc_; /* Set to true when the display needs
640 to be recalculated. */
641
642 Fl_Color mCursor_color;
643
644 Fl_Scrollbar* mHScrollBar;
645 Fl_Scrollbar* mVScrollBar;
646 int scrollbar_width_; // size of scrollbar trough (behavior changed in 1.4)
647 Fl_Align scrollbar_align_;
648 int dragPos, dragType, dragging;
649 int display_insert_position_hint;
650 struct { int x, y, w, h; } text_area;
651
652 int shortcut_;
653
654 Fl_Font textfont_;
655 Fl_Fontsize textsize_;
656 Fl_Color textcolor_;
657 Fl_Color grammar_underline_color_;
658 Fl_Color spelling_underline_color_;
659 Fl_Color secondary_selection_color_;
660
661 // Line number margin and width
662 int mLineNumLeft, mLineNumWidth;
663
664 // Line number font/colors
665 Fl_Font linenumber_font_;
666 Fl_Fontsize linenumber_size_;
667 Fl_Color linenumber_fgcolor_;
668 Fl_Color linenumber_bgcolor_;
669 Fl_Align linenumber_align_;
670 const char* linenumber_format_;
671};
672
673#endif
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:1057
unsigned Fl_Align
FLTK type for alignment control.
Definition Enumerations.H:980
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1114
int Fl_Fontsize
Size of a font in pixels.
Definition Enumerations.H:1086
Fl static class.
Fl_Group and Fl_End classes.
Fl_Widget and Fl_Label classes.
The Fl_Group class is the main FLTK container widget.
Definition Fl_Group.H:59
int handle(int) FL_OVERRIDE
Handles the specified event.
Definition Fl_Group.cxx:148
void resize(int, int, int, int) FL_OVERRIDE
Resizes the Fl_Group widget and all of its children.
Definition Fl_Group.cxx:809
void draw() FL_OVERRIDE
Draws the widget.
Definition Fl_Group.cxx:928
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:501
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:201
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:260
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition Fl_Text_Display.H:381
void hide_cursor()
Hides the text cursor.
Definition Fl_Text_Display.H:238
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition Fl_Text_Display.H:253
void scrollbar_size(int newSize)
Sets the pixel size of the scrollbars' troughs to newSize, in pixels.
Definition Fl_Text_Display.H:306
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition Fl_Text_Display.H:393
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition Fl_Text_Display.H:314
void shortcut(int s)
Definition Fl_Text_Display.H:357
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition Fl_Text_Display.H:189
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:269
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition Fl_Text_Display.H:320
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition Fl_Text_Display.H:327
int scrollbar_size() const
Gets the current size of the scrollbars' troughs, in pixels.
Definition Fl_Text_Display.H:283
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition Fl_Text_Display.H:334
@ 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_Color cursor_color() const
Gets the text cursor color.
Definition Fl_Text_Display.H:247
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition Fl_Text_Display.H:387
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition Fl_Text_Display.H:369
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition Fl_Text_Display.H:215
Fl_Color grammar_underline_color() const
Gets the underline color for style attribute ATTR_GRAMMAR.
Definition Fl_Text_Display.H:405
int shortcut() const
Definition Fl_Text_Display.H:350
@ 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
Fl_Color secondary_selection_color() const
Gets the background color for the secondary selection block.
Definition Fl_Text_Display.H:429
Fl_Color spelling_underline_color() const
Gets the underline color for style attribute ATTR_SPELLING.
Definition Fl_Text_Display.H:417
Fl_Text_Buffer * style_buffer() const
Gets the current style buffer associated with the text widget.
Definition Fl_Text_Display.H:196
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition Fl_Text_Display.H:375
void grammar_underline_color(Fl_Color color)
Sets the underline color for style attribute ATTR_GRAMMAR.
Definition Fl_Text_Display.H:399
void spelling_underline_color(Fl_Color color)
Sets the underline color for style attribute ATTR_SPELLING.
Definition Fl_Text_Display.H:411
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition Fl_Text_Display.H:180
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition Fl_Text_Display.H:363
void secondary_selection_color(Fl_Color color)
Sets the background color for the secondary selection block.
Definition Fl_Text_Display.H:423
Fl_Color color() const
Gets the background color of the widget.
Definition Fl_Widget.H:455
int y() const
Gets the widget position in its window.
Definition Fl_Widget.H:366
int h() const
Gets the widget height.
Definition Fl_Widget.H:376
int w() const
Gets the widget width.
Definition Fl_Widget.H:371
int x() const
Gets the widget position in its window.
Definition Fl_Widget.H:361
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:148
#define FL_OVERRIDE
This macro makes it safe to use the C++11 keyword override with older compilers.
Definition fl_attr.h:38
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:145
Fl_Color color
text color
Definition Fl_Text_Display.H:146
Fl_Fontsize size
text font size
Definition Fl_Text_Display.H:148
Fl_Font font
text font
Definition Fl_Text_Display.H:147
unsigned attr
further attributes for the text style (see ATTR_BGCOLOR, etc.)
Definition Fl_Text_Display.H:149
Fl_Color bgcolor
text background color if ATTR_BGCOLOR or ATTR_BGCOLOR_EXT is set
Definition Fl_Text_Display.H:150