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