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