1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
17 #include <sys/types.h>
25 #include "wx/button.h"
27 #include "wx/textctrl.h"
28 #include "wx/settings.h"
29 #include "wx/filefn.h"
32 #if defined(__BORLANDC__) && !defined(__WIN32__)
34 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__WXMAC_X__)
38 #include "wx/mac/uma.h"
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
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
)
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
)
61 wxTextCtrl::wxTextCtrl()
65 const short kVerticalMargin
= 2 ;
66 const short kHorizontalMargin
= 2 ;
68 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxSize
& size
, long style
,
72 const wxValidator
& validator
,
75 // base initialization
76 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
79 wxSize mySize
= size
;
80 if ( UMAHasAppearance() )
82 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
83 m_macVerticalBorder
= 5 ;
87 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
88 m_macVerticalBorder
= 0 ;
97 if ( UMAHasAppearance() )
102 mySize
.y
+= 2 * m_macVerticalBorder
;
105 MacPreControlCreate( parent
, id
, "" , pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
107 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
108 ( style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
109 MacPostControlCreate() ;
117 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
118 (*teH
)->lineHeight
= -1 ;
121 if( wxApp::s_macDefaultEncodingIsPC
)
122 value
= wxMacMakeMacStringFromPC( st
) ;
125 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
130 wxString
wxTextCtrl::GetValue() const
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
) ;
138 return wxString(wxBuffer
);
141 void wxTextCtrl::GetSelection(long* from
, long* to
) const
143 ControlEditTextSelectionRec selection
;
147 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
149 *from
= (**teH
).selStart
;
150 *to
= (**teH
).selEnd
;
153 void wxTextCtrl::SetValue(const wxString
& st
)
157 if( wxApp::s_macDefaultEncodingIsPC
)
158 value
= wxMacMakeMacStringFromPC( st
) ;
161 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
162 UMADrawControl( m_macControl
) ;
165 // Clipboard operations
166 void wxTextCtrl::Copy()
173 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
177 err
= ClearCurrentScrap( );
186 void wxTextCtrl::Cut()
193 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
197 err
= ClearCurrentScrap( );
203 // MacInvalidateControl() ;
207 void wxTextCtrl::Paste()
214 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
217 UMADrawControl( m_macControl
) ;
221 bool wxTextCtrl::CanCopy() const
223 // Can copy if there's a selection
225 GetSelection(& from
, & to
);
229 bool wxTextCtrl::CanCut() const
231 // Can cut if there's a selection
233 GetSelection(& from
, & to
);
237 bool wxTextCtrl::CanPaste() const
244 OSStatus err
= noErr
;
247 err
= GetCurrentScrap( &scrapRef
);
248 if ( err
!= noTypeErr
&& err
!= memFullErr
)
250 ScrapFlavorFlags flavorFlags
;
253 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
255 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
264 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
272 void wxTextCtrl::SetEditable(bool editable
)
275 UMAActivateControl( m_macControl
) ;
277 UMADeactivateControl( m_macControl
) ;
280 void wxTextCtrl::SetInsertionPoint(long pos
)
282 SetSelection( pos
, pos
) ;
285 void wxTextCtrl::SetInsertionPointEnd()
287 long pos
= GetLastPosition();
288 SetInsertionPoint(pos
);
291 long wxTextCtrl::GetInsertionPoint() const
293 ControlEditTextSelectionRec selection
;
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
;
302 long wxTextCtrl::GetLastPosition() const
304 ControlEditTextSelectionRec selection
;
308 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
310 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
311 return (**teH
).teLength
;
314 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
319 ControlEditTextSelectionRec selection
;
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
) ;
327 TEInsert( value
, value
.Length() , teH
) ;
328 // MacInvalidateControl() ;
331 void wxTextCtrl::Remove(long from
, long to
)
336 ControlEditTextSelectionRec selection
;
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
) ;
343 // MacInvalidateControl() ;
346 void wxTextCtrl::SetSelection(long from
, long to
)
348 ControlEditTextSelectionRec selection
;
352 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
354 selection
.selStart
= from
;
355 selection
.selEnd
= to
;
357 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
358 TESetSelect( selection
.selStart
, selection
.selEnd
, teH
) ;
361 bool wxTextCtrl::LoadFile(const wxString
& file
)
363 if ( wxTextCtrlBase::LoadFile(file
) )
371 void wxTextCtrl::WriteText(const wxString
& text
)
376 memcpy( wxBuffer
, text
, text
.Length() ) ;
377 wxBuffer
[text
.Length() ] = 0 ;
378 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
380 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
382 TEInsert( wxBuffer
, strlen( wxBuffer
) , teH
) ;
386 void wxTextCtrl::AppendText(const wxString
& text
)
388 SetInsertionPointEnd();
392 void wxTextCtrl::Clear()
394 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
398 bool wxTextCtrl::IsModified() const
403 bool wxTextCtrl::IsEditable() const
408 bool wxTextCtrl::AcceptsFocus() const
410 // we don't want focus if we can't be edited
411 return IsEditable() && wxControl::AcceptsFocus();
414 wxSize
wxTextCtrl::DoGetBestSize() const
419 if ( UMAHasAppearance() )
423 hText
+= 2 * m_macHorizontalBorder
;
426 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
428 int wText = DEFAULT_ITEM_WIDTH;
430 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
432 return wxSize(wText, hText);
434 if ( m_windowStyle
& wxTE_MULTILINE
)
436 hText
*= wxMin(GetNumberOfLines(), 5);
438 //else: for single line control everything is ok
439 return wxSize(wText
, hText
);
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 void wxTextCtrl::Undo()
453 void wxTextCtrl::Redo()
460 bool wxTextCtrl::CanUndo() const
465 bool wxTextCtrl::CanRedo() const
470 // Makes 'unmodified'
471 void wxTextCtrl::DiscardEdits()
476 int wxTextCtrl::GetNumberOfLines() const
482 long wxTextCtrl::XYToPosition(long x
, long y
) const
488 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
493 void wxTextCtrl::ShowPosition(long pos
)
498 int wxTextCtrl::GetLineLength(long lineNo
) const
500 return GetValue().Length();
503 wxString
wxTextCtrl::GetLineText(long lineNo
) const
512 void wxTextCtrl::Command(wxCommandEvent
& event
)
514 SetValue (event
.GetString());
515 ProcessCommand (event
);
518 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
520 // By default, load the first file into the text window.
521 if (event
.GetNumberOfFiles() > 0)
523 LoadFile(event
.GetFiles()[0]);
527 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
529 switch ( event
.KeyCode() )
532 if (m_windowStyle
& wxPROCESS_ENTER
)
534 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
535 event
.SetEventObject( this );
536 if ( GetEventHandler()->ProcessEvent(event
) )
539 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
541 wxWindow
*parent
= GetParent();
542 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
543 while ( parent
!= NULL
&& panel
== NULL
)
545 parent
= parent
->GetParent() ;
546 panel
= wxDynamicCast(parent
, wxPanel
);
548 if ( panel
&& panel
->GetDefaultItem() )
550 wxButton
*def
= panel
->GetDefaultItem() ;
551 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
552 event
.SetEventObject(def
);
558 //else: multiline controls need Enter for themselves
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.
568 wxNavigationKeyEvent eventNav
;
569 eventNav
.SetDirection(!event
.ShiftDown());
570 eventNav
.SetWindowChange(event
.ControlDown());
571 eventNav
.SetEventObject(this);
573 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
581 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
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
)
589 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
590 event
.SetString( GetValue() ) ;
591 event
.SetEventObject( this );
592 GetEventHandler()->ProcessEvent(event
);
597 // ----------------------------------------------------------------------------
598 // standard handlers for standard edit menu events
599 // ----------------------------------------------------------------------------
601 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
606 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
611 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
616 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
621 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
626 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
628 event
.Enable( CanCut() );
631 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
633 event
.Enable( CanCopy() );
636 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
638 event
.Enable( CanPaste() );
641 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
643 event
.Enable( CanUndo() );
646 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
648 event
.Enable( CanRedo() );