]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/textctrl.cpp
renamed RGBColor setter to avoid former overload being an override
[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 #endif
21
22 #ifdef __DARWIN__
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #else
26 #include <stat.h>
27 #endif
28
29 #include "wx/msgdlg.h"
30
31 #if wxUSE_STD_IOSTREAM
32 #if wxUSE_IOSTREAMH
33 #include <fstream.h>
34 #else
35 #include <fstream>
36 #endif
37 #endif
38
39 #include "wx/app.h"
40 #include "wx/dc.h"
41 #include "wx/button.h"
42 #include "wx/toplevel.h"
43 #include "wx/settings.h"
44 #include "wx/filefn.h"
45 #include "wx/utils.h"
46 #include "wx/sysopt.h"
47 #include "wx/menu.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 wxMacStringToPascal( font.GetFaceName() , fontName ) ;
1789 fontSize = font.GetPointSize() ;
1790 if ( font.GetUnderlined() )
1791 fontStyle |= underline ;
1792 if ( font.GetWeight() == wxBOLD )
1793 fontStyle |= bold ;
1794 if ( font.GetStyle() == wxITALIC )
1795 fontStyle |= italic ;
1796
1797 typeAttr[attrCount].tag = kTXNQDFontNameAttribute ;
1798 typeAttr[attrCount].size = kTXNQDFontNameAttributeSize ;
1799 typeAttr[attrCount].data.dataPtr = (void*)fontName ;
1800 attrCount++ ;
1801
1802 typeAttr[attrCount].tag = kTXNQDFontSizeAttribute ;
1803 typeAttr[attrCount].size = kTXNFontSizeAttributeSize ;
1804 typeAttr[attrCount].data.dataValue = (fontSize << 16) ;
1805 attrCount++ ;
1806
1807 typeAttr[attrCount].tag = kTXNQDFontStyleAttribute ;
1808 typeAttr[attrCount].size = kTXNQDFontStyleAttributeSize ;
1809 typeAttr[attrCount].data.dataValue = fontStyle ;
1810 attrCount++ ;
1811 }
1812
1813 if ( style.HasTextColour() )
1814 {
1815 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
1816
1817 typeAttr[attrCount].tag = kTXNQDFontColorAttribute ;
1818 typeAttr[attrCount].size = kTXNQDFontColorAttributeSize ;
1819 typeAttr[attrCount].data.dataPtr = (void*) &color ;
1820 attrCount++ ;
1821 }
1822
1823 if ( attrCount > 0 )
1824 {
1825 verify_noerr( TXNSetTypeAttributes( m_txn , attrCount , typeAttr, from , to ) );
1826 }
1827 }
1828
1829 void wxMacMLTEControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
1830 {
1831 wxMacEditHelper help( m_txn ) ;
1832 TXNSetAttribute( wxTextAttr( foreground, wxNullColour, font ), kTXNStartOffset, kTXNEndOffset ) ;
1833 }
1834
1835 void wxMacMLTEControl::SetStyle( long start, long end, const wxTextAttr& style )
1836 {
1837 wxMacEditHelper help( m_txn ) ;
1838 TXNSetAttribute( style, start, end ) ;
1839 }
1840
1841 void wxMacMLTEControl::Copy()
1842 {
1843 ClearCurrentScrap();
1844 TXNCopy( m_txn );
1845 TXNConvertToPublicScrap();
1846 }
1847
1848 void wxMacMLTEControl::Cut()
1849 {
1850 ClearCurrentScrap();
1851 TXNCut( m_txn );
1852 TXNConvertToPublicScrap();
1853 }
1854
1855 void wxMacMLTEControl::Paste()
1856 {
1857 TXNConvertFromPublicScrap();
1858 TXNPaste( m_txn );
1859 }
1860
1861 bool wxMacMLTEControl::CanPaste() const
1862 {
1863 return TXNIsScrapPastable() ;
1864 }
1865
1866 void wxMacMLTEControl::SetEditable(bool editable)
1867 {
1868 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1869 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1870 TXNSetTXNObjectControls( m_txn, false, WXSIZEOF(tag), tag, data ) ;
1871 }
1872
1873 wxTextPos wxMacMLTEControl::GetLastPosition() const
1874 {
1875 wxTextPos actualsize = 0 ;
1876
1877 Handle theText ;
1878 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText, kTXNTextData );
1879
1880 // all done
1881 if ( err == noErr )
1882 {
1883 actualsize = GetHandleSize( theText ) ;
1884 DisposeHandle( theText ) ;
1885 }
1886 else
1887 {
1888 actualsize = 0 ;
1889 }
1890
1891 return actualsize ;
1892 }
1893
1894 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1895 {
1896 wxString value = str ;
1897 wxMacConvertNewlines10To13( &value ) ;
1898
1899 wxMacEditHelper help( m_txn ) ;
1900 wxMacWindowClipper c( m_peer ) ;
1901
1902 TXNSetSelection( m_txn, from, to ) ;
1903 TXNClear( m_txn ) ;
1904 SetTXNData( value, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1905 }
1906
1907 void wxMacMLTEControl::Remove( long from , long to )
1908 {
1909 wxMacWindowClipper c( m_peer ) ;
1910 wxMacEditHelper help( m_txn ) ;
1911 TXNSetSelection( m_txn , from , to ) ;
1912 TXNClear( m_txn ) ;
1913 }
1914
1915 void wxMacMLTEControl::GetSelection( long* from, long* to) const
1916 {
1917 TXNGetSelection( m_txn , (TXNOffset*) from , (TXNOffset*) to ) ;
1918 }
1919
1920 void wxMacMLTEControl::SetSelection( long from , long to )
1921 {
1922 wxMacWindowClipper c( m_peer ) ;
1923
1924 // change the selection
1925 if ((from == -1) && (to == -1))
1926 TXNSelectAll( m_txn );
1927 else
1928 TXNSetSelection( m_txn, from, to );
1929
1930 TXNShowSelection( m_txn, kTXNShowStart );
1931 }
1932
1933 void wxMacMLTEControl::WriteText( const wxString& str )
1934 {
1935 wxString st = str ;
1936 wxMacConvertNewlines10To13( &st ) ;
1937
1938 long start , end , dummy ;
1939
1940 GetSelection( &start , &dummy ) ;
1941 wxMacWindowClipper c( m_peer ) ;
1942
1943 {
1944 wxMacEditHelper helper( m_txn ) ;
1945 SetTXNData( st, kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1946 }
1947
1948 GetSelection( &dummy, &end ) ;
1949
1950 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
1951 }
1952
1953 void wxMacMLTEControl::Clear()
1954 {
1955 wxMacWindowClipper c( m_peer ) ;
1956 wxMacEditHelper st( m_txn ) ;
1957 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
1958 TXNClear( m_txn ) ;
1959 }
1960
1961 bool wxMacMLTEControl::CanUndo() const
1962 {
1963 return TXNCanUndo( m_txn , NULL ) ;
1964 }
1965
1966 void wxMacMLTEControl::Undo()
1967 {
1968 TXNUndo( m_txn ) ;
1969 }
1970
1971 bool wxMacMLTEControl::CanRedo() const
1972 {
1973 return TXNCanRedo( m_txn , NULL ) ;
1974 }
1975
1976 void wxMacMLTEControl::Redo()
1977 {
1978 TXNRedo( m_txn ) ;
1979 }
1980
1981 int wxMacMLTEControl::GetNumberOfLines() const
1982 {
1983 ItemCount lines = 0 ;
1984 TXNGetLineCount( m_txn, &lines ) ;
1985
1986 return lines ;
1987 }
1988
1989 long wxMacMLTEControl::XYToPosition(long x, long y) const
1990 {
1991 Point curpt ;
1992 wxTextPos lastpos ;
1993
1994 // TODO: find a better implementation : while we can get the
1995 // line metrics of a certain line, we don't get its starting
1996 // position, so it would probably be rather a binary search
1997 // for the start position
1998 long xpos = 0, ypos = 0 ;
1999 int lastHeight = 0 ;
2000 ItemCount n ;
2001
2002 lastpos = GetLastPosition() ;
2003 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
2004 {
2005 if ( y == ypos && x == xpos )
2006 return n ;
2007
2008 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2009
2010 if ( curpt.v > lastHeight )
2011 {
2012 xpos = 0 ;
2013 if ( n > 0 )
2014 ++ypos ;
2015
2016 lastHeight = curpt.v ;
2017 }
2018 else
2019 ++xpos ;
2020 }
2021
2022 return 0 ;
2023 }
2024
2025 bool wxMacMLTEControl::PositionToXY( long pos, long *x, long *y ) const
2026 {
2027 Point curpt ;
2028 wxTextPos lastpos ;
2029
2030 if ( y )
2031 *y = 0 ;
2032 if ( x )
2033 *x = 0 ;
2034
2035 lastpos = GetLastPosition() ;
2036 if ( pos <= lastpos )
2037 {
2038 // TODO: find a better implementation - while we can get the
2039 // line metrics of a certain line, we don't get its starting
2040 // position, so it would probably be rather a binary search
2041 // for the start position
2042 long xpos = 0, ypos = 0 ;
2043 int lastHeight = 0 ;
2044 ItemCount n ;
2045
2046 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
2047 {
2048 TXNOffsetToPoint( m_txn, n, &curpt ) ;
2049
2050 if ( curpt.v > lastHeight )
2051 {
2052 xpos = 0 ;
2053 if ( n > 0 )
2054 ++ypos ;
2055
2056 lastHeight = curpt.v ;
2057 }
2058 else
2059 ++xpos ;
2060 }
2061
2062 if ( y )
2063 *y = ypos ;
2064 if ( x )
2065 *x = xpos ;
2066 }
2067
2068 return false ;
2069 }
2070
2071 void wxMacMLTEControl::ShowPosition( long pos )
2072 {
2073 #if TARGET_RT_MAC_MACHO && defined(AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER)
2074 {
2075 Point current, desired ;
2076 TXNOffset selstart, selend;
2077
2078 TXNGetSelection( m_txn, &selstart, &selend );
2079 TXNOffsetToPoint( m_txn, selstart, &current );
2080 TXNOffsetToPoint( m_txn, pos, &desired );
2081
2082 // TODO: use HIPoints for 10.3 and above
2083 if ( (UInt32)TXNScroll != (UInt32)kUnresolvedCFragSymbolAddress )
2084 {
2085 OSErr theErr = noErr;
2086 SInt32 dv = desired.v - current.v;
2087 SInt32 dh = desired.h - current.h;
2088 TXNShowSelection( m_txn, kTXNShowStart ) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2089 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels, &dv, &dh );
2090
2091 // there will be an error returned for classic MLTE implementation when the control is
2092 // invisible, but HITextView works correctly, so we don't assert that one
2093 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2094 }
2095 }
2096 #endif
2097 }
2098
2099 void wxMacMLTEControl::SetTXNData( const wxString& st, TXNOffset start, TXNOffset end )
2100 {
2101 #if wxUSE_UNICODE
2102 #if SIZEOF_WCHAR_T == 2
2103 size_t len = st.Len() ;
2104 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end );
2105 #else
2106 wxMBConvUTF16 converter ;
2107 ByteCount byteBufferLen = converter.WC2MB( NULL, st.wc_str(), 0 ) ;
2108 UniChar *unibuf = (UniChar*)malloc( byteBufferLen ) ;
2109 converter.WC2MB( (char*)unibuf, st.wc_str(), byteBufferLen ) ;
2110 TXNSetData( m_txn, kTXNUnicodeTextData, (void*)unibuf, byteBufferLen, start, end ) ;
2111 free( unibuf ) ;
2112 #endif
2113 #else
2114 wxCharBuffer text = st.mb_str( wxConvLocal ) ;
2115 TXNSetData( m_txn, kTXNTextData, (void*)text.data(), strlen( text ), start, end ) ;
2116 #endif
2117 }
2118
2119 wxString wxMacMLTEControl::GetLineText(long lineNo) const
2120 {
2121 wxString line ;
2122
2123 if ( lineNo < GetNumberOfLines() )
2124 {
2125 Point firstPoint;
2126 Fixed lineWidth, lineHeight, currentHeight;
2127 long ypos ;
2128
2129 // get the first possible position in the control
2130 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2131
2132 // Iterate through the lines until we reach the one we want,
2133 // adding to our current y pixel point position
2134 ypos = 0 ;
2135 currentHeight = 0;
2136 while (ypos < lineNo)
2137 {
2138 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2139 currentHeight += lineHeight;
2140 }
2141
2142 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2143 TXNOffset theOffset;
2144 TXNPointToOffset(m_txn, thePoint, &theOffset);
2145
2146 wxString content = GetStringValue() ;
2147 Point currentPoint = thePoint;
2148 while (thePoint.v == currentPoint.v && theOffset < content.length())
2149 {
2150 line += content[theOffset];
2151 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2152 }
2153 }
2154
2155 return line ;
2156 }
2157
2158 int wxMacMLTEControl::GetLineLength(long lineNo) const
2159 {
2160 int theLength = 0;
2161
2162 if ( lineNo < GetNumberOfLines() )
2163 {
2164 Point firstPoint;
2165 Fixed lineWidth, lineHeight, currentHeight;
2166 long ypos;
2167
2168 // get the first possible position in the control
2169 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2170
2171 // Iterate through the lines until we reach the one we want,
2172 // adding to our current y pixel point position
2173 ypos = 0;
2174 currentHeight = 0;
2175 while (ypos < lineNo)
2176 {
2177 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2178 currentHeight += lineHeight;
2179 }
2180
2181 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2182 TXNOffset theOffset;
2183 TXNPointToOffset(m_txn, thePoint, &theOffset);
2184
2185 wxString content = GetStringValue() ;
2186 Point currentPoint = thePoint;
2187 while (thePoint.v == currentPoint.v && theOffset < content.length())
2188 {
2189 ++theLength;
2190 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2191 }
2192 }
2193
2194 return theLength ;
2195 }
2196
2197 // ----------------------------------------------------------------------------
2198 // MLTE control implementation (classic part)
2199 // ----------------------------------------------------------------------------
2200
2201 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2202 // has to live on. We have different problems coming from outdated implementations on the
2203 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2204 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2205 // no way out, therefore we are using our own implementation and our own scrollbars ....
2206
2207 #ifdef __WXMAC_OSX__
2208
2209 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
2210 ControlActionUPP gTXNScrollActionProc = NULL ;
2211
2212 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2213 SInt32 iValue, SInt32 iMaximumValue,
2214 TXNScrollBarOrientation iScrollBarOrientation, SInt32 iRefCon )
2215 {
2216 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
2217 SInt32 value = wxMax( iValue , 0 ) ;
2218 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
2219
2220 if ( iScrollBarOrientation == kTXNHorizontal )
2221 {
2222 if ( mlte->m_sbHorizontal )
2223 {
2224 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
2225 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
2226 mlte->m_lastHorizontalValue = value ;
2227 }
2228 }
2229 else if ( iScrollBarOrientation == kTXNVertical )
2230 {
2231 if ( mlte->m_sbVertical )
2232 {
2233 SetControl32BitValue( mlte->m_sbVertical , value ) ;
2234 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
2235 mlte->m_lastVerticalValue = value ;
2236 }
2237 }
2238 }
2239
2240 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
2241 {
2242 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
2243 if ( mlte == NULL )
2244 return ;
2245
2246 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
2247 return ;
2248
2249 OSStatus err ;
2250 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
2251
2252 SInt32 minimum = 0 ;
2253 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
2254 SInt32 value = GetControl32BitValue( controlRef ) ;
2255 SInt32 delta = 0;
2256
2257 switch ( partCode )
2258 {
2259 case kControlDownButtonPart :
2260 delta = 10 ;
2261 break ;
2262
2263 case kControlUpButtonPart :
2264 delta = -10 ;
2265 break ;
2266
2267 case kControlPageDownPart :
2268 delta = GetControlViewSize( controlRef ) ;
2269 break ;
2270
2271 case kControlPageUpPart :
2272 delta = -GetControlViewSize( controlRef ) ;
2273 break ;
2274
2275 case kControlIndicatorPart :
2276 delta = value - (isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue) ;
2277 break ;
2278
2279 default :
2280 break ;
2281 }
2282
2283 if ( delta != 0 )
2284 {
2285 SInt32 newValue = value ;
2286
2287 if ( partCode != kControlIndicatorPart )
2288 {
2289 if ( value + delta < minimum )
2290 delta = minimum - value ;
2291 if ( value + delta > maximum )
2292 delta = maximum - value ;
2293
2294 SetControl32BitValue( controlRef , value + delta ) ;
2295 newValue = value + delta ;
2296 }
2297
2298 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
2299 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
2300
2301 err = TXNScroll(
2302 mlte->m_txn, kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
2303 &verticalDelta, &horizontalDelta );
2304 verify_noerr( err );
2305
2306 if ( isHorizontal )
2307 mlte->m_lastHorizontalValue = newValue ;
2308 else
2309 mlte->m_lastVerticalValue = newValue ;
2310 }
2311 }
2312 #endif
2313
2314 // make correct activations
2315 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive)
2316 {
2317 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2318
2319 wxMacWindowClipper clipper( textctrl ) ;
2320 TXNActivate( m_txn, m_txnFrameID, setActive );
2321
2322 ControlRef controlFocus = 0 ;
2323 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2324 if ( controlFocus == m_controlRef )
2325 TXNFocus( m_txn, setActive );
2326 }
2327
2328 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus)
2329 {
2330 TXNFocus( m_txn, setFocus );
2331 }
2332
2333 // guards against inappropriate redraw (hidden objects drawing onto window)
2334
2335 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis)
2336 {
2337 ControlRef controlFocus = 0 ;
2338 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2339
2340 if ( !vis && (controlFocus == m_controlRef ) )
2341 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
2342
2343 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
2344 TXNControlData iControlData[1] = { { (UInt32)false } };
2345
2346 verify_noerr( TXNGetTXNObjectControls( m_txn , 1, iControlTags, iControlData ) ) ;
2347
2348 if ( iControlData[0].uValue != vis )
2349 {
2350 iControlData[0].uValue = vis ;
2351 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1, iControlTags, iControlData ) ) ;
2352 }
2353
2354 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2355 // if we run into further problems we might set the FrameBounds to an empty rect here
2356 }
2357
2358 // make sure that the TXNObject is at the right position
2359
2360 void wxMacMLTEClassicControl::MacUpdatePosition()
2361 {
2362 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2363 if ( textctrl == NULL )
2364 return ;
2365
2366 Rect bounds ;
2367 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2368
2369 wxRect visRect = textctrl->MacGetClippedClientRect() ;
2370 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
2371 int x , y ;
2372 x = y = 0 ;
2373 textctrl->MacWindowToRootWindow( &x , &y ) ;
2374 OffsetRect( &visBounds , x , y ) ;
2375
2376 if ( !EqualRect( &bounds, &m_txnControlBounds ) || !EqualRect( &visBounds, &m_txnVisBounds ) )
2377 {
2378 m_txnControlBounds = bounds ;
2379 m_txnVisBounds = visBounds ;
2380 wxMacWindowClipper cl( textctrl ) ;
2381
2382 #ifdef __WXMAC_OSX__
2383 bool isCompositing = textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ;
2384 if ( m_sbHorizontal || m_sbVertical )
2385 {
2386 int w = bounds.right - bounds.left ;
2387 int h = bounds.bottom - bounds.top ;
2388
2389 if ( m_sbHorizontal )
2390 {
2391 Rect sbBounds ;
2392
2393 sbBounds.left = -1 ;
2394 sbBounds.top = h - 14 ;
2395 sbBounds.right = w + 1 ;
2396 sbBounds.bottom = h + 1 ;
2397
2398 if ( !isCompositing )
2399 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2400
2401 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
2402 SetControlViewSize( m_sbHorizontal , w ) ;
2403 }
2404
2405 if ( m_sbVertical )
2406 {
2407 Rect sbBounds ;
2408
2409 sbBounds.left = w - 14 ;
2410 sbBounds.top = -1 ;
2411 sbBounds.right = w + 1 ;
2412 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
2413
2414 if ( !isCompositing )
2415 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2416
2417 SetControlBounds( m_sbVertical , &sbBounds ) ;
2418 SetControlViewSize( m_sbVertical , h ) ;
2419 }
2420 }
2421
2422 Rect oldviewRect ;
2423 TXNLongRect olddestRect ;
2424 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
2425
2426 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2427 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2428 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2429 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2430 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) ,
2431 m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2432
2433 if ( olddestRect.right >= 10000 )
2434 destRect.right = destRect.left + 32000 ;
2435
2436 if ( olddestRect.bottom >= 0x20000000 )
2437 destRect.bottom = destRect.top + 0x40000000 ;
2438
2439 SectRect( &viewRect , &visBounds , &viewRect ) ;
2440 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
2441
2442 #if 0
2443 TXNSetFrameBounds(
2444 m_txn,
2445 m_txnControlBounds.top,
2446 m_txnControlBounds.left,
2447 m_txnControlBounds.bottom - (m_sbHorizontal ? 14 : 0),
2448 m_txnControlBounds.right - (m_sbVertical ? 14 : 0),
2449 m_txnFrameID );
2450 #endif
2451 #else
2452
2453 TXNSetFrameBounds(
2454 m_txn, m_txnControlBounds.top, m_txnControlBounds.left,
2455 wxMax( m_txnControlBounds.bottom, m_txnControlBounds.top ),
2456 wxMax( m_txnControlBounds.right, m_txnControlBounds.left ), m_txnFrameID );
2457 #endif
2458
2459 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2460 // movement, therefore we have to force it
2461
2462 // this problem has been reported in OSX as well, so we use this here once again
2463
2464 TXNLongRect textRect ;
2465 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
2466 if ( textRect.left < m_txnControlBounds.left )
2467 TXNShowSelection( m_txn , kTXNShowStart ) ;
2468 }
2469 }
2470
2471 void wxMacMLTEClassicControl::SetRect( Rect *r )
2472 {
2473 wxMacControl::SetRect( r ) ;
2474 MacUpdatePosition() ;
2475 }
2476
2477 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 thePart)
2478 {
2479 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2480 if ( textctrl == NULL )
2481 return ;
2482
2483 if ( textctrl->MacIsReallyShown() )
2484 {
2485 wxMacWindowClipper clipper( textctrl ) ;
2486 TXNDraw( m_txn , NULL ) ;
2487 }
2488 }
2489
2490 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
2491 {
2492 Point where = { y , x } ;
2493 ControlPartCode result = kControlNoPart;
2494
2495 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2496 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2497 {
2498 if (PtInRect( where, &m_txnControlBounds ))
2499 {
2500 result = kControlEditTextPart ;
2501 }
2502 else
2503 {
2504 // sometimes we get the coords also in control local coordinates, therefore test again
2505 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2506 {
2507 int x = 0 , y = 0 ;
2508 textctrl->MacClientToRootWindow( &x , &y ) ;
2509 where.h += x ;
2510 where.v += y ;
2511 }
2512
2513 if (PtInRect( where, &m_txnControlBounds ))
2514 result = kControlEditTextPart ;
2515 }
2516 }
2517
2518 return result;
2519 }
2520
2521 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* actionProc )
2522 {
2523 ControlPartCode result = kControlNoPart;
2524
2525 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference( m_controlRef );
2526 if ( (textctrl != NULL) && textctrl->MacIsReallyShown() )
2527 {
2528 Point startPt = { y , x } ;
2529 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2530 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2531 {
2532 int x = 0 , y = 0 ;
2533 textctrl->MacClientToRootWindow( &x , &y ) ;
2534 startPt.h += x ;
2535 startPt.v += y ;
2536 }
2537
2538 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
2539 {
2540 case kControlEditTextPart :
2541 {
2542 wxMacWindowClipper clipper( textctrl ) ;
2543 EventRecord rec ;
2544
2545 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
2546 TXNClick( m_txn, &rec );
2547 }
2548 break;
2549
2550 default :
2551 break;
2552 }
2553 }
2554
2555 return result;
2556 }
2557
2558 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2559 {
2560 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2561 if ( textctrl == NULL )
2562 return ;
2563
2564 if (textctrl->MacIsReallyShown())
2565 {
2566 if (IsControlActive(m_controlRef))
2567 {
2568 Point mousep;
2569
2570 wxMacWindowClipper clipper( textctrl ) ;
2571 GetMouse(&mousep);
2572
2573 TXNIdle(m_txn);
2574
2575 if (PtInRect(mousep, &m_txnControlBounds))
2576 {
2577 RgnHandle theRgn = NewRgn();
2578 RectRgn(theRgn, &m_txnControlBounds);
2579 TXNAdjustCursor(m_txn, theRgn);
2580 DisposeRgn(theRgn);
2581 }
2582 }
2583 }
2584 }
2585
2586 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
2587 {
2588 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2589 if ( textctrl == NULL )
2590 return kControlNoPart;
2591
2592 wxMacWindowClipper clipper( textctrl ) ;
2593
2594 EventRecord ev ;
2595 memset( &ev , 0 , sizeof( ev ) ) ;
2596 ev.what = keyDown ;
2597 ev.modifiers = modifiers ;
2598 ev.message = ((keyCode << 8) & keyCodeMask) | (charCode & charCodeMask);
2599 TXNKeyDown( m_txn , &ev );
2600
2601 return kControlEntireControl;
2602 }
2603
2604 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating)
2605 {
2606 MacActivatePaneText( activating );
2607 }
2608
2609 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
2610 {
2611 ControlPartCode focusResult = kControlFocusNoPart;
2612
2613 wxTextCtrl* textctrl = (wxTextCtrl*)GetControlReference( m_controlRef );
2614 if ( textctrl == NULL )
2615 return focusResult;
2616
2617 wxMacWindowClipper clipper( textctrl ) ;
2618
2619 ControlRef controlFocus = NULL ;
2620 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2621 bool wasFocused = ( controlFocus == m_controlRef ) ;
2622
2623 switch (action)
2624 {
2625 case kControlFocusPrevPart:
2626 case kControlFocusNextPart:
2627 MacFocusPaneText( !wasFocused );
2628 focusResult = (!wasFocused ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart);
2629 break;
2630
2631 case kControlFocusNoPart:
2632 default:
2633 MacFocusPaneText( false );
2634 focusResult = kControlFocusNoPart;
2635 break;
2636 }
2637
2638 return focusResult;
2639 }
2640
2641 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *info )
2642 {
2643 }
2644
2645 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
2646 const wxString& str,
2647 const wxPoint& pos,
2648 const wxSize& size, long style )
2649 : wxMacMLTEControl( wxPeer )
2650 {
2651 m_font = wxPeer->GetFont() ;
2652 m_windowStyle = style ;
2653 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2654
2655 short featureSet =
2656 kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
2657 | kControlWantsActivate | kControlHandlesTracking
2658 // | kControlHasSpecialBackground
2659 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
2660
2661 OSStatus err = ::CreateUserPaneControl(
2662 MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()),
2663 &bounds, featureSet, &m_controlRef );
2664 verify_noerr( err );
2665
2666 DoCreate();
2667
2668 AdjustCreationAttributes( *wxWHITE , true ) ;
2669
2670 MacSetObjectVisibility( wxPeer->MacIsReallyShown() ) ;
2671
2672 {
2673 wxString st = str ;
2674 wxMacConvertNewlines10To13( &st ) ;
2675 wxMacWindowClipper clipper( m_peer ) ;
2676 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2677 TXNSetSelection( m_txn, 0, 0 ) ;
2678 }
2679 }
2680
2681 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2682 {
2683 TXNDeleteObject( m_txn );
2684 m_txn = NULL ;
2685 }
2686
2687 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
2688 {
2689 MacSetObjectVisibility( shown ) ;
2690 wxMacControl::VisibilityChanged( shown ) ;
2691 }
2692
2693 void wxMacMLTEClassicControl::SuperChangedPosition()
2694 {
2695 MacUpdatePosition() ;
2696 wxMacControl::SuperChangedPosition() ;
2697 }
2698
2699 #ifdef __WXMAC_OSX__
2700
2701 ControlUserPaneDrawUPP gTPDrawProc = NULL;
2702 ControlUserPaneHitTestUPP gTPHitProc = NULL;
2703 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
2704 ControlUserPaneIdleUPP gTPIdleProc = NULL;
2705 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
2706 ControlUserPaneActivateUPP gTPActivateProc = NULL;
2707 ControlUserPaneFocusUPP gTPFocusProc = NULL;
2708
2709 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
2710 {
2711 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2712 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2713 if ( win )
2714 win->MacControlUserPaneDrawProc( part ) ;
2715 }
2716
2717 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
2718 {
2719 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2720 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2721 if ( win )
2722 return win->MacControlUserPaneHitTestProc( where.h , where.v ) ;
2723 else
2724 return kControlNoPart ;
2725 }
2726
2727 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
2728 {
2729 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2730 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2731 if ( win )
2732 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc ) ;
2733 else
2734 return kControlNoPart ;
2735 }
2736
2737 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
2738 {
2739 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2740 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2741 if ( win )
2742 win->MacControlUserPaneIdleProc() ;
2743 }
2744
2745 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
2746 {
2747 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2748 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2749 if ( win )
2750 return win->MacControlUserPaneKeyDownProc( keyCode, charCode, modifiers ) ;
2751 else
2752 return kControlNoPart ;
2753 }
2754
2755 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2756 {
2757 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2758 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2759 if ( win )
2760 win->MacControlUserPaneActivateProc( activating ) ;
2761 }
2762
2763 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2764 {
2765 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2766 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2767 if ( win )
2768 return win->MacControlUserPaneFocusProc( action ) ;
2769 else
2770 return kControlNoPart ;
2771 }
2772
2773 #if 0
2774 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2775 {
2776 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2777 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2778 if ( win )
2779 win->MacControlUserPaneBackgroundProc(info) ;
2780 }
2781 #endif
2782
2783 #endif // __WXMAC_OSX__
2784
2785 // TXNRegisterScrollInfoProc
2786
2787 OSStatus wxMacMLTEClassicControl::DoCreate()
2788 {
2789 Rect bounds;
2790 OSStatus err = noErr ;
2791
2792 // set up our globals
2793 #ifdef __WXMAC_OSX__
2794 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2795 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2796 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2797 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2798 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2799 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2800 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2801
2802 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2803 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2804 #endif
2805
2806 // set the initial settings for our private data
2807
2808 m_txnWindow = GetControlOwner(m_controlRef);
2809 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2810
2811 #ifdef __WXMAC_OSX__
2812 // set up the user pane procedures
2813 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2814 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2815 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2816 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2817 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2818 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2819 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2820 #endif
2821
2822 // calculate the rectangles used by the control
2823 UMAGetControlBoundsInWindowCoords( m_controlRef, &bounds );
2824
2825 m_txnControlBounds = bounds ;
2826 m_txnVisBounds = bounds ;
2827
2828 CGrafPtr origPort ;
2829 GDHandle origDev ;
2830
2831 GetGWorld( &origPort, &origDev ) ;
2832 SetPort( m_txnPort );
2833
2834 // create the new edit field
2835 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle );
2836
2837 #ifdef __WXMAC_OSX__
2838 // the scrollbars are not correctly embedded but are inserted at the root:
2839 // this gives us problems as we have erratic redraws even over the structure area
2840
2841 m_sbHorizontal = 0 ;
2842 m_sbVertical = 0 ;
2843 m_lastHorizontalValue = 0 ;
2844 m_lastVerticalValue = 0 ;
2845
2846 Rect sb = { 0 , 0 , 0 , 0 } ;
2847 if ( frameOptions & kTXNWantVScrollBarMask )
2848 {
2849 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbVertical );
2850 SetControlReference( m_sbVertical, (SInt32)this );
2851 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2852 ShowControl( m_sbVertical );
2853 EmbedControl( m_sbVertical , m_controlRef );
2854 frameOptions &= ~kTXNWantVScrollBarMask;
2855 }
2856
2857 if ( frameOptions & kTXNWantHScrollBarMask )
2858 {
2859 CreateScrollBarControl( m_txnWindow, &sb, 0, 0, 100, 1, true, gTXNScrollActionProc, &m_sbHorizontal );
2860 SetControlReference( m_sbHorizontal, (SInt32)this );
2861 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2862 ShowControl( m_sbHorizontal );
2863 EmbedControl( m_sbHorizontal, m_controlRef );
2864 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2865 }
2866
2867 #endif
2868
2869 err = TXNNewObject(
2870 NULL, m_txnWindow, &bounds, frameOptions,
2871 kTXNTextEditStyleFrameType, kTXNTextensionFile, kTXNSystemDefaultEncoding,
2872 &m_txn, &m_txnFrameID, NULL );
2873 verify_noerr( err );
2874
2875 #if 0
2876 TXNControlTag iControlTags[] = { kTXNUseCarbonEvents };
2877 TXNControlData iControlData[] = { { (UInt32)&cInfo } };
2878 int toptag = WXSIZEOF( iControlTags ) ;
2879 TXNCarbonEventInfo cInfo ;
2880 cInfo.useCarbonEvents = false ;
2881 cInfo.filler = 0 ;
2882 cInfo.flags = 0 ;
2883 cInfo.fDictionary = NULL ;
2884
2885 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag, iControlTags, iControlData ) );
2886 #endif
2887
2888 #ifdef __WXMAC_OSX__
2889 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32)this );
2890 #endif
2891
2892 SetGWorld( origPort , origDev ) ;
2893
2894 return err;
2895 }
2896
2897 // ----------------------------------------------------------------------------
2898 // MLTE control implementation (OSX part)
2899 // ----------------------------------------------------------------------------
2900
2901 #if TARGET_API_MAC_OSX
2902
2903 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
2904
2905 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
2906 const wxString& str,
2907 const wxPoint& pos,
2908 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
2909 {
2910 m_font = wxPeer->GetFont() ;
2911 m_windowStyle = style ;
2912 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2913 wxString st = str ;
2914 wxMacConvertNewlines10To13( &st ) ;
2915
2916 HIRect hr = {
2917 { bounds.left , bounds.top },
2918 { bounds.right - bounds.left, bounds.bottom - bounds.top } } ;
2919
2920 m_scrollView = NULL ;
2921 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
2922 if ( frameOptions & (kTXNWantVScrollBarMask | kTXNWantHScrollBarMask) )
2923 {
2924 HIScrollViewCreate(
2925 (frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0)
2926 | (frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll : 0) ,
2927 &m_scrollView ) ;
2928
2929 HIViewSetFrame( m_scrollView, &hr );
2930 HIViewSetVisible( m_scrollView, true );
2931 }
2932
2933 m_textView = NULL ;
2934 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
2935 m_txn = HITextViewGetTXNObject( m_textView ) ;
2936 HIViewSetVisible( m_textView , true ) ;
2937 if ( m_scrollView )
2938 {
2939 HIViewAddSubview( m_scrollView , m_textView ) ;
2940 m_controlRef = m_scrollView ;
2941 wxPeer->MacInstallEventHandler( (WXWidget) m_textView ) ;
2942 }
2943 else
2944 {
2945 HIViewSetFrame( m_textView, &hr );
2946 m_controlRef = m_textView ;
2947 }
2948
2949 AdjustCreationAttributes( *wxWHITE , true ) ;
2950
2951 wxMacWindowClipper c( m_peer ) ;
2952 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2953
2954 TXNSetSelection( m_txn, 0, 0 );
2955 TXNShowSelection( m_txn, kTXNShowStart );
2956 }
2957
2958 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
2959 {
2960 return SetKeyboardFocus( GetControlOwner( m_textView ), m_textView, focusPart ) ;
2961 }
2962
2963 bool wxMacMLTEHIViewControl::HasFocus() const
2964 {
2965 ControlRef control ;
2966 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2967 return control == m_textView ;
2968 }
2969
2970 void wxMacMLTEHIViewControl::SetBackground( const wxBrush &brush )
2971 {
2972 wxMacMLTEControl::SetBackground( brush ) ;
2973
2974 #if 0
2975 CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
2976 RGBColor col = MAC_WXCOLORREF(brush.GetColour().GetPixel()) ;
2977
2978 float component[4] ;
2979 component[0] = col.red / 65536.0 ;
2980 component[1] = col.green / 65536.0 ;
2981 component[2] = col.blue / 65536.0 ;
2982 component[3] = 1.0 ; // alpha
2983
2984 CGColorRef color = CGColorCreate( rgbSpace , component );
2985 HITextViewSetBackgroundColor( m_textView , color );
2986 CGColorSpaceRelease( rgbSpace );
2987 #endif
2988 }
2989
2990 #endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
2991
2992
2993 #endif
2994
2995 #endif // wxUSE_TEXTCTRL