]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/textctrl.cpp
blind build fix for wxMac
[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
9f8062a0 297 virtual bool SetFocus() ;
489468fe
SC
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 511 GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
c9f9deab 512 (EventHandlerRef*) &m_macTextCtrlEventHandler);
489468fe
SC
513}
514
515wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
516{
c9f9deab 517 ::RemoveEventHandler((EventHandlerRef) m_macTextCtrlEventHandler);
489468fe
SC
518}
519
520void 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
538wxString 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
557void 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
489468fe
SC
565void wxMacUnicodeTextControl::Copy()
566{
567 SendHICommand( kHICommandCopy ) ;
568}
569
570void wxMacUnicodeTextControl::Cut()
571{
572 SendHICommand( kHICommandCut ) ;
573}
574
575void wxMacUnicodeTextControl::Paste()
576{
577 SendHICommand( kHICommandPaste ) ;
578}
579
580bool wxMacUnicodeTextControl::CanPaste() const
581{
582 return true ;
583}
584
585void 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
592void 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
606void 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
640void wxMacUnicodeTextControl::WriteText( const wxString& str )
641{
524c47aa
SC
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
489468fe
SC
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
682class wxMacEditHelper
683{
684public :
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
704protected :
705 TXNObject m_txn ;
706 TXNControlData m_data[1] ;
707} ;
708
709wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer )
1e181c7a 710 : wxMacControl( peer )
489468fe
SC
711{
712 SetNeedsFocusRect( true ) ;
713}
714
715wxString 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
796void wxMacMLTEControl::SetStringValue( const wxString &str )
797{
798 wxString st = str;
799 wxMacConvertNewlines10To13( &st );
800
801 {
802#ifndef __LP64__
b2680ced 803 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
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
816TXNFrameOptions 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
852void 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
944void 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
952static inline int wxConvertToTXN(int x)
953{
5c33522f 954 return static_cast<int>(x / 254.0 * 72 + 0.5);
489468fe
SC
955}
956
957void 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 ) );
24c7547c
SC
1066 if (from != to)
1067 relayout = true;
489468fe
SC
1068 }
1069
1070 if ( tabs != NULL )
1071 {
1072 delete[] tabs;
1073 }
1074
1075 if ( relayout )
1076 {
1077 TXNRecalcTextLayout( m_txn );
1078 }
1079}
1080
1081void wxMacMLTEControl::SetFont(const wxFont & font,
1082 const wxColour& foreground,
1083 long WXUNUSED(windowStyle))
1084{
1085 wxMacEditHelper help( m_txn ) ;
1086 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1087}
1088
1089void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1090{
1091 wxMacEditHelper help( m_txn ) ;
1092 TXNSetAttribute( style, start, end ) ;
1093}
1094
1095void wxMacMLTEControl::Copy()
1096{
1097 TXNCopy( m_txn );
1098}
1099
1100void wxMacMLTEControl::Cut()
1101{
1102 TXNCut( m_txn );
1103}
1104
1105void wxMacMLTEControl::Paste()
1106{
1107 TXNPaste( m_txn );
1108}
1109
1110bool wxMacMLTEControl::CanPaste() const
1111{
1112 return TXNIsScrapPastable() ;
1113}
1114
1115void wxMacMLTEControl::SetEditable(bool editable)
1116{
1117 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1118 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1119 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1120}
1121
0b6a49c2 1122long wxMacMLTEControl::GetLastPosition() const
489468fe
SC
1123{
1124 wxTextPos actualsize = 0 ;
1125
1126 Handle theText ;
1127 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1128
1129 // all done
1130 if ( err == noErr )
1131 {
1132 actualsize = GetHandleSize( theText ) ;
1133 DisposeHandle( theText ) ;
1134 }
1135 else
1136 {
1137 actualsize = 0 ;
1138 }
1139
1140 return actualsize ;
1141}
1142
1143void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1144{
1145 wxString value = str ;
1146 wxMacConvertNewlines10To13( &value ) ;
1147
1148 wxMacEditHelper help( m_txn ) ;
1149#ifndef __LP64__
b2680ced 1150 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
1151#endif
1152
1153 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to ) ;
1154 TXNClear( m_txn ) ;
1155 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1156}
1157
1158void wxMacMLTEControl::Remove( long from , long to )
1159{
1160#ifndef __LP64__
b2680ced 1161 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
1162#endif
1163 wxMacEditHelper help( m_txn ) ;
1164 TXNSetSelection( m_txn , from , to ) ;
1165 TXNClear( m_txn ) ;
1166}
1167
1168void wxMacMLTEControl::GetSelection( long* from, long* to) const
1169{
1170 TXNOffset f,t ;
1171 TXNGetSelection( m_txn , &f , &t ) ;
1172 *from = f;
1173 *to = t;
1174}
1175
1176void wxMacMLTEControl::SetSelection( long from , long to )
1177{
1178#ifndef __LP64__
b2680ced 1179 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
1180#endif
1181
1182 // change the selection
1183 if ((from == -1) && (to == -1))
1184 TXNSelectAll( m_txn );
1185 else
1186 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to );
1187
1188 TXNShowSelection( m_txn, kTXNShowStart );
1189}
1190
1191void wxMacMLTEControl::WriteText( const wxString& str )
1192{
524c47aa
SC
1193 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
1194 if ( !wxIsMainThread() )
1195 {
1196#if wxOSX_USE_CARBON
1197 // unfortunately CW 8 is not able to correctly deduce the template types,
1198 // so we have to instantiate explicitly
1199 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( (wxTextCtrl*) GetWXPeer() , &wxTextCtrl::WriteText , str ) ;
1200#endif
1201 return ;
1202 }
1203
489468fe
SC
1204 wxString st = str ;
1205 wxMacConvertNewlines10To13( &st ) ;
1206
1207 long start , end , dummy ;
1208
1209 GetSelection( &start , &dummy ) ;
1210#ifndef __LP64__
b2680ced 1211 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
1212#endif
1213
1214 {
1215 wxMacEditHelper helper( m_txn ) ;
1216 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1217 }
1218
1219 GetSelection( &dummy, &end ) ;
1220
1221 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
1222}
1223
1224void wxMacMLTEControl::Clear()
1225{
1226#ifndef __LP64__
b2680ced 1227 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
1228#endif
1229 wxMacEditHelper st( m_txn ) ;
1230 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
1231 TXNClear( m_txn ) ;
1232}
1233
1234bool wxMacMLTEControl::CanUndo() const
1235{
1236 return TXNCanUndo( m_txn , NULL ) ;
1237}
1238
1239void wxMacMLTEControl::Undo()
1240{
1241 TXNUndo( m_txn ) ;
1242}
1243
1244bool wxMacMLTEControl::CanRedo() const
1245{
1246 return TXNCanRedo( m_txn , NULL ) ;
1247}
1248
1249void wxMacMLTEControl::Redo()
1250{
1251 TXNRedo( m_txn ) ;
1252}
1253
1254int wxMacMLTEControl::GetNumberOfLines() const
1255{
1256 ItemCount lines = 0 ;
1257 TXNGetLineCount( m_txn, &lines ) ;
1258
1259 return lines ;
1260}
1261
1262long wxMacMLTEControl::XYToPosition(long x, long y) const
1263{
1264 Point curpt ;
1265 wxTextPos lastpos ;
1266
1267 // TODO: find a better implementation : while we can get the
1268 // line metrics of a certain line, we don't get its starting
1269 // position, so it would probably be rather a binary search
1270 // for the start position
1271 long xpos = 0, ypos = 0 ;
1272 int lastHeight = 0 ;
1273 ItemCount n ;
1274
1275 lastpos = GetLastPosition() ;
1276 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
1277 {
1278 if ( y == ypos && x == xpos )
1279 return n ;
1280
1281 TXNOffsetToPoint( m_txn, n, &curpt ) ;
1282
1283 if ( curpt.v > lastHeight )
1284 {
1285 xpos = 0 ;
1286 if ( n > 0 )
1287 ++ypos ;
1288
1289 lastHeight = curpt.v ;
1290 }
1291 else
1292 ++xpos ;
1293 }
1294
1295 return 0 ;
1296}
1297
1298bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
1299{
1300 Point curpt ;
1301 wxTextPos lastpos ;
1302
1303 if ( y )
1304 *y = 0 ;
1305 if ( x )
1306 *x = 0 ;
1307
1308 lastpos = GetLastPosition() ;
1309 if ( pos <= lastpos )
1310 {
1311 // TODO: find a better implementation - while we can get the
1312 // line metrics of a certain line, we don't get its starting
1313 // position, so it would probably be rather a binary search
1314 // for the start position
1315 long xpos = 0, ypos = 0 ;
1316 int lastHeight = 0 ;
1317 ItemCount n ;
1318
1319 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
1320 {
1321 TXNOffsetToPoint( m_txn, n, &curpt ) ;
1322
1323 if ( curpt.v > lastHeight )
1324 {
1325 xpos = 0 ;
1326 if ( n > 0 )
1327 ++ypos ;
1328
1329 lastHeight = curpt.v ;
1330 }
1331 else
1332 ++xpos ;
1333 }
1334
1335 if ( y )
1336 *y = ypos ;
1337 if ( x )
1338 *x = xpos ;
1339 }
1340
1341 return false ;
1342}
1343
1344void wxMacMLTEControl::ShowPosition( long pos )
1345{
1346 Point current, desired ;
1347 TXNOffset selstart, selend;
1348
1349 TXNGetSelection( m_txn, &selstart, &selend );
1350 TXNOffsetToPoint( m_txn, selstart, &current );
1351 TXNOffsetToPoint( m_txn, pos, &desired );
1352
1353 // TODO: use HIPoints for 10.3 and above
1354
1355 OSErr theErr = noErr;
1356 long dv = desired.v - current.v;
1357 long dh = desired.h - current.h;
1358 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
1359 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
1360
1361 // there will be an error returned for classic MLTE implementation when the control is
1362 // invisible, but HITextView works correctly, so we don't assert that one
1363 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
1364}
1365
1366void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
1367{
1368#if wxUSE_UNICODE
1369#if SIZEOF_WCHAR_T == 2
1370 size_t len = st.length() ;
1371 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
1372#else
1373 wxMBConvUTF16 converter ;
1374 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
51725fc0
VZ
1375 wxASSERT_MSG( byteBufferLen != wxCONV_FAILED,
1376 _T("Conversion to UTF-16 unexpectedly failed") );
1377 UniChar *unibuf = (UniChar*)malloc( byteBufferLen + 2 ) ; // 2 for NUL in UTF-16
1378 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen + 2 ) ;
489468fe
SC
1379 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
1380 free( unibuf ) ;
1381#endif
1382#else
1383 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
1384 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
1385#endif
1386}
1387
1388wxString wxMacMLTEControl::GetLineText(long lineNo) const
1389{
1390 wxString line ;
1391
1392 if ( lineNo < GetNumberOfLines() )
1393 {
1394 Point firstPoint;
1395 Fixed lineWidth, lineHeight, currentHeight;
1396 long ypos ;
1397
1398 // get the first possible position in the control
1399 TXNOffsetToPoint(m_txn, 0, &firstPoint);
1400
1401 // Iterate through the lines until we reach the one we want,
1402 // adding to our current y pixel point position
1403 ypos = 0 ;
1404 currentHeight = 0;
1405 while (ypos < lineNo)
1406 {
1407 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
1408 currentHeight += lineHeight;
1409 }
1410
1411 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
1412 TXNOffset theOffset;
1413 TXNPointToOffset(m_txn, thePoint, &theOffset);
1414
1415 wxString content = GetStringValue() ;
1416 Point currentPoint = thePoint;
1417 while (thePoint.v == currentPoint.v && theOffset < content.length())
1418 {
1419 line += content[theOffset];
1420 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
1421 }
1422 }
1423
1424 return line ;
1425}
1426
1427int wxMacMLTEControl::GetLineLength(long lineNo) const
1428{
1429 int theLength = 0;
1430
1431 if ( lineNo < GetNumberOfLines() )
1432 {
1433 Point firstPoint;
1434 Fixed lineWidth, lineHeight, currentHeight;
1435 long ypos;
1436
1437 // get the first possible position in the control
1438 TXNOffsetToPoint(m_txn, 0, &firstPoint);
1439
1440 // Iterate through the lines until we reach the one we want,
1441 // adding to our current y pixel point position
1442 ypos = 0;
1443 currentHeight = 0;
1444 while (ypos < lineNo)
1445 {
1446 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
1447 currentHeight += lineHeight;
1448 }
1449
1450 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
1451 TXNOffset theOffset;
1452 TXNPointToOffset(m_txn, thePoint, &theOffset);
1453
1454 wxString content = GetStringValue() ;
1455 Point currentPoint = thePoint;
1456 while (thePoint.v == currentPoint.v && theOffset < content.length())
1457 {
1458 ++theLength;
1459 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
1460 }
1461 }
1462
1463 return theLength ;
1464}
1465
1466#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
1467
1468// ----------------------------------------------------------------------------
1469// MLTE control implementation (classic part)
1470// ----------------------------------------------------------------------------
1471
1472// OS X Notes : We still don't have a full replacement for MLTE, so this implementation
1473// has to live on. We have different problems coming from outdated implementations on the
1474// various OS X versions. Most deal with the scrollbars: they are not correctly embedded
1475// while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
1476// no way out, therefore we are using our own implementation and our own scrollbars ....
1477
1478TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
1479ControlActionUPP gTXNScrollActionProc = NULL ;
1480
1481pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
1482 SInt32 iValue, SInt32 iMaximumValue,
1483 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
1484{
1485 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
1486 SInt32 value = wxMax( iValue , 0 ) ;
1487 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
1488
1489 if ( iScrollBarOrientation == kTXNHorizontal )
1490 {
1491 if ( mlte->m_sbHorizontal )
1492 {
1493 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
1494 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
1495 mlte->m_lastHorizontalValue = value ;
1496 }
1497 }
1498 else if ( iScrollBarOrientation == kTXNVertical )
1499 {
1500 if ( mlte->m_sbVertical )
1501 {
1502 SetControl32BitValue( mlte->m_sbVertical , value ) ;
1503 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
1504 mlte->m_lastVerticalValue = value ;
1505 }
1506 }
1507}
1508
1509pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
1510{
1511 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
1512 if ( mlte == NULL )
1513 return ;
1514
1515 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
1516 return ;
1517
1518 OSStatus err ;
1519 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
1520
1521 SInt32 minimum = 0 ;
1522 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
1523 SInt32 value = GetControl32BitValue( controlRef ) ;
1524 SInt32 delta = 0;
1525
1526 switch ( partCode )
1527 {
1528 case kControlDownButtonPart :
1529 delta = 10 ;
1530 break ;
1531
1532 case kControlUpButtonPart :
1533 delta = -10 ;
1534 break ;
1535
1536 case kControlPageDownPart :
1537 delta = GetControlViewSize( controlRef ) ;
1538 break ;
1539
1540 case kControlPageUpPart :
1541 delta = -GetControlViewSize( controlRef ) ;
1542 break ;
1543
1544 case kControlIndicatorPart :
1545 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
1546 break ;
1547
1548 default :
1549 break ;
1550 }
1551
1552 if ( delta != 0 )
1553 {
1554 SInt32 newValue = value ;
1555
1556 if ( partCode != kControlIndicatorPart )
1557 {
1558 if ( value + delta < minimum )
1559 delta = minimum - value ;
1560 if ( value + delta > maximum )
1561 delta = maximum - value ;
1562
1563 SetControl32BitValue( controlRef , value + delta ) ;
1564 newValue = value + delta ;
1565 }
1566
1567 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
1568 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
1569
1570 err = TXNScroll(
1571 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
1572 &verticalDelta, &horizontalDelta );
1573 verify_noerr( err );
1574
1575 if ( isHorizontal )
1576 mlte->m_lastHorizontalValue = newValue ;
1577 else
1578 mlte->m_lastVerticalValue = newValue ;
1579 }
1580}
1581
1582// make correct activations
1583void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
1584{
1585 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
1586
1587 wxMacWindowClipper clipper( textctrl ) ;
1588 TXNActivate( m_txn, m_txnFrameID, setActive );
1589
1590 ControlRef controlFocus = 0 ;
1591 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1592 if ( controlFocus == m_controlRef )
1593 TXNFocus( m_txn, setActive );
1594}
1595
1596void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
1597{
1598 TXNFocus( m_txn, setFocus );
1599}
1600
1601// guards against inappropriate redraw (hidden objects drawing onto window)
1602
1603void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
1604{
1605 ControlRef controlFocus = 0 ;
1606 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1607
1608 if ( !vis && (controlFocus == m_controlRef ) )
1609 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
1610
1611 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
1612 TXNControlData iControlData[1] = { { (UInt32)false } };
1613
1614 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
1615
1616 if ( iControlData[0].uValue != vis )
1617 {
1618 iControlData[0].uValue = vis ;
1619 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
1620 }
1621
1622 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
1623 // if we run into further problems we might set the FrameBounds to an empty rect here
1624}
1625
1626// make sure that the TXNObject is at the right position
1627
1628void wxMacMLTEClassicControl::MacUpdatePosition()
1629{
1630 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1631 if ( textctrl == NULL )
1632 return ;
1633
1634 Rect bounds ;
1635 GetRectInWindowCoords( &bounds );
1636
1637 wxRect visRect = textctrl->MacGetClippedClientRect() ;
1638 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
1639 int x , y ;
1640 x = y = 0 ;
1641 textctrl->MacWindowToRootWindow( &x , &y ) ;
1642 OffsetRect( &visBounds , x , y ) ;
1643
1644 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
1645 {
1646 m_txnControlBounds = bounds ;
1647 m_txnVisBounds = visBounds ;
1648 wxMacWindowClipper cl( textctrl ) ;
1649
1650 if ( m_sbHorizontal || m_sbVertical )
1651 {
1652 int w = bounds.right - bounds.left ;
1653 int h = bounds.bottom - bounds.top ;
1654
1655 if ( m_sbHorizontal )
1656 {
1657 Rect sbBounds ;
1658
1659 sbBounds.left = -1 ;
1660 sbBounds.top = h - 14 ;
1661 sbBounds.right = w + 1 ;
1662 sbBounds.bottom = h + 1 ;
1663
1664 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
1665 SetControlViewSize( m_sbHorizontal , w ) ;
1666 }
1667
1668 if ( m_sbVertical )
1669 {
1670 Rect sbBounds ;
1671
1672 sbBounds.left = w - 14 ;
1673 sbBounds.top = -1 ;
1674 sbBounds.right = w + 1 ;
1675 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
1676
1677 SetControlBounds( m_sbVertical , &sbBounds ) ;
1678 SetControlViewSize( m_sbVertical , h ) ;
1679 }
1680 }
1681
1682 Rect oldviewRect ;
1683 TXNLongRect olddestRect ;
1684 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
1685
1686 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
1687 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
1688 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
1689 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
1690 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
1691 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
1692
1693 if ( olddestRect.right >= 10000 )
1694 destRect.right = destRect.left + 32000 ;
1695
1696 if ( olddestRect.bottom >= 0x20000000 )
1697 destRect.bottom = destRect.top + 0x40000000 ;
1698
1699 SectRect( &viewRect , &visBounds , &viewRect ) ;
1700 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
1701
1702#if 0
1703 TXNSetFrameBounds(
1704 m_txn,
1705 m_txnControlBounds.top,
1706 m_txnControlBounds.left,
1707 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
1708 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
1709 m_txnFrameID );
1710#endif
1711
1712 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
1713 // movement, therefore we have to force it
1714
1715 // this problem has been reported in OSX as well, so we use this here once again
1716
1717 TXNLongRect textRect ;
1718 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
1719 if ( textRect.left < m_txnControlBounds.left )
1720 TXNShowSelection( m_txn , kTXNShowStart ) ;
1721 }
1722}
1723
b2680ced 1724void wxMacMLTEClassicControl::Move(int x, int y, int width, int height)
489468fe 1725{
b2680ced 1726 wxMacControl::Move(x,y,width,height) ;
489468fe
SC
1727 MacUpdatePosition() ;
1728}
1729
1730void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 WXUNUSED(thePart))
1731{
1732 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1733 if ( textctrl == NULL )
1734 return ;
1735
1736 if ( textctrl->IsShownOnScreen() )
1737 {
1738 wxMacWindowClipper clipper( textctrl ) ;
1739 TXNDraw( m_txn , NULL ) ;
1740 }
1741}
1742
1743wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
1744{
1745 Point where = { y , x } ;
1746 ControlPartCode result = kControlNoPart;
1747
1748 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
1749 if ( (textctrl != NULL) && textctrl->IsShownOnScreen() )
1750 {
1751 if (PtInRect( where, &m_txnControlBounds ))
1752 {
1753 result = kControlEditTextPart ;
1754 }
1755 else
1756 {
1757 // sometimes we get the coords also in control local coordinates, therefore test again
1758 int x = 0 , y = 0 ;
1759 textctrl->MacClientToRootWindow( &x , &y ) ;
1760 where.h += x ;
1761 where.v += y ;
1762
1763 if (PtInRect( where, &m_txnControlBounds ))
1764 result = kControlEditTextPart ;
1765 }
1766 }
1767
1768 return result;
1769}
1770
1771wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* WXUNUSED(actionProc) )
1772{
1773 ControlPartCode result = kControlNoPart;
1774
1775 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
1776 if ( (textctrl != NULL) && textctrl->IsShownOnScreen() )
1777 {
1778 Point startPt = { y , x } ;
1779
1780 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
1781 int x = 0 , y = 0 ;
1782 textctrl->MacClientToRootWindow( &x , &y ) ;
1783 startPt.h += x ;
1784 startPt.v += y ;
1785
1786 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
1787 {
1788 case kControlEditTextPart :
1789 {
1790 wxMacWindowClipper clipper( textctrl ) ;
1791 EventRecord rec ;
1792
1793 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
1794 TXNClick( m_txn, &rec );
1795 }
1796 break;
1797
1798 default :
1799 break;
1800 }
1801 }
1802
1803 return result;
1804}
1805
1806void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
1807{
1808 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1809 if ( textctrl == NULL )
1810 return ;
1811
1812 if (textctrl->IsShownOnScreen())
1813 {
1814 if (IsControlActive(m_controlRef))
1815 {
1816 Point mousep;
1817
1818 wxMacWindowClipper clipper( textctrl ) ;
1819 GetMouse(&mousep);
1820
1821 TXNIdle(m_txn);
1822
1823 if (PtInRect(mousep, &m_txnControlBounds))
1824 {
1825 RgnHandle theRgn = NewRgn();
1826 RectRgn(theRgn, &m_txnControlBounds);
1827 TXNAdjustCursor(m_txn, theRgn);
1828 DisposeRgn(theRgn);
1829 }
1830 }
1831 }
1832}
1833
1834wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
1835{
1836 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1837 if ( textctrl == NULL )
1838 return kControlNoPart;
1839
1840 wxMacWindowClipper clipper( textctrl ) ;
1841
1842 EventRecord ev ;
1843 memset( &ev , 0 , sizeof( ev ) ) ;
1844 ev.what = keyDown ;
1845 ev.modifiers = modifiers ;
1846 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
1847 TXNKeyDown( m_txn , &ev );
1848
1849 return kControlEntireControl;
1850}
1851
1852void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
1853{
1854 MacActivatePaneText( activating );
1855}
1856
1857wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
1858{
1859 ControlPartCode focusResult = kControlFocusNoPart;
1860
1861 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
1862 if ( textctrl == NULL )
1863 return focusResult;
1864
1865 wxMacWindowClipper clipper( textctrl ) ;
1866
1867 ControlRef controlFocus = NULL ;
1868 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
1869 bool wasFocused = ( controlFocus == m_controlRef ) ;
1870
1871 switch (action)
1872 {
1873 case kControlFocusPrevPart:
1874 case kControlFocusNextPart:
1875 MacFocusPaneText( !wasFocused );
1876 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
1877 break;
1878
1879 case kControlFocusNoPart:
1880 default:
1881 MacFocusPaneText( false );
1882 focusResult = kControlFocusNoPart;
1883 break;
1884 }
1885
1886 return focusResult;
1887}
1888
1889void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info) )
1890{
1891}
1892
1893wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
1894 const wxString& str,
1895 const wxPoint& pos,
1896 const wxSize& size, long style )
1897 : wxMacMLTEControl( wxPeer )
1898{
1899 m_font = wxPeer->GetFont() ;
1900 m_windowStyle = style ;
1901 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1902
1903 short featureSet =
1904 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
1905 | kControlWantsActivate | kControlHandlesTracking
1906// | kControlHasSpecialBackground
1907 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
1908
1909 OSStatus err = ::CreateUserPaneControl(
1910 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
1911 &bounds, featureSet, &m_controlRef );
1912 verify_noerr( err );
c4825ef7 1913 SetControlReference( m_controlRef , (URefCon) wxPeer );
489468fe
SC
1914
1915 DoCreate();
1916
1917 AdjustCreationAttributes( *wxWHITE , true ) ;
1918
1919 MacSetObjectVisibility( wxPeer->IsShownOnScreen() ) ;
1920
1921 {
1922 wxString st = str ;
1923 wxMacConvertNewlines10To13( &st ) ;
b2680ced 1924 wxMacWindowClipper clipper( GetWXPeer() ) ;
489468fe
SC
1925 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
1926 TXNSetSelection( m_txn, 0, 0 ) ;
1927 }
1928}
1929
1930wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
1931{
1932 TXNDeleteObject( m_txn );
1933 m_txn = NULL ;
1934}
1935
1936void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
1937{
1938 MacSetObjectVisibility( shown ) ;
1939 wxMacControl::VisibilityChanged( shown ) ;
1940}
1941
1942void wxMacMLTEClassicControl::SuperChangedPosition()
1943{
1944 MacUpdatePosition() ;
1945 wxMacControl::SuperChangedPosition() ;
1946}
1947
1948ControlUserPaneDrawUPP gTPDrawProc = NULL;
1949ControlUserPaneHitTestUPP gTPHitProc = NULL;
1950ControlUserPaneTrackingUPP gTPTrackProc = NULL;
1951ControlUserPaneIdleUPP gTPIdleProc = NULL;
1952ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
1953ControlUserPaneActivateUPP gTPActivateProc = NULL;
1954ControlUserPaneFocusUPP gTPFocusProc = NULL;
1955
1956static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
1957{
b2680ced 1958 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
1959 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1960 if ( win )
1961 win->MacControlUserPaneDrawProc( part ) ;
1962}
1963
1964static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
1965{
b2680ced 1966 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
1967 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1968 if ( win )
1969 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
1970 else
1971 return kControlNoPart ;
1972}
1973
1974static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
1975{
b2680ced 1976 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
1977 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1978 if ( win )
1979 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
1980 else
1981 return kControlNoPart ;
1982}
1983
1984static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
1985{
b2680ced 1986 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
1987 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1988 if ( win )
1989 win->MacControlUserPaneIdleProc() ;
1990}
1991
1992static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
1993{
b2680ced 1994 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
1995 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
1996 if ( win )
1997 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
1998 else
1999 return kControlNoPart ;
2000}
2001
2002static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2003{
b2680ced 2004 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget( (WXWidget)control) , wxTextCtrl ) ;
489468fe
SC
2005 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2006 if ( win )
2007 win->MacControlUserPaneActivateProc( activating ) ;
2008}
2009
2010static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2011{
b2680ced 2012 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget((WXWidget) control) , wxTextCtrl ) ;
489468fe
SC
2013 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2014 if ( win )
2015 return win->MacControlUserPaneFocusProc( action ) ;
2016 else
2017 return kControlNoPart ;
2018}
2019
2020#if 0
2021static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2022{
b2680ced 2023 wxTextCtrl *textCtrl = wxDynamicCast( wxFindWindowFromWXWidget(control) , wxTextCtrl ) ;
489468fe
SC
2024 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2025 if ( win )
2026 win->MacControlUserPaneBackgroundProc(info) ;
2027}
2028#endif
2029
2030// TXNRegisterScrollInfoProc
2031
2032OSStatus wxMacMLTEClassicControl::DoCreate()
2033{
2034 Rect bounds;
2035 OSStatus err = noErr ;
2036
2037 // set up our globals
2038 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2039 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2040 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2041 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2042 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2043 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2044 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2045
2046 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2047 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2048
2049 // set the initial settings for our private data
2050
2051 m_txnWindow = GetControlOwner(m_controlRef);
2052 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2053
2054 // set up the user pane procedures
2055 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2056 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2057 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2058 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2059 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2060 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2061 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2062
2063 // calculate the rectangles used by the control
2064 GetRectInWindowCoords( &bounds );
2065
2066 m_txnControlBounds = bounds ;
2067 m_txnVisBounds = bounds ;
2068
2069 CGrafPtr origPort ;
2070 GDHandle origDev ;
2071
2072 GetGWorld( &origPort, &origDev ) ;
2073 SetPort( m_txnPort );
2074
2075 // create the new edit field
2076 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2077
2078 // the scrollbars are not correctly embedded but are inserted at the root:
2079 // this gives us problems as we have erratic redraws even over the structure area
2080
2081 m_sbHorizontal = 0 ;
2082 m_sbVertical = 0 ;
2083 m_lastHorizontalValue = 0 ;
2084 m_lastVerticalValue = 0 ;
2085
2086 Rect sb = { 0 , 0 , 0 , 0 } ;
2087 if ( frameOptions & kTXNWantVScrollBarMask )
2088 {
2089 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2090 SetControlReference( m_sbVertical, (SInt32)this );
2091 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2092 ShowControl( m_sbVertical );
2093 EmbedControl( m_sbVertical , m_controlRef );
2094 frameOptions &= ~kTXNWantVScrollBarMask;
2095 }
2096
2097 if ( frameOptions & kTXNWantHScrollBarMask )
2098 {
2099 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2100 SetControlReference( m_sbHorizontal, (SInt32)this );
2101 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2102 ShowControl( m_sbHorizontal );
2103 EmbedControl( m_sbHorizontal, m_controlRef );
2104 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2105 }
2106
2107 err = TXNNewObject(
2108 NULL, m_txnWindow, &bounds, frameOptions,
2109 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2110 &m_txn, &m_txnFrameID, NULL );
2111 verify_noerr( err );
2112
2113#if 0
2114 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2115 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2116 int toptag = WXSIZEOF( iControlTags ) ;
2117 TXNCarbonEventInfo cInfo ;
2118 cInfo.useCarbonEvents = false ;
2119 cInfo.filler = 0 ;
2120 cInfo.flags = 0 ;
2121 cInfo.fDictionary = NULL ;
2122
2123 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2124#endif
2125
2126 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2127
2128 SetGWorld( origPort , origDev ) ;
2129
2130 return err;
2131}
2132#endif
2133
2134// ----------------------------------------------------------------------------
2135// MLTE control implementation (OSX part)
2136// ----------------------------------------------------------------------------
2137
2138// tiger multi-line textcontrols with no CR in the entire content
2139// don't scroll automatically, so we need a hack.
2140// This attempt only works 'before' the key (ie before CallNextEventHandler)
2141// is processed, thus the scrolling always occurs one character too late, but
2142// better than nothing ...
2143
2144static const EventTypeSpec eventList[] =
2145{
2146 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
2147} ;
2148
2149static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
2150{
2151 OSStatus result = eventNotHandledErr ;
2152 wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
2153
2154 switch ( GetEventKind( event ) )
2155 {
2156 case kEventTextInputUnicodeForKeyEvent :
2157 {
2158 TXNOffset from , to ;
2159 TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
2160 if ( from == to )
2161 TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
2162 result = CallNextEventHandler(handler,event);
2163 break;
2164 }
2165 default:
2166 break ;
2167 }
2168
2169 return result ;
2170}
2171
2172static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
2173{
2174 OSStatus result = eventNotHandledErr ;
2175
2176 switch ( GetEventClass( event ) )
2177 {
2178 case kEventClassTextInput :
2179 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
2180 break ;
2181
2182 default :
2183 break ;
2184 }
2185 return result ;
2186}
2187
2188DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
2189
2190wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
2191 const wxString& str,
2192 const wxPoint& pos,
2193 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
2194{
2195 m_font = wxPeer->GetFont() ;
2196 m_windowStyle = style ;
2197 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2198 wxString st = str ;
2199 wxMacConvertNewlines10To13( &st ) ;
2200
2201 HIRect hr = {
2202 { bounds.left , bounds.top },
2203 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
2204
2205 m_scrollView = NULL ;
2206 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
2207 if (( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask)) || (frameOptions &kTXNSingleLineOnlyMask))
2208 {
2209 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
2210 {
2211 HIScrollViewCreate(
2212 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
2213 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
2214 &m_scrollView ) ;
2215 }
2216 else
2217 {
2218 HIScrollViewCreate(kHIScrollViewOptionsVertScroll,&m_scrollView);
2219 HIScrollViewSetScrollBarAutoHide(m_scrollView,true);
2220 }
2221
2222 HIViewSetFrame( m_scrollView, &hr );
2223 HIViewSetVisible( m_scrollView, true );
2224 }
2225
2226 m_textView = NULL ;
2227 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
2228 m_txn = HITextViewGetTXNObject( m_textView ) ;
2229 HIViewSetVisible( m_textView , true ) ;
2230 if ( m_scrollView )
2231 {
2232 HIViewAddSubview( m_scrollView , m_textView ) ;
2233 m_controlRef = m_scrollView ;
f55d9f74 2234 InstallEventHandler( (WXWidget) m_textView ) ;
489468fe
SC
2235 }
2236 else
2237 {
2238 HIViewSetFrame( m_textView, &hr );
2239 m_controlRef = m_textView ;
2240 }
2241
2242 AdjustCreationAttributes( *wxWHITE , true ) ;
2243#ifndef __LP64__
b2680ced 2244 wxMacWindowClipper c( GetWXPeer() ) ;
489468fe
SC
2245#endif
2246 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2247
2248 TXNSetSelection( m_txn, 0, 0 );
2249 TXNShowSelection( m_txn, kTXNShowStart );
2250
b2680ced 2251 ::InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
489468fe
SC
2252 GetEventTypeCount(eventList), eventList, this,
2253 NULL);
2254}
2255
2256wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
2257{
2258}
2259
9f8062a0 2260bool wxMacMLTEHIViewControl::SetFocus()
489468fe 2261{
9f8062a0 2262 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, kControlFocusNextPart ) == noErr ;
489468fe
SC
2263}
2264
2265bool wxMacMLTEHIViewControl::HasFocus() const
2266{
2267 ControlRef control ;
2268 if ( GetUserFocusWindow() == NULL )
2269 return false;
2270
2271 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2272 return control == m_textView ;
2273}
2274
2275void wxMacMLTEHIViewControl::SetBackgroundColour(const wxColour& col )
2276{
2277 HITextViewSetBackgroundColor( m_textView, col.GetPixel() );
2278}
2279
2280#endif // wxUSE_TEXTCTRL