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