#if wxUSE_TEXTCTRL
+#include "wx/textctrl.h"
+
+#ifndef WX_PRECOMP
+ #include "wx/intl.h"
+ #include "wx/app.h"
+ #include "wx/utils.h"
+ #include "wx/dc.h"
+ #include "wx/button.h"
+ #include "wx/menu.h"
+ #include "wx/settings.h"
+ #include "wx/msgdlg.h"
+#endif
#ifdef __DARWIN__
- #include <sys/types.h>
- #include <sys/stat.h>
+ #include <sys/types.h>
+ #include <sys/stat.h>
#else
- #include <stat.h>
+ #include <stat.h>
#endif
-#include "wx/msgdlg.h"
-
#if wxUSE_STD_IOSTREAM
#if wxUSE_IOSTREAMH
#include <fstream.h>
#endif
#endif
-#include "wx/app.h"
-#include "wx/dc.h"
-#include "wx/button.h"
#include "wx/toplevel.h"
-#include "wx/textctrl.h"
-#include "wx/settings.h"
#include "wx/filefn.h"
-#include "wx/utils.h"
#include "wx/sysopt.h"
-#include "wx/menu.h"
-#include "wx/intl.h"
#if defined(__BORLANDC__) && !defined(__WIN32__)
- #include <alloc.h>
+ #include <alloc.h>
#elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
- #include <malloc.h>
+ #include <malloc.h>
#endif
#ifndef __DARWIN__
virtual wxString GetLineText(long lineNo) const ;
void SetTXNData( const wxString& st , TXNOffset start , TXNOffset end ) ;
+ TXNObject GetTXNObject() { return m_txn ; }
protected :
void TXNSetAttribute( const wxTextAttr& style , long from , long to ) ;
const wxString& str,
const wxPoint& pos,
const wxSize& size, long style ) ;
+ ~wxMacMLTEHIViewControl() ;
+
virtual OSStatus SetFocus( ControlFocusPart focusPart ) ;
virtual bool HasFocus() const ;
virtual void SetBackground( const wxBrush &brush) ;
protected :
HIViewRef m_scrollView ;
HIViewRef m_textView ;
+ EventHandlerRef m_textEventHandlerRef ;
};
#endif
protected :
// contains the tag for the content (is different for password and non-password controls)
OSType m_valueTag ;
+
+ // as the selection tag only works correctly when the control has the focus we have to mirror the
+ // intended value
+ EventHandlerRef m_focusHandlerRef ;
+public :
+ ControlEditTextSelectionRec m_selection ;
};
#endif
if ( key == 'a' && event.MetaDown() )
{
- SelectAll() ;
-
+ SelectAll() ;
+
return ;
}
#if TARGET_API_MAC_OSX
+// the current unicode textcontrol implementation has a bug : only if the control
+// is currently having the focus, the selection can be retrieved by the corresponding
+// data tag. So we have a mirroring using a member variable
+// TODO : build event table using virtual member functions for wxMacControl
+
+static const EventTypeSpec unicodeTextControlEventList[] =
+{
+ { kEventClassControl , kEventControlSetFocusPart } ,
+} ;
+
+static pascal OSStatus wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ wxMacUnicodeTextControl* focus = (wxMacUnicodeTextControl*) data ;
+ wxMacCarbonEvent cEvent( event ) ;
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventControlSetFocusPart :
+ {
+ ControlPartCode controlPart = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart , typeControlPartCode );
+ if ( controlPart == kControlFocusNoPart )
+ {
+ // about to loose focus -> store selection to field
+ focus->GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
+ }
+ result = CallNextEventHandler(handler,event) ;
+ if ( controlPart != kControlFocusNoPart )
+ {
+ // about to gain focus -> set selection from field
+ focus->SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &focus->m_selection );
+ }
+ break;
+ }
+ default:
+ break ;
+ }
+
+ return result ;
+}
+
+static pascal OSStatus wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassControl :
+ result = wxMacUnicodeTextControlControlEventHandler( handler , event , data ) ;
+ break ;
+
+ default :
+ break ;
+ }
+ return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler )
+
wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl *wxPeer,
const wxString& str,
const wxPoint& pos,
if ( !(m_windowStyle & wxTE_MULTILINE) )
SetData<Boolean>( kControlEditTextPart , kControlEditTextSingleLineTag , true ) ;
+
+ InstallControlEventHandler( m_controlRef , GetwxMacUnicodeTextControlEventHandlerUPP(),
+ GetEventTypeCount(unicodeTextControlEventList), unicodeTextControlEventList, this,
+ &m_focusHandlerRef);
}
wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
{
+ ::RemoveEventHandler( m_focusHandlerRef );
}
void wxMacUnicodeTextControl::VisibilityChanged(bool shown)
void wxMacUnicodeTextControl::GetSelection( long* from, long* to ) const
{
ControlEditTextSelectionRec sel ;
- verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
+ if (HasFocus())
+ verify_noerr( GetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ) ;
+ else
+ sel = m_selection ;
+
if ( from )
*from = sel.selStart ;
if ( to )
void wxMacUnicodeTextControl::SetSelection( long from , long to )
{
ControlEditTextSelectionRec sel ;
+ wxString result ;
+ int textLength = 0 ;
+ CFStringRef value = GetData<CFStringRef>(0, m_valueTag) ;
+ if ( value )
+ {
+ wxMacCFStringHolder cf(value) ;
+ textLength = cf.AsString().Length() ;
+ }
+
if ((from == -1) && (to == -1))
{
from = 0 ;
- to = 32767 ; // sel has 16 bit signed values, max is 32767
+ to = textLength ;
+ }
+ else
+ {
+ from = wxMin(textLength,wxMax(from,0)) ;
+ to = wxMax(0,wxMin(textLength,to)) ;
}
sel.selStart = from ;
sel.selEnd = to ;
- SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
+ if ( HasFocus() )
+ SetData<ControlEditTextSelectionRec>( 0, kControlEditTextSelectionTag, &sel ) ;
+ else
+ m_selection = sel;
}
void wxMacUnicodeTextControl::WriteText( const wxString& str )
wxMacConvertNewlines10To13( &st ) ;
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
+ if ( HasFocus() )
+ {
wxMacCFStringHolder cf(st , m_font.GetEncoding() ) ;
CFStringRef value = cf ;
SetData<CFStringRef>( 0, kControlEditTextInsertCFStringRefTag, &value );
-#else
+ }
+ else
+#endif
+ {
wxString val = GetStringValue() ;
long start , end ;
GetSelection( &start , &end ) ;
val.insert( start , str ) ;
SetStringValue( val ) ;
SetSelection( start + str.length() , start + str.length() ) ;
-#endif
+ }
}
#endif
(((UniChar*)*theText)[actualSize]) = 0 ;
wxMBConvUTF16 converter ;
size_t noChars = converter.MB2WC( NULL , (const char*)*theText , 0 ) ;
+ wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Unable to count the number of characters in this string!") );
ptr = new wxChar[noChars + 1] ;
- noChars = converter.MB2WC( ptr , (const char*)*theText , noChars ) ;
+ noChars = converter.MB2WC( ptr , (const char*)*theText , noChars + 1 ) ;
+ wxASSERT_MSG( noChars != wxCONV_FAILED, _T("Conversion of string failed!") );
ptr[noChars] = 0 ;
HUnlock( theText ) ;
#endif
void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr& style , long from , long to )
{
TXNTypeAttributes typeAttr[4] ;
- Str255 fontName = "\pMonaco" ;
- SInt16 fontSize = 12 ;
- Style fontStyle = normal ;
RGBColor color ;
int attrCount = 0 ;
if ( style.HasFont() )
{
const wxFont &font = style.GetFont() ;
+
+#if 0 // old version
+ Str255 fontName = "\pMonaco" ;
+ SInt16 fontSize = 12 ;
+ Style fontStyle = normal ;
wxMacStringToPascal( font.GetFaceName() , fontName ) ;
fontSize = font.GetPointSize() ;
if ( font.GetUnderlined() )
typeAttr[attrCount].size = kTXNQDFontStyleAttributeSize ;
typeAttr[attrCount].data.dataValue = fontStyle ;
attrCount++ ;
+#else
+ typeAttr[attrCount].tag = kTXNATSUIStyle ;
+ typeAttr[attrCount].size = kTXNATSUIStyleSize ;
+ typeAttr[attrCount].data.dataValue = (UInt32)font.MacGetATSUStyle() ;
+ attrCount++ ;
+#endif
}
if ( style.HasTextColour() )
if ( attrCount > 0 )
{
verify_noerr( TXNSetTypeAttributes( m_txn , attrCount , typeAttr, from , to ) );
+ // unfortunately the relayout is not automatic
+ TXNRecalcTextLayout( m_txn );
}
}
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
+// tiger multi-line textcontrols with no CR in the entire content
+// don't scroll automatically, so we need a hack.
+// This attempt only works 'before' the key (ie before CallNextEventHandler)
+// is processed, thus the scrolling always occurs one character too late, but
+// better than nothing ...
+
+static const EventTypeSpec eventList[] =
+{
+ { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
+} ;
+
+static pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+ wxMacMLTEHIViewControl* focus = (wxMacMLTEHIViewControl*) data ;
+
+ switch ( GetEventKind( event ) )
+ {
+ case kEventTextInputUnicodeForKeyEvent :
+ {
+ if ( UMAGetSystemVersion() >= 0x1040 )
+ {
+ TXNOffset from , to ;
+ TXNGetSelection( focus->GetTXNObject() , &from , &to ) ;
+ if ( from == to )
+ TXNShowSelection( focus->GetTXNObject() , kTXNShowStart );
+ }
+ result = CallNextEventHandler(handler,event);
+ break;
+ }
+ default:
+ break ;
+ }
+
+ return result ;
+}
+
+static pascal OSStatus wxMacTextControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
+{
+ OSStatus result = eventNotHandledErr ;
+
+ switch ( GetEventClass( event ) )
+ {
+ case kEventClassTextInput :
+ result = wxMacUnicodeTextEventHandler( handler , event , data ) ;
+ break ;
+
+ default :
+ break ;
+ }
+ return result ;
+}
+
+DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler )
+
wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl *wxPeer,
const wxString& str,
const wxPoint& pos,
TXNSetSelection( m_txn, 0, 0 );
TXNShowSelection( m_txn, kTXNShowStart );
+
+ InstallControlEventHandler( m_textView , GetwxMacTextControlEventHandlerUPP(),
+ GetEventTypeCount(eventList), eventList, this,
+ &m_textEventHandlerRef);
+}
+
+wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
+{
+ ::RemoveEventHandler( m_textEventHandlerRef ) ;
}
OSStatus wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart )