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