1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // for compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
20 #include "wx/dcclient.h"
21 #include "wx/settings.h"
24 #include "wx/clipbrd.h"
25 #include "wx/tokenzr.h"
27 #include "wx/univ/inphand.h"
28 #include "wx/univ/renderer.h"
29 #include "wx/univ/colschem.h"
30 #include "wx/univ/theme.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type
, int y1
, int y2
, wxTextCtrl
*owner
)
43 m_cursorX
= m_owner
->GetCursorX();
44 m_cursorY
= m_owner
->GetCursorY();
46 if (m_type
== wxSOURCE_UNDO_LINE
)
48 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
50 if (m_type
== wxSOURCE_UNDO_ENTER
)
52 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
54 if (m_type
== wxSOURCE_UNDO_BACK
)
56 for (int i
= m_y1
; i
< m_y2
+2; i
++)
58 if (i
>= (int)m_owner
->m_lines
.GetCount())
59 m_lines
.Add( wxEmptyString
);
61 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
64 if (m_type
== wxSOURCE_UNDO_DELETE
)
66 for (int i
= m_y1
; i
< m_y2
+1; i
++)
68 m_lines
.Add( m_owner
->m_lines
[i
].m_text
);
71 if (m_type
== wxSOURCE_UNDO_PASTE
)
73 m_text
= m_owner
->m_lines
[m_y1
].m_text
;
77 void wxSourceUndoStep::Undo()
79 if (m_type
== wxSOURCE_UNDO_LINE
)
81 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
82 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
83 m_owner
->RefreshLine( m_y1
);
85 if (m_type
== wxSOURCE_UNDO_ENTER
)
87 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
88 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
89 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
90 m_owner
->RefreshDown( m_y1
);
92 if (m_type
== wxSOURCE_UNDO_BACK
)
94 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
95 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[1] ), m_y1
+1 );
96 m_owner
->MyAdjustScrollbars();
97 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
98 m_owner
->RefreshDown( m_y1
);
100 if (m_type
== wxSOURCE_UNDO_DELETE
)
102 m_owner
->m_lines
[m_y1
].m_text
= m_lines
[0];
103 for (int i
= 1; i
< (int)m_lines
.GetCount(); i
++)
104 m_owner
->m_lines
.Insert( new wxSourceLine( m_lines
[i
] ), m_y1
+i
);
105 m_owner
->MyAdjustScrollbars();
106 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
107 m_owner
->RefreshDown( m_y1
);
109 if (m_type
== wxSOURCE_UNDO_PASTE
)
111 m_owner
->m_lines
[m_y1
].m_text
= m_text
;
112 for (int i
= 0; i
< m_y2
-m_y1
; i
++)
113 m_owner
->m_lines
.RemoveAt( m_y1
+1 );
114 m_owner
->MyAdjustScrollbars();
115 m_owner
->MoveCursor( m_cursorX
, m_cursorY
);
116 m_owner
->RefreshDown( m_y1
);
118 if (m_type
== wxSOURCE_UNDO_INSERT_LINE
)
120 m_owner
->m_lines
.RemoveAt( m_y1
);
121 m_owner
->MyAdjustScrollbars();
122 m_owner
->MoveCursor( 0, m_y1
);
123 m_owner
->RefreshDown( m_y1
);
127 #include "wx/arrimpl.cpp"
128 WX_DEFINE_OBJARRAY(wxSourceLineArray
);
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
135 EVT_PAINT(wxTextCtrl::OnPaint
)
136 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
137 EVT_CHAR(wxTextCtrl::OnChar
)
138 EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse
)
139 EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus
)
140 EVT_SET_FOCUS(wxTextCtrl::OnSetFocus
)
142 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
143 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
144 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
145 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
146 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
148 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
149 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
150 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
151 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
152 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
155 void wxTextCtrl::Init()
160 m_lang
= wxSOURCE_LANG_NONE
;
173 m_ignoreInput
= false;
177 m_keywordColour
= wxColour( 10, 140, 10 );
179 m_defineColour
= *wxRED
;
181 m_variableColour
= wxColour( 50, 120, 150 );
183 m_commentColour
= wxColour( 130, 130, 130 );
185 m_stringColour
= wxColour( 10, 140, 10 );
188 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
190 const wxString
&value
,
194 const wxValidator
& validator
,
195 const wxString
&name
)
196 : wxScrollHelper(this)
200 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
203 wxTextCtrl::~wxTextCtrl()
205 WX_CLEAR_LIST(wxList
, m_undos
);
208 bool wxTextCtrl::Create( wxWindow
*parent
,
210 const wxString
&value
,
214 const wxValidator
& validator
,
215 const wxString
&name
)
217 if ((style
& wxBORDER_MASK
) == 0)
218 style
|= wxBORDER_SUNKEN
;
220 if ((style
& wxTE_MULTILINE
) != 0)
221 style
|= wxALWAYS_SHOW_SB
;
223 wxTextCtrlBase::Create( parent
, id
, pos
/* wxDefaultPosition */, size
,
224 style
| wxVSCROLL
| wxHSCROLL
);
226 SetBackgroundColour( *wxWHITE
);
228 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
230 m_editable
= ((m_windowStyle
& wxTE_READONLY
) == 0);
232 if (HasFlag(wxTE_PASSWORD
))
233 m_sourceFont
= wxFont( 12, wxMODERN
, wxNORMAL
, wxNORMAL
);
235 m_sourceFont
= GetFont();
238 dc
.SetFont( m_sourceFont
);
239 m_lineHeight
= dc
.GetCharHeight();
240 m_charWidth
= dc
.GetCharWidth();
244 wxSize
size_best( DoGetBestSize() );
245 wxSize
new_size( size
);
246 if (new_size
.x
== -1)
247 new_size
.x
= size_best
.x
;
248 if (new_size
.y
== -1)
249 new_size
.y
= size_best
.y
;
250 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
251 SetSize( new_size
.x
, new_size
.y
);
253 // We create an input handler since it might be useful
254 CreateInputHandler(wxINP_HANDLER_TEXTCTRL
);
256 MyAdjustScrollbars();
261 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
265 wxString
wxTextCtrl::GetValue() const
268 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
270 ret
+= m_lines
[i
].m_text
;
271 if (i
+1 < m_lines
.GetCount())
278 void wxTextCtrl::DoSetValue(const wxString
& value
, int flags
)
282 wxString oldValue
= GetValue();
292 m_lines
.Add( new wxSourceLine( wxEmptyString
) );
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 // Don't need to refresh if the value hasn't changed
335 if ((GetWindowStyle() & wxTE_MULTILINE
) == 0)
337 if (value
== oldValue
)
341 MyAdjustScrollbars();
345 if ( flags
& SetValue_SendEvent
)
346 SendTextUpdatedEvent();
349 int wxTextCtrl::GetLineLength(long lineNo
) const
351 if (lineNo
>= (long)m_lines
.GetCount())
354 return m_lines
[lineNo
].m_text
.Len();
357 wxString
wxTextCtrl::GetLineText(long lineNo
) const
359 if (lineNo
>= (long)m_lines
.GetCount())
360 return wxEmptyString
;
362 return m_lines
[lineNo
].m_text
;
365 int wxTextCtrl::GetNumberOfLines() const
367 return m_lines
.GetCount();
370 bool wxTextCtrl::IsModified() const
375 bool wxTextCtrl::IsEditable() const
380 void wxTextCtrl::GetSelection(long* from
, long* to
) const
382 if (m_selStartX
== -1 || m_selStartY
== -1 ||
383 m_selEndX
== -1 || m_selEndY
== -1)
385 *from
= GetInsertionPoint();
386 *to
= GetInsertionPoint();
390 *from
= XYToPosition(m_selStartX
, m_selStartY
);
391 *to
= XYToPosition(m_selEndX
, m_selEndY
);
395 void wxTextCtrl::Clear()
403 m_lines
.Add( new wxSourceLine( wxEmptyString
) );
405 SetScrollbars( m_charWidth
, m_lineHeight
, 0, 0, 0, 0 );
407 WX_CLEAR_LIST(wxList
, m_undos
);
410 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
414 void wxTextCtrl::Remove(long from
, long to
)
418 void wxTextCtrl::DiscardEdits()
424 void wxTextCtrl::SetMaxLength(unsigned long len
)
428 int wxTextCtrl::PosToPixel( int line
, int pos
)
430 // TODO add support for Tabs
432 if (line
>= (int)m_lines
.GetCount()) return 0;
433 if (pos
< 0) return 0;
435 wxString text
= m_lines
[line
].m_text
;
437 if (text
.empty()) return 0;
439 if (pos
< (int)text
.Len())
440 text
.Remove( pos
, text
.Len()-pos
);
444 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
449 int wxTextCtrl::PixelToPos( int line
, int pixel
)
451 if (pixel
< 2) return 0;
453 if (line
>= (int)m_lines
.GetCount()) return 0;
455 wxString text
= m_lines
[line
].m_text
;
458 int res
= text
.Len();
461 GetTextExtent( text
, &w
, NULL
, NULL
, NULL
);
467 text
.Remove( res
,1 );
473 void wxTextCtrl::SetLanguage( wxSourceLanguage lang
)
480 void wxTextCtrl::WriteText(const wxString
& text2
)
482 if (text2
.empty()) return;
486 wxString
text( text2
);
489 while ( (pos
= text
.Find('\n')) != -1 )
491 lines
.Add( text
.Left( pos
) );
492 text
.Remove( 0, pos
+1 );
495 int count
= (int)lines
.GetCount();
497 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
498 wxString
tmp2( tmp1
);
499 int len
= (int)tmp1
.Len();
504 for (int i
= 0; i
< m_cursorX
-len
; i
++)
506 m_lines
[m_cursorY
].m_text
.Append( tmp
);
511 tmp1
.Remove( m_cursorX
);
512 tmp2
.Remove( 0, m_cursorX
);
513 tmp1
.Append( lines
[0] );
517 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
520 m_lines
[m_cursorY
].m_text
= tmp1
;
521 RefreshLine( m_cursorY
);
525 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
527 m_lines
[m_cursorY
].m_text
= tmp1
;
529 for (i
= 1; i
< count
; i
++)
530 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
531 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
533 MyAdjustScrollbars();
534 RefreshDown( m_cursorY
);
538 void wxTextCtrl::AppendText(const wxString
& text2
)
540 if (text2
.empty()) return;
544 wxString
text( text2
);
547 while ( (pos
= text
.Find('\n')) != -1 )
549 lines
.Add( text
.Left( pos
) );
550 text
.Remove( 0, pos
+1 );
553 int count
= (int)lines
.GetCount();
555 size_t y
= m_lines
.GetCount()-1;
557 wxString
tmp( m_lines
[y
].m_text
);
558 tmp
.Append( lines
[0] );
562 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, y
, y
, this ) );
564 m_lines
[y
].m_text
= tmp
;
569 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, y
, y
+count
-1, this ) );
571 m_lines
[y
].m_text
= tmp
;
573 for (i
= 1; i
< count
; i
++)
574 m_lines
.Insert( new wxSourceLine( lines
[i
] ), y
+i
);
576 MyAdjustScrollbars();
581 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
586 long wxTextCtrl::XYToPosition(long x
, long y
) const
590 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
594 // Add one for the end-of-line character
595 ret
+= m_lines
[i
].m_text
.Len() + 1;
599 if ((size_t)x
< (m_lines
[i
].m_text
.Len()+1))
602 return (ret
+ m_lines
[i
].m_text
.Len() + 1);
608 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
610 if (m_lines
.GetCount() == 0)
621 for (size_t i
= 0; i
< m_lines
.GetCount(); i
++)
623 //pos -= m_lines[i].m_text.Len();
626 // Add one for the end-of-line character. (In Windows,
627 // there are _two_ positions for each end of line.)
628 if (pos
<= ((int)m_lines
[i
].m_text
.Len()))
635 pos
-= (m_lines
[i
].m_text
.Len() + 1);
640 //xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len();
648 void wxTextCtrl::ShowPosition(long pos
)
652 void wxTextCtrl::Copy()
654 if (!HasSelection()) return;
658 int selStartY
= m_selStartY
;
659 int selEndY
= m_selEndY
;
660 int selStartX
= m_selStartX
;
661 int selEndX
= m_selEndX
;
663 if ((selStartY
> selEndY
) ||
664 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
674 if (selStartY
== selEndY
)
676 sel
= m_lines
[selStartY
].m_text
;
678 if (selStartX
>= (int)sel
.Len()) return;
679 if (selEndX
> (int)sel
.Len())
682 sel
.Remove( selEndX
, sel
.Len()-selEndX
);
683 sel
.Remove( 0, selStartX
);
687 wxString
tmp( m_lines
[selStartY
].m_text
);
689 if (selStartX
< (int)tmp
.Len())
691 tmp
.Remove( 0, selStartX
);
693 sel
.Append( wxT("\n") );
695 for (int i
= selStartY
+1; i
< selEndY
; i
++)
697 sel
.Append( m_lines
[i
].m_text
);
698 sel
.Append( wxT("\n") );
700 tmp
= m_lines
[selEndY
].m_text
;
701 if (selEndX
> (int)tmp
.Len())
705 tmp
.Remove( selEndX
, tmp
.Len()-selEndX
);
710 if (wxTheClipboard
->Open())
712 wxTheClipboard
->SetData( new wxTextDataObject( sel
) );
713 wxTheClipboard
->Close();
717 void wxTextCtrl::Cut()
724 void wxTextCtrl::Paste()
728 if (!wxTheClipboard
->Open()) return;
730 if (!wxTheClipboard
->IsSupported( wxDF_TEXT
))
732 wxTheClipboard
->Close();
737 wxTextDataObject data
;
739 bool ret
= wxTheClipboard
->GetData( data
);
741 wxTheClipboard
->Close();
747 wxString
text( data
.GetText() );
750 while ( (pos
= text
.Find('\n')) != -1 )
752 lines
.Add( text
.Left( pos
) );
753 text
.Remove( 0, pos
+1 );
756 int count
= (int)lines
.GetCount();
758 wxString
tmp1( m_lines
[m_cursorY
].m_text
);
759 wxString
tmp2( tmp1
);
760 int len
= (int)tmp1
.Len();
765 for (int i
= 0; i
< m_cursorX
-len
; i
++)
767 m_lines
[m_cursorY
].m_text
.Append( tmp
);
772 tmp1
.Remove( m_cursorX
);
773 tmp2
.Remove( 0, m_cursorX
);
774 tmp1
.Append( lines
[0] );
778 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
781 m_lines
[m_cursorY
].m_text
= tmp1
;
782 RefreshLine( m_cursorY
);
786 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE
, m_cursorY
, m_cursorY
+count
-1, this ) );
788 m_lines
[m_cursorY
].m_text
= tmp1
;
790 for (i
= 1; i
< count
; i
++)
791 m_lines
.Insert( new wxSourceLine( lines
[i
] ), m_cursorY
+i
);
792 m_lines
[m_cursorY
+i
-1].m_text
.Append( tmp2
);
794 MyAdjustScrollbars();
795 RefreshDown( m_cursorY
);
799 void wxTextCtrl::Undo()
801 if (m_undos
.GetCount() == 0) return;
803 wxList::compatibility_iterator node
= m_undos
.Item( m_undos
.GetCount()-1 );
804 wxSourceUndoStep
*undo
= (wxSourceUndoStep
*) node
->GetData();
809 m_undos
.Erase( node
);
814 void wxTextCtrl::SetInsertionPoint(long pos
)
818 PositionToXY(pos
, & x
, & y
);
821 // TODO: scroll to this position if necessary
825 void wxTextCtrl::SetInsertionPointEnd()
827 SetInsertionPoint(GetLastPosition());
830 long wxTextCtrl::GetInsertionPoint() const
832 return XYToPosition( m_cursorX
, m_cursorY
);
835 wxTextPos
wxTextCtrl::GetLastPosition() const
837 size_t lineCount
= m_lines
.GetCount() - 1;
838 // It's the length of the line, not the length - 1,
839 // because there's a position after the last character.
840 return XYToPosition( m_lines
[lineCount
].m_text
.Len(), lineCount
);
843 void wxTextCtrl::SetSelection(long from
, long to
)
847 void wxTextCtrl::SetEditable(bool editable
)
849 m_editable
= editable
;
852 bool wxTextCtrl::Enable( bool enable
)
857 bool wxTextCtrl::SetFont(const wxFont
& font
)
859 wxTextCtrlBase::SetFont( font
);
864 dc
.SetFont( m_sourceFont
);
865 m_lineHeight
= dc
.GetCharHeight();
866 m_charWidth
= dc
.GetCharWidth();
868 // TODO: recalc longest lines
870 MyAdjustScrollbars();
875 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
877 return wxWindow::SetForegroundColour( colour
);
880 bool wxTextCtrl::SetBackgroundColour(const wxColour
& colour
)
882 return wxWindow::SetBackgroundColour( colour
);
885 //-----------------------------------------------------------------------------
886 // private code and handlers
887 //-----------------------------------------------------------------------------
889 void wxTextCtrl::SearchForBrackets()
891 int oldBracketY
= m_bracketY
;
892 int oldBracketX
= m_bracketX
;
894 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()) return;
896 wxString current
= m_lines
[m_cursorY
].m_text
;
898 // reverse search first
903 bracket
= current
[(size_t) (m_cursorX
-1)];
905 if (bracket
== ')' || bracket
== ']' || bracket
== '}')
907 char antibracket
= '(';
908 if (bracket
== ']') antibracket
= '[';
909 if (bracket
== '}') antibracket
= '{';
913 int endY
= m_cursorY
-60;
914 if (endY
< 0) endY
= 0;
915 for (int y
= m_cursorY
; y
>= endY
; y
--)
917 current
= m_lines
[y
].m_text
;
919 current
.erase(m_cursorX
-1,current
.Len()-m_cursorX
+1);
921 for (int n
= current
.Len()-1; n
>= 0; n
--)
924 if (current
[(size_t) (n
)] == '\'')
926 for (int m
= n
-1; m
>= 0; m
--)
928 if (current
[(size_t) (m
)] == '\'')
930 if (m
== 0 || current
[(size_t) (m
-1)] != '\\')
939 if (current
[(size_t) (n
)] == '\"')
941 for (int m
= n
-1; m
>= 0; m
--)
943 if (current
[(size_t) (m
)] == '\"')
945 if (m
== 0 || current
[(size_t) (m
-1)] != '\\')
953 if (current
[(size_t) (n
)] == antibracket
)
960 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
961 RefreshLine( oldBracketY
);
962 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
963 RefreshLine( m_bracketY
);
967 else if (current
[(size_t) (n
)] == bracket
)
978 if ((int)current
.Len() > m_cursorX
)
979 bracket
= current
[(size_t) (m_cursorX
)];
980 if (bracket
== '(' || bracket
== '[' || bracket
== '{')
982 char antibracket
= ')';
983 if (bracket
== '[') antibracket
= ']';
984 if (bracket
== '{') antibracket
= '}';
988 int endY
= m_cursorY
+60;
989 if (endY
> (int)(m_lines
.GetCount()-1)) endY
= m_lines
.GetCount()-1;
990 for (int y
= m_cursorY
; y
<= endY
; y
++)
992 current
= m_lines
[y
].m_text
;
997 for (int n
= start
; n
< (int)current
.Len(); n
++)
1000 if (current
[(size_t) (n
)] == '\'')
1002 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1004 if (current
[(size_t) (m
)] == '\'')
1006 if (m
== 0 || (current
[(size_t) (m
-1)] != '\\') || (m
>= 2 && current
[(size_t) (m
-2)] == '\\'))
1015 if (current
[(size_t) (n
)] == '\"')
1017 for (int m
= n
+1; m
< (int)current
.Len(); m
++)
1019 if (current
[(size_t) (m
)] == '\"')
1021 if (m
== 0 || (current
[(size_t) (m
-1)] != '\\') || (m
>= 2 && current
[(size_t) (m
-2)] == '\\'))
1029 if (current
[(size_t) (n
)] == antibracket
)
1036 if (oldBracketY
!= m_bracketY
&& oldBracketY
!= -1)
1037 RefreshLine( oldBracketY
);
1038 if (m_bracketY
!= oldBracketY
|| m_bracketX
!= oldBracketX
)
1039 RefreshLine( m_bracketY
);
1043 else if (current
[(size_t) (n
)] == bracket
)
1051 if (oldBracketY
!= -1)
1054 RefreshLine( oldBracketY
);
1058 void wxTextCtrl::Delete()
1060 if (!HasSelection()) return;
1064 int selStartY
= m_selStartY
;
1065 int selEndY
= m_selEndY
;
1066 int selStartX
= m_selStartX
;
1067 int selEndX
= m_selEndX
;
1069 if ((selStartY
> selEndY
) ||
1070 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1072 int tmp
= selStartX
;
1073 selStartX
= selEndX
;
1076 selStartY
= selEndY
;
1080 int len
= (int)m_lines
[selStartY
].m_text
.Len();
1082 if (selStartY
== selEndY
)
1084 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, selStartY
, selStartY
, this ) );
1086 wxString
tmp( m_lines
[selStartY
].m_text
);
1087 if (selStartX
< len
)
1091 tmp
.Remove( selStartX
, selEndX
-selStartX
);
1092 m_lines
[selStartY
].m_text
= tmp
;
1095 m_cursorX
= selStartX
;
1096 RefreshLine( selStartY
);
1100 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, selStartY
, selEndY
, this ) );
1102 if (selStartX
< len
)
1103 m_lines
[selStartY
].m_text
.Remove( selStartX
);
1105 for (int i
= 0; i
< selEndY
-selStartY
-1; i
++)
1106 m_lines
.RemoveAt( selStartY
+1 );
1108 if (selEndX
< (int)m_lines
[selStartY
+1].m_text
.Len())
1109 m_lines
[selStartY
+1].m_text
.Remove( 0, selEndX
);
1111 m_lines
[selStartY
+1].m_text
.Remove( 0 );
1113 m_lines
[selStartY
].m_text
.Append( m_lines
[selStartY
+1].m_text
);
1114 m_lines
.RemoveAt( selStartY
+1 );
1117 MoveCursor( selStartX
, selStartY
);
1118 MyAdjustScrollbars();
1120 RefreshDown( selStartY
);
1124 void wxTextCtrl::DeleteLine()
1126 if (HasSelection()) return;
1128 if (m_cursorY
< 0 || m_cursorY
>= (int)m_lines
.GetCount()-1) return; // TODO
1130 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1132 m_lines
.RemoveAt( m_cursorY
);
1134 if (m_cursorY
>= (int)m_lines
.GetCount()) m_cursorY
--;
1136 MyAdjustScrollbars();
1137 RefreshDown( m_cursorY
);
1140 void wxTextCtrl::DoChar( char c
)
1144 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1146 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1148 if (m_cursorX
>= (int)tmp
.Len())
1150 int len
= tmp
.Len();
1151 for (int i
= 0; i
< m_cursorX
- len
; i
++)
1158 tmp
.SetChar( m_cursorX
, c
);
1160 tmp
.insert( m_cursorX
, 1, c
);
1163 m_lines
[m_cursorY
].m_text
= tmp
;
1165 // if (tmp.Len() > m_longestLine)
1167 // m_longestLine = tmp.Len();
1168 // MyAdjustScrollbars();
1172 GetTextExtent( tmp
, &ww
, NULL
, NULL
, NULL
);
1174 if (ww
> m_longestLine
)
1177 MyAdjustScrollbars();
1182 int y
= m_cursorY
*m_lineHeight
;
1183 // int x = (m_cursorX-1)*m_charWidth;
1184 int x
= PosToPixel( m_cursorY
, m_cursorX
-1 );
1185 CalcScrolledPosition( x
, y
, &x
, &y
);
1186 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1187 Refresh( true, &rect
);
1188 // refresh whole line for syntax colour highlighting
1190 Refresh( false, &rect
);
1194 GetClientSize( &size_x
, &size_y
);
1195 size_x
/= m_charWidth
;
1199 GetViewStart( &view_x
, &view_y
);
1201 //int xx = m_cursorX;
1202 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
1206 else if (xx
> view_x
+size_x
-1)
1207 Scroll( xx
-size_x
+1, -1 );
1210 void wxTextCtrl::DoBack()
1216 if (m_cursorY
== 0) return;
1218 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK
, m_cursorY
-1, m_cursorY
, this ) );
1220 wxString
tmp1( m_lines
[m_cursorY
-1].m_text
);
1222 wxString
tmp2( m_lines
[m_cursorY
].m_text
);
1224 m_cursorX
= tmp1
.Len();
1226 tmp1
.Append( tmp2
);
1227 m_lines
[m_cursorY
].m_text
= tmp1
;
1228 m_lines
.RemoveAt( m_cursorY
+1 );
1230 MyAdjustScrollbars();
1231 RefreshDown( m_cursorY
-1 );
1235 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1237 if (m_cursorX
<= (int)m_lines
[m_cursorY
].m_text
.Len())
1238 m_lines
[m_cursorY
].m_text
.Remove( m_cursorX
-1, 1 );
1241 int y
= m_cursorY
*m_lineHeight
;
1242 // int x = m_cursorX*m_charWidth;
1243 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1244 CalcScrolledPosition( x
, y
, &x
, &y
);
1245 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1246 Refresh( true, &rect
);
1247 // refresh whole line for syntax colour highlighting
1249 Refresh( false, &rect
);
1253 void wxTextCtrl::DoDelete()
1257 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1259 int len
= (int)tmp
.Len();
1260 if (m_cursorX
>= len
)
1262 if (m_cursorY
== (int)m_lines
.GetCount()-1) return;
1264 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE
, m_cursorY
, m_cursorY
+1, this ) );
1266 for (int i
= 0; i
< (m_cursorX
-len
); i
++)
1269 tmp
+= m_lines
[m_cursorY
+1].m_text
;
1271 m_lines
[m_cursorY
] = tmp
;
1272 m_lines
.RemoveAt( m_cursorY
+1 );
1274 MyAdjustScrollbars();
1275 RefreshDown( m_cursorY
);
1279 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, m_cursorY
, m_cursorY
, this ) );
1281 tmp
.Remove( m_cursorX
, 1 );
1282 m_lines
[m_cursorY
].m_text
= tmp
;
1284 int y
= m_cursorY
*m_lineHeight
;
1285 // int x = m_cursorX*m_charWidth;
1286 int x
= PosToPixel( m_cursorY
, m_cursorX
);
1287 CalcScrolledPosition( x
, y
, &x
, &y
);
1288 wxRect
rect( x
+2, y
+2, 10000, m_lineHeight
);
1289 Refresh( true, &rect
);
1290 // refresh whole line for syntax colour highlighting
1292 Refresh( false, &rect
);
1296 void wxTextCtrl::DoReturn()
1300 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER
, m_cursorY
, m_cursorY
, this ) );
1302 wxString
tmp( m_lines
[m_cursorY
].m_text
);
1303 size_t indent
= tmp
.find_first_not_of( ' ' );
1304 if (indent
== wxSTRING_MAXLEN
) indent
= 0;
1306 if (m_cursorX
>= (int)tmp
.Len())
1308 int cursorX
= indent
;
1309 int cursorY
= m_cursorY
+ 1;
1312 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1313 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1315 MyAdjustScrollbars();
1316 MoveCursor( cursorX
, cursorY
);
1317 RefreshDown( m_cursorY
);
1321 wxString
tmp1( tmp
);
1322 tmp1
.Remove( m_cursorX
, tmp
.Len()-m_cursorX
);
1323 m_lines
[m_cursorY
].m_text
= tmp1
;
1325 wxString
tmp2( tmp
);
1326 tmp2
.Remove( 0, m_cursorX
);
1328 int cursorX
= indent
;
1329 int cursorY
= m_cursorY
+ 1;
1332 for (size_t i
= 0; i
< indent
; i
++) new_tmp
.Append( ' ' );
1333 new_tmp
.Append( tmp2
);
1334 m_lines
.Insert( new wxSourceLine( new_tmp
), cursorY
);
1336 MyAdjustScrollbars();
1337 MoveCursor( cursorX
, cursorY
);
1338 RefreshDown( m_cursorY
-1 );
1342 void wxTextCtrl::DoDClick()
1344 wxString
line( m_lines
[ m_cursorY
].m_text
);
1345 if (m_cursorX
>= (int)line
.Len()) return;
1347 char ch
= line
[(size_t) (p
)];
1348 if (((ch
>= 'a') && (ch
<= 'z')) ||
1349 ((ch
>= 'A') && (ch
<= 'Z')) ||
1350 ((ch
>= '0') && (ch
<= '9')) ||
1353 m_selStartY
= m_cursorY
;
1354 m_selEndY
= m_cursorY
;
1357 ch
= line
[(size_t) (p
-1)];
1358 while (((ch
>= 'a') && (ch
<= 'z')) ||
1359 ((ch
>= 'A') && (ch
<= 'Z')) ||
1360 ((ch
>= '0') && (ch
<= '9')) ||
1365 ch
= line
[(size_t) (p
-1)];
1371 if (p
< (int)line
.Len())
1373 ch
= line
[(size_t) (p
)];
1374 while (((ch
>= 'a') && (ch
<= 'z')) ||
1375 ((ch
>= 'A') && (ch
<= 'Z')) ||
1376 ((ch
>= '0') && (ch
<= '9')) ||
1379 if (p
>= (int)line
.Len()) break;
1381 ch
= line
[(size_t) (p
)];
1385 RefreshLine( m_cursorY
);
1389 wxString
wxTextCtrl::GetNextToken( wxString
&line
, size_t &pos
)
1392 size_t len
= line
.Len();
1393 for (size_t p
= pos
; p
< len
; p
++)
1395 if ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
))
1399 for (size_t q
= p
; q
< len
; q
++)
1400 ret
.Append( line
[q
] );
1407 if ((line
[p
] == '/') && (p
+1 < len
) && (line
[(size_t) (p
+1)] == '/'))
1409 for (size_t q
= p
; q
< len
; q
++)
1410 ret
.Append( line
[q
] );
1418 ret
.Append( line
[p
] );
1419 for (size_t q
= p
+1; q
< len
; q
++)
1421 ret
.Append( line
[q
] );
1422 if ((line
[q
] == '"') && ((line
[(size_t) (q
-1)] != '\\') || (q
>= 2 && line
[(size_t) (q
-2)] == '\\')))
1429 if (line
[p
] == '\'')
1431 ret
.Append( line
[p
] );
1432 for (size_t q
= p
+1; q
< len
; q
++)
1434 ret
.Append( line
[q
] );
1435 if ((line
[q
] == '\'') && ((line
[(size_t) (q
-1)] != '\\') || (q
>= 2 && line
[(size_t) (q
-2)] == '\\')))
1442 if (((line
[p
] >= 'a') && (line
[p
] <= 'z')) ||
1443 ((line
[p
] >= 'A') && (line
[p
] <= 'Z')) ||
1447 ret
.Append( line
[p
] );
1448 for (size_t q
= p
+1; q
< len
; q
++)
1450 if (((line
[q
] >= 'a') && (line
[q
] <= 'z')) ||
1451 ((line
[q
] >= 'A') && (line
[q
] <= 'Z')) ||
1452 ((line
[q
] >= '0') && (line
[q
] <= '9')) ||
1455 ret
.Append( line
[q
] );
1472 void wxTextCtrl::OnEraseBackground( wxEraseEvent
&event
)
1477 void wxTextCtrl::DrawLinePart( wxDC
&dc
, int x
, int y
, const wxString
&toDraw
, const wxString
&origin
, const wxColour
&colour
)
1480 size_t len
= origin
.Len();
1481 dc
.SetTextForeground( colour
);
1484 while (toDraw
[pos
] == wxT(' '))
1487 if (pos
== len
) return;
1493 current
+= toDraw
[pos
];
1495 while ( (toDraw
[pos
] == origin
[pos
]) && (pos
< len
))
1497 current
+= toDraw
[pos
];
1502 wxString tmp
= origin
.Left( start
);
1503 GetTextExtent( tmp
, &xx
, NULL
, NULL
, NULL
);
1506 dc
.DrawText( current
, xx
, yy
);
1510 void wxTextCtrl::DrawLine( wxDC
&dc
, int x
, int y
, const wxString
&line2
, int lineNum
)
1512 int selStartY
= m_selStartY
;
1513 int selEndY
= m_selEndY
;
1514 int selStartX
= m_selStartX
;
1515 int selEndX
= m_selEndX
;
1517 if ((selStartY
> selEndY
) ||
1518 ((selStartY
== selEndY
) && (selStartX
> selEndX
)))
1520 int tmp
= selStartX
;
1521 selStartX
= selEndX
;
1524 selStartY
= selEndY
;
1528 wxString
line( line2
);
1529 if (HasFlag(wxTE_PASSWORD
))
1531 size_t len
= line
.Len();
1532 line
= wxString( wxT('*'), len
);
1535 wxString
keyword( ' ', line
.Len() );
1536 wxString
define( ' ', line
.Len() );
1537 wxString
variable( ' ', line
.Len() );
1538 wxString
comment( ' ', line
.Len() );
1539 wxString
my_string( ' ', line
.Len() );
1540 wxString
selection( ' ', line
.Len() );
1542 if (m_lang
!= wxSOURCE_LANG_NONE
)
1544 if (lineNum
== m_bracketY
)
1546 wxString
red( ' ', line
.Len() );
1547 if (m_bracketX
< (int)line
.Len())
1549 red
.SetChar( m_bracketX
, line
[(size_t) (m_bracketX
)] );
1550 line
.SetChar( m_bracketX
, ' ' );
1551 dc
.SetTextForeground( *wxRED
);
1552 dc
.DrawText( red
, x
, y
);
1553 dc
.SetTextForeground( *wxBLACK
);
1558 wxString
token( GetNextToken( line
, pos
) );
1559 while ( !token
.empty() )
1561 if (m_keywords
.Index( token
) != wxNOT_FOUND
)
1563 size_t end_pos
= pos
+ token
.Len();
1564 for (size_t i
= pos
; i
< end_pos
; i
++)
1566 keyword
[i
] = line
[i
];
1570 if (m_defines
.Index( token
) != wxNOT_FOUND
)
1572 size_t end_pos
= pos
+ token
.Len();
1573 for (size_t i
= pos
; i
< end_pos
; i
++)
1575 define
[i
] = line
[i
];
1579 if ((m_variables
.Index( token
) != wxNOT_FOUND
) ||
1580 ((token
.Len() > 2) && (token
[(size_t) (0)] == 'w') && (token
[(size_t) (1)] == 'x')))
1582 size_t end_pos
= pos
+ token
.Len();
1583 for (size_t i
= pos
; i
< end_pos
; i
++)
1585 variable
[i
] = line
[i
];
1589 if ((token
.Len() >= 2) && (token
[(size_t) (0)] == '/') && (token
[(size_t) (1)] == '/') && (m_lang
== wxSOURCE_LANG_CPP
))
1591 size_t end_pos
= pos
+ token
.Len();
1592 for (size_t i
= pos
; i
< end_pos
; i
++)
1594 comment
[i
] = line
[i
];
1598 if ((token
[(size_t) (0)] == '#') &&
1599 ((m_lang
== wxSOURCE_LANG_PYTHON
) || (m_lang
== wxSOURCE_LANG_PERL
)))
1601 size_t end_pos
= pos
+ token
.Len();
1602 for (size_t i
= pos
; i
< end_pos
; i
++)
1604 comment
[i
] = line
[i
];
1608 if ((token
[(size_t) (0)] == '"') || (token
[(size_t) (0)] == '\''))
1610 size_t end_pos
= pos
+ token
.Len();
1611 for (size_t i
= pos
; i
< end_pos
; i
++)
1613 my_string
[i
] = line
[i
];
1618 token
= GetNextToken( line
, pos
);
1622 if ((lineNum
< selStartY
) || (lineNum
> selEndY
))
1624 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1625 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1626 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1627 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1628 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1629 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1630 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1634 if (selStartY
== selEndY
)
1636 // int xx = selStartX*m_charWidth;
1637 int xx
= PosToPixel( lineNum
, selStartX
);
1638 // int ww = (selEndX-selStartX)*m_charWidth;
1639 int ww
= PosToPixel( lineNum
, selEndX
) - xx
;
1640 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1642 for (size_t i
= (size_t)selStartX
; i
< (size_t)selEndX
; i
++)
1644 selection
[i
] = line
[i
];
1648 if ((lineNum
> selStartY
) && (lineNum
< selEndY
))
1650 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1652 for (size_t i
= 0; i
< line
.Len(); i
++)
1654 selection
[i
] = line
[i
];
1658 if (lineNum
== selStartY
)
1660 // int xx = selStartX*m_charWidth;
1661 int xx
= PosToPixel( lineNum
, selStartX
);
1662 dc
.DrawRectangle( xx
+2, lineNum
*m_lineHeight
+2, 10000, m_lineHeight
);
1664 for (size_t i
= (size_t)selStartX
; i
< line
.Len(); i
++)
1666 selection
[i
] = line
[i
];
1670 if (lineNum
== selEndY
)
1672 // int ww = selEndX*m_charWidth;
1673 int ww
= PosToPixel( lineNum
, selEndX
);
1674 dc
.DrawRectangle( 0+2, lineNum
*m_lineHeight
+2, ww
, m_lineHeight
);
1676 for (size_t i
= 0; i
< (size_t)selEndX
; i
++)
1678 selection
[i
] = line
[i
];
1683 DrawLinePart( dc
, x
, y
, line
, line2
, *wxBLACK
);
1684 DrawLinePart( dc
, x
, y
, selection
, line2
, *wxWHITE
);
1685 DrawLinePart( dc
, x
, y
, keyword
, line2
, m_keywordColour
);
1686 DrawLinePart( dc
, x
, y
, define
, line2
, m_defineColour
);
1687 DrawLinePart( dc
, x
, y
, variable
, line2
, m_variableColour
);
1688 DrawLinePart( dc
, x
, y
, comment
, line2
, m_commentColour
);
1689 DrawLinePart( dc
, x
, y
, my_string
, line2
, m_stringColour
);
1692 void wxTextCtrl::OnPaint( wxPaintEvent
&event
)
1696 if (m_lines
.GetCount() == 0) return;
1700 dc
.SetFont( m_sourceFont
);
1703 GetViewStart( NULL
, &scroll_y
);
1705 // We have a inner border of two pixels
1706 // around the text, so scroll units do
1707 // not correspond to lines.
1708 if (scroll_y
> 0) scroll_y
--;
1712 GetClientSize( &size_x
, &size_y
);
1714 dc
.SetPen( *wxTRANSPARENT_PEN
);
1715 dc
.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT
), wxSOLID
) );
1716 int upper
= wxMin( (int)m_lines
.GetCount(), scroll_y
+(size_y
/m_lineHeight
)+2 );
1717 for (int i
= scroll_y
; i
< upper
; i
++)
1720 int y
= i
*m_lineHeight
+2;
1722 int h
= m_lineHeight
;
1723 CalcScrolledPosition( x
,y
,&x
,&y
);
1724 if (IsExposed(x
,y
,w
,h
))
1725 DrawLine( dc
, 0+2, i
*m_lineHeight
+2, m_lines
[i
].m_text
, i
);
1728 if (m_editable
&& (FindFocus() == this))
1730 ///dc.SetBrush( *wxRED_BRUSH );
1731 dc
.SetBrush( *wxBLACK_BRUSH
);
1732 // int xx = m_cursorX*m_charWidth;
1733 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
1734 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
1738 void wxTextCtrl::OnMouse( wxMouseEvent
&event
)
1740 if (m_lines
.GetCount() == 0) return;
1743 #if 0 // there is no middle button on iPAQs
1744 if (event
.MiddleDown())
1751 if (event
.LeftDClick())
1757 if (event
.LeftDown())
1765 m_capturing
= false;
1769 if (event
.LeftDown() ||
1770 (event
.LeftIsDown() && m_capturing
))
1772 int x
= event
.GetX();
1773 int y
= event
.GetY();
1774 CalcUnscrolledPosition( x
, y
, &x
, &y
);
1776 // x /= m_charWidth;
1777 x
= PixelToPos( y
, x
);
1779 wxMin( 1000, wxMax( 0, x
) ),
1780 wxMin( (int)m_lines
.GetCount()-1, wxMax( 0, y
) ),
1781 event
.ShiftDown() || !event
.LeftDown() );
1785 void wxTextCtrl::OnChar( wxKeyEvent
&event
)
1787 if (m_lines
.GetCount() == 0) return;
1789 if (!m_editable
) return;
1793 GetClientSize( &size_x
, &size_y
);
1794 size_x
/= m_charWidth
;
1795 size_y
/= m_lineHeight
;
1798 if (event
.ShiftDown())
1800 switch (event
.GetKeyCode())
1802 case '4': event
.m_keyCode
= WXK_LEFT
; break;
1803 case '8': event
.m_keyCode
= WXK_UP
; break;
1804 case '6': event
.m_keyCode
= WXK_RIGHT
; break;
1805 case '2': event
.m_keyCode
= WXK_DOWN
; break;
1806 case '9': event
.m_keyCode
= WXK_PAGEUP
; break;
1807 case '3': event
.m_keyCode
= WXK_PAGEDOWN
; break;
1808 case '7': event
.m_keyCode
= WXK_HOME
; break;
1809 case '1': event
.m_keyCode
= WXK_END
; break;
1810 case '0': event
.m_keyCode
= WXK_INSERT
; break;
1814 switch (event
.GetKeyCode())
1818 if (m_ignoreInput
) return;
1820 MoveCursor( m_cursorX
, m_cursorY
-1, event
.ShiftDown() );
1821 m_ignoreInput
= true;
1826 if (m_ignoreInput
) return;
1827 if (m_cursorY
< (int)(m_lines
.GetCount()-1))
1828 MoveCursor( m_cursorX
, m_cursorY
+1, event
.ShiftDown() );
1829 m_ignoreInput
= true;
1834 if (m_ignoreInput
) return;
1837 MoveCursor( m_cursorX
-1, m_cursorY
, event
.ShiftDown() );
1842 MoveCursor( m_lines
[m_cursorY
-1].m_text
.Len(), m_cursorY
-1, event
.ShiftDown() );
1844 m_ignoreInput
= true;
1849 if (m_ignoreInput
) return;
1850 if (m_cursorX
< 1000)
1851 MoveCursor( m_cursorX
+1, m_cursorY
, event
.ShiftDown() );
1852 m_ignoreInput
= true;
1857 if (event
.ControlDown())
1858 MoveCursor( 0, 0, event
.ShiftDown() );
1860 MoveCursor( 0, m_cursorY
, event
.ShiftDown() );
1865 if (event
.ControlDown())
1866 MoveCursor( 0, m_lines
.GetCount()-1, event
.ShiftDown() );
1868 MoveCursor( m_lines
[m_cursorY
].m_text
.Len(), m_cursorY
, event
.ShiftDown() );
1873 if (m_ignoreInput
) return;
1874 MoveCursor( m_cursorX
, wxMin( (int)(m_lines
.GetCount()-1), m_cursorY
+size_y
), event
.ShiftDown() );
1875 m_ignoreInput
= true;
1880 if (m_ignoreInput
) return;
1881 MoveCursor( m_cursorX
, wxMax( 0, m_cursorY
-size_y
), event
.ShiftDown() );
1882 m_ignoreInput
= true;
1887 if (event
.ShiftDown())
1889 else if (event
.ControlDown())
1892 m_overwrite
= !m_overwrite
;
1897 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
1899 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
1900 event
.SetEventObject(this);
1901 event
.SetString(GetValue());
1902 if (HandleWindowEvent(event
)) return;
1920 bool save_overwrite
= m_overwrite
;
1921 m_overwrite
= false;
1922 int i
= 4-(m_cursorX
% 4);
1924 for (int c
= 0; c
< i
; c
++)
1926 m_overwrite
= save_overwrite
;
1947 if ( (event
.GetKeyCode() >= 'a') &&
1948 (event
.GetKeyCode() <= 'z') &&
1956 if ( (event
.GetKeyCode() >= 32) &&
1957 (event
.GetKeyCode() <= 255) &&
1958 !(event
.ControlDown() && !event
.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr
1962 DoChar( (char) event
.GetKeyCode() );
1971 void wxTextCtrl::OnInternalIdle()
1973 wxControl::OnInternalIdle();
1975 m_ignoreInput
= false;
1977 if (m_lang
!= wxSOURCE_LANG_NONE
)
1978 SearchForBrackets();
1981 void wxTextCtrl::Indent()
1983 int startY
= m_cursorY
;
1984 int endY
= m_cursorY
;
1987 startY
= m_selStartY
;
1997 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
1999 for (int i
= startY
; i
<= endY
; i
++)
2001 m_lines
[i
].m_text
.insert( 0u, wxT(" ") );
2006 void wxTextCtrl::Unindent()
2008 int startY
= m_cursorY
;
2009 int endY
= m_cursorY
;
2012 startY
= m_selStartY
;
2022 m_undos
.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE
, startY
, endY
, this ) );
2024 for (int i
= startY
; i
<= endY
; i
++)
2026 for (int n
= 0; n
< 4; n
++)
2028 if (m_lines
[i
].m_text
[0u] == wxT(' '))
2029 m_lines
[i
].m_text
.erase(0u,1u);
2034 bool wxTextCtrl::HasSelection()
2036 return ((m_selStartY
!= m_selEndY
) || (m_selStartX
!= m_selEndX
));
2039 void wxTextCtrl::ClearSelection()
2047 void wxTextCtrl::RefreshLine( int n
)
2049 int y
= n
*m_lineHeight
;
2051 CalcScrolledPosition( x
, y
, &x
, &y
);
2052 wxRect
rect( 0+2, y
+2, 10000, m_lineHeight
);
2053 Refresh( true, &rect
);
2056 void wxTextCtrl::RefreshDown( int n
)
2060 GetClientSize( &size_x
, &size_y
);
2064 GetViewStart( &view_x
, &view_y
);
2072 int y
= n
*m_lineHeight
;
2074 CalcScrolledPosition( x
, y
, &x
, &y
);
2076 wxRect
rect( 0+2, y
+2, 10000, size_y
);
2077 Refresh( true, &rect
);
2081 void wxTextCtrl::MoveCursor( int new_x
, int new_y
, bool shift
, bool centre
)
2083 if (!m_editable
) return;
2085 // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
2087 if (new_x
> (int) (m_lines
[new_y
].m_text
.Len()))
2088 new_x
= m_lines
[new_y
].m_text
.Len();
2091 if ((new_x
== m_cursorX
) && (new_y
== m_cursorY
)) return;
2093 bool no_cursor_refresh
= false;
2094 bool has_selection
= HasSelection();
2099 bool erase_background
= true;
2103 m_selStartX
= m_cursorX
;
2104 m_selStartY
= m_cursorY
;
2108 if (new_y
> m_selStartY
)
2110 y
= m_selStartY
*m_lineHeight
;
2111 h
= (new_y
-m_selStartY
+1)*m_lineHeight
;
2113 else if (new_y
== m_selStartY
)
2115 x
= PosToPixel( new_y
, m_selStartX
);
2116 w
= PosToPixel( new_y
, new_x
) - x
;
2120 w
= -w
+ 2; // +2 for the cursor
2122 y
= m_selStartY
*m_lineHeight
;
2127 y
= new_y
*m_lineHeight
;
2128 h
= (-new_y
+m_selStartY
+1)*m_lineHeight
;
2131 no_cursor_refresh
= true;
2137 if (new_y
== m_selEndY
)
2139 y
= new_y
*m_lineHeight
;
2141 if (m_selEndX
> new_x
)
2143 // x = new_x*m_charWidth;
2144 x
= PosToPixel( new_y
, new_x
);
2145 // w = (m_selEndX-new_x)*m_charWidth;
2146 w
= PosToPixel( new_y
, m_selEndX
) - x
;
2150 // x = m_selEndX*m_charWidth;
2151 x
= PosToPixel( new_y
, m_selEndX
);
2152 // w = (-m_selEndX+new_x)*m_charWidth;
2153 w
= PosToPixel( new_y
, new_x
) - x
;
2160 if (new_y
> m_selEndY
)
2162 y
= m_selEndY
*m_lineHeight
;
2163 h
= (new_y
-m_selEndY
+1) * m_lineHeight
;
2165 erase_background
= ((m_selEndY
< m_selStartY
) ||
2166 ((m_selEndY
== m_selStartY
) && (m_selEndX
< m_selStartX
)));
2170 y
= new_y
*m_lineHeight
;
2171 h
= (-new_y
+m_selEndY
+1) * m_lineHeight
;
2173 erase_background
= ((m_selEndY
> m_selStartY
) ||
2174 ((m_selEndY
== m_selStartY
) && (m_selEndX
> m_selStartX
)));
2176 no_cursor_refresh
= true;
2185 CalcScrolledPosition( x
, y
, &x
, &y
);
2186 wxRect
rect( x
+2, y
+2, w
, h
);
2187 Refresh( erase_background
, &rect
);
2193 int ry1
= m_selEndY
;
2194 int ry2
= m_selStartY
;
2208 int y
= ry1
*m_lineHeight
;
2209 CalcScrolledPosition( x
, y
, &x
, &y
);
2210 wxRect
rect( 0, y
+2, 10000, (ry2
-ry1
+1)*m_lineHeight
);
2212 Refresh( true, &rect
);
2217 printf( "startx %d starty %d endx %d endy %d\n",
2218 m_selStartX, m_selStartY, m_selEndX, m_selEndY );
2220 printf( "has %d\n", (int)HasSelection() );
2223 if (!no_cursor_refresh
)
2225 // int x = m_cursorX*m_charWidth;
2226 int x
= PosToPixel( m_cursorY
, m_cursorX
);
2227 int y
= m_cursorY
*m_lineHeight
;
2228 CalcScrolledPosition( x
, y
, &x
, &y
);
2229 wxRect
rect( x
+2, y
+2, 4, m_lineHeight
+2 );
2234 Refresh( true, &rect
);
2236 if (FindFocus() == this)
2238 wxClientDC
dc(this);
2240 dc
.SetPen( *wxTRANSPARENT_PEN
);
2241 //dc.SetBrush( *wxRED_BRUSH );
2242 dc
.SetBrush( *wxBLACK_BRUSH
);
2243 // int xx = m_cursorX*m_charWidth;
2244 int xx
= PosToPixel( m_cursorY
, m_cursorX
);
2245 dc
.DrawRectangle( xx
+2, m_cursorY
*m_lineHeight
+2, 2, m_lineHeight
);
2251 GetClientSize( &size_x
, &size_y
);
2252 size_x
/= m_charWidth
;
2253 size_y
/= m_lineHeight
;
2257 GetViewStart( &view_x
, &view_y
);
2261 int sy
= m_cursorY
- (size_y
/2);
2267 if (m_cursorY
< view_y
)
2268 Scroll( -1, m_cursorY
);
2269 else if (m_cursorY
> view_y
+size_y
-1)
2270 Scroll( -1, m_cursorY
-size_y
+1 );
2273 //int xx = m_cursorX;
2274 int xx
= PosToPixel( m_cursorY
, m_cursorX
) / m_charWidth
;
2278 else if (xx
> view_x
+size_x
-1)
2279 Scroll( xx
-size_x
+1, -1 );
2282 void wxTextCtrl::MyAdjustScrollbars()
2287 int y_range
= m_lines
.GetCount();
2290 GetClientSize( NULL
, &height
);
2292 if (height
>= (int)m_lines
.GetCount() *m_lineHeight
)
2297 GetViewStart( &view_x
, &view_y
);
2299 SetScrollbars( m_charWidth
, m_lineHeight
, m_longestLine
+2, y_range
, view_x
, view_y
);
2302 //-----------------------------------------------------------------------------
2303 // clipboard handlers
2304 //-----------------------------------------------------------------------------
2306 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
2311 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
2316 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
2321 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
2326 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
2331 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
2333 event
.Enable( CanCut() );
2336 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
2338 event
.Enable( CanCopy() );
2341 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
2343 event
.Enable( CanPaste() );
2346 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
2348 event
.Enable( CanUndo() );
2351 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
2353 event
.Enable( CanRedo() );
2356 wxSize
wxTextCtrl::DoGetBestSize() const
2360 wxSize
ret(80, m_lineHeight
+ 4);
2362 if (HasFlag(wxBORDER_SUNKEN
) || HasFlag(wxBORDER_RAISED
))
2365 if (HasFlag(wxBORDER_SIMPLE
))
2372 return wxSize(80, 60);
2376 void wxTextCtrl::OnSetFocus( wxFocusEvent
& event
)
2378 // To hide or show caret, as appropriate
2382 void wxTextCtrl::OnKillFocus( wxFocusEvent
& event
)
2384 // To hide or show caret, as appropriate
2388 // ----------------------------------------------------------------------------
2389 // text control scrolling
2390 // ----------------------------------------------------------------------------
2392 bool wxTextCtrl::ScrollLines(int lines
)
2394 wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented");
2399 bool wxTextCtrl::ScrollPages(int pages
)
2401 wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented");