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