]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/textctrl.cpp
Made wxFileDialog PDA friendlier.
[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 MacRedrawControl() ;
187}
188
189// Clipboard operations
190void wxTextCtrl::Copy()
191{
192 if (CanCopy())
193 {
194 TEHandle teH ;
195 long size ;
196
197 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
198 TECopy( teH ) ;
199 ClearCurrentScrap();
200 TEToScrap() ;
201 }
202}
203
204void wxTextCtrl::Cut()
205{
206 if (CanCut())
207 {
208 TEHandle teH ;
209 long size ;
210
211 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
212 TECut( teH ) ;
213 ClearCurrentScrap();
214 TEToScrap() ;
215 // MacInvalidateControl() ;
216 }
217}
218
219void wxTextCtrl::Paste()
220{
221 if (CanPaste())
222 {
223 TEHandle teH ;
224 long size ;
225
226 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
227 TEFromScrap() ;
228 TEPaste( teH ) ;
229 MacRedrawControl() ;
230 }
231}
232
233bool wxTextCtrl::CanCopy() const
234{
235 // Can copy if there's a selection
236 long from, to;
237 GetSelection(& from, & to);
238 return (from != to);
239}
240
241bool wxTextCtrl::CanCut() const
242{
243 // Can cut if there's a selection
244 long from, to;
245 GetSelection(& from, & to);
246 return (from != to);
247}
248
249bool wxTextCtrl::CanPaste() const
250{
251 if (!IsEditable())
252 return FALSE;
253
254 long offset ;
255#if TARGET_CARBON
256 OSStatus err = noErr;
257 ScrapRef scrapRef;
258
259 err = GetCurrentScrap( &scrapRef );
260 if ( err != noTypeErr && err != memFullErr )
261 {
262 ScrapFlavorFlags flavorFlags;
263 Size byteCount;
264
265 if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr)
266 {
267 if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr)
268 {
269 return TRUE ;
270 }
271 }
272 }
273 return FALSE;
274
275#else
276 if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 )
277 {
278 return TRUE ;
279 }
280#endif
281 return FALSE ;
282}
283
284void wxTextCtrl::SetEditable(bool editable)
285{
286 if ( editable )
287 UMAActivateControl( (ControlHandle) m_macControl ) ;
288 else
289 UMADeactivateControl( (ControlHandle) m_macControl ) ;
290}
291
292void wxTextCtrl::SetInsertionPoint(long pos)
293{
294 SetSelection( pos , pos ) ;
295}
296
297void wxTextCtrl::SetInsertionPointEnd()
298{
299 long pos = GetLastPosition();
300 SetInsertionPoint(pos);
301}
302
303long wxTextCtrl::GetInsertionPoint() const
304{
305 ControlEditTextSelectionRec selection ;
306 TEHandle teH ;
307 long size ;
308
309 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
310// ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
311 return (**teH).selStart ;
312}
313
314long wxTextCtrl::GetLastPosition() const
315{
316 ControlEditTextSelectionRec selection ;
317 TEHandle teH ;
318 long size ;
319
320 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
321
322// ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
323 return (**teH).teLength ;
324}
325
326void wxTextCtrl::Replace(long from, long to, const wxString& value)
327{
328 TEHandle teH ;
329 long size ;
330
331 ControlEditTextSelectionRec selection ;
332
333 selection.selStart = from ;
334 selection.selEnd = to ;
335 ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
336 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
337 TESetSelect( from , to , teH ) ;
338 TEDelete( teH ) ;
339 TEInsert( value , value.Length() , teH ) ;
340 Refresh() ;
341}
342
343void wxTextCtrl::Remove(long from, long to)
344{
345 TEHandle teH ;
346 long size ;
347
348 ControlEditTextSelectionRec selection ;
349
350 selection.selStart = from ;
351 selection.selEnd = to ;
352 ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
353 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
354 TEDelete( teH ) ;
355 Refresh() ;
356}
357
358void wxTextCtrl::SetSelection(long from, long to)
359{
360 ControlEditTextSelectionRec selection ;
361 TEHandle teH ;
362 long size ;
363
364 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
365
366 selection.selStart = from ;
367 selection.selEnd = to ;
368
369 ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
370 TESetSelect( selection.selStart , selection.selEnd , teH ) ;
371}
372
373bool wxTextCtrl::LoadFile(const wxString& file)
374{
375 if ( wxTextCtrlBase::LoadFile(file) )
376 {
377 return TRUE;
378 }
379
380 return FALSE;
381}
382
383void wxTextCtrl::WriteText(const wxString& text)
384{
385 TEHandle teH ;
386 long size ;
387
388 memcpy( wxBuffer, text , text.Length() ) ;
389 wxBuffer[text.Length() ] = 0 ;
390// wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
391
392 ::GetControlData( (ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ;
393
394 TEInsert( wxBuffer , strlen( wxBuffer) , teH ) ;
395 Refresh() ;
396}
397
398void wxTextCtrl::AppendText(const wxString& text)
399{
400 SetInsertionPointEnd();
401 WriteText(text);
402}
403
404void wxTextCtrl::Clear()
405{
406 ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ;
407 Refresh() ;
408}
409
410bool wxTextCtrl::IsModified() const
411{
412 return TRUE;
413}
414
415bool wxTextCtrl::IsEditable() const
416{
417 return IsEnabled();
418}
419
420bool wxTextCtrl::AcceptsFocus() const
421{
422 // we don't want focus if we can't be edited
423 return IsEditable() && wxControl::AcceptsFocus();
424}
425
426wxSize wxTextCtrl::DoGetBestSize() const
427{
428 int wText = 100 ;
429
430 int hText ;
431 if ( UMAHasAppearance() )
432 hText = 13 ;
433 else
434 hText = 24 ;
435 hText += 2 * m_macHorizontalBorder ;
436/*
437 int cx, cy;
438 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
439
440 int wText = DEFAULT_ITEM_WIDTH;
441
442 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
443
444 return wxSize(wText, hText);
445*/
446 if ( m_windowStyle & wxTE_MULTILINE )
447 {
448 hText *= wxMin(GetNumberOfLines(), 5);
449 }
450 //else: for single line control everything is ok
451 return wxSize(wText, hText);
452}
453
454// ----------------------------------------------------------------------------
455// Undo/redo
456// ----------------------------------------------------------------------------
457
458void wxTextCtrl::Undo()
459{
460 if (CanUndo())
461 {
462 }
463}
464
465void wxTextCtrl::Redo()
466{
467 if (CanRedo())
468 {
469 }
470}
471
472bool wxTextCtrl::CanUndo() const
473{
474 return FALSE ;
475}
476
477bool wxTextCtrl::CanRedo() const
478{
479 return FALSE ;
480}
481
482// Makes 'unmodified'
483void wxTextCtrl::DiscardEdits()
484{
485 // TODO
486}
487
488int wxTextCtrl::GetNumberOfLines() const
489{
490 Size actualsize;
491 ::GetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ;
492
493 int count = 1;
494 for (int i = 0; i < actualsize; i++)
495 {
496 if (wxBuffer[i] == '\r') count++;
497 }
498
499 return count;
500}
501
502long wxTextCtrl::XYToPosition(long x, long y) const
503{
504 // TODO
505 return 0;
506}
507
508bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
509{
510 return FALSE ;
511}
512
513void wxTextCtrl::ShowPosition(long pos)
514{
515 // TODO
516}
517
518int wxTextCtrl::GetLineLength(long lineNo) const
519{
520 Size actualsize;
521 ::GetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ;
522
523 // Find line first
524 int count = 0;
525 for (int i = 0; i < actualsize; i++)
526 {
527 if (count == lineNo)
528 {
529 // Count chars in line then
530 count = 0;
531 for (int j = i; j < actualsize; j++)
532 {
533 count++;
534 if (wxBuffer[j] == '\r') return count;
535 }
536
537 return count;
538 }
539 if (wxBuffer[i] == '\r') count++;
540 }
541
542 return 0;
543}
544
545wxString wxTextCtrl::GetLineText(long lineNo) const
546{
547 Size actualsize;
548 ::GetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ;
549
550 // Find line first
551 int count = 0;
552 for (int i = 0; i < actualsize; i++)
553 {
554 if (count == lineNo)
555 {
556 // Add chars in line then
557 wxString tmp("");
558
559 for (int j = i; j < actualsize; j++)
560 {
561 if (wxBuffer[j] == '\r')
562 return tmp;
563
564 tmp += wxBuffer[j];
565 }
566
567 return tmp;
568 }
569 if (wxBuffer[i] == '\r') count++;
570 }
571
572 return wxString("");
573}
574
575/*
576 * Text item
577 */
578
579void wxTextCtrl::Command(wxCommandEvent & event)
580{
581 SetValue (event.GetString());
582 ProcessCommand (event);
583}
584
585void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
586{
587 // By default, load the first file into the text window.
588 if (event.GetNumberOfFiles() > 0)
589 {
590 LoadFile(event.GetFiles()[0]);
591 }
592}
593
594void wxTextCtrl::OnChar(wxKeyEvent& key_event)
595{
596 bool eat_key = FALSE;
597
598 switch ( key_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 event.SetString( GetValue() );
606 if ( GetEventHandler()->ProcessEvent(event) )
607 return;
608 }
609 if ( !(m_windowStyle & wxTE_MULTILINE) )
610 {
611 wxWindow *parent = GetParent();
612 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
613 parent = parent->GetParent() ;
614 }
615 if ( parent && parent->GetDefaultItem() )
616 {
617 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
618 wxButton);
619 if ( def && def->IsEnabled() )
620 {
621 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
622 event.SetEventObject(def);
623 def->Command(event);
624 return ;
625 }
626 }
627
628 // this will make wxWindows eat the ENTER key so that
629 // we actually prevent line wrapping in a single line
630 // text control
631 eat_key = TRUE;
632 }
633
634 break;
635
636 case WXK_TAB:
637 // always produce navigation event - even if we process TAB
638 // ourselves the fact that we got here means that the user code
639 // decided to skip processing of this TAB - probably to let it
640 // do its default job.
641 {
642 wxNavigationKeyEvent eventNav;
643 eventNav.SetDirection(!key_event.ShiftDown());
644 eventNav.SetWindowChange(key_event.ControlDown());
645 eventNav.SetEventObject(this);
646
647 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
648 return;
649
650 key_event.Skip() ;
651 return;
652 }
653 break;
654 }
655
656 EventRecord *ev = (EventRecord*) wxTheApp->MacGetCurrentEvent();
657 short keychar = short(ev->message & charCodeMask);
658 if (!eat_key)
659 {
660 short keycode = short(ev->message & keyCodeMask) >> 8 ;
661 ::HandleControlKey( (ControlHandle) m_macControl , keycode , keychar , ev->modifiers );
662 }
663 if ( keychar >= 0x20 ||
664 key_event.KeyCode() == WXK_RETURN ||
665 key_event.KeyCode() == WXK_DELETE ||
666 key_event.KeyCode() == WXK_BACK)
667 {
668 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
669 event.SetString( GetValue() ) ;
670 event.SetEventObject( this );
671 GetEventHandler()->ProcessEvent(event);
672 }
673}
674
675// ----------------------------------------------------------------------------
676// standard handlers for standard edit menu events
677// ----------------------------------------------------------------------------
678
679void wxTextCtrl::OnCut(wxCommandEvent& event)
680{
681 Cut();
682}
683
684void wxTextCtrl::OnCopy(wxCommandEvent& event)
685{
686 Copy();
687}
688
689void wxTextCtrl::OnPaste(wxCommandEvent& event)
690{
691 Paste();
692}
693
694void wxTextCtrl::OnUndo(wxCommandEvent& event)
695{
696 Undo();
697}
698
699void wxTextCtrl::OnRedo(wxCommandEvent& event)
700{
701 Redo();
702}
703
704void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
705{
706 event.Enable( CanCut() );
707}
708
709void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
710{
711 event.Enable( CanCopy() );
712}
713
714void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
715{
716 event.Enable( CanPaste() );
717}
718
719void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
720{
721 event.Enable( CanUndo() );
722}
723
724void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
725{
726 event.Enable( CanRedo() );
727}
728
729#else
730
731extern wxApp *wxTheApp ;
732// CS:We will replace the TextEdit by using the MultiLanguageTextEngine based on the following code written by apple
733
734/*
735 File: mUPControl.c
736
737 Description:
738 mUPControl implementation.
739
740 Copyright:
741