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