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"
22 #include "wx/dcclient.h"
24 #include "wx/univ/inphand.h"
25 #include "wx/univ/renderer.h"
26 #include "wx/univ/colschem.h"
27 #include "wx/univ/theme.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type
, int y1
, int y2
, wxTextCtrl
*owner
)
40 m_cursorX
= m_owner
->GetCursorX();
41 m_cursorY
= m_owner
->GetCursorY();
43 if (m_type
== wxSOURCE_UNDO_LINE
)
45 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
47 if (m_type
== wxSOURCE_UNDO_ENTER
)
49 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
51 if (m_type
== wxSOURCE_UNDO_BACK
)
53 for (int i
= m_y1
; i
< m_y2
+2; i
++)
55 if (i
>= (int)m_owner
->m_lines
.GetCount())
56 m_lines
.Add( wxT("") );
58 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
61 if (m_type
== wxSOURCE_UNDO_DELETE
)
63 for (int i
= m_y1
; i
< m_y2
+1; i
++)
65 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
68 if (m_type
== wxSOURCE_UNDO_PASTE
)
70 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
74 void wxSourceUndoStep::Undo()
76 if (m_type
== wxSOURCE_UNDO_LINE
)
78 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
79 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
80 m_owner
->RefreshLine( m_y1
);
82 if (m_type
== wxSOURCE_UNDO_ENTER
)
84 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
85 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
86 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
87 m_owner
->RefreshDown( m_y1
);
89 if (m_type
== wxSOURCE_UNDO_BACK
)
91 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
92 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[1] ), m_y1
+1 );
93 m_owner
->MyAdjustScrollbars();
94 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
95 m_owner
->RefreshDown( m_y1
);
97 if (m_type
== wxSOURCE_UNDO_DELETE
)
99 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
100 for (int i
= 1; i
< (int)m_lines
.GetCount(); i
++)
101 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[i
] ), m_y1
+i
);
102 m_owner
->MyAdjustScrollbars();
103 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
104 m_owner
->RefreshDown( m_y1
);
106 if (m_type
== wxSOURCE_UNDO_PASTE
)
108 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
109 for (int i
= 0; i
< m_y2
-m_y1
; i
++)
110 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
111 m_owner
->MyAdjustScrollbars();
112 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
113 m_owner
->RefreshDown( m_y1
);
115 if (m_type
== wxSOURCE_UNDO_INSERT_LINE
)
117 m_owner
->m_lines
.RemoveAt( m_y1
);
118 m_owner
->MyAdjustScrollbars();
119 m_owner
->MoveCursor( 0, m_y1
);
120 m_owner
->RefreshDown( m_y1
);
124 #include "wx/arrimpl.cpp"
125 WX_DEFINE_OBJARRAY(wxSourceLineArray
);
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
133 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
134 EVT_PAINT(wxTextCtrl::OnPaint
)
135 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
136 EVT_CHAR(wxTextCtrl::OnChar
)
137 EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse
)
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 wxString oldValue
= GetValue();
288 m_lines
.Add( new wxSourceLine( wxT("") ) );
296 pos
= value
.find( wxT('\n'), begin
);
299 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, value
.Len()-begin
) );
302 // if (sl->m_text.Len() > m_longestLine)
303 // m_longestLine = sl->m_text.Len();
305 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
307 if (ww
> m_longestLine
)
314 wxSourceLine
*sl
= new wxSourceLine( value
.Mid( begin
, pos
-begin
) );
317 // if (sl->m_text.Len() > m_longestLine)
318 // m_longestLine = sl->m_text.Len();
320 GetTextExtent( sl
->m_text
, &ww
, NULL
, NULL
, NULL
);
322 if (ww
> m_longestLine
)
330 // Don't need to refresh if the value hasn't changed
331 if ((GetWindowStyle() & wxTE_MULTILINE
) == 0)
333 if (value
== oldValue
)
337 MyAdjustScrollbars();
342 int wxTextCtrl::GetLineLength(long lineNo
) const
344 if (lineNo
>= (long)m_lines
.GetCount())
347 return m_lines
[lineNo
].m_text
.Len();
350 wxString
wxTextCtrl::GetLineText(long lineNo
) const
352 if (lineNo
>= (long)m_lines
.GetCount())
355 return m_lines
[lineNo
].m_text
;
358 int wxTextCtrl::GetNumberOfLines() const
360 return m_lines
.GetCount();
363 bool wxTextCtrl::IsModified() const
368 bool wxTextCtrl::IsEditable() const
373 void wxTextCtrl::GetSelection(long* from
, long* to
) const
375 if (m_selStartX
== -1 || m_selStartY
== -1 ||
376 m_selEndX
== -1 || m_selEndY
== -1)
378 *from
= GetInsertionPoint();
379 *to
= GetInsertionPoint();
383 *from
= XYToPosition(m_selStartX
, m_selStartY
);
384 *to
= XYToPosition(m_selEndX
, m_selEndY
);
388 void wxTextCtrl::Clear()
396 m_lines
.Add( new wxSourceLine( wxT("") ) );
398 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
403 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
407 void wxTextCtrl::Remove(long from
, long to
)
412 void wxTextCtrl::DiscardEdits()
418 void wxTextCtrl::SetMaxLength(unsigned long len
)
422 int wxTextCtrl::PosToPixel( int line
, int pos
)
424 // TODO add support for Tabs
426 if (line
>= (int)m_lines
.GetCount()) return 0;
427 if (pos
< 0) return 0;
429 wxString text
= m_lines
[line
].m_text
;
431 if (text
.IsEmpty()) return 0;
433 if (pos
< (int)text
.Len())
434 text
.Remove( pos
, text
.Len()-pos
);
438 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
443 int wxTextCtrl::PixelToPos( int line
, int pixel
)
445 if (pixel
< 2) return 0;
447 if (line
>= (int)m_lines
.GetCount()) return 0;
449 wxString text
= m_lines
[line
].m_text
;
452 int res
= text
.Len();
455 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
461 text
.Remove( res
,1 );
467 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
474 void wxTextCtrl::WriteText(const wxString
& text2
)
476 if (text2
.IsEmpty()) return;
480 wxString
text( text2
);
483 while ( (pos
= text
.Find('\n')) != -1 )
485 lines
.Add( text
.Left( pos
) );
486 text
.Remove( 0, pos
+1 );
489 int count
= (int)lines
.GetCount();
491 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
492 wxString
tmp2( tmp1
);
493 int len
= (int)tmp1
.Len();
498 for (int i
= 0; i
< m_cursorX
-len
; i
++)
500 m_lines
[m_cursorY
].m_text
.Append( tmp
);
505 tmp1
.Remove( m_cursorX
);
506 tmp2
.Remove( 0, m_cursorX
);
507 tmp1
.Append( lines
[0] );
511 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
514 m_lines
[m_cursorY
].m_text
= tmp1
;
515 RefreshLine( m_cursorY
);
519 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
521 m_lines
[m_cursorY
].m_text
= tmp1
;
523 for (i
= 1; i
< count
; i
++)
524 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
525 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
527 MyAdjustScrollbars();
528 RefreshDown( m_cursorY
);
532 void wxTextCtrl::AppendText(const wxString
& text2
)
534 if (text2
.IsEmpty()) return;
538 wxString
text( text2
);
541 while ( (pos
= text
.Find('\n')) != -1 )
543 lines
.Add( text
.Left( pos
) );
544 text
.Remove( 0, pos
+1 );
547 int count
= (int)lines
.GetCount();
549 size_t y
= m_lines
.GetCount()-1;
551 wxString
tmp( m_lines
[y
].m_text
);
552 tmp
.Append( lines
[0] );
556 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
558 m_lines
[y
].m_text
= tmp
;
563 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
565 m_lines
[y
].m_text
= tmp
;
567 for (i
= 1; i
< count
; i
++)
568 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
570 MyAdjustScrollbars();
575 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
580 long wxTextCtrl::XYToPosition(long x
, long y
) const
584 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
588 // Add one for the end-of-line character
589 ret
+= m_lines
[i
].m_text
.Len() + 1;
593 if ((size_t)x
< (m_lines
[i
].m_text
.Len()+1))
596 return (ret
+ m_lines
[i
].m_text
.Len() + 1);
602 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
604 if (m_lines
.GetCount() == 0)
615 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
617 //pos -= m_lines[i].m_text.Len();
620 // Add one for the end-of-line character. (In Windows,
621 // there are _two_ positions for each end of line.)
622 if (pos
<= ((int)m_lines
[i
].m_text
.Len()))
629 pos
-= (m_lines
[i
].m_text
.Len() + 1);
634 //xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len();
642 void wxTextCtrl::ShowPosition(long pos
)
646 void wxTextCtrl::Copy()
648 if (!HasSelection()) return;
652 int selStartY
= m_selStartY
;
653 int selEndY
= m_selEndY
;
654 int selStartX
= m_selStartX
;
655 int selEndX
= m_selEndX
;
657 if ((selStartY
> selEndY
) ||
658 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
668 if (selStartY
== selEndY
)
670 sel
= m_lines
[selStartY
].m_text
;
672 if (selStartX
>= (int)sel
.Len()) return;
673 if (selEndX
> (int)sel
.Len())
676 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
677 sel
.Remove( 0, selStartX
);
681 wxString
tmp( m_lines
[selStartY
].m_text
);
683 if (selStartX
< (int)tmp
.Len())
685 tmp
.Remove( 0, selStartX
);
687 sel
.Append( wxT("\n") );
689 for (int i
= selStartY
+1; i
< selEndY
; i
++)
691 sel
.Append( m_lines
[i
].m_text
);
692 sel
.Append( wxT("\n") );
694 tmp
= m_lines
[selEndY
].m_text
;
695 if (selEndX
> (int)tmp
.Len())
699 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
704 if (wxTheClipboard
->Open())
706 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
707 wxTheClipboard
->Close();
711 void wxTextCtrl::Cut()
718 void wxTextCtrl::Paste()
722 if (!wxTheClipboard
->Open()) return;
724 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
726 wxTheClipboard
->Close();
731 wxTextDataObject data
;
733 bool ret
= wxTheClipboard
->GetData( data
);
735 wxTheClipboard
->Close();
741 wxString
text( data
.GetText() );
744 while ( (pos
= text
.Find('\n')) != -1 )
746 lines
.Add( text
.Left( pos
) );
747 text
.Remove( 0, pos
+1 );
750 int count
= (int)lines
.GetCount();
752 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
753 wxString
tmp2( tmp1
);
754 int len
= (int)tmp1
.Len();
759 for (int i
= 0; i
< m_cursorX
-len
; i
++)
761 m_lines
[m_cursorY
].m_text
.Append( tmp
);
766 tmp1
.Remove( m_cursorX
);
767 tmp2
.Remove( 0, m_cursorX
);
768 tmp1
.Append( lines
[0] );
772 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
775 m_lines
[m_cursorY
].m_text
= tmp1
;
776 RefreshLine( m_cursorY
);
780 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
782 m_lines
[m_cursorY
].m_text
= tmp1
;
784 for (i
= 1; i
< count
; i
++)
785 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
786 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
788 MyAdjustScrollbars();
789 RefreshDown( m_cursorY
);
793 void wxTextCtrl::Undo()
795 if (m_undos
.GetCount() == 0) return;
797 wxList::Node
*node
= m_undos
.Item( m_undos
.GetCount()-1 );
798 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->GetData();
807 void wxTextCtrl::SetInsertionPoint(long pos
)
811 PositionToXY(pos
, & x
, & y
);
814 // TODO: scroll to this position if necessary
818 void wxTextCtrl::SetInsertionPointEnd()
820 SetInsertionPoint(GetLastPosition());
823 long wxTextCtrl::GetInsertionPoint() const
825 return XYToPosition( m_cursorX
, m_cursorY
);
828 long wxTextCtrl::GetLastPosition() const
830 size_t lineCount
= m_lines
.GetCount() - 1;
831 // It's the length of the line, not the length - 1,
832 // because there's a position after the last character.
833 return XYToPosition( m_lines
[lineCount
].m_text
.Len(), lineCount
);
836 void wxTextCtrl::SetSelection(long from
, long to
)
840 void wxTextCtrl::SetEditable(bool editable
)
842 m_editable
= editable
;
845 bool wxTextCtrl::Enable( bool enable
)
850 bool wxTextCtrl::SetFont(const wxFont
& font
)
852 wxTextCtrlBase::SetFont( font
);
857 dc
.SetFont( m_sourceFont
);
858 m_lineHeight
= dc
.GetCharHeight();
859 m_charWidth
= dc
.GetCharWidth();
861 // TODO: recalc longest lines
863 MyAdjustScrollbars();
868 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
870 return wxWindow::SetForegroundColour( colour
);
873 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
875 return wxWindow::SetBackgroundColour( colour
);
878 //-----------------------------------------------------------------------------
879 // private code and handlers
880 //-----------------------------------------------------------------------------
882 void wxTextCtrl::SearchForBrackets()
884 int oldBracketY
= m_bracketY
;
885 int oldBracketX
= m_bracketX
;
887 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
889 wxString current
= m_lines
[m_cursorY
].m_text
;
891 // reverse search first
896 bracket
= current
[(size_t) (m_cursorX
-1)];
898 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
900 char antibracket
= '(';
901 if (bracket
== ']') antibracket
= '[';
902 if (bracket
== '}') antibracket
= '{';
906 int endY
= m_cursorY
-60;
907 if (endY
< 0) endY
= 0;
908 for (int y
= m_cursorY
; y
>= endY
; y
--)
910 current
= m_lines
[y
].m_text
;
912 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
914 for (int n
= current
.Len()-1; n
>= 0; n
--)
917 if (current
[(size_t) (n
)] == '\'')
919 for (int m
= n
-1; m
>= 0; m
--)
921 if (current
[(size_t) (m
)] == '\'')
923 if (m
== 0 || current
[(size_t) (m
-1)] != '\\')
932 if (current
[(size_t) (n
)] == '\"')
934 for (int m
= n
-1; m
>= 0; m
--)
936 if (current
[(size_t) (m
)] == '\"')
938 if (m
== 0 || current
[(size_t) (m
-1)] != '\\')
946 if (current
[(size_t) (n
)] == antibracket
)
953 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
954 RefreshLine( oldBracketY
);
955 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
956 RefreshLine( m_bracketY
);
960 else if (current
[(size_t) (n
)] == bracket
)
971 if ((int)current
.Len() > m_cursorX
)
972 bracket
= current
[(size_t) (m_cursorX
)];
973 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
975 char antibracket
= ')';
976 if (bracket
== '[') antibracket
= ']';
977 if (bracket
== '{') antibracket
= '}';
981 int endY
= m_cursorY
+60;
982 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
983 for (int y
= m_cursorY
; y
<= endY
; y
++)
985 current
= m_lines
[y
].m_text
;
990 for (int n
= start
; n
< (int)current
.Len(); n
++)
993 if (current
[(size_t) (n
)] == '\'')
995 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
997 if (current
[(size_t) (m
)] == '\'')
999 if (m
== 0 || (current
[(size_t) (m
-1)] != '\\') || (m
>= 2 && current
[(size_t) (m
-2)] == '\\'))
1008 if (current
[(size_t) (n
)] == '\"')
1010 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1012 if (current
[(size_t) (m
)] == '\"')
1014 if (m
== 0 || (current
[(size_t) (m
-1)] != '\\') || (m
>= 2 && current
[(size_t) (m
-2)] == '\\'))
1022 if (current
[(size_t) (n
)] == antibracket
)
1029 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1030 RefreshLine( oldBracketY
);
1031 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1032 RefreshLine( m_bracketY
);
1036 else if (current
[(size_t) (n
)] == bracket
)
1044 if (oldBracketY
!= -1)
1047 RefreshLine( oldBracketY
);
1051 void wxTextCtrl::Delete()
1053 if (!HasSelection()) return;
1057 int selStartY
= m_selStartY
;
1058 int selEndY
= m_selEndY
;
1059 int selStartX
= m_selStartX
;
1060 int selEndX
= m_selEndX
;
1062 if ((selStartY
> selEndY
) ||
1063 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1065 int tmp
= selStartX
;
1066 selStartX
= selEndX
;
1069 selStartY
= selEndY
;
1073 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1075 if (selStartY
== selEndY
)
1077 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1079 wxString
tmp( m_lines
[selStartY
].m_text
);
1080 if (selStartX
< len
)
1084 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1085 m_lines
[selStartY
].m_text
= tmp
;
1088 m_cursorX
= selStartX
;
1089 RefreshLine( selStartY
);
1093 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1095 if (selStartX
< len
)
1096 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1098 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1099 m_lines
.RemoveAt( selStartY
+1 );
1101 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1102 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1104 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1106 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1107 m_lines
.RemoveAt( selStartY
+1 );
1110 MoveCursor( selStartX
, selStartY
);
1111 MyAdjustScrollbars();
1113 RefreshDown( selStartY
);
1117 void wxTextCtrl::DeleteLine()
1119 if (HasSelection()) return;
1121 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1123 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1125 m_lines
.RemoveAt( m_cursorY
);
1127 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1129 MyAdjustScrollbars();
1130 RefreshDown( m_cursorY
);
1133 void wxTextCtrl::DoChar( char c
)
1137 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1139 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1141 if (m_cursorX
>= (int)tmp
.Len())
1143 int len
= tmp
.Len();
1144 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1151 tmp
.SetChar( m_cursorX
, c
);
1153 tmp
.insert( m_cursorX
, 1, c
);
1156 m_lines
[m_cursorY
].m_text
= tmp
;
1158 // if (tmp.Len() > m_longestLine)
1160 // m_longestLine = tmp.Len();
1161 // MyAdjustScrollbars();
1165 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1167 if (ww
> m_longestLine
)
1170 MyAdjustScrollbars();
1175 int y
= m_cursorY
*m_lineHeight
;
1176 // int x = (m_cursorX-1)*m_charWidth;
1177 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1178 CalcScrolledPosition( x
, y
, &x
, &y
);
1179 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1180 Refresh( TRUE
, &rect
);
1181 // refresh whole line for syntax colour highlighting
1183 Refresh( FALSE
, &rect
);
1187 GetClientSize( &size_x
, &size_y
);
1188 size_x
/= m_charWidth
;
1192 GetViewStart( &view_x
, &view_y
);
1194 //int xx = m_cursorX;
1195 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1199 else if (xx
> view_x
+size_x
-1)
1200 Scroll( xx
-size_x
+1, -1 );
1203 void wxTextCtrl::DoBack()
1209 if (m_cursorY
== 0) return;
1211 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1213 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1215 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1217 m_cursorX
= tmp1
.Len();
1219 tmp1
.Append( tmp2
);
1220 m_lines
[m_cursorY
].m_text
= tmp1
;
1221 m_lines
.RemoveAt( m_cursorY
+1 );
1223 MyAdjustScrollbars();
1224 RefreshDown( m_cursorY
-1 );
1228 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1230 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1231 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1234 int y
= m_cursorY
*m_lineHeight
;
1235 // int x = m_cursorX*m_charWidth;
1236 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1237 CalcScrolledPosition( x
, y
, &x
, &y
);
1238 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1239 Refresh( TRUE
, &rect
);
1240 // refresh whole line for syntax colour highlighting
1242 Refresh( FALSE
, &rect
);
1246 void wxTextCtrl::DoDelete()
1250 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1252 int len
= (int)tmp
.Len();
1253 if (m_cursorX
>= len
)
1255 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1257 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1259 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1262 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1264 m_lines
[m_cursorY
] = tmp
;
1265 m_lines
.RemoveAt( m_cursorY
+1 );
1267 MyAdjustScrollbars();
1268 RefreshDown( m_cursorY
);
1272 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1274 tmp
.Remove( m_cursorX
, 1 );
1275 m_lines
[m_cursorY
].m_text
= tmp
;
1277 int y
= m_cursorY
*m_lineHeight
;
1278 // int x = m_cursorX*m_charWidth;
1279 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1280 CalcScrolledPosition( x
, y
, &x
, &y
);
1281 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1282 Refresh( TRUE
, &rect
);
1283 // refresh whole line for syntax colour highlighting
1285 Refresh( FALSE
, &rect
);
1289 void wxTextCtrl::DoReturn()
1293 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1295 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1296 size_t indent
= tmp
.find_first_not_of( ' ' );
1297 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1299 if (m_cursorX
>= (int)tmp
.Len())
1301 int cursorX
= indent
;
1302 int cursorY
= m_cursorY
+ 1;
1305 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1306 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1308 MyAdjustScrollbars();
1309 MoveCursor( cursorX
, cursorY
);
1310 RefreshDown( m_cursorY
);
1314 wxString
tmp1( tmp
);
1315 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1316 m_lines
[m_cursorY
].m_text
= tmp1
;
1318 wxString
tmp2( tmp
);
1319 tmp2
.Remove( 0, m_cursorX
);
1321 int cursorX
= indent
;
1322 int cursorY
= m_cursorY
+ 1;
1325 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1326 new_tmp
.Append( tmp2
);
1327 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1329 MyAdjustScrollbars();
1330 MoveCursor( cursorX
, cursorY
);
1331 RefreshDown( m_cursorY
-1 );
1335 void wxTextCtrl::DoDClick()
1337 wxString
line( m_lines
[ m_cursorY
].m_text
);
1338 if (m_cursorX
>= (int)line
.Len()) return;
1340 char ch
= line
[(size_t) (p
)];
1341 if (((ch
>= 'a') && (ch
<= 'z')) ||
1342 ((ch
>= 'A') && (ch
<= 'Z')) ||
1343 ((ch
>= '0') && (ch
<= '9')) ||
1346 m_selStartY
= m_cursorY
;
1347 m_selEndY
= m_cursorY
;
1350 ch
= line
[(size_t) (p
-1)];
1351 while (((ch
>= 'a') && (ch
<= 'z')) ||
1352 ((ch
>= 'A') && (ch
<= 'Z')) ||
1353 ((ch
>= '0') && (ch
<= '9')) ||
1358 ch
= line
[(size_t) (p
-1)];
1364 if (p
< (int)line
.Len())
1366 ch
= line
[(size_t) (p
)];
1367 while (((ch
>= 'a') && (ch
<= 'z')) ||
1368 ((ch
>= 'A') && (ch
<= 'Z')) ||
1369 ((ch
>= '0') && (ch
<= '9')) ||
1372 if (p
>= (int)line
.Len()) break;
1374 ch
= line
[(size_t) (p
)];
1378 RefreshLine( m_cursorY
);
1382 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1385 size_t len
= line
.Len();
1386 for (size_t p
= pos
; p
< len
; p
++)
1388 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1392 for (size_t q
= p
; q
< len
; q
++)
1393 ret
.Append( line
[q
] );
1400 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[(size_t) (p
+1)] == '/'))
1402 for (size_t q
= p
; q
< len
; q
++)
1403 ret
.Append( line
[q
] );
1411 ret
.Append( line
[p
] );
1412 for (size_t q
= p
+1; q
< len
; q
++)
1414 ret
.Append( line
[q
] );
1415 if ((line
[q
] == '"') && ((line
[(size_t) (q
-1)] != '\\') || (q
>= 2 && line
[(size_t) (q
-2)] == '\\')))
1422 if (line
[p
] == '\'')
1424 ret
.Append( line
[p
] );
1425 for (size_t q
= p
+1; q
< len
; q
++)
1427 ret
.Append( line
[q
] );
1428 if ((line
[q
] == '\'') && ((line
[(size_t) (q
-1)] != '\\') || (q
>= 2 && line
[(size_t) (q
-2)] == '\\')))
1435 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1436 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1440 ret
.Append( line
[p
] );
1441 for (size_t q
= p
+1; q
< len
; q
++)
1443 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1444 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1445 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1448 ret
.Append( line
[q
] );
1465 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1470 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1473 size_t len
= origin
.Len();
1474 dc
.SetTextForeground( colour
);
1477 while (toDraw
[pos
] == wxT(' '))
1480 if (pos
== len
) return;
1486 current
+= toDraw
[pos
];
1488 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1490 current
+= toDraw
[pos
];
1495 wxString tmp
= origin
.Left( start
);
1496 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1499 dc
.DrawText( current
, xx
, yy
);
1503 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1505 int selStartY
= m_selStartY
;
1506 int selEndY
= m_selEndY
;
1507 int selStartX
= m_selStartX
;
1508 int selEndX
= m_selEndX
;
1510 if ((selStartY
> selEndY
) ||
1511 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1513 int tmp
= selStartX
;
1514 selStartX
= selEndX
;
1517 selStartY
= selEndY
;
1521 wxString
line( line2
);
1522 if (HasFlag(wxTE_PASSWORD
))
1524 size_t len
= line
.Len();
1525 line
= wxString( wxT('*'), len
);
1528 wxString
keyword( ' ', line
.Len() );
1529 wxString
define( ' ', line
.Len() );
1530 wxString
variable( ' ', line
.Len() );
1531 wxString
comment( ' ', line
.Len() );
1532 wxString
my_string( ' ', line
.Len() );
1533 wxString
selection( ' ', line
.Len() );
1535 if (m_lang
!= wxSOURCE_LANG_NONE
)
1537 if (lineNum
== m_bracketY
)
1539 wxString
red( ' ', line
.Len() );
1540 if (m_bracketX
< (int)line
.Len())
1542 red
.SetChar( m_bracketX
, line
[(size_t) (m_bracketX
)] );
1543 line
.SetChar( m_bracketX
, ' ' );
1544 dc
.SetTextForeground( *wxRED
);
1545 dc
.DrawText( red
, x
, y
);
1546 dc
.SetTextForeground( *wxBLACK
);
1551 wxString
token( GetNextToken( line
, pos
) );
1552 while (!token
.IsNull())
1554 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1556 size_t end_pos
= pos
+ token
.Len();
1557 for (size_t i
= pos
; i
< end_pos
; i
++)
1559 keyword
[i
] = line
[i
];
1563 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1565 size_t end_pos
= pos
+ token
.Len();
1566 for (size_t i
= pos
; i
< end_pos
; i
++)
1568 define
[i
] = line
[i
];
1572 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1573 ((token
.Len() > 2) && (token
[(size_t) (0)] == 'w') && (token
[(size_t) (1)] == 'x')))
1575 size_t end_pos
= pos
+ token
.Len();
1576 for (size_t i
= pos
; i
< end_pos
; i
++)
1578 variable
[i
] = line
[i
];
1582 if ((token
.Len() >= 2) && (token
[(size_t) (0)] == '/') && (token
[(size_t) (1)] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1584 size_t end_pos
= pos
+ token
.Len();
1585 for (size_t i
= pos
; i
< end_pos
; i
++)
1587 comment
[i
] = line
[i
];
1591 if ((token
[(size_t) (0)] == '#') &&
1592 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1594 size_t end_pos
= pos
+ token
.Len();
1595 for (size_t i
= pos
; i
< end_pos
; i
++)
1597 comment
[i
] = line
[i
];
1601 if ((token
[(size_t) (0)] == '"') || (token
[(size_t) (0)] == '\''))
1603 size_t end_pos
= pos
+ token
.Len();
1604 for (size_t i
= pos
; i
< end_pos
; i
++)
1606 my_string
[i
] = line
[i
];
1611 token
= GetNextToken( line
, pos
);
1615 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1617 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1618 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1619 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1620 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1621 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1622 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1623 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1627 if (selStartY
== selEndY
)
1629 // int xx = selStartX*m_charWidth;
1630 int xx
= PosToPixel( lineNum
, selStartX
);
1631 // int ww = (selEndX-selStartX)*m_charWidth;
1632 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1633 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1635 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1637 selection
[i
] = line
[i
];
1641 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1643 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1645 for (size_t i
= 0; i
< line
.Len(); i
++)
1647 selection
[i
] = line
[i
];
1651 if (lineNum
== selStartY
)
1653 // int xx = selStartX*m_charWidth;
1654 int xx
= PosToPixel( lineNum
, selStartX
);
1655 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1657 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1659 selection
[i
] = line
[i
];
1663 if (lineNum
== selEndY
)
1665 // int ww = selEndX*m_charWidth;
1666 int ww
= PosToPixel( lineNum
, selEndX
);
1667 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1669 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1671 selection
[i
] = line
[i
];
1676 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1677 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1678 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1679 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1680 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1681 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1682 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1685 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1689 if (m_lines
.GetCount() == 0) return;
1693 dc
.SetFont( m_sourceFont
);
1696 GetViewStart( NULL
, &scroll_y
);
1698 // We have a inner border of two pixels
1699 // around the text, so scroll units do
1700 // not correspond to lines.
1701 if (scroll_y
> 0) scroll_y
--;
1705 GetClientSize( &size_x
, &size_y
);
1707 dc
.SetPen( *wxTRANSPARENT_PEN
);
1708 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1709 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+2 );
1710 for (int i
= scroll_y
; i
< upper
; i
++)
1713 int y
= i
*m_lineHeight
+2;
1715 int h
= m_lineHeight
;
1716 CalcScrolledPosition( x
,y
,&x
,&y
);
1717 if (IsExposed(x
,y
,w
,h
))
1718 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1721 if (m_editable
&& (FindFocus() == this))
1723 ///dc.SetBrush( *wxRED_BRUSH );
1724 dc
.SetBrush( *wxBLACK_BRUSH
);
1725 // int xx = m_cursorX*m_charWidth;
1726 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1727 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1731 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1733 if (m_lines
.GetCount() == 0) return;
1736 #if 0 // there is no middle button on iPAQs
1737 if (event
.MiddleDown())
1744 if (event
.LeftDClick())
1750 if (event
.LeftDown())
1758 m_capturing
= FALSE
;
1762 if (event
.LeftDown() ||
1763 (event
.LeftIsDown() && m_capturing
))
1765 int x
= event
.GetX();
1766 int y
= event
.GetY();
1767 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1769 // x /= m_charWidth;
1770 x
= PixelToPos( y
, x
);
1772 wxMin( 1000, wxMax( 0, x
) ),
1773 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1774 event
.ShiftDown() || !event
.LeftDown() );
1778 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1780 if (m_lines
.GetCount() == 0) return;
1782 if (!m_editable
) return;
1786 GetClientSize( &size_x
, &size_y
);
1787 size_x
/= m_charWidth
;
1788 size_y
/= m_lineHeight
;
1791 if (event
.ShiftDown())
1793 switch (event
.GetKeyCode())
1795 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1796 case '8': event
.m_keyCode
= WXK_UP
; break;
1797 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1798 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1799 case '9': event
.m_keyCode
= WXK_PRIOR
; break;
1800 case '3': event
.m_keyCode
= WXK_NEXT
; break;
1801 case '7': event
.m_keyCode
= WXK_HOME
; break;
1802 case '1': event
.m_keyCode
= WXK_END
; break;
1803 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1807 switch (event
.GetKeyCode())
1811 if (m_ignoreInput
) return;
1813 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1814 m_ignoreInput
= TRUE
;
1819 if (m_ignoreInput
) return;
1820 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1821 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1822 m_ignoreInput
= TRUE
;
1827 if (m_ignoreInput
) return;
1830 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1835 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1837 m_ignoreInput
= TRUE
;
1842 if (m_ignoreInput
) return;
1843 if (m_cursorX
< 1000)
1844 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1845 m_ignoreInput
= TRUE
;
1850 if (event
.ControlDown())
1851 MoveCursor( 0, 0, event
.ShiftDown() );
1853 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
1858 if (event
.ControlDown())
1859 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
1861 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
1866 if (m_ignoreInput
) return;
1867 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
1868 m_ignoreInput
= TRUE
;
1873 if (m_ignoreInput
) return;
1874 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
1875 m_ignoreInput
= TRUE
;
1880 if (event
.ShiftDown())
1882 else if (event
.ControlDown())
1885 m_overwrite
= !m_overwrite
;
1890 if (m_windowStyle
& wxPROCESS_ENTER
)
1892 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1893 event
.SetEventObject(this);
1894 event
.SetString(GetValue());
1895 if (GetEventHandler()->ProcessEvent(event
)) return;
1913 bool save_overwrite
= m_overwrite
;
1914 m_overwrite
= FALSE
;
1915 int i
= 4-(m_cursorX
% 4);
1917 for (int c
= 0; c
< i
; c
++)
1919 m_overwrite
= save_overwrite
;
1940 if ( (event
.GetKeyCode() >= 'a') &&
1941 (event
.GetKeyCode() <= 'z') &&
1949 if ( (event
.GetKeyCode() >= 32) &&
1950 (event
.GetKeyCode() <= 255) &&
1951 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
1955 DoChar( (char) event
.GetKeyCode() );
1964 void wxTextCtrl::OnInternalIdle()
1966 m_ignoreInput
= FALSE
;
1968 if (m_lang
!= wxSOURCE_LANG_NONE
)
1969 SearchForBrackets();
1972 void wxTextCtrl::Indent()
1974 int startY
= m_cursorY
;
1975 int endY
= m_cursorY
;
1978 startY
= m_selStartY
;
1988 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
1990 for (int i
= startY
; i
<= endY
; i
++)
1992 m_lines
[i
].m_text
.insert( 0u, wxT(" ") );
1997 void wxTextCtrl::Unindent()
1999 int startY
= m_cursorY
;
2000 int endY
= m_cursorY
;
2003 startY
= m_selStartY
;
2013 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2015 for (int i
= startY
; i
<= endY
; i
++)
2017 for (int n
= 0; n
< 4; n
++)
2019 if (m_lines
[i
].m_text
[0u] == wxT(' '))
2020 m_lines
[i
].m_text
.erase(0u,1u);
2025 bool wxTextCtrl::HasSelection()
2027 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2030 void wxTextCtrl::ClearSelection()
2038 void wxTextCtrl::RefreshLine( int n
)
2040 int y
= n
*m_lineHeight
;
2042 CalcScrolledPosition( x
, y
, &x
, &y
);
2043 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2044 Refresh( TRUE
, &rect
);
2047 void wxTextCtrl::RefreshDown( int n
)
2051 GetClientSize( &size_x
, &size_y
);
2055 GetViewStart( &view_x
, &view_y
);
2063 int y
= n
*m_lineHeight
;
2065 CalcScrolledPosition( x
, y
, &x
, &y
);
2067 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2068 Refresh( TRUE
, &rect
);
2072 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2074 if (!m_editable
) return;
2076 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
2078 if (new_x
> (int) (m_lines
[new_y
].m_text
.Len()))
2079 new_x
= m_lines
[new_y
].m_text
.Len();
2082 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2084 bool no_cursor_refresh
= FALSE
;
2085 bool has_selection
= HasSelection();
2090 bool erase_background
= TRUE
;
2094 m_selStartX
= m_cursorX
;
2095 m_selStartY
= m_cursorY
;
2099 if (new_y
> m_selStartY
)
2101 y
= m_selStartY
*m_lineHeight
;
2102 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2104 else if (new_y
== m_selStartY
)
2106 x
= PosToPixel( new_y
, m_selStartX
);
2107 w
= PosToPixel( new_y
, new_x
) - x
;
2111 w
= -w
+ 2; // +2 for the cursor
2113 y
= m_selStartY
*m_lineHeight
;
2118 y
= new_y
*m_lineHeight
;
2119 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2122 no_cursor_refresh
= TRUE
;
2128 if (new_y
== m_selEndY
)
2130 y
= new_y
*m_lineHeight
;
2132 if (m_selEndX
> new_x
)
2134 // x = new_x*m_charWidth;
2135 x
= PosToPixel( new_y
, new_x
);
2136 // w = (m_selEndX-new_x)*m_charWidth;
2137 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2141 // x = m_selEndX*m_charWidth;
2142 x
= PosToPixel( new_y
, m_selEndX
);
2143 // w = (-m_selEndX+new_x)*m_charWidth;
2144 w
= PosToPixel( new_y
, new_x
) - x
;
2151 if (new_y
> m_selEndY
)
2153 y
= m_selEndY
*m_lineHeight
;
2154 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2156 erase_background
= ((m_selEndY
< m_selStartY
) ||
2157 ((m_selEndY
== m_selStartY
) && (m_selEndX
< m_selStartX
)));
2161 y
= new_y
*m_lineHeight
;
2162 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2164 erase_background
= ((m_selEndY
> m_selStartY
) ||
2165 ((m_selEndY
== m_selStartY
) && (m_selEndX
> m_selStartX
)));
2167 no_cursor_refresh
= TRUE
;
2176 CalcScrolledPosition( x
, y
, &x
, &y
);
2177 wxRect
rect( x
+2, y
+2, w
, h
);
2178 Refresh( erase_background
, &rect
);
2184 int ry1
= m_selEndY
;
2185 int ry2
= m_selStartY
;
2199 int y
= ry1
*m_lineHeight
;
2200 CalcScrolledPosition( x
, y
, &x
, &y
);
2201 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2203 Refresh( TRUE
, &rect
);
2208 printf( "startx %d starty %d endx %d endy %d\n",
2209 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2211 printf( "has %d\n", (int)HasSelection() );
2214 if (!no_cursor_refresh
)
2216 // int x = m_cursorX*m_charWidth;
2217 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2218 int y
= m_cursorY
*m_lineHeight
;
2219 CalcScrolledPosition( x
, y
, &x
, &y
);
2220 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2225 Refresh( TRUE
, &rect
);
2227 if (FindFocus() == this)
2229 wxClientDC
dc(this);
2231 dc
.SetPen( *wxTRANSPARENT_PEN
);
2232 //dc.SetBrush( *wxRED_BRUSH );
2233 dc
.SetBrush( *wxBLACK_BRUSH
);
2234 // int xx = m_cursorX*m_charWidth;
2235 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2236 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2242 GetClientSize( &size_x
, &size_y
);
2243 size_x
/= m_charWidth
;
2244 size_y
/= m_lineHeight
;
2248 GetViewStart( &view_x
, &view_y
);
2252 int sy
= m_cursorY
- (size_y
/2);
2258 if (m_cursorY
< view_y
)
2259 Scroll( -1, m_cursorY
);
2260 else if (m_cursorY
> view_y
+size_y
-1)
2261 Scroll( -1, m_cursorY
-size_y
+1 );
2264 //int xx = m_cursorX;
2265 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2269 else if (xx
> view_x
+size_x
-1)
2270 Scroll( xx
-size_x
+1, -1 );
2273 void wxTextCtrl::MyAdjustScrollbars()
2278 int y_range
= m_lines
.GetCount();
2281 GetClientSize( NULL
, &height
);
2283 if (height
>= (int)m_lines
.GetCount() *m_lineHeight
)
2288 GetViewStart( &view_x
, &view_y
);
2290 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2293 //-----------------------------------------------------------------------------
2294 // clipboard handlers
2295 //-----------------------------------------------------------------------------
2297 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2302 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2307 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2312 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2317 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2322 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2324 event
.Enable( CanCut() );
2327 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2329 event
.Enable( CanCopy() );
2332 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2334 event
.Enable( CanPaste() );
2337 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2339 event
.Enable( CanUndo() );
2342 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2344 event
.Enable( CanRedo() );
2347 wxSize
wxTextCtrl::DoGetBestSize() const
2351 wxSize
ret(80, m_lineHeight
+ 4);
2353 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2356 if (HasFlag(wxBORDER_SIMPLE
))
2363 return wxSize(80, 60);
2367 // ----------------------------------------------------------------------------
2369 // ----------------------------------------------------------------------------
2371 void wxTextCtrl::Freeze()
2375 void wxTextCtrl::Thaw()
2379 void wxTextCtrl::OnSetFocus( wxFocusEvent
& event
)
2381 // To hide or show caret, as appropriate
2385 void wxTextCtrl::OnKillFocus( wxFocusEvent
& event
)
2387 // To hide or show caret, as appropriate
2391 // ----------------------------------------------------------------------------
2392 // text control scrolling
2393 // ----------------------------------------------------------------------------
2395 bool wxTextCtrl::ScrollLines(int lines
)
2397 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2402 bool wxTextCtrl::ScrollPages(int pages
)
2404 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");