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