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