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
);
252 MyAdjustScrollbars();
257 //-----------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------
261 wxString
wxTextCtrl::GetValue() const
264 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
266 ret
+= m_lines
[i
].m_text
;
267 if (i
+1 < m_lines
.GetCount())
274 void wxTextCtrl::SetValue(const wxString
& value
)
278 if ((GetWindowStyle() & wxTE_MULTILINE
) == 0)
280 if (value
== GetValue())
292 m_lines
.Add( new wxSourceLine( wxT("") ) );
300 pos
= value
.find( wxT('\n'), begin
);
303 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, value
.Len()-begin
) );
306 // if (sl->m_text.Len() > m_longestLine)
307 // m_longestLine = sl->m_text.Len();
309 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
311 if (ww
> m_longestLine
)
318 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, pos
-begin
) );
321 // if (sl->m_text.Len() > m_longestLine)
322 // m_longestLine = sl->m_text.Len();
324 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
326 if (ww
> m_longestLine
)
334 MyAdjustScrollbars();
339 int wxTextCtrl::GetLineLength(long lineNo
) const
341 if (lineNo
>= (long)m_lines
.GetCount())
344 return m_lines
[lineNo
].m_text
.Len();
347 wxString
wxTextCtrl::GetLineText(long lineNo
) const
349 if (lineNo
>= (long)m_lines
.GetCount())
352 return m_lines
[lineNo
].m_text
;
355 int wxTextCtrl::GetNumberOfLines() const
357 return m_lines
.GetCount();
360 bool wxTextCtrl::IsModified() const
365 bool wxTextCtrl::IsEditable() const
370 void wxTextCtrl::GetSelection(long* from
, long* to
) const
374 void wxTextCtrl::Clear()
382 m_lines
.Add( new wxSourceLine( wxT("") ) );
384 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
389 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
393 void wxTextCtrl::Remove(long from
, long to
)
398 void wxTextCtrl::DiscardEdits()
404 void wxTextCtrl::SetMaxLength(unsigned long len
)
408 int wxTextCtrl::PosToPixel( int line
, int pos
)
410 // TODO add support for Tabs
412 if (line
>= (int)m_lines
.GetCount()) return 0;
413 if (pos
< 0) return 0;
415 wxString text
= m_lines
[line
].m_text
;
417 if (text
.IsEmpty()) return 0;
419 if (pos
< (int)text
.Len())
420 text
.Remove( pos
, text
.Len()-pos
);
424 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
429 int wxTextCtrl::PixelToPos( int line
, int pixel
)
431 if (pixel
< 2) return 0;
433 if (line
>= (int)m_lines
.GetCount()) return 0;
435 wxString text
= m_lines
[line
].m_text
;
438 int res
= text
.Len();
441 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
447 text
.Remove( res
,1 );
453 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
459 if (m_lang
== wxSOURCE_LANG_PYTHON
)
461 m_keywords
.Add( "class" );
462 m_keywords
.Add( "__init__" );
463 m_keywords
.Add( "return" );
464 m_keywords
.Add( "def" );
465 m_keywords
.Add( "try" );
466 m_keywords
.Add( "except" );
467 m_keywords
.Add( "if" );
468 m_keywords
.Add( "else" );
469 m_keywords
.Add( "finally" );
470 m_keywords
.Add( "for" );
471 m_keywords
.Add( "if" );
472 m_keywords
.Add( "elif" );
473 m_keywords
.Add( "in" );
474 m_keywords
.Add( "and" );
475 m_keywords
.Add( "del" );
476 m_keywords
.Add( "is" );
477 m_keywords
.Add( "raise" );
478 m_keywords
.Add( "assert" );
479 m_keywords
.Add( "lambda" );
480 m_keywords
.Add( "break" );
481 m_keywords
.Add( "global" );
482 m_keywords
.Add( "not" );
483 m_keywords
.Add( "or" );
484 m_keywords
.Add( "while" );
485 m_keywords
.Add( "continue" );
486 m_keywords
.Add( "exec" );
487 m_keywords
.Add( "pass" );
488 m_keywords
.Add( "print" );
490 if (m_lang
== wxSOURCE_LANG_PERL
)
492 m_keywords
.Add( "main" );
493 m_keywords
.Add( "sub" );
494 m_keywords
.Add( "shift" );
495 m_keywords
.Add( "push" );
496 m_keywords
.Add( "split" );
497 m_keywords
.Add( "join" );
498 m_keywords
.Add( "chop" );
499 m_keywords
.Add( "grep" );
500 m_keywords
.Add( "open" );
501 m_keywords
.Add( "print" );
502 m_keywords
.Add( "sprint" );
503 m_keywords
.Add( "printf" );
504 m_keywords
.Add( "sprintf" );
505 m_keywords
.Add( "my" );
506 m_keywords
.Add( "local" );
507 m_keywords
.Add( "exit" );
508 m_keywords
.Add( "die" );
509 m_keywords
.Add( "return" );
510 m_keywords
.Add( "for" );
511 m_keywords
.Add( "foreach" );
512 m_keywords
.Add( "while" );
513 m_keywords
.Add( "unless" );
514 m_keywords
.Add( "if" );
515 m_keywords
.Add( "next" );
516 m_keywords
.Add( "last" );
517 m_keywords
.Add( "else" );
518 m_keywords
.Add( "elsif" );
519 m_keywords
.Add( "ne" );
520 m_keywords
.Add( "qe" );
523 if (m_lang
== wxSOURCE_LANG_CPP
)
525 m_keywords
.Add( "class" );
526 m_keywords
.Add( "return" );
527 m_keywords
.Add( "if" );
528 m_keywords
.Add( "then" );
529 m_keywords
.Add( "else" );
530 m_keywords
.Add( "struct" );
531 m_keywords
.Add( "enum" );
532 m_keywords
.Add( "while" );
533 m_keywords
.Add( "do" );
534 m_keywords
.Add( "for" );
535 m_keywords
.Add( "continue" );
536 m_keywords
.Add( "break" );
537 m_keywords
.Add( "switch" );
538 m_keywords
.Add( "case" );
539 m_keywords
.Add( "goto" );
540 m_keywords
.Add( "label" );
541 m_keywords
.Add( "inline" );
542 m_keywords
.Add( "operator" );
543 m_keywords
.Add( "virtual" );
544 m_keywords
.Add( "private" );
545 m_keywords
.Add( "public" );
546 m_keywords
.Add( "protected" );
547 m_keywords
.Add( "friend" );
548 m_keywords
.Add( "exception" );
549 m_keywords
.Add( "throw" );
550 m_keywords
.Add( "catch" );
551 m_keywords
.Add( "delete" );
552 m_keywords
.Add( "new" );
553 m_keywords
.Add( "default" );
554 m_keywords
.Add( "overload" );
555 m_keywords
.Add( "using" );
556 m_keywords
.Add( "template" );
557 m_keywords
.Add( "try" );
558 m_keywords
.Add( "typedef" );
559 m_keywords
.Add( "union" );
560 m_keywords
.Add( "volatile" );
561 m_keywords
.Add( "asm" );
566 if (m_lang
== wxSOURCE_LANG_PYTHON
)
568 m_defines
.Add( "from" );
569 m_defines
.Add( "import" );
571 if (m_lang
== wxSOURCE_LANG_PERL
)
573 m_defines
.Add( "use" );
574 m_defines
.Add( "do" );
575 m_defines
.Add( "package" );
576 m_defines
.Add( "defined" );
578 if (m_lang
== wxSOURCE_LANG_CPP
)
580 m_defines
.Add( "#define" );
581 m_defines
.Add( "#if" );
582 m_defines
.Add( "#ifndef" );
583 m_defines
.Add( "#ifdef" );
584 m_defines
.Add( "#else" );
585 m_defines
.Add( "#elif" );
586 m_defines
.Add( "#endif" );
587 m_defines
.Add( "#pragma" );
588 m_defines
.Add( "#include" );
593 if (m_lang
== wxSOURCE_LANG_PYTHON
)
595 m_variables
.Add( "nil" );
596 m_variables
.Add( "None" );
597 m_variables
.Add( "self" );
598 m_variables
.Add( "false" );
599 m_variables
.Add( "true" );
601 if (m_lang
== wxSOURCE_LANG_PERL
)
603 m_variables
.Add( "undef" );
604 m_variables
.Add( "class" );
605 m_variables
.Add( "this" );
606 m_variables
.Add( "IN" );
607 m_variables
.Add( "OUT" );
608 m_variables
.Add( "STDIN" );
609 m_variables
.Add( "STDOUT" );
610 m_variables
.Add( "STDERR" );
612 if (m_lang
== wxSOURCE_LANG_CPP
)
614 m_variables
.Add( "int" );
615 m_variables
.Add( "bool" );
616 m_variables
.Add( "void" );
617 m_variables
.Add( "long" );
618 m_variables
.Add( "short" );
619 m_variables
.Add( "const" );
620 m_variables
.Add( "signed" );
621 m_variables
.Add( "unsigned" );
622 m_variables
.Add( "char" );
623 m_variables
.Add( "size_t" );
624 m_variables
.Add( "wchar_t" );
625 m_variables
.Add( "NULL" );
626 m_variables
.Add( "this" );
627 m_variables
.Add( "TRUE" );
628 m_variables
.Add( "FALSE" );
629 m_variables
.Add( "float" );
630 m_variables
.Add( "double" );
631 m_variables
.Add( "register" );
632 m_variables
.Add( "extern" );
633 m_variables
.Add( "static" );
634 m_variables
.Add( "sizeof" );
638 void wxTextCtrl::WriteText(const wxString
& text2
)
640 if (text2
.IsEmpty()) return;
644 wxString
text( text2
);
647 while ( (pos
= text
.Find('\n')) != -1 )
649 lines
.Add( text
.Left( pos
) );
650 text
.Remove( 0, pos
+1 );
653 int count
= (int)lines
.GetCount();
655 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
656 wxString
tmp2( tmp1
);
657 int len
= (int)tmp1
.Len();
662 for (int i
= 0; i
< m_cursorX
-len
; i
++)
664 m_lines
[m_cursorY
].m_text
.Append( tmp
);
669 tmp1
.Remove( m_cursorX
);
670 tmp2
.Remove( 0, m_cursorX
);
671 tmp1
.Append( lines
[0] );
675 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
678 m_lines
[m_cursorY
].m_text
= tmp1
;
679 RefreshLine( m_cursorY
);
683 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
685 m_lines
[m_cursorY
].m_text
= tmp1
;
687 for (i
= 1; i
< count
; i
++)
688 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
689 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
691 MyAdjustScrollbars();
692 RefreshDown( m_cursorY
);
696 void wxTextCtrl::AppendText(const wxString
& text2
)
698 if (text2
.IsEmpty()) return;
702 wxString
text( text2
);
705 while ( (pos
= text
.Find('\n')) != -1 )
707 lines
.Add( text
.Left( pos
) );
708 text
.Remove( 0, pos
+1 );
711 int count
= (int)lines
.GetCount();
713 size_t y
= m_lines
.GetCount()-1;
715 wxString
tmp( m_lines
[y
].m_text
);
716 tmp
.Append( lines
[0] );
720 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
722 m_lines
[y
].m_text
= tmp
;
727 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
729 m_lines
[y
].m_text
= tmp
;
731 for (i
= 1; i
< count
; i
++)
732 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
734 MyAdjustScrollbars();
739 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
744 long wxTextCtrl::XYToPosition(long x
, long y
) const
748 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
752 ret
+= m_lines
[i
].m_text
.Len();
756 if ((size_t)x
< m_lines
[i
].m_text
.Len())
759 return (ret
+ m_lines
[i
].m_text
.Len());
765 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
767 if (m_lines
.GetCount() == 0)
778 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
780 pos
-= m_lines
[i
].m_text
.Len();
792 xx
= m_lines
[ m_lines
.GetCount()-1 ].m_text
.Len();
799 void wxTextCtrl::ShowPosition(long pos
)
803 void wxTextCtrl::Copy()
805 if (!HasSelection()) return;
809 int selStartY
= m_selStartY
;
810 int selEndY
= m_selEndY
;
811 int selStartX
= m_selStartX
;
812 int selEndX
= m_selEndX
;
814 if ((selStartY
> selEndY
) ||
815 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
825 if (selStartY
== selEndY
)
827 sel
= m_lines
[selStartY
].m_text
;
829 if (selStartX
>= (int)sel
.Len()) return;
830 if (selEndX
> (int)sel
.Len())
833 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
834 sel
.Remove( 0, selStartX
);
838 wxString
tmp( m_lines
[selStartY
].m_text
);
840 if (selStartX
< (int)tmp
.Len())
842 tmp
.Remove( 0, selStartX
);
846 for (int i
= selStartY
+1; i
< selEndY
; i
++)
848 sel
.Append( m_lines
[i
].m_text
);
851 tmp
= m_lines
[selEndY
].m_text
;
852 if (selEndX
> (int)tmp
.Len())
856 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
861 if (wxTheClipboard
->Open())
863 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
864 wxTheClipboard
->Close();
868 void wxTextCtrl::Cut()
875 void wxTextCtrl::Paste()
879 if (!wxTheClipboard
->Open()) return;
881 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
883 wxTheClipboard
->Close();
888 wxTextDataObject data
;
890 bool ret
= wxTheClipboard
->GetData( data
);
892 wxTheClipboard
->Close();
898 wxString
text( data
.GetText() );
901 while ( (pos
= text
.Find('\n')) != -1 )
903 lines
.Add( text
.Left( pos
) );
904 text
.Remove( 0, pos
+1 );
907 int count
= (int)lines
.GetCount();
909 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
910 wxString
tmp2( tmp1
);
911 int len
= (int)tmp1
.Len();
916 for (int i
= 0; i
< m_cursorX
-len
; i
++)
918 m_lines
[m_cursorY
].m_text
.Append( tmp
);
923 tmp1
.Remove( m_cursorX
);
924 tmp2
.Remove( 0, m_cursorX
);
925 tmp1
.Append( lines
[0] );
929 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
932 m_lines
[m_cursorY
].m_text
= tmp1
;
933 RefreshLine( m_cursorY
);
937 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
939 m_lines
[m_cursorY
].m_text
= tmp1
;
941 for (i
= 1; i
< count
; i
++)
942 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
943 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
945 MyAdjustScrollbars();
946 RefreshDown( m_cursorY
);
950 void wxTextCtrl::Undo()
952 if (m_undos
.GetCount() == 0) return;
954 wxNode
*node
= m_undos
.Nth( m_undos
.GetCount()-1 );
955 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->Data();
964 void wxTextCtrl::SetInsertionPoint(long pos
)
968 void wxTextCtrl::SetInsertionPointEnd()
972 long wxTextCtrl::GetInsertionPoint() const
974 return XYToPosition( m_cursorX
, m_cursorY
);
977 long wxTextCtrl::GetLastPosition() const
979 size_t lineCount
= m_lines
.GetCount() - 1;
980 return XYToPosition( m_lines
[lineCount
].m_text
.Len()-1, lineCount
);
983 void wxTextCtrl::SetSelection(long from
, long to
)
987 void wxTextCtrl::SetEditable(bool editable
)
989 m_editable
= editable
;
992 bool wxTextCtrl::Enable( bool enable
)
997 bool wxTextCtrl::SetFont(const wxFont
& font
)
999 wxTextCtrlBase::SetFont( font
);
1001 m_sourceFont
= font
;
1003 wxClientDC
dc(this);
1004 dc
.SetFont( m_sourceFont
);
1005 m_lineHeight
= dc
.GetCharHeight();
1006 m_charWidth
= dc
.GetCharWidth();
1008 // TODO: recalc longest lines
1010 MyAdjustScrollbars();
1015 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1017 return wxWindow::SetForegroundColour( colour
);
1020 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
1022 return wxWindow::SetBackgroundColour( colour
);
1025 //-----------------------------------------------------------------------------
1026 // private code and handlers
1027 //-----------------------------------------------------------------------------
1029 void wxTextCtrl::SearchForBrackets()
1031 int oldBracketY
= m_bracketY
;
1032 int oldBracketX
= m_bracketX
;
1034 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
1036 wxString current
= m_lines
[m_cursorY
].m_text
;
1038 // reverse search first
1043 bracket
= current
[m_cursorX
-1];
1045 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
1047 char antibracket
= '(';
1048 if (bracket
== ']') antibracket
= '[';
1049 if (bracket
== '}') antibracket
= '{';
1053 int endY
= m_cursorY
-60;
1054 if (endY
< 0) endY
= 0;
1055 for (int y
= m_cursorY
; y
>= endY
; y
--)
1057 current
= m_lines
[y
].m_text
;
1059 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
1061 for (int n
= current
.Len()-1; n
>= 0; n
--)
1064 if (current
[n
] == '\'')
1066 for (int m
= n
-1; m
>= 0; m
--)
1068 if (current
[m
] == '\'')
1070 if (m
== 0 || current
[m
-1] != '\\')
1079 if (current
[n
] == '\"')
1081 for (int m
= n
-1; m
>= 0; m
--)
1083 if (current
[m
] == '\"')
1085 if (m
== 0 || current
[m
-1] != '\\')
1093 if (current
[n
] == antibracket
)
1100 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1101 RefreshLine( oldBracketY
);
1102 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1103 RefreshLine( m_bracketY
);
1107 else if (current
[n
] == bracket
)
1118 if ((int)current
.Len() > m_cursorX
)
1119 bracket
= current
[m_cursorX
];
1120 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
1122 char antibracket
= ')';
1123 if (bracket
== '[') antibracket
= ']';
1124 if (bracket
== '{') antibracket
= '}';
1128 int endY
= m_cursorY
+60;
1129 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
1130 for (int y
= m_cursorY
; y
<= endY
; y
++)
1132 current
= m_lines
[y
].m_text
;
1135 start
= m_cursorX
+1;
1137 for (int n
= start
; n
< (int)current
.Len(); n
++)
1140 if (current
[n
] == '\'')
1142 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1144 if (current
[m
] == '\'')
1146 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1155 if (current
[n
] == '\"')
1157 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1159 if (current
[m
] == '\"')
1161 if (m
== 0 || (current
[m
-1] != '\\') || (m
>= 2 && current
[m
-2] == '\\'))
1169 if (current
[n
] == antibracket
)
1176 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1177 RefreshLine( oldBracketY
);
1178 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1179 RefreshLine( m_bracketY
);
1183 else if (current
[n
] == bracket
)
1191 if (oldBracketY
!= -1)
1194 RefreshLine( oldBracketY
);
1198 void wxTextCtrl::Delete()
1200 if (!HasSelection()) return;
1204 int selStartY
= m_selStartY
;
1205 int selEndY
= m_selEndY
;
1206 int selStartX
= m_selStartX
;
1207 int selEndX
= m_selEndX
;
1209 if ((selStartY
> selEndY
) ||
1210 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1212 int tmp
= selStartX
;
1213 selStartX
= selEndX
;
1216 selStartY
= selEndY
;
1220 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1222 if (selStartY
== selEndY
)
1224 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1226 wxString
tmp( m_lines
[selStartY
].m_text
);
1227 if (selStartX
< len
)
1231 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1232 m_lines
[selStartY
].m_text
= tmp
;
1235 m_cursorX
= selStartX
;
1236 RefreshLine( selStartY
);
1240 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1242 if (selStartX
< len
)
1243 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1245 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1246 m_lines
.RemoveAt( selStartY
+1 );
1248 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1249 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1251 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1253 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1254 m_lines
.RemoveAt( selStartY
+1 );
1257 MoveCursor( selStartX
, selStartY
);
1258 MyAdjustScrollbars();
1260 RefreshDown( selStartY
);
1264 void wxTextCtrl::DeleteLine()
1266 if (HasSelection()) return;
1268 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1270 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1272 m_lines
.RemoveAt( m_cursorY
);
1274 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1276 MyAdjustScrollbars();
1277 RefreshDown( m_cursorY
);
1280 void wxTextCtrl::DoChar( char c
)
1284 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1286 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1288 if (m_cursorX
>= (int)tmp
.Len())
1290 int len
= tmp
.Len();
1291 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1298 tmp
.SetChar( m_cursorX
, c
);
1300 tmp
.insert( m_cursorX
, 1, c
);
1303 m_lines
[m_cursorY
].m_text
= tmp
;
1305 // if (tmp.Len() > m_longestLine)
1307 // m_longestLine = tmp.Len();
1308 // MyAdjustScrollbars();
1312 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1314 if (ww
> m_longestLine
)
1317 MyAdjustScrollbars();
1322 int y
= m_cursorY
*m_lineHeight
;
1323 // int x = (m_cursorX-1)*m_charWidth;
1324 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1325 CalcScrolledPosition( x
, y
, &x
, &y
);
1326 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1327 Refresh( TRUE
, &rect
);
1328 // refresh whole line for syntax colour highlighting
1330 Refresh( FALSE
, &rect
);
1334 GetClientSize( &size_x
, &size_y
);
1335 size_x
/= m_charWidth
;
1339 GetViewStart( &view_x
, &view_y
);
1341 //int xx = m_cursorX;
1342 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1346 else if (xx
> view_x
+size_x
-1)
1347 Scroll( xx
-size_x
+1, -1 );
1350 void wxTextCtrl::DoBack()
1356 if (m_cursorY
== 0) return;
1358 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1360 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1362 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1364 m_cursorX
= tmp1
.Len();
1366 tmp1
.Append( tmp2
);
1367 m_lines
[m_cursorY
].m_text
= tmp1
;
1368 m_lines
.RemoveAt( m_cursorY
+1 );
1370 MyAdjustScrollbars();
1371 RefreshDown( m_cursorY
-1 );
1375 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1377 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1378 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1381 int y
= m_cursorY
*m_lineHeight
;
1382 // int x = m_cursorX*m_charWidth;
1383 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1384 CalcScrolledPosition( x
, y
, &x
, &y
);
1385 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1386 Refresh( TRUE
, &rect
);
1387 // refresh whole line for syntax colour highlighting
1389 Refresh( FALSE
, &rect
);
1393 void wxTextCtrl::DoDelete()
1397 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1399 int len
= (int)tmp
.Len();
1400 if (m_cursorX
>= len
)
1402 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1404 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1406 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1409 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1411 m_lines
[m_cursorY
] = tmp
;
1412 m_lines
.RemoveAt( m_cursorY
+1 );
1414 MyAdjustScrollbars();
1415 RefreshDown( m_cursorY
);
1419 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1421 tmp
.Remove( m_cursorX
, 1 );
1422 m_lines
[m_cursorY
].m_text
= tmp
;
1424 int y
= m_cursorY
*m_lineHeight
;
1425 // int x = m_cursorX*m_charWidth;
1426 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1427 CalcScrolledPosition( x
, y
, &x
, &y
);
1428 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1429 Refresh( TRUE
, &rect
);
1430 // refresh whole line for syntax colour highlighting
1432 Refresh( FALSE
, &rect
);
1436 void wxTextCtrl::DoReturn()
1440 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1442 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1443 size_t indent
= tmp
.find_first_not_of( ' ' );
1444 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1446 if (m_cursorX
>= (int)tmp
.Len())
1448 int cursorX
= indent
;
1449 int cursorY
= m_cursorY
+ 1;
1452 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1453 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1455 MyAdjustScrollbars();
1456 MoveCursor( cursorX
, cursorY
);
1457 RefreshDown( m_cursorY
);
1461 wxString
tmp1( tmp
);
1462 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1463 m_lines
[m_cursorY
].m_text
= tmp1
;
1465 wxString
tmp2( tmp
);
1466 tmp2
.Remove( 0, m_cursorX
);
1468 int cursorX
= indent
;
1469 int cursorY
= m_cursorY
+ 1;
1472 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1473 new_tmp
.Append( tmp2
);
1474 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1476 MyAdjustScrollbars();
1477 MoveCursor( cursorX
, cursorY
);
1478 RefreshDown( m_cursorY
-1 );
1482 void wxTextCtrl::DoDClick()
1484 wxString
line( m_lines
[ m_cursorY
].m_text
);
1485 if (m_cursorX
>= (int)line
.Len()) return;
1488 if (((ch
>= 'a') && (ch
<= 'z')) ||
1489 ((ch
>= 'A') && (ch
<= 'Z')) ||
1490 ((ch
>= '0') && (ch
<= '9')) ||
1493 m_selStartY
= m_cursorY
;
1494 m_selEndY
= m_cursorY
;
1498 while (((ch
>= 'a') && (ch
<= 'z')) ||
1499 ((ch
>= 'A') && (ch
<= 'Z')) ||
1500 ((ch
>= '0') && (ch
<= '9')) ||
1511 if (p
< (int)line
.Len())
1514 while (((ch
>= 'a') && (ch
<= 'z')) ||
1515 ((ch
>= 'A') && (ch
<= 'Z')) ||
1516 ((ch
>= '0') && (ch
<= '9')) ||
1519 if (p
>= (int)line
.Len()) break;
1525 RefreshLine( m_cursorY
);
1529 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1532 size_t len
= line
.Len();
1533 for (size_t p
= pos
; p
< len
; p
++)
1535 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1539 for (size_t q
= p
; q
< len
; q
++)
1540 ret
.Append( line
[q
] );
1547 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[p
+1] == '/'))
1549 for (size_t q
= p
; q
< len
; q
++)
1550 ret
.Append( line
[q
] );
1558 ret
.Append( line
[p
] );
1559 for (size_t q
= p
+1; q
< len
; q
++)
1561 ret
.Append( line
[q
] );
1562 if ((line
[q
] == '"') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1569 if (line
[p
] == '\'')
1571 ret
.Append( line
[p
] );
1572 for (size_t q
= p
+1; q
< len
; q
++)
1574 ret
.Append( line
[q
] );
1575 if ((line
[q
] == '\'') && ((line
[q
-1] != '\\') || (q
>= 2 && line
[q
-2] == '\\')))
1582 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1583 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1587 ret
.Append( line
[p
] );
1588 for (size_t q
= p
+1; q
< len
; q
++)
1590 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1591 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1592 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1595 ret
.Append( line
[q
] );
1612 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1617 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1620 size_t len
= origin
.Len();
1621 dc
.SetTextForeground( colour
);
1624 while (toDraw
[pos
] == wxT(' '))
1627 if (pos
== len
) return;
1633 current
+= toDraw
[pos
];
1635 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1637 current
+= toDraw
[pos
];
1642 wxString tmp
= origin
.Left( start
);
1643 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1646 dc
.DrawText( current
, xx
, yy
);
1650 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1652 int selStartY
= m_selStartY
;
1653 int selEndY
= m_selEndY
;
1654 int selStartX
= m_selStartX
;
1655 int selEndX
= m_selEndX
;
1657 if ((selStartY
> selEndY
) ||
1658 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1660 int tmp
= selStartX
;
1661 selStartX
= selEndX
;
1664 selStartY
= selEndY
;
1668 wxString
line( line2
);
1669 if (HasFlag(wxTE_PASSWORD
))
1671 size_t len
= line
.Len();
1672 line
= wxString( wxT('*'), len
);
1675 wxString
keyword( ' ', line
.Len() );
1676 wxString
define( ' ', line
.Len() );
1677 wxString
variable( ' ', line
.Len() );
1678 wxString
comment( ' ', line
.Len() );
1679 wxString
my_string( ' ', line
.Len() );
1680 wxString
selection( ' ', line
.Len() );
1682 if (m_lang
!= wxSOURCE_LANG_NONE
)
1684 if (lineNum
== m_bracketY
)
1686 wxString
red( ' ', line
.Len() );
1687 if (m_bracketX
< (int)line
.Len())
1689 red
.SetChar( m_bracketX
, line
[m_bracketX
] );
1690 line
.SetChar( m_bracketX
, ' ' );
1691 dc
.SetTextForeground( *wxRED
);
1692 dc
.DrawText( red
, x
, y
);
1693 dc
.SetTextForeground( *wxBLACK
);
1698 wxString
token( GetNextToken( line
, pos
) );
1699 while (!token
.IsNull())
1701 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1703 size_t end_pos
= pos
+ token
.Len();
1704 for (size_t i
= pos
; i
< end_pos
; i
++)
1706 keyword
[i
] = line
[i
];
1710 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1712 size_t end_pos
= pos
+ token
.Len();
1713 for (size_t i
= pos
; i
< end_pos
; i
++)
1715 define
[i
] = line
[i
];
1719 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1720 ((token
.Len() > 2) && (token
[0] == 'w') && (token
[1] == 'x')))
1722 size_t end_pos
= pos
+ token
.Len();
1723 for (size_t i
= pos
; i
< end_pos
; i
++)
1725 variable
[i
] = line
[i
];
1729 if ((token
.Len() >= 2) && (token
[0] == '/') && (token
[1] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1731 size_t end_pos
= pos
+ token
.Len();
1732 for (size_t i
= pos
; i
< end_pos
; i
++)
1734 comment
[i
] = line
[i
];
1738 if ((token
[0] == '#') &&
1739 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1741 size_t end_pos
= pos
+ token
.Len();
1742 for (size_t i
= pos
; i
< end_pos
; i
++)
1744 comment
[i
] = line
[i
];
1748 if ((token
[0] == '"') || (token
[0] == '\''))
1750 size_t end_pos
= pos
+ token
.Len();
1751 for (size_t i
= pos
; i
< end_pos
; i
++)
1753 my_string
[i
] = line
[i
];
1758 token
= GetNextToken( line
, pos
);
1762 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1764 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1765 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1766 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1767 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1768 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1769 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1770 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1774 if (selStartY
== selEndY
)
1776 // int xx = selStartX*m_charWidth;
1777 int xx
= PosToPixel( lineNum
, selStartX
);
1778 // int ww = (selEndX-selStartX)*m_charWidth;
1779 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1780 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1782 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1784 selection
[i
] = line
[i
];
1788 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1790 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1792 for (size_t i
= 0; i
< line
.Len(); i
++)
1794 selection
[i
] = line
[i
];
1798 if (lineNum
== selStartY
)
1800 // int xx = selStartX*m_charWidth;
1801 int xx
= PosToPixel( lineNum
, selStartX
);
1802 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1804 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1806 selection
[i
] = line
[i
];
1810 if (lineNum
== selEndY
)
1812 // int ww = selEndX*m_charWidth;
1813 int ww
= PosToPixel( lineNum
, selEndX
);
1814 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1816 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1818 selection
[i
] = line
[i
];
1823 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1824 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1825 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1826 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1827 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1828 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1829 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1832 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1836 if (m_lines
.GetCount() == 0) return;
1840 dc
.SetFont( m_sourceFont
);
1843 GetViewStart( NULL
, &scroll_y
);
1845 // We have a inner border of two pixels
1846 // around the text, so scroll units do
1847 // not correspond to lines.
1848 if (scroll_y
> 0) scroll_y
--;
1852 GetClientSize( &size_x
, &size_y
);
1854 dc
.SetPen( *wxTRANSPARENT_PEN
);
1855 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1856 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+2 );
1857 for (int i
= scroll_y
; i
< upper
; i
++)
1860 int y
= i
*m_lineHeight
+2;
1862 int h
= m_lineHeight
;
1863 CalcScrolledPosition( x
,y
,&x
,&y
);
1864 if (IsExposed(x
,y
,w
,h
))
1865 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1868 if (m_editable
&& (FindFocus() == this))
1870 ///dc.SetBrush( *wxRED_BRUSH );
1871 dc
.SetBrush( *wxBLACK_BRUSH
);
1872 // int xx = m_cursorX*m_charWidth;
1873 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1874 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1878 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1880 if (m_lines
.GetCount() == 0) return;
1883 #if 0 // there is no middle button on iPAQs
1884 if (event
.MiddleDown())
1891 if (event
.LeftDClick())
1897 if (event
.LeftDown())
1905 m_capturing
= FALSE
;
1909 if (event
.LeftDown() ||
1910 (event
.LeftIsDown() && m_capturing
))
1912 int x
= event
.GetX();
1913 int y
= event
.GetY();
1914 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1916 // x /= m_charWidth;
1917 x
= PixelToPos( y
, x
);
1919 wxMin( 1000, wxMax( 0, x
) ),
1920 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1921 event
.ShiftDown() || !event
.LeftDown() );
1925 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1927 if (m_lines
.GetCount() == 0) return;
1929 if (!m_editable
) return;
1933 GetClientSize( &size_x
, &size_y
);
1934 size_x
/= m_charWidth
;
1935 size_y
/= m_lineHeight
;
1938 if (event
.ShiftDown())
1940 switch (event
.GetKeyCode())
1942 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1943 case '8': event
.m_keyCode
= WXK_UP
; break;
1944 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1945 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1946 case '9': event
.m_keyCode
= WXK_PRIOR
; break;
1947 case '3': event
.m_keyCode
= WXK_NEXT
; break;
1948 case '7': event
.m_keyCode
= WXK_HOME
; break;
1949 case '1': event
.m_keyCode
= WXK_END
; break;
1950 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1954 switch (event
.GetKeyCode())
1958 if (m_ignoreInput
) return;
1960 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1961 m_ignoreInput
= TRUE
;
1966 if (m_ignoreInput
) return;
1967 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1968 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1969 m_ignoreInput
= TRUE
;
1974 if (m_ignoreInput
) return;
1977 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1982 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1984 m_ignoreInput
= TRUE
;
1989 if (m_ignoreInput
) return;
1990 if (m_cursorX
< 1000)
1991 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1992 m_ignoreInput
= TRUE
;
1997 if (event
.ControlDown())
1998 MoveCursor( 0, 0, event
.ShiftDown() );
2000 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
2005 if (event
.ControlDown())
2006 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
2008 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
2013 if (m_ignoreInput
) return;
2014 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
2015 m_ignoreInput
= TRUE
;
2020 if (m_ignoreInput
) return;
2021 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
2022 m_ignoreInput
= TRUE
;
2027 if (event
.ShiftDown())
2029 else if (event
.ControlDown())
2032 m_overwrite
= !m_overwrite
;
2037 if (m_windowStyle
& wxPROCESS_ENTER
)
2039 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
2040 event
.SetEventObject(this);
2041 event
.SetString(GetValue());
2042 if (GetEventHandler()->ProcessEvent(event
)) return;
2060 bool save_overwrite
= m_overwrite
;
2061 m_overwrite
= FALSE
;
2062 int i
= 4-(m_cursorX
% 4);
2064 for (int c
= 0; c
< i
; c
++)
2066 m_overwrite
= save_overwrite
;
2087 if ( (event
.KeyCode() >= 'a') &&
2088 (event
.KeyCode() <= 'z') &&
2096 if ( (event
.KeyCode() >= 32) &&
2097 (event
.KeyCode() <= 255) &&
2098 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
2102 DoChar( (char) event
.KeyCode() );
2111 void wxTextCtrl::OnIdle( wxIdleEvent
&event
)
2113 m_ignoreInput
= FALSE
;
2115 if (m_lang
!= wxSOURCE_LANG_NONE
)
2116 SearchForBrackets();
2121 void wxTextCtrl::Indent()
2123 int startY
= m_cursorY
;
2124 int endY
= m_cursorY
;
2127 startY
= m_selStartY
;
2137 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2139 for (int i
= startY
; i
<= endY
; i
++)
2141 m_lines
[i
].m_text
.insert( 0u, " " );
2146 void wxTextCtrl::Unindent()
2148 int startY
= m_cursorY
;
2149 int endY
= m_cursorY
;
2152 startY
= m_selStartY
;
2162 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2164 for (int i
= startY
; i
<= endY
; i
++)
2166 for (int n
= 0; n
< 4; n
++)
2168 if (m_lines
[i
].m_text
[0u] == ' ')
2169 m_lines
[i
].m_text
.erase(0u,1u);
2174 bool wxTextCtrl::HasSelection()
2176 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2179 void wxTextCtrl::ClearSelection()
2187 void wxTextCtrl::RefreshLine( int n
)
2189 int y
= n
*m_lineHeight
;
2191 CalcScrolledPosition( x
, y
, &x
, &y
);
2192 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2193 Refresh( TRUE
, &rect
);
2196 void wxTextCtrl::RefreshDown( int n
)
2200 GetClientSize( &size_x
, &size_y
);
2204 GetViewStart( &view_x
, &view_y
);
2212 int y
= n
*m_lineHeight
;
2214 CalcScrolledPosition( x
, y
, &x
, &y
);
2216 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2217 Refresh( TRUE
, &rect
);
2221 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2223 if (!m_editable
) return;
2225 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
2227 if (new_x
> m_lines
[new_y
].m_text
.Len())
2228 new_x
= m_lines
[new_y
].m_text
.Len();
2231 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2233 bool no_cursor_refresh
= FALSE
;
2234 bool has_selection
= HasSelection();
2239 bool erase_background
= TRUE
;
2243 m_selStartX
= m_cursorX
;
2244 m_selStartY
= m_cursorY
;
2248 if (new_y
> m_selStartY
)
2250 y
= m_selStartY
*m_lineHeight
;
2251 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2253 else if (new_y
== m_selStartY
)
2255 x
= PosToPixel( new_y
, m_selStartX
);
2256 w
= PosToPixel( new_y
, new_x
) - x
;
2260 w
= -w
+ 2; // +2 for the cursor
2262 y
= m_selStartY
*m_lineHeight
;
2267 y
= new_y
*m_lineHeight
;
2268 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2271 no_cursor_refresh
= TRUE
;
2277 if (new_y
== m_selEndY
)
2279 y
= new_y
*m_lineHeight
;
2281 if (m_selEndX
> new_x
)
2283 // x = new_x*m_charWidth;
2284 x
= PosToPixel( new_y
, new_x
);
2285 // w = (m_selEndX-new_x)*m_charWidth;
2286 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2290 // x = m_selEndX*m_charWidth;
2291 x
= PosToPixel( new_y
, m_selEndX
);
2292 // w = (-m_selEndX+new_x)*m_charWidth;
2293 w
= PosToPixel( new_y
, new_x
) - x
;
2300 if (new_y
> m_selEndY
)
2302 y
= m_selEndY
*m_lineHeight
;
2303 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2305 erase_background
= ((m_selEndY
< m_selStartY
) ||
2306 ((m_selEndY
== m_selStartY
) && (m_selEndX
< m_selStartX
)));
2310 y
= new_y
*m_lineHeight
;
2311 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2313 erase_background
= ((m_selEndY
> m_selStartY
) ||
2314 ((m_selEndY
== m_selStartY
) && (m_selEndX
> m_selStartX
)));
2316 no_cursor_refresh
= TRUE
;
2325 CalcScrolledPosition( x
, y
, &x
, &y
);
2326 wxRect
rect( x
+2, y
+2, w
, h
);
2327 Refresh( erase_background
, &rect
);
2333 int ry1
= m_selEndY
;
2334 int ry2
= m_selStartY
;
2348 int y
= ry1
*m_lineHeight
;
2349 CalcScrolledPosition( x
, y
, &x
, &y
);
2350 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2352 Refresh( TRUE
, &rect
);
2357 printf( "startx %d starty %d endx %d endy %d\n",
2358 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2360 printf( "has %d\n", (int)HasSelection() );
2363 if (!no_cursor_refresh
)
2365 // int x = m_cursorX*m_charWidth;
2366 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2367 int y
= m_cursorY
*m_lineHeight
;
2368 CalcScrolledPosition( x
, y
, &x
, &y
);
2369 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2374 Refresh( TRUE
, &rect
);
2376 if (FindFocus() == this)
2378 wxClientDC
dc(this);
2380 dc
.SetPen( *wxTRANSPARENT_PEN
);
2381 //dc.SetBrush( *wxRED_BRUSH );
2382 dc
.SetBrush( *wxBLACK_BRUSH
);
2383 // int xx = m_cursorX*m_charWidth;
2384 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2385 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2391 GetClientSize( &size_x
, &size_y
);
2392 size_x
/= m_charWidth
;
2393 size_y
/= m_lineHeight
;
2397 GetViewStart( &view_x
, &view_y
);
2401 int sy
= m_cursorY
- (size_y
/2);
2407 if (m_cursorY
< view_y
)
2408 Scroll( -1, m_cursorY
);
2409 else if (m_cursorY
> view_y
+size_y
-1)
2410 Scroll( -1, m_cursorY
-size_y
+1 );
2413 //int xx = m_cursorX;
2414 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2418 else if (xx
> view_x
+size_x
-1)
2419 Scroll( xx
-size_x
+1, -1 );
2422 void wxTextCtrl::MyAdjustScrollbars()
2427 int y_range
= m_lines
.GetCount();
2430 GetClientSize( NULL
, &height
);
2432 if (height
>= m_lines
.GetCount() *m_lineHeight
)
2437 GetViewStart( &view_x
, &view_y
);
2439 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2442 //-----------------------------------------------------------------------------
2443 // clipboard handlers
2444 //-----------------------------------------------------------------------------
2446 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2451 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2456 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2461 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2466 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2471 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2473 event
.Enable( CanCut() );
2476 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2478 event
.Enable( CanCopy() );
2481 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2483 event
.Enable( CanPaste() );
2486 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2488 event
.Enable( CanUndo() );
2491 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2493 event
.Enable( CanRedo() );
2496 wxSize
wxTextCtrl::DoGetBestSize() const
2500 wxSize
ret(80, m_lineHeight
+ 4);
2502 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2505 if (HasFlag(wxBORDER_SIMPLE
))
2512 return wxSize(80, 60);
2516 // ----------------------------------------------------------------------------
2518 // ----------------------------------------------------------------------------
2520 void wxTextCtrl::Freeze()
2524 void wxTextCtrl::Thaw()
2528 void wxTextCtrl::OnSetFocus( wxFocusEvent
& event
)
2530 // To hide or show caret, as appropriate
2534 void wxTextCtrl::OnKillFocus( wxFocusEvent
& event
)
2536 // To hide or show caret, as appropriate
2540 // ----------------------------------------------------------------------------
2541 // text control scrolling
2542 // ----------------------------------------------------------------------------
2544 bool wxTextCtrl::ScrollLines(int lines
)
2546 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2551 bool wxTextCtrl::ScrollPages(int pages
)
2553 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");