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