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