move default button handling code from wxControlContainer to wxTLW (patch 1524441)
[wxWidgets.git] / src / mac / carbon / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_TEXTCTRL
15
16 #include "wx/textctrl.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dc.h"
23 #include "wx/button.h"
24 #include "wx/menu.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
28 #endif
29
30 #ifdef __DARWIN__
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #else
34 #include <stat.h>
35 #endif
36
37 #if wxUSE_STD_IOSTREAM
38 #if wxUSE_IOSTREAMH
39 #include <fstream.h>
40 #else
41 #include <fstream>
42 #endif
43 #endif
44
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
47
48 #if defined(__BORLANDC__) && !defined(__WIN32__)
49 #include <alloc.h>
50 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
51 #include <malloc.h>
52 #endif
53
54 #ifndef __DARWIN__
55 #include <Scrap.h>
56 #endif
57
58 #ifndef __DARWIN__
59 #include <MacTextEditor.h>
60 #include <ATSUnicode.h>
61 #include <TextCommon.h>
62 #include <TextEncodingConverter.h>
63 #endif
64
65 #include "wx/mac/uma.h"
66
67
68 // if this is set to 1 then under OSX 10.2 the 'classic' MLTE implementation will be used
69 // if set to 0 then the unicode textctrl will be used
70 #ifndef wxMAC_AWAYS_USE_MLTE
71 #define wxMAC_AWAYS_USE_MLTE 1
72 #endif
73
74 #ifndef __WXMAC_OSX__
75 enum
76 {
77 kTXNVisibilityTag = 'visb' // set the visibility state of the object
78 };
79 #endif
80
81
82 class wxMacFunctor
83 {
84 public :
85 wxMacFunctor() {}
86 virtual ~wxMacFunctor() {}
87
88 virtual void* operator()() = 0 ;
89
90 static void* CallBackProc( void *param )
91 {
92 wxMacFunctor* f = (wxMacFunctor*) param ;
93 void *result = (*f)() ;
94 return result ;
95 }
96 } ;
97
98 template<typename classtype, typename param1type>
99
100 class wxMacObjectFunctor1 : public wxMacFunctor
101 {
102 typedef void (classtype::*function)( param1type p1 ) ;
103 typedef void (classtype::*ref_function)( const param1type& p1 ) ;
104 public :
105 wxMacObjectFunctor1( classtype *obj , function f , param1type p1 ) :
106 wxMacFunctor()
107 {
108 m_object = obj ;
109 m_function = f ;
110 m_param1 = p1 ;
111 }
112
113 wxMacObjectFunctor1( classtype *obj , ref_function f , param1type p1 ) :
114 wxMacFunctor()
115 {
116 m_object = obj ;
117 m_refFunction = f ;
118 m_param1 = p1 ;
119 }
120
121 ~wxMacObjectFunctor1() {}
122
123 virtual void* operator()()
124 {
125 (m_object->*m_function)( m_param1 ) ;
126 return NULL ;
127 }
128
129 private :
130 classtype* m_object ;
131 param1type m_param1 ;
132 union
133 {
134 function m_function ;
135 ref_function m_refFunction ;
136 } ;
137 } ;
138
139 template<typename classtype, typename param1type>
140 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
141 {
142 wxMacObjectFunctor1<classtype, param1type> params(object, function, p1) ;
143 void *result =
144 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
145 return result ;
146 }
147
148 template<typename classtype, typename param1type>
149 void* wxMacMPRemoteCall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
150 {
151 wxMacObjectFunctor1<classtype,param1type> params(object, function, p1) ;
152 void *result =
153 MPRemoteCall( wxMacFunctor::CallBackProc , &params , kMPOwningProcessRemoteContext ) ;
154 return result ;
155 }
156
157 template<typename classtype, typename param1type>
158 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( param1type p1 ) , param1type p1 )
159 {
160 wxMutexGuiLeave() ;
161 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
162 wxMutexGuiEnter() ;
163 return result ;
164 }
165
166 template<typename classtype, typename param1type>
167 void* wxMacMPRemoteGUICall( classtype *object , void (classtype::*function)( const param1type& p1 ) , param1type p1 )
168 {
169 wxMutexGuiLeave() ;
170 void *result = wxMacMPRemoteCall( object , function , p1 ) ;
171 wxMutexGuiEnter() ;
172 return result ;
173 }
174
175 // common interface for all implementations
176 class wxMacTextControl : public wxMacControl
177 {
178 public :
179 wxMacTextControl( wxTextCtrl *peer ) ;
180 ~wxMacTextControl() ;
181
182 virtual wxString GetStringValue() const = 0 ;
183 virtual void SetStringValue( const wxString &val ) = 0 ;
184 virtual void SetSelection( long from, long to ) = 0 ;
185 virtual void GetSelection( long* from, long* to ) const = 0 ;
186 virtual void WriteText( const wxString& str ) = 0 ;
187
188 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
189 virtual void Copy() ;
190 virtual void Cut() ;
191 virtual void Paste() ;
192 virtual bool CanPaste() const ;
193 virtual void SetEditable( bool editable ) ;
194 virtual wxTextPos GetLastPosition() const ;
195 virtual void Replace( long from, long to, const wxString &str ) ;
196 virtual void Remove( long from, long to ) ;
197
198
199 virtual bool HasOwnContextMenu() const
200 { return false ; }
201
202 virtual bool SetupCursor( const wxPoint& pt )
203 { return false ; }
204
205 virtual void Clear() ;
206 virtual bool CanUndo() const;
207 virtual void Undo() ;
208 virtual bool CanRedo() const;
209 virtual void Redo() ;
210 virtual int GetNumberOfLines() const ;
211 virtual long XYToPosition(long x, long y) const;
212 virtual bool PositionToXY(long pos, long *x, long *y) const ;
213 virtual void ShowPosition(long WXUNUSED(pos)) ;
214 virtual int GetLineLength(long lineNo) const ;
215 virtual wxString GetLineText(long lineNo) const ;
216
217 #ifndef __WXMAC_OSX__
218 virtual void MacControlUserPaneDrawProc(wxInt16 part) = 0 ;
219 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) = 0 ;
220 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) = 0 ;
221 virtual void MacControlUserPaneIdleProc() = 0 ;
222 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) = 0 ;
223 virtual void MacControlUserPaneActivateProc(bool activating) = 0 ;
224 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) = 0 ;
225 virtual void MacControlUserPaneBackgroundProc(void* info) = 0 ;
226 #endif
227 } ;
228
229 // common parts for implementations based on MLTE
230
231 class wxMacMLTEControl : public wxMacTextControl
232 {
233 public :
234 wxMacMLTEControl( wxTextCtrl *peer ) ;
235
236 virtual wxString GetStringValue() const ;
237 virtual void SetStringValue( const wxString &str ) ;
238
239 static TXNFrameOptions FrameOptionsFromWXStyle( long wxStyle ) ;
240
241 void AdjustCreationAttributes( const wxColour& background, bool visible ) ;
242
243 virtual void SetFont( const wxFont & font, const wxColour& foreground, long windowStyle ) ;
244 virtual void SetBackground( const wxBrush &brush ) ;
245 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
246 virtual void Copy() ;
247 virtual void Cut() ;
248 virtual void Paste() ;
249 virtual bool CanPaste() const ;
250 virtual void SetEditable( bool editable ) ;
251 virtual wxTextPos GetLastPosition() const ;
252 virtual void Replace( long from, long to, const wxString &str ) ;
253 virtual void Remove( long from, long to ) ;
254 virtual void GetSelection( long* from, long* to ) const ;
255 virtual void SetSelection( long from, long to ) ;
256
257 virtual void WriteText( const wxString& str ) ;
258
259 virtual bool HasOwnContextMenu() const
260 {
261 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
262 if ( UMAGetSystemVersion() >= 0x1040 )
263 {
264 TXNCommandEventSupportOptions options ;
265 TXNGetCommandEventSupport( m_txn , & options ) ;
266 return options & kTXNSupportEditCommandProcessing ;
267 }
268 #endif
269
270 return false ;
271 }
272
273 virtual void Clear() ;
274
275 virtual bool CanUndo() const ;
276 virtual void Undo() ;
277 virtual bool CanRedo() const;
278 virtual void Redo() ;
279 virtual int GetNumberOfLines() const ;
280 virtual long XYToPosition(long x, long y) const ;
281 virtual bool PositionToXY(long pos, long *x, long *y) const ;
282 virtual void ShowPosition( long pos ) ;
283 virtual int GetLineLength(long lineNo) const ;
284 virtual wxString GetLineText(long lineNo) const ;
285
286 void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
287 TXNObject GetTXNObject() { return m_txn ; }
288
289 protected :
290 void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
291
292 TXNObject m_txn ;
293 } ;
294
295 #if TARGET_API_MAC_OSX
296
297 // implementation available under OSX
298
299 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
300
301 class wxMacMLTEHIViewControl : public wxMacMLTEControl
302 {
303 public :
304 wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
305 const wxString& str,
306 const wxPoint& pos,
307 const wxSize& size, long style ) ;
308 ~wxMacMLTEHIViewControl() ;
309
310 virtual OSStatus SetFocus( ControlFocusPart focusPart ) ;
311 virtual bool HasFocus() const ;
312 virtual void SetBackground( const wxBrush &brush) ;
313
314 protected :
315 HIViewRef m_scrollView ;
316 HIViewRef m_textView ;
317 EventHandlerRef m_textEventHandlerRef ;
318 };
319
320 #endif
321
322 class wxMacUnicodeTextControl : public wxMacTextControl
323 {
324 public :
325 wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
326 const wxString& str,
327 const wxPoint& pos,
328 const wxSize& size, long style ) ;
329 ~wxMacUnicodeTextControl();
330
331 virtual void VisibilityChanged(bool shown);
332 virtual wxString GetStringValue() const ;
333 virtual void SetStringValue( const wxString &str) ;
334 virtual void Copy();
335 virtual void Cut();
336 virtual void Paste();
337 virtual bool CanPaste() const;
338 virtual void SetEditable(bool editable) ;
339 virtual void GetSelection( long* from, long* to) const ;
340 virtual void SetSelection( long from , long to ) ;
341 virtual void WriteText(const wxString& str) ;
342
343 protected :
344 // contains the tag for the content (is different for password and non-password controls)
345 OSType m_valueTag ;
346
347 // as the selection tag only works correctly when the control has the focus we have to mirror the
348 // intended value
349 EventHandlerRef m_focusHandlerRef ;
350 public :
351 ControlEditTextSelectionRec m_selection ;
352 };
353
354 #endif
355
356 // 'classic' MLTE implementation
357
358 class wxMacMLTEClassicControl : public wxMacMLTEControl
359 {
360 public :
361 wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
362 const wxString& str,
363 const wxPoint& pos,
364 const wxSize& size, long style ) ;
365 ~wxMacMLTEClassicControl() ;
366
367 virtual void VisibilityChanged(bool shown) ;
368 virtual void SuperChangedPosition() ;
369
370 virtual void MacControlUserPaneDrawProc(wxInt16 part) ;
371 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) ;
372 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) ;
373 virtual void MacControlUserPaneIdleProc() ;
374 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) ;
375 virtual void MacControlUserPaneActivateProc(bool activating) ;
376 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) ;
377 virtual void MacControlUserPaneBackgroundProc(void* info) ;
378
379 virtual bool SetupCursor( const wxPoint& WXUNUSED(pt) )
380 {
381 MacControlUserPaneIdleProc();
382 return true;
383 }
384
385 virtual void SetRect( Rect *r ) ;
386
387 protected :
388 OSStatus DoCreate();
389
390 void MacUpdatePosition() ;
391 void MacActivatePaneText(bool setActive) ;
392 void MacFocusPaneText(bool setFocus) ;
393 void MacSetObjectVisibility(bool vis) ;
394
395 private :
396 TXNFrameID m_txnFrameID ;
397 GrafPtr m_txnPort ;
398 WindowRef m_txnWindow ;
399 // bounds of the control as we last did set the txn frames
400 Rect m_txnControlBounds ;
401 Rect m_txnVisBounds ;
402
403 #ifdef __WXMAC_OSX__
404 static pascal void TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode ) ;
405 static pascal void TXNScrollInfoProc(
406 SInt32 iValue, SInt32 iMaximumValue,
407 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon ) ;
408
409 ControlRef m_sbHorizontal ;
410 SInt32 m_lastHorizontalValue ;
411 ControlRef m_sbVertical ;
412 SInt32 m_lastVerticalValue ;
413 #endif
414 };
415
416
417 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
418
419 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
420 EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground )
421 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
422 EVT_CHAR(wxTextCtrl::OnChar)
423 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
424 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
425 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
426 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
427 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
428 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
429 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
430
431 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu)
432
433 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
434 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
435 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
436 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
437 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
438 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
439 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
440 END_EVENT_TABLE()
441
442
443 void wxTextCtrl::Init()
444 {
445 m_editable = true ;
446 m_dirty = false;
447
448 m_maxLength = 0;
449 m_privateContextMenu = NULL;
450 m_triggerOnSetValue = true ;
451 }
452
453 wxTextCtrl::~wxTextCtrl()
454 {
455 delete m_privateContextMenu;
456 }
457
458 bool wxTextCtrl::Create( wxWindow *parent,
459 wxWindowID id,
460 const wxString& str,
461 const wxPoint& pos,
462 const wxSize& size,
463 long style,
464 const wxValidator& validator,
465 const wxString& name )
466 {
467 m_macIsUserPane = false ;
468 m_editable = true ;
469
470 if ( ! (style & wxNO_BORDER) )
471 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
472
473 if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
474 return false;
475
476 if ( m_windowStyle & wxTE_MULTILINE )
477 {
478 wxASSERT_MSG(
479 !(m_windowStyle & wxTE_PROCESS_ENTER),
480 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
481
482 m_windowStyle |= wxTE_PROCESS_ENTER;
483 style |= wxTE_PROCESS_ENTER ;
484 }
485
486 bool forceMLTE = false ;
487
488 #if wxUSE_SYSTEM_OPTIONS
489 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE ) == 1))
490 {
491 forceMLTE = true ;
492 }
493 #endif
494
495 #ifdef __WXMAC_OSX__
496 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
497 if ( UMAGetSystemVersion() >= 0x1030 && !forceMLTE )
498 {
499 if ( m_windowStyle & wxTE_MULTILINE )
500 m_peer = new wxMacMLTEHIViewControl( this , str , pos , size , style ) ;
501 }
502 #endif
503
504 if ( !m_peer )
505 {
506 if ( !(m_windowStyle & wxTE_MULTILINE) && !forceMLTE )
507 m_peer = new wxMacUnicodeTextControl( this , str , pos , size , style ) ;
508 }
509 #endif
510
511 if ( !m_peer )
512 m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ;
513
514 MacPostControlCreate(pos, size) ;
515
516 // only now the embedding is correct and we can do a positioning update
517
518 MacSuperChangedPosition() ;
519
520 if ( m_windowStyle & wxTE_READONLY)
521 SetEditable( false ) ;
522
523 SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ;
524
525 return true;
526 }
527
528 void wxTextCtrl::MacSuperChangedPosition()
529 {
530 wxWindow::MacSuperChangedPosition() ;
531 GetPeer()->SuperChangedPosition() ;
532 }
533
534 void wxTextCtrl::MacVisibilityChanged()
535 {
536 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
537 }
538
539 void wxTextCtrl::MacEnabledStateChanged()
540 {
541 }
542
543 wxString wxTextCtrl::GetValue() const
544 {
545 return GetPeer()->GetStringValue() ;
546 }
547
548 void wxTextCtrl::GetSelection(long* from, long* to) const
549 {
550 GetPeer()->GetSelection( from , to ) ;
551 }
552
553 void wxTextCtrl::SetValue(const wxString& str)
554 {
555 // optimize redraws
556 if ( GetValue() == str )
557 return ;
558
559 GetPeer()->SetStringValue( str ) ;
560
561 if ( m_triggerOnSetValue )
562 {
563 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
564 event.SetString( GetValue() );
565 event.SetEventObject( this );
566 GetEventHandler()->ProcessEvent( event );
567 }
568 }
569
570 void wxTextCtrl::SetMaxLength(unsigned long len)
571 {
572 m_maxLength = len ;
573 }
574
575 bool wxTextCtrl::SetFont( const wxFont& font )
576 {
577 if ( !wxTextCtrlBase::SetFont( font ) )
578 return false ;
579
580 GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle() ) ;
581
582 return true ;
583 }
584
585 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
586 {
587 GetPeer()->SetStyle( start , end , style ) ;
588
589 return true ;
590 }
591
592 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
593 {
594 wxTextCtrlBase::SetDefaultStyle( style ) ;
595 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
596
597 return true ;
598 }
599
600 // Clipboard operations
601
602 void wxTextCtrl::Copy()
603 {
604 if (CanCopy())
605 GetPeer()->Copy() ;
606 }
607
608 void wxTextCtrl::Cut()
609 {
610 if (CanCut())
611 {
612 GetPeer()->Cut() ;
613
614 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
615 event.SetEventObject( this );
616 GetEventHandler()->ProcessEvent( event );
617 }
618 }
619
620 void wxTextCtrl::Paste()
621 {
622 if (CanPaste())
623 {
624 GetPeer()->Paste() ;
625
626 // TODO: eventually we should add setting the default style again
627
628 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
629 event.SetEventObject( this );
630 GetEventHandler()->ProcessEvent( event );
631 }
632 }
633
634 bool wxTextCtrl::CanCopy() const
635 {
636 // Can copy if there's a selection
637 long from, to;
638 GetSelection( &from, &to );
639
640 return (from != to);
641 }
642
643 bool wxTextCtrl::CanCut() const
644 {
645 if ( !IsEditable() )
646 return false;
647
648 // Can cut if there's a selection
649 long from, to;
650 GetSelection( &from, &to );
651
652 return (from != to);
653 }
654
655 bool wxTextCtrl::CanPaste() const
656 {
657 if (!IsEditable())
658 return false;
659
660 return GetPeer()->CanPaste() ;
661 }
662
663 void wxTextCtrl::SetEditable(bool editable)
664 {
665 if ( editable != m_editable )
666 {
667 m_editable = editable ;
668 GetPeer()->SetEditable( editable ) ;
669 }
670 }
671
672 void wxTextCtrl::SetInsertionPoint(long pos)
673 {
674 SetSelection( pos , pos ) ;
675 }
676
677 void wxTextCtrl::SetInsertionPointEnd()
678 {
679 wxTextPos pos = GetLastPosition();
680 SetInsertionPoint( pos );
681 }
682
683 long wxTextCtrl::GetInsertionPoint() const
684 {
685 long begin, end ;
686 GetSelection( &begin , &end ) ;
687
688 return begin ;
689 }
690
691 wxTextPos wxTextCtrl::GetLastPosition() const
692 {
693 return GetPeer()->GetLastPosition() ;
694 }
695
696 void wxTextCtrl::Replace(long from, long to, const wxString& str)
697 {
698 GetPeer()->Replace( from , to , str ) ;
699 }
700
701 void wxTextCtrl::Remove(long from, long to)
702 {
703 GetPeer()->Remove( from , to ) ;
704 }
705
706 void wxTextCtrl::SetSelection(long from, long to)
707 {
708 GetPeer()->SetSelection( from , to ) ;
709 }
710
711 bool wxTextCtrl::LoadFile(const wxString& file)
712 {
713 return wxTextCtrlBase::LoadFile( file );
714 }
715
716 void wxTextCtrl::WriteText(const wxString& str)
717 {
718 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
719 if ( !wxIsMainThread() )
720 {
721 // unfortunately CW 8 is not able to correctly deduce the template types,
722 // so we have to instantiate explicitly
723 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( this , &wxTextCtrl::WriteText , str ) ;
724
725 return ;
726 }
727
728 GetPeer()->WriteText( str ) ;
729 }
730
731 void wxTextCtrl::AppendText(const wxString& text)
732 {
733 SetInsertionPointEnd();
734 WriteText( text );
735 }
736
737 void wxTextCtrl::Clear()
738 {
739 GetPeer()->Clear() ;
740 }
741
742 bool wxTextCtrl::IsModified() const
743 {
744 return m_dirty;
745 }
746
747 bool wxTextCtrl::IsEditable() const
748 {
749 return IsEnabled() && m_editable ;
750 }
751
752 bool wxTextCtrl::AcceptsFocus() const
753 {
754 // we don't want focus if we can't be edited
755 return /*IsEditable() && */ wxControl::AcceptsFocus();
756 }
757
758 wxSize wxTextCtrl::DoGetBestSize() const
759 {
760 int wText, hText;
761
762 // these are the numbers from the HIG:
763 // we reduce them by the borders first
764 wText = 100 ;
765
766 switch ( m_windowVariant )
767 {
768 case wxWINDOW_VARIANT_NORMAL :
769 hText = 22 - 6 ;
770 break ;
771
772 case wxWINDOW_VARIANT_SMALL :
773 hText = 19 - 6 ;
774 break ;
775
776 case wxWINDOW_VARIANT_MINI :
777 hText = 15 - 6 ;
778 break ;
779
780 default :
781 hText = 22 - 6;
782 break ;
783 }
784
785 // as the above numbers have some free space around the text
786 // we get 5 lines like this anyway
787 if ( m_windowStyle & wxTE_MULTILINE )
788 hText *= 5 ;
789
790 if ( !HasFlag(wxNO_BORDER) )
791 hText += 6 ;
792
793 return wxSize(wText, hText);
794 }
795
796 // ----------------------------------------------------------------------------
797 // Undo/redo
798 // ----------------------------------------------------------------------------
799
800 void wxTextCtrl::Undo()
801 {
802 if (CanUndo())
803 GetPeer()->Undo() ;
804 }
805
806 void wxTextCtrl::Redo()
807 {
808 if (CanRedo())
809 GetPeer()->Redo() ;
810 }
811
812 bool wxTextCtrl::CanUndo() const
813 {
814 if ( !IsEditable() )
815 return false ;
816
817 return GetPeer()->CanUndo() ;
818 }
819
820 bool wxTextCtrl::CanRedo() const
821 {
822 if ( !IsEditable() )
823 return false ;
824
825 return GetPeer()->CanRedo() ;
826 }
827
828 void wxTextCtrl::MarkDirty()
829 {
830 m_dirty = true;
831 }
832
833 void wxTextCtrl::DiscardEdits()
834 {
835 m_dirty = false;
836 }
837
838 int wxTextCtrl::GetNumberOfLines() const
839 {
840 return GetPeer()->GetNumberOfLines() ;
841 }
842
843 long wxTextCtrl::XYToPosition(long x, long y) const
844 {
845 return GetPeer()->XYToPosition( x , y ) ;
846 }
847
848 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
849 {
850 return GetPeer()->PositionToXY( pos , x , y ) ;
851 }
852
853 void wxTextCtrl::ShowPosition(long pos)
854 {
855 return GetPeer()->ShowPosition(pos) ;
856 }
857
858 int wxTextCtrl::GetLineLength(long lineNo) const
859 {
860 return GetPeer()->GetLineLength(lineNo) ;
861 }
862
863 wxString wxTextCtrl::GetLineText(long lineNo) const
864 {
865 return GetPeer()->GetLineText(lineNo) ;
866 }
867
868 void wxTextCtrl::Command(wxCommandEvent & event)
869 {
870 SetValue(event.GetString());
871 ProcessCommand(event);
872 }
873
874 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
875 {
876 // By default, load the first file into the text window.
877 if (event.GetNumberOfFiles() > 0)
878 LoadFile( event.GetFiles()[0] );
879 }
880
881 void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
882 {
883 // all erasing should be done by the real mac control implementation
884 // while this is true for MLTE under classic, the HITextView is somehow
885 // transparent but background erase is not working correctly, so intercept
886 // things while we can...
887 event.Skip() ;
888 }
889
890 void wxTextCtrl::OnChar(wxKeyEvent& event)
891 {
892 int key = event.GetKeyCode() ;
893 bool eat_key = false ;
894
895 if ( key == 'a' && event.MetaDown() )
896 {
897 SelectAll() ;
898
899 return ;
900 }
901
902 if ( key == 'c' && event.MetaDown() )
903 {
904 if ( CanCopy() )
905 Copy() ;
906
907 return ;
908 }
909
910 if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
911 !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
912 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
913 )
914 {
915 // eat it
916 return ;
917 }
918
919 // Check if we have reached the max # of chars (if it is set), but still
920 // allow navigation and deletion
921 if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
922 key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB &&
923 key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) )
924 )
925 {
926 // eat it, we don't want to add more than allowed # of characters
927
928 // TODO: generate EVT_TEXT_MAXLEN()
929 return;
930 }
931
932 // assume that any key not processed yet is going to modify the control
933 m_dirty = true;
934
935 if ( key == 'v' && event.MetaDown() )
936 {
937 if ( CanPaste() )
938 Paste() ;
939
940 return ;
941 }
942
943 if ( key == 'x' && event.MetaDown() )
944 {
945 if ( CanCut() )
946 Cut() ;
947
948 return ;
949 }
950
951 switch ( key )
952 {
953 case WXK_RETURN:
954 if (m_windowStyle & wxTE_PROCESS_ENTER)
955 {
956 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
957 event.SetEventObject( this );
958 event.SetString( GetValue() );
959 if ( GetEventHandler()->ProcessEvent(event) )
960 return;
961 }
962
963 if ( !(m_windowStyle & wxTE_MULTILINE) )
964 {
965 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
966 if ( tlw && tlw->GetDefaultItem() )
967 {
968 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
969 if ( def && def->IsEnabled() )
970 {
971 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
972 event.SetEventObject(def);
973 def->Command(event);
974
975 return ;
976 }
977 }
978
979 // this will make wxWidgets eat the ENTER key so that
980 // we actually prevent line wrapping in a single line text control
981 eat_key = true;
982 }
983 break;
984
985 case WXK_TAB:
986 if ( !(m_windowStyle & wxTE_PROCESS_TAB))
987 {
988 int flags = 0;
989 if (!event.ShiftDown())
990 flags |= wxNavigationKeyEvent::IsForward ;
991 if (event.ControlDown())
992 flags |= wxNavigationKeyEvent::WinChange ;
993 Navigate(flags);
994
995 return;
996 }
997 else
998 {
999 // This is necessary (don't know why);
1000 // otherwise the tab will not be inserted.
1001 WriteText(wxT("\t"));
1002 }
1003 break;
1004
1005 default:
1006 break;
1007 }
1008
1009 if (!eat_key)
1010 {
1011 // perform keystroke handling
1012 event.Skip(true) ;
1013 }
1014
1015 if ( ( key >= 0x20 && key < WXK_START ) ||
1016 ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
1017 key == WXK_RETURN ||
1018 key == WXK_DELETE ||
1019 key == WXK_BACK)
1020 {
1021 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1022 event1.SetEventObject( this );
1023 wxPostEvent( GetEventHandler(), event1 );
1024 }
1025 }
1026
1027 // ----------------------------------------------------------------------------
1028 // standard handlers for standard edit menu events
1029 // ----------------------------------------------------------------------------
1030
1031 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1032 {
1033 Cut();
1034 }
1035
1036 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1037 {
1038 Copy();
1039 }
1040
1041 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1042 {
1043 Paste();
1044 }
1045
1046 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1047 {
1048 Undo();
1049 }
1050
1051 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1052 {
1053 Redo();
1054 }
1055
1056 void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
1057 {
1058 long from, to;
1059
1060 GetSelection( &from, &to );
1061 if (from != -1 && to != -1)
1062 Remove( from, to );
1063 }
1064
1065 void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
1066 {
1067 SetSelection(-1, -1);
1068 }
1069
1070 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1071 {
1072 event.Enable( CanCut() );
1073 }
1074
1075 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1076 {
1077 event.Enable( CanCopy() );
1078 }
1079
1080 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1081 {
1082 event.Enable( CanPaste() );
1083 }
1084
1085 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1086 {
1087 event.Enable( CanUndo() );
1088 }
1089
1090 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1091 {
1092 event.Enable( CanRedo() );
1093 }
1094
1095 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
1096 {
1097 long from, to;
1098
1099 GetSelection( &from, &to );
1100 event.Enable( from != -1 && to != -1 && from != to && IsEditable() ) ;
1101 }
1102
1103 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
1104 {
1105 event.Enable(GetLastPosition() > 0);
1106 }
1107
1108 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1109
1110 void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
1111 {
1112 if ( GetPeer()->HasOwnContextMenu() )
1113 {
1114 event.Skip() ;
1115 return ;
1116 }
1117
1118 if (m_privateContextMenu == NULL)
1119 {
1120 m_privateContextMenu = new wxMenu;
1121 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
1122 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
1123 m_privateContextMenu->AppendSeparator();
1124 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
1125 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
1126 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
1127 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
1128 m_privateContextMenu->AppendSeparator();
1129 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
1130 }
1131
1132 if (m_privateContextMenu != NULL)
1133 PopupMenu(m_privateContextMenu);
1134 }
1135
1136 bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
1137 {
1138 if ( !GetPeer()->SetupCursor( pt ) )
1139 return wxWindow::MacSetupCursor( pt ) ;
1140 else
1141 return true ;
1142 }
1143
1144 #if !TARGET_API_MAC_OSX
1145
1146 // user pane implementation
1147
1148 void wxTextCtrl::MacControlUserPaneDrawProc(wxInt16 part)
1149 {
1150 GetPeer()->MacControlUserPaneDrawProc( part ) ;
1151 }
1152
1153 wxInt16 wxTextCtrl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
1154 {
1155 return GetPeer()->MacControlUserPaneHitTestProc( x , y ) ;
1156 }
1157
1158 wxInt16 wxTextCtrl::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
1159 {
1160 return GetPeer()->MacControlUserPaneTrackingProc( x , y , actionProc ) ;
1161 }
1162
1163 void wxTextCtrl::MacControlUserPaneIdleProc()
1164 {
1165 GetPeer()->MacControlUserPaneIdleProc( ) ;
1166 }
1167
1168 wxInt16 wxTextCtrl::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
1169 {
1170 return GetPeer()->MacControlUserPaneKeyDownProc( keyCode , charCode , modifiers ) ;
1171 }
1172
1173 void wxTextCtrl::MacControlUserPaneActivateProc(bool activating)
1174 {
1175 GetPeer()->MacControlUserPaneActivateProc( activating ) ;
1176 }
1177
1178 wxInt16 wxTextCtrl::MacControlUserPaneFocusProc(wxInt16 action)
1179 {
1180 return GetPeer()->MacControlUserPaneFocusProc( action ) ;
1181 }
1182
1183 void wxTextCtrl::MacControlUserPaneBackgroundProc(void* info)
1184 {
1185 GetPeer()->MacControlUserPaneBackgroundProc( info ) ;
1186 }
1187
1188 #endif
1189
1190 // ----------------------------------------------------------------------------
1191 // implementation base class
1192 // ----------------------------------------------------------------------------
1193
1194 wxMacTextControl::wxMacTextControl(wxTextCtrl* peer) :
1195 wxMacControl( peer )
1196 {
1197 }
1198
1199 wxMacTextControl::~wxMacTextControl()
1200 {
1201 }
1202
1203 void wxMacTextControl::SetStyle(long start, long end, const wxTextAttr& style)
1204 {
1205 }
1206
1207 void wxMacTextControl::Copy()
1208 {
1209 }
1210
1211 void wxMacTextControl::Cut()
1212 {
1213 }
1214
1215 void wxMacTextControl::Paste()
1216 {
1217 }
1218
1219 bool wxMacTextControl::CanPaste() const
1220 {
1221 return false ;
1222 }
1223
1224 void wxMacTextControl::SetEditable(bool editable)
1225 {
1226 }
1227
1228 wxTextPos wxMacTextControl::GetLastPosition() const
1229 {
1230 return GetStringValue().length() ;
1231 }
1232
1233 void wxMacTextControl::Replace( long from , long to , const wxString &val )
1234 {
1235 SetSelection( from , to ) ;
1236 WriteText( val ) ;
1237 }
1238
1239 void wxMacTextControl::Remove( long from , long to )
1240 {
1241 SetSelection( from , to ) ;
1242 WriteText( wxEmptyString) ;
1243 }
1244
1245 void wxMacTextControl::Clear()
1246 {
1247 SetStringValue( wxEmptyString ) ;
1248 }
1249
1250 bool wxMacTextControl::CanUndo() const
1251 {
1252 return false ;
1253 }
1254
1255 void wxMacTextControl::Undo()
1256 {
1257 }
1258
1259 bool wxMacTextControl::CanRedo() const
1260 {
1261 return false ;
1262 }
1263
1264 void wxMacTextControl::Redo()
1265 {
1266 }
1267
1268 long wxMacTextControl::XYToPosition(long x, long y) const
1269 {
1270 return 0 ;
1271 }
1272
1273 bool wxMacTextControl::PositionToXY(long pos, long *x, long *y) const
1274 {
1275 return false ;
1276 }
1277
1278 void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
1279 {
1280 }
1281
1282 int wxMacTextControl::GetNumberOfLines() const
1283 {
1284 ItemCount lines = 0 ;
1285 wxString content = GetStringValue() ;
1286 lines = 1;
1287
1288 for (size_t i = 0; i < content.length() ; i++)
1289 {
1290 if (content[i] == '\r')
1291 lines++;
1292 }
1293
1294 return lines ;
1295 }
1296
1297 wxString wxMacTextControl::GetLineText(long lineNo) const
1298 {
1299 // TODO: change this if possible to reflect real lines
1300 wxString content = GetStringValue() ;
1301
1302 // Find line first
1303 int count = 0;
1304 for (size_t i = 0; i < content.length() ; i++)
1305 {
1306 if (count == lineNo)
1307 {
1308 // Add chars in line then
1309 wxString tmp;
1310
1311 for (size_t j = i; j < content.length(); j++)
1312 {
1313 if (content[j] == '\n')
1314 return tmp;
1315
1316 tmp += content[j];
1317 }
1318
1319 return tmp;
1320 }
1321
1322 if (content[i] == '\n')
1323 count++;
1324 }
1325
1326 return wxEmptyString ;
1327 }
1328
1329 int wxMacTextControl::GetLineLength(long lineNo) const
1330 {
1331 // TODO: change this if possible to reflect real lines
1332 wxString content = GetStringValue() ;
1333
1334 // Find line first
1335 int count = 0;
1336 for (size_t i = 0; i < content.length() ; i++)
1337 {
1338 if (count == lineNo)
1339 {
1340 // Count chars in line then
1341 count = 0;
1342 for (size_t j = i; j < content.length(); j++)
1343 {
1344 count++;
1345 if (content[j] == '\n')
1346 return count;
1347 }
1348
1349 return count;
1350 }
1351
1352 if (content[i] == '\n')
1353 count++;
1354 }
1355
1356 return 0 ;
1357 }
1358
1359 // ----------------------------------------------------------------------------
1360 // standard unicode control implementation
1361 // ----------------------------------------------------------------------------
1362
1363 #if TARGET_API_MAC_OSX
1364
1365 // the current unicode textcontrol implementation has a bug : only if the control
1366 // is currently having the focus, the selection can be retrieved by the corresponding
1367 // data tag. So we have a mirroring using a member variable
1368 // TODO : build event table using virtual member functions for wxMacControl
1369
1370 static const EventTypeSpec unicodeTextControlEventList[] =
1371 {
1372 { kEventClassControl , kEventControlSetFocusPart } ,
1373 } ;
1374
1375 static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1376 {
1377 OSStatus result = eventNotHandledErr ;
1378 wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ;
1379 wxMacCarbonEvent cEvent( event ) ;
1380
1381 switch ( GetEventKind( event ) )
1382 {
1383 case kEventControlSetFocusPart :
1384 {
1385 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
1386 if ( controlPart == kControlFocusNoPart )
1387 {
1388 // about to loose focus -> store selection to field
1389 focus->GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1390 }
1391 result = CallNextEventHandler(handler,event) ;
1392 if ( controlPart != kControlFocusNoPart )
1393 {
1394 // about to gain focus -> set selection from field
1395 focus->SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1396 }
1397 break;
1398 }
1399 default:
1400 break ;
1401 }
1402
1403 return result ;
1404 }
1405
1406 static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1407 {
1408 OSStatus result = eventNotHandledErr ;
1409
1410 switch ( GetEventClass( event ) )
1411 {
1412 case kEventClassControl :
1413 result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ;
1414 break ;
1415
1416 default :
1417 break ;
1418 }
1419 return result ;
1420 }
1421
1422 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler )
1423
1424 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
1425 const wxString& str,
1426 const wxPoint& pos,
1427 const wxSize& size, long style )
1428 : wxMacTextControl( wxPeer )
1429 {
1430 m_font = wxPeer->GetFont() ;
1431 m_windowStyle = style ;
1432 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1433 wxString st = str ;
1434 wxMacConvertNewlines10To13( &st ) ;
1435 wxMacCFStringHolder cf(st , m_font.GetEncoding()) ;
1436 CFStringRef cfr = cf ;
1437 Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
1438 m_valueTag = isPassword ? kControlEditTextPasswordCFStringTag : kControlEditTextCFStringTag ;
1439
1440 OSStatus err = CreateEditUnicodeTextControl(
1441 MAC_WXHWND(wxPeer->MacGetTopLevelWindowRef()), &bounds , cfr ,
1442 isPassword , NULL , &m_controlRef ) ;
1443 verify_noerr( err );
1444
1445 if ( !(m_windowStyle & wxTE_MULTILINE) )
1446 SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
1447
1448 InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(),
1449 GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
1450 &m_focusHandlerRef);
1451 }
1452
1453 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1454 {
1455 ::RemoveEventHandler( m_focusHandlerRef );
1456 }
1457
1458 void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
1459 {
1460 if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
1461 {
1462 // work around a refresh issue insofar as not always the entire content is shown,
1463 // even if this would be possible
1464 ControlEditTextSelectionRec sel ;
1465 CFStringRef value = NULL ;
1466
1467 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1468 verify_noerr( GetData<CFStringRef>( 0, m_valueTag, &value ) );
1469 verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
1470 verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1471
1472 CFRelease( value ) ;
1473 }
1474 }
1475
1476 wxString wxMacUnicodeTextControl::GetStringValue() const
1477 {
1478 wxString result ;
1479 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1480 if ( value )
1481 {
1482 wxMacCFStringHolder cf(value) ;
1483 result = cf.AsString() ;
1484 }
1485
1486 #if '\n' == 10
1487 wxMacConvertNewlines13To10( &result ) ;
1488 #else
1489 wxMacConvertNewlines10To13( &result ) ;
1490 #endif
1491
1492 return result ;
1493 }
1494
1495 void wxMacUnicodeTextControl::SetStringValue( const wxString &str )
1496 {
1497 wxString st = str ;
1498 wxMacConvertNewlines10To13( &st ) ;
1499 wxMacCFStringHolder cf( st , m_font.GetEncoding() ) ;
1500 verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
1501 }
1502
1503 void wxMacUnicodeTextControl::Copy()
1504 {
1505 SendHICommand( kHICommandCopy ) ;
1506 }
1507
1508 void wxMacUnicodeTextControl::Cut()
1509 {
1510 SendHICommand( kHICommandCut ) ;
1511 }
1512
1513 void wxMacUnicodeTextControl::Paste()
1514 {
1515 SendHICommand( kHICommandPaste ) ;
1516 }
1517
1518 bool wxMacUnicodeTextControl::CanPaste() const
1519 {
1520 return true ;
1521 }
1522
1523 void wxMacUnicodeTextControl::SetEditable(bool editable)
1524 {
1525 #if 0 // leads to problem because text cannot be selected anymore
1526 SetData<Boolean>( kControlEditTextPart , kControlEditTextLockedTag , (Boolean) !editable ) ;
1527 #endif
1528 }
1529
1530 void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const
1531 {
1532 ControlEditTextSelectionRec sel ;
1533 if (HasFocus())
1534 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
1535 else
1536 sel = m_selection ;
1537
1538 if ( from )
1539 *from = sel.selStart ;
1540 if ( to )
1541 *to = sel.selEnd ;
1542 }
1543
1544 void wxMacUnicodeTextControl::SetSelection( long from , long to )
1545 {
1546 ControlEditTextSelectionRec sel ;
1547 wxString result ;
1548 int textLength = 0 ;
1549 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1550 if ( value )
1551 {
1552 wxMacCFStringHolder cf(value) ;
1553 textLength = cf.AsString().length() ;
1554 }
1555
1556 if ((from == -1) && (to == -1))
1557 {
1558 from = 0 ;
1559 to = textLength ;
1560 }
1561 else
1562 {
1563 from = wxMin(textLength,wxMax(from,0)) ;
1564 to = wxMax(0,wxMin(textLength,to)) ;
1565 }
1566
1567 sel.selStart = from ;
1568 sel.selEnd = to ;
1569 if ( HasFocus() )
1570 SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
1571 else
1572 m_selection = sel;
1573 }
1574
1575 void wxMacUnicodeTextControl::WriteText( const wxString& str )
1576 {
1577 wxString st = str ;
1578 wxMacConvertNewlines10To13( &st ) ;
1579
1580 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1581 if ( HasFocus() )
1582 {
1583 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
1584 CFStringRef value = cf ;
1585 SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
1586 }
1587 else
1588 #endif
1589 {
1590 wxString val = GetStringValue() ;
1591 long start , end ;
1592 GetSelection( &start , &end ) ;
1593 val.Remove( start , end - start ) ;
1594 val.insert( start , str ) ;
1595 SetStringValue( val ) ;
1596 SetSelection( start + str.length() , start + str.length() ) ;
1597 }
1598 }
1599
1600 #endif
1601
1602 // ----------------------------------------------------------------------------
1603 // MLTE control implementation (common part)
1604 // ----------------------------------------------------------------------------
1605
1606 // if MTLE is read only, no changes at all are allowed, not even from
1607 // procedural API, in order to allow changes via API all the same we must undo
1608 // the readonly status while we are executing, this class helps to do so
1609
1610 class wxMacEditHelper
1611 {
1612 public :
1613 wxMacEditHelper( TXNObject txn )
1614 {
1615 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1616 m_txn = txn ;
1617 TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
1618 if ( m_data[0].uValue == kTXNReadOnly )
1619 {
1620 TXNControlData data[] = { { kTXNReadWrite } } ;
1621 TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
1622 }
1623 }
1624
1625 ~wxMacEditHelper()
1626 {
1627 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1628 if ( m_data[0].uValue == kTXNReadOnly )
1629 TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
1630 }
1631
1632 protected :
1633 TXNObject m_txn ;
1634 TXNControlData m_data[1] ;
1635 } ;
1636
1637 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer )
1638 : wxMacTextControl( peer )
1639 {
1640 SetNeedsFocusRect( true ) ;
1641 }
1642
1643 wxString wxMacMLTEControl::GetStringValue() const
1644 {
1645 wxString result ;
1646 OSStatus err ;
1647 Size actualSize = 0;
1648
1649 {
1650 #if wxUSE_UNICODE
1651 Handle theText ;
1652 err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNUnicodeTextData );
1653
1654 // all done
1655 if ( err != noErr )
1656 {
1657 actualSize = 0 ;
1658 }
1659 else
1660 {
1661 actualSize = GetHandleSize( theText ) / sizeof(UniChar) ;
1662 if ( actualSize > 0 )
1663 {
1664 wxChar *ptr = NULL ;
1665
1666 #if SIZEOF_WCHAR_T == 2
1667 ptr = new wxChar[actualSize + 1] ;
1668 wxStrncpy( ptr , (wxChar*)(*theText) , actualSize ) ;
1669 #else
1670 SetHandleSize( theText, (actualSize + 1) * sizeof(UniChar) ) ;
1671 HLock( theText ) ;
1672 (((UniChar*)*theText)[actualSize]) = 0 ;
1673 wxMBConvUTF16 converter ;
1674 size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
1675 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") );
1676 ptr = new wxChar[noChars + 1] ;
1677
1678 noChars = converter.MB2WC( ptr , (const char*)*theText , noChars + 1 ) ;
1679 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") );
1680 ptr[noChars] = 0 ;
1681 HUnlock( theText ) ;
1682 #endif
1683
1684 ptr[actualSize] = 0 ;
1685 result = wxString( ptr ) ;
1686 delete [] ptr ;
1687 }
1688
1689 DisposeHandle( theText ) ;
1690 }
1691 #else
1692 Handle theText ;
1693 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1694
1695 // all done
1696 if ( err != noErr )
1697 {
1698 actualSize = 0 ;
1699 }
1700 else
1701 {
1702 actualSize = GetHandleSize( theText ) ;
1703 if ( actualSize > 0 )
1704 {
1705 HLock( theText ) ;
1706 result = wxString( *theText , wxConvLocal , actualSize ) ;
1707 HUnlock( theText ) ;
1708 }
1709
1710 DisposeHandle( theText ) ;
1711 }
1712 #endif
1713 }
1714
1715 #if '\n' == 10
1716 wxMacConvertNewlines13To10( &result ) ;
1717 #else
1718 wxMacConvertNewlines10To13( &result ) ;
1719 #endif
1720
1721 return result ;
1722 }
1723
1724 void wxMacMLTEControl::SetStringValue( const wxString &str )
1725 {
1726 wxString st = str;
1727 wxMacConvertNewlines10To13( &st );
1728
1729 {
1730 wxMacWindowClipper c( m_peer );
1731
1732 {
1733 wxMacEditHelper help( m_txn );
1734 SetTXNData( st, kTXNStartOffset, kTXNEndOffset );
1735 }
1736
1737 TXNSetSelection( m_txn, 0, 0 );
1738 TXNShowSelection( m_txn, kTXNShowStart );
1739 }
1740 }
1741
1742 TXNFrameOptions wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle )
1743 {
1744 TXNFrameOptions frameOptions = kTXNDontDrawCaretWhenInactiveMask;
1745
1746 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1747 frameOptions |= kTXNDoFontSubstitutionMask;
1748 #endif
1749
1750 if ( ! (wxStyle & wxTE_NOHIDESEL) )
1751 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
1752
1753 if ( wxStyle & (wxHSCROLL | wxTE_DONTWRAP) )
1754 frameOptions |= kTXNWantHScrollBarMask ;
1755
1756 if ( wxStyle & wxTE_MULTILINE )
1757 {
1758 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
1759
1760 if ( !(wxStyle & wxTE_NO_VSCROLL) )
1761 {
1762 frameOptions |= kTXNWantVScrollBarMask ;
1763
1764 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1765 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1766 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1767 // if ( frameOptions & kTXNWantHScrollBarMask )
1768 // frameOptions |= kTXNDrawGrowIconMask ;
1769 }
1770 }
1771 else
1772 {
1773 frameOptions |= kTXNSingleLineOnlyMask ;
1774 }
1775
1776 return frameOptions ;
1777 }
1778
1779 void wxMacMLTEControl::AdjustCreationAttributes( const wxColour &background, bool visible )
1780 {
1781 TXNControlTag iControlTags[] =
1782 {
1783 kTXNDoFontSubstitution,
1784 kTXNWordWrapStateTag ,
1785 };
1786 TXNControlData iControlData[] =
1787 {
1788 { true },
1789 { kTXNNoAutoWrap },
1790 };
1791
1792 int toptag = WXSIZEOF( iControlTags ) ;
1793
1794 if ( m_windowStyle & wxTE_MULTILINE )
1795 {
1796 iControlData[1].uValue =
1797 (m_windowStyle & wxTE_DONTWRAP)
1798 ? kTXNNoAutoWrap
1799 : kTXNAutoWrap;
1800 }
1801
1802 OSStatus err = TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) ;
1803 verify_noerr( err );
1804
1805 // setting the default font:
1806 // under 10.2 this causes a visible caret, therefore we avoid it
1807
1808 if ( UMAGetSystemVersion() >= 0x1030 )
1809 {
1810 Str255 fontName ;
1811 SInt16 fontSize ;
1812 Style fontStyle ;
1813
1814 GetThemeFont( kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1815
1816 TXNTypeAttributes typeAttr[] =
1817 {
1818 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
1819 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
1820 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
1821 } ;
1822
1823 err = TXNSetTypeAttributes(
1824 m_txn, sizeof(typeAttr) / sizeof(TXNTypeAttributes),
1825 typeAttr, kTXNStartOffset, kTXNEndOffset );
1826 verify_noerr( err );
1827 }
1828
1829 if ( m_windowStyle & wxTE_PASSWORD )
1830 {
1831 UniChar c = 0x00A5 ;
1832 err = TXNEchoMode( m_txn , c , 0 , true );
1833 verify_noerr( err );
1834 }
1835
1836 TXNBackground tback;
1837 tback.bgType = kTXNBackgroundTypeRGB;
1838 tback.bg.color = MAC_WXCOLORREF( background.GetPixel() );
1839 TXNSetBackground( m_txn , &tback );
1840
1841 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1842 if ( UMAGetSystemVersion() >= 0x1040 )
1843 {
1844 TXNCommandEventSupportOptions options ;
1845 if ( TXNGetCommandEventSupport( m_txn, &options ) == noErr )
1846 {
1847 options |=
1848 kTXNSupportEditCommandProcessing
1849 | kTXNSupportEditCommandUpdating
1850 | kTXNSupportSpellCheckCommandProcessing
1851 | kTXNSupportSpellCheckCommandUpdating
1852 | kTXNSupportFontCommandProcessing
1853 | kTXNSupportFontCommandUpdating;
1854
1855 TXNSetCommandEventSupport( m_txn , options ) ;
1856 }
1857 }
1858 #endif
1859 }
1860
1861 void wxMacMLTEControl::SetBackground( const wxBrush &brush )
1862 {
1863 // currently only solid background are supported
1864 TXNBackground tback;
1865
1866 tback.bgType = kTXNBackgroundTypeRGB;
1867 tback.bg.color = MAC_WXCOLORREF( brush.GetColour().GetPixel() );
1868 TXNSetBackground( m_txn , &tback );
1869 }
1870
1871 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
1872 {
1873 TXNTypeAttributes typeAttr[4] ;
1874 RGBColor color ;
1875 int attrCount = 0 ;
1876
1877 if ( style.HasFont() )
1878 {
1879 const wxFont &font = style.GetFont() ;
1880
1881 #if 0 // old version
1882 Str255 fontName = "\pMonaco" ;
1883 SInt16 fontSize = 12 ;
1884 Style fontStyle = normal ;
1885 wxMacStringToPascal( font.GetFaceName() , fontName ) ;
1886 fontSize = font.GetPointSize() ;
1887 if ( font.GetUnderlined() )
1888 fontStyle |= underline ;
1889 if ( font.GetWeight() == wxBOLD )
1890 fontStyle |= bold ;
1891 if ( font.GetStyle() == wxITALIC )
1892 fontStyle |= italic ;
1893
1894 typeAttr[attrCount].tag = kTXNQDFontNameAttribute ;
1895 typeAttr[attrCount].size = kTXNQDFontNameAttributeSize ;
1896 typeAttr[attrCount].data.dataPtr = (void*)fontName ;
1897 attrCount++ ;
1898
1899 typeAttr[attrCount].tag = kTXNQDFontSizeAttribute ;
1900 typeAttr[attrCount].size = kTXNFontSizeAttributeSize ;
1901 typeAttr[attrCount].data.dataValue = (fontSize << 16) ;
1902 attrCount++ ;
1903
1904 typeAttr[attrCount].tag = kTXNQDFontStyleAttribute ;
1905 typeAttr[attrCount].size = kTXNQDFontStyleAttributeSize ;
1906 typeAttr[attrCount].data.dataValue = fontStyle ;
1907 attrCount++ ;
1908 #else
1909 typeAttr[attrCount].tag = kTXNATSUIStyle ;
1910 typeAttr[attrCount].size = kTXNATSUIStyleSize ;
1911 typeAttr[attrCount].data.dataValue = (UInt32)font.MacGetATSUStyle() ;
1912 attrCount++ ;
1913 #endif
1914 }
1915
1916 if ( style.HasTextColour() )
1917 {
1918 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
1919
1920 typeAttr[attrCount].tag = kTXNQDFontColorAttribute ;
1921 typeAttr[attrCount].size = kTXNQDFontColorAttributeSize ;
1922 typeAttr[attrCount].data.dataPtr = (void*) &color ;
1923 attrCount++ ;
1924 }
1925
1926 if ( attrCount > 0 )
1927 {
1928 verify_noerr( TXNSetTypeAttributes( m_txn , attrCount , typeAttr, from , to ) );
1929 // unfortunately the relayout is not automatic
1930 TXNRecalcTextLayout( m_txn );
1931 }
1932 }
1933
1934 void wxMacMLTEControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
1935 {
1936 wxMacEditHelper help( m_txn ) ;
1937 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1938 }
1939
1940 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1941 {
1942 wxMacEditHelper help( m_txn ) ;
1943 TXNSetAttribute( style, start, end ) ;
1944 }
1945
1946 void wxMacMLTEControl::Copy()
1947 {
1948 ClearCurrentScrap();
1949 TXNCopy( m_txn );
1950 TXNConvertToPublicScrap();
1951 }
1952
1953 void wxMacMLTEControl::Cut()
1954 {
1955 ClearCurrentScrap();
1956 TXNCut( m_txn );
1957 TXNConvertToPublicScrap();
1958 }
1959
1960 void wxMacMLTEControl::Paste()
1961 {
1962 TXNConvertFromPublicScrap();
1963 TXNPaste( m_txn );
1964 }
1965
1966 bool wxMacMLTEControl::CanPaste() const
1967 {
1968 return TXNIsScrapPastable() ;
1969 }
1970
1971 void wxMacMLTEControl::SetEditable(bool editable)
1972 {
1973 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1974 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1975 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1976 }
1977
1978 wxTextPos wxMacMLTEControl::GetLastPosition() const
1979 {
1980 wxTextPos actualsize = 0 ;
1981
1982 Handle theText ;
1983 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1984
1985 // all done
1986 if ( err == noErr )
1987 {
1988 actualsize = GetHandleSize( theText ) ;
1989 DisposeHandle( theText ) ;
1990 }
1991 else
1992 {
1993 actualsize = 0 ;
1994 }
1995
1996 return actualsize ;
1997 }
1998
1999 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
2000 {
2001 wxString value = str ;
2002 wxMacConvertNewlines10To13( &value ) ;
2003
2004 wxMacEditHelper help( m_txn ) ;
2005 wxMacWindowClipper c( m_peer ) ;
2006
2007 TXNSetSelection( m_txn, from, to ) ;
2008 TXNClear( m_txn ) ;
2009 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2010 }
2011
2012 void wxMacMLTEControl::Remove( long from , long to )
2013 {
2014 wxMacWindowClipper c( m_peer ) ;
2015 wxMacEditHelper help( m_txn ) ;
2016 TXNSetSelection( m_txn , from , to ) ;
2017 TXNClear( m_txn ) ;
2018 }
2019
2020 void wxMacMLTEControl::GetSelection( long* from, long* to) const
2021 {
2022 TXNGetSelection( m_txn , (TXNOffset*) from , (TXNOffset*) to ) ;
2023 }
2024
2025 void wxMacMLTEControl::SetSelection( long from , long to )
2026 {
2027 wxMacWindowClipper c( m_peer ) ;
2028
2029 // change the selection
2030 if ((from == -1) && (to == -1))
2031 TXNSelectAll( m_txn );
2032 else
2033 TXNSetSelection( m_txn, from, to );
2034
2035 TXNShowSelection( m_txn, kTXNShowStart );
2036 }
2037
2038 void wxMacMLTEControl::WriteText( const wxString& str )
2039 {
2040 wxString st = str ;
2041 wxMacConvertNewlines10To13( &st ) ;
2042
2043 long start , end , dummy ;
2044
2045 GetSelection( &start , &dummy ) ;
2046 wxMacWindowClipper c( m_peer ) ;
2047
2048 {
2049 wxMacEditHelper helper( m_txn ) ;
2050 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2051 }
2052
2053 GetSelection( &dummy, &end ) ;
2054
2055 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2056 }
2057
2058 void wxMacMLTEControl::Clear()
2059 {
2060 wxMacWindowClipper c( m_peer ) ;
2061 wxMacEditHelper st( m_txn ) ;
2062 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
2063 TXNClear( m_txn ) ;
2064 }
2065
2066 bool wxMacMLTEControl::CanUndo() const
2067 {
2068 return TXNCanUndo( m_txn , NULL ) ;
2069 }
2070
2071 void wxMacMLTEControl::Undo()
2072 {
2073 TXNUndo( m_txn ) ;
2074 }
2075
2076 bool wxMacMLTEControl::CanRedo() const
2077 {
2078 return TXNCanRedo( m_txn , NULL ) ;
2079 }
2080
2081 void wxMacMLTEControl::Redo()
2082 {
2083 TXNRedo( m_txn ) ;
2084 }
2085
2086 int wxMacMLTEControl::GetNumberOfLines() const
2087 {
2088 ItemCount lines = 0 ;
2089 TXNGetLineCount( m_txn, &lines ) ;
2090
2091 return lines ;
2092 }
2093
2094 long wxMacMLTEControl::XYToPosition(long x, long y) const
2095 {
2096 Point curpt ;
2097 wxTextPos lastpos ;
2098
2099 // TODO: find a better implementation : while we can get the
2100 // line metrics of a certain line, we don't get its starting
2101 // position, so it would probably be rather a binary search
2102 // for the start position
2103 long xpos = 0, ypos = 0 ;
2104 int lastHeight = 0 ;
2105 ItemCount n ;
2106
2107 lastpos = GetLastPosition() ;
2108 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
2109 {
2110 if ( y == ypos && x == xpos )
2111 return n ;
2112
2113 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2114
2115 if ( curpt.v > lastHeight )
2116 {
2117 xpos = 0 ;
2118 if ( n > 0 )
2119 ++ypos ;
2120
2121 lastHeight = curpt.v ;
2122 }
2123 else
2124 ++xpos ;
2125 }
2126
2127 return 0 ;
2128 }
2129
2130 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
2131 {
2132 Point curpt ;
2133 wxTextPos lastpos ;
2134
2135 if ( y )
2136 *y = 0 ;
2137 if ( x )
2138 *x = 0 ;
2139
2140 lastpos = GetLastPosition() ;
2141 if ( pos <= lastpos )
2142 {
2143 // TODO: find a better implementation - while we can get the
2144 // line metrics of a certain line, we don't get its starting
2145 // position, so it would probably be rather a binary search
2146 // for the start position
2147 long xpos = 0, ypos = 0 ;
2148 int lastHeight = 0 ;
2149 ItemCount n ;
2150
2151 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
2152 {
2153 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2154
2155 if ( curpt.v > lastHeight )
2156 {
2157 xpos = 0 ;
2158 if ( n > 0 )
2159 ++ypos ;
2160
2161 lastHeight = curpt.v ;
2162 }
2163 else
2164 ++xpos ;
2165 }
2166
2167 if ( y )
2168 *y = ypos ;
2169 if ( x )
2170 *x = xpos ;
2171 }
2172
2173 return false ;
2174 }
2175
2176 void wxMacMLTEControl::ShowPosition( long pos )
2177 {
2178 #if TARGET_RT_MAC_MACHO && defined(AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER)
2179 {
2180 Point current, desired ;
2181 TXNOffset selstart, selend;
2182
2183 TXNGetSelection( m_txn, &selstart, &selend );
2184 TXNOffsetToPoint( m_txn, selstart, &current );
2185 TXNOffsetToPoint( m_txn, pos, &desired );
2186
2187 // TODO: use HIPoints for 10.3 and above
2188 if ( (UInt32)TXNScroll != (UInt32)kUnresolvedCFragSymbolAddress )
2189 {
2190 OSErr theErr = noErr;
2191 SInt32 dv = desired.v - current.v;
2192 SInt32 dh = desired.h - current.h;
2193 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2194 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
2195
2196 // there will be an error returned for classic MLTE implementation when the control is
2197 // invisible, but HITextView works correctly, so we don't assert that one
2198 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2199 }
2200 }
2201 #endif
2202 }
2203
2204 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
2205 {
2206 #if wxUSE_UNICODE
2207 #if SIZEOF_WCHAR_T == 2
2208 size_t len = st.length() ;
2209 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
2210 #else
2211 wxMBConvUTF16 converter ;
2212 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
2213 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
2214 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
2215 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
2216 free( unibuf ) ;
2217 #endif
2218 #else
2219 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
2220 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
2221 #endif
2222 }
2223
2224 wxString wxMacMLTEControl::GetLineText(long lineNo) const
2225 {
2226 wxString line ;
2227
2228 if ( lineNo < GetNumberOfLines() )
2229 {
2230 Point firstPoint;
2231 Fixed lineWidth, lineHeight, currentHeight;
2232 long ypos ;
2233
2234 // get the first possible position in the control
2235 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2236
2237 // Iterate through the lines until we reach the one we want,
2238 // adding to our current y pixel point position
2239 ypos = 0 ;
2240 currentHeight = 0;
2241 while (ypos < lineNo)
2242 {
2243 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2244 currentHeight += lineHeight;
2245 }
2246
2247 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2248 TXNOffset theOffset;
2249 TXNPointToOffset(m_txn, thePoint, &theOffset);
2250
2251 wxString content = GetStringValue() ;
2252 Point currentPoint = thePoint;
2253 while (thePoint.v == currentPoint.v && theOffset < content.length())
2254 {
2255 line += content[theOffset];
2256 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2257 }
2258 }
2259
2260 return line ;
2261 }
2262
2263 int wxMacMLTEControl::GetLineLength(long lineNo) const
2264 {
2265 int theLength = 0;
2266
2267 if ( lineNo < GetNumberOfLines() )
2268 {
2269 Point firstPoint;
2270 Fixed lineWidth, lineHeight, currentHeight;
2271 long ypos;
2272
2273 // get the first possible position in the control
2274 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2275
2276 // Iterate through the lines until we reach the one we want,
2277 // adding to our current y pixel point position
2278 ypos = 0;
2279 currentHeight = 0;
2280 while (ypos < lineNo)
2281 {
2282 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2283 currentHeight += lineHeight;
2284 }
2285
2286 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2287 TXNOffset theOffset;
2288 TXNPointToOffset(m_txn, thePoint, &theOffset);
2289
2290 wxString content = GetStringValue() ;
2291 Point currentPoint = thePoint;
2292 while (thePoint.v == currentPoint.v && theOffset < content.length())
2293 {
2294 ++theLength;
2295 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2296 }
2297 }
2298
2299 return theLength ;
2300 }
2301
2302 // ----------------------------------------------------------------------------
2303 // MLTE control implementation (classic part)
2304 // ----------------------------------------------------------------------------
2305
2306 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2307 // has to live on. We have different problems coming from outdated implementations on the
2308 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2309 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2310 // no way out, therefore we are using our own implementation and our own scrollbars ....
2311
2312 #ifdef __WXMAC_OSX__
2313
2314 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
2315 ControlActionUPP gTXNScrollActionProc = NULL ;
2316
2317 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2318 SInt32 iValue, SInt32 iMaximumValue,
2319 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
2320 {
2321 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
2322 SInt32 value = wxMax( iValue , 0 ) ;
2323 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
2324
2325 if ( iScrollBarOrientation == kTXNHorizontal )
2326 {
2327 if ( mlte->m_sbHorizontal )
2328 {
2329 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
2330 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
2331 mlte->m_lastHorizontalValue = value ;
2332 }
2333 }
2334 else if ( iScrollBarOrientation == kTXNVertical )
2335 {
2336 if ( mlte->m_sbVertical )
2337 {
2338 SetControl32BitValue( mlte->m_sbVertical , value ) ;
2339 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
2340 mlte->m_lastVerticalValue = value ;
2341 }
2342 }
2343 }
2344
2345 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
2346 {
2347 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
2348 if ( mlte == NULL )
2349 return ;
2350
2351 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
2352 return ;
2353
2354 OSStatus err ;
2355 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
2356
2357 SInt32 minimum = 0 ;
2358 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
2359 SInt32 value = GetControl32BitValue( controlRef ) ;
2360 SInt32 delta = 0;
2361
2362 switch ( partCode )
2363 {
2364 case kControlDownButtonPart :
2365 delta = 10 ;
2366 break ;
2367
2368 case kControlUpButtonPart :
2369 delta = -10 ;
2370 break ;
2371
2372 case kControlPageDownPart :
2373 delta = GetControlViewSize( controlRef ) ;
2374 break ;
2375
2376 case kControlPageUpPart :
2377 delta = -GetControlViewSize( controlRef ) ;
2378 break ;
2379
2380 case kControlIndicatorPart :
2381 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
2382 break ;
2383
2384 default :
2385 break ;
2386 }
2387
2388 if ( delta != 0 )
2389 {
2390 SInt32 newValue = value ;
2391
2392 if ( partCode != kControlIndicatorPart )
2393 {
2394 if ( value + delta < minimum )
2395 delta = minimum - value ;
2396 if ( value + delta > maximum )
2397 delta = maximum - value ;
2398
2399 SetControl32BitValue( controlRef , value + delta ) ;
2400 newValue = value + delta ;
2401 }
2402
2403 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
2404 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
2405
2406 err = TXNScroll(
2407 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
2408 &verticalDelta, &horizontalDelta );
2409 verify_noerr( err );
2410
2411 if ( isHorizontal )
2412 mlte->m_lastHorizontalValue = newValue ;
2413 else
2414 mlte->m_lastVerticalValue = newValue ;
2415 }
2416 }
2417 #endif
2418
2419 // make correct activations
2420 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
2421 {
2422 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2423
2424 wxMacWindowClipper clipper( textctrl ) ;
2425 TXNActivate( m_txn, m_txnFrameID, setActive );
2426
2427 ControlRef controlFocus = 0 ;
2428 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2429 if ( controlFocus == m_controlRef )
2430 TXNFocus( m_txn, setActive );
2431 }
2432
2433 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
2434 {
2435 TXNFocus( m_txn, setFocus );
2436 }
2437
2438 // guards against inappropriate redraw (hidden objects drawing onto window)
2439
2440 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
2441 {
2442 ControlRef controlFocus = 0 ;
2443 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2444
2445 if ( !vis && (controlFocus == m_controlRef ) )
2446 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
2447
2448 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
2449 TXNControlData iControlData[1] = { { (UInt32)false } };
2450
2451 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
2452
2453 if ( iControlData[0].uValue != vis )
2454 {
2455 iControlData[0].uValue = vis ;
2456 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
2457 }
2458
2459 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2460 // if we run into further problems we might set the FrameBounds to an empty rect here
2461 }
2462
2463 // make sure that the TXNObject is at the right position
2464
2465 void wxMacMLTEClassicControl::MacUpdatePosition()
2466 {
2467 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2468 if ( textctrl == NULL )
2469 return ;
2470
2471 Rect bounds ;
2472 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2473
2474 wxRect visRect = textctrl->MacGetClippedClientRect() ;
2475 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
2476 int x , y ;
2477 x = y = 0 ;
2478 textctrl->MacWindowToRootWindow( &x , &y ) ;
2479 OffsetRect( &visBounds , x , y ) ;
2480
2481 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
2482 {
2483 m_txnControlBounds = bounds ;
2484 m_txnVisBounds = visBounds ;
2485 wxMacWindowClipper cl( textctrl ) ;
2486
2487 #ifdef __WXMAC_OSX__
2488 bool isCompositing = textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ;
2489 if ( m_sbHorizontal || m_sbVertical )
2490 {
2491 int w = bounds.right - bounds.left ;
2492 int h = bounds.bottom - bounds.top ;
2493
2494 if ( m_sbHorizontal )
2495 {
2496 Rect sbBounds ;
2497
2498 sbBounds.left = -1 ;
2499 sbBounds.top = h - 14 ;
2500 sbBounds.right = w + 1 ;
2501 sbBounds.bottom = h + 1 ;
2502
2503 if ( !isCompositing )
2504 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2505
2506 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
2507 SetControlViewSize( m_sbHorizontal , w ) ;
2508 }
2509
2510 if ( m_sbVertical )
2511 {
2512 Rect sbBounds ;
2513
2514 sbBounds.left = w - 14 ;
2515 sbBounds.top = -1 ;
2516 sbBounds.right = w + 1 ;
2517 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
2518
2519 if ( !isCompositing )
2520 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2521
2522 SetControlBounds( m_sbVertical , &sbBounds ) ;
2523 SetControlViewSize( m_sbVertical , h ) ;
2524 }
2525 }
2526
2527 Rect oldviewRect ;
2528 TXNLongRect olddestRect ;
2529 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
2530
2531 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2532 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2533 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2534 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2535 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2536 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2537
2538 if ( olddestRect.right >= 10000 )
2539 destRect.right = destRect.left + 32000 ;
2540
2541 if ( olddestRect.bottom >= 0x20000000 )
2542 destRect.bottom = destRect.top + 0x40000000 ;
2543
2544 SectRect( &viewRect , &visBounds , &viewRect ) ;
2545 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
2546
2547 #if 0
2548 TXNSetFrameBounds(
2549 m_txn,
2550 m_txnControlBounds.top,
2551 m_txnControlBounds.left,
2552 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
2553 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
2554 m_txnFrameID );
2555 #endif
2556 #else
2557
2558 TXNSetFrameBounds(
2559 m_txn, m_txnControlBounds.top, m_txnControlBounds.left,
2560 wxMax( m_txnControlBounds.bottom, m_txnControlBounds.top ),
2561 wxMax( m_txnControlBounds.right, m_txnControlBounds.left ), m_txnFrameID );
2562 #endif
2563
2564 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2565 // movement, therefore we have to force it
2566
2567 // this problem has been reported in OSX as well, so we use this here once again
2568
2569 TXNLongRect textRect ;
2570 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
2571 if ( textRect.left < m_txnControlBounds.left )
2572 TXNShowSelection( m_txn , kTXNShowStart ) ;
2573 }
2574 }
2575
2576 void wxMacMLTEClassicControl::SetRect( Rect *r )
2577 {
2578 wxMacControl::SetRect( r ) ;
2579 MacUpdatePosition() ;
2580 }
2581
2582 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 thePart)
2583 {
2584 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2585 if ( textctrl == NULL )
2586 return ;
2587
2588 if ( textctrl->MacIsReallyShown() )
2589 {
2590 wxMacWindowClipper clipper( textctrl ) ;
2591 TXNDraw( m_txn , NULL ) ;
2592 }
2593 }
2594
2595 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
2596 {
2597 Point where = { y , x } ;
2598 ControlPartCode result = kControlNoPart;
2599
2600 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2601 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2602 {
2603 if (PtInRect( where, &m_txnControlBounds ))
2604 {
2605 result = kControlEditTextPart ;
2606 }
2607 else
2608 {
2609 // sometimes we get the coords also in control local coordinates, therefore test again
2610 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2611 {
2612 int x = 0 , y = 0 ;
2613 textctrl->MacClientToRootWindow( &x , &y ) ;
2614 where.h += x ;
2615 where.v += y ;
2616 }
2617
2618 if (PtInRect( where, &m_txnControlBounds ))
2619 result = kControlEditTextPart ;
2620 }
2621 }
2622
2623 return result;
2624 }
2625
2626 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* actionProc )
2627 {
2628 ControlPartCode result = kControlNoPart;
2629
2630 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2631 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2632 {
2633 Point startPt = { y , x } ;
2634 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2635 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2636 {
2637 int x = 0 , y = 0 ;
2638 textctrl->MacClientToRootWindow( &x , &y ) ;
2639 startPt.h += x ;
2640 startPt.v += y ;
2641 }
2642
2643 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
2644 {
2645 case kControlEditTextPart :
2646 {
2647 wxMacWindowClipper clipper( textctrl ) ;
2648 EventRecord rec ;
2649
2650 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
2651 TXNClick( m_txn, &rec );
2652 }
2653 break;
2654
2655 default :
2656 break;
2657 }
2658 }
2659
2660 return result;
2661 }
2662
2663 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2664 {
2665 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2666 if ( textctrl == NULL )
2667 return ;
2668
2669 if (textctrl->MacIsReallyShown())
2670 {
2671 if (IsControlActive(m_controlRef))
2672 {
2673 Point mousep;
2674
2675 wxMacWindowClipper clipper( textctrl ) ;
2676 GetMouse(&mousep);
2677
2678 TXNIdle(m_txn);
2679
2680 if (PtInRect(mousep, &m_txnControlBounds))
2681 {
2682 RgnHandle theRgn = NewRgn();
2683 RectRgn(theRgn, &m_txnControlBounds);
2684 TXNAdjustCursor(m_txn, theRgn);
2685 DisposeRgn(theRgn);
2686 }
2687 }
2688 }
2689 }
2690
2691 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
2692 {
2693 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2694 if ( textctrl == NULL )
2695 return kControlNoPart;
2696
2697 wxMacWindowClipper clipper( textctrl ) ;
2698
2699 EventRecord ev ;
2700 memset( &ev , 0 , sizeof( ev ) ) ;
2701 ev.what = keyDown ;
2702 ev.modifiers = modifiers ;
2703 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
2704 TXNKeyDown( m_txn , &ev );
2705
2706 return kControlEntireControl;
2707 }
2708
2709 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
2710 {
2711 MacActivatePaneText( activating );
2712 }
2713
2714 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
2715 {
2716 ControlPartCode focusResult = kControlFocusNoPart;
2717
2718 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2719 if ( textctrl == NULL )
2720 return focusResult;
2721
2722 wxMacWindowClipper clipper( textctrl ) ;
2723
2724 ControlRef controlFocus = NULL ;
2725 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2726 bool wasFocused = ( controlFocus == m_controlRef ) ;
2727
2728 switch (action)
2729 {
2730 case kControlFocusPrevPart:
2731 case kControlFocusNextPart:
2732 MacFocusPaneText( !wasFocused );
2733 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
2734 break;
2735
2736 case kControlFocusNoPart:
2737 default:
2738 MacFocusPaneText( false );
2739 focusResult = kControlFocusNoPart;
2740 break;
2741 }
2742
2743 return focusResult;
2744 }
2745
2746 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *info )
2747 {
2748 }
2749
2750 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
2751 const wxString& str,
2752 const wxPoint& pos,
2753 const wxSize& size, long style )
2754 : wxMacMLTEControl( wxPeer )
2755 {
2756 m_font = wxPeer->GetFont() ;
2757 m_windowStyle = style ;
2758 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2759
2760 short featureSet =
2761 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
2762 | kControlWantsActivate | kControlHandlesTracking
2763 // | kControlHasSpecialBackground
2764 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
2765
2766 OSStatus err = ::CreateUserPaneControl(
2767 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
2768 &bounds, featureSet, &m_controlRef );
2769 verify_noerr( err );
2770
2771 DoCreate();
2772
2773 AdjustCreationAttributes( *wxWHITE , true ) ;
2774
2775 MacSetObjectVisibility( wxPeer->MacIsReallyShown() ) ;
2776
2777 {
2778 wxString st = str ;
2779 wxMacConvertNewlines10To13( &st ) ;
2780 wxMacWindowClipper clipper( m_peer ) ;
2781 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2782 TXNSetSelection( m_txn, 0, 0 ) ;
2783 }
2784 }
2785
2786 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2787 {
2788 TXNDeleteObject( m_txn );
2789 m_txn = NULL ;
2790 }
2791
2792 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
2793 {
2794 MacSetObjectVisibility( shown ) ;
2795 wxMacControl::VisibilityChanged( shown ) ;
2796 }
2797
2798 void wxMacMLTEClassicControl::SuperChangedPosition()
2799 {
2800 MacUpdatePosition() ;
2801 wxMacControl::SuperChangedPosition() ;
2802 }
2803
2804 #ifdef __WXMAC_OSX__
2805
2806 ControlUserPaneDrawUPP gTPDrawProc = NULL;
2807 ControlUserPaneHitTestUPP gTPHitProc = NULL;
2808 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
2809 ControlUserPaneIdleUPP gTPIdleProc = NULL;
2810 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
2811 ControlUserPaneActivateUPP gTPActivateProc = NULL;
2812 ControlUserPaneFocusUPP gTPFocusProc = NULL;
2813
2814 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
2815 {
2816 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2817 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2818 if ( win )
2819 win->MacControlUserPaneDrawProc( part ) ;
2820 }
2821
2822 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
2823 {
2824 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2825 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2826 if ( win )
2827 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
2828 else
2829 return kControlNoPart ;
2830 }
2831
2832 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
2833 {
2834 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2835 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2836 if ( win )
2837 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
2838 else
2839 return kControlNoPart ;
2840 }
2841
2842 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
2843 {
2844 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2845 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2846 if ( win )
2847 win->MacControlUserPaneIdleProc() ;
2848 }
2849
2850 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
2851 {
2852 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2853 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2854 if ( win )
2855 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
2856 else
2857 return kControlNoPart ;
2858 }
2859
2860 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2861 {
2862 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2863 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2864 if ( win )
2865 win->MacControlUserPaneActivateProc( activating ) ;
2866 }
2867
2868 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2869 {
2870 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2871 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2872 if ( win )
2873 return win->MacControlUserPaneFocusProc( action ) ;
2874 else
2875 return kControlNoPart ;
2876 }
2877
2878 #if 0
2879 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2880 {
2881 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2882 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2883 if ( win )
2884 win->MacControlUserPaneBackgroundProc(info) ;
2885 }
2886 #endif
2887
2888 #endif // __WXMAC_OSX__
2889
2890 // TXNRegisterScrollInfoProc
2891
2892 OSStatus wxMacMLTEClassicControl::DoCreate()
2893 {
2894 Rect bounds;
2895 OSStatus err = noErr ;
2896
2897 // set up our globals
2898 #ifdef __WXMAC_OSX__
2899 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2900 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2901 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2902 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2903 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2904 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2905 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2906
2907 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2908 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2909 #endif
2910
2911 // set the initial settings for our private data
2912
2913 m_txnWindow = GetControlOwner(m_controlRef);
2914 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2915
2916 #ifdef __WXMAC_OSX__
2917 // set up the user pane procedures
2918 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2919 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2920 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2921 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2922 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2923 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2924 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2925 #endif
2926
2927 // calculate the rectangles used by the control
2928 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2929
2930 m_txnControlBounds = bounds ;
2931 m_txnVisBounds = bounds ;
2932
2933 CGrafPtr origPort ;
2934 GDHandle origDev ;
2935
2936 GetGWorld( &origPort, &origDev ) ;
2937 SetPort( m_txnPort );
2938
2939 // create the new edit field
2940 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2941
2942 #ifdef __WXMAC_OSX__
2943 // the scrollbars are not correctly embedded but are inserted at the root:
2944 // this gives us problems as we have erratic redraws even over the structure area
2945
2946 m_sbHorizontal = 0 ;
2947 m_sbVertical = 0 ;
2948 m_lastHorizontalValue = 0 ;
2949 m_lastVerticalValue = 0 ;
2950
2951 Rect sb = { 0 , 0 , 0 , 0 } ;
2952 if ( frameOptions & kTXNWantVScrollBarMask )
2953 {
2954 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2955 SetControlReference( m_sbVertical, (SInt32)this );
2956 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2957 ShowControl( m_sbVertical );
2958 EmbedControl( m_sbVertical , m_controlRef );
2959 frameOptions &= ~kTXNWantVScrollBarMask;
2960 }
2961
2962 if ( frameOptions & kTXNWantHScrollBarMask )
2963 {
2964 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2965 SetControlReference( m_sbHorizontal, (SInt32)this );
2966 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2967 ShowControl( m_sbHorizontal );
2968 EmbedControl( m_sbHorizontal, m_controlRef );
2969 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2970 }
2971
2972 #endif
2973
2974 err = TXNNewObject(
2975 NULL, m_txnWindow, &bounds, frameOptions,
2976 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2977 &m_txn, &m_txnFrameID, NULL );
2978 verify_noerr( err );
2979
2980 #if 0
2981 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2982 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2983 int toptag = WXSIZEOF( iControlTags ) ;
2984 TXNCarbonEventInfo cInfo ;
2985 cInfo.useCarbonEvents = false ;
2986 cInfo.filler = 0 ;
2987 cInfo.flags = 0 ;
2988 cInfo.fDictionary = NULL ;
2989
2990 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2991 #endif
2992
2993 #ifdef __WXMAC_OSX__
2994 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2995 #endif
2996
2997 SetGWorld( origPort , origDev ) ;
2998
2999 return err;
3000 }
3001
3002 // ----------------------------------------------------------------------------
3003 // MLTE control implementation (OSX part)
3004 // ----------------------------------------------------------------------------
3005
3006 #if TARGET_API_MAC_OSX
3007
3008 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
3009
3010 // tiger multi-line textcontrols with no CR in the entire content
3011 // don't scroll automatically, so we need a hack.
3012 // This attempt only works 'before' the key (ie before CallNextEventHandler)
3013 // is processed, thus the scrolling always occurs one character too late, but
3014 // better than nothing ...
3015
3016 static const EventTypeSpec eventList[] =
3017 {
3018 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
3019 } ;
3020
3021 static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
3022 {
3023 OSStatus result = eventNotHandledErr ;
3024 wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
3025
3026 switch ( GetEventKind( event ) )
3027 {
3028 case kEventTextInputUnicodeForKeyEvent :
3029 {
3030 if ( UMAGetSystemVersion() >= 0x1040 )
3031 {
3032 TXNOffset from , to ;
3033 TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
3034 if ( from == to )
3035 TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
3036 }
3037 result = CallNextEventHandler(handler,event);
3038 break;
3039 }
3040 default:
3041 break ;
3042 }
3043
3044 return result ;
3045 }
3046
3047 static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
3048 {
3049 OSStatus result = eventNotHandledErr ;
3050
3051 switch ( GetEventClass( event ) )
3052 {
3053 case kEventClassTextInput :
3054 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
3055 break ;
3056
3057 default :
3058 break ;
3059 }
3060 return result ;
3061 }
3062
3063 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
3064
3065 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
3066 const wxString& str,
3067 const wxPoint& pos,
3068 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
3069 {
3070 m_font = wxPeer->GetFont() ;
3071 m_windowStyle = style ;
3072 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
3073 wxString st = str ;
3074 wxMacConvertNewlines10To13( &st ) ;
3075
3076 HIRect hr = {
3077 { bounds.left , bounds.top },
3078 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
3079
3080 m_scrollView = NULL ;
3081 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
3082 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
3083 {
3084 HIScrollViewCreate(
3085 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
3086 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
3087 &m_scrollView ) ;
3088
3089 HIViewSetFrame( m_scrollView, &hr );
3090 HIViewSetVisible( m_scrollView, true );
3091 }
3092
3093 m_textView = NULL ;
3094 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
3095 m_txn = HITextViewGetTXNObject( m_textView ) ;
3096 HIViewSetVisible( m_textView , true ) ;
3097 if ( m_scrollView )
3098 {
3099 HIViewAddSubview( m_scrollView , m_textView ) ;
3100 m_controlRef = m_scrollView ;
3101 wxPeer->MacInstallEventHandler( (WXWidget) m_textView ) ;
3102 }
3103 else
3104 {
3105 HIViewSetFrame( m_textView, &hr );
3106 m_controlRef = m_textView ;
3107 }
3108
3109 AdjustCreationAttributes( *wxWHITE , true ) ;
3110
3111 wxMacWindowClipper c( m_peer ) ;
3112 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
3113
3114 TXNSetSelection( m_txn, 0, 0 );
3115 TXNShowSelection( m_txn, kTXNShowStart );
3116
3117 InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
3118 GetEventTypeCount(eventList), eventList, this,
3119 &m_textEventHandlerRef);
3120 }
3121
3122 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3123 {
3124 ::RemoveEventHandler( m_textEventHandlerRef ) ;
3125 }
3126
3127 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
3128 {
3129 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, focusPart ) ;
3130 }
3131
3132 bool wxMacMLTEHIViewControl::HasFocus() const
3133 {
3134 ControlRef control ;
3135 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
3136 return control == m_textView ;
3137 }
3138
3139 void wxMacMLTEHIViewControl::SetBackground( const wxBrush &brush )
3140 {
3141 wxMacMLTEControl::SetBackground( brush ) ;
3142
3143 #if 0
3144 CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
3145 RGBColor col = MAC_WXCOLORREF(brush.GetColour().GetPixel()) ;
3146
3147 float component[4] ;
3148 component[0] = col.red / 65536.0 ;
3149 component[1] = col.green / 65536.0 ;
3150 component[2] = col.blue / 65536.0 ;
3151 component[3] = 1.0 ; // alpha
3152
3153 CGColorRef color = CGColorCreate( rgbSpace , component );
3154 HITextViewSetBackgroundColor( m_textView , color );
3155 CGColorSpaceRelease( rgbSpace );
3156 #endif
3157 }
3158
3159 #endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
3160
3161
3162 #endif
3163
3164 #endif // wxUSE_TEXTCTRL