📄 textctrl.cpp
字号:
/////////////////////////////////////////////////////////////////////////////// Name: src/mac/classic/textctrl.cpp// Purpose: wxTextCtrl// Author: Stefan Csomor// Modified by:// Created: 1998-01-01// RCS-ID: $Id: textctrl.cpp,v 1.24 2006/09/24 11:40:25 JS Exp $// Copyright: (c) Stefan Csomor// Licence: wxWindows licence/////////////////////////////////////////////////////////////////////////////#include "wx/wxprec.h"#ifdef __BORLANDC__ #pragma hdrstop#endif#if wxUSE_TEXTCTRL#include "wx/textctrl.h"#ifndef WX_PRECOMP #include "wx/app.h" #include "wx/utils.h" #include "wx/dc.h" #include "wx/button.h" #include "wx/settings.h" #include "wx/msgdlg.h" #include "wx/toplevel.h"#endif#ifdef __DARWIN__ #include <sys/types.h> #include <sys/stat.h>#else #include <stat.h>#endif#if wxUSE_STD_IOSTREAM #if wxUSE_IOSTREAMH #include <fstream.h> #else #include <fstream> #endif#endif#include "wx/notebook.h"#include "wx/tabctrl.h"#include "wx/filefn.h"#if defined(__BORLANDC__) && !defined(__WIN32__) #include <alloc.h>#elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__) #include <malloc.h>#endif#ifndef __DARWIN__#include <Scrap.h>#endif#include <MacTextEditor.h>#include <ATSUnicode.h>#include <TextCommon.h>#include <TextEncodingConverter.h>#include "wx/mac/uma.h"#define TE_UNLIMITED_LENGTH 0xFFFFFFFFULextern wxControl *wxFindControlFromMacControl(ControlHandle inControl ) ;// CS:TODO we still have a problem getting properly at the text events of a control because under Carbon// the MLTE engine registers itself for the key events thus the normal flow never occurs, the only measure for the// moment is to avoid setting the true focus on the control, the proper solution at the end would be to have// an alternate path for carbon key events that routes automatically into the same wx flow of events/* part codes *//* kmUPTextPart is the part code we return to indicate the user has clicked in the text area of our control */#define kmUPTextPart 1/* kmUPScrollPart is the part code we return to indicate the user has clicked in the scroll bar part of the control. */#define kmUPScrollPart 2/* routines for using existing user pane controls. These routines are useful for cases where you would like to use an existing user pane control in, say, a dialog window as a scrolling text edit field.*//* 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);/* Utility Routines */enum { kShiftKeyCode = 56};/* kUserClickedToFocusPart is a part code we pass to the SetKeyboardFocus routine. In our focus switching routine this part code is understood as meaning 'the user has clicked in the control and we need to switch the current focus to ourselves before we can continue'. */#define kUserClickedToFocusPart 100/* kmUPClickScrollDelayTicks is a time measurement in ticks used to slow the speed of 'auto scrolling' inside of our clickloop routine. This value prevents the text from wizzzzzing by while the mouse is being held down inside of the text area. */#define kmUPClickScrollDelayTicks 3/* STPTextPaneVars is a structure used for storing the the mUP Control's internal variables and state information. A handle to this record is stored in the pane control's reference value field using the SetControlReference routine. */typedef struct { /* OS records referenced */ TXNObject fTXNRec; /* the txn record */ TXNFrameID fTXNFrame; /* the txn frame ID */ ControlHandle fUserPaneRec; /* handle to the user pane control */ WindowPtr fOwner; /* window containing control */ GrafPtr fDrawingEnvironment; /* grafport where control is drawn */ /* flags */ Boolean fInFocus; /* true while the focus rect is drawn around the control */ Boolean fIsActive; /* true while the control is drawn in the active state */ Boolean fTEActive; /* reflects the activation state of the text edit record */ Boolean fInDialogWindow; /* true if displayed in a dialog window */ /* calculated locations */ Rect fRTextArea; /* area where the text is drawn */ Rect fRFocusOutline; /* rectangle used to draw the focus box */ Rect fRTextOutline; /* rectangle used to draw the border */ RgnHandle fTextBackgroundRgn; /* background region for the text, erased before calling TEUpdate */ /* our focus advance override routine */ EventHandlerUPP handlerUPP; EventHandlerRef handlerRef; bool fMultiline ;} STPTextPaneVars;/* Univerals Procedure Pointer variables used by the mUP Control. These variables are set up the first time that mUPOpenControl is called. */ControlUserPaneDrawUPP gTPDrawProc = NULL;ControlUserPaneHitTestUPP gTPHitProc = NULL;ControlUserPaneTrackingUPP gTPTrackProc = NULL;ControlUserPaneIdleUPP gTPIdleProc = NULL;ControlUserPaneKeyDownUPP gTPKeyProc = NULL;ControlUserPaneActivateUPP gTPActivateProc = NULL;ControlUserPaneFocusUPP gTPFocusProc = NULL;/* TPActivatePaneText activates or deactivates the text edit record according to the value of setActive. The primary purpose of this routine is to ensure each call is only made once. */static void TPActivatePaneText(STPTextPaneVars **tpvars, Boolean setActive) { STPTextPaneVars *varsp; varsp = *tpvars; if (varsp->fTEActive != setActive) { varsp->fTEActive = setActive; TXNActivate(varsp->fTXNRec, varsp->fTXNFrame, varsp->fTEActive); if (varsp->fInFocus) TXNFocus( varsp->fTXNRec, varsp->fTEActive); }}/* TPFocusPaneText set the focus state for the text record. */static void TPFocusPaneText(STPTextPaneVars **tpvars, Boolean setFocus) { STPTextPaneVars *varsp; varsp = *tpvars; if (varsp->fInFocus != setFocus) { varsp->fInFocus = setFocus; TXNFocus( varsp->fTXNRec, varsp->fInFocus); }}/* TPPaneDrawProc is called to redraw the control and for update events referring to the control. This routine erases the text area's background, and redraws the text. This routine assumes the scroll bar has been redrawn by a call to DrawControls. */static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart) { STPTextPaneVars **tpvars, *varsp; char state; Rect bounds; /* set up our globals */ tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* save the drawing state */ SetPort((**tpvars).fDrawingEnvironment); /* verify our boundary */ GetControlBounds(theControl, &bounds); wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; if ( ! EqualRect(&bounds, &varsp->fRFocusOutline) ) { // scrollbar is on the border, we add one Rect oldbounds = varsp->fRFocusOutline ; InsetRect( &oldbounds , -1 , -1 ) ; 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); 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); } }}/* TPPaneHitTestProc is called when the control manager would like to determine what part of the control the mouse resides over. We also call this routine from our tracking proc to determine how to handle mouse clicks. */static pascal ControlPartCode TPPaneHitTestProc(ControlHandle theControl, Point where) { STPTextPaneVars **tpvars; ControlPartCode result; char state; /* set up our locals and lock down our globals*/ result = 0; tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL && IsControlVisible( theControl) ) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); /* find the region where we clicked */ if (PtInRect(where, &(**tpvars).fRTextArea)) { result = kmUPTextPart; } else result = 0; /* release oure globals */ HSetState((Handle) tpvars, state); } return result;}/* TPPaneTrackingProc is called when the mouse is being held down over our control. This routine handles clicks in the text area and in the scroll bar. */static pascal ControlPartCode TPPaneTrackingProc(ControlHandle theControl, Point startPt, ControlActionUPP actionProc) { STPTextPaneVars **tpvars, *varsp; char state; ControlPartCode partCodeResult; /* make sure we have some variables... */ partCodeResult = 0; tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL && IsControlVisible( theControl ) ) { /* lock 'em down */ state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* we don't do any of these functions unless we're in focus */ if ( ! varsp->fInFocus) { WindowPtr owner; owner = GetControlOwner(theControl); ClearKeyboardFocus(owner); SetKeyboardFocus(owner, theControl, kUserClickedToFocusPart); } /* find the location for the click */ switch (TPPaneHitTestProc(theControl, startPt)) { /* handle clicks in the text part */ case kmUPTextPart: { SetPort((**tpvars).fDrawingEnvironment); wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;#if !TARGET_CARBON TXNClick( varsp->fTXNRec, (const EventRecord*) wxTheApp->MacGetCurrentEvent());#else EventRecord rec ; ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ; TXNClick( varsp->fTXNRec, &rec );#endif } break; } HSetState((Handle) tpvars, state); } return partCodeResult;}/* TPPaneIdleProc is our user pane idle routine. When our text field is active and in focus, we use this routine to set the cursor. */static pascal void TPPaneIdleProc(ControlHandle theControl) { STPTextPaneVars **tpvars, *varsp; /* set up locals */ tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL && IsControlVisible( theControl ) ) { /* if we're not active, then we have nothing to say about the cursor */ if ((**tpvars).fIsActive) { char state; Rect bounds; Point mousep; /* lock down the globals */ state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* get the current mouse coordinates (in our window) */ SetPortWindowPort(GetControlOwner(theControl)); wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; GetMouse(&mousep); /* there's a 'focus thing' and an 'unfocused thing' */ if (varsp->fInFocus) { /* flash the cursor */ SetPort((**tpvars).fDrawingEnvironment); TXNIdle(varsp->fTXNRec); /* set the cursor */ if (PtInRect(mousep, &varsp->fRTextArea)) { RgnHandle theRgn; RectRgn((theRgn = NewRgn()), &varsp->fRTextArea); TXNAdjustCursor(varsp->fTXNRec, theRgn); DisposeRgn(theRgn); } else { // SetThemeCursor(kThemeArrowCursor); } } else { /* if it's in our bounds, set the cursor */ GetControlBounds(theControl, &bounds); if (PtInRect(mousep, &bounds)) { // SetThemeCursor(kThemeArrowCursor); } } HSetState((Handle) tpvars, state); } }}/* TPPaneKeyDownProc is called whenever a keydown event is directed at our control. Here, we direct the keydown event to the text edit record and redraw the scroll bar and text field as appropriate. */static pascal ControlPartCode TPPaneKeyDownProc(ControlHandle theControl, SInt16 keyCode, SInt16 charCode, SInt16 modifiers) { STPTextPaneVars **tpvars; tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL) { if ((**tpvars).fInFocus) { /* turn autoscrolling on and send the key event to text edit */ SetPort((**tpvars).fDrawingEnvironment); wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; EventRecord ev ; memset( &ev , 0 , sizeof( ev ) ) ; ev.what = keyDown ; ev.modifiers = modifiers ; ev.message = (( keyCode << 8 ) & keyCodeMask ) + ( charCode & charCodeMask ) ; TXNKeyDown( (**tpvars).fTXNRec, &ev); } } return kControlEntireControl;}/* TPPaneActivateProc is called when the window containing the user pane control receives activate events. Here, we redraw the control and it's text as necessary for the activation state. */static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activating) { Rect bounds; STPTextPaneVars **tpvars, *varsp; char state; /* set up locals */ tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* 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); /* redraw the frame */ if ( IsControlVisible( theControl ) ) { DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive); if (varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive); } HSetState((Handle) tpvars, state); }}/* TPPaneFocusProc is called when every the focus changes to or from our control. Herein, switch the focus appropriately according to the parameters and redraw the control as necessary. */static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlFocusPart action) { ControlPartCode focusResult; STPTextPaneVars **tpvars, *varsp; char state; /* set up locals */ focusResult = kControlFocusNoPart; tpvars = (STPTextPaneVars **) GetControlReference(theControl); if (tpvars != NULL ) { state = HGetState((Handle) tpvars); HLock((Handle) tpvars); varsp = *tpvars; /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -