Allow spell-checking to be turned off with the MLTE implementation of wxTextCtrl.
[wxWidgets.git] / src / mac / carbon / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/textctrl.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
48 #if defined(__BORLANDC__) && !defined(__WIN32__)
49 #include <alloc.h>
50 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
51 #include <malloc.h>
52 #endif
53
54 #ifndef __DARWIN__
55 #include <Scrap.h>
56 #endif
57
58 #ifndef __DARWIN__
59 #include <MacTextEditor.h>
60 #include <ATSUnicode.h>
61 #include <TextCommon.h>
62 #include <TextEncodingConverter.h>
63 #endif
64
65 #include "wx/mac/uma.h"
66
67 #ifndef __WXMAC_OSX__
68 enum
69 {
70 kTXNVisibilityTag = 'visb' // set the visibility state of the object
71 };
72 #endif
73
74
75 class wxMacFunctor
76 {
77 public :
78 wxMacFunctor() {}
79 virtual ~wxMacFunctor() {}
80
81 virtual void* operator()() = 0 ;
82
83 static void* CallBackProc( void *param )
84 {
85 wxMacFunctor* f = (wxMacFunctor*) param ;
86 void *result = (*f)() ;
87 return result ;
88 }
89 } ;
90
91 template<typename classtype, typename param1type>
92
93 class wxMacObjectFunctor1 : public wxMacFunctor
94 {
95 typedef void (classtype::*function)( param1type p1 ) ;
96 typedef void (classtype::*ref_function)( const param1type& p1 ) ;
97 public :
98 wxMacObjectFunctor1( classtype *obj , function f , param1type p1 ) :
99 wxMacFunctor()
100 {
101 m_object = obj ;
102 m_function = f ;
103 m_param1 = p1 ;
104 }
105
106 wxMacObjectFunctor1( classtype *obj , ref_function f , param1type p1 ) :
107 wxMacFunctor()
108 {
109 m_object = obj ;
110 m_refFunction = f ;
111 m_param1 = p1 ;
112 }
113
114 virtual ~wxMacObjectFunctor1() {}
115
116 virtual void* operator()()
117 {
118 (m_object->*m_function)( m_param1 ) ;
119 return NULL ;
120 }
121
122 private :
123 classtype* m_object ;
124 param1type m_param1 ;
125 union
126 {
127 function m_function ;
128 ref_function m_refFunction ;
129 } ;
130 } ;
131
132 template<typename classtype, typename param1type>
133 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
134 {
135 wxMacObjectFunctor1<classtype, param1type> params(object, function, p1) ;
136 void *result =
137 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
138 return result ;
139 }
140
141 template<typename classtype, typename param1type>
142 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
143 {
144 wxMacObjectFunctor1<classtype,param1type> params(object, function, p1) ;
145 void *result =
146 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
147 return result ;
148 }
149
150 template<typename classtype, typename param1type>
151 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
152 {
153 wxMutexGuiLeave() ;
154 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
155 wxMutexGuiEnter() ;
156 return result ;
157 }
158
159 template<typename classtype, typename param1type>
160 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
161 {
162 wxMutexGuiLeave() ;
163 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
164 wxMutexGuiEnter() ;
165 return result ;
166 }
167
168 // common interface for all implementations
169 class wxMacTextControl : public wxMacControl
170 {
171 public :
172 wxMacTextControl( wxTextCtrl *peer ) ;
173 virtual ~wxMacTextControl() ;
174
175 virtual wxString GetStringValue() const = 0 ;
176 virtual void SetStringValue( const wxString &val ) = 0 ;
177 virtual void SetSelection( long from, long to ) = 0 ;
178 virtual void GetSelection( long* from, long* to ) const = 0 ;
179 virtual void WriteText( const wxString& str ) = 0 ;
180
181 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
182 virtual void Copy() ;
183 virtual void Cut() ;
184 virtual void Paste() ;
185 virtual bool CanPaste() const ;
186 virtual void SetEditable( bool editable ) ;
187 virtual wxTextPos GetLastPosition() const ;
188 virtual void Replace( long from, long to, const wxString &str ) ;
189 virtual void Remove( long from, long to ) ;
190
191
192 virtual bool HasOwnContextMenu() const
193 { return false ; }
194
195 virtual bool SetupCursor( const wxPoint& pt )
196 { return false ; }
197
198 virtual void Clear() ;
199 virtual bool CanUndo() const;
200 virtual void Undo() ;
201 virtual bool CanRedo() const;
202 virtual void Redo() ;
203 virtual int GetNumberOfLines() const ;
204 virtual long XYToPosition(long x, long y) const;
205 virtual bool PositionToXY(long pos, long *x, long *y) const ;
206 virtual void ShowPosition(long WXUNUSED(pos)) ;
207 virtual int GetLineLength(long lineNo) const ;
208 virtual wxString GetLineText(long lineNo) const ;
209 virtual void CheckSpelling(bool check) { }
210
211 #ifndef __WXMAC_OSX__
212 virtual void MacControlUserPaneDrawProc(wxInt16 part) = 0 ;
213 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) = 0 ;
214 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) = 0 ;
215 virtual void MacControlUserPaneIdleProc() = 0 ;
216 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) = 0 ;
217 virtual void MacControlUserPaneActivateProc(bool activating) = 0 ;
218 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) = 0 ;
219 virtual void MacControlUserPaneBackgroundProc(void* info) = 0 ;
220 #endif
221 } ;
222
223 // common parts for implementations based on MLTE
224
225 class wxMacMLTEControl : public wxMacTextControl
226 {
227 public :
228 wxMacMLTEControl( wxTextCtrl *peer ) ;
229
230 virtual wxString GetStringValue() const ;
231 virtual void SetStringValue( const wxString &str ) ;
232
233 static TXNFrameOptions FrameOptionsFromWXStyle( long wxStyle ) ;
234
235 void AdjustCreationAttributes( const wxColour& background, bool visible ) ;
236
237 virtual void SetFont( const wxFont & font, const wxColour& foreground, long windowStyle ) ;
238 virtual void SetBackground( const wxBrush &brush ) ;
239 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
240 virtual void Copy() ;
241 virtual void Cut() ;
242 virtual void Paste() ;
243 virtual bool CanPaste() const ;
244 virtual void SetEditable( bool editable ) ;
245 virtual wxTextPos GetLastPosition() const ;
246 virtual void Replace( long from, long to, const wxString &str ) ;
247 virtual void Remove( long from, long to ) ;
248 virtual void GetSelection( long* from, long* to ) const ;
249 virtual void SetSelection( long from, long to ) ;
250
251 virtual void WriteText( const wxString& str ) ;
252
253 virtual bool HasOwnContextMenu() const
254 {
255 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
256 if ( UMAGetSystemVersion() >= 0x1040 )
257 {
258 TXNCommandEventSupportOptions options ;
259 TXNGetCommandEventSupport( m_txn , & options ) ;
260 return options & kTXNSupportEditCommandProcessing ;
261 }
262 #endif
263
264 return false ;
265 }
266
267 virtual void CheckSpelling(bool check)
268 {
269 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
270 TXNSetSpellCheckAsYouType( m_txn, (Boolean) check );
271 #endif
272 }
273 virtual void Clear() ;
274
275 virtual bool CanUndo() const ;
276 virtual void Undo() ;
277 virtual bool CanRedo() const;
278 virtual void Redo() ;
279 virtual int GetNumberOfLines() const ;
280 virtual long XYToPosition(long x, long y) const ;
281 virtual bool PositionToXY(long pos, long *x, long *y) const ;
282 virtual void ShowPosition( long pos ) ;
283 virtual int GetLineLength(long lineNo) const ;
284 virtual wxString GetLineText(long lineNo) const ;
285
286 void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
287 TXNObject GetTXNObject() { return m_txn ; }
288
289 protected :
290 void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
291
292 TXNObject m_txn ;
293 } ;
294
295 #if TARGET_API_MAC_OSX
296
297 // implementation available under OSX
298
299 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
300
301 class wxMacMLTEHIViewControl : public wxMacMLTEControl
302 {
303 public :
304 wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
305 const wxString& str,
306 const wxPoint& pos,
307 const wxSize& size, long style ) ;
308 virtual ~wxMacMLTEHIViewControl() ;
309
310 virtual OSStatus SetFocus( ControlFocusPart focusPart ) ;
311 virtual bool HasFocus() const ;
312 virtual void SetBackground( const wxBrush &brush) ;
313
314 protected :
315 HIViewRef m_scrollView ;
316 HIViewRef m_textView ;
317 EventHandlerRef m_textEventHandlerRef ;
318 };
319
320 #endif
321
322 class wxMacUnicodeTextControl : public wxMacTextControl
323 {
324 public :
325 wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
326 const wxString& str,
327 const wxPoint& pos,
328 const wxSize& size, long style ) ;
329 virtual ~wxMacUnicodeTextControl();
330
331 virtual void VisibilityChanged(bool shown);
332 virtual wxString GetStringValue() const ;
333 virtual void SetStringValue( const wxString &str) ;
334 virtual void Copy();
335 virtual void Cut();
336 virtual void Paste();
337 virtual bool CanPaste() const;
338 virtual void SetEditable(bool editable) ;
339 virtual void GetSelection( long* from, long* to) const ;
340 virtual void SetSelection( long from , long to ) ;
341 virtual void WriteText(const wxString& str) ;
342
343 protected :
344 // contains the tag for the content (is different for password and non-password controls)
345 OSType m_valueTag ;
346
347 // as the selection tag only works correctly when the control has the focus we have to mirror the
348 // intended value
349 EventHandlerRef m_focusHandlerRef ;
350 public :
351 ControlEditTextSelectionRec m_selection ;
352 };
353
354 #endif
355
356 // 'classic' MLTE implementation
357
358 class wxMacMLTEClassicControl : public wxMacMLTEControl
359 {
360 public :
361 wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
362 const wxString& str,
363 const wxPoint& pos,
364 const wxSize& size, long style ) ;
365 virtual ~wxMacMLTEClassicControl() ;
366
367 virtual void VisibilityChanged(bool shown) ;
368 virtual void SuperChangedPosition() ;
369
370 virtual void MacControlUserPaneDrawProc(wxInt16 part) ;
371 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) ;
372 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) ;
373 virtual void MacControlUserPaneIdleProc() ;
374 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) ;
375 virtual void MacControlUserPaneActivateProc(bool activating) ;
376 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) ;
377 virtual void MacControlUserPaneBackgroundProc(void* info) ;
378
379 virtual bool SetupCursor( const wxPoint& WXUNUSED(pt) )
380 {
381 MacControlUserPaneIdleProc();
382 return true;
383 }
384
385 virtual void SetRect( Rect *r ) ;
386
387 protected :
388 OSStatus DoCreate();
389
390 void MacUpdatePosition() ;
391 void MacActivatePaneText(bool setActive) ;
392 void MacFocusPaneText(bool setFocus) ;
393 void MacSetObjectVisibility(bool vis) ;
394
395 private :
396 TXNFrameID m_txnFrameID ;
397 GrafPtr m_txnPort ;
398 WindowRef m_txnWindow ;
399 // bounds of the control as we last did set the txn frames
400 Rect m_txnControlBounds ;
401 Rect m_txnVisBounds ;
402
403 #ifdef __WXMAC_OSX__
404 static pascal void TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode ) ;
405 static pascal void TXNScrollInfoProc(
406 SInt32 iValue, SInt32 iMaximumValue,
407 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon ) ;
408
409 ControlRef m_sbHorizontal ;
410 SInt32 m_lastHorizontalValue ;
411 ControlRef m_sbVertical ;
412 SInt32 m_lastVerticalValue ;
413 #endif
414 };
415
416
417 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
418
419 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
420 EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground )
421 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
422 EVT_CHAR(wxTextCtrl::OnChar)
423 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
424 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
425 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
426 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
427 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
428 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
429 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
430
431 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu)
432
433 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
434 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
435 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
436 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
437 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
438 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
439 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
440 END_EVENT_TABLE()
441
442
443 void wxTextCtrl::Init()
444 {
445 m_editable = true ;
446 m_dirty = false;
447
448 m_maxLength = 0;
449 m_privateContextMenu = NULL;
450 m_triggerOnSetValue = true ;
451 }
452
453 wxTextCtrl::~wxTextCtrl()
454 {
455 delete m_privateContextMenu;
456 }
457
458 bool wxTextCtrl::Create( wxWindow *parent,
459 wxWindowID id,
460 const wxString& str,
461 const wxPoint& pos,
462 const wxSize& size,
463 long style,
464 const wxValidator& validator,
465 const wxString& name )
466 {
467 m_macIsUserPane = false ;
468 m_editable = true ;
469
470 if ( ! (style & wxNO_BORDER) )
471 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
472
473 if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
474 return false;
475
476 if ( m_windowStyle & wxTE_MULTILINE )
477 {
478 // always turn on this style for multi-line controls
479 m_windowStyle |= wxTE_PROCESS_ENTER;
480 style |= wxTE_PROCESS_ENTER ;
481 }
482
483 bool forceMLTE = false ;
484
485 #if wxUSE_SYSTEM_OPTIONS
486 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE ) == 1))
487 {
488 forceMLTE = true ;
489 }
490 #endif
491 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
492 forceMLTE = false;
493 #endif
494
495 #ifdef __WXMAC_OSX__
496 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
497 if ( UMAGetSystemVersion() >= 0x1030 && !forceMLTE )
498 {
499 if ( m_windowStyle & wxTE_MULTILINE )
500 m_peer = new wxMacMLTEHIViewControl( this , str , pos , size , style ) ;
501 }
502 #endif
503
504 if ( !m_peer )
505 {
506 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
507 if ( !(m_windowStyle & wxTE_MULTILINE) && !forceMLTE )
508 #endif
509 {
510 m_peer = new wxMacUnicodeTextControl( this , str , pos , size , style ) ;
511 }
512 }
513 #endif
514
515 // the horizontal single line scrolling bug that made us keep
516 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
517 if ( !m_peer )
518 m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ;
519 #endif
520
521 MacPostControlCreate(pos, size) ;
522
523 // only now the embedding is correct and we can do a positioning update
524
525 MacSuperChangedPosition() ;
526
527 if ( m_windowStyle & wxTE_READONLY)
528 SetEditable( false ) ;
529
530 SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ;
531
532 return true;
533 }
534
535 void wxTextCtrl::MacSuperChangedPosition()
536 {
537 wxWindow::MacSuperChangedPosition() ;
538 GetPeer()->SuperChangedPosition() ;
539 }
540
541 void wxTextCtrl::MacVisibilityChanged()
542 {
543 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
544 }
545
546 void wxTextCtrl::MacEnabledStateChanged()
547 {
548 }
549
550 void wxTextCtrl::MacCheckSpelling(bool check)
551 {
552 GetPeer()->CheckSpelling(check);
553 }
554
555 wxString wxTextCtrl::GetValue() const
556 {
557 return GetPeer()->GetStringValue() ;
558 }
559
560 void wxTextCtrl::GetSelection(long* from, long* to) const
561 {
562 GetPeer()->GetSelection( from , to ) ;
563 }
564
565 void wxTextCtrl::DoSetValue(const wxString& str, int flags)
566 {
567 // optimize redraws
568 if ( GetValue() == str )
569 return;
570
571 GetPeer()->SetStringValue( str ) ;
572
573 if ( (flags & SetValue_SendEvent) && m_triggerOnSetValue )
574 {
575 SendTextUpdatedEvent();
576 }
577 }
578
579 void wxTextCtrl::SetMaxLength(unsigned long len)
580 {
581 m_maxLength = len ;
582 }
583
584 bool wxTextCtrl::SetFont( const wxFont& font )
585 {
586 if ( !wxTextCtrlBase::SetFont( font ) )
587 return false ;
588
589 GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle() ) ;
590
591 return true ;
592 }
593
594 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
595 {
596 GetPeer()->SetStyle( start , end , style ) ;
597
598 return true ;
599 }
600
601 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
602 {
603 wxTextCtrlBase::SetDefaultStyle( style ) ;
604 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
605
606 return true ;
607 }
608
609 // Clipboard operations
610
611 void wxTextCtrl::Copy()
612 {
613 if (CanCopy())
614 GetPeer()->Copy() ;
615 }
616
617 void wxTextCtrl::Cut()
618 {
619 if (CanCut())
620 {
621 GetPeer()->Cut() ;
622
623 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
624 event.SetEventObject( this );
625 GetEventHandler()->ProcessEvent( event );
626 }
627 }
628
629 void wxTextCtrl::Paste()
630 {
631 if (CanPaste())
632 {
633 GetPeer()->Paste() ;
634
635 // TODO: eventually we should add setting the default style again
636
637 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
638 event.SetEventObject( this );
639 GetEventHandler()->ProcessEvent( event );
640 }
641 }
642
643 bool wxTextCtrl::CanCopy() const
644 {
645 // Can copy if there's a selection
646 long from, to;
647 GetSelection( &from, &to );
648
649 return (from != to);
650 }
651
652 bool wxTextCtrl::CanCut() const
653 {
654 if ( !IsEditable() )
655 return false;
656
657 // Can cut if there's a selection
658 long from, to;
659 GetSelection( &from, &to );
660
661 return (from != to);
662 }
663
664 bool wxTextCtrl::CanPaste() const
665 {
666 if (!IsEditable())
667 return false;
668
669 return GetPeer()->CanPaste() ;
670 }
671
672 void wxTextCtrl::SetEditable(bool editable)
673 {
674 if ( editable != m_editable )
675 {
676 m_editable = editable ;
677 GetPeer()->SetEditable( editable ) ;
678 }
679 }
680
681 void wxTextCtrl::SetInsertionPoint(long pos)
682 {
683 SetSelection( pos , pos ) ;
684 }
685
686 void wxTextCtrl::SetInsertionPointEnd()
687 {
688 wxTextPos pos = GetLastPosition();
689 SetInsertionPoint( pos );
690 }
691
692 long wxTextCtrl::GetInsertionPoint() const
693 {
694 long begin, end ;
695 GetSelection( &begin , &end ) ;
696
697 return begin ;
698 }
699
700 wxTextPos wxTextCtrl::GetLastPosition() const
701 {
702 return GetPeer()->GetLastPosition() ;
703 }
704
705 void wxTextCtrl::Replace(long from, long to, const wxString& str)
706 {
707 GetPeer()->Replace( from , to , str ) ;
708 }
709
710 void wxTextCtrl::Remove(long from, long to)
711 {
712 GetPeer()->Remove( from , to ) ;
713 }
714
715 void wxTextCtrl::SetSelection(long from, long to)
716 {
717 GetPeer()->SetSelection( from , to ) ;
718 }
719
720 void wxTextCtrl::WriteText(const wxString& str)
721 {
722 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
723 if ( !wxIsMainThread() )
724 {
725 // unfortunately CW 8 is not able to correctly deduce the template types,
726 // so we have to instantiate explicitly
727 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( this , &wxTextCtrl::WriteText , str ) ;
728
729 return ;
730 }
731
732 GetPeer()->WriteText( str ) ;
733 }
734
735 void wxTextCtrl::AppendText(const wxString& text)
736 {
737 SetInsertionPointEnd();
738 WriteText( text );
739 }
740
741 void wxTextCtrl::Clear()
742 {
743 GetPeer()->Clear() ;
744 }
745
746 bool wxTextCtrl::IsModified() const
747 {
748 return m_dirty;
749 }
750
751 bool wxTextCtrl::IsEditable() const
752 {
753 return IsEnabled() && m_editable ;
754 }
755
756 bool wxTextCtrl::AcceptsFocus() const
757 {
758 // we don't want focus if we can't be edited
759 return /*IsEditable() && */ wxControl::AcceptsFocus();
760 }
761
762 wxSize wxTextCtrl::DoGetBestSize() const
763 {
764 int wText, hText;
765
766 // these are the numbers from the HIG:
767 // we reduce them by the borders first
768 wText = 100 ;
769
770 switch ( m_windowVariant )
771 {
772 case wxWINDOW_VARIANT_NORMAL :
773 hText = 22 - 6 ;
774 break ;
775
776 case wxWINDOW_VARIANT_SMALL :
777 hText = 19 - 6 ;
778 break ;
779
780 case wxWINDOW_VARIANT_MINI :
781 hText = 15 - 6 ;
782 break ;
783
784 default :
785 hText = 22 - 6;
786 break ;
787 }
788
789 // as the above numbers have some free space around the text
790 // we get 5 lines like this anyway
791 if ( m_windowStyle & wxTE_MULTILINE )
792 hText *= 5 ;
793
794 if ( !HasFlag(wxNO_BORDER) )
795 hText += 6 ;
796
797 return wxSize(wText, hText);
798 }
799
800 // ----------------------------------------------------------------------------
801 // Undo/redo
802 // ----------------------------------------------------------------------------
803
804 void wxTextCtrl::Undo()
805 {
806 if (CanUndo())
807 GetPeer()->Undo() ;
808 }
809
810 void wxTextCtrl::Redo()
811 {
812 if (CanRedo())
813 GetPeer()->Redo() ;
814 }
815
816 bool wxTextCtrl::CanUndo() const
817 {
818 if ( !IsEditable() )
819 return false ;
820
821 return GetPeer()->CanUndo() ;
822 }
823
824 bool wxTextCtrl::CanRedo() const
825 {
826 if ( !IsEditable() )
827 return false ;
828
829 return GetPeer()->CanRedo() ;
830 }
831
832 void wxTextCtrl::MarkDirty()
833 {
834 m_dirty = true;
835 }
836
837 void wxTextCtrl::DiscardEdits()
838 {
839 m_dirty = false;
840 }
841
842 int wxTextCtrl::GetNumberOfLines() const
843 {
844 return GetPeer()->GetNumberOfLines() ;
845 }
846
847 long wxTextCtrl::XYToPosition(long x, long y) const
848 {
849 return GetPeer()->XYToPosition( x , y ) ;
850 }
851
852 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
853 {
854 return GetPeer()->PositionToXY( pos , x , y ) ;
855 }
856
857 void wxTextCtrl::ShowPosition(long pos)
858 {
859 return GetPeer()->ShowPosition(pos) ;
860 }
861
862 int wxTextCtrl::GetLineLength(long lineNo) const
863 {
864 return GetPeer()->GetLineLength(lineNo) ;
865 }
866
867 wxString wxTextCtrl::GetLineText(long lineNo) const
868 {
869 return GetPeer()->GetLineText(lineNo) ;
870 }
871
872 void wxTextCtrl::Command(wxCommandEvent & event)
873 {
874 SetValue(event.GetString());
875 ProcessCommand(event);
876 }
877
878 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
879 {
880 // By default, load the first file into the text window.
881 if (event.GetNumberOfFiles() > 0)
882 LoadFile( event.GetFiles()[0] );
883 }
884
885 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
886 {
887 // all erasing should be done by the real mac control implementation
888 // while this is true for MLTE under classic, the HITextView is somehow
889 // transparent but background erase is not working correctly, so intercept
890 // things while we can...
891 event.Skip() ;
892 }
893
894 void wxTextCtrl::OnChar(wxKeyEvent& event)
895 {
896 int key = event.GetKeyCode() ;
897 bool eat_key = false ;
898
899 if ( key == 'a' && event.MetaDown() )
900 {
901 SelectAll() ;
902
903 return ;
904 }
905
906 if ( key == 'c' && event.MetaDown() )
907 {
908 if ( CanCopy() )
909 Copy() ;
910
911 return ;
912 }
913
914 if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
915 !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
916 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
917 )
918 {
919 // eat it
920 return ;
921 }
922
923 // Check if we have reached the max # of chars (if it is set), but still
924 // allow navigation and deletion
925 if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
926 key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB &&
927 key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) )
928 )
929 {
930 // eat it, we don't want to add more than allowed # of characters
931
932 // TODO: generate EVT_TEXT_MAXLEN()
933 return;
934 }
935
936 // assume that any key not processed yet is going to modify the control
937 m_dirty = true;
938
939 if ( key == 'v' && event.MetaDown() )
940 {
941 if ( CanPaste() )
942 Paste() ;
943
944 return ;
945 }
946
947 if ( key == 'x' && event.MetaDown() )
948 {
949 if ( CanCut() )
950 Cut() ;
951
952 return ;
953 }
954
955 switch ( key )
956 {
957 case WXK_RETURN:
958 if (m_windowStyle & wxTE_PROCESS_ENTER)
959 {
960 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
961 event.SetEventObject( this );
962 event.SetString( GetValue() );
963 if ( GetEventHandler()->ProcessEvent(event) )
964 return;
965 }
966
967 if ( !(m_windowStyle & wxTE_MULTILINE) )
968 {
969 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
970 if ( tlw && tlw->GetDefaultItem() )
971 {
972 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
973 if ( def && def->IsEnabled() )
974 {
975 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
976 event.SetEventObject(def);
977 def->Command(event);
978
979 return ;
980 }
981 }
982
983 // this will make wxWidgets eat the ENTER key so that
984 // we actually prevent line wrapping in a single line text control
985 eat_key = true;
986 }
987 break;
988
989 case WXK_TAB:
990 if ( !(m_windowStyle & wxTE_PROCESS_TAB))
991 {
992 int flags = 0;
993 if (!event.ShiftDown())
994 flags |= wxNavigationKeyEvent::IsForward ;
995 if (event.ControlDown())
996 flags |= wxNavigationKeyEvent::WinChange ;
997 Navigate(flags);
998
999 return;
1000 }
1001 else
1002 {
1003 // This is necessary (don't know why);
1004 // otherwise the tab will not be inserted.
1005 WriteText(wxT("\t"));
1006 eat_key = true;
1007 }
1008 break;
1009
1010 default:
1011 break;
1012 }
1013
1014 if (!eat_key)
1015 {
1016 // perform keystroke handling
1017 event.Skip(true) ;
1018 }
1019
1020 if ( ( key >= 0x20 && key < WXK_START ) ||
1021 ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
1022 key == WXK_RETURN ||
1023 key == WXK_DELETE ||
1024 key == WXK_BACK)
1025 {
1026 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1027 event1.SetEventObject( this );
1028 wxPostEvent( GetEventHandler(), event1 );
1029 }
1030 }
1031
1032 // ----------------------------------------------------------------------------
1033 // standard handlers for standard edit menu events
1034 // ----------------------------------------------------------------------------
1035
1036 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1037 {
1038 Cut();
1039 }
1040
1041 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1042 {
1043 Copy();
1044 }
1045
1046 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1047 {
1048 Paste();
1049 }
1050
1051 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1052 {
1053 Undo();
1054 }
1055
1056 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1057 {
1058 Redo();
1059 }
1060
1061 void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
1062 {
1063 long from, to;
1064
1065 GetSelection( &from, &to );
1066 if (from != -1 && to != -1)
1067 Remove( from, to );
1068 }
1069
1070 void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1071 {
1072 SetSelection(-1, -1);
1073 }
1074
1075 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1076 {
1077 event.Enable( CanCut() );
1078 }
1079
1080 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1081 {
1082 event.Enable( CanCopy() );
1083 }
1084
1085 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1086 {
1087 event.Enable( CanPaste() );
1088 }
1089
1090 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1091 {
1092 event.Enable( CanUndo() );
1093 }
1094
1095 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1096 {
1097 event.Enable( CanRedo() );
1098 }
1099
1100 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
1101 {
1102 long from, to;
1103
1104 GetSelection( &from, &to );
1105 event.Enable( from != -1 && to != -1 && from != to && IsEditable() ) ;
1106 }
1107
1108 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
1109 {
1110 event.Enable(GetLastPosition() > 0);
1111 }
1112
1113 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1114
1115 void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
1116 {
1117 if ( GetPeer()->HasOwnContextMenu() )
1118 {
1119 event.Skip() ;
1120 return ;
1121 }
1122
1123 if (m_privateContextMenu == NULL)
1124 {
1125 m_privateContextMenu = new wxMenu;
1126 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
1127 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
1128 m_privateContextMenu->AppendSeparator();
1129 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
1130 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
1131 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
1132 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
1133 m_privateContextMenu->AppendSeparator();
1134 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
1135 }
1136
1137 if (m_privateContextMenu != NULL)
1138 PopupMenu(m_privateContextMenu);
1139 }
1140
1141 bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
1142 {
1143 if ( !GetPeer()->SetupCursor( pt ) )
1144 return wxWindow::MacSetupCursor( pt ) ;
1145 else
1146 return true ;
1147 }
1148
1149 #if !TARGET_API_MAC_OSX
1150
1151 // user pane implementation
1152
1153 void wxTextCtrl::MacControlUserPaneDrawProc(wxInt16 part)
1154 {
1155 GetPeer()->MacControlUserPaneDrawProc( part ) ;
1156 }
1157
1158 wxInt16 wxTextCtrl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
1159 {
1160 return GetPeer()->MacControlUserPaneHitTestProc( x , y ) ;
1161 }
1162
1163 wxInt16 wxTextCtrl::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
1164 {
1165 return GetPeer()->MacControlUserPaneTrackingProc( x , y , actionProc ) ;
1166 }
1167
1168 void wxTextCtrl::MacControlUserPaneIdleProc()
1169 {
1170 GetPeer()->MacControlUserPaneIdleProc( ) ;
1171 }
1172
1173 wxInt16 wxTextCtrl::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
1174 {
1175 return GetPeer()->MacControlUserPaneKeyDownProc( keyCode , charCode , modifiers ) ;
1176 }
1177
1178 void wxTextCtrl::MacControlUserPaneActivateProc(bool activating)
1179 {
1180 GetPeer()->MacControlUserPaneActivateProc( activating ) ;
1181 }
1182
1183 wxInt16 wxTextCtrl::MacControlUserPaneFocusProc(wxInt16 action)
1184 {
1185 return GetPeer()->MacControlUserPaneFocusProc( action ) ;
1186 }
1187
1188 void wxTextCtrl::MacControlUserPaneBackgroundProc(void* info)
1189 {
1190 GetPeer()->MacControlUserPaneBackgroundProc( info ) ;
1191 }
1192
1193 #endif
1194
1195 // ----------------------------------------------------------------------------
1196 // implementation base class
1197 // ----------------------------------------------------------------------------
1198
1199 wxMacTextControl::wxMacTextControl(wxTextCtrl* peer) :
1200 wxMacControl( peer )
1201 {
1202 }
1203
1204 wxMacTextControl::~wxMacTextControl()
1205 {
1206 }
1207
1208 void wxMacTextControl::SetStyle(long start, long end, const wxTextAttr& style)
1209 {
1210 }
1211
1212 void wxMacTextControl::Copy()
1213 {
1214 }
1215
1216 void wxMacTextControl::Cut()
1217 {
1218 }
1219
1220 void wxMacTextControl::Paste()
1221 {
1222 }
1223
1224 bool wxMacTextControl::CanPaste() const
1225 {
1226 return false ;
1227 }
1228
1229 void wxMacTextControl::SetEditable(bool editable)
1230 {
1231 }
1232
1233 wxTextPos wxMacTextControl::GetLastPosition() const
1234 {
1235 return GetStringValue().length() ;
1236 }
1237
1238 void wxMacTextControl::Replace( long from , long to , const wxString &val )
1239 {
1240 SetSelection( from , to ) ;
1241 WriteText( val ) ;
1242 }
1243
1244 void wxMacTextControl::Remove( long from , long to )
1245 {
1246 SetSelection( from , to ) ;
1247 WriteText( wxEmptyString) ;
1248 }
1249
1250 void wxMacTextControl::Clear()
1251 {
1252 SetStringValue( wxEmptyString ) ;
1253 }
1254
1255 bool wxMacTextControl::CanUndo() const
1256 {
1257 return false ;
1258 }
1259
1260 void wxMacTextControl::Undo()
1261 {
1262 }
1263
1264 bool wxMacTextControl::CanRedo() const
1265 {
1266 return false ;
1267 }
1268
1269 void wxMacTextControl::Redo()
1270 {
1271 }
1272
1273 long wxMacTextControl::XYToPosition(long x, long y) const
1274 {
1275 return 0 ;
1276 }
1277
1278 bool wxMacTextControl::PositionToXY(long pos, long *x, long *y) const
1279 {
1280 return false ;
1281 }
1282
1283 void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
1284 {
1285 }
1286
1287 int wxMacTextControl::GetNumberOfLines() const
1288 {
1289 ItemCount lines = 0 ;
1290 wxString content = GetStringValue() ;
1291 lines = 1;
1292
1293 for (size_t i = 0; i < content.length() ; i++)
1294 {
1295 if (content[i] == '\r')
1296 lines++;
1297 }
1298
1299 return lines ;
1300 }
1301
1302 wxString wxMacTextControl::GetLineText(long lineNo) const
1303 {
1304 // TODO: change this if possible to reflect real lines
1305 wxString content = GetStringValue() ;
1306
1307 // Find line first
1308 int count = 0;
1309 for (size_t i = 0; i < content.length() ; i++)
1310 {
1311 if (count == lineNo)
1312 {
1313 // Add chars in line then
1314 wxString tmp;
1315
1316 for (size_t j = i; j < content.length(); j++)
1317 {
1318 if (content[j] == '\n')
1319 return tmp;
1320
1321 tmp += content[j];
1322 }
1323
1324 return tmp;
1325 }
1326
1327 if (content[i] == '\n')
1328 count++;
1329 }
1330
1331 return wxEmptyString ;
1332 }
1333
1334 int wxMacTextControl::GetLineLength(long lineNo) const
1335 {
1336 // TODO: change this if possible to reflect real lines
1337 wxString content = GetStringValue() ;
1338
1339 // Find line first
1340 int count = 0;
1341 for (size_t i = 0; i < content.length() ; i++)
1342 {
1343 if (count == lineNo)
1344 {
1345 // Count chars in line then
1346 count = 0;
1347 for (size_t j = i; j < content.length(); j++)
1348 {
1349 count++;
1350 if (content[j] == '\n')
1351 return count;
1352 }
1353
1354 return count;
1355 }
1356
1357 if (content[i] == '\n')
1358 count++;
1359 }
1360
1361 return 0 ;
1362 }
1363
1364 // ----------------------------------------------------------------------------
1365 // standard unicode control implementation
1366 // ----------------------------------------------------------------------------
1367
1368 #if TARGET_API_MAC_OSX
1369
1370 // the current unicode textcontrol implementation has a bug : only if the control
1371 // is currently having the focus, the selection can be retrieved by the corresponding
1372 // data tag. So we have a mirroring using a member variable
1373 // TODO : build event table using virtual member functions for wxMacControl
1374
1375 static const EventTypeSpec unicodeTextControlEventList[] =
1376 {
1377 { kEventClassControl , kEventControlSetFocusPart } ,
1378 } ;
1379
1380 static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1381 {
1382 OSStatus result = eventNotHandledErr ;
1383 wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ;
1384 wxMacCarbonEvent cEvent( event ) ;
1385
1386 switch ( GetEventKind( event ) )
1387 {
1388 case kEventControlSetFocusPart :
1389 {
1390 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
1391 if ( controlPart == kControlFocusNoPart )
1392 {
1393 // about to loose focus -> store selection to field
1394 focus->GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1395 }
1396 result = CallNextEventHandler(handler,event) ;
1397 if ( controlPart != kControlFocusNoPart )
1398 {
1399 // about to gain focus -> set selection from field
1400 focus->SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1401 }
1402 break;
1403 }
1404 default:
1405 break ;
1406 }
1407
1408 return result ;
1409 }
1410
1411 static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1412 {
1413 OSStatus result = eventNotHandledErr ;
1414
1415 switch ( GetEventClass( event ) )
1416 {
1417 case kEventClassControl :
1418 result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ;
1419 break ;
1420
1421 default :
1422 break ;
1423 }
1424 return result ;
1425 }
1426
1427 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler )
1428
1429 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
1430 const wxString& str,
1431 const wxPoint& pos,
1432 const wxSize& size, long style )
1433 : wxMacTextControl( wxPeer )
1434 {
1435 m_font = wxPeer->GetFont() ;
1436 m_windowStyle = style ;
1437 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1438 wxString st = str ;
1439 wxMacConvertNewlines10To13( &st ) ;
1440 wxMacCFStringHolder cf(st , m_font.GetEncoding()) ;
1441 CFStringRef cfr = cf ;
1442 Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
1443 m_valueTag = isPassword ? kControlEditTextPasswordCFStringTag : kControlEditTextCFStringTag ;
1444
1445 OSStatus err = CreateEditUnicodeTextControl(
1446 MAC_WXHWND(wxPeer->MacGetTopLevelWindowRef()), &bounds , cfr ,
1447 isPassword , NULL , &m_controlRef ) ;
1448 verify_noerr( err );
1449
1450 if ( !(m_windowStyle & wxTE_MULTILINE) )
1451 SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
1452
1453 InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(),
1454 GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
1455 &m_focusHandlerRef);
1456 }
1457
1458 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1459 {
1460 ::RemoveEventHandler( m_focusHandlerRef );
1461 }
1462
1463 void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
1464 {
1465 if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
1466 {
1467 // work around a refresh issue insofar as not always the entire content is shown,
1468 // even if this would be possible
1469 ControlEditTextSelectionRec sel ;
1470 CFStringRef value = NULL ;
1471
1472 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1473 verify_noerr( GetData<CFStringRef>( 0, m_valueTag, &value ) );
1474 verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
1475 verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1476
1477 CFRelease( value ) ;
1478 }
1479 }
1480
1481 wxString wxMacUnicodeTextControl::GetStringValue() const
1482 {
1483 wxString result ;
1484 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1485 if ( value )
1486 {
1487 wxMacCFStringHolder cf(value) ;
1488 result = cf.AsString() ;
1489 }
1490
1491 #if '\n' == 10
1492 wxMacConvertNewlines13To10( &result ) ;
1493 #else
1494 wxMacConvertNewlines10To13( &result ) ;
1495 #endif
1496
1497 return result ;
1498 }
1499
1500 void wxMacUnicodeTextControl::SetStringValue( const wxString &str )
1501 {
1502 wxString st = str ;
1503 wxMacConvertNewlines10To13( &st ) ;
1504 wxMacCFStringHolder cf( st , m_font.GetEncoding() ) ;
1505 verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
1506 }
1507
1508 void wxMacUnicodeTextControl::Copy()
1509 {
1510 SendHICommand( kHICommandCopy ) ;
1511 }
1512
1513 void wxMacUnicodeTextControl::Cut()
1514 {
1515 SendHICommand( kHICommandCut ) ;
1516 }
1517
1518 void wxMacUnicodeTextControl::Paste()
1519 {
1520 SendHICommand( kHICommandPaste ) ;
1521 }
1522
1523 bool wxMacUnicodeTextControl::CanPaste() const
1524 {
1525 return true ;
1526 }
1527
1528 void wxMacUnicodeTextControl::SetEditable(bool editable)
1529 {
1530 #if 0 // leads to problem because text cannot be selected anymore
1531 SetData<Boolean>( kControlEditTextPart , kControlEditTextLockedTag , (Boolean) !editable ) ;
1532 #endif
1533 }
1534
1535 void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const
1536 {
1537 ControlEditTextSelectionRec sel ;
1538 if (HasFocus())
1539 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
1540 else
1541 sel = m_selection ;
1542
1543 if ( from )
1544 *from = sel.selStart ;
1545 if ( to )
1546 *to = sel.selEnd ;
1547 }
1548
1549 void wxMacUnicodeTextControl::SetSelection( long from , long to )
1550 {
1551 ControlEditTextSelectionRec sel ;
1552 wxString result ;
1553 int textLength = 0 ;
1554 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1555 if ( value )
1556 {
1557 wxMacCFStringHolder cf(value) ;
1558 textLength = cf.AsString().length() ;
1559 }
1560
1561 if ((from == -1) && (to == -1))
1562 {
1563 from = 0 ;
1564 to = textLength ;
1565 }
1566 else
1567 {
1568 from = wxMin(textLength,wxMax(from,0)) ;
1569 to = wxMax(0,wxMin(textLength,to)) ;
1570 }
1571
1572 sel.selStart = from ;
1573 sel.selEnd = to ;
1574 if ( HasFocus() )
1575 SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
1576 else
1577 m_selection = sel;
1578 }
1579
1580 void wxMacUnicodeTextControl::WriteText( const wxString& str )
1581 {
1582 wxString st = str ;
1583 wxMacConvertNewlines10To13( &st ) ;
1584
1585 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1586 if ( HasFocus() )
1587 {
1588 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
1589 CFStringRef value = cf ;
1590 SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
1591 }
1592 else
1593 #endif
1594 {
1595 wxString val = GetStringValue() ;
1596 long start , end ;
1597 GetSelection( &start , &end ) ;
1598 val.Remove( start , end - start ) ;
1599 val.insert( start , str ) ;
1600 SetStringValue( val ) ;
1601 SetSelection( start + str.length() , start + str.length() ) ;
1602 }
1603 }
1604
1605 #endif
1606
1607 // ----------------------------------------------------------------------------
1608 // MLTE control implementation (common part)
1609 // ----------------------------------------------------------------------------
1610
1611 // if MTLE is read only, no changes at all are allowed, not even from
1612 // procedural API, in order to allow changes via API all the same we must undo
1613 // the readonly status while we are executing, this class helps to do so
1614
1615 class wxMacEditHelper
1616 {
1617 public :
1618 wxMacEditHelper( TXNObject txn )
1619 {
1620 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1621 m_txn = txn ;
1622 TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
1623 if ( m_data[0].uValue == kTXNReadOnly )
1624 {
1625 TXNControlData data[] = { { kTXNReadWrite } } ;
1626 TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
1627 }
1628 }
1629
1630 ~wxMacEditHelper()
1631 {
1632 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1633 if ( m_data[0].uValue == kTXNReadOnly )
1634 TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
1635 }
1636
1637 protected :
1638 TXNObject m_txn ;
1639 TXNControlData m_data[1] ;
1640 } ;
1641
1642 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer )
1643 : wxMacTextControl( peer )
1644 {
1645 SetNeedsFocusRect( true ) ;
1646 }
1647
1648 wxString wxMacMLTEControl::GetStringValue() const
1649 {
1650 wxString result ;
1651 OSStatus err ;
1652 Size actualSize = 0;
1653
1654 {
1655 #if wxUSE_UNICODE
1656 Handle theText ;
1657 err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNUnicodeTextData );
1658
1659 // all done
1660 if ( err != noErr )
1661 {
1662 actualSize = 0 ;
1663 }
1664 else
1665 {
1666 actualSize = GetHandleSize( theText ) / sizeof(UniChar) ;
1667 if ( actualSize > 0 )
1668 {
1669 wxChar *ptr = NULL ;
1670
1671 #if SIZEOF_WCHAR_T == 2
1672 ptr = new wxChar[actualSize + 1] ;
1673 wxStrncpy( ptr , (wxChar*)(*theText) , actualSize ) ;
1674 #else
1675 SetHandleSize( theText, (actualSize + 1) * sizeof(UniChar) ) ;
1676 HLock( theText ) ;
1677 (((UniChar*)*theText)[actualSize]) = 0 ;
1678 wxMBConvUTF16 converter ;
1679 size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
1680 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") );
1681 ptr = new wxChar[noChars + 1] ;
1682
1683 noChars = converter.MB2WC( ptr , (const char*)*theText , noChars + 1 ) ;
1684 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") );
1685 ptr[noChars] = 0 ;
1686 HUnlock( theText ) ;
1687 #endif
1688
1689 ptr[actualSize] = 0 ;
1690 result = wxString( ptr ) ;
1691 delete [] ptr ;
1692 }
1693
1694 DisposeHandle( theText ) ;
1695 }
1696 #else
1697 Handle theText ;
1698 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1699
1700 // all done
1701 if ( err != noErr )
1702 {
1703 actualSize = 0 ;
1704 }
1705 else
1706 {
1707 actualSize = GetHandleSize( theText ) ;
1708 if ( actualSize > 0 )
1709 {
1710 HLock( theText ) ;
1711 result = wxString( *theText , wxConvLocal , actualSize ) ;
1712 HUnlock( theText ) ;
1713 }
1714
1715 DisposeHandle( theText ) ;
1716 }
1717 #endif
1718 }
1719
1720 #if '\n' == 10
1721 wxMacConvertNewlines13To10( &result ) ;
1722 #else
1723 wxMacConvertNewlines10To13( &result ) ;
1724 #endif
1725
1726 return result ;
1727 }
1728
1729 void wxMacMLTEControl::SetStringValue( const wxString &str )
1730 {
1731 wxString st = str;
1732 wxMacConvertNewlines10To13( &st );
1733
1734 {
1735 #ifndef __LP64__
1736 wxMacWindowClipper c( m_peer ) ;
1737 #endif
1738
1739 {
1740 wxMacEditHelper help( m_txn );
1741 SetTXNData( st, kTXNStartOffset, kTXNEndOffset );
1742 }
1743
1744 TXNSetSelection( m_txn, 0, 0 );
1745 TXNShowSelection( m_txn, kTXNShowStart );
1746 }
1747 }
1748
1749 TXNFrameOptions wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle )
1750 {
1751 TXNFrameOptions frameOptions = kTXNDontDrawCaretWhenInactiveMask;
1752
1753 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1754 frameOptions |= kTXNDoFontSubstitutionMask;
1755 #endif
1756
1757 if ( ! (wxStyle & wxTE_NOHIDESEL) )
1758 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
1759
1760 if ( wxStyle & (wxHSCROLL | wxTE_DONTWRAP) )
1761 frameOptions |= kTXNWantHScrollBarMask ;
1762
1763 if ( wxStyle & wxTE_MULTILINE )
1764 {
1765 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
1766
1767 if ( !(wxStyle & wxTE_NO_VSCROLL) )
1768 {
1769 frameOptions |= kTXNWantVScrollBarMask ;
1770
1771 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1772 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1773 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1774 // if ( frameOptions & kTXNWantHScrollBarMask )
1775 // frameOptions |= kTXNDrawGrowIconMask ;
1776 }
1777 }
1778 else
1779 {
1780 frameOptions |= kTXNSingleLineOnlyMask ;
1781 }
1782
1783 return frameOptions ;
1784 }
1785
1786 void wxMacMLTEControl::AdjustCreationAttributes( const wxColour &background, bool visible )
1787 {
1788 TXNControlTag iControlTags[] =
1789 {
1790 kTXNDoFontSubstitution,
1791 kTXNWordWrapStateTag ,
1792 };
1793 TXNControlData iControlData[] =
1794 {
1795 { true },
1796 { kTXNNoAutoWrap },
1797 };
1798
1799 int toptag = WXSIZEOF( iControlTags ) ;
1800
1801 if ( m_windowStyle & wxTE_MULTILINE )
1802 {
1803 iControlData[1].uValue =
1804 (m_windowStyle & wxTE_DONTWRAP)
1805 ? kTXNNoAutoWrap
1806 : kTXNAutoWrap;
1807 }
1808
1809 OSStatus err = TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) ;
1810 verify_noerr( err );
1811
1812 // setting the default font:
1813 // under 10.2 this causes a visible caret, therefore we avoid it
1814
1815 if ( UMAGetSystemVersion() >= 0x1030 )
1816 {
1817 Str255 fontName ;
1818 SInt16 fontSize ;
1819 Style fontStyle ;
1820
1821 GetThemeFont( kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1822
1823 TXNTypeAttributes typeAttr[] =
1824 {
1825 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
1826 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
1827 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
1828 } ;
1829
1830 err = TXNSetTypeAttributes(
1831 m_txn, sizeof(typeAttr) / sizeof(TXNTypeAttributes),
1832 typeAttr, kTXNStartOffset, kTXNEndOffset );
1833 verify_noerr( err );
1834 }
1835
1836 if ( m_windowStyle & wxTE_PASSWORD )
1837 {
1838 UniChar c = 0x00A5 ;
1839 err = TXNEchoMode( m_txn , c , 0 , true );
1840 verify_noerr( err );
1841 }
1842
1843 TXNBackground tback;
1844 tback.bgType = kTXNBackgroundTypeRGB;
1845 tback.bg.color = MAC_WXCOLORREF( background.GetPixel() );
1846 TXNSetBackground( m_txn , &tback );
1847
1848 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1849 if ( UMAGetSystemVersion() >= 0x1040 )
1850 {
1851 TXNCommandEventSupportOptions options ;
1852 if ( TXNGetCommandEventSupport( m_txn, &options ) == noErr )
1853 {
1854 options |=
1855 kTXNSupportEditCommandProcessing
1856 | kTXNSupportEditCommandUpdating
1857 | kTXNSupportSpellCheckCommandProcessing
1858 | kTXNSupportSpellCheckCommandUpdating
1859 | kTXNSupportFontCommandProcessing
1860 | kTXNSupportFontCommandUpdating;
1861
1862 TXNSetCommandEventSupport( m_txn , options ) ;
1863 }
1864 }
1865 #endif
1866 }
1867
1868 void wxMacMLTEControl::SetBackground( const wxBrush &brush )
1869 {
1870 // currently only solid background are supported
1871 TXNBackground tback;
1872
1873 tback.bgType = kTXNBackgroundTypeRGB;
1874 tback.bg.color = MAC_WXCOLORREF( brush.GetColour().GetPixel() );
1875 TXNSetBackground( m_txn , &tback );
1876 }
1877
1878 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
1879 {
1880 TXNTypeAttributes typeAttr[4] ;
1881 RGBColor color ;
1882 int attrCount = 0 ;
1883
1884 if ( style.HasFont() )
1885 {
1886 const wxFont &font = style.GetFont() ;
1887
1888 #if 0 // old version
1889 Str255 fontName = "\pMonaco" ;
1890 SInt16 fontSize = 12 ;
1891 Style fontStyle = normal ;
1892 wxMacStringToPascal( font.GetFaceName() , fontName ) ;
1893 fontSize = font.GetPointSize() ;
1894 if ( font.GetUnderlined() )
1895 fontStyle |= underline ;
1896 if ( font.GetWeight() == wxBOLD )
1897 fontStyle |= bold ;
1898 if ( font.GetStyle() == wxITALIC )
1899 fontStyle |= italic ;
1900
1901 typeAttr[attrCount].tag = kTXNQDFontNameAttribute ;
1902 typeAttr[attrCount].size = kTXNQDFontNameAttributeSize ;
1903 typeAttr[attrCount].data.dataPtr = (void*)fontName ;
1904 attrCount++ ;
1905
1906 typeAttr[attrCount].tag = kTXNQDFontSizeAttribute ;
1907 typeAttr[attrCount].size = kTXNFontSizeAttributeSize ;
1908 typeAttr[attrCount].data.dataValue = (fontSize << 16) ;
1909 attrCount++ ;
1910
1911 typeAttr[attrCount].tag = kTXNQDFontStyleAttribute ;
1912 typeAttr[attrCount].size = kTXNQDFontStyleAttributeSize ;
1913 typeAttr[attrCount].data.dataValue = fontStyle ;
1914 attrCount++ ;
1915 #else
1916 typeAttr[attrCount].tag = kTXNATSUIStyle ;
1917 typeAttr[attrCount].size = kTXNATSUIStyleSize ;
1918 typeAttr[attrCount].data.dataPtr = font.MacGetATSUStyle() ;
1919 attrCount++ ;
1920 #endif
1921 }
1922
1923 if ( style.HasTextColour() )
1924 {
1925 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
1926
1927 typeAttr[attrCount].tag = kTXNQDFontColorAttribute ;
1928 typeAttr[attrCount].size = kTXNQDFontColorAttributeSize ;
1929 typeAttr[attrCount].data.dataPtr = (void*) &color ;
1930 attrCount++ ;
1931 }
1932
1933 if ( attrCount > 0 )
1934 {
1935 verify_noerr( TXNSetTypeAttributes( m_txn , attrCount , typeAttr, from , to ) );
1936 // unfortunately the relayout is not automatic
1937 TXNRecalcTextLayout( m_txn );
1938 }
1939 }
1940
1941 void wxMacMLTEControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
1942 {
1943 wxMacEditHelper help( m_txn ) ;
1944 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1945 }
1946
1947 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1948 {
1949 wxMacEditHelper help( m_txn ) ;
1950 TXNSetAttribute( style, start, end ) ;
1951 }
1952
1953 void wxMacMLTEControl::Copy()
1954 {
1955 ClearCurrentScrap();
1956 TXNCopy( m_txn );
1957 TXNConvertToPublicScrap();
1958 }
1959
1960 void wxMacMLTEControl::Cut()
1961 {
1962 ClearCurrentScrap();
1963 TXNCut( m_txn );
1964 TXNConvertToPublicScrap();
1965 }
1966
1967 void wxMacMLTEControl::Paste()
1968 {
1969 TXNConvertFromPublicScrap();
1970 TXNPaste( m_txn );
1971 }
1972
1973 bool wxMacMLTEControl::CanPaste() const
1974 {
1975 return TXNIsScrapPastable() ;
1976 }
1977
1978 void wxMacMLTEControl::SetEditable(bool editable)
1979 {
1980 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1981 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1982 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1983 }
1984
1985 wxTextPos wxMacMLTEControl::GetLastPosition() const
1986 {
1987 wxTextPos actualsize = 0 ;
1988
1989 Handle theText ;
1990 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1991
1992 // all done
1993 if ( err == noErr )
1994 {
1995 actualsize = GetHandleSize( theText ) ;
1996 DisposeHandle( theText ) ;
1997 }
1998 else
1999 {
2000 actualsize = 0 ;
2001 }
2002
2003 return actualsize ;
2004 }
2005
2006 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
2007 {
2008 wxString value = str ;
2009 wxMacConvertNewlines10To13( &value ) ;
2010
2011 wxMacEditHelper help( m_txn ) ;
2012 #ifndef __LP64__
2013 wxMacWindowClipper c( m_peer ) ;
2014 #endif
2015
2016 TXNSetSelection( m_txn, from, to ) ;
2017 TXNClear( m_txn ) ;
2018 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2019 }
2020
2021 void wxMacMLTEControl::Remove( long from , long to )
2022 {
2023 #ifndef __LP64__
2024 wxMacWindowClipper c( m_peer ) ;
2025 #endif
2026 wxMacEditHelper help( m_txn ) ;
2027 TXNSetSelection( m_txn , from , to ) ;
2028 TXNClear( m_txn ) ;
2029 }
2030
2031 void wxMacMLTEControl::GetSelection( long* from, long* to) const
2032 {
2033 TXNGetSelection( m_txn , (TXNOffset*) from , (TXNOffset*) to ) ;
2034 }
2035
2036 void wxMacMLTEControl::SetSelection( long from , long to )
2037 {
2038 #ifndef __LP64__
2039 wxMacWindowClipper c( m_peer ) ;
2040 #endif
2041
2042 // change the selection
2043 if ((from == -1) && (to == -1))
2044 TXNSelectAll( m_txn );
2045 else
2046 TXNSetSelection( m_txn, from, to );
2047
2048 TXNShowSelection( m_txn, kTXNShowStart );
2049 }
2050
2051 void wxMacMLTEControl::WriteText( const wxString& str )
2052 {
2053 wxString st = str ;
2054 wxMacConvertNewlines10To13( &st ) ;
2055
2056 long start , end , dummy ;
2057
2058 GetSelection( &start , &dummy ) ;
2059 #ifndef __LP64__
2060 wxMacWindowClipper c( m_peer ) ;
2061 #endif
2062
2063 {
2064 wxMacEditHelper helper( m_txn ) ;
2065 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2066 }
2067
2068 GetSelection( &dummy, &end ) ;
2069
2070 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2071 }
2072
2073 void wxMacMLTEControl::Clear()
2074 {
2075 #ifndef __LP64__
2076 wxMacWindowClipper c( m_peer ) ;
2077 #endif
2078 wxMacEditHelper st( m_txn ) ;
2079 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
2080 TXNClear( m_txn ) ;
2081 }
2082
2083 bool wxMacMLTEControl::CanUndo() const
2084 {
2085 return TXNCanUndo( m_txn , NULL ) ;
2086 }
2087
2088 void wxMacMLTEControl::Undo()
2089 {
2090 TXNUndo( m_txn ) ;
2091 }
2092
2093 bool wxMacMLTEControl::CanRedo() const
2094 {
2095 return TXNCanRedo( m_txn , NULL ) ;
2096 }
2097
2098 void wxMacMLTEControl::Redo()
2099 {
2100 TXNRedo( m_txn ) ;
2101 }
2102
2103 int wxMacMLTEControl::GetNumberOfLines() const
2104 {
2105 ItemCount lines = 0 ;
2106 TXNGetLineCount( m_txn, &lines ) ;
2107
2108 return lines ;
2109 }
2110
2111 long wxMacMLTEControl::XYToPosition(long x, long y) const
2112 {
2113 Point curpt ;
2114 wxTextPos lastpos ;
2115
2116 // TODO: find a better implementation : while we can get the
2117 // line metrics of a certain line, we don't get its starting
2118 // position, so it would probably be rather a binary search
2119 // for the start position
2120 long xpos = 0, ypos = 0 ;
2121 int lastHeight = 0 ;
2122 ItemCount n ;
2123
2124 lastpos = GetLastPosition() ;
2125 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
2126 {
2127 if ( y == ypos && x == xpos )
2128 return n ;
2129
2130 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2131
2132 if ( curpt.v > lastHeight )
2133 {
2134 xpos = 0 ;
2135 if ( n > 0 )
2136 ++ypos ;
2137
2138 lastHeight = curpt.v ;
2139 }
2140 else
2141 ++xpos ;
2142 }
2143
2144 return 0 ;
2145 }
2146
2147 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
2148 {
2149 Point curpt ;
2150 wxTextPos lastpos ;
2151
2152 if ( y )
2153 *y = 0 ;
2154 if ( x )
2155 *x = 0 ;
2156
2157 lastpos = GetLastPosition() ;
2158 if ( pos <= lastpos )
2159 {
2160 // TODO: find a better implementation - while we can get the
2161 // line metrics of a certain line, we don't get its starting
2162 // position, so it would probably be rather a binary search
2163 // for the start position
2164 long xpos = 0, ypos = 0 ;
2165 int lastHeight = 0 ;
2166 ItemCount n ;
2167
2168 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
2169 {
2170 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2171
2172 if ( curpt.v > lastHeight )
2173 {
2174 xpos = 0 ;
2175 if ( n > 0 )
2176 ++ypos ;
2177
2178 lastHeight = curpt.v ;
2179 }
2180 else
2181 ++xpos ;
2182 }
2183
2184 if ( y )
2185 *y = ypos ;
2186 if ( x )
2187 *x = xpos ;
2188 }
2189
2190 return false ;
2191 }
2192
2193 void wxMacMLTEControl::ShowPosition( long pos )
2194 {
2195 Point current, desired ;
2196 TXNOffset selstart, selend;
2197
2198 TXNGetSelection( m_txn, &selstart, &selend );
2199 TXNOffsetToPoint( m_txn, selstart, &current );
2200 TXNOffsetToPoint( m_txn, pos, &desired );
2201
2202 // TODO: use HIPoints for 10.3 and above
2203
2204 OSErr theErr = noErr;
2205 long dv = desired.v - current.v;
2206 long dh = desired.h - current.h;
2207 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2208 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
2209
2210 // there will be an error returned for classic MLTE implementation when the control is
2211 // invisible, but HITextView works correctly, so we don't assert that one
2212 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2213 }
2214
2215 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
2216 {
2217 #if wxUSE_UNICODE
2218 #if SIZEOF_WCHAR_T == 2
2219 size_t len = st.length() ;
2220 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
2221 #else
2222 wxMBConvUTF16 converter ;
2223 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
2224 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
2225 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
2226 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
2227 free( unibuf ) ;
2228 #endif
2229 #else
2230 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
2231 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
2232 #endif
2233 }
2234
2235 wxString wxMacMLTEControl::GetLineText(long lineNo) const
2236 {
2237 wxString line ;
2238
2239 if ( lineNo < GetNumberOfLines() )
2240 {
2241 Point firstPoint;
2242 Fixed lineWidth, lineHeight, currentHeight;
2243 long ypos ;
2244
2245 // get the first possible position in the control
2246 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2247
2248 // Iterate through the lines until we reach the one we want,
2249 // adding to our current y pixel point position
2250 ypos = 0 ;
2251 currentHeight = 0;
2252 while (ypos < lineNo)
2253 {
2254 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2255 currentHeight += lineHeight;
2256 }
2257
2258 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2259 TXNOffset theOffset;
2260 TXNPointToOffset(m_txn, thePoint, &theOffset);
2261
2262 wxString content = GetStringValue() ;
2263 Point currentPoint = thePoint;
2264 while (thePoint.v == currentPoint.v && theOffset < content.length())
2265 {
2266 line += content[theOffset];
2267 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2268 }
2269 }
2270
2271 return line ;
2272 }
2273
2274 int wxMacMLTEControl::GetLineLength(long lineNo) const
2275 {
2276 int theLength = 0;
2277
2278 if ( lineNo < GetNumberOfLines() )
2279 {
2280 Point firstPoint;
2281 Fixed lineWidth, lineHeight, currentHeight;
2282 long ypos;
2283
2284 // get the first possible position in the control
2285 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2286
2287 // Iterate through the lines until we reach the one we want,
2288 // adding to our current y pixel point position
2289 ypos = 0;
2290 currentHeight = 0;
2291 while (ypos < lineNo)
2292 {
2293 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2294 currentHeight += lineHeight;
2295 }
2296
2297 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2298 TXNOffset theOffset;
2299 TXNPointToOffset(m_txn, thePoint, &theOffset);
2300
2301 wxString content = GetStringValue() ;
2302 Point currentPoint = thePoint;
2303 while (thePoint.v == currentPoint.v && theOffset < content.length())
2304 {
2305 ++theLength;
2306 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2307 }
2308 }
2309
2310 return theLength ;
2311 }
2312
2313 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
2314
2315 // ----------------------------------------------------------------------------
2316 // MLTE control implementation (classic part)
2317 // ----------------------------------------------------------------------------
2318
2319 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2320 // has to live on. We have different problems coming from outdated implementations on the
2321 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2322 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2323 // no way out, therefore we are using our own implementation and our own scrollbars ....
2324
2325 #ifdef __WXMAC_OSX__
2326
2327 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
2328 ControlActionUPP gTXNScrollActionProc = NULL ;
2329
2330 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2331 SInt32 iValue, SInt32 iMaximumValue,
2332 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
2333 {
2334 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
2335 SInt32 value = wxMax( iValue , 0 ) ;
2336 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
2337
2338 if ( iScrollBarOrientation == kTXNHorizontal )
2339 {
2340 if ( mlte->m_sbHorizontal )
2341 {
2342 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
2343 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
2344 mlte->m_lastHorizontalValue = value ;
2345 }
2346 }
2347 else if ( iScrollBarOrientation == kTXNVertical )
2348 {
2349 if ( mlte->m_sbVertical )
2350 {
2351 SetControl32BitValue( mlte->m_sbVertical , value ) ;
2352 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
2353 mlte->m_lastVerticalValue = value ;
2354 }
2355 }
2356 }
2357
2358 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
2359 {
2360 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
2361 if ( mlte == NULL )
2362 return ;
2363
2364 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
2365 return ;
2366
2367 OSStatus err ;
2368 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
2369
2370 SInt32 minimum = 0 ;
2371 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
2372 SInt32 value = GetControl32BitValue( controlRef ) ;
2373 SInt32 delta = 0;
2374
2375 switch ( partCode )
2376 {
2377 case kControlDownButtonPart :
2378 delta = 10 ;
2379 break ;
2380
2381 case kControlUpButtonPart :
2382 delta = -10 ;
2383 break ;
2384
2385 case kControlPageDownPart :
2386 delta = GetControlViewSize( controlRef ) ;
2387 break ;
2388
2389 case kControlPageUpPart :
2390 delta = -GetControlViewSize( controlRef ) ;
2391 break ;
2392
2393 case kControlIndicatorPart :
2394 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
2395 break ;
2396
2397 default :
2398 break ;
2399 }
2400
2401 if ( delta != 0 )
2402 {
2403 SInt32 newValue = value ;
2404
2405 if ( partCode != kControlIndicatorPart )
2406 {
2407 if ( value + delta < minimum )
2408 delta = minimum - value ;
2409 if ( value + delta > maximum )
2410 delta = maximum - value ;
2411
2412 SetControl32BitValue( controlRef , value + delta ) ;
2413 newValue = value + delta ;
2414 }
2415
2416 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
2417 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
2418
2419 err = TXNScroll(
2420 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
2421 &verticalDelta, &horizontalDelta );
2422 verify_noerr( err );
2423
2424 if ( isHorizontal )
2425 mlte->m_lastHorizontalValue = newValue ;
2426 else
2427 mlte->m_lastVerticalValue = newValue ;
2428 }
2429 }
2430 #endif
2431
2432 // make correct activations
2433 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
2434 {
2435 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2436
2437 wxMacWindowClipper clipper( textctrl ) ;
2438 TXNActivate( m_txn, m_txnFrameID, setActive );
2439
2440 ControlRef controlFocus = 0 ;
2441 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2442 if ( controlFocus == m_controlRef )
2443 TXNFocus( m_txn, setActive );
2444 }
2445
2446 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
2447 {
2448 TXNFocus( m_txn, setFocus );
2449 }
2450
2451 // guards against inappropriate redraw (hidden objects drawing onto window)
2452
2453 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
2454 {
2455 ControlRef controlFocus = 0 ;
2456 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2457
2458 if ( !vis && (controlFocus == m_controlRef ) )
2459 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
2460
2461 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
2462 TXNControlData iControlData[1] = { { (UInt32)false } };
2463
2464 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
2465
2466 if ( iControlData[0].uValue != vis )
2467 {
2468 iControlData[0].uValue = vis ;
2469 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
2470 }
2471
2472 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2473 // if we run into further problems we might set the FrameBounds to an empty rect here
2474 }
2475
2476 // make sure that the TXNObject is at the right position
2477
2478 void wxMacMLTEClassicControl::MacUpdatePosition()
2479 {
2480 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2481 if ( textctrl == NULL )
2482 return ;
2483
2484 Rect bounds ;
2485 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2486
2487 wxRect visRect = textctrl->MacGetClippedClientRect() ;
2488 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
2489 int x , y ;
2490 x = y = 0 ;
2491 textctrl->MacWindowToRootWindow( &x , &y ) ;
2492 OffsetRect( &visBounds , x , y ) ;
2493
2494 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
2495 {
2496 m_txnControlBounds = bounds ;
2497 m_txnVisBounds = visBounds ;
2498 wxMacWindowClipper cl( textctrl ) ;
2499
2500 #ifdef __WXMAC_OSX__
2501 if ( m_sbHorizontal || m_sbVertical )
2502 {
2503 int w = bounds.right - bounds.left ;
2504 int h = bounds.bottom - bounds.top ;
2505
2506 if ( m_sbHorizontal )
2507 {
2508 Rect sbBounds ;
2509
2510 sbBounds.left = -1 ;
2511 sbBounds.top = h - 14 ;
2512 sbBounds.right = w + 1 ;
2513 sbBounds.bottom = h + 1 ;
2514
2515 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
2516 SetControlViewSize( m_sbHorizontal , w ) ;
2517 }
2518
2519 if ( m_sbVertical )
2520 {
2521 Rect sbBounds ;
2522
2523 sbBounds.left = w - 14 ;
2524 sbBounds.top = -1 ;
2525 sbBounds.right = w + 1 ;
2526 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
2527
2528 SetControlBounds( m_sbVertical , &sbBounds ) ;
2529 SetControlViewSize( m_sbVertical , h ) ;
2530 }
2531 }
2532
2533 Rect oldviewRect ;
2534 TXNLongRect olddestRect ;
2535 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
2536
2537 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2538 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2539 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2540 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2541 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2542 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2543
2544 if ( olddestRect.right >= 10000 )
2545 destRect.right = destRect.left + 32000 ;
2546
2547 if ( olddestRect.bottom >= 0x20000000 )
2548 destRect.bottom = destRect.top + 0x40000000 ;
2549
2550 SectRect( &viewRect , &visBounds , &viewRect ) ;
2551 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
2552
2553 #if 0
2554 TXNSetFrameBounds(
2555 m_txn,
2556 m_txnControlBounds.top,
2557 m_txnControlBounds.left,
2558 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
2559 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
2560 m_txnFrameID );
2561 #endif
2562 #else
2563
2564 TXNSetFrameBounds(
2565 m_txn, m_txnControlBounds.top, m_txnControlBounds.left,
2566 wxMax( m_txnControlBounds.bottom, m_txnControlBounds.top ),
2567 wxMax( m_txnControlBounds.right, m_txnControlBounds.left ), m_txnFrameID );
2568 #endif
2569
2570 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2571 // movement, therefore we have to force it
2572
2573 // this problem has been reported in OSX as well, so we use this here once again
2574
2575 TXNLongRect textRect ;
2576 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
2577 if ( textRect.left < m_txnControlBounds.left )
2578 TXNShowSelection( m_txn , kTXNShowStart ) ;
2579 }
2580 }
2581
2582 void wxMacMLTEClassicControl::SetRect( Rect *r )
2583 {
2584 wxMacControl::SetRect( r ) ;
2585 MacUpdatePosition() ;
2586 }
2587
2588 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 thePart)
2589 {
2590 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2591 if ( textctrl == NULL )
2592 return ;
2593
2594 if ( textctrl->MacIsReallyShown() )
2595 {
2596 wxMacWindowClipper clipper( textctrl ) ;
2597 TXNDraw( m_txn , NULL ) ;
2598 }
2599 }
2600
2601 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
2602 {
2603 Point where = { y , x } ;
2604 ControlPartCode result = kControlNoPart;
2605
2606 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2607 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2608 {
2609 if (PtInRect( where, &m_txnControlBounds ))
2610 {
2611 result = kControlEditTextPart ;
2612 }
2613 else
2614 {
2615 // sometimes we get the coords also in control local coordinates, therefore test again
2616 int x = 0 , y = 0 ;
2617 textctrl->MacClientToRootWindow( &x , &y ) ;
2618 where.h += x ;
2619 where.v += y ;
2620
2621 if (PtInRect( where, &m_txnControlBounds ))
2622 result = kControlEditTextPart ;
2623 }
2624 }
2625
2626 return result;
2627 }
2628
2629 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* actionProc )
2630 {
2631 ControlPartCode result = kControlNoPart;
2632
2633 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2634 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2635 {
2636 Point startPt = { y , x } ;
2637
2638 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2639 int x = 0 , y = 0 ;
2640 textctrl->MacClientToRootWindow( &x , &y ) ;
2641 startPt.h += x ;
2642 startPt.v += y ;
2643
2644 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
2645 {
2646 case kControlEditTextPart :
2647 {
2648 wxMacWindowClipper clipper( textctrl ) ;
2649 EventRecord rec ;
2650
2651 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
2652 TXNClick( m_txn, &rec );
2653 }
2654 break;
2655
2656 default :
2657 break;
2658 }
2659 }
2660
2661 return result;
2662 }
2663
2664 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2665 {
2666 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2667 if ( textctrl == NULL )
2668 return ;
2669
2670 if (textctrl->MacIsReallyShown())
2671 {
2672 if (IsControlActive(m_controlRef))
2673 {
2674 Point mousep;
2675
2676 wxMacWindowClipper clipper( textctrl ) ;
2677 GetMouse(&mousep);
2678
2679 TXNIdle(m_txn);
2680
2681 if (PtInRect(mousep, &m_txnControlBounds))
2682 {
2683 RgnHandle theRgn = NewRgn();
2684 RectRgn(theRgn, &m_txnControlBounds);
2685 TXNAdjustCursor(m_txn, theRgn);
2686 DisposeRgn(theRgn);
2687 }
2688 }
2689 }
2690 }
2691
2692 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
2693 {
2694 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2695 if ( textctrl == NULL )
2696 return kControlNoPart;
2697
2698 wxMacWindowClipper clipper( textctrl ) ;
2699
2700 EventRecord ev ;
2701 memset( &ev , 0 , sizeof( ev ) ) ;
2702 ev.what = keyDown ;
2703 ev.modifiers = modifiers ;
2704 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
2705 TXNKeyDown( m_txn , &ev );
2706
2707 return kControlEntireControl;
2708 }
2709
2710 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
2711 {
2712 MacActivatePaneText( activating );
2713 }
2714
2715 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
2716 {
2717 ControlPartCode focusResult = kControlFocusNoPart;
2718
2719 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2720 if ( textctrl == NULL )
2721 return focusResult;
2722
2723 wxMacWindowClipper clipper( textctrl ) ;
2724
2725 ControlRef controlFocus = NULL ;
2726 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2727 bool wasFocused = ( controlFocus == m_controlRef ) ;
2728
2729 switch (action)
2730 {
2731 case kControlFocusPrevPart:
2732 case kControlFocusNextPart:
2733 MacFocusPaneText( !wasFocused );
2734 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
2735 break;
2736
2737 case kControlFocusNoPart:
2738 default:
2739 MacFocusPaneText( false );
2740 focusResult = kControlFocusNoPart;
2741 break;
2742 }
2743
2744 return focusResult;
2745 }
2746
2747 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *info )
2748 {
2749 }
2750
2751 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
2752 const wxString& str,
2753 const wxPoint& pos,
2754 const wxSize& size, long style )
2755 : wxMacMLTEControl( wxPeer )
2756 {
2757 m_font = wxPeer->GetFont() ;
2758 m_windowStyle = style ;
2759 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2760
2761 short featureSet =
2762 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
2763 | kControlWantsActivate | kControlHandlesTracking
2764 // | kControlHasSpecialBackground
2765 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
2766
2767 OSStatus err = ::CreateUserPaneControl(
2768 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
2769 &bounds, featureSet, &m_controlRef );
2770 verify_noerr( err );
2771
2772 DoCreate();
2773
2774 AdjustCreationAttributes( *wxWHITE , true ) ;
2775
2776 MacSetObjectVisibility( wxPeer->MacIsReallyShown() ) ;
2777
2778 {
2779 wxString st = str ;
2780 wxMacConvertNewlines10To13( &st ) ;
2781 wxMacWindowClipper clipper( m_peer ) ;
2782 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2783 TXNSetSelection( m_txn, 0, 0 ) ;
2784 }
2785 }
2786
2787 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2788 {
2789 TXNDeleteObject( m_txn );
2790 m_txn = NULL ;
2791 }
2792
2793 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
2794 {
2795 MacSetObjectVisibility( shown ) ;
2796 wxMacControl::VisibilityChanged( shown ) ;
2797 }
2798
2799 void wxMacMLTEClassicControl::SuperChangedPosition()
2800 {
2801 MacUpdatePosition() ;
2802 wxMacControl::SuperChangedPosition() ;
2803 }
2804
2805 #ifdef __WXMAC_OSX__
2806
2807 ControlUserPaneDrawUPP gTPDrawProc = NULL;
2808 ControlUserPaneHitTestUPP gTPHitProc = NULL;
2809 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
2810 ControlUserPaneIdleUPP gTPIdleProc = NULL;
2811 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
2812 ControlUserPaneActivateUPP gTPActivateProc = NULL;
2813 ControlUserPaneFocusUPP gTPFocusProc = NULL;
2814
2815 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
2816 {
2817 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2818 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2819 if ( win )
2820 win->MacControlUserPaneDrawProc( part ) ;
2821 }
2822
2823 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
2824 {
2825 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2826 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2827 if ( win )
2828 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
2829 else
2830 return kControlNoPart ;
2831 }
2832
2833 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
2834 {
2835 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2836 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2837 if ( win )
2838 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
2839 else
2840 return kControlNoPart ;
2841 }
2842
2843 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
2844 {
2845 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2846 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2847 if ( win )
2848 win->MacControlUserPaneIdleProc() ;
2849 }
2850
2851 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
2852 {
2853 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2854 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2855 if ( win )
2856 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
2857 else
2858 return kControlNoPart ;
2859 }
2860
2861 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2862 {
2863 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2864 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2865 if ( win )
2866 win->MacControlUserPaneActivateProc( activating ) ;
2867 }
2868
2869 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2870 {
2871 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2872 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2873 if ( win )
2874 return win->MacControlUserPaneFocusProc( action ) ;
2875 else
2876 return kControlNoPart ;
2877 }
2878
2879 #if 0
2880 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2881 {
2882 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2883 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2884 if ( win )
2885 win->MacControlUserPaneBackgroundProc(info) ;
2886 }
2887 #endif
2888
2889 #endif // __WXMAC_OSX__
2890
2891 // TXNRegisterScrollInfoProc
2892
2893 OSStatus wxMacMLTEClassicControl::DoCreate()
2894 {
2895 Rect bounds;
2896 OSStatus err = noErr ;
2897
2898 // set up our globals
2899 #ifdef __WXMAC_OSX__
2900 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2901 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2902 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2903 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2904 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2905 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2906 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2907
2908 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2909 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2910 #endif
2911
2912 // set the initial settings for our private data
2913
2914 m_txnWindow = GetControlOwner(m_controlRef);
2915 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2916
2917 #ifdef __WXMAC_OSX__
2918 // set up the user pane procedures
2919 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2920 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2921 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2922 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2923 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2924 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2925 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2926 #endif
2927
2928 // calculate the rectangles used by the control
2929 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2930
2931 m_txnControlBounds = bounds ;
2932 m_txnVisBounds = bounds ;
2933
2934 CGrafPtr origPort ;
2935 GDHandle origDev ;
2936
2937 GetGWorld( &origPort, &origDev ) ;
2938 SetPort( m_txnPort );
2939
2940 // create the new edit field
2941 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2942
2943 #ifdef __WXMAC_OSX__
2944 // the scrollbars are not correctly embedded but are inserted at the root:
2945 // this gives us problems as we have erratic redraws even over the structure area
2946
2947 m_sbHorizontal = 0 ;
2948 m_sbVertical = 0 ;
2949 m_lastHorizontalValue = 0 ;
2950 m_lastVerticalValue = 0 ;
2951
2952 Rect sb = { 0 , 0 , 0 , 0 } ;
2953 if ( frameOptions & kTXNWantVScrollBarMask )
2954 {
2955 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2956 SetControlReference( m_sbVertical, (SInt32)this );
2957 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2958 ShowControl( m_sbVertical );
2959 EmbedControl( m_sbVertical , m_controlRef );
2960 frameOptions &= ~kTXNWantVScrollBarMask;
2961 }
2962
2963 if ( frameOptions & kTXNWantHScrollBarMask )
2964 {
2965 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2966 SetControlReference( m_sbHorizontal, (SInt32)this );
2967 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2968 ShowControl( m_sbHorizontal );
2969 EmbedControl( m_sbHorizontal, m_controlRef );
2970 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2971 }
2972
2973 #endif
2974
2975 err = TXNNewObject(
2976 NULL, m_txnWindow, &bounds, frameOptions,
2977 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2978 &m_txn, &m_txnFrameID, NULL );
2979 verify_noerr( err );
2980
2981 #if 0
2982 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2983 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2984 int toptag = WXSIZEOF( iControlTags ) ;
2985 TXNCarbonEventInfo cInfo ;
2986 cInfo.useCarbonEvents = false ;
2987 cInfo.filler = 0 ;
2988 cInfo.flags = 0 ;
2989 cInfo.fDictionary = NULL ;
2990
2991 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2992 #endif
2993
2994 #ifdef __WXMAC_OSX__
2995 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2996 #endif
2997
2998 SetGWorld( origPort , origDev ) ;
2999
3000 return err;
3001 }
3002 #endif
3003
3004 // ----------------------------------------------------------------------------
3005 // MLTE control implementation (OSX part)
3006 // ----------------------------------------------------------------------------
3007
3008 #if TARGET_API_MAC_OSX
3009
3010 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
3011
3012 // tiger multi-line textcontrols with no CR in the entire content
3013 // don't scroll automatically, so we need a hack.
3014 // This attempt only works 'before' the key (ie before CallNextEventHandler)
3015 // is processed, thus the scrolling always occurs one character too late, but
3016 // better than nothing ...
3017
3018 static const EventTypeSpec eventList[] =
3019 {
3020 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
3021 } ;
3022
3023 static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
3024 {
3025 OSStatus result = eventNotHandledErr ;
3026 wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
3027
3028 switch ( GetEventKind( event ) )
3029 {
3030 case kEventTextInputUnicodeForKeyEvent :
3031 {
3032 if ( UMAGetSystemVersion() >= 0x1040 )
3033 {
3034 TXNOffset from , to ;
3035 TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
3036 if ( from == to )
3037 TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
3038 }
3039 result = CallNextEventHandler(handler,event);
3040 break;
3041 }
3042 default:
3043 break ;
3044 }
3045
3046 return result ;
3047 }
3048
3049 static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
3050 {
3051 OSStatus result = eventNotHandledErr ;
3052
3053 switch ( GetEventClass( event ) )
3054 {
3055 case kEventClassTextInput :
3056 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
3057 break ;
3058
3059 default :
3060 break ;
3061 }
3062 return result ;
3063 }
3064
3065 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
3066
3067 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
3068 const wxString& str,
3069 const wxPoint& pos,
3070 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
3071 {
3072 m_font = wxPeer->GetFont() ;
3073 m_windowStyle = style ;
3074 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
3075 wxString st = str ;
3076 wxMacConvertNewlines10To13( &st ) ;
3077
3078 HIRect hr = {
3079 { bounds.left , bounds.top },
3080 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
3081
3082 m_scrollView = NULL ;
3083 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
3084 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
3085 {
3086 HIScrollViewCreate(
3087 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
3088 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
3089 &m_scrollView ) ;
3090
3091 HIViewSetFrame( m_scrollView, &hr );
3092 HIViewSetVisible( m_scrollView, true );
3093 }
3094
3095 m_textView = NULL ;
3096 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
3097 m_txn = HITextViewGetTXNObject( m_textView ) ;
3098 HIViewSetVisible( m_textView , true ) ;
3099 if ( m_scrollView )
3100 {
3101 HIViewAddSubview( m_scrollView , m_textView ) ;
3102 m_controlRef = m_scrollView ;
3103 wxPeer->MacInstallEventHandler( (WXWidget) m_textView ) ;
3104 }
3105 else
3106 {
3107 HIViewSetFrame( m_textView, &hr );
3108 m_controlRef = m_textView ;
3109 }
3110
3111 AdjustCreationAttributes( *wxWHITE , true ) ;
3112 #ifndef __LP64__
3113 wxMacWindowClipper c( m_peer ) ;
3114 #endif
3115 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
3116
3117 TXNSetSelection( m_txn, 0, 0 );
3118 TXNShowSelection( m_txn, kTXNShowStart );
3119
3120 InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
3121 GetEventTypeCount(eventList), eventList, this,
3122 &m_textEventHandlerRef);
3123 }
3124
3125 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3126 {
3127 ::RemoveEventHandler( m_textEventHandlerRef ) ;
3128 }
3129
3130 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
3131 {
3132 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, focusPart ) ;
3133 }
3134
3135 bool wxMacMLTEHIViewControl::HasFocus() const
3136 {
3137 ControlRef control ;
3138 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
3139 return control == m_textView ;
3140 }
3141
3142 void wxMacMLTEHIViewControl::SetBackground( const wxBrush &brush )
3143 {
3144 wxMacMLTEControl::SetBackground( brush ) ;
3145
3146 #if 0
3147 CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
3148 RGBColor col = MAC_WXCOLORREF(brush.GetColour().GetPixel()) ;
3149
3150 float component[4] ;
3151 component[0] = col.red / 65536.0 ;
3152 component[1] = col.green / 65536.0 ;
3153 component[2] = col.blue / 65536.0 ;
3154 component[3] = 1.0 ; // alpha
3155
3156 CGColorRef color = CGColorCreate( rgbSpace , component );
3157 HITextViewSetBackgroundColor( m_textView , color );
3158 CGColorSpaceRelease( rgbSpace );
3159 #endif
3160 }
3161
3162 #endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
3163
3164
3165 #endif
3166
3167 #endif // wxUSE_TEXTCTRL