]> git.saurik.com Git - wxWidgets.git/blob - src/mac/textctrl.cpp
warning fix
[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" , false , 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 }
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 }
286 }
287 }
288 }
289
290 bool wxTextCtrl::CanCopy() const
291 {
292 // Can copy if there's a selection
293 long from, to;
294 GetSelection(& from, & to);
295 return (from != to);
296 }
297
298 bool wxTextCtrl::CanCut() const
299 {
300 // Can cut if there's a selection
301 long from, to;
302 GetSelection(& from, & to);
303 return (from != to);
304 }
305
306 bool wxTextCtrl::CanPaste() const
307 {
308 if (!IsEditable())
309 return FALSE;
310
311 long offset ;
312 #if TARGET_CARBON
313 OSStatus err = noErr;
314 ScrapRef scrapRef;
315
316 err = GetCurrentScrap( &scrapRef );
317 if ( err != noTypeErr && err != memFullErr )
318 {
319 ScrapFlavorFlags flavorFlags;
320 Size byteCount;
321
322 if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr)
323 {
324 if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr)
325 {
326 return TRUE ;
327 }
328 }
329 }
330 return FALSE;
331
332 #else
333 if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 )
334 {
335 return TRUE ;
336 }
337 #endif
338 return FALSE ;
339 }
340
341 void wxTextCtrl::SetEditable(bool editable)
342 {
343 if ( editable )
344 UMAActivateControl( m_macControl ) ;
345 else
346 UMADeactivateControl( m_macControl ) ;
347 }
348
349 void wxTextCtrl::SetInsertionPoint(long pos)
350 {
351 SetSelection( pos , pos ) ;
352 }
353
354 void wxTextCtrl::SetInsertionPointEnd()
355 {
356 long pos = GetLastPosition();
357 SetInsertionPoint(pos);
358 }
359
360 long wxTextCtrl::GetInsertionPoint() const
361 {
362 ControlEditTextSelectionRec selection ;
363 TEHandle teH ;
364 long size ;
365
366 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
367 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
368 return (**teH).selStart ;
369 }
370
371 long wxTextCtrl::GetLastPosition() const
372 {
373 ControlEditTextSelectionRec selection ;
374 TEHandle teH ;
375 long size ;
376
377 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
378
379 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
380 return (**teH).teLength ;
381 }
382
383 void wxTextCtrl::Replace(long from, long to, const wxString& value)
384 {
385 TEHandle teH ;
386 long size ;
387
388 ControlEditTextSelectionRec selection ;
389
390 selection.selStart = from ;
391 selection.selEnd = to ;
392 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
393 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
394 TESetSelect( from , to , teH ) ;
395 TEDelete( teH ) ;
396 TEInsert( value , value.Length() , teH ) ;
397 Refresh() ;
398 }
399
400 void wxTextCtrl::Remove(long from, long to)
401 {
402 TEHandle teH ;
403 long size ;
404
405 ControlEditTextSelectionRec selection ;
406
407 selection.selStart = from ;
408 selection.selEnd = to ;
409 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
410 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
411 TEDelete( teH ) ;
412 Refresh() ;
413 }
414
415 void wxTextCtrl::SetSelection(long from, long to)
416 {
417 ControlEditTextSelectionRec selection ;
418 TEHandle teH ;
419 long size ;
420
421 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
422
423 selection.selStart = from ;
424 selection.selEnd = to ;
425
426 UMASetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
427 TESetSelect( selection.selStart , selection.selEnd , teH ) ;
428 }
429
430 bool wxTextCtrl::LoadFile(const wxString& file)
431 {
432 if ( wxTextCtrlBase::LoadFile(file) )
433 {
434 return TRUE;
435 }
436
437 return FALSE;
438 }
439
440 void wxTextCtrl::WriteText(const wxString& text)
441 {
442 TEHandle teH ;
443 long size ;
444
445 memcpy( wxBuffer, text , text.Length() ) ;
446 wxBuffer[text.Length() ] = 0 ;
447 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
448
449 UMAGetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
450
451 TEInsert( wxBuffer , strlen( wxBuffer) , teH ) ;
452 Refresh() ;
453 }
454
455 void wxTextCtrl::AppendText(const wxString& text)
456 {
457 SetInsertionPointEnd();
458 WriteText(text);
459 }
460
461 void wxTextCtrl::Clear()
462 {
463 UMASetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ;
464 Refresh() ;
465 }
466
467 bool wxTextCtrl::IsModified() const
468 {
469 return TRUE;
470 }
471
472 bool wxTextCtrl::IsEditable() const
473 {
474 return IsEnabled();
475 }
476
477 bool wxTextCtrl::AcceptsFocus() const
478 {
479 // we don't want focus if we can't be edited
480 return IsEditable() && wxControl::AcceptsFocus();
481 }
482
483 wxSize wxTextCtrl::DoGetBestSize() const
484 {
485 int wText = 100 ;
486
487 int hText ;
488 if ( UMAHasAppearance() )
489 hText = 13 ;
490 else
491 hText = 24 ;
492 hText += 2 * m_macHorizontalBorder ;
493 /*
494 int cx, cy;
495 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
496
497 int wText = DEFAULT_ITEM_WIDTH;
498
499 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
500
501 return wxSize(wText, hText);
502 */
503 if ( m_windowStyle & wxTE_MULTILINE )
504 {
505 hText *= wxMin(GetNumberOfLines(), 5);
506 }
507 //else: for single line control everything is ok
508 return wxSize(wText, hText);
509 }
510
511 // ----------------------------------------------------------------------------
512 // Undo/redo
513 // ----------------------------------------------------------------------------
514
515 void wxTextCtrl::Undo()
516 {
517 if (CanUndo())
518 {
519 }
520 }
521
522 void wxTextCtrl::Redo()
523 {
524 if (CanRedo())
525 {
526 }
527 }
528
529 bool wxTextCtrl::CanUndo() const
530 {
531 return FALSE ;
532 }
533
534 bool wxTextCtrl::CanRedo() const
535 {
536 return FALSE ;
537 }
538
539 // Makes 'unmodified'
540 void wxTextCtrl::DiscardEdits()
541 {
542 // TODO
543 }
544
545 int wxTextCtrl::GetNumberOfLines() const
546 {
547 // TODO
548 return 0;
549 }
550
551 long wxTextCtrl::XYToPosition(long x, long y) const
552 {
553 // TODO
554 return 0;
555 }
556
557 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
558 {
559 return FALSE ;
560 }
561
562 void wxTextCtrl::ShowPosition(long pos)
563 {
564 // TODO
565 }
566
567 int wxTextCtrl::GetLineLength(long lineNo) const
568 {
569 return GetValue().Length();
570 }
571
572 wxString wxTextCtrl::GetLineText(long lineNo) const
573 {
574 return GetValue();
575 }
576
577 /*
578 * Text item
579 */
580
581 void wxTextCtrl::Command(wxCommandEvent & event)
582 {
583 SetValue (event.GetString());
584 ProcessCommand (event);
585 }
586
587 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
588 {
589 // By default, load the first file into the text window.
590 if (event.GetNumberOfFiles() > 0)
591 {
592 LoadFile(event.GetFiles()[0]);
593 }
594 }
595
596 void wxTextCtrl::OnChar(wxKeyEvent& event)
597 {
598 switch ( event.KeyCode() )
599 {
600 case WXK_RETURN:
601 if (m_windowStyle & wxPROCESS_ENTER)
602 {
603 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
604 event.SetEventObject( this );
605 if ( GetEventHandler()->ProcessEvent(event) )
606 return;
607 }
608 if ( !(m_windowStyle & wxTE_MULTILINE) )
609 {
610 wxWindow *parent = GetParent();
611 wxPanel *panel = wxDynamicCast(parent, wxPanel);
612 while ( parent != NULL && panel == NULL )
613 {
614 parent = parent->GetParent() ;
615 panel = wxDynamicCast(parent, wxPanel);
616 }
617 if ( panel && panel->GetDefaultItem() )
618 {
619 wxButton *def = panel->GetDefaultItem() ;
620 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
621 event.SetEventObject(def);
622 def->Command(event);
623 event.Skip() ;
624 return ;
625 }
626 }
627 //else: multiline controls need Enter for themselves
628
629 break;
630
631 case WXK_TAB:
632 // always produce navigation event - even if we process TAB
633 // ourselves the fact that we got here means that the user code
634 // decided to skip processing of this TAB - probably to let it
635 // do its default job.
636 {
637 wxNavigationKeyEvent eventNav;
638 eventNav.SetDirection(!event.ShiftDown());
639 eventNav.SetWindowChange(event.ControlDown());
640 eventNav.SetEventObject(this);
641
642 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
643 return;
644 event.Skip() ;
645 return ;
646 }
647 break;
648 }
649
650 EventRecord *ev = wxTheApp->MacGetCurrentEvent() ;
651 short keycode ;
652 short keychar ;
653 keychar = short(ev->message & charCodeMask);
654 keycode = short(ev->message & keyCodeMask) >> 8 ;
655 UMAHandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ;
656 if ( keychar >= 0x20 || event.KeyCode() == WXK_RETURN)
657 {
658 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
659 event.SetString( GetValue() ) ;
660 event.SetEventObject( this );
661 GetEventHandler()->ProcessEvent(event);
662 }
663
664 }
665
666 // ----------------------------------------------------------------------------
667 // standard handlers for standard edit menu events
668 // ----------------------------------------------------------------------------
669
670 void wxTextCtrl::OnCut(wxCommandEvent& event)
671 {
672 Cut();
673 }
674
675 void wxTextCtrl::OnCopy(wxCommandEvent& event)
676 {
677 Copy();
678 }
679
680 void wxTextCtrl::OnPaste(wxCommandEvent& event)
681 {
682 Paste();
683 }
684
685 void wxTextCtrl::OnUndo(wxCommandEvent& event)
686 {
687 Undo();
688 }
689
690 void wxTextCtrl::OnRedo(wxCommandEvent& event)
691 {
692 Redo();
693 }
694
695 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
696 {
697 event.Enable( CanCut() );
698 }
699
700 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
701 {
702 event.Enable( CanCopy() );
703 }
704
705 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
706 {
707 event.Enable( CanPaste() );
708 }
709
710 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
711 {
712 event.Enable( CanUndo() );
713 }
714
715 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
716 {
717 event.Enable( CanRedo() );
718 }
719