default implementations for Replace and Remove
[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 ) ;
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 virtual void SetBackground( const wxBrush &brush) ;
275 protected :
276 HIViewRef m_scrollView ;
277 HIViewRef m_textView ;
278 } ;
279
280 #endif
281
282 class wxMacUnicodeTextControl : public wxMacTextControl
283 {
284 public :
285 wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
286 const wxString& str,
287 const wxPoint& pos,
288 const wxSize& size, long style ) ;
289 ~wxMacUnicodeTextControl();
290 virtual void VisibilityChanged(bool shown);
291 virtual wxString GetStringValue() const ;
292 virtual void SetStringValue( const wxString &str) ;
293 virtual void Copy();
294 virtual void Cut();
295 virtual void Paste();
296 virtual bool CanPaste() const;
297 virtual void SetEditable(bool editable) ;
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 &val )
1177 {
1178 SetSelection( from , to ) ;
1179 WriteText( val) ;
1180 }
1181
1182 void wxMacTextControl::Remove( long from , long to )
1183 {
1184 SetSelection( from , to ) ;
1185 WriteText( wxEmptyString) ;
1186 }
1187
1188 void wxMacTextControl::Clear()
1189 {
1190 SetStringValue( wxEmptyString ) ;
1191 }
1192
1193 bool wxMacTextControl::CanUndo() const
1194 {
1195 return false ;
1196 }
1197
1198 void wxMacTextControl::Undo() { }
1199
1200 bool wxMacTextControl::CanRedo() const
1201 {
1202 return false ;
1203 }
1204
1205 void wxMacTextControl::Redo()
1206 {
1207 }
1208
1209 long wxMacTextControl::XYToPosition(long x, long y) const
1210 {
1211 return 0 ;
1212 }
1213
1214 bool wxMacTextControl::PositionToXY(long pos, long *x, long *y) const
1215 {
1216 return false ;
1217 }
1218
1219 void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
1220 {
1221 }
1222
1223 int wxMacTextControl::GetNumberOfLines() const
1224 {
1225 ItemCount lines = 0 ;
1226 wxString content = GetStringValue() ;
1227 lines = 1;
1228 for (size_t i = 0; i < content.Length() ; i++)
1229 {
1230 if (content[i] == '\r') lines++;
1231 }
1232 return lines ;
1233 }
1234
1235 wxString wxMacTextControl::GetLineText(long lineNo) const
1236 {
1237 // TODO change this if possible to reflect real lines
1238 wxString content = GetStringValue() ;
1239
1240 // Find line first
1241 int count = 0;
1242 for (size_t i = 0; i < content.Length() ; i++)
1243 {
1244 if (count == lineNo)
1245 {
1246 // Add chars in line then
1247 wxString tmp;
1248
1249 for (size_t j = i; j < content.Length(); j++)
1250 {
1251 if (content[j] == '\n')
1252 return tmp;
1253
1254 tmp += content[j];
1255 }
1256
1257 return tmp;
1258 }
1259 if (content[i] == '\n') count++;
1260 }
1261 return wxEmptyString ;
1262 }
1263
1264 int wxMacTextControl::GetLineLength(long lineNo) const
1265 {
1266 // TODO change this if possible to reflect real lines
1267 wxString content = GetStringValue() ;
1268
1269 // Find line first
1270 int count = 0;
1271 for (size_t i = 0; i < content.Length() ; i++)
1272 {
1273 if (count == lineNo)
1274 {
1275 // Count chars in line then
1276 count = 0;
1277 for (size_t j = i; j < content.Length(); j++)
1278 {
1279 count++;
1280 if (content[j] == '\n') return count;
1281 }
1282
1283 return count;
1284 }
1285 if (content[i] == '\n') count++;
1286 }
1287 return 0 ;
1288 }
1289
1290 // ----------------------------------------------------------------------------
1291 // standard unicode control implementation
1292 // ----------------------------------------------------------------------------
1293
1294 #if TARGET_API_MAC_OSX
1295
1296 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
1297 const wxString& str,
1298 const wxPoint& pos,
1299 const wxSize& size, long style ) : wxMacTextControl( wxPeer )
1300 {
1301 m_font = wxPeer->GetFont() ;
1302 m_windowStyle = style ;
1303 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
1304 wxString st = str ;
1305 wxMacConvertNewlines10To13( &st ) ;
1306 wxMacCFStringHolder cf(st , m_font.GetEncoding()) ;
1307 CFStringRef cfr = cf ;
1308 Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
1309 m_valueTag = isPassword ? kControlEditTextPasswordCFStringTag : kControlEditTextCFStringTag ;
1310 CreateEditUnicodeTextControl( MAC_WXHWND(wxPeer->MacGetTopLevelWindowRef()), &bounds , cfr , isPassword , NULL , &m_controlRef ) ;
1311
1312 if ( !(m_windowStyle & wxTE_MULTILINE) )
1313 {
1314 SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
1315 }
1316 }
1317
1318 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1319 {
1320 }
1321
1322 void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
1323 {
1324 if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
1325 {
1326 // work around a refresh issue insofar as not always the entire content is shown even if this would be possible
1327 ControlEditTextSelectionRec sel ;
1328 CFStringRef value = NULL ;
1329
1330 verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1331 verify_noerr( GetData<CFStringRef>( 0, m_valueTag , &value ) );
1332 verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
1333 verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
1334
1335 CFRelease( value ) ;
1336 }
1337 }
1338 wxString wxMacUnicodeTextControl::GetStringValue() const
1339 {
1340 wxString result ;
1341 CFStringRef value = GetData<CFStringRef>(0,m_valueTag) ;
1342 if ( value )
1343 {
1344 wxMacCFStringHolder cf(value) ;
1345 result = cf.AsString() ;
1346 }
1347 #if '\n' == 10
1348 wxMacConvertNewlines13To10( &result ) ;
1349 #else
1350 wxMacConvertNewlines10To13( &result ) ;
1351 #endif
1352 return result ;
1353 }
1354 void wxMacUnicodeTextControl::SetStringValue( const wxString &str)
1355 {
1356 wxString st = str ;
1357 wxMacConvertNewlines10To13( &st ) ;
1358 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
1359 verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
1360 }
1361 void wxMacUnicodeTextControl::Copy()
1362 {
1363 SendHICommand( kHICommandCopy ) ;
1364 }
1365 void wxMacUnicodeTextControl::Cut()
1366 {
1367 SendHICommand( kHICommandCut ) ;
1368 }
1369 void wxMacUnicodeTextControl::Paste()
1370 {
1371 SendHICommand( kHICommandPaste ) ;
1372 }
1373 bool wxMacUnicodeTextControl::CanPaste() const
1374 {
1375 return true ;
1376 }
1377 void wxMacUnicodeTextControl::SetEditable(bool editable)
1378 {
1379 SetData<Boolean>( 0 , kControlEditTextLockedTag , (Boolean) !editable ) ;
1380 }
1381
1382 void wxMacUnicodeTextControl::GetSelection( long* from, long* to) const
1383 {
1384 ControlEditTextSelectionRec sel ;
1385 verify_noerr(GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
1386 if ( from ) *from = sel.selStart ;
1387 if ( to ) *to = sel.selEnd ;
1388 }
1389
1390 void wxMacUnicodeTextControl::SetSelection( long from , long to )
1391 {
1392 ControlEditTextSelectionRec sel ;
1393 if ((from == -1) && (to == -1))
1394 {
1395 from = 0 ;
1396 to = 32767 ; // sel has 16 bit signed values, max is 32767
1397 }
1398 sel.selStart = from ;
1399 sel.selEnd = to ;
1400 SetData<ControlEditTextSelectionRec>( 0 , kControlEditTextSelectionTag, &sel ) ;
1401 }
1402
1403 void wxMacUnicodeTextControl::WriteText(const wxString& str)
1404 {
1405 wxString st = str ;
1406 wxMacConvertNewlines10To13( &st ) ;
1407 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1408 wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
1409 CFStringRef value = cf ;
1410 SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
1411 #else
1412 wxString val = GetStringValue() ;
1413 long start , end ;
1414 GetSelection( &start , &end ) ;
1415 val.Remove( start , end - start ) ;
1416 val.insert( start , str ) ;
1417 SetStringValue( val ) ;
1418 SetSelection( start + str.Length() , start + str.Length() ) ;
1419 #endif
1420 }
1421
1422 #endif
1423
1424 // ----------------------------------------------------------------------------
1425 // MLTE control implementation (common part)
1426 // ----------------------------------------------------------------------------
1427
1428 // if mlte is on read only , no changes at all are allowed, not even from
1429 // procedural API, in order to allow changes via API all the same we must undo
1430 // the readonly status while we are executing, this class helps to do so
1431
1432 class wxMacEditHelper
1433 {
1434 public :
1435 wxMacEditHelper( TXNObject txn )
1436 {
1437 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1438 m_txn = txn ;
1439 TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
1440 if ( m_data[0].uValue == kTXNReadOnly )
1441 {
1442 TXNControlData data[] = { { kTXNReadWrite } } ;
1443 TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
1444 }
1445 }
1446 ~wxMacEditHelper()
1447 {
1448 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1449 if ( m_data[0].uValue == kTXNReadOnly )
1450 {
1451 TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
1452 }
1453 }
1454 protected :
1455 TXNObject m_txn ;
1456 TXNControlData m_data[1] ;
1457 } ;
1458
1459 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer ) : wxMacTextControl( peer )
1460 {
1461 SetNeedsFocusRect( true ) ;
1462 }
1463
1464 wxString wxMacMLTEControl::GetStringValue() const
1465 {
1466 wxString result ;
1467 OSStatus err ;
1468 Size actualSize = 0;
1469 {
1470 #if wxUSE_UNICODE
1471 Handle theText ;
1472 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText , kTXNUnicodeTextData );
1473 // all done
1474 if ( err )
1475 {
1476 actualSize = 0 ;
1477 }
1478 else
1479 {
1480 actualSize = GetHandleSize( theText ) / sizeof( UniChar) ;
1481 if ( actualSize > 0 )
1482 {
1483 wxChar *ptr = NULL ;
1484 #if SIZEOF_WCHAR_T == 2
1485 ptr = new wxChar[actualSize + 1 ] ;
1486 wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ;
1487 #else
1488 SetHandleSize( theText , ( actualSize + 1 ) * sizeof( UniChar ) ) ;
1489 HLock( theText ) ;
1490 (((UniChar*)*theText)[actualSize]) = 0 ;
1491 wxMBConvUTF16 converter ;
1492 size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
1493 ptr = new wxChar[noChars + 1] ;
1494
1495 noChars = converter.MB2WC( ptr , (const char*)*theText , noChars ) ;
1496 ptr[noChars] = 0 ;
1497 HUnlock( theText ) ;
1498 #endif
1499 ptr[actualSize] = 0 ;
1500 result = wxString( ptr ) ;
1501 delete[] ptr ;
1502 }
1503 DisposeHandle( theText ) ;
1504 }
1505 #else
1506 Handle theText ;
1507 err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
1508 // all done
1509 if ( err )
1510 {
1511 actualSize = 0 ;
1512 }
1513 else
1514 {
1515 actualSize = GetHandleSize( theText ) ;
1516 if ( actualSize > 0 )
1517 {
1518 HLock( theText ) ;
1519 result = wxString( *theText , wxConvLocal , actualSize ) ;
1520 HUnlock( theText ) ;
1521 }
1522 DisposeHandle( theText ) ;
1523 }
1524 #endif
1525 }
1526 #if '\n' == 10
1527 wxMacConvertNewlines13To10( &result ) ;
1528 #else
1529 wxMacConvertNewlines10To13( &result ) ;
1530 #endif
1531 return result ;
1532 }
1533
1534 void wxMacMLTEControl::SetStringValue( const wxString &str)
1535 {
1536 wxString st = str ;
1537
1538 wxMacConvertNewlines10To13( &st ) ;
1539 {
1540 wxMacWindowClipper c( m_peer ) ;
1541 {
1542 wxMacEditHelper help(m_txn) ;
1543 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
1544 }
1545 TXNSetSelection( m_txn, 0, 0);
1546 TXNShowSelection( m_txn, kTXNShowStart);
1547 }
1548 }
1549
1550 TXNFrameOptions wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle )
1551 {
1552 TXNFrameOptions frameOptions =
1553 kTXNDontDrawCaretWhenInactiveMask
1554 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1555 | kTXNDoFontSubstitutionMask
1556 #endif
1557 ;
1558
1559 if ( ! ( wxStyle & wxTE_NOHIDESEL ) )
1560 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
1561
1562 if ( wxStyle & wxTE_MULTILINE )
1563 {
1564 if ( ! ( wxStyle & wxTE_DONTWRAP ) )
1565 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
1566 else
1567 {
1568 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
1569 frameOptions |= kTXNWantHScrollBarMask ;
1570 }
1571
1572 if ( !(wxStyle & wxTE_NO_VSCROLL ) )
1573 {
1574 frameOptions |= kTXNWantVScrollBarMask ;
1575 if ( frameOptions & kTXNWantHScrollBarMask )
1576 frameOptions |= kTXNDrawGrowIconMask ;
1577 }
1578 }
1579 else
1580 frameOptions |= kTXNSingleLineOnlyMask ;
1581
1582 if ( wxStyle & wxHSCROLL )
1583 frameOptions |= kTXNWantHScrollBarMask ;
1584
1585 return frameOptions ;
1586 }
1587
1588 void wxMacMLTEControl::AdjustCreationAttributes( const wxColour &background, bool visible )
1589 {
1590 TXNControlTag iControlTags[] =
1591 {
1592 kTXNDoFontSubstitution,
1593 kTXNWordWrapStateTag ,
1594 };
1595 TXNControlData iControlData[] =
1596 {
1597 {true},
1598 {kTXNNoAutoWrap},
1599 };
1600
1601 int toptag = WXSIZEOF( iControlTags ) ;
1602
1603 if ( m_windowStyle & wxTE_MULTILINE )
1604 {
1605 if (m_windowStyle & wxTE_DONTWRAP)
1606 iControlData[1].uValue = kTXNNoAutoWrap ;
1607 else
1608 iControlData[1].uValue = kTXNAutoWrap ;
1609 }
1610 verify_noerr( TXNSetTXNObjectControls( m_txn, false, toptag,
1611 iControlTags, iControlData )) ;
1612
1613 // setting the default font
1614 // under 10.2 this causes a visible caret, therefore we avoid it
1615
1616 if ( UMAGetSystemVersion() >= 0x1030 )
1617 {
1618 Str255 fontName ;
1619 SInt16 fontSize ;
1620 Style fontStyle ;
1621
1622 GetThemeFont(kThemeSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1623
1624 TXNTypeAttributes typeAttr[] =
1625 {
1626 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
1627 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
1628 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
1629 } ;
1630
1631 verify_noerr( TXNSetTypeAttributes (m_txn, sizeof( typeAttr ) / sizeof(TXNTypeAttributes) , typeAttr,
1632 kTXNStartOffset,
1633 kTXNEndOffset) );
1634 }
1635
1636 if ( m_windowStyle & wxTE_PASSWORD )
1637 {
1638 UniChar c = 0xA5 ;
1639 verify_noerr(TXNEchoMode( m_txn , c , 0 , true )) ;
1640 }
1641
1642 TXNBackground tback;
1643 tback.bgType = kTXNBackgroundTypeRGB;
1644 tback.bg.color = MAC_WXCOLORREF( background.GetPixel() );
1645 TXNSetBackground( m_txn , &tback);
1646 }
1647
1648 void wxMacMLTEControl::SetBackground( const wxBrush &brush )
1649 {
1650 // currently only solid background are supported
1651 TXNBackground tback;
1652 tback.bgType = kTXNBackgroundTypeRGB;
1653 tback.bg.color = MAC_WXCOLORREF( brush.GetColour().GetPixel() );
1654 TXNSetBackground( m_txn , &tback);
1655 }
1656
1657 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to)
1658 {
1659 TXNTypeAttributes typeAttr[4] ;
1660 Str255 fontName = "\pMonaco" ;
1661 SInt16 fontSize = 12 ;
1662 Style fontStyle = normal ;
1663 RGBColor color ;
1664 int attrCounter = 0 ;
1665 if ( style.HasFont() )
1666 {
1667 const wxFont &font = style.GetFont() ;
1668 wxMacStringToPascal( font.GetFaceName() , fontName ) ;
1669 fontSize = font.GetPointSize() ;
1670 if ( font.GetUnderlined() )
1671 fontStyle |= underline ;
1672 if ( font.GetWeight() == wxBOLD )
1673 fontStyle |= bold ;
1674 if ( font.GetStyle() == wxITALIC )
1675 fontStyle |= italic ;
1676
1677 typeAttr[attrCounter].tag = kTXNQDFontNameAttribute ;
1678 typeAttr[attrCounter].size = kTXNQDFontNameAttributeSize ;
1679 typeAttr[attrCounter].data.dataPtr = (void*) fontName ;
1680 typeAttr[attrCounter+1].tag = kTXNQDFontSizeAttribute ;
1681 typeAttr[attrCounter+1].size = kTXNFontSizeAttributeSize ;
1682 typeAttr[attrCounter+1].data.dataValue = (fontSize << 16) ;
1683 typeAttr[attrCounter+2].tag = kTXNQDFontStyleAttribute ;
1684 typeAttr[attrCounter+2].size = kTXNQDFontStyleAttributeSize ;
1685 typeAttr[attrCounter+2].data.dataValue = fontStyle ;
1686 attrCounter += 3 ;
1687 }
1688 if ( style.HasTextColour() )
1689 {
1690 typeAttr[attrCounter].tag = kTXNQDFontColorAttribute ;
1691 typeAttr[attrCounter].size = kTXNQDFontColorAttributeSize ;
1692 typeAttr[attrCounter].data.dataPtr = (void*) &color ;
1693 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
1694 attrCounter += 1 ;
1695 }
1696 if ( attrCounter > 0 )
1697 {
1698 verify_noerr( TXNSetTypeAttributes ( m_txn , attrCounter , typeAttr, from , to) );
1699 }
1700 }
1701
1702 void wxMacMLTEControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
1703 {
1704 wxMacEditHelper help(m_txn) ;
1705 TXNSetAttribute( wxTextAttr(foreground,wxNullColour,font) , kTXNStartOffset,kTXNEndOffset ) ;
1706 }
1707 void wxMacMLTEControl::SetStyle(long start, long end, const wxTextAttr& style)
1708 {
1709 wxMacEditHelper help(m_txn) ;
1710 TXNSetAttribute( style , start,end ) ;
1711 }
1712
1713 void wxMacMLTEControl::Copy()
1714 {
1715 ClearCurrentScrap();
1716 TXNCopy(m_txn);
1717 TXNConvertToPublicScrap();
1718 }
1719
1720 void wxMacMLTEControl::Cut()
1721 {
1722 ClearCurrentScrap();
1723 TXNCut(m_txn);
1724 TXNConvertToPublicScrap();
1725 }
1726
1727 void wxMacMLTEControl::Paste()
1728 {
1729 TXNConvertFromPublicScrap();
1730 TXNPaste(m_txn);
1731 }
1732
1733 bool wxMacMLTEControl::CanPaste() const
1734 {
1735 return TXNIsScrapPastable() ;
1736 }
1737
1738 void wxMacMLTEControl::SetEditable(bool editable)
1739 {
1740 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1741 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1742 TXNSetTXNObjectControls( m_txn , false , WXSIZEOF(tag) , tag , data ) ;
1743 }
1744
1745 wxTextPos wxMacMLTEControl::GetLastPosition() const
1746 {
1747 wxTextPos actualsize = 0 ;
1748
1749 Handle theText ;
1750 OSErr err = TXNGetDataEncoded( m_txn, kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
1751 /* all done */
1752 if ( err )
1753 {
1754 actualsize = 0 ;
1755 }
1756 else
1757 {
1758 actualsize = GetHandleSize( theText ) ;
1759 DisposeHandle( theText ) ;
1760 }
1761
1762 return actualsize ;
1763 }
1764
1765 void wxMacMLTEControl::Replace( long from , long to , const wxString &str )
1766 {
1767 wxString value = str ;
1768 wxMacConvertNewlines10To13( &value ) ;
1769
1770 wxMacEditHelper help( m_txn ) ;
1771 wxMacWindowClipper c( m_peer ) ;
1772
1773 TXNSetSelection(m_txn , from , to ) ;
1774 TXNClear( m_txn ) ;
1775 SetTXNData( value , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1776 }
1777
1778 void wxMacMLTEControl::Remove( long from , long to )
1779 {
1780 wxMacWindowClipper c( m_peer ) ;
1781 wxMacEditHelper help( m_txn ) ;
1782 TXNSetSelection(m_txn , from , to ) ;
1783 TXNClear( m_txn ) ;
1784 }
1785
1786 void wxMacMLTEControl::GetSelection( long* from, long* to) const
1787 {
1788 TXNGetSelection( m_txn , (TXNOffset*) from , (TXNOffset*) to ) ;
1789 }
1790
1791 void wxMacMLTEControl::SetSelection( long from , long to )
1792 {
1793 wxMacWindowClipper c( m_peer ) ;
1794 /* change the selection */
1795 if ((from == -1) && (to == -1))
1796 TXNSelectAll(m_txn);
1797 else
1798 TXNSetSelection( m_txn, from, to);
1799 TXNShowSelection( m_txn, kTXNShowStart);
1800 }
1801
1802 void wxMacMLTEControl::WriteText(const wxString& str)
1803 {
1804 wxString st = str ;
1805 wxMacConvertNewlines10To13( &st ) ;
1806
1807 long start , end , dummy ;
1808 GetSelection( &start , &dummy ) ;
1809 wxMacWindowClipper c( m_peer ) ;
1810 {
1811 wxMacEditHelper helper( m_txn ) ;
1812 SetTXNData( st , kTXNUseCurrentSelection, kTXNUseCurrentSelection ) ;
1813 }
1814 GetSelection( &dummy , &end ) ;
1815 // TODO SetStyle( start , end , GetDefaultStyle() ) ;
1816 }
1817
1818 void wxMacMLTEControl::Clear()
1819 {
1820 wxMacWindowClipper c( m_peer ) ;
1821 wxMacEditHelper st(m_txn) ;
1822 TXNSetSelection( m_txn , kTXNStartOffset , kTXNEndOffset ) ;
1823 TXNClear(m_txn);
1824 }
1825
1826 bool wxMacMLTEControl::CanUndo() const
1827 {
1828 return TXNCanUndo( m_txn , NULL ) ;
1829 }
1830
1831 void wxMacMLTEControl::Undo()
1832 {
1833 TXNUndo( m_txn ) ;
1834 }
1835
1836 bool wxMacMLTEControl::CanRedo() const
1837 {
1838 return TXNCanRedo( m_txn , NULL ) ;
1839 }
1840
1841 void wxMacMLTEControl::Redo()
1842 {
1843 TXNRedo( m_txn ) ;
1844 }
1845
1846 int wxMacMLTEControl::GetNumberOfLines() const
1847 {
1848 ItemCount lines = 0 ;
1849 TXNGetLineCount(m_txn, &lines ) ;
1850 return lines ;
1851 }
1852
1853 long wxMacMLTEControl::XYToPosition(long x, long y) const
1854 {
1855 Point curpt ;
1856
1857 wxTextPos lastpos = GetLastPosition() ;
1858
1859 // TODO find a better implementation : while we can get the
1860 // line metrics of a certain line, we don't get its starting
1861 // position, so it would probably be rather a binary search
1862 // for the start position
1863 long xpos = 0 ;
1864 long ypos = 0 ;
1865 int lastHeight = 0 ;
1866
1867 ItemCount n ;
1868 for ( n = 0 ; n <= (ItemCount) lastpos ; ++n )
1869 {
1870 if ( y == ypos && x == xpos )
1871 return n ;
1872
1873 TXNOffsetToPoint( m_txn , n , &curpt);
1874
1875 if ( curpt.v > lastHeight )
1876 {
1877 xpos = 0 ;
1878 if ( n > 0 )
1879 ++ypos ;
1880 lastHeight = curpt.v ;
1881 }
1882 else
1883 ++xpos ;
1884 }
1885 return 0 ;
1886 }
1887
1888 bool wxMacMLTEControl::PositionToXY(long pos, long *x, long *y) const
1889 {
1890 Point curpt ;
1891
1892 wxTextPos lastpos = GetLastPosition() ;
1893
1894 if ( y ) *y = 0 ;
1895 if ( x ) *x = 0 ;
1896
1897 if ( pos <= lastpos )
1898 {
1899 // TODO find a better implementation : while we can get the
1900 // line metrics of a certain line, we don't get its starting
1901 // position, so it would probably be rather a binary search
1902 // for the start position
1903 long xpos = 0 ;
1904 long ypos = 0 ;
1905 int lastHeight = 0 ;
1906
1907 ItemCount n ;
1908 for ( n = 0 ; n <= (ItemCount) pos ; ++n )
1909 {
1910 TXNOffsetToPoint(m_txn , n , &curpt);
1911
1912 if ( curpt.v > lastHeight )
1913 {
1914 xpos = 0 ;
1915 if ( n > 0 )
1916 ++ypos ;
1917 lastHeight = curpt.v ;
1918 }
1919 else
1920 ++xpos ;
1921 }
1922 if ( y ) *y = ypos ;
1923 if ( x ) *x = xpos ;
1924 }
1925
1926 return false ;
1927 }
1928
1929 void wxMacMLTEControl::ShowPosition( long pos )
1930 {
1931 #if TARGET_RT_MAC_MACHO && defined(AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER)
1932 {
1933 Point current ;
1934 Point desired ;
1935 TXNOffset selstart , selend ;
1936 TXNGetSelection( m_txn , &selstart , &selend) ;
1937 TXNOffsetToPoint( m_txn, selstart , &current);
1938 TXNOffsetToPoint( m_txn, pos , &desired);
1939 //TODO use HIPoints for 10.3 and above
1940 if ( (UInt32) TXNScroll != (UInt32) kUnresolvedCFragSymbolAddress )
1941 {
1942 OSErr theErr = noErr;
1943 SInt32 dv = desired.v - current.v ;
1944 SInt32 dh = desired.h - current.h ;
1945 TXNShowSelection( m_txn , true ) ;
1946 theErr = TXNScroll( m_txn, kTXNScrollUnitsInPixels , kTXNScrollUnitsInPixels , &dv , &dh );
1947 // there will be an error returned for classic mlte implementation when the control is
1948 // invisible, but HITextView works correctly, so we don't assert that one
1949 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
1950 }
1951 }
1952 #endif
1953 }
1954
1955 void wxMacMLTEControl::SetTXNData( const wxString& st , TXNOffset start , TXNOffset end )
1956 {
1957 #if wxUSE_UNICODE
1958 #if SIZEOF_WCHAR_T == 2
1959 size_t len = st.Len() ;
1960 TXNSetData( m_txn , kTXNUnicodeTextData, (void*)st.wc_str(), len * 2,
1961 start, end);
1962 #else
1963 wxMBConvUTF16 converter ;
1964 ByteCount byteBufferLen = converter.WC2MB( NULL , st.wc_str() , 0 ) ;
1965 UniChar *unibuf = (UniChar*) malloc(byteBufferLen) ;
1966 converter.WC2MB( (char*) unibuf , st.wc_str() , byteBufferLen ) ;
1967 TXNSetData( m_txn , kTXNUnicodeTextData, (void*)unibuf, byteBufferLen ,
1968 start, end);
1969 free( unibuf ) ;
1970 #endif
1971 #else
1972 wxCharBuffer text = st.mb_str(wxConvLocal) ;
1973 TXNSetData( m_txn , kTXNTextData, (void*)text.data(), strlen( text ) ,
1974 start, end);
1975 #endif
1976 }
1977
1978
1979 wxString wxMacMLTEControl::GetLineText(long lineNo) const
1980 {
1981 wxString line ;
1982
1983 if ( lineNo < GetNumberOfLines() )
1984 {
1985 long ypos = 0 ;
1986
1987 Fixed lineWidth,
1988 lineHeight,
1989 currentHeight = 0;
1990
1991 // get the first possible position in the control
1992 Point firstPoint;
1993 TXNOffsetToPoint(m_txn, 0, &firstPoint);
1994
1995 // Iterate through the lines until we reach the one we want,
1996 // adding to our current y pixel point position
1997 while (ypos < lineNo)
1998 {
1999 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2000 currentHeight += lineHeight;
2001 }
2002
2003 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2004 TXNOffset theOffset;
2005 TXNPointToOffset(m_txn, thePoint, &theOffset);
2006
2007 wxString content = GetStringValue() ;
2008 Point currentPoint = thePoint;
2009 while(thePoint.v == currentPoint.v && theOffset < content.length())
2010 {
2011 line += content[theOffset];
2012 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2013 }
2014 }
2015 return line ;
2016 }
2017
2018 int wxMacMLTEControl::GetLineLength(long lineNo) const
2019 {
2020 int theLength = 0;
2021
2022 if ( lineNo < GetNumberOfLines() )
2023 {
2024 long ypos = 0 ;
2025
2026 Fixed lineWidth,
2027 lineHeight,
2028 currentHeight = 0;
2029
2030 // get the first possible position in the control
2031 Point firstPoint;
2032 TXNOffsetToPoint(m_txn, 0, &firstPoint);
2033
2034 // Iterate through the lines until we reach the one we want,
2035 // adding to our current y pixel point position
2036 while (ypos < lineNo)
2037 {
2038 TXNGetLineMetrics(m_txn, ypos++, &lineWidth, &lineHeight);
2039 currentHeight += lineHeight;
2040 }
2041
2042 Point thePoint = { firstPoint.v + (currentHeight >> 16), firstPoint.h + (0) };
2043 TXNOffset theOffset;
2044 TXNPointToOffset(m_txn, thePoint, &theOffset);
2045
2046 wxString content = GetStringValue() ;
2047 Point currentPoint = thePoint;
2048 while(thePoint.v == currentPoint.v && theOffset < content.length())
2049 {
2050 ++theLength;
2051 TXNOffsetToPoint(m_txn, ++theOffset, &currentPoint);
2052 }
2053 }
2054 return theLength ;
2055 }
2056
2057
2058 // ----------------------------------------------------------------------------
2059 // MLTE control implementation (classic part)
2060 // ----------------------------------------------------------------------------
2061
2062 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2063 // has to live on. We have different problems coming from outdated implementations on the
2064 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2065 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2066 // no way out, therefore we are using our own implementation and our own scrollbars ....
2067
2068 #ifdef __WXMAC_OSX__
2069
2070 TXNScrollInfoUPP gTXNScrollInfoProc = NULL ;
2071 ControlActionUPP gTXNScrollActionProc = NULL ;
2072
2073 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc (SInt32 iValue, SInt32 iMaximumValue, TXNScrollBarOrientation
2074 iScrollBarOrientation, SInt32 iRefCon)
2075 {
2076 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) iRefCon ;
2077 SInt32 value = wxMax( iValue , 0 ) ;
2078 SInt32 maximum = wxMax( iMaximumValue , 0 ) ;
2079
2080 if ( iScrollBarOrientation == kTXNHorizontal )
2081 {
2082 if ( mlte->m_sbHorizontal )
2083 {
2084 SetControl32BitValue( mlte->m_sbHorizontal , value ) ;
2085 SetControl32BitMaximum( mlte->m_sbHorizontal , maximum ) ;
2086 mlte->m_lastHorizontalValue = value ;
2087 }
2088 }
2089 else if ( iScrollBarOrientation == kTXNVertical )
2090 {
2091 if ( mlte->m_sbVertical )
2092 {
2093 SetControl32BitValue( mlte->m_sbVertical , value ) ;
2094 SetControl32BitMaximum( mlte->m_sbVertical , maximum ) ;
2095 mlte->m_lastVerticalValue = value ;
2096 }
2097 }
2098 }
2099
2100 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef , ControlPartCode partCode )
2101 {
2102 OSStatus err ;
2103 wxMacMLTEClassicControl* mlte = (wxMacMLTEClassicControl*) GetControlReference( controlRef ) ;
2104 if ( mlte == NULL )
2105 return ;
2106
2107 if ( controlRef != mlte->m_sbVertical && controlRef != mlte->m_sbHorizontal )
2108 return ;
2109
2110 bool isHorizontal = ( controlRef == mlte->m_sbHorizontal ) ;
2111
2112 SInt32 minimum = 0 ;
2113 SInt32 maximum = GetControl32BitMaximum( controlRef ) ;
2114 SInt32 value = GetControl32BitValue( controlRef ) ;
2115 SInt32 delta = 0;
2116 switch ( partCode )
2117 {
2118 case kControlDownButtonPart :
2119 delta = 10 ;
2120 break ;
2121 case kControlUpButtonPart :
2122 delta = -10 ;
2123 break ;
2124 case kControlPageDownPart :
2125 delta = GetControlViewSize( controlRef ) ;
2126 break ;
2127 case kControlPageUpPart :
2128 delta = -GetControlViewSize( controlRef ) ;
2129 break ;
2130 case kControlIndicatorPart :
2131 delta = value -
2132 ( isHorizontal ? mlte->m_lastHorizontalValue : mlte->m_lastVerticalValue ) ;
2133 break ;
2134 default :
2135 break ;
2136 }
2137 if ( delta != 0 )
2138 {
2139 SInt32 newValue = value ;
2140
2141 if ( partCode != kControlIndicatorPart )
2142 {
2143 if( value + delta < minimum )
2144 delta = minimum - value ;
2145 if ( value + delta > maximum )
2146 delta = maximum - value ;
2147
2148 SetControl32BitValue( controlRef , value + delta ) ;
2149 newValue = value + delta ;
2150 }
2151
2152 SInt32 verticalDelta = isHorizontal ? 0 : delta ;
2153 SInt32 horizontalDelta = isHorizontal ? delta : 0 ;
2154
2155 err = TXNScroll( mlte->m_txn , kTXNScrollUnitsInPixels, kTXNScrollUnitsInPixels,
2156 &verticalDelta , &horizontalDelta );
2157
2158 if ( isHorizontal )
2159 mlte->m_lastHorizontalValue = newValue ;
2160 else
2161 mlte->m_lastVerticalValue = newValue ;
2162 }
2163 }
2164 #endif
2165
2166 // make correct activations
2167 void wxMacMLTEClassicControl::MacActivatePaneText(Boolean setActive)
2168 {
2169 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2170
2171 wxMacWindowClipper clipper( textctrl ) ;
2172 TXNActivate(m_txn, m_txnFrameID, setActive);
2173
2174 ControlRef controlFocus = 0 ;
2175 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2176 if ( controlFocus == m_controlRef )
2177 TXNFocus( m_txn, setActive);
2178 }
2179
2180 void wxMacMLTEClassicControl::MacFocusPaneText(Boolean setFocus)
2181 {
2182 TXNFocus( m_txn, setFocus);
2183 }
2184
2185 // guards against inappropriate redraw (hidden objects drawing onto window)
2186
2187 void wxMacMLTEClassicControl::MacSetObjectVisibility(Boolean vis)
2188 {
2189 ControlRef controlFocus = 0 ;
2190 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2191
2192 if ( controlFocus == m_controlRef && vis == false )
2193 {
2194 SetKeyboardFocus( m_txnWindow , m_controlRef , kControlFocusNoPart ) ;
2195 }
2196
2197 TXNControlTag iControlTags[1] = { kTXNVisibilityTag };
2198 TXNControlData iControlData[1] = { {(UInt32) false } };
2199
2200 verify_noerr( TXNGetTXNObjectControls( m_txn , 1,
2201 iControlTags, iControlData ) ) ;
2202
2203 if ( iControlData[0].uValue != vis )
2204 {
2205 iControlData[0].uValue = vis ;
2206 verify_noerr( TXNSetTXNObjectControls( m_txn, false , 1,
2207 iControlTags, iControlData )) ;
2208 }
2209 // we right now are always clipping as partial visibility (overlapped) visibility
2210 // is also a problem, if we run into further problems we might set the FrameBounds to an empty
2211 // rect here
2212 }
2213
2214 // make sure that the TXNObject is at the right position
2215
2216 void wxMacMLTEClassicControl::MacUpdatePosition()
2217 {
2218 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2219 if ( textctrl == NULL )
2220 return ;
2221
2222 Rect bounds ;
2223 UMAGetControlBoundsInWindowCoords(m_controlRef, &bounds);
2224
2225 wxRect visRect = textctrl->MacGetClippedClientRect() ;
2226 Rect visBounds = { visRect.y , visRect.x , visRect.y + visRect.height , visRect.x + visRect.width } ;
2227 int x , y ;
2228 x = y = 0 ;
2229 textctrl->MacWindowToRootWindow( &x , &y ) ;
2230 OffsetRect( &visBounds , x , y ) ;
2231
2232 if ( !EqualRect( &bounds , &m_txnControlBounds ) || !EqualRect( &visBounds , &m_txnVisBounds) )
2233 {
2234 m_txnControlBounds = bounds ;
2235 m_txnVisBounds = visBounds ;
2236 wxMacWindowClipper cl(textctrl) ;
2237
2238 #ifdef __WXMAC_OSX__
2239 bool isCompositing = textctrl->MacGetTopLevelWindow()->MacUsesCompositing() ;
2240 if ( m_sbHorizontal || m_sbVertical )
2241 {
2242 int w = bounds.right - bounds.left ;
2243 int h = bounds.bottom - bounds.top ;
2244
2245 if ( m_sbHorizontal )
2246 {
2247 Rect sbBounds ;
2248
2249 sbBounds.left = -1 ;
2250 sbBounds.top = h - 14 ;
2251 sbBounds.right = w + 1 ;
2252 sbBounds.bottom = h + 1 ;
2253
2254 if ( !isCompositing )
2255 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2256
2257 SetControlBounds( m_sbHorizontal , &sbBounds ) ;
2258 SetControlViewSize( m_sbHorizontal , w ) ;
2259 }
2260 if ( m_sbVertical )
2261 {
2262 Rect sbBounds ;
2263
2264 sbBounds.left = w - 14 ;
2265 sbBounds.top = -1 ;
2266 sbBounds.right = w + 1 ;
2267 sbBounds.bottom = m_sbHorizontal ? h - 14 : h + 1 ;
2268
2269 if ( !isCompositing )
2270 OffsetRect( &sbBounds , m_txnControlBounds.left , m_txnControlBounds.top ) ;
2271
2272 SetControlBounds( m_sbVertical , &sbBounds ) ;
2273 SetControlViewSize( m_sbVertical , h ) ;
2274 }
2275 }
2276
2277 Rect oldviewRect ;
2278 TXNLongRect olddestRect ;
2279 TXNGetRectBounds( m_txn , &oldviewRect , &olddestRect , NULL ) ;
2280
2281 Rect viewRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2282 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) , m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2283 TXNLongRect destRect = { m_txnControlBounds.top, m_txnControlBounds.left,
2284 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) , m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ) } ;
2285
2286 if ( olddestRect.right >= 10000 )
2287 destRect.right = destRect.left + 32000 ;
2288
2289 if ( olddestRect.bottom >= 0x20000000 )
2290 destRect.bottom = destRect.top + 0x40000000 ;
2291
2292 SectRect( &viewRect , &visBounds , &viewRect ) ;
2293 TXNSetRectBounds( m_txn , &viewRect , &destRect , true ) ;
2294 /*
2295 TXNSetFrameBounds( m_txn, m_txnControlBounds.top, m_txnControlBounds.left,
2296 m_txnControlBounds.bottom - ( m_sbHorizontal ? 14 : 0 ) , m_txnControlBounds.right - ( m_sbVertical ? 14 : 0 ), m_txnFrameID);
2297 */
2298 #else
2299
2300 TXNSetFrameBounds( m_txn, m_txnControlBounds.top, m_txnControlBounds.left,
2301 wxMax( m_txnControlBounds.bottom , m_txnControlBounds.top ) ,
2302 wxMax( m_txnControlBounds.right , m_txnControlBounds.left ) , m_txnFrameID);
2303 #endif
2304 // the SetFrameBounds method unter classic sometimes does not correctly scroll a selection into sight after a
2305 // movement, therefore we have to force it
2306
2307 // according to David Surovell this problem also sometimes occurs under OSX, so we use this as well
2308
2309 TXNLongRect textRect ;
2310 TXNGetRectBounds( m_txn , NULL , NULL , &textRect ) ;
2311 if ( textRect.left < m_txnControlBounds.left )
2312 {
2313 TXNShowSelection( m_txn , false ) ;
2314 }
2315 }
2316 }
2317
2318 void wxMacMLTEClassicControl::SetRect( Rect *r )
2319 {
2320 wxMacControl::SetRect( r ) ;
2321 MacUpdatePosition() ;
2322 }
2323
2324 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 thePart)
2325 {
2326 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2327 if ( textctrl == NULL )
2328 return ;
2329
2330 if ( textctrl->MacIsReallyShown() )
2331 {
2332 wxMacWindowClipper clipper( textctrl ) ;
2333 TXNDraw( m_txn , NULL ) ;
2334 }
2335 }
2336
2337 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
2338 {
2339 Point where = { y , x } ;
2340 ControlPartCode result;
2341
2342 result = 0;
2343 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2344 if ( textctrl == NULL )
2345 return 0 ;
2346
2347 if (textctrl->MacIsReallyShown() )
2348 {
2349 if (PtInRect(where, &m_txnControlBounds))
2350 result = kControlEditTextPart ;
2351 else
2352 {
2353 // sometimes we get the coords also in control local coordinates, therefore test again
2354 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2355 {
2356 int x = 0 , y = 0 ;
2357 textctrl->MacClientToRootWindow( &x , &y ) ;
2358 where.h += x ;
2359 where.v += y ;
2360 }
2361 if (PtInRect(where, &m_txnControlBounds))
2362 result = kControlEditTextPart ;
2363 else
2364 result = 0;
2365 }
2366 }
2367 return result;
2368 }
2369
2370 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x, wxInt16 y, void* actionProc )
2371 {
2372 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2373 if ( textctrl == NULL )
2374 return 0;
2375
2376 ControlPartCode partCodeResult = 0;
2377
2378 if (textctrl->MacIsReallyShown() )
2379 {
2380 Point startPt = { y ,x } ;
2381 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2382 if ( textctrl->MacGetTopLevelWindow()->MacUsesCompositing() )
2383 {
2384 int x = 0 , y = 0 ;
2385 textctrl->MacClientToRootWindow( &x , &y ) ;
2386 startPt.h += x ;
2387 startPt.v += y ;
2388 }
2389
2390 switch (MacControlUserPaneHitTestProc( startPt.h , startPt.v ))
2391 {
2392 case kControlEditTextPart :
2393 {
2394 wxMacWindowClipper clipper( textctrl ) ;
2395
2396 EventRecord rec ;
2397 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
2398 TXNClick( m_txn, &rec );
2399
2400 }
2401 break;
2402 }
2403 }
2404 return partCodeResult;
2405 }
2406
2407 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2408 {
2409 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2410 if ( textctrl == NULL )
2411 return ;
2412
2413 if (textctrl->MacIsReallyShown())
2414 {
2415 if (IsControlActive(m_controlRef))
2416 {
2417 Point mousep;
2418
2419 wxMacWindowClipper clipper( textctrl ) ;
2420 GetMouse(&mousep);
2421
2422 TXNIdle(m_txn);
2423
2424 if (PtInRect(mousep, &m_txnControlBounds))
2425 {
2426 RgnHandle theRgn;
2427 RectRgn((theRgn = NewRgn()), &m_txnControlBounds);
2428 TXNAdjustCursor(m_txn, theRgn);
2429 DisposeRgn(theRgn);
2430 }
2431 }
2432 }
2433 }
2434
2435 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
2436 {
2437 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2438 if ( textctrl == NULL )
2439 return 0;
2440
2441 wxMacWindowClipper clipper( textctrl ) ;
2442
2443 EventRecord ev ;
2444 memset( &ev , 0 , sizeof( ev ) ) ;
2445 ev.what = keyDown ;
2446 ev.modifiers = modifiers ;
2447 ev.message = (( keyCode << 8 ) & keyCodeMask ) + ( charCode & charCodeMask ) ;
2448 TXNKeyDown( m_txn , &ev);
2449
2450 return kControlEntireControl;
2451 }
2452
2453 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc( bool activating)
2454 {
2455 MacActivatePaneText( activating );
2456 }
2457
2458 wxInt16 wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action)
2459 {
2460 ControlPartCode focusResult;
2461
2462 focusResult = kControlFocusNoPart;
2463 wxTextCtrl* textctrl = (wxTextCtrl*) GetControlReference(m_controlRef);
2464 if ( textctrl == NULL )
2465 return 0;
2466
2467 wxMacWindowClipper clipper( textctrl ) ;
2468
2469 ControlRef controlFocus = 0 ;
2470 GetKeyboardFocus( m_txnWindow , &controlFocus ) ;
2471 bool wasFocused = ( controlFocus == m_controlRef ) ;
2472
2473 switch (action)
2474 {
2475 case kControlFocusPrevPart:
2476 case kControlFocusNextPart:
2477 MacFocusPaneText( ( !wasFocused));
2478 focusResult = (!wasFocused) ? (ControlPartCode) kControlEditTextPart : (ControlPartCode) kControlFocusNoPart;
2479 break;
2480
2481 case kControlFocusNoPart:
2482 default:
2483 MacFocusPaneText( false);
2484 focusResult = kControlFocusNoPart;
2485 break;
2486 }
2487
2488 return focusResult;
2489 }
2490
2491 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *info )
2492 {
2493 }
2494
2495 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl *wxPeer,
2496 const wxString& str,
2497 const wxPoint& pos,
2498 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
2499 {
2500 m_font = wxPeer->GetFont() ;
2501 m_windowStyle = style ;
2502 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2503
2504 short featurSet;
2505
2506 featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
2507 | kControlWantsActivate | kControlHandlesTracking // | kControlHasSpecialBackground
2508 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
2509
2510 verify_noerr( ::CreateUserPaneControl( MAC_WXHWND(wxPeer->GetParent()->MacGetTopLevelWindowRef()), &bounds, featurSet, &m_controlRef ) );
2511
2512 DoCreate();
2513
2514 AdjustCreationAttributes( *wxWHITE , true) ;
2515
2516 MacSetObjectVisibility( wxPeer->MacIsReallyShown() ) ;
2517
2518 {
2519 wxString st = str ;
2520 wxMacConvertNewlines10To13( &st ) ;
2521 wxMacWindowClipper clipper( m_peer ) ;
2522 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2523 TXNSetSelection( m_txn, 0, 0);
2524 }
2525 }
2526
2527 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2528 {
2529 TXNDeleteObject(m_txn);
2530 m_txn = NULL ;
2531 }
2532
2533 void wxMacMLTEClassicControl::VisibilityChanged(bool shown)
2534 {
2535 MacSetObjectVisibility( shown ) ;
2536 wxMacControl::VisibilityChanged( shown ) ;
2537 }
2538
2539 void wxMacMLTEClassicControl::SuperChangedPosition()
2540 {
2541 MacUpdatePosition() ;
2542 wxMacControl::SuperChangedPosition() ;
2543 }
2544
2545 #ifdef __WXMAC_OSX__
2546
2547 ControlUserPaneDrawUPP gTPDrawProc = NULL;
2548 ControlUserPaneHitTestUPP gTPHitProc = NULL;
2549 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
2550 ControlUserPaneIdleUPP gTPIdleProc = NULL;
2551 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
2552 ControlUserPaneActivateUPP gTPActivateProc = NULL;
2553 ControlUserPaneFocusUPP gTPFocusProc = NULL;
2554
2555 static pascal void wxMacControlUserPaneDrawProc(ControlRef control, SInt16 part)
2556 {
2557 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2558 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2559 if ( win )
2560 win->MacControlUserPaneDrawProc(part) ;
2561 }
2562
2563 static pascal ControlPartCode wxMacControlUserPaneHitTestProc(ControlRef control, Point where)
2564 {
2565 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2566 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2567 if ( win )
2568 return win->MacControlUserPaneHitTestProc(where.h , where.v) ;
2569 else
2570 return kControlNoPart ;
2571 }
2572
2573 static pascal ControlPartCode wxMacControlUserPaneTrackingProc(ControlRef control, Point startPt, ControlActionUPP actionProc)
2574 {
2575 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2576 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2577 if ( win )
2578 return win->MacControlUserPaneTrackingProc( startPt.h , startPt.v , (void*) actionProc) ;
2579 else
2580 return kControlNoPart ;
2581 }
2582
2583 static pascal void wxMacControlUserPaneIdleProc(ControlRef control)
2584 {
2585 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2586 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2587 if ( win )
2588 win->MacControlUserPaneIdleProc() ;
2589 }
2590
2591 static pascal ControlPartCode wxMacControlUserPaneKeyDownProc(ControlRef control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
2592 {
2593 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2594 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2595 if ( win )
2596 return win->MacControlUserPaneKeyDownProc(keyCode,charCode,modifiers) ;
2597 else
2598 return kControlNoPart ;
2599 }
2600
2601 static pascal void wxMacControlUserPaneActivateProc(ControlRef control, Boolean activating)
2602 {
2603 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2604 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2605 if ( win )
2606 win->MacControlUserPaneActivateProc(activating) ;
2607 }
2608
2609 static pascal ControlPartCode wxMacControlUserPaneFocusProc(ControlRef control, ControlFocusPart action)
2610 {
2611 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2612 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2613 if ( win )
2614 return win->MacControlUserPaneFocusProc(action) ;
2615 else
2616 return kControlNoPart ;
2617 }
2618
2619 /*
2620 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control, ControlBackgroundPtr info)
2621 {
2622 wxTextCtrl *textCtrl = wxDynamicCast( wxFindControlFromMacControl(control) , wxTextCtrl ) ;
2623 wxMacMLTEClassicControl * win = textCtrl ? (wxMacMLTEClassicControl*)(textCtrl->GetPeer()) : NULL ;
2624 if ( win )
2625 win->MacControlUserPaneBackgroundProc(info) ;
2626 }
2627 */
2628 #endif
2629
2630 // TXNRegisterScrollInfoProc
2631
2632 OSStatus wxMacMLTEClassicControl::DoCreate()
2633 {
2634 Rect bounds;
2635
2636 OSStatus err = noErr ;
2637
2638 /* set up our globals */
2639 #ifdef __WXMAC_OSX__
2640 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc);
2641 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc);
2642 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc);
2643 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc);
2644 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc);
2645 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc);
2646 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc);
2647
2648 if (gTXNScrollInfoProc == NULL ) gTXNScrollInfoProc = NewTXNScrollInfoUPP(TXNScrollInfoProc) ;
2649 if (gTXNScrollActionProc == NULL ) gTXNScrollActionProc = NewControlActionUPP(TXNScrollActionProc) ;
2650 #endif
2651
2652 /* set the initial settings for our private data */
2653
2654 m_txnWindow =GetControlOwner(m_controlRef);
2655 m_txnPort = (GrafPtr) GetWindowPort(m_txnWindow);
2656
2657 #ifdef __WXMAC_OSX__
2658 /* set up the user pane procedures */
2659 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
2660 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
2661 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
2662 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
2663 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
2664 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
2665 SetControlData(m_controlRef, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
2666 #endif
2667 /* calculate the rectangles used by the control */
2668 UMAGetControlBoundsInWindowCoords(m_controlRef, &bounds);
2669
2670 m_txnControlBounds = bounds ;
2671 m_txnVisBounds = bounds ;
2672
2673 CGrafPtr origPort = NULL ;
2674 GDHandle origDev = NULL ;
2675 GetGWorld( &origPort , &origDev ) ;
2676 SetPort(m_txnPort);
2677
2678 /* create the new edit field */
2679
2680 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( m_windowStyle ) ;
2681
2682 #ifdef __WXMAC_OSX__
2683
2684 // the scrollbars are not correctly embedded but are inserted at the root
2685 // this gives us problems as we have erratic redraws even over the structure
2686 // area
2687
2688 m_sbHorizontal = 0 ;
2689 m_sbVertical = 0 ;
2690 m_lastHorizontalValue = 0 ;
2691 m_lastVerticalValue = 0 ;
2692
2693 Rect sb = { 0 , 0 , 0 , 0 } ;
2694 if ( frameOptions & kTXNWantVScrollBarMask )
2695 {
2696 CreateScrollBarControl( m_txnWindow , &sb , 0 , 0 , 100 , 1 , true , gTXNScrollActionProc , &m_sbVertical ) ;
2697 SetControlReference( m_sbVertical , (SInt32) this ) ;
2698 SetControlAction( m_sbVertical, gTXNScrollActionProc );
2699 ShowControl( m_sbVertical ) ;
2700 EmbedControl( m_sbVertical , m_controlRef ) ;
2701 frameOptions &= ~kTXNWantVScrollBarMask ;
2702 }
2703 if ( frameOptions & kTXNWantHScrollBarMask )
2704 {
2705 CreateScrollBarControl( m_txnWindow , &sb , 0 , 0 , 100 , 1 , true , gTXNScrollActionProc , &m_sbHorizontal ) ;
2706 SetControlReference( m_sbHorizontal , (SInt32) this ) ;
2707 SetControlAction( m_sbHorizontal, gTXNScrollActionProc );
2708 ShowControl( m_sbHorizontal ) ;
2709 EmbedControl( m_sbHorizontal , m_controlRef ) ;
2710 frameOptions &= ~(kTXNWantHScrollBarMask | kTXNDrawGrowIconMask);
2711 }
2712
2713 #endif
2714
2715 verify_noerr(TXNNewObject(NULL, m_txnWindow , &bounds,
2716 frameOptions ,
2717 kTXNTextEditStyleFrameType,
2718 kTXNTextensionFile,
2719 kTXNSystemDefaultEncoding,
2720 &m_txn, &m_txnFrameID, NULL ) );
2721 /*
2722 TXNCarbonEventInfo cInfo ;
2723
2724 cInfo.useCarbonEvents = false ;
2725 cInfo.filler = 0 ;
2726 cInfo.flags = 0 ;
2727 cInfo.fDictionary = NULL ;
2728
2729 TXNControlTag iControlTags[] =
2730 {
2731 kTXNUseCarbonEvents ,
2732 };
2733 TXNControlData iControlData[] =
2734 {
2735 {(UInt32) &cInfo },
2736 };
2737
2738 int toptag = WXSIZEOF( iControlTags ) ;
2739
2740 verify_noerr( TXNSetTXNObjectControls( m_txn, false , toptag,
2741 iControlTags, iControlData )) ;
2742
2743 */
2744 #ifdef __WXMAC_OSX__
2745 TXNRegisterScrollInfoProc( m_txn, gTXNScrollInfoProc, (SInt32) this);
2746 #endif
2747
2748 SetGWorld( origPort , origDev ) ;
2749 return err;
2750 }
2751
2752 // ----------------------------------------------------------------------------
2753 // MLTE control implementation (OSX part)
2754 // ----------------------------------------------------------------------------
2755
2756 #if TARGET_API_MAC_OSX
2757
2758 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
2759
2760 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
2761 const wxString& str,
2762 const wxPoint& pos,
2763 const wxSize& size, long style ) : wxMacMLTEControl( wxPeer )
2764 {
2765 m_font = wxPeer->GetFont() ;
2766 m_windowStyle = style ;
2767 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
2768 wxString st = str ;
2769 wxMacConvertNewlines10To13( &st ) ;
2770
2771 HIRect hr = { { bounds.left , bounds.top} ,
2772 { bounds.right - bounds.left , bounds.bottom - bounds.top} } ;
2773
2774 m_scrollView = NULL ;
2775 TXNFrameOptions frameOptions = FrameOptionsFromWXStyle( style ) ;
2776 if ( frameOptions & (kTXNWantVScrollBarMask|kTXNWantHScrollBarMask) )
2777 {
2778 HIScrollViewCreate(( frameOptions & kTXNWantHScrollBarMask ? kHIScrollViewOptionsHorizScroll : 0) |
2779 ( frameOptions & kTXNWantVScrollBarMask ? kHIScrollViewOptionsVertScroll: 0 ) , &m_scrollView ) ;
2780
2781 HIViewSetFrame( m_scrollView, &hr );
2782 HIViewSetVisible( m_scrollView, true );
2783 }
2784
2785 m_textView = NULL ;
2786 HITextViewCreate( NULL , 0, frameOptions , &m_textView ) ;
2787 m_txn = HITextViewGetTXNObject( m_textView) ;
2788 HIViewSetVisible( m_textView , true ) ;
2789 if ( m_scrollView )
2790 {
2791 HIViewAddSubview( m_scrollView , m_textView ) ;
2792 m_controlRef = m_scrollView ;
2793 wxPeer->MacInstallEventHandler( (WXWidget) m_textView ) ;
2794 }
2795 else
2796 {
2797 HIViewSetFrame( m_textView, &hr );
2798 m_controlRef = m_textView ;
2799 }
2800
2801 AdjustCreationAttributes( *wxWHITE , true ) ;
2802
2803 wxMacWindowClipper c( m_peer ) ;
2804 SetTXNData( st , kTXNStartOffset, kTXNEndOffset ) ;
2805
2806 TXNSetSelection( m_txn, 0, 0);
2807 TXNShowSelection( m_txn, kTXNShowStart);
2808
2809 }
2810
2811 OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )
2812 {
2813 return SetKeyboardFocus( GetControlOwner( m_textView ) ,
2814 m_textView , focusPart ) ;
2815 }
2816
2817 bool wxMacMLTEHIViewControl::HasFocus() const
2818 {
2819 ControlRef control ;
2820 GetKeyboardFocus( GetUserFocusWindow() , &control ) ;
2821 return control == m_textView ;
2822 }
2823
2824 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
2825 #define kCGColorSpaceGenericRGB CFSTR("kCGColorSpaceGenericRGB")
2826 #endif
2827
2828 void wxMacMLTEHIViewControl::SetBackground( const wxBrush &brush )
2829 {
2830 wxMacMLTEControl::SetBackground( brush ) ;
2831 /*
2832 CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
2833 RGBColor col = MAC_WXCOLORREF(brush.GetColour().GetPixel()) ;
2834
2835 float component[4] ;
2836 component[0] = col.red / 65536.0 ;
2837 component[1] = col.green / 65536.0 ;
2838 component[2] = col.blue / 65536.0 ;
2839 component[3] = 1.0 ; // alpha
2840
2841 CGColorRef color = CGColorCreate (rgbSpace , component );
2842 HITextViewSetBackgroundColor( m_textView , color ) ;
2843 CGColorSpaceRelease( rgbSpace );
2844 */
2845 }
2846
2847 #endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
2848
2849
2850 #endif
2851
2852 #endif // wxUSE_TEXTCTRL