1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
21 #include <sys/types.h>
30 #include "wx/button.h"
31 #include "wx/toplevel.h"
32 #include "wx/textctrl.h"
33 #include "wx/notebook.h"
34 #include "wx/tabctrl.h"
35 #include "wx/settings.h"
36 #include "wx/filefn.h"
39 #if defined(__BORLANDC__) && !defined(__WIN32__)
41 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
48 #include <MacTextEditor.h>
49 #include "ATSUnicode.h"
50 #include "TextCommon.h"
51 #include "TextEncodingConverter.h"
52 #include "wx/mac/uma.h"
54 extern wxApp
*wxTheApp
;
56 // CS:TODO we still have a problem getting properly at the text events of a control because under Carbon
57 // the MLTE engine registers itself for the key events thus the normal flow never occurs, the only measure for the
58 // moment is to avoid setting the true focus on the control, the proper solution at the end would be to have
59 // an alternate path for carbon key events that routes automatically into the same wx flow of events
61 #include "MacTextEditor.h"
65 /* kmUPTextPart is the part code we return to indicate the user has clicked
66 in the text area of our control */
67 #define kmUPTextPart 1
69 /* kmUPScrollPart is the part code we return to indicate the user has clicked
70 in the scroll bar part of the control. */
71 #define kmUPScrollPart 2
74 /* routines for using existing user pane controls.
75 These routines are useful for cases where you would like to use an
76 existing user pane control in, say, a dialog window as a scrolling
79 /* mUPOpenControl initializes a user pane control so it will be drawn
80 and will behave as a scrolling text edit field inside of a window.
81 This routine performs all of the initialization steps necessary,
82 except it does not create the user pane control itself. theControl
83 should refer to a user pane control that you have either created
84 yourself or extracted from a dialog's control heirarchy using
85 the GetDialogItemAsControl routine. */
86 OSStatus
mUPOpenControl(ControlHandle theControl
, bool multiline
);
88 /* Utility Routines */
94 /* kUserClickedToFocusPart is a part code we pass to the SetKeyboardFocus
95 routine. In our focus switching routine this part code is understood
96 as meaning 'the user has clicked in the control and we need to switch
97 the current focus to ourselves before we can continue'. */
98 #define kUserClickedToFocusPart 100
101 /* kmUPClickScrollDelayTicks is a time measurement in ticks used to
102 slow the speed of 'auto scrolling' inside of our clickloop routine.
103 This value prevents the text from wizzzzzing by while the mouse
104 is being held down inside of the text area. */
105 #define kmUPClickScrollDelayTicks 3
108 /* STPTextPaneVars is a structure used for storing the the mUP Control's
109 internal variables and state information. A handle to this record is
110 stored in the pane control's reference value field using the
111 SetControlReference routine. */
114 /* OS records referenced */
115 TXNObject fTXNRec
; /* the txn record */
116 TXNFrameID fTXNFrame
; /* the txn frame ID */
117 ControlHandle fUserPaneRec
; /* handle to the user pane control */
118 WindowPtr fOwner
; /* window containing control */
119 GrafPtr fDrawingEnvironment
; /* grafport where control is drawn */
121 Boolean fInFocus
; /* true while the focus rect is drawn around the control */
122 Boolean fIsActive
; /* true while the control is drawn in the active state */
123 Boolean fTEActive
; /* reflects the activation state of the text edit record */
124 Boolean fInDialogWindow
; /* true if displayed in a dialog window */
125 /* calculated locations */
126 Rect fRTextArea
; /* area where the text is drawn */
127 Rect fRFocusOutline
; /* rectangle used to draw the focus box */
128 Rect fRTextOutline
; /* rectangle used to draw the border */
129 RgnHandle fTextBackgroundRgn
; /* background region for the text, erased before calling TEUpdate */
130 /* our focus advance override routine */
131 EventHandlerUPP handlerUPP
;
132 EventHandlerRef handlerRef
;
139 /* Univerals Procedure Pointer variables used by the
140 mUP Control. These variables are set up
141 the first time that mUPOpenControl is called. */
142 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
143 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
144 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
145 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
146 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
147 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
148 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
150 /* TPActivatePaneText activates or deactivates the text edit record
151 according to the value of setActive. The primary purpose of this
152 routine is to ensure each call is only made once. */
153 static void TPActivatePaneText(STPTextPaneVars
**tpvars
, Boolean setActive
) {
154 STPTextPaneVars
*varsp
;
156 if (varsp
->fTEActive
!= setActive
) {
158 varsp
->fTEActive
= setActive
;
160 TXNActivate(varsp
->fTXNRec
, varsp
->fTXNFrame
, varsp
->fTEActive
);
163 TXNFocus( varsp
->fTXNRec
, varsp
->fTEActive
);
168 /* TPFocusPaneText set the focus state for the text record. */
169 static void TPFocusPaneText(STPTextPaneVars
**tpvars
, Boolean setFocus
) {
170 STPTextPaneVars
*varsp
;
172 if (varsp
->fInFocus
!= setFocus
) {
173 varsp
->fInFocus
= setFocus
;
174 TXNFocus( varsp
->fTXNRec
, varsp
->fInFocus
);
179 /* TPPaneDrawProc is called to redraw the control and for update events
180 referring to the control. This routine erases the text area's background,
181 and redraws the text. This routine assumes the scroll bar has been
182 redrawn by a call to DrawControls. */
183 static pascal void TPPaneDrawProc(ControlRef theControl
, ControlPartCode thePart
) {
184 STPTextPaneVars
**tpvars
, *varsp
;
187 /* set up our globals */
188 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
189 if (tpvars
!= NULL
) {
190 state
= HGetState((Handle
) tpvars
);
191 HLock((Handle
) tpvars
);
194 /* save the drawing state */
195 SetPort((**tpvars
).fDrawingEnvironment
);
196 /* verify our boundary */
197 GetControlBounds(theControl
, &bounds
);
198 if ( ! EqualRect(&bounds
, &varsp
->fRFocusOutline
) ) {
199 // scrollbar is on the border, we add one
200 Rect oldbounds
= varsp
->fRFocusOutline
;
201 InsetRect( &oldbounds
, -1 , -1 ) ;
203 InvalWindowRect( GetControlOwner( theControl
) , &oldbounds
) ;
204 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
205 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
206 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
207 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
208 RectRgn(varsp
->fTextBackgroundRgn
, &varsp
->fRTextOutline
);
209 TXNSetFrameBounds( varsp
->fTXNRec
, varsp
->fRTextArea
.top
, varsp
->fRTextArea
.left
,
210 varsp
->fRTextArea
.bottom
, varsp
->fRTextArea
.right
, varsp
->fTXNFrame
);
213 /* update the text region */
214 RGBColor white
= { 65535 , 65535 , 65535 } ;
215 RGBBackColor( &white
) ;
216 EraseRgn(varsp
->fTextBackgroundRgn
);
217 TXNDraw(varsp
->fTXNRec
, NULL
);
218 /* restore the drawing environment */
219 /* draw the text frame and focus frame (if necessary) */
220 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
221 if ((**tpvars
).fIsActive
&& varsp
->fInFocus
) DrawThemeFocusRect(&varsp
->fRFocusOutline
, true);
222 /* release our globals */
223 HSetState((Handle
) tpvars
, state
);
228 /* TPPaneHitTestProc is called when the control manager would
229 like to determine what part of the control the mouse resides over.
230 We also call this routine from our tracking proc to determine how
231 to handle mouse clicks. */
232 static pascal ControlPartCode
TPPaneHitTestProc(ControlHandle theControl
, Point where
) {
233 STPTextPaneVars
**tpvars
;
234 ControlPartCode result
;
236 /* set up our locals and lock down our globals*/
238 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
239 if (tpvars
!= NULL
) {
240 state
= HGetState((Handle
) tpvars
);
241 HLock((Handle
) tpvars
);
242 /* find the region where we clicked */
243 if (PtInRect(where
, &(**tpvars
).fRTextArea
)) {
244 result
= kmUPTextPart
;
246 /* release oure globals */
247 HSetState((Handle
) tpvars
, state
);
256 /* TPPaneTrackingProc is called when the mouse is being held down
257 over our control. This routine handles clicks in the text area
258 and in the scroll bar. */
259 static pascal ControlPartCode
TPPaneTrackingProc(ControlHandle theControl
, Point startPt
, ControlActionUPP actionProc
) {
260 STPTextPaneVars
**tpvars
, *varsp
;
262 ControlPartCode partCodeResult
;
263 /* make sure we have some variables... */
265 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
266 if (tpvars
!= NULL
) {
268 state
= HGetState((Handle
) tpvars
);
269 HLock((Handle
) tpvars
);
271 /* we don't do any of these functions unless we're in focus */
272 if ( ! varsp
->fInFocus
) {
274 owner
= GetControlOwner(theControl
);
275 ClearKeyboardFocus(owner
);
276 SetKeyboardFocus(owner
, theControl
, kUserClickedToFocusPart
);
278 /* find the location for the click */
279 switch (TPPaneHitTestProc(theControl
, startPt
)) {
281 /* handle clicks in the text part */
283 { SetPort((**tpvars
).fDrawingEnvironment
);
284 TXNClick( varsp
->fTXNRec
, (const EventRecord
*) wxTheApp
->MacGetCurrentEvent());
290 HSetState((Handle
) tpvars
, state
);
292 return partCodeResult
;
296 /* TPPaneIdleProc is our user pane idle routine. When our text field
297 is active and in focus, we use this routine to set the cursor. */
298 static pascal void TPPaneIdleProc(ControlHandle theControl
) {
299 STPTextPaneVars
**tpvars
, *varsp
;
301 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
302 if (tpvars
!= NULL
) {
303 /* if we're not active, then we have nothing to say about the cursor */
304 if ((**tpvars
).fIsActive
) {
308 /* lock down the globals */
309 state
= HGetState((Handle
) tpvars
);
310 HLock((Handle
) tpvars
);
312 /* get the current mouse coordinates (in our window) */
313 SetPortWindowPort(GetControlOwner(theControl
));
315 /* there's a 'focus thing' and an 'unfocused thing' */
316 if (varsp
->fInFocus
) {
317 /* flash the cursor */
318 SetPort((**tpvars
).fDrawingEnvironment
);
319 TXNIdle(varsp
->fTXNRec
);
321 if (PtInRect(mousep
, &varsp
->fRTextArea
)) {
323 RectRgn((theRgn
= NewRgn()), &varsp
->fRTextArea
);
324 TXNAdjustCursor(varsp
->fTXNRec
, theRgn
);
326 } else SetThemeCursor(kThemeArrowCursor
);
328 /* if it's in our bounds, set the cursor */
329 GetControlBounds(theControl
, &bounds
);
330 if (PtInRect(mousep
, &bounds
))
331 SetThemeCursor(kThemeArrowCursor
);
334 HSetState((Handle
) tpvars
, state
);
340 /* TPPaneKeyDownProc is called whenever a keydown event is directed
341 at our control. Here, we direct the keydown event to the text
342 edit record and redraw the scroll bar and text field as appropriate. */
343 static pascal ControlPartCode
TPPaneKeyDownProc(ControlHandle theControl
,
344 SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
) {
345 STPTextPaneVars
**tpvars
;
346 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
347 if (tpvars
!= NULL
) {
348 if ((**tpvars
).fInFocus
) {
349 /* turn autoscrolling on and send the key event to text edit */
350 SetPort((**tpvars
).fDrawingEnvironment
);
352 memset( &ev
, 0 , sizeof( ev
) ) ;
354 ev
.modifiers
= modifiers
;
355 ev
.message
= (( keyCode
& keyCodeMask
) << 8 ) + ( charCode
& charCodeMask
) ;
356 TXNKeyDown( (**tpvars
).fTXNRec
, &ev
);
359 return kControlEntireControl
;
363 /* TPPaneActivateProc is called when the window containing
364 the user pane control receives activate events. Here, we redraw
365 the control and it's text as necessary for the activation state. */
366 static pascal void TPPaneActivateProc(ControlHandle theControl
, Boolean activating
) {
368 STPTextPaneVars
**tpvars
, *varsp
;
371 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
372 if (tpvars
!= NULL
) {
373 state
= HGetState((Handle
) tpvars
);
374 HLock((Handle
) tpvars
);
376 /* de/activate the text edit record */
377 SetPort((**tpvars
).fDrawingEnvironment
);
378 GetControlBounds(theControl
, &bounds
);
379 varsp
->fIsActive
= activating
;
380 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
381 /* redraw the frame */
382 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
383 if (varsp
->fInFocus
) DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
);
384 HSetState((Handle
) tpvars
, state
);
389 /* TPPaneFocusProc is called when every the focus changes to or
390 from our control. Herein, switch the focus appropriately
391 according to the parameters and redraw the control as
393 static pascal ControlPartCode
TPPaneFocusProc(ControlHandle theControl
, ControlFocusPart action
) {
394 ControlPartCode focusResult
;
395 STPTextPaneVars
**tpvars
, *varsp
;
398 focusResult
= kControlFocusNoPart
;
399 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
400 if (tpvars
!= NULL
) {
401 state
= HGetState((Handle
) tpvars
);
402 HLock((Handle
) tpvars
);
404 /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
405 tabbing forwards (or shift tabbing backwards) through the items in the dialog,
406 and kControlFocusNextPart will be received. When the user clicks in our field
407 and it is not the current focus, then the constant kUserClickedToFocusPart will
408 be received. The constant kControlFocusNoPart will be received when our control
409 is the current focus and the user clicks in another control. In your focus routine,
410 you should respond to these codes as follows:
412 kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw
413 the control and the focus rectangle as necessary.
415 kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off
416 depending on its current state. redraw the control and the focus rectangle
417 as appropriate for the new focus state. If the focus state is 'off', return the constant
418 kControlFocusNoPart, otherwise return a non-zero part code.
419 kUserClickedToFocusPart - is a constant defined for this example. You should
420 define your own value for handling click-to-focus type events. */
421 /* save the drawing state */
422 SetPort((**tpvars
).fDrawingEnvironment
);
423 /* calculate the next highlight state */
426 case kControlFocusNoPart
:
427 TPFocusPaneText(tpvars
, false);
428 focusResult
= kControlFocusNoPart
;
430 case kUserClickedToFocusPart
:
431 TPFocusPaneText(tpvars
, true);
434 case kControlFocusPrevPart
:
435 case kControlFocusNextPart
:
436 TPFocusPaneText(tpvars
, ( ! varsp
->fInFocus
));
437 focusResult
= varsp
->fInFocus
? 1 : kControlFocusNoPart
;
440 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
441 /* redraw the text fram and focus rectangle to indicate the
443 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
444 DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
&& varsp
->fInFocus
);
446 HSetState((Handle
) tpvars
, state
);
452 /* mUPOpenControl initializes a user pane control so it will be drawn
453 and will behave as a scrolling text edit field inside of a window.
454 This routine performs all of the initialization steps necessary,
455 except it does not create the user pane control itself. theControl
456 should refer to a user pane control that you have either created
457 yourself or extracted from a dialog's control heirarchy using
458 the GetDialogItemAsControl routine. */
459 OSStatus
mUPOpenControl(ControlHandle theControl
, bool multiline
)
463 STPTextPaneVars
**tpvars
, *varsp
;
465 RGBColor rgbWhite
= {0xFFFF, 0xFFFF, 0xFFFF};
468 /* set up our globals */
469 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(TPPaneDrawProc
);
470 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(TPPaneHitTestProc
);
471 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(TPPaneTrackingProc
);
472 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(TPPaneIdleProc
);
473 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc
);
474 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(TPPaneActivateProc
);
475 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(TPPaneFocusProc
);
477 /* allocate our private storage */
478 tpvars
= (STPTextPaneVars
**) NewHandleClear(sizeof(STPTextPaneVars
));
479 SetControlReference(theControl
, (long) tpvars
);
480 HLock((Handle
) tpvars
);
482 /* set the initial settings for our private data */
483 varsp
->fMultiline
= multiline
;
484 varsp
->fInFocus
= false;
485 varsp
->fIsActive
= true;
486 varsp
->fTEActive
= true; // in order to get a deactivate
487 varsp
->fUserPaneRec
= theControl
;
488 theWindow
= varsp
->fOwner
= GetControlOwner(theControl
);
490 varsp
->fDrawingEnvironment
= (GrafPtr
) GetWindowPort(theWindow
);
492 varsp
->fInDialogWindow
= ( GetWindowKind(varsp
->fOwner
) == kDialogWindowKind
);
493 /* set up the user pane procedures */
494 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
495 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
496 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
497 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
498 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
499 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
500 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
501 /* calculate the rectangles used by the control */
502 GetControlBounds(theControl
, &bounds
);
503 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
504 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
505 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
506 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
507 /* calculate the background region for the text. In this case, it's kindof
508 and irregular region because we're setting the scroll bar a little ways inside
510 RectRgn((varsp
->fTextBackgroundRgn
= NewRgn()), &varsp
->fRTextOutline
);
512 /* set up the drawing environment */
513 SetPort(varsp
->fDrawingEnvironment
);
515 /* create the new edit field */
516 TXNNewObject(NULL
, varsp
->fOwner
, &varsp
->fRTextArea
,
517 ( multiline
? kTXNWantVScrollBarMask
: 0 ) |
518 kTXNDontDrawCaretWhenInactiveMask
|
519 kTXNDontDrawSelectionWhenInactiveMask
|
520 kTXNAlwaysWrapAtViewEdgeMask
,
521 kTXNTextEditStyleFrameType
,
523 kTXNSystemDefaultEncoding
,
524 &varsp
->fTXNRec
, &varsp
->fTXNFrame
, (TXNObjectRefcon
) tpvars
);
530 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
532 TXNTypeAttributes typeAttr
[] =
534 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
535 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
536 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
539 OSStatus status
= TXNSetTypeAttributes (varsp
->fTXNRec
, sizeof( typeAttr
) / sizeof(TXNTypeAttributes
) , typeAttr
,
542 /* set the field's background */
543 tback
.bgType
= kTXNBackgroundTypeRGB
;
544 tback
.bg
.color
= rgbWhite
;
545 TXNSetBackground( varsp
->fTXNRec
, &tback
);
547 /* unlock our storage */
548 HUnlock((Handle
) tpvars
);
549 /* perform final activations and setup for our text field. Here,
550 we assume that the window is going to be the 'active' window. */
551 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
559 #if !USE_SHARED_LIBRARY
560 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
562 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
563 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
564 EVT_CHAR(wxTextCtrl::OnChar
)
565 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
566 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
567 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
568 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
569 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
571 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
572 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
573 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
574 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
575 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
580 wxTextCtrl::wxTextCtrl()
584 m_macTXNvars
= NULL
;
585 m_macUsesTXN
= false ;
589 wxTextCtrl::~wxTextCtrl()
593 SetControlReference((ControlHandle
)m_macControl
, 0) ;
594 TXNDeleteObject((TXNObject
)m_macTXN
);
595 /* delete our private storage */
596 DisposeHandle((Handle
) m_macTXNvars
);
597 /* zero the control reference */
601 const short kVerticalMargin
= 2 ;
602 const short kHorizontalMargin
= 2 ;
604 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
607 const wxSize
& size
, long style
,
608 const wxValidator
& validator
,
609 const wxString
& name
)
613 m_macTXNvars
= NULL
;
614 m_macUsesTXN
= false ;
617 m_macUsesTXN
= ! (style
& wxTE_PASSWORD
) ;
619 m_macUsesTXN
&= (TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
) ;
621 // base initialization
622 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
625 wxSize mySize
= size
;
628 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
629 m_macVerticalBorder
= 3 ;
633 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
634 m_macVerticalBorder
= 5 ;
641 if ( mySize.y == -1 )
644 if ( m_windowStyle & wxTE_MULTILINE )
647 mySize.y += 2 * m_macVerticalBorder ;
650 MacPreControlCreate( parent
, id
, "" , pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
652 if ( m_windowStyle
& wxTE_MULTILINE
)
654 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
655 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
657 m_windowStyle
|= wxTE_PROCESS_ENTER
;
663 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, "\p" , true , 0 , 0 , 1,
664 (style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
666 ::GetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &((TEHandle
) m_macTE
) , &size
) ;
673 featurSet
= kControlSupportsEmbedding
| kControlSupportsFocus
// | kControlWantsIdle
674 | kControlWantsActivate
| kControlHandlesTracking
| kControlHasSpecialBackground
675 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
676 /* create the control */
677 m_macControl
= NewControl(MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, "\p", true, featurSet
, 0, featurSet
, kControlUserPaneProc
, 0);
678 /* set up the mUP specific features and data */
679 mUPOpenControl((ControlHandle
) m_macControl
, m_windowStyle
& wxTE_MULTILINE
);
682 parent
->MacGetTopLevelWindow()->MacInstallEventHandler() ;
685 MacPostControlCreate() ;
689 if( wxApp::s_macDefaultEncodingIsPC
)
690 value
= wxMacMakeMacStringFromPC( st
) ;
696 ::SetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
700 STPTextPaneVars
**tpvars
;
702 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
703 /* set the text in the record */
704 TXNSetData( (**tpvars
).fTXNRec
, kTXNTextData
, (void*)value
.c_str(), value
.Length(),
705 kTXNStartOffset
, kTXNEndOffset
);
706 m_macTXN
= (**tpvars
).fTXNRec
;
707 m_macTXNvars
= tpvars
;
708 m_macUsesTXN
= true ;
714 wxString
wxTextCtrl::GetValue() const
719 ::GetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 32767 , wxBuffer
, &actualsize
) ;
724 OSStatus err
= TXNGetDataEncoded( ((TXNObject
) m_macTXN
), kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
732 actualsize
= GetHandleSize( theText
) ;
734 strncpy( wxBuffer
, *theText
, actualsize
) ;
735 DisposeHandle( theText
) ;
738 wxBuffer
[actualsize
] = 0 ;
739 if( wxApp::s_macDefaultEncodingIsPC
)
740 return wxMacMakePCStringFromMac( wxBuffer
) ;
742 return wxString(wxBuffer
);
745 void wxTextCtrl::GetSelection(long* from
, long* to
) const
749 *from
= (**((TEHandle
) m_macTE
)).selStart
;
750 *to
= (**((TEHandle
) m_macTE
)).selEnd
;
754 TXNGetSelection( ((TXNObject
) m_macTXN
) , (TXNOffset
*) from
, (TXNOffset
*) to
) ;
758 void wxTextCtrl::SetValue(const wxString
& st
)
762 if( wxApp::s_macDefaultEncodingIsPC
)
763 value
= wxMacMakeMacStringFromPC( st
) ;
768 ::SetControlData((ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
772 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*)value
.c_str(), value
.Length(),
773 kTXNStartOffset
, kTXNEndOffset
);
778 // Clipboard operations
779 void wxTextCtrl::Copy()
785 TECopy( ((TEHandle
) m_macTE
) ) ;
793 TXNCopy((TXNObject
)m_macTXN
);
794 TXNConvertToPublicScrap();
799 void wxTextCtrl::Cut()
805 TECut( ((TEHandle
) m_macTE
) ) ;
813 TXNCut((TXNObject
)m_macTXN
);
814 TXNConvertToPublicScrap();
816 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
817 event
.SetString( GetValue() ) ;
818 event
.SetEventObject( this );
819 GetEventHandler()->ProcessEvent(event
);
823 void wxTextCtrl::Paste()
830 TEPaste( (TEHandle
) m_macTE
) ;
835 TXNConvertFromPublicScrap();
836 TXNPaste((TXNObject
)m_macTXN
);
838 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
839 event
.SetString( GetValue() ) ;
840 event
.SetEventObject( this );
841 GetEventHandler()->ProcessEvent(event
);
845 bool wxTextCtrl::CanCopy() const
847 // Can copy if there's a selection
849 GetSelection(& from
, & to
);
853 bool wxTextCtrl::CanCut() const
859 // Can cut if there's a selection
861 GetSelection(& from
, & to
);
865 bool wxTextCtrl::CanPaste() const
872 OSStatus err
= noErr
;
875 err
= GetCurrentScrap( &scrapRef
);
876 if ( err
!= noTypeErr
&& err
!= memFullErr
)
878 ScrapFlavorFlags flavorFlags
;
881 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
883 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
892 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
900 void wxTextCtrl::SetEditable(bool editable
)
902 if ( editable
!= m_editable
)
904 m_editable
= editable
;
906 UMAActivateControl( (ControlHandle
) m_macControl
) ;
908 UMADeactivateControl((ControlHandle
) m_macControl
) ;
912 void wxTextCtrl::SetInsertionPoint(long pos
)
914 SetSelection( pos
, pos
) ;
917 void wxTextCtrl::SetInsertionPointEnd()
919 long pos
= GetLastPosition();
920 SetInsertionPoint(pos
);
923 long wxTextCtrl::GetInsertionPoint() const
926 GetSelection( &begin
, &end
) ;
930 long wxTextCtrl::GetLastPosition() const
934 return (**((TEHandle
) m_macTE
)).teLength
;
940 OSErr err
= TXNGetDataEncoded( (TXNObject
) m_macTXN
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
948 actualsize
= GetHandleSize( theText
) ;
949 DisposeHandle( theText
) ;
955 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
959 ControlEditTextSelectionRec selection
;
961 selection
.selStart
= from
;
962 selection
.selEnd
= to
;
963 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
964 TESetSelect( from
, to
, ((TEHandle
) m_macTE
) ) ;
965 TEDelete( ((TEHandle
) m_macTE
) ) ;
966 TEInsert( value
, value
.Length() , ((TEHandle
) m_macTE
) ) ;
970 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
971 TXNClear( ((TXNObject
) m_macTXN
) ) ;
972 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*)value
.c_str(), value
.Length(),
973 kTXNUseCurrentSelection
, kTXNUseCurrentSelection
);
978 void wxTextCtrl::Remove(long from
, long to
)
982 ControlEditTextSelectionRec selection
;
984 selection
.selStart
= from
;
985 selection
.selEnd
= to
;
986 ::SetControlData( (ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
987 TEDelete( ((TEHandle
) m_macTE
) ) ;
991 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
992 TXNClear( ((TXNObject
) m_macTXN
) ) ;
997 void wxTextCtrl::SetSelection(long from
, long to
)
1000 if ( !m_macUsesTXN
)
1002 ControlEditTextSelectionRec selection
;
1003 selection
.selStart
= from
;
1004 selection
.selEnd
= to
;
1006 TESetSelect( selection
.selStart
, selection
.selEnd
, ((TEHandle
) m_macTE
) ) ;
1007 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
1011 STPTextPaneVars
**tpvars
;
1012 /* set up our locals */
1013 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
1014 /* and our drawing environment as the operation
1015 may force a redraw in the text area. */
1016 SetPort((**tpvars
).fDrawingEnvironment
);
1017 /* change the selection */
1018 TXNSetSelection( (**tpvars
).fTXNRec
, from
, to
);
1022 bool wxTextCtrl::LoadFile(const wxString
& file
)
1024 if ( wxTextCtrlBase::LoadFile(file
) )
1032 void wxTextCtrl::WriteText(const wxString
& text
)
1035 if( wxApp::s_macDefaultEncodingIsPC
)
1036 value
= wxMacMakeMacStringFromPC( text
) ;
1039 if ( !m_macUsesTXN
)
1041 TEInsert( value
, value
.Length() , ((TEHandle
) m_macTE
) ) ;
1045 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*) (const char*)value
, value
.Length(),
1046 kTXNUseCurrentSelection
, kTXNUseCurrentSelection
);
1048 MacRedrawControl() ;
1051 void wxTextCtrl::AppendText(const wxString
& text
)
1053 SetInsertionPointEnd();
1057 void wxTextCtrl::Clear()
1059 if ( !IsEditable() )
1063 if ( !m_macUsesTXN
)
1065 ::SetControlData((ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
1069 ClearCurrentScrap();
1070 TXNClear((TXNObject
)m_macTXN
);
1075 bool wxTextCtrl::IsModified() const
1080 bool wxTextCtrl::IsEditable() const
1082 return IsEnabled() && m_editable
;
1085 bool wxTextCtrl::AcceptsFocus() const
1087 // we don't want focus if we can't be edited
1088 return /*IsEditable() && */ wxControl::AcceptsFocus();
1091 wxSize
wxTextCtrl::DoGetBestSize() const
1106 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1108 int wText = DEFAULT_ITEM_WIDTH;
1110 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1112 return wxSize(wText, hText);
1114 if ( m_windowStyle
& wxTE_MULTILINE
)
1118 hText
+= 2 * m_macVerticalBorder
;
1119 wText
+= 2 * m_macHorizontalBorder
;
1120 //else: for single line control everything is ok
1121 return wxSize(wText
, hText
);
1124 // ----------------------------------------------------------------------------
1126 // ----------------------------------------------------------------------------
1128 void wxTextCtrl::Undo()
1135 void wxTextCtrl::Redo()
1142 bool wxTextCtrl::CanUndo() const
1147 bool wxTextCtrl::CanRedo() const
1152 // Makes 'unmodified'
1153 void wxTextCtrl::DiscardEdits()
1158 int wxTextCtrl::GetNumberOfLines() const
1160 // TODO change this if possible to reflect real lines
1161 wxString content
= GetValue() ;
1164 for (int i
= 0; i
< content
.Length() ; i
++)
1166 if (content
[i
] == '\r') count
++;
1172 long wxTextCtrl::XYToPosition(long x
, long y
) const
1178 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1183 void wxTextCtrl::ShowPosition(long pos
)
1188 int wxTextCtrl::GetLineLength(long lineNo
) const
1190 // TODO change this if possible to reflect real lines
1191 wxString content
= GetValue() ;
1195 for (int i
= 0; i
< content
.Length() ; i
++)
1197 if (count
== lineNo
)
1199 // Count chars in line then
1201 for (int j
= i
; j
< content
.Length(); j
++)
1204 if (content
[j
] == '\r') return count
;
1209 if (content
[i
] == '\r') count
++;
1214 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1216 // TODO change this if possible to reflect real lines
1217 wxString content
= GetValue() ;
1221 for (int i
= 0; i
< content
.Length() ; i
++)
1223 if (count
== lineNo
)
1225 // Add chars in line then
1228 for (int j
= i
; j
< content
.Length(); j
++)
1230 if (content
[j
] == '\r')
1238 if (content
[i
] == '\r') count
++;
1247 void wxTextCtrl::Command(wxCommandEvent
& event
)
1249 SetValue (event
.GetString());
1250 ProcessCommand (event
);
1253 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1255 // By default, load the first file into the text window.
1256 if (event
.GetNumberOfFiles() > 0)
1258 LoadFile(event
.GetFiles()[0]);
1262 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1264 int key
= event
.GetKeyCode() ;
1265 bool eat_key
= false ;
1267 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
1268 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxPROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
1269 /* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */
1278 if (m_windowStyle
& wxPROCESS_ENTER
)
1280 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1281 event
.SetEventObject( this );
1282 event
.SetString( GetValue() );
1283 if ( GetEventHandler()->ProcessEvent(event
) )
1286 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1288 wxWindow
*parent
= GetParent();
1289 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
1290 parent
= parent
->GetParent() ;
1292 if ( parent
&& parent
->GetDefaultItem() )
1294 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
1296 if ( def
&& def
->IsEnabled() )
1298 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1299 event
.SetEventObject(def
);
1300 def
->Command(event
);
1305 // this will make wxWindows eat the ENTER key so that
1306 // we actually prevent line wrapping in a single line
1314 // always produce navigation event - even if we process TAB
1315 // ourselves the fact that we got here means that the user code
1316 // decided to skip processing of this TAB - probably to let it
1317 // do its default job.
1319 wxNavigationKeyEvent eventNav
;
1320 eventNav
.SetDirection(!event
.ShiftDown());
1321 eventNav
.SetWindowChange(event
.ControlDown());
1322 eventNav
.SetEventObject(this);
1324 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1333 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent();
1334 short keychar
= short(ev
->message
& charCodeMask
);
1337 short keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1338 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
);
1340 if ( keychar
>= 0x20 ||
1341 event
.KeyCode() == WXK_RETURN
||
1342 event
.KeyCode() == WXK_DELETE
||
1343 event
.KeyCode() == WXK_BACK
)
1345 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1346 event1
.SetString( GetValue() ) ;
1347 event1
.SetEventObject( this );
1348 GetEventHandler()->ProcessEvent(event1
);
1352 // ----------------------------------------------------------------------------
1353 // standard handlers for standard edit menu events
1354 // ----------------------------------------------------------------------------
1356 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1361 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1366 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1371 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1376 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1381 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1383 event
.Enable( CanCut() );
1386 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1388 event
.Enable( CanCopy() );
1391 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1393 event
.Enable( CanPaste() );
1396 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1398 event
.Enable( CanUndo() );
1401 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1403 event
.Enable( CanRedo() );