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