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