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