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