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