]> git.saurik.com Git - wxWidgets.git/blame - src/x11/textctrl.cpp
Applied patch [ 1207531 ] Fix for wxCalendarCtrl being UNUSABLE with some languages
[wxWidgets.git] / src / x11 / textctrl.cpp
CommitLineData
4175e952
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
4175e952
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4175e952
RR
11#pragma implementation "textctrl.h"
12#endif
13
14#include "wx/textctrl.h"
47cd6610 15
4175e952
RR
16#include "wx/utils.h"
17#include "wx/intl.h"
18#include "wx/log.h"
19#include "wx/settings.h"
20#include "wx/panel.h"
21#include "wx/clipbrd.h"
22#include "wx/tokenzr.h"
ed39ff57 23#include "wx/dcclient.h"
4175e952
RR
24
25#include "wx/univ/inphand.h"
26#include "wx/univ/renderer.h"
27#include "wx/univ/colschem.h"
28#include "wx/univ/theme.h"
29
30//-----------------------------------------------------------------------------
31// helpers
32//-----------------------------------------------------------------------------
33
34wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type, int y1, int y2, wxTextCtrl *owner )
35{
36 m_type = type;
37 m_y1 = y1;
38 m_y2 = y2;
39 m_owner = owner;
7d8268a1 40
4175e952
RR
41 m_cursorX = m_owner->GetCursorX();
42 m_cursorY = m_owner->GetCursorY();
7d8268a1 43
4175e952
RR
44 if (m_type == wxSOURCE_UNDO_LINE)
45 {
46 m_text = m_owner->m_lines[m_y1].m_text;
47 } else
48 if (m_type == wxSOURCE_UNDO_ENTER)
49 {
50 m_text = m_owner->m_lines[m_y1].m_text;
51 } else
52 if (m_type == wxSOURCE_UNDO_BACK)
53 {
54 for (int i = m_y1; i < m_y2+2; i++)
55 {
56 if (i >= (int)m_owner->m_lines.GetCount())
2b5f62a0 57 m_lines.Add( wxT("") );
4175e952
RR
58 else
59 m_lines.Add( m_owner->m_lines[i].m_text );
60 }
61 } else
62 if (m_type == wxSOURCE_UNDO_DELETE)
63 {
64 for (int i = m_y1; i < m_y2+1; i++)
65 {
66 m_lines.Add( m_owner->m_lines[i].m_text );
67 }
68 } else
69 if (m_type == wxSOURCE_UNDO_PASTE)
70 {
71 m_text = m_owner->m_lines[m_y1].m_text;
72 }
73}
74
75void wxSourceUndoStep::Undo()
76{
77 if (m_type == wxSOURCE_UNDO_LINE)
78 {
79 m_owner->m_lines[m_y1].m_text = m_text;
80 m_owner->MoveCursor( m_cursorX, m_cursorY );
81 m_owner->RefreshLine( m_y1 );
82 } else
83 if (m_type == wxSOURCE_UNDO_ENTER)
84 {
85 m_owner->m_lines[m_y1].m_text = m_text;
86 m_owner->m_lines.RemoveAt( m_y1+1 );
87 m_owner->MoveCursor( m_cursorX, m_cursorY );
88 m_owner->RefreshDown( m_y1 );
89 } else
90 if (m_type == wxSOURCE_UNDO_BACK)
91 {
92 m_owner->m_lines[m_y1].m_text = m_lines[0];
93 m_owner->m_lines.Insert( new wxSourceLine( m_lines[1] ), m_y1+1 );
94 m_owner->MyAdjustScrollbars();
95 m_owner->MoveCursor( m_cursorX, m_cursorY );
96 m_owner->RefreshDown( m_y1 );
97 } else
98 if (m_type == wxSOURCE_UNDO_DELETE)
99 {
100 m_owner->m_lines[m_y1].m_text = m_lines[0];
101 for (int i = 1; i < (int)m_lines.GetCount(); i++)
102 m_owner->m_lines.Insert( new wxSourceLine( m_lines[i] ), m_y1+i );
103 m_owner->MyAdjustScrollbars();
104 m_owner->MoveCursor( m_cursorX, m_cursorY );
105 m_owner->RefreshDown( m_y1 );
106 } else
107 if (m_type == wxSOURCE_UNDO_PASTE)
108 {
109 m_owner->m_lines[m_y1].m_text = m_text;
110 for (int i = 0; i < m_y2-m_y1; i++)
111 m_owner->m_lines.RemoveAt( m_y1+1 );
112 m_owner->MyAdjustScrollbars();
113 m_owner->MoveCursor( m_cursorX, m_cursorY );
114 m_owner->RefreshDown( m_y1 );
115 } else
116 if (m_type == wxSOURCE_UNDO_INSERT_LINE)
117 {
118 m_owner->m_lines.RemoveAt( m_y1 );
119 m_owner->MyAdjustScrollbars();
120 m_owner->MoveCursor( 0, m_y1 );
121 m_owner->RefreshDown( m_y1 );
122 }
123}
124
125#include "wx/arrimpl.cpp"
126WX_DEFINE_OBJARRAY(wxSourceLineArray);
127
128//-----------------------------------------------------------------------------
129// wxTextCtrl
130//-----------------------------------------------------------------------------
131
132IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl)
133
134BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
135 EVT_PAINT(wxTextCtrl::OnPaint)
46e87167 136 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
4175e952
RR
137 EVT_CHAR(wxTextCtrl::OnChar)
138 EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse)
968602f8
JS
139 EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus)
140 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
7d8268a1 141
4175e952
RR
142 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
143 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
144 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
145 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
146 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
147
148 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
149 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
150 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
151 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
152 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
153END_EVENT_TABLE()
154
155void wxTextCtrl::Init()
156{
7d8268a1
WS
157 m_editable = true;
158 m_modified = false;
159
4175e952 160 m_lang = wxSOURCE_LANG_NONE;
7d8268a1
WS
161
162 m_capturing = false;
163
4175e952
RR
164 m_cursorX = 0;
165 m_cursorY = 0;
7d8268a1 166
4175e952 167 m_longestLine = 0;
7d8268a1 168
4175e952
RR
169 m_bracketX = -1;
170 m_bracketY = -1;
7d8268a1
WS
171
172 m_overwrite = false;
173 m_ignoreInput = false;
174
4175e952 175 ClearSelection();
7d8268a1 176
4175e952 177 m_keywordColour = wxColour( 10, 140, 10 );
7d8268a1 178
4175e952 179 m_defineColour = *wxRED;
7d8268a1 180
4175e952 181 m_variableColour = wxColour( 50, 120, 150 );
7d8268a1 182
4175e952 183 m_commentColour = wxColour( 130, 130, 130 );
7d8268a1 184
4175e952
RR
185 m_stringColour = wxColour( 10, 140, 10 );
186}
187
188wxTextCtrl::wxTextCtrl( wxWindow *parent,
189 wxWindowID id,
190 const wxString &value,
191 const wxPoint &pos,
192 const wxSize &size,
193 long style,
194 const wxValidator& validator,
195 const wxString &name )
196 : wxScrollHelper(this)
197{
198 Init();
199
200 Create( parent, id, value, pos, size, style, validator, name );
201}
202
d122f8c7
MB
203wxTextCtrl::~wxTextCtrl()
204{
205 WX_CLEAR_LIST(wxList, m_undos);
206}
207
4175e952
RR
208bool wxTextCtrl::Create( wxWindow *parent,
209 wxWindowID id,
210 const wxString &value,
211 const wxPoint &pos,
212 const wxSize &size,
213 long style,
214 const wxValidator& validator,
215 const wxString &name )
216{
217 if ((style & wxBORDER_MASK) == 0)
218 style |= wxBORDER_SUNKEN;
7d8268a1 219
4175e952
RR
220 if ((style & wxTE_MULTILINE) != 0)
221 style |= wxALWAYS_SHOW_SB;
7d8268a1 222
968602f8 223 wxTextCtrlBase::Create( parent, id, pos /* wxDefaultPosition */, size,
e441e1f4 224 style | wxVSCROLL | wxHSCROLL);
7d8268a1 225
4175e952 226 SetBackgroundColour( *wxWHITE );
7d8268a1 227
4175e952 228 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
7d8268a1 229
40de795f 230 m_editable = ((m_windowStyle & wxTE_READONLY) == 0);
7d8268a1 231
82434104
RR
232 if (HasFlag(wxTE_PASSWORD))
233 m_sourceFont = wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL );
234 else
235 m_sourceFont = GetFont();
236
237 wxClientDC dc(this);
238 dc.SetFont( m_sourceFont );
239 m_lineHeight = dc.GetCharHeight();
240 m_charWidth = dc.GetCharWidth();
7d8268a1 241
4175e952
RR
242 SetValue( value );
243
244 wxSize size_best( DoGetBestSize() );
245 wxSize new_size( size );
246 if (new_size.x == -1)
247 new_size.x = size_best.x;
248 if (new_size.y == -1)
249 new_size.y = size_best.y;
250 if ((new_size.x != size.x) || (new_size.y != size.y))
251 SetSize( new_size.x, new_size.y );
7d8268a1 252
4175e952
RR
253 // We create an input handler since it might be useful
254 CreateInputHandler(wxINP_HANDLER_TEXTCTRL);
7d8268a1 255
5d830139 256 MyAdjustScrollbars();
7d8268a1
WS
257
258 return true;
4175e952
RR
259}
260
261//-----------------------------------------------------------------------------
262// public methods
263//-----------------------------------------------------------------------------
264
265wxString wxTextCtrl::GetValue() const
266{
267 wxString ret;
268 for (size_t i = 0; i < m_lines.GetCount(); i++)
269 {
270 ret += m_lines[i].m_text;
cf76eeec 271 if (i+1 < m_lines.GetCount())
4175e952
RR
272 ret += wxT('\n');
273 }
7d8268a1 274
4175e952
RR
275 return ret;
276}
277
278void wxTextCtrl::SetValue(const wxString& value)
279{
7d8268a1 280 m_modified = false;
968602f8 281
68148776
JS
282 wxString oldValue = GetValue();
283
4175e952
RR
284 m_cursorX = 0;
285 m_cursorY = 0;
286 ClearSelection();
287 m_lines.Clear();
288 m_longestLine = 0;
ee1797f1 289
7d8268a1 290 if (value.empty())
ee1797f1
RR
291 {
292 m_lines.Add( new wxSourceLine( wxT("") ) );
293 }
294 else
4175e952 295 {
ee1797f1
RR
296 int begin = 0;
297 int pos = 0;
298 for (;;)
4175e952 299 {
ee1797f1
RR
300 pos = value.find( wxT('\n'), begin );
301 if (pos < 0)
302 {
82434104
RR
303 wxSourceLine *sl = new wxSourceLine( value.Mid( begin, value.Len()-begin ) );
304 m_lines.Add( sl );
7d8268a1 305
82434104
RR
306 // if (sl->m_text.Len() > m_longestLine)
307 // m_longestLine = sl->m_text.Len();
308 int ww = 0;
309 GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL );
310 ww /= m_charWidth;
311 if (ww > m_longestLine)
312 m_longestLine = ww;
7d8268a1 313
ee1797f1
RR
314 break;
315 }
316 else
317 {
82434104
RR
318 wxSourceLine *sl = new wxSourceLine( value.Mid( begin, pos-begin ) );
319 m_lines.Add( sl );
7d8268a1 320
82434104
RR
321 // if (sl->m_text.Len() > m_longestLine)
322 // m_longestLine = sl->m_text.Len();
323 int ww = 0;
324 GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL );
325 ww /= m_charWidth;
326 if (ww > m_longestLine)
327 m_longestLine = ww;
7d8268a1 328
ee1797f1
RR
329 begin = pos+1;
330 }
4175e952
RR
331 }
332 }
68148776
JS
333
334 // Don't need to refresh if the value hasn't changed
335 if ((GetWindowStyle() & wxTE_MULTILINE) == 0)
336 {
337 if (value == oldValue)
338 return;
339 }
7d8268a1 340
4175e952 341 MyAdjustScrollbars();
7d8268a1 342
4175e952
RR
343 Refresh();
344}
345
346int wxTextCtrl::GetLineLength(long lineNo) const
347{
348 if (lineNo >= (long)m_lines.GetCount())
349 return 0;
7d8268a1 350
4175e952
RR
351 return m_lines[lineNo].m_text.Len();
352}
353
354wxString wxTextCtrl::GetLineText(long lineNo) const
355{
356 if (lineNo >= (long)m_lines.GetCount())
357 return wxT("");
7d8268a1 358
4175e952
RR
359 return m_lines[lineNo].m_text;
360}
361
362int wxTextCtrl::GetNumberOfLines() const
363{
364 return m_lines.GetCount();
365}
366
367bool wxTextCtrl::IsModified() const
368{
369 return m_modified;
370}
371
372bool wxTextCtrl::IsEditable() const
373{
374 return m_editable;
375}
376
377void wxTextCtrl::GetSelection(long* from, long* to) const
378{
65c745fe
JS
379 if (m_selStartX == -1 || m_selStartY == -1 ||
380 m_selEndX == -1 || m_selEndY == -1)
381 {
382 *from = GetInsertionPoint();
383 *to = GetInsertionPoint();
384 }
385 else
386 {
387 *from = XYToPosition(m_selStartX, m_selStartY);
388 *to = XYToPosition(m_selEndX, m_selEndY);
389 }
4175e952
RR
390}
391
392void wxTextCtrl::Clear()
393{
7d8268a1 394 m_modified = true;
4175e952
RR
395 m_cursorX = 0;
396 m_cursorY = 0;
397 ClearSelection();
7d8268a1 398
4175e952 399 m_lines.Clear();
ee1797f1 400 m_lines.Add( new wxSourceLine( wxT("") ) );
7d8268a1 401
4175e952
RR
402 SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 );
403 Refresh();
d122f8c7 404 WX_CLEAR_LIST(wxList, m_undos);
4175e952
RR
405}
406
407void wxTextCtrl::Replace(long from, long to, const wxString& value)
408{
409}
410
411void wxTextCtrl::Remove(long from, long to)
412{
4175e952
RR
413}
414
415void wxTextCtrl::DiscardEdits()
416{
417 ClearSelection();
418 Refresh();
419}
420
421void wxTextCtrl::SetMaxLength(unsigned long len)
422{
423}
424
82434104
RR
425int wxTextCtrl::PosToPixel( int line, int pos )
426{
427 // TODO add support for Tabs
428
46e87167
RR
429 if (line >= (int)m_lines.GetCount()) return 0;
430 if (pos < 0) return 0;
7d8268a1 431
82434104 432 wxString text = m_lines[line].m_text;
7d8268a1
WS
433
434 if (text.empty()) return 0;
435
46e87167 436 if (pos < (int)text.Len())
82434104 437 text.Remove( pos, text.Len()-pos );
7d8268a1 438
82434104 439 int w = 0;
7d8268a1 440
82434104
RR
441 GetTextExtent( text, &w, NULL, NULL, NULL );
442
443 return w;
444}
445
446int wxTextCtrl::PixelToPos( int line, int pixel )
447{
448 if (pixel < 2) return 0;
7d8268a1 449
46e87167 450 if (line >= (int)m_lines.GetCount()) return 0;
7d8268a1 451
82434104 452 wxString text = m_lines[line].m_text;
7d8268a1 453
82434104
RR
454 int w = 0;
455 int res = text.Len();
456 while (res > 0)
457 {
458 GetTextExtent( text, &w, NULL, NULL, NULL );
7d8268a1 459
82434104
RR
460 if (w < pixel)
461 return res;
7d8268a1 462
82434104
RR
463 res--;
464 text.Remove( res,1 );
465 }
7d8268a1 466
82434104
RR
467 return 0;
468}
469
46e87167
RR
470void wxTextCtrl::SetLanguage( wxSourceLanguage lang )
471{
472 m_lang = lang;
7d8268a1 473
46e87167 474 m_keywords.Clear();
46e87167
RR
475}
476
4175e952
RR
477void wxTextCtrl::WriteText(const wxString& text2)
478{
7d8268a1
WS
479 if (text2.empty()) return;
480
481 m_modified = true;
ee1797f1 482
4175e952
RR
483 wxString text( text2 );
484 wxArrayString lines;
485 int pos;
486 while ( (pos = text.Find('\n')) != -1 )
487 {
488 lines.Add( text.Left( pos ) );
489 text.Remove( 0, pos+1 );
490 }
491 lines.Add( text );
492 int count = (int)lines.GetCount();
7d8268a1 493
4175e952
RR
494 wxString tmp1( m_lines[m_cursorY].m_text );
495 wxString tmp2( tmp1 );
496 int len = (int)tmp1.Len();
7d8268a1 497
4175e952
RR
498 if (len < m_cursorX)
499 {
500 wxString tmp;
501 for (int i = 0; i < m_cursorX-len; i++)
502 tmp.Append( ' ' );
503 m_lines[m_cursorY].m_text.Append( tmp );
504 tmp1.Append( tmp );
505 tmp2.Append( tmp );
506 }
507
508 tmp1.Remove( m_cursorX );
509 tmp2.Remove( 0, m_cursorX );
510 tmp1.Append( lines[0] );
7d8268a1 511
4175e952
RR
512 if (count == 1)
513 {
514 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) );
7d8268a1 515
4175e952
RR
516 tmp1.Append( tmp2 );
517 m_lines[m_cursorY].m_text = tmp1;
518 RefreshLine( m_cursorY );
519 }
520 else
521 {
522 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, m_cursorY, m_cursorY+count-1, this ) );
7d8268a1 523
4175e952
RR
524 m_lines[m_cursorY].m_text = tmp1;
525 int i;
526 for (i = 1; i < count; i++)
527 m_lines.Insert( new wxSourceLine( lines[i] ), m_cursorY+i );
528 m_lines[m_cursorY+i-1].m_text.Append( tmp2 );
7d8268a1 529
4175e952
RR
530 MyAdjustScrollbars();
531 RefreshDown( m_cursorY );
532 }
533}
534
ee1797f1 535void wxTextCtrl::AppendText(const wxString& text2)
4175e952 536{
7d8268a1
WS
537 if (text2.empty()) return;
538
539 m_modified = true;
4175e952 540
ee1797f1
RR
541 wxString text( text2 );
542 wxArrayString lines;
543 int pos;
544 while ( (pos = text.Find('\n')) != -1 )
545 {
546 lines.Add( text.Left( pos ) );
547 text.Remove( 0, pos+1 );
548 }
549 lines.Add( text );
550 int count = (int)lines.GetCount();
7d8268a1 551
ee1797f1
RR
552 size_t y = m_lines.GetCount()-1;
553
554 wxString tmp( m_lines[y].m_text );
555 tmp.Append( lines[0] );
7d8268a1 556
ee1797f1
RR
557 if (count == 1)
558 {
559 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, y, y, this ) );
7d8268a1 560
ee1797f1
RR
561 m_lines[y].m_text = tmp;
562 RefreshLine( y );
563 }
564 else
565 {
566 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, y, y+count-1, this ) );
7d8268a1 567
ee1797f1
RR
568 m_lines[y].m_text = tmp;
569 int i;
570 for (i = 1; i < count; i++)
571 m_lines.Insert( new wxSourceLine( lines[i] ), y+i );
7d8268a1 572
ee1797f1
RR
573 MyAdjustScrollbars();
574 RefreshDown( y );
575 }
4175e952
RR
576}
577
578bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
579{
7d8268a1 580 return false;
4175e952
RR
581}
582
583long wxTextCtrl::XYToPosition(long x, long y) const
584{
585 long ret = 0;
7d8268a1 586
4175e952
RR
587 for (size_t i = 0; i < m_lines.GetCount(); i++)
588 {
589 if (i < (size_t)y)
590 {
65c745fe
JS
591 // Add one for the end-of-line character
592 ret += m_lines[i].m_text.Len() + 1;
4175e952
RR
593 continue;
594 }
7d8268a1 595
65c745fe 596 if ((size_t)x < (m_lines[i].m_text.Len()+1))
4175e952
RR
597 return (ret + x);
598 else
65c745fe 599 return (ret + m_lines[i].m_text.Len() + 1);
4175e952 600 }
7d8268a1 601
4175e952
RR
602 return ret;
603}
604
605bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
606{
607 if (m_lines.GetCount() == 0)
608 {
609 if (x) *x = 0;
610 if (y) *y = 0;
611
612 return (pos == 0);
613 }
614
615 long xx = 0;
616 long yy = 0;
7d8268a1 617
4175e952
RR
618 for (size_t i = 0; i < m_lines.GetCount(); i++)
619 {
65c745fe
JS
620 //pos -= m_lines[i].m_text.Len();
621 //if (pos <= 0)
622
623 // Add one for the end-of-line character. (In Windows,
624 // there are _two_ positions for each end of line.)
625 if (pos <= ((int)m_lines[i].m_text.Len()))
4175e952 626 {
65c745fe 627 xx = pos;
4175e952
RR
628 if (x) *x = xx;
629 if (y) *y = yy;
7d8268a1 630 return true;
4175e952 631 }
65c745fe 632 pos -= (m_lines[i].m_text.Len() + 1);
4175e952
RR
633 yy++;
634 }
7d8268a1 635
4175e952 636 // Last pos
65c745fe
JS
637 //xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len();
638 xx = pos;
4175e952
RR
639 if (x) *x = xx;
640 if (y) *y = yy;
7d8268a1
WS
641
642 return false;
4175e952
RR
643}
644
645void wxTextCtrl::ShowPosition(long pos)
646{
647}
648
649void wxTextCtrl::Copy()
650{
651 if (!HasSelection()) return;
7d8268a1 652
4175e952 653 wxString sel;
7d8268a1 654
4175e952
RR
655 int selStartY = m_selStartY;
656 int selEndY = m_selEndY;
657 int selStartX = m_selStartX;
658 int selEndX = m_selEndX;
7d8268a1 659
4175e952
RR
660 if ((selStartY > selEndY) ||
661 ((selStartY == selEndY) && (selStartX > selEndX)))
662 {
663 int tmp = selStartX;
664 selStartX = selEndX;
665 selEndX = tmp;
666 tmp = selStartY;
667 selStartY = selEndY;
668 selEndY = tmp;
669 }
7d8268a1 670
4175e952
RR
671 if (selStartY == selEndY)
672 {
673 sel = m_lines[selStartY].m_text;
7d8268a1 674
4175e952
RR
675 if (selStartX >= (int)sel.Len()) return;
676 if (selEndX > (int)sel.Len())
677 selEndX = sel.Len();
7d8268a1 678
4175e952
RR
679 sel.Remove( selEndX, sel.Len()-selEndX );
680 sel.Remove( 0, selStartX );
681 }
682 else
683 {
684 wxString tmp( m_lines[selStartY].m_text );
7d8268a1 685
4175e952
RR
686 if (selStartX < (int)tmp.Len())
687 {
688 tmp.Remove( 0, selStartX );
689 sel = tmp;
2b5f62a0 690 sel.Append( wxT("\n") );
4175e952
RR
691 }
692 for (int i = selStartY+1; i < selEndY; i++)
693 {
694 sel.Append( m_lines[i].m_text );
2b5f62a0 695 sel.Append( wxT("\n") );
4175e952
RR
696 }
697 tmp = m_lines[selEndY].m_text;
698 if (selEndX > (int)tmp.Len())
699 selEndX = tmp.Len();
700 if (selEndX > 0)
701 {
702 tmp.Remove( selEndX, tmp.Len()-selEndX );
703 sel.Append( tmp );
704 }
705 }
7d8268a1 706
4175e952
RR
707 if (wxTheClipboard->Open())
708 {
709 wxTheClipboard->SetData( new wxTextDataObject( sel ) );
710 wxTheClipboard->Close();
711 }
712}
713
714void wxTextCtrl::Cut()
715{
7d8268a1
WS
716 Copy();
717
718 Delete();
4175e952
RR
719}
720
721void wxTextCtrl::Paste()
722{
723 Delete();
7d8268a1 724
4175e952 725 if (!wxTheClipboard->Open()) return;
7d8268a1 726
4175e952
RR
727 if (!wxTheClipboard->IsSupported( wxDF_TEXT ))
728 {
729 wxTheClipboard->Close();
7d8268a1 730
4175e952
RR
731 return;
732 }
7d8268a1 733
4175e952 734 wxTextDataObject data;
7d8268a1 735
4175e952 736 bool ret = wxTheClipboard->GetData( data );
7d8268a1 737
4175e952 738 wxTheClipboard->Close();
7d8268a1 739
4175e952 740 if (!ret) return;
7d8268a1
WS
741
742 m_modified = true;
743
4175e952
RR
744 wxString text( data.GetText() );
745 wxArrayString lines;
746 int pos;
747 while ( (pos = text.Find('\n')) != -1 )
748 {
749 lines.Add( text.Left( pos ) );
750 text.Remove( 0, pos+1 );
751 }
752 lines.Add( text );
753 int count = (int)lines.GetCount();
7d8268a1 754
4175e952
RR
755 wxString tmp1( m_lines[m_cursorY].m_text );
756 wxString tmp2( tmp1 );
757 int len = (int)tmp1.Len();
7d8268a1 758
4175e952
RR
759 if (len < m_cursorX)
760 {
761 wxString tmp;
762 for (int i = 0; i < m_cursorX-len; i++)
763 tmp.Append( ' ' );
764 m_lines[m_cursorY].m_text.Append( tmp );
765 tmp1.Append( tmp );
766 tmp2.Append( tmp );
767 }
768
769 tmp1.Remove( m_cursorX );
770 tmp2.Remove( 0, m_cursorX );
771 tmp1.Append( lines[0] );
7d8268a1 772
4175e952
RR
773 if (count == 1)
774 {
775 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) );
7d8268a1 776
4175e952
RR
777 tmp1.Append( tmp2 );
778 m_lines[m_cursorY].m_text = tmp1;
779 RefreshLine( m_cursorY );
780 }
781 else
782 {
783 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, m_cursorY, m_cursorY+count-1, this ) );
7d8268a1 784
4175e952
RR
785 m_lines[m_cursorY].m_text = tmp1;
786 int i;
787 for (i = 1; i < count; i++)
788 m_lines.Insert( new wxSourceLine( lines[i] ), m_cursorY+i );
789 m_lines[m_cursorY+i-1].m_text.Append( tmp2 );
7d8268a1 790
4175e952
RR
791 MyAdjustScrollbars();
792 RefreshDown( m_cursorY );
793 }
794}
795
796void wxTextCtrl::Undo()
797{
798 if (m_undos.GetCount() == 0) return;
7d8268a1 799
d122f8c7 800 wxList::compatibility_iterator node = m_undos.Item( m_undos.GetCount()-1 );
395cb311 801 wxSourceUndoStep *undo = (wxSourceUndoStep*) node->GetData();
7d8268a1 802
4175e952 803 undo->Undo();
d122f8c7
MB
804
805 delete undo;
806 m_undos.Erase( node );
7d8268a1
WS
807
808 m_modified = true;
4175e952
RR
809}
810
811void wxTextCtrl::SetInsertionPoint(long pos)
812{
65c745fe
JS
813 ClearSelection();
814 long x, y;
815 PositionToXY(pos, & x, & y);
816 m_cursorX = x;
817 m_cursorY = y;
818 // TODO: scroll to this position if necessary
819 Refresh();
4175e952
RR
820}
821
822void wxTextCtrl::SetInsertionPointEnd()
823{
65c745fe 824 SetInsertionPoint(GetLastPosition());
4175e952
RR
825}
826
827long wxTextCtrl::GetInsertionPoint() const
828{
829 return XYToPosition( m_cursorX, m_cursorY );
830}
831
7d8268a1 832wxTextPos wxTextCtrl::GetLastPosition() const
4175e952
RR
833{
834 size_t lineCount = m_lines.GetCount() - 1;
65c745fe
JS
835 // It's the length of the line, not the length - 1,
836 // because there's a position after the last character.
837 return XYToPosition( m_lines[lineCount].m_text.Len(), lineCount );
4175e952
RR
838}
839
840void wxTextCtrl::SetSelection(long from, long to)
841{
842}
843
844void wxTextCtrl::SetEditable(bool editable)
845{
846 m_editable = editable;
847}
848
849bool wxTextCtrl::Enable( bool enable )
850{
7d8268a1 851 return false;
4175e952
RR
852}
853
854bool wxTextCtrl::SetFont(const wxFont& font)
855{
40de795f 856 wxTextCtrlBase::SetFont( font );
7d8268a1 857
40de795f 858 m_sourceFont = font;
7d8268a1 859
40de795f
RR
860 wxClientDC dc(this);
861 dc.SetFont( m_sourceFont );
862 m_lineHeight = dc.GetCharHeight();
863 m_charWidth = dc.GetCharWidth();
7d8268a1 864
40de795f 865 // TODO: recalc longest lines
7d8268a1 866
40de795f 867 MyAdjustScrollbars();
7d8268a1
WS
868
869 return true;
4175e952
RR
870}
871
872bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
873{
874 return wxWindow::SetForegroundColour( colour );
875}
876
877bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
878{
879 return wxWindow::SetBackgroundColour( colour );
880}
881
882//-----------------------------------------------------------------------------
883// private code and handlers
884//-----------------------------------------------------------------------------
885
886void wxTextCtrl::SearchForBrackets()
887{
888 int oldBracketY = m_bracketY;
889 int oldBracketX = m_bracketX;
7d8268a1 890
4175e952 891 if (m_cursorY < 0 || m_cursorY >= (int)m_lines.GetCount()) return;
7d8268a1 892
4175e952 893 wxString current = m_lines[m_cursorY].m_text;
7d8268a1 894
4175e952
RR
895 // reverse search first
896
897 char bracket = ' ';
7d8268a1 898
4175e952 899 if (m_cursorX > 0)
f9c62cfc 900 bracket = current[(size_t) (m_cursorX-1)];
7d8268a1 901
4175e952
RR
902 if (bracket == ')' || bracket == ']' || bracket == '}')
903 {
904 char antibracket = '(';
905 if (bracket == ']') antibracket = '[';
906 if (bracket == '}') antibracket = '{';
7d8268a1 907
4175e952 908 int count = 1;
7d8268a1 909
4175e952
RR
910 int endY = m_cursorY-60;
911 if (endY < 0) endY = 0;
912 for (int y = m_cursorY; y >= endY; y--)
913 {
914 current = m_lines[y].m_text;
915 if (y == m_cursorY)
916 current.erase(m_cursorX-1,current.Len()-m_cursorX+1);
7d8268a1 917
4175e952
RR
918 for (int n = current.Len()-1; n >= 0; n--)
919 {
920 // ignore chars
8c3289da 921 if (current[(size_t) (n)] == '\'')
4175e952
RR
922 {
923 for (int m = n-1; m >= 0; m--)
924 {
8c3289da 925 if (current[(size_t) (m)] == '\'')
4175e952 926 {
f9c62cfc 927 if (m == 0 || current[(size_t) (m-1)] != '\\')
4175e952
RR
928 break;
929 }
930 n = m-1;
931 }
932 continue;
933 }
7d8268a1 934
4175e952 935 // ignore strings
8c3289da 936 if (current[(size_t) (n)] == '\"')
4175e952
RR
937 {
938 for (int m = n-1; m >= 0; m--)
939 {
8c3289da 940 if (current[(size_t) (m)] == '\"')
4175e952 941 {
f9c62cfc 942 if (m == 0 || current[(size_t) (m-1)] != '\\')
4175e952
RR
943 break;
944 }
945 n = m-1;
946 }
947 continue;
948 }
7d8268a1 949
8c3289da 950 if (current[(size_t) (n)] == antibracket)
4175e952
RR
951 {
952 count--;
953 if (count == 0)
954 {
955 m_bracketY = y;
956 m_bracketX = n;
957 if (oldBracketY != m_bracketY && oldBracketY != -1)
958 RefreshLine( oldBracketY );
959 if (m_bracketY != oldBracketY || m_bracketX != oldBracketX)
960 RefreshLine( m_bracketY );
961 return;
962 }
963 }
8c3289da 964 else if (current[(size_t) (n)] == bracket)
4175e952
RR
965 {
966 count++;
967 }
968 }
969 }
970 }
7d8268a1 971
4175e952
RR
972 // then forward
973
974 bracket = ' ';
975 if ((int)current.Len() > m_cursorX)
8c3289da 976 bracket = current[(size_t) (m_cursorX)];
4175e952
RR
977 if (bracket == '(' || bracket == '[' || bracket == '{')
978 {
979 char antibracket = ')';
980 if (bracket == '[') antibracket = ']';
981 if (bracket == '{') antibracket = '}';
7d8268a1 982
4175e952 983 int count = 1;
7d8268a1 984
4175e952
RR
985 int endY = m_cursorY+60;
986 if (endY > (int)(m_lines.GetCount()-1)) endY = m_lines.GetCount()-1;
987 for (int y = m_cursorY; y <= endY; y++)
988 {
989 current = m_lines[y].m_text;
990 int start = 0;
991 if (y == m_cursorY)
992 start = m_cursorX+1;
7d8268a1 993
4175e952
RR
994 for (int n = start; n < (int)current.Len(); n++)
995 {
996 // ignore chars
8c3289da 997 if (current[(size_t) (n)] == '\'')
4175e952
RR
998 {
999 for (int m = n+1; m < (int)current.Len(); m++)
1000 {
8c3289da 1001 if (current[(size_t) (m)] == '\'')
4175e952 1002 {
f9c62cfc 1003 if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\'))
4175e952
RR
1004 break;
1005 }
1006 n = m+1;
1007 }
1008 continue;
1009 }
7d8268a1 1010
4175e952 1011 // ignore strings
8c3289da 1012 if (current[(size_t) (n)] == '\"')
4175e952
RR
1013 {
1014 for (int m = n+1; m < (int)current.Len(); m++)
1015 {
8c3289da 1016 if (current[(size_t) (m)] == '\"')
4175e952 1017 {
f9c62cfc 1018 if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\'))
4175e952
RR
1019 break;
1020 }
1021 n = m+1;
1022 }
1023 continue;
1024 }
7d8268a1 1025
8c3289da 1026 if (current[(size_t) (n)] == antibracket)
4175e952
RR
1027 {
1028 count--;
1029 if (count == 0)
1030 {
1031 m_bracketY = y;
1032 m_bracketX = n;
1033 if (oldBracketY != m_bracketY && oldBracketY != -1)
1034 RefreshLine( oldBracketY );
1035 if (m_bracketY != oldBracketY || m_bracketX != oldBracketX)
1036 RefreshLine( m_bracketY );
1037 return;
1038 }
1039 }
8c3289da 1040 else if (current[(size_t) (n)] == bracket)
4175e952
RR
1041 {
1042 count++;
1043 }
1044 }
1045 }
1046 }
7d8268a1 1047
4175e952
RR
1048 if (oldBracketY != -1)
1049 {
1050 m_bracketY = -1;
1051 RefreshLine( oldBracketY );
1052 }
1053}
1054
1055void wxTextCtrl::Delete()
1056{
1057 if (!HasSelection()) return;
7d8268a1
WS
1058
1059 m_modified = true;
1060
4175e952
RR
1061 int selStartY = m_selStartY;
1062 int selEndY = m_selEndY;
1063 int selStartX = m_selStartX;
1064 int selEndX = m_selEndX;
7d8268a1 1065
4175e952
RR
1066 if ((selStartY > selEndY) ||
1067 ((selStartY == selEndY) && (selStartX > selEndX)))
1068 {
1069 int tmp = selStartX;
1070 selStartX = selEndX;
1071 selEndX = tmp;
1072 tmp = selStartY;
1073 selStartY = selEndY;
1074 selEndY = tmp;
1075 }
7d8268a1 1076
4175e952 1077 int len = (int)m_lines[selStartY].m_text.Len();
7d8268a1 1078
4175e952
RR
1079 if (selStartY == selEndY)
1080 {
1081 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, selStartY, selStartY, this ) );
7d8268a1 1082
4175e952
RR
1083 wxString tmp( m_lines[selStartY].m_text );
1084 if (selStartX < len)
1085 {
1086 if (selEndX > len)
1087 selEndX = len;
1088 tmp.Remove( selStartX, selEndX-selStartX );
1089 m_lines[selStartY].m_text = tmp;
1090 }
1091 ClearSelection();
1092 m_cursorX = selStartX;
1093 RefreshLine( selStartY );
1094 }
1095 else
1096 {
1097 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, selStartY, selEndY, this ) );
7d8268a1 1098
4175e952
RR
1099 if (selStartX < len)
1100 m_lines[selStartY].m_text.Remove( selStartX );
7d8268a1 1101
4175e952
RR
1102 for (int i = 0; i < selEndY-selStartY-1; i++)
1103 m_lines.RemoveAt( selStartY+1 );
7d8268a1 1104
4175e952
RR
1105 if (selEndX < (int)m_lines[selStartY+1].m_text.Len())
1106 m_lines[selStartY+1].m_text.Remove( 0, selEndX );
1107 else
1108 m_lines[selStartY+1].m_text.Remove( 0 );
7d8268a1 1109
4175e952
RR
1110 m_lines[selStartY].m_text.Append( m_lines[selStartY+1].m_text );
1111 m_lines.RemoveAt( selStartY+1 );
7d8268a1 1112
4175e952
RR
1113 ClearSelection();
1114 MoveCursor( selStartX, selStartY );
1115 MyAdjustScrollbars();
7d8268a1 1116
4175e952
RR
1117 RefreshDown( selStartY );
1118 }
1119}
1120
1121void wxTextCtrl::DeleteLine()
1122{
1123 if (HasSelection()) return;
7d8268a1 1124
4175e952 1125 if (m_cursorY < 0 || m_cursorY >= (int)m_lines.GetCount()-1) return; // TODO
7d8268a1 1126
4175e952 1127 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, m_cursorY, m_cursorY+1, this ) );
7d8268a1 1128
4175e952
RR
1129 m_lines.RemoveAt( m_cursorY );
1130 m_cursorX = 0;
1131 if (m_cursorY >= (int)m_lines.GetCount()) m_cursorY--;
7d8268a1 1132
4175e952
RR
1133 MyAdjustScrollbars();
1134 RefreshDown( m_cursorY );
1135}
1136
1137void wxTextCtrl::DoChar( char c )
1138{
7d8268a1
WS
1139 m_modified = true;
1140
4175e952 1141 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) );
7d8268a1 1142
4175e952
RR
1143 wxString tmp( m_lines[m_cursorY].m_text );
1144 tmp.Trim();
1145 if (m_cursorX >= (int)tmp.Len())
1146 {
1147 int len = tmp.Len();
1148 for (int i = 0; i < m_cursorX - len; i++)
1149 tmp.Append( ' ' );
1150 tmp.Append( c );
1151 }
1152 else
1153 {
1154 if (m_overwrite)
1155 tmp.SetChar( m_cursorX, c );
1156 else
1157 tmp.insert( m_cursorX, 1, c );
1158 }
7d8268a1 1159
4175e952 1160 m_lines[m_cursorY].m_text = tmp;
7d8268a1 1161
82434104
RR
1162// if (tmp.Len() > m_longestLine)
1163// {
1164// m_longestLine = tmp.Len();
1165// MyAdjustScrollbars();
1166// }
7d8268a1 1167
82434104
RR
1168 int ww = 0;
1169 GetTextExtent( tmp, &ww, NULL, NULL, NULL );
1170 ww /= m_charWidth;
1171 if (ww > m_longestLine)
4175e952 1172 {
82434104 1173 m_longestLine = ww;
4175e952
RR
1174 MyAdjustScrollbars();
1175 }
82434104 1176
4175e952 1177 m_cursorX++;
7d8268a1 1178
4175e952 1179 int y = m_cursorY*m_lineHeight;
82434104
RR
1180 // int x = (m_cursorX-1)*m_charWidth;
1181 int x = PosToPixel( m_cursorY, m_cursorX-1 );
4175e952
RR
1182 CalcScrolledPosition( x, y, &x, &y );
1183 wxRect rect( x+2, y+2, 10000, m_lineHeight );
7d8268a1 1184 Refresh( true, &rect );
46e87167
RR
1185 // refresh whole line for syntax colour highlighting
1186 rect.x = 0;
7d8268a1
WS
1187 Refresh( false, &rect );
1188
4175e952
RR
1189 int size_x = 0;
1190 int size_y = 0;
1191 GetClientSize( &size_x, &size_y );
1192 size_x /= m_charWidth;
7d8268a1 1193
4175e952
RR
1194 int view_x = 0;
1195 int view_y = 0;
1196 GetViewStart( &view_x, &view_y );
7d8268a1 1197
82434104
RR
1198 //int xx = m_cursorX;
1199 int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth;
7d8268a1 1200
82434104
RR
1201 if (xx < view_x)
1202 Scroll( xx, -1 );
1203 else if (xx > view_x+size_x-1)
1204 Scroll( xx-size_x+1, -1 );
4175e952
RR
1205}
1206
1207void wxTextCtrl::DoBack()
1208{
7d8268a1
WS
1209 m_modified = true;
1210
4175e952
RR
1211 if (m_cursorX == 0)
1212 {
1213 if (m_cursorY == 0) return;
7d8268a1 1214
4175e952 1215 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK, m_cursorY-1, m_cursorY, this ) );
7d8268a1 1216
4175e952
RR
1217 wxString tmp1( m_lines[m_cursorY-1].m_text );
1218 tmp1.Trim();
1219 wxString tmp2( m_lines[m_cursorY].m_text );
1220 tmp2.Trim();
1221 m_cursorX = tmp1.Len();
1222 m_cursorY--;
1223 tmp1.Append( tmp2 );
1224 m_lines[m_cursorY].m_text = tmp1;
1225 m_lines.RemoveAt( m_cursorY+1 );
7d8268a1 1226
4175e952
RR
1227 MyAdjustScrollbars();
1228 RefreshDown( m_cursorY-1 );
1229 }
1230 else
1231 {
1232 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) );
7d8268a1 1233
4175e952
RR
1234 if (m_cursorX <= (int)m_lines[m_cursorY].m_text.Len())
1235 m_lines[m_cursorY].m_text.Remove( m_cursorX-1, 1 );
1236 m_cursorX--;
7d8268a1 1237
4175e952 1238 int y = m_cursorY*m_lineHeight;
82434104
RR
1239 // int x = m_cursorX*m_charWidth;
1240 int x = PosToPixel( m_cursorY, m_cursorX );
4175e952
RR
1241 CalcScrolledPosition( x, y, &x, &y );
1242 wxRect rect( x+2, y+2, 10000, m_lineHeight );
7d8268a1 1243 Refresh( true, &rect );
46e87167
RR
1244 // refresh whole line for syntax colour highlighting
1245 rect.x = 0;
7d8268a1 1246 Refresh( false, &rect );
4175e952
RR
1247 }
1248}
1249
1250void wxTextCtrl::DoDelete()
1251{
7d8268a1
WS
1252 m_modified = true;
1253
4175e952
RR
1254 wxString tmp( m_lines[m_cursorY].m_text );
1255 tmp.Trim();
1256 int len = (int)tmp.Len();
1257 if (m_cursorX >= len)
1258 {
1259 if (m_cursorY == (int)m_lines.GetCount()-1) return;
1260
1261 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, m_cursorY, m_cursorY+1, this ) );
7d8268a1 1262
4175e952
RR
1263 for (int i = 0; i < (m_cursorX-len); i++)
1264 tmp += ' ';
7d8268a1 1265
4175e952 1266 tmp += m_lines[m_cursorY+1].m_text;
7d8268a1 1267
4175e952
RR
1268 m_lines[m_cursorY] = tmp;
1269 m_lines.RemoveAt( m_cursorY+1 );
7d8268a1 1270
4175e952
RR
1271 MyAdjustScrollbars();
1272 RefreshDown( m_cursorY );
1273 }
1274 else
1275 {
1276 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) );
7d8268a1 1277
4175e952
RR
1278 tmp.Remove( m_cursorX, 1 );
1279 m_lines[m_cursorY].m_text = tmp;
7d8268a1 1280
4175e952 1281 int y = m_cursorY*m_lineHeight;
82434104
RR
1282 // int x = m_cursorX*m_charWidth;
1283 int x = PosToPixel( m_cursorY, m_cursorX );
4175e952
RR
1284 CalcScrolledPosition( x, y, &x, &y );
1285 wxRect rect( x+2, y+2, 10000, m_lineHeight );
7d8268a1 1286 Refresh( true, &rect );
46e87167
RR
1287 // refresh whole line for syntax colour highlighting
1288 rect.x = 0;
7d8268a1 1289 Refresh( false, &rect );
4175e952
RR
1290 }
1291}
1292
1293void wxTextCtrl::DoReturn()
1294{
7d8268a1
WS
1295 m_modified = true;
1296
4175e952 1297 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER, m_cursorY, m_cursorY, this ) );
7d8268a1 1298
4175e952
RR
1299 wxString tmp( m_lines[m_cursorY].m_text );
1300 size_t indent = tmp.find_first_not_of( ' ' );
1301 if (indent == wxSTRING_MAXLEN) indent = 0;
1302 tmp.Trim();
1303 if (m_cursorX >= (int)tmp.Len())
1304 {
1305 int cursorX = indent;
1306 int cursorY = m_cursorY + 1;
7d8268a1 1307
4175e952
RR
1308 wxString new_tmp;
1309 for (size_t i = 0; i < indent; i++) new_tmp.Append( ' ' );
1310 m_lines.Insert( new wxSourceLine( new_tmp ), cursorY );
7d8268a1 1311
4175e952
RR
1312 MyAdjustScrollbars();
1313 MoveCursor( cursorX, cursorY );
1314 RefreshDown( m_cursorY );
1315 }
1316 else
1317 {
1318 wxString tmp1( tmp );
1319 tmp1.Remove( m_cursorX, tmp.Len()-m_cursorX );
1320 m_lines[m_cursorY].m_text = tmp1;
7d8268a1 1321
4175e952
RR
1322 wxString tmp2( tmp );
1323 tmp2.Remove( 0, m_cursorX );
7d8268a1 1324
4175e952
RR
1325 int cursorX = indent;
1326 int cursorY = m_cursorY + 1;
7d8268a1 1327
4175e952
RR
1328 wxString new_tmp;
1329 for (size_t i = 0; i < indent; i++) new_tmp.Append( ' ' );
1330 new_tmp.Append( tmp2 );
1331 m_lines.Insert( new wxSourceLine( new_tmp ), cursorY );
7d8268a1 1332
4175e952
RR
1333 MyAdjustScrollbars();
1334 MoveCursor( cursorX, cursorY );
1335 RefreshDown( m_cursorY-1 );
1336 }
1337}
1338
1339void wxTextCtrl::DoDClick()
1340{
1341 wxString line( m_lines[ m_cursorY ].m_text );
1342 if (m_cursorX >= (int)line.Len()) return;
1343 int p = m_cursorX;
8c3289da 1344 char ch = line[(size_t) (p)];
4175e952
RR
1345 if (((ch >= 'a') && (ch <= 'z')) ||
1346 ((ch >= 'A') && (ch <= 'Z')) ||
1347 ((ch >= '0') && (ch <= '9')) ||
1348 (ch == '_'))
1349 {
1350 m_selStartY = m_cursorY;
1351 m_selEndY = m_cursorY;
1352 if (p > 0)
1353 {
8c3289da 1354 ch = line[(size_t) (p-1)];
4175e952
RR
1355 while (((ch >= 'a') && (ch <= 'z')) ||
1356 ((ch >= 'A') && (ch <= 'Z')) ||
1357 ((ch >= '0') && (ch <= '9')) ||
1358 (ch == '_'))
1359 {
1360 p--;
1361 if (p == 0) break;
8c3289da 1362 ch = line[(size_t) (p-1)];
4175e952
RR
1363 }
1364 }
1365 m_selStartX = p;
7d8268a1 1366
4175e952
RR
1367 p = m_cursorX;
1368 if (p < (int)line.Len())
1369 {
8c3289da 1370 ch = line[(size_t) (p)];
4175e952
RR
1371 while (((ch >= 'a') && (ch <= 'z')) ||
1372 ((ch >= 'A') && (ch <= 'Z')) ||
1373 ((ch >= '0') && (ch <= '9')) ||
1374 (ch == '_'))
1375 {
1376 if (p >= (int)line.Len()) break;
1377 p++;
8c3289da 1378 ch = line[(size_t) (p)];
4175e952
RR
1379 }
1380 }
1381 m_selEndX = p;
1382 RefreshLine( m_cursorY );
1383 }
1384}
1385
46e87167 1386wxString wxTextCtrl::GetNextToken( wxString &line, size_t &pos )
4175e952
RR
1387{
1388 wxString ret;
46e87167
RR
1389 size_t len = line.Len();
1390 for (size_t p = pos; p < len; p++)
4175e952
RR
1391 {
1392 if ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL))
1393 {
1394 if (line[p] == '#')
1395 {
46e87167 1396 for (size_t q = p; q < len; q++)
4175e952
RR
1397 ret.Append( line[q] );
1398 pos = p;
1399 return ret;
1400 }
1401 }
1402 else
1403 {
f9c62cfc 1404 if ((line[p] == '/') && (p+1 < len) && (line[(size_t) (p+1)] == '/'))
4175e952 1405 {
46e87167 1406 for (size_t q = p; q < len; q++)
4175e952
RR
1407 ret.Append( line[q] );
1408 pos = p;
1409 return ret;
1410 }
1411 }
7d8268a1 1412
4175e952
RR
1413 if (line[p] == '"')
1414 {
1415 ret.Append( line[p] );
46e87167 1416 for (size_t q = p+1; q < len; q++)
4175e952
RR
1417 {
1418 ret.Append( line[q] );
f9c62cfc 1419 if ((line[q] == '"') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\')))
4175e952
RR
1420 break;
1421 }
1422 pos = p;
1423 return ret;
1424 }
7d8268a1 1425
4175e952
RR
1426 if (line[p] == '\'')
1427 {
1428 ret.Append( line[p] );
46e87167 1429 for (size_t q = p+1; q < len; q++)
4175e952
RR
1430 {
1431 ret.Append( line[q] );
f9c62cfc 1432 if ((line[q] == '\'') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\')))
4175e952
RR
1433 break;
1434 }
1435 pos = p;
1436 return ret;
1437 }
7d8268a1 1438
4175e952
RR
1439 if (((line[p] >= 'a') && (line[p] <= 'z')) ||
1440 ((line[p] >= 'A') && (line[p] <= 'Z')) ||
1441 (line[p] == '_') ||
1442 (line[p] == '#'))
1443 {
1444 ret.Append( line[p] );
46e87167 1445 for (size_t q = p+1; q < len; q++)
4175e952
RR
1446 {
1447 if (((line[q] >= 'a') && (line[q] <= 'z')) ||
1448 ((line[q] >= 'A') && (line[q] <= 'Z')) ||
1449 ((line[q] >= '0') && (line[q] <= '9')) ||
1450 (line[q] == '_'))
1451 {
1452 ret.Append( line[q] );
1453 continue;
1454 }
1455 else
1456 {
1457 pos = p;
1458 return ret;
1459 }
1460 }
1461 pos = p;
1462 return ret;
1463 }
1464 }
7d8268a1 1465
4175e952
RR
1466 return ret;
1467}
1468
46e87167
RR
1469void wxTextCtrl::OnEraseBackground( wxEraseEvent &event )
1470{
1471 event.Skip();
1472}
1473
1474void wxTextCtrl::DrawLinePart( wxDC &dc, int x, int y, const wxString &toDraw, const wxString &origin, const wxColour &colour )
1475{
1476 size_t pos = 0;
1477 size_t len = origin.Len();
1478 dc.SetTextForeground( colour );
1479 while (pos < len)
1480 {
1481 while (toDraw[pos] == wxT(' '))
1482 {
1483 pos++;
1484 if (pos == len) return;
1485 }
7d8268a1 1486
46e87167 1487 size_t start = pos;
7d8268a1 1488
46e87167
RR
1489 wxString current;
1490 current += toDraw[pos];
1491 pos++;
1492 while ( (toDraw[pos] == origin[pos]) && (pos < len))
1493 {
1494 current += toDraw[pos];
1495 pos++;
1496 }
7d8268a1 1497
46e87167
RR
1498 int xx = 0;
1499 wxString tmp = origin.Left( start );
1500 GetTextExtent( tmp, &xx, NULL, NULL, NULL );
1501 xx += x;
1502 int yy = y;
1503 dc.DrawText( current, xx, yy );
1504 }
1505}
1506
4175e952
RR
1507void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int lineNum )
1508{
1509 int selStartY = m_selStartY;
1510 int selEndY = m_selEndY;
1511 int selStartX = m_selStartX;
1512 int selEndX = m_selEndX;
7d8268a1 1513
4175e952
RR
1514 if ((selStartY > selEndY) ||
1515 ((selStartY == selEndY) && (selStartX > selEndX)))
1516 {
1517 int tmp = selStartX;
1518 selStartX = selEndX;
1519 selEndX = tmp;
1520 tmp = selStartY;
1521 selStartY = selEndY;
1522 selEndY = tmp;
1523 }
1524
1525 wxString line( line2 );
1526 if (HasFlag(wxTE_PASSWORD))
1527 {
1528 size_t len = line.Len();
1529 line = wxString( wxT('*'), len );
1530 }
1531
1532 wxString keyword( ' ', line.Len() );
1533 wxString define( ' ', line.Len() );
1534 wxString variable( ' ', line.Len() );
1535 wxString comment( ' ', line.Len() );
1536 wxString my_string( ' ', line.Len() );
46e87167 1537 wxString selection( ' ', line.Len() );
7d8268a1 1538
82434104 1539 if (m_lang != wxSOURCE_LANG_NONE)
4175e952 1540 {
82434104 1541 if (lineNum == m_bracketY)
4175e952 1542 {
82434104
RR
1543 wxString red( ' ', line.Len() );
1544 if (m_bracketX < (int)line.Len())
4175e952 1545 {
8c3289da 1546 red.SetChar( m_bracketX, line[(size_t) (m_bracketX)] );
82434104
RR
1547 line.SetChar( m_bracketX, ' ' );
1548 dc.SetTextForeground( *wxRED );
1549 dc.DrawText( red, x, y );
1550 dc.SetTextForeground( *wxBLACK );
4175e952 1551 }
82434104 1552 }
7d8268a1 1553
46e87167
RR
1554 size_t pos = 0;
1555 wxString token( GetNextToken( line, pos ) );
82434104 1556 while (!token.IsNull())
4175e952 1557 {
82434104 1558 if (m_keywords.Index( token ) != wxNOT_FOUND)
4175e952 1559 {
46e87167
RR
1560 size_t end_pos = pos + token.Len();
1561 for (size_t i = pos; i < end_pos; i++)
82434104 1562 {
46e87167
RR
1563 keyword[i] = line[i];
1564 line[i] = ' ';
82434104
RR
1565 }
1566 } else
1567 if (m_defines.Index( token ) != wxNOT_FOUND)
4175e952 1568 {
46e87167
RR
1569 size_t end_pos = pos + token.Len();
1570 for (size_t i = pos; i < end_pos; i++)
82434104 1571 {
46e87167
RR
1572 define[i] = line[i];
1573 line[i] = ' ';
82434104
RR
1574 }
1575 } else
1576 if ((m_variables.Index( token ) != wxNOT_FOUND) ||
8c3289da 1577 ((token.Len() > 2) && (token[(size_t) (0)] == 'w') && (token[(size_t) (1)] == 'x')))
4175e952 1578 {
46e87167
RR
1579 size_t end_pos = pos + token.Len();
1580 for (size_t i = pos; i < end_pos; i++)
82434104 1581 {
46e87167
RR
1582 variable[i] = line[i];
1583 line[i] = ' ';
82434104
RR
1584 }
1585 } else
8c3289da 1586 if ((token.Len() >= 2) && (token[(size_t) (0)] == '/') && (token[(size_t) (1)] == '/') && (m_lang == wxSOURCE_LANG_CPP))
4175e952 1587 {
46e87167
RR
1588 size_t end_pos = pos + token.Len();
1589 for (size_t i = pos; i < end_pos; i++)
82434104 1590 {
46e87167
RR
1591 comment[i] = line[i];
1592 line[i] = ' ';
82434104
RR
1593 }
1594 } else
8c3289da 1595 if ((token[(size_t) (0)] == '#') &&
82434104 1596 ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL)))
4175e952 1597 {
46e87167
RR
1598 size_t end_pos = pos + token.Len();
1599 for (size_t i = pos; i < end_pos; i++)
82434104 1600 {
46e87167
RR
1601 comment[i] = line[i];
1602 line[i] = ' ';
82434104 1603 }
82434104 1604 } else
8c3289da 1605 if ((token[(size_t) (0)] == '"') || (token[(size_t) (0)] == '\''))
82434104 1606 {
46e87167
RR
1607 size_t end_pos = pos + token.Len();
1608 for (size_t i = pos; i < end_pos; i++)
82434104 1609 {
46e87167
RR
1610 my_string[i] = line[i];
1611 line[i] = ' ';
82434104 1612 }
4175e952 1613 }
82434104 1614 pos += token.Len();
46e87167 1615 token = GetNextToken( line, pos );
4175e952 1616 }
4175e952
RR
1617 }
1618
1619 if ((lineNum < selStartY) || (lineNum > selEndY))
1620 {
46e87167
RR
1621 DrawLinePart( dc, x, y, line, line2, *wxBLACK );
1622 DrawLinePart( dc, x, y, selection, line2, *wxWHITE );
1623 DrawLinePart( dc, x, y, keyword, line2, m_keywordColour );
1624 DrawLinePart( dc, x, y, define, line2, m_defineColour );
1625 DrawLinePart( dc, x, y, variable, line2, m_variableColour );
1626 DrawLinePart( dc, x, y, comment, line2, m_commentColour );
1627 DrawLinePart( dc, x, y, my_string, line2, m_stringColour );
4175e952
RR
1628 return;
1629 }
7d8268a1 1630
4175e952
RR
1631 if (selStartY == selEndY)
1632 {
82434104
RR
1633 // int xx = selStartX*m_charWidth;
1634 int xx = PosToPixel( lineNum, selStartX );
1635 // int ww = (selEndX-selStartX)*m_charWidth;
1636 int ww = PosToPixel( lineNum, selEndX ) - xx;
1637 dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, ww, m_lineHeight );
7d8268a1 1638
46e87167 1639 for (size_t i = (size_t)selStartX; i < (size_t)selEndX; i++)
82434104 1640 {
46e87167
RR
1641 selection[i] = line[i];
1642 line[i] = ' ';
82434104 1643 }
4175e952
RR
1644 } else
1645 if ((lineNum > selStartY) && (lineNum < selEndY))
1646 {
1647 dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, 10000, m_lineHeight );
7d8268a1 1648
46e87167 1649 for (size_t i = 0; i < line.Len(); i++)
82434104 1650 {
46e87167
RR
1651 selection[i] = line[i];
1652 line[i] = ' ';
82434104 1653 }
4175e952
RR
1654 } else
1655 if (lineNum == selStartY)
1656 {
82434104
RR
1657 // int xx = selStartX*m_charWidth;
1658 int xx = PosToPixel( lineNum, selStartX );
1659 dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, 10000, m_lineHeight );
7d8268a1 1660
46e87167 1661 for (size_t i = (size_t)selStartX; i < line.Len(); i++)
82434104 1662 {
46e87167
RR
1663 selection[i] = line[i];
1664 line[i] = ' ';
82434104 1665 }
4175e952
RR
1666 } else
1667 if (lineNum == selEndY)
1668 {
82434104
RR
1669 // int ww = selEndX*m_charWidth;
1670 int ww = PosToPixel( lineNum, selEndX );
1671 dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, ww, m_lineHeight );
7d8268a1 1672
46e87167 1673 for (size_t i = 0; i < (size_t)selEndX; i++)
82434104 1674 {
46e87167
RR
1675 selection[i] = line[i];
1676 line[i] = ' ';
82434104 1677 }
4175e952 1678 }
7d8268a1 1679
46e87167
RR
1680 DrawLinePart( dc, x, y, line, line2, *wxBLACK );
1681 DrawLinePart( dc, x, y, selection, line2, *wxWHITE );
1682 DrawLinePart( dc, x, y, keyword, line2, m_keywordColour );
1683 DrawLinePart( dc, x, y, define, line2, m_defineColour );
1684 DrawLinePart( dc, x, y, variable, line2, m_variableColour );
1685 DrawLinePart( dc, x, y, comment, line2, m_commentColour );
1686 DrawLinePart( dc, x, y, my_string, line2, m_stringColour );
4175e952
RR
1687}
1688
1689void wxTextCtrl::OnPaint( wxPaintEvent &event )
1690{
1691 wxPaintDC dc(this);
7d8268a1 1692
4175e952 1693 if (m_lines.GetCount() == 0) return;
7d8268a1 1694
4175e952 1695 PrepareDC( dc );
7d8268a1 1696
4175e952 1697 dc.SetFont( m_sourceFont );
7d8268a1 1698
4175e952
RR
1699 int scroll_y = 0;
1700 GetViewStart( NULL, &scroll_y );
7d8268a1 1701
40de795f
RR
1702 // We have a inner border of two pixels
1703 // around the text, so scroll units do
1704 // not correspond to lines.
1705 if (scroll_y > 0) scroll_y--;
7d8268a1 1706
4175e952
RR
1707 int size_x = 0;
1708 int size_y = 0;
1709 GetClientSize( &size_x, &size_y );
7d8268a1 1710
4175e952
RR
1711 dc.SetPen( *wxTRANSPARENT_PEN );
1712 dc.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT), wxSOLID ) );
40de795f 1713 int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+2 );
4175e952
RR
1714 for (int i = scroll_y; i < upper; i++)
1715 {
1716 int x = 0+2;
1717 int y = i*m_lineHeight+2;
1718 int w = 10000;
1719 int h = m_lineHeight;
1720 CalcScrolledPosition( x,y,&x,&y );
1721 if (IsExposed(x,y,w,h))
1722 DrawLine( dc, 0+2, i*m_lineHeight+2, m_lines[i].m_text, i );
1723 }
7d8268a1 1724
968602f8 1725 if (m_editable && (FindFocus() == this))
40de795f 1726 {
968602f8
JS
1727 ///dc.SetBrush( *wxRED_BRUSH );
1728 dc.SetBrush( *wxBLACK_BRUSH );
40de795f
RR
1729 // int xx = m_cursorX*m_charWidth;
1730 int xx = PosToPixel( m_cursorY, m_cursorX );
1731 dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
1732 }
4175e952
RR
1733}
1734
1735void wxTextCtrl::OnMouse( wxMouseEvent &event )
1736{
1737 if (m_lines.GetCount() == 0) return;
1738
7d8268a1 1739
4175e952
RR
1740#if 0 // there is no middle button on iPAQs
1741 if (event.MiddleDown())
1742 {
1743 Paste( TRUE );
1744 return;
1745 }
1746#endif
7d8268a1 1747
4175e952
RR
1748 if (event.LeftDClick())
1749 {
1750 DoDClick();
1751 return;
1752 }
7d8268a1 1753
4175e952 1754 if (event.LeftDown())
7d8268a1
WS
1755 {
1756 m_capturing = true;
4175e952
RR
1757 CaptureMouse();
1758 }
7d8268a1 1759
4175e952 1760 if (event.LeftUp())
7d8268a1
WS
1761 {
1762 m_capturing = false;
4175e952
RR
1763 ReleaseMouse();
1764 }
1765
7d8268a1 1766 if (event.LeftDown() ||
4175e952
RR
1767 (event.LeftIsDown() && m_capturing))
1768 {
1769 int x = event.GetX();
1770 int y = event.GetY();
1771 CalcUnscrolledPosition( x, y, &x, &y );
4175e952 1772 y /= m_lineHeight;
82434104
RR
1773 // x /= m_charWidth;
1774 x = PixelToPos( y, x );
7d8268a1
WS
1775 MoveCursor(
1776 wxMin( 1000, wxMax( 0, x ) ),
4175e952
RR
1777 wxMin( (int)m_lines.GetCount()-1, wxMax( 0, y ) ),
1778 event.ShiftDown() || !event.LeftDown() );
1779 }
1780}
1781
1782void wxTextCtrl::OnChar( wxKeyEvent &event )
1783{
1784 if (m_lines.GetCount() == 0) return;
1785
40de795f 1786 if (!m_editable) return;
7d8268a1 1787
4175e952
RR
1788 int size_x = 0;
1789 int size_y = 0;
1790 GetClientSize( &size_x, &size_y );
1791 size_x /= m_charWidth;
1792 size_y /= m_lineHeight;
1793 size_y--;
7d8268a1 1794
4175e952
RR
1795 if (event.ShiftDown())
1796 {
1797 switch (event.GetKeyCode())
1798 {
1799 case '4': event.m_keyCode = WXK_LEFT; break;
1800 case '8': event.m_keyCode = WXK_UP; break;
1801 case '6': event.m_keyCode = WXK_RIGHT; break;
1802 case '2': event.m_keyCode = WXK_DOWN; break;
1803 case '9': event.m_keyCode = WXK_PRIOR; break;
1804 case '3': event.m_keyCode = WXK_NEXT; break;
1805 case '7': event.m_keyCode = WXK_HOME; break;
1806 case '1': event.m_keyCode = WXK_END; break;
1807 case '0': event.m_keyCode = WXK_INSERT; break;
1808 }
1809 }
7d8268a1 1810
4175e952
RR
1811 switch (event.GetKeyCode())
1812 {
1813 case WXK_UP:
1814 {
1815 if (m_ignoreInput) return;
1816 if (m_cursorY > 0)
1817 MoveCursor( m_cursorX, m_cursorY-1, event.ShiftDown() );
7d8268a1 1818 m_ignoreInput = true;
4175e952
RR
1819 return;
1820 }
1821 case WXK_DOWN:
1822 {
1823 if (m_ignoreInput) return;
1824 if (m_cursorY < (int)(m_lines.GetCount()-1))
1825 MoveCursor( m_cursorX, m_cursorY+1, event.ShiftDown() );
7d8268a1 1826 m_ignoreInput = true;
4175e952
RR
1827 return;
1828 }
1829 case WXK_LEFT:
1830 {
1831 if (m_ignoreInput) return;
1832 if (m_cursorX > 0)
1833 {
1834 MoveCursor( m_cursorX-1, m_cursorY, event.ShiftDown() );
1835 }
1836 else
1837 {
1838 if (m_cursorY > 0)
1839 MoveCursor( m_lines[m_cursorY-1].m_text.Len(), m_cursorY-1, event.ShiftDown() );
1840 }
7d8268a1 1841 m_ignoreInput = true;
4175e952
RR
1842 return;
1843 }
1844 case WXK_RIGHT:
1845 {
1846 if (m_ignoreInput) return;
1847 if (m_cursorX < 1000)
1848 MoveCursor( m_cursorX+1, m_cursorY, event.ShiftDown() );
7d8268a1 1849 m_ignoreInput = true;
4175e952
RR
1850 return;
1851 }
1852 case WXK_HOME:
1853 {
1854 if (event.ControlDown())
1855 MoveCursor( 0, 0, event.ShiftDown() );
1856 else
1857 MoveCursor( 0, m_cursorY, event.ShiftDown() );
1858 return;
1859 }
1860 case WXK_END:
1861 {
1862 if (event.ControlDown())
1863 MoveCursor( 0, m_lines.GetCount()-1, event.ShiftDown() );
1864 else
1865 MoveCursor( m_lines[m_cursorY].m_text.Len(), m_cursorY, event.ShiftDown() );
1866 return;
1867 }
1868 case WXK_NEXT:
1869 {
1870 if (m_ignoreInput) return;
1871 MoveCursor( m_cursorX, wxMin( (int)(m_lines.GetCount()-1), m_cursorY+size_y ), event.ShiftDown() );
7d8268a1 1872 m_ignoreInput = true;
4175e952
RR
1873 return;
1874 }
1875 case WXK_PRIOR:
1876 {
1877 if (m_ignoreInput) return;
1878 MoveCursor( m_cursorX, wxMax( 0, m_cursorY-size_y ), event.ShiftDown() );
7d8268a1 1879 m_ignoreInput = true;
4175e952
RR
1880 return;
1881 }
1882 case WXK_INSERT:
1883 {
1884 if (event.ShiftDown())
1885 Paste();
1886 else if (event.ControlDown())
1887 Copy();
1888 else
1889 m_overwrite = !m_overwrite;
1890 return;
1891 }
1892 case WXK_RETURN:
1893 {
cf76eeec
RR
1894 if (m_windowStyle & wxPROCESS_ENTER)
1895 {
1896 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1897 event.SetEventObject(this);
1898 event.SetString(GetValue());
1899 if (GetEventHandler()->ProcessEvent(event)) return;
1900 }
7d8268a1 1901
4175e952
RR
1902 if (IsSingleLine())
1903 {
1904 event.Skip();
1905 return;
1906 }
7d8268a1 1907
4175e952
RR
1908 if (HasSelection())
1909 Delete();
1910 DoReturn();
1911 return;
1912 }
1913 case WXK_TAB:
1914 {
1915 if (HasSelection())
1916 Delete();
1917 bool save_overwrite = m_overwrite;
7d8268a1 1918 m_overwrite = false;
4175e952
RR
1919 int i = 4-(m_cursorX % 4);
1920 if (i == 0) i = 4;
1921 for (int c = 0; c < i; c++)
1922 DoChar( ' ' );
1923 m_overwrite = save_overwrite;
1924 return;
1925 }
1926 case WXK_BACK:
1927 {
1928 if (HasSelection())
1929 Delete();
1930 else
1931 DoBack();
1932 return;
1933 }
1934 case WXK_DELETE:
1935 {
1936 if (HasSelection())
1937 Delete();
1938 else
1939 DoDelete();
1940 return;
1941 }
7d8268a1 1942 default:
4175e952 1943 {
395cb311
MB
1944 if ( (event.GetKeyCode() >= 'a') &&
1945 (event.GetKeyCode() <= 'z') &&
4175e952
RR
1946 (event.AltDown()) )
1947 {
1948 // Alt-F etc.
1949 event.Skip();
1950 return;
1951 }
7d8268a1
WS
1952
1953 if ( (event.GetKeyCode() >= 32) &&
395cb311 1954 (event.GetKeyCode() <= 255) &&
4175e952
RR
1955 !(event.ControlDown() && !event.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
1956 {
1957 if (HasSelection())
1958 Delete();
395cb311 1959 DoChar( (char) event.GetKeyCode() );
4175e952
RR
1960 return;
1961 }
1962 }
1963 }
7d8268a1 1964
4175e952
RR
1965 event.Skip();
1966}
1967
e39af974 1968void wxTextCtrl::OnInternalIdle()
4175e952 1969{
47cd6610 1970 wxControl::OnInternalIdle();
7d8268a1
WS
1971
1972 m_ignoreInput = false;
1973
82434104
RR
1974 if (m_lang != wxSOURCE_LANG_NONE)
1975 SearchForBrackets();
4175e952
RR
1976}
1977
1978void wxTextCtrl::Indent()
1979{
1980 int startY = m_cursorY;
1981 int endY = m_cursorY;
1982 if (HasSelection())
1983 {
1984 startY = m_selStartY;
1985 endY = m_selEndY;
1986 if (endY < startY)
1987 {
1988 int tmp = startY;
1989 startY = endY;
1990 endY = tmp;
1991 }
1992 }
7d8268a1 1993
4175e952 1994 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) );
7d8268a1 1995
4175e952
RR
1996 for (int i = startY; i <= endY; i++)
1997 {
2b5f62a0 1998 m_lines[i].m_text.insert( 0u, wxT(" ") );
4175e952
RR
1999 RefreshLine( i );
2000 }
2001}
2002
2003void wxTextCtrl::Unindent()
2004{
2005 int startY = m_cursorY;
2006 int endY = m_cursorY;
2007 if (HasSelection())
2008 {
2009 startY = m_selStartY;
2010 endY = m_selEndY;
2011 if (endY < startY)
2012 {
2013 int tmp = startY;
2014 startY = endY;
2015 endY = tmp;
2016 }
2017 }
7d8268a1 2018
4175e952 2019 m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) );
7d8268a1 2020
4175e952
RR
2021 for (int i = startY; i <= endY; i++)
2022 {
2023 for (int n = 0; n < 4; n++)
2024 {
2b5f62a0 2025 if (m_lines[i].m_text[0u] == wxT(' '))
4175e952
RR
2026 m_lines[i].m_text.erase(0u,1u);
2027 }
2028 RefreshLine( i );
2029 }
2030}
2031bool wxTextCtrl::HasSelection()
2032{
2033 return ((m_selStartY != m_selEndY) || (m_selStartX != m_selEndX));
2034}
2035
2036void wxTextCtrl::ClearSelection()
2037{
2038 m_selStartX = -1;
2039 m_selStartY = -1;
2040 m_selEndX = -1;
2041 m_selEndY = -1;
2042}
2043
2044void wxTextCtrl::RefreshLine( int n )
2045{
2046 int y = n*m_lineHeight;
2047 int x = 0;
2048 CalcScrolledPosition( x, y, &x, &y );
2049 wxRect rect( 0+2, y+2, 10000, m_lineHeight );
7d8268a1 2050 Refresh( true, &rect );
4175e952
RR
2051}
2052
2053void wxTextCtrl::RefreshDown( int n )
2054{
2055 int size_x = 0;
2056 int size_y = 0;
2057 GetClientSize( &size_x, &size_y );
2058
2059 int view_x = 0;
2060 int view_y = 0;
2061 GetViewStart( &view_x, &view_y );
2062
2063 if (n < view_y)
2064 {
2065 Refresh();
2066 }
2067 else
2068 {
2069 int y = n*m_lineHeight;
2070 int x = 0;
2071 CalcScrolledPosition( x, y, &x, &y );
7d8268a1 2072
4175e952 2073 wxRect rect( 0+2, y+2, 10000, size_y );
7d8268a1 2074 Refresh( true, &rect );
4175e952
RR
2075 }
2076}
2077
2078void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
2079{
40de795f
RR
2080 if (!m_editable) return;
2081
2082 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
4175e952 2083 {
65c745fe 2084 if (new_x > (int) (m_lines[new_y].m_text.Len()))
4175e952
RR
2085 new_x = m_lines[new_y].m_text.Len();
2086 }
2087
2088 if ((new_x == m_cursorX) && (new_y == m_cursorY)) return;
2089
7d8268a1 2090 bool no_cursor_refresh = false;
46e87167 2091 bool has_selection = HasSelection();
4175e952
RR
2092
2093 if (shift)
2094 {
2095 int x,y,w,h;
7d8268a1
WS
2096 bool erase_background = true;
2097
46e87167 2098 if (!has_selection)
4175e952
RR
2099 {
2100 m_selStartX = m_cursorX;
2101 m_selStartY = m_cursorY;
7d8268a1 2102
4175e952
RR
2103 x = 0;
2104 w = 10000;
2105 if (new_y > m_selStartY)
2106 {
2107 y = m_selStartY*m_lineHeight;
2108 h = (new_y-m_selStartY+1)*m_lineHeight;
2109 }
2110 else if (new_y == m_selStartY)
2111 {
46e87167
RR
2112 x = PosToPixel( new_y, m_selStartX );
2113 w = PosToPixel( new_y, new_x ) - x;
2114 if (w < 0)
2115 {
2116 x += w;
2117 w = -w + 2; // +2 for the cursor
2118 }
4175e952
RR
2119 y = m_selStartY*m_lineHeight;
2120 h = m_lineHeight;
2121 }
2122 else
2123 {
2124 y = new_y*m_lineHeight;
2125 h = (-new_y+m_selStartY+1)*m_lineHeight;
2126 }
7d8268a1
WS
2127
2128 no_cursor_refresh = true;
4175e952
RR
2129 m_cursorX = new_x;
2130 m_cursorY = new_y;
2131 }
2132 else
2133 {
2134 if (new_y == m_selEndY)
2135 {
2136 y = new_y *m_lineHeight;
2137 h = m_lineHeight;
2138 if (m_selEndX > new_x)
2139 {
82434104
RR
2140 // x = new_x*m_charWidth;
2141 x = PosToPixel( new_y, new_x );
2142 // w = (m_selEndX-new_x)*m_charWidth;
2143 w = PosToPixel( new_y, m_selEndX ) - x;
4175e952
RR
2144 }
2145 else
2146 {
82434104
RR
2147 // x = m_selEndX*m_charWidth;
2148 x = PosToPixel( new_y, m_selEndX );
2149 // w = (-m_selEndX+new_x)*m_charWidth;
2150 w = PosToPixel( new_y, new_x ) - x;
4175e952
RR
2151 }
2152 }
2153 else
2154 {
2155 x = 0;
2156 w = 10000;
2157 if (new_y > m_selEndY)
2158 {
2159 y = m_selEndY*m_lineHeight;
2160 h = (new_y-m_selEndY+1) * m_lineHeight;
7d8268a1 2161
8c695972
RR
2162 erase_background = ((m_selEndY < m_selStartY) ||
2163 ((m_selEndY == m_selStartY) && (m_selEndX < m_selStartX)));
4175e952
RR
2164 }
2165 else
2166 {
2167 y = new_y*m_lineHeight;
2168 h = (-new_y+m_selEndY+1) * m_lineHeight;
7d8268a1 2169
8c695972
RR
2170 erase_background = ((m_selEndY > m_selStartY) ||
2171 ((m_selEndY == m_selStartY) && (m_selEndX > m_selStartX)));
4175e952 2172 }
7d8268a1 2173 no_cursor_refresh = true;
4175e952
RR
2174 m_cursorX = new_x;
2175 m_cursorY = new_y;
2176 }
2177 }
7d8268a1 2178
4175e952
RR
2179 m_selEndX = new_x;
2180 m_selEndY = new_y;
7d8268a1 2181
4175e952
RR
2182 CalcScrolledPosition( x, y, &x, &y );
2183 wxRect rect( x+2, y+2, w, h );
8c695972 2184 Refresh( erase_background, &rect );
4175e952
RR
2185 }
2186 else
2187 {
46e87167 2188 if (has_selection)
4175e952
RR
2189 {
2190 int ry1 = m_selEndY;
2191 int ry2 = m_selStartY;
2192 m_selEndX = -1;
2193 m_selEndY = -1;
2194 m_selStartX = -1;
2195 m_selStartY = -1;
7d8268a1 2196
4175e952
RR
2197 if (ry1 > ry2)
2198 {
2199 int tmp = ry2;
2200 ry2 = ry1;
2201 ry1 = tmp;
2202 }
7d8268a1 2203
4175e952
RR
2204 int x = 0;
2205 int y = ry1*m_lineHeight;
2206 CalcScrolledPosition( x, y, &x, &y );
82434104 2207 wxRect rect( 0, y+2, 10000, (ry2-ry1+1)*m_lineHeight );
7d8268a1
WS
2208
2209 Refresh( true, &rect );
4175e952
RR
2210 }
2211 }
7d8268a1 2212
4175e952 2213/*
7d8268a1 2214 printf( "startx %d starty %d endx %d endy %d\n",
4175e952 2215 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
7d8268a1 2216
4175e952
RR
2217 printf( "has %d\n", (int)HasSelection() );
2218*/
2219
2220 if (!no_cursor_refresh)
2221 {
82434104
RR
2222 // int x = m_cursorX*m_charWidth;
2223 int x = PosToPixel( m_cursorY, m_cursorX );
4175e952
RR
2224 int y = m_cursorY*m_lineHeight;
2225 CalcScrolledPosition( x, y, &x, &y );
2226 wxRect rect( x+2, y+2, 4, m_lineHeight+2 );
7d8268a1 2227
4175e952
RR
2228 m_cursorX = new_x;
2229 m_cursorY = new_y;
7d8268a1
WS
2230
2231 Refresh( true, &rect );
968602f8
JS
2232
2233 if (FindFocus() == this)
2234 {
2235 wxClientDC dc(this);
2236 PrepareDC( dc );
2237 dc.SetPen( *wxTRANSPARENT_PEN );
2238 //dc.SetBrush( *wxRED_BRUSH );
2239 dc.SetBrush( *wxBLACK_BRUSH );
2240 // int xx = m_cursorX*m_charWidth;
2241 int xx = PosToPixel( m_cursorY, m_cursorX );
2242 dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
2243 }
4175e952 2244 }
7d8268a1 2245
4175e952
RR
2246 int size_x = 0;
2247 int size_y = 0;
2248 GetClientSize( &size_x, &size_y );
2249 size_x /= m_charWidth;
2250 size_y /= m_lineHeight;
7d8268a1 2251
4175e952
RR
2252 int view_x = 0;
2253 int view_y = 0;
2254 GetViewStart( &view_x, &view_y );
7d8268a1 2255
4175e952
RR
2256 if (centre)
2257 {
2258 int sy = m_cursorY - (size_y/2);
2259 if (sy < 0) sy = 0;
2260 Scroll( -1, sy );
2261 }
2262 else
2263 {
2264 if (m_cursorY < view_y)
2265 Scroll( -1, m_cursorY );
2266 else if (m_cursorY > view_y+size_y-1)
2267 Scroll( -1, m_cursorY-size_y+1 );
2268 }
7d8268a1 2269
82434104
RR
2270 //int xx = m_cursorX;
2271 int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth;
7d8268a1 2272
82434104
RR
2273 if (xx < view_x)
2274 Scroll( xx, -1 );
2275 else if (xx > view_x+size_x-1)
2276 Scroll( xx-size_x+1, -1 );
4175e952
RR
2277}
2278
2279void wxTextCtrl::MyAdjustScrollbars()
2280{
2281 if (IsSingleLine())
2282 return;
2283
2284 int y_range = m_lines.GetCount();
2285
2286 int height = 0;
2287 GetClientSize( NULL, &height );
2288 height -= 4;
65c745fe 2289 if (height >= (int)m_lines.GetCount() *m_lineHeight)
4175e952 2290 y_range = 0;
7d8268a1 2291
4175e952
RR
2292 int view_x = 0;
2293 int view_y = 0;
2294 GetViewStart( &view_x, &view_y );
7d8268a1 2295
4175e952
RR
2296 SetScrollbars( m_charWidth, m_lineHeight, m_longestLine+2, y_range, view_x, view_y );
2297}
2298
2299//-----------------------------------------------------------------------------
2300// clipboard handlers
2301//-----------------------------------------------------------------------------
2302
2303void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
2304{
2305 Cut();
2306}
2307
2308void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
2309{
2310 Copy();
2311}
2312
2313void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
2314{
2315 Paste();
2316}
2317
2318void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
2319{
2320 Undo();
2321}
2322
2323void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
2324{
2325 Redo();
2326}
2327
2328void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
2329{
2330 event.Enable( CanCut() );
2331}
2332
2333void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
2334{
2335 event.Enable( CanCopy() );
2336}
2337
2338void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
2339{
2340 event.Enable( CanPaste() );
2341}
2342
2343void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
2344{
2345 event.Enable( CanUndo() );
2346}
2347
2348void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
2349{
2350 event.Enable( CanRedo() );
2351}
2352
2353wxSize wxTextCtrl::DoGetBestSize() const
2354{
2355 if (IsSingleLine())
2356 {
2357 wxSize ret(80, m_lineHeight + 4);
7d8268a1 2358
4175e952
RR
2359 if (HasFlag(wxBORDER_SUNKEN) || HasFlag(wxBORDER_RAISED))
2360 ret.y += 4;
7d8268a1 2361
4175e952
RR
2362 if (HasFlag(wxBORDER_SIMPLE))
2363 ret.y += 2;
7d8268a1 2364
4175e952
RR
2365 return ret;
2366 }
2367 else
2368 {
2369 return wxSize(80, 60);
2370 }
2371}
2372
2373// ----------------------------------------------------------------------------
2374// freeze/thaw
2375// ----------------------------------------------------------------------------
2376
2377void wxTextCtrl::Freeze()
2378{
2379}
2380
2381void wxTextCtrl::Thaw()
2382{
2383}
2384
968602f8
JS
2385void wxTextCtrl::OnSetFocus( wxFocusEvent& event )
2386{
2387 // To hide or show caret, as appropriate
2388 Refresh();
2389}
2390
2391void wxTextCtrl::OnKillFocus( wxFocusEvent& event )
2392{
2393 // To hide or show caret, as appropriate
2394 Refresh();
2395}
2396
4175e952
RR
2397// ----------------------------------------------------------------------------
2398// text control scrolling
2399// ----------------------------------------------------------------------------
2400
2401bool wxTextCtrl::ScrollLines(int lines)
2402{
2403 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2404
7d8268a1 2405 return false;
4175e952
RR
2406}
2407
2408bool wxTextCtrl::ScrollPages(int pages)
2409{
2410 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");
7d8268a1
WS
2411
2412 return false;
4175e952
RR
2413}
2414