]> git.saurik.com Git - wxWidgets.git/blob - src/mac/textctrl.cpp
wxCocoa: Added preliminary Blit support
[wxWidgets.git] / src / mac / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "textctrl.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #if wxUSE_TEXTCTRL
19
20 #ifdef __DARWIN__
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #else
24 #include <stat.h>
25 #endif
26
27 #if wxUSE_STD_IOSTREAM
28 #if wxUSE_IOSTREAMH
29 #include <fstream.h>
30 #else
31 #include <fstream>
32 #endif
33 #endif
34
35 #include "wx/app.h"
36 #include "wx/dc.h"
37 #include "wx/button.h"
38 #include "wx/toplevel.h"
39 #include "wx/textctrl.h"
40 #include "wx/notebook.h"
41 #include "wx/tabctrl.h"
42 #include "wx/settings.h"
43 #include "wx/filefn.h"
44 #include "wx/utils.h"
45
46 #if defined(__BORLANDC__) && !defined(__WIN32__)
47 #include <alloc.h>
48 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
49 #include <malloc.h>
50 #endif
51
52 #ifndef __DARWIN__
53 #include <Scrap.h>
54 #endif
55 #include <MacTextEditor.h>
56 #include "ATSUnicode.h"
57 #include "TextCommon.h"
58 #include "TextEncodingConverter.h"
59 #include "wx/mac/uma.h"
60
61 #define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL
62
63 extern wxApp *wxTheApp ;
64 extern wxControl *wxFindControlFromMacControl(ControlHandle inControl ) ;
65
66 // CS:TODO we still have a problem getting properly at the text events of a control because under Carbon
67 // the MLTE engine registers itself for the key events thus the normal flow never occurs, the only measure for the
68 // moment is to avoid setting the true focus on the control, the proper solution at the end would be to have
69 // an alternate path for carbon key events that routes automatically into the same wx flow of events
70
71 #include "MacTextEditor.h"
72
73 /* part codes */
74
75 /* kmUPTextPart is the part code we return to indicate the user has clicked
76 in the text area of our control */
77 #define kmUPTextPart 1
78
79 /* kmUPScrollPart is the part code we return to indicate the user has clicked
80 in the scroll bar part of the control. */
81 #define kmUPScrollPart 2
82
83
84 /* routines for using existing user pane controls.
85 These routines are useful for cases where you would like to use an
86 existing user pane control in, say, a dialog window as a scrolling
87 text edit field.*/
88
89 /* mUPOpenControl initializes a user pane control so it will be drawn
90 and will behave as a scrolling text edit field inside of a window.
91 This routine performs all of the initialization steps necessary,
92 except it does not create the user pane control itself. theControl
93 should refer to a user pane control that you have either created
94 yourself or extracted from a dialog's control heirarchy using
95 the GetDialogItemAsControl routine. */
96 OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle);
97
98 /* Utility Routines */
99
100 enum {
101 kShiftKeyCode = 56
102 };
103
104 /* kUserClickedToFocusPart is a part code we pass to the SetKeyboardFocus
105 routine. In our focus switching routine this part code is understood
106 as meaning 'the user has clicked in the control and we need to switch
107 the current focus to ourselves before we can continue'. */
108 #define kUserClickedToFocusPart 100
109
110
111 /* kmUPClickScrollDelayTicks is a time measurement in ticks used to
112 slow the speed of 'auto scrolling' inside of our clickloop routine.
113 This value prevents the text from wizzzzzing by while the mouse
114 is being held down inside of the text area. */
115 #define kmUPClickScrollDelayTicks 3
116
117
118 /* STPTextPaneVars is a structure used for storing the the mUP Control's
119 internal variables and state information. A handle to this record is
120 stored in the pane control's reference value field using the
121 SetControlReference routine. */
122
123 typedef struct {
124 /* OS records referenced */
125 TXNObject fTXNRec; /* the txn record */
126 TXNFrameID fTXNFrame; /* the txn frame ID */
127 ControlHandle fUserPaneRec; /* handle to the user pane control */
128 WindowPtr fOwner; /* window containing control */
129 GrafPtr fDrawingEnvironment; /* grafport where control is drawn */
130 /* flags */
131 Boolean fInFocus; /* true while the focus rect is drawn around the control */
132 Boolean fIsActive; /* true while the control is drawn in the active state */
133 Boolean fTEActive; /* reflects the activation state of the text edit record */
134 Boolean fInDialogWindow; /* true if displayed in a dialog window */
135 /* calculated locations */
136 Rect fRTextArea; /* area where the text is drawn */
137 Rect fRFocusOutline; /* rectangle used to draw the focus box */
138 Rect fRTextOutline; /* rectangle used to draw the border */
139 RgnHandle fTextBackgroundRgn; /* background region for the text, erased before calling TEUpdate */
140 /* our focus advance override routine */
141 EventHandlerUPP handlerUPP;
142 EventHandlerRef handlerRef;
143 bool fMultiline ;
144 } STPTextPaneVars;
145
146
147
148
149 /* Univerals Procedure Pointer variables used by the
150 mUP Control. These variables are set up
151 the first time that mUPOpenControl is called. */
152 ControlUserPaneDrawUPP gTPDrawProc = NULL;
153 ControlUserPaneHitTestUPP gTPHitProc = NULL;
154 ControlUserPaneTrackingUPP gTPTrackProc = NULL;
155 ControlUserPaneIdleUPP gTPIdleProc = NULL;
156 ControlUserPaneKeyDownUPP gTPKeyProc = NULL;
157 ControlUserPaneActivateUPP gTPActivateProc = NULL;
158 ControlUserPaneFocusUPP gTPFocusProc = NULL;
159
160 /* TPActivatePaneText activates or deactivates the text edit record
161 according to the value of setActive. The primary purpose of this
162 routine is to ensure each call is only made once. */
163 static void TPActivatePaneText(STPTextPaneVars **tpvars, Boolean setActive) {
164 STPTextPaneVars *varsp;
165 varsp = *tpvars;
166 if (varsp->fTEActive != setActive) {
167
168 varsp->fTEActive = setActive;
169
170 TXNActivate(varsp->fTXNRec, varsp->fTXNFrame, varsp->fTEActive);
171
172 if (varsp->fInFocus)
173 TXNFocus( varsp->fTXNRec, varsp->fTEActive);
174 }
175 }
176
177
178 /* TPFocusPaneText set the focus state for the text record. */
179 static void TPFocusPaneText(STPTextPaneVars **tpvars, Boolean setFocus) {
180 STPTextPaneVars *varsp;
181 varsp = *tpvars;
182 if (varsp->fInFocus != setFocus) {
183 varsp->fInFocus = setFocus;
184 TXNFocus( varsp->fTXNRec, varsp->fInFocus);
185 }
186 }
187
188
189 /* TPPaneDrawProc is called to redraw the control and for update events
190 referring to the control. This routine erases the text area's background,
191 and redraws the text. This routine assumes the scroll bar has been
192 redrawn by a call to DrawControls. */
193 static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart) {
194 STPTextPaneVars **tpvars, *varsp;
195 char state;
196 Rect bounds;
197 /* set up our globals */
198
199 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
200 if (tpvars != NULL) {
201 state = HGetState((Handle) tpvars);
202 HLock((Handle) tpvars);
203 varsp = *tpvars;
204
205 /* save the drawing state */
206 SetPort((**tpvars).fDrawingEnvironment);
207 /* verify our boundary */
208 GetControlBounds(theControl, &bounds);
209
210 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
211 if ( ! EqualRect(&bounds, &varsp->fRFocusOutline) ) {
212 // scrollbar is on the border, we add one
213 Rect oldbounds = varsp->fRFocusOutline ;
214 InsetRect( &oldbounds , -1 , -1 ) ;
215
216 InvalWindowRect( GetControlOwner( theControl ) , &oldbounds ) ;
217 SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
218 SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
219 SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
220 bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
221 RectRgn(varsp->fTextBackgroundRgn, &varsp->fRTextOutline);
222 TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left,
223 varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame);
224 }
225
226 /* update the text region */
227 RGBColor white = { 65535 , 65535 , 65535 } ;
228 RGBBackColor( &white ) ;
229 EraseRgn(varsp->fTextBackgroundRgn);
230 TXNDraw(varsp->fTXNRec, NULL);
231 /* restore the drawing environment */
232 /* draw the text frame and focus frame (if necessary) */
233 DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
234 if ((**tpvars).fIsActive && varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, true);
235 /* release our globals */
236 HSetState((Handle) tpvars, state);
237
238 }
239 }
240
241
242 /* TPPaneHitTestProc is called when the control manager would
243 like to determine what part of the control the mouse resides over.
244 We also call this routine from our tracking proc to determine how
245 to handle mouse clicks. */
246 static pascal ControlPartCode TPPaneHitTestProc(ControlHandle theControl, Point where) {
247 STPTextPaneVars **tpvars;
248 ControlPartCode result;
249 char state;
250 /* set up our locals and lock down our globals*/
251 result = 0;
252 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
253 if (tpvars != NULL) {
254 state = HGetState((Handle) tpvars);
255 HLock((Handle) tpvars);
256 /* find the region where we clicked */
257 if (PtInRect(where, &(**tpvars).fRTextArea)) {
258 result = kmUPTextPart;
259 } else result = 0;
260 /* release oure globals */
261 HSetState((Handle) tpvars, state);
262 }
263 return result;
264 }
265
266
267
268
269
270 /* TPPaneTrackingProc is called when the mouse is being held down
271 over our control. This routine handles clicks in the text area
272 and in the scroll bar. */
273 static pascal ControlPartCode TPPaneTrackingProc(ControlHandle theControl, Point startPt, ControlActionUPP actionProc) {
274 STPTextPaneVars **tpvars, *varsp;
275 char state;
276 ControlPartCode partCodeResult;
277 /* make sure we have some variables... */
278 partCodeResult = 0;
279 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
280 if (tpvars != NULL) {
281 /* lock 'em down */
282 state = HGetState((Handle) tpvars);
283 HLock((Handle) tpvars);
284 varsp = *tpvars;
285 /* we don't do any of these functions unless we're in focus */
286 if ( ! varsp->fInFocus) {
287 WindowPtr owner;
288 owner = GetControlOwner(theControl);
289 ClearKeyboardFocus(owner);
290 SetKeyboardFocus(owner, theControl, kUserClickedToFocusPart);
291 }
292 /* find the location for the click */
293 switch (TPPaneHitTestProc(theControl, startPt)) {
294
295 /* handle clicks in the text part */
296 case kmUPTextPart:
297 { SetPort((**tpvars).fDrawingEnvironment);
298 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
299 #if !TARGET_CARBON
300 TXNClick( varsp->fTXNRec, (const EventRecord*) wxTheApp->MacGetCurrentEvent());
301 #else
302 EventRecord rec ;
303 ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ;
304 TXNClick( varsp->fTXNRec, &rec );
305 #endif
306 }
307 break;
308
309 }
310
311 HSetState((Handle) tpvars, state);
312 }
313 return partCodeResult;
314 }
315
316
317 /* TPPaneIdleProc is our user pane idle routine. When our text field
318 is active and in focus, we use this routine to set the cursor. */
319 static pascal void TPPaneIdleProc(ControlHandle theControl) {
320 STPTextPaneVars **tpvars, *varsp;
321 /* set up locals */
322 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
323 if (tpvars != NULL) {
324 /* if we're not active, then we have nothing to say about the cursor */
325 if ((**tpvars).fIsActive) {
326 char state;
327 Rect bounds;
328 Point mousep;
329 /* lock down the globals */
330 state = HGetState((Handle) tpvars);
331 HLock((Handle) tpvars);
332 varsp = *tpvars;
333 /* get the current mouse coordinates (in our window) */
334 SetPortWindowPort(GetControlOwner(theControl));
335 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
336 GetMouse(&mousep);
337 /* there's a 'focus thing' and an 'unfocused thing' */
338 if (varsp->fInFocus) {
339 /* flash the cursor */
340 SetPort((**tpvars).fDrawingEnvironment);
341 TXNIdle(varsp->fTXNRec);
342 /* set the cursor */
343 if (PtInRect(mousep, &varsp->fRTextArea)) {
344 RgnHandle theRgn;
345 RectRgn((theRgn = NewRgn()), &varsp->fRTextArea);
346 TXNAdjustCursor(varsp->fTXNRec, theRgn);
347 DisposeRgn(theRgn);
348 }
349 else
350 {
351 // SetThemeCursor(kThemeArrowCursor);
352 }
353 } else {
354 /* if it's in our bounds, set the cursor */
355 GetControlBounds(theControl, &bounds);
356 if (PtInRect(mousep, &bounds))
357 {
358 // SetThemeCursor(kThemeArrowCursor);
359 }
360 }
361
362 HSetState((Handle) tpvars, state);
363 }
364 }
365 }
366
367
368 /* TPPaneKeyDownProc is called whenever a keydown event is directed
369 at our control. Here, we direct the keydown event to the text
370 edit record and redraw the scroll bar and text field as appropriate. */
371 static pascal ControlPartCode TPPaneKeyDownProc(ControlHandle theControl,
372 SInt16 keyCode, SInt16 charCode, SInt16 modifiers) {
373 STPTextPaneVars **tpvars;
374 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
375 if (tpvars != NULL) {
376 if ((**tpvars).fInFocus) {
377 /* turn autoscrolling on and send the key event to text edit */
378 SetPort((**tpvars).fDrawingEnvironment);
379 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
380 EventRecord ev ;
381 memset( &ev , 0 , sizeof( ev ) ) ;
382 ev.what = keyDown ;
383 ev.modifiers = modifiers ;
384 ev.message = (( keyCode << 8 ) & keyCodeMask ) + ( charCode & charCodeMask ) ;
385 TXNKeyDown( (**tpvars).fTXNRec, &ev);
386 }
387 }
388 return kControlEntireControl;
389 }
390
391
392 /* TPPaneActivateProc is called when the window containing
393 the user pane control receives activate events. Here, we redraw
394 the control and it's text as necessary for the activation state. */
395 static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activating) {
396 Rect bounds;
397 STPTextPaneVars **tpvars, *varsp;
398 char state;
399 /* set up locals */
400 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
401 if (tpvars != NULL) {
402 state = HGetState((Handle) tpvars);
403 HLock((Handle) tpvars);
404 varsp = *tpvars;
405 /* de/activate the text edit record */
406 SetPort((**tpvars).fDrawingEnvironment);
407 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
408 GetControlBounds(theControl, &bounds);
409 varsp->fIsActive = activating;
410 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
411 /* redraw the frame */
412 DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
413 if (varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive);
414 HSetState((Handle) tpvars, state);
415 }
416 }
417
418
419 /* TPPaneFocusProc is called when every the focus changes to or
420 from our control. Herein, switch the focus appropriately
421 according to the parameters and redraw the control as
422 necessary. */
423 static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlFocusPart action) {
424 ControlPartCode focusResult;
425 STPTextPaneVars **tpvars, *varsp;
426 char state;
427 /* set up locals */
428 focusResult = kControlFocusNoPart;
429 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
430 if (tpvars != NULL) {
431 state = HGetState((Handle) tpvars);
432 HLock((Handle) tpvars);
433 varsp = *tpvars;
434 /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
435 tabbing forwards (or shift tabbing backwards) through the items in the dialog,
436 and kControlFocusNextPart will be received. When the user clicks in our field
437 and it is not the current focus, then the constant kUserClickedToFocusPart will
438 be received. The constant kControlFocusNoPart will be received when our control
439 is the current focus and the user clicks in another control. In your focus routine,
440 you should respond to these codes as follows:
441
442 kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw
443 the control and the focus rectangle as necessary.
444
445 kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off
446 depending on its current state. redraw the control and the focus rectangle
447 as appropriate for the new focus state. If the focus state is 'off', return the constant
448 kControlFocusNoPart, otherwise return a non-zero part code.
449 kUserClickedToFocusPart - is a constant defined for this example. You should
450 define your own value for handling click-to-focus type events. */
451 /* save the drawing state */
452 SetPort((**tpvars).fDrawingEnvironment);
453 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
454 /* calculate the next highlight state */
455 switch (action) {
456 default:
457 case kControlFocusNoPart:
458 TPFocusPaneText(tpvars, false);
459 focusResult = kControlFocusNoPart;
460 break;
461 case kUserClickedToFocusPart:
462 TPFocusPaneText(tpvars, true);
463 focusResult = 1;
464 break;
465 case kControlFocusPrevPart:
466 case kControlFocusNextPart:
467 TPFocusPaneText(tpvars, ( ! varsp->fInFocus));
468 focusResult = varsp->fInFocus ? 1 : kControlFocusNoPart;
469 break;
470 }
471 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
472 /* redraw the text fram and focus rectangle to indicate the
473 new focus state */
474 DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
475 DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus);
476 /* done */
477 HSetState((Handle) tpvars, state);
478 }
479 return focusResult;
480 }
481
482
483 /* mUPOpenControl initializes a user pane control so it will be drawn
484 and will behave as a scrolling text edit field inside of a window.
485 This routine performs all of the initialization steps necessary,
486 except it does not create the user pane control itself. theControl
487 should refer to a user pane control that you have either created
488 yourself or extracted from a dialog's control heirarchy using
489 the GetDialogItemAsControl routine. */
490 OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle )
491 {
492 Rect bounds;
493 WindowRef theWindow;
494 STPTextPaneVars **tpvars, *varsp;
495 OSStatus err = noErr ;
496 RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF};
497 TXNBackground tback;
498
499 /* set up our globals */
500 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(TPPaneDrawProc);
501 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(TPPaneHitTestProc);
502 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(TPPaneTrackingProc);
503 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(TPPaneIdleProc);
504 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc);
505 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(TPPaneActivateProc);
506 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(TPPaneFocusProc);
507
508 /* allocate our private storage */
509 tpvars = (STPTextPaneVars **) NewHandleClear(sizeof(STPTextPaneVars));
510 SetControlReference(theControl, (long) tpvars);
511 HLock((Handle) tpvars);
512 varsp = *tpvars;
513 /* set the initial settings for our private data */
514 varsp->fMultiline = wxStyle & wxTE_MULTILINE ;
515 varsp->fInFocus = false;
516 varsp->fIsActive = true;
517 varsp->fTEActive = true; // in order to get a deactivate
518 varsp->fUserPaneRec = theControl;
519 theWindow = varsp->fOwner = GetControlOwner(theControl);
520
521 varsp->fDrawingEnvironment = (GrafPtr) GetWindowPort(theWindow);
522
523 varsp->fInDialogWindow = ( GetWindowKind(varsp->fOwner) == kDialogWindowKind );
524 /* set up the user pane procedures */
525 SetControlData(theControl, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
526 SetControlData(theControl, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
527 SetControlData(theControl, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
528 SetControlData(theControl, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
529 SetControlData(theControl, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
530 SetControlData(theControl, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
531 SetControlData(theControl, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
532 /* calculate the rectangles used by the control */
533 GetControlBounds(theControl, &bounds);
534 SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
535 SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
536 SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
537 bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
538 /* calculate the background region for the text. In this case, it's kindof
539 and irregular region because we're setting the scroll bar a little ways inside
540 of the text area. */
541 RectRgn((varsp->fTextBackgroundRgn = NewRgn()), &varsp->fRTextOutline);
542
543 /* set up the drawing environment */
544 SetPort(varsp->fDrawingEnvironment);
545
546 /* create the new edit field */
547
548 TXNFrameOptions frameOptions =
549 kTXNDontDrawCaretWhenInactiveMask ;
550 if ( ! ( wxStyle & wxTE_NOHIDESEL ) )
551 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
552
553 if ( wxStyle & wxTE_MULTILINE )
554 {
555 if ( ! ( wxStyle & wxTE_DONTWRAP ) )
556 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
557 else
558 {
559 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
560 frameOptions |= kTXNWantHScrollBarMask ;
561 }
562
563 if ( !(wxStyle & wxTE_NO_VSCROLL ) )
564 frameOptions |= kTXNWantVScrollBarMask ;
565 }
566 else
567 frameOptions |= kTXNSingleLineOnlyMask ;
568
569 if ( wxStyle & wxTE_READONLY )
570 frameOptions |= kTXNReadOnlyMask ;
571
572 TXNNewObject(NULL, varsp->fOwner, &varsp->fRTextArea,
573 frameOptions ,
574 kTXNTextEditStyleFrameType,
575 kTXNTextensionFile,
576 kTXNSystemDefaultEncoding,
577 &varsp->fTXNRec, &varsp->fTXNFrame, (TXNObjectRefcon) tpvars);
578
579 Str255 fontName ;
580 SInt16 fontSize ;
581 Style fontStyle ;
582
583 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
584
585 TXNTypeAttributes typeAttr[] =
586 {
587 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
588 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
589 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
590 } ;
591
592 err = TXNSetTypeAttributes (varsp->fTXNRec, sizeof( typeAttr ) / sizeof(TXNTypeAttributes) , typeAttr,
593 kTXNStartOffset,
594 kTXNEndOffset);
595 /* set the field's background */
596
597 tback.bgType = kTXNBackgroundTypeRGB;
598 tback.bg.color = rgbWhite;
599 TXNSetBackground( varsp->fTXNRec, &tback);
600
601 /* unlock our storage */
602 HUnlock((Handle) tpvars);
603 /* perform final activations and setup for our text field. Here,
604 we assume that the window is going to be the 'active' window. */
605 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
606 /* all done */
607 return err;
608 }
609
610
611
612
613 #if !USE_SHARED_LIBRARY
614 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
615
616 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
617 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
618 EVT_CHAR(wxTextCtrl::OnChar)
619 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
620 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
621 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
622 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
623 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
624
625 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
626 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
627 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
628 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
629 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
630 END_EVENT_TABLE()
631 #endif
632
633 // Text item
634 wxTextCtrl::wxTextCtrl()
635 {
636 m_macTE = NULL ;
637 m_macTXN = NULL ;
638 m_macTXNvars = NULL ;
639 m_macUsesTXN = false ;
640 m_editable = true ;
641 m_maxLength = TE_UNLIMITED_LENGTH ;
642 }
643
644 wxTextCtrl::~wxTextCtrl()
645 {
646 if ( m_macUsesTXN )
647 {
648 SetControlReference((ControlHandle)m_macControl, 0) ;
649 TXNDeleteObject((TXNObject)m_macTXN);
650 /* delete our private storage */
651 DisposeHandle((Handle) m_macTXNvars);
652 /* zero the control reference */
653 }
654 }
655
656 const short kVerticalMargin = 2 ;
657 const short kHorizontalMargin = 2 ;
658
659 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
660 const wxString& st,
661 const wxPoint& pos,
662 const wxSize& size, long style,
663 const wxValidator& validator,
664 const wxString& name)
665 {
666 m_macTE = NULL ;
667 m_macTXN = NULL ;
668 m_macTXNvars = NULL ;
669 m_macUsesTXN = false ;
670 m_editable = true ;
671
672 m_macUsesTXN = ! (style & wxTE_PASSWORD ) ;
673
674 m_macUsesTXN &= (TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress) ;
675
676 // base initialization
677 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
678 return FALSE;
679
680 wxSize mySize = size ;
681 if ( m_macUsesTXN )
682 {
683 m_macHorizontalBorder = 5 ; // additional pixels around the real control
684 m_macVerticalBorder = 3 ;
685 }
686 else
687 {
688 m_macHorizontalBorder = 5 ; // additional pixels around the real control
689 m_macVerticalBorder = 5 ;
690 }
691
692
693 Rect bounds ;
694 Str255 title ;
695 /*
696 if ( mySize.y == -1 )
697 {
698 mySize.y = 13 ;
699 if ( m_windowStyle & wxTE_MULTILINE )
700 mySize.y *= 5 ;
701
702 mySize.y += 2 * m_macVerticalBorder ;
703 }
704 */
705 MacPreControlCreate( parent , id , wxEmptyString , pos , mySize ,style, validator , name , &bounds , title ) ;
706
707 if ( m_windowStyle & wxTE_MULTILINE )
708 {
709 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
710 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
711
712 m_windowStyle |= wxTE_PROCESS_ENTER;
713 }
714
715 if ( m_windowStyle & wxTE_READONLY)
716 {
717 m_editable = FALSE ;
718 }
719
720 if ( !m_macUsesTXN )
721 {
722 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , "\p" , true , 0 , 0 , 1,
723 (style & wxTE_PASSWORD) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ;
724 long size ;
725 ::GetControlData((ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*)((TEHandle *)&m_macTE) , &size ) ;
726
727 }
728 else
729 {
730 short featurSet;
731
732 featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
733 | kControlWantsActivate | kControlHandlesTracking | kControlHasSpecialBackground
734 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
735 /* create the control */
736 m_macControl = NewControl(MAC_WXHWND(parent->MacGetRootWindow()), &bounds, "\p", true, featurSet, 0, featurSet, kControlUserPaneProc, 0);
737 /* set up the mUP specific features and data */
738 mUPOpenControl((ControlHandle) m_macControl, m_windowStyle );
739 }
740 MacPostControlCreate() ;
741
742 if ( !m_macUsesTXN )
743 {
744 wxCharBuffer text = wxMacStringToCString( st ) ;
745 ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ;
746 }
747 else
748 {
749 STPTextPaneVars **tpvars;
750 /* set up locals */
751 tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl);
752 /* set the text in the record */
753 m_macTXN = (**tpvars).fTXNRec ;
754 #if wxUSE_UNICODE
755 TXNSetData( ((TXNObject) m_macTXN) , kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2,
756 kTXNStartOffset, kTXNEndOffset);
757 #else
758 wxCharBuffer text = wxMacStringToCString( st ) ;
759 TXNSetData( ((TXNObject) m_macTXN) , kTXNTextData, (void*)text.data(), strlen( text ) ,
760 kTXNStartOffset, kTXNEndOffset);
761 #endif
762 m_macTXNvars = tpvars ;
763 m_macUsesTXN = true ;
764 TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
765 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
766 }
767
768 return TRUE;
769 }
770
771 wxString wxTextCtrl::GetValue() const
772 {
773 Size actualSize = 0;
774 wxString result ;
775 OSStatus err ;
776 if ( !m_macUsesTXN )
777 {
778 err = ::GetControlDataSize((ControlHandle) m_macControl, 0,
779 ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, &actualSize ) ;
780
781 if ( err )
782 return wxEmptyString ;
783
784 if ( actualSize > 0 )
785 {
786 wxCharBuffer buf(actualSize) ;
787 ::GetControlData( (ControlHandle) m_macControl, 0,
788 ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag,
789 actualSize , buf.data() , &actualSize ) ;
790 result = wxMacMakeStringFromCString( buf ) ;
791 }
792 }
793 else
794 {
795 #if wxUSE_UNICODE
796 Handle theText ;
797 err = TXNGetDataEncoded( ((TXNObject) m_macTXN), kTXNStartOffset, kTXNEndOffset, &theText , kTXNUnicodeTextData );
798 // all done
799 if ( err )
800 {
801 actualSize = 0 ;
802 }
803 else
804 {
805 actualSize = GetHandleSize( theText ) ;
806 if ( actualSize > 0 )
807 {
808 wxChar *ptr = result.GetWriteBuf(actualSize*sizeof(wxChar)) ;
809 wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ;
810 ptr[actualSize] = 0 ;
811 result.UngetWriteBuf( actualSize ) ;
812 }
813 DisposeHandle( theText ) ;
814 }
815 #else
816 Handle theText ;
817 err = TXNGetDataEncoded( ((TXNObject) m_macTXN), kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
818 // all done
819 if ( err )
820 {
821 actualSize = 0 ;
822 }
823 else
824 {
825 actualSize = GetHandleSize( theText ) ;
826 if ( actualSize > 0 )
827 {
828 HLock( theText ) ;
829 result = wxMacMakeStringFromCString( *theText , actualSize ) ;
830 HUnlock( theText ) ;
831 }
832 DisposeHandle( theText ) ;
833 }
834 #endif
835 }
836
837 return result ;
838 }
839
840 void wxTextCtrl::GetSelection(long* from, long* to) const
841 {
842 if ( !m_macUsesTXN )
843 {
844 *from = (**((TEHandle) m_macTE)).selStart;
845 *to = (**((TEHandle) m_macTE)).selEnd;
846 }
847 else
848 {
849 TXNGetSelection( ((TXNObject) m_macTXN) , (TXNOffset*) from , (TXNOffset*) to ) ;
850 }
851 }
852
853 void wxTextCtrl::SetValue(const wxString& st)
854 {
855 if ( !m_macUsesTXN )
856 {
857 wxCharBuffer text = wxMacStringToCString( st ) ;
858 ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ;
859 }
860 else
861 {
862 bool formerEditable = m_editable ;
863 if ( !formerEditable )
864 SetEditable(true) ;
865 #if wxUSE_UNICODE
866 TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2 ,
867 kTXNStartOffset, kTXNEndOffset);
868 #else
869 wxCharBuffer text = wxMacStringToCString( st ) ;
870 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)text.data(), strlen( text ) ,
871 kTXNStartOffset, kTXNEndOffset);
872 #endif
873 TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
874 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
875 if ( !formerEditable )
876 SetEditable(formerEditable) ;
877 }
878 MacRedrawControl() ;
879 }
880
881 void wxTextCtrl::SetMaxLength(unsigned long len)
882 {
883 m_maxLength = len ;
884 }
885
886 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
887 {
888 if ( m_macUsesTXN )
889 {
890 bool formerEditable = m_editable ;
891 if ( !formerEditable )
892 SetEditable(true) ;
893 TXNTypeAttributes typeAttr[4] ;
894 Str255 fontName = "\pMonaco" ;
895 SInt16 fontSize = 12 ;
896 Style fontStyle = normal ;
897 RGBColor color ;
898 int attrCounter = 0 ;
899 if ( style.HasFont() )
900 {
901 const wxFont &font = style.GetFont() ;
902 wxMacStringToPascal( font.GetFaceName() , fontName ) ;
903 fontSize = font.GetPointSize() ;
904 if ( font.GetUnderlined() )
905 fontStyle |= underline ;
906 if ( font.GetWeight() == wxBOLD )
907 fontStyle |= bold ;
908 if ( font.GetStyle() == wxITALIC )
909 fontStyle |= italic ;
910
911 typeAttr[attrCounter].tag = kTXNQDFontNameAttribute ;
912 typeAttr[attrCounter].size = kTXNQDFontNameAttributeSize ;
913 typeAttr[attrCounter].data.dataPtr = (void*) fontName ;
914 typeAttr[attrCounter+1].tag = kTXNQDFontSizeAttribute ;
915 typeAttr[attrCounter+1].size = kTXNFontSizeAttributeSize ;
916 typeAttr[attrCounter+1].data.dataValue = (fontSize << 16) ;
917 typeAttr[attrCounter+2].tag = kTXNQDFontStyleAttribute ;
918 typeAttr[attrCounter+2].size = kTXNQDFontStyleAttributeSize ;
919 typeAttr[attrCounter+2].data.dataValue = fontStyle ;
920 attrCounter += 3 ;
921
922 }
923 if ( style.HasTextColour() )
924 {
925 typeAttr[attrCounter].tag = kTXNQDFontColorAttribute ;
926 typeAttr[attrCounter].size = kTXNQDFontColorAttributeSize ;
927 typeAttr[attrCounter].data.dataPtr = (void*) &color ;
928 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
929 attrCounter += 1 ;
930 }
931
932 if ( attrCounter > 0 )
933 {
934 #ifdef __WXDEBUG__
935 OSStatus status =
936 #endif // __WXDEBUG__
937 TXNSetTypeAttributes ((TXNObject)m_macTXN, attrCounter , typeAttr, start,end);
938 wxASSERT_MSG( status == noErr , wxT("Couldn't set text attributes") ) ;
939 }
940 if ( !formerEditable )
941 SetEditable(formerEditable) ;
942 }
943 return TRUE ;
944 }
945
946 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
947 {
948 wxTextCtrlBase::SetDefaultStyle( style ) ;
949 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
950 return TRUE ;
951 }
952
953 // Clipboard operations
954 void wxTextCtrl::Copy()
955 {
956 if (CanCopy())
957 {
958 if ( !m_macUsesTXN )
959 {
960 TECopy( ((TEHandle) m_macTE) ) ;
961 ClearCurrentScrap();
962 TEToScrap() ;
963 MacRedrawControl() ;
964 }
965 else
966 {
967 ClearCurrentScrap();
968 TXNCopy((TXNObject)m_macTXN);
969 TXNConvertToPublicScrap();
970 }
971 }
972 }
973
974 void wxTextCtrl::Cut()
975 {
976 if (CanCut())
977 {
978 if ( !m_macUsesTXN )
979 {
980 TECut( ((TEHandle) m_macTE) ) ;
981 ClearCurrentScrap();
982 TEToScrap() ;
983 MacRedrawControl() ;
984 }
985 else
986 {
987 ClearCurrentScrap();
988 TXNCut((TXNObject)m_macTXN);
989 TXNConvertToPublicScrap();
990 }
991 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
992 event.SetString( GetValue() ) ;
993 event.SetEventObject( this );
994 GetEventHandler()->ProcessEvent(event);
995 }
996 }
997
998 void wxTextCtrl::Paste()
999 {
1000 if (CanPaste())
1001 {
1002 if ( !m_macUsesTXN )
1003 {
1004 TEFromScrap() ;
1005 TEPaste( (TEHandle) m_macTE ) ;
1006 MacRedrawControl() ;
1007 }
1008 else
1009 {
1010 TXNConvertFromPublicScrap();
1011 TXNPaste((TXNObject)m_macTXN);
1012 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
1013 }
1014 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1015 event.SetString( GetValue() ) ;
1016 event.SetEventObject( this );
1017 GetEventHandler()->ProcessEvent(event);
1018 }
1019 }
1020
1021 bool wxTextCtrl::CanCopy() const
1022 {
1023 // Can copy if there's a selection
1024 long from, to;
1025 GetSelection(& from, & to);
1026 return (from != to);
1027 }
1028
1029 bool wxTextCtrl::CanCut() const
1030 {
1031 if ( !IsEditable() )
1032 {
1033 return false ;
1034 }
1035 // Can cut if there's a selection
1036 long from, to;
1037 GetSelection(& from, & to);
1038 return (from != to);
1039 }
1040
1041 bool wxTextCtrl::CanPaste() const
1042 {
1043 if (!IsEditable())
1044 return FALSE;
1045
1046 #if TARGET_CARBON
1047 OSStatus err = noErr;
1048 ScrapRef scrapRef;
1049
1050 err = GetCurrentScrap( &scrapRef );
1051 if ( err != noTypeErr && err != memFullErr )
1052 {
1053 ScrapFlavorFlags flavorFlags;
1054 Size byteCount;
1055
1056 if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr)
1057 {
1058 if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr)
1059 {
1060 return TRUE ;
1061 }
1062 }
1063 }
1064 return FALSE;
1065
1066 #else
1067 long offset ;
1068 if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 )
1069 {
1070 return TRUE ;
1071 }
1072 #endif
1073 return FALSE ;
1074 }
1075
1076 void wxTextCtrl::SetEditable(bool editable)
1077 {
1078 if ( editable != m_editable )
1079 {
1080 m_editable = editable ;
1081 if ( !m_macUsesTXN )
1082 {
1083 if ( editable )
1084 UMAActivateControl( (ControlHandle) m_macControl ) ;
1085 else
1086 UMADeactivateControl((ControlHandle) m_macControl ) ;
1087 }
1088 else
1089 {
1090 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1091 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1092 TXNSetTXNObjectControls( (TXNObject) m_macTXN , false , sizeof(tag) / sizeof (TXNControlTag) , tag , data ) ;
1093 }
1094 }
1095 }
1096
1097 void wxTextCtrl::SetInsertionPoint(long pos)
1098 {
1099 SetSelection( pos , pos ) ;
1100 }
1101
1102 void wxTextCtrl::SetInsertionPointEnd()
1103 {
1104 long pos = GetLastPosition();
1105 SetInsertionPoint(pos);
1106 }
1107
1108 long wxTextCtrl::GetInsertionPoint() const
1109 {
1110 long begin,end ;
1111 GetSelection( &begin , &end ) ;
1112 return begin ;
1113 }
1114
1115 long wxTextCtrl::GetLastPosition() const
1116 {
1117 if ( !m_macUsesTXN )
1118 {
1119 return (**((TEHandle) m_macTE)).teLength ;
1120 }
1121 else
1122 {
1123 Handle theText ;
1124 long actualsize ;
1125 OSErr err = TXNGetDataEncoded( (TXNObject) m_macTXN, kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
1126 /* all done */
1127 if ( err )
1128 {
1129 actualsize = 0 ;
1130 }
1131 else
1132 {
1133 actualsize = GetHandleSize( theText ) ;
1134 DisposeHandle( theText ) ;
1135 }
1136 return actualsize ;
1137 }
1138 }
1139
1140 void wxTextCtrl::Replace(long from, long to, const wxString& value)
1141 {
1142 if ( !m_macUsesTXN )
1143 {
1144 ControlEditTextSelectionRec selection ;
1145
1146 selection.selStart = from ;
1147 selection.selEnd = to ;
1148 ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1149 TESetSelect( from , to , ((TEHandle) m_macTE) ) ;
1150 TEDelete( ((TEHandle) m_macTE) ) ;
1151 TEInsert( value , value.Length() , ((TEHandle) m_macTE) ) ;
1152 }
1153 else
1154 {
1155 bool formerEditable = m_editable ;
1156 if ( !formerEditable )
1157 SetEditable(true) ;
1158 TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ;
1159 TXNClear( ((TXNObject) m_macTXN) ) ;
1160 #if wxUSE_UNICODE
1161 TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)value.wc_str(), value.Length() * 2 ,
1162 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1163 #else
1164 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)value.c_str(), value.Length(),
1165 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1166 #endif
1167 if ( !formerEditable )
1168 SetEditable( formerEditable ) ;
1169 }
1170 Refresh() ;
1171 }
1172
1173 void wxTextCtrl::Remove(long from, long to)
1174 {
1175 if ( !m_macUsesTXN )
1176 {
1177 ControlEditTextSelectionRec selection ;
1178
1179 selection.selStart = from ;
1180 selection.selEnd = to ;
1181 ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1182 TEDelete( ((TEHandle) m_macTE) ) ;
1183 }
1184 else
1185 {
1186 bool formerEditable = m_editable ;
1187 if ( !formerEditable )
1188 SetEditable(true) ;
1189 TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ;
1190 TXNClear( ((TXNObject) m_macTXN) ) ;
1191 if ( !formerEditable )
1192 SetEditable( formerEditable ) ;
1193 }
1194 Refresh() ;
1195 }
1196
1197 void wxTextCtrl::SetSelection(long from, long to)
1198 {
1199 if ( from == -1 )
1200 from = 0;
1201
1202 if ( to == -1 )
1203 to = GetLastPosition();
1204
1205 if ( !m_macUsesTXN )
1206 {
1207 ControlEditTextSelectionRec selection ;
1208 selection.selStart = from ;
1209 selection.selEnd = to ;
1210
1211 TESetSelect( selection.selStart , selection.selEnd , ((TEHandle) m_macTE) ) ;
1212 ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1213 }
1214 else
1215 {
1216 STPTextPaneVars **tpvars;
1217 /* set up our locals */
1218 tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl);
1219 /* and our drawing environment as the operation
1220 may force a redraw in the text area. */
1221 SetPort((**tpvars).fDrawingEnvironment);
1222 /* change the selection */
1223 TXNSetSelection( (**tpvars).fTXNRec, from, to);
1224 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
1225 }
1226 }
1227
1228 bool wxTextCtrl::LoadFile(const wxString& file)
1229 {
1230 if ( wxTextCtrlBase::LoadFile(file) )
1231 {
1232 return TRUE;
1233 }
1234
1235 return FALSE;
1236 }
1237
1238 void wxTextCtrl::WriteText(const wxString& st)
1239 {
1240 if ( !m_macUsesTXN )
1241 {
1242 wxCharBuffer text = wxMacStringToCString( st ) ;
1243 TEInsert( text , strlen(text) , ((TEHandle) m_macTE) ) ;
1244 }
1245 else
1246 {
1247 bool formerEditable = m_editable ;
1248 if ( !formerEditable )
1249 SetEditable(true) ;
1250 long start , end , dummy ;
1251 GetSelection( &start , &dummy ) ;
1252 #if wxUSE_UNICODE
1253 TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2 ,
1254 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1255 #else
1256 wxCharBuffer text = wxMacStringToCString( st ) ;
1257 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)text.data(), strlen( text ) ,
1258 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1259 #endif
1260 GetSelection( &dummy , &end ) ;
1261 SetStyle( start , end , GetDefaultStyle() ) ;
1262 if ( !formerEditable )
1263 SetEditable( formerEditable ) ;
1264 }
1265 MacRedrawControl() ;
1266 }
1267
1268 void wxTextCtrl::AppendText(const wxString& text)
1269 {
1270 SetInsertionPointEnd();
1271 WriteText(text);
1272 }
1273
1274 void wxTextCtrl::Clear()
1275 {
1276 if ( !IsEditable() )
1277 {
1278 return ;
1279 }
1280 if ( !m_macUsesTXN )
1281 {
1282 ::SetControlData((ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ;
1283 }
1284 else
1285 {
1286 TXNSetSelection( (TXNObject)m_macTXN , kTXNStartOffset , kTXNEndOffset ) ;
1287 TXNClear((TXNObject)m_macTXN);
1288 }
1289 Refresh() ;
1290 }
1291
1292 bool wxTextCtrl::IsModified() const
1293 {
1294 return TRUE;
1295 }
1296
1297 bool wxTextCtrl::IsEditable() const
1298 {
1299 return IsEnabled() && m_editable ;
1300 }
1301
1302 bool wxTextCtrl::AcceptsFocus() const
1303 {
1304 // we don't want focus if we can't be edited
1305 return /*IsEditable() && */ wxControl::AcceptsFocus();
1306 }
1307
1308 wxSize wxTextCtrl::DoGetBestSize() const
1309 {
1310 int wText = 100 ;
1311
1312 int hText;
1313 if ( m_macUsesTXN )
1314 {
1315 hText = 17 ;
1316 }
1317 else
1318 {
1319 hText = 13 ;
1320 }
1321 /*
1322 int cx, cy;
1323 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1324
1325 int wText = DEFAULT_ITEM_WIDTH;
1326
1327 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1328
1329 return wxSize(wText, hText);
1330 */
1331 if ( m_windowStyle & wxTE_MULTILINE )
1332 {
1333 hText *= 5 ;
1334 }
1335 hText += 2 * m_macVerticalBorder ;
1336 wText += 2 * m_macHorizontalBorder ;
1337 //else: for single line control everything is ok
1338 return wxSize(wText, hText);
1339 }
1340
1341 // ----------------------------------------------------------------------------
1342 // Undo/redo
1343 // ----------------------------------------------------------------------------
1344
1345 void wxTextCtrl::Undo()
1346 {
1347 if (CanUndo())
1348 {
1349 if ( m_macUsesTXN )
1350 {
1351 TXNUndo((TXNObject)m_macTXN);
1352 }
1353 }
1354 }
1355
1356 void wxTextCtrl::Redo()
1357 {
1358 if (CanRedo())
1359 {
1360 if ( m_macUsesTXN )
1361 {
1362 TXNRedo((TXNObject)m_macTXN);
1363 }
1364 }
1365 }
1366
1367 bool wxTextCtrl::CanUndo() const
1368 {
1369 if ( !IsEditable() )
1370 {
1371 return false ;
1372 }
1373 if ( m_macUsesTXN )
1374 {
1375 return TXNCanUndo((TXNObject)m_macTXN,NULL);
1376 }
1377 return FALSE ;
1378 }
1379
1380 bool wxTextCtrl::CanRedo() const
1381 {
1382 if ( !IsEditable() )
1383 {
1384 return false ;
1385 }
1386 if ( m_macUsesTXN )
1387 {
1388 return TXNCanRedo((TXNObject)m_macTXN,NULL);
1389 }
1390 return FALSE ;
1391 }
1392
1393 // Makes 'unmodified'
1394 void wxTextCtrl::DiscardEdits()
1395 {
1396 // TODO
1397 }
1398
1399 int wxTextCtrl::GetNumberOfLines() const
1400 {
1401 // TODO change this if possible to reflect real lines
1402 wxString content = GetValue() ;
1403
1404 int count = 1;
1405 for (size_t i = 0; i < content.Length() ; i++)
1406 {
1407 if (content[i] == '\r') count++;
1408 }
1409
1410 return count;
1411 }
1412
1413 long wxTextCtrl::XYToPosition(long x, long y) const
1414 {
1415 // TODO
1416 return 0;
1417 }
1418
1419 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
1420 {
1421 return FALSE ;
1422 }
1423
1424 void wxTextCtrl::ShowPosition(long pos)
1425 {
1426 // TODO
1427 }
1428
1429 int wxTextCtrl::GetLineLength(long lineNo) const
1430 {
1431 // TODO change this if possible to reflect real lines
1432 wxString content = GetValue() ;
1433
1434 // Find line first
1435 int count = 0;
1436 for (size_t i = 0; i < content.Length() ; i++)
1437 {
1438 if (count == lineNo)
1439 {
1440 // Count chars in line then
1441 count = 0;
1442 for (size_t j = i; j < content.Length(); j++)
1443 {
1444 count++;
1445 if (content[j] == '\r') return count;
1446 }
1447
1448 return count;
1449 }
1450 if (content[i] == '\r') count++;
1451 }
1452 return 0;
1453 }
1454
1455 wxString wxTextCtrl::GetLineText(long lineNo) const
1456 {
1457 // TODO change this if possible to reflect real lines
1458 wxString content = GetValue() ;
1459
1460 // Find line first
1461 int count = 0;
1462 for (size_t i = 0; i < content.Length() ; i++)
1463 {
1464 if (count == lineNo)
1465 {
1466 // Add chars in line then
1467 wxString tmp;
1468
1469 for (size_t j = i; j < content.Length(); j++)
1470 {
1471 if (content[j] == '\r')
1472 return tmp;
1473
1474 tmp += content[j];
1475 }
1476
1477 return tmp;
1478 }
1479 if (content[i] == '\r') count++;
1480 }
1481 return wxEmptyString ;
1482 }
1483
1484 /*
1485 * Text item
1486 */
1487
1488 void wxTextCtrl::Command(wxCommandEvent & event)
1489 {
1490 SetValue (event.GetString());
1491 ProcessCommand (event);
1492 }
1493
1494 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1495 {
1496 // By default, load the first file into the text window.
1497 if (event.GetNumberOfFiles() > 0)
1498 {
1499 LoadFile(event.GetFiles()[0]);
1500 }
1501 }
1502
1503 void wxTextCtrl::OnChar(wxKeyEvent& event)
1504 {
1505 int key = event.GetKeyCode() ;
1506 bool eat_key = false ;
1507
1508 if ( key == 'c' && event.MetaDown() )
1509 {
1510 if ( CanCopy() )
1511 Copy() ;
1512 return ;
1513 }
1514
1515 if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
1516 !( key == WXK_RETURN && ( (m_windowStyle & wxPROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
1517 /* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */
1518 )
1519 {
1520 // eat it
1521 return ;
1522 }
1523 if ( key == 'v' && event.MetaDown() )
1524 {
1525 if ( CanPaste() )
1526 Paste() ;
1527 return ;
1528 }
1529 if ( key == 'x' && event.MetaDown() )
1530 {
1531 if ( CanCut() )
1532 Cut() ;
1533 return ;
1534 }
1535 switch ( key )
1536 {
1537 case WXK_RETURN:
1538 if (m_windowStyle & wxPROCESS_ENTER)
1539 {
1540 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1541 event.SetEventObject( this );
1542 event.SetString( GetValue() );
1543 if ( GetEventHandler()->ProcessEvent(event) )
1544 return;
1545 }
1546 if ( !(m_windowStyle & wxTE_MULTILINE) )
1547 {
1548 wxWindow *parent = GetParent();
1549 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
1550 parent = parent->GetParent() ;
1551 }
1552 if ( parent && parent->GetDefaultItem() )
1553 {
1554 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1555 wxButton);
1556 if ( def && def->IsEnabled() )
1557 {
1558 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1559 event.SetEventObject(def);
1560 def->Command(event);
1561 return ;
1562 }
1563 }
1564
1565 // this will make wxWindows eat the ENTER key so that
1566 // we actually prevent line wrapping in a single line
1567 // text control
1568 eat_key = TRUE;
1569 }
1570
1571 break;
1572
1573 case WXK_TAB:
1574 // always produce navigation event - even if we process TAB
1575 // ourselves the fact that we got here means that the user code
1576 // decided to skip processing of this TAB - probably to let it
1577 // do its default job.
1578 {
1579 wxNavigationKeyEvent eventNav;
1580 eventNav.SetDirection(!event.ShiftDown());
1581 eventNav.SetWindowChange(event.ControlDown());
1582 eventNav.SetEventObject(this);
1583
1584 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
1585 return;
1586
1587 event.Skip() ;
1588 return;
1589 }
1590 break;
1591 }
1592
1593 if (!eat_key)
1594 {
1595 // default handling
1596 event.Skip() ;
1597 }
1598 if ( ( key >= 0x20 && key < WXK_START ) ||
1599 key == WXK_RETURN ||
1600 key == WXK_DELETE ||
1601 key == WXK_BACK)
1602 {
1603 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1604 event1.SetString( GetValue() ) ;
1605 event1.SetEventObject( this );
1606 wxPostEvent(GetEventHandler(),event1);
1607 }
1608 }
1609
1610 void wxTextCtrl::MacSuperShown( bool show )
1611 {
1612 bool former = m_macControlIsShown ;
1613 wxControl::MacSuperShown( show ) ;
1614 if ( (former != m_macControlIsShown) && m_macUsesTXN )
1615 {
1616 if ( m_macControlIsShown )
1617 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1618 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1619 else
1620 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1621 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1622 }
1623 }
1624
1625 bool wxTextCtrl::Show(bool show)
1626 {
1627 bool former = m_macControlIsShown ;
1628
1629 bool retval = wxControl::Show( show ) ;
1630
1631 if ( former != m_macControlIsShown && m_macUsesTXN )
1632 {
1633 if ( m_macControlIsShown )
1634 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1635 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1636 else
1637 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1638 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1639 }
1640
1641 return retval ;
1642 }
1643
1644 // ----------------------------------------------------------------------------
1645 // standard handlers for standard edit menu events
1646 // ----------------------------------------------------------------------------
1647
1648 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1649 {
1650 Cut();
1651 }
1652
1653 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1654 {
1655 Copy();
1656 }
1657
1658 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1659 {
1660 Paste();
1661 }
1662
1663 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1664 {
1665 Undo();
1666 }
1667
1668 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1669 {
1670 Redo();
1671 }
1672
1673 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1674 {
1675 event.Enable( CanCut() );
1676 }
1677
1678 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1679 {
1680 event.Enable( CanCopy() );
1681 }
1682
1683 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1684 {
1685 event.Enable( CanPaste() );
1686 }
1687
1688 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1689 {
1690 event.Enable( CanUndo() );
1691 }
1692
1693 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1694 {
1695 event.Enable( CanRedo() );
1696 }
1697
1698 bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
1699 {
1700 if ( m_macUsesTXN )
1701 return true ;
1702 else
1703 return wxWindow::MacSetupCursor( pt ) ;
1704 }
1705
1706 #endif
1707 // wxUSE_TEXTCTRL