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