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