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