FLTK 1.3.3
Fl_Text_Display.H
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 
21 /* \file
22  Fl_Text_Display widget . */
23 
24 #ifndef FL_TEXT_DISPLAY_H
25 #define FL_TEXT_DISPLAY_H
26 
27 #include "fl_draw.H"
28 #include "Fl_Group.H"
29 #include "Fl_Widget.H"
30 #include "Fl_Scrollbar.H"
31 #include "Fl_Text_Buffer.H"
32 
82 class FL_EXPORT Fl_Text_Display: public Fl_Group {
83 
84 public:
85 
89  enum {
95  SIMPLE_CURSOR
96  };
97 
103  enum {
104  CURSOR_POS,
105  CHARACTER_POS
106  };
107 
113  enum {
114  DRAG_NONE = -2,
115  DRAG_START_DND = -1,
116  DRAG_CHAR = 0,
117  DRAG_WORD = 1,
118  DRAG_LINE = 2
119  };
120 
124  enum {
128  WRAP_AT_BOUNDS
129  };
130 
131  friend void fl_text_drag_me(int pos, Fl_Text_Display* d);
132 
133  typedef void (*Unfinished_Style_Cb)(int, void *);
134 
140  Fl_Color color;
141  Fl_Font font;
142  Fl_Fontsize size;
143  unsigned attr;
144  };
145 
146  Fl_Text_Display(int X, int Y, int W, int H, const char *l = 0);
147  ~Fl_Text_Display();
148 
149  virtual int handle(int e);
150 
151  void buffer(Fl_Text_Buffer* buf);
152 
158  void buffer(Fl_Text_Buffer& buf) { buffer(&buf); }
159 
165  Fl_Text_Buffer* buffer() const { return mBuffer; }
166 
167  void redisplay_range(int start, int end);
168  void scroll(int topLineNum, int horizOffset);
169  void insert(const char* text);
170  void overstrike(const char* text);
171  void insert_position(int newPos);
172 
177  int insert_position() const { return mCursorPos; }
178  int position_to_xy(int pos, int* x, int* y) const;
179 
180  int in_selection(int x, int y) const;
181  void show_insert_position();
182 
183  int move_right();
184  int move_left();
185  int move_up();
186  int move_down();
187  int count_lines(int start, int end, bool start_pos_is_line_start) const;
188  int line_start(int pos) const;
189  int line_end(int startPos, bool startPosIsLineStart) const;
190  int skip_lines(int startPos, int nLines, bool startPosIsLineStart);
191  int rewind_lines(int startPos, int nLines);
192  void next_word(void);
193  void previous_word(void);
194 
195  void show_cursor(int b = 1);
196 
200  void hide_cursor() { show_cursor(0); }
201 
202  void cursor_style(int style);
203 
208  Fl_Color cursor_color() const {return mCursor_color;}
209 
214  void cursor_color(Fl_Color n) {mCursor_color = n;}
215 
220  int scrollbar_width() const { return scrollbar_width_; }
221 
226  void scrollbar_width(int W) { scrollbar_width_ = W; }
227 
232  Fl_Align scrollbar_align() const { return scrollbar_align_; }
233 
238  void scrollbar_align(Fl_Align a) { scrollbar_align_ = a; }
239 
245  int word_start(int pos) const { return buffer()->word_start(pos); }
246 
252  int word_end(int pos) const { return buffer()->word_end(pos); }
253 
254 
255  void highlight_data(Fl_Text_Buffer *styleBuffer,
256  const Style_Table_Entry *styleTable,
257  int nStyles, char unfinishedStyle,
258  Unfinished_Style_Cb unfinishedHighlightCB,
259  void *cbArg);
260 
261  int position_style(int lineStartPos, int lineLen, int lineIndex) const;
262 
268  int shortcut() const {return shortcut_;}
269 
275  void shortcut(int s) {shortcut_ = s;}
276 
281  Fl_Font textfont() const {return textfont_;}
282 
287  void textfont(Fl_Font s) {textfont_ = s; mColumnScale = 0;}
288 
293  Fl_Fontsize textsize() const {return textsize_;}
294 
299  void textsize(Fl_Fontsize s) {textsize_ = s; mColumnScale = 0;}
300 
305  Fl_Color textcolor() const {return textcolor_;}
306 
311  void textcolor(Fl_Color n) {textcolor_ = n;}
312 
313  int wrapped_column(int row, int column) const;
314  int wrapped_row(int row) const;
315  void wrap_mode(int wrap, int wrap_margin);
316 
317  virtual void resize(int X, int Y, int W, int H);
318 
324  double x_to_col(double x) const;
325 
332  double col_to_x(double col) const;
333 
334  void linenumber_width(int width);
335  int linenumber_width() const;
336  void linenumber_font(Fl_Font val);
337  Fl_Font linenumber_font() const;
338  void linenumber_size(Fl_Fontsize val);
339  Fl_Fontsize linenumber_size() const;
340  void linenumber_fgcolor(Fl_Color val);
341  Fl_Color linenumber_fgcolor() const;
342  void linenumber_bgcolor(Fl_Color val);
343  Fl_Color linenumber_bgcolor() const;
344  void linenumber_align(Fl_Align val);
345  Fl_Align linenumber_align() const;
346  void linenumber_format(const char* val);
347  const char* linenumber_format() const;
348 
349 protected:
350  // Most (all?) of this stuff should only be called from resize() or
351  // draw().
352  // Anything with "vline" indicates thats it deals with currently
353  // visible lines.
354 
355  virtual void draw();
356  void draw_text(int X, int Y, int W, int H);
357  void draw_range(int start, int end);
358  void draw_cursor(int, int);
359 
360  void draw_string(int style, int x, int y, int toX, const char *string,
361  int nChars) const;
362 
363  void draw_vline(int visLineNum, int leftClip, int rightClip,
364  int leftCharIndex, int rightCharIndex);
365 
366  int find_x(const char *s, int len, int style, int x) const;
367 
368  enum {
369  DRAW_LINE,
370  FIND_INDEX,
371  FIND_INDEX_FROM_ZERO,
372  GET_WIDTH
373  };
374 
375  int handle_vline(int mode,
376  int lineStart, int lineLen, int leftChar, int rightChar,
377  int topClip, int bottomClip,
378  int leftClip, int rightClip) const;
379 
380  void draw_line_numbers(bool clearAll);
381 
382  void clear_rect(int style, int x, int y, int width, int height) const;
383  void display_insert();
384 
385  void offset_line_starts(int newTopLineNum);
386 
387  void calc_line_starts(int startLine, int endLine);
388 
389  void update_line_starts(int pos, int charsInserted, int charsDeleted,
390  int linesInserted, int linesDeleted, int *scrolled);
391 
392  void calc_last_char();
393 
394  int position_to_line( int pos, int* lineNum ) const;
395  double string_width(const char* string, int length, int style) const;
396 
397  static void scroll_timer_cb(void*);
398 
399  static void buffer_predelete_cb(int pos, int nDeleted, void* cbArg);
400  static void buffer_modified_cb(int pos, int nInserted, int nDeleted,
401  int nRestyled, const char* deletedText,
402  void* cbArg);
403 
404  static void h_scrollbar_cb(Fl_Scrollbar* w, Fl_Text_Display* d);
405  static void v_scrollbar_cb( Fl_Scrollbar* w, Fl_Text_Display* d);
406  void update_v_scrollbar();
407  void update_h_scrollbar();
408  int measure_vline(int visLineNum) const;
409  int longest_vline() const;
410  int empty_vlines() const;
411  int vline_length(int visLineNum) const;
412  int xy_to_position(int x, int y, int PosType = CHARACTER_POS) const;
413 
414  void xy_to_rowcol(int x, int y, int* row, int* column,
415  int PosType = CHARACTER_POS) const;
416  void maintain_absolute_top_line_number(int state);
417  int get_absolute_top_line_number() const;
418  void absolute_top_line_number(int oldFirstChar);
419  int maintaining_absolute_top_line_number() const;
420  void reset_absolute_top_line_number();
421  int position_to_linecol(int pos, int* lineNum, int* column) const;
422  int scroll_(int topLineNum, int horizOffset);
423 
424  void extend_range_for_styles(int* start, int* end);
425 
426  void find_wrap_range(const char *deletedText, int pos, int nInserted,
427  int nDeleted, int *modRangeStart, int *modRangeEnd,
428  int *linesInserted, int *linesDeleted);
429  void measure_deleted_lines(int pos, int nDeleted);
430  void wrapped_line_counter(Fl_Text_Buffer *buf, int startPos, int maxPos,
431  int maxLines, bool startPosIsLineStart,
432  int styleBufOffset, int *retPos, int *retLines,
433  int *retLineStart, int *retLineEnd,
434  bool countLastLineMissingNewLine = true) const;
435  void find_line_end(int pos, bool start_pos_is_line_start, int *lineEnd,
436  int *nextLineStart) const;
437  double measure_proportional_character(const char *s, int colNum, int pos) const;
438  int wrap_uses_character(int lineEndPos) const;
439 
440  int damage_range1_start, damage_range1_end;
441  int damage_range2_start, damage_range2_end;
442  int mCursorPos;
443  int mCursorOn;
444  int mCursorOldY; /* Y pos. of cursor for blanking */
445  int mCursorToHint; /* Tells the buffer modified callback
446  where to move the cursor, to reduce
447  the number of redraw calls */
448  int mCursorStyle; /* One of enum cursorStyles above */
449  int mCursorPreferredXPos; /* Pixel position for vert. cursor movement */
450  int mNVisibleLines; /* # of visible (displayed) lines */
451  int mNBufferLines; /* # of newlines in the buffer */
452  Fl_Text_Buffer* mBuffer; /* Contains text to be displayed */
453  Fl_Text_Buffer* mStyleBuffer; /* Optional parallel buffer containing
454  color and font information */
455  int mFirstChar, mLastChar; /* Buffer positions of first and last
456  displayed character (lastChar points
457  either to a newline or one character
458  beyond the end of the buffer) */
459  int mContinuousWrap; /* Wrap long lines when displaying */
460  int mWrapMarginPix; /* Margin in # of pixels for
461  wrapping in continuousWrap mode */
462  int* mLineStarts;
463  int mTopLineNum; /* Line number of top displayed line
464  of file (first line of file is 1) */
465  int mAbsTopLineNum; /* In continuous wrap mode, the line
466  number of the top line if the text
467  were not wrapped (note that this is
468  only maintained as needed). */
469  int mNeedAbsTopLineNum; /* Externally settable flag to continue
470  maintaining absTopLineNum even if
471  it isn't needed for line # display */
472  int mHorizOffset; /* Horizontal scroll pos. in pixels */
473  int mTopLineNumHint; /* Line number of top displayed line
474  of file (first line of file is 1) */
475  int mHorizOffsetHint; /* Horizontal scroll pos. in pixels */
476  int mNStyles; /* Number of entries in styleTable */
477  const Style_Table_Entry *mStyleTable; /* Table of fonts and colors for
478  coloring/syntax-highlighting */
479  char mUnfinishedStyle; /* Style buffer entry which triggers
480  on-the-fly reparsing of region */
481  Unfinished_Style_Cb mUnfinishedHighlightCB; /* Callback to parse "unfinished" */
482  /* regions */
483  void* mHighlightCBArg; /* Arg to unfinishedHighlightCB */
484 
485  int mMaxsize;
486 
487  int mSuppressResync; /* Suppress resynchronization of line
488  starts during buffer updates */
489  int mNLinesDeleted; /* Number of lines deleted during
490  buffer modification (only used
491  when resynchronization is suppressed) */
492  int mModifyingTabDistance; /* Whether tab distance is being
493  modified */
494 
495  mutable double mColumnScale; /* Width in pixels of an average character. This
496  value is calculated as needed (lazy eval); it
497  needs to be mutable so that it can be calculated
498  within a method marked as "const" */
499 
500  Fl_Color mCursor_color;
501 
502  Fl_Scrollbar* mHScrollBar;
503  Fl_Scrollbar* mVScrollBar;
504  int scrollbar_width_;
505  Fl_Align scrollbar_align_;
506  int dragPos, dragType, dragging;
507  int display_insert_position_hint;
508  struct { int x, y, w, h; } text_area;
509 
510  int shortcut_;
511 
512  Fl_Font textfont_;
513  Fl_Fontsize textsize_;
514  Fl_Color textcolor_;
515 
516  // Line number margin and width
517  int mLineNumLeft, mLineNumWidth;
518 
519  // Line number font/colors
520 #if FLTK_ABI_VERSION >= 10303
521  Fl_Font linenumber_font_;
522  Fl_Fontsize linenumber_size_;
523  Fl_Color linenumber_fgcolor_;
524  Fl_Color linenumber_bgcolor_;
525  Fl_Align linenumber_align_;
526  const char* linenumber_format_;
527 #endif
528 };
529 
530 #endif
531 
532 //
533 // End of "$Id: Fl_Text_Display.H 10152 2014-05-21 06:56:59Z greg.ercolano $".
534 //
void hide_cursor()
Hides the text cursor.
Definition: Fl_Text_Display.H:200
Fl_Font textfont() const
Gets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:281
thick I-beam
Definition: Fl_Text_Display.H:94
int scrollbar_width() const
Gets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:220
int x() const
Gets the widget position in its window.
Definition: Fl_Widget.H:273
wrap text at a pixel position
Definition: Fl_Text_Display.H:127
Fl_Align scrollbar_align() const
Gets the scrollbar alignment type.
Definition: Fl_Text_Display.H:232
This structure associates the color, font, andsize of a string to draw with an attribute mask matchin...
Definition: Fl_Text_Display.H:139
int h() const
Gets the widget height.
Definition: Fl_Widget.H:288
int insert_position() const
Gets the position of the text insertion cursor for text display.
Definition: Fl_Text_Display.H:177
void scrollbar_align(Fl_Align a)
Sets the scrollbar alignment type.
Definition: Fl_Text_Display.H:238
int word_end(int pos) const
Moves the insert position to the end of the current word.
Definition: Fl_Text_Display.H:252
This class manages unicode displayed in one or more Fl_Text_Display widgets.
Definition: Fl_Text_Buffer.H:157
int w() const
Gets the widget width.
Definition: Fl_Widget.H:283
Fl_Color cursor_color() const
Gets the text cursor color.
Definition: Fl_Text_Display.H:208
I-beam.
Definition: Fl_Text_Display.H:90
void draw()
Draws the widget.
Definition: Fl_Group.cxx:738
void textfont(Fl_Font s)
Sets the default font used when drawing text in the widget.
Definition: Fl_Text_Display.H:287
int y() const
Gets the widget position in its window.
Definition: Fl_Widget.H:278
void insert(Fl_Widget &, int i)
The widget is removed from its current group (if any) and then inserted into this group...
Definition: Fl_Group.cxx:458
The Fl_Group class is the FLTK container widget.
Definition: Fl_Group.H:41
int handle(int)
Handles the specified event.
Definition: Fl_Group.cxx:147
caret under the text
Definition: Fl_Text_Display.H:91
Fl_Widget, Fl_Label classes .
don't wrap text at all
Definition: Fl_Text_Display.H:125
void textsize(Fl_Fontsize s)
Sets the default size of text in the widget.
Definition: Fl_Text_Display.H:299
void buffer(Fl_Text_Buffer &buf)
Sets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:158
int Fl_Fontsize
Size of a font in pixels.
Definition: Enumerations.H:768
void resize(int, int, int, int)
Resizes the Fl_Group widget and all of its children.
Definition: Fl_Group.cxx:634
int shortcut() const
Definition: Fl_Text_Display.H:268
Fl_Fontsize textsize() const
Gets the default size of text in the widget.
Definition: Fl_Text_Display.H:293
unfille box under the current character
Definition: Fl_Text_Display.H:93
void textcolor(Fl_Color n)
Sets the default color of text in the widget.
Definition: Fl_Text_Display.H:311
utility header to pull drawing functions together
unsigned int Fl_Color
An FLTK color value; see also Colors.
Definition: Enumerations.H:796
int Fl_Font
A font number is an index into the internal font table.
Definition: Enumerations.H:739
void cursor_color(Fl_Color n)
Sets the text cursor color.
Definition: Fl_Text_Display.H:214
unsigned Fl_Align
FLTK type for alignment control.
Definition: Enumerations.H:690
wrap text at the given text column
Definition: Fl_Text_Display.H:126
int word_start(int pos) const
Moves the insert position to the beginning of the current word.
Definition: Fl_Text_Display.H:245
Fl_Text_Buffer * buffer() const
Gets the current text buffer associated with the text widget.
Definition: Fl_Text_Display.H:165
Fl_Color textcolor() const
Gets the default color of text in the widget.
Definition: Fl_Text_Display.H:305
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition: Fl_Scrollbar.H:43
Rich text display widget.
Definition: Fl_Text_Display.H:82
void shortcut(int s)
Definition: Fl_Text_Display.H:275
dim I-beam
Definition: Fl_Text_Display.H:92
void scrollbar_width(int W)
Sets the width/height of the scrollbars.
Definition: Fl_Text_Display.H:226