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