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