+ // all erasing should be done by the real mac control implementation
+ // while this is true for MLTE under classic, the HITextView is somehow
+ // transparent but background erase is not working correctly, so intercept
+ // things while we can...
+ event.Skip() ;
+}
+
+void wxTextCtrl::OnChar(wxKeyEvent& event)
+{
+ int key = event.GetKeyCode() ;
+ bool eat_key = false ;
+
+ if ( key == 'c' && event.MetaDown() )
+ {
+ if ( CanCopy() )
+ Copy() ;
+ return ;
+ }
+
+ if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
+ !( key == WXK_RETURN && ( (m_windowStyle & wxPROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
+/* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */
+ )
+ {
+ // eat it
+ return ;
+ }
+
+ // Check if we have reached the max # of chars, but still allow navigation and deletion
+ if ( !IsMultiLine() && GetValue().Length() >= m_maxLength &&
+ key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB &&
+ key != WXK_BACK && !( key == WXK_RETURN && (m_windowStyle & wxPROCESS_ENTER) )
+ )
+ {
+ // eat it, we don't want to add more than allowed # of characters
+ return;
+ }
+
+ // 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:
+ if (m_windowStyle & wxPROCESS_ENTER)
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+ event.SetEventObject( this );
+ event.SetString( GetValue() );
+ if ( GetEventHandler()->ProcessEvent(event) )
+ return;
+ }
+ if ( !(m_windowStyle & wxTE_MULTILINE) )
+ {
+ wxWindow *parent = GetParent();
+ while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
+ parent = parent->GetParent() ;
+ }
+ if ( parent && parent->GetDefaultItem() )
+ {
+ wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
+ wxButton);
+ if ( def && def->IsEnabled() )
+ {
+ wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
+ event.SetEventObject(def);
+ def->Command(event);
+ return ;
+ }
+ }
+
+ // this will make wxWidgets eat the ENTER key so that
+ // we actually prevent line wrapping in a single line
+ // text control
+ eat_key = true;
+ }
+
+ break;
+
+ case WXK_TAB:
+ if ( !(m_windowStyle & wxTE_PROCESS_TAB))
+ {
+ int flags = 0;
+ if (!event.ShiftDown())
+ flags |= wxNavigationKeyEvent::IsForward ;
+ if (event.ControlDown())
+ flags |= wxNavigationKeyEvent::WinChange ;
+ Navigate(flags);
+ return;
+ }
+ else
+ {
+ // This is necessary (don't know why) or the tab will not
+ // be inserted.
+ WriteText(wxT("\t"));
+ }
+
+ break;
+ }
+
+ if (!eat_key)
+ {
+ // perform keystroke handling
+ if ( wxTheApp->MacGetCurrentEvent() != NULL && wxTheApp->MacGetCurrentEventHandlerCallRef() != NULL )
+ CallNextEventHandler((EventHandlerCallRef)wxTheApp->MacGetCurrentEventHandlerCallRef() , (EventRef) wxTheApp->MacGetCurrentEvent() ) ;
+ else
+ {
+ EventRecord rec ;
+ if ( wxMacConvertEventToRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) )
+ {
+ EventRecord *ev = &rec ;
+ short keycode ;
+ short keychar ;
+ keychar = short(ev->message & charCodeMask);
+ keycode = short(ev->message & keyCodeMask) >> 8 ;
+
+ m_peer->HandleKey( keycode , keychar , ev->modifiers ) ;
+ }
+ }
+ }
+ if ( ( key >= 0x20 && key < WXK_START ) ||
+ key == WXK_RETURN ||
+ key == WXK_DELETE ||
+ key == WXK_BACK)
+ {
+ wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
+ event1.SetEventObject( this );
+ wxPostEvent(GetEventHandler(),event1);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// standard handlers for standard edit menu events
+// ----------------------------------------------------------------------------
+
+void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
+{
+ Cut();
+}
+
+void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
+{
+ Copy();
+}
+
+void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
+{
+ Paste();
+}
+
+void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
+{
+ Undo();
+}
+
+void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
+{
+ Redo();
+}
+
+void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event))
+{
+ long from, to;
+ GetSelection(& from, & to);
+ if (from != -1 && to != -1)
+ Remove(from, to);
+}
+
+void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event))
+{
+ SetSelection(-1, -1);
+}
+
+void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
+{
+ event.Enable( CanCut() );
+}
+
+void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
+{
+ event.Enable( CanCopy() );
+}
+
+void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
+{
+ event.Enable( CanPaste() );
+}
+
+void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
+{
+ event.Enable( CanUndo() );
+}
+
+void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
+{
+ event.Enable( CanRedo() );
+}
+
+void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event)
+{
+ long from, to;
+ GetSelection(& from, & to);
+ event.Enable(from != -1 && to != -1 && from != to && IsEditable()) ;
+}
+
+void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event)
+{
+ 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 (m_privateContextMenu == NULL)
+ {
+ m_privateContextMenu = new wxMenu;
+ m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
+ m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
+ m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
+ m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
+ m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
+ m_privateContextMenu->AppendSeparator();
+ m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
+ }
+
+ if (m_privateContextMenu != NULL)
+ PopupMenu(m_privateContextMenu);
+}
+
+bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
+{
+ if ( !GetPeer()->SetupCursor(pt) )
+ return wxWindow::MacSetupCursor( pt ) ;
+ else
+ return true ;
+}
+#if !TARGET_API_MAC_OSX
+
+// user pane implementation
+
+void wxTextCtrl::MacControlUserPaneDrawProc(wxInt16 part)
+{
+ GetPeer()->MacControlUserPaneDrawProc( part ) ;
+}
+
+wxInt16 wxTextCtrl::MacControlUserPaneHitTestProc(wxInt16 x, wxInt16 y)
+{
+ return GetPeer()->MacControlUserPaneHitTestProc( x , y ) ;
+}
+
+wxInt16 wxTextCtrl::MacControlUserPaneTrackingProc(wxInt16 x, wxInt16 y, void* actionProc)
+{
+ return GetPeer()->MacControlUserPaneTrackingProc( x , y , actionProc ) ;
+}
+
+void wxTextCtrl::MacControlUserPaneIdleProc()
+{
+ GetPeer()->MacControlUserPaneIdleProc( ) ;
+}
+
+wxInt16 wxTextCtrl::MacControlUserPaneKeyDownProc(wxInt16 keyCode, wxInt16 charCode, wxInt16 modifiers)
+{
+ return GetPeer()->MacControlUserPaneKeyDownProc( keyCode , charCode , modifiers ) ;
+}
+
+void wxTextCtrl::MacControlUserPaneActivateProc(bool activating)
+{
+ GetPeer()->MacControlUserPaneActivateProc( activating ) ;
+}
+
+wxInt16 wxTextCtrl::MacControlUserPaneFocusProc(wxInt16 action)
+{
+ return GetPeer()->MacControlUserPaneFocusProc( action ) ;
+}
+
+void wxTextCtrl::MacControlUserPaneBackgroundProc(void* info)
+{
+ GetPeer()->MacControlUserPaneBackgroundProc( info ) ;
+}
+
+#endif
+// ----------------------------------------------------------------------------
+// implementation base class
+// ----------------------------------------------------------------------------
+
+wxMacTextControl::wxMacTextControl(wxTextCtrl* peer) :
+ wxMacControl( peer )
+{
+}
+
+wxMacTextControl::~wxMacTextControl()
+{
+}
+
+void wxMacTextControl::SetStyle(long start, long end, const wxTextAttr& style)
+{
+}
+
+void wxMacTextControl::Copy()
+{
+}
+
+void wxMacTextControl::Cut()
+{
+}
+
+void wxMacTextControl::Paste()
+{
+}
+
+bool wxMacTextControl::CanPaste() const
+{
+ return false ;
+}
+
+void wxMacTextControl::SetEditable(bool editable)
+{
+}
+
+wxTextPos wxMacTextControl::GetLastPosition() const
+{
+ return GetStringValue().Length() ;
+}
+
+void wxMacTextControl::Replace( long from , long to , const wxString str )
+{
+}
+
+void wxMacTextControl::Clear()
+{
+ SetStringValue( wxEmptyString ) ;
+}
+
+bool wxMacTextControl::CanUndo() const
+{
+ return false ;
+}
+
+void wxMacTextControl::Undo() { }
+
+bool wxMacTextControl::CanRedo() const
+{
+ return false ;
+}
+
+void wxMacTextControl::Redo()
+{
+}
+
+long wxMacTextControl::XYToPosition(long x, long y) const
+{
+ return 0 ;
+}
+
+bool wxMacTextControl::PositionToXY(long pos, long *x, long *y) const
+{
+ return false ;
+}
+
+void wxMacTextControl::ShowPosition( long WXUNUSED(pos) )
+{
+}
+
+int wxMacTextControl::GetNumberOfLines() const
+{
+ ItemCount lines = 0 ;
+ wxString content = GetStringValue() ;
+ lines = 1;
+ for (size_t i = 0; i < content.Length() ; i++)
+ {
+ if (content[i] == '\r') lines++;
+ }
+ return lines ;
+}
+
+wxString wxMacTextControl::GetLineText(long lineNo) const
+{
+ // TODO change this if possible to reflect real lines
+ wxString content = GetStringValue() ;
+
+ // Find line first
+ int count = 0;
+ for (size_t i = 0; i < content.Length() ; i++)
+ {
+ if (count == lineNo)
+ {
+ // Add chars in line then
+ wxString tmp;
+
+ for (size_t j = i; j < content.Length(); j++)
+ {
+ if (content[j] == '\n')
+ return tmp;
+
+ tmp += content[j];
+ }
+
+ return tmp;
+ }
+ if (content[i] == '\n') count++;
+ }
+ return wxEmptyString ;
+}
+
+int wxMacTextControl::GetLineLength(long lineNo) const
+{
+ // TODO change this if possible to reflect real lines
+ wxString content = GetStringValue() ;
+
+ // Find line first
+ int count = 0;
+ for (size_t i = 0; i < content.Length() ; i++)
+ {
+ if (count == lineNo)
+ {
+ // Count chars in line then
+ count = 0;
+ for (size_t j = i; j < content.Length(); j++)
+ {
+ count++;
+ if (content[j] == '\n') return count;
+ }
+
+ return count;
+ }
+ if (content[i] == '\n') count++;
+ }
+ return 0 ;
+}
+
+// ----------------------------------------------------------------------------
+// standard unicode control implementation
+// ----------------------------------------------------------------------------
+
+#if TARGET_API_MAC_OSX
+
+wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
+ const wxString& str,
+ const wxPoint& pos,
+ const wxSize& size, long style ) : wxMacTextControl( wxPeer )
+{
+ m_font = wxPeer->GetFont() ;
+ m_windowStyle = style ;
+ Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
+ wxString st = str ;
+ wxMacConvertNewlines10To13( &st ) ;
+ wxMacCFStringHolder cf(st , m_font.GetEncoding()) ;
+ CFStringRef cfr = cf ;
+ Boolean isPassword = ( m_windowStyle & wxTE_PASSWORD ) != 0 ;
+ m_valueTag = isPassword ? kControlEditTextPasswordCFStringTag : kControlEditTextCFStringTag ;
+ CreateEditUnicodeTextControl( MAC_WXHWND(wxPeer->MacGetTopLevelWindowRef()), &bounds , cfr , isPassword , NULL , &m_controlRef ) ;
+
+ if ( !(m_windowStyle & wxTE_MULTILINE) )
+ {
+ SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
+ }
+}
+
+wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
+{
+}
+
+void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
+{
+ if ( !(m_windowStyle & wxTE_MULTILINE) && shown )
+ {
+ // work around a refresh issue insofar as not always the entire content is shown even if this would be possible
+ ControlEditTextSelectionRec sel ;
+ CFStringRef value = NULL ;
+
+ verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
+ verify_noerr( GetData<CFStringRef>( 0, m_valueTag , &value ) );
+ verify_noerr( SetData<CFStringRef>( 0, m_valueTag, &value ) );
+ verify_noerr( SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) );
+
+ CFRelease( value ) ;
+ }
+}
+wxString wxMacUnicodeTextControl::GetStringValue() const
+{
+ wxString result ;
+ CFStringRef value = GetData<CFStringRef>(0,m_valueTag) ;
+ if ( value )
+ {
+ wxMacCFStringHolder cf(value) ;
+ result = cf.AsString() ;
+ }
+#if '\n' == 10
+ wxMacConvertNewlines13To10( &result ) ;
+#else
+ wxMacConvertNewlines10To13( &result ) ;
+#endif
+ return result ;
+}
+void wxMacUnicodeTextControl::SetStringValue( const wxString &str)
+{
+ wxString st = str ;
+ wxMacConvertNewlines10To13( &st ) ;
+ wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
+ verify_noerr( SetData<CFStringRef>( 0, m_valueTag , cf ) ) ;
+}
+void wxMacUnicodeTextControl::Copy()
+{
+ SendHICommand( kHICommandCopy ) ;
+}
+void wxMacUnicodeTextControl::Cut()
+{
+ SendHICommand( kHICommandCut ) ;
+}
+void wxMacUnicodeTextControl::Paste()
+{
+ SendHICommand( kHICommandPaste ) ;
+}
+bool wxMacUnicodeTextControl::CanPaste() const
+{
+ return true ;
+}
+void wxMacUnicodeTextControl::SetEditable(bool editable)
+{
+ SetData<Boolean>( 0 , kControlEditTextLockedTag , (Boolean) !editable ) ;
+}
+void wxMacUnicodeTextControl::Remove( long from , long to )
+{
+}
+
+void wxMacUnicodeTextControl::GetSelection( long* from, long* to) const
+{
+ ControlEditTextSelectionRec sel ;
+ verify_noerr(GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
+ if ( from ) *from = sel.selStart ;
+ if ( to ) *to = sel.selEnd ;
+}
+
+void wxMacUnicodeTextControl::SetSelection( long from , long to )
+{
+ ControlEditTextSelectionRec sel ;
+ if ((from == -1) && (to == -1))
+ {
+ from = 0 ;
+ to = 32767 ; // sel has 16 bit signed values, max is 32767
+ }
+ sel.selStart = from ;
+ sel.selEnd = to ;
+ SetData<ControlEditTextSelectionRec>( 0 , kControlEditTextSelectionTag, &sel ) ;
+}
+
+void wxMacUnicodeTextControl::WriteText(const wxString& str)
+{
+ wxString st = str ;
+ wxMacConvertNewlines10To13( &st ) ;
+ #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
+ wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
+ CFStringRef value = cf ;
+ SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
+ #else
+ wxString val = GetStringValue() ;
+ long start , end ;
+ GetSelection( &start , &end ) ;
+ val.Remove( start , end - start ) ;
+ val.insert( start , str ) ;
+ SetStringValue( val ) ;
+ SetSelection( start + str.Length() , start + str.Length() ) ;
+ #endif
+}
+
+#endif
+
+// ----------------------------------------------------------------------------
+// MLTE control implementation (common part)
+// ----------------------------------------------------------------------------
+
+// if mlte is on read only , no changes at all are allowed, not even from
+// procedural API, in order to allow changes via API all the same we must undo
+// the readonly status while we are executing, this class helps to do so
+
+class wxMacEditHelper
+{
+public :
+ wxMacEditHelper( TXNObject txn )
+ {
+ TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
+ m_txn = txn ;
+ TXNGetTXNObjectControls( m_txn , 1 , tag , m_data ) ;
+ if ( m_data[0].uValue == kTXNReadOnly )
+ {
+ TXNControlData data[] = { { kTXNReadWrite } } ;
+ TXNSetTXNObjectControls( m_txn , false , 1 , tag , data ) ;
+ }
+ }
+ ~wxMacEditHelper()
+ {
+ TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
+ if ( m_data[0].uValue == kTXNReadOnly )
+ {
+ TXNSetTXNObjectControls( m_txn , false , 1 , tag , m_data ) ;
+ }
+ }
+ protected :
+ TXNObject m_txn ;
+ TXNControlData m_data[1] ;
+} ;
+
+wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl *peer ) : wxMacTextControl( peer )
+{
+ SetNeedsFocusRect( true ) ;
+}
+
+wxString wxMacMLTEControl::GetStringValue() const
+{
+ wxString result ;
+ OSStatus err ;
+ Size actualSize = 0;
+ {
+#if wxUSE_UNICODE
+ Handle theText ;
+ err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText , kTXNUnicodeTextData );
+ // all done
+ if ( err )
+ {
+ actualSize = 0 ;
+ }
+ else
+ {
+ actualSize = GetHandleSize( theText ) / sizeof( UniChar) ;
+ if ( actualSize > 0 )
+ {
+ wxChar *ptr = NULL ;
+#if SIZEOF_WCHAR_T == 2
+ ptr = new wxChar[actualSize + 1 ] ;
+ wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ;
+#else
+ SetHandleSize( theText , ( actualSize + 1 ) * sizeof( UniChar ) ) ;
+ HLock( theText ) ;
+ (((UniChar*)*theText)[actualSize]) = 0 ;
+ wxMBConvUTF16 converter ;
+ size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
+ ptr = new wxChar[noChars + 1] ;
+
+ noChars = converter.MB2WC( ptr , (const char*)*theText , noChars ) ;
+ ptr[noChars] = 0 ;
+ HUnlock( theText ) ;
+#endif
+ ptr[actualSize] = 0 ;
+ result = wxString( ptr ) ;
+ delete[] ptr ;
+ }
+ DisposeHandle( theText ) ;
+ }
+#else
+ Handle theText ;
+ err = TXNGetDataEncoded( m_txn , kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
+ // all done
+ if ( err )
+ {
+ actualSize = 0 ;
+ }
+ else
+ {
+ actualSize = GetHandleSize( theText ) ;
+ if ( actualSize > 0 )
+ {
+ HLock( theText ) ;
+ result = wxString( *theText , wxConvLocal , actualSize ) ;
+ HUnlock( theText ) ;
+ }
+ DisposeHandle( theText ) ;
+ }