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