1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
17 #include <sys/types.h>
24 #include "wx/textctrl.h"
25 #include "wx/settings.h"
26 #include "wx/filefn.h"
29 #if defined(__BORLANDC__) && !defined(__WIN32__)
39 #include "wx/mac/uma.h"
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
44 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
45 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
46 EVT_CHAR(wxTextCtrl::OnChar
)
47 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
48 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
49 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
50 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
51 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
53 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
54 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
55 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
56 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
57 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
62 wxTextCtrl::wxTextCtrl()
66 const short kVerticalMargin
= 2 ;
67 const short kHorizontalMargin
= 2 ;
69 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxSize
& size
, long style
,
73 const wxValidator
& validator
,
76 // base initialization
77 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
80 wxSize mySize
= size
;
81 if ( UMAHasAppearance() )
83 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
84 m_macVerticalBorder
= 5 ;
88 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
89 m_macVerticalBorder
= 0 ;
98 if ( UMAHasAppearance() )
103 mySize
.y
+= 2 * m_macVerticalBorder
;
106 MacPreControlCreate( parent
, id
, "" , pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
108 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
109 ( style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
110 MacPostControlCreate() ;
118 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
119 (*teH
)->lineHeight
= -1 ;
122 if( wxApp::s_macDefaultEncodingIsPC
)
123 value
= wxMacMakeMacStringFromPC( st
) ;
126 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
131 wxString
wxTextCtrl::GetValue() const
134 UMAGetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 32767 , wxBuffer
, &actualsize
) ;
135 wxBuffer
[actualsize
] = 0 ;
136 if( wxApp::s_macDefaultEncodingIsPC
)
137 return wxMacMakePCStringFromMac( wxBuffer
) ;
139 return wxString(wxBuffer
);
142 void wxTextCtrl::GetSelection(long* from
, long* to
) const
144 ControlEditTextSelectionRec selection
;
148 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
150 *from
= (**teH
).selStart
;
151 *to
= (**teH
).selEnd
;
154 void wxTextCtrl::SetValue(const wxString
& st
)
158 if( wxApp::s_macDefaultEncodingIsPC
)
159 value
= wxMacMakeMacStringFromPC( st
) ;
162 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
163 UMADrawControl( m_macControl
) ;
166 // Clipboard operations
167 void wxTextCtrl::Copy()
174 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
178 err
= ClearCurrentScrap( );
187 void wxTextCtrl::Cut()
194 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
198 err
= ClearCurrentScrap( );
204 // MacInvalidateControl() ;
208 void wxTextCtrl::Paste()
215 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
218 UMADrawControl( m_macControl
) ;
222 bool wxTextCtrl::CanCopy() const
224 // Can copy if there's a selection
226 GetSelection(& from
, & to
);
230 bool wxTextCtrl::CanCut() const
232 // Can cut if there's a selection
234 GetSelection(& from
, & to
);
238 bool wxTextCtrl::CanPaste() const
245 OSStatus err
= noErr
;
248 err
= GetCurrentScrap( &scrapRef
);
249 if ( err
!= noTypeErr
&& err
!= memFullErr
)
251 ScrapFlavorFlags flavorFlags
;
254 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
256 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
265 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
273 void wxTextCtrl::SetEditable(bool editable
)
276 UMAActivateControl( m_macControl
) ;
278 UMADeactivateControl( m_macControl
) ;
281 void wxTextCtrl::SetInsertionPoint(long pos
)
283 SetSelection( pos
, pos
) ;
286 void wxTextCtrl::SetInsertionPointEnd()
288 long pos
= GetLastPosition();
289 SetInsertionPoint(pos
);
292 long wxTextCtrl::GetInsertionPoint() const
294 ControlEditTextSelectionRec selection
;
298 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
299 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
300 return (**teH
).selStart
;
303 long wxTextCtrl::GetLastPosition() const
305 ControlEditTextSelectionRec selection
;
309 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
311 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
312 return (**teH
).teLength
;
315 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
320 ControlEditTextSelectionRec selection
;
322 selection
.selStart
= from
;
323 selection
.selEnd
= to
;
324 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
325 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
326 TESetSelect( from
, to
, teH
) ;
328 TEInsert( value
, value
.Length() , teH
) ;
329 // MacInvalidateControl() ;
332 void wxTextCtrl::Remove(long from
, long to
)
337 ControlEditTextSelectionRec selection
;
339 selection
.selStart
= from
;
340 selection
.selEnd
= to
;
341 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
342 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
344 // MacInvalidateControl() ;
347 void wxTextCtrl::SetSelection(long from
, long to
)
349 ControlEditTextSelectionRec selection
;
353 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
355 selection
.selStart
= from
;
356 selection
.selEnd
= to
;
358 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
359 TESetSelect( selection
.selStart
, selection
.selEnd
, teH
) ;
362 bool wxTextCtrl::LoadFile(const wxString
& file
)
364 if ( wxTextCtrlBase::LoadFile(file
) )
372 void wxTextCtrl::WriteText(const wxString
& text
)
377 memcpy( wxBuffer
, text
, text
.Length() ) ;
378 wxBuffer
[text
.Length() ] = 0 ;
379 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
381 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
383 TEInsert( wxBuffer
, strlen( wxBuffer
) , teH
) ;
387 void wxTextCtrl::AppendText(const wxString
& text
)
389 SetInsertionPointEnd();
393 void wxTextCtrl::Clear()
395 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
399 bool wxTextCtrl::IsModified() const
404 bool wxTextCtrl::IsEditable() const
409 bool wxTextCtrl::AcceptsFocus() const
411 // we don't want focus if we can't be edited
412 return IsEditable() && wxControl::AcceptsFocus();
415 wxSize
wxTextCtrl::DoGetBestSize() const
420 if ( UMAHasAppearance() )
424 hText
+= 2 * m_macHorizontalBorder
;
427 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
429 int wText = DEFAULT_ITEM_WIDTH;
431 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
433 return wxSize(wText, hText);
435 if ( m_windowStyle
& wxTE_MULTILINE
)
437 hText
*= wxMin(GetNumberOfLines(), 5);
439 //else: for single line control everything is ok
440 return wxSize(wText
, hText
);
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 void wxTextCtrl::Undo()
454 void wxTextCtrl::Redo()
461 bool wxTextCtrl::CanUndo() const
466 bool wxTextCtrl::CanRedo() const
471 // Makes 'unmodified'
472 void wxTextCtrl::DiscardEdits()
477 int wxTextCtrl::GetNumberOfLines() const
483 long wxTextCtrl::XYToPosition(long x
, long y
) const
489 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
494 void wxTextCtrl::ShowPosition(long pos
)
499 int wxTextCtrl::GetLineLength(long lineNo
) const
501 return GetValue().Length();
504 wxString
wxTextCtrl::GetLineText(long lineNo
) const
513 void wxTextCtrl::Command(wxCommandEvent
& event
)
515 SetValue (event
.GetString());
516 ProcessCommand (event
);
519 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
521 // By default, load the first file into the text window.
522 if (event
.GetNumberOfFiles() > 0)
524 LoadFile(event
.GetFiles()[0]);
528 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
530 switch ( event
.KeyCode() )
533 if (m_windowStyle
& wxPROCESS_ENTER
)
535 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
536 event
.SetEventObject( this );
537 if ( GetEventHandler()->ProcessEvent(event
) )
540 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
542 wxWindow
*parent
= GetParent();
543 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
544 while ( parent
!= NULL
&& panel
== NULL
)
546 parent
= parent
->GetParent() ;
547 panel
= wxDynamicCast(parent
, wxPanel
);
549 if ( panel
&& panel
->GetDefaultItem() )
551 wxButton
*def
= panel
->GetDefaultItem() ;
552 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
553 event
.SetEventObject(def
);
559 //else: multiline controls need Enter for themselves
564 // always produce navigation event - even if we process TAB
565 // ourselves the fact that we got here means that the user code
566 // decided to skip processing of this TAB - probably to let it
567 // do its default job.
569 wxNavigationKeyEvent eventNav
;
570 eventNav
.SetDirection(!event
.ShiftDown());
571 eventNav
.SetWindowChange(event
.ControlDown());
572 eventNav
.SetEventObject(this);
574 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
582 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
585 keychar
= short(ev
->message
& charCodeMask
);
586 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
587 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
588 if ( keychar
>= 0x20 || event
.KeyCode() == WXK_RETURN
)
590 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
591 event
.SetString( GetValue() ) ;
592 event
.SetEventObject( this );
593 GetEventHandler()->ProcessEvent(event
);
598 // ----------------------------------------------------------------------------
599 // standard handlers for standard edit menu events
600 // ----------------------------------------------------------------------------
602 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
607 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
612 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
617 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
622 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
627 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
629 event
.Enable( CanCut() );
632 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
634 event
.Enable( CanCopy() );
637 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
639 event
.Enable( CanPaste() );
642 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
644 event
.Enable( CanUndo() );
647 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
649 event
.Enable( CanRedo() );