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