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