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