~haggai-eran/nux/rtl-rebased

« back to all changes in this revision

Viewing changes to Nux/KeyboardHandler.cpp

  • Committer: Jay Taoko
  • Date: 2011-10-21 23:49:15 UTC
  • mfrom: (508.1.2 nux-20)
  • Revision ID: jay.taoko@canonical.com-20111021234915-hnzakb5ndebica8i
* Removed custom Nux types: t_u32, t_s32, t_bool, ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
namespace nux
30
30
{
31
31
 
32
 
  t_s32 BaseKeyboardHandler::s_jump_offset_at_borders = 60;
33
 
  t_s32 BaseKeyboardHandler::s_cursor_width = 4;
 
32
  int BaseKeyboardHandler::s_jump_offset_at_borders = 60;
 
33
  int BaseKeyboardHandler::s_cursor_width = 4;
34
34
 
35
35
  BaseKeyboardHandler::BaseKeyboardHandler()
36
36
  {
55
55
 
56
56
  void BaseKeyboardHandler::DeleteSelectionText()
57
57
  {
58
 
    t_s32 nFirst = Min(m_caret, m_selection_start);
59
 
    t_s32 nLast = Max(m_caret, m_selection_start);
 
58
    int nFirst = Min(m_caret, m_selection_start);
 
59
    int nLast = Max(m_caret, m_selection_start);
60
60
    // Update caret and selection
61
61
    PlaceCaret(nFirst);
62
62
    m_selection_start = m_caret;
63
63
 
64
64
    // Remove the characters
65
 
    for (t_s32 i = nFirst; i < nLast; ++i)
 
65
    for (int i = nFirst; i < nLast; ++i)
66
66
      m_textline.Erase(nFirst, 1);
67
67
  }
68
68
 
69
 
  void BaseKeyboardHandler::InsertChar(t_u32 character)
 
69
  void BaseKeyboardHandler::InsertChar(unsigned int character)
70
70
  {
71
71
 
72
72
  }
81
81
    m_textline.Clear();
82
82
  }
83
83
 
84
 
  void BaseKeyboardHandler::PlaceCaret(t_u32 nCP)
 
84
  void BaseKeyboardHandler::PlaceCaret(unsigned int nCP)
85
85
  {
86
 
    nuxAssert((nCP >= 0) && (nCP <= (t_u32) m_textline.Length()));
 
86
    nuxAssert((nCP >= 0) && (nCP <= (unsigned int) m_textline.Length()));
87
87
    m_previous_cursor_position = m_caret;
88
88
    m_caret = nCP;
89
89
  }
90
90
 
91
 
  t_u32 BaseKeyboardHandler::NextWordPosition(t_u32 cp)
 
91
  unsigned int BaseKeyboardHandler::NextWordPosition(unsigned int cp)
92
92
  {
93
 
    t_u32 num_char = (t_u32) m_textline.Length();
 
93
    unsigned int num_char = (unsigned int) m_textline.Length();
94
94
 
95
95
    if (cp == num_char)
96
96
      return cp;
97
97
 
98
 
    t_u32 c = cp + 1;
 
98
    unsigned int c = cp + 1;
99
99
 
100
100
    while (c < num_char - 1)
101
101
    {
108
108
    return num_char;
109
109
  }
110
110
 
111
 
  t_u32 BaseKeyboardHandler::PrevWordPosition(t_u32 cp)
 
111
  unsigned int BaseKeyboardHandler::PrevWordPosition(unsigned int cp)
112
112
  {
113
113
    if (cp == 0)
114
114
      return 0;
115
115
 
116
 
    t_u32 c = cp - 1;
 
116
    unsigned int c = cp - 1;
117
117
 
118
118
    while (c > 1)
119
119
    {
126
126
    return 0;
127
127
  }
128
128
 
129
 
  void BaseKeyboardHandler::MouseDown(t_s32 x, t_s32 y)
 
129
  void BaseKeyboardHandler::MouseDown(int x, int y)
130
130
  {
131
131
    ResolveCaretPosition(x, y);
132
132
    m_mouse_drag = true;
134
134
    m_entering_focus = false;
135
135
  }
136
136
 
137
 
  void BaseKeyboardHandler::MouseUp(t_s32 x, t_s32 y)
 
137
  void BaseKeyboardHandler::MouseUp(int x, int y)
138
138
  {
139
139
    m_mouse_drag = false;
140
140
    m_entering_focus = false;
141
141
  }
142
142
 
143
 
  void BaseKeyboardHandler::MouseDrag(t_s32 x, t_s32 y)
 
143
  void BaseKeyboardHandler::MouseDrag(int x, int y)
144
144
  {
145
145
    if (m_entering_focus)
146
146
      return;
154
154
    m_entering_focus = true;
155
155
  }
156
156
 
157
 
  void BaseKeyboardHandler::ResolveCaretPosition(t_s32 x, t_s32 y)
 
157
  void BaseKeyboardHandler::ResolveCaretPosition(int x, int y)
158
158
  {
159
159
    if (m_entering_focus)
160
160
      return;
162
162
    if (m_textline.Length() == 0)
163
163
      return;
164
164
 
165
 
    t_u32 StrLength = (t_u32) m_textline.Length();
166
 
    t_s32 total = m_text_positionx;
167
 
    t_u32 nCP = StrLength;
 
165
    unsigned int StrLength = (unsigned int) m_textline.Length();
 
166
    int total = m_text_positionx;
 
167
    unsigned int nCP = StrLength;
168
168
 
169
 
    for (t_u32 i = 0; i < StrLength; i++)
 
169
    for (unsigned int i = 0; i < StrLength; i++)
170
170
    {
171
 
      t_u32 cw0 = GetFont()->GetCharWidth(m_textline[i]);
172
 
      t_u32 cw1 = 0;
 
171
      unsigned int cw0 = GetFont()->GetCharWidth(m_textline[i]);
 
172
      unsigned int cw1 = 0;
173
173
 
174
174
      if (i+1 < StrLength)
175
175
      {
182
182
          nCP = i;
183
183
          break;
184
184
        }
185
 
        else if (x < total + (t_s32) cw0 / 2)
 
185
        else if (x < total + (int) cw0 / 2)
186
186
        {
187
187
          nCP = i;
188
188
          break;
189
189
        }
190
 
        else if (x < total + (t_s32) cw1 / 2)
 
190
        else if (x < total + (int) cw1 / 2)
191
191
        {
192
192
          // I don't quite understand why we need this???
193
193
          nCP = i + 1;
205
205
    }
206
206
  }
207
207
 
208
 
  void BaseKeyboardHandler::CaretAutoScroll(t_s32 x, t_s32 y, Geometry geo)
 
208
  void BaseKeyboardHandler::CaretAutoScroll(int x, int y, Geometry geo)
209
209
  {
210
210
    if (m_entering_focus)
211
211
      return;
213
213
    if (m_textline.Length() == 0)
214
214
      return;
215
215
 
216
 
    t_s32 StrLength = (t_s32) m_textline.Length();
 
216
    int StrLength = (int) m_textline.Length();
217
217
 
218
218
    //nuxDebugMsg("[BaseKeyboardHandler::ResolveCaretPosition]");
219
219
    if (x < geo.x)
311
311
                // If we are in overwrite mode and there is already
312
312
                // a char at the caret's position, simply replace it.
313
313
                // Otherwise, we insert the char as normal.
314
 
                if ( !m_insert_mode && m_caret < (t_s32) m_textline.Length())
 
314
                if ( !m_insert_mode && m_caret < (int) m_textline.Length())
315
315
                {
316
316
                  m_textline[m_caret] = key;
317
317
                  PlaceCaret(m_caret + 1 );
320
320
                else
321
321
                {
322
322
                  // Insert the char
323
 
                  if ( m_caret <= (t_s32) m_textline.Length())
 
323
                  if ( m_caret <= (int) m_textline.Length())
324
324
                  {
325
325
                    m_textline.Insert(m_caret, 1, key);
326
326
                    PlaceCaret(m_caret + 1 );
334
334
              // If we are in overwrite mode and there is already
335
335
              // a char at the caret's position, simply replace it.
336
336
              // Otherwise, we insert the char as normal.
337
 
              if ( !m_insert_mode && m_caret < (t_s32) m_textline.Length())
 
337
              if ( !m_insert_mode && m_caret < (int) m_textline.Length())
338
338
              {
339
339
                m_textline[m_caret] = key;
340
340
                PlaceCaret(m_caret + 1 );
343
343
              else
344
344
              {
345
345
                // Insert the char
346
 
                if ( m_caret <= (t_s32) m_textline.Length())
 
346
                if ( m_caret <= (int) m_textline.Length())
347
347
                {
348
348
                  m_textline.Insert(m_caret, 1, key);
349
349
                  PlaceCaret(m_caret + 1 );
371
371
              // If we are in overwrite mode and there is already
372
372
              // a char at the caret's position, simply replace it.
373
373
              // Otherwise, we insert the char as normal.
374
 
              if (!m_insert_mode && (m_caret < (t_s32) m_textline.Length()))
 
374
              if (!m_insert_mode && (m_caret < (int) m_textline.Length()))
375
375
              {
376
376
                m_textline[m_caret] = key;
377
377
                PlaceCaret(m_caret + 1 );
380
380
              else
381
381
              {
382
382
                // Insert the char
383
 
                if ( m_caret <= (t_s32) m_textline.Length())
 
383
                if ( m_caret <= (int) m_textline.Length())
384
384
                {
385
385
                  m_textline.Insert(m_caret, 1, key);
386
386
                  PlaceCaret(m_caret + 1 );
393
393
              // If we are in overwrite mode and there is already
394
394
              // a char at the caret's position, simply replace it.
395
395
              // Otherwise, we insert the char as normal.
396
 
              if (!m_insert_mode && (m_caret < (t_s32) m_textline.Length()))
 
396
              if (!m_insert_mode && (m_caret < (int) m_textline.Length()))
397
397
              {
398
398
                m_textline[m_caret] = key;
399
399
                PlaceCaret(m_caret + 1 );
402
402
              else
403
403
              {
404
404
                // Insert the char
405
 
                if (m_caret <= (t_s32) m_textline.Length())
 
405
                if (m_caret <= (int) m_textline.Length())
406
406
                {
407
407
                  m_textline.Insert(m_caret, 1, key);
408
408
                  PlaceCaret(m_caret + 1 );
428
428
            // If we are in overwrite mode and there is already
429
429
            // a char at the caret's position, simply replace it.
430
430
            // Otherwise, we insert the char as normal.
431
 
            if (!m_insert_mode && (m_caret < (t_s32) m_textline.Length()))
 
431
            if (!m_insert_mode && (m_caret < (int) m_textline.Length()))
432
432
            {
433
433
              m_textline[m_caret] = key;
434
434
              PlaceCaret(m_caret + 1 );
437
437
            else
438
438
            {
439
439
              // Insert the char
440
 
              if (m_caret <= (t_s32) m_textline.Length())
 
440
              if (m_caret <= (int) m_textline.Length())
441
441
              {
442
442
                m_textline.Insert(m_caret, 1, key);
443
443
                PlaceCaret(m_caret + 1 );
463
463
            // If we are in overwrite mode and there is already
464
464
            // a char at the caret's position, simply replace it.
465
465
            // Otherwise, we insert the char as normal.
466
 
            if (!m_insert_mode && (m_caret < (t_s32) m_textline.Length()))
 
466
            if (!m_insert_mode && (m_caret < (int) m_textline.Length()))
467
467
            {
468
468
              m_textline[m_caret] = key;
469
469
              PlaceCaret(m_caret + 1 );
472
472
            else
473
473
            {
474
474
              // Insert the char
475
 
              if (m_caret <= (t_s32) m_textline.Length())
 
475
              if (m_caret <= (int) m_textline.Length())
476
476
              {
477
477
                m_textline.Insert(m_caret, 1, key);
478
478
                PlaceCaret(m_caret + 1 );
497
497
            // If we are in overwrite mode and there is already
498
498
            // a char at the caret's position, simply replace it.
499
499
            // Otherwise, we insert the char as normal.
500
 
            if (!m_insert_mode && (m_caret < (t_s32) m_textline.Length()))
 
500
            if (!m_insert_mode && (m_caret < (int) m_textline.Length()))
501
501
            {
502
502
              m_textline[m_caret] = key;
503
503
              PlaceCaret(m_caret + 1 );
506
506
            else
507
507
            {
508
508
              // Insert the char
509
 
              if (m_caret <= (t_s32) m_textline.Length())
 
509
              if (m_caret <= (int) m_textline.Length())
510
510
              {
511
511
                m_textline.Insert(m_caret, 1, key);
512
512
                PlaceCaret(m_caret + 1 );
538
538
#elif defined(NUX_OS_LINUX)
539
539
      NString s = "Paste not implemented yet";
540
540
#endif
541
 
      t_u32 start = GetTextSelectionStart();
542
 
      t_u32 end = GetTextSelectionEnd();
 
541
      unsigned int start = GetTextSelectionStart();
 
542
      unsigned int end = GetTextSelectionEnd();
543
543
      m_textline.Replace(start, end - start, s.m_string);
544
544
 
545
 
      m_caret = start + (t_u32) s.Length();
 
545
      m_caret = start + (unsigned int) s.Length();
546
546
      UnselectAllText();
547
547
    }
548
548
 
585
585
      else
586
586
      {
587
587
        // Deleting one character
588
 
        if (m_caret < (t_s32) m_textline.Length())
 
588
        if (m_caret < (int) m_textline.Length())
589
589
        {
590
590
          m_textline.Erase(m_caret, 1);
591
591
        }
637
637
          m_caret = NextWordPosition( m_caret);
638
638
          PlaceCaret(m_caret );
639
639
        }
640
 
        else if (m_caret < (t_s32) m_textline.Length())
 
640
        else if (m_caret < (int) m_textline.Length())
641
641
          PlaceCaret(m_caret + 1 );
642
642
 
643
643
        if ((state & NUX_STATE_SHIFT) == 0)
666
666
    {
667
667
      if ((state & NUX_STATE_SHIFT) == 0)
668
668
      {
669
 
        PlaceCaret((t_u32) m_textline.Length());
 
669
        PlaceCaret((unsigned int) m_textline.Length());
670
670
        // Shift is not down. Update selection
671
671
        // start along with the caret.
672
672
        m_selection_start = m_caret;
673
673
      }
674
674
      else
675
675
      {
676
 
        PlaceCaret((t_u32) m_textline.Length());
 
676
        PlaceCaret((unsigned int) m_textline.Length());
677
677
      }
678
678
    }
679
679
    else if (virtual_code == NUX_VK_ESCAPE)
695
695
    }
696
696
    else if (virtual_code == NUX_VK_END)
697
697
    {
698
 
      t_u32 str_width = GetFont()->GetStringWidth(m_textline.GetTCharPtr());
 
698
      unsigned int str_width = GetFont()->GetStringWidth(m_textline.GetTCharPtr());
699
699
 
700
 
      if (str_width + s_cursor_width > (t_u32) m_clip_region.GetWidth())
 
700
      if (str_width + s_cursor_width > (unsigned int) m_clip_region.GetWidth())
701
701
        m_text_positionx = m_clip_region.GetWidth() - (str_width + s_cursor_width);
702
702
      else
703
703
        m_text_positionx = 0;
719
719
 
720
720
  void BaseKeyboardHandler::AdjustCursorAndTextPosition()
721
721
  {
722
 
    t_s32 str_width = GetFont()->GetStringWidth(m_textline.GetTCharPtr());
 
722
    int str_width = GetFont()->GetStringWidth(m_textline.GetTCharPtr());
723
723
    NString temp0;
724
724
 
725
725
    if (m_caret > 0)
738
738
 
739
739
    NString temp1 = m_textline.GetSubString(0, m_caret).GetTStringRef();
740
740
    NString temp2 = m_textline.GetSubString(0, m_caret + 1).GetTStringRef();
741
 
    t_s32 str_width0 = GetFont()->GetStringWidth(temp0);
742
 
    t_s32 str_width1 = GetFont()->GetStringWidth(temp1);
743
 
    t_s32 str_width2 = GetFont()->GetStringWidth(temp2);
 
741
    int str_width0 = GetFont()->GetStringWidth(temp0);
 
742
    int str_width1 = GetFont()->GetStringWidth(temp1);
 
743
    int str_width2 = GetFont()->GetStringWidth(temp2);
744
744
 
745
745
 
746
746
    if ((m_text_positionx + str_width1 + s_cursor_width) > m_clip_region.GetWidth())
747
747
    {
748
748
      // Hitting the end of the text box
749
 
      if (m_caret == (t_s32) m_textline.Length())
 
749
      if (m_caret == (int) m_textline.Length())
750
750
      {
751
 
        m_text_positionx = - (str_width + (t_s32) s_cursor_width - m_clip_region.GetWidth());
 
751
        m_text_positionx = - (str_width + (int) s_cursor_width - m_clip_region.GetWidth());
752
752
      }
753
753
      else
754
754
      {
755
 
        t_s32 PreviousCharWidth = str_width1 - str_width0;
756
 
        t_s32 Offset = Min<t_s32> (s_jump_offset_at_borders, str_width + s_cursor_width - str_width1);
 
755
        int PreviousCharWidth = str_width1 - str_width0;
 
756
        int Offset = Min<int> (s_jump_offset_at_borders, str_width + s_cursor_width - str_width1);
757
757
 
758
758
        while (Offset > m_clip_region.GetWidth())
759
759
        {
765
765
 
766
766
        m_text_positionx -= Offset;
767
767
 
768
 
        if (m_text_positionx + str_width + s_cursor_width < (t_s32) m_clip_region.GetWidth())
 
768
        if (m_text_positionx + str_width + s_cursor_width < (int) m_clip_region.GetWidth())
769
769
        {
770
 
          m_text_positionx = - (str_width + (t_s32) s_cursor_width - m_clip_region.GetWidth());
 
770
          m_text_positionx = - (str_width + (int) s_cursor_width - m_clip_region.GetWidth());
771
771
        }
772
772
      }
773
773
    }
780
780
      }
781
781
      else
782
782
      {
783
 
        t_s32 NextCharWidth = str_width2 - str_width1;
784
 
        t_s32 Offset = Min<t_s32> (s_jump_offset_at_borders, str_width1);
 
783
        int NextCharWidth = str_width2 - str_width1;
 
784
        int Offset = Min<int> (s_jump_offset_at_borders, str_width1);
785
785
 
786
786
        while (Offset > m_clip_region.GetWidth())
787
787
        {
814
814
  }
815
815
  void BaseKeyboardHandler::MoveCursorAtEnd()
816
816
  {
817
 
    t_u32 StrLength = ( t_u32) m_textline.Length();
 
817
    unsigned int StrLength = ( unsigned int) m_textline.Length();
818
818
    m_previous_cursor_position = m_caret;
819
819
    m_caret = StrLength;
820
820
  }
835
835
    // Every time we set the text, we reposition the cursor at the beginning of the text,
836
836
    // and the text position is set to zero with regard to the start of the geometry area.
837
837
    MoveCursorAtStart();
838
 
    m_selection_start = Min<t_s32> (m_selection_start, (t_s32) StringLength(str));
839
 
    m_caret = Min<t_s32> (m_caret, (t_s32) StringLength(str));
 
838
    m_selection_start = Min<int> (m_selection_start, (int) StringLength(str));
 
839
    m_caret = Min<int> (m_caret, (int) StringLength(str));
840
840
 
841
841
    if (m_caret < m_selection_start)
842
 
      m_selection_start = Max<t_s32> (m_selection_start, (t_s32) StringLength(str));
 
842
      m_selection_start = Max<int> (m_selection_start, (int) StringLength(str));
843
843
    else if (m_caret > m_selection_start)
844
 
      m_caret = Max<t_s32> (m_caret, (t_s32) StringLength(str));
 
844
      m_caret = Max<int> (m_caret, (int) StringLength(str));
845
845
 
846
846
    m_text_positionx = 0;
847
847
  }
861
861
    m_clip_region = g;
862
862
  }
863
863
 
864
 
  void BaseKeyboardHandler::GetTextSelection(t_s32 *start, t_s32 *end) const
 
864
  void BaseKeyboardHandler::GetTextSelection(int *start, int *end) const
865
865
  {
866
866
    *start = Min(m_selection_start, m_caret);
867
867
    *end = Max(m_selection_start, m_caret);
868
868
  }
869
869
 
870
 
  t_s32 BaseKeyboardHandler::GetTextSelectionStart() const
 
870
  int BaseKeyboardHandler::GetTextSelectionStart() const
871
871
  {
872
872
    return Min(m_selection_start, m_caret);
873
873
  }
874
874
 
875
 
  t_s32 BaseKeyboardHandler::GetTextSelectionEnd() const
 
875
  int BaseKeyboardHandler::GetTextSelectionEnd() const
876
876
  {
877
877
    return Max(m_selection_start, m_caret);
878
878
  }
880
880
  void BaseKeyboardHandler::SelectAllText()
881
881
  {
882
882
    m_selection_start = 0;
883
 
    m_caret = (t_u32) m_textline.Length();
 
883
    m_caret = (unsigned int) m_textline.Length();
884
884
    AdjustCursorAndTextPosition();
885
885
  }
886
886