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