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