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" , false , 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 ) ;
201 // Clipboard operations
202 void wxTextCtrl::Copy()
209 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
213 err
= ClearCurrentScrap( );
222 void wxTextCtrl::Cut()
229 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
233 err
= ClearCurrentScrap( );
239 // MacInvalidateControl() ;
243 void wxTextCtrl::Paste()
250 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
253 WindowRef window
= GetMacRootWindow() ;
256 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
259 wxMacDrawingHelper
help( win
) ;
260 // the mac control manager always assumes to have the origin at 0,0
263 bool hasTabBehind
= false ;
264 wxWindow
* parent
= GetParent() ;
267 if( parent
->MacGetWindowData() )
269 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
273 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
275 if ( ((wxControl
*)parent
)->GetMacControl() )
276 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
280 parent
= parent
->GetParent() ;
283 UMADrawControl( m_macControl
) ;
284 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
290 bool wxTextCtrl::CanCopy() const
292 // Can copy if there's a selection
294 GetSelection(& from
, & to
);
298 bool wxTextCtrl::CanCut() const
300 // Can cut if there's a selection
302 GetSelection(& from
, & to
);
306 bool wxTextCtrl::CanPaste() const
313 OSStatus err
= noErr
;
316 err
= GetCurrentScrap( &scrapRef
);
317 if ( err
!= noTypeErr
&& err
!= memFullErr
)
319 ScrapFlavorFlags flavorFlags
;
322 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
324 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
333 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
341 void wxTextCtrl::SetEditable(bool editable
)
344 UMAActivateControl( m_macControl
) ;
346 UMADeactivateControl( m_macControl
) ;
349 void wxTextCtrl::SetInsertionPoint(long pos
)
351 SetSelection( pos
, pos
) ;
354 void wxTextCtrl::SetInsertionPointEnd()
356 long pos
= GetLastPosition();
357 SetInsertionPoint(pos
);
360 long wxTextCtrl::GetInsertionPoint() const
362 ControlEditTextSelectionRec selection
;
366 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
367 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
368 return (**teH
).selStart
;
371 long wxTextCtrl::GetLastPosition() const
373 ControlEditTextSelectionRec selection
;
377 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
379 // UMAGetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ;
380 return (**teH
).teLength
;
383 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
388 ControlEditTextSelectionRec selection
;
390 selection
.selStart
= from
;
391 selection
.selEnd
= to
;
392 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
393 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
394 TESetSelect( from
, to
, teH
) ;
396 TEInsert( value
, value
.Length() , teH
) ;
400 void wxTextCtrl::Remove(long from
, long to
)
405 ControlEditTextSelectionRec selection
;
407 selection
.selStart
= from
;
408 selection
.selEnd
= to
;
409 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
410 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
415 void wxTextCtrl::SetSelection(long from
, long to
)
417 ControlEditTextSelectionRec selection
;
421 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
423 selection
.selStart
= from
;
424 selection
.selEnd
= to
;
426 UMASetControlData( m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
427 TESetSelect( selection
.selStart
, selection
.selEnd
, teH
) ;
430 bool wxTextCtrl::LoadFile(const wxString
& file
)
432 if ( wxTextCtrlBase::LoadFile(file
) )
440 void wxTextCtrl::WriteText(const wxString
& text
)
445 memcpy( wxBuffer
, text
, text
.Length() ) ;
446 wxBuffer
[text
.Length() ] = 0 ;
447 // wxMacConvertNewlines( wxBuffer , wxBuffer ) ;
449 UMAGetControlData( m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &teH
, &size
) ;
451 TEInsert( wxBuffer
, strlen( wxBuffer
) , teH
) ;
455 void wxTextCtrl::AppendText(const wxString
& text
)
457 SetInsertionPointEnd();
461 void wxTextCtrl::Clear()
463 UMASetControlData( m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
467 bool wxTextCtrl::IsModified() const
472 bool wxTextCtrl::IsEditable() const
477 bool wxTextCtrl::AcceptsFocus() const
479 // we don't want focus if we can't be edited
480 return IsEditable() && wxControl::AcceptsFocus();
483 wxSize
wxTextCtrl::DoGetBestSize() const
488 if ( UMAHasAppearance() )
492 hText
+= 2 * m_macHorizontalBorder
;
495 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
497 int wText = DEFAULT_ITEM_WIDTH;
499 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
501 return wxSize(wText, hText);
503 if ( m_windowStyle
& wxTE_MULTILINE
)
505 hText
*= wxMin(GetNumberOfLines(), 5);
507 //else: for single line control everything is ok
508 return wxSize(wText
, hText
);
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 void wxTextCtrl::Undo()
522 void wxTextCtrl::Redo()
529 bool wxTextCtrl::CanUndo() const
534 bool wxTextCtrl::CanRedo() const
539 // Makes 'unmodified'
540 void wxTextCtrl::DiscardEdits()
545 int wxTextCtrl::GetNumberOfLines() const
551 long wxTextCtrl::XYToPosition(long x
, long y
) const
557 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
562 void wxTextCtrl::ShowPosition(long pos
)
567 int wxTextCtrl::GetLineLength(long lineNo
) const
569 return GetValue().Length();
572 wxString
wxTextCtrl::GetLineText(long lineNo
) const
581 void wxTextCtrl::Command(wxCommandEvent
& event
)
583 SetValue (event
.GetString());
584 ProcessCommand (event
);
587 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
589 // By default, load the first file into the text window.
590 if (event
.GetNumberOfFiles() > 0)
592 LoadFile(event
.GetFiles()[0]);
596 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
598 switch ( event
.KeyCode() )
601 if (m_windowStyle
& wxPROCESS_ENTER
)
603 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
604 event
.SetEventObject( this );
605 if ( GetEventHandler()->ProcessEvent(event
) )
608 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
610 wxWindow
*parent
= GetParent();
611 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
612 while ( parent
!= NULL
&& panel
== NULL
)
614 parent
= parent
->GetParent() ;
615 panel
= wxDynamicCast(parent
, wxPanel
);
617 if ( panel
&& panel
->GetDefaultItem() )
619 wxButton
*def
= panel
->GetDefaultItem() ;
620 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
621 event
.SetEventObject(def
);
627 //else: multiline controls need Enter for themselves
632 // always produce navigation event - even if we process TAB
633 // ourselves the fact that we got here means that the user code
634 // decided to skip processing of this TAB - probably to let it
635 // do its default job.
637 wxNavigationKeyEvent eventNav
;
638 eventNav
.SetDirection(!event
.ShiftDown());
639 eventNav
.SetWindowChange(event
.ControlDown());
640 eventNav
.SetEventObject(this);
642 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
650 EventRecord
*ev
= wxTheApp
->MacGetCurrentEvent() ;
653 keychar
= short(ev
->message
& charCodeMask
);
654 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
655 UMAHandleControlKey( m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
656 if ( keychar
>= 0x20 || event
.KeyCode() == WXK_RETURN
)
658 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
659 event
.SetString( GetValue() ) ;
660 event
.SetEventObject( this );
661 GetEventHandler()->ProcessEvent(event
);
666 // ----------------------------------------------------------------------------
667 // standard handlers for standard edit menu events
668 // ----------------------------------------------------------------------------
670 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
675 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
680 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
685 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
690 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
695 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
697 event
.Enable( CanCut() );
700 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
702 event
.Enable( CanCopy() );
705 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
707 event
.Enable( CanPaste() );
710 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
712 event
.Enable( CanUndo() );
715 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
717 event
.Enable( CanRedo() );