Debian Codegraph @

src/widgets/Fl_Text_Display_mod.cxx

raw
1 //
2 // "$Id: Fl_Text_Display_mod.cxx 8808 2011-06-16 13:31:25Z manolo $"
3 //
4 // Copyright 2001-2010 by Bill Spitzak and others.
5 // Original code Copyright Mark Edel. Permission to distribute under
6 // the LGPL for the FLTK library granted by Mark Edel.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the
20 //
21 // Free Software Foundation, Inc.
22 // 51 Franklin Street, Fifth Floor
23 // Boston, MA 02110-1301 USA.
24 //
25 // Please report all bugs and problems on the following page:
26 //
27 // http://www.fltk.org/str.php
28 //
29
30 // TODO: rendering of the "optional hyphen"
31 // TODO: make line numbering work again
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <FL/fl_utf8.h>
36 #include "missing_strings.h"
37 #include <limits.h>
38 #include <ctype.h>
39 #include <FL/Fl.H>
40 #include <FL/Fl_Window.H>
41 #include <FL/Fl_Printer.H>
42 #include "Fl_Text_Buffer_mod.H"
43 #include "Fl_Text_Display_mod.H"
44
45 #include <config.h>
46
47 #if FLCLUSTER_FLTK_API_MAJOR == 1 && FLCLUSTER_FLTK_API_MINOR < 3
48 # error "FLTK versions less then 1.3 are no longer supported"
49 #endif
50
51 #undef min
52 #undef max
53
54 // Text area margins. Left & right margins should be at least 3 so that
55 // there is some room for the overhanging parts of the cursor!
56 const int Fl_Text_Display_mod::DEFAULT_TOP_MARGIN = 1;
57 const int Fl_Text_Display_mod::DEFAULT_BOTTOM_MARGIN = 1;
58 const int Fl_Text_Display_mod::DEFAULT_LEFT_MARGIN = 3;
59 const int Fl_Text_Display_mod::DEFAULT_RIGHT_MARGIN = 3;
60 //#define TOP_MARGIN 1
61 //#define BOTTOM_MARGIN 1
62 //#define LEFT_MARGIN 3
63 //#define RIGHT_MARGIN 3
64
65 #define NO_HINT -1
66
67 /* Masks for text drawing methods. These are or'd together to form an
68 integer which describes what drawing calls to use to draw a string */
69 #define FILL_MASK 0x0100
70 #define SECONDARY_MASK 0x0200
71 #define PRIMARY_MASK 0x0400
72 #define HIGHLIGHT_MASK 0x0800
73 #define BG_ONLY_MASK 0x1000
74 #define TEXT_ONLY_MASK 0x2000
75 #define STYLE_LOOKUP_MASK 0xff
76
77 /* Maximum displayable line length (how many characters will fit across the
78 widest window). This amount of memory is temporarily allocated from the
79 stack in the draw_vline() method for drawing strings */
80 #define MAX_DISP_LINE_LEN 1000
81
82 static int max( int i1, int i2 );
83 static int min( int i1, int i2 );
84 static int countlines( const char *string );
85
86 /* The variables below are used in a timer event to allow smooth
87 scrolling of the text area when the pointer has left the area. */
88 static int scroll_direction = 0;
89 static int scroll_amount = 0;
90 static int scroll_y = 0;
91 static int scroll_x = 0;
92
93 // CET - FIXME
94 #define TMPFONTWIDTH 6
95
96
97
98 /**
99 \brief Creates a new text display widget.
100
101 \param X, Y, W, H position and size of widget
102 \param l label text, defaults to none
103 */
104 Fl_Text_Display_mod::Fl_Text_Display_mod(int X, int Y, int W, int H, const char* l)
105 : Fl_Group(X, Y, W, H, l) {
106
107 TOP_MARGIN = DEFAULT_TOP_MARGIN;
108 BOTTOM_MARGIN = DEFAULT_BOTTOM_MARGIN;
109 LEFT_MARGIN = DEFAULT_LEFT_MARGIN;
110 RIGHT_MARGIN = DEFAULT_RIGHT_MARGIN;
111
112 mMaxsize = 0;
113 damage_range1_start = damage_range1_end = -1;
114 damage_range2_start = damage_range2_end = -1;
115 dragPos = dragging = 0;
116 dragType = DRAG_CHAR;
117 display_insert_position_hint = 0;
118 shortcut_ = 0;
119
120 color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR);
121 box(FL_DOWN_FRAME);
122 textsize(FL_NORMAL_SIZE);
123 textcolor(FL_FOREGROUND_COLOR);
124 textfont(FL_HELVETICA);
125 set_flag(SHORTCUT_LABEL);
126
127 text_area.x = 0;
128 text_area.y = 0;
129 text_area.w = 0;
130 text_area.h = 0;
131
132 mVScrollBar = new Fl_Scrollbar(0,0,1,1);
133 mVScrollBar->callback((Fl_Callback*)v_scrollbar_cb, this);
134 mHScrollBar = new Fl_Scrollbar(0,0,1,1);
135 mHScrollBar->callback((Fl_Callback*)h_scrollbar_cb, this);
136 mHScrollBar->type(FL_HORIZONTAL);
137
138 end();
139
140 scrollbar_width(Fl::scrollbar_size());
141 scrollbar_align(FL_ALIGN_BOTTOM_RIGHT);
142
143 mCursorOn = 0;
144 mCursorPos = 0;
145 mCursorOldY = -100;
146 mCursorToHint = NO_HINT;
147 mCursorStyle = NORMAL_CURSOR;
148 mCursorPreferredXPos = -1;
149 mBuffer = 0;
150 mFirstChar = 0;
151 mLastChar = 0;
152 mNBufferLines = 0;
153 mTopLineNum = mTopLineNumHint = 1;
154 mAbsTopLineNum = 1;
155 mNeedAbsTopLineNum = 0;
156 mHorizOffset = mHorizOffsetHint = 0;
157
158 mCursor_color = FL_FOREGROUND_COLOR;
159
160 mStyleBuffer = 0;
161 mStyleTable = 0;
162 mNStyles = 0;
163
164 mNVisibleLines = 1;
165 mLineStarts = new int[mNVisibleLines + 1];
166 mLineStarts[0] = 0;
167 mLineStarts[1] = -1;
168
169 mSuppressResync = 0;
170 mNLinesDeleted = 0;
171 mModifyingTabDistance = 0;
172
173 mUnfinishedStyle = 0;
174 mUnfinishedHighlightCB = 0;
175 mHighlightCBArg = 0;
176
177 mLineNumLeft = mLineNumWidth = 0;
178 mContinuousWrap = 0;
179 mFastDisplay = 0;
180 s_text.clear();
181 s_style.clear();
182 mWrapMarginPix = 0;
183 mSuppressResync = mNLinesDeleted = mModifyingTabDistance = 0;
184 }
185
186
187
188 /**
189 Free a text display and release its associated memory.
190
191 Note, the text BUFFER that the text display displays is a separate
192 entity and is not freed, nor are the style buffer or style table.
193 */
194 Fl_Text_Display_mod::~Fl_Text_Display_mod() {
195 if (scroll_direction) {
196 Fl::remove_timeout(scroll_timer_cb, this);
197 scroll_direction = 0;
198 }
199 //df
200 // if (mBuffer) {
201 // mBuffer->remove_modify_callback(buffer_modified_cb, this);
202 // mBuffer->remove_predelete_callback(buffer_predelete_cb, this);
203 // }
204 if (mLineStarts) delete[] mLineStarts;
205 }
206
207
208
209 /**
210 Attach a text buffer to display, replacing the current buffer (if any)
211 \param buf attach this text buffer
212 */
213 void Fl_Text_Display_mod::buffer( Fl_Text_Buffer_mod *buf ) {
214 /* If the text display is already displaying a buffer, clear it off
215 of the display and remove our callback from it */
216 if ( buf == mBuffer) return;
217 if ( mBuffer != 0 ) {
218 // we must provide a copy of the buffer that we are deleting!
219 char *deletedText = mBuffer->text();
220 buffer_modified_cb( 0, 0, mBuffer->length(), 0, deletedText, this );
221 free(deletedText);
222 mNBufferLines = 0;
223 mBuffer->remove_modify_callback( buffer_modified_cb, this );
224 mBuffer->remove_predelete_callback( buffer_predelete_cb, this );
225 }
226
227 /* Add the buffer to the display, and attach a callback to the buffer for
228 receiving modification information when the buffer contents change */
229 mBuffer = buf;
230 if (mBuffer) {
231 mBuffer->add_modify_callback( buffer_modified_cb, this );
232 mBuffer->add_predelete_callback( buffer_predelete_cb, this );
233
234 /* Update the display */
235 buffer_modified_cb( 0, buf->length(), 0, 0, 0, this );
236 }
237
238 /* Resize the widget to update the screen... */
239 resize(x(), y(), w(), h());
240 }
241
242
243
244 /**
245 \brief Attach (or remove) highlight information in text display and redisplay.
246
247 Highlighting information consists of a style buffer which parallels the
248 normal text buffer, but codes font and color information for the display;
249 a style table which translates style buffer codes (indexed by buffer
250 character - 'A') into fonts and colors; and a callback mechanism for
251 as-needed highlighting, triggered by a style buffer entry of
252 "unfinishedStyle". Style buffer can trigger additional redisplay during
253 a normal buffer modification if the buffer contains a primary Fl_Text_Selection
254 (see extendRangeForStyleMods for more information on this protocol).
255
256 Style buffers, tables and their associated memory are managed by the caller.
257
258 Styles are ranged from 65 ('A') to 126.
259
260 \param styleBuffer this buffer works in parallel to the text buffer. For every
261 character in the text buffer, the stye buffer has a byte at the same offset
262 that contains an index into an array of possible styles.
263 \param styleTable a list of styles indexed by the style buffer
264 \param nStyles number of styles in the style table
265 \param unfinishedStyle if this style is found, the callback below is called
266 \param unfinishedHighlightCB if a character with an unfinished style is found,
267 this callback will be called
268 \param cbArg and optional argument for the callback above, usually a pointer
269 to the Text Display.
270 */
271 void Fl_Text_Display_mod::highlight_data(Fl_Text_Buffer_mod *styleBuffer,
272 const Style_Table_Entry *styleTable,
273 int nStyles, char unfinishedStyle,
274 Unfinished_Style_Cb unfinishedHighlightCB,
275 void *cbArg ) {
276 mStyleBuffer = styleBuffer;
277 mStyleTable = styleTable;
278 mNStyles = nStyles;
279 mUnfinishedStyle = unfinishedStyle;
280 mUnfinishedHighlightCB = unfinishedHighlightCB;
281 mHighlightCBArg = cbArg;
282 mColumnScale = 0;
283
284 mStyleBuffer->canUndo(0);
285 damage(FL_DAMAGE_EXPOSE);
286 }
287
288
289
290 /**
291 \brief Find the longest line of all visible lines.
292 \return the width of the longest visible line in pixels
293 */
294 int Fl_Text_Display_mod::longest_vline() const {
295 int longest = 0;
296 for (int i = 0; i < mNVisibleLines; i++)
297 longest = max(longest, measure_vline(i));
298 return longest;
299 }
300
301
302
303 /**
304 \brief Change the size of the displayed text area.
305 Calling this function will trigger a recalculation of all lines visible and
306 of all scrollbar sizes.
307 \param X, Y, W, H new position and size of this widget
308 */
309 void Fl_Text_Display_mod::resize(int X, int Y, int W, int H) {
310 #ifdef DEBUG
311 printf("Fl_Text_Display_mod::resize(X=%d, Y=%d, W=%d, H=%d)\n", X, Y, W, H);
312 #endif // DEBUG
313 const int oldWidth = w();
314 #ifdef DEBUG
315 printf(" oldWidth=%d, mContinuousWrap=%d, mWrapMargin=%d\n", oldWidth, mContinuousWrap, mWrapMarginPix);
316 #endif // DEBUG
317 Fl_Widget::resize(X,Y,W,H);
318 if (!buffer()) return;
319 X += Fl::box_dx(box());
320 Y += Fl::box_dy(box());
321 W -= Fl::box_dw(box());
322 H -= Fl::box_dh(box());
323
324 text_area.x = X+LEFT_MARGIN;
325 text_area.y = Y+TOP_MARGIN;
326 text_area.w = W-LEFT_MARGIN-RIGHT_MARGIN - (mVScrollBar->visible() ? scrollbar_width() : 0);
327 text_area.h = H-TOP_MARGIN-BOTTOM_MARGIN - (mHScrollBar->visible() ? scrollbar_width() : 0);
328 int i;
329
330 if (!mStyleTable)
331 return;
332
333 /* Find the new maximum font height for this text display */
334 for (i = 0, mMaxsize = fl_height(textfont(), textsize()); i < mNStyles; i++)
335 mMaxsize = max(mMaxsize, fl_height(mStyleTable[i].font, mStyleTable[i].size));
336
337 // did we have scrollbars initially?
338 unsigned int hscrollbarvisible = mHScrollBar->visible();
339 unsigned int vscrollbarvisible = mVScrollBar->visible();
340
341 // try without scrollbars first
342 mVScrollBar->clear_visible();
343 mHScrollBar->clear_visible();
344
345 for (int again = 1; again;) {
346 again = 0;
347 /* In continuous wrap mode, a change in width affects the total number of
348 lines in the buffer, and can leave the top line number incorrect, and
349 the top character no longer pointing at a valid line start */
350 if (!mFastDisplay && mContinuousWrap && !mWrapMarginPix && W!=oldWidth) {
351 int oldFirstChar = mFirstChar;
352 mNBufferLines = count_lines(0, buffer()->length(), true);
353 mFirstChar = line_start(mFirstChar);
354 mTopLineNum = count_lines(0, mFirstChar, true)+1;
355 absolute_top_line_number(oldFirstChar);
356 #ifdef DEBUG
357 printf(" mNBufferLines=%d\n", mNBufferLines);
358 #endif // DEBUG
359 }
360
361 /* reallocate and update the line starts array, which may have changed
362 size and / or contents. */
363 int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize;
364 if (nvlines < 1) nvlines = 1;
365 if (mNVisibleLines != nvlines) {
366 mNVisibleLines = nvlines;
367 if (mLineStarts) delete[] mLineStarts;
368 mLineStarts = new int [mNVisibleLines + 1];
369 }
370
371 calc_line_starts(0, mNVisibleLines);
372 calc_last_char();
373
374 // figure the scrollbars
375 if (scrollbar_width()) {
376 /* Decide if the vertical scrollbar needs to be visible */
377 // always display vertical scroll bar
378 // if (scrollbar_align() & (FL_ALIGN_LEFT|FL_ALIGN_RIGHT) &&
379 // mNBufferLines >= mNVisibleLines - 1)
380 {
381 mVScrollBar->set_visible();
382 if (scrollbar_align() & FL_ALIGN_LEFT) {
383 text_area.x = X+scrollbar_width()+LEFT_MARGIN;
384 text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN - scrollbar_width();
385 mVScrollBar->resize(X, text_area.y-TOP_MARGIN, scrollbar_width(),
386 text_area.h+TOP_MARGIN+BOTTOM_MARGIN);
387 } else {
388 text_area.x = X+LEFT_MARGIN;
389 text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN;
390 mVScrollBar->resize(X+W-scrollbar_width(), text_area.y-TOP_MARGIN,
391 scrollbar_width(), text_area.h+TOP_MARGIN+BOTTOM_MARGIN);
392 }
393 }
394
395 /*
396 Decide if the horizontal scrollbar needs to be visible. If there
397 is a vertical scrollbar, a horizontal is always created too. This
398 is because the alternatives are unattractive:
399 * Dynamically creating a horizontal scrollbar based on the currently
400 visible lines is what the original nedit does, but it always wastes
401 space for the scrollbar even when it's not used. Since the FLTK
402 widget dynamically allocates the space for the scrollbar and
403 rearranges the widget to make room for it, this would create a very
404 visually displeasing "bounce" effect when the vertical scrollbar is
405 dragged. Trust me, I tried it and it looks really bad.
406 * The other alternative would be to keep track of what the longest
407 line in the entire buffer is and base the scrollbar on that. I
408 didn't do this because I didn't see any easy way to do that using
409 the nedit code and this could involve a lengthy calculation for
410 large buffers. If an efficient and non-costly way of doing this
411 can be found, this might be a way to go.
412 */
413 /* WAS: Suggestion: Try turning the horizontal scrollbar on when
414 you first see a line that is too wide in the window, but then
415 don't turn it off (ie mix both of your solutions). */
416 if (!mFastDisplay && !mContinuousWrap && scrollbar_align() & (FL_ALIGN_TOP|FL_ALIGN_BOTTOM) &&
417 (mVScrollBar->visible() || longest_vline() > text_area.w))
418 {
419 if (!mHScrollBar->visible()) {
420 mHScrollBar->set_visible();
421 again = 1; // loop again to see if we now need vert. & recalc sizes
422 }
423 if (scrollbar_align() & FL_ALIGN_TOP) {
424 text_area.y = Y + scrollbar_width()+TOP_MARGIN;
425 text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN;
426 mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y,
427 text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width());
428 } else {
429 text_area.y = Y+TOP_MARGIN;
430 text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN;
431 mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y+H-scrollbar_width(),
432 text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width());
433 }
434 }
435 }
436 }
437
438 // user request to change viewport
439 if (mTopLineNumHint != mTopLineNum || mHorizOffsetHint != mHorizOffset)
440 scroll_(mTopLineNumHint, mHorizOffsetHint);
441
442 // everything will fit in the viewport
443 if (mNBufferLines < mNVisibleLines || mBuffer == NULL || mBuffer->length() == 0) {
444 scroll_(1, mHorizOffset);
445 /* if empty lines become visible, there may be an opportunity to
446 display more text by scrolling down */
447 } else {
448 while ( mNVisibleLines>=2
449 && (mLineStarts[mNVisibleLines-2]==-1)
450 && scroll_(mTopLineNum-1, mHorizOffset))
451 { }
452 }
453
454 // user request to display insert position
455 if (display_insert_position_hint)
456 display_insert();
457
458 // in case horizontal offset is now greater than longest line
459 int maxhoffset = max(0, longest_vline()-text_area.w);
460 if (mHorizOffset > maxhoffset)
461 scroll_(mTopLineNumHint, maxhoffset);
462
463 mTopLineNumHint = mTopLineNum;
464 mHorizOffsetHint = mHorizOffset;
465 display_insert_position_hint = 0;
466
467 if ((!mFastDisplay && mContinuousWrap) ||
468 hscrollbarvisible != mHScrollBar->visible() ||
469 vscrollbarvisible != mVScrollBar->visible())
470 redraw();
471
472 update_v_scrollbar();
473 update_h_scrollbar();
474 }
475
476
477
478 /**
479 \brief Refresh a rectangle of the text display.
480 \param left, top are in coordinates of the text drawing window.
481 \param width, height size in pixels
482 */
483 void Fl_Text_Display_mod::draw_text( int left, int top, int width, int height ) {
484 int fontHeight, firstLine, lastLine, line;
485
486 /* find the line number range of the display */
487 fontHeight = mMaxsize ? mMaxsize : textsize_;
488 firstLine = ( top - text_area.y - fontHeight + 1 ) / fontHeight;
489 lastLine = ( top + height - text_area.y ) / fontHeight + 1;
490
491 fl_push_clip( left, top, width, height );
492
493 /* draw the lines */
494 for ( line = firstLine; line <= lastLine; line++ )
495 draw_vline( line, left, left + width, 0, INT_MAX );
496
497 /* draw the line numbers if exposed area includes them */
498 if (mLineNumWidth != 0 && left <= mLineNumLeft + mLineNumWidth)
499 draw_line_numbers(false);
500
501 fl_pop_clip();
502 }
503
504
505
506 /**
507 \brief Marks text from start to end as needing a redraw.
508 This function will trigger a damage event and later a redraw of parts of
509 the widget.
510 \param startpos index of first character needing redraw
511 \param endpos index after last character needing redraw
512 */
513 void Fl_Text_Display_mod::redisplay_range(int startpos, int endpos) {
514 IS_UTF8_ALIGNED2(buffer(), startpos)
515 IS_UTF8_ALIGNED2(buffer(), endpos)
516
517 if (damage_range1_start == -1 && damage_range1_end == -1) {
518 damage_range1_start = startpos;
519 damage_range1_end = endpos;
520 } else if ((startpos >= damage_range1_start && startpos <= damage_range1_end) ||
521 (endpos >= damage_range1_start && endpos <= damage_range1_end)) {
522 damage_range1_start = min(damage_range1_start, startpos);
523 damage_range1_end = max(damage_range1_end, endpos);
524 } else if (damage_range2_start == -1 && damage_range2_end == -1) {
525 damage_range2_start = startpos;
526 damage_range2_end = endpos;
527 } else {
528 damage_range2_start = min(damage_range2_start, startpos);
529 damage_range2_end = max(damage_range2_end, endpos);
530 }
531 damage(FL_DAMAGE_SCROLL);
532 }
533
534
535
536 /**
537 \brief Draw a range of text.
538
539 Refresh all of the text between buffer positions \p startpos and
540 \p endpos not including the character at the position \p endpos.
541
542 If \p endpos points beyond the end of the buffer, refresh the whole display
543 after \p startpos, including blank lines which are not technically part of
544 any range of characters.
545
546 \param startpos index of first character to draw
547 \param endpos index after last character to draw
548 */
549 void Fl_Text_Display_mod::draw_range(int startpos, int endpos) {
550 startpos = buffer()->utf8_align(startpos);
551 endpos = buffer()->utf8_align(endpos);
552
553 int i, startLine, lastLine, startIndex, endIndex;
554
555 /* If the range is outside of the displayed text, just return */
556 if ( endpos < mFirstChar || ( startpos > mLastChar && !empty_vlines() ) )
557 return;
558
559 /* Clean up the starting and ending values */
560 if ( startpos < 0 ) startpos = 0;
561 if ( startpos > mBuffer->length() ) startpos = mBuffer->length();
562 if ( endpos < 0 ) endpos = 0;
563 if ( endpos > mBuffer->length() ) endpos = mBuffer->length();
564
565 /* Get the starting and ending lines */
566 if ( startpos < mFirstChar )
567 startpos = mFirstChar;
568 if ( !position_to_line( startpos, &startLine ) )
569 startLine = mNVisibleLines - 1;
570 if ( endpos >= mLastChar ) {
571 lastLine = mNVisibleLines - 1;
572 } else {
573 if ( !position_to_line( endpos, &lastLine ) ) {
574 /* shouldn't happen */
575 lastLine = mNVisibleLines - 1;
576 }
577 }
578
579 /* Get the starting and ending positions within the lines */
580 startIndex = mLineStarts[ startLine ] == -1 ? 0 : startpos - mLineStarts[ startLine ];
581 if ( endpos >= mLastChar )
582 endIndex = INT_MAX;
583 else if ( mLineStarts[ lastLine ] == -1 )
584 endIndex = 0;
585 else
586 endIndex = endpos - mLineStarts[ lastLine ];
587
588 /* If the starting and ending lines are the same, redisplay the single
589 line between "start" and "end" */
590 if ( startLine == lastLine ) {
591 draw_vline( startLine, 0, INT_MAX, startIndex, endIndex );
592 return;
593 }
594
595 /* Redisplay the first line from "start" */
596 draw_vline( startLine, 0, INT_MAX, startIndex, INT_MAX );
597
598 /* Redisplay the lines in between at their full width */
599 for ( i = startLine + 1; i < lastLine; i++ )
600 draw_vline( i, 0, INT_MAX, 0, INT_MAX );
601
602 /* Redisplay the last line to "end" */
603 draw_vline( lastLine, 0, INT_MAX, 0, endIndex );
604 }
605
606
607
608 /**
609 \brief Sets the position of the text insertion cursor for text display.
610 Move the insertion cursor in front of the character at \p newPos.
611 This function may trigger a redraw.
612 \param newPos new caret position
613 */
614 void Fl_Text_Display_mod::insert_position( int newPos ) {
615 IS_UTF8_ALIGNED2(buffer(), newPos)
616
617 /* make sure new position is ok, do nothing if it hasn't changed */
618 if ( newPos == mCursorPos ) return;
619 if ( newPos < 0 ) newPos = 0;
620 if ( newPos > mBuffer->length() ) newPos = mBuffer->length();
621
622 /* cursor movement cancels vertical cursor motion column */
623 mCursorPreferredXPos = -1;
624
625 /* erase the cursor at its previous position */
626 redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos));
627
628 mCursorPos = newPos;
629
630 /* draw cursor at its new position */
631 redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos));
632 }
633
634
635
636 /**
637 \brief Shows the text cursor.
638 This function may trigger a redraw.
639 \param b show(1) or hide(0) the text cursor (caret).
640 */
641 void Fl_Text_Display_mod::show_cursor(int b) {
642 mCursorOn = b;
643 redisplay_range(buffer()->prev_char_clipped(mCursorPos), buffer()->next_char(mCursorPos));
644 }
645
646
647
648 /**
649 \brief Sets the text cursor style.
650 Sets the text cursor style to one of the following:
651
652 \li Fl_Text_Display_mod::NORMAL_CURSOR - Shows an I beam.
653 \li Fl_Text_Display_mod::CARET_CURSOR - Shows a caret under the text.
654 \li Fl_Text_Display_mod::DIM_CURSOR - Shows a dimmed I beam.
655 \li Fl_Text_Display_mod::BLOCK_CURSOR - Shows an unfilled box around the current
656 character.
657 \li Fl_Text_Display_mod::HEAVY_CURSOR - Shows a thick I beam.
658
659 This call also switches the cursor on and may trigger a redraw.
660
661 \param style new cursor style
662 */
663 void Fl_Text_Display_mod::cursor_style(int style) {
664 mCursorStyle = style;
665 if (mCursorOn) show_cursor();
666 }
667
668
669
670 /**
671 \brief Set the new text wrap mode.
672
673 If \p wrap mode is not zero, this call enables automatic word wrapping at column
674 \p wrapMargin. Word-wrapping does not change the text buffer itself, only the way
675 the text is displayed. Different Text Displays can have different wrap modes,
676 even if they share the same Text Buffer.
677
678 \param wrap new wrap mode is WRAP_NONE (don't wrap text at all), WRAP_AT_COLUMN
679 (wrap text at the given text column), WRAP_AT_PIXEL (wrap text at a pixel
680 position), or WRAP_AT_BOUNDS (wrap text so that it fits into the
681 widget width)
682 \param wrapMargin in WRAP_AT_COLUMN mode, text will wrap at the n'th character.
683 For variable width fonts, an average character width is calculated. The
684 column width is calculated using the current textfont or the first style
685 when this function is called. If the font size changes, this function
686 must be called again. In WRAP_AT_PIXEL mode, this is the pixel position.
687 \todo we need new wrap modes to wrap at the window edge and based on pixel width
688 or average character width.
689 */
690 void Fl_Text_Display_mod::wrap_mode(int wrap, int wrapMargin) {
691 switch (wrap) {
692 case WRAP_NONE:
693 mWrapMarginPix = 0;
694 mContinuousWrap = 0;
695 break;
696 case WRAP_AT_COLUMN:
697 default:
698 mWrapMarginPix = int(col_to_x(wrapMargin));
699 mContinuousWrap = 1;
700 break;
701 case WRAP_AT_PIXEL:
702 mWrapMarginPix = wrapMargin;
703 mContinuousWrap = 1;
704 break;
705 case WRAP_AT_BOUNDS:
706 mWrapMarginPix = 0;
707 mContinuousWrap = 1;
708 break;
709 }
710
711 if (buffer()) {
712 /* wrapping can change the total number of lines, re-count */
713 mNBufferLines = count_lines(0, buffer()->length(), true);
714
715 /* changing wrap margins or changing from wrapped mode to non-wrapped
716 can leave the character at the top no longer at a line start, and/or
717 change the line number */
718 mFirstChar = line_start(mFirstChar);
719 mTopLineNum = count_lines(0, mFirstChar, true) + 1;
720
721 reset_absolute_top_line_number();
722
723 /* update the line starts array */
724 calc_line_starts(0, mNVisibleLines);
725 calc_last_char();
726 } else {
727 // No buffer, so just clear the state info for later...
728 mNBufferLines = 0;
729 mFirstChar = 0;
730 mTopLineNum = 1;
731 mAbsTopLineNum = 0;
732 }
733
734 resize(x(), y(), w(), h());
735 }
736
737
738
739 /**
740 \brief Inserts "text" at the current cursor location.
741
742 This has the same effect as inserting the text into the buffer using BufInsert
743 and then moving the insert position after the newly inserted text, except
744 that it's optimized to do less redrawing.
745
746 \param text new text in UTF-8 encoding.
747 */
748 void Fl_Text_Display_mod::insert(const char* text) {
749 IS_UTF8_ALIGNED2(buffer(), mCursorPos)
750 IS_UTF8_ALIGNED(text)
751
752 int pos = mCursorPos;
753
754 mCursorToHint = pos + strlen( text );
755 mBuffer->insert( pos, text );
756 mCursorToHint = NO_HINT;
757 }
758
759
760
761 /**
762 \brief Replaces text at the current insert position.
763 \param text new text in UTF-8 encoding
764
765 \todo Unicode? Find out exactly what we do here and simplify.
766 */
767 void Fl_Text_Display_mod::overstrike(const char* text) {
768 IS_UTF8_ALIGNED2(buffer(), mCursorPos)
769 IS_UTF8_ALIGNED(text)
770
771 int startPos = mCursorPos;
772 Fl_Text_Buffer_mod *buf = mBuffer;
773 int lineStart = buf->line_start( startPos );
774 int textLen = strlen( text );
775 int i, p, endPos, indent, startIndent, endIndent;
776 const char *c;
777 unsigned int ch;
778 char *paddedText = NULL;
779
780 /* determine how many displayed character positions are covered */
781 startIndent = mBuffer->count_displayed_characters( lineStart, startPos );
782 indent = startIndent;
783 for ( c = text; *c != '\0'; c += fl_utf8len1(*c) )
784 indent++;
785 endIndent = indent;
786
787 /* find which characters to remove, and if necessary generate additional
788 padding to make up for removed control characters at the end */
789 indent = startIndent;
790 for ( p = startPos; ; p=buffer()->next_char(p) ) {
791 if ( p == buf->length() )
792 break;
793 ch = buf->char_at( p );
794 if ( ch == '\n' )
795 break;
796 indent++;
797 if ( indent == endIndent ) {
798 p++;
799 break;
800 } else if ( indent > endIndent ) {
801 if ( ch != '\t' ) {
802 p++;
803 paddedText = new char [ textLen + FL_TEXT_MAX_EXP_CHAR_LEN + 1 ];
804 strcpy( paddedText, text );
805 for ( i = 0; i < indent - endIndent; i++ )
806 paddedText[ textLen + i ] = ' ';
807 paddedText[ textLen + i ] = '\0';
808 }
809 break;
810 }
811 }
812 endPos = p;
813
814 mCursorToHint = startPos + textLen;
815 buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText );
816 mCursorToHint = NO_HINT;
817 if ( paddedText != NULL )
818 delete [] paddedText;
819 }
820
821
822
823 /**
824 \brief Convert a character index into a pixel position.
825
826 Translate a buffer text position to the XY location where the top left of the
827 cursor would be positioned to point to that character. Returns 0 if the
828 position is not displayed because it is \e \b vertically out of view.
829 If the position is horizontally out of view, returns the X coordinate where
830 the position would be if it were visible.
831
832 \param pos character index
833 \param[out] X, Y pixel position of character on screen
834 \return 0 if character vertically out of view, X position otherwise
835 */
836 int Fl_Text_Display_mod::position_to_xy( int pos, int* X, int* Y ) const {
837 IS_UTF8_ALIGNED2(buffer(), pos)
838
839 int lineStartPos, fontHeight;
840 int visLineNum;
841
842 /* If position is not displayed, return false */
843 if (pos < mFirstChar || (pos > mLastChar && !empty_vlines())) {
844 return 0;
845 }
846
847 /* Calculate Y coordinate */
848 if (!position_to_line(pos, &visLineNum)) {
849 return 0;
850 }
851 if (visLineNum < 0 || visLineNum > mNBufferLines) {
852 return 0;
853 }
854
855 fontHeight = mMaxsize;
856 *Y = text_area.y + visLineNum * fontHeight;
857
858 /* Get the text, length, and buffer position of the line. If the position
859 is beyond the end of the buffer and should be at the first position on
860 the first empty line, don't try to get or scan the text */
861 lineStartPos = mLineStarts[visLineNum];
862 if ( lineStartPos == -1 ) {
863 *X = text_area.x - mHorizOffset;
864 return 1;
865 }
866 *X = text_area.x + handle_vline(GET_WIDTH, lineStartPos, pos-lineStartPos, 0, 0, 0, 0, 0, 0) - mHorizOffset;
867 return 1;
868 }
869
870
871
872 /**
873 \brief Find the line and column number of position \p pos.
874
875 This only works for displayed lines. If the line is not displayed, the
876 function returns 0 (without the mLineStarts array it could turn in to very long
877 calculation involving scanning large amounts of text in the buffer).
878 If continuous wrap mode is on, returns the absolute line number (as opposed
879 to the wrapped line number which is used for scrolling).
880
881 \param pos character index
882 \param[out] lineNum absolute (unwrapped) line number
883 \param[out] column character offset to the beginning of the line
884 \return 0 if \p pos is off screen, line number otherwise
885 \todo a column number makes little sense in the UTF-8/variable font width
886 environment. We will have to further define what exactly we want to return.
887 Please check the functions that call this particular function.
888 */
889 int Fl_Text_Display_mod::position_to_linecol( int pos, int* lineNum, int* column ) const {
890 IS_UTF8_ALIGNED2(buffer(), pos)
891
892 int retVal;
893
894 /* In continuous wrap mode, the absolute (non-wrapped) line count is
895 maintained separately, as needed. Only return it if we're actually
896 keeping track of it and pos is in the displayed text */
897 if (!mFastDisplay && mContinuousWrap) {
898 if (!maintaining_absolute_top_line_number() || pos < mFirstChar || pos > mLastChar)
899 return 0;
900 *lineNum = mAbsTopLineNum + buffer()->count_lines(mFirstChar, pos);
901 *column = buffer()->count_displayed_characters(buffer()->line_start(pos), pos);
902 return 1;
903 }
904
905 retVal = position_to_line( pos, lineNum );
906 if ( retVal ) {
907 *column = mBuffer->count_displayed_characters( mLineStarts[ *lineNum ], pos );
908 *lineNum += mTopLineNum;
909 }
910 return retVal;
911 }
912
913
914
915 /**
916 \brief Check if a pixel position is within the primary selection.
917 \param X, Y pixel position to test
918 \return 1 if position (X, Y) is inside of the primary Fl_Text_Selection
919 */
920 int Fl_Text_Display_mod::in_selection( int X, int Y ) const {
921 int pos = xy_to_position( X, Y, CHARACTER_POS );
922 IS_UTF8_ALIGNED2(buffer(), pos)
923 Fl_Text_Buffer_mod *buf = mBuffer;
924 return buf->primary_selection()->includes(pos);
925 }
926
927
928
929 /**
930 \brief Nobody knows what this function does.
931
932 Correct a column number based on an unconstrained position (as returned by
933 TextDXYToUnconstrainedPosition) to be relative to the last actual newline
934 in the buffer before the row and column position given, rather than the
935 last line start created by line wrapping. This is an adapter
936 for rectangular selections and code written before continuous wrap mode,
937 which thinks that the unconstrained column is the number of characters
938 from the last newline. Obviously this is time consuming, because it
939 invloves character re-counting.
940
941 \param row
942 \param column
943 \return something unknown
944 \todo What does this do and how is it useful? Column numbers mean little in
945 this context. Which functions depend on this one?
946
947 \todo Unicode?
948 */
949 int Fl_Text_Display_mod::wrapped_column(int row, int column) const {
950 int lineStart, dispLineStart;
951
952 if (mFastDisplay || !mContinuousWrap || row < 0 || row > mNVisibleLines)
953 return column;
954 dispLineStart = mLineStarts[row];
955 if (dispLineStart == -1)
956 return column;
957 lineStart = buffer()->line_start(dispLineStart);
958 return column + buffer()->count_displayed_characters(lineStart, dispLineStart);
959 }
960
961
962
963 /**
964 \brief Nobody knows what this function does.
965
966 Correct a row number from an unconstrained position (as returned by
967 TextDXYToUnconstrainedPosition) to a straight number of newlines from the
968 top line of the display. Because rectangular selections are based on
969 newlines, rather than display wrapping, and anywhere a rectangular selection
970 needs a row, it needs it in terms of un-wrapped lines.
971
972 \param row
973 \return something unknown
974 \todo What does this do and how is it useful? Column numbers mean little in
975 this context. Which functions depend on this one?
976 */
977 int Fl_Text_Display_mod::wrapped_row(int row) const {
978 if (mFastDisplay || !mContinuousWrap || row < 0 || row > mNVisibleLines)
979 return row;
980 return buffer()->count_lines(mFirstChar, mLineStarts[row]);
981 }
982
983
984
985 /**
986 \brief Scroll the display to bring insertion cursor into view.
987
988 Note: it would be nice to be able to do this without counting lines twice
989 (scroll_() counts them too) and/or to count from the most efficient
990 starting point, but the efficiency of this routine is not as important to
991 the overall performance of the text display.
992
993 \todo Unicode?
994 */
995 void Fl_Text_Display_mod::display_insert() {
996 int hOffset, topLine, X, Y;
997 hOffset = mHorizOffset;
998 topLine = mTopLineNum;
999
1000 if (insert_position() < mFirstChar) {
1001 topLine -= count_lines(insert_position(), mFirstChar, false);
1002 } else if (mNVisibleLines>=2 && mLineStarts[mNVisibleLines-2] != -1) {
1003 int lastChar = line_end(mLineStarts[mNVisibleLines-2],true);
1004 if (insert_position() >= lastChar)
1005 topLine += count_lines(lastChar - (wrap_uses_character(mLastChar) ? 0 : 1),
1006 insert_position(), false);
1007 }
1008
1009 /* Find the new setting for horizontal offset (this is a bit ungraceful).
1010 If the line is visible, just use PositionToXY to get the position
1011 to scroll to, otherwise, do the vertical scrolling first, then the
1012 horizontal */
1013 if (!position_to_xy( mCursorPos, &X, &Y )) {
1014 scroll_(topLine, hOffset);
1015 if (!position_to_xy( mCursorPos, &X, &Y )) {
1016 #ifdef DEBUG
1017 printf ("*** display_insert/position_to_xy # GIVE UP !\n"); fflush(stdout);
1018 #endif // DEBUG
1019 return; /* Give up, it's not worth it (but why does it fail?) */
1020 }
1021 }
1022 if (X > text_area.x + text_area.w)
1023 hOffset += X-(text_area.x + text_area.w);
1024 else if (X < text_area.x)
1025 hOffset += X-text_area.x;
1026
1027 /* Do the scroll */
1028 if (topLine != mTopLineNum || hOffset != mHorizOffset)
1029 scroll_(topLine, hOffset);
1030 }
1031
1032
1033 /**
1034 \brief Scrolls the text buffer to show the current insert position.
1035 This function triggers a complete recalculation, ending in a call to
1036 Fl_Text_Display_mod::display_insert()
1037 */
1038 void Fl_Text_Display_mod::show_insert_position() {
1039 display_insert_position_hint = 1;
1040 resize(x(), y(), w(), h());
1041 }
1042
1043
1044 /*
1045 Cursor movement functions
1046 */
1047
1048 /**
1049 \brief Moves the current insert position right one character.
1050 \return 1 if the cursor moved, 0 if the end of the text was reached
1051 */
1052 int Fl_Text_Display_mod::move_right() {
1053 if ( mCursorPos >= mBuffer->length() )
1054 return 0;
1055 int p = insert_position();
1056 int q = buffer()->next_char(p);
1057 insert_position(q);
1058 return 1;
1059 }
1060
1061
1062
1063 /**
1064 \brief Moves the current insert position left one character.
1065 \return 1 if the cursor moved, 0 if the beginning of the text was reached
1066 */
1067 int Fl_Text_Display_mod::move_left() {
1068 if ( mCursorPos <= 0 )
1069 return 0;
1070 int p = insert_position();
1071 int q = buffer()->prev_char_clipped(p);
1072 insert_position(q);
1073 return 1;
1074 }
1075
1076
1077
1078 /**
1079 \brief Moves the current insert position up one line.
1080 \return 1 if the cursor moved, 0 if the beginning of the text was reached
1081 */
1082 int Fl_Text_Display_mod::move_up() {
1083 int lineStartPos, xPos, prevLineStartPos, newPos, visLineNum;
1084
1085 /* Find the position of the start of the line. Use the line starts array
1086 if possible */
1087 if ( position_to_line( mCursorPos, &visLineNum ) )
1088 lineStartPos = mLineStarts[ visLineNum ];
1089 else {
1090 lineStartPos = line_start( mCursorPos );
1091 visLineNum = -1;
1092 }
1093 if ( lineStartPos == 0 )
1094 return 0;
1095
1096 /* Decide what column to move to, if there's a preferred column use that */
1097 if (mCursorPreferredXPos >= 0)
1098 xPos = mCursorPreferredXPos;
1099 else
1100 xPos = handle_vline(GET_WIDTH, lineStartPos, mCursorPos-lineStartPos,
1101 0, 0, 0, 0, 0, INT_MAX);
1102
1103 /* count forward from the start of the previous line to reach the column */
1104 if ( visLineNum != -1 && visLineNum != 0 )
1105 prevLineStartPos = mLineStarts[ visLineNum - 1 ];
1106 else
1107 prevLineStartPos = rewind_lines( lineStartPos, 1 );
1108
1109 int lineEnd = line_end(prevLineStartPos, true);
1110 newPos = handle_vline(FIND_INDEX_FROM_ZERO, prevLineStartPos, lineEnd-prevLineStartPos,
1111 0, 0, 0, 0, 0, xPos);
1112
1113 /* move the cursor */
1114 insert_position( newPos );
1115
1116 /* if a preferred column wasn't aleady established, establish it */
1117 mCursorPreferredXPos = xPos;
1118 return 1;
1119 }
1120
1121
1122
1123 /**
1124 \brief Moves the current insert position down one line.
1125 \return 1 if the cursor moved, 0 if the beginning of the text was reached
1126 */
1127 int Fl_Text_Display_mod::move_down() {
1128 int lineStartPos, xPos, newPos, visLineNum;
1129
1130 if ( mCursorPos == mBuffer->length() )
1131 return 0;
1132
1133 if ( position_to_line( mCursorPos, &visLineNum ) )
1134 lineStartPos = mLineStarts[ visLineNum ];
1135 else {
1136 lineStartPos = line_start( mCursorPos );
1137 visLineNum = -1;
1138 }
1139 if (mCursorPreferredXPos >= 0) {
1140 xPos = mCursorPreferredXPos;
1141 } else {
1142 xPos = handle_vline(GET_WIDTH, lineStartPos, mCursorPos-lineStartPos,
1143 0, 0, 0, 0, 0, INT_MAX);
1144 }
1145
1146 int nextLineStartPos = skip_lines( lineStartPos, 1, true );
1147 int lineEnd = line_end(nextLineStartPos, true);
1148 newPos = handle_vline(FIND_INDEX_FROM_ZERO, nextLineStartPos, lineEnd-nextLineStartPos,
1149 0, 0, 0, 0, 0, xPos);
1150
1151 insert_position( newPos );
1152 mCursorPreferredXPos = xPos;
1153 return 1;
1154 }
1155
1156
1157
1158 /**
1159 \brief Count the number of lines between two positions.
1160
1161 Same as BufCountLines, but takes into account wrapping if wrapping is
1162 turned on. If the caller knows that \p startPos is at a line start, it
1163 can pass \p startPosIsLineStart as True to make the call more efficient
1164 by avoiding the additional step of scanning back to the last newline.
1165
1166 \param startPos index to first character
1167 \param endPos index after last character
1168 \param startPosIsLineStart avoid scanning back to the line start
1169 \return number of lines
1170 */
1171 int Fl_Text_Display_mod::count_lines(int startPos, int endPos,
1172 bool startPosIsLineStart) const {
1173 IS_UTF8_ALIGNED2(buffer(), startPos)
1174 IS_UTF8_ALIGNED2(buffer(), endPos)
1175
1176 int retLines, retPos, retLineStart, retLineEnd;
1177
1178 #ifdef DEBUG
1179 printf("Fl_Text_Display_mod::count_lines(startPos=%d, endPos=%d, startPosIsLineStart=%d\n",
1180 startPos, endPos, startPosIsLineStart);
1181 #endif // DEBUG
1182
1183 /* If we're not wrapping use simple (and more efficient) BufCountLines */
1184 if (mFastDisplay || !mContinuousWrap)
1185 return buffer()->count_lines(startPos, endPos);
1186
1187 wrapped_line_counter(buffer(), startPos, endPos, INT_MAX,
1188 startPosIsLineStart, 0, &retPos, &retLines, &retLineStart,
1189 &retLineEnd);
1190
1191 #ifdef DEBUG
1192 printf(" # after WLC: retPos=%d, retLines=%d, retLineStart=%d, retLineEnd=%d\n",
1193 retPos, retLines, retLineStart, retLineEnd);
1194 #endif // DEBUG
1195
1196 return retLines;
1197 }
1198
1199
1200
1201 /**
1202 \brief Skip a number of lines forward.
1203
1204 Same as BufCountForwardNLines, but takes into account line breaks when
1205 wrapping is turned on. If the caller knows that startPos is at a line start,
1206 it can pass "startPosIsLineStart" as True to make the call more efficient
1207 by avoiding the additional step of scanning back to the last newline.
1208
1209 \param startPos index to starting character
1210 \param nLines number of lines to skip ahead
1211 \param startPosIsLineStart avoid scanning back to the line start
1212 \return new position as index
1213 */
1214 int Fl_Text_Display_mod::skip_lines(int startPos, int nLines,
1215 bool startPosIsLineStart) {
1216 IS_UTF8_ALIGNED2(buffer(), startPos)
1217
1218 int retLines, retPos, retLineStart, retLineEnd;
1219
1220 /* if we're not wrapping use more efficient BufCountForwardNLines */
1221 if (mFastDisplay || !mContinuousWrap)
1222 return buffer()->skip_lines(startPos, nLines);
1223
1224 /* wrappedLineCounter can't handle the 0 lines case */
1225 if (nLines == 0)
1226 return startPos;
1227
1228 /* use the common line counting routine to count forward */
1229 wrapped_line_counter(buffer(), startPos, buffer()->length(),
1230 nLines, startPosIsLineStart, 0,
1231 &retPos, &retLines, &retLineStart, &retLineEnd);
1232 IS_UTF8_ALIGNED2(buffer(), retPos)
1233 return retPos;
1234 }
1235
1236
1237
1238 /**
1239 \brief Returns the end of a line.
1240
1241 Same as BufEndOfLine, but takes into account line breaks when wrapping
1242 is turned on. If the caller knows that \p startPos is at a line start, it
1243 can pass "startPosIsLineStart" as True to make the call more efficient
1244 by avoiding the additional step of scanning back to the last newline.
1245
1246 Note that the definition of the end of a line is less clear when continuous
1247 wrap is on. With continuous wrap off, it's just a pointer to the newline
1248 that ends the line. When it's on, it's the character beyond the last
1249 \b displayable character on the line, where a whitespace character which has
1250 been "converted" to a newline for wrapping is not considered displayable.
1251 Also note that a line can be wrapped at a non-whitespace character if the
1252 line had no whitespace. In this case, this routine returns a pointer to
1253 the start of the next line. This is also consistent with the model used by
1254 visLineLength.
1255
1256 \param startPos index to starting character
1257 \param startPosIsLineStart avoid scanning back to the line start
1258 \return new position as index
1259 */
1260 int Fl_Text_Display_mod::line_end(int startPos, bool startPosIsLineStart) const {
1261 IS_UTF8_ALIGNED2(buffer(), startPos)
1262
1263 int retLines, retPos, retLineStart, retLineEnd;
1264
1265 /* If we're not wrapping use more efficient BufEndOfLine */
1266 if (mFastDisplay || !mContinuousWrap)
1267 return buffer()->line_end(startPos);
1268
1269 if (startPos == buffer()->length())
1270 return startPos;
1271
1272 wrapped_line_counter(buffer(), startPos, buffer()->length(), 1,
1273 startPosIsLineStart, 0, &retPos, &retLines, &retLineStart,
1274 &retLineEnd);
1275
1276 IS_UTF8_ALIGNED2(buffer(), retLineEnd)
1277 return retLineEnd;
1278 }
1279
1280
1281
1282 /**
1283 \brief Return the beginning of a line.
1284
1285 Same as BufStartOfLine, but returns the character after last wrap point
1286 rather than the last newline.
1287
1288 \param pos index to starting character
1289 \return new position as index
1290 */
1291 int Fl_Text_Display_mod::line_start(int pos) const {
1292 IS_UTF8_ALIGNED2(buffer(), pos)
1293
1294 int retLines, retPos, retLineStart, retLineEnd;
1295
1296 /* If we're not wrapping, use the more efficient BufStartOfLine */
1297 if (mFastDisplay || !mContinuousWrap)
1298 return buffer()->line_start(pos);
1299
1300 wrapped_line_counter(buffer(), buffer()->line_start(pos), pos, INT_MAX, true, 0,
1301 &retPos, &retLines, &retLineStart, &retLineEnd);
1302
1303 IS_UTF8_ALIGNED2(buffer(), retLineStart)
1304 return retLineStart;
1305 }
1306
1307
1308
1309 /**
1310 \brief Skip a number of lines back.
1311
1312 Same as BufCountBackwardNLines, but takes into account line breaks when
1313 wrapping is turned on.
1314
1315 \param startPos index to starting character
1316 \param nLines number of lines to skip back
1317 \return new position as index
1318 */
1319 int Fl_Text_Display_mod::rewind_lines(int startPos, int nLines) {
1320 IS_UTF8_ALIGNED2(buffer(), startPos)
1321
1322 Fl_Text_Buffer_mod *buf = buffer();
1323 int pos, lineStart, retLines, retPos, retLineStart, retLineEnd;
1324
1325 /* If we're not wrapping, use the more efficient BufCountBackwardNLines */
1326 if (mFastDisplay || !mContinuousWrap)
1327 return buf->rewind_lines(startPos, nLines);
1328
1329 pos = startPos;
1330 for (;;) {
1331 lineStart = buf->line_start(pos);
1332 wrapped_line_counter(buf, lineStart, pos, INT_MAX, true, 0,
1333 &retPos, &retLines, &retLineStart, &retLineEnd, false);
1334 if (retLines > nLines)
1335 return skip_lines(lineStart, retLines-nLines, true);
1336 nLines -= retLines;
1337 pos = lineStart - 1;
1338 if (pos < 0)
1339 return 0;
1340 nLines -= 1;
1341 }
1342 }
1343
1344
1345
1346 static inline int fl_isseparator(unsigned int c) {
1347 // FIXME: this does not take UCS-4 encoding into account
1348 return c != '$' && c != '_' && (isspace(c) || ispunct(c));
1349 }
1350
1351
1352
1353 /**
1354 \brief Moves the current insert position right one word.
1355 */
1356 void Fl_Text_Display_mod::next_word() {
1357 int pos = insert_position();
1358
1359 while (pos < buffer()->length() && !fl_isseparator(buffer()->char_at(pos))) {
1360 pos = buffer()->next_char(pos);
1361 }
1362
1363 while (pos < buffer()->length() && fl_isseparator(buffer()->char_at(pos))) {
1364 pos = buffer()->next_char(pos);
1365 }
1366
1367 insert_position( pos );
1368 }
1369
1370
1371
1372 /**
1373 \brief Moves the current insert position left one word.
1374 */
1375 void Fl_Text_Display_mod::previous_word() {
1376 int pos = insert_position();
1377 if (pos==0) return;
1378 pos = buffer()->prev_char(pos);
1379
1380 while (pos && fl_isseparator(buffer()->char_at(pos))) {
1381 pos = buffer()->prev_char(pos);
1382 }
1383
1384 while (pos && !fl_isseparator(buffer()->char_at(pos))) {
1385 pos = buffer()->prev_char(pos);
1386 }
1387
1388 if (fl_isseparator(buffer()->char_at(pos))) {
1389 pos = buffer()->next_char(pos);
1390 }
1391
1392 insert_position( pos );
1393 }
1394
1395
1396
1397 /**
1398 \brief This is called before any characters are deleted.
1399
1400 Callback attached to the text buffer to receive delete information before
1401 the modifications are actually made.
1402
1403 \param pos starting index of deletion
1404 \param nDeleted number of bytes we will delete (must be UTF-8 aligned!)
1405 \param cbArg "this" pointer for static callback function
1406 */
1407 void Fl_Text_Display_mod::buffer_predelete_cb(int pos, int nDeleted, void *cbArg) {
1408 Fl_Text_Display_mod *textD = (Fl_Text_Display_mod *)cbArg;
1409 if (textD->mContinuousWrap && !textD->mFastDisplay) {
1410 /* Note: we must perform this measurement, even if there is not a
1411 single character deleted; the number of "deleted" lines is the
1412 number of visual lines spanned by the real line in which the
1413 modification takes place.
1414 Also, a modification of the tab distance requires the same
1415 kind of calculations in advance, even if the font width is "fixed",
1416 because when the width of the tab characters changes, the layout
1417 of the text may be completely different. */
1418 IS_UTF8_ALIGNED2(textD->buffer(), pos)
1419 textD->measure_deleted_lines(pos, nDeleted);
1420 } else {
1421 textD->mSuppressResync = 0; /* Probably not needed, but just in case */
1422 }
1423 }
1424
1425
1426
1427 /**
1428 \brief This is called whenever the buffer is modified.
1429
1430 Callback attached to the text buffer to receive modification information
1431
1432 \param pos starting index of modification
1433 \param nInserted number of bytes we inserted (must be UTF-8 aligned!)
1434 \param nDeleted number of bytes deleted (must be UTF-8 aligned!)
1435 \param nRestyled ??
1436 \param deletedText this is what was removed, must not be NULL if nDeleted is set
1437 \param cbArg "this" pointer for static callback function
1438 */
1439 void Fl_Text_Display_mod::buffer_modified_cb( int pos, int nInserted, int nDeleted,
1440 int nRestyled, const char *deletedText, void *cbArg ) {
1441 int linesInserted, linesDeleted, startDispPos, endDispPos;
1442 Fl_Text_Display_mod *textD = ( Fl_Text_Display_mod * ) cbArg;
1443 Fl_Text_Buffer_mod *buf = textD->mBuffer;
1444 int oldFirstChar = textD->mFirstChar;
1445 int scrolled, origCursorPos = textD->mCursorPos;
1446 int wrapModStart = 0, wrapModEnd = 0;
1447
1448 IS_UTF8_ALIGNED2(buf, pos)
1449 IS_UTF8_ALIGNED2(buf, oldFirstChar)
1450
1451 /* buffer modification cancels vertical cursor motion column */
1452 if ( nInserted != 0 || nDeleted != 0 )
1453 textD->mCursorPreferredXPos = -1;
1454
1455 /* Count the number of lines inserted and deleted, and in the case
1456 of continuous wrap mode, how much has changed */
1457 if (textD->mContinuousWrap && !textD->mFastDisplay) {
1458 textD->find_wrap_range(deletedText, pos, nInserted, nDeleted,
1459 &wrapModStart, &wrapModEnd, &linesInserted, &linesDeleted);
1460 } else {
1461 linesInserted = nInserted == 0 ? 0 : buf->count_lines( pos, pos + nInserted );
1462 linesDeleted = nDeleted == 0 ? 0 : countlines( deletedText );
1463 }
1464
1465 /* Update the line starts and mTopLineNum */
1466 if ( nInserted != 0 || nDeleted != 0 ) {
1467 if (textD->mContinuousWrap && !textD->mFastDisplay) {
1468 textD->update_line_starts( wrapModStart, wrapModEnd-wrapModStart,
1469 nDeleted + pos-wrapModStart + (wrapModEnd-(pos+nInserted)),
1470 linesInserted, linesDeleted, &scrolled );
1471 } else {
1472 textD->update_line_starts( pos, nInserted, nDeleted, linesInserted,
1473 linesDeleted, &scrolled );
1474 }
1475 } else
1476 scrolled = 0;
1477
1478 /* If we're counting non-wrapped lines as well, maintain the absolute
1479 (non-wrapped) line number of the text displayed */
1480 if (textD->maintaining_absolute_top_line_number() &&
1481 (nInserted != 0 || nDeleted != 0)) {
1482 if (deletedText && (pos + nDeleted < oldFirstChar))
1483 textD->mAbsTopLineNum += buf->count_lines(pos, pos + nInserted) -
1484 countlines(deletedText);
1485 else if (pos < oldFirstChar)
1486 textD->reset_absolute_top_line_number();
1487 }
1488
1489 /* Update the line count for the whole buffer */
1490 textD->mNBufferLines += linesInserted - linesDeleted;
1491
1492 /* Update the cursor position */
1493 if ( textD->mCursorToHint != NO_HINT ) {
1494 textD->mCursorPos = textD->mCursorToHint;
1495 textD->mCursorToHint = NO_HINT;
1496 } else if ( textD->mCursorPos > pos ) {
1497 if ( textD->mCursorPos < pos + nDeleted )
1498 textD->mCursorPos = pos;
1499 else
1500 textD->mCursorPos += nInserted - nDeleted;
1501 }
1502
1503 // refigure scrollbars & stuff
1504 textD->resize(textD->x(), textD->y(), textD->w(), textD->h());
1505
1506 // don't need to do anything else if not visible?
1507 if (!textD->visible_r()) return;
1508
1509 /* If the changes caused scrolling, re-paint everything and we're done. */
1510 if ( scrolled ) {
1511 textD->damage(FL_DAMAGE_EXPOSE);
1512 if ( textD->mStyleBuffer ) /* See comments in extendRangeForStyleMods */
1513 textD->mStyleBuffer->primary_selection()->selected(0);
1514 return;
1515 }
1516
1517 /* If the changes didn't cause scrolling, decide the range of characters
1518 that need to be re-painted. Also if the cursor position moved, be
1519 sure that the redisplay range covers the old cursor position so the
1520 old cursor gets erased, and erase the bits of the cursor which extend
1521 beyond the left and right edges of the text. */
1522 startDispPos = (textD->mContinuousWrap && !textD->mFastDisplay) ? wrapModStart : pos;
1523 IS_UTF8_ALIGNED2(buf, startDispPos)
1524
1525 if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos )
1526 startDispPos = min( startDispPos, buf->prev_char_clipped(origCursorPos) );
1527 IS_UTF8_ALIGNED2(buf, startDispPos)
1528
1529 if ( linesInserted == linesDeleted ) {
1530 if ( nInserted == 0 && nDeleted == 0 )
1531 endDispPos = pos + nRestyled;
1532 else {
1533 endDispPos = textD->mContinuousWrap ? wrapModEnd :
1534 buf->line_end( pos + nInserted ) + 1;
1535 if (textD->mContinuousWrap && !textD->mFastDisplay)
1536 endDispPos = wrapModEnd;
1537 else
1538 endDispPos = buf->next_char(buf->line_end( pos + nInserted ));
1539
1540 // CET - FIXME if ( origCursorPos >= startDispPos &&
1541 // ( origCursorPos <= endDispPos || endDispPos == buf->length() ) )
1542 }
1543
1544 if (linesInserted > 1) textD->draw_line_numbers(false);
1545 } else {
1546 endDispPos = buf->next_char(textD->mLastChar);
1547 // CET - FIXME if ( origCursorPos >= pos )
1548 /* If more than one line is inserted/deleted, a line break may have
1549 been inserted or removed in between, and the line numbers may
1550 have changed. If only one line is altered, line numbers cannot
1551 be affected (the insertion or removal of a line break always
1552 results in at least two lines being redrawn). */
1553 textD->draw_line_numbers(false);
1554 }
1555 IS_UTF8_ALIGNED2(buf, startDispPos)
1556 IS_UTF8_ALIGNED2(buf, endDispPos)
1557
1558 /* If there is a style buffer, check if the modification caused additional
1559 changes that need to be redisplayed. (Redisplaying separately would
1560 cause double-redraw on almost every modification involving styled
1561 text). Extend the redraw range to incorporate style changes */
1562 if ( textD->mStyleBuffer )
1563 textD->extend_range_for_styles( &startDispPos, &endDispPos );
1564 IS_UTF8_ALIGNED2(buf, startDispPos)
1565 IS_UTF8_ALIGNED2(buf, endDispPos)
1566
1567 /* Redisplay computed range */
1568 textD->redisplay_range( startDispPos, endDispPos );
1569 }
1570
1571
1572
1573 /**
1574 \brief Line numbering stuff, currently unused.
1575
1576 In continuous wrap mode, internal line numbers are calculated after
1577 wrapping. A separate non-wrapped line count is maintained when line
1578 numbering is turned on. There is some performance cost to maintaining this
1579 line count, so normally absolute line numbers are not tracked if line
1580 numbering is off. This routine allows callers to specify that they still
1581 want this line count maintained (for use via TextDPosToLineAndCol).
1582 More specifically, this allows the line number reported in the statistics
1583 line to be calibrated in absolute lines, rather than post-wrapped lines.
1584 */
1585 void Fl_Text_Display_mod::maintain_absolute_top_line_number(int state) {
1586 mNeedAbsTopLineNum = state;
1587 reset_absolute_top_line_number();
1588 }
1589
1590
1591
1592 /**
1593 \brief Line numbering stuff, currently unused.
1594
1595 Returns the absolute (non-wrapped) line number of the first line displayed.
1596 Returns 0 if the absolute top line number is not being maintained.
1597 */
1598 int Fl_Text_Display_mod::get_absolute_top_line_number() const {
1599 if (mFastDisplay || !mContinuousWrap)
1600 return mTopLineNum;
1601 if (maintaining_absolute_top_line_number())
1602 return mAbsTopLineNum;
1603 return 0;
1604 }
1605
1606
1607
1608 /**
1609 \brief Line numbering stuff, currently unused.
1610
1611 Re-calculate absolute top line number for a change in scroll position.
1612 */
1613 void Fl_Text_Display_mod::absolute_top_line_number(int oldFirstChar) {
1614 if (maintaining_absolute_top_line_number()) {
1615 if (mFirstChar < oldFirstChar)
1616 mAbsTopLineNum -= buffer()->count_lines(mFirstChar, oldFirstChar);
1617 else
1618 mAbsTopLineNum += buffer()->count_lines(oldFirstChar, mFirstChar);
1619 }
1620 }
1621
1622
1623
1624 /**
1625 \brief Line numbering stuff, currently unused.
1626
1627 Return true if a separate absolute top line number is being maintained
1628 (for displaying line numbers or showing in the statistics line).
1629 */
1630 int Fl_Text_Display_mod::maintaining_absolute_top_line_number() const {
1631 return (!mFastDisplay && mContinuousWrap && (mLineNumWidth != 0 || mNeedAbsTopLineNum));
1632 }
1633
1634
1635
1636 /**
1637 \brief Line numbering stuff, probably unused.
1638
1639 Count lines from the beginning of the buffer to reestablish the
1640 absolute (non-wrapped) top line number. If mode is not continuous wrap,
1641 or the number is not being maintained, does nothing.
1642 */
1643 void Fl_Text_Display_mod::reset_absolute_top_line_number() {
1644 mAbsTopLineNum = 1;
1645 absolute_top_line_number(0);
1646 }
1647
1648
1649
1650 /**
1651 \brief Convert a position index into a line number offset.
1652
1653 Find the line number of position \p pos relative to the first line of
1654 displayed text. Returns 0 if the line is not displayed.
1655
1656 \param pos ??
1657 \param[out] lineNum ??
1658 \return ??
1659 \todo What does this do?
1660 */
1661 int Fl_Text_Display_mod::position_to_line( int pos, int *lineNum ) const {
1662 IS_UTF8_ALIGNED2(buffer(), pos)
1663
1664 int i;
1665
1666 *lineNum = 0;
1667 if ( pos < mFirstChar ) return 0;
1668 if ( pos > mLastChar ) {
1669 if ( empty_vlines() ) {
1670 if ( mLastChar < mBuffer->length() ) {
1671 if ( !position_to_line( mLastChar, lineNum ) ) {
1672 Fl::error("Fl_Text_Display_mod::position_to_line(): Consistency check ptvl failed");
1673 return 0;
1674 }
1675 return ++( *lineNum ) <= mNVisibleLines - 1;
1676 } else {
1677 position_to_line( buffer()->prev_char_clipped(mLastChar), lineNum );
1678 return 1;
1679 }
1680 }
1681 return 0;
1682 }
1683
1684 for ( i = mNVisibleLines - 1; i >= 0; i-- ) {
1685 if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) {
1686 *lineNum = i;
1687 return 1;
1688 }
1689 }
1690 return 0; /* probably never be reached */
1691 }
1692
1693
1694 /**
1695 Universal pixel machine.
1696
1697 We use a single function that handles all line layout, measuring, and drawing
1698 \li draw a text range
1699 \li return the width of a text range in pixels
1700 \li return the index of a character that is at a pixel position
1701
1702 \param[in] mode DRAW_LINE, GET_WIDTH, FIND_INDEX
1703 \param[in] lineStartPos index of first character
1704 \param[in] lineLen size of string in bytes
1705 \param[in] leftChar, rightChar
1706 \param[in] Y drawing position
1707 \param[in] bottomClip, leftClip, rightClip stop work when we reach the clipped
1708 area. rightClip is the X position that we search in FIND_INDEX.
1709 \retval DRAW_LINE index of last drawn character
1710 \retval GET_WIDTH width in pixels of text segment if we would draw it
1711 \retval FIND_INDEX index of character at given x position in window coordinates
1712 \retval FIND_INDEX_FROM_ZERO index of character at given x position without scrolling and widget offsets
1713 \todo we need to handle hidden hyphens and tabs here!
1714 \todo we handle all styles and selections
1715 \todo we must provide code to get pixel positions of the middle of a character as well
1716 */
1717 int Fl_Text_Display_mod::handle_vline(
1718 int mode,
1719 int lineStartPos, int lineLen, int leftChar, int rightChar,
1720 int Y, int bottomClip,
1721 int leftClip, int rightClip) const
1722 {
1723 IS_UTF8_ALIGNED2(buffer(), lineStartPos)
1724
1725 // FIXME: we need to allow two modes for FIND_INDEX: one on the edge of the
1726 // FIXME: character for selection, and one on the character center for cursors.
1727 int i, X, startX, startIndex, style, charStyle;
1728 char *lineStr;
1729
1730 if ( lineStartPos == -1 ) {
1731 lineStr = NULL;
1732 } else {
1733 lineStr = mBuffer->text_range( lineStartPos, lineStartPos + lineLen );
1734 }
1735
1736 if (mode==GET_WIDTH) {
1737 X = 0;
1738 } else if (mode==FIND_INDEX_FROM_ZERO) {
1739 X = 0;
1740 mode = FIND_INDEX;
1741 } else {
1742 X = text_area.x - mHorizOffset;
1743 }
1744
1745 startX = X;
1746 startIndex = 0;
1747 if (!lineStr) {
1748 // just clear the background
1749 if (mode==DRAW_LINE) {
1750 style = position_style(lineStartPos, lineLen, -1);
1751 draw_string( style|BG_ONLY_MASK, text_area.x, Y, text_area.x+text_area.w, lineStr, lineLen );
1752 }
1753 if (mode==FIND_INDEX) {
1754 IS_UTF8_ALIGNED2(buffer(), lineStartPos)
1755 return lineStartPos;
1756 }
1757 return 0;
1758 }
1759
1760 char currChar = 0, prevChar = 0;
1761 // draw the line
1762 style = position_style(lineStartPos, lineLen, 0);
1763 for (i=0; i<lineLen; ) {
1764 currChar = lineStr[i]; // one byte is enough to handele tabs and other cases
1765 int len = fl_utf8len1(currChar);
1766 if (len<=0) len = 1; // OUCH!
1767 charStyle = position_style(lineStartPos, lineLen, i);
1768 if (charStyle!=style || currChar=='\t' || prevChar=='\t') {
1769 // draw a segment whenever the style changes or a Tab is found
1770 int w = 0;
1771 if (prevChar=='\t') {
1772 // draw a single Tab space
1773 int tab = (int)col_to_x(mBuffer->tab_distance());
1774 int xAbs = (mode==GET_WIDTH) ? startX : startX+mHorizOffset-text_area.x;
1775 w = (((xAbs/tab)+1)*tab) - xAbs;
1776 if (mode==DRAW_LINE)
1777 draw_string( style|BG_ONLY_MASK, startX, Y, startX+w, 0, 0 );
1778 if (mode==FIND_INDEX && startX+w>rightClip) {
1779 // find x pos inside block
1780 free(lineStr);
1781 return lineStartPos + startIndex;
1782 }
1783 } else {
1784 // draw a text segment
1785 w = int( string_width( lineStr+startIndex, i-startIndex, style ) );
1786 if (mode==DRAW_LINE)
1787 draw_string( style, startX, Y, startX+w, lineStr+startIndex, i-startIndex );
1788 if (mode==FIND_INDEX && startX+w>rightClip) {
1789 // find x pos inside block
1790 int di = find_x(lineStr+startIndex, i-startIndex, style, rightClip-startX);
1791 free(lineStr);
1792 IS_UTF8_ALIGNED2(buffer(), (lineStartPos+startIndex+di))
1793 return lineStartPos + startIndex + di;
1794 }
1795 }
1796 style = charStyle;
1797 startX += w;
1798 startIndex = i;
1799 }
1800 i += len;
1801 prevChar = currChar;
1802 }
1803 int w = 0;
1804 if (currChar=='\t') {
1805 // draw a single Tab space
1806 int tab = (int)col_to_x(mBuffer->tab_distance());
1807 int xAbs = (mode==GET_WIDTH) ? startX : startX+mHorizOffset-text_area.x;
1808 w = (((xAbs/tab)+1)*tab) - xAbs;
1809 if (mode==DRAW_LINE)
1810 draw_string( style|BG_ONLY_MASK, startX, Y, startX+w, 0, 0 );
1811 if (mode==FIND_INDEX) {
1812 // find x pos inside block
1813 free(lineStr);
1814 return lineStartPos + startIndex + ( rightClip-startX>w ? 1 : 0 );
1815 }
1816 } else {
1817 w = int( string_width( lineStr+startIndex, i-startIndex, style ) );
1818 if (mode==DRAW_LINE)
1819 draw_string( style, startX, Y, startX+w, lineStr+startIndex, i-startIndex );
1820 if (mode==FIND_INDEX) {
1821 // find x pos inside block
1822 int di = find_x(lineStr+startIndex, i-startIndex, style, rightClip-startX);
1823 free(lineStr);
1824 IS_UTF8_ALIGNED2(buffer(), (lineStartPos+startIndex+di))
1825 return lineStartPos + startIndex + di;
1826 }
1827 }
1828 if (mode==GET_WIDTH) {
1829 free(lineStr);
1830 return startX+w;
1831 }
1832
1833 // clear the rest of the line
1834 startX += w;
1835 style = position_style(lineStartPos, lineLen, i);
1836 if (mode==DRAW_LINE)
1837 draw_string( style|BG_ONLY_MASK, startX, Y, text_area.x+text_area.w, lineStr, lineLen );
1838
1839 free(lineStr);
1840 IS_UTF8_ALIGNED2(buffer(), (lineStartPos+lineLen))
1841 return lineStartPos + lineLen;
1842 }
1843
1844
1845 /**
1846 \brief Find the index of the character that lies at the given x position.
1847 \param s UTF-8 text string
1848 \param len length of string
1849 \param style index into style lookup table
1850 \param x position in pixels
1851 \return index into buffer
1852 */
1853 int Fl_Text_Display_mod::find_x(const char *s, int len, int style, int x) const {
1854 IS_UTF8_ALIGNED(s)
1855
1856 // TODO: use binary search which may be quicker.
1857 int i = 0;
1858 while (i<len) {
1859 int cl = fl_utf8len1(s[i]);
1860 int w = int( string_width(s, i+cl, style) );
1861 if (w>x)
1862 return i;
1863 i += cl;
1864 }
1865 return len;
1866 }
1867
1868
1869
1870 /**
1871 \brief Draw a single line of text.
1872
1873 Draw the text on a single line represented by \p visLineNum (the
1874 number of lines down from the top of the display), limited by
1875 \p leftClip and \p rightClip window coordinates and \p leftCharIndex and
1876 \p rightCharIndex character positions (not including the character at
1877 position \p rightCharIndex).
1878
1879 \param visLineNum index of line in the visible line number lookup
1880 \param leftClip, rightClip pixel position of clipped area
1881 \param leftCharIndex, rightCharIndex index into line of segment that we want to draw
1882 */
1883 void Fl_Text_Display_mod::draw_vline(int visLineNum, int leftClip, int rightClip,
1884 int leftCharIndex, int rightCharIndex) {
1885 int Y, lineStartPos, lineLen, fontHeight;
1886
1887 // printf("draw_vline(visLineNum=%d, leftClip=%d, rightClip=%d, leftCharIndex=%d, rightCharIndex=%d)\n",
1888 // visLineNum, leftClip, rightClip, leftCharIndex, rightCharIndex);
1889 // printf("nNVisibleLines=%d\n", mNVisibleLines);
1890
1891 /* If line is not displayed, skip it */
1892 if ( visLineNum < 0 || visLineNum >= mNVisibleLines )
1893 return;
1894
1895 /* Calculate Y coordinate of the string to draw */
1896 fontHeight = mMaxsize;
1897 Y = text_area.y + visLineNum * fontHeight;
1898
1899 /* Get the text, length, and buffer position of the line to display */
1900 lineStartPos = mLineStarts[ visLineNum ];
1901 if ( lineStartPos == -1 ) {
1902 lineLen = 0;
1903 } else {
1904 lineLen = vline_length( visLineNum );
1905 }
1906
1907 /* Shrink the clipping range to the active display area */
1908 leftClip = max( text_area.x, leftClip );
1909 rightClip = min( rightClip, text_area.x + text_area.w );
1910
1911 handle_vline(DRAW_LINE,
1912 lineStartPos, lineLen, leftCharIndex, rightCharIndex,
1913 Y, Y+fontHeight, leftClip, rightClip);
1914 return;
1915 }
1916
1917
1918
1919 /**
1920 \brief Draw a text segment in a single style.
1921
1922 Draw a string or blank area according to parameter \p style, using the
1923 appropriate colors and drawing method for that style, with top left
1924 corner at \p X, \p Y. If style says to draw text, use \p string as
1925 source of characters, and draw \p nChars, if style is FILL, erase
1926 rectangle where text would have drawn from \p X to \p toX and from
1927 \p Y to the maximum y extent of the current font(s).
1928
1929 \param style index into style lookup table
1930 \param X, Y drawing origin
1931 \param toX rightmost position if this is a fill operation
1932 \param string text if this is a drawing operation
1933 \param nChars number of characters to draw
1934 */
1935 void Fl_Text_Display_mod::draw_string(int style,
1936 int X, int Y, int toX,
1937 const char *string, int nChars) const {
1938 IS_UTF8_ALIGNED(string)
1939
1940 const Style_Table_Entry * styleRec;
1941
1942 /* Draw blank area rather than text, if that was the request */
1943 if ( style & FILL_MASK ) {
1944 if (style & TEXT_ONLY_MASK) return;
1945 clear_rect( style, X, Y, toX - X, mMaxsize );
1946 return;
1947 }
1948 /* Set font, color, and gc depending on style. For normal text, GCs
1949 for normal drawing, or drawing within a Fl_Text_Selection or highlight are
1950 pre-allocated and pre-configured. For syntax highlighting, GCs are
1951 configured here, on the fly. */
1952
1953 Fl_Font font = textfont();
1954 int fsize = textsize();
1955 Fl_Color foreground;
1956 Fl_Color background;
1957
1958 if ( style & STYLE_LOOKUP_MASK ) {
1959 int si = (style & STYLE_LOOKUP_MASK) - 'A';
1960 if (si < 0) si = 0;
1961 else if (si >= mNStyles) si = mNStyles - 1;
1962
1963 styleRec = mStyleTable + si;
1964 font = styleRec->font;
1965 fsize = styleRec->size;
1966
1967 if (style & PRIMARY_MASK) {
1968 if (Fl::focus() == (Fl_Widget*)this) background = selection_color();
1969 else background = fl_color_average(color(), selection_color(), 0.4f);
1970 } else if (style & HIGHLIGHT_MASK) {
1971 if (Fl::focus() == (Fl_Widget*)this) background = fl_color_average(color(), selection_color(), 0.5f);
1972 else background = fl_color_average(color(), selection_color(), 0.6f);
1973 } else background = color();
1974 foreground = fl_contrast(styleRec->color, background);
1975 } else if (style & PRIMARY_MASK) {
1976 if (Fl::focus() == (Fl_Widget*)this) background = selection_color();
1977 else background = fl_color_average(color(), selection_color(), 0.4f);
1978 foreground = fl_contrast(textcolor(), background);
1979 } else if (style & HIGHLIGHT_MASK) {
1980 if (Fl::focus() == (Fl_Widget*)this) background = fl_color_average(color(), selection_color(), 0.5f);
1981 else background = fl_color_average(color(), selection_color(), 0.6f);
1982 foreground = fl_contrast(textcolor(), background);
1983 } else {
1984 foreground = textcolor();
1985 background = color();
1986 }
1987
1988 if (!(style & TEXT_ONLY_MASK)) {
1989 fl_color( background );
1990 fl_rectf( X, Y, toX - X, mMaxsize );
1991 }
1992 if (!(style & BG_ONLY_MASK)) {
1993 fl_color( foreground );
1994 fl_font( font, fsize );
1995 #if !(defined(__APPLE__) || defined(WIN32)) && USE_XFT
1996 // makes sure antialiased ÄÖÜ do not leak on line above
1997 fl_push_clip(X, Y, toX - X, mMaxsize);
1998 #endif
1999 fl_draw( string, nChars, X, Y + mMaxsize - fl_descent());
2000 #if !(defined(__APPLE__) || defined(WIN32)) && USE_XFT
2001 fl_pop_clip();
2002 #endif
2003 }
2004
2005 // CET - FIXME
2006 /* If any space around the character remains unfilled (due to use of
2007 different sized fonts for highlighting), fill in above or below
2008 to erase previously drawn characters */
2009 /*
2010 if (fs->ascent < mAscent)
2011 clear_rect( style, X, Y, toX - X, mAscent - fs->ascent);
2012 if (fs->descent < mDescent)
2013 clear_rect( style, X, Y + mAscent + fs->descent, toX - x,
2014 mDescent - fs->descent);
2015 */
2016 /* Underline if style is secondary Fl_Text_Selection */
2017
2018 /*
2019 if (style & SECONDARY_MASK)
2020 XDrawLine(XtDisplay(mW), XtWindow(mW), gc, x,
2021 y + mAscent, toX - 1, Y + fs->ascent);
2022 */
2023 }
2024
2025
2026
2027 /**
2028 \brief Clear a rectangle with the appropriate background color for \p style.
2029
2030 \param style index into style table
2031 \param X, Y, width, height size and position of background area
2032 */
2033 void Fl_Text_Display_mod::clear_rect(int style,
2034 int X, int Y,
2035 int width, int height) const {
2036 /* A width of zero means "clear to end of window" to XClearArea */
2037 if ( width == 0 )
2038 return;
2039
2040 if (style & PRIMARY_MASK) {
2041 if (Fl::focus()==(Fl_Widget*)this) {
2042 fl_color(selection_color());
2043 } else {
2044 fl_color(fl_color_average(color(), selection_color(), 0.4f));
2045 }
2046 } else if (style & HIGHLIGHT_MASK) {
2047 if (Fl::focus()==(Fl_Widget*)this) {
2048 fl_color(fl_color_average(color(), selection_color(), 0.5f));
2049 } else {
2050 fl_color(fl_color_average(color(), selection_color(), 0.6f));
2051 }
2052 } else {
2053 fl_color( color() );
2054 }
2055 fl_rectf( X, Y, width, height );
2056 }
2057
2058
2059
2060 /**
2061 \brief Draw a cursor with top center at \p X, \p Y.
2062
2063 \param X, Y cursor position in pixels
2064 */
2065 void Fl_Text_Display_mod::draw_cursor( int X, int Y ) {
2066
2067 typedef struct {
2068 int x1, y1, x2, y2;
2069 }
2070 Segment;
2071
2072 Segment segs[ 5 ];
2073 int left, right, cursorWidth, midY;
2074 // int fontWidth = mFontStruct->min_bounds.width, nSegs = 0;
2075 int fontWidth = TMPFONTWIDTH; // CET - FIXME
2076 int nSegs = 0;
2077 int fontHeight = mMaxsize;
2078 int bot = Y + fontHeight - 1;
2079
2080 if ( X < text_area.x - 1 || X > text_area.x + text_area.w )
2081 return;
2082
2083 /* For cursors other than the block, make them around 2/3 of a character
2084 width, rounded to an even number of pixels so that X will draw an
2085 odd number centered on the stem at x. */
2086 cursorWidth = 4; //(fontWidth/3) * 2;
2087 left = X - cursorWidth / 2;
2088 right = left + cursorWidth;
2089
2090 /* Create segments and draw cursor */
2091 if ( mCursorStyle == CARET_CURSOR ) {
2092 midY = bot - fontHeight / 5;
2093 segs[ 0 ].x1 = left; segs[ 0 ].y1 = bot; segs[ 0 ].x2 = X; segs[ 0 ].y2 = midY;
2094 segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot;
2095 segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = midY - 1;
2096 segs[ 3 ].x1 = X; segs[ 3 ].y1 = midY - 1; segs[ 3 ].x2 = right; segs[ 3 ].y2 = bot;
2097 nSegs = 4;
2098 } else if ( mCursorStyle == NORMAL_CURSOR ) {
2099 segs[ 0 ].x1 = left; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y;
2100 segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot;
2101 segs[ 2 ].x1 = left; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = right; segs[ 2 ].y2 = bot;
2102 nSegs = 3;
2103 } else if ( mCursorStyle == HEAVY_CURSOR ) {
2104 segs[ 0 ].x1 = X - 1; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X - 1; segs[ 0 ].y2 = bot;
2105 segs[ 1 ].x1 = X; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = X; segs[ 1 ].y2 = bot;
2106 segs[ 2 ].x1 = X + 1; segs[ 2 ].y1 = Y; segs[ 2 ].x2 = X + 1; segs[ 2 ].y2 = bot;
2107 segs[ 3 ].x1 = left; segs[ 3 ].y1 = Y; segs[ 3 ].x2 = right; segs[ 3 ].y2 = Y;
2108 segs[ 4 ].x1 = left; segs[ 4 ].y1 = bot; segs[ 4 ].x2 = right; segs[ 4 ].y2 = bot;
2109 nSegs = 5;
2110 } else if ( mCursorStyle == DIM_CURSOR ) {
2111 midY = Y + fontHeight / 2;
2112 segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = X; segs[ 0 ].y2 = Y;
2113 segs[ 1 ].x1 = X; segs[ 1 ].y1 = midY; segs[ 1 ].x2 = X; segs[ 1 ].y2 = midY;
2114 segs[ 2 ].x1 = X; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot;
2115 nSegs = 3;
2116 } else if ( mCursorStyle == BLOCK_CURSOR ) {
2117 right = X + fontWidth;
2118 segs[ 0 ].x1 = X; segs[ 0 ].y1 = Y; segs[ 0 ].x2 = right; segs[ 0 ].y2 = Y;
2119 segs[ 1 ].x1 = right; segs[ 1 ].y1 = Y; segs[ 1 ].x2 = right; segs[ 1 ].y2 = bot;
2120 segs[ 2 ].x1 = right; segs[ 2 ].y1 = bot; segs[ 2 ].x2 = X; segs[ 2 ].y2 = bot;
2121 segs[ 3 ].x1 = X; segs[ 3 ].y1 = bot; segs[ 3 ].x2 = X; segs[ 3 ].y2 = Y;
2122 nSegs = 4;
2123 }
2124 fl_color( mCursor_color );
2125
2126 for ( int k = 0; k < nSegs; k++ ) {
2127 fl_line( segs[ k ].x1, segs[ k ].y1, segs[ k ].x2, segs[ k ].y2 );
2128 }
2129 }
2130
2131
2132
2133 /**
2134 \brief Find the correct style for a character.
2135
2136 Determine the drawing method to use to draw a specific character from "buf".
2137 \p lineStartPos gives the character index where the line begins, \p lineIndex,
2138 the number of characters past the beginning of the line, and \p lineIndex
2139 the number of displayed characters past the beginning of the line. Passing
2140 \p lineStartPos of -1 returns the drawing style for "no text".
2141
2142 Why not just: position_style(pos)? Because style applies to blank areas
2143 of the window beyond the text boundaries, and because this routine must also
2144 decide whether a position is inside of a rectangular Fl_Text_Selection, and do
2145 so efficiently, without re-counting character positions from the start of the
2146 line.
2147
2148 Note that style is a somewhat incorrect name, drawing method would
2149 be more appropriate.
2150
2151 \param lineStartPos beginning of this line
2152 \param lineLen number of bytes in line
2153 \param lineIndex position of character within line
2154 \return style for the given character
2155 */
2156 int Fl_Text_Display_mod::position_style( int lineStartPos, int lineLen, int lineIndex) const
2157 {
2158 IS_UTF8_ALIGNED2(buffer(), lineStartPos)
2159
2160 Fl_Text_Buffer_mod * buf = mBuffer;
2161 Fl_Text_Buffer_mod *styleBuf = mStyleBuffer;
2162 int pos, style = 0;
2163
2164 if ( lineStartPos == -1 || buf == NULL )
2165 return FILL_MASK;
2166
2167 pos = lineStartPos + min( lineIndex, lineLen );
2168
2169 if ( lineIndex >= lineLen )
2170 style = FILL_MASK;
2171 else if ( styleBuf != NULL ) {
2172 style = ( unsigned char ) styleBuf->byte_at( pos );
2173 if (style == mUnfinishedStyle && mUnfinishedHighlightCB) {
2174 /* encountered "unfinished" style, trigger parsing */
2175 (mUnfinishedHighlightCB)( pos, mHighlightCBArg);
2176 style = (unsigned char) styleBuf->byte_at( pos);
2177 }
2178 }
2179 if (buf->primary_selection()->includes(pos))
2180 style |= PRIMARY_MASK;
2181 if (buf->highlight_selection()->includes(pos))
2182 style |= HIGHLIGHT_MASK;
2183 if (buf->secondary_selection()->includes(pos))
2184 style |= SECONDARY_MASK;
2185 return style;
2186 }
2187
2188
2189 /**
2190 \brief Find the width of a string in the font of a particular style.
2191
2192 \param string the text
2193 \param length number of bytes in string
2194 \param style index into style table
2195 \return width of text segment in pixels
2196 */
2197 double Fl_Text_Display_mod::string_width( const char *string, int length, int style ) const {
2198 IS_UTF8_ALIGNED(string)
2199
2200 Fl_Font font;
2201 Fl_Fontsize fsize;
2202
2203 if ( mNStyles && (style & STYLE_LOOKUP_MASK) ) {
2204 int si = (style & STYLE_LOOKUP_MASK) - 'A';
2205 if (si < 0) si = 0;
2206 else if (si >= mNStyles) si = mNStyles - 1;
2207
2208 font = mStyleTable[si].font;
2209 fsize = mStyleTable[si].size;
2210 } else {
2211 font = textfont();
2212 fsize = textsize();
2213 }
2214 fl_font( font, fsize );
2215 return fl_width( string, length );
2216 }
2217
2218
2219
2220 /**
2221 \brief Translate a pixel position into a character index.
2222
2223 Translate window coordinates to the nearest (insert cursor or character
2224 cell) text position. The parameter \p posType specifies how to interpret the
2225 position: CURSOR_POS means translate the coordinates to the nearest cursor
2226 position, and CHARACTER_POS means return the position of the character
2227 closest to (\p X, \p Y).
2228
2229 \param X, Y pixel position
2230 \param posType CURSOR_POS or CHARACTER_POS
2231 \return index into text buffer
2232 */
2233 int Fl_Text_Display_mod::xy_to_position( int X, int Y, int posType ) const {
2234 int lineStart, lineLen, fontHeight;
2235 int visLineNum;
2236
2237 /* Find the visible line number corresponding to the Y coordinate */
2238 fontHeight = mMaxsize;
2239 visLineNum = ( Y - text_area.y ) / fontHeight;
2240 if ( visLineNum < 0 )
2241 return mFirstChar;
2242 if ( visLineNum >= mNVisibleLines )
2243 visLineNum = mNVisibleLines - 1;
2244
2245 /* Find the position at the start of the line */
2246 lineStart = mLineStarts[ visLineNum ];
2247
2248 /* If the line start was empty, return the last position in the buffer */
2249 if ( lineStart == -1 )
2250 return mBuffer->length();
2251
2252 /* Get the line text and its length */
2253 lineLen = vline_length( visLineNum );
2254
2255 return handle_vline(FIND_INDEX,
2256 lineStart, lineLen, 0, 0,
2257 0, 0,
2258 text_area.x, X);
2259 }
2260
2261
2262
2263 /**
2264 \brief Translate pixel coordinates into row and column.
2265
2266 Translate window coordinates to the nearest row and column number for
2267 positioning the cursor. This, of course, makes no sense when the font is
2268 proportional, since there are no absolute columns. The parameter posType
2269 specifies how to interpret the position: CURSOR_POS means translate the
2270 coordinates to the nearest position between characters, and CHARACTER_POS
2271 means translate the position to the nearest character cell.
2272
2273 \param X, Y pixel coordinates
2274 \param[out] row, column neares row and column
2275 \param posType CURSOR_POS or CHARACTER_POS
2276 */
2277 void Fl_Text_Display_mod::xy_to_rowcol( int X, int Y, int *row,
2278 int *column, int posType ) const {
2279 int fontHeight = mMaxsize;
2280 int fontWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width;
2281
2282 /* Find the visible line number corresponding to the Y coordinate */
2283 *row = ( Y - text_area.y ) / fontHeight;
2284 if ( *row < 0 ) *row = 0;
2285 if ( *row >= mNVisibleLines ) *row = mNVisibleLines - 1;
2286
2287 *column = ( ( X - text_area.x ) + mHorizOffset +
2288 ( posType == CURSOR_POS ? fontWidth / 2 : 0 ) ) / fontWidth;
2289 if ( *column < 0 ) * column = 0;
2290 }
2291
2292
2293
2294 /**
2295 \brief Offset line start counters for a new vertical scroll position.
2296
2297 Offset the line starts array, mTopLineNum, mFirstChar and lastChar, for a new
2298 vertical scroll position given by newTopLineNum. If any currently displayed
2299 lines will still be visible, salvage the line starts values, otherwise,
2300 count lines from the nearest known line start (start or end of buffer, or
2301 the closest value in the mLineStarts array)
2302
2303 \param newTopLineNum index into buffer
2304 */
2305 void Fl_Text_Display_mod::offset_line_starts( int newTopLineNum ) {
2306 int oldTopLineNum = mTopLineNum;
2307 int oldFirstChar = mFirstChar;
2308 int lineDelta = newTopLineNum - oldTopLineNum;
2309 int nVisLines = mNVisibleLines;
2310 int *lineStarts = mLineStarts;
2311 int i, lastLineNum;
2312 Fl_Text_Buffer_mod *buf = mBuffer;
2313
2314 /* If there was no offset, nothing needs to be changed */
2315 if ( lineDelta == 0 )
2316 return;
2317
2318 /* Find the new value for mFirstChar by counting lines from the nearest
2319 known line start (start or end of buffer, or the closest value in the
2320 lineStarts array) */
2321 lastLineNum = oldTopLineNum + nVisLines - 1;
2322 if ( newTopLineNum < oldTopLineNum && newTopLineNum < -lineDelta ) {
2323 mFirstChar = skip_lines( 0, newTopLineNum - 1, true );
2324 } else if ( newTopLineNum < oldTopLineNum ) {
2325 mFirstChar = rewind_lines( mFirstChar, -lineDelta );
2326 } else if ( newTopLineNum < lastLineNum ) {
2327 mFirstChar = lineStarts[ newTopLineNum - oldTopLineNum ];
2328 } else if ( newTopLineNum - lastLineNum < mNBufferLines - newTopLineNum ) {
2329 mFirstChar = skip_lines( lineStarts[ nVisLines - 1 ],
2330 newTopLineNum - lastLineNum, true );
2331 } else {
2332 mFirstChar = rewind_lines( buf->length(), mNBufferLines - newTopLineNum + 1 );
2333 }
2334
2335 /* Fill in the line starts array */
2336 if ( lineDelta < 0 && -lineDelta < nVisLines ) {
2337 for ( i = nVisLines - 1; i >= -lineDelta; i-- )
2338 lineStarts[ i ] = lineStarts[ i + lineDelta ];
2339 calc_line_starts( 0, -lineDelta );
2340 } else if ( lineDelta > 0 && lineDelta < nVisLines ) {
2341 for ( i = 0; i < nVisLines - lineDelta; i++ )
2342 lineStarts[ i ] = lineStarts[ i + lineDelta ];
2343 calc_line_starts( nVisLines - lineDelta, nVisLines - 1 );
2344 } else
2345 calc_line_starts( 0, nVisLines );
2346
2347 /* Set lastChar and mTopLineNum */
2348 calc_last_char();
2349 mTopLineNum = newTopLineNum;
2350
2351 /* If we're numbering lines or being asked to maintain an absolute line
2352 number, re-calculate the absolute line number */
2353 absolute_top_line_number(oldFirstChar);
2354 }
2355
2356
2357
2358 /**
2359 \brief Update line start arrays and variables.
2360
2361 Update the line starts array, mTopLineNum, mFirstChar and lastChar for this
2362 text display after a modification to the text buffer, given by the
2363 position \p pos where the change began, and the numbers of characters
2364 and lines inserted and deleted.
2365
2366 \param pos index into buffer of recent changes
2367 \param charsInserted number of bytes(!) inserted
2368 \param charsDeleted number of bytes(!) deleted
2369 \param linesInserted number of lines
2370 \param linesDeleted number of lines
2371 \param[out] scrolled set to 1 if the text display needs to be scrolled
2372 */
2373 void Fl_Text_Display_mod::update_line_starts(int pos, int charsInserted,
2374 int charsDeleted, int linesInserted,
2375 int linesDeleted, int *scrolled ) {
2376 IS_UTF8_ALIGNED2(buffer(), pos)
2377
2378 int *lineStarts = mLineStarts;
2379 int i, lineOfPos, lineOfEnd, nVisLines = mNVisibleLines;
2380 int charDelta = charsInserted - charsDeleted;
2381 int lineDelta = linesInserted - linesDeleted;
2382
2383 /* If all of the changes were before the displayed text, the display
2384 doesn't change, just update the top line num and offset the line
2385 start entries and first and last characters */
2386 if ( pos + charsDeleted < mFirstChar ) {
2387 mTopLineNum += lineDelta;
2388 for ( i = 0; i < nVisLines && lineStarts[i] != -1; i++ )
2389 lineStarts[ i ] += charDelta;
2390 mFirstChar += charDelta;
2391 mLastChar += charDelta;
2392 *scrolled = 0;
2393 return;
2394 }
2395
2396 /* The change began before the beginning of the displayed text, but
2397 part or all of the displayed text was deleted */
2398 if ( pos < mFirstChar ) {
2399 /* If some text remains in the window, anchor on that */
2400 if ( position_to_line( pos + charsDeleted, &lineOfEnd ) &&
2401 ++lineOfEnd < nVisLines && lineStarts[ lineOfEnd ] != -1 ) {
2402 mTopLineNum = max( 1, mTopLineNum + lineDelta );
2403 mFirstChar = rewind_lines(lineStarts[ lineOfEnd ] + charDelta, lineOfEnd );
2404 /* Otherwise anchor on original line number and recount everything */
2405 } else {
2406 if ( mTopLineNum > mNBufferLines + lineDelta ) {
2407 mTopLineNum = 1;
2408 mFirstChar = 0;
2409 } else
2410 mFirstChar = skip_lines( 0, mTopLineNum - 1, true );
2411 }
2412 calc_line_starts( 0, nVisLines - 1 );
2413 /* calculate lastChar by finding the end of the last displayed line */
2414 calc_last_char();
2415 *scrolled = 1;
2416 return;
2417 }
2418
2419 /* If the change was in the middle of the displayed text (it usually is),
2420 salvage as much of the line starts array as possible by moving and
2421 offsetting the entries after the changed area, and re-counting the
2422 added lines or the lines beyond the salvaged part of the line starts
2423 array */
2424 if ( pos <= mLastChar ) {
2425 /* find line on which the change began */
2426 position_to_line( pos, &lineOfPos );
2427 /* salvage line starts after the changed area */
2428 if ( lineDelta == 0 ) {
2429 for ( i = lineOfPos + 1; i < nVisLines && lineStarts[ i ] != -1; i++ )
2430 lineStarts[ i ] += charDelta;
2431 } else if ( lineDelta > 0 ) {
2432 for ( i = nVisLines - 1; i >= lineOfPos + lineDelta + 1; i-- )
2433 lineStarts[ i ] = lineStarts[ i - lineDelta ] +
2434 ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta );
2435 } else /* (lineDelta < 0) */ {
2436 for ( i = max( 0, lineOfPos + 1 ); i < nVisLines + lineDelta; i++ )
2437 lineStarts[ i ] = lineStarts[ i - lineDelta ] +
2438 ( lineStarts[ i - lineDelta ] == -1 ? 0 : charDelta );
2439 }
2440 /* fill in the missing line starts */
2441 if ( linesInserted >= 0 )
2442 calc_line_starts( lineOfPos + 1, lineOfPos + linesInserted );
2443 if ( lineDelta < 0 )
2444 calc_line_starts( nVisLines + lineDelta, nVisLines );
2445 /* calculate lastChar by finding the end of the last displayed line */
2446 calc_last_char();
2447 *scrolled = 0;
2448 return;
2449 }
2450
2451 /* Change was past the end of the displayed text, but displayable by virtue
2452 of being an insert at the end of the buffer into visible blank lines */
2453 if ( empty_vlines() ) {
2454 position_to_line( pos, &lineOfPos );
2455 calc_line_starts( lineOfPos, lineOfPos + linesInserted );
2456 calc_last_char();
2457 *scrolled = 0;
2458 return;
2459 }
2460
2461 /* Change was beyond the end of the buffer and not visible, do nothing */
2462 *scrolled = 0;
2463 }
2464
2465
2466
2467 /**
2468 \brief Update the line start arrays.
2469
2470 Scan through the text in the "textD"'s buffer and recalculate the line
2471 starts array values beginning at index "startLine" and continuing through
2472 (including) "endLine". It assumes that the line starts entry preceding
2473 "startLine" (or mFirstChar if startLine is 0) is good, and re-counts
2474 newlines to fill in the requested entries. Out of range values for
2475 "startLine" and "endLine" are acceptable.
2476
2477 \param startLine, endLine range of lines to scan as line numbers
2478 */
2479 void Fl_Text_Display_mod::calc_line_starts( int startLine, int endLine ) {
2480 int startPos, bufLen = mBuffer->length();
2481 int line, lineEnd, nextLineStart, nVis = mNVisibleLines;
2482 int *lineStarts = mLineStarts;
2483
2484 /* Clean up (possibly) messy input parameters */
2485 if ( endLine < 0 ) endLine = 0;
2486 if ( endLine >= nVis ) endLine = nVis - 1;
2487 if ( startLine < 0 ) startLine = 0;
2488 if ( startLine >= nVis ) startLine = nVis - 1;
2489 if ( startLine > endLine )
2490 return;
2491
2492 /* Find the last known good line number -> position mapping */
2493 if ( startLine == 0 ) {
2494 lineStarts[ 0 ] = mFirstChar;
2495 startLine = 1;
2496 }
2497 startPos = lineStarts[ startLine - 1 ];
2498
2499 /* If the starting position is already past the end of the text,
2500 fill in -1's (means no text on line) and return */
2501 if ( startPos == -1 ) {
2502 for ( line = startLine; line <= endLine; line++ )
2503 lineStarts[ line ] = -1;
2504 return;
2505 }
2506
2507 /* Loop searching for ends of lines and storing the positions of the
2508 start of the next line in lineStarts */
2509 for ( line = startLine; line <= endLine; line++ ) {
2510 find_line_end(startPos, true, &lineEnd, &nextLineStart);
2511 startPos = nextLineStart;
2512 if ( startPos >= bufLen ) {
2513 /* If the buffer ends with a newline or line break, put
2514 buf->length() in the next line start position (instead of
2515 a -1 which is the normal marker for an empty line) to
2516 indicate that the cursor may safely be displayed there */
2517 if ( line == 0 || ( lineStarts[ line - 1 ] != bufLen &&
2518 lineEnd != nextLineStart ) ) {
2519 lineStarts[ line ] = bufLen;
2520 line++;
2521 }
2522 break;
2523 }
2524 lineStarts[ line ] = startPos;
2525 }
2526
2527 /* Set any entries beyond the end of the text to -1 */
2528 for ( ; line <= endLine; line++ )
2529 lineStarts[ line ] = -1;
2530 }
2531
2532
2533
2534 /**
2535 \brief Update last display character index.
2536
2537 Given a Fl_Text_Display_mod with a complete, up-to-date lineStarts array, update
2538 the lastChar entry to point to the last buffer position displayed.
2539 */
2540 void Fl_Text_Display_mod::calc_last_char() {
2541 int i;
2542 for (i = mNVisibleLines - 1; i >= 0 && mLineStarts[i] == -1; i--) ;
2543 mLastChar = i < 0 ? 0 : line_end(mLineStarts[i], true);
2544 }
2545
2546
2547
2548 /**
2549 \brief Scrolls the current buffer to start at the specified line and column.
2550 \param topLineNum top line number
2551 \param horizOffset column number
2552 \todo Column numbers make little sense here.
2553 */
2554 void Fl_Text_Display_mod::scroll(int topLineNum, int horizOffset) {
2555 mTopLineNumHint = topLineNum;
2556 mHorizOffsetHint = horizOffset;
2557 resize(x(), y(), w(), h());
2558 }
2559
2560
2561
2562 /**
2563 \brief Scrolls the current buffer to start at the specified line and column.
2564 \param topLineNum top line number
2565 \param horizOffset in pixels
2566 \return 0 if nothing changed, 1 if we scrolled
2567 */
2568 int Fl_Text_Display_mod::scroll_(int topLineNum, int horizOffset) {
2569 /* Limit the requested scroll position to allowable values */
2570 if (topLineNum > mNBufferLines + 3 - mNVisibleLines)
2571 topLineNum = mNBufferLines + 3 - mNVisibleLines;
2572 if (topLineNum < 1) topLineNum = 1;
2573
2574 if (horizOffset > longest_vline() - text_area.w)
2575 horizOffset = longest_vline() - text_area.w;
2576 if (horizOffset < 0) horizOffset = 0;
2577
2578 /* Do nothing if scroll position hasn't actually changed or there's no
2579 window to draw in yet */
2580 if (mHorizOffset == horizOffset && mTopLineNum == topLineNum)
2581 return 0;
2582
2583 /* If the vertical scroll position has changed, update the line
2584 starts array and related counters in the text display */
2585 offset_line_starts(topLineNum);
2586
2587 /* Just setting mHorizOffset is enough information for redisplay */
2588 mHorizOffset = horizOffset;
2589
2590 // redraw all text
2591 damage(FL_DAMAGE_EXPOSE);
2592 return 1;
2593 }
2594
2595
2596
2597 /**
2598 \brief Update vertical scrollbar.
2599
2600 Update the minimum, maximum, slider size, page increment, and value
2601 for vertical scrollbar.
2602 */
2603 void Fl_Text_Display_mod::update_v_scrollbar() {
2604 /* The vertical scrollbar value and slider size directly represent the top
2605 line number, and the number of visible lines respectively. The scroll
2606 bar maximum value is chosen to generally represent the size of the whole
2607 buffer, with minor adjustments to keep the scrollbar widget happy */
2608 #ifdef DEBUG
2609 printf("Fl_Text_Display_mod::update_v_scrollbar():\n"
2610 " mTopLineNum=%d, mNVisibleLines=%d, mNBufferLines=%d\n",
2611 mTopLineNum, mNVisibleLines, mNBufferLines);
2612 #endif // DEBUG
2613
2614 mVScrollBar->value(mTopLineNum, mNVisibleLines, 1, mNBufferLines + 2);
2615 mVScrollBar->linesize(3);
2616 }
2617
2618
2619 /**
2620 \brief Update vertical scrollbar.
2621
2622 Update the minimum, maximum, slider size, page increment, and value
2623 for the horizontal scrollbar.
2624 */
2625 void Fl_Text_Display_mod::update_h_scrollbar() {
2626 int sliderMax = max(longest_vline(), text_area.w + mHorizOffset);
2627 mHScrollBar->value( mHorizOffset, text_area.w, 0, sliderMax );
2628 }
2629
2630
2631
2632 /**
2633 \brief Callbacks for drag or valueChanged on scrollbars.
2634 */
2635 void Fl_Text_Display_mod::v_scrollbar_cb(Fl_Scrollbar* b, Fl_Text_Display_mod* textD) {
2636 if (b->value() == textD->mTopLineNum) return;
2637 textD->scroll(b->value(), textD->mHorizOffset);
2638 }
2639
2640
2641
2642 /**
2643 \brief Callbacks for drag or valueChanged on scrollbars.
2644 */
2645 void Fl_Text_Display_mod::h_scrollbar_cb(Fl_Scrollbar* b, Fl_Text_Display_mod* textD) {
2646 if (b->value() == textD->mHorizOffset) return;
2647 textD->scroll(textD->mTopLineNum, b->value());
2648 }
2649
2650
2651
2652 /**
2653 \brief Refresh the line number area.
2654
2655 If clearAll is False, writes only over the character cell areas. Setting
2656 clearAll to True will clear out any stray marks outside of the character cell
2657 area, which might have been left from before a resize or font change.
2658
2659 This function is not used.
2660 */
2661 void Fl_Text_Display_mod::draw_line_numbers(bool /*clearAll*/) {
2662 #if 0
2663 int y, line, visLine, nCols, lineStart;
2664 char lineNumString[12];
2665 int lineHeight = mMaxsize ? mMaxsize : textsize_;
2666 int charWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width;
2667
2668 /* Don't draw if mLineNumWidth == 0 (line numbers are hidden), or widget is
2669 not yet realized */
2670 if (mLineNumWidth == 0 || visible_r())
2671 return;
2672
2673 /* GC is allocated on demand, since not everyone will use line numbering */
2674 if (textD->lineNumGC == NULL) {
2675 XGCValues values;
2676 values.foreground = textD->lineNumFGPixel;
2677 values.background = textD->bgPixel;
2678 values.font = textD->fontStruct->fid;
2679 textD->lineNumGC = XtGetGC(textD->w,
2680 GCFont| GCForeground | GCBackground, &values);
2681 }
2682
2683 /* Erase the previous contents of the line number area, if requested */
2684 if (clearAll)
2685 XClearArea(XtDisplay(textD->w), XtWindow(textD->w), textD->lineNumLeft,
2686 textD->top, textD->lineNumWidth, textD->height, False);
2687
2688 /* Draw the line numbers, aligned to the text */
2689 nCols = min(11, textD->lineNumWidth / charWidth);
2690 y = textD->top;
2691 line = getAbsTopLineNum(textD);
2692 for (visLine=0; visLine < textD->nVisibleLines; visLine++) {
2693 lineStart = textD->lineStarts[visLine];
2694 if (lineStart != -1 && (lineStart==0 ||
2695 BufGetCharacter(textD->buffer, lineStart-1)=='\n')) {
2696 sprintf(lineNumString, "%*d", nCols, line);
2697 XDrawImageString(XtDisplay(textD->w), XtWindow(textD->w),
2698 textD->lineNumGC, textD->lineNumLeft, y + textD->ascent,
2699 lineNumString, strlen(lineNumString));
2700 line++;
2701 } else {
2702 XClearArea(XtDisplay(textD->w), XtWindow(textD->w),
2703 textD->lineNumLeft, y, textD->lineNumWidth,
2704 textD->ascent + textD->descent, False);
2705 if (visLine == 0)
2706 line++;
2707 }
2708 y += lineHeight;
2709 }
2710 #endif
2711 }
2712
2713 static int max( int i1, int i2 ) {
2714 return i1 >= i2 ? i1 : i2;
2715 }
2716
2717 static int min( int i1, int i2 ) {
2718 return i1 <= i2 ? i1 : i2;
2719 }
2720
2721
2722
2723 /**
2724 Count the number of newlines in a null-terminated text string;
2725 */
2726 static int countlines( const char *string ) {
2727 IS_UTF8_ALIGNED(string)
2728
2729 const char * c;
2730 int lineCount = 0;
2731
2732 if (!string) return 0;
2733
2734 for ( c = string; *c != '\0'; c++ )
2735 if ( *c == '\n' ) lineCount++;
2736 return lineCount;
2737 }
2738
2739
2740
2741
2742 /**
2743 \brief Returns the width in pixels of the displayed line pointed to by "visLineNum".
2744 \param visLineNum index into visible lines array
2745 \return width of line in pixels
2746 */
2747 int Fl_Text_Display_mod::measure_vline( int visLineNum ) const {
2748 int lineLen = vline_length( visLineNum );
2749 int lineStartPos = mLineStarts[ visLineNum ];
2750 if (lineStartPos < 0 || lineLen == 0) return 0;
2751 return handle_vline(GET_WIDTH, lineStartPos, lineLen, 0, 0, 0, 0, 0, 0);
2752 }
2753
2754
2755
2756 /**
2757 \brief Return true if there are lines visible with no corresponding buffer text.
2758 \return 1 if there are empty lines
2759 */
2760 int Fl_Text_Display_mod::empty_vlines() const {
2761 return (mNVisibleLines > 0) && (mLineStarts[ mNVisibleLines - 1 ] == -1);
2762 }
2763
2764
2765 /**
2766 \brief Count number of bytes in a visible line.
2767
2768 Return the length of a line (number of bytes) by examining
2769 entries in the line starts array rather than by scanning for newlines.
2770
2771 \param visLineNum index of line in visible line array
2772 \return number of bytes in this line
2773 */
2774 int Fl_Text_Display_mod::vline_length( int visLineNum ) const {
2775 int nextLineStart, lineStartPos;
2776
2777 if (visLineNum < 0 || visLineNum >= mNVisibleLines)
2778 return (0);
2779
2780 lineStartPos = mLineStarts[ visLineNum ];
2781
2782 if ( lineStartPos == -1 )
2783 return 0;
2784
2785 if ( visLineNum + 1 >= mNVisibleLines )
2786 return mLastChar - lineStartPos;
2787
2788 nextLineStart = mLineStarts[ visLineNum + 1 ];
2789 if ( nextLineStart == -1 )
2790 return mLastChar - lineStartPos;
2791
2792 int nextLineStartMinus1 = buffer()->prev_char(nextLineStart);
2793 if (wrap_uses_character(nextLineStartMinus1))
2794 return nextLineStartMinus1 - lineStartPos;
2795
2796 return nextLineStart - lineStartPos;
2797 }
2798
2799
2800
2801 /**
2802 \brief Wrapping calculations.
2803
2804 When continuous wrap is on, and the user inserts or deletes characters,
2805 wrapping can happen before and beyond the changed position. This routine
2806 finds the extent of the changes, and counts the deleted and inserted lines
2807 over that range. It also attempts to minimize the size of the range to
2808 what has to be counted and re-displayed, so the results can be useful
2809 both for delimiting where the line starts need to be recalculated, and
2810 for deciding what part of the text to redisplay.
2811
2812 \param deletedText
2813 \param pos
2814 \param nInserted
2815 \param nDeleted
2816 \param modRangeStart
2817 \param modRangeEnd
2818 \param linesInserted
2819 \param linesDeleted
2820 */
2821 void Fl_Text_Display_mod::find_wrap_range(const char *deletedText, int pos,
2822 int nInserted, int nDeleted,
2823 int *modRangeStart, int *modRangeEnd,
2824 int *linesInserted, int *linesDeleted) {
2825 IS_UTF8_ALIGNED(deletedText)
2826 IS_UTF8_ALIGNED2(buffer(), pos)
2827
2828 int length, retPos, retLines, retLineStart, retLineEnd;
2829 Fl_Text_Buffer_mod *deletedTextBuf, *buf = buffer();
2830 int nVisLines = mNVisibleLines;
2831 int *lineStarts = mLineStarts;
2832 int countFrom, countTo, lineStart, adjLineStart, i;
2833 int visLineNum = 0, nLines = 0;
2834
2835 /*
2836 ** Determine where to begin searching: either the previous newline, or
2837 ** if possible, limit to the start of the (original) previous displayed
2838 ** line, using information from the existing line starts array
2839 */
2840 if (pos >= mFirstChar && pos <= mLastChar) {
2841 for (i=nVisLines-1; i>0; i--) {
2842 if (lineStarts[i] != -1 && pos >= lineStarts[i]) {
2843 break;
2844 }
2845 }
2846 if (i > 0) {
2847 countFrom = lineStarts[i-1];
2848 visLineNum = i-1;
2849 } else {
2850 countFrom = buf->line_start(pos);
2851 }
2852 } else {
2853 countFrom = buf->line_start(pos);
2854 }
2855
2856 IS_UTF8_ALIGNED2(buf, countFrom)
2857
2858 /*
2859 ** Move forward through the (new) text one line at a time, counting
2860 ** displayed lines, and looking for either a real newline, or for the
2861 ** line starts to re-sync with the original line starts array
2862 */
2863 lineStart = countFrom;
2864 *modRangeStart = countFrom;
2865 for (;;) {
2866
2867 /* advance to the next line. If the line ended in a real newline
2868 or the end of the buffer, that's far enough */
2869 wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0,
2870 &retPos, &retLines, &retLineStart, &retLineEnd);
2871 if (retPos >= buf->length()) {
2872 countTo = buf->length();
2873 *modRangeEnd = countTo;
2874 if (retPos != retLineEnd)
2875 nLines++;
2876 break;
2877 } else {
2878 lineStart = retPos;
2879 }
2880 nLines++;
2881 if (lineStart > pos + nInserted && buf->char_at(buf->prev_char(lineStart)) == '\n') {
2882 countTo = lineStart;
2883 *modRangeEnd = lineStart;
2884 break;
2885 }
2886
2887 /* Don't try to resync in continuous wrap mode with non-fixed font
2888 sizes; it would result in a chicken-and-egg dependency between
2889 the calculations for the inserted and the deleted lines.
2890 If we're in that mode, the number of deleted lines is calculated in
2891 advance, without resynchronization, so we shouldn't resynchronize
2892 for the inserted lines either. */
2893 if (mSuppressResync)
2894 continue;
2895
2896 /* check for synchronization with the original line starts array
2897 before pos, if so, the modified range can begin later */
2898 if (lineStart <= pos) {
2899 while (visLineNum<nVisLines && lineStarts[visLineNum] < lineStart)
2900 visLineNum++;
2901 if (visLineNum < nVisLines && lineStarts[visLineNum] == lineStart) {
2902 countFrom = lineStart;
2903 nLines = 0;
2904 if (visLineNum+1 < nVisLines && lineStarts[visLineNum+1] != -1)
2905 *modRangeStart = min(pos, buf->prev_char(lineStarts[visLineNum+1]));
2906 else
2907 *modRangeStart = countFrom;
2908 } else
2909 *modRangeStart = min(*modRangeStart, buf->prev_char(lineStart));
2910 }
2911
2912 /* check for synchronization with the original line starts array
2913 after pos, if so, the modified range can end early */
2914 else if (lineStart > pos + nInserted) {
2915 adjLineStart = lineStart - nInserted + nDeleted;
2916 while (visLineNum<nVisLines && lineStarts[visLineNum]<adjLineStart)
2917 visLineNum++;
2918 if (visLineNum < nVisLines && lineStarts[visLineNum] != -1 &&
2919 lineStarts[visLineNum] == adjLineStart) {
2920 countTo = line_end(lineStart, true);
2921 *modRangeEnd = lineStart;
2922 break;
2923 }
2924 }
2925 }
2926 *linesInserted = nLines;
2927
2928
2929 /* Count deleted lines between countFrom and countTo as the text existed
2930 before the modification (that is, as if the text between pos and
2931 pos+nInserted were replaced by "deletedText"). This extra context is
2932 necessary because wrapping can occur outside of the modified region
2933 as a result of adding or deleting text in the region. This is done by
2934 creating a textBuffer containing the deleted text and the necessary
2935 additional context, and calling the wrappedLineCounter on it.
2936
2937 NOTE: This must not be done in continuous wrap mode when the font
2938 width is not fixed. In that case, the calculation would try
2939 to access style information that is no longer available (deleted
2940 text), or out of date (updated highlighting), possibly leading
2941 to completely wrong calculations and/or even crashes eventually.
2942 (This is not theoretical; it really happened.)
2943
2944 In that case, the calculation of the number of deleted lines
2945 has happened before the buffer was modified (only in that case,
2946 because resynchronization of the line starts is impossible
2947 in that case, which makes the whole calculation less efficient).
2948 */
2949 if (mSuppressResync) {
2950 *linesDeleted = mNLinesDeleted;
2951 mSuppressResync = 0;
2952 return;
2953 }
2954
2955 length = (pos-countFrom) + nDeleted +(countTo-(pos+nInserted));
2956 deletedTextBuf = new Fl_Text_Buffer_mod(length);
2957 deletedTextBuf->copy(buffer(), countFrom, pos, 0);
2958 if (nDeleted != 0)
2959 deletedTextBuf->insert(pos-countFrom, deletedText);
2960 deletedTextBuf->copy(buffer(), pos+nInserted, countTo, pos-countFrom+nDeleted);
2961 /* Note that we need to take into account an offset for the style buffer:
2962 the deletedTextBuf can be out of sync with the style buffer. */
2963 wrapped_line_counter(deletedTextBuf, 0, length, INT_MAX, true, countFrom,
2964 &retPos, &retLines, &retLineStart, &retLineEnd, false);
2965 delete deletedTextBuf;
2966 *linesDeleted = retLines;
2967 mSuppressResync = 0;
2968 }
2969
2970
2971
2972 /**
2973 \brief Wrapping calculations.
2974
2975 This is a stripped-down version of the findWrapRange() function above,
2976 intended to be used to calculate the number of "deleted" lines during
2977 a buffer modification. It is called _before_ the modification takes place.
2978
2979 This function should only be called in continuous wrap mode with a
2980 non-fixed font width. In that case, it is impossible to calculate
2981 the number of deleted lines, because the necessary style information
2982 is no longer available _after_ the modification. In other cases, we
2983 can still perform the calculation afterwards (possibly even more
2984 efficiently).
2985
2986 \param pos
2987 \param nDeleted
2988 */
2989 void Fl_Text_Display_mod::measure_deleted_lines(int pos, int nDeleted) {
2990 IS_UTF8_ALIGNED2(buffer(), pos)
2991
2992 int retPos, retLines, retLineStart, retLineEnd;
2993 Fl_Text_Buffer_mod *buf = buffer();
2994 int nVisLines = mNVisibleLines;
2995 int *lineStarts = mLineStarts;
2996 int countFrom, lineStart;
2997 int nLines = 0, i;
2998 /*
2999 ** Determine where to begin searching: either the previous newline, or
3000 ** if possible, limit to the start of the (original) previous displayed
3001 ** line, using information from the existing line starts array
3002 */
3003 if (pos >= mFirstChar && pos <= mLastChar) {
3004 for (i=nVisLines-1; i>0; i--)
3005 if (lineStarts[i] != -1 && pos >= lineStarts[i])
3006 break;
3007 if (i > 0) {
3008 countFrom = lineStarts[i-1];
3009 } else
3010 countFrom = buf->line_start(pos);
3011 } else
3012 countFrom = buf->line_start(pos);
3013
3014 /*
3015 ** Move forward through the (new) text one line at a time, counting
3016 ** displayed lines, and looking for either a real newline, or for the
3017 ** line starts to re-sync with the original line starts array
3018 */
3019 lineStart = countFrom;
3020 for (;;) {
3021 /* advance to the next line. If the line ended in a real newline
3022 or the end of the buffer, that's far enough */
3023 wrapped_line_counter(buf, lineStart, buf->length(), 1, true, 0,
3024 &retPos, &retLines, &retLineStart, &retLineEnd);
3025 if (retPos >= buf->length()) {
3026 if (retPos != retLineEnd)
3027 nLines++;
3028 break;
3029 } else
3030 lineStart = retPos;
3031 nLines++;
3032 if (lineStart > pos + nDeleted && buf->char_at(lineStart-1) == '\n') {
3033 break;
3034 }
3035
3036 /* Unlike in the findWrapRange() function above, we don't try to
3037 resync with the line starts, because we don't know the length
3038 of the inserted text yet, nor the updated style information.
3039
3040 Because of that, we also shouldn't resync with the line starts
3041 after the modification either, because we must perform the
3042 calculations for the deleted and inserted lines in the same way.
3043
3044 This can result in some unnecessary recalculation and redrawing
3045 overhead, and therefore we should only use this two-phase mode
3046 of calculation when it's really needed (continuous wrap + variable
3047 font width). */
3048 }
3049 mNLinesDeleted = nLines;
3050 mSuppressResync = 1;
3051 }
3052
3053
3054
3055 /**
3056 \brief Wrapping calculations.
3057
3058 Count forward from startPos to either maxPos or maxLines (whichever is
3059 reached first), and return all relevant positions and line count.
3060 The provided textBuffer may differ from the actual text buffer of the
3061 widget. In that case it must be a (partial) copy of the actual text buffer
3062 and the styleBufOffset argument must indicate the starting position of the
3063 copy, to take into account the correct style information.
3064
3065 \param buf
3066 \param startPos
3067 \param maxPos
3068 \param maxLines
3069 \param startPosIsLineStart
3070 \param styleBufOffset
3071
3072 \param[out] retPos Position where counting ended. When counting lines, the
3073 position returned is the start of the line "maxLines" lines
3074 beyond "startPos".
3075 \param[out] retLines Number of line breaks counted
3076 \param[out] retLineStart Start of the line where counting ended
3077 \param[out] retLineEnd End position of the last line traversed
3078 \param[out] countLastLineMissingNewLine
3079 */
3080 void Fl_Text_Display_mod::wrapped_line_counter(Fl_Text_Buffer_mod *buf, int startPos,
3081 int maxPos, int maxLines, bool startPosIsLineStart, int styleBufOffset,
3082 int *retPos, int *retLines, int *retLineStart, int *retLineEnd,
3083 bool countLastLineMissingNewLine) const {
3084 IS_UTF8_ALIGNED2(buf, startPos)
3085 IS_UTF8_ALIGNED2(buf, maxPos)
3086
3087 int lineStart, newLineStart = 0, b, p, colNum, wrapMarginPix;
3088 int i, foundBreak;
3089 double width;
3090 int nLines = 0;
3091 unsigned int c;
3092
3093 /* Set the wrap margin to the wrap column or the view width */
3094 if (mWrapMarginPix != 0) {
3095 wrapMarginPix = mWrapMarginPix;
3096 } else {
3097 wrapMarginPix = text_area.w;
3098 }
3099
3100 /* Find the start of the line if the start pos is not marked as a
3101 line start. */
3102 if (startPosIsLineStart)
3103 lineStart = startPos;
3104 else
3105 lineStart = line_start(startPos);
3106
3107 /*
3108 ** Loop until position exceeds maxPos or line count exceeds maxLines.
3109 ** (actually, continues beyond maxPos to end of line containing maxPos,
3110 ** in case later characters cause a word wrap back before maxPos)
3111 */
3112 colNum = 0;
3113 width = 0;
3114 for (p=lineStart; p<buf->length(); p=buf->next_char(p)) {
3115 c = buf->char_at(p); // UCS-4
3116
3117 /* If the character was a newline, count the line and start over,
3118 otherwise, add it to the width and column counts */
3119 if (c == '\n') {
3120 if (p >= maxPos) {
3121 *retPos = maxPos;
3122 *retLines = nLines;
3123 *retLineStart = lineStart;
3124 *retLineEnd = maxPos;
3125 return;
3126 }
3127 nLines++;
3128 int p1 = buf->next_char(p);
3129 if (nLines >= maxLines) {
3130 *retPos = p1;
3131 *retLines = nLines;
3132 *retLineStart = p1;
3133 *retLineEnd = p;
3134 return;
3135 }
3136 lineStart = p1;
3137 colNum = 0;
3138 width = 0;
3139 } else {
3140 const char *s = buf->address(p);
3141 colNum++;
3142 // FIXME: it is not a good idea to simply add character widths because on
3143 // some platforms, the width is a floating point value and depends on the
3144 // previous character as well.
3145 width += measure_proportional_character(s, (int)width, p+styleBufOffset);
3146 }
3147
3148 /* If character exceeded wrap margin, find the break point and wrap there */
3149 if (width > wrapMarginPix) {
3150 foundBreak = false;
3151 for (b=p; b>=lineStart; b=buf->prev_char(b)) {
3152 c = buf->char_at(b);
3153 if (c == '\t' || c == ' ') {
3154 newLineStart = buf->next_char(b);
3155 colNum = 0;
3156 width = 0;
3157 int iMax = buf->next_char(p);
3158 for (i=buf->next_char(b); i<iMax; i = buf->next_char(i)) {
3159 width += measure_proportional_character(buf->address(i), (int)width,
3160 i+styleBufOffset);
3161 colNum++;
3162 }
3163 foundBreak = true;
3164 break;
3165 }
3166 }
3167 if (!foundBreak) { /* no whitespace, just break at margin */
3168 newLineStart = max(p, buf->next_char(lineStart));
3169 const char *s = buf->address(b);
3170 colNum++;
3171 width = measure_proportional_character(s, 0, p+styleBufOffset);
3172 }
3173 if (p >= maxPos) {
3174 *retPos = maxPos;
3175 *retLines = maxPos < newLineStart ? nLines : nLines + 1;
3176 *retLineStart = maxPos < newLineStart ? lineStart : newLineStart;
3177 *retLineEnd = maxPos;
3178 return;
3179 }
3180 nLines++;
3181 if (nLines >= maxLines) {
3182 *retPos = foundBreak ? buf->next_char(b) : max(p, buf->next_char(lineStart));
3183 *retLines = nLines;
3184 *retLineStart = lineStart;
3185 *retLineEnd = foundBreak ? b : p;
3186 return;
3187 }
3188 lineStart = newLineStart;
3189 }
3190 }
3191
3192 /* reached end of buffer before reaching pos or line target */
3193 *retPos = buf->length();
3194 *retLines = nLines;
3195 if (countLastLineMissingNewLine && colNum > 0)
3196 *retLines = buf->next_char(*retLines);
3197 *retLineStart = lineStart;
3198 *retLineEnd = buf->length();
3199 }
3200
3201
3202
3203 /**
3204 \brief Wrapping calculations.
3205
3206 Measure the width in pixels of the first character of string "s" at a
3207 particular column "colNum" and buffer position "pos". This is for measuring
3208 characters in proportional or mixed-width highlighting fonts.
3209
3210 A note about proportional and mixed-width fonts: the mixed width and
3211 proportional font code in nedit does not get much use in general editing,
3212 because nedit doesn't allow per-language-mode fonts, and editing programs
3213 in a proportional font is usually a bad idea, so very few users would
3214 choose a proportional font as a default. There are still probably mixed-
3215 width syntax highlighting cases where things don't redraw properly for
3216 insertion/deletion, though static display and wrapping and resizing
3217 should now be solid because they are now used for online help display.
3218
3219 \param s text string
3220 \param xPix x pixel position needed for calculating tab widths
3221 \param pos offset within string
3222 \return width of character in pixels
3223 */
3224 double Fl_Text_Display_mod::measure_proportional_character(const char *s, int xPix, int pos) const {
3225 IS_UTF8_ALIGNED(s)
3226
3227 if (*s=='\t') {
3228 int tab = (int)col_to_x(mBuffer->tab_distance());
3229 return (((xPix/tab)+1)*tab) - xPix;
3230 }
3231
3232 int charLen = fl_utf8len1(*s), style = 0;
3233 if (mStyleBuffer) {
3234 style = mStyleBuffer->byte_at(pos);
3235 }
3236 return string_width(s, charLen, style);
3237 }
3238
3239
3240
3241 /**
3242 \brief Finds both the end of the current line and the start of the next line.
3243
3244 Why?
3245 In continuous wrap mode, if you need to know both, figuring out one from the
3246 other can be expensive or error prone. The problem comes when there's a
3247 trailing space or tab just before the end of the buffer. To translate an
3248 end of line value to or from the next lines start value, you need to know
3249 whether the trailing space or tab is being used as a line break or just a
3250 normal character, and to find that out would otherwise require counting all
3251 the way back to the beginning of the line.
3252
3253 \param startPos
3254 \param startPosIsLineStart
3255 \param[out] lineEnd
3256 \param[out] nextLineStart
3257 */
3258 void Fl_Text_Display_mod::find_line_end(int startPos, bool startPosIsLineStart,
3259 int *lineEnd, int *nextLineStart) const {
3260 IS_UTF8_ALIGNED2(buffer(), startPos)
3261
3262 int retLines, retLineStart;
3263
3264 /* if we're not wrapping use more efficient BufEndOfLine */
3265 if (mFastDisplay || !mContinuousWrap) {
3266 int le = buffer()->line_end(startPos);
3267 int ls = buffer()->next_char(le);
3268 *lineEnd = le;
3269 *nextLineStart = min(buffer()->length(), ls);
3270 return;
3271 }
3272
3273 /* use the wrapped line counter routine to count forward one line */
3274 wrapped_line_counter(buffer(), startPos, buffer()->length(),
3275 1, startPosIsLineStart, 0, nextLineStart, &retLines,
3276 &retLineStart, lineEnd);
3277 }
3278
3279
3280
3281 /**
3282 \brief Check if the line break is caused by a \\n or by line wrapping.
3283
3284 Line breaks in continuous wrap mode usually happen at newlines or
3285 whitespace. This line-terminating character is not included in line
3286 width measurements and has a special status as a non-visible character.
3287 However, lines with no whitespace are wrapped without the benefit of a
3288 line terminating character, and this distinction causes endless trouble
3289 with all of the text display code which was originally written without
3290 continuous wrap mode and always expects to wrap at a newline character.
3291
3292 Given the position of the end of the line, as returned by TextDEndOfLine
3293 or BufEndOfLine, this returns true if there is a line terminating
3294 character, and false if there's not. On the last character in the
3295 buffer, this function can't tell for certain whether a trailing space was
3296 used as a wrap point, and just guesses that it wasn't. So if an exact
3297 accounting is necessary, don't use this function.
3298
3299 \param lineEndPos index of character where the line wraps
3300 \return 1 if a \\n character causes the line wrap
3301 */
3302 int Fl_Text_Display_mod::wrap_uses_character(int lineEndPos) const {
3303 IS_UTF8_ALIGNED2(buffer(), lineEndPos)
3304
3305 unsigned int c;
3306
3307 if (mFastDisplay || !mContinuousWrap || lineEndPos == buffer()->length())
3308 return 1;
3309
3310 c = buffer()->char_at(lineEndPos);
3311 return c == '\n' || ((c == '\t' || c == ' ') &&
3312 lineEndPos + 1 < buffer()->length());
3313 }
3314
3315 /**
3316 \brief I don't know what this does!
3317
3318 Extend the range of a redraw request (from *start to *end) with additional
3319 redraw requests resulting from changes to the attached style buffer (which
3320 contains auxiliary information for coloring or styling text).
3321
3322 \param startpos ??
3323 \param endpos ??
3324
3325 \todo Unicode?
3326 */
3327 void Fl_Text_Display_mod::extend_range_for_styles( int *startpos, int *endpos ) {
3328 IS_UTF8_ALIGNED2(buffer(), (*startpos))
3329 IS_UTF8_ALIGNED2(buffer(), (*endpos))
3330
3331 Fl_Text_Selection_mod * sel = mStyleBuffer->primary_selection();
3332 int extended = 0;
3333
3334 /* The peculiar protocol used here is that modifications to the style
3335 buffer are marked by selecting them with the buffer's primary Fl_Text_Selection.
3336 The style buffer is usually modified in response to a modify callback on
3337 the text buffer BEFORE Fl_Text_Display_mod.c's modify callback, so that it can keep
3338 the style buffer in step with the text buffer. The style-update
3339 callback can't just call for a redraw, because Fl_Text_Display_mod hasn't processed
3340 the original text changes yet. Anyhow, to minimize redrawing and to
3341 avoid the complexity of scheduling redraws later, this simple protocol
3342 tells the text display's buffer modify callback to extend its redraw
3343 range to show the text color/and font changes as well. */
3344 if ( sel->selected() ) {
3345 if ( sel->start() < *startpos ) {
3346 *startpos = sel->start();
3347 // somewhere while deleting, alignment is lost. We do this just to be sure.
3348 *startpos = buffer()->utf8_align(*startpos);
3349 IS_UTF8_ALIGNED2(buffer(), (*startpos))
3350 extended = 1;
3351 }
3352 if ( sel->end() > *endpos ) {
3353 *endpos = sel->end();
3354 *endpos = buffer()->utf8_align(*endpos);
3355 IS_UTF8_ALIGNED2(buffer(), (*endpos))
3356 extended = 1;
3357 }
3358 }
3359
3360 /* If the Fl_Text_Selection was extended due to a style change, and some of the
3361 fonts don't match in spacing, extend redraw area to end of line to
3362 redraw characters exposed by possible font size changes */
3363 if ( extended )
3364 *endpos = mBuffer->line_end( *endpos ) + 1;
3365
3366 IS_UTF8_ALIGNED2(buffer(), (*endpos))
3367 }
3368
3369
3370
3371 /**
3372 \brief Draw the widget.
3373
3374 This function tries to limit drawing to smaller areas if possible.
3375 */
3376 void Fl_Text_Display_mod::draw(void) {
3377 // don't even try if there is no associated text buffer!
3378 if (!buffer()) { draw_box(); return; }
3379
3380 fl_push_clip(x(),y(),w(),h()); // prevent drawing outside widget area
3381
3382 // draw the non-text, non-scrollbar areas.
3383 if (damage() & FL_DAMAGE_ALL) {
3384 // draw the box()
3385 int W = w(), H = h();
3386 draw_box(box(), x(), y(), W, H, color());
3387
3388 if (mHScrollBar->visible())
3389 W -= scrollbar_width();
3390 if (mVScrollBar->visible())
3391 H -= scrollbar_width();
3392
3393 // left margin
3394 fl_rectf(text_area.x-LEFT_MARGIN, text_area.y-TOP_MARGIN,
3395 LEFT_MARGIN, text_area.h+TOP_MARGIN+BOTTOM_MARGIN,
3396 color());
3397
3398 // right margin
3399 fl_rectf(text_area.x+text_area.w, text_area.y-TOP_MARGIN,
3400 RIGHT_MARGIN, text_area.h+TOP_MARGIN+BOTTOM_MARGIN,
3401 color());
3402
3403 // top margin
3404 fl_rectf(text_area.x, text_area.y-TOP_MARGIN,
3405 text_area.w, TOP_MARGIN, color());
3406
3407 // bottom margin
3408 fl_rectf(text_area.x, text_area.y+text_area.h,
3409 text_area.w, BOTTOM_MARGIN, color());
3410
3411 // draw that little box in the corner of the scrollbars
3412 if (mVScrollBar->visible() && mHScrollBar->visible())
3413 fl_rectf(mVScrollBar->x(), mHScrollBar->y(),
3414 mVScrollBar->w(), mHScrollBar->h(),
3415 FL_GRAY);
3416
3417 // blank the previous cursor protrusions
3418 }
3419 else if (damage() & (FL_DAMAGE_SCROLL | FL_DAMAGE_EXPOSE)) {
3420 // printf("blanking previous cursor extrusions at Y: %d\n", mCursorOldY);
3421 // CET - FIXME - save old cursor position instead and just draw side needed?
3422 fl_push_clip(text_area.x-LEFT_MARGIN,
3423 text_area.y,
3424 text_area.w+LEFT_MARGIN+RIGHT_MARGIN,
3425 text_area.h);
3426 fl_rectf(text_area.x-LEFT_MARGIN, mCursorOldY,
3427 LEFT_MARGIN, mMaxsize, color());
3428 fl_rectf(text_area.x+text_area.w, mCursorOldY,
3429 RIGHT_MARGIN, mMaxsize, color());
3430 fl_pop_clip();
3431 }
3432
3433 // draw the scrollbars
3434 if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_CHILD)) {
3435 mVScrollBar->damage(FL_DAMAGE_ALL);
3436 mHScrollBar->damage(FL_DAMAGE_ALL);
3437 }
3438 update_child(*mVScrollBar);
3439 update_child(*mHScrollBar);
3440
3441 // draw all of the text
3442 if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_EXPOSE)) {
3443 //printf("drawing all text\n");
3444 int X, Y, W, H;
3445 if (fl_clip_box(text_area.x, text_area.y, text_area.w, text_area.h,
3446 X, Y, W, H)) {
3447 // Draw text using the intersected clipping box...
3448 // (this sets the clipping internally)
3449 draw_text(X, Y, W, H);
3450 } else {
3451 // Draw the whole area...
3452 draw_text(text_area.x, text_area.y, text_area.w, text_area.h);
3453 }
3454 }
3455 else if (damage() & FL_DAMAGE_SCROLL) {
3456 // draw some lines of text
3457 fl_push_clip(text_area.x, text_area.y,
3458 text_area.w, text_area.h);
3459 //printf("drawing text from %d to %d\n", damage_range1_start, damage_range1_end);
3460 draw_range(damage_range1_start, damage_range1_end);
3461 if (damage_range2_end != -1) {
3462 //printf("drawing text from %d to %d\n", damage_range2_start, damage_range2_end);
3463 draw_range(damage_range2_start, damage_range2_end);
3464 }
3465 damage_range1_start = damage_range1_end = -1;
3466 damage_range2_start = damage_range2_end = -1;
3467 fl_pop_clip();
3468 }
3469
3470 // draw the text cursor
3471 if (damage() & (FL_DAMAGE_ALL | FL_DAMAGE_SCROLL | FL_DAMAGE_EXPOSE)
3472 && !buffer()->primary_selection()->selected() &&
3473 mCursorOn && Fl::focus() == (Fl_Widget*)this ) {
3474 fl_push_clip(text_area.x-LEFT_MARGIN,
3475 text_area.y,
3476 text_area.w+LEFT_MARGIN+RIGHT_MARGIN,
3477 text_area.h);
3478
3479 int X, Y;
3480 if (position_to_xy(mCursorPos, &X, &Y)) draw_cursor(X, Y);
3481 // else puts("position_to_xy() failed - unable to draw cursor!");
3482 //printf("drew cursor at pos: %d (%d,%d)\n", mCursorPos, X, Y);
3483 mCursorOldY = Y;
3484 fl_pop_clip();
3485 }
3486 fl_pop_clip();
3487 }
3488
3489
3490
3491 // this processes drag events due to mouse for Fl_Text_Display_mod and
3492 // also drags due to cursor movement with shift held down for
3493 // Fl_Text_Editor_mod
3494 void fl_text_drag_me(int pos, Fl_Text_Display_mod* d) {
3495 if (d->dragType == Fl_Text_Display_mod::DRAG_CHAR) {
3496 if (pos >= d->dragPos) {
3497 d->buffer()->select(d->dragPos, pos);
3498 } else {
3499 d->buffer()->select(pos, d->dragPos);
3500 }
3501 d->insert_position(pos);
3502 } else if (d->dragType == Fl_Text_Display_mod::DRAG_WORD) {
3503 if (pos >= d->dragPos) {
3504 d->insert_position(d->word_end(pos));
3505 d->buffer()->select(d->word_start(d->dragPos), d->word_end(pos));
3506 } else {
3507 d->insert_position(d->word_start(pos));
3508 d->buffer()->select(d->word_start(pos), d->word_end(d->dragPos));
3509 }
3510 } else if (d->dragType == Fl_Text_Display_mod::DRAG_LINE) {
3511 if (pos >= d->dragPos) {
3512 d->insert_position(d->buffer()->line_end(pos)+1);
3513 d->buffer()->select(d->buffer()->line_start(d->dragPos),
3514 d->buffer()->line_end(pos)+1);
3515 } else {
3516 d->insert_position(d->buffer()->line_start(pos));
3517 d->buffer()->select(d->buffer()->line_start(pos),
3518 d->buffer()->line_end(d->dragPos)+1);
3519 }
3520 }
3521 }
3522
3523
3524
3525 /**
3526 \brief Timer callback for scroll events.
3527
3528 This timer event scrolls the text view proportionally to
3529 how far the mouse pointer has left the text area. This
3530 allows for smooth scrolling without "wiggeling" the mouse.
3531 */
3532 void Fl_Text_Display_mod::scroll_timer_cb(void *user_data) {
3533 Fl_Text_Display_mod *w = (Fl_Text_Display_mod*)user_data;
3534 int pos;
3535 switch (scroll_direction) {
3536 case 1: // mouse is to the right, scroll left
3537 w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount);
3538 pos = w->xy_to_position(w->text_area.x + w->text_area.w, scroll_y, CURSOR_POS);
3539 break;
3540 case 2: // mouse is to the left, scroll right
3541 w->scroll(w->mTopLineNum, w->mHorizOffset + scroll_amount);
3542 pos = w->xy_to_position(w->text_area.x, scroll_y, CURSOR_POS);
3543 break;
3544 case 3: // mouse is above, scroll down
3545 w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset);
3546 pos = w->xy_to_position(scroll_x, w->text_area.y, CURSOR_POS);
3547 break;
3548 case 4: // mouse is below, scroll up
3549 w->scroll(w->mTopLineNum + scroll_amount, w->mHorizOffset);
3550 pos = w->xy_to_position(scroll_x, w->text_area.y + w->text_area.h, CURSOR_POS);
3551 break;
3552 default:
3553 return;
3554 }
3555 fl_text_drag_me(pos, w);
3556 Fl::repeat_timeout(.1, scroll_timer_cb, user_data);
3557 }
3558
3559
3560
3561 /**
3562 \brief Event handling.
3563 */
3564 int Fl_Text_Display_mod::handle(int event) {
3565 if (!buffer()) return 0;
3566 // This isn't very elegant!
3567 if (!Fl::event_inside(text_area.x, text_area.y, text_area.w, text_area.h) &&
3568 !dragging && event != FL_LEAVE && event != FL_ENTER &&
3569 event != FL_MOVE && event != FL_FOCUS && event != FL_UNFOCUS &&
3570 event != FL_KEYBOARD && event != FL_KEYUP) {
3571 return Fl_Group::handle(event);
3572 }
3573
3574 switch (event) {
3575 case FL_ENTER:
3576 case FL_MOVE:
3577 if (active_r()) {
3578 if (Fl::event_inside(text_area.x, text_area.y, text_area.w,
3579 text_area.h)) window()->cursor(FL_CURSOR_INSERT);
3580 else window()->cursor(FL_CURSOR_DEFAULT);
3581 return 1;
3582 } else {
3583 return 0;
3584 }
3585
3586 case FL_LEAVE:
3587 case FL_HIDE:
3588 if (active_r() && window()) {
3589 window()->cursor(FL_CURSOR_DEFAULT);
3590
3591 return 1;
3592 } else {
3593 return 0;
3594 }
3595
3596 case FL_PUSH: {
3597 if (active_r() && window()) {
3598 if (Fl::event_inside(text_area.x, text_area.y, text_area.w,
3599 text_area.h)) window()->cursor(FL_CURSOR_INSERT);
3600 else window()->cursor(FL_CURSOR_DEFAULT);
3601 }
3602
3603 if (Fl::focus() != this) {
3604 Fl::focus(this);
3605 handle(FL_FOCUS);
3606 }
3607 if (Fl_Group::handle(event)) return 1;
3608 if (Fl::event_state()&FL_SHIFT) return handle(FL_DRAG);
3609 dragging = 1;
3610 int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS);
3611 dragPos = pos;
3612 if (buffer()->primary_selection()->includes(pos)) {
3613 dragType = DRAG_START_DND;
3614 return 1;
3615 }
3616 dragType = Fl::event_clicks();
3617 if (dragType == DRAG_CHAR) {
3618 buffer()->unselect();
3619 // Fl::copy("", 0, 0); /* removed for STR 2668 */
3620 }
3621 else if (dragType == DRAG_WORD) {
3622 buffer()->select(word_start(pos), word_end(pos));
3623 dragPos = word_start(pos);
3624 }
3625
3626 if (buffer()->primary_selection()->selected())
3627 insert_position(buffer()->primary_selection()->end());
3628 else
3629 insert_position(pos);
3630 show_insert_position();
3631 return 1;
3632 }
3633
3634 case FL_DRAG: {
3635 if (dragType==DRAG_NONE)
3636 return 1;
3637 if (dragType==DRAG_START_DND) {
3638 if (!Fl::event_is_click() && Fl::dnd_text_ops()) {
3639 const char* copy = buffer()->selection_text();
3640 Fl::dnd();
3641 free((void*)copy);
3642 }
3643 return 1;
3644 }
3645 int X = Fl::event_x(), Y = Fl::event_y(), pos = insert_position();
3646 // if we leave the text_area, we start a timer event
3647 // that will take care of scrolling and selecting
3648 if (Y < text_area.y) {
3649 scroll_x = X;
3650 scroll_amount = (Y - text_area.y) / 5 - 1;
3651 if (!scroll_direction) {
3652 Fl::add_timeout(.01, scroll_timer_cb, this);
3653 }
3654 scroll_direction = 3;
3655 } else if (Y >= text_area.y+text_area.h) {
3656 scroll_x = X;
3657 scroll_amount = (Y - text_area.y - text_area.h) / 5 + 1;
3658 if (!scroll_direction) {
3659 Fl::add_timeout(.01, scroll_timer_cb, this);
3660 }
3661 scroll_direction = 4;
3662 } else if (X < text_area.x) {
3663 scroll_y = Y;
3664 scroll_amount = (X - text_area.x) / 2 - 1;
3665 if (!scroll_direction) {
3666 Fl::add_timeout(.01, scroll_timer_cb, this);
3667 }
3668 scroll_direction = 2;
3669 } else if (X >= text_area.x+text_area.w) {
3670 scroll_y = Y;
3671 scroll_amount = (X - text_area.x - text_area.w) / 2 + 1;
3672 if (!scroll_direction) {
3673 Fl::add_timeout(.01, scroll_timer_cb, this);
3674 }
3675 scroll_direction = 1;
3676 } else {
3677 if (scroll_direction) {
3678 Fl::remove_timeout(scroll_timer_cb, this);
3679 scroll_direction = 0;
3680 }
3681 pos = xy_to_position(X, Y, CURSOR_POS);
3682 pos = buffer()->next_char(pos);
3683 }
3684 fl_text_drag_me(pos, this);
3685 return 1;
3686 }
3687
3688 case FL_RELEASE: {
3689 if (Fl::event_is_click() && (! Fl::event_clicks()) &&
3690 buffer()->primary_selection()->includes(dragPos) && !(Fl::event_state()&FL_SHIFT) ) {
3691 buffer()->unselect(); // clicking in the selection: unselect and move cursor
3692 insert_position(dragPos);
3693 return 1;
3694 } else if (Fl::event_clicks() == DRAG_LINE && Fl::event_button() == FL_LEFT_MOUSE) {
3695 buffer()->select(buffer()->line_start(dragPos), buffer()->next_char(buffer()->line_end(dragPos)));
3696 dragPos = line_start(dragPos);
3697 dragType = DRAG_CHAR;
3698 } else {
3699 dragging = 0;
3700 if (scroll_direction) {
3701 Fl::remove_timeout(scroll_timer_cb, this);
3702 scroll_direction = 0;
3703 }
3704
3705 // convert from WORD or LINE selection to CHAR
3706 /*if (insert_position() >= dragPos)
3707 dragPos = buffer()->primary_selection()->start();
3708 else
3709 dragPos = buffer()->primary_selection()->end();*/
3710 dragType = DRAG_CHAR;
3711 }
3712
3713 const char* copy = buffer()->selection_text();
3714 if (*copy) Fl::copy(copy, strlen(copy), 0);
3715 free((void*)copy);
3716 return 1;
3717 }
3718
3719 case FL_MOUSEWHEEL:
3720 if (Fl::event_dy()) return mVScrollBar->handle(event);
3721 else return mHScrollBar->handle(event);
3722
3723 case FL_UNFOCUS:
3724 if (active_r() && window()) window()->cursor(FL_CURSOR_DEFAULT);
3725 case FL_FOCUS:
3726 if (buffer()->selected()) {
3727 int start, end;
3728 if (buffer()->selection_position(&start, &end))
3729 redisplay_range(start, end);
3730 }
3731 if (buffer()->secondary_selected()) {
3732 int start, end;
3733 if (buffer()->secondary_selection_position(&start, &end))
3734 redisplay_range(start, end);
3735 }
3736 if (buffer()->highlight()) {
3737 int start, end;
3738 if (buffer()->highlight_position(&start, &end))
3739 redisplay_range(start, end);
3740 }
3741 return 1;
3742
3743 case FL_KEYBOARD:
3744 // Copy?
3745 if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='c') {
3746 if (!buffer()->selected()) return 1;
3747 const char *copy = buffer()->selection_text();
3748 if (*copy) Fl::copy(copy, strlen(copy), 1);
3749 free((void*)copy);
3750 return 1;
3751 }
3752
3753 // Select all ?
3754 if ((Fl::event_state()&(FL_CTRL|FL_COMMAND)) && Fl::event_key()=='a') {
3755 buffer()->select(0,buffer()->length());
3756 const char *copy = buffer()->selection_text();
3757 if (*copy) Fl::copy(copy, strlen(copy), 0);
3758 free((void*)copy);
3759 return 1;
3760 }
3761
3762 if (mVScrollBar->handle(event)) return 1;
3763 if (mHScrollBar->handle(event)) return 1;
3764
3765 break;
3766
3767 case FL_SHORTCUT:
3768 if (!(shortcut() ? Fl::test_shortcut(shortcut()) : test_shortcut()))
3769 return 0;
3770 if (Fl::visible_focus() && handle(FL_FOCUS)) {
3771 Fl::focus(this);
3772 return 1;
3773 }
3774 break;
3775
3776 }
3777
3778 return 0;
3779 }
3780
3781
3782 /*
3783 Convert an x pixel position into a column number.
3784 */
3785 double Fl_Text_Display_mod::x_to_col(double y) const
3786 {
3787 if (!mColumnScale) {
3788 mColumnScale = string_width("Mitg", 4, 'A') / 4.0;
3789 }
3790 return (y/mColumnScale)+0.5;
3791 }
3792
3793
3794 /**
3795 Convert a column number into an x pixel position.
3796 */
3797 double Fl_Text_Display_mod::col_to_x(double col) const
3798 {
3799 if (!mColumnScale) {
3800 // recalculate column scale value
3801 x_to_col(0);
3802 }
3803 return col*mColumnScale;
3804 }
3805
3806 //
3807 // End of "$Id: Fl_Text_Display_mod.cxx 8808 2011-06-16 13:31:25Z manolo $".
3808 //