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