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