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