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