]> git.saurik.com Git - wxWidgets.git/blame - src/osx/textctrl_osx.cpp
Fix recurring typo in "theming".
[wxWidgets.git] / src / osx / textctrl_osx.cpp
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/textctrl_osx.cpp
3// Purpose: wxTextCtrl
4// Author: Stefan Csomor
5// Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
6// Created: 1998-01-01
7// RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_TEXTCTRL
15
16#include "wx/textctrl.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dc.h"
23 #include "wx/button.h"
24 #include "wx/menu.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
28#endif
29
30#ifdef __DARWIN__
31 #include <sys/types.h>
32 #include <sys/stat.h>
33#else
34 #include <stat.h>
35#endif
36
37#if wxUSE_STD_IOSTREAM
38 #if wxUSE_IOSTREAMH
39 #include <fstream.h>
40 #else
41 #include <fstream>
42 #endif
43#endif
44
45#include "wx/filefn.h"
46#include "wx/sysopt.h"
47#include "wx/thread.h"
48
49#include "wx/osx/private.h"
524c47aa
SC
50
51IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
52
53BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
54 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
55 EVT_CHAR(wxTextCtrl::OnChar)
56 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
57 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
58 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
59 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
60 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
61 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
62 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
63
64 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu)
65
66 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
67 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
68 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
69 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
70 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
71 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
72 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
73END_EVENT_TABLE()
74
75
76void wxTextCtrl::Init()
77{
78 m_editable = true ;
79 m_dirty = false;
80
81 m_maxLength = 0;
82 m_privateContextMenu = NULL;
d9d551f6 83 m_triggerUpdateEvents = true ;
524c47aa
SC
84}
85
86wxTextCtrl::~wxTextCtrl()
87{
88#if wxUSE_MENUS
89 delete m_privateContextMenu;
90#endif
91}
92
93bool wxTextCtrl::Create( wxWindow *parent,
94 wxWindowID id,
95 const wxString& str,
96 const wxPoint& pos,
97 const wxSize& size,
98 long style,
99 const wxValidator& validator,
100 const wxString& name )
101{
102 m_macIsUserPane = false ;
103 m_editable = true ;
104
105 if ( ! (style & wxNO_BORDER) )
106 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
107
108 if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
109 return false;
110
111 if ( m_windowStyle & wxTE_MULTILINE )
112 {
113 // always turn on this style for multi-line controls
114 m_windowStyle |= wxTE_PROCESS_ENTER;
115 style |= wxTE_PROCESS_ENTER ;
116 }
117
524c47aa 118
1e181c7a 119 m_peer = wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str, pos, size, style, GetExtraStyle() );
524c47aa
SC
120
121 MacPostControlCreate(pos, size) ;
122
123 // only now the embedding is correct and we can do a positioning update
124
125 MacSuperChangedPosition() ;
126
127 if ( m_windowStyle & wxTE_READONLY)
128 SetEditable( false ) ;
129
130 SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ;
131
132 return true;
133}
134
1e181c7a 135wxTextWidgetImpl* wxTextCtrl::GetTextPeer() const
524c47aa 136{
03647350 137 return dynamic_cast<wxTextWidgetImpl*> (m_peer);
524c47aa
SC
138}
139
140void wxTextCtrl::MacSuperChangedPosition()
141{
142 wxWindow::MacSuperChangedPosition() ;
143#if wxOSX_USE_CARBON
144 GetPeer()->SuperChangedPosition() ;
145#endif
146}
147
148void wxTextCtrl::MacVisibilityChanged()
149{
150#if wxOSX_USE_CARBON
151 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
152#endif
153}
154
155void wxTextCtrl::MacCheckSpelling(bool check)
156{
1e181c7a 157 GetTextPeer()->CheckSpelling(check);
524c47aa
SC
158}
159
33b3ade5 160wxString wxTextCtrl::DoGetValue() const
524c47aa 161{
1e181c7a 162 return GetTextPeer()->GetStringValue() ;
524c47aa
SC
163}
164
165void wxTextCtrl::GetSelection(long* from, long* to) const
166{
1e181c7a 167 GetTextPeer()->GetSelection( from , to ) ;
524c47aa
SC
168}
169
524c47aa
SC
170void wxTextCtrl::SetMaxLength(unsigned long len)
171{
172 m_maxLength = len ;
173}
174
175bool wxTextCtrl::SetFont( const wxFont& font )
176{
177 if ( !wxTextCtrlBase::SetFont( font ) )
178 return false ;
179
1e181c7a 180 GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
524c47aa
SC
181
182 return true ;
183}
184
185bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
186{
1e181c7a 187 GetTextPeer()->SetStyle( start , end , style ) ;
524c47aa
SC
188
189 return true ;
190}
191
192bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
193{
194 wxTextCtrlBase::SetDefaultStyle( style ) ;
195 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
196
197 return true ;
198}
199
200// Clipboard operations
201
202void wxTextCtrl::Copy()
203{
204 if (CanCopy())
1e181c7a 205 GetTextPeer()->Copy() ;
524c47aa
SC
206}
207
208void wxTextCtrl::Cut()
209{
210 if (CanCut())
211 {
1e181c7a 212 GetTextPeer()->Cut() ;
524c47aa 213
d9d551f6
SC
214 SendTextUpdatedEvent();
215 }
524c47aa
SC
216}
217
218void wxTextCtrl::Paste()
219{
220 if (CanPaste())
221 {
1e181c7a 222 GetTextPeer()->Paste() ;
524c47aa
SC
223
224 // TODO: eventually we should add setting the default style again
d9d551f6 225 SendTextUpdatedEvent();
524c47aa
SC
226 }
227}
228
229bool wxTextCtrl::CanCopy() const
230{
231 // Can copy if there's a selection
232 long from, to;
233 GetSelection( &from, &to );
234
235 return (from != to);
236}
237
238bool wxTextCtrl::CanCut() const
239{
240 if ( !IsEditable() )
241 return false;
242
243 // Can cut if there's a selection
244 long from, to;
245 GetSelection( &from, &to );
246
247 return (from != to);
248}
249
250bool wxTextCtrl::CanPaste() const
251{
252 if (!IsEditable())
253 return false;
254
1e181c7a 255 return GetTextPeer()->CanPaste() ;
524c47aa
SC
256}
257
258void wxTextCtrl::SetEditable(bool editable)
259{
260 if ( editable != m_editable )
261 {
262 m_editable = editable ;
1e181c7a 263 GetTextPeer()->SetEditable( editable ) ;
524c47aa
SC
264 }
265}
266
267void wxTextCtrl::SetInsertionPoint(long pos)
268{
269 SetSelection( pos , pos ) ;
270}
271
272void wxTextCtrl::SetInsertionPointEnd()
273{
0b6a49c2 274 long pos = GetLastPosition();
524c47aa
SC
275 SetInsertionPoint( pos );
276}
277
278long wxTextCtrl::GetInsertionPoint() const
279{
280 long begin, end ;
281 GetSelection( &begin , &end ) ;
282
283 return begin ;
284}
285
286wxTextPos wxTextCtrl::GetLastPosition() const
287{
1e181c7a 288 return GetTextPeer()->GetLastPosition() ;
524c47aa
SC
289}
290
524c47aa
SC
291void wxTextCtrl::Remove(long from, long to)
292{
1e181c7a 293 GetTextPeer()->Remove( from , to ) ;
d9d551f6
SC
294 if ( m_triggerUpdateEvents )
295 SendTextUpdatedEvent();
524c47aa
SC
296}
297
298void wxTextCtrl::SetSelection(long from, long to)
299{
1e181c7a 300 GetTextPeer()->SetSelection( from , to ) ;
524c47aa
SC
301}
302
303void wxTextCtrl::WriteText(const wxString& str)
304{
1e181c7a 305 GetTextPeer()->WriteText( str ) ;
d9d551f6
SC
306 if ( m_triggerUpdateEvents )
307 SendTextUpdatedEvent();
524c47aa
SC
308}
309
310void wxTextCtrl::Clear()
311{
1e181c7a 312 GetTextPeer()->Clear() ;
d9d551f6 313 SendTextUpdatedEvent();
524c47aa
SC
314}
315
316bool wxTextCtrl::IsModified() const
317{
318 return m_dirty;
319}
320
321bool wxTextCtrl::IsEditable() const
322{
323 return IsEnabled() && m_editable ;
324}
325
326bool wxTextCtrl::AcceptsFocus() const
327{
328 // we don't want focus if we can't be edited
329 return /*IsEditable() && */ wxControl::AcceptsFocus();
330}
331
332wxSize wxTextCtrl::DoGetBestSize() const
333{
334 int wText, hText;
335
336 // these are the numbers from the HIG:
337 // we reduce them by the borders first
338 wText = 100 ;
339
340 switch ( m_windowVariant )
341 {
342 case wxWINDOW_VARIANT_NORMAL :
343 hText = 22 - 6 ;
344 break ;
345
346 case wxWINDOW_VARIANT_SMALL :
347 hText = 19 - 6 ;
348 break ;
349
350 case wxWINDOW_VARIANT_MINI :
351 hText = 15 - 6 ;
352 break ;
353
354 default :
355 hText = 22 - 6;
356 break ;
357 }
358
359 // as the above numbers have some free space around the text
360 // we get 5 lines like this anyway
361 if ( m_windowStyle & wxTE_MULTILINE )
362 hText *= 5 ;
363
364 if ( !HasFlag(wxNO_BORDER) )
365 hText += 6 ;
366
367 return wxSize(wText, hText);
368}
369
370// ----------------------------------------------------------------------------
371// Undo/redo
372// ----------------------------------------------------------------------------
373
374void wxTextCtrl::Undo()
375{
376 if (CanUndo())
1e181c7a 377 GetTextPeer()->Undo() ;
524c47aa
SC
378}
379
380void wxTextCtrl::Redo()
381{
382 if (CanRedo())
1e181c7a 383 GetTextPeer()->Redo() ;
524c47aa
SC
384}
385
386bool wxTextCtrl::CanUndo() const
387{
388 if ( !IsEditable() )
389 return false ;
390
1e181c7a 391 return GetTextPeer()->CanUndo() ;
524c47aa
SC
392}
393
394bool wxTextCtrl::CanRedo() const
395{
396 if ( !IsEditable() )
397 return false ;
398
1e181c7a 399 return GetTextPeer()->CanRedo() ;
524c47aa
SC
400}
401
402void wxTextCtrl::MarkDirty()
403{
404 m_dirty = true;
405}
406
407void wxTextCtrl::DiscardEdits()
408{
409 m_dirty = false;
410}
411
412int wxTextCtrl::GetNumberOfLines() const
413{
1e181c7a 414 return GetTextPeer()->GetNumberOfLines() ;
524c47aa
SC
415}
416
417long wxTextCtrl::XYToPosition(long x, long y) const
418{
1e181c7a 419 return GetTextPeer()->XYToPosition( x , y ) ;
524c47aa
SC
420}
421
422bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
423{
1e181c7a 424 return GetTextPeer()->PositionToXY( pos , x , y ) ;
524c47aa
SC
425}
426
427void wxTextCtrl::ShowPosition(long pos)
428{
1e181c7a 429 return GetTextPeer()->ShowPosition(pos) ;
524c47aa
SC
430}
431
432int wxTextCtrl::GetLineLength(long lineNo) const
433{
1e181c7a 434 return GetTextPeer()->GetLineLength(lineNo) ;
524c47aa
SC
435}
436
437wxString wxTextCtrl::GetLineText(long lineNo) const
438{
1e181c7a 439 return GetTextPeer()->GetLineText(lineNo) ;
524c47aa
SC
440}
441
442void wxTextCtrl::Command(wxCommandEvent & event)
443{
444 SetValue(event.GetString());
445 ProcessCommand(event);
446}
447
448void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
449{
450 // By default, load the first file into the text window.
451 if (event.GetNumberOfFiles() > 0)
452 LoadFile( event.GetFiles()[0] );
453}
454
455void wxTextCtrl::OnChar(wxKeyEvent& event)
456{
457 int key = event.GetKeyCode() ;
458 bool eat_key = false ;
459 long from, to;
460
461 if ( key == 'a' && event.MetaDown() )
462 {
463 SelectAll() ;
464
465 return ;
466 }
467
468 if ( key == 'c' && event.MetaDown() )
469 {
470 if ( CanCopy() )
471 Copy() ;
472
473 return ;
474 }
475
7a34307e 476 if ( !IsEditable() && !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB) &&
524c47aa
SC
477 !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
478// && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
479 )
480 {
481 // eat it
482 return ;
483 }
484
485 // Check if we have reached the max # of chars (if it is set), but still
486 // allow navigation and deletion
487 GetSelection( &from, &to );
488 if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
7a34307e
VZ
489 !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) &&
490 !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
524c47aa
SC
491 from == to )
492 {
493 // eat it, we don't want to add more than allowed # of characters
494
495 // TODO: generate EVT_TEXT_MAXLEN()
496 return;
497 }
498
499 // assume that any key not processed yet is going to modify the control
500 m_dirty = true;
501
502 if ( key == 'v' && event.MetaDown() )
503 {
504 if ( CanPaste() )
505 Paste() ;
506
507 return ;
508 }
509
510 if ( key == 'x' && event.MetaDown() )
511 {
512 if ( CanCut() )
513 Cut() ;
514
515 return ;
516 }
517
518 switch ( key )
519 {
520 case WXK_RETURN:
521 if (m_windowStyle & wxTE_PROCESS_ENTER)
522 {
523 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
524 event.SetEventObject( this );
525 event.SetString( GetValue() );
526 if ( HandleWindowEvent(event) )
527 return;
528 }
529
530 if ( !(m_windowStyle & wxTE_MULTILINE) )
531 {
532 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
533 if ( tlw && tlw->GetDefaultItem() )
534 {
535 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
536 if ( def && def->IsEnabled() )
537 {
538 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
539 event.SetEventObject(def);
540 def->Command(event);
541
542 return ;
543 }
544 }
545
546 // this will make wxWidgets eat the ENTER key so that
547 // we actually prevent line wrapping in a single line text control
548 eat_key = true;
549 }
550 break;
551
552 case WXK_TAB:
553 if ( !(m_windowStyle & wxTE_PROCESS_TAB))
554 {
555 int flags = 0;
556 if (!event.ShiftDown())
557 flags |= wxNavigationKeyEvent::IsForward ;
558 if (event.ControlDown())
559 flags |= wxNavigationKeyEvent::WinChange ;
560 Navigate(flags);
561
562 return;
563 }
564 else
565 {
566 // This is necessary (don't know why);
567 // otherwise the tab will not be inserted.
568 WriteText(wxT("\t"));
569 eat_key = true;
570 }
571 break;
572
573 default:
574 break;
575 }
576
577 if (!eat_key)
578 {
579 // perform keystroke handling
580 event.Skip(true) ;
581 }
582
7cb2a241
SC
583 // osx_cocoa sends its event upon insertText
584#if wxOSX_USE_CARBON
524c47aa
SC
585 if ( ( key >= 0x20 && key < WXK_START ) ||
586 ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
587 key == WXK_RETURN ||
588 key == WXK_DELETE ||
589 key == WXK_BACK)
590 {
591 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
592 event1.SetEventObject( this );
593 wxPostEvent( GetEventHandler(), event1 );
594 }
7cb2a241 595#endif
524c47aa
SC
596}
597
598// ----------------------------------------------------------------------------
599// standard handlers for standard edit menu events
600// ----------------------------------------------------------------------------
601
602void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
603{
604 Cut();
605}
606
607void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
608{
609 Copy();
610}
611
612void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
613{
614 Paste();
615}
616
617void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
618{
619 Undo();
620}
621
622void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
623{
624 Redo();
625}
626
627void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
628{
629 long from, to;
630
631 GetSelection( &from, &to );
632 if (from != -1 && to != -1)
633 Remove( from, to );
634}
635
636void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
637{
638 SetSelection(-1, -1);
639}
640
641void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
642{
643 event.Enable( CanCut() );
644}
645
646void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
647{
648 event.Enable( CanCopy() );
649}
650
651void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
652{
653 event.Enable( CanPaste() );
654}
655
656void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
657{
658 event.Enable( CanUndo() );
659}
660
661void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
662{
663 event.Enable( CanRedo() );
664}
665
666void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
667{
668 long from, to;
669
670 GetSelection( &from, &to );
671 event.Enable( from != -1 && to != -1 && from != to && IsEditable() ) ;
672}
673
674void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
675{
676 event.Enable(GetLastPosition() > 0);
677}
678
679// CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
680
681void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
682{
1e181c7a 683 if ( GetTextPeer()->HasOwnContextMenu() )
524c47aa
SC
684 {
685 event.Skip() ;
686 return ;
687 }
688
689#if wxUSE_MENUS
690 if (m_privateContextMenu == NULL)
691 {
692 m_privateContextMenu = new wxMenu;
693 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
694 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
695 m_privateContextMenu->AppendSeparator();
696 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
697 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
698 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
699 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
700 m_privateContextMenu->AppendSeparator();
701 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
702 }
703
704 if (m_privateContextMenu != NULL)
705 PopupMenu(m_privateContextMenu);
706#endif
707}
708
709bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
710{
1e181c7a 711 if ( !GetTextPeer()->SetupCursor( pt ) )
524c47aa
SC
712 return wxWindow::MacSetupCursor( pt ) ;
713 else
714 return true ;
715}
716
717// ----------------------------------------------------------------------------
718// implementation base class
719// ----------------------------------------------------------------------------
720
1e181c7a 721void wxTextWidgetImpl::SetStyle(long WXUNUSED(start),
524c47aa
SC
722 long WXUNUSED(end),
723 const wxTextAttr& WXUNUSED(style))
724{
725}
726
1e181c7a 727void wxTextWidgetImpl::Copy()
524c47aa
SC
728{
729}
730
1e181c7a 731void wxTextWidgetImpl::Cut()
524c47aa
SC
732{
733}
734
1e181c7a 735void wxTextWidgetImpl::Paste()
524c47aa
SC
736{
737}
738
1e181c7a 739bool wxTextWidgetImpl::CanPaste() const
524c47aa
SC
740{
741 return false ;
742}
743
1e181c7a 744void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable))
524c47aa
SC
745{
746}
747
0b6a49c2 748long wxTextWidgetImpl::GetLastPosition() const
524c47aa
SC
749{
750 return GetStringValue().length() ;
751}
752
1e181c7a 753void wxTextWidgetImpl::Replace( long from , long to , const wxString &val )
524c47aa
SC
754{
755 SetSelection( from , to ) ;
756 WriteText( val ) ;
757}
758
1e181c7a 759void wxTextWidgetImpl::Remove( long from , long to )
524c47aa
SC
760{
761 SetSelection( from , to ) ;
762 WriteText( wxEmptyString) ;
763}
764
1e181c7a 765void wxTextWidgetImpl::Clear()
524c47aa
SC
766{
767 SetStringValue( wxEmptyString ) ;
768}
769
1e181c7a 770bool wxTextWidgetImpl::CanUndo() const
524c47aa
SC
771{
772 return false ;
773}
774
1e181c7a 775void wxTextWidgetImpl::Undo()
524c47aa
SC
776{
777}
778
1e181c7a 779bool wxTextWidgetImpl::CanRedo() const
524c47aa
SC
780{
781 return false ;
782}
783
1e181c7a 784void wxTextWidgetImpl::Redo()
524c47aa
SC
785{
786}
787
1e181c7a 788long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const
524c47aa
SC
789{
790 return 0 ;
791}
792
1e181c7a 793bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos),
524c47aa
SC
794 long *WXUNUSED(x),
795 long *WXUNUSED(y)) const
796{
797 return false ;
798}
799
1e181c7a 800void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos) )
524c47aa
SC
801{
802}
803
1e181c7a 804int wxTextWidgetImpl::GetNumberOfLines() const
524c47aa
SC
805{
806 ItemCount lines = 0 ;
807 wxString content = GetStringValue() ;
808 lines = 1;
809
810 for (size_t i = 0; i < content.length() ; i++)
811 {
812 if (content[i] == '\r')
813 lines++;
814 }
815
816 return lines ;
817}
818
1e181c7a 819wxString wxTextWidgetImpl::GetLineText(long lineNo) const
524c47aa
SC
820{
821 // TODO: change this if possible to reflect real lines
822 wxString content = GetStringValue() ;
823
824 // Find line first
825 int count = 0;
826 for (size_t i = 0; i < content.length() ; i++)
827 {
828 if (count == lineNo)
829 {
830 // Add chars in line then
831 wxString tmp;
832
833 for (size_t j = i; j < content.length(); j++)
834 {
835 if (content[j] == '\n')
836 return tmp;
837
838 tmp += content[j];
839 }
840
841 return tmp;
842 }
843
844 if (content[i] == '\n')
845 count++;
846 }
847
848 return wxEmptyString ;
849}
850
1e181c7a 851int wxTextWidgetImpl::GetLineLength(long lineNo) const
524c47aa
SC
852{
853 // TODO: change this if possible to reflect real lines
854 wxString content = GetStringValue() ;
855
856 // Find line first
857 int count = 0;
858 for (size_t i = 0; i < content.length() ; i++)
859 {
860 if (count == lineNo)
861 {
862 // Count chars in line then
863 count = 0;
864 for (size_t j = i; j < content.length(); j++)
865 {
866 count++;
867 if (content[j] == '\n')
868 return count;
869 }
870
871 return count;
872 }
873
874 if (content[i] == '\n')
875 count++;
876 }
877
878 return 0 ;
879}
880
524c47aa 881#endif // wxUSE_TEXTCTRL