X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ef4a634b1b90cb13426b9006d8e18380b9809fdb..3ef37e7f4ec5f0688a4fb47e530ca2d30a6bb3ed:/src/mac/textctrl.cpp?ds=sidebyside diff --git a/src/mac/textctrl.cpp b/src/mac/textctrl.cpp index 5f71de9973..a28eb9241c 100644 --- a/src/mac/textctrl.cpp +++ b/src/mac/textctrl.cpp @@ -60,7 +60,6 @@ #define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL -extern wxApp *wxTheApp ; extern wxControl *wxFindControlFromMacControl(ControlHandle inControl ) ; // CS:TODO we still have a problem getting properly at the text events of a control because under Carbon @@ -213,28 +212,37 @@ static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart Rect oldbounds = varsp->fRFocusOutline ; InsetRect( &oldbounds , -1 , -1 ) ; - InvalWindowRect( GetControlOwner( theControl ) , &oldbounds ) ; + if ( IsControlVisible( theControl ) ) + InvalWindowRect( GetControlOwner( theControl ) , &oldbounds ) ; SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom); SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom); SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) , bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2)); RectRgn(varsp->fTextBackgroundRgn, &varsp->fRTextOutline); - TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left, - varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame); + if ( IsControlVisible( theControl ) ) + TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left, + varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame); + else + TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top + 30000 , varsp->fRTextArea.left + 30000 , + varsp->fRTextArea.bottom + 30000 , varsp->fRTextArea.right + 30000 , varsp->fTXNFrame); + } + if ( IsControlVisible( theControl ) ) + { /* update the text region */ - RGBColor white = { 65535 , 65535 , 65535 } ; - RGBBackColor( &white ) ; - EraseRgn(varsp->fTextBackgroundRgn); - TXNDraw(varsp->fTXNRec, NULL); - /* restore the drawing environment */ - /* draw the text frame and focus frame (if necessary) */ - DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); - if ((**tpvars).fIsActive && varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, true); - /* release our globals */ - HSetState((Handle) tpvars, state); - + RGBColor white = { 65535 , 65535 , 65535 } ; + RGBBackColor( &white ) ; + EraseRgn(varsp->fTextBackgroundRgn); + TXNDraw(varsp->fTXNRec, NULL); + /* restore the drawing environment */ + /* draw the text frame and focus frame (if necessary) */ + DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); + if ((**tpvars).fIsActive && varsp->fInFocus) + DrawThemeFocusRect(&varsp->fRFocusOutline, true); + /* release our globals */ + HSetState((Handle) tpvars, state); + } } } @@ -250,7 +258,7 @@ static pascal ControlPartCode TPPaneHitTestProc(ControlHandle theControl, Point /* set up our locals and lock down our globals*/ result = 0; tpvars = (STPTextPaneVars **) GetControlReference(theControl); - if (tpvars != NULL) { + if (tpvars != NULL && IsControlVisible( theControl) ) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); /* find the region where we clicked */ @@ -277,7 +285,7 @@ static pascal ControlPartCode TPPaneTrackingProc(ControlHandle theControl, Point /* make sure we have some variables... */ partCodeResult = 0; tpvars = (STPTextPaneVars **) GetControlReference(theControl); - if (tpvars != NULL) { + if (tpvars != NULL && IsControlVisible( theControl ) ) { /* lock 'em down */ state = HGetState((Handle) tpvars); HLock((Handle) tpvars); @@ -320,7 +328,7 @@ static pascal void TPPaneIdleProc(ControlHandle theControl) { STPTextPaneVars **tpvars, *varsp; /* set up locals */ tpvars = (STPTextPaneVars **) GetControlReference(theControl); - if (tpvars != NULL) { + if (tpvars != NULL && IsControlVisible( theControl ) ) { /* if we're not active, then we have nothing to say about the cursor */ if ((**tpvars).fIsActive) { char state; @@ -405,12 +413,16 @@ static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activati /* de/activate the text edit record */ SetPort((**tpvars).fDrawingEnvironment); wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; - GetControlBounds(theControl, &bounds); - varsp->fIsActive = activating; - TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus); + GetControlBounds(theControl, &bounds); + varsp->fIsActive = activating; + TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus); /* redraw the frame */ - DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); - if (varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive); + if ( IsControlVisible( theControl ) ) + { + DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); + if (varsp->fInFocus) + DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive); + } HSetState((Handle) tpvars, state); } } @@ -427,7 +439,7 @@ static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlF /* set up locals */ focusResult = kControlFocusNoPart; tpvars = (STPTextPaneVars **) GetControlReference(theControl); - if (tpvars != NULL) { + if (tpvars != NULL ) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; @@ -448,10 +460,7 @@ static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlF kControlFocusNoPart, otherwise return a non-zero part code. kUserClickedToFocusPart - is a constant defined for this example. You should define your own value for handling click-to-focus type events. */ - /* save the drawing state */ - SetPort((**tpvars).fDrawingEnvironment); - wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; - /* calculate the next highlight state */ + /* calculate the next highlight state */ switch (action) { default: case kControlFocusNoPart: @@ -468,11 +477,17 @@ static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlF focusResult = varsp->fInFocus ? 1 : kControlFocusNoPart; break; } - TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus); - /* redraw the text fram and focus rectangle to indicate the - new focus state */ - DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); - DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus); + TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus); + /* redraw the text fram and focus rectangle to indicate the + new focus state */ + if ( IsControlVisible( theControl ) ) + { + /* save the drawing state */ + SetPort((**tpvars).fDrawingEnvironment); + wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; + DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); + DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus); + } /* done */ HSetState((Handle) tpvars, state); } @@ -576,6 +591,18 @@ OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle ) kTXNSystemDefaultEncoding, &varsp->fTXNRec, &varsp->fTXNFrame, (TXNObjectRefcon) tpvars); + if ( !IsControlVisible( theControl ) ) + TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top + 30000 , varsp->fRTextArea.left + 30000 , + varsp->fRTextArea.bottom + 30000 , varsp->fRTextArea.right + 30000 , varsp->fTXNFrame); + + + if ( (wxStyle & wxTE_MULTILINE) && (wxStyle & wxTE_DONTWRAP) ) + { + TXNControlTag tag = kTXNWordWrapStateTag ; + TXNControlData dat ; + dat.uValue = kTXNNoAutoWrap ; + TXNSetTXNObjectControls( varsp->fTXNRec , false , 1 , &tag , &dat ) ; + } Str255 fontName ; SInt16 fontSize ; Style fontStyle ; @@ -631,13 +658,16 @@ END_EVENT_TABLE() #endif // Text item -wxTextCtrl::wxTextCtrl() +void wxTextCtrl::Init() { m_macTE = NULL ; m_macTXN = NULL ; m_macTXNvars = NULL ; m_macUsesTXN = false ; + m_editable = true ; + m_dirty = false; + m_maxLength = TE_UNLIMITED_LENGTH ; } @@ -657,7 +687,7 @@ const short kVerticalMargin = 2 ; const short kHorizontalMargin = 2 ; bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, - const wxString& st, + const wxString& str, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, @@ -674,7 +704,7 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, m_macUsesTXN &= (TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress) ; // base initialization - if ( !CreateBase(parent, id, pos, size, style, validator, name) ) + if ( !wxTextCtrlBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) return FALSE; wxSize mySize = size ; @@ -717,12 +747,14 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, m_editable = FALSE ; } + wxString st = str ; + wxMacConvertNewlines13To10( &st ) ; if ( !m_macUsesTXN ) { - m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , "\p" , true , 0 , 0 , 1, + m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , "\p" , false , 0 , 0 , 1, (style & wxTE_PASSWORD) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ; long size ; - ::GetControlData((ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &((TEHandle) m_macTE) , &size ) ; + ::GetControlData((ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*)((TEHandle *)&m_macTE) , &size ) ; } else @@ -733,7 +765,7 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | kControlWantsActivate | kControlHandlesTracking | kControlHasSpecialBackground | kControlGetsFocusOnClick | kControlSupportsLiveFeedback; /* create the control */ - m_macControl = NewControl(MAC_WXHWND(parent->MacGetRootWindow()), &bounds, "\p", true, featurSet, 0, featurSet, kControlUserPaneProc, 0); + m_macControl = NewControl(MAC_WXHWND(parent->MacGetRootWindow()), &bounds, "\p", false , featurSet, 0, featurSet, kControlUserPaneProc, 0); /* set up the mUP specific features and data */ mUPOpenControl((ControlHandle) m_macControl, m_windowStyle ); } @@ -741,7 +773,7 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, if ( !m_macUsesTXN ) { - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ; } else @@ -755,7 +787,7 @@ bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, TXNSetData( ((TXNObject) m_macTXN) , kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2, kTXNStartOffset, kTXNEndOffset); #else - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; TXNSetData( ((TXNObject) m_macTXN) , kTXNTextData, (void*)text.data(), strlen( text ) , kTXNStartOffset, kTXNEndOffset); #endif @@ -787,7 +819,7 @@ wxString wxTextCtrl::GetValue() const ::GetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, actualSize , buf.data() , &actualSize ) ; - result = wxMacMakeStringFromCString( buf ) ; + result = wxString( buf , wxConvLocal) ; } } else @@ -826,14 +858,14 @@ wxString wxTextCtrl::GetValue() const if ( actualSize > 0 ) { HLock( theText ) ; - result = wxMacMakeStringFromCString( *theText , actualSize ) ; + result = wxString( *theText , wxConvLocal , actualSize ) ; HUnlock( theText ) ; } DisposeHandle( theText ) ; } #endif } - + wxMacConvertNewlines10To13( &result ) ; return result ; } @@ -850,23 +882,25 @@ void wxTextCtrl::GetSelection(long* from, long* to) const } } -void wxTextCtrl::SetValue(const wxString& st) +void wxTextCtrl::SetValue(const wxString& str) { + wxString st = str ; + wxMacConvertNewlines13To10( &st ) ; if ( !m_macUsesTXN ) { - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ; } else { - bool formerEditable = IsEditable() ; + bool formerEditable = m_editable ; if ( !formerEditable ) SetEditable(true) ; #if wxUSE_UNICODE TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2 , kTXNStartOffset, kTXNEndOffset); #else - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)text.data(), strlen( text ) , kTXNStartOffset, kTXNEndOffset); #endif @@ -887,7 +921,7 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) { if ( m_macUsesTXN ) { - bool formerEditable = IsEditable() ; + bool formerEditable = m_editable ; if ( !formerEditable ) SetEditable(true) ; TXNTypeAttributes typeAttr[4] ; @@ -931,8 +965,10 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) if ( attrCounter > 0 ) { - OSStatus status = TXNSetTypeAttributes ((TXNObject)m_macTXN, attrCounter , typeAttr, - start,end); +#ifdef __WXDEBUG__ + OSStatus status = +#endif // __WXDEBUG__ + TXNSetTypeAttributes ((TXNObject)m_macTXN, attrCounter , typeAttr, start,end); wxASSERT_MSG( status == noErr , wxT("Couldn't set text attributes") ) ; } if ( !formerEditable ) @@ -1135,8 +1171,10 @@ long wxTextCtrl::GetLastPosition() const } } -void wxTextCtrl::Replace(long from, long to, const wxString& value) +void wxTextCtrl::Replace(long from, long to, const wxString& str) { + wxString value = str ; + wxMacConvertNewlines13To10( &value ) ; if ( !m_macUsesTXN ) { ControlEditTextSelectionRec selection ; @@ -1150,7 +1188,7 @@ void wxTextCtrl::Replace(long from, long to, const wxString& value) } else { - bool formerEditable = IsEditable() ; + bool formerEditable = m_editable ; if ( !formerEditable ) SetEditable(true) ; TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ; @@ -1181,7 +1219,7 @@ void wxTextCtrl::Remove(long from, long to) } else { - bool formerEditable = IsEditable() ; + bool formerEditable = m_editable ; if ( !formerEditable ) SetEditable(true) ; TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ; @@ -1194,17 +1232,19 @@ void wxTextCtrl::Remove(long from, long to) void wxTextCtrl::SetSelection(long from, long to) { - if ( from == -1 ) - from = 0; - - if ( to == -1 ) - to = GetLastPosition(); - if ( !m_macUsesTXN ) { ControlEditTextSelectionRec selection ; - selection.selStart = from ; - selection.selEnd = to ; + if ((from == -1) && (to == -1)) + { + selection.selStart = 0 ; + selection.selEnd = 32767 ; + } + else + { + selection.selStart = from ; + selection.selEnd = to ; + } TESetSelect( selection.selStart , selection.selEnd , ((TEHandle) m_macTE) ) ; ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; @@ -1218,7 +1258,10 @@ void wxTextCtrl::SetSelection(long from, long to) may force a redraw in the text area. */ SetPort((**tpvars).fDrawingEnvironment); /* change the selection */ - TXNSetSelection( (**tpvars).fTXNRec, from, to); + if ((from == -1) && (to == -1)) + TXNSelectAll((TXNObject) m_macTXN); + else + TXNSetSelection( (**tpvars).fTXNRec, from, to); TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart); } } @@ -1233,16 +1276,18 @@ bool wxTextCtrl::LoadFile(const wxString& file) return FALSE; } -void wxTextCtrl::WriteText(const wxString& st) +void wxTextCtrl::WriteText(const wxString& str) { + wxString st = str ; + wxMacConvertNewlines13To10( &st ) ; if ( !m_macUsesTXN ) { - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; TEInsert( text , strlen(text) , ((TEHandle) m_macTE) ) ; } else { - bool formerEditable = IsEditable() ; + bool formerEditable = m_editable ; if ( !formerEditable ) SetEditable(true) ; long start , end , dummy ; @@ -1251,7 +1296,7 @@ void wxTextCtrl::WriteText(const wxString& st) TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2 , kTXNUseCurrentSelection, kTXNUseCurrentSelection); #else - wxCharBuffer text = wxMacStringToCString( st ) ; + wxCharBuffer text = st.mb_str(wxConvLocal) ; TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)text.data(), strlen( text ) , kTXNUseCurrentSelection, kTXNUseCurrentSelection); #endif @@ -1289,7 +1334,7 @@ void wxTextCtrl::Clear() bool wxTextCtrl::IsModified() const { - return TRUE; + return m_dirty; } bool wxTextCtrl::IsEditable() const @@ -1344,6 +1389,10 @@ void wxTextCtrl::Undo() { if (CanUndo()) { + if ( m_macUsesTXN ) + { + TXNUndo((TXNObject)m_macTXN); + } } } @@ -1351,23 +1400,48 @@ void wxTextCtrl::Redo() { if (CanRedo()) { + if ( m_macUsesTXN ) + { + TXNRedo((TXNObject)m_macTXN); + } } } bool wxTextCtrl::CanUndo() const { + if ( !IsEditable() ) + { + return false ; + } + if ( m_macUsesTXN ) + { + return TXNCanUndo((TXNObject)m_macTXN,NULL); + } return FALSE ; } bool wxTextCtrl::CanRedo() const { + if ( !IsEditable() ) + { + return false ; + } + if ( m_macUsesTXN ) + { + return TXNCanRedo((TXNObject)m_macTXN,NULL); + } return FALSE ; } -// Makes 'unmodified' +// Makes modifie or unmodified +void wxTextCtrl::MarkDirty() +{ + m_dirty = true; +} + void wxTextCtrl::DiscardEdits() { - // TODO + m_dirty = false; } int wxTextCtrl::GetNumberOfLines() const @@ -1416,12 +1490,12 @@ int wxTextCtrl::GetLineLength(long lineNo) const for (size_t j = i; j < content.Length(); j++) { count++; - if (content[j] == '\r') return count; + if (content[j] == '\n') return count; } return count; } - if (content[i] == '\r') count++; + if (content[i] == '\n') count++; } return 0; } @@ -1442,7 +1516,7 @@ wxString wxTextCtrl::GetLineText(long lineNo) const for (size_t j = i; j < content.Length(); j++) { - if (content[j] == '\r') + if (content[j] == '\n') return tmp; tmp += content[j]; @@ -1450,7 +1524,7 @@ wxString wxTextCtrl::GetLineText(long lineNo) const return tmp; } - if (content[i] == '\r') count++; + if (content[i] == '\n') count++; } return wxEmptyString ; } @@ -1494,6 +1568,10 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) // eat it return ; } + + // assume that any key not processed yet is going to modify the control + m_dirty = true; + if ( key == 'v' && event.MetaDown() ) { if ( CanPaste() ) @@ -1566,8 +1644,33 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) if (!eat_key) { - // default handling - event.Skip() ; + // perform keystroke handling +#if TARGET_CARBON + if ( m_macUsesTXN && 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 ; + + ::HandleControlKey( (ControlHandle) m_macControl , keycode , keychar , ev->modifiers ) ; + } + } +#else + EventRecord *ev = (EventRecord*) wxTheApp->MacGetCurrentEvent() ; + short keycode ; + short keychar ; + keychar = short(ev->message & charCodeMask); + keycode = short(ev->message & keyCodeMask) >> 8 ; + + ::HandleControlKey( (ControlHandle) m_macControl , keycode , keychar , ev->modifiers ) ; +#endif } if ( ( key >= 0x20 && key < WXK_START ) || key == WXK_RETURN ||