1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
17 #include <sys/types.h>
26 #include "wx/button.h"
28 #include "wx/textctrl.h"
29 #include "wx/notebook.h"
30 #include "wx/tabctrl.h"
31 #include "wx/settings.h"
32 #include "wx/filefn.h"
35 #if defined(__BORLANDC__) && !defined(__WIN32__)
37 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__WXMAC_X__)
41 #include "wx/mac/uma.h"
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
46 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
47 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
48 EVT_CHAR(wxTextCtrl::OnChar
)
49 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
50 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
51 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
52 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
53 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
55 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
56 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
57 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
58 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
59 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
64 wxTextCtrl::wxTextCtrl()
68 const short kVerticalMargin
= 2 ;
69 const short kHorizontalMargin
= 2 ;
71 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
74 const wxSize
& size
, long style
,
75 const wxValidator
& validator
,
78 // base initialization
79 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
82 wxSize mySize
= size
;
83 if ( UMAHasAppearance() )
85 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
86 m_macVerticalBorder
= 5 ;
90 m_macHorizontalBorder
= 0 ; // additional pixels around the real control
91 m_macVerticalBorder
= 0 ;
100 if ( UMAHasAppearance() )
105 mySize
.y
+= 2 * m_macVerticalBorder
;
108 MacPreControlCreate( parent
, id
, "" , pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
110 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
111 ( style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
112 MacPostControlCreate() ;
120 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
121 (*teH
)->lineHeight
= -1 ;
124 if( wxApp::s_macDefaultEncodingIsPC
)
125 value
= wxMacMakeMacStringFromPC( st
) ;
128 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
133 wxString
wxTextCtrl::GetValue() const
136 UMAGetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 32767 , wxBuffer
, &actualsize
) ;
137 wxBuffer
[actualsize
] = 0 ;
138 if( wxApp::s_macDefaultEncodingIsPC
)
139 return wxMacMakePCStringFromMac( wxBuffer
) ;
141 return wxString(wxBuffer
);
144 void wxTextCtrl::GetSelection(long* from
, long* to
) const
146 ControlEditTextSelectionRec selection
;
150 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
152 *from
= (**teH
).selStart
;
153 *to
= (**teH
).selEnd
;
156 void wxTextCtrl::SetValue(const wxString
& st
)
160 if( wxApp::s_macDefaultEncodingIsPC
)
161 value
= wxMacMakeMacStringFromPC( st
) ;
164 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
165 WindowRef window
= GetMacRootWindow() ;
168 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
171 wxMacDrawingHelper
help( win
) ;
172 // the mac control manager always assumes to have the origin at 0,0
175 bool hasTabBehind
= false ;
176 wxWindow
* parent
= GetParent() ;
179 if( parent
->MacGetWindowData() )
181 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
185 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
187 if ( ((wxControl
*)parent
)->GetMacControl() )
188 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
192 parent
= parent
->GetParent() ;
195 UMADrawControl( m_macControl
) ;
196 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
197 wxDC::MacInvalidateSetup() ;
202 // Clipboard operations
203 void wxTextCtrl::Copy()
210 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
214 err
= ClearCurrentScrap( );
223 void wxTextCtrl::Cut()
230 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
234 err
= ClearCurrentScrap( );
240 // MacInvalidateControl() ;
244 void wxTextCtrl::Paste()
251 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
254 WindowRef window
= GetMacRootWindow() ;
257 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
260 wxMacDrawingHelper
help( win
) ;
261 // the mac control manager always assumes to have the origin at 0,0
264 bool hasTabBehind
= false ;
265 wxWindow
* parent
= GetParent() ;
268 if( parent
->MacGetWindowData() )
270 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
274 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
276 if ( ((wxControl
*)parent
)->GetMacControl() )
277 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
281 parent
= parent
->GetParent() ;
284 UMADrawControl( m_macControl
) ;
285 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
286 wxDC::MacInvalidateSetup() ;
292 bool wxTextCtrl::CanCopy() const
294 // Can copy if there's a selection
296 GetSelection(& from
, & to
);
300 bool wxTextCtrl::CanCut() const
302 // Can cut if there's a selection
304 GetSelection(& from
, & to
);
308 bool wxTextCtrl::CanPaste() const
315 OSStatus err
= noErr
;
318 err
= GetCurrentScrap( &scrapRef
);
319 if ( err
!= noTypeErr
&& err
!= memFullErr
)
321 ScrapFlavorFlags flavorFlags
;
324 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
326 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
335 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
343 void wxTextCtrl::SetEditable(bool editable
)
346 UMAActivateControl( m_macControl
) ;
348 UMADeactivateControl( m_macControl
) ;
351 void wxTextCtrl::SetInsertionPoint(long pos
)
353 SetSelection( pos
, pos
) ;
356 void wxTextCtrl::SetInsertionPointEnd()
358 long pos
= GetLastPosition();
359 SetInsertionPoint(pos
);
362 long wxTextCtrl::GetInsertionPoint() const
364 ControlEditTextSelectionRec selection
;
368 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
369 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
370 return (**teH
).selStart
;
373 long wxTextCtrl::GetLastPosition() const
375 ControlEditTextSelectionRec selection
;
379 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
381 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
382 return (**teH
).teLength
;
385 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
390 ControlEditTextSelectionRec selection
;
392 selection
.selStart
= from
;
393 selection
.selEnd
= to
;
394 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
395 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
396 TESetSelect( from
, to
, teH
) ;
398 TEInsert( value
, value
.Length() , teH
) ;
402 void wxTextCtrl::Remove(long from
, long to
)
407 ControlEditTextSelectionRec selection
;
409 selection
.selStart
= from
;
410 selection
.selEnd
= to
;
411 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
412 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
417 void wxTextCtrl::SetSelection(long from
, long to
)
419 ControlEditTextSelectionRec selection
;
423 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
425 selection
.selStart
= from
;
426 selection
.selEnd
= to
;
428 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
429 TESetSelect( selection
.selStart
, selection
.selEnd
, teH
) ;
432 bool wxTextCtrl::LoadFile(const wxString
& file
)
434 if ( wxTextCtrlBase::LoadFile(file
) )
442 void wxTextCtrl::WriteText(const wxString
& text
)
447 memcpy( wxBuffer
, text
, text
.Length() ) ;
448 wxBuffer
[text
.Length() ] = 0 ;
449 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
451 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
453 TEInsert( wxBuffer
, strlen( wxBuffer
) , teH
) ;
457 void wxTextCtrl::AppendText(const wxString
& text
)
459 SetInsertionPointEnd();
463 void wxTextCtrl::Clear()
465 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
469 bool wxTextCtrl::IsModified() const
474 bool wxTextCtrl::IsEditable() const
479 bool wxTextCtrl::AcceptsFocus() const
481 // we don't want focus if we can't be edited
482 return IsEditable() && wxControl::AcceptsFocus();
485 wxSize
wxTextCtrl::DoGetBestSize() const
490 if ( UMAHasAppearance() )
494 hText
+= 2 * m_macHorizontalBorder
;
497 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
499 int wText = DEFAULT_ITEM_WIDTH;
501 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
503 return wxSize(wText, hText);
505 if ( m_windowStyle
& wxTE_MULTILINE
)
507 hText
*= wxMin(GetNumberOfLines(), 5);
509 //else: for single line control everything is ok
510 return wxSize(wText
, hText
);
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
517 void wxTextCtrl::Undo()
524 void wxTextCtrl::Redo()
531 bool wxTextCtrl::CanUndo() const
536 bool wxTextCtrl::CanRedo() const
541 // Makes 'unmodified'
542 void wxTextCtrl::DiscardEdits()
547 int wxTextCtrl::GetNumberOfLines() const
553 long wxTextCtrl::XYToPosition(long x
, long y
) const
559 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
564 void wxTextCtrl::ShowPosition(long pos
)
569 int wxTextCtrl::GetLineLength(long lineNo
) const
571 return GetValue().Length();
574 wxString
wxTextCtrl::GetLineText(long lineNo
) const
583 void wxTextCtrl::Command(wxCommandEvent
& event
)
585 SetValue (event
.GetString());
586 ProcessCommand (event
);
589 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
591 // By default, load the first file into the text window.
592 if (event
.GetNumberOfFiles() > 0)
594 LoadFile(event
.GetFiles()[0]);
598 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
600 switch ( event
.KeyCode() )
603 if (m_windowStyle
& wxPROCESS_ENTER
)
605 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
606 event
.SetEventObject( this );
607 if ( GetEventHandler()->ProcessEvent(event
) )
610 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
612 wxWindow
*parent
= GetParent();
613 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
614 while ( parent
!= NULL
&& panel
== NULL
)
616 parent
= parent
->GetParent() ;
617 panel
= wxDynamicCast(parent
, wxPanel
);
619 if ( panel
&& panel
->GetDefaultItem() )
621 wxButton
*def
= panel
->GetDefaultItem() ;
622 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
623 event
.SetEventObject(def
);
629 //else: multiline controls need Enter for themselves
634 // always produce navigation event - even if we process TAB
635 // ourselves the fact that we got here means that the user code
636 // decided to skip processing of this TAB - probably to let it
637 // do its default job.
639 wxNavigationKeyEvent eventNav
;
640 eventNav
.SetDirection(!event
.ShiftDown());
641 eventNav
.SetWindowChange(event
.ControlDown());
642 eventNav
.SetEventObject(this);
644 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
652 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
655 keychar
= short(ev
->message
& charCodeMask
);
656 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
657 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
658 if ( keychar
>= 0x20 || event
.KeyCode() == WXK_RETURN
)
660 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
661 event
.SetString( GetValue() ) ;
662 event
.SetEventObject( this );
663 GetEventHandler()->ProcessEvent(event
);
668 // ----------------------------------------------------------------------------
669 // standard handlers for standard edit menu events
670 // ----------------------------------------------------------------------------
672 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
677 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
682 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
687 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
692 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
697 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
699 event
.Enable( CanCut() );
702 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
704 event
.Enable( CanCopy() );
707 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
709 event
.Enable( CanPaste() );
712 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
714 event
.Enable( CanUndo() );
717 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
719 event
.Enable( CanRedo() );