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