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