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 */
189 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
190 if (tpvars
!= NULL
) {
191 state
= HGetState((Handle
) tpvars
);
192 HLock((Handle
) tpvars
);
195 /* save the drawing state */
196 SetPort((**tpvars
).fDrawingEnvironment
);
197 /* verify our boundary */
198 GetControlBounds(theControl
, &bounds
);
199 if ( ! EqualRect(&bounds
, &varsp
->fRFocusOutline
) ) {
200 // scrollbar is on the border, we add one
201 Rect oldbounds
= varsp
->fRFocusOutline
;
202 InsetRect( &oldbounds
, -1 , -1 ) ;
204 InvalWindowRect( GetControlOwner( theControl
) , &oldbounds
) ;
205 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
206 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
207 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
208 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
209 RectRgn(varsp
->fTextBackgroundRgn
, &varsp
->fRTextOutline
);
210 TXNSetFrameBounds( varsp
->fTXNRec
, varsp
->fRTextArea
.top
, varsp
->fRTextArea
.left
,
211 varsp
->fRTextArea
.bottom
, varsp
->fRTextArea
.right
, varsp
->fTXNFrame
);
214 /* update the text region */
215 RGBColor white
= { 65535 , 65535 , 65535 } ;
216 RGBBackColor( &white
) ;
217 EraseRgn(varsp
->fTextBackgroundRgn
);
218 TXNDraw(varsp
->fTXNRec
, NULL
);
219 /* restore the drawing environment */
220 /* draw the text frame and focus frame (if necessary) */
221 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
222 if ((**tpvars
).fIsActive
&& varsp
->fInFocus
) DrawThemeFocusRect(&varsp
->fRFocusOutline
, true);
223 /* release our globals */
224 HSetState((Handle
) tpvars
, state
);
229 /* TPPaneHitTestProc is called when the control manager would
230 like to determine what part of the control the mouse resides over.
231 We also call this routine from our tracking proc to determine how
232 to handle mouse clicks. */
233 static pascal ControlPartCode
TPPaneHitTestProc(ControlHandle theControl
, Point where
) {
234 STPTextPaneVars
**tpvars
;
235 ControlPartCode result
;
237 /* set up our locals and lock down our globals*/
239 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
240 if (tpvars
!= NULL
) {
241 state
= HGetState((Handle
) tpvars
);
242 HLock((Handle
) tpvars
);
243 /* find the region where we clicked */
244 if (PtInRect(where
, &(**tpvars
).fRTextArea
)) {
245 result
= kmUPTextPart
;
247 /* release oure globals */
248 HSetState((Handle
) tpvars
, state
);
257 /* TPPaneTrackingProc is called when the mouse is being held down
258 over our control. This routine handles clicks in the text area
259 and in the scroll bar. */
260 static pascal ControlPartCode
TPPaneTrackingProc(ControlHandle theControl
, Point startPt
, ControlActionUPP actionProc
) {
261 STPTextPaneVars
**tpvars
, *varsp
;
263 ControlPartCode partCodeResult
;
264 /* make sure we have some variables... */
266 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
267 if (tpvars
!= NULL
) {
269 state
= HGetState((Handle
) tpvars
);
270 HLock((Handle
) tpvars
);
272 /* we don't do any of these functions unless we're in focus */
273 if ( ! varsp
->fInFocus
) {
275 owner
= GetControlOwner(theControl
);
276 ClearKeyboardFocus(owner
);
277 SetKeyboardFocus(owner
, theControl
, kUserClickedToFocusPart
);
279 /* find the location for the click */
280 switch (TPPaneHitTestProc(theControl
, startPt
)) {
282 /* handle clicks in the text part */
284 { SetPort((**tpvars
).fDrawingEnvironment
);
285 TXNClick( varsp
->fTXNRec
, (const EventRecord
*) wxTheApp
->MacGetCurrentEvent());
291 HSetState((Handle
) tpvars
, state
);
293 return partCodeResult
;
297 /* TPPaneIdleProc is our user pane idle routine. When our text field
298 is active and in focus, we use this routine to set the cursor. */
299 static pascal void TPPaneIdleProc(ControlHandle theControl
) {
300 STPTextPaneVars
**tpvars
, *varsp
;
302 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
303 if (tpvars
!= NULL
) {
304 /* if we're not active, then we have nothing to say about the cursor */
305 if ((**tpvars
).fIsActive
) {
309 /* lock down the globals */
310 state
= HGetState((Handle
) tpvars
);
311 HLock((Handle
) tpvars
);
313 /* get the current mouse coordinates (in our window) */
314 SetPortWindowPort(GetControlOwner(theControl
));
316 /* there's a 'focus thing' and an 'unfocused thing' */
317 if (varsp
->fInFocus
) {
318 /* flash the cursor */
319 SetPort((**tpvars
).fDrawingEnvironment
);
320 TXNIdle(varsp
->fTXNRec
);
322 if (PtInRect(mousep
, &varsp
->fRTextArea
)) {
324 RectRgn((theRgn
= NewRgn()), &varsp
->fRTextArea
);
325 TXNAdjustCursor(varsp
->fTXNRec
, theRgn
);
327 } else SetThemeCursor(kThemeArrowCursor
);
329 /* if it's in our bounds, set the cursor */
330 GetControlBounds(theControl
, &bounds
);
331 if (PtInRect(mousep
, &bounds
))
332 SetThemeCursor(kThemeArrowCursor
);
335 HSetState((Handle
) tpvars
, state
);
341 /* TPPaneKeyDownProc is called whenever a keydown event is directed
342 at our control. Here, we direct the keydown event to the text
343 edit record and redraw the scroll bar and text field as appropriate. */
344 static pascal ControlPartCode
TPPaneKeyDownProc(ControlHandle theControl
,
345 SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
) {
346 STPTextPaneVars
**tpvars
;
347 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
348 if (tpvars
!= NULL
) {
349 if ((**tpvars
).fInFocus
) {
350 /* turn autoscrolling on and send the key event to text edit */
351 SetPort((**tpvars
).fDrawingEnvironment
);
353 memset( &ev
, 0 , sizeof( ev
) ) ;
355 ev
.modifiers
= modifiers
;
356 ev
.message
= (( keyCode
<< 8 ) & keyCodeMask
) + ( charCode
& charCodeMask
) ;
357 TXNKeyDown( (**tpvars
).fTXNRec
, &ev
);
360 return kControlEntireControl
;
364 /* TPPaneActivateProc is called when the window containing
365 the user pane control receives activate events. Here, we redraw
366 the control and it's text as necessary for the activation state. */
367 static pascal void TPPaneActivateProc(ControlHandle theControl
, Boolean activating
) {
369 STPTextPaneVars
**tpvars
, *varsp
;
372 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
373 if (tpvars
!= NULL
) {
374 state
= HGetState((Handle
) tpvars
);
375 HLock((Handle
) tpvars
);
377 /* de/activate the text edit record */
378 SetPort((**tpvars
).fDrawingEnvironment
);
379 GetControlBounds(theControl
, &bounds
);
380 varsp
->fIsActive
= activating
;
381 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
382 /* redraw the frame */
383 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
384 if (varsp
->fInFocus
) DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
);
385 HSetState((Handle
) tpvars
, state
);
390 /* TPPaneFocusProc is called when every the focus changes to or
391 from our control. Herein, switch the focus appropriately
392 according to the parameters and redraw the control as
394 static pascal ControlPartCode
TPPaneFocusProc(ControlHandle theControl
, ControlFocusPart action
) {
395 ControlPartCode focusResult
;
396 STPTextPaneVars
**tpvars
, *varsp
;
399 focusResult
= kControlFocusNoPart
;
400 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
401 if (tpvars
!= NULL
) {
402 state
= HGetState((Handle
) tpvars
);
403 HLock((Handle
) tpvars
);
405 /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
406 tabbing forwards (or shift tabbing backwards) through the items in the dialog,
407 and kControlFocusNextPart will be received. When the user clicks in our field
408 and it is not the current focus, then the constant kUserClickedToFocusPart will
409 be received. The constant kControlFocusNoPart will be received when our control
410 is the current focus and the user clicks in another control. In your focus routine,
411 you should respond to these codes as follows:
413 kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw
414 the control and the focus rectangle as necessary.
416 kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off
417 depending on its current state. redraw the control and the focus rectangle
418 as appropriate for the new focus state. If the focus state is 'off', return the constant
419 kControlFocusNoPart, otherwise return a non-zero part code.
420 kUserClickedToFocusPart - is a constant defined for this example. You should
421 define your own value for handling click-to-focus type events. */
422 /* save the drawing state */
423 SetPort((**tpvars
).fDrawingEnvironment
);
424 /* calculate the next highlight state */
427 case kControlFocusNoPart
:
428 TPFocusPaneText(tpvars
, false);
429 focusResult
= kControlFocusNoPart
;
431 case kUserClickedToFocusPart
:
432 TPFocusPaneText(tpvars
, true);
435 case kControlFocusPrevPart
:
436 case kControlFocusNextPart
:
437 TPFocusPaneText(tpvars
, ( ! varsp
->fInFocus
));
438 focusResult
= varsp
->fInFocus
? 1 : kControlFocusNoPart
;
441 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
442 /* redraw the text fram and focus rectangle to indicate the
444 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
445 DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
&& varsp
->fInFocus
);
447 HSetState((Handle
) tpvars
, state
);
453 /* mUPOpenControl initializes a user pane control so it will be drawn
454 and will behave as a scrolling text edit field inside of a window.
455 This routine performs all of the initialization steps necessary,
456 except it does not create the user pane control itself. theControl
457 should refer to a user pane control that you have either created
458 yourself or extracted from a dialog's control heirarchy using
459 the GetDialogItemAsControl routine. */
460 OSStatus
mUPOpenControl(ControlHandle theControl
, bool multiline
)
464 STPTextPaneVars
**tpvars
, *varsp
;
466 RGBColor rgbWhite
= {0xFFFF, 0xFFFF, 0xFFFF};
469 /* set up our globals */
470 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(TPPaneDrawProc
);
471 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(TPPaneHitTestProc
);
472 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(TPPaneTrackingProc
);
473 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(TPPaneIdleProc
);
474 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc
);
475 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(TPPaneActivateProc
);
476 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(TPPaneFocusProc
);
478 /* allocate our private storage */
479 tpvars
= (STPTextPaneVars
**) NewHandleClear(sizeof(STPTextPaneVars
));
480 SetControlReference(theControl
, (long) tpvars
);
481 HLock((Handle
) tpvars
);
483 /* set the initial settings for our private data */
484 varsp
->fMultiline
= multiline
;
485 varsp
->fInFocus
= false;
486 varsp
->fIsActive
= true;
487 varsp
->fTEActive
= true; // in order to get a deactivate
488 varsp
->fUserPaneRec
= theControl
;
489 theWindow
= varsp
->fOwner
= GetControlOwner(theControl
);
491 varsp
->fDrawingEnvironment
= (GrafPtr
) GetWindowPort(theWindow
);
493 varsp
->fInDialogWindow
= ( GetWindowKind(varsp
->fOwner
) == kDialogWindowKind
);
494 /* set up the user pane procedures */
495 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
496 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
497 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
498 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
499 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
500 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
501 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
502 /* calculate the rectangles used by the control */
503 GetControlBounds(theControl
, &bounds
);
504 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
505 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
506 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
507 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
508 /* calculate the background region for the text. In this case, it's kindof
509 and irregular region because we're setting the scroll bar a little ways inside
511 RectRgn((varsp
->fTextBackgroundRgn
= NewRgn()), &varsp
->fRTextOutline
);
513 /* set up the drawing environment */
514 SetPort(varsp
->fDrawingEnvironment
);
516 /* create the new edit field */
517 TXNNewObject(NULL
, varsp
->fOwner
, &varsp
->fRTextArea
,
518 ( multiline
? kTXNWantVScrollBarMask
: 0 ) |
519 kTXNDontDrawCaretWhenInactiveMask
|
520 kTXNDontDrawSelectionWhenInactiveMask
|
521 kTXNAlwaysWrapAtViewEdgeMask
,
522 kTXNTextEditStyleFrameType
,
524 kTXNSystemDefaultEncoding
,
525 &varsp
->fTXNRec
, &varsp
->fTXNFrame
, (TXNObjectRefcon
) tpvars
);
531 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
533 TXNTypeAttributes typeAttr
[] =
535 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
536 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
537 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
540 OSStatus status
= TXNSetTypeAttributes (varsp
->fTXNRec
, sizeof( typeAttr
) / sizeof(TXNTypeAttributes
) , typeAttr
,
543 /* set the field's background */
544 tback
.bgType
= kTXNBackgroundTypeRGB
;
545 tback
.bg
.color
= rgbWhite
;
546 TXNSetBackground( varsp
->fTXNRec
, &tback
);
548 /* unlock our storage */
549 HUnlock((Handle
) tpvars
);
550 /* perform final activations and setup for our text field. Here,
551 we assume that the window is going to be the 'active' window. */
552 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
560 #if !USE_SHARED_LIBRARY
561 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
563 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
564 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
565 EVT_CHAR(wxTextCtrl::OnChar
)
566 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
567 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
568 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
569 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
570 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
572 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
573 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
574 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
575 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
576 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
581 wxTextCtrl::wxTextCtrl()
585 m_macTXNvars
= NULL
;
586 m_macUsesTXN
= false ;
590 wxTextCtrl::~wxTextCtrl()
594 SetControlReference((ControlHandle
)m_macControl
, 0) ;
595 TXNDeleteObject((TXNObject
)m_macTXN
);
596 /* delete our private storage */
597 DisposeHandle((Handle
) m_macTXNvars
);
598 /* zero the control reference */
602 const short kVerticalMargin
= 2 ;
603 const short kHorizontalMargin
= 2 ;
605 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
608 const wxSize
& size
, long style
,
609 const wxValidator
& validator
,
610 const wxString
& name
)
614 m_macTXNvars
= NULL
;
615 m_macUsesTXN
= false ;
618 m_macUsesTXN
= ! (style
& wxTE_PASSWORD
) ;
620 m_macUsesTXN
&= (TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
) ;
622 // base initialization
623 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
626 wxSize mySize
= size
;
629 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
630 m_macVerticalBorder
= 3 ;
634 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
635 m_macVerticalBorder
= 5 ;
642 if ( mySize.y == -1 )
645 if ( m_windowStyle & wxTE_MULTILINE )
648 mySize.y += 2 * m_macVerticalBorder ;
651 MacPreControlCreate( parent
, id
, "" , pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
653 if ( m_windowStyle
& wxTE_MULTILINE
)
655 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
656 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
658 m_windowStyle
|= wxTE_PROCESS_ENTER
;
664 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, "\p" , true , 0 , 0 , 1,
665 (style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
667 ::GetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*) &((TEHandle
) m_macTE
) , &size
) ;
674 featurSet
= kControlSupportsEmbedding
| kControlSupportsFocus
// | kControlWantsIdle
675 | kControlWantsActivate
| kControlHandlesTracking
| kControlHasSpecialBackground
676 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
677 /* create the control */
678 m_macControl
= NewControl(MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, "\p", true, featurSet
, 0, featurSet
, kControlUserPaneProc
, 0);
679 /* set up the mUP specific features and data */
680 mUPOpenControl((ControlHandle
) m_macControl
, m_windowStyle
& wxTE_MULTILINE
);
683 parent
->MacGetTopLevelWindow()->MacInstallEventHandler() ;
686 MacPostControlCreate() ;
690 if( wxApp::s_macDefaultEncodingIsPC
)
691 value
= wxMacMakeMacStringFromPC( st
) ;
697 ::SetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
701 STPTextPaneVars
**tpvars
;
703 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
704 /* set the text in the record */
705 TXNSetData( (**tpvars
).fTXNRec
, kTXNTextData
, (void*)value
.c_str(), value
.Length(),
706 kTXNStartOffset
, kTXNEndOffset
);
707 m_macTXN
= (**tpvars
).fTXNRec
;
708 m_macTXNvars
= tpvars
;
709 m_macUsesTXN
= true ;
715 wxString
wxTextCtrl::GetValue() const
720 ::GetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 32767 , wxBuffer
, &actualsize
) ;
725 OSStatus err
= TXNGetDataEncoded( ((TXNObject
) m_macTXN
), kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
733 actualsize
= GetHandleSize( theText
) ;
735 strncpy( wxBuffer
, *theText
, actualsize
) ;
736 DisposeHandle( theText
) ;
739 wxBuffer
[actualsize
] = 0 ;
740 if( wxApp::s_macDefaultEncodingIsPC
)
741 return wxMacMakePCStringFromMac( wxBuffer
) ;
743 return wxString(wxBuffer
);
746 void wxTextCtrl::GetSelection(long* from
, long* to
) const
750 *from
= (**((TEHandle
) m_macTE
)).selStart
;
751 *to
= (**((TEHandle
) m_macTE
)).selEnd
;
755 TXNGetSelection( ((TXNObject
) m_macTXN
) , (TXNOffset
*) from
, (TXNOffset
*) to
) ;
759 void wxTextCtrl::SetValue(const wxString
& st
)
763 if( wxApp::s_macDefaultEncodingIsPC
)
764 value
= wxMacMakeMacStringFromPC( st
) ;
769 ::SetControlData((ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, value
.Length() , (char*) ((const char*)value
) ) ;
773 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*)value
.c_str(), value
.Length(),
774 kTXNStartOffset
, kTXNEndOffset
);
779 // Clipboard operations
780 void wxTextCtrl::Copy()
786 TECopy( ((TEHandle
) m_macTE
) ) ;
794 TXNCopy((TXNObject
)m_macTXN
);
795 TXNConvertToPublicScrap();
800 void wxTextCtrl::Cut()
806 TECut( ((TEHandle
) m_macTE
) ) ;
814 TXNCut((TXNObject
)m_macTXN
);
815 TXNConvertToPublicScrap();
817 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
818 event
.SetString( GetValue() ) ;
819 event
.SetEventObject( this );
820 GetEventHandler()->ProcessEvent(event
);
824 void wxTextCtrl::Paste()
831 TEPaste( (TEHandle
) m_macTE
) ;
836 TXNConvertFromPublicScrap();
837 TXNPaste((TXNObject
)m_macTXN
);
839 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
840 event
.SetString( GetValue() ) ;
841 event
.SetEventObject( this );
842 GetEventHandler()->ProcessEvent(event
);
846 bool wxTextCtrl::CanCopy() const
848 // Can copy if there's a selection
850 GetSelection(& from
, & to
);
854 bool wxTextCtrl::CanCut() const
860 // Can cut if there's a selection
862 GetSelection(& from
, & to
);
866 bool wxTextCtrl::CanPaste() const
873 OSStatus err
= noErr
;
876 err
= GetCurrentScrap( &scrapRef
);
877 if ( err
!= noTypeErr
&& err
!= memFullErr
)
879 ScrapFlavorFlags flavorFlags
;
882 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
884 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
893 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
901 void wxTextCtrl::SetEditable(bool editable
)
903 if ( editable
!= m_editable
)
905 m_editable
= editable
;
907 UMAActivateControl( (ControlHandle
) m_macControl
) ;
909 UMADeactivateControl((ControlHandle
) m_macControl
) ;
913 void wxTextCtrl::SetInsertionPoint(long pos
)
915 SetSelection( pos
, pos
) ;
918 void wxTextCtrl::SetInsertionPointEnd()
920 long pos
= GetLastPosition();
921 SetInsertionPoint(pos
);
924 long wxTextCtrl::GetInsertionPoint() const
927 GetSelection( &begin
, &end
) ;
931 long wxTextCtrl::GetLastPosition() const
935 return (**((TEHandle
) m_macTE
)).teLength
;
941 OSErr err
= TXNGetDataEncoded( (TXNObject
) m_macTXN
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
949 actualsize
= GetHandleSize( theText
) ;
950 DisposeHandle( theText
) ;
956 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
960 ControlEditTextSelectionRec selection
;
962 selection
.selStart
= from
;
963 selection
.selEnd
= to
;
964 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
965 TESetSelect( from
, to
, ((TEHandle
) m_macTE
) ) ;
966 TEDelete( ((TEHandle
) m_macTE
) ) ;
967 TEInsert( value
, value
.Length() , ((TEHandle
) m_macTE
) ) ;
971 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
972 TXNClear( ((TXNObject
) m_macTXN
) ) ;
973 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*)value
.c_str(), value
.Length(),
974 kTXNUseCurrentSelection
, kTXNUseCurrentSelection
);
979 void wxTextCtrl::Remove(long from
, long to
)
983 ControlEditTextSelectionRec selection
;
985 selection
.selStart
= from
;
986 selection
.selEnd
= to
;
987 ::SetControlData( (ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
988 TEDelete( ((TEHandle
) m_macTE
) ) ;
992 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
993 TXNClear( ((TXNObject
) m_macTXN
) ) ;
998 void wxTextCtrl::SetSelection(long from
, long to
)
1001 if ( !m_macUsesTXN
)
1003 ControlEditTextSelectionRec selection
;
1004 selection
.selStart
= from
;
1005 selection
.selEnd
= to
;
1007 TESetSelect( selection
.selStart
, selection
.selEnd
, ((TEHandle
) m_macTE
) ) ;
1008 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
1012 STPTextPaneVars
**tpvars
;
1013 /* set up our locals */
1014 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
1015 /* and our drawing environment as the operation
1016 may force a redraw in the text area. */
1017 SetPort((**tpvars
).fDrawingEnvironment
);
1018 /* change the selection */
1019 TXNSetSelection( (**tpvars
).fTXNRec
, from
, to
);
1023 bool wxTextCtrl::LoadFile(const wxString
& file
)
1025 if ( wxTextCtrlBase::LoadFile(file
) )
1033 void wxTextCtrl::WriteText(const wxString
& text
)
1036 if( wxApp::s_macDefaultEncodingIsPC
)
1037 value
= wxMacMakeMacStringFromPC( text
) ;
1040 if ( !m_macUsesTXN
)
1042 TEInsert( value
, value
.Length() , ((TEHandle
) m_macTE
) ) ;
1046 TXNSetData( ((TXNObject
) m_macTXN
), kTXNTextData
, (void*) (const char*)value
, value
.Length(),
1047 kTXNUseCurrentSelection
, kTXNUseCurrentSelection
);
1049 MacRedrawControl() ;
1052 void wxTextCtrl::AppendText(const wxString
& text
)
1054 SetInsertionPointEnd();
1058 void wxTextCtrl::Clear()
1060 if ( !IsEditable() )
1064 if ( !m_macUsesTXN
)
1066 ::SetControlData((ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
1070 ClearCurrentScrap();
1071 TXNClear((TXNObject
)m_macTXN
);
1076 bool wxTextCtrl::IsModified() const
1081 bool wxTextCtrl::IsEditable() const
1083 return IsEnabled() && m_editable
;
1086 bool wxTextCtrl::AcceptsFocus() const
1088 // we don't want focus if we can't be edited
1089 return /*IsEditable() && */ wxControl::AcceptsFocus();
1092 wxSize
wxTextCtrl::DoGetBestSize() const
1107 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1109 int wText = DEFAULT_ITEM_WIDTH;
1111 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1113 return wxSize(wText, hText);
1115 if ( m_windowStyle
& wxTE_MULTILINE
)
1119 hText
+= 2 * m_macVerticalBorder
;
1120 wText
+= 2 * m_macHorizontalBorder
;
1121 //else: for single line control everything is ok
1122 return wxSize(wText
, hText
);
1125 // ----------------------------------------------------------------------------
1127 // ----------------------------------------------------------------------------
1129 void wxTextCtrl::Undo()
1136 void wxTextCtrl::Redo()
1143 bool wxTextCtrl::CanUndo() const
1148 bool wxTextCtrl::CanRedo() const
1153 // Makes 'unmodified'
1154 void wxTextCtrl::DiscardEdits()
1159 int wxTextCtrl::GetNumberOfLines() const
1161 // TODO change this if possible to reflect real lines
1162 wxString content
= GetValue() ;
1165 for (int i
= 0; i
< content
.Length() ; i
++)
1167 if (content
[i
] == '\r') count
++;
1173 long wxTextCtrl::XYToPosition(long x
, long y
) const
1179 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1184 void wxTextCtrl::ShowPosition(long pos
)
1189 int wxTextCtrl::GetLineLength(long lineNo
) const
1191 // TODO change this if possible to reflect real lines
1192 wxString content
= GetValue() ;
1196 for (int i
= 0; i
< content
.Length() ; i
++)
1198 if (count
== lineNo
)
1200 // Count chars in line then
1202 for (int j
= i
; j
< content
.Length(); j
++)
1205 if (content
[j
] == '\r') return count
;
1210 if (content
[i
] == '\r') count
++;
1215 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1217 // TODO change this if possible to reflect real lines
1218 wxString content
= GetValue() ;
1222 for (int i
= 0; i
< content
.Length() ; i
++)
1224 if (count
== lineNo
)
1226 // Add chars in line then
1229 for (int j
= i
; j
< content
.Length(); j
++)
1231 if (content
[j
] == '\r')
1239 if (content
[i
] == '\r') count
++;
1248 void wxTextCtrl::Command(wxCommandEvent
& event
)
1250 SetValue (event
.GetString());
1251 ProcessCommand (event
);
1254 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1256 // By default, load the first file into the text window.
1257 if (event
.GetNumberOfFiles() > 0)
1259 LoadFile(event
.GetFiles()[0]);
1263 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1265 int key
= event
.GetKeyCode() ;
1266 bool eat_key
= false ;
1268 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
1269 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxPROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
1270 /* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */
1279 if (m_windowStyle
& wxPROCESS_ENTER
)
1281 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1282 event
.SetEventObject( this );
1283 event
.SetString( GetValue() );
1284 if ( GetEventHandler()->ProcessEvent(event
) )
1287 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1289 wxWindow
*parent
= GetParent();
1290 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
1291 parent
= parent
->GetParent() ;
1293 if ( parent
&& parent
->GetDefaultItem() )
1295 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
1297 if ( def
&& def
->IsEnabled() )
1299 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1300 event
.SetEventObject(def
);
1301 def
->Command(event
);
1306 // this will make wxWindows eat the ENTER key so that
1307 // we actually prevent line wrapping in a single line
1315 // always produce navigation event - even if we process TAB
1316 // ourselves the fact that we got here means that the user code
1317 // decided to skip processing of this TAB - probably to let it
1318 // do its default job.
1320 wxNavigationKeyEvent eventNav
;
1321 eventNav
.SetDirection(!event
.ShiftDown());
1322 eventNav
.SetWindowChange(event
.ControlDown());
1323 eventNav
.SetEventObject(this);
1325 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav
) )
1340 key
== WXK_RETURN
||
1341 key
== WXK_DELETE
||
1344 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1345 event1
.SetString( GetValue() ) ;
1346 event1
.SetEventObject( this );
1347 wxPostEvent(GetEventHandler(),event1
);
1351 void wxTextCtrl::MacSuperShown( bool show
)
1353 bool former
= m_macControlIsShown
;
1354 wxControl::MacSuperShown( show
) ;
1355 if ( (former
!= m_macControlIsShown
) && m_macUsesTXN
)
1357 if ( m_macControlIsShown
)
1358 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1359 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
,(**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1361 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1362 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1366 bool wxTextCtrl::Show(bool show
)
1368 bool former
= m_macControlIsShown
;
1370 bool retval
= wxControl::Show( show
) ;
1372 if ( former
!= m_macControlIsShown
)
1374 if ( m_macControlIsShown
)
1375 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1376 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
,(**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1378 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1379 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1385 // ----------------------------------------------------------------------------
1386 // standard handlers for standard edit menu events
1387 // ----------------------------------------------------------------------------
1389 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
1394 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
1399 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
1404 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
1409 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
1414 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1416 event
.Enable( CanCut() );
1419 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1421 event
.Enable( CanCopy() );
1424 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1426 event
.Enable( CanPaste() );
1429 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1431 event
.Enable( CanUndo() );
1434 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1436 event
.Enable( CanRedo() );