FLTK 1.4.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 void resize(int X, int Y, int W, int H) FL_OVERRIDE;
437
443 double x_to_col(double x) const;
444
451 double col_to_x(double col) const;
452
453 void linenumber_width(int width);
454 int linenumber_width() const;
455 void linenumber_font(Fl_Font val);
456 Fl_Font linenumber_font() const;
457 void linenumber_size(Fl_Fontsize val);
458 Fl_Fontsize linenumber_size() const;
459 void linenumber_fgcolor(Fl_Color val);
460 Fl_Color linenumber_fgcolor() const;
461 void linenumber_bgcolor(Fl_Color val);
462 Fl_Color linenumber_bgcolor() const;
463 void linenumber_align(Fl_Align val);
464 Fl_Align linenumber_align() const;
465 void linenumber_format(const char* val);
466 const char* linenumber_format() const;
467
468protected:
469 // Most (all?) of this stuff should only be called from resize() or
470 // draw().
471 // Anything with "vline" indicates thats it deals with currently
472 // visible lines.
473
474 void draw() FL_OVERRIDE;
475 void draw_text(int X, int Y, int W, int H);
476 void draw_range(int start, int end);
477 void draw_cursor(int, int);
478
479 void draw_string(int style, int x, int y, int toX, const char *string,
480 int nChars) const;
481
482 void draw_vline(int visLineNum, int leftClip, int rightClip,
483 int leftCharIndex, int rightCharIndex);
484
485 int find_x(const char *s, int len, int style, int x) const;
486
487 enum {
488 DRAW_LINE,
489 FIND_INDEX,
490 FIND_INDEX_FROM_ZERO,
491 GET_WIDTH,
492 FIND_CURSOR_INDEX // STR #2788
493 };
494
495 int handle_vline(int mode,
496 int lineStart, int lineLen, int leftChar, int rightChar,
497 int topClip, int bottomClip,
498 int leftClip, int rightClip) const;
499
500 int handle_rmb(int readonly);
501
502 void draw_line_numbers(bool clearAll);
503
504 void clear_rect(int style, int x, int y, int width, int height) const;
505 void display_insert();
506
507 void offset_line_starts(int newTopLineNum);
508
509 void calc_line_starts(int startLine, int endLine);
510
511 void update_line_starts(int pos, int charsInserted, int charsDeleted,
512 int linesInserted, int linesDeleted, int *scrolled);
513
514 void calc_last_char();
515
516 int position_to_line( int pos, int* lineNum ) const;
517 double string_width(const char* string, int length, int style) const;
518
519 static void scroll_timer_cb(void*);
520
521 static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
522 static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
523 int nRestyled, const char* deletedText,
524 void* cbArg);
525
526 static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
527 static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
528 void update_v_scrollbar();
529 void update_h_scrollbar();
530 int measure_vline(int visLineNum) const;
531 int longest_vline() const;
532 int empty_vlines() const;
533 int vline_length(int visLineNum) const;
534 int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
535
536 void xy_to_rowcol(int x, int y, int* row, int* column,
537 int PosType = CHARACTER_POS) const;
538 void maintain_absolute_top_line_number(int state);
539 int get_absolute_top_line_number() const;
540 void absolute_top_line_number(int oldFirstChar);
541 int maintaining_absolute_top_line_number() const;
542 void reset_absolute_top_line_number();
543 int position_to_linecol(int pos, int* lineNum, int* column) const;
544 int scroll_(int topLineNum, int horizOffset);
545
546 void extend_range_for_styles(int* start, int* end);
547
548 void find_wrap_range(const char *deletedText, int pos, int nInserted,
549 int nDeleted, int *modRangeStart, int *modRangeEnd,
550 int *linesInserted, int *linesDeleted);
551 void measure_deleted_lines(int pos, int nDeleted);
552 void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
553 int maxLines, bool startPosIsLineStart,
554 int styleBufOffset, int *retPos, int *retLines,
555 int *retLineStart, int *retLineEnd,
556 bool countLastLineMissingNewLine = true) const;
557 void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
558 int *nextLineStart) const;
559 double measure_proportional_character(const char *s, int colNum, int pos) const;
560 int wrap_uses_character(int lineEndPos) const;
561
562 int damage_range1_start, damage_range1_end;
563 int damage_range2_start, damage_range2_end;
564 int mCursorPos;
565 int mCursorOn;
566 int mCursorOldY; /* Y pos. of cursor for blanking */
567 int mCursorToHint; /* Tells the buffer modified callback
568 where to move the cursor, to reduce
569 the number of redraw calls */
570 int mCursorStyle; /* One of enum cursorStyles above */
571 int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
572 int mNVisibleLines; /* # of visible (displayed) lines. This is
573 also the size of the mLineStarts[] array. */
574 int mNBufferLines; /* # of newlines in the buffer */
575 Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
576 Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
577 color and font information */
578 int mFirstChar, mLastChar; /* Buffer positions of first and last
579 displayed character (lastChar points
580 either to a newline or one character
581 beyond the end of the buffer) */
582 int mContinuousWrap; /* Wrap long lines when displaying */
583 int mWrapMarginPix; /* Margin in # of pixels for
584 wrapping in continuousWrap mode */
585 int* mLineStarts; /* Array of the size mNVisibleLines.
586 This array only keeps track of lines
587 within the display area. Each entry
588 contains the starting character offset
589 (from the beginning of the text buffer)
590 for each /visible/ line.
591 If wrap enabled, points to the beginning
592 of each wrap. So a long line wrapping
593 into 3 separate lines in the display
594 will take up 3 separate array entries. */
595 int mTopLineNum; /* Line number of top displayed line
596 of file (first line of file is 1) */
597 int mAbsTopLineNum; /* In continuous wrap mode, the line
598 number of the top line if the text
599 were not wrapped (note that this is
600 only maintained as needed). */
601 int mNeedAbsTopLineNum; /* Externally settable flag to continue
602 maintaining absTopLineNum even if
603 it isn't needed for line # display */
604 int mHorizOffset; /* Horizontal scroll pos. in pixels */
605 int mTopLineNumHint; /* Line number of top displayed line
606 of file (first line of file is 1) */
607 int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
608 int mNStyles; /* Number of entries in styleTable */
609 const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
610 coloring/syntax-highlighting */
611 char mUnfinishedStyle; /* Style buffer entry which triggers
612 on-the-fly reparsing of region */
613 Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
614 /* regions */
615 void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
616
617 int mMaxsize;
618
619 int mSuppressResync; /* Suppress resynchronization of line
620 starts during buffer updates */
621 int mNLinesDeleted; /* Number of lines deleted during
622 buffer modification (only used
623 when resynchronization is suppressed) */
624 int mModifyingTabDistance; /* Whether tab distance is being modified XXX: UNUSED */
625
626 mutable double mColumnScale; /* Width in pixels of an average character. This
627 value is calculated as needed (lazy eval); it
628 needs to be mutable so that it can be calculated
629 within a method marked as "const" */
630
631 Fl_Color mCursor_color;
632
633 Fl_Scrollbar* mHScrollBar;
634 Fl_Scrollbar* mVScrollBar;
635 int scrollbar_width_; // size of scrollbar trough (behavior changed in 1.4)
636 Fl_Align scrollbar_align_;
637 int dragPos, dragType, dragging;
638 int display_insert_position_hint;
639 struct { int x, y, w, h; } text_area;
640
641 int shortcut_;
642
643 Fl_Font textfont_;
644 Fl_Fontsize textsize_;
645 Fl_Color textcolor_;
646 Fl_Color grammar_underline_color_;
647 Fl_Color spelling_underline_color_;
648 Fl_Color secondary_selection_color_;
649
650 // Line number margin and width
651 int mLineNumLeft, mLineNumWidth;
652
653 // Line number font/colors
654 Fl_Font linenumber_font_;
655 Fl_Fontsize linenumber_size_;
656 Fl_Color linenumber_fgcolor_;
657 Fl_Color linenumber_bgcolor_;
658 Fl_Align linenumber_align_;
659 const char* linenumber_format_;
660};
661
662#endif
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:1044
unsigned Fl_Align
FLTK type for alignment control.
Definition Enumerations.H:967
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1101
int Fl_Fontsize
Size of a font in pixels.
Definition Enumerations.H:1073
Fl static class.
Fl_Group and Fl_End classes.
Fl_Widget and Fl_Label classes.
The Fl_Group class is the FLTK container widget.
Definition Fl_Group.H:56
int handle(int) FL_OVERRIDE
Handles the specified event.
Definition Fl_Group.cxx:145
void resize(int, int, int, int) FL_OVERRIDE
Resizes the Fl_Group widget and all of its children.
Definition Fl_Group.cxx:823
void draw() FL_OVERRIDE
Draws the widget.
Definition Fl_Group.cxx:926
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:509
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
@ 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: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
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
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
@ 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: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:447
int y() const
Gets the widget position in its window.
Definition Fl_Widget.H:358
int h() const
Gets the widget height.
Definition Fl_Widget.H:368
int w() const
Gets the widget width.
Definition Fl_Widget.H:363
int x() const
Gets the widget position in its window.
Definition Fl_Widget.H:353
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:46
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