1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "textctrl.h"
14 #include "wx/textctrl.h"
18 #include "wx/settings.h"
20 #include "wx/clipbrd.h"
21 #include "wx/tokenzr.h"
23 #include "wx/univ/inphand.h"
24 #include "wx/univ/renderer.h"
25 #include "wx/univ/colschem.h"
26 #include "wx/univ/theme.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type
, int y1
, int y2
, wxTextCtrl
*owner
)
39 m_cursorX
= m_owner
->GetCursorX();
40 m_cursorY
= m_owner
->GetCursorY();
42 if (m_type
== wxSOURCE_UNDO_LINE
)
44 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
46 if (m_type
== wxSOURCE_UNDO_ENTER
)
48 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
50 if (m_type
== wxSOURCE_UNDO_BACK
)
52 for (int i
= m_y1
; i
< m_y2
+2; i
++)
54 if (i
>= (int)m_owner
->m_lines
.GetCount())
57 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
60 if (m_type
== wxSOURCE_UNDO_DELETE
)
62 for (int i
= m_y1
; i
< m_y2
+1; i
++)
64 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
67 if (m_type
== wxSOURCE_UNDO_PASTE
)
69 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
73 void wxSourceUndoStep::Undo()
75 if (m_type
== wxSOURCE_UNDO_LINE
)
77 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
78 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
79 m_owner
->RefreshLine( m_y1
);
81 if (m_type
== wxSOURCE_UNDO_ENTER
)
83 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
84 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
85 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
86 m_owner
->RefreshDown( m_y1
);
88 if (m_type
== wxSOURCE_UNDO_BACK
)
90 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
91 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[1] ), m_y1
+1 );
92 m_owner
->MyAdjustScrollbars();
93 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
94 m_owner
->RefreshDown( m_y1
);
96 if (m_type
== wxSOURCE_UNDO_DELETE
)
98 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
99 for (int i
= 1; i
< (int)m_lines
.GetCount(); i
++)
100 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[i
] ), m_y1
+i
);
101 m_owner
->MyAdjustScrollbars();
102 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
103 m_owner
->RefreshDown( m_y1
);
105 if (m_type
== wxSOURCE_UNDO_PASTE
)
107 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
108 for (int i
= 0; i
< m_y2
-m_y1
; i
++)
109 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
110 m_owner
->MyAdjustScrollbars();
111 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
112 m_owner
->RefreshDown( m_y1
);
114 if (m_type
== wxSOURCE_UNDO_INSERT_LINE
)
116 m_owner
->m_lines
.RemoveAt( m_y1
);
117 m_owner
->MyAdjustScrollbars();
118 m_owner
->MoveCursor( 0, m_y1
);
119 m_owner
->RefreshDown( m_y1
);
123 #include "wx/arrimpl.cpp"
124 WX_DEFINE_OBJARRAY(wxSourceLineArray
);
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
132 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
133 EVT_PAINT(wxTextCtrl::OnPaint
)
134 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
135 EVT_CHAR(wxTextCtrl::OnChar
)
136 EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse
)
137 EVT_IDLE(wxTextCtrl::OnIdle
)
139 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
140 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
141 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
142 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
143 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
145 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
146 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
147 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
148 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
149 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
152 void wxTextCtrl::Init()
157 m_undos
.DeleteContents( TRUE
);
159 m_lang
= wxSOURCE_LANG_NONE
;
172 m_ignoreInput
= FALSE
;
176 m_keywordColour
= wxColour( 10, 140, 10 );
178 m_defineColour
= *wxRED
;
180 m_variableColour
= wxColour( 50, 120, 150 );
182 m_commentColour
= wxColour( 130, 130, 130 );
184 m_stringColour
= wxColour( 10, 140, 10 );
187 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
189 const wxString
&value
,
193 const wxValidator
& validator
,
194 const wxString
&name
)
195 : wxScrollHelper(this)
199 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
202 bool wxTextCtrl::Create( wxWindow
*parent
,
204 const wxString
&value
,
208 const wxValidator
& validator
,
209 const wxString
&name
)
211 if ((style
& wxBORDER_MASK
) == 0)
212 style
|= wxBORDER_SUNKEN
;
214 if ((style
& wxTE_MULTILINE
) != 0)
215 style
|= wxALWAYS_SHOW_SB
;
217 wxTextCtrlBase::Create( parent
, id
, wxDefaultPosition
, size
,
218 style
|wxVSCROLL
|wxHSCROLL
|wxNO_FULL_REPAINT_ON_RESIZE
);
220 SetBackgroundColour( *wxWHITE
);
222 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
224 m_editable
= ((m_windowStyle
& wxTE_READONLY
) == 0);
226 if (HasFlag(wxTE_PASSWORD
))
227 m_sourceFont
= wxFont( 12, wxMODERN
, wxNORMAL
, wxNORMAL
);
229 m_sourceFont
= GetFont();
232 dc
.SetFont( m_sourceFont
);
233 m_lineHeight
= dc
.GetCharHeight();
234 m_charWidth
= dc
.GetCharWidth();
238 wxSize
size_best( DoGetBestSize() );
239 wxSize
new_size( size
);
240 if (new_size
.x
== -1)
241 new_size
.x
= size_best
.x
;
242 if (new_size
.y
== -1)
243 new_size
.y
= size_best
.y
;
244 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
245 SetSize( new_size
.x
, new_size
.y
);
247 // We create an input handler since it might be useful
248 CreateInputHandler(wxINP_HANDLER_TEXTCTRL
);
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
257 wxString
wxTextCtrl::GetValue() const
260 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
262 ret
+= m_lines
[i
].m_text
;
263 if (i
+1 < m_lines
.GetCount())
270 void wxTextCtrl::SetValue(const wxString
& value
)
281 m_lines
.Add( new wxSourceLine( wxT("") ) );
289 pos
= value
.find( wxT('\n'), begin
);
292 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, value
.Len()-begin
) );
295 // if (sl->m_text.Len() > m_longestLine)
296 // m_longestLine = sl->m_text.Len();
298 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
300 if (ww
> m_longestLine
)
307 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, pos
-begin
) );
310 // if (sl->m_text.Len() > m_longestLine)
311 // m_longestLine = sl->m_text.Len();
313 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
315 if (ww
> m_longestLine
)
323 MyAdjustScrollbars();
328 int wxTextCtrl::GetLineLength(long lineNo
) const
330 if (lineNo
>= (long)m_lines
.GetCount())
333 return m_lines
[lineNo
].m_text
.Len();
336 wxString
wxTextCtrl::GetLineText(long lineNo
) const
338 if (lineNo
>= (long)m_lines
.GetCount())
341 return m_lines
[lineNo
].m_text
;
344 int wxTextCtrl::GetNumberOfLines() const
346 return m_lines
.GetCount();
349 bool wxTextCtrl::IsModified() const
354 bool wxTextCtrl::IsEditable() const
359 void wxTextCtrl::GetSelection(long* from
, long* to
) const
363 void wxTextCtrl::Clear()
371 m_lines
.Add( new wxSourceLine( wxT("") ) );
373 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
378 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
382 void wxTextCtrl::Remove(long from
, long to
)
387 void wxTextCtrl::DiscardEdits()
393 void wxTextCtrl::SetMaxLength(unsigned long len
)
397 int wxTextCtrl::PosToPixel( int line
, int pos
)
399 // TODO add support for Tabs
401 if (line
>= (int)m_lines
.GetCount()) return 0;
402 if (pos
< 0) return 0;
404 wxString text
= m_lines
[line
].m_text
;
406 if (text
.IsEmpty()) return 0;
408 if (pos
< (int)text
.Len())
409 text
.Remove( pos
, text
.Len()-pos
);
413 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
418 int wxTextCtrl::PixelToPos( int line
, int pixel
)
420 if (pixel
< 2) return 0;
422 if (line
>= (int)m_lines
.GetCount()) return 0;
424 wxString text
= m_lines
[line
].m_text
;
427 int res
= text
.Len();
430 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
436 text
.Remove( res
,1 );
442 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
448 if (m_lang
== wxSOURCE_LANG_PYTHON
)
450 m_keywords
.Add( "class" );
451 m_keywords
.Add( "__init__" );
452 m_keywords
.Add( "return" );
453 m_keywords
.Add( "def" );
454 m_keywords
.Add( "try" );
455 m_keywords
.Add( "except" );
456 m_keywords
.Add( "if" );
457 m_keywords
.Add( "else" );
458 m_keywords
.Add( "finally" );
459 m_keywords
.Add( "for" );
460 m_keywords
.Add( "if" );
461 m_keywords
.Add( "elif" );
462 m_keywords
.Add( "in" );
463 m_keywords
.Add( "and" );
464 m_keywords
.Add( "del" );
465 m_keywords
.Add( "is" );
466 m_keywords
.Add( "raise" );
467 m_keywords
.Add( "assert" );
468 m_keywords
.Add( "lambda" );
469 m_keywords
.Add( "break" );
470 m_keywords
.Add( "global" );
471 m_keywords
.Add( "not" );
472 m_keywords
.Add( "or" );
473 m_keywords
.Add( "while" );
474 m_keywords
.Add( "continue" );
475 m_keywords
.Add( "exec" );
476 m_keywords
.Add( "pass" );
477 m_keywords
.Add( "print" );
479 if (m_lang
== wxSOURCE_LANG_PERL
)
481 m_keywords
.Add( "main" );
482 m_keywords
.Add( "sub" );
483 m_keywords
.Add( "shift" );
484 m_keywords
.Add( "push" );
485 m_keywords
.Add( "split" );
486 m_keywords
.Add( "join" );
487 m_keywords
.Add( "chop" );
488 m_keywords
.Add( "grep" );
489 m_keywords
.Add( "open" );
490 m_keywords
.Add( "print" );
491 m_keywords
.Add( "sprint" );
492 m_keywords
.Add( "printf" );
493 m_keywords
.Add( "sprintf" );
494 m_keywords
.Add( "my" );
495 m_keywords
.Add( "local" );
496 m_keywords
.Add( "exit" );
497 m_keywords
.Add( "die" );
498 m_keywords
.Add( "return" );
499 m_keywords
.Add( "for" );
500 m_keywords
.Add( "foreach" );
501 m_keywords
.Add( "while" );
502 m_keywords
.Add( "unless" );
503 m_keywords
.Add( "if" );
504 m_keywords
.Add( "next" );
505 m_keywords
.Add( "last" );
506 m_keywords
.Add( "else" );
507 m_keywords
.Add( "elsif" );
508 m_keywords
.Add( "ne" );
509 m_keywords
.Add( "qe" );
512 if (m_lang
== wxSOURCE_LANG_CPP
)
514 m_keywords
.Add( "class" );
515 m_keywords
.Add( "return" );
516 m_keywords
.Add( "if" );
517 m_keywords
.Add( "then" );
518 m_keywords
.Add( "else" );
519 m_keywords
.Add( "struct" );
520 m_keywords
.Add( "enum" );
521 m_keywords
.Add( "while" );
522 m_keywords
.Add( "do" );
523 m_keywords
.Add( "for" );
524 m_keywords
.Add( "continue" );
525 m_keywords
.Add( "break" );
526 m_keywords
.Add( "switch" );
527 m_keywords
.Add( "case" );
528 m_keywords
.Add( "goto" );
529 m_keywords
.Add( "label" );
530 m_keywords
.Add( "inline" );
531 m_keywords
.Add( "operator" );
532 m_keywords
.Add( "virtual" );
533 m_keywords
.Add( "private" );
534 m_keywords
.Add( "public" );
535 m_keywords
.Add( "protected" );
536 m_keywords
.Add( "friend" );
537 m_keywords
.Add( "exception" );
538 m_keywords
.Add( "throw" );
539 m_keywords
.Add( "catch" );
540 m_keywords
.Add( "delete" );
541 m_keywords
.Add( "new" );
542 m_keywords
.Add( "default" );
543 m_keywords
.Add( "overload" );
544 m_keywords
.Add( "using" );
545 m_keywords
.Add( "template" );
546 m_keywords
.Add( "try" );
547 m_keywords
.Add( "typedef" );
548 m_keywords
.Add( "union" );
549 m_keywords
.Add( "volatile" );
550 m_keywords
.Add( "asm" );
555 if (m_lang
== wxSOURCE_LANG_PYTHON
)
557 m_defines
.Add( "from" );
558 m_defines
.Add( "import" );
560 if (m_lang
== wxSOURCE_LANG_PERL
)
562 m_defines
.Add( "use" );
563 m_defines
.Add( "do" );
564 m_defines
.Add( "package" );
565 m_defines
.Add( "defined" );
567 if (m_lang
== wxSOURCE_LANG_CPP
)
569 m_defines
.Add( "#define" );
570 m_defines
.Add( "#if" );
571 m_defines
.Add( "#ifndef" );
572 m_defines
.Add( "#ifdef" );
573 m_defines
.Add( "#else" );
574 m_defines
.Add( "#elif" );
575 m_defines
.Add( "#endif" );
576 m_defines
.Add( "#pragma" );
577 m_defines
.Add( "#include" );
582 if (m_lang
== wxSOURCE_LANG_PYTHON
)
584 m_variables
.Add( "nil" );
585 m_variables
.Add( "None" );
586 m_variables
.Add( "self" );
587 m_variables
.Add( "false" );
588 m_variables
.Add( "true" );
590 if (m_lang
== wxSOURCE_LANG_PERL
)
592 m_variables
.Add( "undef" );
593 m_variables
.Add( "class" );
594 m_variables
.Add( "this" );
595 m_variables
.Add( "IN" );
596 m_variables
.Add( "OUT" );
597 m_variables
.Add( "STDIN" );
598 m_variables
.Add( "STDOUT" );
599 m_variables
.Add( "STDERR" );
601 if (m_lang
== wxSOURCE_LANG_CPP
)
603 m_variables
.Add( "int" );
604 m_variables
.Add( "bool" );
605 m_variables
.Add( "void" );
606 m_variables
.Add( "long" );
607 m_variables
.Add( "short" );
608 m_variables
.Add( "const" );
609 m_variables
.Add( "signed" );
610 m_variables
.Add( "unsigned" );
611 m_variables
.Add( "char" );
612 m_variables
.Add( "size_t" );
613 m_variables
.Add( "wchar_t" );
614 m_variables
.Add( "NULL" );
615 m_variables
.Add( "this" );
616 m_variables
.Add( "TRUE" );
617 m_variables
.Add( "FALSE" );
618 m_variables
.Add( "float" );
619 m_variables
.Add( "double" );
620 m_variables
.Add( "register" );
621 m_variables
.Add( "extern" );
622 m_variables
.Add( "static" );
623 m_variables
.Add( "sizeof" );
627 void wxTextCtrl::WriteText(const wxString
& text2
)
629 if (text2
.IsEmpty()) return;
633 wxString
text( text2
);
636 while ( (pos
= text
.Find('\n')) != -1 )
638 lines
.Add( text
.Left( pos
) );
639 text
.Remove( 0, pos
+1 );
642 int count
= (int)lines
.GetCount();
644 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
645 wxString
tmp2( tmp1
);
646 int len
= (int)tmp1
.Len();
651 for (int i
= 0; i
< m_cursorX
-len
; i
++)
653 m_lines
[m_cursorY
].m_text
.Append( tmp
);
658 tmp1
.Remove( m_cursorX
);
659 tmp2
.Remove( 0, m_cursorX
);
660 tmp1
.Append( lines
[0] );
664 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
667 m_lines
[m_cursorY
].m_text
= tmp1
;
668 RefreshLine( m_cursorY
);
672 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
674 m_lines
[m_cursorY
].m_text
= tmp1
;
676 for (i
= 1; i
< count
; i
++)
677 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
678 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
680 MyAdjustScrollbars();
681 RefreshDown( m_cursorY
);
685 void wxTextCtrl::AppendText(const wxString
& text2
)
687 if (text2
.IsEmpty()) return;
691 wxString
text( text2
);
694 while ( (pos
= text
.Find('\n')) != -1 )
696 lines
.Add( text
.Left( pos
) );
697 text
.Remove( 0, pos
+1 );
700 int count
= (int)lines
.GetCount();
702 size_t y
= m_lines
.GetCount()-1;
704 wxString
tmp( m_lines
[y
].m_text
);
705 tmp
.Append( lines
[0] );
709 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
711 m_lines
[y
].m_text
= tmp
;
716 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
718 m_lines
[y
].m_text
= tmp
;
720 for (i
= 1; i
< count
; i
++)
721 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
723 MyAdjustScrollbars();
728 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
733 long wxTextCtrl::XYToPosition(long x
, long y
) const
737 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
741 ret
+= m_lines
[i
].m_text
.Len();
745 if ((size_t)x
< m_lines
[i
].m_text
.Len())
748 return (ret
+ m_lines
[i
].m_text
.Len());
754 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
756 if (m_lines
.GetCount() == 0)
767 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
769 pos
-= m_lines
[i
].m_text
.Len();
781 xx
= m_lines
[ m_lines
.GetCount()-1 ].m_text
.Len();
788 void wxTextCtrl::ShowPosition(long pos
)
792 void wxTextCtrl::Copy()
794 if (!HasSelection()) return;
798 int selStartY
= m_selStartY
;
799 int selEndY
= m_selEndY
;
800 int selStartX
= m_selStartX
;
801 int selEndX
= m_selEndX
;
803 if ((selStartY
> selEndY
) ||
804 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
814 if (selStartY
== selEndY
)
816 sel
= m_lines
[selStartY
].m_text
;
818 if (selStartX
>= (int)sel
.Len()) return;
819 if (selEndX
> (int)sel
.Len())
822 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
823 sel
.Remove( 0, selStartX
);
827 wxString
tmp( m_lines
[selStartY
].m_text
);
829 if (selStartX
< (int)tmp
.Len())
831 tmp
.Remove( 0, selStartX
);
835 for (int i
= selStartY
+1; i
< selEndY
; i
++)
837 sel
.Append( m_lines
[i
].m_text
);
840 tmp
= m_lines
[selEndY
].m_text
;
841 if (selEndX
> (int)tmp
.Len())
845 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
850 if (wxTheClipboard
->Open())
852 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
853 wxTheClipboard
->Close();
857 void wxTextCtrl::Cut()
864 void wxTextCtrl::Paste()
868 if (!wxTheClipboard
->Open()) return;
870 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
872 wxTheClipboard
->Close();
877 wxTextDataObject data
;
879 bool ret
= wxTheClipboard
->GetData( data
);
881 wxTheClipboard
->Close();
887 wxString
text( data
.GetText() );
890 while ( (pos
= text
.Find('\n')) != -1 )
892 lines
.Add( text
.Left( pos
) );
893 text
.Remove( 0, pos
+1 );
896 int count
= (int)lines
.GetCount();
898 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
899 wxString
tmp2( tmp1
);
900 int len
= (int)tmp1
.Len();
905 for (int i
= 0; i
< m_cursorX
-len
; i
++)
907 m_lines
[m_cursorY
].m_text
.Append( tmp
);
912 tmp1
.Remove( m_cursorX
);
913 tmp2
.Remove( 0, m_cursorX
);
914 tmp1
.Append( lines
[0] );
918 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
921 m_lines
[m_cursorY
].m_text
= tmp1
;
922 RefreshLine( m_cursorY
);
926 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
928 m_lines
[m_cursorY
].m_text
= tmp1
;
930 for (i
= 1; i
< count
; i
++)
931 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
932 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
934 MyAdjustScrollbars();
935 RefreshDown( m_cursorY
);
939 void wxTextCtrl::Undo()
941 if (m_undos
.GetCount() == 0) return;
943 wxNode
*node
= m_undos
.Nth( m_undos
.GetCount()-1 );
944 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->Data();
953 void wxTextCtrl::SetInsertionPoint(long pos
)
957 void wxTextCtrl::SetInsertionPointEnd()
961 long wxTextCtrl::GetInsertionPoint() const
963 return XYToPosition( m_cursorX
, m_cursorY
);
966 long wxTextCtrl::GetLastPosition() const
968 size_t lineCount
= m_lines
.GetCount() - 1;
969 return XYToPosition( m_lines
[lineCount
].m_text
.Len()-1, lineCount
);
972 void wxTextCtrl::SetSelection(long from
, long to
)
976 void wxTextCtrl::SetEditable(bool editable
)
978 m_editable
= editable
;
981 bool wxTextCtrl::Enable( bool enable
)
986 bool wxTextCtrl::SetFont(const wxFont
& font
)
988 wxTextCtrlBase::SetFont( font
);
993 dc
.SetFont( m_sourceFont
);
994 m_lineHeight
= dc
.GetCharHeight();
995 m_charWidth
= dc
.GetCharWidth();
997 // TODO: recalc longest lines
999 MyAdjustScrollbars();
1004 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1006 return wxWindow::SetForegroundColour( colour
);
1009 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1011 return wxWindow::SetBackgroundColour( colour
);
1014 //-----------------------------------------------------------------------------
1015 // private code and handlers
1016 //-----------------------------------------------------------------------------
1018 void wxTextCtrl::SearchForBrackets()
1020 int oldBracketY
= m_bracketY
;
1021 int oldBracketX
= m_bracketX
;
1023 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
1025 wxString current
= m_lines
[m_cursorY
].m_text
;
1027 // reverse search first
1032 bracket
= current
[m_cursorX
-1];
1034 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
1036 char antibracket
= '(';
1037 if (bracket
== ']') antibracket
= '[';
1038 if (bracket
== '}') antibracket
= '{';
1042 int endY
= m_cursorY
-60;
1043 if (endY
< 0) endY
= 0;
1044 for (int y
= m_cursorY
; y
>= endY
; y
--)
1046 current
= m_lines
[y
].m_text
;
1048 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
1050 for (int n
= current
.Len()-1; n
>= 0; n
--)
1053 if (current
[n
] == '\'')
1055 for (int m
= n
-1; m
>= 0; m
--)
1057 if (current
[m
] == '\'')
1059 if (m
== 0 || current
[m
-1] != '\\')
1068 if (current
[n
] == '\"')
1070 for (int m
= n
-1; m
>= 0; m
--)
1072 if (current
[m
] == '\"')
1074 if (m
== 0 || current
[m
-1] != '\\')
1082 if (current
[n
] == antibracket
)
1089 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1090 RefreshLine( oldBracketY
);
1091 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1092 RefreshLine( m_bracketY
);
1096 else if (current
[n
] == bracket
)
1107 if ((int)current
.Len() > m_cursorX
)
1108 bracket
= current
[m_cursorX
];
1109 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
1111 char antibracket
= ')';
1112 if (bracket
== '[') antibracket
= ']';
1113 if (bracket
== '{') antibracket
= '}';
1117 int endY
= m_cursorY
+60;
1118 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
1119 for (int y
= m_cursorY
; y
<= endY
; y
++)
1121 current
= m_lines
[y
].m_text
;
1124 start
= m_cursorX
+1;
1126 for (int n
= start
; n
< (int)current
.Len(); n
++)
1129 if (current
[n
] == '\'')
1131 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1133 if (current
[m
] == '\'')
1135 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1144 if (current
[n
] == '\"')
1146 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1148 if (current
[m
] == '\"')
1150 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1158 if (current
[n
] == antibracket
)
1165 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1166 RefreshLine( oldBracketY
);
1167 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1168 RefreshLine( m_bracketY
);
1172 else if (current
[n
] == bracket
)
1180 if (oldBracketY
!= -1)
1183 RefreshLine( oldBracketY
);
1187 void wxTextCtrl::Delete()
1189 if (!HasSelection()) return;
1193 int selStartY
= m_selStartY
;
1194 int selEndY
= m_selEndY
;
1195 int selStartX
= m_selStartX
;
1196 int selEndX
= m_selEndX
;
1198 if ((selStartY
> selEndY
) ||
1199 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1201 int tmp
= selStartX
;
1202 selStartX
= selEndX
;
1205 selStartY
= selEndY
;
1209 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1211 if (selStartY
== selEndY
)
1213 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1215 wxString
tmp( m_lines
[selStartY
].m_text
);
1216 if (selStartX
< len
)
1220 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1221 m_lines
[selStartY
].m_text
= tmp
;
1224 m_cursorX
= selStartX
;
1225 RefreshLine( selStartY
);
1229 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1231 if (selStartX
< len
)
1232 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1234 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1235 m_lines
.RemoveAt( selStartY
+1 );
1237 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1238 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1240 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1242 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1243 m_lines
.RemoveAt( selStartY
+1 );
1246 MoveCursor( selStartX
, selStartY
);
1247 MyAdjustScrollbars();
1249 RefreshDown( selStartY
);
1253 void wxTextCtrl::DeleteLine()
1255 if (HasSelection()) return;
1257 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1259 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1261 m_lines
.RemoveAt( m_cursorY
);
1263 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1265 MyAdjustScrollbars();
1266 RefreshDown( m_cursorY
);
1269 void wxTextCtrl::DoChar( char c
)
1273 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1275 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1277 if (m_cursorX
>= (int)tmp
.Len())
1279 int len
= tmp
.Len();
1280 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1287 tmp
.SetChar( m_cursorX
, c
);
1289 tmp
.insert( m_cursorX
, 1, c
);
1292 m_lines
[m_cursorY
].m_text
= tmp
;
1294 // if (tmp.Len() > m_longestLine)
1296 // m_longestLine = tmp.Len();
1297 // MyAdjustScrollbars();
1301 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1303 if (ww
> m_longestLine
)
1306 MyAdjustScrollbars();
1311 int y
= m_cursorY
*m_lineHeight
;
1312 // int x = (m_cursorX-1)*m_charWidth;
1313 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1314 CalcScrolledPosition( x
, y
, &x
, &y
);
1315 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1316 Refresh( TRUE
, &rect
);
1317 // refresh whole line for syntax colour highlighting
1319 Refresh( FALSE
, &rect
);
1323 GetClientSize( &size_x
, &size_y
);
1324 size_x
/= m_charWidth
;
1328 GetViewStart( &view_x
, &view_y
);
1330 //int xx = m_cursorX;
1331 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1335 else if (xx
> view_x
+size_x
-1)
1336 Scroll( xx
-size_x
+1, -1 );
1339 void wxTextCtrl::DoBack()
1345 if (m_cursorY
== 0) return;
1347 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1349 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1351 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1353 m_cursorX
= tmp1
.Len();
1355 tmp1
.Append( tmp2
);
1356 m_lines
[m_cursorY
].m_text
= tmp1
;
1357 m_lines
.RemoveAt( m_cursorY
+1 );
1359 MyAdjustScrollbars();
1360 RefreshDown( m_cursorY
-1 );
1364 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1366 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1367 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1370 int y
= m_cursorY
*m_lineHeight
;
1371 // int x = m_cursorX*m_charWidth;
1372 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1373 CalcScrolledPosition( x
, y
, &x
, &y
);
1374 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1375 Refresh( TRUE
, &rect
);
1376 // refresh whole line for syntax colour highlighting
1378 Refresh( FALSE
, &rect
);
1382 void wxTextCtrl::DoDelete()
1386 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1388 int len
= (int)tmp
.Len();
1389 if (m_cursorX
>= len
)
1391 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1393 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1395 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1398 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1400 m_lines
[m_cursorY
] = tmp
;
1401 m_lines
.RemoveAt( m_cursorY
+1 );
1403 MyAdjustScrollbars();
1404 RefreshDown( m_cursorY
);
1408 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1410 tmp
.Remove( m_cursorX
, 1 );
1411 m_lines
[m_cursorY
].m_text
= tmp
;
1413 int y
= m_cursorY
*m_lineHeight
;
1414 // int x = m_cursorX*m_charWidth;
1415 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1416 CalcScrolledPosition( x
, y
, &x
, &y
);
1417 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1418 Refresh( TRUE
, &rect
);
1419 // refresh whole line for syntax colour highlighting
1421 Refresh( FALSE
, &rect
);
1425 void wxTextCtrl::DoReturn()
1429 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1431 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1432 size_t indent
= tmp
.find_first_not_of( ' ' );
1433 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1435 if (m_cursorX
>= (int)tmp
.Len())
1437 int cursorX
= indent
;
1438 int cursorY
= m_cursorY
+ 1;
1441 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1442 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1444 MyAdjustScrollbars();
1445 MoveCursor( cursorX
, cursorY
);
1446 RefreshDown( m_cursorY
);
1450 wxString
tmp1( tmp
);
1451 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1452 m_lines
[m_cursorY
].m_text
= tmp1
;
1454 wxString
tmp2( tmp
);
1455 tmp2
.Remove( 0, m_cursorX
);
1457 int cursorX
= indent
;
1458 int cursorY
= m_cursorY
+ 1;
1461 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1462 new_tmp
.Append( tmp2
);
1463 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1465 MyAdjustScrollbars();
1466 MoveCursor( cursorX
, cursorY
);
1467 RefreshDown( m_cursorY
-1 );
1471 void wxTextCtrl::DoDClick()
1473 wxString
line( m_lines
[ m_cursorY
].m_text
);
1474 if (m_cursorX
>= (int)line
.Len()) return;
1477 if (((ch
>= 'a') && (ch
<= 'z')) ||
1478 ((ch
>= 'A') && (ch
<= 'Z')) ||
1479 ((ch
>= '0') && (ch
<= '9')) ||
1482 m_selStartY
= m_cursorY
;
1483 m_selEndY
= m_cursorY
;
1487 while (((ch
>= 'a') && (ch
<= 'z')) ||
1488 ((ch
>= 'A') && (ch
<= 'Z')) ||
1489 ((ch
>= '0') && (ch
<= '9')) ||
1500 if (p
< (int)line
.Len())
1503 while (((ch
>= 'a') && (ch
<= 'z')) ||
1504 ((ch
>= 'A') && (ch
<= 'Z')) ||
1505 ((ch
>= '0') && (ch
<= '9')) ||
1508 if (p
>= (int)line
.Len()) break;
1514 RefreshLine( m_cursorY
);
1518 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1521 size_t len
= line
.Len();
1522 for (size_t p
= pos
; p
< len
; p
++)
1524 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1528 for (size_t q
= p
; q
< len
; q
++)
1529 ret
.Append( line
[q
] );
1536 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[p
+1] == '/'))
1538 for (size_t q
= p
; q
< len
; q
++)
1539 ret
.Append( line
[q
] );
1547 ret
.Append( line
[p
] );
1548 for (size_t q
= p
+1; q
< len
; q
++)
1550 ret
.Append( line
[q
] );
1551 if ((line
[q
] == '"') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1558 if (line
[p
] == '\'')
1560 ret
.Append( line
[p
] );
1561 for (size_t q
= p
+1; q
< len
; q
++)
1563 ret
.Append( line
[q
] );
1564 if ((line
[q
] == '\'') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1571 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1572 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1576 ret
.Append( line
[p
] );
1577 for (size_t q
= p
+1; q
< len
; q
++)
1579 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1580 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1581 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1584 ret
.Append( line
[q
] );
1601 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1606 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1609 size_t len
= origin
.Len();
1610 dc
.SetTextForeground( colour
);
1613 while (toDraw
[pos
] == wxT(' '))
1616 if (pos
== len
) return;
1622 current
+= toDraw
[pos
];
1624 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1626 current
+= toDraw
[pos
];
1631 wxString tmp
= origin
.Left( start
);
1632 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1635 dc
.DrawText( current
, xx
, yy
);
1639 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1641 int selStartY
= m_selStartY
;
1642 int selEndY
= m_selEndY
;
1643 int selStartX
= m_selStartX
;
1644 int selEndX
= m_selEndX
;
1646 if ((selStartY
> selEndY
) ||
1647 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1649 int tmp
= selStartX
;
1650 selStartX
= selEndX
;
1653 selStartY
= selEndY
;
1657 wxString
line( line2
);
1658 if (HasFlag(wxTE_PASSWORD
))
1660 size_t len
= line
.Len();
1661 line
= wxString( wxT('*'), len
);
1664 wxString
keyword( ' ', line
.Len() );
1665 wxString
define( ' ', line
.Len() );
1666 wxString
variable( ' ', line
.Len() );
1667 wxString
comment( ' ', line
.Len() );
1668 wxString
my_string( ' ', line
.Len() );
1669 wxString
selection( ' ', line
.Len() );
1671 if (m_lang
!= wxSOURCE_LANG_NONE
)
1673 if (lineNum
== m_bracketY
)
1675 wxString
red( ' ', line
.Len() );
1676 if (m_bracketX
< (int)line
.Len())
1678 red
.SetChar( m_bracketX
, line
[m_bracketX
] );
1679 line
.SetChar( m_bracketX
, ' ' );
1680 dc
.SetTextForeground( *wxRED
);
1681 dc
.DrawText( red
, x
, y
);
1682 dc
.SetTextForeground( *wxBLACK
);
1687 wxString
token( GetNextToken( line
, pos
) );
1688 while (!token
.IsNull())
1690 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1692 size_t end_pos
= pos
+ token
.Len();
1693 for (size_t i
= pos
; i
< end_pos
; i
++)
1695 keyword
[i
] = line
[i
];
1699 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1701 size_t end_pos
= pos
+ token
.Len();
1702 for (size_t i
= pos
; i
< end_pos
; i
++)
1704 define
[i
] = line
[i
];
1708 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1709 ((token
.Len() > 2) && (token
[0] == 'w') && (token
[1] == 'x')))
1711 size_t end_pos
= pos
+ token
.Len();
1712 for (size_t i
= pos
; i
< end_pos
; i
++)
1714 variable
[i
] = line
[i
];
1718 if ((token
.Len() >= 2) && (token
[0] == '/') && (token
[1] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1720 size_t end_pos
= pos
+ token
.Len();
1721 for (size_t i
= pos
; i
< end_pos
; i
++)
1723 comment
[i
] = line
[i
];
1727 if ((token
[0] == '#') &&
1728 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1730 size_t end_pos
= pos
+ token
.Len();
1731 for (size_t i
= pos
; i
< end_pos
; i
++)
1733 comment
[i
] = line
[i
];
1737 if ((token
[0] == '"') || (token
[0] == '\''))
1739 size_t end_pos
= pos
+ token
.Len();
1740 for (size_t i
= pos
; i
< end_pos
; i
++)
1742 my_string
[i
] = line
[i
];
1747 token
= GetNextToken( line
, pos
);
1751 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1753 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1754 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1755 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1756 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1757 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1758 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1759 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1763 if (selStartY
== selEndY
)
1765 // int xx = selStartX*m_charWidth;
1766 int xx
= PosToPixel( lineNum
, selStartX
);
1767 // int ww = (selEndX-selStartX)*m_charWidth;
1768 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1769 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1771 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1773 selection
[i
] = line
[i
];
1777 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1779 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1781 for (size_t i
= 0; i
< line
.Len(); i
++)
1783 selection
[i
] = line
[i
];
1787 if (lineNum
== selStartY
)
1789 // int xx = selStartX*m_charWidth;
1790 int xx
= PosToPixel( lineNum
, selStartX
);
1791 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1793 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1795 selection
[i
] = line
[i
];
1799 if (lineNum
== selEndY
)
1801 // int ww = selEndX*m_charWidth;
1802 int ww
= PosToPixel( lineNum
, selEndX
);
1803 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1805 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1807 selection
[i
] = line
[i
];
1812 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1813 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1814 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1815 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1816 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1817 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1818 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1821 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1825 if (m_lines
.GetCount() == 0) return;
1829 dc
.SetFont( m_sourceFont
);
1832 GetViewStart( NULL
, &scroll_y
);
1834 // We have a inner border of two pixels
1835 // around the text, so scroll units do
1836 // not correspond to lines.
1837 if (scroll_y
> 0) scroll_y
--;
1841 GetClientSize( &size_x
, &size_y
);
1843 dc
.SetPen( *wxTRANSPARENT_PEN
);
1844 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1845 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+2 );
1846 for (int i
= scroll_y
; i
< upper
; i
++)
1849 int y
= i
*m_lineHeight
+2;
1851 int h
= m_lineHeight
;
1852 CalcScrolledPosition( x
,y
,&x
,&y
);
1853 if (IsExposed(x
,y
,w
,h
))
1854 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1859 dc
.SetBrush( *wxRED_BRUSH
);
1860 // int xx = m_cursorX*m_charWidth;
1861 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1862 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1866 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1868 if (m_lines
.GetCount() == 0) return;
1871 #if 0 // there is no middle button on iPAQs
1872 if (event
.MiddleDown())
1879 if (event
.LeftDClick())
1885 if (event
.LeftDown())
1893 m_capturing
= FALSE
;
1897 if (event
.LeftDown() ||
1898 (event
.LeftIsDown() && m_capturing
))
1900 int x
= event
.GetX();
1901 int y
= event
.GetY();
1902 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1904 // x /= m_charWidth;
1905 x
= PixelToPos( y
, x
);
1907 wxMin( 1000, wxMax( 0, x
) ),
1908 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1909 event
.ShiftDown() || !event
.LeftDown() );
1913 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1915 if (m_lines
.GetCount() == 0) return;
1917 if (!m_editable
) return;
1921 GetClientSize( &size_x
, &size_y
);
1922 size_x
/= m_charWidth
;
1923 size_y
/= m_lineHeight
;
1926 if (event
.ShiftDown())
1928 switch (event
.GetKeyCode())
1930 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1931 case '8': event
.m_keyCode
= WXK_UP
; break;
1932 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1933 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1934 case '9': event
.m_keyCode
= WXK_PRIOR
; break;
1935 case '3': event
.m_keyCode
= WXK_NEXT
; break;
1936 case '7': event
.m_keyCode
= WXK_HOME
; break;
1937 case '1': event
.m_keyCode
= WXK_END
; break;
1938 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1942 switch (event
.GetKeyCode())
1946 if (m_ignoreInput
) return;
1948 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1949 m_ignoreInput
= TRUE
;
1954 if (m_ignoreInput
) return;
1955 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1956 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1957 m_ignoreInput
= TRUE
;
1962 if (m_ignoreInput
) return;
1965 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1970 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1972 m_ignoreInput
= TRUE
;
1977 if (m_ignoreInput
) return;
1978 if (m_cursorX
< 1000)
1979 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1980 m_ignoreInput
= TRUE
;
1985 if (event
.ControlDown())
1986 MoveCursor( 0, 0, event
.ShiftDown() );
1988 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
1993 if (event
.ControlDown())
1994 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
1996 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
2001 if (m_ignoreInput
) return;
2002 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
2003 m_ignoreInput
= TRUE
;
2008 if (m_ignoreInput
) return;
2009 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
2010 m_ignoreInput
= TRUE
;
2015 if (event
.ShiftDown())
2017 else if (event
.ControlDown())
2020 m_overwrite
= !m_overwrite
;
2025 if (m_windowStyle
& wxPROCESS_ENTER
)
2027 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
2028 event
.SetEventObject(this);
2029 event
.SetString(GetValue());
2030 if (GetEventHandler()->ProcessEvent(event
)) return;
2048 bool save_overwrite
= m_overwrite
;
2049 m_overwrite
= FALSE
;
2050 int i
= 4-(m_cursorX
% 4);
2052 for (int c
= 0; c
< i
; c
++)
2054 m_overwrite
= save_overwrite
;
2075 if ( (event
.KeyCode() >= 'a') &&
2076 (event
.KeyCode() <= 'z') &&
2084 if ( (event
.KeyCode() >= 32) &&
2085 (event
.KeyCode() <= 255) &&
2086 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
2090 DoChar( (char) event
.KeyCode() );
2099 void wxTextCtrl::OnIdle( wxIdleEvent
&event
)
2101 m_ignoreInput
= FALSE
;
2103 if (m_lang
!= wxSOURCE_LANG_NONE
)
2104 SearchForBrackets();
2109 void wxTextCtrl::Indent()
2111 int startY
= m_cursorY
;
2112 int endY
= m_cursorY
;
2115 startY
= m_selStartY
;
2125 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2127 for (int i
= startY
; i
<= endY
; i
++)
2129 m_lines
[i
].m_text
.insert( 0u, " " );
2134 void wxTextCtrl::Unindent()
2136 int startY
= m_cursorY
;
2137 int endY
= m_cursorY
;
2140 startY
= m_selStartY
;
2150 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2152 for (int i
= startY
; i
<= endY
; i
++)
2154 for (int n
= 0; n
< 4; n
++)
2156 if (m_lines
[i
].m_text
[0u] == ' ')
2157 m_lines
[i
].m_text
.erase(0u,1u);
2162 bool wxTextCtrl::HasSelection()
2164 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2167 void wxTextCtrl::ClearSelection()
2175 void wxTextCtrl::RefreshLine( int n
)
2177 int y
= n
*m_lineHeight
;
2179 CalcScrolledPosition( x
, y
, &x
, &y
);
2180 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2181 Refresh( TRUE
, &rect
);
2184 void wxTextCtrl::RefreshDown( int n
)
2188 GetClientSize( &size_x
, &size_y
);
2192 GetViewStart( &view_x
, &view_y
);
2200 int y
= n
*m_lineHeight
;
2202 CalcScrolledPosition( x
, y
, &x
, &y
);
2204 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2205 Refresh( TRUE
, &rect
);
2209 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2211 if (!m_editable
) return;
2213 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
2215 if (new_x
> m_lines
[new_y
].m_text
.Len())
2216 new_x
= m_lines
[new_y
].m_text
.Len();
2219 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2221 bool no_cursor_refresh
= FALSE
;
2222 bool has_selection
= HasSelection();
2230 m_selStartX
= m_cursorX
;
2231 m_selStartY
= m_cursorY
;
2235 if (new_y
> m_selStartY
)
2237 y
= m_selStartY
*m_lineHeight
;
2238 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2240 else if (new_y
== m_selStartY
)
2242 x
= PosToPixel( new_y
, m_selStartX
);
2243 w
= PosToPixel( new_y
, new_x
) - x
;
2247 w
= -w
+ 2; // +2 for the cursor
2249 y
= m_selStartY
*m_lineHeight
;
2254 y
= new_y
*m_lineHeight
;
2255 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2258 no_cursor_refresh
= TRUE
;
2264 if (new_y
== m_selEndY
)
2266 y
= new_y
*m_lineHeight
;
2268 if (m_selEndX
> new_x
)
2270 // x = new_x*m_charWidth;
2271 x
= PosToPixel( new_y
, new_x
);
2272 // w = (m_selEndX-new_x)*m_charWidth;
2273 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2277 // x = m_selEndX*m_charWidth;
2278 x
= PosToPixel( new_y
, m_selEndX
);
2279 // w = (-m_selEndX+new_x)*m_charWidth;
2280 w
= PosToPixel( new_y
, new_x
) - x
;
2287 if (new_y
> m_selEndY
)
2289 y
= m_selEndY
*m_lineHeight
;
2290 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2294 y
= new_y
*m_lineHeight
;
2295 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2297 no_cursor_refresh
= TRUE
;
2306 CalcScrolledPosition( x
, y
, &x
, &y
);
2307 wxRect
rect( x
+2, y
+2, w
, h
);
2308 Refresh( TRUE
, &rect
);
2314 int ry1
= m_selEndY
;
2315 int ry2
= m_selStartY
;
2329 int y
= ry1
*m_lineHeight
;
2330 CalcScrolledPosition( x
, y
, &x
, &y
);
2331 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2333 Refresh( TRUE
, &rect
);
2338 printf( "startx %d starty %d endx %d endy %d\n",
2339 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2341 printf( "has %d\n", (int)HasSelection() );
2344 if (!no_cursor_refresh
)
2346 // int x = m_cursorX*m_charWidth;
2347 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2348 int y
= m_cursorY
*m_lineHeight
;
2349 CalcScrolledPosition( x
, y
, &x
, &y
);
2350 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2355 Refresh( TRUE
, &rect
);
2357 wxClientDC
dc(this);
2359 dc
.SetPen( *wxTRANSPARENT_PEN
);
2360 dc
.SetBrush( *wxRED_BRUSH
);
2361 // int xx = m_cursorX*m_charWidth;
2362 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2363 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2368 GetClientSize( &size_x
, &size_y
);
2369 size_x
/= m_charWidth
;
2370 size_y
/= m_lineHeight
;
2374 GetViewStart( &view_x
, &view_y
);
2378 int sy
= m_cursorY
- (size_y
/2);
2384 if (m_cursorY
< view_y
)
2385 Scroll( -1, m_cursorY
);
2386 else if (m_cursorY
> view_y
+size_y
-1)
2387 Scroll( -1, m_cursorY
-size_y
+1 );
2390 //int xx = m_cursorX;
2391 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2395 else if (xx
> view_x
+size_x
-1)
2396 Scroll( xx
-size_x
+1, -1 );
2399 void wxTextCtrl::MyAdjustScrollbars()
2404 int y_range
= m_lines
.GetCount();
2407 GetClientSize( NULL
, &height
);
2409 if (height
>= m_lines
.GetCount() *m_lineHeight
)
2414 GetViewStart( &view_x
, &view_y
);
2416 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2419 //-----------------------------------------------------------------------------
2420 // clipboard handlers
2421 //-----------------------------------------------------------------------------
2423 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2428 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2433 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2438 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2443 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2448 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2450 event
.Enable( CanCut() );
2453 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2455 event
.Enable( CanCopy() );
2458 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2460 event
.Enable( CanPaste() );
2463 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2465 event
.Enable( CanUndo() );
2468 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2470 event
.Enable( CanRedo() );
2473 wxSize
wxTextCtrl::DoGetBestSize() const
2477 wxSize
ret(80, m_lineHeight
+ 4);
2479 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2482 if (HasFlag(wxBORDER_SIMPLE
))
2489 return wxSize(80, 60);
2493 // ----------------------------------------------------------------------------
2495 // ----------------------------------------------------------------------------
2497 void wxTextCtrl::Freeze()
2501 void wxTextCtrl::Thaw()
2505 // ----------------------------------------------------------------------------
2506 // text control scrolling
2507 // ----------------------------------------------------------------------------
2509 bool wxTextCtrl::ScrollLines(int lines
)
2511 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2516 bool wxTextCtrl::ScrollPages(int pages
)
2518 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");