Fl_Text_Display.H 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. //
  2. // "$Id: Fl_Text_Display.H 12570 2017-11-23 19:34:33Z greg.ercolano $"
  3. //
  4. // Header file for Fl_Text_Display class.
  5. //
  6. // Copyright 2001-2017 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. /* \file
  21. Fl_Text_Display widget . */
  22. #ifndef FL_TEXT_DISPLAY_H
  23. #define FL_TEXT_DISPLAY_H
  24. #include <FL/Fl.H> // Fl::scrollbar_size()
  25. #include "fl_draw.H"
  26. #include "Fl_Group.H"
  27. #include "Fl_Widget.H"
  28. #include "Fl_Scrollbar.H"
  29. #include "Fl_Text_Buffer.H"
  30. /**
  31. \brief Rich text display widget.
  32. This is the FLTK text display widget. It allows the user to view
  33. multiple lines of text and supports highlighting, word wrap, mixes
  34. of font faces and colors, line numbers and scrolling. The buffer
  35. that is displayed in the widget is managed by the Fl_Text_Buffer
  36. class. A single Text Buffer can be displayed by multiple Text
  37. Displays.
  38. \image html editor.png "Fl_Text_Display widget"
  39. \image latex editor.png "Fl_Text_Display widget" width=6cm
  40. <BR><BR>
  41. \image html editor-linenumbers.png "Fl_Text_Display widget with line numbers enabled"
  42. \image latex editor-linenumbers.png "Fl_Text_Display widget with line numbers enabled" width=6cm
  43. \b Example \b Use
  44. \code
  45. #include <FL/FL_Text_Display.H>
  46. ..
  47. int main() {
  48. ..
  49. Fl_Text_Buffer *buff = new Fl_Text_Buffer();
  50. Fl_Text_Display *disp = new Fl_Text_Display(10, 10, 640, 480);
  51. disp->buffer(buff); // attach text buffer to display widget
  52. buff->text("line one\nline two"); // add some text to buffer
  53. ..
  54. }
  55. \endcode
  56. \b Features
  57. - Word wrap: wrap_mode(), wrapped_column(), wrapped_row()
  58. - Font control: textfont(), textsize(), textcolor()
  59. - Font styling: highlight_data()
  60. - Cursor: cursor_style(), show_cursor(), hide_cursor(), cursor_color()
  61. - Line numbers: linenumber_width(), linenumber_font(),
  62. linenumber_size(), linenumber_fgcolor(), linenumber_bgcolor(),
  63. linenumber_align(), linenumber_format()
  64. Note that other features may be available via Fl_Text_Editor
  65. and Fl_Text_Buffer classes.
  66. \note Line numbers were added in FLTK 1.3.3.
  67. */
  68. class FL_EXPORT Fl_Text_Display: public Fl_Group {
  69. public:
  70. /**
  71. text display cursor shapes enumeration
  72. */
  73. enum {
  74. NORMAL_CURSOR, /**< I-beam */
  75. CARET_CURSOR, /**< caret under the text */
  76. DIM_CURSOR, /**< dim I-beam */
  77. BLOCK_CURSOR, /**< unfille box under the current character */
  78. HEAVY_CURSOR, /**< thick I-beam */
  79. SIMPLE_CURSOR /**< as cursor as Fl_Input cursor */
  80. };
  81. /**
  82. the character position is the left edge of a character, whereas
  83. the cursor is thought to be between the centers of two consecutive
  84. characters.
  85. */
  86. enum {
  87. CURSOR_POS,
  88. CHARACTER_POS
  89. };
  90. /**
  91. drag types - they match Fl::event_clicks() so that single clicking to
  92. start a collection selects by character, double clicking selects by
  93. word and triple clicking selects by line.
  94. */
  95. enum {
  96. DRAG_NONE = -2,
  97. DRAG_START_DND = -1,
  98. DRAG_CHAR = 0,
  99. DRAG_WORD = 1,
  100. DRAG_LINE = 2
  101. };
  102. /**
  103. wrap types - used in wrap_mode()
  104. */
  105. enum {
  106. WRAP_NONE, /**< don't wrap text at all */
  107. WRAP_AT_COLUMN, /**< wrap text at the given text column */
  108. WRAP_AT_PIXEL, /**< wrap text at a pixel position */
  109. WRAP_AT_BOUNDS /**< wrap text so that it fits into the widget width */
  110. };
  111. friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
  112. typedef void (*Unfinished_Style_Cb)(int, void *);
  113. /**
  114. This structure associates the color, font, and font size of a string to draw
  115. with an attribute mask matching attr.
  116. There must be one entry for each style that can be used in an
  117. Fl_Text_Display for displaying text. The style table is an array of
  118. struct Style_Table_Entry.
  119. The style table is associated with an Fl_Text_Display by using
  120. Fl_Text_Display::highlight_data().
  121. \see Fl_Text_Display::highlight_data()
  122. */
  123. struct Style_Table_Entry {
  124. Fl_Color color; ///< text color
  125. Fl_Font font; ///< text font
  126. Fl_Fontsize size; ///< text font size
  127. unsigned attr; ///< currently unused (this may be changed in the future)
  128. };
  129. Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
  130. ~Fl_Text_Display();
  131. virtual int handle(int e);
  132. void buffer(Fl_Text_Buffer* buf);
  133. /**
  134. Sets the current text buffer associated with the text widget.
  135. Multiple text widgets can be associated with the same text buffer.
  136. \param buf new text buffer
  137. \see Fl_Text_Display::buffer(Fl_Text_Buffer* buf)
  138. */
  139. void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
  140. /**
  141. Gets the current text buffer associated with the text widget.
  142. Multiple text widgets can be associated with the same text buffer.
  143. \return current text buffer
  144. \see Fl_Text_Display::buffer(Fl_Text_Buffer* buf)
  145. \see Fl_Text_Display::buffer(Fl_Text_Buffer& buf)
  146. */
  147. Fl_Text_Buffer* buffer() const { return mBuffer; }
  148. void redisplay_range(int start, int end);
  149. void scroll(int topLineNum, int horizOffset);
  150. void insert(const char* text);
  151. void overstrike(const char* text);
  152. void insert_position(int newPos);
  153. /**
  154. Gets the position of the text insertion cursor for text display.
  155. \return insert position index into text buffer
  156. */
  157. int insert_position() const { return mCursorPos; }
  158. int position_to_xy(int pos, int* x, int* y) const;
  159. int in_selection(int x, int y) const;
  160. void show_insert_position();
  161. int move_right();
  162. int move_left();
  163. int move_up();
  164. int move_down();
  165. int count_lines(int start, int end, bool start_pos_is_line_start) const;
  166. int line_start(int pos) const;
  167. int line_end(int startPos, bool startPosIsLineStart) const;
  168. int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
  169. int rewind_lines(int startPos, int nLines);
  170. void next_word(void);
  171. void previous_word(void);
  172. void show_cursor(int b = 1);
  173. /**
  174. Hides the text cursor.
  175. */
  176. void hide_cursor() { show_cursor(0); }
  177. void cursor_style(int style);
  178. int cursor_style() const { return mCursorStyle; }
  179. /**
  180. Gets the text cursor color.
  181. \return cursor color
  182. */
  183. Fl_Color cursor_color() const {return mCursor_color;}
  184. /**
  185. Sets the text cursor color.
  186. \param n new cursor color
  187. */
  188. void cursor_color(Fl_Color n) {mCursor_color = n;}
  189. /**
  190. This method has been deprecated, existing for backwards compatibility only.
  191. Use scrollbar_size() instead.
  192. This method returns the global value Fl::scrollbar_size() unless
  193. a specific scrollbar_width_ has been set.
  194. \todo This method should eventually be removed.
  195. */
  196. int scrollbar_width() const {
  197. return scrollbar_width_ ? scrollbar_width_ : Fl::scrollbar_size();
  198. }
  199. /**
  200. This method has been deprecated, existing for backwards compatibility only.
  201. Use scrollbar_size(int) instead.
  202. This method sets the global Fl::scrollbar_size(), and forces this
  203. instance of the widget to use it.
  204. \todo This method should eventually be removed
  205. */
  206. void scrollbar_width(int width) {
  207. Fl::scrollbar_size(width);
  208. scrollbar_width_ = 0;
  209. }
  210. /**
  211. Gets the current size of the scrollbars' troughs, in pixels.
  212. If this value is zero (default), this widget will use the
  213. Fl::scrollbar_size() value as the scrollbar's width.
  214. \returns Scrollbar size in pixels, or 0 if the global Fl::scrollbar_size() is being used.
  215. \see Fl::scrollbar_size(int)
  216. */
  217. int scrollbar_size() const {
  218. return(scrollbar_width_);
  219. }
  220. /**
  221. Sets the pixel size of the scrollbars' troughs to \p newSize, in pixels.
  222. Normally you should not need this method, and should use
  223. Fl::scrollbar_size(int) instead to manage the size of ALL
  224. your widgets' scrollbars. This ensures your application
  225. has a consistent UI, is the default behavior, and is normally
  226. what you want.
  227. Only use THIS method if you really need to override the global
  228. scrollbar size. The need for this should be rare.
  229. Setting \p newSize to the special value of 0 causes the widget to
  230. track the global Fl::scrollbar_size(), which is the default.
  231. \param[in] newSize Sets the scrollbar size in pixels.\n
  232. If 0 (default), scrollbar size tracks the global Fl::scrollbar_size()
  233. \see Fl::scrollbar_size()
  234. */
  235. void scrollbar_size(int newSize) {
  236. scrollbar_width_ = newSize;
  237. }
  238. /**
  239. Gets the scrollbar alignment type.
  240. \return scrollbar alignment
  241. */
  242. Fl_Align scrollbar_align() const { return scrollbar_align_; }
  243. /**
  244. Sets the scrollbar alignment type.
  245. \param a new scrollbar alignment
  246. */
  247. void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
  248. /**
  249. Moves the insert position to the beginning of the current word.
  250. \param pos start calculation at this index
  251. \return beginning of the words
  252. */
  253. int word_start(int pos) const { return buffer()->word_start(pos); }
  254. /**
  255. Moves the insert position to the end of the current word.
  256. \param pos start calculation at this index
  257. \return index of first character after the end of the word
  258. */
  259. int word_end(int pos) const { return buffer()->word_end(pos); }
  260. void highlight_data(Fl_Text_Buffer *styleBuffer,
  261. const Style_Table_Entry *styleTable,
  262. int nStyles, char unfinishedStyle,
  263. Unfinished_Style_Cb unfinishedHighlightCB,
  264. void *cbArg);
  265. int position_style(int lineStartPos, int lineLen, int lineIndex) const;
  266. /**
  267. \todo FIXME : get set methods pointing on shortcut_
  268. have no effects as shortcut_ is unused in this class and derived!
  269. \return the current shortcut key
  270. */
  271. int shortcut() const {return shortcut_;}
  272. /**
  273. \todo FIXME : get set methods pointing on shortcut_
  274. have no effects as shortcut_ is unused in this class and derived!
  275. \param s the new shortcut key
  276. */
  277. void shortcut(int s) {shortcut_ = s;}
  278. /**
  279. Gets the default font used when drawing text in the widget.
  280. \return current text font face unless overridden by a style
  281. */
  282. Fl_Font textfont() const {return textfont_;}
  283. /**
  284. Sets the default font used when drawing text in the widget.
  285. \param s default text font face
  286. */
  287. void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0; }
  288. /**
  289. Gets the default size of text in the widget.
  290. \return current text height unless overridden by a style
  291. */
  292. Fl_Fontsize textsize() const {return textsize_;}
  293. /**
  294. Sets the default size of text in the widget.
  295. \param s new text size
  296. */
  297. void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0; }
  298. /**
  299. Gets the default color of text in the widget.
  300. \return text color unless overridden by a style
  301. */
  302. Fl_Color textcolor() const {return textcolor_;}
  303. /**
  304. Sets the default color of text in the widget.
  305. \param n new text color
  306. */
  307. void textcolor(Fl_Color n) {textcolor_ = n;}
  308. int wrapped_column(int row, int column) const;
  309. int wrapped_row(int row) const;
  310. void wrap_mode(int wrap, int wrap_margin);
  311. virtual void recalc_display();
  312. virtual void resize(int X, int Y, int W, int H);
  313. /**
  314. Convert an x pixel position into a column number.
  315. \param x number of pixels from the left margin
  316. \return an approximate column number based on the main font
  317. */
  318. double x_to_col(double x) const;
  319. /**
  320. Convert a column number into an x pixel position.
  321. \param col an approximate column number based on the main font
  322. \return number of pixels from the left margin to the left of an
  323. average sized character
  324. */
  325. double col_to_x(double col) const;
  326. void linenumber_width(int width);
  327. int linenumber_width() const;
  328. void linenumber_font(Fl_Font val);
  329. Fl_Font linenumber_font() const;
  330. void linenumber_size(Fl_Fontsize val);
  331. Fl_Fontsize linenumber_size() const;
  332. void linenumber_fgcolor(Fl_Color val);
  333. Fl_Color linenumber_fgcolor() const;
  334. void linenumber_bgcolor(Fl_Color val);
  335. Fl_Color linenumber_bgcolor() const;
  336. void linenumber_align(Fl_Align val);
  337. Fl_Align linenumber_align() const;
  338. void linenumber_format(const char* val);
  339. const char* linenumber_format() const;
  340. protected:
  341. // Most (all?) of this stuff should only be called from resize() or
  342. // draw().
  343. // Anything with "vline" indicates thats it deals with currently
  344. // visible lines.
  345. virtual void draw();
  346. void draw_text(int X, int Y, int W, int H);
  347. void draw_range(int start, int end);
  348. void draw_cursor(int, int);
  349. void draw_string(int style, int x, int y, int toX, const char *string,
  350. int nChars) const;
  351. void draw_vline(int visLineNum, int leftClip, int rightClip,
  352. int leftCharIndex, int rightCharIndex);
  353. int find_x(const char *s, int len, int style, int x) const;
  354. enum {
  355. DRAW_LINE,
  356. FIND_INDEX,
  357. FIND_INDEX_FROM_ZERO,
  358. GET_WIDTH,
  359. FIND_CURSOR_INDEX // STR #2788
  360. };
  361. int handle_vline(int mode,
  362. int lineStart, int lineLen, int leftChar, int rightChar,
  363. int topClip, int bottomClip,
  364. int leftClip, int rightClip) const;
  365. void draw_line_numbers(bool clearAll);
  366. void clear_rect(int style, int x, int y, int width, int height) const;
  367. void display_insert();
  368. void offset_line_starts(int newTopLineNum);
  369. void calc_line_starts(int startLine, int endLine);
  370. void update_line_starts(int pos, int charsInserted, int charsDeleted,
  371. int linesInserted, int linesDeleted, int *scrolled);
  372. void calc_last_char();
  373. int position_to_line( int pos, int* lineNum ) const;
  374. double string_width(const char* string, int length, int style) const;
  375. static void scroll_timer_cb(void*);
  376. static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
  377. static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
  378. int nRestyled, const char* deletedText,
  379. void* cbArg);
  380. static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
  381. static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
  382. void update_v_scrollbar();
  383. void update_h_scrollbar();
  384. int measure_vline(int visLineNum) const;
  385. int longest_vline() const;
  386. int empty_vlines() const;
  387. int vline_length(int visLineNum) const;
  388. int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
  389. void xy_to_rowcol(int x, int y, int* row, int* column,
  390. int PosType = CHARACTER_POS) const;
  391. void maintain_absolute_top_line_number(int state);
  392. int get_absolute_top_line_number() const;
  393. void absolute_top_line_number(int oldFirstChar);
  394. int maintaining_absolute_top_line_number() const;
  395. void reset_absolute_top_line_number();
  396. int position_to_linecol(int pos, int* lineNum, int* column) const;
  397. int scroll_(int topLineNum, int horizOffset);
  398. void extend_range_for_styles(int* start, int* end);
  399. void find_wrap_range(const char *deletedText, int pos, int nInserted,
  400. int nDeleted, int *modRangeStart, int *modRangeEnd,
  401. int *linesInserted, int *linesDeleted);
  402. void measure_deleted_lines(int pos, int nDeleted);
  403. void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
  404. int maxLines, bool startPosIsLineStart,
  405. int styleBufOffset, int *retPos, int *retLines,
  406. int *retLineStart, int *retLineEnd,
  407. bool countLastLineMissingNewLine = true) const;
  408. void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
  409. int *nextLineStart) const;
  410. double measure_proportional_character(const char *s, int colNum, int pos) const;
  411. int wrap_uses_character(int lineEndPos) const;
  412. int damage_range1_start, damage_range1_end;
  413. int damage_range2_start, damage_range2_end;
  414. int mCursorPos;
  415. int mCursorOn;
  416. int mCursorOldY; /* Y pos. of cursor for blanking */
  417. int mCursorToHint; /* Tells the buffer modified callback
  418. where to move the cursor, to reduce
  419. the number of redraw calls */
  420. int mCursorStyle; /* One of enum cursorStyles above */
  421. int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
  422. int mNVisibleLines; /* # of visible (displayed) lines. This is
  423. also the size of the mLineStarts[] array. */
  424. int mNBufferLines; /* # of newlines in the buffer */
  425. Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
  426. Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
  427. color and font information */
  428. int mFirstChar, mLastChar; /* Buffer positions of first and last
  429. displayed character (lastChar points
  430. either to a newline or one character
  431. beyond the end of the buffer) */
  432. int mContinuousWrap; /* Wrap long lines when displaying */
  433. int mWrapMarginPix; /* Margin in # of pixels for
  434. wrapping in continuousWrap mode */
  435. int* mLineStarts; /* Array of the size mNVisibleLines.
  436. This array only keeps track of lines
  437. within the display area. Each entry
  438. contains the starting character offset
  439. (from the beginning of the text buffer)
  440. for each /visible/ line.
  441. If wrap enabled, points to the beginning
  442. of each wrap. So a long line wrapping
  443. into 3 separate lines in the display
  444. will take up 3 separate array entries. */
  445. int mTopLineNum; /* Line number of top displayed line
  446. of file (first line of file is 1) */
  447. int mAbsTopLineNum; /* In continuous wrap mode, the line
  448. number of the top line if the text
  449. were not wrapped (note that this is
  450. only maintained as needed). */
  451. int mNeedAbsTopLineNum; /* Externally settable flag to continue
  452. maintaining absTopLineNum even if
  453. it isn't needed for line # display */
  454. int mHorizOffset; /* Horizontal scroll pos. in pixels */
  455. int mTopLineNumHint; /* Line number of top displayed line
  456. of file (first line of file is 1) */
  457. int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
  458. int mNStyles; /* Number of entries in styleTable */
  459. const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
  460. coloring/syntax-highlighting */
  461. char mUnfinishedStyle; /* Style buffer entry which triggers
  462. on-the-fly reparsing of region */
  463. Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
  464. /* regions */
  465. void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
  466. int mMaxsize;
  467. int mSuppressResync; /* Suppress resynchronization of line
  468. starts during buffer updates */
  469. int mNLinesDeleted; /* Number of lines deleted during
  470. buffer modification (only used
  471. when resynchronization is suppressed) */
  472. int mModifyingTabDistance; /* Whether tab distance is being modified XXX: UNUSED */
  473. mutable double mColumnScale; /* Width in pixels of an average character. This
  474. value is calculated as needed (lazy eval); it
  475. needs to be mutable so that it can be calculated
  476. within a method marked as "const" */
  477. Fl_Color mCursor_color;
  478. Fl_Scrollbar* mHScrollBar;
  479. Fl_Scrollbar* mVScrollBar;
  480. int scrollbar_width_; // size of scrollbar trough (behavior changed in 1.4)
  481. Fl_Align scrollbar_align_;
  482. int dragPos, dragType, dragging;
  483. int display_insert_position_hint;
  484. struct { int x, y, w, h; } text_area;
  485. int shortcut_;
  486. Fl_Font textfont_;
  487. Fl_Fontsize textsize_;
  488. Fl_Color textcolor_;
  489. // Line number margin and width
  490. int mLineNumLeft, mLineNumWidth;
  491. // Line number font/colors
  492. Fl_Font linenumber_font_;
  493. Fl_Fontsize linenumber_size_;
  494. Fl_Color linenumber_fgcolor_;
  495. Fl_Color linenumber_bgcolor_;
  496. Fl_Align linenumber_align_;
  497. const char* linenumber_format_;
  498. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Text_Display, Fl_Group)
  499. };
  500. #endif
  501. //
  502. // End of "$Id: Fl_Text_Display.H 12570 2017-11-23 19:34:33Z greg.ercolano $".
  503. //