Debian Codegraph @

src/include/Fl_Text_Display_mod.H

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