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