Fl_Text_Display.H 19 KB

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