Fl_Text_Display.H 17 KB

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