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