]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/textctrl.cpp
implement support for custom button labels in wxMessageBox under MSW; refactor the...
[wxWidgets.git] / src / osx / carbon / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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 #include "wx/thread.h"
48
49 #include "wx/osx/private.h"
50 #include "wx/osx/carbon/private/mactext.h"
51
52 class wxMacFunctor
53 {
54 public :
55 wxMacFunctor() {}
56 virtual ~wxMacFunctor() {}
57
58 virtual void* operator()() = 0 ;
59
60 static void* CallBackProc( void *param )
61 {
62 wxMacFunctor* f = (wxMacFunctor*) param ;
63 void *result = (*f)() ;
64 return result ;
65 }
66 } ;
67
68 template<typename classtype, typename param1type>
69
70 class wxMacObjectFunctor1 : public wxMacFunctor
71 {
72 typedef void (classtype::*function)( param1type p1 ) ;
73 typedef void (classtype::*ref_function)( const param1type& p1 ) ;
74 public :
75 wxMacObjectFunctor1( classtype *obj , function f , param1type p1 ) :
76 wxMacFunctor()
77 {
78 m_object = obj ;
79 m_function = f ;
80 m_param1 = p1 ;
81 }
82
83 wxMacObjectFunctor1( classtype *obj , ref_function f , param1type p1 ) :
84 wxMacFunctor()
85 {
86 m_object = obj ;
87 m_refFunction = f ;
88 m_param1 = p1 ;
89 }
90
91 virtual ~wxMacObjectFunctor1() {}
92
93 virtual void* operator()()
94 {
95 (m_object->*m_function)( m_param1 ) ;
96 return NULL ;
97 }
98
99 private :
100 classtype* m_object ;
101 param1type m_param1 ;
102 union
103 {
104 function m_function ;
105 ref_function m_refFunction ;
106 } ;
107 } ;
108
109 template<typename classtype, typename param1type>
110 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
111 {
112 wxMacObjectFunctor1<classtype, param1type> params(object, function, p1) ;
113 void *result =
114 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
115 return result ;
116 }
117
118 template<typename classtype, typename param1type>
119 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
120 {
121 wxMacObjectFunctor1<classtype,param1type> params(object, function, p1) ;
122 void *result =
123 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
124 return result ;
125 }
126
127 template<typename classtype, typename param1type>
128 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
129 {
130 wxMutexGuiLeave() ;
131 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
132 wxMutexGuiEnter() ;
133 return result ;
134 }
135
136 template<typename classtype, typename param1type>
137 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
138 {
139 wxMutexGuiLeave() ;
140 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
141 wxMutexGuiEnter() ;
142 return result ;
143 }
144
145 class WXDLLEXPORT wxMacPortSaver
146 {
147 DECLARE_NO_COPY_CLASS(wxMacPortSaver)
148
149 public:
150 wxMacPortSaver( GrafPtr port );
151 ~wxMacPortSaver();
152 private :
153 GrafPtr m_port;
154 };
155
156
157 /*
158 Clips to the visible region of a control within the current port
159 */
160
161 class WXDLLEXPORT wxMacWindowClipper : public wxMacPortSaver
162 {
163 DECLARE_NO_COPY_CLASS(wxMacWindowClipper)
164
165 public:
166 wxMacWindowClipper( const wxWindow* win );
167 ~wxMacWindowClipper();
168 private:
169 GrafPtr m_newPort;
170 RgnHandle m_formerClip;
171 RgnHandle m_newClip;
172 };
173
174 wxMacPortSaver::wxMacPortSaver( GrafPtr port )
175 {
176 ::GetPort( &m_port );
177 ::SetPort( port );
178 }
179
180 wxMacPortSaver::~wxMacPortSaver()
181 {
182 ::SetPort( m_port );
183 }
184
185 wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win ) :
186 wxMacPortSaver( (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) )
187 {
188 m_newPort = (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) ;
189 m_formerClip = NewRgn() ;
190 m_newClip = NewRgn() ;
191 GetClip( m_formerClip ) ;
192
193 if ( win )
194 {
195 // guard against half constructed objects, this just leads to a empty clip
196 if ( win->GetPeer() )
197 {
198 int x = 0 , y = 0;
199 win->MacWindowToRootWindow( &x, &y ) ;
200
201 // get area including focus rect
202 HIShapeGetAsQDRgn( ((wxWindow*)win)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip );
203 if ( !EmptyRgn( m_newClip ) )
204 OffsetRgn( m_newClip , x , y ) ;
205 }
206
207 SetClip( m_newClip ) ;
208 }
209 }
210
211 wxMacWindowClipper::~wxMacWindowClipper()
212 {
213 SetPort( m_newPort ) ;
214 SetClip( m_formerClip ) ;
215 DisposeRgn( m_newClip ) ;
216 DisposeRgn( m_formerClip ) ;
217 }
218
219 // common parts for implementations based on MLTE
220
221 class wxMacMLTEControl : public wxMacTextControl
222 {
223 public :
224 wxMacMLTEControl( wxTextCtrl *peer ) ;
225
226 virtual wxString GetStringValue() const ;
227 virtual void SetStringValue( const wxString &str ) ;
228
229 static TXNFrameOptions FrameOptionsFromWXStyle( long wxStyle ) ;
230
231 void AdjustCreationAttributes( const wxColour& background, bool visible ) ;
232
233 virtual void SetFont( const wxFont & font, const wxColour& foreground, long windowStyle ) ;
234 virtual void SetBackgroundColour(const wxColour& col );
235 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
236 virtual void Copy() ;
237 virtual void Cut() ;
238 virtual void Paste() ;
239 virtual bool CanPaste() const ;
240 virtual void SetEditable( bool editable ) ;
241 virtual wxTextPos GetLastPosition() const ;
242 virtual void Replace( long from, long to, const wxString &str ) ;
243 virtual void Remove( long from, long to ) ;
244 virtual void GetSelection( long* from, long* to ) const ;
245 virtual void SetSelection( long from, long to ) ;
246
247 virtual void WriteText( const wxString& str ) ;
248
249 virtual bool HasOwnContextMenu() const
250 {
251 TXNCommandEventSupportOptions options ;
252 TXNGetCommandEventSupport( m_txn , & options ) ;
253 return options & kTXNSupportEditCommandProcessing ;
254 }
255
256 virtual void CheckSpelling(bool check)
257 {
258 TXNSetSpellCheckAsYouType( m_txn, (Boolean) check );
259 }
260 virtual void Clear() ;
261
262 virtual bool CanUndo() const ;
263 virtual void Undo() ;
264 virtual bool CanRedo() const;
265 virtual void Redo() ;
266 virtual int GetNumberOfLines() const ;
267 virtual long XYToPosition(long x, long y) const ;
268 virtual bool PositionToXY(long pos, long *x, long *y) const ;
269 virtual void ShowPosition( long pos ) ;
270 virtual int GetLineLength(long lineNo) const ;
271 virtual wxString GetLineText(long lineNo) const ;
272
273 void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
274 TXNObject GetTXNObject() { return m_txn ; }
275
276 protected :
277 void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
278
279 TXNObject m_txn ;
280 } ;
281
282 // implementation available under OSX
283
284 class wxMacMLTEHIViewControl : public wxMacMLTEControl
285 {
286 public :
287 wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
288 const wxString& str,
289 const wxPoint& pos,
290 const wxSize& size, long style ) ;
291 virtual ~wxMacMLTEHIViewControl() ;
292
293 virtual OSStatus SetFocus( ControlFocusPart focusPart ) ;
294 virtual bool HasFocus() const ;
295 virtual void SetBackgroundColour(const wxColour& col ) ;
296
297 protected :
298 HIViewRef m_scrollView ;
299 HIViewRef m_textView ;
300 };
301
302 // 'classic' MLTE implementation
303
304 class wxMacMLTEClassicControl : public wxMacMLTEControl
305 {
306 public :
307 wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
308 const wxString& str,
309 const wxPoint& pos,
310 const wxSize& size, long style ) ;
311 virtual ~wxMacMLTEClassicControl() ;
312
313 virtual void VisibilityChanged(bool shown) ;
314 virtual void SuperChangedPosition() ;
315
316 virtual void MacControlUserPaneDrawProc(wxInt16 part) ;
317 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) ;
318 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) ;
319 virtual void MacControlUserPaneIdleProc() ;
320 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) ;
321 virtual void MacControlUserPaneActivateProc(bool activating) ;
322 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) ;
323 virtual void MacControlUserPaneBackgroundProc(void* info) ;
324
325 virtual bool SetupCursor( const wxPoint& WXUNUSED(pt) )
326 {
327 MacControlUserPaneIdleProc();
328 return true;
329 }
330
331 virtual void Move(int x, int y, int width, int height);
332
333 protected :
334 OSStatus DoCreate();
335
336 void MacUpdatePosition() ;
337 void MacActivatePaneText(bool setActive) ;
338 void MacFocusPaneText(bool setFocus) ;
339 void MacSetObjectVisibility(bool vis) ;
340
341 private :
342 TXNFrameID m_txnFrameID ;
343 GrafPtr m_txnPort ;
344 WindowRef m_txnWindow ;
345 // bounds of the control as we last did set the txn frames
346 Rect m_txnControlBounds ;
347 Rect m_txnVisBounds ;
348
349 static pascal void TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode ) ;
350 static pascal void TXNScrollInfoProc(
351 SInt32 iValue, SInt32 iMaximumValue,
352 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon ) ;
353
354 ControlRef m_sbHorizontal ;
355 SInt32 m_lastHorizontalValue ;
356 ControlRef m_sbVertical ;
357 SInt32 m_lastVerticalValue ;
358 };
359
360 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
361 wxWindowMac* parent,
362 wxWindowID id,
363 const wxString& str,
364 const wxPoint& pos,
365 const wxSize& size,
366 long style,
367 long extraStyle)
368 {
369 bool forceMLTE = false ;
370
371 #if wxUSE_SYSTEM_OPTIONS
372 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE ) == 1))
373 {
374 forceMLTE = true ;
375 }
376 #endif
377
378 if ( UMAGetSystemVersion() >= 0x1050 )
379 forceMLTE = false;
380
381 wxMacTextControl* peer = NULL;
382
383 if ( !forceMLTE )
384 {
385 if ( style & wxTE_MULTILINE || ( UMAGetSystemVersion() >= 0x1050 ) )
386 peer = new wxMacMLTEHIViewControl( wxpeer , str , pos , size , style ) ;
387 }
388
389 if ( !peer )
390 {
391 if ( !(style & wxTE_MULTILINE) && !forceMLTE )
392 {
393 peer = new wxMacUnicodeTextControl( wxpeer , str , pos , size , style ) ;
394 }
395 }
396
397 // the horizontal single line scrolling bug that made us keep the classic implementation
398 // is fixed in 10.5
399 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
400 if ( !peer )
401 peer = new wxMacMLTEClassicControl( wxpeer , str , pos , size , style ) ;
402 #endif
403 return peer;
404 }
405
406 // ----------------------------------------------------------------------------
407 // standard unicode control implementation
408 // ----------------------------------------------------------------------------
409
410 // the current unicode textcontrol implementation has a bug : only if the control
411 // is currently having the focus, the selection can be retrieved by the corresponding
412 // data tag. So we have a mirroring using a member variable
413 // TODO : build event table using virtual member functions for wxMacControl
414
415 static const EventTypeSpec unicodeTextControlEventList[] =
416 {
417 { kEventClassControl , kEventControlSetFocusPart } ,
418 } ;
419
420 static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
421 {
422 OSStatus result = eventNotHandledErr ;
423 wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ;
424 wxMacCarbonEvent cEvent( event ) ;
425
426 switch ( GetEventKind( event ) )
427 {
428 case kEventControlSetFocusPart :
429 {
430 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
431 if ( controlPart == kControlFocusNoPart )
432 {
433 // about to loose focus -> store selection to field
434 focus->GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
435 }
436 result = CallNextEventHandler(handler,event) ;
437 if ( controlPart != kControlFocusNoPart )
438 {
439 // about to gain focus -> set selection from field
440 focus->SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
441 }
442 break;
443 }
444 default:
445 break ;
446 }
447
448 return result ;
449 }
450
451 static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
452 {
453 OSStatus result = eventNotHandledErr ;
454
455 switch ( GetEventClass( event ) )
456 {
457 case kEventClassControl :
458 result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ;
459 break ;
460
461 default :
462 break ;
463 }
464 return result ;
465 }
466
467 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler )
468
469 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer ) : wxMacTextControl( wxPeer )
470 {
471 }
472
473 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
474 const wxString& str,
475 const wxPoint& pos,
476 const wxSize& size, long style )
477 : wxMacTextControl( wxPeer )
478 {
479 Create( wxPeer, str, pos, size, style );
480 }
481
482 bool wxMacUnicodeTextControl::Create( wxTextCtrl *wxPeer,
483 const wxString& str,
484 const wxPoint& pos,
485 const wxSize& size, long style )
486 {
487 m_font = wxPeer->GetFont() ;
488 m_windowStyle = style ;
489 m_selection.selStart = m_selection.selEnd = 0;
490 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
491 wxString st = str ;
492 wxMacConvertNewlines10To13( &st ) ;
493 wxCFStringRef cf(st , m_font.GetEncoding()) ;
494 CFStringRef cfr = cf ;
495
496 m_valueTag = kControlEditTextCFStringTag ;
497 CreateControl( wxPeer, &bounds, cfr );
498
499 if ( !(m_windowStyle & wxTE_MULTILINE) )
500 SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
501
502 ::InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(),
503 GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
504 NULL);
505
506 return true;
507 }
508
509 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
510 {
511 }
512
513 void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
514 {
515 if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
516 {
517 // work around a refresh issue insofar as not always the entire content is shown,
518 // even if this would be possible
519 ControlEditTextSelectionRec sel ;
520 CFStringRef value = NULL ;
521
522 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
523 verify_noerr( GetData<CFStringRef>( 0, m_valueTag, &value ) );
524 verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
525 verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
526
527 CFRelease( value ) ;
528 }
529 }
530
531 wxString wxMacUnicodeTextControl::GetStringValue() const
532 {
533 wxString result ;
534 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
535 if ( value )
536 {
537 wxCFStringRef cf(value) ;
538 result = cf.AsString() ;
539 }
540
541 #if '\n' == 10
542 wxMacConvertNewlines13To10( &result ) ;
543 #else
544 wxMacConvertNewlines10To13( &result ) ;
545 #endif
546
547 return result ;
548 }
549
550 void wxMacUnicodeTextControl::SetStringValue( const wxString &str )
551 {
552 wxString st = str ;
553 wxMacConvertNewlines10To13( &st ) ;
554 wxCFStringRef cf( st , m_font.GetEncoding() ) ;
555 verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
556 }
557
558 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef cfr )
559 {
560 Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
561 if ( isPassword )
562 {
563 m_valueTag = kControlEditTextPasswordCFStringTag ;
564 }
565 OSStatus err = CreateEditUnicodeTextControl(
566 MAC_WXHWND(peer->MacGetTopLevelWindowRef()), bounds , cfr ,
567 isPassword , NULL , &m_controlRef ) ;
568 verify_noerr( err );
569 }
570
571 void wxMacUnicodeTextControl::Copy()
572 {
573 SendHICommand( kHICommandCopy ) ;
574 }
575
576 void wxMacUnicodeTextControl::Cut()
577 {
578 SendHICommand( kHICommandCut ) ;
579 }
580
581 void wxMacUnicodeTextControl::Paste()
582 {
583 SendHICommand( kHICommandPaste ) ;
584 }
585
586 bool wxMacUnicodeTextControl::CanPaste() const
587 {
588 return true ;
589 }
590
591 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable))
592 {
593 #if 0 // leads to problem because text cannot be selected anymore
594 SetData<Boolean>( kControlEditTextPart , kControlEditTextLockedTag , (Boolean) !editable ) ;
595 #endif
596 }
597
598 void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const
599 {
600 ControlEditTextSelectionRec sel ;
601 if (HasFocus())
602 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
603 else
604 sel = m_selection ;
605
606 if ( from )
607 *from = sel.selStart ;
608 if ( to )
609 *to = sel.selEnd ;
610 }
611
612 void wxMacUnicodeTextControl::SetSelection( long from , long to )
613 {
614 ControlEditTextSelectionRec sel ;
615 wxString result ;
616 int textLength = 0 ;
617 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
618 if ( value )
619 {
620 wxCFStringRef cf(value) ;
621 textLength = cf.AsString().length() ;
622 }
623
624 if ((from == -1) && (to == -1))
625 {
626 from = 0 ;
627 to = textLength ;
628 }
629 else
630 {
631 from = wxMin(textLength,wxMax(from,0)) ;
632 if ( to == -1 )
633 to = textLength;
634 else
635 to = wxMax(0,wxMin(textLength,to)) ;
636 }
637
638 sel.selStart = from ;
639 sel.selEnd = to ;
640 if ( HasFocus() )
641 SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
642 else
643 m_selection = sel;
644 }
645
646 void wxMacUnicodeTextControl::WriteText( const wxString& str )
647 {
648 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
649 if ( !wxIsMainThread() )
650 {
651 #if wxOSX_USE_CARBON
652 // unfortunately CW 8 is not able to correctly deduce the template types,
653 // so we have to instantiate explicitly
654 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( (wxTextCtrl*) GetWXPeer() , &wxTextCtrl::WriteText , str ) ;
655 #endif
656 return ;
657 }
658
659 wxString st = str ;
660 wxMacConvertNewlines10To13( &st ) ;
661
662 if ( HasFocus() )
663 {
664 wxCFStringRef cf(st , m_font.GetEncoding() ) ;
665 CFStringRef value = cf ;
666 SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
667 }
668 else
669 {
670 wxString val = GetStringValue() ;
671 long start , end ;
672 GetSelection( &start , &end ) ;
673 val.Remove( start , end - start ) ;
674 val.insert( start , str ) ;
675 SetStringValue( val ) ;
676 SetSelection( start + str.length() , start + str.length() ) ;
677 }
678 }
679
680 // ----------------------------------------------------------------------------
681 // MLTE control implementation (common part)
682 // ----------------------------------------------------------------------------
683
684 // if MTLE is read only, no changes at all are allowed, not even from
685 // procedural API, in order to allow changes via API all the same we must undo
686 // the readonly status while we are executing, this class helps to do so
687
688 class wxMacEditHelper
689 {
690 public :
691 wxMacEditHelper( TXNObject txn )
692 {
693 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
694 m_txn = txn ;
695 TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
696 if ( m_data[0].uValue == kTXNReadOnly )
697 {
698 TXNControlData data[] = { { kTXNReadWrite } } ;
699 TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
700 }
701 }
702
703 ~wxMacEditHelper()
704 {
705 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
706 if ( m_data[0].uValue == kTXNReadOnly )
707 TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
708 }
709
710 protected :
711 TXNObject m_txn ;
712 TXNControlData m_data[1] ;
713 } ;
714
715 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer )
716 : wxMacTextControl( peer )
717 {
718 SetNeedsFocusRect( true ) ;
719 }
720
721 wxString wxMacMLTEControl::GetStringValue() const
722 {
723 wxString result ;
724 OSStatus err ;
725 Size actualSize = 0;
726
727 {
728 #if wxUSE_UNICODE
729 Handle theText ;
730 err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNUnicodeTextData );
731
732 // all done
733 if ( err != noErr )
734 {
735 actualSize = 0 ;
736 }
737 else
738 {
739 actualSize = GetHandleSize( theText ) / sizeof(UniChar) ;
740 if ( actualSize > 0 )
741 {
742 wxChar *ptr = NULL ;
743
744 #if SIZEOF_WCHAR_T == 2
745 ptr = new wxChar[actualSize + 1] ;
746 wxStrncpy( ptr , (wxChar*)(*theText) , actualSize ) ;
747 #else
748 SetHandleSize( theText, (actualSize + 1) * sizeof(UniChar) ) ;
749 HLock( theText ) ;
750 (((UniChar*)*theText)[actualSize]) = 0 ;
751 wxMBConvUTF16 converter ;
752 size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
753 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") );
754 ptr = new wxChar[noChars + 1] ;
755
756 noChars = converter.MB2WC( ptr , (const char*)*theText , noChars + 1 ) ;
757 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") );
758 ptr[noChars] = 0 ;
759 HUnlock( theText ) ;
760 #endif
761
762 ptr[actualSize] = 0 ;
763 result = wxString( ptr ) ;
764 delete [] ptr ;
765 }
766
767 DisposeHandle( theText ) ;
768 }
769 #else
770 Handle theText ;
771 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
772
773 // all done
774 if ( err != noErr )
775 {
776 actualSize = 0 ;
777 }
778 else
779 {
780 actualSize = GetHandleSize( theText ) ;
781 if ( actualSize > 0 )
782 {
783 HLock( theText ) ;
784 result = wxString( *theText , wxConvLocal , actualSize ) ;
785 HUnlock( theText ) ;
786 }
787
788 DisposeHandle( theText ) ;
789 }
790 #endif
791 }
792
793 #if '\n' == 10
794 wxMacConvertNewlines13To10( &result ) ;
795 #else
796 wxMacConvertNewlines10To13( &result ) ;
797 #endif
798
799 return result ;
800 }
801
802 void wxMacMLTEControl::SetStringValue( const wxString &str )
803 {
804 wxString st = str;
805 wxMacConvertNewlines10To13( &st );
806
807 {
808 #ifndef __LP64__
809 wxMacWindowClipper c( GetWXPeer() ) ;
810 #endif
811
812 {
813 wxMacEditHelper help( m_txn );
814 SetTXNData( st, kTXNStartOffset, kTXNEndOffset );
815 }
816
817 TXNSetSelection( m_txn, 0, 0 );
818 TXNShowSelection( m_txn, kTXNShowStart );
819 }
820 }
821
822 TXNFrameOptions wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle )
823 {
824 TXNFrameOptions frameOptions = kTXNDontDrawCaretWhenInactiveMask;
825
826 frameOptions |= kTXNDoFontSubstitutionMask;
827
828 if ( ! (wxStyle & wxTE_NOHIDESEL) )
829 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
830
831 if ( wxStyle & (wxHSCROLL | wxTE_DONTWRAP) )
832 frameOptions |= kTXNWantHScrollBarMask ;
833
834 if ( wxStyle & wxTE_MULTILINE )
835 {
836 if ( ! (wxStyle & wxTE_DONTWRAP ) )
837 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
838
839 if ( !(wxStyle & wxTE_NO_VSCROLL) )
840 {
841 frameOptions |= kTXNWantVScrollBarMask ;
842
843 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
844 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
845 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
846 // if ( frameOptions & kTXNWantHScrollBarMask )
847 // frameOptions |= kTXNDrawGrowIconMask ;
848 }
849 }
850 else
851 {
852 frameOptions |= kTXNSingleLineOnlyMask ;
853 }
854
855 return frameOptions ;
856 }
857
858 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour &background,
859 bool WXUNUSED(visible))
860 {
861 TXNControlTag iControlTags[] =
862 {
863 kTXNDoFontSubstitution,
864 kTXNWordWrapStateTag ,
865 };
866 TXNControlData iControlData[] =
867 {
868 { true },
869 { kTXNNoAutoWrap },
870 };
871
872 int toptag = WXSIZEOF( iControlTags ) ;
873
874 if ( m_windowStyle & wxTE_MULTILINE )
875 {
876 iControlData[1].uValue =
877 (m_windowStyle & wxTE_DONTWRAP)
878 ? kTXNNoAutoWrap
879 : kTXNAutoWrap;
880 }
881
882 OSStatus err = TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) ;
883 verify_noerr( err );
884
885 // setting the default font:
886 // under 10.2 this causes a visible caret, therefore we avoid it
887
888 Str255 fontName ;
889 SInt16 fontSize ;
890 Style fontStyle ;
891
892 GetThemeFont( kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
893
894 TXNTypeAttributes typeAttr[] =
895 {
896 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
897 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
898 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
899 } ;
900
901 err = TXNSetTypeAttributes(
902 m_txn, sizeof(typeAttr) / sizeof(TXNTypeAttributes),
903 typeAttr, kTXNStartOffset, kTXNEndOffset );
904 verify_noerr( err );
905
906 if ( m_windowStyle & wxTE_PASSWORD )
907 {
908 UniChar c = 0x00A5 ;
909 err = TXNEchoMode( m_txn , c , 0 , true );
910 verify_noerr( err );
911 }
912
913 TXNBackground tback;
914 tback.bgType = kTXNBackgroundTypeRGB;
915 background.GetRGBColor( &tback.bg.color );
916 TXNSetBackground( m_txn , &tback );
917
918
919 TXNCommandEventSupportOptions options ;
920 if ( TXNGetCommandEventSupport( m_txn, &options ) == noErr )
921 {
922 options |=
923 kTXNSupportEditCommandProcessing
924 | kTXNSupportEditCommandUpdating
925 | kTXNSupportFontCommandProcessing
926 | kTXNSupportFontCommandUpdating;
927
928 // only spell check when not read-only
929 // use system options for the default
930 bool checkSpelling = false ;
931 if ( !(m_windowStyle & wxTE_READONLY) )
932 {
933 #if wxUSE_SYSTEM_OPTIONS
934 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER ) == 1) )
935 {
936 checkSpelling = true ;
937 }
938 #endif
939 }
940
941 if ( checkSpelling )
942 options |=
943 kTXNSupportSpellCheckCommandProcessing
944 | kTXNSupportSpellCheckCommandUpdating;
945
946 TXNSetCommandEventSupport( m_txn , options ) ;
947 }
948 }
949
950 void wxMacMLTEControl::SetBackgroundColour(const wxColour& col )
951 {
952 TXNBackground tback;
953 tback.bgType = kTXNBackgroundTypeRGB;
954 col.GetRGBColor(&tback.bg.color);
955 TXNSetBackground( m_txn , &tback );
956 }
957
958 static inline int wxConvertToTXN(int x)
959 {
960 return wx_static_cast(int, x / 254.0 * 72 + 0.5);
961 }
962
963 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
964 {
965 TXNTypeAttributes typeAttr[4] ;
966 RGBColor color ;
967 size_t typeAttrCount = 0 ;
968
969 TXNMargins margins;
970 TXNControlTag controlTags[4];
971 TXNControlData controlData[4];
972 size_t controlAttrCount = 0;
973
974 TXNTab* tabs = NULL;
975
976 bool relayout = false;
977 wxFont font ;
978
979 if ( style.HasFont() )
980 {
981 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
982 font = style.GetFont() ;
983 typeAttr[typeAttrCount].tag = kTXNATSUIStyle ;
984 typeAttr[typeAttrCount].size = kTXNATSUIStyleSize ;
985 typeAttr[typeAttrCount].data.dataPtr = font.MacGetATSUStyle() ;
986 typeAttrCount++ ;
987 }
988
989 if ( style.HasTextColour() )
990 {
991 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
992 style.GetTextColour().GetRGBColor( &color );
993 typeAttr[typeAttrCount].tag = kTXNQDFontColorAttribute ;
994 typeAttr[typeAttrCount].size = kTXNQDFontColorAttributeSize ;
995 typeAttr[typeAttrCount].data.dataPtr = (void*) &color ;
996 typeAttrCount++ ;
997 }
998
999 if ( style.HasAlignment() )
1000 {
1001 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1002 SInt32 align;
1003
1004 switch ( style.GetAlignment() )
1005 {
1006 case wxTEXT_ALIGNMENT_LEFT:
1007 align = kTXNFlushLeft;
1008 break;
1009 case wxTEXT_ALIGNMENT_CENTRE:
1010 align = kTXNCenter;
1011 break;
1012 case wxTEXT_ALIGNMENT_RIGHT:
1013 align = kTXNFlushRight;
1014 break;
1015 case wxTEXT_ALIGNMENT_JUSTIFIED:
1016 align = kTXNFullJust;
1017 break;
1018 default :
1019 case wxTEXT_ALIGNMENT_DEFAULT:
1020 align = kTXNFlushDefault;
1021 break;
1022 }
1023
1024 controlTags[controlAttrCount] = kTXNJustificationTag ;
1025 controlData[controlAttrCount].sValue = align ;
1026 controlAttrCount++ ;
1027 }
1028
1029 if ( style.HasLeftIndent() || style.HasRightIndent() )
1030 {
1031 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1032 controlTags[controlAttrCount] = kTXNMarginsTag;
1033 controlData[controlAttrCount].marginsPtr = &margins;
1034 verify_noerr( TXNGetTXNObjectControls (m_txn, 1 ,
1035 &controlTags[controlAttrCount], &controlData[controlAttrCount]) );
1036 if ( style.HasLeftIndent() )
1037 {
1038 margins.leftMargin = wxConvertToTXN(style.GetLeftIndent());
1039 }
1040 if ( style.HasRightIndent() )
1041 {
1042 margins.rightMargin = wxConvertToTXN(style.GetRightIndent());
1043 }
1044 controlAttrCount++ ;
1045 }
1046
1047 if ( style.HasTabs() )
1048 {
1049 const wxArrayInt& tabarray = style.GetTabs();
1050 // unfortunately Mac only applies a tab distance, not individually different tabs
1051 controlTags[controlAttrCount] = kTXNTabSettingsTag;
1052 if ( tabarray.size() > 0 )
1053 controlData[controlAttrCount].tabValue.value = wxConvertToTXN(tabarray[0]);
1054 else
1055 controlData[controlAttrCount].tabValue.value = 72 ;
1056
1057 controlData[controlAttrCount].tabValue.tabType = kTXNLeftTab;
1058 controlAttrCount++ ;
1059 }
1060
1061 // unfortunately the relayout is not automatic
1062 if ( controlAttrCount > 0 )
1063 {
1064 verify_noerr( TXNSetTXNObjectControls (m_txn, false /* don't clear all */, controlAttrCount,
1065 controlTags, controlData) );
1066 relayout = true;
1067 }
1068
1069 if ( typeAttrCount > 0 )
1070 {
1071 verify_noerr( TXNSetTypeAttributes( m_txn , typeAttrCount, typeAttr, from , to ) );
1072 relayout = true;
1073 }
1074
1075 if ( tabs != NULL )
1076 {
1077 delete[] tabs;
1078 }
1079
1080 if ( relayout )
1081 {
1082 TXNRecalcTextLayout( m_txn );
1083 }
1084 }
1085
1086 void wxMacMLTEControl::SetFont(const wxFont & font,
1087 const wxColour& foreground,
1088 long WXUNUSED(windowStyle))
1089 {
1090 wxMacEditHelper help( m_txn ) ;
1091 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1092 }
1093
1094 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1095 {
1096 wxMacEditHelper help( m_txn ) ;
1097 TXNSetAttribute( style, start, end ) ;
1098 }
1099
1100 void wxMacMLTEControl::Copy()
1101 {
1102 TXNCopy( m_txn );
1103 }
1104
1105 void wxMacMLTEControl::Cut()
1106 {
1107 TXNCut( m_txn );
1108 }
1109
1110 void wxMacMLTEControl::Paste()
1111 {
1112 TXNPaste( m_txn );
1113 }
1114
1115 bool wxMacMLTEControl::CanPaste() const
1116 {
1117 return TXNIsScrapPastable() ;
1118 }
1119
1120 void wxMacMLTEControl::SetEditable(bool editable)
1121 {
1122 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1123 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1124 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1125 }
1126
1127 wxTextPos wxMacMLTEControl::GetLastPosition() const
1128 {
1129 wxTextPos actualsize = 0 ;
1130
1131 Handle theText ;
1132 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1133
1134 // all done
1135 if ( err == noErr )
1136 {
1137 actualsize = GetHandleSize( theText ) ;
1138 DisposeHandle( theText ) ;
1139 }
1140 else
1141 {
1142 actualsize = 0 ;
1143 }
1144
1145 return actualsize ;
1146 }
1147
1148 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1149 {
1150 wxString value = str ;
1151 wxMacConvertNewlines10To13( &value ) ;
1152
1153 wxMacEditHelper help( m_txn ) ;
1154 #ifndef __LP64__
1155 wxMacWindowClipper c( GetWXPeer() ) ;
1156 #endif
1157
1158 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to ) ;
1159 TXNClear( m_txn ) ;
1160 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1161 }
1162
1163 void wxMacMLTEControl::Remove( long from , long to )
1164 {
1165 #ifndef __LP64__
1166 wxMacWindowClipper c( GetWXPeer() ) ;
1167 #endif
1168 wxMacEditHelper help( m_txn ) ;
1169 TXNSetSelection( m_txn , from , to ) ;
1170 TXNClear( m_txn ) ;
1171 }
1172
1173 void wxMacMLTEControl::GetSelection( long* from, long* to) const
1174 {
1175 TXNOffset f,t ;
1176 TXNGetSelection( m_txn , &f , &t ) ;
1177 *from = f;
1178 *to = t;
1179 }
1180
1181 void wxMacMLTEControl::SetSelection( long from , long to )
1182 {
1183 #ifndef __LP64__
1184 wxMacWindowClipper c( GetWXPeer() ) ;
1185 #endif
1186
1187 // change the selection
1188 if ((from == -1) && (to == -1))
1189 TXNSelectAll( m_txn );
1190 else
1191 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to );
1192
1193 TXNShowSelection( m_txn, kTXNShowStart );
1194 }
1195
1196 void wxMacMLTEControl::WriteText( const wxString& str )
1197 {
1198 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
1199 if ( !wxIsMainThread() )
1200 {
1201 #if wxOSX_USE_CARBON
1202 // unfortunately CW 8 is not able to correctly deduce the template types,
1203 // so we have to instantiate explicitly
1204 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( (wxTextCtrl*) GetWXPeer() , &wxTextCtrl::WriteText , str ) ;
1205 #endif
1206 return ;
1207 }
1208
1209 wxString st = str ;
1210 wxMacConvertNewlines10To13( &st ) ;
1211
1212 long start , end , dummy ;
1213
1214 GetSelection( &start , &dummy ) ;
1215 #ifndef __LP64__
1216 wxMacWindowClipper c( GetWXPeer() ) ;
1217 #endif
1218
1219 {
1220 wxMacEditHelper helper( m_txn ) ;
1221 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1222 }
1223
1224 GetSelection( &dummy, &end ) ;
1225
1226 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
1227 }
1228
1229 void wxMacMLTEControl::Clear()
1230 {
1231 #ifndef __LP64__
1232 wxMacWindowClipper c( GetWXPeer() ) ;
1233 #endif
1234 wxMacEditHelper st( m_txn ) ;
1235 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
1236 TXNClear( m_txn ) ;
1237 }
1238
1239 bool wxMacMLTEControl::CanUndo() const
1240 {
1241 return TXNCanUndo( m_txn , NULL ) ;
1242 }
1243
1244 void wxMacMLTEControl::Undo()
1245 {
1246 TXNUndo( m_txn ) ;
1247 }
1248
1249 bool wxMacMLTEControl::CanRedo() const
1250 {
1251 return TXNCanRedo( m_txn , NULL ) ;
1252 }
1253
1254 void wxMacMLTEControl::Redo()
1255 {
1256 TXNRedo( m_txn ) ;
1257 }
1258
1259 int wxMacMLTEControl::GetNumberOfLines() const
1260 {
1261 ItemCount lines = 0 ;
1262 TXNGetLineCount( m_txn, &lines ) ;
1263
1264 return lines ;
1265 }
1266
1267 long wxMacMLTEControl::XYToPosition(long x, long y) const
1268 {
1269 Point curpt ;
1270 wxTextPos lastpos ;
1271
1272 // TODO: find a better implementation : while we can get the
1273 // line metrics of a certain line, we don't get its starting
1274 // position, so it would probably be rather a binary search
1275 // for the start position
1276 long xpos = 0, ypos = 0 ;
1277 int lastHeight = 0 ;
1278 ItemCount n ;
1279
1280 lastpos = GetLastPosition() ;
1281 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
1282 {
1283 if ( y == ypos && x == xpos )
1284 return n ;
1285
1286 TXNOffsetToPoint( m_txn, n, &curpt ) ;
1287
1288 if ( curpt.v > lastHeight )
1289 {
1290 xpos = 0 ;
1291 if ( n > 0 )
1292 ++ypos ;
1293
1294 lastHeight = curpt.v ;
1295 }
1296 else
1297 ++xpos ;
1298 }
1299
1300 return 0 ;
1301 }
1302
1303 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
1304 {
1305 Point curpt ;
1306 wxTextPos lastpos ;
1307
1308 if ( y )
1309 *y = 0 ;
1310 if ( x )
1311 *x = 0 ;
1312
1313 lastpos = GetLastPosition() ;
1314 if ( pos <= lastpos )
1315 {
1316 // TODO: find a better implementation - while we can get the
1317 // line metrics of a certain line, we don't get its starting
1318 // position, so it would probably be rather a binary search
1319 // for the start position
1320 long xpos = 0, ypos = 0 ;
1321 int lastHeight = 0 ;
1322 ItemCount n ;
1323
1324 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
1325 {
1326 TXNOffsetToPoint( m_txn, n, &curpt ) ;
1327
1328 if ( curpt.v > lastHeight )
1329 {
1330 xpos = 0 ;
1331 if ( n > 0 )
1332 ++ypos ;
1333
1334 lastHeight = curpt.v ;
1335 }
1336 else
1337 ++xpos ;
1338 }
1339
1340 if ( y )
1341 *y = ypos ;
1342 if ( x )
1343 *x = xpos ;
1344 }
1345
1346 return false ;
1347 }
1348
1349 void wxMacMLTEControl::ShowPosition( long pos )
1350 {
1351 Point current, desired ;
1352 TXNOffset selstart, selend;
1353
1354 TXNGetSelection( m_txn, &selstart, &selend );
1355 TXNOffsetToPoint( m_txn, selstart, &current );
1356 TXNOffsetToPoint( m_txn, pos, &desired );
1357
1358 // TODO: use HIPoints for 10.3 and above
1359
1360 OSErr theErr = noErr;
1361 long dv = desired.v - current.v;
1362 long dh = desired.h - current.h;
1363 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
1364 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
1365
1366 // there will be an error returned for classic MLTE implementation when the control is
1367 // invisible, but HITextView works correctly, so we don't assert that one
1368 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
1369 }
1370
1371 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
1372 {
1373 #if wxUSE_UNICODE
1374 #if SIZEOF_WCHAR_T == 2
1375 size_t len = st.length() ;
1376 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
1377 #else
1378 wxMBConvUTF16 converter ;
1379 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
1380 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
1381 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
1382 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
1383 free( unibuf ) ;
1384 #endif
1385 #else
1386 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
1387 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
1388 #endif
1389 }
1390
1391 wxString wxMacMLTEControl::GetLineText(long lineNo) const
1392 {
1393 wxString line ;
1394
1395 if ( lineNo < GetNumberOfLines() )
1396 {
1397 Point firstPoint;
1398 Fixed lineWidth, lineHeight, currentHeight;
1399 long ypos ;
1400
1401 // get the first possible position in the control
1402 TXNOffsetToPoint(m_txn, 0, &firstPoint);
1403
1404 // Iterate through the lines until we reach the one we want,
1405 // adding to our current y pixel point position
1406 ypos = 0 ;
1407 currentHeight = 0;
1408 while (ypos < lineNo)
1409 {
1410 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
1411 currentHeight += lineHeight;
1412 }
1413
1414 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
1415 TXNOffset theOffset;
1416 TXNPointToOffset(m_txn, thePoint, &theOffset);
1417
1418 wxString content = GetStringValue() ;
1419 Point currentPoint = thePoint;
1420 while (thePoint.v == currentPoint.v && theOffset < content.length())
1421 {
1422 line += content[theOffset];
1423 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
1424 }
1425 }
1426
1427 return line ;
1428 }
1429
1430 int wxMacMLTEControl::GetLineLength(long lineNo) const
1431 {
1432 int theLength = 0;
1433
1434 if ( lineNo < GetNumberOfLines() )
1435 {
1436 Point firstPoint;
1437 Fixed lineWidth, lineHeight, currentHeight;
1438 long ypos;
1439
1440 // get the first possible position in the control
1441 TXNOffsetToPoint(m_txn, 0, &firstPoint);
1442
1443 // Iterate through the lines until we reach the one we want,
1444 // adding to our current y pixel point position
1445 ypos = 0;
1446 currentHeight = 0;
1447 while (ypos < lineNo)
1448 {
1449 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
1450 currentHeight += lineHeight;
1451 }
1452
1453 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
1454 TXNOffset theOffset;
1455 TXNPointToOffset(m_txn, thePoint, &theOffset);
1456
1457 wxString content = GetStringValue() ;
1458 Point currentPoint = thePoint;
1459 while (thePoint.v == currentPoint.v && theOffset < content.length())
1460 {
1461 ++theLength;
1462 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
1463 }
1464 }
1465
1466 return theLength ;
1467 }
1468
1469 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
1470
1471 // ----------------------------------------------------------------------------
1472 // MLTE control implementation (classic part)
1473 // ----------------------------------------------------------------------------
1474
1475 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
1476 // has to live on. We have different problems coming from outdated implementations on the
1477 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
1478 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
1479 // no way out, therefore we are using our own implementation and our own scrollbars ....
1480
1481 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
1482 ControlActionUPP gTXNScrollActionProc = NULL ;
1483
1484 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
1485 SInt32 iValue, SInt32 iMaximumValue,
1486 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
1487 {
1488 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
1489 SInt32 value = wxMax( iValue , 0 ) ;
1490 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
1491
1492 if ( iScrollBarOrientation == kTXNHorizontal )
1493 {
1494 if ( mlte->m_sbHorizontal )
1495 {
1496 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
1497 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
1498 mlte->m_lastHorizontalValue = value ;
1499 }
1500 }
1501 else if ( iScrollBarOrientation == kTXNVertical )
1502 {
1503 if ( mlte->m_sbVertical )
1504 {
1505 SetControl32BitValue( mlte->m_sbVertical , value ) ;
1506 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
1507 mlte->m_lastVerticalValue = value ;
1508 }
1509 }
1510 }
1511
1512 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
1513 {
1514 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
1515 if ( mlte == NULL )
1516 return ;
1517
1518 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
1519 return ;
1520
1521 OSStatus err ;
1522 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
1523
1524 SInt32 minimum = 0 ;
1525 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
1526 SInt32 value = GetControl32BitValue( controlRef ) ;
1527 SInt32 delta = 0;
1528
1529 switch ( partCode )
1530 {
1531 case kControlDownButtonPart :
1532 delta = 10 ;
1533 break ;
1534
1535 case kControlUpButtonPart :
1536 delta = -10 ;
1537 break ;
1538
1539 case kControlPageDownPart :
1540 delta = GetControlViewSize( controlRef ) ;
1541 break ;
1542
1543 case kControlPageUpPart :
1544 delta = -GetControlViewSize( controlRef ) ;
1545 break ;
1546
1547 case kControlIndicatorPart :
1548 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
1549 break ;
1550
1551 default :
1552 break ;
1553 }
1554
1555 if ( delta != 0 )
1556 {
1557 SInt32 newValue = value ;
1558
1559 if ( partCode != kControlIndicatorPart )
1560 {
1561 if ( value + delta < minimum )
1562 delta = minimum - value ;
1563 if ( value + delta > maximum )
1564 delta = maximum - value ;
1565
1566 SetControl32BitValue( controlRef , value + delta ) ;
1567 newValue = value + delta ;
1568 }
1569
1570 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
1571 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
1572
1573 err = TXNScroll(
1574 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
1575 &verticalDelta, &horizontalDelta );
1576 verify_noerr( err );
1577
1578 if ( isHorizontal )
1579 mlte->m_lastHorizontalValue = newValue ;
1580 else
1581 mlte->m_lastVerticalValue = newValue ;
1582 }
1583 }
1584
1585 // make correct activations
1586 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
1587 {
1588 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
1589
1590 wxMacWindowClipper clipper( textctrl ) ;
1591 TXNActivate( m_txn, m_txnFrameID, setActive );
1592
1593 ControlRef controlFocus = 0 ;
1594 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1595 if ( controlFocus == m_controlRef )
1596 TXNFocus( m_txn, setActive );
1597 }
1598
1599 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
1600 {
1601 TXNFocus( m_txn, setFocus );
1602 }
1603
1604 // guards against inappropriate redraw (hidden objects drawing onto window)
1605
1606 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
1607 {
1608 ControlRef controlFocus = 0 ;
1609 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1610
1611 if ( !vis && (controlFocus == m_controlRef ) )
1612 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
1613
1614 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
1615 TXNControlData iControlData[1] = { { (UInt32)false } };
1616
1617 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
1618
1619 if ( iControlData[0].uValue != vis )
1620 {
1621 iControlData[0].uValue = vis ;
1622 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
1623 }
1624
1625 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
1626 // if we run into further problems we might set the FrameBounds to an empty rect here
1627 }
1628
1629 // make sure that the TXNObject is at the right position
1630
1631 void wxMacMLTEClassicControl::MacUpdatePosition()
1632 {
1633 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1634 if ( textctrl == NULL )
1635 return ;
1636
1637 Rect bounds ;
1638 GetRectInWindowCoords( &bounds );
1639
1640 wxRect visRect = textctrl->MacGetClippedClientRect() ;
1641 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
1642 int x , y ;
1643 x = y = 0 ;
1644 textctrl->MacWindowToRootWindow( &x , &y ) ;
1645 OffsetRect( &visBounds , x , y ) ;
1646
1647 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
1648 {
1649 m_txnControlBounds = bounds ;
1650 m_txnVisBounds = visBounds ;
1651 wxMacWindowClipper cl( textctrl ) ;
1652
1653 if ( m_sbHorizontal || m_sbVertical )
1654 {
1655 int w = bounds.right - bounds.left ;
1656 int h = bounds.bottom - bounds.top ;
1657
1658 if ( m_sbHorizontal )
1659 {
1660 Rect sbBounds ;
1661
1662 sbBounds.left = -1 ;
1663 sbBounds.top = h - 14 ;
1664 sbBounds.right = w + 1 ;
1665 sbBounds.bottom = h + 1 ;
1666
1667 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
1668 SetControlViewSize( m_sbHorizontal , w ) ;
1669 }
1670
1671 if ( m_sbVertical )
1672 {
1673 Rect sbBounds ;
1674
1675 sbBounds.left = w - 14 ;
1676 sbBounds.top = -1 ;
1677 sbBounds.right = w + 1 ;
1678 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
1679
1680 SetControlBounds( m_sbVertical , &sbBounds ) ;
1681 SetControlViewSize( m_sbVertical , h ) ;
1682 }
1683 }
1684
1685 Rect oldviewRect ;
1686 TXNLongRect olddestRect ;
1687 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
1688
1689 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
1690 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
1691 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
1692 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
1693 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
1694 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
1695
1696 if ( olddestRect.right >= 10000 )
1697 destRect.right = destRect.left + 32000 ;
1698
1699 if ( olddestRect.bottom >= 0x20000000 )
1700 destRect.bottom = destRect.top + 0x40000000 ;
1701
1702 SectRect( &viewRect , &visBounds , &viewRect ) ;
1703 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
1704
1705 #if 0
1706 TXNSetFrameBounds(
1707 m_txn,
1708 m_txnControlBounds.top,
1709 m_txnControlBounds.left,
1710 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
1711 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
1712 m_txnFrameID );
1713 #endif
1714
1715 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
1716 // movement, therefore we have to force it
1717
1718 // this problem has been reported in OSX as well, so we use this here once again
1719
1720 TXNLongRect textRect ;
1721 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
1722 if ( textRect.left < m_txnControlBounds.left )
1723 TXNShowSelection( m_txn , kTXNShowStart ) ;
1724 }
1725 }
1726
1727 void wxMacMLTEClassicControl::Move(int x, int y, int width, int height)
1728 {
1729 wxMacControl::Move(x,y,width,height) ;
1730 MacUpdatePosition() ;
1731 }
1732
1733 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 WXUNUSED(thePart))
1734 {
1735 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1736 if ( textctrl == NULL )
1737 return ;
1738
1739 if ( textctrl->IsShownOnScreen() )
1740 {
1741 wxMacWindowClipper clipper( textctrl ) ;
1742 TXNDraw( m_txn , NULL ) ;
1743 }
1744 }
1745
1746 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
1747 {
1748 Point where = { y , x } ;
1749 ControlPartCode result = kControlNoPart;
1750
1751 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
1752 if ( (textctrl != NULL) && textctrl->IsShownOnScreen() )
1753 {
1754 if (PtInRect( where, &m_txnControlBounds ))
1755 {
1756 result = kControlEditTextPart ;
1757 }
1758 else
1759 {
1760 // sometimes we get the coords also in control local coordinates, therefore test again
1761 int x = 0 , y = 0 ;
1762 textctrl->MacClientToRootWindow( &x , &y ) ;
1763 where.h += x ;
1764 where.v += y ;
1765
1766 if (PtInRect( where, &m_txnControlBounds ))
1767 result = kControlEditTextPart ;
1768 }
1769 }
1770
1771 return result;
1772 }
1773
1774 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* WXUNUSED(actionProc) )
1775 {
1776 ControlPartCode result = kControlNoPart;
1777
1778 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
1779 if ( (textctrl != NULL) && textctrl->IsShownOnScreen() )
1780 {
1781 Point startPt = { y , x } ;
1782
1783 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
1784 int x = 0 , y = 0 ;
1785 textctrl->MacClientToRootWindow( &x , &y ) ;
1786 startPt.h += x ;
1787 startPt.v += y ;
1788
1789 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
1790 {
1791 case kControlEditTextPart :
1792 {
1793 wxMacWindowClipper clipper( textctrl ) ;
1794 EventRecord rec ;
1795
1796 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
1797 TXNClick( m_txn, &rec );
1798 }
1799 break;
1800
1801 default :
1802 break;
1803 }
1804 }
1805
1806 return result;
1807 }
1808
1809 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
1810 {
1811 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1812 if ( textctrl == NULL )
1813 return ;
1814
1815 if (textctrl->IsShownOnScreen())
1816 {
1817 if (IsControlActive(m_controlRef))
1818 {
1819 Point mousep;
1820
1821 wxMacWindowClipper clipper( textctrl ) ;
1822 GetMouse(&mousep);
1823
1824 TXNIdle(m_txn);
1825
1826 if (PtInRect(mousep, &m_txnControlBounds))
1827 {
1828 RgnHandle theRgn = NewRgn();
1829 RectRgn(theRgn, &m_txnControlBounds);
1830 TXNAdjustCursor(m_txn, theRgn);
1831 DisposeRgn(theRgn);
1832 }
1833 }
1834 }
1835 }
1836
1837 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
1838 {
1839 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1840 if ( textctrl == NULL )
1841 return kControlNoPart;
1842
1843 wxMacWindowClipper clipper( textctrl ) ;
1844
1845 EventRecord ev ;
1846 memset( &ev , 0 , sizeof( ev ) ) ;
1847 ev.what = keyDown ;
1848 ev.modifiers = modifiers ;
1849 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
1850 TXNKeyDown( m_txn , &ev );
1851
1852 return kControlEntireControl;
1853 }
1854
1855 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
1856 {
1857 MacActivatePaneText( activating );
1858 }
1859
1860 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
1861 {
1862 ControlPartCode focusResult = kControlFocusNoPart;
1863
1864 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1865 if ( textctrl == NULL )
1866 return focusResult;
1867
1868 wxMacWindowClipper clipper( textctrl ) ;
1869
1870 ControlRef controlFocus = NULL ;
1871 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1872 bool wasFocused = ( controlFocus == m_controlRef ) ;
1873
1874 switch (action)
1875 {
1876 case kControlFocusPrevPart:
1877 case kControlFocusNextPart:
1878 MacFocusPaneText( !wasFocused );
1879 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
1880 break;
1881
1882 case kControlFocusNoPart:
1883 default:
1884 MacFocusPaneText( false );
1885 focusResult = kControlFocusNoPart;
1886 break;
1887 }
1888
1889 return focusResult;
1890 }
1891
1892 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info) )
1893 {
1894 }
1895
1896 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
1897 const wxString& str,
1898 const wxPoint& pos,
1899 const wxSize& size, long style )
1900 : wxMacMLTEControl( wxPeer )
1901 {
1902 m_font = wxPeer->GetFont() ;
1903 m_windowStyle = style ;
1904 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1905
1906 short featureSet =
1907 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
1908 | kControlWantsActivate | kControlHandlesTracking
1909 // | kControlHasSpecialBackground
1910 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
1911
1912 OSStatus err = ::CreateUserPaneControl(
1913 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
1914 &bounds, featureSet, &m_controlRef );
1915 verify_noerr( err );
1916
1917 DoCreate();
1918
1919 AdjustCreationAttributes( *wxWHITE , true ) ;
1920
1921 MacSetObjectVisibility( wxPeer->IsShownOnScreen() ) ;
1922
1923 {
1924 wxString st = str ;
1925 wxMacConvertNewlines10To13( &st ) ;
1926 wxMacWindowClipper clipper( GetWXPeer() ) ;
1927 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
1928 TXNSetSelection( m_txn, 0, 0 ) ;
1929 }
1930 }
1931
1932 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
1933 {
1934 TXNDeleteObject( m_txn );
1935 m_txn = NULL ;
1936 }
1937
1938 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
1939 {
1940 MacSetObjectVisibility( shown ) ;
1941 wxMacControl::VisibilityChanged( shown ) ;
1942 }
1943
1944 void wxMacMLTEClassicControl::SuperChangedPosition()
1945 {
1946 MacUpdatePosition() ;
1947 wxMacControl::SuperChangedPosition() ;
1948 }
1949
1950 ControlUserPaneDrawUPP gTPDrawProc = NULL;
1951 ControlUserPaneHitTestUPP gTPHitProc = NULL;
1952 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
1953 ControlUserPaneIdleUPP gTPIdleProc = NULL;
1954 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
1955 ControlUserPaneActivateUPP gTPActivateProc = NULL;
1956 ControlUserPaneFocusUPP gTPFocusProc = NULL;
1957
1958 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
1959 {
1960 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
1961 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1962 if ( win )
1963 win->MacControlUserPaneDrawProc( part ) ;
1964 }
1965
1966 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
1967 {
1968 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
1969 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1970 if ( win )
1971 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
1972 else
1973 return kControlNoPart ;
1974 }
1975
1976 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
1977 {
1978 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
1979 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1980 if ( win )
1981 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
1982 else
1983 return kControlNoPart ;
1984 }
1985
1986 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
1987 {
1988 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
1989 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1990 if ( win )
1991 win->MacControlUserPaneIdleProc() ;
1992 }
1993
1994 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
1995 {
1996 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
1997 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1998 if ( win )
1999 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
2000 else
2001 return kControlNoPart ;
2002 }
2003
2004 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2005 {
2006 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget)control) , wxTextCtrl ) ;
2007 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2008 if ( win )
2009 win->MacControlUserPaneActivateProc( activating ) ;
2010 }
2011
2012 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2013 {
2014 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
2015 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2016 if ( win )
2017 return win->MacControlUserPaneFocusProc( action ) ;
2018 else
2019 return kControlNoPart ;
2020 }
2021
2022 #if 0
2023 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2024 {
2025 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget(control) , wxTextCtrl ) ;
2026 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2027 if ( win )
2028 win->MacControlUserPaneBackgroundProc(info) ;
2029 }
2030 #endif
2031
2032 // TXNRegisterScrollInfoProc
2033
2034 OSStatus wxMacMLTEClassicControl::DoCreate()
2035 {
2036 Rect bounds;
2037 OSStatus err = noErr ;
2038
2039 // set up our globals
2040 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2041 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2042 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2043 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2044 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2045 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2046 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2047
2048 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2049 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2050
2051 // set the initial settings for our private data
2052
2053 m_txnWindow = GetControlOwner(m_controlRef);
2054 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2055
2056 // set up the user pane procedures
2057 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2058 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2059 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2060 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2061 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2062 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2063 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2064
2065 // calculate the rectangles used by the control
2066 GetRectInWindowCoords( &bounds );
2067
2068 m_txnControlBounds = bounds ;
2069 m_txnVisBounds = bounds ;
2070
2071 CGrafPtr origPort ;
2072 GDHandle origDev ;
2073
2074 GetGWorld( &origPort, &origDev ) ;
2075 SetPort( m_txnPort );
2076
2077 // create the new edit field
2078 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2079
2080 // the scrollbars are not correctly embedded but are inserted at the root:
2081 // this gives us problems as we have erratic redraws even over the structure area
2082
2083 m_sbHorizontal = 0 ;
2084 m_sbVertical = 0 ;
2085 m_lastHorizontalValue = 0 ;
2086 m_lastVerticalValue = 0 ;
2087
2088 Rect sb = { 0 , 0 , 0 , 0 } ;
2089 if ( frameOptions & kTXNWantVScrollBarMask )
2090 {
2091 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2092 SetControlReference( m_sbVertical, (SInt32)this );
2093 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2094 ShowControl( m_sbVertical );
2095 EmbedControl( m_sbVertical , m_controlRef );
2096 frameOptions &= ~kTXNWantVScrollBarMask;
2097 }
2098
2099 if ( frameOptions & kTXNWantHScrollBarMask )
2100 {
2101 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2102 SetControlReference( m_sbHorizontal, (SInt32)this );
2103 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2104 ShowControl( m_sbHorizontal );
2105 EmbedControl( m_sbHorizontal, m_controlRef );
2106 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2107 }
2108
2109 err = TXNNewObject(
2110 NULL, m_txnWindow, &bounds, frameOptions,
2111 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2112 &m_txn, &m_txnFrameID, NULL );
2113 verify_noerr( err );
2114
2115 #if 0
2116 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2117 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2118 int toptag = WXSIZEOF( iControlTags ) ;
2119 TXNCarbonEventInfo cInfo ;
2120 cInfo.useCarbonEvents = false ;
2121 cInfo.filler = 0 ;
2122 cInfo.flags = 0 ;
2123 cInfo.fDictionary = NULL ;
2124
2125 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2126 #endif
2127
2128 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2129
2130 SetGWorld( origPort , origDev ) ;
2131
2132 return err;
2133 }
2134 #endif
2135
2136 // ----------------------------------------------------------------------------
2137 // MLTE control implementation (OSX part)
2138 // ----------------------------------------------------------------------------
2139
2140 // tiger multi-line textcontrols with no CR in the entire content
2141 // don't scroll automatically, so we need a hack.
2142 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2143 // is processed, thus the scrolling always occurs one character too late, but
2144 // better than nothing ...
2145
2146 static const EventTypeSpec eventList[] =
2147 {
2148 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
2149 } ;
2150
2151 static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
2152 {
2153 OSStatus result = eventNotHandledErr ;
2154 wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
2155
2156 switch ( GetEventKind( event ) )
2157 {
2158 case kEventTextInputUnicodeForKeyEvent :
2159 {
2160 TXNOffset from , to ;
2161 TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
2162 if ( from == to )
2163 TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
2164 result = CallNextEventHandler(handler,event);
2165 break;
2166 }
2167 default:
2168 break ;
2169 }
2170
2171 return result ;
2172 }
2173
2174 static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
2175 {
2176 OSStatus result = eventNotHandledErr ;
2177
2178 switch ( GetEventClass( event ) )
2179 {
2180 case kEventClassTextInput :
2181 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
2182 break ;
2183
2184 default :
2185 break ;
2186 }
2187 return result ;
2188 }
2189
2190 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
2191
2192 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
2193 const wxString& str,
2194 const wxPoint& pos,
2195 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
2196 {
2197 m_font = wxPeer->GetFont() ;
2198 m_windowStyle = style ;
2199 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2200 wxString st = str ;
2201 wxMacConvertNewlines10To13( &st ) ;
2202
2203 HIRect hr = {
2204 { bounds.left , bounds.top },
2205 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
2206
2207 m_scrollView = NULL ;
2208 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
2209 if (( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask)) || (frameOptions &kTXNSingleLineOnlyMask))
2210 {
2211 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
2212 {
2213 HIScrollViewCreate(
2214 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
2215 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
2216 &m_scrollView ) ;
2217 }
2218 else
2219 {
2220 HIScrollViewCreate(kHIScrollViewOptionsVertScroll,&m_scrollView);
2221 HIScrollViewSetScrollBarAutoHide(m_scrollView,true);
2222 }
2223
2224 HIViewSetFrame( m_scrollView, &hr );
2225 HIViewSetVisible( m_scrollView, true );
2226 }
2227
2228 m_textView = NULL ;
2229 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
2230 m_txn = HITextViewGetTXNObject( m_textView ) ;
2231 HIViewSetVisible( m_textView , true ) ;
2232 if ( m_scrollView )
2233 {
2234 HIViewAddSubview( m_scrollView , m_textView ) ;
2235 m_controlRef = m_scrollView ;
2236 wxMacControl::MacInstallEventHandler( m_textView, wxPeer ) ;
2237 }
2238 else
2239 {
2240 HIViewSetFrame( m_textView, &hr );
2241 m_controlRef = m_textView ;
2242 }
2243
2244 AdjustCreationAttributes( *wxWHITE , true ) ;
2245 #ifndef __LP64__
2246 wxMacWindowClipper c( GetWXPeer() ) ;
2247 #endif
2248 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2249
2250 TXNSetSelection( m_txn, 0, 0 );
2251 TXNShowSelection( m_txn, kTXNShowStart );
2252
2253 ::InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
2254 GetEventTypeCount(eventList), eventList, this,
2255 NULL);
2256 }
2257
2258 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
2259 {
2260 }
2261
2262 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
2263 {
2264 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, focusPart ) ;
2265 }
2266
2267 bool wxMacMLTEHIViewControl::HasFocus() const
2268 {
2269 ControlRef control ;
2270 if ( GetUserFocusWindow() == NULL )
2271 return false;
2272
2273 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2274 return control == m_textView ;
2275 }
2276
2277 void wxMacMLTEHIViewControl::SetBackgroundColour(const wxColour& col )
2278 {
2279 HITextViewSetBackgroundColor( m_textView, col.GetPixel() );
2280 }
2281
2282 #endif // wxUSE_TEXTCTRL