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 if (HasFlag(wxTE_PASSWORD
))
225 m_sourceFont
= wxFont( 12, wxMODERN
, wxNORMAL
, wxNORMAL
);
227 m_sourceFont
= GetFont();
230 dc
.SetFont( m_sourceFont
);
231 m_lineHeight
= dc
.GetCharHeight();
232 m_charWidth
= dc
.GetCharWidth();
236 wxSize
size_best( DoGetBestSize() );
237 wxSize
new_size( size
);
238 if (new_size
.x
== -1)
239 new_size
.x
= size_best
.x
;
240 if (new_size
.y
== -1)
241 new_size
.y
= size_best
.y
;
242 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
243 SetSize( new_size
.x
, new_size
.y
);
245 // We create an input handler since it might be useful
246 CreateInputHandler(wxINP_HANDLER_TEXTCTRL
);
251 //-----------------------------------------------------------------------------
253 //-----------------------------------------------------------------------------
255 wxString
wxTextCtrl::GetValue() const
258 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
260 ret
+= m_lines
[i
].m_text
;
261 if (i
+1 < m_lines
.GetCount())
268 void wxTextCtrl::SetValue(const wxString
& value
)
279 m_lines
.Add( new wxSourceLine( wxT("") ) );
287 pos
= value
.find( wxT('\n'), begin
);
290 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, value
.Len()-begin
) );
293 // if (sl->m_text.Len() > m_longestLine)
294 // m_longestLine = sl->m_text.Len();
296 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
298 if (ww
> m_longestLine
)
305 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, pos
-begin
) );
308 // if (sl->m_text.Len() > m_longestLine)
309 // m_longestLine = sl->m_text.Len();
311 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
313 if (ww
> m_longestLine
)
321 MyAdjustScrollbars();
326 int wxTextCtrl::GetLineLength(long lineNo
) const
328 if (lineNo
>= (long)m_lines
.GetCount())
331 return m_lines
[lineNo
].m_text
.Len();
334 wxString
wxTextCtrl::GetLineText(long lineNo
) const
336 if (lineNo
>= (long)m_lines
.GetCount())
339 return m_lines
[lineNo
].m_text
;
342 int wxTextCtrl::GetNumberOfLines() const
344 return m_lines
.GetCount();
347 bool wxTextCtrl::IsModified() const
352 bool wxTextCtrl::IsEditable() const
357 void wxTextCtrl::GetSelection(long* from
, long* to
) const
361 void wxTextCtrl::Clear()
369 m_lines
.Add( new wxSourceLine( wxT("") ) );
371 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
376 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
380 void wxTextCtrl::Remove(long from
, long to
)
385 void wxTextCtrl::DiscardEdits()
391 void wxTextCtrl::SetMaxLength(unsigned long len
)
395 int wxTextCtrl::PosToPixel( int line
, int pos
)
397 // TODO add support for Tabs
399 if (line
>= (int)m_lines
.GetCount()) return 0;
400 if (pos
< 0) return 0;
402 wxString text
= m_lines
[line
].m_text
;
404 if (text
.IsEmpty()) return 0;
406 if (pos
< (int)text
.Len())
407 text
.Remove( pos
, text
.Len()-pos
);
411 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
416 int wxTextCtrl::PixelToPos( int line
, int pixel
)
418 if (pixel
< 2) return 0;
420 if (line
>= (int)m_lines
.GetCount()) return 0;
422 wxString text
= m_lines
[line
].m_text
;
425 int res
= text
.Len();
428 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
434 text
.Remove( res
,1 );
440 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
446 if (m_lang
== wxSOURCE_LANG_PYTHON
)
448 m_keywords
.Add( "class" );
449 m_keywords
.Add( "__init__" );
450 m_keywords
.Add( "return" );
451 m_keywords
.Add( "def" );
452 m_keywords
.Add( "try" );
453 m_keywords
.Add( "except" );
454 m_keywords
.Add( "if" );
455 m_keywords
.Add( "else" );
456 m_keywords
.Add( "finally" );
457 m_keywords
.Add( "for" );
458 m_keywords
.Add( "if" );
459 m_keywords
.Add( "elif" );
460 m_keywords
.Add( "in" );
461 m_keywords
.Add( "and" );
462 m_keywords
.Add( "del" );
463 m_keywords
.Add( "is" );
464 m_keywords
.Add( "raise" );
465 m_keywords
.Add( "assert" );
466 m_keywords
.Add( "lambda" );
467 m_keywords
.Add( "break" );
468 m_keywords
.Add( "global" );
469 m_keywords
.Add( "not" );
470 m_keywords
.Add( "or" );
471 m_keywords
.Add( "while" );
472 m_keywords
.Add( "continue" );
473 m_keywords
.Add( "exec" );
474 m_keywords
.Add( "pass" );
475 m_keywords
.Add( "print" );
477 if (m_lang
== wxSOURCE_LANG_PERL
)
479 m_keywords
.Add( "main" );
480 m_keywords
.Add( "sub" );
481 m_keywords
.Add( "shift" );
482 m_keywords
.Add( "push" );
483 m_keywords
.Add( "split" );
484 m_keywords
.Add( "join" );
485 m_keywords
.Add( "chop" );
486 m_keywords
.Add( "grep" );
487 m_keywords
.Add( "open" );
488 m_keywords
.Add( "print" );
489 m_keywords
.Add( "sprint" );
490 m_keywords
.Add( "printf" );
491 m_keywords
.Add( "sprintf" );
492 m_keywords
.Add( "my" );
493 m_keywords
.Add( "local" );
494 m_keywords
.Add( "exit" );
495 m_keywords
.Add( "die" );
496 m_keywords
.Add( "return" );
497 m_keywords
.Add( "for" );
498 m_keywords
.Add( "foreach" );
499 m_keywords
.Add( "while" );
500 m_keywords
.Add( "unless" );
501 m_keywords
.Add( "if" );
502 m_keywords
.Add( "next" );
503 m_keywords
.Add( "last" );
504 m_keywords
.Add( "else" );
505 m_keywords
.Add( "elsif" );
506 m_keywords
.Add( "ne" );
507 m_keywords
.Add( "qe" );
510 if (m_lang
== wxSOURCE_LANG_CPP
)
512 m_keywords
.Add( "class" );
513 m_keywords
.Add( "return" );
514 m_keywords
.Add( "if" );
515 m_keywords
.Add( "then" );
516 m_keywords
.Add( "else" );
517 m_keywords
.Add( "struct" );
518 m_keywords
.Add( "enum" );
519 m_keywords
.Add( "while" );
520 m_keywords
.Add( "do" );
521 m_keywords
.Add( "for" );
522 m_keywords
.Add( "continue" );
523 m_keywords
.Add( "break" );
524 m_keywords
.Add( "switch" );
525 m_keywords
.Add( "case" );
526 m_keywords
.Add( "goto" );
527 m_keywords
.Add( "label" );
528 m_keywords
.Add( "inline" );
529 m_keywords
.Add( "operator" );
530 m_keywords
.Add( "virtual" );
531 m_keywords
.Add( "private" );
532 m_keywords
.Add( "public" );
533 m_keywords
.Add( "protected" );
534 m_keywords
.Add( "friend" );
535 m_keywords
.Add( "exception" );
536 m_keywords
.Add( "throw" );
537 m_keywords
.Add( "catch" );
538 m_keywords
.Add( "delete" );
539 m_keywords
.Add( "new" );
540 m_keywords
.Add( "default" );
541 m_keywords
.Add( "overload" );
542 m_keywords
.Add( "using" );
543 m_keywords
.Add( "template" );
544 m_keywords
.Add( "try" );
545 m_keywords
.Add( "typedef" );
546 m_keywords
.Add( "union" );
547 m_keywords
.Add( "volatile" );
548 m_keywords
.Add( "asm" );
553 if (m_lang
== wxSOURCE_LANG_PYTHON
)
555 m_defines
.Add( "from" );
556 m_defines
.Add( "import" );
558 if (m_lang
== wxSOURCE_LANG_PERL
)
560 m_defines
.Add( "use" );
561 m_defines
.Add( "do" );
562 m_defines
.Add( "package" );
563 m_defines
.Add( "defined" );
565 if (m_lang
== wxSOURCE_LANG_CPP
)
567 m_defines
.Add( "#define" );
568 m_defines
.Add( "#if" );
569 m_defines
.Add( "#ifndef" );
570 m_defines
.Add( "#ifdef" );
571 m_defines
.Add( "#else" );
572 m_defines
.Add( "#elif" );
573 m_defines
.Add( "#endif" );
574 m_defines
.Add( "#pragma" );
575 m_defines
.Add( "#include" );
580 if (m_lang
== wxSOURCE_LANG_PYTHON
)
582 m_variables
.Add( "nil" );
583 m_variables
.Add( "None" );
584 m_variables
.Add( "self" );
585 m_variables
.Add( "false" );
586 m_variables
.Add( "true" );
588 if (m_lang
== wxSOURCE_LANG_PERL
)
590 m_variables
.Add( "undef" );
591 m_variables
.Add( "class" );
592 m_variables
.Add( "this" );
593 m_variables
.Add( "IN" );
594 m_variables
.Add( "OUT" );
595 m_variables
.Add( "STDIN" );
596 m_variables
.Add( "STDOUT" );
597 m_variables
.Add( "STDERR" );
599 if (m_lang
== wxSOURCE_LANG_CPP
)
601 m_variables
.Add( "int" );
602 m_variables
.Add( "bool" );
603 m_variables
.Add( "void" );
604 m_variables
.Add( "long" );
605 m_variables
.Add( "short" );
606 m_variables
.Add( "const" );
607 m_variables
.Add( "signed" );
608 m_variables
.Add( "unsigned" );
609 m_variables
.Add( "char" );
610 m_variables
.Add( "size_t" );
611 m_variables
.Add( "wchar_t" );
612 m_variables
.Add( "NULL" );
613 m_variables
.Add( "this" );
614 m_variables
.Add( "TRUE" );
615 m_variables
.Add( "FALSE" );
616 m_variables
.Add( "float" );
617 m_variables
.Add( "double" );
618 m_variables
.Add( "register" );
619 m_variables
.Add( "extern" );
620 m_variables
.Add( "static" );
621 m_variables
.Add( "sizeof" );
625 void wxTextCtrl::WriteText(const wxString
& text2
)
627 if (text2
.IsEmpty()) return;
631 wxString
text( text2
);
634 while ( (pos
= text
.Find('\n')) != -1 )
636 lines
.Add( text
.Left( pos
) );
637 text
.Remove( 0, pos
+1 );
640 int count
= (int)lines
.GetCount();
642 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
643 wxString
tmp2( tmp1
);
644 int len
= (int)tmp1
.Len();
649 for (int i
= 0; i
< m_cursorX
-len
; i
++)
651 m_lines
[m_cursorY
].m_text
.Append( tmp
);
656 tmp1
.Remove( m_cursorX
);
657 tmp2
.Remove( 0, m_cursorX
);
658 tmp1
.Append( lines
[0] );
662 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
665 m_lines
[m_cursorY
].m_text
= tmp1
;
666 RefreshLine( m_cursorY
);
670 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
672 m_lines
[m_cursorY
].m_text
= tmp1
;
674 for (i
= 1; i
< count
; i
++)
675 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
676 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
678 MyAdjustScrollbars();
679 RefreshDown( m_cursorY
);
683 void wxTextCtrl::AppendText(const wxString
& text2
)
685 if (text2
.IsEmpty()) return;
689 wxString
text( text2
);
692 while ( (pos
= text
.Find('\n')) != -1 )
694 lines
.Add( text
.Left( pos
) );
695 text
.Remove( 0, pos
+1 );
698 int count
= (int)lines
.GetCount();
700 size_t y
= m_lines
.GetCount()-1;
702 wxString
tmp( m_lines
[y
].m_text
);
703 tmp
.Append( lines
[0] );
707 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
709 m_lines
[y
].m_text
= tmp
;
714 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
716 m_lines
[y
].m_text
= tmp
;
718 for (i
= 1; i
< count
; i
++)
719 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
721 MyAdjustScrollbars();
726 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
731 long wxTextCtrl::XYToPosition(long x
, long y
) const
735 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
739 ret
+= m_lines
[i
].m_text
.Len();
743 if ((size_t)x
< m_lines
[i
].m_text
.Len())
746 return (ret
+ m_lines
[i
].m_text
.Len());
752 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
754 if (m_lines
.GetCount() == 0)
765 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
767 pos
-= m_lines
[i
].m_text
.Len();
779 xx
= m_lines
[ m_lines
.GetCount()-1 ].m_text
.Len();
786 void wxTextCtrl::ShowPosition(long pos
)
790 void wxTextCtrl::Copy()
792 if (!HasSelection()) return;
796 int selStartY
= m_selStartY
;
797 int selEndY
= m_selEndY
;
798 int selStartX
= m_selStartX
;
799 int selEndX
= m_selEndX
;
801 if ((selStartY
> selEndY
) ||
802 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
812 if (selStartY
== selEndY
)
814 sel
= m_lines
[selStartY
].m_text
;
816 if (selStartX
>= (int)sel
.Len()) return;
817 if (selEndX
> (int)sel
.Len())
820 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
821 sel
.Remove( 0, selStartX
);
825 wxString
tmp( m_lines
[selStartY
].m_text
);
827 if (selStartX
< (int)tmp
.Len())
829 tmp
.Remove( 0, selStartX
);
833 for (int i
= selStartY
+1; i
< selEndY
; i
++)
835 sel
.Append( m_lines
[i
].m_text
);
838 tmp
= m_lines
[selEndY
].m_text
;
839 if (selEndX
> (int)tmp
.Len())
843 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
848 if (wxTheClipboard
->Open())
850 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
851 wxTheClipboard
->Close();
855 void wxTextCtrl::Cut()
862 void wxTextCtrl::Paste()
866 if (!wxTheClipboard
->Open()) return;
868 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
870 wxTheClipboard
->Close();
875 wxTextDataObject data
;
877 bool ret
= wxTheClipboard
->GetData( data
);
879 wxTheClipboard
->Close();
885 wxString
text( data
.GetText() );
888 while ( (pos
= text
.Find('\n')) != -1 )
890 lines
.Add( text
.Left( pos
) );
891 text
.Remove( 0, pos
+1 );
894 int count
= (int)lines
.GetCount();
896 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
897 wxString
tmp2( tmp1
);
898 int len
= (int)tmp1
.Len();
903 for (int i
= 0; i
< m_cursorX
-len
; i
++)
905 m_lines
[m_cursorY
].m_text
.Append( tmp
);
910 tmp1
.Remove( m_cursorX
);
911 tmp2
.Remove( 0, m_cursorX
);
912 tmp1
.Append( lines
[0] );
916 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
919 m_lines
[m_cursorY
].m_text
= tmp1
;
920 RefreshLine( m_cursorY
);
924 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
926 m_lines
[m_cursorY
].m_text
= tmp1
;
928 for (i
= 1; i
< count
; i
++)
929 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
930 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
932 MyAdjustScrollbars();
933 RefreshDown( m_cursorY
);
937 void wxTextCtrl::Undo()
939 if (m_undos
.GetCount() == 0) return;
941 wxNode
*node
= m_undos
.Nth( m_undos
.GetCount()-1 );
942 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->Data();
951 void wxTextCtrl::SetInsertionPoint(long pos
)
955 void wxTextCtrl::SetInsertionPointEnd()
959 long wxTextCtrl::GetInsertionPoint() const
961 return XYToPosition( m_cursorX
, m_cursorY
);
964 long wxTextCtrl::GetLastPosition() const
966 size_t lineCount
= m_lines
.GetCount() - 1;
967 return XYToPosition( m_lines
[lineCount
].m_text
.Len()-1, lineCount
);
970 void wxTextCtrl::SetSelection(long from
, long to
)
974 void wxTextCtrl::SetEditable(bool editable
)
976 m_editable
= editable
;
979 bool wxTextCtrl::Enable( bool enable
)
984 bool wxTextCtrl::SetFont(const wxFont
& font
)
989 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
991 return wxWindow::SetForegroundColour( colour
);
994 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
996 return wxWindow::SetBackgroundColour( colour
);
999 //-----------------------------------------------------------------------------
1000 // private code and handlers
1001 //-----------------------------------------------------------------------------
1003 void wxTextCtrl::SearchForBrackets()
1005 int oldBracketY
= m_bracketY
;
1006 int oldBracketX
= m_bracketX
;
1008 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
1010 wxString current
= m_lines
[m_cursorY
].m_text
;
1012 // reverse search first
1017 bracket
= current
[m_cursorX
-1];
1019 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
1021 char antibracket
= '(';
1022 if (bracket
== ']') antibracket
= '[';
1023 if (bracket
== '}') antibracket
= '{';
1027 int endY
= m_cursorY
-60;
1028 if (endY
< 0) endY
= 0;
1029 for (int y
= m_cursorY
; y
>= endY
; y
--)
1031 current
= m_lines
[y
].m_text
;
1033 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
1035 for (int n
= current
.Len()-1; n
>= 0; n
--)
1038 if (current
[n
] == '\'')
1040 for (int m
= n
-1; m
>= 0; m
--)
1042 if (current
[m
] == '\'')
1044 if (m
== 0 || current
[m
-1] != '\\')
1053 if (current
[n
] == '\"')
1055 for (int m
= n
-1; m
>= 0; m
--)
1057 if (current
[m
] == '\"')
1059 if (m
== 0 || current
[m
-1] != '\\')
1067 if (current
[n
] == antibracket
)
1074 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1075 RefreshLine( oldBracketY
);
1076 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1077 RefreshLine( m_bracketY
);
1081 else if (current
[n
] == bracket
)
1092 if ((int)current
.Len() > m_cursorX
)
1093 bracket
= current
[m_cursorX
];
1094 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
1096 char antibracket
= ')';
1097 if (bracket
== '[') antibracket
= ']';
1098 if (bracket
== '{') antibracket
= '}';
1102 int endY
= m_cursorY
+60;
1103 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
1104 for (int y
= m_cursorY
; y
<= endY
; y
++)
1106 current
= m_lines
[y
].m_text
;
1109 start
= m_cursorX
+1;
1111 for (int n
= start
; n
< (int)current
.Len(); n
++)
1114 if (current
[n
] == '\'')
1116 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1118 if (current
[m
] == '\'')
1120 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
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] == '\\'))
1143 if (current
[n
] == antibracket
)
1150 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1151 RefreshLine( oldBracketY
);
1152 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1153 RefreshLine( m_bracketY
);
1157 else if (current
[n
] == bracket
)
1165 if (oldBracketY
!= -1)
1168 RefreshLine( oldBracketY
);
1172 void wxTextCtrl::Delete()
1174 if (!HasSelection()) return;
1178 int selStartY
= m_selStartY
;
1179 int selEndY
= m_selEndY
;
1180 int selStartX
= m_selStartX
;
1181 int selEndX
= m_selEndX
;
1183 if ((selStartY
> selEndY
) ||
1184 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1186 int tmp
= selStartX
;
1187 selStartX
= selEndX
;
1190 selStartY
= selEndY
;
1194 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1196 if (selStartY
== selEndY
)
1198 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1200 wxString
tmp( m_lines
[selStartY
].m_text
);
1201 if (selStartX
< len
)
1205 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1206 m_lines
[selStartY
].m_text
= tmp
;
1209 m_cursorX
= selStartX
;
1210 RefreshLine( selStartY
);
1214 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1216 if (selStartX
< len
)
1217 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1219 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1220 m_lines
.RemoveAt( selStartY
+1 );
1222 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1223 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1225 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1227 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1228 m_lines
.RemoveAt( selStartY
+1 );
1231 MoveCursor( selStartX
, selStartY
);
1232 MyAdjustScrollbars();
1234 RefreshDown( selStartY
);
1238 void wxTextCtrl::DeleteLine()
1240 if (HasSelection()) return;
1242 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1244 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1246 m_lines
.RemoveAt( m_cursorY
);
1248 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1250 MyAdjustScrollbars();
1251 RefreshDown( m_cursorY
);
1254 void wxTextCtrl::DoChar( char c
)
1258 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1260 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1262 if (m_cursorX
>= (int)tmp
.Len())
1264 int len
= tmp
.Len();
1265 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1272 tmp
.SetChar( m_cursorX
, c
);
1274 tmp
.insert( m_cursorX
, 1, c
);
1277 m_lines
[m_cursorY
].m_text
= tmp
;
1279 // if (tmp.Len() > m_longestLine)
1281 // m_longestLine = tmp.Len();
1282 // MyAdjustScrollbars();
1286 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1288 if (ww
> m_longestLine
)
1291 MyAdjustScrollbars();
1296 int y
= m_cursorY
*m_lineHeight
;
1297 // int x = (m_cursorX-1)*m_charWidth;
1298 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1299 CalcScrolledPosition( x
, y
, &x
, &y
);
1300 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1301 Refresh( TRUE
, &rect
);
1302 // refresh whole line for syntax colour highlighting
1304 Refresh( FALSE
, &rect
);
1308 GetClientSize( &size_x
, &size_y
);
1309 size_x
/= m_charWidth
;
1313 GetViewStart( &view_x
, &view_y
);
1315 //int xx = m_cursorX;
1316 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1320 else if (xx
> view_x
+size_x
-1)
1321 Scroll( xx
-size_x
+1, -1 );
1324 void wxTextCtrl::DoBack()
1330 if (m_cursorY
== 0) return;
1332 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1334 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1336 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1338 m_cursorX
= tmp1
.Len();
1340 tmp1
.Append( tmp2
);
1341 m_lines
[m_cursorY
].m_text
= tmp1
;
1342 m_lines
.RemoveAt( m_cursorY
+1 );
1344 MyAdjustScrollbars();
1345 RefreshDown( m_cursorY
-1 );
1349 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1351 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1352 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1355 int y
= m_cursorY
*m_lineHeight
;
1356 // int x = m_cursorX*m_charWidth;
1357 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1358 CalcScrolledPosition( x
, y
, &x
, &y
);
1359 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1360 Refresh( TRUE
, &rect
);
1361 // refresh whole line for syntax colour highlighting
1363 Refresh( FALSE
, &rect
);
1367 void wxTextCtrl::DoDelete()
1371 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1373 int len
= (int)tmp
.Len();
1374 if (m_cursorX
>= len
)
1376 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1378 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1380 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1383 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1385 m_lines
[m_cursorY
] = tmp
;
1386 m_lines
.RemoveAt( m_cursorY
+1 );
1388 MyAdjustScrollbars();
1389 RefreshDown( m_cursorY
);
1393 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1395 tmp
.Remove( m_cursorX
, 1 );
1396 m_lines
[m_cursorY
].m_text
= tmp
;
1398 int y
= m_cursorY
*m_lineHeight
;
1399 // int x = m_cursorX*m_charWidth;
1400 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1401 CalcScrolledPosition( x
, y
, &x
, &y
);
1402 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1403 Refresh( TRUE
, &rect
);
1404 // refresh whole line for syntax colour highlighting
1406 Refresh( FALSE
, &rect
);
1410 void wxTextCtrl::DoReturn()
1414 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1416 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1417 size_t indent
= tmp
.find_first_not_of( ' ' );
1418 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1420 if (m_cursorX
>= (int)tmp
.Len())
1422 int cursorX
= indent
;
1423 int cursorY
= m_cursorY
+ 1;
1426 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1427 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1429 MyAdjustScrollbars();
1430 MoveCursor( cursorX
, cursorY
);
1431 RefreshDown( m_cursorY
);
1435 wxString
tmp1( tmp
);
1436 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1437 m_lines
[m_cursorY
].m_text
= tmp1
;
1439 wxString
tmp2( tmp
);
1440 tmp2
.Remove( 0, m_cursorX
);
1442 int cursorX
= indent
;
1443 int cursorY
= m_cursorY
+ 1;
1446 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1447 new_tmp
.Append( tmp2
);
1448 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1450 MyAdjustScrollbars();
1451 MoveCursor( cursorX
, cursorY
);
1452 RefreshDown( m_cursorY
-1 );
1456 void wxTextCtrl::DoDClick()
1458 wxString
line( m_lines
[ m_cursorY
].m_text
);
1459 if (m_cursorX
>= (int)line
.Len()) return;
1462 if (((ch
>= 'a') && (ch
<= 'z')) ||
1463 ((ch
>= 'A') && (ch
<= 'Z')) ||
1464 ((ch
>= '0') && (ch
<= '9')) ||
1467 m_selStartY
= m_cursorY
;
1468 m_selEndY
= m_cursorY
;
1472 while (((ch
>= 'a') && (ch
<= 'z')) ||
1473 ((ch
>= 'A') && (ch
<= 'Z')) ||
1474 ((ch
>= '0') && (ch
<= '9')) ||
1485 if (p
< (int)line
.Len())
1488 while (((ch
>= 'a') && (ch
<= 'z')) ||
1489 ((ch
>= 'A') && (ch
<= 'Z')) ||
1490 ((ch
>= '0') && (ch
<= '9')) ||
1493 if (p
>= (int)line
.Len()) break;
1499 RefreshLine( m_cursorY
);
1503 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1506 size_t len
= line
.Len();
1507 for (size_t p
= pos
; p
< len
; p
++)
1509 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1513 for (size_t q
= p
; q
< len
; q
++)
1514 ret
.Append( line
[q
] );
1521 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[p
+1] == '/'))
1523 for (size_t q
= p
; q
< len
; q
++)
1524 ret
.Append( line
[q
] );
1532 ret
.Append( line
[p
] );
1533 for (size_t q
= p
+1; q
< len
; q
++)
1535 ret
.Append( line
[q
] );
1536 if ((line
[q
] == '"') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1543 if (line
[p
] == '\'')
1545 ret
.Append( line
[p
] );
1546 for (size_t q
= p
+1; q
< len
; q
++)
1548 ret
.Append( line
[q
] );
1549 if ((line
[q
] == '\'') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1556 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1557 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1561 ret
.Append( line
[p
] );
1562 for (size_t q
= p
+1; q
< len
; q
++)
1564 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1565 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1566 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1569 ret
.Append( line
[q
] );
1586 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1591 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1594 size_t len
= origin
.Len();
1595 dc
.SetTextForeground( colour
);
1598 while (toDraw
[pos
] == wxT(' '))
1601 if (pos
== len
) return;
1607 current
+= toDraw
[pos
];
1609 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1611 current
+= toDraw
[pos
];
1616 wxString tmp
= origin
.Left( start
);
1617 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1620 dc
.DrawText( current
, xx
, yy
);
1624 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1626 int selStartY
= m_selStartY
;
1627 int selEndY
= m_selEndY
;
1628 int selStartX
= m_selStartX
;
1629 int selEndX
= m_selEndX
;
1631 if ((selStartY
> selEndY
) ||
1632 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1634 int tmp
= selStartX
;
1635 selStartX
= selEndX
;
1638 selStartY
= selEndY
;
1642 wxString
line( line2
);
1643 if (HasFlag(wxTE_PASSWORD
))
1645 size_t len
= line
.Len();
1646 line
= wxString( wxT('*'), len
);
1649 wxString
keyword( ' ', line
.Len() );
1650 wxString
define( ' ', line
.Len() );
1651 wxString
variable( ' ', line
.Len() );
1652 wxString
comment( ' ', line
.Len() );
1653 wxString
my_string( ' ', line
.Len() );
1654 wxString
selection( ' ', line
.Len() );
1656 if (m_lang
!= wxSOURCE_LANG_NONE
)
1658 if (lineNum
== m_bracketY
)
1660 wxString
red( ' ', line
.Len() );
1661 if (m_bracketX
< (int)line
.Len())
1663 red
.SetChar( m_bracketX
, line
[m_bracketX
] );
1664 line
.SetChar( m_bracketX
, ' ' );
1665 dc
.SetTextForeground( *wxRED
);
1666 dc
.DrawText( red
, x
, y
);
1667 dc
.SetTextForeground( *wxBLACK
);
1672 wxString
token( GetNextToken( line
, pos
) );
1673 while (!token
.IsNull())
1675 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1677 size_t end_pos
= pos
+ token
.Len();
1678 for (size_t i
= pos
; i
< end_pos
; i
++)
1680 keyword
[i
] = line
[i
];
1684 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1686 size_t end_pos
= pos
+ token
.Len();
1687 for (size_t i
= pos
; i
< end_pos
; i
++)
1689 define
[i
] = line
[i
];
1693 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1694 ((token
.Len() > 2) && (token
[0] == 'w') && (token
[1] == 'x')))
1696 size_t end_pos
= pos
+ token
.Len();
1697 for (size_t i
= pos
; i
< end_pos
; i
++)
1699 variable
[i
] = line
[i
];
1703 if ((token
.Len() >= 2) && (token
[0] == '/') && (token
[1] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1705 size_t end_pos
= pos
+ token
.Len();
1706 for (size_t i
= pos
; i
< end_pos
; i
++)
1708 comment
[i
] = line
[i
];
1712 if ((token
[0] == '#') &&
1713 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1715 size_t end_pos
= pos
+ token
.Len();
1716 for (size_t i
= pos
; i
< end_pos
; i
++)
1718 comment
[i
] = line
[i
];
1722 if ((token
[0] == '"') || (token
[0] == '\''))
1724 size_t end_pos
= pos
+ token
.Len();
1725 for (size_t i
= pos
; i
< end_pos
; i
++)
1727 my_string
[i
] = line
[i
];
1732 token
= GetNextToken( line
, pos
);
1736 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1738 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1739 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1740 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1741 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1742 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1743 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1744 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1748 if (selStartY
== selEndY
)
1750 // int xx = selStartX*m_charWidth;
1751 int xx
= PosToPixel( lineNum
, selStartX
);
1752 // int ww = (selEndX-selStartX)*m_charWidth;
1753 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1754 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1756 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1758 selection
[i
] = line
[i
];
1762 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1764 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1766 for (size_t i
= 0; i
< line
.Len(); i
++)
1768 selection
[i
] = line
[i
];
1772 if (lineNum
== selStartY
)
1774 // int xx = selStartX*m_charWidth;
1775 int xx
= PosToPixel( lineNum
, selStartX
);
1776 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1778 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1780 selection
[i
] = line
[i
];
1784 if (lineNum
== selEndY
)
1786 // int ww = selEndX*m_charWidth;
1787 int ww
= PosToPixel( lineNum
, selEndX
);
1788 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1790 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1792 selection
[i
] = line
[i
];
1797 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1798 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1799 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1800 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1801 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1802 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1803 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1806 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1810 if (m_lines
.GetCount() == 0) return;
1814 dc
.SetFont( m_sourceFont
);
1817 GetViewStart( NULL
, &scroll_y
);
1821 GetClientSize( &size_x
, &size_y
);
1823 dc
.SetPen( *wxTRANSPARENT_PEN
);
1824 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1825 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+1 );
1826 for (int i
= scroll_y
; i
< upper
; i
++)
1829 int y
= i
*m_lineHeight
+2;
1831 int h
= m_lineHeight
;
1832 CalcScrolledPosition( x
,y
,&x
,&y
);
1833 if (IsExposed(x
,y
,w
,h
))
1834 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1837 dc
.SetBrush( *wxRED_BRUSH
);
1838 // int xx = m_cursorX*m_charWidth;
1839 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1840 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1843 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1845 if (m_lines
.GetCount() == 0) return;
1848 #if 0 // there is no middle button on iPAQs
1849 if (event
.MiddleDown())
1856 if (event
.LeftDClick())
1862 if (event
.LeftDown())
1870 m_capturing
= FALSE
;
1874 if (event
.LeftDown() ||
1875 (event
.LeftIsDown() && m_capturing
))
1877 int x
= event
.GetX();
1878 int y
= event
.GetY();
1879 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1881 // x /= m_charWidth;
1882 x
= PixelToPos( y
, x
);
1884 wxMin( 1000, wxMax( 0, x
) ),
1885 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1886 event
.ShiftDown() || !event
.LeftDown() );
1890 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1892 if (m_lines
.GetCount() == 0) return;
1896 GetClientSize( &size_x
, &size_y
);
1897 size_x
/= m_charWidth
;
1898 size_y
/= m_lineHeight
;
1901 if (event
.ShiftDown())
1903 switch (event
.GetKeyCode())
1905 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1906 case '8': event
.m_keyCode
= WXK_UP
; break;
1907 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1908 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1909 case '9': event
.m_keyCode
= WXK_PRIOR
; break;
1910 case '3': event
.m_keyCode
= WXK_NEXT
; break;
1911 case '7': event
.m_keyCode
= WXK_HOME
; break;
1912 case '1': event
.m_keyCode
= WXK_END
; break;
1913 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1917 switch (event
.GetKeyCode())
1921 if (m_ignoreInput
) return;
1923 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1924 m_ignoreInput
= TRUE
;
1929 if (m_ignoreInput
) return;
1930 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1931 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1932 m_ignoreInput
= TRUE
;
1937 if (m_ignoreInput
) return;
1940 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1945 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1947 m_ignoreInput
= TRUE
;
1952 if (m_ignoreInput
) return;
1953 if (m_cursorX
< 1000)
1954 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1955 m_ignoreInput
= TRUE
;
1960 if (event
.ControlDown())
1961 MoveCursor( 0, 0, event
.ShiftDown() );
1963 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
1968 if (event
.ControlDown())
1969 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
1971 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
1976 if (m_ignoreInput
) return;
1977 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
1978 m_ignoreInput
= TRUE
;
1983 if (m_ignoreInput
) return;
1984 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
1985 m_ignoreInput
= TRUE
;
1990 if (event
.ShiftDown())
1992 else if (event
.ControlDown())
1995 m_overwrite
= !m_overwrite
;
2000 if (m_windowStyle
& wxPROCESS_ENTER
)
2002 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
2003 event
.SetEventObject(this);
2004 event
.SetString(GetValue());
2005 if (GetEventHandler()->ProcessEvent(event
)) return;
2023 bool save_overwrite
= m_overwrite
;
2024 m_overwrite
= FALSE
;
2025 int i
= 4-(m_cursorX
% 4);
2027 for (int c
= 0; c
< i
; c
++)
2029 m_overwrite
= save_overwrite
;
2050 if ( (event
.KeyCode() >= 'a') &&
2051 (event
.KeyCode() <= 'z') &&
2059 if ( (event
.KeyCode() >= 32) &&
2060 (event
.KeyCode() <= 255) &&
2061 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
2065 DoChar( (char) event
.KeyCode() );
2074 void wxTextCtrl::OnIdle( wxIdleEvent
&event
)
2076 m_ignoreInput
= FALSE
;
2078 if (m_lang
!= wxSOURCE_LANG_NONE
)
2079 SearchForBrackets();
2084 void wxTextCtrl::Indent()
2086 int startY
= m_cursorY
;
2087 int endY
= m_cursorY
;
2090 startY
= m_selStartY
;
2100 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2102 for (int i
= startY
; i
<= endY
; i
++)
2104 m_lines
[i
].m_text
.insert( 0u, " " );
2109 void wxTextCtrl::Unindent()
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 for (int n
= 0; n
< 4; n
++)
2131 if (m_lines
[i
].m_text
[0u] == ' ')
2132 m_lines
[i
].m_text
.erase(0u,1u);
2137 bool wxTextCtrl::HasSelection()
2139 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2142 void wxTextCtrl::ClearSelection()
2150 void wxTextCtrl::RefreshLine( int n
)
2152 int y
= n
*m_lineHeight
;
2154 CalcScrolledPosition( x
, y
, &x
, &y
);
2155 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2156 Refresh( TRUE
, &rect
);
2159 void wxTextCtrl::RefreshDown( int n
)
2163 GetClientSize( &size_x
, &size_y
);
2167 GetViewStart( &view_x
, &view_y
);
2175 int y
= n
*m_lineHeight
;
2177 CalcScrolledPosition( x
, y
, &x
, &y
);
2179 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2180 Refresh( TRUE
, &rect
);
2184 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2186 // if (IsSingleLine())
2188 if (new_x
> m_lines
[new_y
].m_text
.Len())
2189 new_x
= m_lines
[new_y
].m_text
.Len();
2192 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2194 bool no_cursor_refresh
= FALSE
;
2195 bool has_selection
= HasSelection();
2203 m_selStartX
= m_cursorX
;
2204 m_selStartY
= m_cursorY
;
2208 if (new_y
> m_selStartY
)
2210 y
= m_selStartY
*m_lineHeight
;
2211 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2213 else if (new_y
== m_selStartY
)
2215 x
= PosToPixel( new_y
, m_selStartX
);
2216 w
= PosToPixel( new_y
, new_x
) - x
;
2220 w
= -w
+ 2; // +2 for the cursor
2222 y
= m_selStartY
*m_lineHeight
;
2227 y
= new_y
*m_lineHeight
;
2228 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2231 no_cursor_refresh
= TRUE
;
2237 if (new_y
== m_selEndY
)
2239 y
= new_y
*m_lineHeight
;
2241 if (m_selEndX
> new_x
)
2243 // x = new_x*m_charWidth;
2244 x
= PosToPixel( new_y
, new_x
);
2245 // w = (m_selEndX-new_x)*m_charWidth;
2246 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2250 // x = m_selEndX*m_charWidth;
2251 x
= PosToPixel( new_y
, m_selEndX
);
2252 // w = (-m_selEndX+new_x)*m_charWidth;
2253 w
= PosToPixel( new_y
, new_x
) - x
;
2260 if (new_y
> m_selEndY
)
2262 y
= m_selEndY
*m_lineHeight
;
2263 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2267 y
= new_y
*m_lineHeight
;
2268 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2270 no_cursor_refresh
= TRUE
;
2279 CalcScrolledPosition( x
, y
, &x
, &y
);
2280 wxRect
rect( x
+2, y
+2, w
, h
);
2281 Refresh( TRUE
, &rect
);
2287 int ry1
= m_selEndY
;
2288 int ry2
= m_selStartY
;
2302 int y
= ry1
*m_lineHeight
;
2303 CalcScrolledPosition( x
, y
, &x
, &y
);
2304 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2306 Refresh( TRUE
, &rect
);
2311 printf( "startx %d starty %d endx %d endy %d\n",
2312 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2314 printf( "has %d\n", (int)HasSelection() );
2317 if (!no_cursor_refresh
)
2319 // int x = m_cursorX*m_charWidth;
2320 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2321 int y
= m_cursorY
*m_lineHeight
;
2322 CalcScrolledPosition( x
, y
, &x
, &y
);
2323 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2328 Refresh( TRUE
, &rect
);
2330 wxClientDC
dc(this);
2332 dc
.SetPen( *wxTRANSPARENT_PEN
);
2333 dc
.SetBrush( *wxRED_BRUSH
);
2334 // int xx = m_cursorX*m_charWidth;
2335 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2336 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2341 GetClientSize( &size_x
, &size_y
);
2342 size_x
/= m_charWidth
;
2343 size_y
/= m_lineHeight
;
2347 GetViewStart( &view_x
, &view_y
);
2351 int sy
= m_cursorY
- (size_y
/2);
2357 if (m_cursorY
< view_y
)
2358 Scroll( -1, m_cursorY
);
2359 else if (m_cursorY
> view_y
+size_y
-1)
2360 Scroll( -1, m_cursorY
-size_y
+1 );
2363 //int xx = m_cursorX;
2364 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2368 else if (xx
> view_x
+size_x
-1)
2369 Scroll( xx
-size_x
+1, -1 );
2372 void wxTextCtrl::MyAdjustScrollbars()
2377 int y_range
= m_lines
.GetCount();
2380 GetClientSize( NULL
, &height
);
2382 if (height
>= m_lines
.GetCount() *m_lineHeight
)
2387 GetViewStart( &view_x
, &view_y
);
2389 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2392 //-----------------------------------------------------------------------------
2393 // clipboard handlers
2394 //-----------------------------------------------------------------------------
2396 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2401 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2406 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2411 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2416 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2421 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2423 event
.Enable( CanCut() );
2426 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2428 event
.Enable( CanCopy() );
2431 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2433 event
.Enable( CanPaste() );
2436 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2438 event
.Enable( CanUndo() );
2441 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2443 event
.Enable( CanRedo() );
2446 wxSize
wxTextCtrl::DoGetBestSize() const
2450 wxSize
ret(80, m_lineHeight
+ 4);
2452 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2455 if (HasFlag(wxBORDER_SIMPLE
))
2462 return wxSize(80, 60);
2466 // ----------------------------------------------------------------------------
2468 // ----------------------------------------------------------------------------
2470 void wxTextCtrl::Freeze()
2474 void wxTextCtrl::Thaw()
2478 // ----------------------------------------------------------------------------
2479 // text control scrolling
2480 // ----------------------------------------------------------------------------
2482 bool wxTextCtrl::ScrollLines(int lines
)
2484 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2489 bool wxTextCtrl::ScrollPages(int pages
)
2491 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");