1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/textctrl.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/textctrl.h"
26 #include "wx/button.h"
30 #include <sys/types.h>
36 #include "wx/msgdlg.h"
38 #if wxUSE_STD_IOSTREAM
46 #include "wx/toplevel.h"
47 #include "wx/notebook.h"
48 #include "wx/tabctrl.h"
49 #include "wx/settings.h"
50 #include "wx/filefn.h"
52 #if defined(__BORLANDC__) && !defined(__WIN32__)
54 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
61 #include <MacTextEditor.h>
62 #include <ATSUnicode.h>
63 #include <TextCommon.h>
64 #include <TextEncodingConverter.h>
65 #include "wx/mac/uma.h"
67 #define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL
69 extern wxControl
*wxFindControlFromMacControl(ControlHandle inControl
) ;
71 // CS:TODO we still have a problem getting properly at the text events of a control because under Carbon
72 // the MLTE engine registers itself for the key events thus the normal flow never occurs, the only measure for the
73 // moment is to avoid setting the true focus on the control, the proper solution at the end would be to have
74 // an alternate path for carbon key events that routes automatically into the same wx flow of events
78 /* kmUPTextPart is the part code we return to indicate the user has clicked
79 in the text area of our control */
80 #define kmUPTextPart 1
82 /* kmUPScrollPart is the part code we return to indicate the user has clicked
83 in the scroll bar part of the control. */
84 #define kmUPScrollPart 2
87 /* routines for using existing user pane controls.
88 These routines are useful for cases where you would like to use an
89 existing user pane control in, say, a dialog window as a scrolling
92 /* mUPOpenControl initializes a user pane control so it will be drawn
93 and will behave as a scrolling text edit field inside of a window.
94 This routine performs all of the initialization steps necessary,
95 except it does not create the user pane control itself. theControl
96 should refer to a user pane control that you have either created
97 yourself or extracted from a dialog's control heirarchy using
98 the GetDialogItemAsControl routine. */
99 OSStatus
mUPOpenControl(ControlHandle theControl
, long wxStyle
);
101 /* Utility Routines */
107 /* kUserClickedToFocusPart is a part code we pass to the SetKeyboardFocus
108 routine. In our focus switching routine this part code is understood
109 as meaning 'the user has clicked in the control and we need to switch
110 the current focus to ourselves before we can continue'. */
111 #define kUserClickedToFocusPart 100
114 /* kmUPClickScrollDelayTicks is a time measurement in ticks used to
115 slow the speed of 'auto scrolling' inside of our clickloop routine.
116 This value prevents the text from wizzzzzing by while the mouse
117 is being held down inside of the text area. */
118 #define kmUPClickScrollDelayTicks 3
121 /* STPTextPaneVars is a structure used for storing the the mUP Control's
122 internal variables and state information. A handle to this record is
123 stored in the pane control's reference value field using the
124 SetControlReference routine. */
127 /* OS records referenced */
128 TXNObject fTXNRec
; /* the txn record */
129 TXNFrameID fTXNFrame
; /* the txn frame ID */
130 ControlHandle fUserPaneRec
; /* handle to the user pane control */
131 WindowPtr fOwner
; /* window containing control */
132 GrafPtr fDrawingEnvironment
; /* grafport where control is drawn */
134 Boolean fInFocus
; /* true while the focus rect is drawn around the control */
135 Boolean fIsActive
; /* true while the control is drawn in the active state */
136 Boolean fTEActive
; /* reflects the activation state of the text edit record */
137 Boolean fInDialogWindow
; /* true if displayed in a dialog window */
138 /* calculated locations */
139 Rect fRTextArea
; /* area where the text is drawn */
140 Rect fRFocusOutline
; /* rectangle used to draw the focus box */
141 Rect fRTextOutline
; /* rectangle used to draw the border */
142 RgnHandle fTextBackgroundRgn
; /* background region for the text, erased before calling TEUpdate */
143 /* our focus advance override routine */
144 EventHandlerUPP handlerUPP
;
145 EventHandlerRef handlerRef
;
152 /* Univerals Procedure Pointer variables used by the
153 mUP Control. These variables are set up
154 the first time that mUPOpenControl is called. */
155 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
156 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
157 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
158 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
159 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
160 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
161 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
163 /* TPActivatePaneText activates or deactivates the text edit record
164 according to the value of setActive. The primary purpose of this
165 routine is to ensure each call is only made once. */
166 static void TPActivatePaneText(STPTextPaneVars
**tpvars
, Boolean setActive
) {
167 STPTextPaneVars
*varsp
;
169 if (varsp
->fTEActive
!= setActive
) {
171 varsp
->fTEActive
= setActive
;
173 TXNActivate(varsp
->fTXNRec
, varsp
->fTXNFrame
, varsp
->fTEActive
);
176 TXNFocus( varsp
->fTXNRec
, varsp
->fTEActive
);
181 /* TPFocusPaneText set the focus state for the text record. */
182 static void TPFocusPaneText(STPTextPaneVars
**tpvars
, Boolean setFocus
) {
183 STPTextPaneVars
*varsp
;
185 if (varsp
->fInFocus
!= setFocus
) {
186 varsp
->fInFocus
= setFocus
;
187 TXNFocus( varsp
->fTXNRec
, varsp
->fInFocus
);
192 /* TPPaneDrawProc is called to redraw the control and for update events
193 referring to the control. This routine erases the text area's background,
194 and redraws the text. This routine assumes the scroll bar has been
195 redrawn by a call to DrawControls. */
196 static pascal void TPPaneDrawProc(ControlRef theControl
, ControlPartCode thePart
) {
197 STPTextPaneVars
**tpvars
, *varsp
;
200 /* set up our globals */
202 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
203 if (tpvars
!= NULL
) {
204 state
= HGetState((Handle
) tpvars
);
205 HLock((Handle
) tpvars
);
208 /* save the drawing state */
209 SetPort((**tpvars
).fDrawingEnvironment
);
210 /* verify our boundary */
211 GetControlBounds(theControl
, &bounds
);
213 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
214 if ( ! EqualRect(&bounds
, &varsp
->fRFocusOutline
) ) {
215 // scrollbar is on the border, we add one
216 Rect oldbounds
= varsp
->fRFocusOutline
;
217 InsetRect( &oldbounds
, -1 , -1 ) ;
219 if ( IsControlVisible( theControl
) )
220 InvalWindowRect( GetControlOwner( theControl
) , &oldbounds
) ;
221 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
222 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
223 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
224 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
225 RectRgn(varsp
->fTextBackgroundRgn
, &varsp
->fRTextOutline
);
226 if ( IsControlVisible( theControl
) )
227 TXNSetFrameBounds( varsp
->fTXNRec
, varsp
->fRTextArea
.top
, varsp
->fRTextArea
.left
,
228 varsp
->fRTextArea
.bottom
, varsp
->fRTextArea
.right
, varsp
->fTXNFrame
);
230 TXNSetFrameBounds( varsp
->fTXNRec
, varsp
->fRTextArea
.top
+ 30000 , varsp
->fRTextArea
.left
+ 30000 ,
231 varsp
->fRTextArea
.bottom
+ 30000 , varsp
->fRTextArea
.right
+ 30000 , varsp
->fTXNFrame
);
235 if ( IsControlVisible( theControl
) )
237 /* update the text region */
238 RGBColor white
= { 65535 , 65535 , 65535 } ;
239 RGBBackColor( &white
) ;
240 EraseRgn(varsp
->fTextBackgroundRgn
);
241 TXNDraw(varsp
->fTXNRec
, NULL
);
242 /* restore the drawing environment */
243 /* draw the text frame and focus frame (if necessary) */
244 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
245 if ((**tpvars
).fIsActive
&& varsp
->fInFocus
)
246 DrawThemeFocusRect(&varsp
->fRFocusOutline
, true);
247 /* release our globals */
248 HSetState((Handle
) tpvars
, state
);
254 /* TPPaneHitTestProc is called when the control manager would
255 like to determine what part of the control the mouse resides over.
256 We also call this routine from our tracking proc to determine how
257 to handle mouse clicks. */
258 static pascal ControlPartCode
TPPaneHitTestProc(ControlHandle theControl
, Point where
) {
259 STPTextPaneVars
**tpvars
;
260 ControlPartCode result
;
262 /* set up our locals and lock down our globals*/
264 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
265 if (tpvars
!= NULL
&& IsControlVisible( theControl
) ) {
266 state
= HGetState((Handle
) tpvars
);
267 HLock((Handle
) tpvars
);
268 /* find the region where we clicked */
269 if (PtInRect(where
, &(**tpvars
).fRTextArea
)) {
270 result
= kmUPTextPart
;
272 /* release oure globals */
273 HSetState((Handle
) tpvars
, state
);
282 /* TPPaneTrackingProc is called when the mouse is being held down
283 over our control. This routine handles clicks in the text area
284 and in the scroll bar. */
285 static pascal ControlPartCode
TPPaneTrackingProc(ControlHandle theControl
, Point startPt
, ControlActionUPP actionProc
) {
286 STPTextPaneVars
**tpvars
, *varsp
;
288 ControlPartCode partCodeResult
;
289 /* make sure we have some variables... */
291 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
292 if (tpvars
!= NULL
&& IsControlVisible( theControl
) ) {
294 state
= HGetState((Handle
) tpvars
);
295 HLock((Handle
) tpvars
);
297 /* we don't do any of these functions unless we're in focus */
298 if ( ! varsp
->fInFocus
) {
300 owner
= GetControlOwner(theControl
);
301 ClearKeyboardFocus(owner
);
302 SetKeyboardFocus(owner
, theControl
, kUserClickedToFocusPart
);
304 /* find the location for the click */
305 switch (TPPaneHitTestProc(theControl
, startPt
)) {
307 /* handle clicks in the text part */
309 { SetPort((**tpvars
).fDrawingEnvironment
);
310 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
312 TXNClick( varsp
->fTXNRec
, (const EventRecord
*) wxTheApp
->MacGetCurrentEvent());
315 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
316 TXNClick( varsp
->fTXNRec
, &rec
);
323 HSetState((Handle
) tpvars
, state
);
325 return partCodeResult
;
329 /* TPPaneIdleProc is our user pane idle routine. When our text field
330 is active and in focus, we use this routine to set the cursor. */
331 static pascal void TPPaneIdleProc(ControlHandle theControl
) {
332 STPTextPaneVars
**tpvars
, *varsp
;
334 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
335 if (tpvars
!= NULL
&& IsControlVisible( theControl
) ) {
336 /* if we're not active, then we have nothing to say about the cursor */
337 if ((**tpvars
).fIsActive
) {
341 /* lock down the globals */
342 state
= HGetState((Handle
) tpvars
);
343 HLock((Handle
) tpvars
);
345 /* get the current mouse coordinates (in our window) */
346 SetPortWindowPort(GetControlOwner(theControl
));
347 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
349 /* there's a 'focus thing' and an 'unfocused thing' */
350 if (varsp
->fInFocus
) {
351 /* flash the cursor */
352 SetPort((**tpvars
).fDrawingEnvironment
);
353 TXNIdle(varsp
->fTXNRec
);
355 if (PtInRect(mousep
, &varsp
->fRTextArea
)) {
357 RectRgn((theRgn
= NewRgn()), &varsp
->fRTextArea
);
358 TXNAdjustCursor(varsp
->fTXNRec
, theRgn
);
363 // SetThemeCursor(kThemeArrowCursor);
366 /* if it's in our bounds, set the cursor */
367 GetControlBounds(theControl
, &bounds
);
368 if (PtInRect(mousep
, &bounds
))
370 // SetThemeCursor(kThemeArrowCursor);
374 HSetState((Handle
) tpvars
, state
);
380 /* TPPaneKeyDownProc is called whenever a keydown event is directed
381 at our control. Here, we direct the keydown event to the text
382 edit record and redraw the scroll bar and text field as appropriate. */
383 static pascal ControlPartCode
TPPaneKeyDownProc(ControlHandle theControl
,
384 SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
) {
385 STPTextPaneVars
**tpvars
;
386 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
387 if (tpvars
!= NULL
) {
388 if ((**tpvars
).fInFocus
) {
389 /* turn autoscrolling on and send the key event to text edit */
390 SetPort((**tpvars
).fDrawingEnvironment
);
391 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
393 memset( &ev
, 0 , sizeof( ev
) ) ;
395 ev
.modifiers
= modifiers
;
396 ev
.message
= (( keyCode
<< 8 ) & keyCodeMask
) + ( charCode
& charCodeMask
) ;
397 TXNKeyDown( (**tpvars
).fTXNRec
, &ev
);
400 return kControlEntireControl
;
404 /* TPPaneActivateProc is called when the window containing
405 the user pane control receives activate events. Here, we redraw
406 the control and it's text as necessary for the activation state. */
407 static pascal void TPPaneActivateProc(ControlHandle theControl
, Boolean activating
) {
409 STPTextPaneVars
**tpvars
, *varsp
;
412 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
413 if (tpvars
!= NULL
) {
414 state
= HGetState((Handle
) tpvars
);
415 HLock((Handle
) tpvars
);
417 /* de/activate the text edit record */
418 SetPort((**tpvars
).fDrawingEnvironment
);
419 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
420 GetControlBounds(theControl
, &bounds
);
421 varsp
->fIsActive
= activating
;
422 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
423 /* redraw the frame */
424 if ( IsControlVisible( theControl
) )
426 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
428 DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
);
430 HSetState((Handle
) tpvars
, state
);
435 /* TPPaneFocusProc is called when every the focus changes to or
436 from our control. Herein, switch the focus appropriately
437 according to the parameters and redraw the control as
439 static pascal ControlPartCode
TPPaneFocusProc(ControlHandle theControl
, ControlFocusPart action
) {
440 ControlPartCode focusResult
;
441 STPTextPaneVars
**tpvars
, *varsp
;
444 focusResult
= kControlFocusNoPart
;
445 tpvars
= (STPTextPaneVars
**) GetControlReference(theControl
);
446 if (tpvars
!= NULL
) {
447 state
= HGetState((Handle
) tpvars
);
448 HLock((Handle
) tpvars
);
450 /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
451 tabbing forwards (or shift tabbing backwards) through the items in the dialog,
452 and kControlFocusNextPart will be received. When the user clicks in our field
453 and it is not the current focus, then the constant kUserClickedToFocusPart will
454 be received. The constant kControlFocusNoPart will be received when our control
455 is the current focus and the user clicks in another control. In your focus routine,
456 you should respond to these codes as follows:
458 kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw
459 the control and the focus rectangle as necessary.
461 kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off
462 depending on its current state. redraw the control and the focus rectangle
463 as appropriate for the new focus state. If the focus state is 'off', return the constant
464 kControlFocusNoPart, otherwise return a non-zero part code.
465 kUserClickedToFocusPart - is a constant defined for this example. You should
466 define your own value for handling click-to-focus type events. */
467 /* calculate the next highlight state */
470 case kControlFocusNoPart
:
471 TPFocusPaneText(tpvars
, false);
472 focusResult
= kControlFocusNoPart
;
474 case kUserClickedToFocusPart
:
475 TPFocusPaneText(tpvars
, true);
478 case kControlFocusPrevPart
:
479 case kControlFocusNextPart
:
480 TPFocusPaneText(tpvars
, ( ! varsp
->fInFocus
));
481 focusResult
= varsp
->fInFocus
? 1 : kControlFocusNoPart
;
484 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
485 /* redraw the text fram and focus rectangle to indicate the
487 if ( IsControlVisible( theControl
) )
489 /* save the drawing state */
490 SetPort((**tpvars
).fDrawingEnvironment
);
491 wxMacWindowClipper
clipper( wxFindControlFromMacControl(theControl
) ) ;
492 DrawThemeEditTextFrame(&varsp
->fRTextOutline
, varsp
->fIsActive
? kThemeStateActive
: kThemeStateInactive
);
493 DrawThemeFocusRect(&varsp
->fRFocusOutline
, varsp
->fIsActive
&& varsp
->fInFocus
);
496 HSetState((Handle
) tpvars
, state
);
502 /* mUPOpenControl initializes a user pane control so it will be drawn
503 and will behave as a scrolling text edit field inside of a window.
504 This routine performs all of the initialization steps necessary,
505 except it does not create the user pane control itself. theControl
506 should refer to a user pane control that you have either created
507 yourself or extracted from a dialog's control heirarchy using
508 the GetDialogItemAsControl routine. */
509 OSStatus
mUPOpenControl(ControlHandle theControl
, long wxStyle
)
513 STPTextPaneVars
**tpvars
, *varsp
;
514 OSStatus err
= noErr
;
515 RGBColor rgbWhite
= {0xFFFF, 0xFFFF, 0xFFFF};
518 /* set up our globals */
519 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(TPPaneDrawProc
);
520 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(TPPaneHitTestProc
);
521 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(TPPaneTrackingProc
);
522 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(TPPaneIdleProc
);
523 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc
);
524 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(TPPaneActivateProc
);
525 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(TPPaneFocusProc
);
527 /* allocate our private storage */
528 tpvars
= (STPTextPaneVars
**) NewHandleClear(sizeof(STPTextPaneVars
));
529 SetControlReference(theControl
, (long) tpvars
);
530 HLock((Handle
) tpvars
);
532 /* set the initial settings for our private data */
533 varsp
->fMultiline
= wxStyle
& wxTE_MULTILINE
;
534 varsp
->fInFocus
= false;
535 varsp
->fIsActive
= true;
536 varsp
->fTEActive
= true; // in order to get a deactivate
537 varsp
->fUserPaneRec
= theControl
;
538 theWindow
= varsp
->fOwner
= GetControlOwner(theControl
);
540 varsp
->fDrawingEnvironment
= (GrafPtr
) GetWindowPort(theWindow
);
542 varsp
->fInDialogWindow
= ( GetWindowKind(varsp
->fOwner
) == kDialogWindowKind
);
543 /* set up the user pane procedures */
544 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
545 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
546 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
547 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
548 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
549 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
550 SetControlData(theControl
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
551 /* calculate the rectangles used by the control */
552 GetControlBounds(theControl
, &bounds
);
553 SetRect(&varsp
->fRFocusOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
554 SetRect(&varsp
->fRTextOutline
, bounds
.left
, bounds
.top
, bounds
.right
, bounds
.bottom
);
555 SetRect(&varsp
->fRTextArea
, bounds
.left
+ 2 , bounds
.top
+ (varsp
->fMultiline
? 0 : 2) ,
556 bounds
.right
- (varsp
->fMultiline
? 0 : 2), bounds
.bottom
- (varsp
->fMultiline
? 0 : 2));
557 /* calculate the background region for the text. In this case, it's kindof
558 and irregular region because we're setting the scroll bar a little ways inside
560 RectRgn((varsp
->fTextBackgroundRgn
= NewRgn()), &varsp
->fRTextOutline
);
562 /* set up the drawing environment */
563 SetPort(varsp
->fDrawingEnvironment
);
565 /* create the new edit field */
567 TXNFrameOptions frameOptions
=
568 kTXNDontDrawCaretWhenInactiveMask
;
569 if ( ! ( wxStyle
& wxTE_NOHIDESEL
) )
570 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
572 if ( wxStyle
& wxTE_MULTILINE
)
574 if ( ! ( wxStyle
& wxTE_DONTWRAP
) )
575 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
578 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
579 frameOptions
|= kTXNWantHScrollBarMask
;
582 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
583 frameOptions
|= kTXNWantVScrollBarMask
;
586 frameOptions
|= kTXNSingleLineOnlyMask
;
588 if ( wxStyle
& wxTE_READONLY
)
589 frameOptions
|= kTXNReadOnlyMask
;
591 TXNNewObject(NULL
, varsp
->fOwner
, &varsp
->fRTextArea
,
593 kTXNTextEditStyleFrameType
,
595 kTXNSystemDefaultEncoding
,
596 &varsp
->fTXNRec
, &varsp
->fTXNFrame
, (TXNObjectRefcon
) tpvars
);
598 if ( !IsControlVisible( theControl
) )
599 TXNSetFrameBounds( varsp
->fTXNRec
, varsp
->fRTextArea
.top
+ 30000 , varsp
->fRTextArea
.left
+ 30000 ,
600 varsp
->fRTextArea
.bottom
+ 30000 , varsp
->fRTextArea
.right
+ 30000 , varsp
->fTXNFrame
);
603 if ( (wxStyle
& wxTE_MULTILINE
) && (wxStyle
& wxTE_DONTWRAP
) )
605 TXNControlTag tag
= kTXNWordWrapStateTag
;
607 dat
.uValue
= kTXNNoAutoWrap
;
608 TXNSetTXNObjectControls( varsp
->fTXNRec
, false , 1 , &tag
, &dat
) ;
614 GetThemeFont(kThemeSmallSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
616 TXNTypeAttributes typeAttr
[] =
618 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
619 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
620 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
623 err
= TXNSetTypeAttributes (varsp
->fTXNRec
, sizeof( typeAttr
) / sizeof(TXNTypeAttributes
) , typeAttr
,
626 /* set the field's background */
628 tback
.bgType
= kTXNBackgroundTypeRGB
;
629 tback
.bg
.color
= rgbWhite
;
630 TXNSetBackground( varsp
->fTXNRec
, &tback
);
632 /* unlock our storage */
633 HUnlock((Handle
) tpvars
);
634 /* perform final activations and setup for our text field. Here,
635 we assume that the window is going to be the 'active' window. */
636 TPActivatePaneText(tpvars
, varsp
->fIsActive
&& varsp
->fInFocus
);
644 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
646 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
647 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
648 EVT_CHAR(wxTextCtrl::OnChar
)
649 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
650 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
651 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
652 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
653 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
655 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
656 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
657 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
658 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
659 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
662 static void SetTXNData( TXNObject txn
, const wxString
& st
, TXNOffset start
, TXNOffset end
)
665 #if SIZEOF_WCHAR_T == 2
666 size_t len
= st
.Len() ;
667 TXNSetData( txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2,
670 wxMBConvUTF16BE converter
;
671 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str() , 0 ) ;
672 UniChar
*unibuf
= (UniChar
*) malloc(byteBufferLen
) ;
673 converter
.WC2MB( (char*) unibuf
, st
.wc_str() , byteBufferLen
) ;
674 TXNSetData( txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
,
679 wxCharBuffer text
= st
.mb_str(wxConvLocal
) ;
680 TXNSetData( txn
, kTXNTextData
, (void*)text
.data(), strlen( text
) ,
686 void wxTextCtrl::Init()
690 m_macTXNvars
= NULL
;
691 m_macUsesTXN
= false ;
696 m_maxLength
= TE_UNLIMITED_LENGTH
;
699 wxTextCtrl::~wxTextCtrl()
703 SetControlReference((ControlHandle
)m_macControl
, 0) ;
704 TXNDeleteObject((TXNObject
)m_macTXN
);
705 /* delete our private storage */
706 DisposeHandle((Handle
) m_macTXNvars
);
707 /* zero the control reference */
711 const short kVerticalMargin
= 2 ;
712 const short kHorizontalMargin
= 2 ;
714 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
717 const wxSize
& size
, long style
,
718 const wxValidator
& validator
,
719 const wxString
& name
)
723 m_macTXNvars
= NULL
;
724 m_macUsesTXN
= false ;
727 m_macUsesTXN
= ! (style
& wxTE_PASSWORD
) ;
729 m_macUsesTXN
&= (TXNInitTextension
!= (void*) kUnresolvedCFragSymbolAddress
) ;
731 // base initialization
732 if ( !wxTextCtrlBase::Create(parent
, id
, pos
, size
, style
& ~(wxHSCROLL
|wxVSCROLL
), validator
, name
) )
735 wxSize mySize
= size
;
738 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
739 m_macVerticalBorder
= 3 ;
743 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
744 m_macVerticalBorder
= 5 ;
751 if ( mySize.y == -1 )
754 if ( m_windowStyle & wxTE_MULTILINE )
757 mySize.y += 2 * m_macVerticalBorder ;
760 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, mySize
,style
, validator
, name
, &bounds
, title
) ;
762 if ( m_windowStyle
& wxTE_MULTILINE
)
764 wxASSERT_MSG( !(m_windowStyle
& wxTE_PROCESS_ENTER
),
765 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
767 m_windowStyle
|= wxTE_PROCESS_ENTER
;
770 if ( m_windowStyle
& wxTE_READONLY
)
776 wxMacConvertNewlines13To10( &st
) ;
779 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, "\p" , false , 0 , 0 , 1,
780 (style
& wxTE_PASSWORD
) ? kControlEditTextPasswordProc
: kControlEditTextProc
, (long) this ) ;
782 ::GetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextTEHandleTag
, sizeof( TEHandle
) , (char*)((TEHandle
*)&m_macTE
) , &size
) ;
789 featurSet
= kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
790 | kControlWantsActivate
| kControlHandlesTracking
| kControlHasSpecialBackground
791 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
792 /* create the control */
793 m_macControl
= (WXWidget
) ::NewControl(MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, "\p", false , featurSet
, 0, featurSet
, kControlUserPaneProc
, 0);
794 /* set up the mUP specific features and data */
795 mUPOpenControl((ControlHandle
) m_macControl
, m_windowStyle
);
797 MacPostControlCreate() ;
801 wxCharBuffer text
= st
.mb_str(wxConvLocal
) ;
802 ::SetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, strlen(text
) , text
) ;
806 STPTextPaneVars
**tpvars
;
808 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
809 /* set the text in the record */
810 m_macTXN
= (**tpvars
).fTXNRec
;
811 SetTXNData( (TXNObject
) m_macTXN
, st
, kTXNStartOffset
, kTXNEndOffset
) ;
812 m_macTXNvars
= tpvars
;
813 m_macUsesTXN
= true ;
814 TXNSetSelection( (TXNObject
) m_macTXN
, 0, 0);
815 TXNShowSelection( (TXNObject
) m_macTXN
, kTXNShowStart
);
821 wxString
wxTextCtrl::GetValue() const
828 err
= ::GetControlDataSize((ControlHandle
) m_macControl
, 0,
829 ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, &actualSize
) ;
832 return wxEmptyString
;
834 if ( actualSize
> 0 )
836 wxCharBuffer
buf(actualSize
) ;
837 ::GetControlData( (ControlHandle
) m_macControl
, 0,
838 ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
,
839 actualSize
, buf
.data() , &actualSize
) ;
840 result
= wxString( buf
, wxConvLocal
) ;
847 err
= TXNGetDataEncoded( ((TXNObject
) m_macTXN
), kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
855 actualSize
= GetHandleSize( theText
) / sizeof( UniChar
) ;
856 if ( actualSize
> 0 )
858 wxChar
*ptr
= result
.GetWriteBuf(actualSize
*sizeof(wxChar
)) ;
859 #if SIZEOF_WCHAR_T == 2
860 wxStrncpy( ptr
, (wxChar
*) *theText
, actualSize
) ;
862 wxMBConvUTF16BE converter
;
864 converter
.MB2WC( ptr
, (const char*)*theText
, actualSize
) ;
867 ptr
[actualSize
] = 0 ;
868 result
.UngetWriteBuf( actualSize
*sizeof(wxChar
) ) ;
870 DisposeHandle( theText
) ;
874 err
= TXNGetDataEncoded( ((TXNObject
) m_macTXN
), kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
882 actualSize
= GetHandleSize( theText
) ;
883 if ( actualSize
> 0 )
886 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
889 DisposeHandle( theText
) ;
893 wxMacConvertNewlines10To13( &result
) ;
897 void wxTextCtrl::GetSelection(long* from
, long* to
) const
901 *from
= (**((TEHandle
) m_macTE
)).selStart
;
902 *to
= (**((TEHandle
) m_macTE
)).selEnd
;
906 TXNGetSelection( (TXNObject
) m_macTXN
, (TXNOffset
*) from
, (TXNOffset
*) to
) ;
910 void wxTextCtrl::SetValue(const wxString
& str
)
913 wxMacConvertNewlines13To10( &st
) ;
916 wxCharBuffer text
= st
.mb_str(wxConvLocal
) ;
917 ::SetControlData( (ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, strlen(text
) , text
) ;
921 bool formerEditable
= m_editable
;
922 if ( !formerEditable
)
924 SetTXNData( (TXNObject
) m_macTXN
, st
, kTXNStartOffset
, kTXNEndOffset
) ;
925 TXNSetSelection( (TXNObject
) m_macTXN
, 0, 0);
926 TXNShowSelection( (TXNObject
) m_macTXN
, kTXNShowStart
);
927 if ( !formerEditable
)
928 SetEditable(formerEditable
) ;
933 void wxTextCtrl::SetMaxLength(unsigned long len
)
938 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
942 bool formerEditable
= m_editable
;
943 if ( !formerEditable
)
945 TXNTypeAttributes typeAttr
[4] ;
946 Str255 fontName
= "\pMonaco" ;
947 SInt16 fontSize
= 12 ;
948 Style fontStyle
= normal
;
950 int attrCounter
= 0 ;
951 if ( style
.HasFont() )
953 const wxFont
&font
= style
.GetFont() ;
954 wxMacStringToPascal( font
.GetFaceName() , fontName
) ;
955 fontSize
= font
.GetPointSize() ;
956 if ( font
.GetUnderlined() )
957 fontStyle
|= underline
;
958 if ( font
.GetWeight() == wxBOLD
)
960 if ( font
.GetStyle() == wxITALIC
)
961 fontStyle
|= italic
;
963 typeAttr
[attrCounter
].tag
= kTXNQDFontNameAttribute
;
964 typeAttr
[attrCounter
].size
= kTXNQDFontNameAttributeSize
;
965 typeAttr
[attrCounter
].data
.dataPtr
= (void*) fontName
;
966 typeAttr
[attrCounter
+1].tag
= kTXNQDFontSizeAttribute
;
967 typeAttr
[attrCounter
+1].size
= kTXNFontSizeAttributeSize
;
968 typeAttr
[attrCounter
+1].data
.dataValue
= (fontSize
<< 16) ;
969 typeAttr
[attrCounter
+2].tag
= kTXNQDFontStyleAttribute
;
970 typeAttr
[attrCounter
+2].size
= kTXNQDFontStyleAttributeSize
;
971 typeAttr
[attrCounter
+2].data
.dataValue
= fontStyle
;
975 if ( style
.HasTextColour() )
977 typeAttr
[attrCounter
].tag
= kTXNQDFontColorAttribute
;
978 typeAttr
[attrCounter
].size
= kTXNQDFontColorAttributeSize
;
979 typeAttr
[attrCounter
].data
.dataPtr
= (void*) &color
;
980 color
= MAC_WXCOLORREF(style
.GetTextColour().GetPixel()) ;
984 if ( attrCounter
> 0 )
988 #endif // __WXDEBUG__
989 TXNSetTypeAttributes ((TXNObject
)m_macTXN
, attrCounter
, typeAttr
, start
,end
);
990 wxASSERT_MSG( status
== noErr
, wxT("Couldn't set text attributes") ) ;
992 if ( !formerEditable
)
993 SetEditable(formerEditable
) ;
998 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
1000 wxTextCtrlBase::SetDefaultStyle( style
) ;
1001 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
1005 // Clipboard operations
1006 void wxTextCtrl::Copy()
1010 if ( !m_macUsesTXN
)
1012 TECopy( ((TEHandle
) m_macTE
) ) ;
1013 ClearCurrentScrap();
1015 MacRedrawControl() ;
1019 ClearCurrentScrap();
1020 TXNCopy((TXNObject
)m_macTXN
);
1021 TXNConvertToPublicScrap();
1026 void wxTextCtrl::Cut()
1030 if ( !m_macUsesTXN
)
1032 TECut( ((TEHandle
) m_macTE
) ) ;
1033 ClearCurrentScrap();
1035 MacRedrawControl() ;
1039 ClearCurrentScrap();
1040 TXNCut((TXNObject
)m_macTXN
);
1041 TXNConvertToPublicScrap();
1043 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1044 event
.SetEventObject( this );
1045 GetEventHandler()->ProcessEvent(event
);
1049 void wxTextCtrl::Paste()
1053 if ( !m_macUsesTXN
)
1056 TEPaste( (TEHandle
) m_macTE
) ;
1057 MacRedrawControl() ;
1061 TXNConvertFromPublicScrap();
1062 TXNPaste((TXNObject
)m_macTXN
);
1063 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
1065 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1066 event
.SetEventObject( this );
1067 GetEventHandler()->ProcessEvent(event
);
1071 bool wxTextCtrl::CanCopy() const
1073 // Can copy if there's a selection
1075 GetSelection(& from
, & to
);
1076 return (from
!= to
);
1079 bool wxTextCtrl::CanCut() const
1081 if ( !IsEditable() )
1085 // Can cut if there's a selection
1087 GetSelection(& from
, & to
);
1088 return (from
!= to
);
1091 bool wxTextCtrl::CanPaste() const
1097 OSStatus err
= noErr
;
1100 err
= GetCurrentScrap( &scrapRef
);
1101 if ( err
!= noTypeErr
&& err
!= memFullErr
)
1103 ScrapFlavorFlags flavorFlags
;
1106 if (( err
= GetScrapFlavorFlags( scrapRef
, 'TEXT', &flavorFlags
)) == noErr
)
1108 if (( err
= GetScrapFlavorSize( scrapRef
, 'TEXT', &byteCount
)) == noErr
)
1118 if ( GetScrap( NULL
, 'TEXT' , &offset
) > 0 )
1126 void wxTextCtrl::SetEditable(bool editable
)
1128 if ( editable
!= m_editable
)
1130 m_editable
= editable
;
1131 if ( !m_macUsesTXN
)
1134 UMAActivateControl( (ControlHandle
) m_macControl
) ;
1136 UMADeactivateControl((ControlHandle
) m_macControl
) ;
1140 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1141 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1142 TXNSetTXNObjectControls( (TXNObject
) m_macTXN
, false , sizeof(tag
) / sizeof (TXNControlTag
) , tag
, data
) ;
1147 void wxTextCtrl::SetInsertionPoint(long pos
)
1149 SetSelection( pos
, pos
) ;
1152 void wxTextCtrl::SetInsertionPointEnd()
1154 wxTextPos pos
= GetLastPosition();
1155 SetInsertionPoint(pos
);
1158 long wxTextCtrl::GetInsertionPoint() const
1161 GetSelection( &begin
, &end
) ;
1165 wxTextPos
wxTextCtrl::GetLastPosition() const
1167 if ( !m_macUsesTXN
)
1169 return (**((TEHandle
) m_macTE
)).teLength
;
1175 OSErr err
= TXNGetDataEncoded( (TXNObject
) m_macTXN
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1183 actualsize
= GetHandleSize( theText
) ;
1184 DisposeHandle( theText
) ;
1190 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
1192 wxString value
= str
;
1193 wxMacConvertNewlines13To10( &value
) ;
1194 if ( !m_macUsesTXN
)
1196 ControlEditTextSelectionRec selection
;
1198 selection
.selStart
= from
;
1199 selection
.selEnd
= to
;
1200 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
1201 TESetSelect( from
, to
, ((TEHandle
) m_macTE
) ) ;
1202 TEDelete( ((TEHandle
) m_macTE
) ) ;
1203 TEInsert( value
, value
.length() , ((TEHandle
) m_macTE
) ) ;
1207 bool formerEditable
= m_editable
;
1208 if ( !formerEditable
)
1210 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
1211 TXNClear( ((TXNObject
) m_macTXN
) ) ;
1212 SetTXNData( (TXNObject
) m_macTXN
, str
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1213 if ( !formerEditable
)
1214 SetEditable( formerEditable
) ;
1219 void wxTextCtrl::Remove(long from
, long to
)
1221 if ( !m_macUsesTXN
)
1223 ControlEditTextSelectionRec selection
;
1225 selection
.selStart
= from
;
1226 selection
.selEnd
= to
;
1227 ::SetControlData( (ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
1228 TEDelete( ((TEHandle
) m_macTE
) ) ;
1232 bool formerEditable
= m_editable
;
1233 if ( !formerEditable
)
1235 TXNSetSelection( ((TXNObject
) m_macTXN
) , from
, to
) ;
1236 TXNClear( ((TXNObject
) m_macTXN
) ) ;
1237 if ( !formerEditable
)
1238 SetEditable( formerEditable
) ;
1243 void wxTextCtrl::SetSelection(long from
, long to
)
1245 if ( !m_macUsesTXN
)
1247 ControlEditTextSelectionRec selection
;
1248 if ((from
== -1) && (to
== -1))
1250 selection
.selStart
= 0 ;
1251 selection
.selEnd
= 32767 ;
1255 selection
.selStart
= from
;
1256 selection
.selEnd
= to
;
1259 TESetSelect( selection
.selStart
, selection
.selEnd
, ((TEHandle
) m_macTE
) ) ;
1260 ::SetControlData((ControlHandle
) m_macControl
, 0, kControlEditTextSelectionTag
, sizeof( selection
) , (char*) &selection
) ;
1264 STPTextPaneVars
**tpvars
;
1265 /* set up our locals */
1266 tpvars
= (STPTextPaneVars
**) GetControlReference((ControlHandle
) m_macControl
);
1267 /* and our drawing environment as the operation
1268 may force a redraw in the text area. */
1269 SetPort((**tpvars
).fDrawingEnvironment
);
1270 /* change the selection */
1271 if ((from
== -1) && (to
== -1))
1272 TXNSelectAll((TXNObject
) m_macTXN
);
1274 TXNSetSelection( (**tpvars
).fTXNRec
, from
, to
);
1275 TXNShowSelection( (TXNObject
) m_macTXN
, kTXNShowStart
);
1279 bool wxTextCtrl::LoadFile(const wxString
& file
)
1281 if ( wxTextCtrlBase::LoadFile(file
) )
1289 void wxTextCtrl::WriteText(const wxString
& str
)
1292 wxMacConvertNewlines13To10( &st
) ;
1293 if ( !m_macUsesTXN
)
1295 wxCharBuffer text
= st
.mb_str(wxConvLocal
) ;
1296 TEInsert( text
, strlen(text
) , ((TEHandle
) m_macTE
) ) ;
1300 bool formerEditable
= m_editable
;
1301 if ( !formerEditable
)
1303 long start
, end
, dummy
;
1304 GetSelection( &start
, &dummy
) ;
1305 SetTXNData( (TXNObject
) m_macTXN
, st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1306 GetSelection( &dummy
, &end
) ;
1307 SetStyle( start
, end
, GetDefaultStyle() ) ;
1308 if ( !formerEditable
)
1309 SetEditable( formerEditable
) ;
1311 MacRedrawControl() ;
1314 void wxTextCtrl::AppendText(const wxString
& text
)
1316 SetInsertionPointEnd();
1320 void wxTextCtrl::Clear()
1322 if ( !m_macUsesTXN
)
1324 ::SetControlData((ControlHandle
) m_macControl
, 0, ( m_windowStyle
& wxTE_PASSWORD
) ? kControlEditTextPasswordTag
: kControlEditTextTextTag
, 0 , (char*) ((const char*)NULL
) ) ;
1328 TXNSetSelection( (TXNObject
)m_macTXN
, kTXNStartOffset
, kTXNEndOffset
) ;
1329 TXNClear((TXNObject
)m_macTXN
);
1334 bool wxTextCtrl::IsModified() const
1339 bool wxTextCtrl::IsEditable() const
1341 return IsEnabled() && m_editable
;
1344 bool wxTextCtrl::AcceptsFocus() const
1346 // we don't want focus if we can't be edited
1347 return /*IsEditable() && */ wxControl::AcceptsFocus();
1350 wxSize
wxTextCtrl::DoGetBestSize() const
1365 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1367 int wText = DEFAULT_ITEM_WIDTH;
1369 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1371 return wxSize(wText, hText);
1373 if ( m_windowStyle
& wxTE_MULTILINE
)
1377 hText
+= 2 * m_macVerticalBorder
;
1378 wText
+= 2 * m_macHorizontalBorder
;
1379 //else: for single line control everything is ok
1380 return wxSize(wText
, hText
);
1383 // ----------------------------------------------------------------------------
1385 // ----------------------------------------------------------------------------
1387 void wxTextCtrl::Undo()
1393 TXNUndo((TXNObject
)m_macTXN
);
1398 void wxTextCtrl::Redo()
1404 TXNRedo((TXNObject
)m_macTXN
);
1409 bool wxTextCtrl::CanUndo() const
1411 if ( !IsEditable() )
1417 return TXNCanUndo((TXNObject
)m_macTXN
,NULL
);
1422 bool wxTextCtrl::CanRedo() const
1424 if ( !IsEditable() )
1430 return TXNCanRedo((TXNObject
)m_macTXN
,NULL
);
1435 // Makes modifie or unmodified
1436 void wxTextCtrl::MarkDirty()
1441 void wxTextCtrl::DiscardEdits()
1446 int wxTextCtrl::GetNumberOfLines() const
1451 TXNGetLineCount((TXNObject
)m_macTXN
, &lines
) ;
1456 wxString content
= GetValue() ;
1459 for (size_t i
= 0; i
< content
.length() ; i
++)
1461 if (content
[i
] == '\r') count
++;
1467 long wxTextCtrl::XYToPosition(long x
, long y
) const
1473 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
1478 void wxTextCtrl::ShowPosition(long pos
)
1480 #if TARGET_RT_MAC_MACHO && defined(AVAILABLE_MAC_OS_X_VERSION_10_2_AND_LATER)
1485 TXNOffset selstart
, selend
;
1486 TXNGetSelection( (TXNObject
) m_macTXN
, &selstart
, &selend
) ;
1487 TXNOffsetToPoint( (TXNObject
) m_macTXN
, selstart
, ¤t
);
1488 TXNOffsetToPoint( (TXNObject
) m_macTXN
, pos
, &desired
);
1489 //TODO use HIPoints for 10.3 and above
1490 if ( (UInt32
) TXNScroll
!= (UInt32
) kUnresolvedCFragSymbolAddress
)
1492 OSErr theErr
= noErr
;
1493 SInt32 dv
= desired
.v
- current
.v
;
1494 SInt32 dh
= desired
.h
- current
.h
;
1495 TXNShowSelection( (TXNObject
) m_macTXN
, true ) ;
1496 theErr
= TXNScroll( (TXNObject
) m_macTXN
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
1497 wxASSERT_MSG( theErr
== noErr
, _T("TXNScroll returned an error!") );
1503 int wxTextCtrl::GetLineLength(long lineNo
) const
1505 // TODO change this if possible to reflect real lines
1506 wxString content
= GetValue() ;
1510 for (size_t i
= 0; i
< content
.length() ; i
++)
1512 if (count
== lineNo
)
1514 // Count chars in line then
1516 for (size_t j
= i
; j
< content
.length(); j
++)
1519 if (content
[j
] == '\n') return count
;
1524 if (content
[i
] == '\n') count
++;
1529 wxString
wxTextCtrl::GetLineText(long lineNo
) const
1531 // TODO change this if possible to reflect real lines
1532 wxString content
= GetValue() ;
1536 for (size_t i
= 0; i
< content
.length() ; i
++)
1538 if (count
== lineNo
)
1540 // Add chars in line then
1543 for (size_t j
= i
; j
< content
.length(); j
++)
1545 if (content
[j
] == '\n')
1553 if (content
[i
] == '\n') count
++;
1555 return wxEmptyString
;
1562 void wxTextCtrl::Command(wxCommandEvent
& event
)
1564 SetValue (event
.GetString());
1565 ProcessCommand (event
);
1568 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
1570 // By default, load the first file into the text window.
1571 if (event
.GetNumberOfFiles() > 0)
1573 LoadFile(event
.GetFiles()[0]);
1577 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
1579 int key
= event
.GetKeyCode() ;
1580 bool eat_key
= false ;
1582 if ( key
== 'c' && event
.MetaDown() )
1589 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
1590 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
1591 /* && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END */
1598 // assume that any key not processed yet is going to modify the control
1601 if ( key
== 'v' && event
.MetaDown() )
1607 if ( key
== 'x' && event
.MetaDown() )
1616 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
1618 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1619 event
.SetEventObject( this );
1620 event
.SetString( GetValue() );
1621 if ( GetEventHandler()->ProcessEvent(event
) )
1624 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1626 wxWindow
*parent
= GetParent();
1627 while( parent
&& !parent
->IsTopLevel() && parent
->GetDefaultItem() == NULL
) {
1628 parent
= parent
->GetParent() ;
1630 if ( parent
&& parent
->GetDefaultItem() )
1632 wxButton
*def
= wxDynamicCast(parent
->GetDefaultItem(),
1634 if ( def
&& def
->IsEnabled() )
1636 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
1637 event
.SetEventObject(def
);
1638 def
->Command(event
);
1643 // this will make wxWidgets eat the ENTER key so that
1644 // we actually prevent line wrapping in a single line
1652 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
1655 if (!event
.ShiftDown())
1656 flags
|= wxNavigationKeyEvent::IsForward
;
1657 if (event
.ControlDown())
1658 flags
|= wxNavigationKeyEvent::WinChange
;
1664 // This is necessary (don't know why) or the tab will not
1666 WriteText(wxT("\t"));
1673 // perform keystroke handling
1675 if ( m_macUsesTXN
&& wxTheApp
->MacGetCurrentEvent() != NULL
&& wxTheApp
->MacGetCurrentEventHandlerCallRef() != NULL
)
1676 CallNextEventHandler((EventHandlerCallRef
)wxTheApp
->MacGetCurrentEventHandlerCallRef() , (EventRef
) wxTheApp
->MacGetCurrentEvent() ) ;
1680 if ( wxMacConvertEventToRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) )
1682 EventRecord
*ev
= &rec
;
1685 keychar
= short(ev
->message
& charCodeMask
);
1686 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1688 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
1692 EventRecord
*ev
= (EventRecord
*) wxTheApp
->MacGetCurrentEvent() ;
1695 keychar
= short(ev
->message
& charCodeMask
);
1696 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
1698 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
1701 if ( ( key
>= 0x20 && key
< WXK_START
) ||
1702 key
== WXK_RETURN
||
1703 key
== WXK_DELETE
||
1706 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
1707 event1
.SetEventObject( this );
1708 wxPostEvent(GetEventHandler(),event1
);
1712 void wxTextCtrl::MacSuperShown( bool show
)
1714 bool former
= m_macControlIsShown
;
1715 wxControl::MacSuperShown( show
) ;
1716 if ( (former
!= m_macControlIsShown
) && m_macUsesTXN
)
1718 if ( m_macControlIsShown
)
1719 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1720 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
,(**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1722 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1723 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1727 bool wxTextCtrl::Show(bool show
)
1729 bool former
= m_macControlIsShown
;
1731 bool retval
= wxControl::Show( show
) ;
1733 if ( former
!= m_macControlIsShown
&& m_macUsesTXN
)
1735 if ( m_macControlIsShown
)
1736 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1737 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
,(**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1739 TXNSetFrameBounds( (TXNObject
) m_macTXN
, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.top
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.left
,
1740 (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.bottom
+ 30000, (**(STPTextPaneVars
**)m_macTXNvars
).fRTextArea
.right
, (**(STPTextPaneVars
**)m_macTXNvars
).fTXNFrame
);
1746 // ----------------------------------------------------------------------------
1747 // standard handlers for standard edit menu events
1748 // ----------------------------------------------------------------------------
1750 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1755 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1760 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1765 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1770 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1775 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1777 event
.Enable( CanCut() );
1780 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1782 event
.Enable( CanCopy() );
1785 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1787 event
.Enable( CanPaste() );
1790 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1792 event
.Enable( CanUndo() );
1795 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1797 event
.Enable( CanRedo() );
1800 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1805 return wxWindow::MacSetupCursor( pt
) ;