]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/textctrl.cpp
341d97c5e9700d98ea4efc46b4a5095e9c45a4f6
[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 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
1798 {
1799 TXNTypeAttributes typeAttr[4] ;
1800 RGBColor color ;
1801 size_t typeAttrCount = 0 ;
1802
1803 TXNMargins margins;
1804 TXNControlTag controlTags[4];
1805 TXNControlData controlData[4];
1806 size_t controlAttrCount = 0;
1807
1808 TXNTab* tabs = NULL;
1809
1810 bool relayout = false;
1811
1812 if ( style.HasFont() )
1813 {
1814 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
1815 const wxFont &font = style.GetFont() ;
1816 typeAttr[typeAttrCount].tag = kTXNATSUIStyle ;
1817 typeAttr[typeAttrCount].size = kTXNATSUIStyleSize ;
1818 typeAttr[typeAttrCount].data.dataPtr = font.MacGetATSUStyle() ;
1819 typeAttrCount++ ;
1820 }
1821
1822 if ( style.HasTextColour() )
1823 {
1824 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
1825 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
1826
1827 typeAttr[typeAttrCount].tag = kTXNQDFontColorAttribute ;
1828 typeAttr[typeAttrCount].size = kTXNQDFontColorAttributeSize ;
1829 typeAttr[typeAttrCount].data.dataPtr = (void*) &color ;
1830 typeAttrCount++ ;
1831 }
1832
1833 if ( style.HasAlignment() )
1834 {
1835 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1836 SInt32 align;
1837
1838 switch ( style.GetAlignment() )
1839 {
1840 case wxTEXT_ALIGNMENT_LEFT:
1841 align = kTXNFlushLeft;
1842 break;
1843 case wxTEXT_ALIGNMENT_CENTRE:
1844 align = kTXNCenter;
1845 break;
1846 case wxTEXT_ALIGNMENT_RIGHT:
1847 align = kTXNFlushRight;
1848 break;
1849 case wxTEXT_ALIGNMENT_JUSTIFIED:
1850 align = kTXNFullJust;
1851 break;
1852 default :
1853 case wxTEXT_ALIGNMENT_DEFAULT:
1854 align = kTXNFlushDefault;
1855 break;
1856 }
1857
1858 controlTags[controlAttrCount] = kTXNJustificationTag ;
1859 controlData[controlAttrCount].sValue = align ;
1860 controlAttrCount++ ;
1861 }
1862
1863 if ( style.HasLeftIndent() || style.HasRightIndent() )
1864 {
1865 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1866 controlTags[controlAttrCount] = kTXNMarginsTag;
1867 controlData[controlAttrCount].marginsPtr = &margins;
1868 verify_noerr( TXNGetTXNObjectControls (m_txn, 1 ,
1869 &controlTags[controlAttrCount], &controlData[controlAttrCount]) );
1870 if ( style.HasLeftIndent() )
1871 {
1872 margins.leftMargin = style.GetLeftIndent() / 254.0 * 72 + 0.5;
1873 }
1874 if ( style.HasRightIndent() )
1875 {
1876 margins.rightMargin = style.GetRightIndent() / 254.0 * 72 + 0.5;
1877 }
1878 controlAttrCount++ ;
1879 }
1880
1881 if ( style.HasTabs() )
1882 {
1883 const wxArrayInt& tabarray = style.GetTabs();
1884 // unfortunately Mac only applies a tab distance, not individually different tabs
1885 controlTags[controlAttrCount] = kTXNTabSettingsTag;
1886 if ( tabarray.size() > 0 )
1887 controlData[controlAttrCount].tabValue.value = tabarray[0] / 254.0 * 72 + 0.5;
1888 else
1889 controlData[controlAttrCount].tabValue.value = 72 ;
1890
1891 controlData[controlAttrCount].tabValue.tabType = kTXNLeftTab;
1892 controlAttrCount++ ;
1893 }
1894
1895 // unfortunately the relayout is not automatic
1896 if ( controlAttrCount > 0 )
1897 {
1898 verify_noerr( TXNSetTXNObjectControls (m_txn, false /* don't clear all */, controlAttrCount,
1899 controlTags, controlData) );
1900 relayout = true;
1901 }
1902
1903 if ( typeAttrCount > 0 )
1904 {
1905 verify_noerr( TXNSetTypeAttributes( m_txn , typeAttrCount, typeAttr, from , to ) );
1906 relayout = true;
1907 }
1908
1909 if ( tabs != NULL )
1910 {
1911 delete[] tabs;
1912 }
1913
1914 if ( relayout )
1915 {
1916 TXNRecalcTextLayout( m_txn );
1917 }
1918 }
1919
1920 void wxMacMLTEControl::SetFont(const wxFont & font,
1921 const wxColour& foreground,
1922 long WXUNUSED(windowStyle))
1923 {
1924 wxMacEditHelper help( m_txn ) ;
1925 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1926 }
1927
1928 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1929 {
1930 wxMacEditHelper help( m_txn ) ;
1931 TXNSetAttribute( style, start, end ) ;
1932 }
1933
1934 void wxMacMLTEControl::Copy()
1935 {
1936 TXNCopy( m_txn );
1937 }
1938
1939 void wxMacMLTEControl::Cut()
1940 {
1941 TXNCut( m_txn );
1942 }
1943
1944 void wxMacMLTEControl::Paste()
1945 {
1946 TXNPaste( m_txn );
1947 }
1948
1949 bool wxMacMLTEControl::CanPaste() const
1950 {
1951 return TXNIsScrapPastable() ;
1952 }
1953
1954 void wxMacMLTEControl::SetEditable(bool editable)
1955 {
1956 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1957 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1958 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1959 }
1960
1961 wxTextPos wxMacMLTEControl::GetLastPosition() const
1962 {
1963 wxTextPos actualsize = 0 ;
1964
1965 Handle theText ;
1966 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1967
1968 // all done
1969 if ( err == noErr )
1970 {
1971 actualsize = GetHandleSize( theText ) ;
1972 DisposeHandle( theText ) ;
1973 }
1974 else
1975 {
1976 actualsize = 0 ;
1977 }
1978
1979 return actualsize ;
1980 }
1981
1982 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1983 {
1984 wxString value = str ;
1985 wxMacConvertNewlines10To13( &value ) ;
1986
1987 wxMacEditHelper help( m_txn ) ;
1988 #ifndef __LP64__
1989 wxMacWindowClipper c( m_peer ) ;
1990 #endif
1991
1992 TXNSetSelection( m_txn, from, to ) ;
1993 TXNClear( m_txn ) ;
1994 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1995 }
1996
1997 void wxMacMLTEControl::Remove( long from , long to )
1998 {
1999 #ifndef __LP64__
2000 wxMacWindowClipper c( m_peer ) ;
2001 #endif
2002 wxMacEditHelper help( m_txn ) ;
2003 TXNSetSelection( m_txn , from , to ) ;
2004 TXNClear( m_txn ) ;
2005 }
2006
2007 void wxMacMLTEControl::GetSelection( long* from, long* to) const
2008 {
2009 TXNOffset f,t ;
2010 TXNGetSelection( m_txn , &f , &t ) ;
2011 *from = f;
2012 *to = t;
2013 }
2014
2015 void wxMacMLTEControl::SetSelection( long from , long to )
2016 {
2017 #ifndef __LP64__
2018 wxMacWindowClipper c( m_peer ) ;
2019 #endif
2020
2021 // change the selection
2022 if ((from == -1) && (to == -1))
2023 TXNSelectAll( m_txn );
2024 else
2025 TXNSetSelection( m_txn, from, to );
2026
2027 TXNShowSelection( m_txn, kTXNShowStart );
2028 }
2029
2030 void wxMacMLTEControl::WriteText( const wxString& str )
2031 {
2032 wxString st = str ;
2033 wxMacConvertNewlines10To13( &st ) ;
2034
2035 long start , end , dummy ;
2036
2037 GetSelection( &start , &dummy ) ;
2038 #ifndef __LP64__
2039 wxMacWindowClipper c( m_peer ) ;
2040 #endif
2041
2042 {
2043 wxMacEditHelper helper( m_txn ) ;
2044 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2045 }
2046
2047 GetSelection( &dummy, &end ) ;
2048
2049 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2050 }
2051
2052 void wxMacMLTEControl::Clear()
2053 {
2054 #ifndef __LP64__
2055 wxMacWindowClipper c( m_peer ) ;
2056 #endif
2057 wxMacEditHelper st( m_txn ) ;
2058 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
2059 TXNClear( m_txn ) ;
2060 }
2061
2062 bool wxMacMLTEControl::CanUndo() const
2063 {
2064 return TXNCanUndo( m_txn , NULL ) ;
2065 }
2066
2067 void wxMacMLTEControl::Undo()
2068 {
2069 TXNUndo( m_txn ) ;
2070 }
2071
2072 bool wxMacMLTEControl::CanRedo() const
2073 {
2074 return TXNCanRedo( m_txn , NULL ) ;
2075 }
2076
2077 void wxMacMLTEControl::Redo()
2078 {
2079 TXNRedo( m_txn ) ;
2080 }
2081
2082 int wxMacMLTEControl::GetNumberOfLines() const
2083 {
2084 ItemCount lines = 0 ;
2085 TXNGetLineCount( m_txn, &lines ) ;
2086
2087 return lines ;
2088 }
2089
2090 long wxMacMLTEControl::XYToPosition(long x, long y) const
2091 {
2092 Point curpt ;
2093 wxTextPos lastpos ;
2094
2095 // TODO: find a better implementation : while we can get the
2096 // line metrics of a certain line, we don't get its starting
2097 // position, so it would probably be rather a binary search
2098 // for the start position
2099 long xpos = 0, ypos = 0 ;
2100 int lastHeight = 0 ;
2101 ItemCount n ;
2102
2103 lastpos = GetLastPosition() ;
2104 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
2105 {
2106 if ( y == ypos && x == xpos )
2107 return n ;
2108
2109 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2110
2111 if ( curpt.v > lastHeight )
2112 {
2113 xpos = 0 ;
2114 if ( n > 0 )
2115 ++ypos ;
2116
2117 lastHeight = curpt.v ;
2118 }
2119 else
2120 ++xpos ;
2121 }
2122
2123 return 0 ;
2124 }
2125
2126 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
2127 {
2128 Point curpt ;
2129 wxTextPos lastpos ;
2130
2131 if ( y )
2132 *y = 0 ;
2133 if ( x )
2134 *x = 0 ;
2135
2136 lastpos = GetLastPosition() ;
2137 if ( pos <= lastpos )
2138 {
2139 // TODO: find a better implementation - while we can get the
2140 // line metrics of a certain line, we don't get its starting
2141 // position, so it would probably be rather a binary search
2142 // for the start position
2143 long xpos = 0, ypos = 0 ;
2144 int lastHeight = 0 ;
2145 ItemCount n ;
2146
2147 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
2148 {
2149 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2150
2151 if ( curpt.v > lastHeight )
2152 {
2153 xpos = 0 ;
2154 if ( n > 0 )
2155 ++ypos ;
2156
2157 lastHeight = curpt.v ;
2158 }
2159 else
2160 ++xpos ;
2161 }
2162
2163 if ( y )
2164 *y = ypos ;
2165 if ( x )
2166 *x = xpos ;
2167 }
2168
2169 return false ;
2170 }
2171
2172 void wxMacMLTEControl::ShowPosition( long pos )
2173 {
2174 Point current, desired ;
2175 TXNOffset selstart, selend;
2176
2177 TXNGetSelection( m_txn, &selstart, &selend );
2178 TXNOffsetToPoint( m_txn, selstart, &current );
2179 TXNOffsetToPoint( m_txn, pos, &desired );
2180
2181 // TODO: use HIPoints for 10.3 and above
2182
2183 OSErr theErr = noErr;
2184 long dv = desired.v - current.v;
2185 long dh = desired.h - current.h;
2186 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2187 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
2188
2189 // there will be an error returned for classic MLTE implementation when the control is
2190 // invisible, but HITextView works correctly, so we don't assert that one
2191 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2192 }
2193
2194 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
2195 {
2196 #if wxUSE_UNICODE
2197 #if SIZEOF_WCHAR_T == 2
2198 size_t len = st.length() ;
2199 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
2200 #else
2201 wxMBConvUTF16 converter ;
2202 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
2203 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
2204 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
2205 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
2206 free( unibuf ) ;
2207 #endif
2208 #else
2209 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
2210 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
2211 #endif
2212 }
2213
2214 wxString wxMacMLTEControl::GetLineText(long lineNo) const
2215 {
2216 wxString line ;
2217
2218 if ( lineNo < GetNumberOfLines() )
2219 {
2220 Point firstPoint;
2221 Fixed lineWidth, lineHeight, currentHeight;
2222 long ypos ;
2223
2224 // get the first possible position in the control
2225 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2226
2227 // Iterate through the lines until we reach the one we want,
2228 // adding to our current y pixel point position
2229 ypos = 0 ;
2230 currentHeight = 0;
2231 while (ypos < lineNo)
2232 {
2233 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2234 currentHeight += lineHeight;
2235 }
2236
2237 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2238 TXNOffset theOffset;
2239 TXNPointToOffset(m_txn, thePoint, &theOffset);
2240
2241 wxString content = GetStringValue() ;
2242 Point currentPoint = thePoint;
2243 while (thePoint.v == currentPoint.v && theOffset < content.length())
2244 {
2245 line += content[theOffset];
2246 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2247 }
2248 }
2249
2250 return line ;
2251 }
2252
2253 int wxMacMLTEControl::GetLineLength(long lineNo) const
2254 {
2255 int theLength = 0;
2256
2257 if ( lineNo < GetNumberOfLines() )
2258 {
2259 Point firstPoint;
2260 Fixed lineWidth, lineHeight, currentHeight;
2261 long ypos;
2262
2263 // get the first possible position in the control
2264 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2265
2266 // Iterate through the lines until we reach the one we want,
2267 // adding to our current y pixel point position
2268 ypos = 0;
2269 currentHeight = 0;
2270 while (ypos < lineNo)
2271 {
2272 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2273 currentHeight += lineHeight;
2274 }
2275
2276 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2277 TXNOffset theOffset;
2278 TXNPointToOffset(m_txn, thePoint, &theOffset);
2279
2280 wxString content = GetStringValue() ;
2281 Point currentPoint = thePoint;
2282 while (thePoint.v == currentPoint.v && theOffset < content.length())
2283 {
2284 ++theLength;
2285 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2286 }
2287 }
2288
2289 return theLength ;
2290 }
2291
2292 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2293
2294 // ----------------------------------------------------------------------------
2295 // MLTE control implementation (classic part)
2296 // ----------------------------------------------------------------------------
2297
2298 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2299 // has to live on. We have different problems coming from outdated implementations on the
2300 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2301 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2302 // no way out, therefore we are using our own implementation and our own scrollbars ....
2303
2304 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
2305 ControlActionUPP gTXNScrollActionProc = NULL ;
2306
2307 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2308 SInt32 iValue, SInt32 iMaximumValue,
2309 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
2310 {
2311 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
2312 SInt32 value = wxMax( iValue , 0 ) ;
2313 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
2314
2315 if ( iScrollBarOrientation == kTXNHorizontal )
2316 {
2317 if ( mlte->m_sbHorizontal )
2318 {
2319 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
2320 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
2321 mlte->m_lastHorizontalValue = value ;
2322 }
2323 }
2324 else if ( iScrollBarOrientation == kTXNVertical )
2325 {
2326 if ( mlte->m_sbVertical )
2327 {
2328 SetControl32BitValue( mlte->m_sbVertical , value ) ;
2329 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
2330 mlte->m_lastVerticalValue = value ;
2331 }
2332 }
2333 }
2334
2335 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
2336 {
2337 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
2338 if ( mlte == NULL )
2339 return ;
2340
2341 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
2342 return ;
2343
2344 OSStatus err ;
2345 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
2346
2347 SInt32 minimum = 0 ;
2348 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
2349 SInt32 value = GetControl32BitValue( controlRef ) ;
2350 SInt32 delta = 0;
2351
2352 switch ( partCode )
2353 {
2354 case kControlDownButtonPart :
2355 delta = 10 ;
2356 break ;
2357
2358 case kControlUpButtonPart :
2359 delta = -10 ;
2360 break ;
2361
2362 case kControlPageDownPart :
2363 delta = GetControlViewSize( controlRef ) ;
2364 break ;
2365
2366 case kControlPageUpPart :
2367 delta = -GetControlViewSize( controlRef ) ;
2368 break ;
2369
2370 case kControlIndicatorPart :
2371 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
2372 break ;
2373
2374 default :
2375 break ;
2376 }
2377
2378 if ( delta != 0 )
2379 {
2380 SInt32 newValue = value ;
2381
2382 if ( partCode != kControlIndicatorPart )
2383 {
2384 if ( value + delta < minimum )
2385 delta = minimum - value ;
2386 if ( value + delta > maximum )
2387 delta = maximum - value ;
2388
2389 SetControl32BitValue( controlRef , value + delta ) ;
2390 newValue = value + delta ;
2391 }
2392
2393 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
2394 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
2395
2396 err = TXNScroll(
2397 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
2398 &verticalDelta, &horizontalDelta );
2399 verify_noerr( err );
2400
2401 if ( isHorizontal )
2402 mlte->m_lastHorizontalValue = newValue ;
2403 else
2404 mlte->m_lastVerticalValue = newValue ;
2405 }
2406 }
2407
2408 // make correct activations
2409 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
2410 {
2411 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2412
2413 wxMacWindowClipper clipper( textctrl ) ;
2414 TXNActivate( m_txn, m_txnFrameID, setActive );
2415
2416 ControlRef controlFocus = 0 ;
2417 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2418 if ( controlFocus == m_controlRef )
2419 TXNFocus( m_txn, setActive );
2420 }
2421
2422 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
2423 {
2424 TXNFocus( m_txn, setFocus );
2425 }
2426
2427 // guards against inappropriate redraw (hidden objects drawing onto window)
2428
2429 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
2430 {
2431 ControlRef controlFocus = 0 ;
2432 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2433
2434 if ( !vis && (controlFocus == m_controlRef ) )
2435 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
2436
2437 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
2438 TXNControlData iControlData[1] = { { (UInt32)false } };
2439
2440 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
2441
2442 if ( iControlData[0].uValue != vis )
2443 {
2444 iControlData[0].uValue = vis ;
2445 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
2446 }
2447
2448 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2449 // if we run into further problems we might set the FrameBounds to an empty rect here
2450 }
2451
2452 // make sure that the TXNObject is at the right position
2453
2454 void wxMacMLTEClassicControl::MacUpdatePosition()
2455 {
2456 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2457 if ( textctrl == NULL )
2458 return ;
2459
2460 Rect bounds ;
2461 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2462
2463 wxRect visRect = textctrl->MacGetClippedClientRect() ;
2464 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
2465 int x , y ;
2466 x = y = 0 ;
2467 textctrl->MacWindowToRootWindow( &x , &y ) ;
2468 OffsetRect( &visBounds , x , y ) ;
2469
2470 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
2471 {
2472 m_txnControlBounds = bounds ;
2473 m_txnVisBounds = visBounds ;
2474 wxMacWindowClipper cl( textctrl ) ;
2475
2476 if ( m_sbHorizontal || m_sbVertical )
2477 {
2478 int w = bounds.right - bounds.left ;
2479 int h = bounds.bottom - bounds.top ;
2480
2481 if ( m_sbHorizontal )
2482 {
2483 Rect sbBounds ;
2484
2485 sbBounds.left = -1 ;
2486 sbBounds.top = h - 14 ;
2487 sbBounds.right = w + 1 ;
2488 sbBounds.bottom = h + 1 ;
2489
2490 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
2491 SetControlViewSize( m_sbHorizontal , w ) ;
2492 }
2493
2494 if ( m_sbVertical )
2495 {
2496 Rect sbBounds ;
2497
2498 sbBounds.left = w - 14 ;
2499 sbBounds.top = -1 ;
2500 sbBounds.right = w + 1 ;
2501 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
2502
2503 SetControlBounds( m_sbVertical , &sbBounds ) ;
2504 SetControlViewSize( m_sbVertical , h ) ;
2505 }
2506 }
2507
2508 Rect oldviewRect ;
2509 TXNLongRect olddestRect ;
2510 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
2511
2512 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2513 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2514 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2515 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2516 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2517 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2518
2519 if ( olddestRect.right >= 10000 )
2520 destRect.right = destRect.left + 32000 ;
2521
2522 if ( olddestRect.bottom >= 0x20000000 )
2523 destRect.bottom = destRect.top + 0x40000000 ;
2524
2525 SectRect( &viewRect , &visBounds , &viewRect ) ;
2526 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
2527
2528 #if 0
2529 TXNSetFrameBounds(
2530 m_txn,
2531 m_txnControlBounds.top,
2532 m_txnControlBounds.left,
2533 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
2534 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
2535 m_txnFrameID );
2536 #endif
2537
2538 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2539 // movement, therefore we have to force it
2540
2541 // this problem has been reported in OSX as well, so we use this here once again
2542
2543 TXNLongRect textRect ;
2544 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
2545 if ( textRect.left < m_txnControlBounds.left )
2546 TXNShowSelection( m_txn , kTXNShowStart ) ;
2547 }
2548 }
2549
2550 void wxMacMLTEClassicControl::SetRect( Rect *r )
2551 {
2552 wxMacControl::SetRect( r ) ;
2553 MacUpdatePosition() ;
2554 }
2555
2556 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 WXUNUSED(thePart))
2557 {
2558 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2559 if ( textctrl == NULL )
2560 return ;
2561
2562 if ( textctrl->MacIsReallyShown() )
2563 {
2564 wxMacWindowClipper clipper( textctrl ) ;
2565 TXNDraw( m_txn , NULL ) ;
2566 }
2567 }
2568
2569 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
2570 {
2571 Point where = { y , x } ;
2572 ControlPartCode result = kControlNoPart;
2573
2574 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2575 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2576 {
2577 if (PtInRect( where, &m_txnControlBounds ))
2578 {
2579 result = kControlEditTextPart ;
2580 }
2581 else
2582 {
2583 // sometimes we get the coords also in control local coordinates, therefore test again
2584 int x = 0 , y = 0 ;
2585 textctrl->MacClientToRootWindow( &x , &y ) ;
2586 where.h += x ;
2587 where.v += y ;
2588
2589 if (PtInRect( where, &m_txnControlBounds ))
2590 result = kControlEditTextPart ;
2591 }
2592 }
2593
2594 return result;
2595 }
2596
2597 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* WXUNUSED(actionProc) )
2598 {
2599 ControlPartCode result = kControlNoPart;
2600
2601 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2602 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2603 {
2604 Point startPt = { y , x } ;
2605
2606 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2607 int x = 0 , y = 0 ;
2608 textctrl->MacClientToRootWindow( &x , &y ) ;
2609 startPt.h += x ;
2610 startPt.v += y ;
2611
2612 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
2613 {
2614 case kControlEditTextPart :
2615 {
2616 wxMacWindowClipper clipper( textctrl ) ;
2617 EventRecord rec ;
2618
2619 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
2620 TXNClick( m_txn, &rec );
2621 }
2622 break;
2623
2624 default :
2625 break;
2626 }
2627 }
2628
2629 return result;
2630 }
2631
2632 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2633 {
2634 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2635 if ( textctrl == NULL )
2636 return ;
2637
2638 if (textctrl->MacIsReallyShown())
2639 {
2640 if (IsControlActive(m_controlRef))
2641 {
2642 Point mousep;
2643
2644 wxMacWindowClipper clipper( textctrl ) ;
2645 GetMouse(&mousep);
2646
2647 TXNIdle(m_txn);
2648
2649 if (PtInRect(mousep, &m_txnControlBounds))
2650 {
2651 RgnHandle theRgn = NewRgn();
2652 RectRgn(theRgn, &m_txnControlBounds);
2653 TXNAdjustCursor(m_txn, theRgn);
2654 DisposeRgn(theRgn);
2655 }
2656 }
2657 }
2658 }
2659
2660 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
2661 {
2662 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2663 if ( textctrl == NULL )
2664 return kControlNoPart;
2665
2666 wxMacWindowClipper clipper( textctrl ) ;
2667
2668 EventRecord ev ;
2669 memset( &ev , 0 , sizeof( ev ) ) ;
2670 ev.what = keyDown ;
2671 ev.modifiers = modifiers ;
2672 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
2673 TXNKeyDown( m_txn , &ev );
2674
2675 return kControlEntireControl;
2676 }
2677
2678 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
2679 {
2680 MacActivatePaneText( activating );
2681 }
2682
2683 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
2684 {
2685 ControlPartCode focusResult = kControlFocusNoPart;
2686
2687 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2688 if ( textctrl == NULL )
2689 return focusResult;
2690
2691 wxMacWindowClipper clipper( textctrl ) ;
2692
2693 ControlRef controlFocus = NULL ;
2694 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2695 bool wasFocused = ( controlFocus == m_controlRef ) ;
2696
2697 switch (action)
2698 {
2699 case kControlFocusPrevPart:
2700 case kControlFocusNextPart:
2701 MacFocusPaneText( !wasFocused );
2702 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
2703 break;
2704
2705 case kControlFocusNoPart:
2706 default:
2707 MacFocusPaneText( false );
2708 focusResult = kControlFocusNoPart;
2709 break;
2710 }
2711
2712 return focusResult;
2713 }
2714
2715 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info) )
2716 {
2717 }
2718
2719 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
2720 const wxString& str,
2721 const wxPoint& pos,
2722 const wxSize& size, long style )
2723 : wxMacMLTEControl( wxPeer )
2724 {
2725 m_font = wxPeer->GetFont() ;
2726 m_windowStyle = style ;
2727 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2728
2729 short featureSet =
2730 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
2731 | kControlWantsActivate | kControlHandlesTracking
2732 // | kControlHasSpecialBackground
2733 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
2734
2735 OSStatus err = ::CreateUserPaneControl(
2736 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
2737 &bounds, featureSet, &m_controlRef );
2738 verify_noerr( err );
2739
2740 DoCreate();
2741
2742 AdjustCreationAttributes( *wxWHITE , true ) ;
2743
2744 MacSetObjectVisibility( wxPeer->MacIsReallyShown() ) ;
2745
2746 {
2747 wxString st = str ;
2748 wxMacConvertNewlines10To13( &st ) ;
2749 wxMacWindowClipper clipper( m_peer ) ;
2750 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2751 TXNSetSelection( m_txn, 0, 0 ) ;
2752 }
2753 }
2754
2755 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2756 {
2757 TXNDeleteObject( m_txn );
2758 m_txn = NULL ;
2759 }
2760
2761 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
2762 {
2763 MacSetObjectVisibility( shown ) ;
2764 wxMacControl::VisibilityChanged( shown ) ;
2765 }
2766
2767 void wxMacMLTEClassicControl::SuperChangedPosition()
2768 {
2769 MacUpdatePosition() ;
2770 wxMacControl::SuperChangedPosition() ;
2771 }
2772
2773 ControlUserPaneDrawUPP gTPDrawProc = NULL;
2774 ControlUserPaneHitTestUPP gTPHitProc = NULL;
2775 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
2776 ControlUserPaneIdleUPP gTPIdleProc = NULL;
2777 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
2778 ControlUserPaneActivateUPP gTPActivateProc = NULL;
2779 ControlUserPaneFocusUPP gTPFocusProc = NULL;
2780
2781 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
2782 {
2783 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2784 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2785 if ( win )
2786 win->MacControlUserPaneDrawProc( part ) ;
2787 }
2788
2789 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
2790 {
2791 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2792 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2793 if ( win )
2794 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
2795 else
2796 return kControlNoPart ;
2797 }
2798
2799 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
2800 {
2801 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2802 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2803 if ( win )
2804 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
2805 else
2806 return kControlNoPart ;
2807 }
2808
2809 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
2810 {
2811 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2812 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2813 if ( win )
2814 win->MacControlUserPaneIdleProc() ;
2815 }
2816
2817 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
2818 {
2819 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2820 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2821 if ( win )
2822 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
2823 else
2824 return kControlNoPart ;
2825 }
2826
2827 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2828 {
2829 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2830 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2831 if ( win )
2832 win->MacControlUserPaneActivateProc( activating ) ;
2833 }
2834
2835 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2836 {
2837 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2838 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2839 if ( win )
2840 return win->MacControlUserPaneFocusProc( action ) ;
2841 else
2842 return kControlNoPart ;
2843 }
2844
2845 #if 0
2846 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2847 {
2848 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2849 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2850 if ( win )
2851 win->MacControlUserPaneBackgroundProc(info) ;
2852 }
2853 #endif
2854
2855 // TXNRegisterScrollInfoProc
2856
2857 OSStatus wxMacMLTEClassicControl::DoCreate()
2858 {
2859 Rect bounds;
2860 OSStatus err = noErr ;
2861
2862 // set up our globals
2863 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2864 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2865 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2866 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2867 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2868 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2869 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2870
2871 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2872 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2873
2874 // set the initial settings for our private data
2875
2876 m_txnWindow = GetControlOwner(m_controlRef);
2877 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2878
2879 // set up the user pane procedures
2880 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2881 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2882 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2883 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2884 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2885 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2886 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2887
2888 // calculate the rectangles used by the control
2889 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2890
2891 m_txnControlBounds = bounds ;
2892 m_txnVisBounds = bounds ;
2893
2894 CGrafPtr origPort ;
2895 GDHandle origDev ;
2896
2897 GetGWorld( &origPort, &origDev ) ;
2898 SetPort( m_txnPort );
2899
2900 // create the new edit field
2901 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2902
2903 // the scrollbars are not correctly embedded but are inserted at the root:
2904 // this gives us problems as we have erratic redraws even over the structure area
2905
2906 m_sbHorizontal = 0 ;
2907 m_sbVertical = 0 ;
2908 m_lastHorizontalValue = 0 ;
2909 m_lastVerticalValue = 0 ;
2910
2911 Rect sb = { 0 , 0 , 0 , 0 } ;
2912 if ( frameOptions & kTXNWantVScrollBarMask )
2913 {
2914 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2915 SetControlReference( m_sbVertical, (SInt32)this );
2916 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2917 ShowControl( m_sbVertical );
2918 EmbedControl( m_sbVertical , m_controlRef );
2919 frameOptions &= ~kTXNWantVScrollBarMask;
2920 }
2921
2922 if ( frameOptions & kTXNWantHScrollBarMask )
2923 {
2924 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2925 SetControlReference( m_sbHorizontal, (SInt32)this );
2926 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2927 ShowControl( m_sbHorizontal );
2928 EmbedControl( m_sbHorizontal, m_controlRef );
2929 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2930 }
2931
2932 err = TXNNewObject(
2933 NULL, m_txnWindow, &bounds, frameOptions,
2934 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2935 &m_txn, &m_txnFrameID, NULL );
2936 verify_noerr( err );
2937
2938 #if 0
2939 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2940 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2941 int toptag = WXSIZEOF( iControlTags ) ;
2942 TXNCarbonEventInfo cInfo ;
2943 cInfo.useCarbonEvents = false ;
2944 cInfo.filler = 0 ;
2945 cInfo.flags = 0 ;
2946 cInfo.fDictionary = NULL ;
2947
2948 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2949 #endif
2950
2951 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2952
2953 SetGWorld( origPort , origDev ) ;
2954
2955 return err;
2956 }
2957 #endif
2958
2959 // ----------------------------------------------------------------------------
2960 // MLTE control implementation (OSX part)
2961 // ----------------------------------------------------------------------------
2962
2963 // tiger multi-line textcontrols with no CR in the entire content
2964 // don't scroll automatically, so we need a hack.
2965 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2966 // is processed, thus the scrolling always occurs one character too late, but
2967 // better than nothing ...
2968
2969 static const EventTypeSpec eventList[] =
2970 {
2971 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
2972 } ;
2973
2974 static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
2975 {
2976 OSStatus result = eventNotHandledErr ;
2977 wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
2978
2979 switch ( GetEventKind( event ) )
2980 {
2981 case kEventTextInputUnicodeForKeyEvent :
2982 {
2983 if ( UMAGetSystemVersion() >= 0x1040 )
2984 {
2985 TXNOffset from , to ;
2986 TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
2987 if ( from == to )
2988 TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
2989 }
2990 result = CallNextEventHandler(handler,event);
2991 break;
2992 }
2993 default:
2994 break ;
2995 }
2996
2997 return result ;
2998 }
2999
3000 static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
3001 {
3002 OSStatus result = eventNotHandledErr ;
3003
3004 switch ( GetEventClass( event ) )
3005 {
3006 case kEventClassTextInput :
3007 result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
3008 break ;
3009
3010 default :
3011 break ;
3012 }
3013 return result ;
3014 }
3015
3016 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
3017
3018 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
3019 const wxString& str,
3020 const wxPoint& pos,
3021 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
3022 {
3023 m_font = wxPeer->GetFont() ;
3024 m_windowStyle = style ;
3025 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
3026 wxString st = str ;
3027 wxMacConvertNewlines10To13( &st ) ;
3028
3029 HIRect hr = {
3030 { bounds.left , bounds.top },
3031 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
3032
3033 m_scrollView = NULL ;
3034 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
3035 if (( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask)) || !(frameOptions &kTXNSingleLineOnlyMask))
3036 {
3037 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
3038 {
3039 HIScrollViewCreate(
3040 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
3041 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
3042 &m_scrollView ) ;
3043 }
3044 else
3045 {
3046 HIScrollViewCreate(kHIScrollViewOptionsVertScroll,&m_scrollView);
3047 HIScrollViewSetScrollBarAutoHide(m_scrollView,true);
3048 }
3049
3050 HIViewSetFrame( m_scrollView, &hr );
3051 HIViewSetVisible( m_scrollView, true );
3052 }
3053
3054 m_textView = NULL ;
3055 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
3056 m_txn = HITextViewGetTXNObject( m_textView ) ;
3057 HIViewSetVisible( m_textView , true ) ;
3058 if ( m_scrollView )
3059 {
3060 HIViewAddSubview( m_scrollView , m_textView ) ;
3061 m_controlRef = m_scrollView ;
3062 wxPeer->MacInstallEventHandler( (WXWidget) m_textView ) ;
3063 }
3064 else
3065 {
3066 HIViewSetFrame( m_textView, &hr );
3067 m_controlRef = m_textView ;
3068 }
3069
3070 AdjustCreationAttributes( *wxWHITE , true ) ;
3071 #ifndef __LP64__
3072 wxMacWindowClipper c( m_peer ) ;
3073 #endif
3074 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
3075
3076 TXNSetSelection( m_txn, 0, 0 );
3077 TXNShowSelection( m_txn, kTXNShowStart );
3078
3079 InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
3080 GetEventTypeCount(eventList), eventList, this,
3081 NULL);
3082 }
3083
3084 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3085 {
3086 }
3087
3088 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
3089 {
3090 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, focusPart ) ;
3091 }
3092
3093 bool wxMacMLTEHIViewControl::HasFocus() const
3094 {
3095 ControlRef control ;
3096 if ( GetUserFocusWindow() == NULL )
3097 return false;
3098
3099 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
3100 return control == m_textView ;
3101 }
3102
3103 void wxMacMLTEHIViewControl::SetBackground( const wxBrush &brush )
3104 {
3105 wxMacMLTEControl::SetBackground( brush ) ;
3106
3107 #if 0
3108 CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
3109 RGBColor col = MAC_WXCOLORREF(brush.GetColour().GetPixel()) ;
3110
3111 float component[4] ;
3112 component[0] = col.red / 65536.0 ;
3113 component[1] = col.green / 65536.0 ;
3114 component[2] = col.blue / 65536.0 ;
3115 component[3] = 1.0 ; // alpha
3116
3117 CGColorRef color = CGColorCreate( rgbSpace , component );
3118 HITextViewSetBackgroundColor( m_textView , color );
3119 CGColorSpaceRelease( rgbSpace );
3120 #endif
3121 }
3122
3123 #endif // wxUSE_TEXTCTRL