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