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