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