You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
16KB

  1. //
  2. // "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z 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. public:
  47. /**
  48. text display cursor shapes enumeration
  49. */
  50. enum {
  51. NORMAL_CURSOR, /**< I-beam */
  52. CARET_CURSOR, /**< caret under the text */
  53. DIM_CURSOR, /**< dim I-beam */
  54. BLOCK_CURSOR, /**< unfille box under the current character */
  55. HEAVY_CURSOR /**< thick I-beam */
  56. };
  57. /**
  58. the character position is the left edge of a character, whereas
  59. the cursor is thought to be between the centers of two consecutive
  60. characters.
  61. */
  62. enum {
  63. CURSOR_POS,
  64. CHARACTER_POS
  65. };
  66. /**
  67. drag types - they match Fl::event_clicks() so that single clicking to
  68. start a collection selects by character, double clicking selects by
  69. word and triple clicking selects by line.
  70. */
  71. enum {
  72. DRAG_NONE = -2,
  73. DRAG_START_DND = -1,
  74. DRAG_CHAR = 0,
  75. DRAG_WORD = 1,
  76. DRAG_LINE = 2
  77. };
  78. /**
  79. wrap types - used in wrap_mode()
  80. */
  81. enum {
  82. WRAP_NONE, /**< don't wrap text at all */
  83. WRAP_AT_COLUMN, /**< wrap text at the given text column */
  84. WRAP_AT_PIXEL, /**< wrap text at a pixel position */
  85. WRAP_AT_BOUNDS /**< wrap text so that it fits into the widget width */
  86. };
  87. friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
  88. typedef void (*Unfinished_Style_Cb)(int, void *);
  89. /**
  90. This structure associates the color, font, andsize of a string to draw
  91. with an attribute mask matching attr
  92. */
  93. struct Style_Table_Entry {
  94. Fl_Color color;
  95. Fl_Font font;
  96. Fl_Fontsize size;
  97. unsigned attr;
  98. };
  99. Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
  100. ~Fl_Text_Display();
  101. virtual int handle(int e);
  102. void buffer(Fl_Text_Buffer* buf);
  103. /**
  104. Sets the current text buffer associated with the text widget.
  105. Multiple text widgets can be associated with the same text buffer.
  106. \param buf new text buffer
  107. */
  108. void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
  109. /**
  110. Gets the current text buffer associated with the text widget.
  111. Multiple text widgets can be associated with the same text buffer.
  112. \return current text buffer
  113. */
  114. Fl_Text_Buffer* buffer() const { return mBuffer; }
  115. void redisplay_range(int start, int end);
  116. void scroll(int topLineNum, int horizOffset);
  117. void insert(const char* text);
  118. void overstrike(const char* text);
  119. void insert_position(int newPos);
  120. /**
  121. Gets the position of the text insertion cursor for text display.
  122. \return insert position index into text buffer
  123. */
  124. int insert_position() const { return mCursorPos; }
  125. int position_to_xy(int pos, int* x, int* y) const;
  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 startPos, bool startPosIsLineStart) 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 words
  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 overridden 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 overridden 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 overridden 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 from the left margin
  242. \return an approximate column number based on the main font
  243. */
  244. double x_to_col(double x) 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 from the left margin to the left of an
  249. average 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. void maintain_absolute_top_line_number(int state);
  303. int get_absolute_top_line_number() const;
  304. void absolute_top_line_number(int oldFirstChar);
  305. int maintaining_absolute_top_line_number() const;
  306. void reset_absolute_top_line_number();
  307. int position_to_linecol(int pos, int* lineNum, int* column) const;
  308. int scroll_(int topLineNum, int horizOffset);
  309. void extend_range_for_styles(int* start, int* end);
  310. void find_wrap_range(const char *deletedText, int pos, int nInserted,
  311. int nDeleted, int *modRangeStart, int *modRangeEnd,
  312. int *linesInserted, int *linesDeleted);
  313. void measure_deleted_lines(int pos, int nDeleted);
  314. void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
  315. int maxLines, bool startPosIsLineStart,
  316. int styleBufOffset, int *retPos, int *retLines,
  317. int *retLineStart, int *retLineEnd,
  318. bool countLastLineMissingNewLine = true) const;
  319. void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
  320. int *nextLineStart) const;
  321. double measure_proportional_character(const char *s, int colNum, int pos) const;
  322. int wrap_uses_character(int lineEndPos) const;
  323. int damage_range1_start, damage_range1_end;
  324. int damage_range2_start, damage_range2_end;
  325. int mCursorPos;
  326. int mCursorOn;
  327. int mCursorOldY; /* Y pos. of cursor for blanking */
  328. int mCursorToHint; /* Tells the buffer modified callback
  329. where to move the cursor, to reduce
  330. the number of redraw calls */
  331. int mCursorStyle; /* One of enum cursorStyles above */
  332. int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
  333. int mNVisibleLines; /* # of visible (displayed) lines */
  334. int mNBufferLines; /* # of newlines in the buffer */
  335. Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
  336. Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
  337. color and font information */
  338. int mFirstChar, mLastChar; /* Buffer positions of first and last
  339. displayed character (lastChar points
  340. either to a newline or one character
  341. beyond the end of the buffer) */
  342. int mContinuousWrap; /* Wrap long lines when displaying */
  343. int mWrapMarginPix; /* Margin in # of pixels for
  344. wrapping in continuousWrap mode */
  345. int* mLineStarts;
  346. int mTopLineNum; /* Line number of top displayed line
  347. of file (first line of file is 1) */
  348. int mAbsTopLineNum; /* In continuous wrap mode, the line
  349. number of the top line if the text
  350. were not wrapped (note that this is
  351. only maintained as needed). */
  352. int mNeedAbsTopLineNum; /* Externally settable flag to continue
  353. maintaining absTopLineNum even if
  354. it isn't needed for line # display */
  355. int mHorizOffset; /* Horizontal scroll pos. in pixels */
  356. int mTopLineNumHint; /* Line number of top displayed line
  357. of file (first line of file is 1) */
  358. int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
  359. int mNStyles; /* Number of entries in styleTable */
  360. const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
  361. coloring/syntax-highlighting */
  362. char mUnfinishedStyle; /* Style buffer entry which triggers
  363. on-the-fly reparsing of region */
  364. Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
  365. /* regions */
  366. void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
  367. int mMaxsize;
  368. int mSuppressResync; /* Suppress resynchronization of line
  369. starts during buffer updates */
  370. int mNLinesDeleted; /* Number of lines deleted during
  371. buffer modification (only used
  372. when resynchronization is suppressed) */
  373. int mModifyingTabDistance; /* Whether tab distance is being
  374. modified */
  375. mutable double mColumnScale; /* Width in pixels of an average character. This
  376. value is calculated as needed (lazy eval); it
  377. needs to be mutable so that it can be calculated
  378. within a method marked as "const" */
  379. Fl_Color mCursor_color;
  380. Fl_Scrollbar* mHScrollBar;
  381. Fl_Scrollbar* mVScrollBar;
  382. int scrollbar_width_;
  383. Fl_Align scrollbar_align_;
  384. int dragPos, dragType, dragging;
  385. int display_insert_position_hint;
  386. struct { int x, y, w, h; } text_area;
  387. int shortcut_;
  388. Fl_Font textfont_;
  389. Fl_Fontsize textsize_;
  390. Fl_Color textcolor_;
  391. // The following are not presently used from the original NEdit code,
  392. // but are being put here so that future versions of Fl_Text_Display
  393. // can implement line numbers without breaking binary compatibility.
  394. /* Line number margin and width */
  395. int mLineNumLeft, mLineNumWidth;
  396. };
  397. #endif
  398. //
  399. // End of "$Id: Fl_Text_Display.H 8306 2011-01-24 17:04:22Z matt $".
  400. //