Fl_Text_Display.H 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // "$Id: Fl_Text_Display.H 7903 2010-11-28 21:06:39Z matt $"
  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; you can redistribute it and/or
  11. // modify it under the terms of the GNU Library General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Library General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Library General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. // USA.
  24. //
  25. // Please report all bugs and problems on the following page:
  26. //
  27. // http://www.fltk.org/str.php
  28. //
  29. /* \file
  30. Fl_Text_Display widget . */
  31. #ifndef FL_TEXT_DISPLAY_H
  32. #define FL_TEXT_DISPLAY_H
  33. #include "fl_draw.H"
  34. #include "Fl_Group.H"
  35. #include "Fl_Widget.H"
  36. #include "Fl_Scrollbar.H"
  37. #include "Fl_Text_Buffer.H"
  38. /**
  39. \brief Rich text display widget.
  40. This is the FLTK text display widget. It allows the user to view multiple lines
  41. of text and supports highlighting and scrolling. The buffer that is displayed
  42. in the widget is managed by the Fl_Text_Buffer class. A single Text Buffer
  43. can be displayed by multiple Text Displays.
  44. */
  45. class FL_EXPORT Fl_Text_Display: public Fl_Group
  46. {
  47. public:
  48. /**
  49. text display cursor shapes enumeration
  50. */
  51. enum {
  52. NORMAL_CURSOR, /**< I-beam */
  53. CARET_CURSOR, /**< caret under the text */
  54. DIM_CURSOR, /**< dim I-beam */
  55. BLOCK_CURSOR, /**< unfille box under the current character */
  56. HEAVY_CURSOR /**< thick I-beam */
  57. };
  58. /**
  59. the character position is the left edge of a character wheras
  60. the cursor is thought to be between the centers of to consecutive
  61. characters.
  62. */
  63. enum {
  64. CURSOR_POS,
  65. CHARACTER_POS
  66. };
  67. /**
  68. drag types - they match Fl::event_clicks() so that single clicking to
  69. start a collection selects by character, double clicking selects by
  70. word and triple clicking selects by line.
  71. */
  72. enum {
  73. DRAG_NONE = -2,
  74. DRAG_START_DND = -1,
  75. DRAG_CHAR = 0,
  76. DRAG_WORD = 1,
  77. DRAG_LINE = 2
  78. };
  79. /**
  80. wrap types - usedin wrap_mode()
  81. */
  82. enum {
  83. WRAP_NONE, /**< don't wrap text at all */
  84. WRAP_AT_COLUMN, /**< wrap text at the given text column */
  85. WRAP_AT_PIXEL, /**< wrap text at a pixel position */
  86. WRAP_AT_BOUNDS /**< wrap text so that it fits into the widget width */
  87. };
  88. friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
  89. typedef void (*Unfinished_Style_Cb)(int, void *);
  90. /**
  91. This structure associates the color,font,size of a string to draw
  92. with an attribute mask matching attr
  93. */
  94. struct Style_Table_Entry {
  95. Fl_Color color;
  96. Fl_Font font;
  97. Fl_Fontsize size;
  98. unsigned attr;
  99. };
  100. Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
  101. ~Fl_Text_Display();
  102. virtual int handle(int e);
  103. void buffer(Fl_Text_Buffer* buf);
  104. /**
  105. Sets the current text buffer associated with the text widget.
  106. Multiple text widgets can be associated with the same text buffer.
  107. \param buf new text buffer
  108. */
  109. void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
  110. /**
  111. Gets the current text buffer associated with the text widget.
  112. Multiple text widgets can be associated with the same text buffer.
  113. \return current text buffer
  114. */
  115. Fl_Text_Buffer* buffer() const { return mBuffer; }
  116. void redisplay_range(int start, int end);
  117. void scroll(int topLineNum, int horizOffset);
  118. void insert(const char* text);
  119. void overstrike(const char* text);
  120. void insert_position(int newPos);
  121. /**
  122. Gets the position of the text insertion cursor for text display
  123. \return insert position index into text buffer
  124. */
  125. int insert_position() const { return mCursorPos; }
  126. int in_selection(int x, int y) const;
  127. void show_insert_position();
  128. int move_right();
  129. int move_left();
  130. int move_up();
  131. int move_down();
  132. int count_lines(int start, int end, bool start_pos_is_line_start) const;
  133. int line_start(int pos) const;
  134. int line_end(int pos, bool start_pos_is_line_start) const;
  135. int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
  136. int rewind_lines(int startPos, int nLines);
  137. void next_word(void);
  138. void previous_word(void);
  139. void show_cursor(int b = 1);
  140. /**
  141. Hides the text cursor
  142. */
  143. void hide_cursor() { show_cursor(0); }
  144. void cursor_style(int style);
  145. /**
  146. Gets the text cursor color.
  147. \return cursor color
  148. */
  149. Fl_Color cursor_color() const {return mCursor_color;}
  150. /**
  151. Sets the text cursor color.
  152. \param n new cursor color
  153. */
  154. void cursor_color(Fl_Color n) {mCursor_color = n;}
  155. /**
  156. Gets the width/height of the scrollbars.
  157. /return width of scrollbars
  158. */
  159. int scrollbar_width() const { return scrollbar_width_; }
  160. /**
  161. Sets the width/height of the scrollbars.
  162. \param W width of scrollbars
  163. */
  164. void scrollbar_width(int W) { scrollbar_width_ = W; }
  165. /**
  166. Gets the scrollbar alignment type
  167. \return scrollbar alignment
  168. */
  169. Fl_Align scrollbar_align() const { return scrollbar_align_; }
  170. /**
  171. Sets the scrollbar alignment type
  172. \param a new scrollbar alignment
  173. */
  174. void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
  175. /**
  176. Moves the insert position to the beginning of the current word.
  177. \param pos start calculation at this index
  178. \return beginning of the wors
  179. */
  180. int word_start(int pos) const { return buffer()->word_start(pos); }
  181. /**
  182. Moves the insert position to the end of the current word.
  183. \param pos start calculation at this index
  184. \return index of first character after the end of the word
  185. */
  186. int word_end(int pos) const { return buffer()->word_end(pos); }
  187. void highlight_data(Fl_Text_Buffer *styleBuffer,
  188. const Style_Table_Entry *styleTable,
  189. int nStyles, char unfinishedStyle,
  190. Unfinished_Style_Cb unfinishedHighlightCB,
  191. void *cbArg);
  192. int position_style(int lineStartPos, int lineLen, int lineIndex) const;
  193. /**
  194. \todo FIXME : get set methods pointing on shortcut_
  195. have no effects as shortcut_ is unused in this class and derived!
  196. \return the current shortcut key
  197. */
  198. int shortcut() const {return shortcut_;}
  199. /**
  200. \todo FIXME : get set methods pointing on shortcut_
  201. have no effects as shortcut_ is unused in this class and derived!
  202. \param s the new shortcut key
  203. */
  204. void shortcut(int s) {shortcut_ = s;}
  205. /**
  206. Gets the default font used when drawing text in the widget.
  207. \return current text font face unless overriden by a style
  208. */
  209. Fl_Font textfont() const {return textfont_;}
  210. /**
  211. Sets the default font used when drawing text in the widget.
  212. \param s default text font face
  213. */
  214. void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;}
  215. /**
  216. Gets the default size of text in the widget.
  217. \return current text height unless overriden by a style
  218. */
  219. Fl_Fontsize textsize() const {return textsize_;}
  220. /**
  221. Sets the default size of text in the widget.
  222. \param s new text size
  223. */
  224. void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;}
  225. /**
  226. Gets the default color of text in the widget.
  227. \return text color unless overriden by a style
  228. */
  229. Fl_Color textcolor() const {return textcolor_;}
  230. /**
  231. Sets the default color of text in the widget.
  232. \param n new text color
  233. */
  234. void textcolor(Fl_Color n) {textcolor_ = n;}
  235. int wrapped_column(int row, int column) const;
  236. int wrapped_row(int row) const;
  237. void wrap_mode(int wrap, int wrap_margin);
  238. virtual void resize(int X, int Y, int W, int H);
  239. /**
  240. Convert an x pixel position into a column number.
  241. \param x number of pixels form the left margin
  242. \return an approximate column number based on the main font
  243. */
  244. double x_to_col(double y) const;
  245. /**
  246. Convert a column number into an x pixel position.
  247. \param col an approximate column number based on the main font
  248. \return number of pixels form the left margin to the left of an average
  249. sized character
  250. */
  251. double col_to_x(double col) const;
  252. protected:
  253. // Most (all?) of this stuff should only be called from resize() or
  254. // draw().
  255. // Anything with "vline" indicates thats it deals with currently
  256. // visible lines.
  257. virtual void draw();
  258. void draw_text(int X, int Y, int W, int H);
  259. void draw_range(int start, int end);
  260. void draw_cursor(int, int);
  261. void draw_string(int style, int x, int y, int toX, const char *string,
  262. int nChars) const;
  263. void draw_vline(int visLineNum, int leftClip, int rightClip,
  264. int leftCharIndex, int rightCharIndex);
  265. int find_x(const char *s, int len, int style, int x) const;
  266. enum {
  267. DRAW_LINE,
  268. FIND_INDEX,
  269. FIND_INDEX_FROM_ZERO,
  270. GET_WIDTH
  271. };
  272. int handle_vline(int mode,
  273. int lineStart, int lineLen, int leftChar, int rightChar,
  274. int topClip, int bottomClip,
  275. int leftClip, int rightClip) const;
  276. void draw_line_numbers(bool clearAll);
  277. void clear_rect(int style, int x, int y, int width, int height) const;
  278. void display_insert();
  279. void offset_line_starts(int newTopLineNum);
  280. void calc_line_starts(int startLine, int endLine);
  281. void update_line_starts(int pos, int charsInserted, int charsDeleted,
  282. int linesInserted, int linesDeleted, int *scrolled);
  283. void calc_last_char();
  284. int position_to_line( int pos, int* lineNum ) const;
  285. double string_width(const char* string, int length, int style) const;
  286. static void scroll_timer_cb(void*);
  287. static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
  288. static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
  289. int nRestyled, const char* deletedText,
  290. void* cbArg);
  291. static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
  292. static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
  293. void update_v_scrollbar();
  294. void update_h_scrollbar();
  295. int measure_vline(int visLineNum) const;
  296. int longest_vline() const;
  297. int empty_vlines() const;
  298. int vline_length(int visLineNum) const;
  299. int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
  300. void xy_to_rowcol(int x, int y, int* row, int* column,
  301. int PosType = CHARACTER_POS) const;
  302. int position_to_xy(int pos, int* x, int* y) const;
  303. void maintain_absolute_top_line_number(int state);
  304. int get_absolute_top_line_number() const;
  305. void absolute_top_line_number(int oldFirstChar);
  306. int maintaining_absolute_top_line_number() const;
  307. void reset_absolute_top_line_number();
  308. int position_to_linecol(int pos, int* lineNum, int* column) const;
  309. int scroll_(int topLineNum, int horizOffset);
  310. void extend_range_for_styles(int* start, int* end);
  311. void find_wrap_range(const char *deletedText, int pos, int nInserted,
  312. int nDeleted, int *modRangeStart, int *modRangeEnd,
  313. int *linesInserted, int *linesDeleted);
  314. void measure_deleted_lines(int pos, int nDeleted);
  315. void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
  316. int maxLines, bool startPosIsLineStart,
  317. int styleBufOffset, int *retPos, int *retLines,
  318. int *retLineStart, int *retLineEnd,
  319. bool countLastLineMissingNewLine = true) const;
  320. void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
  321. int *nextLineStart) const;
  322. double measure_proportional_character(const char *s, int colNum, int pos) const;
  323. int wrap_uses_character(int lineEndPos) const;
  324. int damage_range1_start, damage_range1_end;
  325. int damage_range2_start, damage_range2_end;
  326. int mCursorPos;
  327. int mCursorOn;
  328. int mCursorOldY; /* Y pos. of cursor for blanking */
  329. int mCursorToHint; /* Tells the buffer modified callback
  330. where to move the cursor, to reduce
  331. the number of redraw calls */
  332. int mCursorStyle; /* One of enum cursorStyles above */
  333. int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
  334. int mNVisibleLines; /* # of visible (displayed) lines */
  335. int mNBufferLines; /* # of newlines in the buffer */
  336. Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
  337. Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
  338. color and font information */
  339. int mFirstChar, mLastChar; /* Buffer positions of first and last
  340. displayed character (lastChar points
  341. either to a newline or one character
  342. beyond the end of the buffer) */
  343. int mContinuousWrap; /* Wrap long lines when displaying */
  344. int mWrapMarginPix; /* Margin in # of pixels for
  345. wrapping in continuousWrap mode */
  346. int* mLineStarts;
  347. int mTopLineNum; /* Line number of top displayed line
  348. of file (first line of file is 1) */
  349. int mAbsTopLineNum; /* In continuous wrap mode, the line
  350. number of the top line if the text
  351. were not wrapped (note that this is
  352. only maintained as needed). */
  353. int mNeedAbsTopLineNum; /* Externally settable flag to continue
  354. maintaining absTopLineNum even if
  355. it isn't needed for line # display */
  356. int mHorizOffset; /* Horizontal scroll pos. in pixels */
  357. int mTopLineNumHint; /* Line number of top displayed line
  358. of file (first line of file is 1) */
  359. int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
  360. int mNStyles; /* Number of entries in styleTable */
  361. const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
  362. coloring/syntax-highlighting */
  363. char mUnfinishedStyle; /* Style buffer entry which triggers
  364. on-the-fly reparsing of region */
  365. Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
  366. /* regions */
  367. void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
  368. int mMaxsize;
  369. int mSuppressResync; /* Suppress resynchronization of line
  370. starts during buffer updates */
  371. int mNLinesDeleted; /* Number of lines deleted during
  372. buffer modification (only used
  373. when resynchronization is suppressed) */
  374. int mModifyingTabDistance; /* Whether tab distance is being
  375. modified */
  376. mutable double mColumnScale; /* Width in pixels of an average character. This
  377. value is calculated as needed (lazy eval); it
  378. needs to be mutable so that it can be calculated
  379. within a method marked as "const" */
  380. Fl_Color mCursor_color;
  381. Fl_Scrollbar* mHScrollBar;
  382. Fl_Scrollbar* mVScrollBar;
  383. int scrollbar_width_;
  384. Fl_Align scrollbar_align_;
  385. int dragPos, dragType, dragging;
  386. int display_insert_position_hint;
  387. struct { int x, y, w, h; } text_area;
  388. int shortcut_;
  389. Fl_Font textfont_;
  390. Fl_Fontsize textsize_;
  391. Fl_Color textcolor_;
  392. // The following are not presently used from the original NEdit code,
  393. // but are being put here so that future versions of Fl_Text_Display
  394. // can implement line numbers without breaking binary compatibility.
  395. /* Line number margin and width */
  396. int mLineNumLeft, mLineNumWidth;
  397. };
  398. #endif
  399. //
  400. // End of "$Id: Fl_Text_Display.H 7903 2010-11-28 21:06:39Z matt $".
  401. //