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