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