]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/textctrl.cpp
weak link test is not optimal, as CoreText is already having some symbols defined...
[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 // common parts for implementations based on MLTE
174
175 class wxMacMLTEControl : public wxMacTextControl
176 {
177 public :
178 wxMacMLTEControl( wxTextCtrl *peer ) ;
179
180 virtual wxString GetStringValue() const ;
181 virtual void SetStringValue( const wxString &str ) ;
182
183 static TXNFrameOptions FrameOptionsFromWXStyle( long wxStyle ) ;
184
185 void AdjustCreationAttributes( const wxColour& background, bool visible ) ;
186
187 virtual void SetFont( const wxFont & font, const wxColour& foreground, long windowStyle ) ;
188 virtual void SetBackgroundColour(const wxColour& col );
189 virtual void SetStyle( long start, long end, const wxTextAttr& style ) ;
190 virtual void Copy() ;
191 virtual void Cut() ;
192 virtual void Paste() ;
193 virtual bool CanPaste() const ;
194 virtual void SetEditable( bool editable ) ;
195 virtual wxTextPos GetLastPosition() const ;
196 virtual void Replace( long from, long to, const wxString &str ) ;
197 virtual void Remove( long from, long to ) ;
198 virtual void GetSelection( long* from, long* to ) const ;
199 virtual void SetSelection( long from, long to ) ;
200
201 virtual void WriteText( const wxString& str ) ;
202
203 virtual bool HasOwnContextMenu() const
204 {
205 TXNCommandEventSupportOptions options ;
206 TXNGetCommandEventSupport( m_txn , & options ) ;
207 return options & kTXNSupportEditCommandProcessing ;
208 }
209
210 virtual void CheckSpelling(bool check)
211 {
212 TXNSetSpellCheckAsYouType( m_txn, (Boolean) check );
213 }
214 virtual void Clear() ;
215
216 virtual bool CanUndo() const ;
217 virtual void Undo() ;
218 virtual bool CanRedo() const;
219 virtual void Redo() ;
220 virtual int GetNumberOfLines() const ;
221 virtual long XYToPosition(long x, long y) const ;
222 virtual bool PositionToXY(long pos, long *x, long *y) const ;
223 virtual void ShowPosition( long pos ) ;
224 virtual int GetLineLength(long lineNo) const ;
225 virtual wxString GetLineText(long lineNo) const ;
226
227 void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
228 TXNObject GetTXNObject() { return m_txn ; }
229
230 protected :
231 void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
232
233 TXNObject m_txn ;
234 } ;
235
236 // implementation available under OSX
237
238 class wxMacMLTEHIViewControl : public wxMacMLTEControl
239 {
240 public :
241 wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
242 const wxString& str,
243 const wxPoint& pos,
244 const wxSize& size, long style ) ;
245 virtual ~wxMacMLTEHIViewControl() ;
246
247 virtual OSStatus SetFocus( ControlFocusPart focusPart ) ;
248 virtual bool HasFocus() const ;
249 virtual void SetBackgroundColour(const wxColour& col ) ;
250
251 protected :
252 HIViewRef m_scrollView ;
253 HIViewRef m_textView ;
254 };
255
256 // 'classic' MLTE implementation
257
258 class wxMacMLTEClassicControl : public wxMacMLTEControl
259 {
260 public :
261 wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
262 const wxString& str,
263 const wxPoint& pos,
264 const wxSize& size, long style ) ;
265 virtual ~wxMacMLTEClassicControl() ;
266
267 virtual void VisibilityChanged(bool shown) ;
268 virtual void SuperChangedPosition() ;
269
270 virtual void MacControlUserPaneDrawProc(wxInt16 part) ;
271 virtual wxInt16 MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y) ;
272 virtual wxInt16 MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc) ;
273 virtual void MacControlUserPaneIdleProc() ;
274 virtual wxInt16 MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers) ;
275 virtual void MacControlUserPaneActivateProc(bool activating) ;
276 virtual wxInt16 MacControlUserPaneFocusProc(wxInt16 action) ;
277 virtual void MacControlUserPaneBackgroundProc(void* info) ;
278
279 virtual bool SetupCursor( const wxPoint& WXUNUSED(pt) )
280 {
281 MacControlUserPaneIdleProc();
282 return true;
283 }
284
285 virtual void SetRect( Rect *r ) ;
286
287 protected :
288 OSStatus DoCreate();
289
290 void MacUpdatePosition() ;
291 void MacActivatePaneText(bool setActive) ;
292 void MacFocusPaneText(bool setFocus) ;
293 void MacSetObjectVisibility(bool vis) ;
294
295 private :
296 TXNFrameID m_txnFrameID ;
297 GrafPtr m_txnPort ;
298 WindowRef m_txnWindow ;
299 // bounds of the control as we last did set the txn frames
300 Rect m_txnControlBounds ;
301 Rect m_txnVisBounds ;
302
303 static pascal void TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode ) ;
304 static pascal void TXNScrollInfoProc(
305 SInt32 iValue, SInt32 iMaximumValue,
306 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon ) ;
307
308 ControlRef m_sbHorizontal ;
309 SInt32 m_lastHorizontalValue ;
310 ControlRef m_sbVertical ;
311 SInt32 m_lastVerticalValue ;
312 };
313
314
315 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
316
317 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
318 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
319 EVT_CHAR(wxTextCtrl::OnChar)
320 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
321 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
322 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
323 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
324 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
325 EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete)
326 EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll)
327
328 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu)
329
330 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
331 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
332 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
333 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
334 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
335 EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete)
336 EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll)
337 END_EVENT_TABLE()
338
339
340 void wxTextCtrl::Init()
341 {
342 m_editable = true ;
343 m_dirty = false;
344
345 m_maxLength = 0;
346 m_privateContextMenu = NULL;
347 m_triggerOnSetValue = true ;
348 }
349
350 wxTextCtrl::~wxTextCtrl()
351 {
352 delete m_privateContextMenu;
353 }
354
355 bool wxTextCtrl::Create( wxWindow *parent,
356 wxWindowID id,
357 const wxString& str,
358 const wxPoint& pos,
359 const wxSize& size,
360 long style,
361 const wxValidator& validator,
362 const wxString& name )
363 {
364 m_macIsUserPane = false ;
365 m_editable = true ;
366
367 if ( ! (style & wxNO_BORDER) )
368 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
369
370 if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
371 return false;
372
373 if ( m_windowStyle & wxTE_MULTILINE )
374 {
375 // always turn on this style for multi-line controls
376 m_windowStyle |= wxTE_PROCESS_ENTER;
377 style |= wxTE_PROCESS_ENTER ;
378 }
379
380 CreatePeer( str, pos, size, style );
381
382 MacPostControlCreate(pos, size) ;
383
384 // only now the embedding is correct and we can do a positioning update
385
386 MacSuperChangedPosition() ;
387
388 if ( m_windowStyle & wxTE_READONLY)
389 SetEditable( false ) ;
390
391 SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ;
392
393 return true;
394 }
395
396 void wxTextCtrl::CreatePeer(
397 const wxString& str,
398 const wxPoint& pos,
399 const wxSize& size, long style )
400 {
401 bool forceMLTE = false ;
402
403 #if wxUSE_SYSTEM_OPTIONS
404 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE ) == 1))
405 {
406 forceMLTE = true ;
407 }
408 #endif
409
410 if ( UMAGetSystemVersion() >= 0x1050 )
411 forceMLTE = false;
412
413 if ( !forceMLTE )
414 {
415 if ( m_windowStyle & wxTE_MULTILINE || ( UMAGetSystemVersion() >= 0x1050 ) )
416 m_peer = new wxMacMLTEHIViewControl( this , str , pos , size , style ) ;
417 }
418
419 if ( !m_peer )
420 {
421 if ( !(m_windowStyle & wxTE_MULTILINE) && !forceMLTE )
422 {
423 m_peer = new wxMacUnicodeTextControl( this , str , pos , size , style ) ;
424 }
425 }
426
427 // the horizontal single line scrolling bug that made us keep the classic implementation
428 // is fixed in 10.5
429 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
430 if ( !m_peer )
431 m_peer = new wxMacMLTEClassicControl( this , str , pos , size , style ) ;
432 #endif
433 }
434
435 void wxTextCtrl::MacSuperChangedPosition()
436 {
437 wxWindow::MacSuperChangedPosition() ;
438 GetPeer()->SuperChangedPosition() ;
439 }
440
441 void wxTextCtrl::MacVisibilityChanged()
442 {
443 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
444 }
445
446 void wxTextCtrl::MacCheckSpelling(bool check)
447 {
448 GetPeer()->CheckSpelling(check);
449 }
450
451 wxString wxTextCtrl::GetValue() const
452 {
453 return GetPeer()->GetStringValue() ;
454 }
455
456 void wxTextCtrl::GetSelection(long* from, long* to) const
457 {
458 GetPeer()->GetSelection( from , to ) ;
459 }
460
461 void wxTextCtrl::DoSetValue(const wxString& str, int flags)
462 {
463 // optimize redraws
464 if ( GetValue() == str )
465 return;
466
467 GetPeer()->SetStringValue( str ) ;
468
469 if ( (flags & SetValue_SendEvent) && m_triggerOnSetValue )
470 {
471 SendTextUpdatedEvent();
472 }
473 }
474
475 void wxTextCtrl::SetMaxLength(unsigned long len)
476 {
477 m_maxLength = len ;
478 }
479
480 bool wxTextCtrl::SetFont( const wxFont& font )
481 {
482 if ( !wxTextCtrlBase::SetFont( font ) )
483 return false ;
484
485 GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle() ) ;
486
487 return true ;
488 }
489
490 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
491 {
492 GetPeer()->SetStyle( start , end , style ) ;
493
494 return true ;
495 }
496
497 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
498 {
499 wxTextCtrlBase::SetDefaultStyle( style ) ;
500 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
501
502 return true ;
503 }
504
505 // Clipboard operations
506
507 void wxTextCtrl::Copy()
508 {
509 if (CanCopy())
510 GetPeer()->Copy() ;
511 }
512
513 void wxTextCtrl::Cut()
514 {
515 if (CanCut())
516 {
517 GetPeer()->Cut() ;
518
519 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
520 event.SetEventObject( this );
521 HandleWindowEvent( event );
522 }
523 }
524
525 void wxTextCtrl::Paste()
526 {
527 if (CanPaste())
528 {
529 GetPeer()->Paste() ;
530
531 // TODO: eventually we should add setting the default style again
532
533 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
534 event.SetEventObject( this );
535 HandleWindowEvent( event );
536 }
537 }
538
539 bool wxTextCtrl::CanCopy() const
540 {
541 // Can copy if there's a selection
542 long from, to;
543 GetSelection( &from, &to );
544
545 return (from != to);
546 }
547
548 bool wxTextCtrl::CanCut() const
549 {
550 if ( !IsEditable() )
551 return false;
552
553 // Can cut if there's a selection
554 long from, to;
555 GetSelection( &from, &to );
556
557 return (from != to);
558 }
559
560 bool wxTextCtrl::CanPaste() const
561 {
562 if (!IsEditable())
563 return false;
564
565 return GetPeer()->CanPaste() ;
566 }
567
568 void wxTextCtrl::SetEditable(bool editable)
569 {
570 if ( editable != m_editable )
571 {
572 m_editable = editable ;
573 GetPeer()->SetEditable( editable ) ;
574 }
575 }
576
577 void wxTextCtrl::SetInsertionPoint(long pos)
578 {
579 SetSelection( pos , pos ) ;
580 }
581
582 void wxTextCtrl::SetInsertionPointEnd()
583 {
584 wxTextPos pos = GetLastPosition();
585 SetInsertionPoint( pos );
586 }
587
588 long wxTextCtrl::GetInsertionPoint() const
589 {
590 long begin, end ;
591 GetSelection( &begin , &end ) ;
592
593 return begin ;
594 }
595
596 wxTextPos wxTextCtrl::GetLastPosition() const
597 {
598 return GetPeer()->GetLastPosition() ;
599 }
600
601 void wxTextCtrl::Replace(long from, long to, const wxString& str)
602 {
603 GetPeer()->Replace( from , to , str ) ;
604 }
605
606 void wxTextCtrl::Remove(long from, long to)
607 {
608 GetPeer()->Remove( from , to ) ;
609 }
610
611 void wxTextCtrl::SetSelection(long from, long to)
612 {
613 GetPeer()->SetSelection( from , to ) ;
614 }
615
616 void wxTextCtrl::WriteText(const wxString& str)
617 {
618 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
619 if ( !wxIsMainThread() )
620 {
621 // unfortunately CW 8 is not able to correctly deduce the template types,
622 // so we have to instantiate explicitly
623 wxMacMPRemoteGUICall<wxTextCtrl,wxString>( this , &wxTextCtrl::WriteText , str ) ;
624
625 return ;
626 }
627
628 GetPeer()->WriteText( str ) ;
629 }
630
631 void wxTextCtrl::AppendText(const wxString& text)
632 {
633 SetInsertionPointEnd();
634 WriteText( text );
635 }
636
637 void wxTextCtrl::Clear()
638 {
639 GetPeer()->Clear() ;
640 }
641
642 bool wxTextCtrl::IsModified() const
643 {
644 return m_dirty;
645 }
646
647 bool wxTextCtrl::IsEditable() const
648 {
649 return IsEnabled() && m_editable ;
650 }
651
652 bool wxTextCtrl::AcceptsFocus() const
653 {
654 // we don't want focus if we can't be edited
655 return /*IsEditable() && */ wxControl::AcceptsFocus();
656 }
657
658 wxSize wxTextCtrl::DoGetBestSize() const
659 {
660 int wText, hText;
661
662 // these are the numbers from the HIG:
663 // we reduce them by the borders first
664 wText = 100 ;
665
666 switch ( m_windowVariant )
667 {
668 case wxWINDOW_VARIANT_NORMAL :
669 hText = 22 - 6 ;
670 break ;
671
672 case wxWINDOW_VARIANT_SMALL :
673 hText = 19 - 6 ;
674 break ;
675
676 case wxWINDOW_VARIANT_MINI :
677 hText = 15 - 6 ;
678 break ;
679
680 default :
681 hText = 22 - 6;
682 break ;
683 }
684
685 // as the above numbers have some free space around the text
686 // we get 5 lines like this anyway
687 if ( m_windowStyle & wxTE_MULTILINE )
688 hText *= 5 ;
689
690 if ( !HasFlag(wxNO_BORDER) )
691 hText += 6 ;
692
693 return wxSize(wText, hText);
694 }
695
696 // ----------------------------------------------------------------------------
697 // Undo/redo
698 // ----------------------------------------------------------------------------
699
700 void wxTextCtrl::Undo()
701 {
702 if (CanUndo())
703 GetPeer()->Undo() ;
704 }
705
706 void wxTextCtrl::Redo()
707 {
708 if (CanRedo())
709 GetPeer()->Redo() ;
710 }
711
712 bool wxTextCtrl::CanUndo() const
713 {
714 if ( !IsEditable() )
715 return false ;
716
717 return GetPeer()->CanUndo() ;
718 }
719
720 bool wxTextCtrl::CanRedo() const
721 {
722 if ( !IsEditable() )
723 return false ;
724
725 return GetPeer()->CanRedo() ;
726 }
727
728 void wxTextCtrl::MarkDirty()
729 {
730 m_dirty = true;
731 }
732
733 void wxTextCtrl::DiscardEdits()
734 {
735 m_dirty = false;
736 }
737
738 int wxTextCtrl::GetNumberOfLines() const
739 {
740 return GetPeer()->GetNumberOfLines() ;
741 }
742
743 long wxTextCtrl::XYToPosition(long x, long y) const
744 {
745 return GetPeer()->XYToPosition( x , y ) ;
746 }
747
748 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
749 {
750 return GetPeer()->PositionToXY( pos , x , y ) ;
751 }
752
753 void wxTextCtrl::ShowPosition(long pos)
754 {
755 return GetPeer()->ShowPosition(pos) ;
756 }
757
758 int wxTextCtrl::GetLineLength(long lineNo) const
759 {
760 return GetPeer()->GetLineLength(lineNo) ;
761 }
762
763 wxString wxTextCtrl::GetLineText(long lineNo) const
764 {
765 return GetPeer()->GetLineText(lineNo) ;
766 }
767
768 void wxTextCtrl::Command(wxCommandEvent & event)
769 {
770 SetValue(event.GetString());
771 ProcessCommand(event);
772 }
773
774 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
775 {
776 // By default, load the first file into the text window.
777 if (event.GetNumberOfFiles() > 0)
778 LoadFile( event.GetFiles()[0] );
779 }
780
781 void wxTextCtrl::OnChar(wxKeyEvent& event)
782 {
783 int key = event.GetKeyCode() ;
784 bool eat_key = false ;
785
786 if ( key == 'a' && event.MetaDown() )
787 {
788 SelectAll() ;
789
790 return ;
791 }
792
793 if ( key == 'c' && event.MetaDown() )
794 {
795 if ( CanCopy() )
796 Copy() ;
797
798 return ;
799 }
800
801 if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
802 !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
803 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
804 )
805 {
806 // eat it
807 return ;
808 }
809
810 // Check if we have reached the max # of chars (if it is set), but still
811 // allow navigation and deletion
812 if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
813 key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB &&
814 key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) )
815 )
816 {
817 // eat it, we don't want to add more than allowed # of characters
818
819 // TODO: generate EVT_TEXT_MAXLEN()
820 return;
821 }
822
823 // assume that any key not processed yet is going to modify the control
824 m_dirty = true;
825
826 if ( key == 'v' && event.MetaDown() )
827 {
828 if ( CanPaste() )
829 Paste() ;
830
831 return ;
832 }
833
834 if ( key == 'x' && event.MetaDown() )
835 {
836 if ( CanCut() )
837 Cut() ;
838
839 return ;
840 }
841
842 switch ( key )
843 {
844 case WXK_RETURN:
845 if (m_windowStyle & wxTE_PROCESS_ENTER)
846 {
847 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
848 event.SetEventObject( this );
849 event.SetString( GetValue() );
850 if ( HandleWindowEvent(event) )
851 return;
852 }
853
854 if ( !(m_windowStyle & wxTE_MULTILINE) )
855 {
856 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
857 if ( tlw && tlw->GetDefaultItem() )
858 {
859 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
860 if ( def && def->IsEnabled() )
861 {
862 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
863 event.SetEventObject(def);
864 def->Command(event);
865
866 return ;
867 }
868 }
869
870 // this will make wxWidgets eat the ENTER key so that
871 // we actually prevent line wrapping in a single line text control
872 eat_key = true;
873 }
874 break;
875
876 case WXK_TAB:
877 if ( !(m_windowStyle & wxTE_PROCESS_TAB))
878 {
879 int flags = 0;
880 if (!event.ShiftDown())
881 flags |= wxNavigationKeyEvent::IsForward ;
882 if (event.ControlDown())
883 flags |= wxNavigationKeyEvent::WinChange ;
884 Navigate(flags);
885
886 return;
887 }
888 else
889 {
890 // This is necessary (don't know why);
891 // otherwise the tab will not be inserted.
892 WriteText(wxT("\t"));
893 eat_key = true;
894 }
895 break;
896
897 default:
898 break;
899 }
900
901 if (!eat_key)
902 {
903 // perform keystroke handling
904 event.Skip(true) ;
905 }
906
907 if ( ( key >= 0x20 && key < WXK_START ) ||
908 ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
909 key == WXK_RETURN ||
910 key == WXK_DELETE ||
911 key == WXK_BACK)
912 {
913 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
914 event1.SetEventObject( this );
915 wxPostEvent( GetEventHandler(), event1 );
916 }
917 }
918
919 // ----------------------------------------------------------------------------
920 // standard handlers for standard edit menu events
921 // ----------------------------------------------------------------------------
922
923 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
924 {
925 Cut();
926 }
927
928 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
929 {
930 Copy();
931 }
932
933 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
934 {
935 Paste();
936 }
937
938 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
939 {
940 Undo();
941 }
942
943 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
944 {
945 Redo();
946 }
947
948 void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
949 {
950 long from, to;
951
952 GetSelection( &from, &to );
953 if (from != -1 && to != -1)
954 Remove( from, to );
955 }
956
957 void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
958 {
959 SetSelection(-1, -1);
960 }
961
962 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
963 {
964 event.Enable( CanCut() );
965 }
966
967 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
968 {
969 event.Enable( CanCopy() );
970 }
971
972 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
973 {
974 event.Enable( CanPaste() );
975 }
976
977 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
978 {
979 event.Enable( CanUndo() );
980 }
981
982 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
983 {
984 event.Enable( CanRedo() );
985 }
986
987 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
988 {
989 long from, to;
990
991 GetSelection( &from, &to );
992 event.Enable( from != -1 && to != -1 && from != to && IsEditable() ) ;
993 }
994
995 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
996 {
997 event.Enable(GetLastPosition() > 0);
998 }
999
1000 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1001
1002 void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
1003 {
1004 if ( GetPeer()->HasOwnContextMenu() )
1005 {
1006 event.Skip() ;
1007 return ;
1008 }
1009
1010 if (m_privateContextMenu == NULL)
1011 {
1012 m_privateContextMenu = new wxMenu;
1013 m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
1014 m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
1015 m_privateContextMenu->AppendSeparator();
1016 m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
1017 m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
1018 m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
1019 m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
1020 m_privateContextMenu->AppendSeparator();
1021 m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
1022 }
1023
1024 if (m_privateContextMenu != NULL)
1025 PopupMenu(m_privateContextMenu);
1026 }
1027
1028 bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
1029 {
1030 if ( !GetPeer()->SetupCursor( pt ) )
1031 return wxWindow::MacSetupCursor( pt ) ;
1032 else
1033 return true ;
1034 }
1035
1036 // ----------------------------------------------------------------------------
1037 // implementation base class
1038 // ----------------------------------------------------------------------------
1039
1040 wxMacTextControl::wxMacTextControl(wxTextCtrl* peer) :
1041 wxMacControl( peer )
1042 {
1043 }
1044
1045 wxMacTextControl::~wxMacTextControl()
1046 {
1047 }
1048
1049 void wxMacTextControl::SetStyle(long WXUNUSED(start),
1050 long WXUNUSED(end),
1051 const wxTextAttr& WXUNUSED(style))
1052 {
1053 }
1054
1055 void wxMacTextControl::Copy()
1056 {
1057 }
1058
1059 void wxMacTextControl::Cut()
1060 {
1061 }
1062
1063 void wxMacTextControl::Paste()
1064 {
1065 }
1066
1067 bool wxMacTextControl::CanPaste() const
1068 {
1069 return false ;
1070 }
1071
1072 void wxMacTextControl::SetEditable(bool WXUNUSED(editable))
1073 {
1074 }
1075
1076 wxTextPos wxMacTextControl::GetLastPosition() const
1077 {
1078 return GetStringValue().length() ;
1079 }
1080
1081 void wxMacTextControl::Replace( long from , long to , const wxString &val )
1082 {
1083 SetSelection( from , to ) ;
1084 WriteText( val ) ;
1085 }
1086
1087 void wxMacTextControl::Remove( long from , long to )
1088 {
1089 SetSelection( from , to ) ;
1090 WriteText( wxEmptyString) ;
1091 }
1092
1093 void wxMacTextControl::Clear()
1094 {
1095 SetStringValue( wxEmptyString ) ;
1096 }
1097
1098 bool wxMacTextControl::CanUndo() const
1099 {
1100 return false ;
1101 }
1102
1103 void wxMacTextControl::Undo()
1104 {
1105 }
1106
1107 bool wxMacTextControl::CanRedo() const
1108 {
1109 return false ;
1110 }
1111
1112 void wxMacTextControl::Redo()
1113 {
1114 }
1115
1116 long wxMacTextControl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const
1117 {
1118 return 0 ;
1119 }
1120
1121 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos),
1122 long *WXUNUSED(x),
1123 long *WXUNUSED(y)) const
1124 {
1125 return false ;
1126 }
1127
1128 void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
1129 {
1130 }
1131
1132 int wxMacTextControl::GetNumberOfLines() const
1133 {
1134 ItemCount lines = 0 ;
1135 wxString content = GetStringValue() ;
1136 lines = 1;
1137
1138 for (size_t i = 0; i < content.length() ; i++)
1139 {
1140 if (content[i] == '\r')
1141 lines++;
1142 }
1143
1144 return lines ;
1145 }
1146
1147 wxString wxMacTextControl::GetLineText(long lineNo) const
1148 {
1149 // TODO: change this if possible to reflect real lines
1150 wxString content = GetStringValue() ;
1151
1152 // Find line first
1153 int count = 0;
1154 for (size_t i = 0; i < content.length() ; i++)
1155 {
1156 if (count == lineNo)
1157 {
1158 // Add chars in line then
1159 wxString tmp;
1160
1161 for (size_t j = i; j < content.length(); j++)
1162 {
1163 if (content[j] == '\n')
1164 return tmp;
1165
1166 tmp += content[j];
1167 }
1168
1169 return tmp;
1170 }
1171
1172 if (content[i] == '\n')
1173 count++;
1174 }
1175
1176 return wxEmptyString ;
1177 }
1178
1179 int wxMacTextControl::GetLineLength(long lineNo) const
1180 {
1181 // TODO: change this if possible to reflect real lines
1182 wxString content = GetStringValue() ;
1183
1184 // Find line first
1185 int count = 0;
1186 for (size_t i = 0; i < content.length() ; i++)
1187 {
1188 if (count == lineNo)
1189 {
1190 // Count chars in line then
1191 count = 0;
1192 for (size_t j = i; j < content.length(); j++)
1193 {
1194 count++;
1195 if (content[j] == '\n')
1196 return count;
1197 }
1198
1199 return count;
1200 }
1201
1202 if (content[i] == '\n')
1203 count++;
1204 }
1205
1206 return 0 ;
1207 }
1208
1209 // ----------------------------------------------------------------------------
1210 // standard unicode control implementation
1211 // ----------------------------------------------------------------------------
1212
1213 // the current unicode textcontrol implementation has a bug : only if the control
1214 // is currently having the focus, the selection can be retrieved by the corresponding
1215 // data tag. So we have a mirroring using a member variable
1216 // TODO : build event table using virtual member functions for wxMacControl
1217
1218 static const EventTypeSpec unicodeTextControlEventList[] =
1219 {
1220 { kEventClassControl , kEventControlSetFocusPart } ,
1221 } ;
1222
1223 static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1224 {
1225 OSStatus result = eventNotHandledErr ;
1226 wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ;
1227 wxMacCarbonEvent cEvent( event ) ;
1228
1229 switch ( GetEventKind( event ) )
1230 {
1231 case kEventControlSetFocusPart :
1232 {
1233 ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
1234 if ( controlPart == kControlFocusNoPart )
1235 {
1236 // about to loose focus -> store selection to field
1237 focus->GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1238 }
1239 result = CallNextEventHandler(handler,event) ;
1240 if ( controlPart != kControlFocusNoPart )
1241 {
1242 // about to gain focus -> set selection from field
1243 focus->SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
1244 }
1245 break;
1246 }
1247 default:
1248 break ;
1249 }
1250
1251 return result ;
1252 }
1253
1254 static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
1255 {
1256 OSStatus result = eventNotHandledErr ;
1257
1258 switch ( GetEventClass( event ) )
1259 {
1260 case kEventClassControl :
1261 result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ;
1262 break ;
1263
1264 default :
1265 break ;
1266 }
1267 return result ;
1268 }
1269
1270 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler )
1271
1272 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer ) : wxMacTextControl( wxPeer )
1273 {
1274 }
1275
1276 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
1277 const wxString& str,
1278 const wxPoint& pos,
1279 const wxSize& size, long style )
1280 : wxMacTextControl( wxPeer )
1281 {
1282 Create( wxPeer, str, pos, size, style );
1283 }
1284
1285 bool wxMacUnicodeTextControl::Create( wxTextCtrl *wxPeer,
1286 const wxString& str,
1287 const wxPoint& pos,
1288 const wxSize& size, long style )
1289 {
1290 m_font = wxPeer->GetFont() ;
1291 m_windowStyle = style ;
1292 m_selection.selStart = m_selection.selEnd = 0;
1293 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1294 wxString st = str ;
1295 wxMacConvertNewlines10To13( &st ) ;
1296 wxMacCFStringHolder cf(st , m_font.GetEncoding()) ;
1297 CFStringRef cfr = cf ;
1298
1299 m_valueTag = kControlEditTextCFStringTag ;
1300 CreateControl( wxPeer, &bounds, cfr );
1301
1302 if ( !(m_windowStyle & wxTE_MULTILINE) )
1303 SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
1304
1305 InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(),
1306 GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
1307 NULL);
1308
1309 return true;
1310 }
1311
1312 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1313 {
1314 }
1315
1316 void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
1317 {
1318 if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
1319 {
1320 // work around a refresh issue insofar as not always the entire content is shown,
1321 // even if this would be possible
1322 ControlEditTextSelectionRec sel ;
1323 CFStringRef value = NULL ;
1324
1325 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1326 verify_noerr( GetData<CFStringRef>( 0, m_valueTag, &value ) );
1327 verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
1328 verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1329
1330 CFRelease( value ) ;
1331 }
1332 }
1333
1334 wxString wxMacUnicodeTextControl::GetStringValue() const
1335 {
1336 wxString result ;
1337 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1338 if ( value )
1339 {
1340 wxMacCFStringHolder cf(value) ;
1341 result = cf.AsString() ;
1342 }
1343
1344 #if '\n' == 10
1345 wxMacConvertNewlines13To10( &result ) ;
1346 #else
1347 wxMacConvertNewlines10To13( &result ) ;
1348 #endif
1349
1350 return result ;
1351 }
1352
1353 void wxMacUnicodeTextControl::SetStringValue( const wxString &str )
1354 {
1355 wxString st = str ;
1356 wxMacConvertNewlines10To13( &st ) ;
1357 wxMacCFStringHolder cf( st , m_font.GetEncoding() ) ;
1358 verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
1359 }
1360
1361 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef cfr )
1362 {
1363 Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
1364 if ( isPassword )
1365 {
1366 m_valueTag = kControlEditTextPasswordCFStringTag ;
1367 }
1368 OSStatus err = CreateEditUnicodeTextControl(
1369 MAC_WXHWND(peer->MacGetTopLevelWindowRef()), bounds , cfr ,
1370 isPassword , NULL , &m_controlRef ) ;
1371 verify_noerr( err );
1372 }
1373
1374 void wxMacUnicodeTextControl::Copy()
1375 {
1376 SendHICommand( kHICommandCopy ) ;
1377 }
1378
1379 void wxMacUnicodeTextControl::Cut()
1380 {
1381 SendHICommand( kHICommandCut ) ;
1382 }
1383
1384 void wxMacUnicodeTextControl::Paste()
1385 {
1386 SendHICommand( kHICommandPaste ) ;
1387 }
1388
1389 bool wxMacUnicodeTextControl::CanPaste() const
1390 {
1391 return true ;
1392 }
1393
1394 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable))
1395 {
1396 #if 0 // leads to problem because text cannot be selected anymore
1397 SetData<Boolean>( kControlEditTextPart , kControlEditTextLockedTag , (Boolean) !editable ) ;
1398 #endif
1399 }
1400
1401 void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const
1402 {
1403 ControlEditTextSelectionRec sel ;
1404 if (HasFocus())
1405 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
1406 else
1407 sel = m_selection ;
1408
1409 if ( from )
1410 *from = sel.selStart ;
1411 if ( to )
1412 *to = sel.selEnd ;
1413 }
1414
1415 void wxMacUnicodeTextControl::SetSelection( long from , long to )
1416 {
1417 ControlEditTextSelectionRec sel ;
1418 wxString result ;
1419 int textLength = 0 ;
1420 CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
1421 if ( value )
1422 {
1423 wxMacCFStringHolder cf(value) ;
1424 textLength = cf.AsString().length() ;
1425 }
1426
1427 if ((from == -1) && (to == -1))
1428 {
1429 from = 0 ;
1430 to = textLength ;
1431 }
1432 else
1433 {
1434 from = wxMin(textLength,wxMax(from,0)) ;
1435 if ( to == -1 )
1436 to = textLength;
1437 else
1438 to = wxMax(0,wxMin(textLength,to)) ;
1439 }
1440
1441 sel.selStart = from ;
1442 sel.selEnd = to ;
1443 if ( HasFocus() )
1444 SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
1445 else
1446 m_selection = sel;
1447 }
1448
1449 void wxMacUnicodeTextControl::WriteText( const wxString& str )
1450 {
1451 wxString st = str ;
1452 wxMacConvertNewlines10To13( &st ) ;
1453
1454 if ( HasFocus() )
1455 {
1456 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
1457 CFStringRef value = cf ;
1458 SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
1459 }
1460 else
1461 {
1462 wxString val = GetStringValue() ;
1463 long start , end ;
1464 GetSelection( &start , &end ) ;
1465 val.Remove( start , end - start ) ;
1466 val.insert( start , str ) ;
1467 SetStringValue( val ) ;
1468 SetSelection( start + str.length() , start + str.length() ) ;
1469 }
1470 }
1471
1472 // ----------------------------------------------------------------------------
1473 // MLTE control implementation (common part)
1474 // ----------------------------------------------------------------------------
1475
1476 // if MTLE is read only, no changes at all are allowed, not even from
1477 // procedural API, in order to allow changes via API all the same we must undo
1478 // the readonly status while we are executing, this class helps to do so
1479
1480 class wxMacEditHelper
1481 {
1482 public :
1483 wxMacEditHelper( TXNObject txn )
1484 {
1485 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1486 m_txn = txn ;
1487 TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
1488 if ( m_data[0].uValue == kTXNReadOnly )
1489 {
1490 TXNControlData data[] = { { kTXNReadWrite } } ;
1491 TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
1492 }
1493 }
1494
1495 ~wxMacEditHelper()
1496 {
1497 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1498 if ( m_data[0].uValue == kTXNReadOnly )
1499 TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
1500 }
1501
1502 protected :
1503 TXNObject m_txn ;
1504 TXNControlData m_data[1] ;
1505 } ;
1506
1507 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer )
1508 : wxMacTextControl( peer )
1509 {
1510 SetNeedsFocusRect( true ) ;
1511 }
1512
1513 wxString wxMacMLTEControl::GetStringValue() const
1514 {
1515 wxString result ;
1516 OSStatus err ;
1517 Size actualSize = 0;
1518
1519 {
1520 #if wxUSE_UNICODE
1521 Handle theText ;
1522 err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNUnicodeTextData );
1523
1524 // all done
1525 if ( err != noErr )
1526 {
1527 actualSize = 0 ;
1528 }
1529 else
1530 {
1531 actualSize = GetHandleSize( theText ) / sizeof(UniChar) ;
1532 if ( actualSize > 0 )
1533 {
1534 wxChar *ptr = NULL ;
1535
1536 #if SIZEOF_WCHAR_T == 2
1537 ptr = new wxChar[actualSize + 1] ;
1538 wxStrncpy( ptr , (wxChar*)(*theText) , actualSize ) ;
1539 #else
1540 SetHandleSize( theText, (actualSize + 1) * sizeof(UniChar) ) ;
1541 HLock( theText ) ;
1542 (((UniChar*)*theText)[actualSize]) = 0 ;
1543 wxMBConvUTF16 converter ;
1544 size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
1545 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") );
1546 ptr = new wxChar[noChars + 1] ;
1547
1548 noChars = converter.MB2WC( ptr , (const char*)*theText , noChars + 1 ) ;
1549 wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") );
1550 ptr[noChars] = 0 ;
1551 HUnlock( theText ) ;
1552 #endif
1553
1554 ptr[actualSize] = 0 ;
1555 result = wxString( ptr ) ;
1556 delete [] ptr ;
1557 }
1558
1559 DisposeHandle( theText ) ;
1560 }
1561 #else
1562 Handle theText ;
1563 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1564
1565 // all done
1566 if ( err != noErr )
1567 {
1568 actualSize = 0 ;
1569 }
1570 else
1571 {
1572 actualSize = GetHandleSize( theText ) ;
1573 if ( actualSize > 0 )
1574 {
1575 HLock( theText ) ;
1576 result = wxString( *theText , wxConvLocal , actualSize ) ;
1577 HUnlock( theText ) ;
1578 }
1579
1580 DisposeHandle( theText ) ;
1581 }
1582 #endif
1583 }
1584
1585 #if '\n' == 10
1586 wxMacConvertNewlines13To10( &result ) ;
1587 #else
1588 wxMacConvertNewlines10To13( &result ) ;
1589 #endif
1590
1591 return result ;
1592 }
1593
1594 void wxMacMLTEControl::SetStringValue( const wxString &str )
1595 {
1596 wxString st = str;
1597 wxMacConvertNewlines10To13( &st );
1598
1599 {
1600 #ifndef __LP64__
1601 wxMacWindowClipper c( m_peer ) ;
1602 #endif
1603
1604 {
1605 wxMacEditHelper help( m_txn );
1606 SetTXNData( st, kTXNStartOffset, kTXNEndOffset );
1607 }
1608
1609 TXNSetSelection( m_txn, 0, 0 );
1610 TXNShowSelection( m_txn, kTXNShowStart );
1611 }
1612 }
1613
1614 TXNFrameOptions wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle )
1615 {
1616 TXNFrameOptions frameOptions = kTXNDontDrawCaretWhenInactiveMask;
1617
1618 frameOptions |= kTXNDoFontSubstitutionMask;
1619
1620 if ( ! (wxStyle & wxTE_NOHIDESEL) )
1621 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
1622
1623 if ( wxStyle & (wxHSCROLL | wxTE_DONTWRAP) )
1624 frameOptions |= kTXNWantHScrollBarMask ;
1625
1626 if ( wxStyle & wxTE_MULTILINE )
1627 {
1628 if ( ! (wxStyle & wxTE_DONTWRAP ) )
1629 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
1630
1631 if ( !(wxStyle & wxTE_NO_VSCROLL) )
1632 {
1633 frameOptions |= kTXNWantVScrollBarMask ;
1634
1635 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1636 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1637 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1638 // if ( frameOptions & kTXNWantHScrollBarMask )
1639 // frameOptions |= kTXNDrawGrowIconMask ;
1640 }
1641 }
1642 else
1643 {
1644 frameOptions |= kTXNSingleLineOnlyMask ;
1645 }
1646
1647 return frameOptions ;
1648 }
1649
1650 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour &background,
1651 bool WXUNUSED(visible))
1652 {
1653 TXNControlTag iControlTags[] =
1654 {
1655 kTXNDoFontSubstitution,
1656 kTXNWordWrapStateTag ,
1657 };
1658 TXNControlData iControlData[] =
1659 {
1660 { true },
1661 { kTXNNoAutoWrap },
1662 };
1663
1664 int toptag = WXSIZEOF( iControlTags ) ;
1665
1666 if ( m_windowStyle & wxTE_MULTILINE )
1667 {
1668 iControlData[1].uValue =
1669 (m_windowStyle & wxTE_DONTWRAP)
1670 ? kTXNNoAutoWrap
1671 : kTXNAutoWrap;
1672 }
1673
1674 OSStatus err = TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) ;
1675 verify_noerr( err );
1676
1677 // setting the default font:
1678 // under 10.2 this causes a visible caret, therefore we avoid it
1679
1680 Str255 fontName ;
1681 SInt16 fontSize ;
1682 Style fontStyle ;
1683
1684 GetThemeFont( kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1685
1686 TXNTypeAttributes typeAttr[] =
1687 {
1688 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
1689 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
1690 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
1691 } ;
1692
1693 err = TXNSetTypeAttributes(
1694 m_txn, sizeof(typeAttr) / sizeof(TXNTypeAttributes),
1695 typeAttr, kTXNStartOffset, kTXNEndOffset );
1696 verify_noerr( err );
1697
1698 if ( m_windowStyle & wxTE_PASSWORD )
1699 {
1700 UniChar c = 0x00A5 ;
1701 err = TXNEchoMode( m_txn , c , 0 , true );
1702 verify_noerr( err );
1703 }
1704
1705 TXNBackground tback;
1706 tback.bgType = kTXNBackgroundTypeRGB;
1707 background.GetRGBColor( &tback.bg.color );
1708 TXNSetBackground( m_txn , &tback );
1709
1710
1711 TXNCommandEventSupportOptions options ;
1712 if ( TXNGetCommandEventSupport( m_txn, &options ) == noErr )
1713 {
1714 options |=
1715 kTXNSupportEditCommandProcessing
1716 | kTXNSupportEditCommandUpdating
1717 | kTXNSupportFontCommandProcessing
1718 | kTXNSupportFontCommandUpdating;
1719
1720 // only spell check when not read-only
1721 // use system options for the default
1722 bool checkSpelling = false ;
1723 if ( !(m_windowStyle & wxTE_READONLY) )
1724 {
1725 #if wxUSE_SYSTEM_OPTIONS
1726 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER ) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER ) == 1) )
1727 {
1728 checkSpelling = true ;
1729 }
1730 #endif
1731 }
1732
1733 if ( checkSpelling )
1734 options |=
1735 kTXNSupportSpellCheckCommandProcessing
1736 | kTXNSupportSpellCheckCommandUpdating;
1737
1738 TXNSetCommandEventSupport( m_txn , options ) ;
1739 }
1740 }
1741
1742 void wxMacMLTEControl::SetBackgroundColour(const wxColour& col )
1743 {
1744 TXNBackground tback;
1745 tback.bgType = kTXNBackgroundTypeRGB;
1746 col.GetRGBColor(&tback.bg.color);
1747 TXNSetBackground( m_txn , &tback );
1748 }
1749
1750 static inline int wxConvertToTXN(int x)
1751 {
1752 return wx_static_cast(int, x / 254.0 * 72 + 0.5);
1753 }
1754
1755 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
1756 {
1757 TXNTypeAttributes typeAttr[4] ;
1758 RGBColor color ;
1759 size_t typeAttrCount = 0 ;
1760
1761 TXNMargins margins;
1762 TXNControlTag controlTags[4];
1763 TXNControlData controlData[4];
1764 size_t controlAttrCount = 0;
1765
1766 TXNTab* tabs = NULL;
1767
1768 bool relayout = false;
1769
1770 if ( style.HasFont() )
1771 {
1772 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
1773 const wxFont &font = style.GetFont() ;
1774 typeAttr[typeAttrCount].tag = kTXNATSUIStyle ;
1775 typeAttr[typeAttrCount].size = kTXNATSUIStyleSize ;
1776 typeAttr[typeAttrCount].data.dataPtr = font.MacGetATSUStyle() ;
1777 typeAttrCount++ ;
1778 }
1779
1780 if ( style.HasTextColour() )
1781 {
1782 wxASSERT( typeAttrCount < WXSIZEOF(typeAttr) );
1783 style.GetTextColour().GetRGBColor( &color );
1784 typeAttr[typeAttrCount].tag = kTXNQDFontColorAttribute ;
1785 typeAttr[typeAttrCount].size = kTXNQDFontColorAttributeSize ;
1786 typeAttr[typeAttrCount].data.dataPtr = (void*) &color ;
1787 typeAttrCount++ ;
1788 }
1789
1790 if ( style.HasAlignment() )
1791 {
1792 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1793 SInt32 align;
1794
1795 switch ( style.GetAlignment() )
1796 {
1797 case wxTEXT_ALIGNMENT_LEFT:
1798 align = kTXNFlushLeft;
1799 break;
1800 case wxTEXT_ALIGNMENT_CENTRE:
1801 align = kTXNCenter;
1802 break;
1803 case wxTEXT_ALIGNMENT_RIGHT:
1804 align = kTXNFlushRight;
1805 break;
1806 case wxTEXT_ALIGNMENT_JUSTIFIED:
1807 align = kTXNFullJust;
1808 break;
1809 default :
1810 case wxTEXT_ALIGNMENT_DEFAULT:
1811 align = kTXNFlushDefault;
1812 break;
1813 }
1814
1815 controlTags[controlAttrCount] = kTXNJustificationTag ;
1816 controlData[controlAttrCount].sValue = align ;
1817 controlAttrCount++ ;
1818 }
1819
1820 if ( style.HasLeftIndent() || style.HasRightIndent() )
1821 {
1822 wxASSERT( controlAttrCount < WXSIZEOF(controlTags) );
1823 controlTags[controlAttrCount] = kTXNMarginsTag;
1824 controlData[controlAttrCount].marginsPtr = &margins;
1825 verify_noerr( TXNGetTXNObjectControls (m_txn, 1 ,
1826 &controlTags[controlAttrCount], &controlData[controlAttrCount]) );
1827 if ( style.HasLeftIndent() )
1828 {
1829 margins.leftMargin = wxConvertToTXN(style.GetLeftIndent());
1830 }
1831 if ( style.HasRightIndent() )
1832 {
1833 margins.rightMargin = wxConvertToTXN(style.GetRightIndent());
1834 }
1835 controlAttrCount++ ;
1836 }
1837
1838 if ( style.HasTabs() )
1839 {
1840 const wxArrayInt& tabarray = style.GetTabs();
1841 // unfortunately Mac only applies a tab distance, not individually different tabs
1842 controlTags[controlAttrCount] = kTXNTabSettingsTag;
1843 if ( tabarray.size() > 0 )
1844 controlData[controlAttrCount].tabValue.value = wxConvertToTXN(tabarray[0]);
1845 else
1846 controlData[controlAttrCount].tabValue.value = 72 ;
1847
1848 controlData[controlAttrCount].tabValue.tabType = kTXNLeftTab;
1849 controlAttrCount++ ;
1850 }
1851
1852 // unfortunately the relayout is not automatic
1853 if ( controlAttrCount > 0 )
1854 {
1855 verify_noerr( TXNSetTXNObjectControls (m_txn, false /* don't clear all */, controlAttrCount,
1856 controlTags, controlData) );
1857 relayout = true;
1858 }
1859
1860 if ( typeAttrCount > 0 )
1861 {
1862 verify_noerr( TXNSetTypeAttributes( m_txn , typeAttrCount, typeAttr, from , to ) );
1863 relayout = true;
1864 }
1865
1866 if ( tabs != NULL )
1867 {
1868 delete[] tabs;
1869 }
1870
1871 if ( relayout )
1872 {
1873 TXNRecalcTextLayout( m_txn );
1874 }
1875 }
1876
1877 void wxMacMLTEControl::SetFont(const wxFont & font,
1878 const wxColour& foreground,
1879 long WXUNUSED(windowStyle))
1880 {
1881 wxMacEditHelper help( m_txn ) ;
1882 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1883 }
1884
1885 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1886 {
1887 wxMacEditHelper help( m_txn ) ;
1888 TXNSetAttribute( style, start, end ) ;
1889 }
1890
1891 void wxMacMLTEControl::Copy()
1892 {
1893 TXNCopy( m_txn );
1894 }
1895
1896 void wxMacMLTEControl::Cut()
1897 {
1898 TXNCut( m_txn );
1899 }
1900
1901 void wxMacMLTEControl::Paste()
1902 {
1903 TXNPaste( m_txn );
1904 }
1905
1906 bool wxMacMLTEControl::CanPaste() const
1907 {
1908 return TXNIsScrapPastable() ;
1909 }
1910
1911 void wxMacMLTEControl::SetEditable(bool editable)
1912 {
1913 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1914 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1915 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1916 }
1917
1918 wxTextPos wxMacMLTEControl::GetLastPosition() const
1919 {
1920 wxTextPos actualsize = 0 ;
1921
1922 Handle theText ;
1923 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1924
1925 // all done
1926 if ( err == noErr )
1927 {
1928 actualsize = GetHandleSize( theText ) ;
1929 DisposeHandle( theText ) ;
1930 }
1931 else
1932 {
1933 actualsize = 0 ;
1934 }
1935
1936 return actualsize ;
1937 }
1938
1939 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1940 {
1941 wxString value = str ;
1942 wxMacConvertNewlines10To13( &value ) ;
1943
1944 wxMacEditHelper help( m_txn ) ;
1945 #ifndef __LP64__
1946 wxMacWindowClipper c( m_peer ) ;
1947 #endif
1948
1949 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to ) ;
1950 TXNClear( m_txn ) ;
1951 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1952 }
1953
1954 void wxMacMLTEControl::Remove( long from , long to )
1955 {
1956 #ifndef __LP64__
1957 wxMacWindowClipper c( m_peer ) ;
1958 #endif
1959 wxMacEditHelper help( m_txn ) ;
1960 TXNSetSelection( m_txn , from , to ) ;
1961 TXNClear( m_txn ) ;
1962 }
1963
1964 void wxMacMLTEControl::GetSelection( long* from, long* to) const
1965 {
1966 TXNOffset f,t ;
1967 TXNGetSelection( m_txn , &f , &t ) ;
1968 *from = f;
1969 *to = t;
1970 }
1971
1972 void wxMacMLTEControl::SetSelection( long from , long to )
1973 {
1974 #ifndef __LP64__
1975 wxMacWindowClipper c( m_peer ) ;
1976 #endif
1977
1978 // change the selection
1979 if ((from == -1) && (to == -1))
1980 TXNSelectAll( m_txn );
1981 else
1982 TXNSetSelection( m_txn, from, to == -1 ? kTXNEndOffset : to );
1983
1984 TXNShowSelection( m_txn, kTXNShowStart );
1985 }
1986
1987 void wxMacMLTEControl::WriteText( const wxString& str )
1988 {
1989 wxString st = str ;
1990 wxMacConvertNewlines10To13( &st ) ;
1991
1992 long start , end , dummy ;
1993
1994 GetSelection( &start , &dummy ) ;
1995 #ifndef __LP64__
1996 wxMacWindowClipper c( m_peer ) ;
1997 #endif
1998
1999 {
2000 wxMacEditHelper helper( m_txn ) ;
2001 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
2002 }
2003
2004 GetSelection( &dummy, &end ) ;
2005
2006 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2007 }
2008
2009 void wxMacMLTEControl::Clear()
2010 {
2011 #ifndef __LP64__
2012 wxMacWindowClipper c( m_peer ) ;
2013 #endif
2014 wxMacEditHelper st( m_txn ) ;
2015 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
2016 TXNClear( m_txn ) ;
2017 }
2018
2019 bool wxMacMLTEControl::CanUndo() const
2020 {
2021 return TXNCanUndo( m_txn , NULL ) ;
2022 }
2023
2024 void wxMacMLTEControl::Undo()
2025 {
2026 TXNUndo( m_txn ) ;
2027 }
2028
2029 bool wxMacMLTEControl::CanRedo() const
2030 {
2031 return TXNCanRedo( m_txn , NULL ) ;
2032 }
2033
2034 void wxMacMLTEControl::Redo()
2035 {
2036 TXNRedo( m_txn ) ;
2037 }
2038
2039 int wxMacMLTEControl::GetNumberOfLines() const
2040 {
2041 ItemCount lines = 0 ;
2042 TXNGetLineCount( m_txn, &lines ) ;
2043
2044 return lines ;
2045 }
2046
2047 long wxMacMLTEControl::XYToPosition(long x, long y) const
2048 {
2049 Point curpt ;
2050 wxTextPos lastpos ;
2051
2052 // TODO: find a better implementation : while we can get the
2053 // line metrics of a certain line, we don't get its starting
2054 // position, so it would probably be rather a binary search
2055 // for the start position
2056 long xpos = 0, ypos = 0 ;
2057 int lastHeight = 0 ;
2058 ItemCount n ;
2059
2060 lastpos = GetLastPosition() ;
2061 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
2062 {
2063 if ( y == ypos && x == xpos )
2064 return n ;
2065
2066 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2067
2068 if ( curpt.v > lastHeight )
2069 {
2070 xpos = 0 ;
2071 if ( n > 0 )
2072 ++ypos ;
2073
2074 lastHeight = curpt.v ;
2075 }
2076 else
2077 ++xpos ;
2078 }
2079
2080 return 0 ;
2081 }
2082
2083 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
2084 {
2085 Point curpt ;
2086 wxTextPos lastpos ;
2087
2088 if ( y )
2089 *y = 0 ;
2090 if ( x )
2091 *x = 0 ;
2092
2093 lastpos = GetLastPosition() ;
2094 if ( pos <= lastpos )
2095 {
2096 // TODO: find a better implementation - while we can get the
2097 // line metrics of a certain line, we don't get its starting
2098 // position, so it would probably be rather a binary search
2099 // for the start position
2100 long xpos = 0, ypos = 0 ;
2101 int lastHeight = 0 ;
2102 ItemCount n ;
2103
2104 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
2105 {
2106 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2107
2108 if ( curpt.v > lastHeight )
2109 {
2110 xpos = 0 ;
2111 if ( n > 0 )
2112 ++ypos ;
2113
2114 lastHeight = curpt.v ;
2115 }
2116 else
2117 ++xpos ;
2118 }
2119
2120 if ( y )
2121 *y = ypos ;
2122 if ( x )
2123 *x = xpos ;
2124 }
2125
2126 return false ;
2127 }
2128
2129 void wxMacMLTEControl::ShowPosition( long pos )
2130 {
2131 Point current, desired ;
2132 TXNOffset selstart, selend;
2133
2134 TXNGetSelection( m_txn, &selstart, &selend );
2135 TXNOffsetToPoint( m_txn, selstart, &current );
2136 TXNOffsetToPoint( m_txn, pos, &desired );
2137
2138 // TODO: use HIPoints for 10.3 and above
2139
2140 OSErr theErr = noErr;
2141 long dv = desired.v - current.v;
2142 long dh = desired.h - current.h;
2143 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2144 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
2145
2146 // there will be an error returned for classic MLTE implementation when the control is
2147 // invisible, but HITextView works correctly, so we don't assert that one
2148 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2149 }
2150
2151 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
2152 {
2153 #if wxUSE_UNICODE
2154 #if SIZEOF_WCHAR_T == 2
2155 size_t len = st.length() ;
2156 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
2157 #else
2158 wxMBConvUTF16 converter ;
2159 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
2160 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
2161 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
2162 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
2163 free( unibuf ) ;
2164 #endif
2165 #else
2166 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
2167 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
2168 #endif
2169 }
2170
2171 wxString wxMacMLTEControl::GetLineText(long lineNo) const
2172 {
2173 wxString line ;
2174
2175 if ( lineNo < GetNumberOfLines() )
2176 {
2177 Point firstPoint;
2178 Fixed lineWidth, lineHeight, currentHeight;
2179 long ypos ;
2180
2181 // get the first possible position in the control
2182 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2183
2184 // Iterate through the lines until we reach the one we want,
2185 // adding to our current y pixel point position
2186 ypos = 0 ;
2187 currentHeight = 0;
2188 while (ypos < lineNo)
2189 {
2190 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2191 currentHeight += lineHeight;
2192 }
2193
2194 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2195 TXNOffset theOffset;
2196 TXNPointToOffset(m_txn, thePoint, &theOffset);
2197
2198 wxString content = GetStringValue() ;
2199 Point currentPoint = thePoint;
2200 while (thePoint.v == currentPoint.v && theOffset < content.length())
2201 {
2202 line += content[theOffset];
2203 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2204 }
2205 }
2206
2207 return line ;
2208 }
2209
2210 int wxMacMLTEControl::GetLineLength(long lineNo) const
2211 {
2212 int theLength = 0;
2213
2214 if ( lineNo < GetNumberOfLines() )
2215 {
2216 Point firstPoint;
2217 Fixed lineWidth, lineHeight, currentHeight;
2218 long ypos;
2219
2220 // get the first possible position in the control
2221 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2222
2223 // Iterate through the lines until we reach the one we want,
2224 // adding to our current y pixel point position
2225 ypos = 0;
2226 currentHeight = 0;
2227 while (ypos < lineNo)
2228 {
2229 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2230 currentHeight += lineHeight;
2231 }
2232
2233 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2234 TXNOffset theOffset;
2235 TXNPointToOffset(m_txn, thePoint, &theOffset);
2236
2237 wxString content = GetStringValue() ;
2238 Point currentPoint = thePoint;
2239 while (thePoint.v == currentPoint.v && theOffset < content.length())
2240 {
2241 ++theLength;
2242 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2243 }
2244 }
2245
2246 return theLength ;
2247 }
2248
2249 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2250
2251 // ----------------------------------------------------------------------------
2252 // MLTE control implementation (classic part)
2253 // ----------------------------------------------------------------------------
2254
2255 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2256 // has to live on. We have different problems coming from outdated implementations on the
2257 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2258 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2259 // no way out, therefore we are using our own implementation and our own scrollbars ....
2260
2261 wxMacPortSaver::wxMacPortSaver( GrafPtr port )
2262 {
2263 ::GetPort( &m_port );
2264 ::SetPort( port );
2265 }
2266
2267 wxMacPortSaver::~wxMacPortSaver()
2268 {
2269 ::SetPort( m_port );
2270 }
2271
2272 wxMacWindowClipper::wxMacWindowClipper( const wxWindow* win ) :
2273 wxMacPortSaver( (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) )
2274 {
2275 m_newPort = (GrafPtr) GetWindowPort( (WindowRef) win->MacGetTopLevelWindowRef() ) ;
2276 m_formerClip = NewRgn() ;
2277 m_newClip = NewRgn() ;
2278 GetClip( m_formerClip ) ;
2279
2280 if ( win )
2281 {
2282 // guard against half constructed objects, this just leads to a empty clip
2283 if ( win->GetPeer() )
2284 {
2285 int x = 0 , y = 0;
2286 win->MacWindowToRootWindow( &x, &y ) ;
2287
2288 // get area including focus rect
2289 HIShapeGetAsQDRgn( ((wxWindow*)win)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip );
2290 if ( !EmptyRgn( m_newClip ) )
2291 OffsetRgn( m_newClip , x , y ) ;
2292 }
2293
2294 SetClip( m_newClip ) ;
2295 }
2296 }
2297
2298 wxMacWindowClipper::~wxMacWindowClipper()
2299 {
2300 SetPort( m_newPort ) ;
2301 SetClip( m_formerClip ) ;
2302 DisposeRgn( m_newClip ) ;
2303 DisposeRgn( m_formerClip ) ;
2304 }
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