/
Fl_Text_Display.cxx
1980 lines (1743 loc) · 69.7 KB
/
Fl_Text_Display.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// "$Id: Fl_Text_Display.cxx,v 1.12.2.27 2002/08/20 17:12:19 easysw Exp $"
//
// Copyright 2001-2002 by Bill Spitzak and others.
// Original code Copyright Mark Edel. Permission to distribute under
// the LGPL for the FLTK library granted by Mark Edel.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//
#include <FL/Fl.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H>
#include <stdio.h>
#include <stdlib.h>
#include "flstring.h"
#include <limits.h>
#include <ctype.h>
#undef min
#undef max
// Text area margins. Left & right margins should be at least 3 so that
// there is some room for the overhanging parts of the cursor!
#define TOP_MARGIN 1
#define BOTTOM_MARGIN 1
#define LEFT_MARGIN 3
#define RIGHT_MARGIN 3
#define NO_HINT -1
/* Masks for text drawing methods. These are or'd together to form an
integer which describes what drawing calls to use to draw a string */
#define FILL_MASK 0x100
#define SECONDARY_MASK 0x200
#define PRIMARY_MASK 0x400
#define HIGHLIGHT_MASK 0x800
#define STYLE_LOOKUP_MASK 0xff
/* Maximum displayable line length (how many characters will fit across the
widest window). This amount of memory is temporarily allocated from the
stack in the draw_vline() method for drawing strings */
#define MAX_DISP_LINE_LEN 1000
static int max( int i1, int i2 );
static int min( int i1, int i2 );
static int countlines( const char *string );
// CET - FIXME
#define TMPFONTWIDTH 6
Fl_Text_Display::Fl_Text_Display(int X, int Y, int W, int H, const char* l)
: Fl_Group(X, Y, W, H, l) {
mMaxsize = 0;
damage_range1_start = damage_range1_end = -1;
damage_range2_start = damage_range2_end = -1;
dragPos = dragType = dragging = 0;
display_insert_position_hint = 0;
color(FL_BACKGROUND2_COLOR, FL_SELECTION_COLOR);
box(FL_DOWN_FRAME);
textsize(FL_NORMAL_SIZE);
textcolor(FL_FOREGROUND_COLOR);
textfont(FL_HELVETICA);
text_area.x = 0;
text_area.y = 0;
text_area.w = 0;
text_area.h = 0;
mVScrollBar = new Fl_Scrollbar(0,0,1,1);
mVScrollBar->callback((Fl_Callback*)v_scrollbar_cb, this);
mHScrollBar = new Fl_Scrollbar(0,0,1,1);
mHScrollBar->callback((Fl_Callback*)h_scrollbar_cb, this);
mHScrollBar->type(FL_HORIZONTAL);
end();
scrollbar_width(16);
scrollbar_align(FL_ALIGN_BOTTOM_RIGHT);
mCursorOn = 0;
mCursorPos = 0;
mCursorOldY = -100;
mCursorToHint = NO_HINT;
mCursorStyle = NORMAL_CURSOR;
mCursorPreferredCol = -1;
mBuffer = 0;
mFirstChar = 0;
mLastChar = 0;
mNBufferLines = 0;
mTopLineNum = mTopLineNumHint = 1;
mHorizOffset = mHorizOffsetHint = 0;
mCursor_color = FL_BLACK;
mFixedFontWidth = TMPFONTWIDTH;// CET - FIXME
mStyleBuffer = 0;
mStyleTable = 0;
mNStyles = 0;
mNVisibleLines = 1;
mLineStarts = new int[mNVisibleLines];
mLineStarts[0] = 0;
mUnfinishedStyle = 0;
mUnfinishedHighlightCB = 0;
mHighlightCBArg = 0;
}
/*
** Free a text display and release its associated memory. Note, the text
** BUFFER that the text display displays is a separate entity and is not
** freed, nor are the style buffer or style table.
*/
Fl_Text_Display::~Fl_Text_Display() {
if (mBuffer) mBuffer->remove_modify_callback(buffer_modified_cb, this);
if (mLineStarts) delete[] mLineStarts;
}
/*
** Attach a text buffer to display, replacing the current buffer (if any)
*/
void Fl_Text_Display::buffer( Fl_Text_Buffer *buf ) {
/* If the text display is already displaying a buffer, clear it off
of the display and remove our callback from it */
if ( mBuffer != 0 ) {
buffer_modified_cb( 0, 0, mBuffer->length(), 0, 0, this );
mBuffer->remove_modify_callback( buffer_modified_cb, this );
}
/* Add the buffer to the display, and attach a callback to the buffer for
receiving modification information when the buffer contents change */
mBuffer = buf;
mBuffer->add_modify_callback( buffer_modified_cb, this );
/* Update the display */
buffer_modified_cb( 0, buf->length(), 0, 0, 0, this );
/* Resize the widget to update the screen... */
resize(x(), y(), w(), h());
}
/*
** Attach (or remove) highlight information in text display and redisplay.
** Highlighting information consists of a style buffer which parallels the
** normal text buffer, but codes font and color information for the display;
** a style table which translates style buffer codes (indexed by buffer
** character - 'A') into fonts and colors; and a callback mechanism for
** as-needed highlighting, triggered by a style buffer entry of
** "unfinishedStyle". Style buffer can trigger additional redisplay during
** a normal buffer modification if the buffer contains a primary Fl_Text_Selection
** (see extendRangeForStyleMods for more information on this protocol).
**
** Style buffers, tables and their associated memory are managed by the caller.
*/
void
Fl_Text_Display::highlight_data(Fl_Text_Buffer *styleBuffer,
const Style_Table_Entry *styleTable,
int nStyles, char unfinishedStyle,
Unfinished_Style_Cb unfinishedHighlightCB,
void *cbArg ) {
mStyleBuffer = styleBuffer;
mStyleTable = styleTable;
mNStyles = nStyles;
mUnfinishedStyle = unfinishedStyle;
mUnfinishedHighlightCB = unfinishedHighlightCB;
mHighlightCBArg = cbArg;
damage(FL_DAMAGE_EXPOSE);
}
int Fl_Text_Display::longest_vline() {
int longest = 0;
for (int i = 0; i < mNVisibleLines; i++)
longest = max(longest, measure_vline(i));
return longest;
}
/*
** Change the size of the displayed text area
*/
void Fl_Text_Display::resize(int X, int Y, int W, int H) {
Fl_Widget::resize(X,Y,W,H);
if (!buffer()) return;
X += Fl::box_dx(box());
Y += Fl::box_dy(box());
W -= Fl::box_dw(box());
H -= Fl::box_dh(box());
text_area.x = X+LEFT_MARGIN;
text_area.y = Y+BOTTOM_MARGIN;
text_area.w = W-LEFT_MARGIN-RIGHT_MARGIN;
text_area.h = H-TOP_MARGIN-BOTTOM_MARGIN;
int i;
/* Find the new maximum font height for this text display */
for (i = 0, mMaxsize = fl_height(textfont(), textsize()); i < mNStyles; i++)
mMaxsize = max(mMaxsize, fl_height(mStyleTable[i].font, mStyleTable[i].size));
// did we have scrollbars initially?
int hscrollbarvisible = mHScrollBar->visible();
int vscrollbarvisible = mVScrollBar->visible();
// try without scrollbars first
mVScrollBar->clear_visible();
mHScrollBar->clear_visible();
for (int again = 1; again;) {
again = 0;
/* reallocate and update the line starts array, which may have changed
size and / or contents. */
int nvlines = (text_area.h + mMaxsize - 1) / mMaxsize;
if (mNVisibleLines != nvlines) {
mNVisibleLines = nvlines;
if (mLineStarts) delete[] mLineStarts;
mLineStarts = new int [mNVisibleLines];
calc_line_starts(0, mNVisibleLines);
calc_last_char();
}
// figure the scrollbars
if (scrollbar_width()) {
/* Decide if the vertical scroll bar needs to be visible */
if (scrollbar_align() & (FL_ALIGN_LEFT|FL_ALIGN_RIGHT) &&
mNBufferLines >= mNVisibleLines - 1)
{
mVScrollBar->set_visible();
if (scrollbar_align() & FL_ALIGN_LEFT) {
text_area.x = X+scrollbar_width()+LEFT_MARGIN;
text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN;
mVScrollBar->resize(X, text_area.y-TOP_MARGIN, scrollbar_width(),
text_area.h+TOP_MARGIN+BOTTOM_MARGIN);
} else {
text_area.x = X+LEFT_MARGIN;
text_area.w = W-scrollbar_width()-LEFT_MARGIN-RIGHT_MARGIN;
mVScrollBar->resize(X+W-scrollbar_width(), text_area.y-TOP_MARGIN,
scrollbar_width(), text_area.h+TOP_MARGIN+BOTTOM_MARGIN);
}
}
/*
Decide if the horizontal scroll bar needs to be visible. If there
is a vertical scrollbar, a horizontal is always created too. This
is because the alternatives are unatractive:
* Dynamically creating a horizontal scrollbar based on the currently
visible lines is what the original nedit does, but it always wastes
space for the scrollbar even when it's not used. Since the FLTK
widget dynamically allocates the space for the scrollbar and
rearranges the widget to make room for it, this would create a very
visually displeasing "bounce" effect when the vertical scrollbar is
dragged. Trust me, I tried it and it looks really bad.
* The other alternative would be to keep track of what the longest
line in the entire buffer is and base the scrollbar on that. I
didn't do this because I didn't see any easy way to do that using
the nedit code and this could involve a lengthy calculation for
large buffers. If an efficient and non-costly way of doing this
can be found, this might be a way to go.
*/
/* WAS: Suggestion: Try turning the horizontal scrollbar on when
you first see a line that is too wide in the window, but then
don't turn it off (ie mix both of your solutions). */
if (scrollbar_align() & (FL_ALIGN_TOP|FL_ALIGN_BOTTOM) &&
(mVScrollBar->visible() || longest_vline() > text_area.w))
{
if (!mHScrollBar->visible()) {
mHScrollBar->set_visible();
again = 1; // loop again to see if we now need vert. & recalc sizes
}
if (scrollbar_align() & FL_ALIGN_TOP) {
text_area.y = Y + scrollbar_width()+TOP_MARGIN;
text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN;
mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y,
text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width());
} else {
text_area.y = Y+TOP_MARGIN;
text_area.h = H - scrollbar_width()-TOP_MARGIN-BOTTOM_MARGIN;
mHScrollBar->resize(text_area.x-LEFT_MARGIN, Y+H-scrollbar_width(),
text_area.w+LEFT_MARGIN+RIGHT_MARGIN, scrollbar_width());
}
}
}
}
// user request to change viewport
if (mTopLineNumHint != mTopLineNum || mHorizOffsetHint != mHorizOffset)
scroll_(mTopLineNumHint, mHorizOffsetHint);
// everything will fit in the viewport
if (mNBufferLines < mNVisibleLines || mBuffer == NULL || mBuffer->length() == 0)
scroll_(1, mHorizOffset);
/* if empty lines become visible, there may be an opportunity to
display more text by scrolling down */
else while (mLineStarts[mNVisibleLines-2] == -1)
scroll_(mTopLineNum-1, mHorizOffset);
// user request to display insert position
if (display_insert_position_hint)
display_insert();
// in case horizontal offset is now greater than longest line
int maxhoffset = max(0, longest_vline()-text_area.w);
if (mHorizOffset > maxhoffset)
scroll_(mTopLineNumHint, maxhoffset);
mTopLineNumHint = mTopLineNum;
mHorizOffsetHint = mHorizOffset;
display_insert_position_hint = 0;
if (hscrollbarvisible != mHScrollBar->visible() ||
vscrollbarvisible != mVScrollBar->visible())
redraw();
update_v_scrollbar();
update_h_scrollbar();
}
/*
** Refresh a rectangle of the text display. left and top are in coordinates of
** the text drawing window
*/
void Fl_Text_Display::draw_text( int left, int top, int width, int height ) {
int fontHeight, firstLine, lastLine, line;
/* find the line number range of the display */
fontHeight = mMaxsize ? mMaxsize : textsize_;
firstLine = ( top - text_area.y - fontHeight + 1 ) / fontHeight;
lastLine = ( top + height - text_area.y ) / fontHeight + 1;
fl_push_clip( left, top, width, height );
/* draw the lines */
for ( line = firstLine; line <= lastLine; line++ )
draw_vline( line, left, left + width, 0, INT_MAX );
fl_pop_clip();
}
void Fl_Text_Display::redisplay_range(int startpos, int endpos) {
if (damage_range1_start == -1 && damage_range1_end == -1) {
damage_range1_start = startpos;
damage_range1_end = endpos;
} else if ((startpos >= damage_range1_start && startpos <= damage_range1_end) ||
(endpos >= damage_range1_start && endpos <= damage_range1_end)) {
damage_range1_start = min(damage_range1_start, startpos);
damage_range1_end = max(damage_range1_end, endpos);
} else if (damage_range2_start == -1 && damage_range2_end == -1) {
damage_range2_start = startpos;
damage_range2_end = endpos;
} else {
damage_range2_start = min(damage_range2_start, startpos);
damage_range2_end = max(damage_range2_end, endpos);
}
damage(FL_DAMAGE_SCROLL);
}
/*
** Refresh all of the text between buffer positions "start" and "end"
** not including the character at the position "end".
** If end points beyond the end of the buffer, refresh the whole display
** after pos, including blank lines which are not technically part of
** any range of characters.
*/
void Fl_Text_Display::draw_range(int startpos, int endpos) {
int i, startLine, lastLine, startIndex, endIndex;
/* If the range is outside of the displayed text, just return */
if ( endpos < mFirstChar || ( startpos > mLastChar &&
!empty_vlines() ) ) return;
/* Clean up the starting and ending values */
if ( startpos < 0 ) startpos = 0;
if ( startpos > mBuffer->length() ) startpos = mBuffer->length();
if ( endpos < 0 ) endpos = 0;
if ( endpos > mBuffer->length() ) endpos = mBuffer->length();
/* Get the starting and ending lines */
if ( startpos < mFirstChar )
startpos = mFirstChar;
if ( !position_to_line( startpos, &startLine ) )
startLine = mNVisibleLines - 1;
if ( endpos >= mLastChar ) {
lastLine = mNVisibleLines - 1;
} else {
if ( !position_to_line( endpos, &lastLine ) ) {
/* shouldn't happen */
lastLine = mNVisibleLines - 1;
}
}
/* Get the starting and ending positions within the lines */
startIndex = mLineStarts[ startLine ] == -1 ? 0 :
startpos - mLineStarts[ startLine ];
if ( endpos >= mLastChar )
endIndex = INT_MAX;
else if ( mLineStarts[ lastLine ] == -1 )
endIndex = 0;
else
endIndex = endpos - mLineStarts[ lastLine ];
/* If the starting and ending lines are the same, redisplay the single
line between "start" and "end" */
if ( startLine == lastLine ) {
draw_vline( startLine, 0, INT_MAX, startIndex, endIndex );
return;
}
/* Redisplay the first line from "start" */
draw_vline( startLine, 0, INT_MAX, startIndex, INT_MAX );
/* Redisplay the lines in between at their full width */
for ( i = startLine + 1; i < lastLine; i++ )
draw_vline( i, 0, INT_MAX, 0, INT_MAX );
/* Redisplay the last line to "end" */
draw_vline( lastLine, 0, INT_MAX, 0, endIndex );
}
/*
** Set the position of the text insertion cursor for text display
*/
void Fl_Text_Display::insert_position( int newPos ) {
/* make sure new position is ok, do nothing if it hasn't changed */
if ( newPos == mCursorPos )
return;
if ( newPos < 0 ) newPos = 0;
if ( newPos > mBuffer->length() ) newPos = mBuffer->length();
/* cursor movement cancels vertical cursor motion column */
mCursorPreferredCol = -1;
/* erase the cursor at it's previous position */
redisplay_range(mCursorPos - 1, mCursorPos + 1);
mCursorPos = newPos;
/* draw cursor at its new position */
redisplay_range(mCursorPos - 1, mCursorPos + 1);
}
void Fl_Text_Display::show_cursor(int b) {
mCursorOn = b;
redisplay_range(mCursorPos - 1, mCursorPos + 1);
}
void Fl_Text_Display::cursor_style(int style) {
mCursorStyle = style;
if (mCursorOn) show_cursor();
}
/*
** Insert "text" at the current cursor location. This has the same
** effect as inserting the text into the buffer using BufInsert and
** then moving the insert position after the newly inserted text, except
** that it's optimized to do less redrawing.
*/
void Fl_Text_Display::insert(const char* text) {
int pos = mCursorPos;
mCursorToHint = pos + strlen( text );
mBuffer->insert( pos, text );
mCursorToHint = NO_HINT;
}
/*
** Insert "text" (which must not contain newlines), overstriking the current
** cursor location.
*/
void Fl_Text_Display::overstrike(const char* text) {
int startPos = mCursorPos;
Fl_Text_Buffer *buf = mBuffer;
int lineStart = buf->line_start( startPos );
int textLen = strlen( text );
int i, p, endPos, indent, startIndent, endIndent;
const char *c;
char ch, *paddedText = NULL;
/* determine how many displayed character positions are covered */
startIndent = mBuffer->count_displayed_characters( lineStart, startPos );
indent = startIndent;
for ( c = text; *c != '\0'; c++ )
indent += Fl_Text_Buffer::character_width( *c, indent, buf->tab_distance(), buf->null_substitution_character() );
endIndent = indent;
/* find which characters to remove, and if necessary generate additional
padding to make up for removed control characters at the end */
indent = startIndent;
for ( p = startPos; ; p++ ) {
if ( p == buf->length() )
break;
ch = buf->character( p );
if ( ch == '\n' )
break;
indent += Fl_Text_Buffer::character_width( ch, indent, buf->tab_distance(), buf->null_substitution_character() );
if ( indent == endIndent ) {
p++;
break;
} else if ( indent > endIndent ) {
if ( ch != '\t' ) {
p++;
paddedText = new char [ textLen + FL_TEXT_MAX_EXP_CHAR_LEN + 1 ];
strcpy( paddedText, text );
for ( i = 0; i < indent - endIndent; i++ )
paddedText[ textLen + i ] = ' ';
paddedText[ textLen + i ] = '\0';
}
break;
}
}
endPos = p;
mCursorToHint = startPos + textLen;
buf->replace( startPos, endPos, paddedText == NULL ? text : paddedText );
mCursorToHint = NO_HINT;
if ( paddedText != NULL )
delete [] paddedText;
}
/*
** Translate a buffer text position to the XY location where the top left
** of the cursor would be positioned to point to that character. Returns
** 0 if the position is not displayed because it is VERTICALLY out
** of view. If the position is horizontally out of view, returns the
** X coordinate where the position would be if it were visible.
*/
int Fl_Text_Display::position_to_xy( int pos, int* X, int* Y ) {
int charIndex, lineStartPos, fontHeight, lineLen;
int visLineNum, charLen, outIndex, xStep, charStyle;
char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ];
const char *lineStr;
/* If position is not displayed, return false */
if (pos < mFirstChar || (pos > mLastChar && !empty_vlines()))
return 0;
/* Calculate Y coordinate */
if (!position_to_line(pos, &visLineNum)) return 0;
fontHeight = mMaxsize;
*Y = text_area.y + visLineNum * fontHeight;
/* Get the text, length, and buffer position of the line. If the position
is beyond the end of the buffer and should be at the first position on
the first empty line, don't try to get or scan the text */
lineStartPos = mLineStarts[visLineNum];
if ( lineStartPos == -1 ) {
*X = text_area.x - mHorizOffset;
return 1;
}
lineLen = vline_length( visLineNum );
lineStr = mBuffer->text_range( lineStartPos, lineStartPos + lineLen );
/* Step through character positions from the beginning of the line
to "pos" to calculate the X coordinate */
xStep = text_area.x - mHorizOffset;
outIndex = 0;
for ( charIndex = 0; charIndex < pos - lineStartPos; charIndex++ ) {
charLen = Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar,
mBuffer->tab_distance(), mBuffer->null_substitution_character() );
charStyle = position_style( lineStartPos, lineLen, charIndex,
outIndex );
xStep += string_width( expandedChar, charLen, charStyle );
outIndex += charLen;
}
*X = xStep;
delete [] (char *)lineStr;
return 1;
}
/*
** Find the line number of position "pos". Note: this only works for
** displayed lines. If the line is not displayed, the function returns
** 0 (without the lineStarts array it could turn in to very long
** calculation involving scanning large amounts of text in the buffer).
*/
int Fl_Text_Display::position_to_linecol( int pos, int* lineNum, int* column ) {
int retVal;
retVal = position_to_line( pos, lineNum );
if ( retVal ) {
*column = mBuffer->count_displayed_characters(
mLineStarts[ *lineNum ], pos );
*lineNum += mTopLineNum;
}
return retVal;
}
/*
** Return 1 if position (X, Y) is inside of the primary Fl_Text_Selection
*/
int Fl_Text_Display::in_selection( int X, int Y ) {
int row, column, pos = xy_to_position( X, Y, CHARACTER_POS );
Fl_Text_Buffer *buf = mBuffer;
xy_to_rowcol( X, Y, &row, &column, CHARACTER_POS );
return buf->primary_selection()->includes(pos, buf->line_start( pos ), column);
}
/*
** Scroll the display to bring insertion cursor into view.
**
** Note: it would be nice to be able to do this without counting lines twice
** (scroll_() counts them too) and/or to count from the most efficient
** starting point, but the efficiency of this routine is not as important to
** the overall performance of the text display.
*/
void Fl_Text_Display::display_insert() {
int hOffset, topLine, X, Y;
hOffset = mHorizOffset;
topLine = mTopLineNum;
if (insert_position() < mFirstChar) {
topLine -= buffer()->count_lines(insert_position(), mFirstChar);
} else if (mLineStarts[mNVisibleLines-2] != -1) {
int lastChar = buffer()->line_end(mLineStarts[mNVisibleLines-2]);
if (insert_position() > lastChar)
topLine += buffer()->count_lines(lastChar, insert_position());
}
/* Find the new setting for horizontal offset (this is a bit ungraceful).
If the line is visible, just use PositionToXY to get the position
to scroll to, otherwise, do the vertical scrolling first, then the
horizontal */
if (!position_to_xy( mCursorPos, &X, &Y )) {
scroll_(topLine, hOffset);
if (!position_to_xy( mCursorPos, &X, &Y ))
return; /* Give up, it's not worth it (but why does it fail?) */
}
if (X > text_area.x + text_area.w)
hOffset += X-(text_area.x + text_area.w);
else if (X < text_area.x)
hOffset += X-text_area.x;
/* Do the scroll */
if (topLine != mTopLineNum || hOffset != mHorizOffset)
scroll_(topLine, hOffset);
}
void Fl_Text_Display::show_insert_position() {
display_insert_position_hint = 1;
resize(x(), y(), w(), h());
}
/*
** Cursor movement functions
*/
int Fl_Text_Display::move_right() {
if ( mCursorPos >= mBuffer->length() )
return 0;
insert_position( mCursorPos + 1 );
return 1;
}
int Fl_Text_Display::move_left() {
if ( mCursorPos <= 0 )
return 0;
insert_position( mCursorPos - 1 );
return 1;
}
int Fl_Text_Display::move_up() {
int lineStartPos, column, prevLineStartPos, newPos, visLineNum;
/* Find the position of the start of the line. Use the line starts array
if possible */
if ( position_to_line( mCursorPos, &visLineNum ) )
lineStartPos = mLineStarts[ visLineNum ];
else {
lineStartPos = buffer()->line_start( mCursorPos );
visLineNum = -1;
}
if ( lineStartPos == 0 )
return 0;
/* Decide what column to move to, if there's a preferred column use that */
column = mCursorPreferredCol >= 0 ? mCursorPreferredCol :
mBuffer->count_displayed_characters( lineStartPos, mCursorPos );
/* count forward from the start of the previous line to reach the column */
if ( visLineNum != -1 && visLineNum != 0 )
prevLineStartPos = mLineStarts[ visLineNum - 1 ];
else
prevLineStartPos = buffer()->rewind_lines( lineStartPos, 1 );
newPos = mBuffer->skip_displayed_characters( prevLineStartPos, column );
/* move the cursor */
insert_position( newPos );
/* if a preferred column wasn't aleady established, establish it */
mCursorPreferredCol = column;
return 1;
}
int Fl_Text_Display::move_down() {
int lineStartPos, column, nextLineStartPos, newPos, visLineNum;
if ( mCursorPos == mBuffer->length() )
return 0;
if ( position_to_line( mCursorPos, &visLineNum ) )
lineStartPos = mLineStarts[ visLineNum ];
else {
lineStartPos = buffer()->line_start( mCursorPos );
visLineNum = -1;
}
column = mCursorPreferredCol >= 0 ? mCursorPreferredCol :
mBuffer->count_displayed_characters( lineStartPos, mCursorPos );
nextLineStartPos = buffer()->skip_lines( lineStartPos, 1 );
newPos = mBuffer->skip_displayed_characters( nextLineStartPos, column );
insert_position( newPos );
mCursorPreferredCol = column;
return 1;
}
void Fl_Text_Display::next_word() {
int pos = insert_position();
while ( pos < buffer()->length() && (
isalnum( buffer()->character( pos ) ) || buffer()->character( pos ) == '_' ) ) {
pos++;
}
while ( pos < buffer()->length() && !( isalnum( buffer()->character( pos ) ) || buffer()->character( pos ) == '_' ) ) {
pos++;
}
insert_position( pos );
}
void Fl_Text_Display::previous_word() {
int pos = insert_position();
pos--;
while ( pos && !( isalnum( buffer()->character( pos ) ) || buffer()->character( pos ) == '_' ) ) {
pos--;
}
while ( pos && ( isalnum( buffer()->character( pos ) ) || buffer()->character( pos ) == '_' ) ) {
pos--;
}
if ( !( isalnum( buffer()->character( pos ) ) || buffer()->character( pos ) == '_' ) ) pos++;
insert_position( pos );
}
/*
** Callback attached to the text buffer to receive modification information
*/
void Fl_Text_Display::buffer_modified_cb( int pos, int nInserted, int nDeleted,
int nRestyled, const char *deletedText, void *cbArg ) {
int linesInserted, linesDeleted, startDispPos, endDispPos;
Fl_Text_Display *textD = ( Fl_Text_Display * ) cbArg;
Fl_Text_Buffer *buf = textD->mBuffer;
int scrolled, origCursorPos = textD->mCursorPos;
/* buffer modification cancels vertical cursor motion column */
if ( nInserted != 0 || nDeleted != 0 )
textD->mCursorPreferredCol = -1;
/* Count the number of lines inserted and deleted */
linesInserted = nInserted == 0 ? 0 :
textD->buffer()->count_lines( pos, pos + nInserted );
linesDeleted = nDeleted == 0 ? 0 : countlines( deletedText );
/* Update the line starts and topLineNum */
if ( nInserted != 0 || nDeleted != 0 ) {
textD->update_line_starts( pos, nInserted, nDeleted, linesInserted,
linesDeleted, &scrolled );
} else
scrolled = 0;
/* Update the line count for the whole buffer */
textD->mNBufferLines += linesInserted - linesDeleted;
/* Update the cursor position */
if ( textD->mCursorToHint != NO_HINT ) {
textD->mCursorPos = textD->mCursorToHint;
textD->mCursorToHint = NO_HINT;
} else if ( textD->mCursorPos > pos ) {
if ( textD->mCursorPos < pos + nDeleted )
textD->mCursorPos = pos;
else
textD->mCursorPos += nInserted - nDeleted;
}
// refigure scrollbars & stuff
textD->resize(textD->x(), textD->y(), textD->w(), textD->h());
// don't need to do anything else if not visible?
if (!textD->visible_r()) return;
/* If the changes caused scrolling, re-paint everything and we're done. */
if ( scrolled ) {
textD->damage(FL_DAMAGE_EXPOSE);
if ( textD->mStyleBuffer ) /* See comments in extendRangeForStyleMods */
textD->mStyleBuffer->primary_selection()->selected(0);
return;
}
/* If the changes didn't cause scrolling, decide the range of characters
that need to be re-painted. Also if the cursor position moved, be
sure that the redisplay range covers the old cursor position so the
old cursor gets erased, and erase the bits of the cursor which extend
beyond the left and right edges of the text. */
startDispPos = pos;
if ( origCursorPos == startDispPos && textD->mCursorPos != startDispPos )
startDispPos = min( startDispPos, origCursorPos - 1 );
if ( linesInserted == linesDeleted ) {
if ( nInserted == 0 && nDeleted == 0 )
endDispPos = pos + nRestyled;
else {
endDispPos = buf->line_end( pos + nInserted ) + 1;
// CET - FIXME if ( origCursorPos >= startDispPos &&
// ( origCursorPos <= endDispPos || endDispPos == buf->length() ) )
}
} else {
endDispPos = textD->mLastChar + 1;
// CET - FIXME if ( origCursorPos >= pos )
}
/* If there is a style buffer, check if the modification caused additional
changes that need to be redisplayed. (Redisplaying separately would
cause double-redraw on almost every modification involving styled
text). Extend the redraw range to incorporate style changes */
if ( textD->mStyleBuffer )
textD->extend_range_for_styles( &startDispPos, &endDispPos );
/* Redisplay computed range */
textD->redisplay_range( startDispPos, endDispPos );
}
/*
** Find the line number of position "pos" relative to the first line of
** displayed text. Returns 0 if the line is not displayed.
*/
int Fl_Text_Display::position_to_line( int pos, int *lineNum ) {
int i;
if ( pos < mFirstChar )
return 0;
if ( pos > mLastChar ) {
if ( empty_vlines() ) {
if ( mLastChar < mBuffer->length() ) {
if ( !position_to_line( mLastChar, lineNum ) ) {
fprintf( stderr, "Consistency check ptvl failed\n" );
return 0;
}
return ++( *lineNum ) <= mNVisibleLines - 1;
} else {
position_to_line( mLastChar - 1, lineNum );
return 1;
}
}
return 0;
}
for ( i = mNVisibleLines - 1; i >= 0; i-- ) {
if ( mLineStarts[ i ] != -1 && pos >= mLineStarts[ i ] ) {
*lineNum = i;
return 1;
}
}
return 0; /* probably never be reached */
}
/*
** Draw the text on a single line represented by "visLineNum" (the
** number of lines down from the top of the display), limited by
** "leftClip" and "rightClip" window coordinates and "leftCharIndex" and
** "rightCharIndex" character positions (not including the character at
** position "rightCharIndex").
*/
void Fl_Text_Display::draw_vline(int visLineNum, int leftClip, int rightClip,
int leftCharIndex, int rightCharIndex) {
Fl_Text_Buffer * buf = mBuffer;
int i, X, Y, startX, charIndex, lineStartPos, lineLen, fontHeight;
int stdCharWidth, charWidth, startIndex, charStyle, style;
int charLen, outStartIndex, outIndex;
int dispIndexOffset;
char expandedChar[ FL_TEXT_MAX_EXP_CHAR_LEN ], outStr[ MAX_DISP_LINE_LEN ];
char *outPtr;
const char *lineStr;
// printf("draw_vline(visLineNum=%d, leftClip=%d, rightClip=%d, leftCharIndex=%d, rightCharIndex=%d)\n",
// visLineNum, leftClip, rightClip, leftCharIndex, rightCharIndex);
// printf("nNVisibleLines=%d\n", mNVisibleLines);
/* If line is not displayed, skip it */
if ( visLineNum < 0 || visLineNum >= mNVisibleLines )
return;
/* Calculate Y coordinate of the string to draw */
fontHeight = mMaxsize;
Y = text_area.y + visLineNum * fontHeight;
/* Get the text, length, and buffer position of the line to display */
lineStartPos = mLineStarts[ visLineNum ];
// printf("lineStartPos=%d\n", lineStartPos);
if ( lineStartPos == -1 ) {
lineLen = 0;
lineStr = NULL;
} else {
lineLen = vline_length( visLineNum );
lineStr = buf->text_range( lineStartPos, lineStartPos + lineLen );
}
/* Space beyond the end of the line is still counted in units of characters
of a standardized character width (this is done mostly because style
changes based on character position can still occur in this region due
to rectangular Fl_Text_Selections). stdCharWidth must be non-zero to prevent a
potential infinite loop if X does not advance */
stdCharWidth = TMPFONTWIDTH; //mFontStruct->max_bounds.width;
if ( stdCharWidth <= 0 ) {
fprintf( stderr, "Internal Error, bad font measurement\n" );
delete [] (char *)lineStr;
return;
}
/* Shrink the clipping range to the active display area */
leftClip = max( text_area.x, leftClip );
rightClip = min( rightClip, text_area.x + text_area.w );
/* Rectangular Fl_Text_Selections are based on "real" line starts (after a newline
or start of buffer). Calculate the difference between the last newline
position and the line start we're using. Since scanning back to find a
newline is expensive, only do so if there's actually a rectangular
Fl_Text_Selection which needs it */
dispIndexOffset = 0;
/* Step through character positions from the beginning of the line (even if
that's off the left edge of the displayed area) to find the first
character position that's not clipped, and the X coordinate for drawing
that character */
X = text_area.x - mHorizOffset;
outIndex = 0;
for ( charIndex = 0; ; charIndex++ ) {
charLen = charIndex >= lineLen ? 1 :
Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex,
expandedChar, buf->tab_distance(), buf->null_substitution_character() );
style = position_style( lineStartPos, lineLen, charIndex,
outIndex + dispIndexOffset );
charWidth = charIndex >= lineLen ? stdCharWidth :
string_width( expandedChar, charLen, style );
if ( X + charWidth >= leftClip && charIndex >= leftCharIndex ) {
startIndex = charIndex;
outStartIndex = outIndex;
startX = X;
break;
}
X += charWidth;
outIndex += charLen;
}
/* Scan character positions from the beginning of the clipping range, and
draw parts whenever the style changes (also note if the cursor is on
this line, and where it should be drawn to take advantage of the x
position which we've gone to so much trouble to calculate) */
outPtr = outStr;
outIndex = outStartIndex;
X = startX;
for ( charIndex = startIndex; charIndex < rightCharIndex; charIndex++ ) {
charLen = charIndex >= lineLen ? 1 :
Fl_Text_Buffer::expand_character( lineStr[ charIndex ], outIndex, expandedChar,
buf->tab_distance(), buf->null_substitution_character() );
charStyle = position_style( lineStartPos, lineLen, charIndex,
outIndex + dispIndexOffset );
for ( i = 0; i < charLen; i++ ) {
if ( i != 0 && charIndex < lineLen && lineStr[ charIndex ] == '\t' )
charStyle = position_style( lineStartPos, lineLen,
charIndex, outIndex + dispIndexOffset );
if ( charStyle != style ) {
draw_string( style, startX, Y, X, outStr, outPtr - outStr );
outPtr = outStr;
startX = X;
style = charStyle;
}
if ( charIndex < lineLen ) {
*outPtr = expandedChar[ i ];
charWidth = string_width( &expandedChar[ i ], 1, charStyle );
} else
charWidth = stdCharWidth;
outPtr++;
X += charWidth;
outIndex++;
}
if ( outPtr - outStr + FL_TEXT_MAX_EXP_CHAR_LEN >= MAX_DISP_LINE_LEN || X >= rightClip )
break;
}
/* Draw the remaining style segment */
draw_string( style, startX, Y, X, outStr, outPtr - outStr );