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