📄 textctrl.cpp
字号:
tabbing forwards (or shift tabbing backwards) through the items in the dialog, and kControlFocusNextPart will be received. When the user clicks in our field and it is not the current focus, then the constant kUserClickedToFocusPart will be received. The constant kControlFocusNoPart will be received when our control is the current focus and the user clicks in another control. In your focus routine, you should respond to these codes as follows: kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw the control and the focus rectangle as necessary. kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off depending on its current state. redraw the control and the focus rectangle as appropriate for the new focus state. If the focus state is 'off', return the constant 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. */ /* calculate the next highlight state */ switch (action) { default: case kControlFocusNoPart: TPFocusPaneText(tpvars, false); focusResult = kControlFocusNoPart; break; case kUserClickedToFocusPart: TPFocusPaneText(tpvars, true); focusResult = 1; break; case kControlFocusPrevPart: case kControlFocusNextPart: TPFocusPaneText(tpvars, ( ! varsp->fInFocus)); 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 */ 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); } return focusResult;}/* mUPOpenControl initializes a user pane control so it will be drawn and will behave as a scrolling text edit field inside of a window. This routine performs all of the initialization steps necessary, except it does not create the user pane control itself. theControl should refer to a user pane control that you have either created yourself or extracted from a dialog's control heirarchy using the GetDialogItemAsControl routine. */OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle ){ Rect bounds; WindowRef theWindow; STPTextPaneVars **tpvars, *varsp; OSStatus err = noErr ; RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}; TXNBackground tback; /* set up our globals */ if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(TPPaneDrawProc); if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(TPPaneHitTestProc); if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(TPPaneTrackingProc); if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(TPPaneIdleProc); if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc); if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(TPPaneActivateProc); if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(TPPaneFocusProc); /* allocate our private storage */ tpvars = (STPTextPaneVars **) NewHandleClear(sizeof(STPTextPaneVars)); SetControlReference(theControl, (long) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* set the initial settings for our private data */ varsp->fMultiline = wxStyle & wxTE_MULTILINE ; varsp->fInFocus = false; varsp->fIsActive = true; varsp->fTEActive = true; // in order to get a deactivate varsp->fUserPaneRec = theControl; theWindow = varsp->fOwner = GetControlOwner(theControl); varsp->fDrawingEnvironment = (GrafPtr) GetWindowPort(theWindow); varsp->fInDialogWindow = ( GetWindowKind(varsp->fOwner) == kDialogWindowKind ); /* set up the user pane procedures */ SetControlData(theControl, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc); SetControlData(theControl, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc); /* calculate the rectangles used by the control */ GetControlBounds(theControl, &bounds); 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)); /* calculate the background region for the text. In this case, it's kindof and irregular region because we're setting the scroll bar a little ways inside of the text area. */ RectRgn((varsp->fTextBackgroundRgn = NewRgn()), &varsp->fRTextOutline); /* set up the drawing environment */ SetPort(varsp->fDrawingEnvironment); /* create the new edit field */ TXNFrameOptions frameOptions = kTXNDontDrawCaretWhenInactiveMask ; if ( ! ( wxStyle & wxTE_NOHIDESEL ) ) frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ; if ( wxStyle & wxTE_MULTILINE ) { if ( ! ( wxStyle & wxTE_DONTWRAP ) ) frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ; else { frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ; frameOptions |= kTXNWantHScrollBarMask ; } if ( !(wxStyle & wxTE_NO_VSCROLL ) ) frameOptions |= kTXNWantVScrollBarMask ; } else frameOptions |= kTXNSingleLineOnlyMask ; if ( wxStyle & wxTE_READONLY ) frameOptions |= kTXNReadOnlyMask ; TXNNewObject(NULL, varsp->fOwner, &varsp->fRTextArea, frameOptions , kTXNTextEditStyleFrameType, kTXNTextensionFile, 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 ; GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ; TXNTypeAttributes typeAttr[] = { { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } , { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } , { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } , } ; err = TXNSetTypeAttributes (varsp->fTXNRec, sizeof( typeAttr ) / sizeof(TXNTypeAttributes) , typeAttr, kTXNStartOffset, kTXNEndOffset); /* set the field's background */ tback.bgType = kTXNBackgroundTypeRGB; tback.bg.color = rgbWhite; TXNSetBackground( varsp->fTXNRec, &tback); /* unlock our storage */ HUnlock((Handle) tpvars); /* perform final activations and setup for our text field. Here, we assume that the window is going to be the 'active' window. */ TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus); /* all done */ return err;}IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) EVT_DROP_FILES(wxTextCtrl::OnDropFiles) EVT_CHAR(wxTextCtrl::OnChar) EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)END_EVENT_TABLE()static void SetTXNData( TXNObject txn , const wxString& st , TXNOffset start , TXNOffset end ){#if wxUSE_UNICODE#if SIZEOF_WCHAR_T == 2 size_t len = st.length() ; TXNSetData( txn , kTXNUnicodeTextData, (void*)st.wc_str(), len * 2, start, end);#else wxMBConvUTF16BE converter ; ByteCount byteBufferLen = converter.WC2MB( NULL , st.wc_str() , 0 ) ; UniChar *unibuf = (UniChar*) malloc(byteBufferLen) ; converter.WC2MB( (char*) unibuf , st.wc_str() , byteBufferLen ) ; TXNSetData( txn , kTXNUnicodeTextData, (void*)unibuf, byteBufferLen , start, end); free( unibuf ) ;#endif#else wxCharBuffer text = st.mb_str(wxConvLocal) ; TXNSetData( txn , kTXNTextData, (void*)text.data(), strlen( text ) , start, end);#endif}// Text itemvoid 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 ;}wxTextCtrl::~wxTextCtrl(){ if ( m_macUsesTXN ) { SetControlReference((ControlHandle)m_macControl, 0) ; TXNDeleteObject((TXNObject)m_macTXN); /* delete our private storage */ DisposeHandle((Handle) m_macTXNvars); /* zero the control reference */ }}const short kVerticalMargin = 2 ;const short kHorizontalMargin = 2 ;bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, const wxString& str, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name){ m_macTE = NULL ; m_macTXN = NULL ; m_macTXNvars = NULL ; m_macUsesTXN = false ; m_editable = true ; m_macUsesTXN = ! (style & wxTE_PASSWORD ) ; m_macUsesTXN &= (TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress) ; // base initialization if ( !wxTextCtrlBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) return false; wxSize mySize = size ; if ( m_macUsesTXN ) { m_macHorizontalBorder = 5 ; // additional pixels around the real control m_macVerticalBorder = 3 ; } else { m_macHorizontalBorder = 5 ; // additional pixels around the real control m_macVerticalBorder = 5 ; } Rect bounds ; Str255 title ; /* if ( mySize.y == -1 ) { mySize.y = 13 ; if ( m_windowStyle & wxTE_MULTILINE ) mySize.y *= 5 ; mySize.y += 2 * m_macVerticalBorder ; } */ MacPreControlCreate( parent , id , wxEmptyString , pos , mySize ,style, validator , name , &bounds , title ) ; if ( m_windowStyle & wxTE_MULTILINE ) { wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); m_windowStyle |= wxTE_PROCESS_ENTER; } if ( m_windowStyle & wxTE_READONLY) { m_editable = false ; } wxString st = str ; wxMacConvertNewlines13To10( &st ) ; if ( !m_macUsesTXN ) { m_macControl = (WXWidget) ::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 ) ; } else { short featurSet; featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle | kControlWantsActivate | kControlHandlesTracking | kControlHasSpecialBackground | kControlGetsFocusOnClick | kControlSupportsLiveFeedback; /* create the control */ m_macControl = (WXWidget) ::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 ); } MacPostControlCreate() ; if ( !m_macUsesTXN ) { wxCharBuffer text = st.mb_str(wxConvLocal) ; ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ; } else { STPTextPaneVars **tpvars; /* set up locals */ tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl); /* set the text in the record */ m_macTXN = (**tpvars).fTXNRec ; SetTXNData( (TXNObject) m_macTXN , st , kTXNStartOffset, kTXNEndOffset ) ; m_macTXNvars = tpvars ; m_macUsesTXN = true ; TXNSetSelection( (TXNObject) m_macTXN, 0, 0); TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart); } return true;}wxString wxTextCtrl::GetValue() const{ Size actualSize = 0; wxString result ; OSStatus err ; if ( !m_macUsesTXN ) { err = ::GetControlDataSize((ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, &actualSize ) ; if ( err ) return wxEmptyString ; if ( actualSize > 0 ) { wxCharBuffer buf(actualSize) ; ::GetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, actualSize , buf.data() , &actualSize ) ; result = wxString( buf , wxConvLocal) ; } } else {#if wxUSE_UNICODE Handle theText ; err = TXNGetDataEncoded( ((TXNObject) m_macTXN), kTXNStartOffset, kTXNEndOffset, &theText , kTXNUnicodeTextData ); // all done if ( err ) { actualSize = 0 ; } else { actualSize = GetHandleSize( theText ) / sizeof( UniChar) ; if ( actualSize > 0 ) { wxChar *ptr = result.GetWriteBuf(actualSize*sizeof(wxChar)) ;#if SIZEOF_WCHAR_T == 2 wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ;#else wxMBConvUTF16BE converter ; HLock( theText ) ; converter.MB2WC( ptr , (const char*)*theText , actualSize ) ; HUnlock( theText ) ;#endif ptr[actualSize] = 0 ; result.UngetWriteBuf( actualSize *sizeof(wxChar) ) ; } DisposeHandle( theText ) ; }#else Handle theText ; err = TXNGetDataEncoded( ((TXNObject) m_macTXN), 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 ) ; }#endif } wxMacConvertNewlines10To13( &result ) ; return result ;}void wxTextCtrl::GetSelection(long* from, long* to) const{ if ( !m_macUsesTXN )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -