~unity-team/nux/nux.text-entry-im-auto-test

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
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 *
 */


#include "Basic/NKernel.h"
#include "TextView.h"
#include "TextViewInternal.h"
#include "HScrollBar.h"
#include "VScrollBar.h"

int ScrollDir (int counter, int dir);

//
//	WM_MOUSEACTIVATE
//
//	Grab the keyboard input focus
//
// LONG TextView::OnMouseActivate(HWND hwndTop, UINT nHitTest, UINT nMessage)
// {
// 	SetFocus(m_hWnd);
// 	return MA_ACTIVATE;
// }

//
//	WM_LBUTTONDOWN
//
//  Position caret to nearest text character under mouse
//
void TextView::OnLButtonDown (int mx, int my, unsigned long button_flags, unsigned long key_flags /*UINT nFlags, int mx, int my*/)
{
  inl::t_u32 nLineNo;
  inl::t_u32 nFileOff;

  mx = mx - getBorder() - GetViewContentLeftMargin();
  my = my - getTopBorder() - GetViewContentTopMargin();

  // remove any existing selection
  InvalidateRange (m_nSelectionStart, m_nSelectionEnd);

  // regular mouse input - mouse is within
  if (mx >= LeftMarginWidth() )
  {
    m_CharacterAtCursor = 0;
    m_CursorCharacterPosition = 0;
    // map the mouse-coordinates to a real file-offset-coordinate
    MouseCoordToFilePos (mx, my, &nLineNo, &nFileOff, &m_nCaretPosX, &m_CharacterAtCursor, &m_CursorCharacterPosition);
    m_nAnchorPosX = m_nCaretPosX;

    UpdateCaretXY (m_nCaretPosX, nLineNo);

    // Any key but <shift>
    if (IsKeyPressed (VK_SHIFT) == false)
    {
      // remove any existing selection
      InvalidateRange (m_nSelectionStart, m_nSelectionEnd);
      // reset cursor and selection offsets to the same location
      m_nSelectionStart	= nFileOff;
      m_nSelectionEnd		= nFileOff;
      m_nCursorOffset		= nFileOff;
    }
    else
    {
      // redraw to cursor
      InvalidateRange (m_nSelectionEnd, nFileOff);

      // extend selection to cursor
      m_nSelectionEnd		= nFileOff;
      m_nCursorOffset		= nFileOff;
    }

    if (IsKeyPressed (VK_MENU) )
    {
      m_cpBlockStart.line = nLineNo;
      m_cpBlockStart.xpos = m_nCaretPosX;
      m_nSelectionType	= SEL_BLOCK;
    }
    else
    {
      m_nSelectionType	= SEL_NORMAL;
    }

    // set capture for mouse-move selections
    m_nSelectionMode = IsKeyPressed (VK_MENU) ? SEL_BLOCK : SEL_NORMAL;
  }
  // mouse clicked within margin
  else
  {
    // remove any existing selection
    InvalidateRange (m_nSelectionStart, m_nSelectionEnd);

    nLineNo = (my / m_nLineHeight) + m_nVScrollPos;

    //
    // if we click in the margin then jump back to start of line
    //
    if (m_nHScrollPos != 0)
    {
      m_nHScrollPos = 0;
      SetupScrollbars();
      RefreshWindow();
    }

    m_pTextDoc->lineinfo_from_lineno (nLineNo, &m_nSelectionStart, &m_nSelectionEnd, 0, 0);
    m_nSelectionEnd    += m_nSelectionStart;
    m_nCursorOffset	    = m_nSelectionStart;

    m_nSelMarginOffset1 = m_nSelectionStart;
    m_nSelMarginOffset2 = m_nSelectionEnd;

    InvalidateRange (m_nSelectionStart, m_nSelectionEnd);

    UpdateCaretOffset (m_nCursorOffset, FALSE, &m_nCaretPosX, &m_nCurrentLine);
    m_nAnchorPosX = m_nCaretPosX;
    //RepositionCaret();

    // set capture for mouse-move selections
    m_nSelectionMode = SEL_MARGIN;
  }

  UpdateLine (nLineNo);

  SetCapture (GetThreadGLWindow()->GetWindowHandle() );

  StopBlinkCursor (false);
  StartBlinkCursor (false);
  NeedSoftRedraw();
}

//
//	WM_LBUTTONUP
//
//	Release capture and cancel any mouse-scrolling
//
void TextView::OnLButtonUp (int mx, int my, unsigned long button_flags, unsigned long key_flags /*UINT nFlags, int mx, int my*/)
{
  mx = mx - getBorder() - GetViewContentLeftMargin();
  my = my - getTopBorder() - GetViewContentTopMargin();

  // shift cursor to end of selection
  if (m_nSelectionMode == SEL_MARGIN)
  {
    m_nCursorOffset = m_nSelectionEnd;
    // Invalidate the selected range just to delete the cursor that was in it.
    InvalidateRange (m_nSelectionStart, m_nSelectionEnd);
    UpdateCaretOffset (m_nCursorOffset, FALSE, &m_nCaretPosX, &m_nCurrentLine);
  }

  if (m_nSelectionMode)
  {
    // cancel the scroll-timer if it is still running
    if (MouseAutoScrollHandle != 0)
    {
      GetThreadTimer()->RemoveTimerHandler (MouseAutoScrollHandle);
      MouseAutoScrollHandle = 0;
    }

    m_nSelectionMode = SEL_NONE;
    ReleaseCapture();
  }

  NeedSoftRedraw();
}

//
//	WM_LBUTTONDBKCLK
//
//	Select the word under the mouse
//
void TextView::OnLButtonDblClick (int mx, int my, unsigned long button_flags, unsigned long key_flags)
{
  mx = mx - getBorder() - GetViewContentLeftMargin();
  my = my - getTopBorder() - GetViewContentTopMargin();

  // remove any existing selection
  InvalidateRange (m_nSelectionStart, m_nSelectionEnd);

  // regular mouse input - mouse is within scrolling viewport
  if (mx >= LeftMarginWidth() )
  {
    inl::t_u32 lineno, fileoff;
    //int   xpos;

    // map the mouse-coordinates to a real file-offset-coordinate
    MouseCoordToFilePos (mx, my, &lineno, &fileoff, &m_nCaretPosX, &m_CharacterAtCursor, &m_CursorCharacterPosition);
    m_nAnchorPosX = m_nCaretPosX;

    // move selection-start to start of word
    //-->MoveWordStart();
    m_nSelectionStart = m_nCursorOffset;

    // move selection-end to end of word
    //-->MoveWordEnd();
    m_nSelectionEnd = m_nCursorOffset;

    // update caret position
    InvalidateRange (m_nSelectionStart, m_nSelectionEnd);
    UpdateCaretOffset (m_nCursorOffset, TRUE, &m_nCaretPosX, &m_nCurrentLine);
    m_nAnchorPosX = m_nCaretPosX;

    //NotifyParent(TVN_CURSOR_CHANGE);
  }
}

//
//	WM_MOUSEMOVE
//
//	Set the selection end-point if we are dragging the mouse
//
void TextView::RecvMouseMove (int mx, int my, int dx, int dy, unsigned long button_flags, unsigned long key_flags /*UINT nFlags, int mx, int my*/)
{
  mx = mx - getBorder() - GetViewContentLeftMargin();
  my = my - getTopBorder() - GetViewContentTopMargin();

  if (m_nSelectionMode)
  {
    inl::t_u32	nLineNo, nFileOff;
    bool	fCurChanged = FALSE;

    Geometry	rect;
    inl::Point pt (mx, my);
    //int		cx;					// caret coordinates

    //
    //	First thing we must do is switch from margin-mode to normal-mode
    //	if the mouse strays into the main document area
    //
    if ( (m_nSelectionMode == SEL_MARGIN) && (mx > LeftMarginWidth() ) )
    {
      m_nSelectionMode = SEL_NORMAL;
      GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_IBEAM) );
    }

    //
    //	Mouse-scrolling: detect if the mouse
    //	is inside/outside of the TextView scrolling area
    //  and stop/start a scrolling timer appropriately
    //
    //GetClientRect(m_hWnd, &rect);

    rect = GetTextAreaGeometry();

    // build the scrolling area
    rect.OffsetPosition (LeftMarginWidth(), 0);
    rect.OffsetSize (-LeftMarginWidth(), -rect.GetHeight() % m_nLineHeight);


    // If mouse is within this area, we don't need to scroll
    if (rect.IsPointInside (pt.GetX(), pt.GetY() ) )
    {
      if (MouseAutoScrollHandle != 0)
      {
        GetThreadTimer()->RemoveTimerHandler (MouseAutoScrollHandle);
        MouseAutoScrollHandle = 0;
      }
    }
    // If mouse is outside window, start a timer in
    // order to generate regular scrolling intervals
    else
    {
      if (m_nScrollTimer == 0)
      {
        m_nScrollCounter = 0;
        MouseAutoScrollHandle = GetThreadTimer()->AddTimerHandler (10, MouseAutoScrollTimer, this);
        inlDebugMsg (TEXT ("Here") );
      }
    }

    m_CharacterAtCursor = 0;
    m_CursorCharacterPosition = 0;
    // get new cursor offset+coordinates
    MouseCoordToFilePos (mx, my, &nLineNo, &nFileOff, &m_nCaretPosX, &m_CharacterAtCursor, &m_CursorCharacterPosition);
    m_nAnchorPosX = m_nCaretPosX;

    m_cpBlockEnd.line = nLineNo;
    m_cpBlockEnd.xpos = mx + m_nHScrollPos * m_nFontWidth - LeftMarginWidth();//m_nCaretPosX;


    // redraw the old and new lines if they are different
    UpdateLine (nLineNo);

    // update the region of text that has changed selection state
    fCurChanged = m_nSelectionEnd == nFileOff ? FALSE : TRUE;
    //if(m_nSelectionEnd != nFileOff)
    {
      inl::t_u32 linelen;
      m_pTextDoc->lineinfo_from_lineno (nLineNo, 0, &linelen, 0, 0);

      m_nCursorOffset	= nFileOff;

      if (m_nSelectionMode == SEL_MARGIN)
      {
        if (nFileOff >= m_nSelectionStart)
        {
          nFileOff += linelen;
          m_nSelectionStart = m_nSelMarginOffset1;
        }
        else
        {
          m_nSelectionStart = m_nSelMarginOffset2;
        }
      }

      // redraw from old selection-pos to new position
      InvalidateRange (m_nSelectionEnd, nFileOff);

      // adjust the cursor + selection to the new offset
      m_nSelectionEnd = nFileOff;
    }

    // always set the caret position because we might be scrolling
    UpdateCaretXY (m_nCaretPosX, nLineNo);

    NeedSoftRedraw();
  }
  // mouse isn't being used for a selection, so set the cursor instead
  else
  {

    Geometry	rect = GetTextAreaGeometry();

    if (mx >= 0 && mx < LeftMarginWidth() )
    {
      GetThreadGLWindow()->SetWindowCursor (m_hMarginCursor);
    }
    else if (rect.IsPointInside (mx + getBorder(), my + getTopBorder() ) )
      GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_IBEAM) );
    else
    {
      GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_ARROW) );
    }
  }
}

void TextView::RecvMouseEnter (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  x = x - getBorder() - GetViewContentLeftMargin();
  y = y - getTopBorder() - GetViewContentTopMargin();

  Geometry	rect = GetTextAreaGeometry();

  if (x >= 0 && x < LeftMarginWidth() )
  {
#if defined (INL_OS_WINDOWS)
    GetThreadGLWindow()->SetWindowCursor (m_hMarginCursor);
#endif
  }
  else if (rect.IsPointInside (x + getBorder(), y + getTopBorder() ) )
  {
#if defined (INL_OS_WINDOWS)
    GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_IBEAM) );
#endif
  }
  else
  {
#if defined (INL_OS_WINDOWS)
    GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_ARROW) );
#endif
  }
}

void TextView::RecvMouseLeave (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  x = x - getBorder() - GetViewContentLeftMargin();
  y = y - getTopBorder() - GetViewContentTopMargin();

  GetThreadGLWindow()->SetWindowCursor (LoadCursor (0, IDC_ARROW) );
}


void TextView::MouseAutoScrollHandler (void *v)
{
  MouseAutoScrollHandle = GetThreadTimer()->AddTimerHandler (10, MouseAutoScrollTimer, this);
}

//
//	WM_TIMER handler
//
//	Used to create regular scrolling
//
void TextView::RecvTimer (void *v)
{
  int	  dx = 0, dy = 0;	// scrolling vectors
  Geometry  rect;

  // get the mouse's client-coordinates
  inl::Point point = GetThreadGLWindow()->GetMouseWindowCoord();

  // find client area, but make it an even no. of lines
  rect = GetTextAreaGeometry();
  rect.OffsetPosition (LeftMarginWidth(), 0);
  rect.OffsetSize (-LeftMarginWidth(), -rect.GetHeight() % m_nLineHeight);

  //
  // scrolling up / down??
  //
  if (point.Y() < rect.GetY() )
  {
    dy = ScrollDir (m_nScrollCounter, point.Y() - rect.GetY() );
    ScrollUp (1, 2 /*m_nLineHeight*/);
  }
  else if (point.Y() >= rect.GetY() + rect.GetHeight() )
  {
    dy = ScrollDir (m_nScrollCounter, point.Y() - (rect.GetY() + rect.GetHeight() ) );
    ScrollDown (1, 2 /*m_nLineHeight*/);
  }

  //
  // scrolling left / right?
  //
  if (point.X() < rect.GetX() )
  {
    dx = ScrollDir (m_nScrollCounter, point.X() - rect.GetX() );
    ScrollLeft (1, 2);
  }
  else if (point.X() > rect.GetX() + rect.GetWidth() )
  {
    dx = ScrollDir (m_nScrollCounter, point.X() - (rect.GetX() + rect.GetWidth() ) );
    ScrollRight (1, 2);
  }

  //
  // do the redraw now that the selection offsets are all
  // pointing to the right places and the scroll positions are valid.
  //
  if ( (dy != 0) /*(m_nSelectionMode != SEL_MARGIN)*/ /*hrgnUpdate != NULL*/)
  {
    // We perform a "fake" WM_MOUSEMOVE for two reasons:
    //
    // 1. To get the cursor/caret/selection offsets set to the correct place
    //    *before* we redraw (so everything is synchronized correctly)
    //
    // 2. To invalidate any areas due to mouse-movement which won't
    //    get done until the next WM_MOUSEMOVE - and then it would
    //    be too late because we need to redraw *now*
    //
    RecvMouseMove (point.GetX(), point.GetY(), 0, 0, 0, 0);

  }

  // keep track of how many WM_TIMERs we process because
  // we might want to skip the next one
  m_nScrollCounter++;
}

//
//	Convert mouse(client) coordinates to a file-relative offset
//
//	Currently only uses the main font so will not support other
//	fonts introduced by syntax highlighting
//
bool TextView::MouseCoordToFilePos (	int		 mx,           // [in]  mouse x-coord
                                      int		 my,            // [in]  mouse x-coord
                                      inl::t_u32	*pnLineNo,      // [out] line number
                                      inl::t_u32	*pnFileOffset,  // [out] zero-based file-offset (in chars)
                                      int		*psnappedX,     // [out] adjusted x coord of caret
                                      TCHAR   *Character,
                                      inl::t_u32   *CharacterPosition
                                   )
{
  inl::t_u32 nLineNo;
  inl::t_u32 off_chars;
  Geometry  rect;
  inl::t_u32	  cp;

  // Get scrollable area
  rect = GetTextAreaGeometry();
  // Compute a rectangle that contains full lines (no partial lines)
  rect.OffsetSize (0, -rect.GetHeight() % m_nLineHeight);

  // Take left margin into account
  mx -= LeftMarginWidth();

  // Clip mouse to edge of window
  if (mx < 0)
    mx = 0;

  if (my < 0)
    my = 0;

  if (my >= rect.GetY() + rect.GetHeight() )
    my = rect.GetY() + rect.GetHeight() - 1;

  if (mx >= rect.GetX() + rect.GetWidth() )
    mx = rect.GetX() + rect.GetWidth()  - 1;

  // It's easy to find the line-number: just divide 'y' by the line-height
  nLineNo = (my / m_nLineHeight) + m_nVScrollPos;

  // make sure we don't go outside of the document
  if (nLineNo >= m_nLineCount)
  {
    nLineNo   = m_nLineCount ? m_nLineCount - 1 : 0;
    off_chars = m_pTextDoc->size();
  }

  mx += m_nHScrollPos * m_nFontWidth;

  // get the USPDATA object for the selected line!!
  //USPDATA *uspData = GetUspData(0, nLineNo);

  // convert mouse-x coordinate to a character-offset relative to start of line
  UspSnapXToOffset (nLineNo, mx, &mx, &cp, 0);

  // return coords!
  TextIterator itor = m_pTextDoc->iterate_line (nLineNo, &off_chars);

  if (pnLineNo)
    *pnLineNo		= nLineNo;

  if (pnLineNo)
    *pnFileOffset	= cp + off_chars;

  if (pnLineNo)
    *psnappedX		= mx;// - m_nHScrollPos * m_nFontWidth;

  //*psnappedX		+= LeftMarginWidth();

  if (CharacterPosition)
    *CharacterPosition = cp;

  // now find the x-coordinate on the specified line
  if (Character)
  {
    TCHAR buf[TEXTBUFSIZE];
    inl::t_u32 len = 0;
    int offset = 0;

    while ( (len += itor.gettext (buf, TEXTBUFSIZE) ) > 0)
    {
      if (offset + len > *CharacterPosition)
      {
        *Character       = * (buf + *CharacterPosition);

        if (*Character < TEXT (' ') )
          *Character = 0;

        break;
      }

      offset += len;
    }
  }

  return 0;
}

LONG TextView::InvalidateLine (inl::t_u32 nLineNo)
{
  if (nLineNo >= m_nVScrollPos && nLineNo <= m_nVScrollPos + m_nWindowLines)
  {
    RECT rect;

    rect.top    = (nLineNo - m_nVScrollPos) * m_nLineHeight;
    rect.bottom = rect.top + m_nLineHeight;
  }

  return 0;
}

//
//	Redraw the specified range of text/data in the control
//
LONG TextView::InvalidateRange (inl::t_u32 nStart, inl::t_u32 nFinish)
{
  inl::t_u32 start  = std::min (nStart, nFinish);
  inl::t_u32 finish = std::max (nStart, nFinish);

  int   ypos;
  Geometry  rect;
  Geometry  client;
  TextIterator itor;

  // information about current line:
  inl::t_u32 lineno;
  inl::t_u32 off_chars;
  inl::t_u32 len_chars;
  client = GetTextAreaGeometry();

  // nothing to do?
  if (start == finish)
  {
    // We still need to clean the line in case the cursor was on it.
  }

  //
  //	Find the start-of-line information from specified file-offset
  //
  lineno = m_pTextDoc->lineno_from_offset (start);

  // clip to top of window
  if (lineno < m_nVScrollPos)
  {
    lineno = m_nVScrollPos;
    itor   = m_pTextDoc->iterate_line (lineno, &off_chars, &len_chars);
    start  = off_chars;
    AddDirtyLine (lineno);
    NeedSoftRedraw();
  }
  else
  {
    itor   = m_pTextDoc->iterate_line (lineno, &off_chars, &len_chars);
    AddDirtyLine (lineno);
    NeedSoftRedraw();
  }

  if (!itor || start >= finish)
    return 0;

  ypos = (lineno - m_nVScrollPos) * m_nLineHeight;


  // invalidate *whole* lines. don't care about flickering anymore because
  // all output is double-buffered now, and this method is much simpler
  while (itor && off_chars < finish)
  {
    // jump down to next line
    itor = m_pTextDoc->iterate_line (++lineno, &off_chars, &len_chars);
    ypos   += m_nLineHeight;
    AddDirtyLine (lineno);
    NeedSoftRedraw();
  }

  return 0;
}

//
//	Wrapper around SetCaretPos, hides the caret when it goes
//  off-screen (in case x/y wrap around)
//
void TextView::MoveCaret (int xpos, inl::t_u32 lineno)
{
  bool visible = false;

  // convert x-coord to window-relative
  // xpos -= m_nHScrollPos * m_nFontWidth;
  // xpos += LeftMarginWidth();

  if (lineno >= m_nVScrollPos && lineno <= m_nVScrollPos + m_nWindowLines)
  {
    if (xpos >= LeftMarginWidth() )
      visible = true;
  }

  // hide caret if it was previously visible
  if (visible == false && m_fHideCaret == false)
  {
    m_fHideCaret = true;
    //HideCaret(m_hWnd);
  }
  // show caret if it was previously hidden
  else if (visible == true && m_fHideCaret == true)
  {
    m_fHideCaret = false;
    //ShowCaret(m_hWnd);
  }

  if (m_fHideCaret == false)
    m_CaretPosition.Set (xpos + getBorder() + GetViewContentLeftMargin(), (lineno - m_nVScrollPos) * m_nLineHeight + getTopBorder() + GetViewContentTopMargin() );

  m_RedrawCaret = true;
}

//
//	x		- x-coord relative to start of line
//	lineno	- line-number
//
void TextView::UpdateCaretXY (int xpos, inl::t_u32 lineno)
{
  bool visible = false;

  // convert x-coord to window-relative
  xpos -= m_nHScrollPos * m_nFontWidth;
  xpos += LeftMarginWidth();

  // only show caret if it is visible within viewport
  if (lineno >= m_nVScrollPos && lineno <= m_nVScrollPos + m_nWindowLines)
  {
    if (xpos >= LeftMarginWidth() /* && (GetThreadGLWindow()->GetCurrentEvent().e_event != INL_MOUSE_PRESSED)*/)
      visible = true;
  }

  // hide caret if it was previously visible
  if (visible == false && m_fHideCaret == false)
  {
    m_fHideCaret = true;
    //HideCaret(m_hWnd);
  }
  // show caret if it was previously hidden
  else if (visible == true && m_fHideCaret == true)
  {
    m_fHideCaret = false;
    //ShowCaret(m_hWnd);
  }

  // set caret position if within window viewport
  if (m_fHideCaret == false)
  {
    m_CaretPosition.Set (xpos + getBorder() + GetViewContentLeftMargin(), (lineno - m_nVScrollPos) * m_nLineHeight + getTopBorder() + GetViewContentTopMargin() );
  }

  inlDebugMsg (TEXT ("Caret x: %d"), xpos + getBorder() + GetViewContentLeftMargin() );
  m_RedrawCaret = true;
}

//
//	Reposition the caret based on cursor-offset
//	return the resulting x-coord and line#
//
void TextView::UpdateCaretOffset (inl::t_u32 offset, bool fTrailing, int *outx, inl::t_u32 *outlineno)
{
  inl::t_u32		lineno = 0;
  int			xpos = 0;
  inl::t_u32		off_chars;
  //USPDATA	  * uspData;

  // get line information from cursor-offset
  if (m_pTextDoc->lineinfo_from_offset (offset, &lineno, &off_chars, 0, 0, 0) )
  {
    // locate the USPDATA for this line
    //if((uspData = GetUspData(NULL, lineno)) != 0)
    {
      // convert character-offset to x-coordinate
      off_chars = m_nCursorOffset - off_chars;

      if (fTrailing && off_chars > 0)
        UspOffsetToX (/*uspData,*/ lineno, off_chars - 1, TRUE, &xpos);
      else
        UspOffsetToX (/*uspData,*/ lineno, off_chars, FALSE, &xpos);

      // update caret position
      UpdateCaretXY (xpos, lineno);
    }
  }

  if (outx)	  *outx = xpos;

  if (outlineno) *outlineno = lineno;
}

void TextView::RepositionCaret()
{
  UpdateCaretXY (m_nCaretPosX, m_nCurrentLine);
}

void TextView::UpdateLine (inl::t_u32 nLineNo)
{
  m_nPreviousLine = m_nCurrentLine;
  AddDirtyLine (m_nCurrentLine);

  // redraw the old and new lines if they are different
  if (m_nCurrentLine != nLineNo)
  {
    if (CheckStyle (TXS_HIGHLIGHTCURLINE) )
      InvalidateLine (m_nCurrentLine);

    AddDirtyLine (nLineNo);
    m_nCurrentLine = nLineNo;

    if (CheckStyle (TXS_HIGHLIGHTCURLINE) )
      InvalidateLine (m_nCurrentLine);
  }
}

void TextView::AddDirtyLine (inl::t_u32 nLineNo)
{
  static int n = 0;
  std::vector<int>::iterator it;
  it = find (m_DirtyLines.begin(), m_DirtyLines.end(), nLineNo);

  if (it == m_DirtyLines.end() )
  {
    //inlDebugMsg(TEXT("Set Dirty Line (%d): %d"), n, nLineNo);
    ++n;
    m_DirtyLines.push_back (nLineNo);
  }
}

//
//	return direction to scroll (+ve, -ve or 0) based on
//  distance of mouse from window edge
//
//	note: counter now redundant, we scroll multiple lines at
//  a time (with a slower timer than before) to achieve
//	variable-speed scrolling
//
int ScrollDir (int counter, int distance)
{
  if (distance > 48)		return 5;

  if (distance > 16)		return 2;

  if (distance > 3)		return 1;

  if (distance > 0)		return counter % 5 == 0 ? 1 : 0;

  if (distance < -48)		return -5;

  if (distance < -16)		return -2;

  if (distance < -3)		return -1;

  if (distance < 0)		return counter % 5 == 0 ? -1 : 0;

  return 0;
}