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