#include "wx/thread.h"
#include "wx/osx/private.h"
-#include "wx/osx/carbon/private/mactext.h"
IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
EVT_CHAR(wxTextCtrl::OnChar)
+ EVT_KEY_DOWN(wxTextCtrl::OnKeyDown)
EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
void wxTextCtrl::Init()
{
- m_editable = true ;
m_dirty = false;
- m_maxLength = 0;
m_privateContextMenu = NULL;
- m_triggerOnSetValue = true ;
+ m_triggerUpdateEvents = true ;
}
wxTextCtrl::~wxTextCtrl()
style |= wxTE_PROCESS_ENTER ;
}
- m_peer = wxWidgetImpl::CreateTextControl( this, parent, id, str, pos, size, style, GetExtraStyle() );
- // CreatePeer( str, pos, size, style );
+ m_peer = wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str, pos, size, style, GetExtraStyle() );
MacPostControlCreate(pos, size) ;
-
+
+#if wxOSX_USE_COCOA
+ // under carbon everything can already be set before the MacPostControlCreate embedding takes place
+ // but under cocoa for single line textfields this only works after everything has been set up
+ GetTextPeer()->SetStringValue(str);
+#endif
+
// only now the embedding is correct and we can do a positioning update
MacSuperChangedPosition() ;
return true;
}
-void wxTextCtrl::CreatePeer(
- const wxString& str,
- const wxPoint& pos,
- const wxSize& size, long style )
+wxTextWidgetImpl* wxTextCtrl::GetTextPeer() const
{
+ return dynamic_cast<wxTextWidgetImpl*> (m_peer);
}
void wxTextCtrl::MacSuperChangedPosition()
void wxTextCtrl::MacCheckSpelling(bool check)
{
- GetPeer()->CheckSpelling(check);
-}
-
-wxString wxTextCtrl::GetValue() const
-{
- return GetPeer()->GetStringValue() ;
-}
-
-void wxTextCtrl::GetSelection(long* from, long* to) const
-{
- GetPeer()->GetSelection( from , to ) ;
-}
-
-void wxTextCtrl::DoSetValue(const wxString& str, int flags)
-{
- // optimize redraws
- if ( GetValue() == str )
- return;
-
- GetPeer()->SetStringValue( str ) ;
-
- if ( (flags & SetValue_SendEvent) && m_triggerOnSetValue )
- {
- SendTextUpdatedEvent();
- }
+ GetTextPeer()->CheckSpelling(check);
}
void wxTextCtrl::SetMaxLength(unsigned long len)
if ( !wxTextCtrlBase::SetFont( font ) )
return false ;
- GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle() ) ;
+ GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
return true ;
}
bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
{
- GetPeer()->SetStyle( start , end , style ) ;
+ if (GetTextPeer())
+ GetTextPeer()->SetStyle( start , end , style ) ;
return true ;
}
return true ;
}
-// Clipboard operations
-
-void wxTextCtrl::Copy()
-{
- if (CanCopy())
- GetPeer()->Copy() ;
-}
-
-void wxTextCtrl::Cut()
-{
- if (CanCut())
- {
- GetPeer()->Cut() ;
-
- wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
- event.SetEventObject( this );
- HandleWindowEvent( event );
- }
-}
-
-void wxTextCtrl::Paste()
-{
- if (CanPaste())
- {
- GetPeer()->Paste() ;
-
- // TODO: eventually we should add setting the default style again
-
- wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
- event.SetEventObject( this );
- HandleWindowEvent( event );
- }
-}
-
-bool wxTextCtrl::CanCopy() const
-{
- // Can copy if there's a selection
- long from, to;
- GetSelection( &from, &to );
-
- return (from != to);
-}
-
-bool wxTextCtrl::CanCut() const
-{
- if ( !IsEditable() )
- return false;
-
- // Can cut if there's a selection
- long from, to;
- GetSelection( &from, &to );
-
- return (from != to);
-}
-
-bool wxTextCtrl::CanPaste() const
-{
- if (!IsEditable())
- return false;
-
- return GetPeer()->CanPaste() ;
-}
-
-void wxTextCtrl::SetEditable(bool editable)
-{
- if ( editable != m_editable )
- {
- m_editable = editable ;
- GetPeer()->SetEditable( editable ) ;
- }
-}
-
-void wxTextCtrl::SetInsertionPoint(long pos)
-{
- SetSelection( pos , pos ) ;
-}
-
-void wxTextCtrl::SetInsertionPointEnd()
-{
- wxTextPos pos = GetLastPosition();
- SetInsertionPoint( pos );
-}
-
-long wxTextCtrl::GetInsertionPoint() const
-{
- long begin, end ;
- GetSelection( &begin , &end ) ;
-
- return begin ;
-}
-
-wxTextPos wxTextCtrl::GetLastPosition() const
-{
- return GetPeer()->GetLastPosition() ;
-}
-
-void wxTextCtrl::Replace(long from, long to, const wxString& str)
-{
- GetPeer()->Replace( from , to , str ) ;
-}
-
-void wxTextCtrl::Remove(long from, long to)
-{
- GetPeer()->Remove( from , to ) ;
-}
-
-void wxTextCtrl::SetSelection(long from, long to)
-{
- GetPeer()->SetSelection( from , to ) ;
-}
-
-void wxTextCtrl::WriteText(const wxString& str)
-{
- GetPeer()->WriteText( str ) ;
-}
-
-void wxTextCtrl::AppendText(const wxString& text)
-{
- SetInsertionPointEnd();
- WriteText( text );
-}
-
-void wxTextCtrl::Clear()
-{
- GetPeer()->Clear() ;
-}
-
bool wxTextCtrl::IsModified() const
{
return m_dirty;
}
-bool wxTextCtrl::IsEditable() const
-{
- return IsEnabled() && m_editable ;
-}
-
bool wxTextCtrl::AcceptsFocus() const
{
// we don't want focus if we can't be edited
wxSize wxTextCtrl::DoGetBestSize() const
{
+ if (GetTextPeer())
+ {
+ wxSize size = GetTextPeer()->GetBestSize();
+ if (size.x > 0 && size.y > 0)
+ return size;
+ }
+
int wText, hText;
// these are the numbers from the HIG:
return wxSize(wText, hText);
}
-// ----------------------------------------------------------------------------
-// Undo/redo
-// ----------------------------------------------------------------------------
-
-void wxTextCtrl::Undo()
+bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
{
- if (CanUndo())
- GetPeer()->Undo() ;
-}
-
-void wxTextCtrl::Redo()
-{
- if (CanRedo())
- GetPeer()->Redo() ;
-}
-
-bool wxTextCtrl::CanUndo() const
-{
- if ( !IsEditable() )
- return false ;
-
- return GetPeer()->CanUndo() ;
-}
-
-bool wxTextCtrl::CanRedo() const
-{
- if ( !IsEditable() )
- return false ;
-
- return GetPeer()->CanRedo() ;
+ return GetTextPeer()->GetStyle(position, style);
}
void wxTextCtrl::MarkDirty()
int wxTextCtrl::GetNumberOfLines() const
{
- return GetPeer()->GetNumberOfLines() ;
+ return GetTextPeer()->GetNumberOfLines() ;
}
long wxTextCtrl::XYToPosition(long x, long y) const
{
- return GetPeer()->XYToPosition( x , y ) ;
+ return GetTextPeer()->XYToPosition( x , y ) ;
}
bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
{
- return GetPeer()->PositionToXY( pos , x , y ) ;
+ return GetTextPeer()->PositionToXY( pos , x , y ) ;
}
void wxTextCtrl::ShowPosition(long pos)
{
- return GetPeer()->ShowPosition(pos) ;
+ return GetTextPeer()->ShowPosition(pos) ;
}
int wxTextCtrl::GetLineLength(long lineNo) const
{
- return GetPeer()->GetLineLength(lineNo) ;
+ return GetTextPeer()->GetLineLength(lineNo) ;
}
wxString wxTextCtrl::GetLineText(long lineNo) const
{
- return GetPeer()->GetLineText(lineNo) ;
+ return GetTextPeer()->GetLineText(lineNo) ;
}
-void wxTextCtrl::Command(wxCommandEvent & event)
+void wxTextCtrl::Remove(long from, long to)
{
- SetValue(event.GetString());
- ProcessCommand(event);
+ wxTextEntry::Remove(from, to);
+ if ( m_triggerUpdateEvents )
+ SendTextUpdatedEvent();
}
-void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
+void wxTextCtrl::WriteText(const wxString& str)
{
- // By default, load the first file into the text window.
- if (event.GetNumberOfFiles() > 0)
- LoadFile( event.GetFiles()[0] );
+ wxTextEntry::WriteText( str ) ;
+ if ( m_triggerUpdateEvents )
+ SendTextUpdatedEvent();
}
-void wxTextCtrl::OnChar(wxKeyEvent& event)
+void wxTextCtrl::Clear()
{
- int key = event.GetKeyCode() ;
- bool eat_key = false ;
- long from, to;
+ wxTextEntry::Clear() ;
+ SendTextUpdatedEvent();
+}
- if ( key == 'a' && event.MetaDown() )
+void wxTextCtrl::Cut()
+{
+ if (CanCut())
{
- SelectAll() ;
+ wxTextEntry::Cut() ;
- return ;
+ SendTextUpdatedEvent();
}
+}
- if ( key == 'c' && event.MetaDown() )
+void wxTextCtrl::Paste()
+{
+ if (CanPaste())
{
- if ( CanCopy() )
- Copy() ;
+ wxTextEntry::Paste();
- return ;
+ // TODO: eventually we should add setting the default style again
+ SendTextUpdatedEvent();
}
+}
- if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
+void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
+{
+ // By default, load the first file into the text window.
+ if (event.GetNumberOfFiles() > 0)
+ LoadFile( event.GetFiles()[0] );
+}
+
+void wxTextCtrl::OnKeyDown(wxKeyEvent& event)
+{
+ if ( event.GetModifiers() == wxMOD_CMD )
+ {
+ switch( event.GetKeyCode() )
+ {
+ case 'A':
+ SelectAll();
+ return;
+ case 'C':
+ if ( CanCopy() )
+ Copy() ;
+ return;
+ case 'V':
+ if ( CanPaste() )
+ Paste() ;
+ return;
+ case 'X':
+ if ( CanCut() )
+ Cut() ;
+ return;
+ default:
+ break;
+ }
+ }
+ // no, we didn't process it
+ event.Skip();
+}
+
+void wxTextCtrl::OnChar(wxKeyEvent& event)
+{
+ int key = event.GetKeyCode() ;
+ bool eat_key = false ;
+ long from, to;
+
+ if ( !IsEditable() && !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB) &&
!( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
// && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
)
// allow navigation and deletion
GetSelection( &from, &to );
if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
- key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && key != WXK_UP && key != WXK_DOWN &&
- key != WXK_BACK && key != WXK_DELETE && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
+ !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) &&
+ !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
from == to )
{
// eat it, we don't want to add more than allowed # of characters
// assume that any key not processed yet is going to modify the control
m_dirty = true;
- if ( key == 'v' && event.MetaDown() )
- {
- if ( CanPaste() )
- Paste() ;
-
- return ;
- }
-
- if ( key == 'x' && event.MetaDown() )
- {
- if ( CanCut() )
- Cut() ;
-
- return ;
- }
-
switch ( key )
{
case WXK_RETURN:
event.Skip(true) ;
}
+ // osx_cocoa sends its event upon insertText
+#if wxOSX_USE_CARBON
if ( ( key >= 0x20 && key < WXK_START ) ||
( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
key == WXK_RETURN ||
event1.SetEventObject( this );
wxPostEvent( GetEventHandler(), event1 );
}
+#endif
+}
+
+void wxTextCtrl::Command(wxCommandEvent & event)
+{
+ SetValue(event.GetString());
+ ProcessCommand(event);
}
// ----------------------------------------------------------------------------
// standard handlers for standard edit menu events
// ----------------------------------------------------------------------------
+// CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
+
void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
{
Cut();
event.Enable(GetLastPosition() > 0);
}
-// CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
-
void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
{
- if ( GetPeer()->HasOwnContextMenu() )
+ if ( GetTextPeer()->HasOwnContextMenu() )
{
event.Skip() ;
return ;
m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
}
- if (m_privateContextMenu != NULL)
- PopupMenu(m_privateContextMenu);
+ PopupMenu(m_privateContextMenu);
#endif
}
bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
{
- if ( !GetPeer()->SetupCursor( pt ) )
+ if ( !GetTextPeer()->SetupCursor( pt ) )
return wxWindow::MacSetupCursor( pt ) ;
else
return true ;
// implementation base class
// ----------------------------------------------------------------------------
-#if wxOSX_USE_CARBON
- wxMacTextControl::wxMacTextControl(wxTextCtrl* peer) :
- wxMacControl( peer )
-#else
- wxMacTextControl::wxMacTextControl(wxTextCtrl* peer, WXWidget w) :
- wxWidgetCocoaImpl( peer, w )
-#endif
-{
-}
-
-wxMacTextControl::~wxMacTextControl()
+bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position),
+ wxTextAttr& WXUNUSED(style))
{
+ return false;
}
-void wxMacTextControl::SetStyle(long WXUNUSED(start),
+void wxTextWidgetImpl::SetStyle(long WXUNUSED(start),
long WXUNUSED(end),
const wxTextAttr& WXUNUSED(style))
{
}
-void wxMacTextControl::Copy()
+void wxTextWidgetImpl::Copy()
{
}
-void wxMacTextControl::Cut()
+void wxTextWidgetImpl::Cut()
{
}
-void wxMacTextControl::Paste()
+void wxTextWidgetImpl::Paste()
{
}
-bool wxMacTextControl::CanPaste() const
+bool wxTextWidgetImpl::CanPaste() const
{
return false ;
}
-void wxMacTextControl::SetEditable(bool WXUNUSED(editable))
+void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable))
{
}
-wxTextPos wxMacTextControl::GetLastPosition() const
+long wxTextWidgetImpl::GetLastPosition() const
{
return GetStringValue().length() ;
}
-void wxMacTextControl::Replace( long from , long to , const wxString &val )
+void wxTextWidgetImpl::Replace( long from , long to , const wxString &val )
{
SetSelection( from , to ) ;
WriteText( val ) ;
}
-void wxMacTextControl::Remove( long from , long to )
+void wxTextWidgetImpl::Remove( long from , long to )
{
SetSelection( from , to ) ;
WriteText( wxEmptyString) ;
}
-void wxMacTextControl::Clear()
+void wxTextWidgetImpl::Clear()
{
SetStringValue( wxEmptyString ) ;
}
-bool wxMacTextControl::CanUndo() const
+bool wxTextWidgetImpl::CanUndo() const
{
return false ;
}
-void wxMacTextControl::Undo()
+void wxTextWidgetImpl::Undo()
{
}
-bool wxMacTextControl::CanRedo() const
+bool wxTextWidgetImpl::CanRedo() const
{
return false ;
}
-void wxMacTextControl::Redo()
+void wxTextWidgetImpl::Redo()
{
}
-long wxMacTextControl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const
+long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const
{
return 0 ;
}
-bool wxMacTextControl::PositionToXY(long WXUNUSED(pos),
+bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos),
long *WXUNUSED(x),
long *WXUNUSED(y)) const
{
return false ;
}
-void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
+void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos) )
{
}
-int wxMacTextControl::GetNumberOfLines() const
+int wxTextWidgetImpl::GetNumberOfLines() const
{
- ItemCount lines = 0 ;
wxString content = GetStringValue() ;
- lines = 1;
+ ItemCount lines = 1;
for (size_t i = 0; i < content.length() ; i++)
{
+#if wxOSX_USE_COCOA
+ if (content[i] == '\n')
+#else
if (content[i] == '\r')
+#endif
lines++;
}
return lines ;
}
-wxString wxMacTextControl::GetLineText(long lineNo) const
+wxString wxTextWidgetImpl::GetLineText(long lineNo) const
{
// TODO: change this if possible to reflect real lines
wxString content = GetStringValue() ;
return wxEmptyString ;
}
-int wxMacTextControl::GetLineLength(long lineNo) const
+int wxTextWidgetImpl::GetLineLength(long lineNo) const
{
// TODO: change this if possible to reflect real lines
wxString content = GetStringValue() ;
return 0 ;
}
-void wxMacTextControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle )
-{
-#if wxOSX_USE_CARBON
- wxMacControl::SetFont(font, foreground, windowStyle );
-
- // overrule the barrier in wxMacControl for supporting disabled controls, in order to support
- // setting the color to eg red and back to black by controllers
-
- if ( foreground == *wxBLACK )
- {
- ControlFontStyleRec fontStyle;
- fontStyle.foreColor.red = fontStyle.foreColor.green = fontStyle.foreColor.blue = 0;
- fontStyle.flags = kControlUseForeColorMask;
- ::SetControlFontStyle( m_controlRef , &fontStyle );
- }
-#endif
-}
-
#endif // wxUSE_TEXTCTRL