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