]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/textctrl.cpp
updated source to not use deprecated wxList methods
[wxWidgets.git] / src / mac / carbon / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
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 TXNClick( varsp->fTXNRec, (const EventRecord*) wxTheApp->MacGetCurrentEvent());
300 }
301 break;
302
303 }
304
305 HSetState((Handle) tpvars, state);
306 }
307 return partCodeResult;
308 }
309
310
311 /* TPPaneIdleProc is our user pane idle routine. When our text field
312 is active and in focus, we use this routine to set the cursor. */
313 static pascal void TPPaneIdleProc(ControlHandle theControl) {
314 STPTextPaneVars **tpvars, *varsp;
315 /* set up locals */
316 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
317 if (tpvars != NULL) {
318 /* if we're not active, then we have nothing to say about the cursor */
319 if ((**tpvars).fIsActive) {
320 char state;
321 Rect bounds;
322 Point mousep;
323 /* lock down the globals */
324 state = HGetState((Handle) tpvars);
325 HLock((Handle) tpvars);
326 varsp = *tpvars;
327 /* get the current mouse coordinates (in our window) */
328 SetPortWindowPort(GetControlOwner(theControl));
329 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
330 GetMouse(&mousep);
331 /* there's a 'focus thing' and an 'unfocused thing' */
332 if (varsp->fInFocus) {
333 /* flash the cursor */
334 SetPort((**tpvars).fDrawingEnvironment);
335 TXNIdle(varsp->fTXNRec);
336 /* set the cursor */
337 if (PtInRect(mousep, &varsp->fRTextArea)) {
338 RgnHandle theRgn;
339 RectRgn((theRgn = NewRgn()), &varsp->fRTextArea);
340 TXNAdjustCursor(varsp->fTXNRec, theRgn);
341 DisposeRgn(theRgn);
342 }
343 else
344 {
345 // SetThemeCursor(kThemeArrowCursor);
346 }
347 } else {
348 /* if it's in our bounds, set the cursor */
349 GetControlBounds(theControl, &bounds);
350 if (PtInRect(mousep, &bounds))
351 {
352 // SetThemeCursor(kThemeArrowCursor);
353 }
354 }
355
356 HSetState((Handle) tpvars, state);
357 }
358 }
359 }
360
361
362 /* TPPaneKeyDownProc is called whenever a keydown event is directed
363 at our control. Here, we direct the keydown event to the text
364 edit record and redraw the scroll bar and text field as appropriate. */
365 static pascal ControlPartCode TPPaneKeyDownProc(ControlHandle theControl,
366 SInt16 keyCode, SInt16 charCode, SInt16 modifiers) {
367 STPTextPaneVars **tpvars;
368 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
369 if (tpvars != NULL) {
370 if ((**tpvars).fInFocus) {
371 /* turn autoscrolling on and send the key event to text edit */
372 SetPort((**tpvars).fDrawingEnvironment);
373 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
374 EventRecord ev ;
375 memset( &ev , 0 , sizeof( ev ) ) ;
376 ev.what = keyDown ;
377 ev.modifiers = modifiers ;
378 ev.message = (( keyCode << 8 ) & keyCodeMask ) + ( charCode & charCodeMask ) ;
379 TXNKeyDown( (**tpvars).fTXNRec, &ev);
380 }
381 }
382 return kControlEntireControl;
383 }
384
385
386 /* TPPaneActivateProc is called when the window containing
387 the user pane control receives activate events. Here, we redraw
388 the control and it's text as necessary for the activation state. */
389 static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activating) {
390 Rect bounds;
391 STPTextPaneVars **tpvars, *varsp;
392 char state;
393 /* set up locals */
394 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
395 if (tpvars != NULL) {
396 state = HGetState((Handle) tpvars);
397 HLock((Handle) tpvars);
398 varsp = *tpvars;
399 /* de/activate the text edit record */
400 SetPort((**tpvars).fDrawingEnvironment);
401 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
402 GetControlBounds(theControl, &bounds);
403 varsp->fIsActive = activating;
404 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
405 /* redraw the frame */
406 DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
407 if (varsp->fInFocus) DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive);
408 HSetState((Handle) tpvars, state);
409 }
410 }
411
412
413 /* TPPaneFocusProc is called when every the focus changes to or
414 from our control. Herein, switch the focus appropriately
415 according to the parameters and redraw the control as
416 necessary. */
417 static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlFocusPart action) {
418 ControlPartCode focusResult;
419 STPTextPaneVars **tpvars, *varsp;
420 char state;
421 /* set up locals */
422 focusResult = kControlFocusNoPart;
423 tpvars = (STPTextPaneVars **) GetControlReference(theControl);
424 if (tpvars != NULL) {
425 state = HGetState((Handle) tpvars);
426 HLock((Handle) tpvars);
427 varsp = *tpvars;
428 /* if kControlFocusPrevPart and kControlFocusNextPart are received when the user is
429 tabbing forwards (or shift tabbing backwards) through the items in the dialog,
430 and kControlFocusNextPart will be received. When the user clicks in our field
431 and it is not the current focus, then the constant kUserClickedToFocusPart will
432 be received. The constant kControlFocusNoPart will be received when our control
433 is the current focus and the user clicks in another control. In your focus routine,
434 you should respond to these codes as follows:
435
436 kControlFocusNoPart - turn off focus and return kControlFocusNoPart. redraw
437 the control and the focus rectangle as necessary.
438
439 kControlFocusPrevPart or kControlFocusNextPart - toggle focus on or off
440 depending on its current state. redraw the control and the focus rectangle
441 as appropriate for the new focus state. If the focus state is 'off', return the constant
442 kControlFocusNoPart, otherwise return a non-zero part code.
443 kUserClickedToFocusPart - is a constant defined for this example. You should
444 define your own value for handling click-to-focus type events. */
445 /* save the drawing state */
446 SetPort((**tpvars).fDrawingEnvironment);
447 wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ;
448 /* calculate the next highlight state */
449 switch (action) {
450 default:
451 case kControlFocusNoPart:
452 TPFocusPaneText(tpvars, false);
453 focusResult = kControlFocusNoPart;
454 break;
455 case kUserClickedToFocusPart:
456 TPFocusPaneText(tpvars, true);
457 focusResult = 1;
458 break;
459 case kControlFocusPrevPart:
460 case kControlFocusNextPart:
461 TPFocusPaneText(tpvars, ( ! varsp->fInFocus));
462 focusResult = varsp->fInFocus ? 1 : kControlFocusNoPart;
463 break;
464 }
465 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
466 /* redraw the text fram and focus rectangle to indicate the
467 new focus state */
468 DrawThemeEditTextFrame(&varsp->fRTextOutline, varsp->fIsActive ? kThemeStateActive: kThemeStateInactive);
469 DrawThemeFocusRect(&varsp->fRFocusOutline, varsp->fIsActive && varsp->fInFocus);
470 /* done */
471 HSetState((Handle) tpvars, state);
472 }
473 return focusResult;
474 }
475
476
477 /* mUPOpenControl initializes a user pane control so it will be drawn
478 and will behave as a scrolling text edit field inside of a window.
479 This routine performs all of the initialization steps necessary,
480 except it does not create the user pane control itself. theControl
481 should refer to a user pane control that you have either created
482 yourself or extracted from a dialog's control heirarchy using
483 the GetDialogItemAsControl routine. */
484 OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle )
485 {
486 Rect bounds;
487 WindowRef theWindow;
488 STPTextPaneVars **tpvars, *varsp;
489 OSStatus err = noErr ;
490 RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF};
491 TXNBackground tback;
492
493 /* set up our globals */
494 if (gTPDrawProc == NULL) gTPDrawProc = NewControlUserPaneDrawUPP(TPPaneDrawProc);
495 if (gTPHitProc == NULL) gTPHitProc = NewControlUserPaneHitTestUPP(TPPaneHitTestProc);
496 if (gTPTrackProc == NULL) gTPTrackProc = NewControlUserPaneTrackingUPP(TPPaneTrackingProc);
497 if (gTPIdleProc == NULL) gTPIdleProc = NewControlUserPaneIdleUPP(TPPaneIdleProc);
498 if (gTPKeyProc == NULL) gTPKeyProc = NewControlUserPaneKeyDownUPP(TPPaneKeyDownProc);
499 if (gTPActivateProc == NULL) gTPActivateProc = NewControlUserPaneActivateUPP(TPPaneActivateProc);
500 if (gTPFocusProc == NULL) gTPFocusProc = NewControlUserPaneFocusUPP(TPPaneFocusProc);
501
502 /* allocate our private storage */
503 tpvars = (STPTextPaneVars **) NewHandleClear(sizeof(STPTextPaneVars));
504 SetControlReference(theControl, (long) tpvars);
505 HLock((Handle) tpvars);
506 varsp = *tpvars;
507 /* set the initial settings for our private data */
508 varsp->fMultiline = wxStyle & wxTE_MULTILINE ;
509 varsp->fInFocus = false;
510 varsp->fIsActive = true;
511 varsp->fTEActive = true; // in order to get a deactivate
512 varsp->fUserPaneRec = theControl;
513 theWindow = varsp->fOwner = GetControlOwner(theControl);
514
515 varsp->fDrawingEnvironment = (GrafPtr) GetWindowPort(theWindow);
516
517 varsp->fInDialogWindow = ( GetWindowKind(varsp->fOwner) == kDialogWindowKind );
518 /* set up the user pane procedures */
519 SetControlData(theControl, kControlEntireControl, kControlUserPaneDrawProcTag, sizeof(gTPDrawProc), &gTPDrawProc);
520 SetControlData(theControl, kControlEntireControl, kControlUserPaneHitTestProcTag, sizeof(gTPHitProc), &gTPHitProc);
521 SetControlData(theControl, kControlEntireControl, kControlUserPaneTrackingProcTag, sizeof(gTPTrackProc), &gTPTrackProc);
522 SetControlData(theControl, kControlEntireControl, kControlUserPaneIdleProcTag, sizeof(gTPIdleProc), &gTPIdleProc);
523 SetControlData(theControl, kControlEntireControl, kControlUserPaneKeyDownProcTag, sizeof(gTPKeyProc), &gTPKeyProc);
524 SetControlData(theControl, kControlEntireControl, kControlUserPaneActivateProcTag, sizeof(gTPActivateProc), &gTPActivateProc);
525 SetControlData(theControl, kControlEntireControl, kControlUserPaneFocusProcTag, sizeof(gTPFocusProc), &gTPFocusProc);
526 /* calculate the rectangles used by the control */
527 GetControlBounds(theControl, &bounds);
528 SetRect(&varsp->fRFocusOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
529 SetRect(&varsp->fRTextOutline, bounds.left, bounds.top, bounds.right, bounds.bottom);
530 SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) ,
531 bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2));
532 /* calculate the background region for the text. In this case, it's kindof
533 and irregular region because we're setting the scroll bar a little ways inside
534 of the text area. */
535 RectRgn((varsp->fTextBackgroundRgn = NewRgn()), &varsp->fRTextOutline);
536
537 /* set up the drawing environment */
538 SetPort(varsp->fDrawingEnvironment);
539
540 /* create the new edit field */
541
542 TXNFrameOptions frameOptions =
543 kTXNDontDrawCaretWhenInactiveMask ;
544 if ( ! ( wxStyle & wxTE_NOHIDESEL ) )
545 frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ;
546
547 if ( wxStyle & wxTE_MULTILINE )
548 {
549 if ( ! ( wxStyle & wxTE_DONTWRAP ) )
550 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
551 else
552 {
553 frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ;
554 frameOptions |= kTXNWantHScrollBarMask ;
555 }
556
557 if ( !(wxStyle & wxTE_NO_VSCROLL ) )
558 frameOptions |= kTXNWantVScrollBarMask ;
559 }
560 else
561 frameOptions |= kTXNSingleLineOnlyMask ;
562
563 if ( wxStyle & wxTE_READONLY )
564 frameOptions |= kTXNReadOnlyMask ;
565
566 TXNNewObject(NULL, varsp->fOwner, &varsp->fRTextArea,
567 frameOptions ,
568 kTXNTextEditStyleFrameType,
569 kTXNTextensionFile,
570 kTXNSystemDefaultEncoding,
571 &varsp->fTXNRec, &varsp->fTXNFrame, (TXNObjectRefcon) tpvars);
572
573 Str255 fontName ;
574 SInt16 fontSize ;
575 Style fontStyle ;
576
577 GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
578
579 TXNTypeAttributes typeAttr[] =
580 {
581 { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } ,
582 { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } ,
583 { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } ,
584 } ;
585
586 err = TXNSetTypeAttributes (varsp->fTXNRec, sizeof( typeAttr ) / sizeof(TXNTypeAttributes) , typeAttr,
587 kTXNStartOffset,
588 kTXNEndOffset);
589 /* set the field's background */
590 tback.bgType = kTXNBackgroundTypeRGB;
591 tback.bg.color = rgbWhite;
592 TXNSetBackground( varsp->fTXNRec, &tback);
593
594 /* unlock our storage */
595 HUnlock((Handle) tpvars);
596 /* perform final activations and setup for our text field. Here,
597 we assume that the window is going to be the 'active' window. */
598 TPActivatePaneText(tpvars, varsp->fIsActive && varsp->fInFocus);
599 /* all done */
600 return err;
601 }
602
603
604
605
606 #if !USE_SHARED_LIBRARY
607 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
608
609 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
610 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
611 EVT_CHAR(wxTextCtrl::OnChar)
612 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
613 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
614 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
615 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
616 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
617
618 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
619 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
620 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
621 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
622 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
623 END_EVENT_TABLE()
624 #endif
625
626 // Text item
627 wxTextCtrl::wxTextCtrl()
628 {
629 m_macTE = NULL ;
630 m_macTXN = NULL ;
631 m_macTXNvars = NULL ;
632 m_macUsesTXN = false ;
633 m_editable = true ;
634 m_maxLength = TE_UNLIMITED_LENGTH ;
635 }
636
637 wxTextCtrl::~wxTextCtrl()
638 {
639 if ( m_macUsesTXN )
640 {
641 SetControlReference((ControlHandle)m_macControl, 0) ;
642 TXNDeleteObject((TXNObject)m_macTXN);
643 /* delete our private storage */
644 DisposeHandle((Handle) m_macTXNvars);
645 /* zero the control reference */
646 }
647 }
648
649 const short kVerticalMargin = 2 ;
650 const short kHorizontalMargin = 2 ;
651
652 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
653 const wxString& st,
654 const wxPoint& pos,
655 const wxSize& size, long style,
656 const wxValidator& validator,
657 const wxString& name)
658 {
659 m_macTE = NULL ;
660 m_macTXN = NULL ;
661 m_macTXNvars = NULL ;
662 m_macUsesTXN = false ;
663 m_editable = true ;
664
665 m_macUsesTXN = ! (style & wxTE_PASSWORD ) ;
666
667 m_macUsesTXN &= (TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress) ;
668
669 // base initialization
670 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
671 return FALSE;
672
673 wxSize mySize = size ;
674 if ( m_macUsesTXN )
675 {
676 m_macHorizontalBorder = 5 ; // additional pixels around the real control
677 m_macVerticalBorder = 3 ;
678 }
679 else
680 {
681 m_macHorizontalBorder = 5 ; // additional pixels around the real control
682 m_macVerticalBorder = 5 ;
683 }
684
685
686 Rect bounds ;
687 Str255 title ;
688 /*
689 if ( mySize.y == -1 )
690 {
691 mySize.y = 13 ;
692 if ( m_windowStyle & wxTE_MULTILINE )
693 mySize.y *= 5 ;
694
695 mySize.y += 2 * m_macVerticalBorder ;
696 }
697 */
698 MacPreControlCreate( parent , id , "" , pos , mySize ,style, validator , name , &bounds , title ) ;
699
700 if ( m_windowStyle & wxTE_MULTILINE )
701 {
702 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
703 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
704
705 m_windowStyle |= wxTE_PROCESS_ENTER;
706 }
707
708 if ( m_windowStyle & wxTE_READONLY)
709 {
710 m_editable = FALSE ;
711 }
712
713 if ( !m_macUsesTXN )
714 {
715 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , "\p" , true , 0 , 0 , 1,
716 (style & wxTE_PASSWORD) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ;
717 long size ;
718 ::GetControlData((ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &((TEHandle) m_macTE) , &size ) ;
719
720 }
721 else
722 {
723 short featurSet;
724
725 featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle
726 | kControlWantsActivate | kControlHandlesTracking | kControlHasSpecialBackground
727 | kControlGetsFocusOnClick | kControlSupportsLiveFeedback;
728 /* create the control */
729 m_macControl = NewControl(MAC_WXHWND(parent->MacGetRootWindow()), &bounds, "\p", true, featurSet, 0, featurSet, kControlUserPaneProc, 0);
730 /* set up the mUP specific features and data */
731 mUPOpenControl((ControlHandle) m_macControl, m_windowStyle );
732 if ( parent )
733 {
734 parent->MacGetTopLevelWindow()->MacInstallEventHandler() ;
735 }
736 }
737 MacPostControlCreate() ;
738
739 wxString value ;
740
741 if( wxApp::s_macDefaultEncodingIsPC )
742 value = wxMacMakeMacStringFromPC( st ) ;
743 else
744 value = st ;
745
746 if ( !m_macUsesTXN )
747 {
748 ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ;
749 }
750 else
751 {
752 STPTextPaneVars **tpvars;
753 /* set up locals */
754 tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl);
755 /* set the text in the record */
756 TXNSetData( (**tpvars).fTXNRec, kTXNTextData, (void*)value.c_str(), value.Length(),
757 kTXNStartOffset, kTXNEndOffset);
758 m_macTXN = (**tpvars).fTXNRec ;
759 m_macTXNvars = tpvars ;
760 m_macUsesTXN = true ;
761 TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
762 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
763 }
764
765 return TRUE;
766 }
767
768 wxString wxTextCtrl::GetValue() const
769 {
770 Size actualsize;
771
772 if ( !m_macUsesTXN )
773 {
774 ::GetControlData( (ControlHandle) m_macControl, 0,
775 ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag,
776 32767 , wxBuffer , &actualsize ) ;
777 }
778 else
779 {
780 Handle theText ;
781 OSStatus err = TXNGetDataEncoded( ((TXNObject) m_macTXN), kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
782 // all done
783 if ( err )
784 {
785 actualsize = 0 ;
786 }
787 else
788 {
789 actualsize = GetHandleSize( theText ) ;
790 if (actualsize != 0)
791 strncpy( wxBuffer , *theText , actualsize ) ;
792 DisposeHandle( theText ) ;
793 }
794 }
795
796 wxBuffer[actualsize] = 0 ;
797
798 wxString value;
799
800 if( wxApp::s_macDefaultEncodingIsPC )
801 {
802 value = wxMacMakePCStringFromMac( wxBuffer ) ;
803 value.Replace( "\r", "\n" );
804 }
805 else
806 value = wxBuffer;
807
808
809 return value;
810 }
811
812 void wxTextCtrl::GetSelection(long* from, long* to) const
813 {
814 if ( !m_macUsesTXN )
815 {
816 *from = (**((TEHandle) m_macTE)).selStart;
817 *to = (**((TEHandle) m_macTE)).selEnd;
818 }
819 else
820 {
821 TXNGetSelection( ((TXNObject) m_macTXN) , (TXNOffset*) from , (TXNOffset*) to ) ;
822 }
823 }
824
825 void wxTextCtrl::SetValue(const wxString& st)
826 {
827 wxString value;
828
829 if( wxApp::s_macDefaultEncodingIsPC )
830 {
831 value = wxMacMakeMacStringFromPC( st ) ;
832 value.Replace( "\n", "\r" );
833 }
834 else
835 value = st;
836
837
838 if ( !m_macUsesTXN )
839 {
840 ::SetControlData((ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ;
841 }
842 else
843 {
844 bool formerEditable = IsEditable() ;
845 if ( !formerEditable )
846 SetEditable(true) ;
847 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)value.c_str(), value.Length(),
848 kTXNStartOffset, kTXNEndOffset);
849 TXNSetSelection( (TXNObject) m_macTXN, 0, 0);
850 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
851 if ( !formerEditable )
852 SetEditable(formerEditable) ;
853 }
854 MacRedrawControl() ;
855 }
856
857 void wxTextCtrl::SetMaxLength(unsigned long len)
858 {
859 m_maxLength = len ;
860 }
861
862 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
863 {
864 if ( m_macUsesTXN )
865 {
866 bool formerEditable = IsEditable() ;
867 if ( !formerEditable )
868 SetEditable(true) ;
869 TXNTypeAttributes typeAttr[4] ;
870 Str255 fontName = "\pMonaco" ;
871 SInt16 fontSize = 12 ;
872 Style fontStyle = normal ;
873 RGBColor color ;
874 int attrCounter = 0 ;
875 if ( style.HasFont() )
876 {
877 const wxFont &font = style.GetFont() ;
878 CopyCStringToPascal( font.GetFaceName().c_str() , fontName ) ;
879 fontSize = font.GetPointSize() ;
880 if ( font.GetUnderlined() )
881 fontStyle |= underline ;
882 if ( font.GetWeight() == wxBOLD )
883 fontStyle |= bold ;
884 if ( font.GetStyle() == wxITALIC )
885 fontStyle |= italic ;
886
887 typeAttr[attrCounter].tag = kTXNQDFontNameAttribute ;
888 typeAttr[attrCounter].size = kTXNQDFontNameAttributeSize ;
889 typeAttr[attrCounter].data.dataPtr = (void*) fontName ;
890 typeAttr[attrCounter+1].tag = kTXNQDFontSizeAttribute ;
891 typeAttr[attrCounter+1].size = kTXNFontSizeAttributeSize ;
892 typeAttr[attrCounter+1].data.dataValue = (fontSize << 16) ;
893 typeAttr[attrCounter+2].tag = kTXNQDFontStyleAttribute ;
894 typeAttr[attrCounter+2].size = kTXNQDFontStyleAttributeSize ;
895 typeAttr[attrCounter+2].data.dataValue = fontStyle ;
896 attrCounter += 3 ;
897
898 }
899 if ( style.HasTextColour() )
900 {
901 typeAttr[attrCounter].tag = kTXNQDFontColorAttribute ;
902 typeAttr[attrCounter].size = kTXNQDFontColorAttributeSize ;
903 typeAttr[attrCounter].data.dataPtr = (void*) &color ;
904 color = MAC_WXCOLORREF(style.GetTextColour().GetPixel()) ;
905 attrCounter += 1 ;
906 }
907
908 if ( attrCounter > 0 )
909 {
910 OSStatus status = TXNSetTypeAttributes ((TXNObject)m_macTXN, attrCounter , typeAttr,
911 start,end);
912 wxASSERT_MSG( status == noErr , "Couldn't set text attributes" ) ;
913 }
914 if ( !formerEditable )
915 SetEditable(formerEditable) ;
916 }
917 return TRUE ;
918 }
919
920 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style)
921 {
922 wxTextCtrlBase::SetDefaultStyle( style ) ;
923 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
924 return TRUE ;
925 }
926
927 // Clipboard operations
928 void wxTextCtrl::Copy()
929 {
930 if (CanCopy())
931 {
932 if ( !m_macUsesTXN )
933 {
934 TECopy( ((TEHandle) m_macTE) ) ;
935 ClearCurrentScrap();
936 TEToScrap() ;
937 MacRedrawControl() ;
938 }
939 else
940 {
941 ClearCurrentScrap();
942 TXNCopy((TXNObject)m_macTXN);
943 TXNConvertToPublicScrap();
944 }
945 }
946 }
947
948 void wxTextCtrl::Cut()
949 {
950 if (CanCut())
951 {
952 if ( !m_macUsesTXN )
953 {
954 TECut( ((TEHandle) m_macTE) ) ;
955 ClearCurrentScrap();
956 TEToScrap() ;
957 MacRedrawControl() ;
958 }
959 else
960 {
961 ClearCurrentScrap();
962 TXNCut((TXNObject)m_macTXN);
963 TXNConvertToPublicScrap();
964 }
965 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
966 event.SetString( GetValue() ) ;
967 event.SetEventObject( this );
968 GetEventHandler()->ProcessEvent(event);
969 }
970 }
971
972 void wxTextCtrl::Paste()
973 {
974 if (CanPaste())
975 {
976 if ( !m_macUsesTXN )
977 {
978 TEFromScrap() ;
979 TEPaste( (TEHandle) m_macTE ) ;
980 MacRedrawControl() ;
981 }
982 else
983 {
984 TXNConvertFromPublicScrap();
985 TXNPaste((TXNObject)m_macTXN);
986 SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ;
987 }
988 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
989 event.SetString( GetValue() ) ;
990 event.SetEventObject( this );
991 GetEventHandler()->ProcessEvent(event);
992 }
993 }
994
995 bool wxTextCtrl::CanCopy() const
996 {
997 // Can copy if there's a selection
998 long from, to;
999 GetSelection(& from, & to);
1000 return (from != to);
1001 }
1002
1003 bool wxTextCtrl::CanCut() const
1004 {
1005 if ( !IsEditable() )
1006 {
1007 return false ;
1008 }
1009 // Can cut if there's a selection
1010 long from, to;
1011 GetSelection(& from, & to);
1012 return (from != to);
1013 }
1014
1015 bool wxTextCtrl::CanPaste() const
1016 {
1017 if (!IsEditable())
1018 return FALSE;
1019
1020 #if TARGET_CARBON
1021 OSStatus err = noErr;
1022 ScrapRef scrapRef;
1023
1024 err = GetCurrentScrap( &scrapRef );
1025 if ( err != noTypeErr && err != memFullErr )
1026 {
1027 ScrapFlavorFlags flavorFlags;
1028 Size byteCount;
1029
1030 if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr)
1031 {
1032 if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr)
1033 {
1034 return TRUE ;
1035 }
1036 }
1037 }
1038 return FALSE;
1039
1040 #else
1041 long offset ;
1042 if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 )
1043 {
1044 return TRUE ;
1045 }
1046 #endif
1047 return FALSE ;
1048 }
1049
1050 void wxTextCtrl::SetEditable(bool editable)
1051 {
1052 if ( editable != m_editable )
1053 {
1054 m_editable = editable ;
1055 if ( !m_macUsesTXN )
1056 {
1057 if ( editable )
1058 UMAActivateControl( (ControlHandle) m_macControl ) ;
1059 else
1060 UMADeactivateControl((ControlHandle) m_macControl ) ;
1061 }
1062 else
1063 {
1064 TXNControlTag tag[] = { kTXNIOPrivilegesTag } ;
1065 TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ;
1066 TXNSetTXNObjectControls( (TXNObject) m_macTXN , false , sizeof(tag) / sizeof (TXNControlTag) , tag , data ) ;
1067 }
1068 }
1069 }
1070
1071 void wxTextCtrl::SetInsertionPoint(long pos)
1072 {
1073 SetSelection( pos , pos ) ;
1074 }
1075
1076 void wxTextCtrl::SetInsertionPointEnd()
1077 {
1078 long pos = GetLastPosition();
1079 SetInsertionPoint(pos);
1080 }
1081
1082 long wxTextCtrl::GetInsertionPoint() const
1083 {
1084 long begin,end ;
1085 GetSelection( &begin , &end ) ;
1086 return begin ;
1087 }
1088
1089 long wxTextCtrl::GetLastPosition() const
1090 {
1091 if ( !m_macUsesTXN )
1092 {
1093 return (**((TEHandle) m_macTE)).teLength ;
1094 }
1095 else
1096 {
1097 Handle theText ;
1098 long actualsize ;
1099 OSErr err = TXNGetDataEncoded( (TXNObject) m_macTXN, kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData );
1100 /* all done */
1101 if ( err )
1102 {
1103 actualsize = 0 ;
1104 }
1105 else
1106 {
1107 actualsize = GetHandleSize( theText ) ;
1108 DisposeHandle( theText ) ;
1109 }
1110 return actualsize ;
1111 }
1112 }
1113
1114 void wxTextCtrl::Replace(long from, long to, const wxString& value)
1115 {
1116 if ( !m_macUsesTXN )
1117 {
1118 ControlEditTextSelectionRec selection ;
1119
1120 selection.selStart = from ;
1121 selection.selEnd = to ;
1122 ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1123 TESetSelect( from , to , ((TEHandle) m_macTE) ) ;
1124 TEDelete( ((TEHandle) m_macTE) ) ;
1125 TEInsert( value , value.Length() , ((TEHandle) m_macTE) ) ;
1126 }
1127 else
1128 {
1129 bool formerEditable = IsEditable() ;
1130 if ( !formerEditable )
1131 SetEditable(true) ;
1132 TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ;
1133 TXNClear( ((TXNObject) m_macTXN) ) ;
1134 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)value.c_str(), value.Length(),
1135 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1136 if ( !formerEditable )
1137 SetEditable( formerEditable ) ;
1138 }
1139 Refresh() ;
1140 }
1141
1142 void wxTextCtrl::Remove(long from, long to)
1143 {
1144 if ( !m_macUsesTXN )
1145 {
1146 ControlEditTextSelectionRec selection ;
1147
1148 selection.selStart = from ;
1149 selection.selEnd = to ;
1150 ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1151 TEDelete( ((TEHandle) m_macTE) ) ;
1152 }
1153 else
1154 {
1155 bool formerEditable = IsEditable() ;
1156 if ( !formerEditable )
1157 SetEditable(true) ;
1158 TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ;
1159 TXNClear( ((TXNObject) m_macTXN) ) ;
1160 if ( !formerEditable )
1161 SetEditable( formerEditable ) ;
1162 }
1163 Refresh() ;
1164 }
1165
1166 void wxTextCtrl::SetSelection(long from, long to)
1167 {
1168
1169 if ( !m_macUsesTXN )
1170 {
1171 ControlEditTextSelectionRec selection ;
1172 selection.selStart = from ;
1173 selection.selEnd = to ;
1174
1175 TESetSelect( selection.selStart , selection.selEnd , ((TEHandle) m_macTE) ) ;
1176 ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ;
1177 }
1178 else
1179 {
1180 STPTextPaneVars **tpvars;
1181 /* set up our locals */
1182 tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl);
1183 /* and our drawing environment as the operation
1184 may force a redraw in the text area. */
1185 SetPort((**tpvars).fDrawingEnvironment);
1186 /* change the selection */
1187 TXNSetSelection( (**tpvars).fTXNRec, from, to);
1188 TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart);
1189 }
1190 }
1191
1192 bool wxTextCtrl::LoadFile(const wxString& file)
1193 {
1194 if ( wxTextCtrlBase::LoadFile(file) )
1195 {
1196 return TRUE;
1197 }
1198
1199 return FALSE;
1200 }
1201
1202 void wxTextCtrl::WriteText(const wxString& text)
1203 {
1204 wxString value ;
1205 if( wxApp::s_macDefaultEncodingIsPC )
1206 {
1207 value = wxMacMakeMacStringFromPC( text ) ;
1208 value.Replace( "\n", "\r" );
1209 }
1210 else
1211 value = text ;
1212
1213 if ( !m_macUsesTXN )
1214 {
1215 TEInsert( value , value.Length() , ((TEHandle) m_macTE) ) ;
1216 }
1217 else
1218 {
1219 bool formerEditable = IsEditable() ;
1220 if ( !formerEditable )
1221 SetEditable(true) ;
1222 long start , end , dummy ;
1223 GetSelection( &start , &dummy ) ;
1224 TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*) (const char*)value, value.Length(),
1225 kTXNUseCurrentSelection, kTXNUseCurrentSelection);
1226 GetSelection( &dummy , &end ) ;
1227 SetStyle( start , end , GetDefaultStyle() ) ;
1228 if ( !formerEditable )
1229 SetEditable( formerEditable ) ;
1230 }
1231 MacRedrawControl() ;
1232 }
1233
1234 void wxTextCtrl::AppendText(const wxString& text)
1235 {
1236 SetInsertionPointEnd();
1237 WriteText(text);
1238 }
1239
1240 void wxTextCtrl::Clear()
1241 {
1242 if ( !IsEditable() )
1243 {
1244 return ;
1245 }
1246 if ( !m_macUsesTXN )
1247 {
1248 ::SetControlData((ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ;
1249 }
1250 else
1251 {
1252 TXNSetSelection( (TXNObject)m_macTXN , kTXNStartOffset , kTXNEndOffset ) ;
1253 TXNClear((TXNObject)m_macTXN);
1254 }
1255 Refresh() ;
1256 }
1257
1258 bool wxTextCtrl::IsModified() const
1259 {
1260 return TRUE;
1261 }
1262
1263 bool wxTextCtrl::IsEditable() const
1264 {
1265 return IsEnabled() && m_editable ;
1266 }
1267
1268 bool wxTextCtrl::AcceptsFocus() const
1269 {
1270 // we don't want focus if we can't be edited
1271 return /*IsEditable() && */ wxControl::AcceptsFocus();
1272 }
1273
1274 wxSize wxTextCtrl::DoGetBestSize() const
1275 {
1276 int wText = 100 ;
1277
1278 int hText;
1279 if ( m_macUsesTXN )
1280 {
1281 hText = 17 ;
1282 }
1283 else
1284 {
1285 hText = 13 ;
1286 }
1287 /*
1288 int cx, cy;
1289 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1290
1291 int wText = DEFAULT_ITEM_WIDTH;
1292
1293 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1294
1295 return wxSize(wText, hText);
1296 */
1297 if ( m_windowStyle & wxTE_MULTILINE )
1298 {
1299 hText *= 5 ;
1300 }
1301 hText += 2 * m_macVerticalBorder ;
1302 wText += 2 * m_macHorizontalBorder ;
1303 //else: for single line control everything is ok
1304 return wxSize(wText, hText);
1305 }
1306
1307 // ----------------------------------------------------------------------------
1308 // Undo/redo
1309 // ----------------------------------------------------------------------------
1310
1311 void wxTextCtrl::Undo()
1312 {
1313 if (CanUndo())
1314 {
1315 }
1316 }
1317
1318 void wxTextCtrl::Redo()
1319 {
1320 if (CanRedo())
1321 {
1322 }
1323 }
1324
1325 bool wxTextCtrl::CanUndo() const
1326 {
1327 return FALSE ;
1328 }
1329
1330 bool wxTextCtrl::CanRedo() const
1331 {
1332 return FALSE ;
1333 }
1334
1335 // Makes 'unmodified'
1336 void wxTextCtrl::DiscardEdits()
1337 {
1338 // TODO
1339 }
1340
1341 int wxTextCtrl::GetNumberOfLines() const
1342 {
1343 // TODO change this if possible to reflect real lines
1344 wxString content = GetValue() ;
1345
1346 int count = 1;
1347 for (size_t i = 0; i < content.Length() ; i++)
1348 {
1349 if (content[i] == '\r') count++;
1350 }
1351
1352 return count;
1353 }
1354
1355 long wxTextCtrl::XYToPosition(long x, long y) const
1356 {
1357 // TODO
1358 return 0;
1359 }
1360
1361 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
1362 {
1363 return FALSE ;
1364 }
1365
1366 void wxTextCtrl::ShowPosition(long pos)
1367 {
1368 // TODO
1369 }
1370
1371 int wxTextCtrl::GetLineLength(long lineNo) const
1372 {
1373 // TODO change this if possible to reflect real lines
1374 wxString content = GetValue() ;
1375
1376 // Find line first
1377 int count = 0;
1378 for (size_t i = 0; i < content.Length() ; i++)
1379 {
1380 if (count == lineNo)
1381 {
1382 // Count chars in line then
1383 count = 0;
1384 for (size_t j = i; j < content.Length(); j++)
1385 {
1386 count++;
1387 if (content[j] == '\r') return count;
1388 }
1389
1390 return count;
1391 }
1392 if (content[i] == '\r') count++;
1393 }
1394 return 0;
1395 }
1396
1397 wxString wxTextCtrl::GetLineText(long lineNo) const
1398 {
1399 // TODO change this if possible to reflect real lines
1400 wxString content = GetValue() ;
1401
1402 // Find line first
1403 int count = 0;
1404 for (size_t i = 0; i < content.Length() ; i++)
1405 {
1406 if (count == lineNo)
1407 {
1408 // Add chars in line then
1409 wxString tmp("");
1410
1411 for (size_t j = i; j < content.Length(); j++)
1412 {
1413 if (content[j] == '\r')
1414 return tmp;
1415
1416 tmp += content[j];
1417 }
1418
1419 return tmp;
1420 }
1421 if (content[i] == '\r') count++;
1422 }
1423 return "" ;
1424 }
1425
1426 /*
1427 * Text item
1428 */
1429
1430 void wxTextCtrl::Command(wxCommandEvent & event)
1431 {
1432 SetValue (event.GetString());
1433 ProcessCommand (event);
1434 }
1435
1436 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1437 {
1438 // By default, load the first file into the text window.
1439 if (event.GetNumberOfFiles() > 0)
1440 {
1441 LoadFile(event.GetFiles()[0]);
1442 }
1443 }
1444
1445 void wxTextCtrl::OnChar(wxKeyEvent& event)
1446 {
1447 int key = event.GetKeyCode() ;
1448 bool eat_key = false ;
1449
1450 if ( key == 'c' && event.MetaDown() )
1451 {
1452 if ( CanCopy() )
1453 Copy() ;
1454 return ;
1455 }
1456
1457 if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
1458 !( key == WXK_RETURN && ( (m_windowStyle & wxPROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) )
1459 /* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */
1460 )
1461 {
1462 // eat it
1463 return ;
1464 }
1465 if ( key == 'v' && event.MetaDown() )
1466 {
1467 if ( CanPaste() )
1468 Paste() ;
1469 return ;
1470 }
1471 if ( key == 'x' && event.MetaDown() )
1472 {
1473 if ( CanCut() )
1474 Cut() ;
1475 return ;
1476 }
1477 switch ( key )
1478 {
1479 case WXK_RETURN:
1480 if (m_windowStyle & wxPROCESS_ENTER)
1481 {
1482 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
1483 event.SetEventObject( this );
1484 event.SetString( GetValue() );
1485 if ( GetEventHandler()->ProcessEvent(event) )
1486 return;
1487 }
1488 if ( !(m_windowStyle & wxTE_MULTILINE) )
1489 {
1490 wxWindow *parent = GetParent();
1491 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
1492 parent = parent->GetParent() ;
1493 }
1494 if ( parent && parent->GetDefaultItem() )
1495 {
1496 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1497 wxButton);
1498 if ( def && def->IsEnabled() )
1499 {
1500 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1501 event.SetEventObject(def);
1502 def->Command(event);
1503 return ;
1504 }
1505 }
1506
1507 // this will make wxWindows eat the ENTER key so that
1508 // we actually prevent line wrapping in a single line
1509 // text control
1510 eat_key = TRUE;
1511 }
1512
1513 break;
1514
1515 case WXK_TAB:
1516 // always produce navigation event - even if we process TAB
1517 // ourselves the fact that we got here means that the user code
1518 // decided to skip processing of this TAB - probably to let it
1519 // do its default job.
1520 {
1521 wxNavigationKeyEvent eventNav;
1522 eventNav.SetDirection(!event.ShiftDown());
1523 eventNav.SetWindowChange(event.ControlDown());
1524 eventNav.SetEventObject(this);
1525
1526 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
1527 return;
1528
1529 event.Skip() ;
1530 return;
1531 }
1532 break;
1533 }
1534
1535 if (!eat_key)
1536 {
1537 // default handling
1538 event.Skip() ;
1539 }
1540 if ( ( key >= 0x20 && key < WXK_START ) ||
1541 key == WXK_RETURN ||
1542 key == WXK_DELETE ||
1543 key == WXK_BACK)
1544 {
1545 wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
1546 event1.SetString( GetValue() ) ;
1547 event1.SetEventObject( this );
1548 wxPostEvent(GetEventHandler(),event1);
1549 }
1550 }
1551
1552 void wxTextCtrl::MacSuperShown( bool show )
1553 {
1554 bool former = m_macControlIsShown ;
1555 wxControl::MacSuperShown( show ) ;
1556 if ( (former != m_macControlIsShown) && m_macUsesTXN )
1557 {
1558 if ( m_macControlIsShown )
1559 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1560 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1561 else
1562 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1563 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1564 }
1565 }
1566
1567 bool wxTextCtrl::Show(bool show)
1568 {
1569 bool former = m_macControlIsShown ;
1570
1571 bool retval = wxControl::Show( show ) ;
1572
1573 if ( former != m_macControlIsShown && show )
1574 {
1575 if ( m_macControlIsShown )
1576 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1577 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1578 else
1579 TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left,
1580 (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame);
1581 }
1582
1583 return retval ;
1584 }
1585
1586 // ----------------------------------------------------------------------------
1587 // standard handlers for standard edit menu events
1588 // ----------------------------------------------------------------------------
1589
1590 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
1591 {
1592 Cut();
1593 }
1594
1595 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
1596 {
1597 Copy();
1598 }
1599
1600 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
1601 {
1602 Paste();
1603 }
1604
1605 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
1606 {
1607 Undo();
1608 }
1609
1610 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
1611 {
1612 Redo();
1613 }
1614
1615 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1616 {
1617 event.Enable( CanCut() );
1618 }
1619
1620 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1621 {
1622 event.Enable( CanCopy() );
1623 }
1624
1625 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1626 {
1627 event.Enable( CanPaste() );
1628 }
1629
1630 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1631 {
1632 event.Enable( CanUndo() );
1633 }
1634
1635 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1636 {
1637 event.Enable( CanRedo() );
1638 }
1639
1640 bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
1641 {
1642 if ( m_macUsesTXN )
1643 return true ;
1644 else
1645 return wxWindow::MacSetupCursor( pt ) ;
1646 }
1647
1648 #endif
1649 // wxUSE_TEXTCTRL