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