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