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