]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/textctrl.cpp
moved splines rendering code into wxDCBase
[wxWidgets.git] / src / mac / carbon / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "textctrl.h"
14 #endif
15
16 #ifdef __UNIX__
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #else
20 #include <stat.h>
21 #endif
22 #include <fstream.h>
23
24 #include "wx/app.h"
25 #include "wx/button.h"
26 #include "wx/panel.h"
27 #include "wx/textctrl.h"
28 #include "wx/notebook.h"
29 #include "wx/tabctrl.h"
30 #include "wx/settings.h"
31 #include "wx/filefn.h"
32 #include "wx/utils.h"
33
34 #if defined(__BORLANDC__) && !defined(__WIN32__)
35 #include <alloc.h>
36 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__WXMAC_X__)
37 #include <malloc.h>
38 #endif
39
40 #include "wx/mac/uma.h"
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
44
45 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
46 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
47 EVT_CHAR(wxTextCtrl::OnChar)
48 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
49 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
50 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
51 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
52 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
53
54 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
55 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
56 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
57 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
58 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
59 END_EVENT_TABLE()
60 #endif
61
62 // Text item
63 wxTextCtrl::wxTextCtrl()
64 {
65 }
66
67 const short kVerticalMargin = 2 ;
68 const short kHorizontalMargin = 2 ;
69
70 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
71 const wxString& st,
72 const wxPoint& pos,
73 const wxSize& size, long style,
74 const wxValidator& validator,
75 const wxString& name)
76 {
77 // base initialization
78 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
79 return FALSE;
80
81 wxSize mySize = size ;
82 if ( UMAHasAppearance() )
83 {
84 m_macHorizontalBorder = 5 ; // additional pixels around the real control
85 m_macVerticalBorder = 5 ;
86 }
87 else
88 {
89 m_macHorizontalBorder = 0 ; // additional pixels around the real control
90 m_macVerticalBorder = 0 ;
91 }
92
93
94 Rect bounds ;
95 Str255 title ;
96
97 if ( mySize.y == -1 )
98 {
99 if ( UMAHasAppearance() )
100 mySize.y = 13 ;
101 else
102 mySize.y = 24 ;
103
104 mySize.y += 2 * m_macVerticalBorder ;
105 }
106
107 MacPreControlCreate( parent , id , "" , pos , mySize ,style, validator , name , &bounds , title ) ;
108
109 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
110 ( style & wxTE_PASSWORD ) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ;
111 MacPostControlCreate() ;
112
113 wxString value ;
114
115 {
116 TEHandle teH ;
117 long size ;
118
119 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
120 (*teH)->lineHeight = -1 ;
121 }
122
123 if( wxApp::s_macDefaultEncodingIsPC )
124 value = wxMacMakeMacStringFromPC( st ) ;
125 else
126 value = st ;
127 UMASetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ;
128
129 return TRUE;
130 }
131
132 wxString wxTextCtrl::GetValue() const
133 {
134 Size actualsize;
135 UMAGetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ;
136 wxBuffer[actualsize] = 0 ;
137 if( wxApp::s_macDefaultEncodingIsPC )
138 return wxMacMakePCStringFromMac( wxBuffer ) ;
139 else
140 return wxString(wxBuffer);
141 }
142
143 void wxTextCtrl::GetSelection(long* from, long* to) const
144 {
145 ControlEditTextSelectionRec selection ;
146 TEHandle teH ;
147 long size ;
148
149 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
150
151 *from = (**teH).selStart;
152 *to = (**teH).selEnd;
153 }
154
155 void wxTextCtrl::SetValue(const wxString& st)
156 {
157 wxString value ;
158
159 if( wxApp::s_macDefaultEncodingIsPC )
160 value = wxMacMakeMacStringFromPC( st ) ;
161 else
162 value = st ;
163 UMASetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ;
164 WindowRef window = GetMacRootWindow() ;
165 if ( window )
166 {
167 wxWindow* win = wxFindWinFromMacWindow( window ) ;
168 if ( win )
169 {
170 wxMacDrawingHelper help( win ) ;
171 // the mac control manager always assumes to have the origin at 0,0
172 SetOrigin( 0 , 0 ) ;
173
174 bool hasTabBehind = false ;
175 wxWindow* parent = GetParent() ;
176 while ( parent )
177 {
178 if( parent->MacGetWindowData() )
179 {
180 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
181 break ;
182 }
183
184 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
185 {
186 if ( ((wxControl*)parent)->GetMacControl() )
187 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
188 break ;
189 }
190
191 parent = parent->GetParent() ;
192 }
193
194 UMADrawControl( m_macControl ) ;
195 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
196 wxDC::MacInvalidateSetup() ;
197 }
198 }
199 }
200
201 // Clipboard operations
202 void wxTextCtrl::Copy()
203 {
204 if (CanCopy())
205 {
206 TEHandle teH ;
207 long size ;
208
209 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
210 TECopy( teH ) ;
211 #if TARGET_CARBON
212 OSStatus err ;
213 err = ClearCurrentScrap( );
214 #else
215 OSErr err ;
216 err = ZeroScrap( );
217 #endif
218 TEToScrap() ;
219 }
220 }
221
222 void wxTextCtrl::Cut()
223 {
224 if (CanCut())
225 {
226 TEHandle teH ;
227 long size ;
228
229 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
230 TECut( teH ) ;
231 #if TARGET_CARBON
232 OSStatus err ;
233 err = ClearCurrentScrap( );
234 #else
235 OSErr err ;
236 err = ZeroScrap( );
237 #endif
238 TEToScrap() ;
239 // MacInvalidateControl() ;
240 }
241 }
242
243 void wxTextCtrl::Paste()
244 {
245 if (CanPaste())
246 {
247 TEHandle teH ;
248 long size ;
249
250 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
251 TEFromScrap() ;
252 TEPaste( teH ) ;
253 WindowRef window = GetMacRootWindow() ;
254 if ( window )
255 {
256 wxWindow* win = wxFindWinFromMacWindow( window ) ;
257 if ( win )
258 {
259 wxMacDrawingHelper help( win ) ;
260 // the mac control manager always assumes to have the origin at 0,0
261 SetOrigin( 0 , 0 ) ;
262
263 bool hasTabBehind = false ;
264 wxWindow* parent = GetParent() ;
265 while ( parent )
266 {
267 if( parent->MacGetWindowData() )
268 {
269 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
270 break ;
271 }
272
273 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
274 {
275 if ( ((wxControl*)parent)->GetMacControl() )
276 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
277 break ;
278 }
279
280 parent = parent->GetParent() ;
281 }
282
283 UMADrawControl( m_macControl ) ;
284 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
285 wxDC::MacInvalidateSetup() ;
286 }
287 }
288 }
289 }
290
291 bool wxTextCtrl::CanCopy() const
292 {
293 // Can copy if there's a selection
294 long from, to;
295 GetSelection(& from, & to);
296 return (from != to);
297 }
298
299 bool wxTextCtrl::CanCut() const
300 {
301 // Can cut if there's a selection
302 long from, to;
303 GetSelection(& from, & to);
304 return (from != to);
305 }
306
307 bool wxTextCtrl::CanPaste() const
308 {
309 if (!IsEditable())
310 return FALSE;
311
312 long offset ;
313 #if TARGET_CARBON
314 OSStatus err = noErr;
315 ScrapRef scrapRef;
316
317 err = GetCurrentScrap( &scrapRef );
318 if ( err != noTypeErr && err != memFullErr )
319 {
320 ScrapFlavorFlags flavorFlags;
321 Size byteCount;
322
323 if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr)
324 {
325 if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr)
326 {
327 return TRUE ;
328 }
329 }
330 }
331 return FALSE;
332
333 #else
334 if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 )
335 {
336 return TRUE ;
337 }
338 #endif
339 return FALSE ;
340 }
341
342 void wxTextCtrl::SetEditable(bool editable)
343 {
344 if ( editable )
345 UMAActivateControl( m_macControl ) ;
346 else
347 UMADeactivateControl( m_macControl ) ;
348 }
349
350 void wxTextCtrl::SetInsertionPoint(long pos)
351 {
352 SetSelection( pos , pos ) ;
353 }
354
355 void wxTextCtrl::SetInsertionPointEnd()
356 {
357 long pos = GetLastPosition();
358 SetInsertionPoint(pos);
359 }
360
361 long wxTextCtrl::GetInsertionPoint() const
362 {
363 ControlEditTextSelectionRec selection ;
364 TEHandle teH ;
365 long size ;
366
367 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
368 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
369 return (**teH).selStart ;
370 }
371
372 long wxTextCtrl::GetLastPosition() const
373 {
374 ControlEditTextSelectionRec selection ;
375 TEHandle teH ;
376 long size ;
377
378 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
379
380 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
381 return (**teH).teLength ;
382 }
383
384 void wxTextCtrl::Replace(long from, long to, const wxString& value)
385 {
386 TEHandle teH ;
387 long size ;
388
389 ControlEditTextSelectionRec selection ;
390
391 selection.selStart = from ;
392 selection.selEnd = to ;
393 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
394 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
395 TESetSelect( from , to , teH ) ;
396 TEDelete( teH ) ;
397 TEInsert( value , value.Length() , teH ) ;
398 // MacInvalidateControl() ;
399 }
400
401 void wxTextCtrl::Remove(long from, long to)
402 {
403 TEHandle teH ;
404 long size ;
405
406 ControlEditTextSelectionRec selection ;
407
408 selection.selStart = from ;
409 selection.selEnd = to ;
410 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
411 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
412 TEDelete( teH ) ;
413 // MacInvalidateControl() ;
414 }
415
416 void wxTextCtrl::SetSelection(long from, long to)
417 {
418 ControlEditTextSelectionRec selection ;
419 TEHandle teH ;
420 long size ;
421
422 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
423
424 selection.selStart = from ;
425 selection.selEnd = to ;
426
427 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
428 TESetSelect( selection.selStart , selection.selEnd , teH ) ;
429 }
430
431 bool wxTextCtrl::LoadFile(const wxString& file)
432 {
433 if ( wxTextCtrlBase::LoadFile(file) )
434 {
435 return TRUE;
436 }
437
438 return FALSE;
439 }
440
441 void wxTextCtrl::WriteText(const wxString& text)
442 {
443 TEHandle teH ;
444 long size ;
445
446 memcpy( wxBuffer, text , text.Length() ) ;
447 wxBuffer[text.Length() ] = 0 ;
448 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
449
450 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
451
452 TEInsert( wxBuffer , strlen( wxBuffer) , teH ) ;
453 Refresh() ;
454 }
455
456 void wxTextCtrl::AppendText(const wxString& text)
457 {
458 SetInsertionPointEnd();
459 WriteText(text);
460 }
461
462 void wxTextCtrl::Clear()
463 {
464 UMASetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ;
465 Refresh() ;
466 }
467
468 bool wxTextCtrl::IsModified() const
469 {
470 return TRUE;
471 }
472
473 bool wxTextCtrl::IsEditable() const
474 {
475 return IsEnabled();
476 }
477
478 bool wxTextCtrl::AcceptsFocus() const
479 {
480 // we don't want focus if we can't be edited
481 return IsEditable() && wxControl::AcceptsFocus();
482 }
483
484 wxSize wxTextCtrl::DoGetBestSize() const
485 {
486 int wText = 100 ;
487
488 int hText ;
489 if ( UMAHasAppearance() )
490 hText = 13 ;
491 else
492 hText = 24 ;
493 hText += 2 * m_macHorizontalBorder ;
494 /*
495 int cx, cy;
496 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
497
498 int wText = DEFAULT_ITEM_WIDTH;
499
500 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
501
502 return wxSize(wText, hText);
503 */
504 if ( m_windowStyle & wxTE_MULTILINE )
505 {
506 hText *= wxMin(GetNumberOfLines(), 5);
507 }
508 //else: for single line control everything is ok
509 return wxSize(wText, hText);
510 }
511
512 // ----------------------------------------------------------------------------
513 // Undo/redo
514 // ----------------------------------------------------------------------------
515
516 void wxTextCtrl::Undo()
517 {
518 if (CanUndo())
519 {
520 }
521 }
522
523 void wxTextCtrl::Redo()
524 {
525 if (CanRedo())
526 {
527 }
528 }
529
530 bool wxTextCtrl::CanUndo() const
531 {
532 return FALSE ;
533 }
534
535 bool wxTextCtrl::CanRedo() const
536 {
537 return FALSE ;
538 }
539
540 // Makes 'unmodified'
541 void wxTextCtrl::DiscardEdits()
542 {
543 // TODO
544 }
545
546 int wxTextCtrl::GetNumberOfLines() const
547 {
548 // TODO
549 return 0;
550 }
551
552 long wxTextCtrl::XYToPosition(long x, long y) const
553 {
554 // TODO
555 return 0;
556 }
557
558 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
559 {
560 return FALSE ;
561 }
562
563 void wxTextCtrl::ShowPosition(long pos)
564 {
565 // TODO
566 }
567
568 int wxTextCtrl::GetLineLength(long lineNo) const
569 {
570 return GetValue().Length();
571 }
572
573 wxString wxTextCtrl::GetLineText(long lineNo) const
574 {
575 return GetValue();
576 }
577
578 /*
579 * Text item
580 */
581
582 void wxTextCtrl::Command(wxCommandEvent & event)
583 {
584 SetValue (event.GetString());
585 ProcessCommand (event);
586 }
587
588 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
589 {
590 // By default, load the first file into the text window.
591 if (event.GetNumberOfFiles() > 0)
592 {
593 LoadFile(event.GetFiles()[0]);
594 }
595 }
596
597 void wxTextCtrl::OnChar(wxKeyEvent& event)
598 {
599 switch ( event.KeyCode() )
600 {
601 case WXK_RETURN:
602 if (m_windowStyle & wxPROCESS_ENTER)
603 {
604 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
605 event.SetEventObject( this );
606 if ( GetEventHandler()->ProcessEvent(event) )
607 return;
608 }
609 if ( !(m_windowStyle & wxTE_MULTILINE) )
610 {
611 wxWindow *parent = GetParent();
612 wxPanel *panel = wxDynamicCast(parent, wxPanel);
613 while ( parent != NULL && panel == NULL )
614 {
615 parent = parent->GetParent() ;
616 panel = wxDynamicCast(parent, wxPanel);
617 }
618 if ( panel && panel->GetDefaultItem() )
619 {
620 wxButton *def = panel->GetDefaultItem() ;
621 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
622 event.SetEventObject(def);
623 def->Command(event);
624 event.Skip() ;
625 return ;
626 }
627 }
628 //else: multiline controls need Enter for themselves
629
630 break;
631
632 case WXK_TAB:
633 // always produce navigation event - even if we process TAB
634 // ourselves the fact that we got here means that the user code
635 // decided to skip processing of this TAB - probably to let it
636 // do its default job.
637 {
638 wxNavigationKeyEvent eventNav;
639 eventNav.SetDirection(!event.ShiftDown());
640 eventNav.SetWindowChange(event.ControlDown());
641 eventNav.SetEventObject(this);
642
643 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
644 return;
645 event.Skip() ;
646 return ;
647 }
648 break;
649 }
650
651 EventRecord *ev = wxTheApp->MacGetCurrentEvent() ;
652 short keycode ;
653 short keychar ;
654 keychar = short(ev->message & charCodeMask);
655 keycode = short(ev->message & keyCodeMask) >> 8 ;
656 UMAHandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ;
657 if ( keychar >= 0x20 || event.KeyCode() == WXK_RETURN)
658 {
659 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
660 event.SetString( GetValue() ) ;
661 event.SetEventObject( this );
662 GetEventHandler()->ProcessEvent(event);
663 }
664
665 }
666
667 // ----------------------------------------------------------------------------
668 // standard handlers for standard edit menu events
669 // ----------------------------------------------------------------------------
670
671 void wxTextCtrl::OnCut(wxCommandEvent& event)
672 {
673 Cut();
674 }
675
676 void wxTextCtrl::OnCopy(wxCommandEvent& event)
677 {
678 Copy();
679 }
680
681 void wxTextCtrl::OnPaste(wxCommandEvent& event)
682 {
683 Paste();
684 }
685
686 void wxTextCtrl::OnUndo(wxCommandEvent& event)
687 {
688 Undo();
689 }
690
691 void wxTextCtrl::OnRedo(wxCommandEvent& event)
692 {
693 Redo();
694 }
695
696 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
697 {
698 event.Enable( CanCut() );
699 }
700
701 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
702 {
703 event.Enable( CanCopy() );
704 }
705
706 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
707 {
708 event.Enable( CanPaste() );
709 }
710
711 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
712 {
713 event.Enable( CanUndo() );
714 }
715
716 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
717 {
718 event.Enable( CanRedo() );
719 }
720