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