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() ;
114 if( wxApp::s_macDefaultEncodingIsPC
)
115 value
= wxMacMakeMacStringFromPC( st
) ;
118 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
123 wxString
wxTextCtrl::GetValue() const
126 UMAGetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 32767 , wxBuffer
, &actualsize
) ;
127 wxBuffer
[actualsize
] = 0 ;
128 if( wxApp::s_macDefaultEncodingIsPC
)
129 return wxMacMakePCStringFromMac( wxBuffer
) ;
131 return wxString(wxBuffer
);
134 void wxTextCtrl::GetSelection(long* from
, long* to
) const
136 ControlEditTextSelectionRec selection
;
140 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
142 *from
= (**teH
).selStart
;
143 *to
= (**teH
).selEnd
;
146 void wxTextCtrl::SetValue(const wxString
& st
)
150 if( wxApp::s_macDefaultEncodingIsPC
)
151 value
= wxMacMakeMacStringFromPC( st
) ;
154 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
156 // MacInvalidateControl() ;
159 // Clipboard operations
160 void wxTextCtrl::Copy()
167 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
171 err
= ClearCurrentScrap( );
180 void wxTextCtrl::Cut()
187 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
191 err
= ClearCurrentScrap( );
197 // MacInvalidateControl() ;
201 void wxTextCtrl::Paste()
208 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
211 // MacInvalidateControl() ;
215 bool wxTextCtrl::CanCopy() const
217 // Can copy if there's a selection
219 GetSelection(& from
, & to
);
223 bool wxTextCtrl::CanCut() const
225 // Can cut if there's a selection
227 GetSelection(& from
, & to
);
231 bool wxTextCtrl::CanPaste() const
238 OSStatus err
= noErr
;
241 err
= GetCurrentScrap( &scrapRef
);
242 if ( err
!= noTypeErr
&& err
!= memFullErr
)
244 ScrapFlavorFlags flavorFlags
;
247 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
249 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
258 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
266 void wxTextCtrl::SetEditable(bool editable
)
269 UMAActivateControl( m_macControl
) ;
271 UMADeactivateControl( m_macControl
) ;
274 void wxTextCtrl::SetInsertionPoint(long pos
)
276 SetSelection( pos
, pos
) ;
279 void wxTextCtrl::SetInsertionPointEnd()
281 long pos
= GetLastPosition();
282 SetInsertionPoint(pos
);
285 long wxTextCtrl::GetInsertionPoint() const
287 ControlEditTextSelectionRec selection
;
291 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
292 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
293 return (**teH
).selStart
;
296 long wxTextCtrl::GetLastPosition() const
298 ControlEditTextSelectionRec selection
;
302 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
304 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
305 return (**teH
).teLength
;
308 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
313 ControlEditTextSelectionRec selection
;
315 selection
.selStart
= from
;
316 selection
.selEnd
= to
;
317 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
318 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
319 TESetSelect( from
, to
, teH
) ;
321 TEInsert( value
, value
.Length() , teH
) ;
322 // MacInvalidateControl() ;
325 void wxTextCtrl::Remove(long from
, long to
)
330 ControlEditTextSelectionRec selection
;
332 selection
.selStart
= from
;
333 selection
.selEnd
= to
;
334 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
335 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
337 // MacInvalidateControl() ;
340 void wxTextCtrl::SetSelection(long from
, long to
)
342 ControlEditTextSelectionRec selection
;
346 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
348 selection
.selStart
= from
;
349 selection
.selEnd
= to
;
351 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
352 TESetSelect( selection
.selStart
, selection
.selEnd
, teH
) ;
355 bool wxTextCtrl::LoadFile(const wxString
& file
)
357 if ( wxTextCtrlBase::LoadFile(file
) )
365 void wxTextCtrl::WriteText(const wxString
& text
)
370 memcpy( wxBuffer
, text
, text
.Length() ) ;
371 wxBuffer
[text
.Length() ] = 0 ;
372 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
374 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
376 TEInsert( wxBuffer
, strlen( wxBuffer
) , teH
) ;
380 void wxTextCtrl::AppendText(const wxString
& text
)
382 SetInsertionPointEnd();
386 void wxTextCtrl::Clear()
388 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
392 bool wxTextCtrl::IsModified() const
397 bool wxTextCtrl::IsEditable() const
402 bool wxTextCtrl::AcceptsFocus() const
404 // we don't want focus if we can't be edited
405 return IsEditable() && wxControl::AcceptsFocus();
408 wxSize
wxTextCtrl::DoGetBestSize() const
413 if ( UMAHasAppearance() )
417 hText
+= 2 * m_macHorizontalBorder
;
420 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
422 int wText = DEFAULT_ITEM_WIDTH;
424 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
426 return wxSize(wText, hText);
428 if ( m_windowStyle
& wxTE_MULTILINE
)
430 hText
*= wxMin(GetNumberOfLines(), 5);
432 //else: for single line control everything is ok
433 return wxSize(wText
, hText
);
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 void wxTextCtrl::Undo()
447 void wxTextCtrl::Redo()
454 bool wxTextCtrl::CanUndo() const
459 bool wxTextCtrl::CanRedo() const
464 // Makes 'unmodified'
465 void wxTextCtrl::DiscardEdits()
470 int wxTextCtrl::GetNumberOfLines() const
476 long wxTextCtrl::XYToPosition(long x
, long y
) const
482 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
487 void wxTextCtrl::ShowPosition(long pos
)
492 int wxTextCtrl::GetLineLength(long lineNo
) const
494 return GetValue().Length();
497 wxString
wxTextCtrl::GetLineText(long lineNo
) const
506 void wxTextCtrl::Command(wxCommandEvent
& event
)
508 SetValue (event
.GetString());
509 ProcessCommand (event
);
512 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
514 // By default, load the first file into the text window.
515 if (event
.GetNumberOfFiles() > 0)
517 LoadFile(event
.GetFiles()[0]);
521 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
523 switch ( event
.KeyCode() )
526 if (m_windowStyle
& wxPROCESS_ENTER
)
528 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
529 event
.SetEventObject( this );
530 if ( GetEventHandler()->ProcessEvent(event
) )
533 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
535 wxWindow
*parent
= GetParent();
536 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
537 while ( parent
!= NULL
&& panel
== NULL
)
539 parent
= parent
->GetParent() ;
540 panel
= wxDynamicCast(parent
, wxPanel
);
542 if ( panel
&& panel
->GetDefaultItem() )
544 wxButton
*def
= panel
->GetDefaultItem() ;
545 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
546 event
.SetEventObject(def
);
552 //else: multiline controls need Enter for themselves
557 // always produce navigation event - even if we process TAB
558 // ourselves the fact that we got here means that the user code
559 // decided to skip processing of this TAB - probably to let it
560 // do its default job.
562 wxNavigationKeyEvent eventNav
;
563 eventNav
.SetDirection(!event
.ShiftDown());
564 eventNav
.SetWindowChange(event
.ControlDown());
565 eventNav
.SetEventObject(this);
567 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
575 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
578 keychar
= short(ev
->message
& charCodeMask
);
579 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
580 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
581 if ( keychar
>= 0x20 || event
.KeyCode() == WXK_RETURN
)
583 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
584 event
.SetString( GetValue() ) ;
585 event
.SetEventObject( this );
586 GetEventHandler()->ProcessEvent(event
);
591 // ----------------------------------------------------------------------------
592 // standard handlers for standard edit menu events
593 // ----------------------------------------------------------------------------
595 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
600 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
605 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
610 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
615 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
620 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
622 event
.Enable( CanCut() );
625 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
627 event
.Enable( CanCopy() );
630 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
632 event
.Enable( CanPaste() );
635 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
637 event
.Enable( CanUndo() );
640 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
642 event
.Enable( CanRedo() );