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