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
)
138 EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus
)
139 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
141 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
142 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
143 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
144 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
145 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
147 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
148 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
149 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
150 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
151 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
154 void wxTextCtrl::Init()
159 m_undos
.DeleteContents( TRUE
);
161 m_lang
= wxSOURCE_LANG_NONE
;
174 m_ignoreInput
= FALSE
;
178 m_keywordColour
= wxColour( 10, 140, 10 );
180 m_defineColour
= *wxRED
;
182 m_variableColour
= wxColour( 50, 120, 150 );
184 m_commentColour
= wxColour( 130, 130, 130 );
186 m_stringColour
= wxColour( 10, 140, 10 );
189 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
191 const wxString
&value
,
195 const wxValidator
& validator
,
196 const wxString
&name
)
197 : wxScrollHelper(this)
201 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
204 bool wxTextCtrl::Create( wxWindow
*parent
,
206 const wxString
&value
,
210 const wxValidator
& validator
,
211 const wxString
&name
)
213 if ((style
& wxBORDER_MASK
) == 0)
214 style
|= wxBORDER_SUNKEN
;
216 if ((style
& wxTE_MULTILINE
) != 0)
217 style
|= wxALWAYS_SHOW_SB
;
219 wxTextCtrlBase::Create( parent
, id
, pos
/* wxDefaultPosition */, size
,
220 style
|wxVSCROLL
|wxHSCROLL
|wxNO_FULL_REPAINT_ON_RESIZE
);
222 SetBackgroundColour( *wxWHITE
);
224 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
226 m_editable
= ((m_windowStyle
& wxTE_READONLY
) == 0);
228 if (HasFlag(wxTE_PASSWORD
))
229 m_sourceFont
= wxFont( 12, wxMODERN
, wxNORMAL
, wxNORMAL
);
231 m_sourceFont
= GetFont();
234 dc
.SetFont( m_sourceFont
);
235 m_lineHeight
= dc
.GetCharHeight();
236 m_charWidth
= dc
.GetCharWidth();
240 wxSize
size_best( DoGetBestSize() );
241 wxSize
new_size( size
);
242 if (new_size
.x
== -1)
243 new_size
.x
= size_best
.x
;
244 if (new_size
.y
== -1)
245 new_size
.y
= size_best
.y
;
246 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
247 SetSize( new_size
.x
, new_size
.y
);
249 // We create an input handler since it might be useful
250 CreateInputHandler(wxINP_HANDLER_TEXTCTRL
);
255 //-----------------------------------------------------------------------------
257 //-----------------------------------------------------------------------------
259 wxString
wxTextCtrl::GetValue() const
262 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
264 ret
+= m_lines
[i
].m_text
;
265 if (i
+1 < m_lines
.GetCount())
272 void wxTextCtrl::SetValue(const wxString
& value
)
276 if ((GetWindowStyle() & wxTE_MULTILINE
) == 0)
278 if (value
== GetValue())
290 m_lines
.Add( new wxSourceLine( wxT("") ) );
298 pos
= value
.find( wxT('\n'), begin
);
301 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, value
.Len()-begin
) );
304 // if (sl->m_text.Len() > m_longestLine)
305 // m_longestLine = sl->m_text.Len();
307 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
309 if (ww
> m_longestLine
)
316 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, pos
-begin
) );
319 // if (sl->m_text.Len() > m_longestLine)
320 // m_longestLine = sl->m_text.Len();
322 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
324 if (ww
> m_longestLine
)
332 MyAdjustScrollbars();
337 int wxTextCtrl::GetLineLength(long lineNo
) const
339 if (lineNo
>= (long)m_lines
.GetCount())
342 return m_lines
[lineNo
].m_text
.Len();
345 wxString
wxTextCtrl::GetLineText(long lineNo
) const
347 if (lineNo
>= (long)m_lines
.GetCount())
350 return m_lines
[lineNo
].m_text
;
353 int wxTextCtrl::GetNumberOfLines() const
355 return m_lines
.GetCount();
358 bool wxTextCtrl::IsModified() const
363 bool wxTextCtrl::IsEditable() const
368 void wxTextCtrl::GetSelection(long* from
, long* to
) const
372 void wxTextCtrl::Clear()
380 m_lines
.Add( new wxSourceLine( wxT("") ) );
382 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
387 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
391 void wxTextCtrl::Remove(long from
, long to
)
396 void wxTextCtrl::DiscardEdits()
402 void wxTextCtrl::SetMaxLength(unsigned long len
)
406 int wxTextCtrl::PosToPixel( int line
, int pos
)
408 // TODO add support for Tabs
410 if (line
>= (int)m_lines
.GetCount()) return 0;
411 if (pos
< 0) return 0;
413 wxString text
= m_lines
[line
].m_text
;
415 if (text
.IsEmpty()) return 0;
417 if (pos
< (int)text
.Len())
418 text
.Remove( pos
, text
.Len()-pos
);
422 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
427 int wxTextCtrl::PixelToPos( int line
, int pixel
)
429 if (pixel
< 2) return 0;
431 if (line
>= (int)m_lines
.GetCount()) return 0;
433 wxString text
= m_lines
[line
].m_text
;
436 int res
= text
.Len();
439 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
445 text
.Remove( res
,1 );
451 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
457 if (m_lang
== wxSOURCE_LANG_PYTHON
)
459 m_keywords
.Add( "class" );
460 m_keywords
.Add( "__init__" );
461 m_keywords
.Add( "return" );
462 m_keywords
.Add( "def" );
463 m_keywords
.Add( "try" );
464 m_keywords
.Add( "except" );
465 m_keywords
.Add( "if" );
466 m_keywords
.Add( "else" );
467 m_keywords
.Add( "finally" );
468 m_keywords
.Add( "for" );
469 m_keywords
.Add( "if" );
470 m_keywords
.Add( "elif" );
471 m_keywords
.Add( "in" );
472 m_keywords
.Add( "and" );
473 m_keywords
.Add( "del" );
474 m_keywords
.Add( "is" );
475 m_keywords
.Add( "raise" );
476 m_keywords
.Add( "assert" );
477 m_keywords
.Add( "lambda" );
478 m_keywords
.Add( "break" );
479 m_keywords
.Add( "global" );
480 m_keywords
.Add( "not" );
481 m_keywords
.Add( "or" );
482 m_keywords
.Add( "while" );
483 m_keywords
.Add( "continue" );
484 m_keywords
.Add( "exec" );
485 m_keywords
.Add( "pass" );
486 m_keywords
.Add( "print" );
488 if (m_lang
== wxSOURCE_LANG_PERL
)
490 m_keywords
.Add( "main" );
491 m_keywords
.Add( "sub" );
492 m_keywords
.Add( "shift" );
493 m_keywords
.Add( "push" );
494 m_keywords
.Add( "split" );
495 m_keywords
.Add( "join" );
496 m_keywords
.Add( "chop" );
497 m_keywords
.Add( "grep" );
498 m_keywords
.Add( "open" );
499 m_keywords
.Add( "print" );
500 m_keywords
.Add( "sprint" );
501 m_keywords
.Add( "printf" );
502 m_keywords
.Add( "sprintf" );
503 m_keywords
.Add( "my" );
504 m_keywords
.Add( "local" );
505 m_keywords
.Add( "exit" );
506 m_keywords
.Add( "die" );
507 m_keywords
.Add( "return" );
508 m_keywords
.Add( "for" );
509 m_keywords
.Add( "foreach" );
510 m_keywords
.Add( "while" );
511 m_keywords
.Add( "unless" );
512 m_keywords
.Add( "if" );
513 m_keywords
.Add( "next" );
514 m_keywords
.Add( "last" );
515 m_keywords
.Add( "else" );
516 m_keywords
.Add( "elsif" );
517 m_keywords
.Add( "ne" );
518 m_keywords
.Add( "qe" );
521 if (m_lang
== wxSOURCE_LANG_CPP
)
523 m_keywords
.Add( "class" );
524 m_keywords
.Add( "return" );
525 m_keywords
.Add( "if" );
526 m_keywords
.Add( "then" );
527 m_keywords
.Add( "else" );
528 m_keywords
.Add( "struct" );
529 m_keywords
.Add( "enum" );
530 m_keywords
.Add( "while" );
531 m_keywords
.Add( "do" );
532 m_keywords
.Add( "for" );
533 m_keywords
.Add( "continue" );
534 m_keywords
.Add( "break" );
535 m_keywords
.Add( "switch" );
536 m_keywords
.Add( "case" );
537 m_keywords
.Add( "goto" );
538 m_keywords
.Add( "label" );
539 m_keywords
.Add( "inline" );
540 m_keywords
.Add( "operator" );
541 m_keywords
.Add( "virtual" );
542 m_keywords
.Add( "private" );
543 m_keywords
.Add( "public" );
544 m_keywords
.Add( "protected" );
545 m_keywords
.Add( "friend" );
546 m_keywords
.Add( "exception" );
547 m_keywords
.Add( "throw" );
548 m_keywords
.Add( "catch" );
549 m_keywords
.Add( "delete" );
550 m_keywords
.Add( "new" );
551 m_keywords
.Add( "default" );
552 m_keywords
.Add( "overload" );
553 m_keywords
.Add( "using" );
554 m_keywords
.Add( "template" );
555 m_keywords
.Add( "try" );
556 m_keywords
.Add( "typedef" );
557 m_keywords
.Add( "union" );
558 m_keywords
.Add( "volatile" );
559 m_keywords
.Add( "asm" );
564 if (m_lang
== wxSOURCE_LANG_PYTHON
)
566 m_defines
.Add( "from" );
567 m_defines
.Add( "import" );
569 if (m_lang
== wxSOURCE_LANG_PERL
)
571 m_defines
.Add( "use" );
572 m_defines
.Add( "do" );
573 m_defines
.Add( "package" );
574 m_defines
.Add( "defined" );
576 if (m_lang
== wxSOURCE_LANG_CPP
)
578 m_defines
.Add( "#define" );
579 m_defines
.Add( "#if" );
580 m_defines
.Add( "#ifndef" );
581 m_defines
.Add( "#ifdef" );
582 m_defines
.Add( "#else" );
583 m_defines
.Add( "#elif" );
584 m_defines
.Add( "#endif" );
585 m_defines
.Add( "#pragma" );
586 m_defines
.Add( "#include" );
591 if (m_lang
== wxSOURCE_LANG_PYTHON
)
593 m_variables
.Add( "nil" );
594 m_variables
.Add( "None" );
595 m_variables
.Add( "self" );
596 m_variables
.Add( "false" );
597 m_variables
.Add( "true" );
599 if (m_lang
== wxSOURCE_LANG_PERL
)
601 m_variables
.Add( "undef" );
602 m_variables
.Add( "class" );
603 m_variables
.Add( "this" );
604 m_variables
.Add( "IN" );
605 m_variables
.Add( "OUT" );
606 m_variables
.Add( "STDIN" );
607 m_variables
.Add( "STDOUT" );
608 m_variables
.Add( "STDERR" );
610 if (m_lang
== wxSOURCE_LANG_CPP
)
612 m_variables
.Add( "int" );
613 m_variables
.Add( "bool" );
614 m_variables
.Add( "void" );
615 m_variables
.Add( "long" );
616 m_variables
.Add( "short" );
617 m_variables
.Add( "const" );
618 m_variables
.Add( "signed" );
619 m_variables
.Add( "unsigned" );
620 m_variables
.Add( "char" );
621 m_variables
.Add( "size_t" );
622 m_variables
.Add( "wchar_t" );
623 m_variables
.Add( "NULL" );
624 m_variables
.Add( "this" );
625 m_variables
.Add( "TRUE" );
626 m_variables
.Add( "FALSE" );
627 m_variables
.Add( "float" );
628 m_variables
.Add( "double" );
629 m_variables
.Add( "register" );
630 m_variables
.Add( "extern" );
631 m_variables
.Add( "static" );
632 m_variables
.Add( "sizeof" );
636 void wxTextCtrl::WriteText(const wxString
& text2
)
638 if (text2
.IsEmpty()) return;
642 wxString
text( text2
);
645 while ( (pos
= text
.Find('\n')) != -1 )
647 lines
.Add( text
.Left( pos
) );
648 text
.Remove( 0, pos
+1 );
651 int count
= (int)lines
.GetCount();
653 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
654 wxString
tmp2( tmp1
);
655 int len
= (int)tmp1
.Len();
660 for (int i
= 0; i
< m_cursorX
-len
; i
++)
662 m_lines
[m_cursorY
].m_text
.Append( tmp
);
667 tmp1
.Remove( m_cursorX
);
668 tmp2
.Remove( 0, m_cursorX
);
669 tmp1
.Append( lines
[0] );
673 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
676 m_lines
[m_cursorY
].m_text
= tmp1
;
677 RefreshLine( m_cursorY
);
681 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
683 m_lines
[m_cursorY
].m_text
= tmp1
;
685 for (i
= 1; i
< count
; i
++)
686 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
687 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
689 MyAdjustScrollbars();
690 RefreshDown( m_cursorY
);
694 void wxTextCtrl::AppendText(const wxString
& text2
)
696 if (text2
.IsEmpty()) return;
700 wxString
text( text2
);
703 while ( (pos
= text
.Find('\n')) != -1 )
705 lines
.Add( text
.Left( pos
) );
706 text
.Remove( 0, pos
+1 );
709 int count
= (int)lines
.GetCount();
711 size_t y
= m_lines
.GetCount()-1;
713 wxString
tmp( m_lines
[y
].m_text
);
714 tmp
.Append( lines
[0] );
718 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
720 m_lines
[y
].m_text
= tmp
;
725 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
727 m_lines
[y
].m_text
= tmp
;
729 for (i
= 1; i
< count
; i
++)
730 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
732 MyAdjustScrollbars();
737 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
742 long wxTextCtrl::XYToPosition(long x
, long y
) const
746 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
750 ret
+= m_lines
[i
].m_text
.Len();
754 if ((size_t)x
< m_lines
[i
].m_text
.Len())
757 return (ret
+ m_lines
[i
].m_text
.Len());
763 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
765 if (m_lines
.GetCount() == 0)
776 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
778 pos
-= m_lines
[i
].m_text
.Len();
790 xx
= m_lines
[ m_lines
.GetCount()-1 ].m_text
.Len();
797 void wxTextCtrl::ShowPosition(long pos
)
801 void wxTextCtrl::Copy()
803 if (!HasSelection()) return;
807 int selStartY
= m_selStartY
;
808 int selEndY
= m_selEndY
;
809 int selStartX
= m_selStartX
;
810 int selEndX
= m_selEndX
;
812 if ((selStartY
> selEndY
) ||
813 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
823 if (selStartY
== selEndY
)
825 sel
= m_lines
[selStartY
].m_text
;
827 if (selStartX
>= (int)sel
.Len()) return;
828 if (selEndX
> (int)sel
.Len())
831 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
832 sel
.Remove( 0, selStartX
);
836 wxString
tmp( m_lines
[selStartY
].m_text
);
838 if (selStartX
< (int)tmp
.Len())
840 tmp
.Remove( 0, selStartX
);
844 for (int i
= selStartY
+1; i
< selEndY
; i
++)
846 sel
.Append( m_lines
[i
].m_text
);
849 tmp
= m_lines
[selEndY
].m_text
;
850 if (selEndX
> (int)tmp
.Len())
854 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
859 if (wxTheClipboard
->Open())
861 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
862 wxTheClipboard
->Close();
866 void wxTextCtrl::Cut()
873 void wxTextCtrl::Paste()
877 if (!wxTheClipboard
->Open()) return;
879 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
881 wxTheClipboard
->Close();
886 wxTextDataObject data
;
888 bool ret
= wxTheClipboard
->GetData( data
);
890 wxTheClipboard
->Close();
896 wxString
text( data
.GetText() );
899 while ( (pos
= text
.Find('\n')) != -1 )
901 lines
.Add( text
.Left( pos
) );
902 text
.Remove( 0, pos
+1 );
905 int count
= (int)lines
.GetCount();
907 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
908 wxString
tmp2( tmp1
);
909 int len
= (int)tmp1
.Len();
914 for (int i
= 0; i
< m_cursorX
-len
; i
++)
916 m_lines
[m_cursorY
].m_text
.Append( tmp
);
921 tmp1
.Remove( m_cursorX
);
922 tmp2
.Remove( 0, m_cursorX
);
923 tmp1
.Append( lines
[0] );
927 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
930 m_lines
[m_cursorY
].m_text
= tmp1
;
931 RefreshLine( m_cursorY
);
935 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
937 m_lines
[m_cursorY
].m_text
= tmp1
;
939 for (i
= 1; i
< count
; i
++)
940 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
941 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
943 MyAdjustScrollbars();
944 RefreshDown( m_cursorY
);
948 void wxTextCtrl::Undo()
950 if (m_undos
.GetCount() == 0) return;
952 wxNode
*node
= m_undos
.Nth( m_undos
.GetCount()-1 );
953 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->Data();
962 void wxTextCtrl::SetInsertionPoint(long pos
)
966 void wxTextCtrl::SetInsertionPointEnd()
970 long wxTextCtrl::GetInsertionPoint() const
972 return XYToPosition( m_cursorX
, m_cursorY
);
975 long wxTextCtrl::GetLastPosition() const
977 size_t lineCount
= m_lines
.GetCount() - 1;
978 return XYToPosition( m_lines
[lineCount
].m_text
.Len()-1, lineCount
);
981 void wxTextCtrl::SetSelection(long from
, long to
)
985 void wxTextCtrl::SetEditable(bool editable
)
987 m_editable
= editable
;
990 bool wxTextCtrl::Enable( bool enable
)
995 bool wxTextCtrl::SetFont(const wxFont
& font
)
997 wxTextCtrlBase::SetFont( font
);
1001 wxClientDC
dc(this);
1002 dc
.SetFont( m_sourceFont
);
1003 m_lineHeight
= dc
.GetCharHeight();
1004 m_charWidth
= dc
.GetCharWidth();
1006 // TODO: recalc longest lines
1008 MyAdjustScrollbars();
1013 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1015 return wxWindow::SetForegroundColour( colour
);
1018 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1020 return wxWindow::SetBackgroundColour( colour
);
1023 //-----------------------------------------------------------------------------
1024 // private code and handlers
1025 //-----------------------------------------------------------------------------
1027 void wxTextCtrl::SearchForBrackets()
1029 int oldBracketY
= m_bracketY
;
1030 int oldBracketX
= m_bracketX
;
1032 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
1034 wxString current
= m_lines
[m_cursorY
].m_text
;
1036 // reverse search first
1041 bracket
= current
[m_cursorX
-1];
1043 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
1045 char antibracket
= '(';
1046 if (bracket
== ']') antibracket
= '[';
1047 if (bracket
== '}') antibracket
= '{';
1051 int endY
= m_cursorY
-60;
1052 if (endY
< 0) endY
= 0;
1053 for (int y
= m_cursorY
; y
>= endY
; y
--)
1055 current
= m_lines
[y
].m_text
;
1057 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
1059 for (int n
= current
.Len()-1; n
>= 0; n
--)
1062 if (current
[n
] == '\'')
1064 for (int m
= n
-1; m
>= 0; m
--)
1066 if (current
[m
] == '\'')
1068 if (m
== 0 || current
[m
-1] != '\\')
1077 if (current
[n
] == '\"')
1079 for (int m
= n
-1; m
>= 0; m
--)
1081 if (current
[m
] == '\"')
1083 if (m
== 0 || current
[m
-1] != '\\')
1091 if (current
[n
] == antibracket
)
1098 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1099 RefreshLine( oldBracketY
);
1100 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1101 RefreshLine( m_bracketY
);
1105 else if (current
[n
] == bracket
)
1116 if ((int)current
.Len() > m_cursorX
)
1117 bracket
= current
[m_cursorX
];
1118 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
1120 char antibracket
= ')';
1121 if (bracket
== '[') antibracket
= ']';
1122 if (bracket
== '{') antibracket
= '}';
1126 int endY
= m_cursorY
+60;
1127 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
1128 for (int y
= m_cursorY
; y
<= endY
; y
++)
1130 current
= m_lines
[y
].m_text
;
1133 start
= m_cursorX
+1;
1135 for (int n
= start
; n
< (int)current
.Len(); n
++)
1138 if (current
[n
] == '\'')
1140 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1142 if (current
[m
] == '\'')
1144 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1153 if (current
[n
] == '\"')
1155 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1157 if (current
[m
] == '\"')
1159 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1167 if (current
[n
] == antibracket
)
1174 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1175 RefreshLine( oldBracketY
);
1176 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1177 RefreshLine( m_bracketY
);
1181 else if (current
[n
] == bracket
)
1189 if (oldBracketY
!= -1)
1192 RefreshLine( oldBracketY
);
1196 void wxTextCtrl::Delete()
1198 if (!HasSelection()) return;
1202 int selStartY
= m_selStartY
;
1203 int selEndY
= m_selEndY
;
1204 int selStartX
= m_selStartX
;
1205 int selEndX
= m_selEndX
;
1207 if ((selStartY
> selEndY
) ||
1208 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1210 int tmp
= selStartX
;
1211 selStartX
= selEndX
;
1214 selStartY
= selEndY
;
1218 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1220 if (selStartY
== selEndY
)
1222 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1224 wxString
tmp( m_lines
[selStartY
].m_text
);
1225 if (selStartX
< len
)
1229 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1230 m_lines
[selStartY
].m_text
= tmp
;
1233 m_cursorX
= selStartX
;
1234 RefreshLine( selStartY
);
1238 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1240 if (selStartX
< len
)
1241 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1243 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1244 m_lines
.RemoveAt( selStartY
+1 );
1246 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1247 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1249 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1251 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1252 m_lines
.RemoveAt( selStartY
+1 );
1255 MoveCursor( selStartX
, selStartY
);
1256 MyAdjustScrollbars();
1258 RefreshDown( selStartY
);
1262 void wxTextCtrl::DeleteLine()
1264 if (HasSelection()) return;
1266 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1268 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1270 m_lines
.RemoveAt( m_cursorY
);
1272 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1274 MyAdjustScrollbars();
1275 RefreshDown( m_cursorY
);
1278 void wxTextCtrl::DoChar( char c
)
1282 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1284 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1286 if (m_cursorX
>= (int)tmp
.Len())
1288 int len
= tmp
.Len();
1289 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1296 tmp
.SetChar( m_cursorX
, c
);
1298 tmp
.insert( m_cursorX
, 1, c
);
1301 m_lines
[m_cursorY
].m_text
= tmp
;
1303 // if (tmp.Len() > m_longestLine)
1305 // m_longestLine = tmp.Len();
1306 // MyAdjustScrollbars();
1310 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1312 if (ww
> m_longestLine
)
1315 MyAdjustScrollbars();
1320 int y
= m_cursorY
*m_lineHeight
;
1321 // int x = (m_cursorX-1)*m_charWidth;
1322 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1323 CalcScrolledPosition( x
, y
, &x
, &y
);
1324 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1325 Refresh( TRUE
, &rect
);
1326 // refresh whole line for syntax colour highlighting
1328 Refresh( FALSE
, &rect
);
1332 GetClientSize( &size_x
, &size_y
);
1333 size_x
/= m_charWidth
;
1337 GetViewStart( &view_x
, &view_y
);
1339 //int xx = m_cursorX;
1340 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1344 else if (xx
> view_x
+size_x
-1)
1345 Scroll( xx
-size_x
+1, -1 );
1348 void wxTextCtrl::DoBack()
1354 if (m_cursorY
== 0) return;
1356 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1358 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1360 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1362 m_cursorX
= tmp1
.Len();
1364 tmp1
.Append( tmp2
);
1365 m_lines
[m_cursorY
].m_text
= tmp1
;
1366 m_lines
.RemoveAt( m_cursorY
+1 );
1368 MyAdjustScrollbars();
1369 RefreshDown( m_cursorY
-1 );
1373 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1375 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1376 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1379 int y
= m_cursorY
*m_lineHeight
;
1380 // int x = m_cursorX*m_charWidth;
1381 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1382 CalcScrolledPosition( x
, y
, &x
, &y
);
1383 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1384 Refresh( TRUE
, &rect
);
1385 // refresh whole line for syntax colour highlighting
1387 Refresh( FALSE
, &rect
);
1391 void wxTextCtrl::DoDelete()
1395 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1397 int len
= (int)tmp
.Len();
1398 if (m_cursorX
>= len
)
1400 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1402 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1404 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1407 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1409 m_lines
[m_cursorY
] = tmp
;
1410 m_lines
.RemoveAt( m_cursorY
+1 );
1412 MyAdjustScrollbars();
1413 RefreshDown( m_cursorY
);
1417 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1419 tmp
.Remove( m_cursorX
, 1 );
1420 m_lines
[m_cursorY
].m_text
= tmp
;
1422 int y
= m_cursorY
*m_lineHeight
;
1423 // int x = m_cursorX*m_charWidth;
1424 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1425 CalcScrolledPosition( x
, y
, &x
, &y
);
1426 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1427 Refresh( TRUE
, &rect
);
1428 // refresh whole line for syntax colour highlighting
1430 Refresh( FALSE
, &rect
);
1434 void wxTextCtrl::DoReturn()
1438 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1440 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1441 size_t indent
= tmp
.find_first_not_of( ' ' );
1442 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1444 if (m_cursorX
>= (int)tmp
.Len())
1446 int cursorX
= indent
;
1447 int cursorY
= m_cursorY
+ 1;
1450 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1451 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1453 MyAdjustScrollbars();
1454 MoveCursor( cursorX
, cursorY
);
1455 RefreshDown( m_cursorY
);
1459 wxString
tmp1( tmp
);
1460 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1461 m_lines
[m_cursorY
].m_text
= tmp1
;
1463 wxString
tmp2( tmp
);
1464 tmp2
.Remove( 0, m_cursorX
);
1466 int cursorX
= indent
;
1467 int cursorY
= m_cursorY
+ 1;
1470 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1471 new_tmp
.Append( tmp2
);
1472 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1474 MyAdjustScrollbars();
1475 MoveCursor( cursorX
, cursorY
);
1476 RefreshDown( m_cursorY
-1 );
1480 void wxTextCtrl::DoDClick()
1482 wxString
line( m_lines
[ m_cursorY
].m_text
);
1483 if (m_cursorX
>= (int)line
.Len()) return;
1486 if (((ch
>= 'a') && (ch
<= 'z')) ||
1487 ((ch
>= 'A') && (ch
<= 'Z')) ||
1488 ((ch
>= '0') && (ch
<= '9')) ||
1491 m_selStartY
= m_cursorY
;
1492 m_selEndY
= m_cursorY
;
1496 while (((ch
>= 'a') && (ch
<= 'z')) ||
1497 ((ch
>= 'A') && (ch
<= 'Z')) ||
1498 ((ch
>= '0') && (ch
<= '9')) ||
1509 if (p
< (int)line
.Len())
1512 while (((ch
>= 'a') && (ch
<= 'z')) ||
1513 ((ch
>= 'A') && (ch
<= 'Z')) ||
1514 ((ch
>= '0') && (ch
<= '9')) ||
1517 if (p
>= (int)line
.Len()) break;
1523 RefreshLine( m_cursorY
);
1527 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1530 size_t len
= line
.Len();
1531 for (size_t p
= pos
; p
< len
; p
++)
1533 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1537 for (size_t q
= p
; q
< len
; q
++)
1538 ret
.Append( line
[q
] );
1545 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[p
+1] == '/'))
1547 for (size_t q
= p
; q
< len
; q
++)
1548 ret
.Append( line
[q
] );
1556 ret
.Append( line
[p
] );
1557 for (size_t q
= p
+1; q
< len
; q
++)
1559 ret
.Append( line
[q
] );
1560 if ((line
[q
] == '"') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1567 if (line
[p
] == '\'')
1569 ret
.Append( line
[p
] );
1570 for (size_t q
= p
+1; q
< len
; q
++)
1572 ret
.Append( line
[q
] );
1573 if ((line
[q
] == '\'') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1580 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1581 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1585 ret
.Append( line
[p
] );
1586 for (size_t q
= p
+1; q
< len
; q
++)
1588 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1589 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1590 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1593 ret
.Append( line
[q
] );
1610 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1615 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1618 size_t len
= origin
.Len();
1619 dc
.SetTextForeground( colour
);
1622 while (toDraw
[pos
] == wxT(' '))
1625 if (pos
== len
) return;
1631 current
+= toDraw
[pos
];
1633 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1635 current
+= toDraw
[pos
];
1640 wxString tmp
= origin
.Left( start
);
1641 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1644 dc
.DrawText( current
, xx
, yy
);
1648 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1650 int selStartY
= m_selStartY
;
1651 int selEndY
= m_selEndY
;
1652 int selStartX
= m_selStartX
;
1653 int selEndX
= m_selEndX
;
1655 if ((selStartY
> selEndY
) ||
1656 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1658 int tmp
= selStartX
;
1659 selStartX
= selEndX
;
1662 selStartY
= selEndY
;
1666 wxString
line( line2
);
1667 if (HasFlag(wxTE_PASSWORD
))
1669 size_t len
= line
.Len();
1670 line
= wxString( wxT('*'), len
);
1673 wxString
keyword( ' ', line
.Len() );
1674 wxString
define( ' ', line
.Len() );
1675 wxString
variable( ' ', line
.Len() );
1676 wxString
comment( ' ', line
.Len() );
1677 wxString
my_string( ' ', line
.Len() );
1678 wxString
selection( ' ', line
.Len() );
1680 if (m_lang
!= wxSOURCE_LANG_NONE
)
1682 if (lineNum
== m_bracketY
)
1684 wxString
red( ' ', line
.Len() );
1685 if (m_bracketX
< (int)line
.Len())
1687 red
.SetChar( m_bracketX
, line
[m_bracketX
] );
1688 line
.SetChar( m_bracketX
, ' ' );
1689 dc
.SetTextForeground( *wxRED
);
1690 dc
.DrawText( red
, x
, y
);
1691 dc
.SetTextForeground( *wxBLACK
);
1696 wxString
token( GetNextToken( line
, pos
) );
1697 while (!token
.IsNull())
1699 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1701 size_t end_pos
= pos
+ token
.Len();
1702 for (size_t i
= pos
; i
< end_pos
; i
++)
1704 keyword
[i
] = line
[i
];
1708 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1710 size_t end_pos
= pos
+ token
.Len();
1711 for (size_t i
= pos
; i
< end_pos
; i
++)
1713 define
[i
] = line
[i
];
1717 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1718 ((token
.Len() > 2) && (token
[0] == 'w') && (token
[1] == 'x')))
1720 size_t end_pos
= pos
+ token
.Len();
1721 for (size_t i
= pos
; i
< end_pos
; i
++)
1723 variable
[i
] = line
[i
];
1727 if ((token
.Len() >= 2) && (token
[0] == '/') && (token
[1] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1729 size_t end_pos
= pos
+ token
.Len();
1730 for (size_t i
= pos
; i
< end_pos
; i
++)
1732 comment
[i
] = line
[i
];
1736 if ((token
[0] == '#') &&
1737 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1739 size_t end_pos
= pos
+ token
.Len();
1740 for (size_t i
= pos
; i
< end_pos
; i
++)
1742 comment
[i
] = line
[i
];
1746 if ((token
[0] == '"') || (token
[0] == '\''))
1748 size_t end_pos
= pos
+ token
.Len();
1749 for (size_t i
= pos
; i
< end_pos
; i
++)
1751 my_string
[i
] = line
[i
];
1756 token
= GetNextToken( line
, pos
);
1760 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1762 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1763 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1764 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1765 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1766 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1767 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1768 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1772 if (selStartY
== selEndY
)
1774 // int xx = selStartX*m_charWidth;
1775 int xx
= PosToPixel( lineNum
, selStartX
);
1776 // int ww = (selEndX-selStartX)*m_charWidth;
1777 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1778 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1780 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1782 selection
[i
] = line
[i
];
1786 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1788 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1790 for (size_t i
= 0; i
< line
.Len(); i
++)
1792 selection
[i
] = line
[i
];
1796 if (lineNum
== selStartY
)
1798 // int xx = selStartX*m_charWidth;
1799 int xx
= PosToPixel( lineNum
, selStartX
);
1800 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1802 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1804 selection
[i
] = line
[i
];
1808 if (lineNum
== selEndY
)
1810 // int ww = selEndX*m_charWidth;
1811 int ww
= PosToPixel( lineNum
, selEndX
);
1812 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1814 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1816 selection
[i
] = line
[i
];
1821 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1822 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1823 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1824 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1825 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1826 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1827 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1830 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1834 if (m_lines
.GetCount() == 0) return;
1838 dc
.SetFont( m_sourceFont
);
1841 GetViewStart( NULL
, &scroll_y
);
1843 // We have a inner border of two pixels
1844 // around the text, so scroll units do
1845 // not correspond to lines.
1846 if (scroll_y
> 0) scroll_y
--;
1850 GetClientSize( &size_x
, &size_y
);
1852 dc
.SetPen( *wxTRANSPARENT_PEN
);
1853 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1854 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+2 );
1855 for (int i
= scroll_y
; i
< upper
; i
++)
1858 int y
= i
*m_lineHeight
+2;
1860 int h
= m_lineHeight
;
1861 CalcScrolledPosition( x
,y
,&x
,&y
);
1862 if (IsExposed(x
,y
,w
,h
))
1863 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1866 if (m_editable
&& (FindFocus() == this))
1868 ///dc.SetBrush( *wxRED_BRUSH );
1869 dc
.SetBrush( *wxBLACK_BRUSH
);
1870 // int xx = m_cursorX*m_charWidth;
1871 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1872 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1876 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1878 if (m_lines
.GetCount() == 0) return;
1881 #if 0 // there is no middle button on iPAQs
1882 if (event
.MiddleDown())
1889 if (event
.LeftDClick())
1895 if (event
.LeftDown())
1903 m_capturing
= FALSE
;
1907 if (event
.LeftDown() ||
1908 (event
.LeftIsDown() && m_capturing
))
1910 int x
= event
.GetX();
1911 int y
= event
.GetY();
1912 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1914 // x /= m_charWidth;
1915 x
= PixelToPos( y
, x
);
1917 wxMin( 1000, wxMax( 0, x
) ),
1918 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1919 event
.ShiftDown() || !event
.LeftDown() );
1923 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1925 if (m_lines
.GetCount() == 0) return;
1927 if (!m_editable
) return;
1931 GetClientSize( &size_x
, &size_y
);
1932 size_x
/= m_charWidth
;
1933 size_y
/= m_lineHeight
;
1936 if (event
.ShiftDown())
1938 switch (event
.GetKeyCode())
1940 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1941 case '8': event
.m_keyCode
= WXK_UP
; break;
1942 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1943 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1944 case '9': event
.m_keyCode
= WXK_PRIOR
; break;
1945 case '3': event
.m_keyCode
= WXK_NEXT
; break;
1946 case '7': event
.m_keyCode
= WXK_HOME
; break;
1947 case '1': event
.m_keyCode
= WXK_END
; break;
1948 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1952 switch (event
.GetKeyCode())
1956 if (m_ignoreInput
) return;
1958 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1959 m_ignoreInput
= TRUE
;
1964 if (m_ignoreInput
) return;
1965 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1966 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1967 m_ignoreInput
= TRUE
;
1972 if (m_ignoreInput
) return;
1975 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1980 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1982 m_ignoreInput
= TRUE
;
1987 if (m_ignoreInput
) return;
1988 if (m_cursorX
< 1000)
1989 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1990 m_ignoreInput
= TRUE
;
1995 if (event
.ControlDown())
1996 MoveCursor( 0, 0, event
.ShiftDown() );
1998 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
2003 if (event
.ControlDown())
2004 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
2006 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
2011 if (m_ignoreInput
) return;
2012 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
2013 m_ignoreInput
= TRUE
;
2018 if (m_ignoreInput
) return;
2019 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
2020 m_ignoreInput
= TRUE
;
2025 if (event
.ShiftDown())
2027 else if (event
.ControlDown())
2030 m_overwrite
= !m_overwrite
;
2035 if (m_windowStyle
& wxPROCESS_ENTER
)
2037 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
2038 event
.SetEventObject(this);
2039 event
.SetString(GetValue());
2040 if (GetEventHandler()->ProcessEvent(event
)) return;
2058 bool save_overwrite
= m_overwrite
;
2059 m_overwrite
= FALSE
;
2060 int i
= 4-(m_cursorX
% 4);
2062 for (int c
= 0; c
< i
; c
++)
2064 m_overwrite
= save_overwrite
;
2085 if ( (event
.KeyCode() >= 'a') &&
2086 (event
.KeyCode() <= 'z') &&
2094 if ( (event
.KeyCode() >= 32) &&
2095 (event
.KeyCode() <= 255) &&
2096 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
2100 DoChar( (char) event
.KeyCode() );
2109 void wxTextCtrl::OnIdle( wxIdleEvent
&event
)
2111 m_ignoreInput
= FALSE
;
2113 if (m_lang
!= wxSOURCE_LANG_NONE
)
2114 SearchForBrackets();
2119 void wxTextCtrl::Indent()
2121 int startY
= m_cursorY
;
2122 int endY
= m_cursorY
;
2125 startY
= m_selStartY
;
2135 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2137 for (int i
= startY
; i
<= endY
; i
++)
2139 m_lines
[i
].m_text
.insert( 0u, " " );
2144 void wxTextCtrl::Unindent()
2146 int startY
= m_cursorY
;
2147 int endY
= m_cursorY
;
2150 startY
= m_selStartY
;
2160 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2162 for (int i
= startY
; i
<= endY
; i
++)
2164 for (int n
= 0; n
< 4; n
++)
2166 if (m_lines
[i
].m_text
[0u] == ' ')
2167 m_lines
[i
].m_text
.erase(0u,1u);
2172 bool wxTextCtrl::HasSelection()
2174 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2177 void wxTextCtrl::ClearSelection()
2185 void wxTextCtrl::RefreshLine( int n
)
2187 int y
= n
*m_lineHeight
;
2189 CalcScrolledPosition( x
, y
, &x
, &y
);
2190 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2191 Refresh( TRUE
, &rect
);
2194 void wxTextCtrl::RefreshDown( int n
)
2198 GetClientSize( &size_x
, &size_y
);
2202 GetViewStart( &view_x
, &view_y
);
2210 int y
= n
*m_lineHeight
;
2212 CalcScrolledPosition( x
, y
, &x
, &y
);
2214 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2215 Refresh( TRUE
, &rect
);
2219 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2221 if (!m_editable
) return;
2223 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
2225 if (new_x
> m_lines
[new_y
].m_text
.Len())
2226 new_x
= m_lines
[new_y
].m_text
.Len();
2229 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2231 bool no_cursor_refresh
= FALSE
;
2232 bool has_selection
= HasSelection();
2240 m_selStartX
= m_cursorX
;
2241 m_selStartY
= m_cursorY
;
2245 if (new_y
> m_selStartY
)
2247 y
= m_selStartY
*m_lineHeight
;
2248 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2250 else if (new_y
== m_selStartY
)
2252 x
= PosToPixel( new_y
, m_selStartX
);
2253 w
= PosToPixel( new_y
, new_x
) - x
;
2257 w
= -w
+ 2; // +2 for the cursor
2259 y
= m_selStartY
*m_lineHeight
;
2264 y
= new_y
*m_lineHeight
;
2265 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2268 no_cursor_refresh
= TRUE
;
2274 if (new_y
== m_selEndY
)
2276 y
= new_y
*m_lineHeight
;
2278 if (m_selEndX
> new_x
)
2280 // x = new_x*m_charWidth;
2281 x
= PosToPixel( new_y
, new_x
);
2282 // w = (m_selEndX-new_x)*m_charWidth;
2283 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2287 // x = m_selEndX*m_charWidth;
2288 x
= PosToPixel( new_y
, m_selEndX
);
2289 // w = (-m_selEndX+new_x)*m_charWidth;
2290 w
= PosToPixel( new_y
, new_x
) - x
;
2297 if (new_y
> m_selEndY
)
2299 y
= m_selEndY
*m_lineHeight
;
2300 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2304 y
= new_y
*m_lineHeight
;
2305 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2307 no_cursor_refresh
= TRUE
;
2316 CalcScrolledPosition( x
, y
, &x
, &y
);
2317 wxRect
rect( x
+2, y
+2, w
, h
);
2318 Refresh( TRUE
, &rect
);
2324 int ry1
= m_selEndY
;
2325 int ry2
= m_selStartY
;
2339 int y
= ry1
*m_lineHeight
;
2340 CalcScrolledPosition( x
, y
, &x
, &y
);
2341 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2343 Refresh( TRUE
, &rect
);
2348 printf( "startx %d starty %d endx %d endy %d\n",
2349 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2351 printf( "has %d\n", (int)HasSelection() );
2354 if (!no_cursor_refresh
)
2356 // int x = m_cursorX*m_charWidth;
2357 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2358 int y
= m_cursorY
*m_lineHeight
;
2359 CalcScrolledPosition( x
, y
, &x
, &y
);
2360 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2365 Refresh( TRUE
, &rect
);
2367 if (FindFocus() == this)
2369 wxClientDC
dc(this);
2371 dc
.SetPen( *wxTRANSPARENT_PEN
);
2372 //dc.SetBrush( *wxRED_BRUSH );
2373 dc
.SetBrush( *wxBLACK_BRUSH
);
2374 // int xx = m_cursorX*m_charWidth;
2375 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2376 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2382 GetClientSize( &size_x
, &size_y
);
2383 size_x
/= m_charWidth
;
2384 size_y
/= m_lineHeight
;
2388 GetViewStart( &view_x
, &view_y
);
2392 int sy
= m_cursorY
- (size_y
/2);
2398 if (m_cursorY
< view_y
)
2399 Scroll( -1, m_cursorY
);
2400 else if (m_cursorY
> view_y
+size_y
-1)
2401 Scroll( -1, m_cursorY
-size_y
+1 );
2404 //int xx = m_cursorX;
2405 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2409 else if (xx
> view_x
+size_x
-1)
2410 Scroll( xx
-size_x
+1, -1 );
2413 void wxTextCtrl::MyAdjustScrollbars()
2418 int y_range
= m_lines
.GetCount();
2421 GetClientSize( NULL
, &height
);
2423 if (height
>= m_lines
.GetCount() *m_lineHeight
)
2428 GetViewStart( &view_x
, &view_y
);
2430 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2433 //-----------------------------------------------------------------------------
2434 // clipboard handlers
2435 //-----------------------------------------------------------------------------
2437 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2442 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2447 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2452 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2457 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2462 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2464 event
.Enable( CanCut() );
2467 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2469 event
.Enable( CanCopy() );
2472 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2474 event
.Enable( CanPaste() );
2477 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2479 event
.Enable( CanUndo() );
2482 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2484 event
.Enable( CanRedo() );
2487 wxSize
wxTextCtrl::DoGetBestSize() const
2491 wxSize
ret(80, m_lineHeight
+ 4);
2493 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2496 if (HasFlag(wxBORDER_SIMPLE
))
2503 return wxSize(80, 60);
2507 // ----------------------------------------------------------------------------
2509 // ----------------------------------------------------------------------------
2511 void wxTextCtrl::Freeze()
2515 void wxTextCtrl::Thaw()
2519 void wxTextCtrl::OnSetFocus( wxFocusEvent
& event
)
2521 // To hide or show caret, as appropriate
2525 void wxTextCtrl::OnKillFocus( wxFocusEvent
& event
)
2527 // To hide or show caret, as appropriate
2531 // ----------------------------------------------------------------------------
2532 // text control scrolling
2533 // ----------------------------------------------------------------------------
2535 bool wxTextCtrl::ScrollLines(int lines
)
2537 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2542 bool wxTextCtrl::ScrollPages(int pages
)
2544 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");