]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: wxTextCtrl | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
ed8c2780 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "textctrl.h" | |
14 | #endif | |
15 | ||
fedad417 GD |
16 | #include "wx/defs.h" |
17 | ||
18 | #if wxUSE_TEXTCTRL | |
19 | ||
f5c6eb5c | 20 | #ifdef __DARWIN__ |
03e11df5 GD |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
e9576ca5 | 23 | #else |
03e11df5 | 24 | #include <stat.h> |
e9576ca5 | 25 | #endif |
2b5f62a0 VZ |
26 | |
27 | #if wxUSE_STD_IOSTREAM | |
28 | #if wxUSE_IOSTREAMH | |
29 | #include <fstream.h> | |
30 | #else | |
31 | #include <fstream> | |
32 | #endif | |
33 | #endif | |
e9576ca5 | 34 | |
03e11df5 | 35 | #include "wx/app.h" |
5fde6fcc | 36 | #include "wx/dc.h" |
03e11df5 | 37 | #include "wx/button.h" |
422644a3 | 38 | #include "wx/toplevel.h" |
e9576ca5 | 39 | #include "wx/textctrl.h" |
90b22aca SC |
40 | #include "wx/notebook.h" |
41 | #include "wx/tabctrl.h" | |
e9576ca5 SC |
42 | #include "wx/settings.h" |
43 | #include "wx/filefn.h" | |
44 | #include "wx/utils.h" | |
45 | ||
46 | #if defined(__BORLANDC__) && !defined(__WIN32__) | |
03e11df5 | 47 | #include <alloc.h> |
f5c6eb5c | 48 | #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__) |
03e11df5 | 49 | #include <malloc.h> |
e9576ca5 SC |
50 | #endif |
51 | ||
66a09d47 SC |
52 | #ifndef __DARWIN__ |
53 | #include <Scrap.h> | |
1b2b1638 SC |
54 | #endif |
55 | #include <MacTextEditor.h> | |
56 | #include "ATSUnicode.h" | |
57 | #include "TextCommon.h" | |
58 | #include "TextEncodingConverter.h" | |
59 | #include "wx/mac/uma.h" | |
72055702 | 60 | |
29b30405 SC |
61 | #define TE_UNLIMITED_LENGTH 0xFFFFFFFFUL |
62 | ||
7759d157 | 63 | extern wxApp *wxTheApp ; |
e600c175 | 64 | extern wxControl *wxFindControlFromMacControl(ControlHandle inControl ) ; |
72055702 | 65 | |
7759d157 SC |
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 | |
72055702 SC |
70 | |
71 | #include "MacTextEditor.h" | |
72 | ||
73 | /* part codes */ | |
74 | ||
75 | /* kmUPTextPart is the part code we return to indicate the user has clicked | |
ed8c2780 | 76 | in the text area of our control */ |
72055702 SC |
77 | #define kmUPTextPart 1 |
78 | ||
79 | /* kmUPScrollPart is the part code we return to indicate the user has clicked | |
ed8c2780 | 80 | in the scroll bar part of the control. */ |
72055702 SC |
81 | #define kmUPScrollPart 2 |
82 | ||
83 | ||
84 | /* routines for using existing user pane controls. | |
ed8c2780 RR |
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.*/ | |
ef4a634b | 88 | |
72055702 | 89 | /* mUPOpenControl initializes a user pane control so it will be drawn |
ed8c2780 RR |
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. */ | |
29b30405 | 96 | OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle); |
72055702 SC |
97 | |
98 | /* Utility Routines */ | |
99 | ||
72055702 | 100 | enum { |
ed8c2780 | 101 | kShiftKeyCode = 56 |
72055702 SC |
102 | }; |
103 | ||
104 | /* kUserClickedToFocusPart is a part code we pass to the SetKeyboardFocus | |
ed8c2780 RR |
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'. */ | |
72055702 SC |
108 | #define kUserClickedToFocusPart 100 |
109 | ||
110 | ||
111 | /* kmUPClickScrollDelayTicks is a time measurement in ticks used to | |
ed8c2780 RR |
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. */ | |
72055702 SC |
115 | #define kmUPClickScrollDelayTicks 3 |
116 | ||
117 | ||
118 | /* STPTextPaneVars is a structure used for storing the the mUP Control's | |
ed8c2780 RR |
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. */ | |
72055702 SC |
122 | |
123 | typedef struct { | |
ed8c2780 RR |
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 */ | |
ef4a634b RD |
133 | Boolean fTEActive; /* reflects the activation state of the text edit record */ |
134 | Boolean fInDialogWindow; /* true if displayed in a dialog window */ | |
ed8c2780 RR |
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; | |
29e4a190 | 143 | bool fMultiline ; |
72055702 SC |
144 | } STPTextPaneVars; |
145 | ||
146 | ||
147 | ||
148 | ||
149 | /* Univerals Procedure Pointer variables used by the | |
ed8c2780 RR |
150 | mUP Control. These variables are set up |
151 | the first time that mUPOpenControl is called. */ | |
72055702 SC |
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 | ||
72055702 | 160 | /* TPActivatePaneText activates or deactivates the text edit record |
ed8c2780 RR |
161 | according to the value of setActive. The primary purpose of this |
162 | routine is to ensure each call is only made once. */ | |
72055702 | 163 | static void TPActivatePaneText(STPTextPaneVars **tpvars, Boolean setActive) { |
ed8c2780 RR |
164 | STPTextPaneVars *varsp; |
165 | varsp = *tpvars; | |
166 | if (varsp->fTEActive != setActive) { | |
ef4a634b | 167 | |
ed8c2780 | 168 | varsp->fTEActive = setActive; |
ef4a634b | 169 | |
ed8c2780 | 170 | TXNActivate(varsp->fTXNRec, varsp->fTXNFrame, varsp->fTEActive); |
7759d157 | 171 | |
ed8c2780 RR |
172 | if (varsp->fInFocus) |
173 | TXNFocus( varsp->fTXNRec, varsp->fTEActive); | |
174 | } | |
72055702 SC |
175 | } |
176 | ||
177 | ||
178 | /* TPFocusPaneText set the focus state for the text record. */ | |
179 | static void TPFocusPaneText(STPTextPaneVars **tpvars, Boolean setFocus) { | |
ed8c2780 RR |
180 | STPTextPaneVars *varsp; |
181 | varsp = *tpvars; | |
182 | if (varsp->fInFocus != setFocus) { | |
183 | varsp->fInFocus = setFocus; | |
184 | TXNFocus( varsp->fTXNRec, varsp->fInFocus); | |
185 | } | |
72055702 SC |
186 | } |
187 | ||
188 | ||
189 | /* TPPaneDrawProc is called to redraw the control and for update events | |
ed8c2780 RR |
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. */ | |
72055702 | 193 | static pascal void TPPaneDrawProc(ControlRef theControl, ControlPartCode thePart) { |
ed8c2780 RR |
194 | STPTextPaneVars **tpvars, *varsp; |
195 | char state; | |
196 | Rect bounds; | |
197 | /* set up our globals */ | |
ef4a634b | 198 | |
ed8c2780 RR |
199 | tpvars = (STPTextPaneVars **) GetControlReference(theControl); |
200 | if (tpvars != NULL) { | |
201 | state = HGetState((Handle) tpvars); | |
202 | HLock((Handle) tpvars); | |
203 | varsp = *tpvars; | |
ef4a634b | 204 | |
ed8c2780 RR |
205 | /* save the drawing state */ |
206 | SetPort((**tpvars).fDrawingEnvironment); | |
e600c175 | 207 | /* verify our boundary */ |
ed8c2780 | 208 | GetControlBounds(theControl, &bounds); |
e600c175 SC |
209 | |
210 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; | |
29e4a190 RR |
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 ) ; | |
ef4a634b | 215 | |
29e4a190 RR |
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); | |
ef4a634b | 219 | SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) , |
29e4a190 | 220 | bounds.right - (varsp->fMultiline ? 0 : 2), bounds.bottom - (varsp->fMultiline ? 0 : 2)); |
ed8c2780 | 221 | RectRgn(varsp->fTextBackgroundRgn, &varsp->fRTextOutline); |
ef4a634b | 222 | TXNSetFrameBounds( varsp->fTXNRec, varsp->fRTextArea.top, varsp->fRTextArea.left, |
29e4a190 | 223 | varsp->fRTextArea.bottom, varsp->fRTextArea.right, varsp->fTXNFrame); |
ed8c2780 RR |
224 | } |
225 | ||
226 | /* update the text region */ | |
29e4a190 RR |
227 | RGBColor white = { 65535 , 65535 , 65535 } ; |
228 | RGBBackColor( &white ) ; | |
ed8c2780 RR |
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); | |
e600c175 | 237 | |
ed8c2780 | 238 | } |
72055702 SC |
239 | } |
240 | ||
241 | ||
242 | /* TPPaneHitTestProc is called when the control manager would | |
ed8c2780 RR |
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. */ | |
72055702 | 246 | static pascal ControlPartCode TPPaneHitTestProc(ControlHandle theControl, Point where) { |
ed8c2780 RR |
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; | |
72055702 SC |
264 | } |
265 | ||
266 | ||
267 | ||
268 | ||
269 | ||
270 | /* TPPaneTrackingProc is called when the mouse is being held down | |
ed8c2780 RR |
271 | over our control. This routine handles clicks in the text area |
272 | and in the scroll bar. */ | |
72055702 | 273 | static pascal ControlPartCode TPPaneTrackingProc(ControlHandle theControl, Point startPt, ControlActionUPP actionProc) { |
ed8c2780 RR |
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)) { | |
ef4a634b | 294 | |
ed8c2780 RR |
295 | /* handle clicks in the text part */ |
296 | case kmUPTextPart: | |
297 | { SetPort((**tpvars).fDrawingEnvironment); | |
e600c175 | 298 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; |
ef4a634b | 299 | #if !TARGET_CARBON |
1b2b1638 | 300 | TXNClick( varsp->fTXNRec, (const EventRecord*) wxTheApp->MacGetCurrentEvent()); |
c5c9378c | 301 | #else |
e40298d5 JS |
302 | EventRecord rec ; |
303 | ConvertEventRefToEventRecord( (EventRef) wxTheApp->MacGetCurrentEvent() , &rec ) ; | |
c5c9378c SC |
304 | TXNClick( varsp->fTXNRec, &rec ); |
305 | #endif | |
ed8c2780 RR |
306 | } |
307 | break; | |
ef4a634b | 308 | |
ed8c2780 | 309 | } |
ef4a634b | 310 | |
ed8c2780 RR |
311 | HSetState((Handle) tpvars, state); |
312 | } | |
313 | return partCodeResult; | |
72055702 SC |
314 | } |
315 | ||
316 | ||
317 | /* TPPaneIdleProc is our user pane idle routine. When our text field | |
ed8c2780 | 318 | is active and in focus, we use this routine to set the cursor. */ |
72055702 | 319 | static pascal void TPPaneIdleProc(ControlHandle theControl) { |
ed8c2780 RR |
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) */ | |
1b2b1638 | 334 | SetPortWindowPort(GetControlOwner(theControl)); |
e600c175 | 335 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; |
ed8c2780 RR |
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); | |
ef4a634b RD |
348 | } |
349 | else | |
2b5f62a0 VZ |
350 | { |
351 | // SetThemeCursor(kThemeArrowCursor); | |
352 | } | |
ed8c2780 RR |
353 | } else { |
354 | /* if it's in our bounds, set the cursor */ | |
355 | GetControlBounds(theControl, &bounds); | |
356 | if (PtInRect(mousep, &bounds)) | |
2b5f62a0 VZ |
357 | { |
358 | // SetThemeCursor(kThemeArrowCursor); | |
359 | } | |
ed8c2780 | 360 | } |
ef4a634b | 361 | |
ed8c2780 RR |
362 | HSetState((Handle) tpvars, state); |
363 | } | |
364 | } | |
72055702 SC |
365 | } |
366 | ||
367 | ||
368 | /* TPPaneKeyDownProc is called whenever a keydown event is directed | |
ed8c2780 RR |
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. */ | |
72055702 | 371 | static pascal ControlPartCode TPPaneKeyDownProc(ControlHandle theControl, |
ed8c2780 RR |
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); | |
e600c175 | 379 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; |
8c82361c SC |
380 | EventRecord ev ; |
381 | memset( &ev , 0 , sizeof( ev ) ) ; | |
382 | ev.what = keyDown ; | |
383 | ev.modifiers = modifiers ; | |
45c8d9e5 | 384 | ev.message = (( keyCode << 8 ) & keyCodeMask ) + ( charCode & charCodeMask ) ; |
8c82361c | 385 | TXNKeyDown( (**tpvars).fTXNRec, &ev); |
ed8c2780 RR |
386 | } |
387 | } | |
388 | return kControlEntireControl; | |
72055702 SC |
389 | } |
390 | ||
391 | ||
392 | /* TPPaneActivateProc is called when the window containing | |
ed8c2780 RR |
393 | the user pane control receives activate events. Here, we redraw |
394 | the control and it's text as necessary for the activation state. */ | |
72055702 | 395 | static pascal void TPPaneActivateProc(ControlHandle theControl, Boolean activating) { |
ed8c2780 RR |
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); | |
e600c175 | 407 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; |
ed8c2780 RR |
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 | } | |
72055702 SC |
416 | } |
417 | ||
418 | ||
419 | /* TPPaneFocusProc is called when every the focus changes to or | |
ed8c2780 RR |
420 | from our control. Herein, switch the focus appropriately |
421 | according to the parameters and redraw the control as | |
422 | necessary. */ | |
72055702 | 423 | static pascal ControlPartCode TPPaneFocusProc(ControlHandle theControl, ControlFocusPart action) { |
ed8c2780 RR |
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); | |
e600c175 | 453 | wxMacWindowClipper clipper( wxFindControlFromMacControl(theControl ) ) ; |
ed8c2780 RR |
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 */ | |
1b2b1638 | 477 | HSetState((Handle) tpvars, state); |
ed8c2780 | 478 | } |
1b2b1638 | 479 | return focusResult; |
72055702 SC |
480 | } |
481 | ||
72055702 | 482 | |
1b2b1638 | 483 | /* mUPOpenControl initializes a user pane control so it will be drawn |
29e4a190 RR |
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. */ | |
ef4a634b | 490 | OSStatus mUPOpenControl(ControlHandle theControl, long wxStyle ) |
1b2b1638 | 491 | { |
29e4a190 RR |
492 | Rect bounds; |
493 | WindowRef theWindow; | |
494 | STPTextPaneVars **tpvars, *varsp; | |
29b30405 | 495 | OSStatus err = noErr ; |
29e4a190 RR |
496 | RGBColor rgbWhite = {0xFFFF, 0xFFFF, 0xFFFF}; |
497 | TXNBackground tback; | |
ef4a634b | 498 | |
29e4a190 RR |
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); | |
ef4a634b | 507 | |
29e4a190 RR |
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 */ | |
29b30405 | 514 | varsp->fMultiline = wxStyle & wxTE_MULTILINE ; |
29e4a190 RR |
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); | |
1b2b1638 SC |
520 | |
521 | varsp->fDrawingEnvironment = (GrafPtr) GetWindowPort(theWindow); | |
72055702 | 522 | |
1b2b1638 SC |
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); | |
ef4a634b | 536 | SetRect(&varsp->fRTextArea, bounds.left + 2 , bounds.top + (varsp->fMultiline ? 0 : 2) , |
1b2b1638 SC |
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); | |
72055702 | 542 | |
1b2b1638 SC |
543 | /* set up the drawing environment */ |
544 | SetPort(varsp->fDrawingEnvironment); | |
545 | ||
546 | /* create the new edit field */ | |
ef4a634b RD |
547 | |
548 | TXNFrameOptions frameOptions = | |
29b30405 SC |
549 | kTXNDontDrawCaretWhenInactiveMask ; |
550 | if ( ! ( wxStyle & wxTE_NOHIDESEL ) ) | |
2b5f62a0 | 551 | frameOptions |= kTXNDontDrawSelectionWhenInactiveMask ; |
ef4a634b | 552 | |
2b5f62a0 VZ |
553 | if ( wxStyle & wxTE_MULTILINE ) |
554 | { | |
555 | if ( ! ( wxStyle & wxTE_DONTWRAP ) ) | |
556 | frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ; | |
557 | else | |
558 | { | |
559 | frameOptions |= kTXNAlwaysWrapAtViewEdgeMask ; | |
560 | frameOptions |= kTXNWantHScrollBarMask ; | |
561 | } | |
ef4a634b | 562 | |
2b5f62a0 VZ |
563 | if ( !(wxStyle & wxTE_NO_VSCROLL ) ) |
564 | frameOptions |= kTXNWantVScrollBarMask ; | |
565 | } | |
566 | else | |
567 | frameOptions |= kTXNSingleLineOnlyMask ; | |
ef4a634b | 568 | |
2b5f62a0 VZ |
569 | if ( wxStyle & wxTE_READONLY ) |
570 | frameOptions |= kTXNReadOnlyMask ; | |
ef4a634b | 571 | |
1b2b1638 | 572 | TXNNewObject(NULL, varsp->fOwner, &varsp->fRTextArea, |
2b5f62a0 | 573 | frameOptions , |
1b2b1638 SC |
574 | kTXNTextEditStyleFrameType, |
575 | kTXNTextensionFile, | |
ef4a634b | 576 | kTXNSystemDefaultEncoding, |
1b2b1638 SC |
577 | &varsp->fTXNRec, &varsp->fTXNFrame, (TXNObjectRefcon) tpvars); |
578 | ||
29e4a190 RR |
579 | Str255 fontName ; |
580 | SInt16 fontSize ; | |
581 | Style fontStyle ; | |
ef4a634b | 582 | |
29e4a190 | 583 | GetThemeFont(kThemeSmallSystemFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ; |
1b2b1638 | 584 | |
29e4a190 RR |
585 | TXNTypeAttributes typeAttr[] = |
586 | { | |
402679b0 | 587 | { kTXNQDFontNameAttribute , kTXNQDFontNameAttributeSize , { (void*) fontName } } , |
29e4a190 RR |
588 | { kTXNQDFontSizeAttribute , kTXNFontSizeAttributeSize , { (void*) (fontSize << 16) } } , |
589 | { kTXNQDFontStyleAttribute , kTXNQDFontStyleAttributeSize , { (void*) normal } } , | |
402679b0 | 590 | } ; |
1b2b1638 | 591 | |
29b30405 SC |
592 | err = TXNSetTypeAttributes (varsp->fTXNRec, sizeof( typeAttr ) / sizeof(TXNTypeAttributes) , typeAttr, |
593 | kTXNStartOffset, | |
594 | kTXNEndOffset); | |
1b2b1638 | 595 | /* set the field's background */ |
427ff662 | 596 | |
1b2b1638 SC |
597 | tback.bgType = kTXNBackgroundTypeRGB; |
598 | tback.bg.color = rgbWhite; | |
599 | TXNSetBackground( varsp->fTXNRec, &tback); | |
ef4a634b | 600 | |
1b2b1638 SC |
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); | |
ed8c2780 | 606 | /* all done */ |
29b30405 | 607 | return err; |
72055702 SC |
608 | } |
609 | ||
1b2b1638 SC |
610 | |
611 | ||
612 | ||
72055702 SC |
613 | #if !USE_SHARED_LIBRARY |
614 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
615 | ||
616 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
ed8c2780 RR |
617 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) |
618 | EVT_CHAR(wxTextCtrl::OnChar) | |
72055702 SC |
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 | { | |
402679b0 SC |
636 | m_macTE = NULL ; |
637 | m_macTXN = NULL ; | |
638 | m_macTXNvars = NULL ; | |
1b2b1638 SC |
639 | m_macUsesTXN = false ; |
640 | m_editable = true ; | |
29b30405 | 641 | m_maxLength = TE_UNLIMITED_LENGTH ; |
1b2b1638 SC |
642 | } |
643 | ||
644 | wxTextCtrl::~wxTextCtrl() | |
645 | { | |
646 | if ( m_macUsesTXN ) | |
647 | { | |
402679b0 | 648 | SetControlReference((ControlHandle)m_macControl, 0) ; |
1b2b1638 | 649 | TXNDeleteObject((TXNObject)m_macTXN); |
1b2b1638 SC |
650 | /* delete our private storage */ |
651 | DisposeHandle((Handle) m_macTXNvars); | |
652 | /* zero the control reference */ | |
1b2b1638 | 653 | } |
72055702 SC |
654 | } |
655 | ||
656 | const short kVerticalMargin = 2 ; | |
657 | const short kHorizontalMargin = 2 ; | |
658 | ||
659 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | |
ed8c2780 | 660 | const wxString& st, |
72055702 SC |
661 | const wxPoint& pos, |
662 | const wxSize& size, long style, | |
663 | const wxValidator& validator, | |
664 | const wxString& name) | |
665 | { | |
e40298d5 JS |
666 | m_macTE = NULL ; |
667 | m_macTXN = NULL ; | |
668 | m_macTXNvars = NULL ; | |
669 | m_macUsesTXN = false ; | |
670 | m_editable = true ; | |
ef4a634b RD |
671 | |
672 | m_macUsesTXN = ! (style & wxTE_PASSWORD ) ; | |
673 | ||
e40298d5 | 674 | m_macUsesTXN &= (TXNInitTextension != (void*) kUnresolvedCFragSymbolAddress) ; |
ef4a634b | 675 | |
72055702 SC |
676 | // base initialization |
677 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) | |
678 | return FALSE; | |
679 | ||
ed8c2780 | 680 | wxSize mySize = size ; |
29e4a190 RR |
681 | if ( m_macUsesTXN ) |
682 | { | |
683 | m_macHorizontalBorder = 5 ; // additional pixels around the real control | |
684 | m_macVerticalBorder = 3 ; | |
685 | } | |
686 | else | |
ed8c2780 RR |
687 | { |
688 | m_macHorizontalBorder = 5 ; // additional pixels around the real control | |
689 | m_macVerticalBorder = 5 ; | |
690 | } | |
ed8c2780 RR |
691 | |
692 | ||
693 | Rect bounds ; | |
694 | Str255 title ; | |
402679b0 | 695 | /* |
ed8c2780 RR |
696 | if ( mySize.y == -1 ) |
697 | { | |
402679b0 SC |
698 | mySize.y = 13 ; |
699 | if ( m_windowStyle & wxTE_MULTILINE ) | |
700 | mySize.y *= 5 ; | |
ef4a634b | 701 | |
ed8c2780 RR |
702 | mySize.y += 2 * m_macVerticalBorder ; |
703 | } | |
402679b0 | 704 | */ |
427ff662 | 705 | MacPreControlCreate( parent , id , wxEmptyString , pos , mySize ,style, validator , name , &bounds , title ) ; |
72055702 SC |
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 | ||
29b30405 SC |
715 | if ( m_windowStyle & wxTE_READONLY) |
716 | { | |
717 | m_editable = FALSE ; | |
718 | } | |
72055702 | 719 | |
1b2b1638 | 720 | if ( !m_macUsesTXN ) |
9c641c05 | 721 | { |
ef4a634b | 722 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , "\p" , true , 0 , 0 , 1, |
29e4a190 RR |
723 | (style & wxTE_PASSWORD) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ; |
724 | long size ; | |
1b2b1638 SC |
725 | ::GetControlData((ControlHandle) m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &((TEHandle) m_macTE) , &size ) ; |
726 | ||
ed8c2780 | 727 | } |
29e4a190 RR |
728 | else |
729 | { | |
1b2b1638 SC |
730 | short featurSet; |
731 | ||
29b30405 | 732 | featurSet = kControlSupportsEmbedding | kControlSupportsFocus | kControlWantsIdle |
1b2b1638 SC |
733 | | kControlWantsActivate | kControlHandlesTracking | kControlHasSpecialBackground |
734 | | kControlGetsFocusOnClick | kControlSupportsLiveFeedback; | |
735 | /* create the control */ | |
175a6271 | 736 | m_macControl = NewControl(MAC_WXHWND(parent->MacGetRootWindow()), &bounds, "\p", true, featurSet, 0, featurSet, kControlUserPaneProc, 0); |
1b2b1638 | 737 | /* set up the mUP specific features and data */ |
2b5f62a0 | 738 | mUPOpenControl((ControlHandle) m_macControl, m_windowStyle ); |
29e4a190 | 739 | } |
ed8c2780 RR |
740 | MacPostControlCreate() ; |
741 | ||
29e4a190 RR |
742 | if ( !m_macUsesTXN ) |
743 | { | |
427ff662 SC |
744 | wxCharBuffer text = wxMacStringToCString( st ) ; |
745 | ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ; | |
1b2b1638 | 746 | } |
ed8c2780 | 747 | else |
ed8c2780 | 748 | { |
1b2b1638 | 749 | STPTextPaneVars **tpvars; |
29e4a190 | 750 | /* set up locals */ |
1b2b1638 | 751 | tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl); |
29e4a190 | 752 | /* set the text in the record */ |
1b2b1638 | 753 | m_macTXN = (**tpvars).fTXNRec ; |
427ff662 SC |
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 | |
1b2b1638 SC |
762 | m_macTXNvars = tpvars ; |
763 | m_macUsesTXN = true ; | |
2b5f62a0 | 764 | TXNSetSelection( (TXNObject) m_macTXN, 0, 0); |
e600c175 | 765 | TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart); |
1b2b1638 | 766 | } |
ef4a634b | 767 | |
e40298d5 | 768 | return TRUE; |
72055702 SC |
769 | } |
770 | ||
771 | wxString wxTextCtrl::GetValue() const | |
772 | { | |
c5c9378c SC |
773 | Size actualSize = 0; |
774 | wxString result ; | |
775 | OSStatus err ; | |
29e4a190 RR |
776 | if ( !m_macUsesTXN ) |
777 | { | |
e40298d5 | 778 | err = ::GetControlDataSize((ControlHandle) m_macControl, 0, |
c5c9378c | 779 | ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, &actualSize ) ; |
ef4a634b | 780 | |
427ff662 SC |
781 | if ( err ) |
782 | return wxEmptyString ; | |
ef4a634b | 783 | |
427ff662 SC |
784 | if ( actualSize > 0 ) |
785 | { | |
ef4a634b | 786 | wxCharBuffer buf(actualSize) ; |
e40298d5 | 787 | ::GetControlData( (ControlHandle) m_macControl, 0, |
ef4a634b | 788 | ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag, |
427ff662 SC |
789 | actualSize , buf.data() , &actualSize ) ; |
790 | result = wxMacMakeStringFromCString( buf ) ; | |
c5c9378c | 791 | } |
29e4a190 RR |
792 | } |
793 | else | |
794 | { | |
427ff662 | 795 | #if wxUSE_UNICODE |
29e4a190 | 796 | Handle theText ; |
427ff662 | 797 | err = TXNGetDataEncoded( ((TXNObject) m_macTXN), kTXNStartOffset, kTXNEndOffset, &theText , kTXNUnicodeTextData ); |
29e4a190 RR |
798 | // all done |
799 | if ( err ) | |
800 | { | |
c5c9378c | 801 | actualSize = 0 ; |
29e4a190 RR |
802 | } |
803 | else | |
804 | { | |
c5c9378c | 805 | actualSize = GetHandleSize( theText ) ; |
427ff662 SC |
806 | if ( actualSize > 0 ) |
807 | { | |
808 | wxChar *ptr = result.GetWriteBuf(actualSize*sizeof(wxChar)) ; | |
809 | wxStrncpy( ptr , (wxChar*) *theText , actualSize ) ; | |
e40298d5 JS |
810 | ptr[actualSize] = 0 ; |
811 | result.UngetWriteBuf( actualSize ) ; | |
812 | } | |
813 | DisposeHandle( theText ) ; | |
29e4a190 | 814 | } |
427ff662 SC |
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 | |
29e4a190 | 835 | } |
ef4a634b | 836 | |
427ff662 | 837 | return result ; |
72055702 SC |
838 | } |
839 | ||
840 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
841 | { | |
1b2b1638 | 842 | if ( !m_macUsesTXN ) |
72055702 | 843 | { |
1b2b1638 SC |
844 | *from = (**((TEHandle) m_macTE)).selStart; |
845 | *to = (**((TEHandle) m_macTE)).selEnd; | |
72055702 SC |
846 | } |
847 | else | |
848 | { | |
1b2b1638 | 849 | TXNGetSelection( ((TXNObject) m_macTXN) , (TXNOffset*) from , (TXNOffset*) to ) ; |
72055702 SC |
850 | } |
851 | } | |
852 | ||
853 | void wxTextCtrl::SetValue(const wxString& st) | |
854 | { | |
29e4a190 RR |
855 | if ( !m_macUsesTXN ) |
856 | { | |
427ff662 SC |
857 | wxCharBuffer text = wxMacStringToCString( st ) ; |
858 | ::SetControlData( (ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , strlen(text) , text ) ; | |
29e4a190 RR |
859 | } |
860 | else | |
861 | { | |
68698f03 | 862 | bool formerEditable = m_editable ; |
2b5f62a0 VZ |
863 | if ( !formerEditable ) |
864 | SetEditable(true) ; | |
427ff662 SC |
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 ) , | |
2b5f62a0 | 871 | kTXNStartOffset, kTXNEndOffset); |
427ff662 | 872 | #endif |
2b5f62a0 | 873 | TXNSetSelection( (TXNObject) m_macTXN, 0, 0); |
e600c175 | 874 | TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart); |
2b5f62a0 VZ |
875 | if ( !formerEditable ) |
876 | SetEditable(formerEditable) ; | |
29e4a190 | 877 | } |
1b2b1638 | 878 | MacRedrawControl() ; |
72055702 SC |
879 | } |
880 | ||
ef4a634b | 881 | void wxTextCtrl::SetMaxLength(unsigned long len) |
29b30405 SC |
882 | { |
883 | m_maxLength = len ; | |
884 | } | |
885 | ||
886 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
887 | { | |
888 | if ( m_macUsesTXN ) | |
889 | { | |
68698f03 | 890 | bool formerEditable = m_editable ; |
2b5f62a0 VZ |
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() ; | |
427ff662 | 902 | wxMacStringToPascal( font.GetFaceName() , fontName ) ; |
2b5f62a0 VZ |
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 ; | |
ef4a634b | 910 | |
2b5f62a0 VZ |
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 ; | |
ef4a634b | 921 | |
2b5f62a0 VZ |
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 | } | |
ef4a634b | 931 | |
29b30405 SC |
932 | if ( attrCounter > 0 ) |
933 | { | |
934 | OSStatus status = TXNSetTypeAttributes ((TXNObject)m_macTXN, attrCounter , typeAttr, | |
935 | start,end); | |
427ff662 | 936 | wxASSERT_MSG( status == noErr , wxT("Couldn't set text attributes") ) ; |
29b30405 | 937 | } |
2b5f62a0 VZ |
938 | if ( !formerEditable ) |
939 | SetEditable(formerEditable) ; | |
29b30405 SC |
940 | } |
941 | return TRUE ; | |
942 | } | |
943 | ||
944 | bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) | |
945 | { | |
946 | wxTextCtrlBase::SetDefaultStyle( style ) ; | |
947 | SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ; | |
948 | return TRUE ; | |
949 | } | |
950 | ||
72055702 SC |
951 | // Clipboard operations |
952 | void wxTextCtrl::Copy() | |
953 | { | |
954 | if (CanCopy()) | |
955 | { | |
1b2b1638 | 956 | if ( !m_macUsesTXN ) |
72055702 | 957 | { |
29e4a190 RR |
958 | TECopy( ((TEHandle) m_macTE) ) ; |
959 | ClearCurrentScrap(); | |
960 | TEToScrap() ; | |
961 | MacRedrawControl() ; | |
962 | } | |
963 | else | |
964 | { | |
32b5be3d | 965 | ClearCurrentScrap(); |
ef4a634b | 966 | TXNCopy((TXNObject)m_macTXN); |
1b2b1638 | 967 | TXNConvertToPublicScrap(); |
29e4a190 | 968 | } |
32b5be3d | 969 | } |
72055702 SC |
970 | } |
971 | ||
972 | void wxTextCtrl::Cut() | |
973 | { | |
974 | if (CanCut()) | |
975 | { | |
1b2b1638 | 976 | if ( !m_macUsesTXN ) |
32b5be3d | 977 | { |
29e4a190 RR |
978 | TECut( ((TEHandle) m_macTE) ) ; |
979 | ClearCurrentScrap(); | |
980 | TEToScrap() ; | |
981 | MacRedrawControl() ; | |
32b5be3d | 982 | } |
29e4a190 RR |
983 | else |
984 | { | |
1b2b1638 | 985 | ClearCurrentScrap(); |
ef4a634b | 986 | TXNCut((TXNObject)m_macTXN); |
1b2b1638 | 987 | TXNConvertToPublicScrap(); |
29e4a190 | 988 | } |
1b2b1638 SC |
989 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); |
990 | event.SetString( GetValue() ) ; | |
991 | event.SetEventObject( this ); | |
992 | GetEventHandler()->ProcessEvent(event); | |
29e4a190 | 993 | } |
72055702 SC |
994 | } |
995 | ||
996 | void wxTextCtrl::Paste() | |
997 | { | |
998 | if (CanPaste()) | |
999 | { | |
1b2b1638 SC |
1000 | if ( !m_macUsesTXN ) |
1001 | { | |
1002 | TEFromScrap() ; | |
1003 | TEPaste( (TEHandle) m_macTE ) ; | |
1004 | MacRedrawControl() ; | |
1005 | } | |
29e4a190 RR |
1006 | else |
1007 | { | |
1b2b1638 | 1008 | TXNConvertFromPublicScrap(); |
ef4a634b | 1009 | TXNPaste((TXNObject)m_macTXN); |
e600c175 | 1010 | SetStyle( kTXNUseCurrentSelection , kTXNUseCurrentSelection , GetDefaultStyle() ) ; |
29e4a190 | 1011 | } |
1b2b1638 SC |
1012 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); |
1013 | event.SetString( GetValue() ) ; | |
1014 | event.SetEventObject( this ); | |
1015 | GetEventHandler()->ProcessEvent(event); | |
32b5be3d | 1016 | } |
72055702 SC |
1017 | } |
1018 | ||
1019 | bool wxTextCtrl::CanCopy() const | |
1020 | { | |
1021 | // Can copy if there's a selection | |
1022 | long from, to; | |
1023 | GetSelection(& from, & to); | |
1024 | return (from != to); | |
1025 | } | |
1026 | ||
1027 | bool wxTextCtrl::CanCut() const | |
1028 | { | |
1b2b1638 SC |
1029 | if ( !IsEditable() ) |
1030 | { | |
1031 | return false ; | |
1032 | } | |
72055702 SC |
1033 | // Can cut if there's a selection |
1034 | long from, to; | |
1035 | GetSelection(& from, & to); | |
1036 | return (from != to); | |
1037 | } | |
1038 | ||
1039 | bool wxTextCtrl::CanPaste() const | |
1040 | { | |
1041 | if (!IsEditable()) | |
1042 | return FALSE; | |
1043 | ||
72055702 | 1044 | #if TARGET_CARBON |
ed8c2780 RR |
1045 | OSStatus err = noErr; |
1046 | ScrapRef scrapRef; | |
ef4a634b | 1047 | |
ed8c2780 | 1048 | err = GetCurrentScrap( &scrapRef ); |
ef4a634b | 1049 | if ( err != noTypeErr && err != memFullErr ) |
ed8c2780 | 1050 | { |
29e4a190 RR |
1051 | ScrapFlavorFlags flavorFlags; |
1052 | Size byteCount; | |
ef4a634b | 1053 | |
ed8c2780 RR |
1054 | if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr) |
1055 | { | |
1056 | if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr) | |
1057 | { | |
1058 | return TRUE ; | |
1059 | } | |
1060 | } | |
1061 | } | |
1062 | return FALSE; | |
ef4a634b | 1063 | |
72055702 | 1064 | #else |
49b45dcd | 1065 | long offset ; |
ed8c2780 RR |
1066 | if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 ) |
1067 | { | |
1068 | return TRUE ; | |
1069 | } | |
72055702 | 1070 | #endif |
ed8c2780 | 1071 | return FALSE ; |
72055702 SC |
1072 | } |
1073 | ||
1074 | void wxTextCtrl::SetEditable(bool editable) | |
1075 | { | |
1b2b1638 SC |
1076 | if ( editable != m_editable ) |
1077 | { | |
1078 | m_editable = editable ; | |
e600c175 SC |
1079 | if ( !m_macUsesTXN ) |
1080 | { | |
1081 | if ( editable ) | |
2b5f62a0 | 1082 | UMAActivateControl( (ControlHandle) m_macControl ) ; |
e600c175 | 1083 | else |
2b5f62a0 | 1084 | UMADeactivateControl((ControlHandle) m_macControl ) ; |
e600c175 | 1085 | } |
1b2b1638 | 1086 | else |
e600c175 SC |
1087 | { |
1088 | TXNControlTag tag[] = { kTXNIOPrivilegesTag } ; | |
49b45dcd | 1089 | TXNControlData data[] = { { editable ? kTXNReadWrite : kTXNReadOnly } } ; |
e600c175 SC |
1090 | TXNSetTXNObjectControls( (TXNObject) m_macTXN , false , sizeof(tag) / sizeof (TXNControlTag) , tag , data ) ; |
1091 | } | |
1b2b1638 | 1092 | } |
72055702 SC |
1093 | } |
1094 | ||
1095 | void wxTextCtrl::SetInsertionPoint(long pos) | |
1096 | { | |
ed8c2780 | 1097 | SetSelection( pos , pos ) ; |
72055702 SC |
1098 | } |
1099 | ||
1100 | void wxTextCtrl::SetInsertionPointEnd() | |
1101 | { | |
1102 | long pos = GetLastPosition(); | |
1103 | SetInsertionPoint(pos); | |
1104 | } | |
1105 | ||
1106 | long wxTextCtrl::GetInsertionPoint() const | |
1107 | { | |
e40298d5 JS |
1108 | long begin,end ; |
1109 | GetSelection( &begin , &end ) ; | |
1110 | return begin ; | |
72055702 SC |
1111 | } |
1112 | ||
1113 | long wxTextCtrl::GetLastPosition() const | |
1114 | { | |
1b2b1638 | 1115 | if ( !m_macUsesTXN ) |
32b5be3d | 1116 | { |
e40298d5 | 1117 | return (**((TEHandle) m_macTE)).teLength ; |
32b5be3d RR |
1118 | } |
1119 | else | |
1120 | { | |
e40298d5 JS |
1121 | Handle theText ; |
1122 | long actualsize ; | |
1123 | OSErr err = TXNGetDataEncoded( (TXNObject) m_macTXN, kTXNStartOffset, kTXNEndOffset, &theText , kTXNTextData ); | |
1124 | /* all done */ | |
1125 | if ( err ) | |
1126 | { | |
1127 | actualsize = 0 ; | |
1128 | } | |
1129 | else | |
1130 | { | |
1131 | actualsize = GetHandleSize( theText ) ; | |
1132 | DisposeHandle( theText ) ; | |
1133 | } | |
1134 | return actualsize ; | |
32b5be3d | 1135 | } |
72055702 SC |
1136 | } |
1137 | ||
1138 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
1139 | { | |
e40298d5 JS |
1140 | if ( !m_macUsesTXN ) |
1141 | { | |
29e4a190 | 1142 | ControlEditTextSelectionRec selection ; |
ef4a634b | 1143 | |
29e4a190 RR |
1144 | selection.selStart = from ; |
1145 | selection.selEnd = to ; | |
1146 | ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
1147 | TESetSelect( from , to , ((TEHandle) m_macTE) ) ; | |
1148 | TEDelete( ((TEHandle) m_macTE) ) ; | |
1149 | TEInsert( value , value.Length() , ((TEHandle) m_macTE) ) ; | |
1150 | } | |
1151 | else | |
1152 | { | |
68698f03 | 1153 | bool formerEditable = m_editable ; |
2b5f62a0 VZ |
1154 | if ( !formerEditable ) |
1155 | SetEditable(true) ; | |
1156 | TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ; | |
1157 | TXNClear( ((TXNObject) m_macTXN) ) ; | |
427ff662 SC |
1158 | #if wxUSE_UNICODE |
1159 | TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)value.wc_str(), value.Length() * 2 , | |
1160 | kTXNUseCurrentSelection, kTXNUseCurrentSelection); | |
1161 | #else | |
2b5f62a0 | 1162 | TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)value.c_str(), value.Length(), |
e40298d5 | 1163 | kTXNUseCurrentSelection, kTXNUseCurrentSelection); |
427ff662 | 1164 | #endif |
2b5f62a0 VZ |
1165 | if ( !formerEditable ) |
1166 | SetEditable( formerEditable ) ; | |
29e4a190 | 1167 | } |
32b5be3d | 1168 | Refresh() ; |
72055702 SC |
1169 | } |
1170 | ||
1171 | void wxTextCtrl::Remove(long from, long to) | |
1172 | { | |
e40298d5 JS |
1173 | if ( !m_macUsesTXN ) |
1174 | { | |
1175 | ControlEditTextSelectionRec selection ; | |
ef4a634b | 1176 | |
e40298d5 JS |
1177 | selection.selStart = from ; |
1178 | selection.selEnd = to ; | |
1179 | ::SetControlData( (ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
1180 | TEDelete( ((TEHandle) m_macTE) ) ; | |
1181 | } | |
1182 | else | |
1183 | { | |
68698f03 | 1184 | bool formerEditable = m_editable ; |
e40298d5 JS |
1185 | if ( !formerEditable ) |
1186 | SetEditable(true) ; | |
ef4a634b RD |
1187 | TXNSetSelection( ((TXNObject) m_macTXN) , from , to ) ; |
1188 | TXNClear( ((TXNObject) m_macTXN) ) ; | |
e40298d5 JS |
1189 | if ( !formerEditable ) |
1190 | SetEditable( formerEditable ) ; | |
1191 | } | |
ed8c2780 | 1192 | Refresh() ; |
72055702 SC |
1193 | } |
1194 | ||
1195 | void wxTextCtrl::SetSelection(long from, long to) | |
1196 | { | |
ef4a634b RD |
1197 | if ( from == -1 ) |
1198 | from = 0; | |
1199 | ||
1200 | if ( to == -1 ) | |
1201 | to = GetLastPosition(); | |
1202 | ||
e40298d5 JS |
1203 | if ( !m_macUsesTXN ) |
1204 | { | |
1205 | ControlEditTextSelectionRec selection ; | |
1206 | selection.selStart = from ; | |
1207 | selection.selEnd = to ; | |
ef4a634b | 1208 | |
e40298d5 JS |
1209 | TESetSelect( selection.selStart , selection.selEnd , ((TEHandle) m_macTE) ) ; |
1210 | ::SetControlData((ControlHandle) m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
1211 | } | |
1212 | else | |
1213 | { | |
1214 | STPTextPaneVars **tpvars; | |
29e4a190 | 1215 | /* set up our locals */ |
e40298d5 | 1216 | tpvars = (STPTextPaneVars **) GetControlReference((ControlHandle) m_macControl); |
29e4a190 RR |
1217 | /* and our drawing environment as the operation |
1218 | may force a redraw in the text area. */ | |
e40298d5 | 1219 | SetPort((**tpvars).fDrawingEnvironment); |
29e4a190 | 1220 | /* change the selection */ |
e40298d5 JS |
1221 | TXNSetSelection( (**tpvars).fTXNRec, from, to); |
1222 | TXNShowSelection( (TXNObject) m_macTXN, kTXNShowStart); | |
1223 | } | |
72055702 SC |
1224 | } |
1225 | ||
1226 | bool wxTextCtrl::LoadFile(const wxString& file) | |
1227 | { | |
1228 | if ( wxTextCtrlBase::LoadFile(file) ) | |
1229 | { | |
1230 | return TRUE; | |
1231 | } | |
1232 | ||
1233 | return FALSE; | |
1234 | } | |
1235 | ||
427ff662 | 1236 | void wxTextCtrl::WriteText(const wxString& st) |
ef4a634b | 1237 | { |
1b2b1638 | 1238 | if ( !m_macUsesTXN ) |
72055702 | 1239 | { |
427ff662 SC |
1240 | wxCharBuffer text = wxMacStringToCString( st ) ; |
1241 | TEInsert( text , strlen(text) , ((TEHandle) m_macTE) ) ; | |
29e4a190 RR |
1242 | } |
1243 | else | |
1244 | { | |
68698f03 | 1245 | bool formerEditable = m_editable ; |
2b5f62a0 VZ |
1246 | if ( !formerEditable ) |
1247 | SetEditable(true) ; | |
1248 | long start , end , dummy ; | |
1249 | GetSelection( &start , &dummy ) ; | |
427ff662 SC |
1250 | #if wxUSE_UNICODE |
1251 | TXNSetData( ((TXNObject) m_macTXN), kTXNUnicodeTextData, (void*)st.wc_str(), st.Length() * 2 , | |
29e4a190 | 1252 | kTXNUseCurrentSelection, kTXNUseCurrentSelection); |
427ff662 SC |
1253 | #else |
1254 | wxCharBuffer text = wxMacStringToCString( st ) ; | |
1255 | TXNSetData( ((TXNObject) m_macTXN), kTXNTextData, (void*)text.data(), strlen( text ) , | |
1256 | kTXNUseCurrentSelection, kTXNUseCurrentSelection); | |
1257 | #endif | |
2b5f62a0 | 1258 | GetSelection( &dummy , &end ) ; |
29b30405 | 1259 | SetStyle( start , end , GetDefaultStyle() ) ; |
2b5f62a0 VZ |
1260 | if ( !formerEditable ) |
1261 | SetEditable( formerEditable ) ; | |
29e4a190 RR |
1262 | } |
1263 | MacRedrawControl() ; | |
72055702 SC |
1264 | } |
1265 | ||
1266 | void wxTextCtrl::AppendText(const wxString& text) | |
1267 | { | |
1268 | SetInsertionPointEnd(); | |
1269 | WriteText(text); | |
1270 | } | |
1271 | ||
1272 | void wxTextCtrl::Clear() | |
1273 | { | |
1b2b1638 SC |
1274 | if ( !IsEditable() ) |
1275 | { | |
1276 | return ; | |
1277 | } | |
e40298d5 JS |
1278 | if ( !m_macUsesTXN ) |
1279 | { | |
1280 | ::SetControlData((ControlHandle) m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ; | |
1281 | } | |
1282 | else | |
1283 | { | |
9c34dd9d | 1284 | TXNSetSelection( (TXNObject)m_macTXN , kTXNStartOffset , kTXNEndOffset ) ; |
ef4a634b | 1285 | TXNClear((TXNObject)m_macTXN); |
e40298d5 | 1286 | } |
32b5be3d | 1287 | Refresh() ; |
72055702 SC |
1288 | } |
1289 | ||
1290 | bool wxTextCtrl::IsModified() const | |
1291 | { | |
1292 | return TRUE; | |
1293 | } | |
1294 | ||
1295 | bool wxTextCtrl::IsEditable() const | |
1296 | { | |
1b2b1638 | 1297 | return IsEnabled() && m_editable ; |
72055702 SC |
1298 | } |
1299 | ||
1300 | bool wxTextCtrl::AcceptsFocus() const | |
1301 | { | |
1302 | // we don't want focus if we can't be edited | |
1b2b1638 | 1303 | return /*IsEditable() && */ wxControl::AcceptsFocus(); |
72055702 SC |
1304 | } |
1305 | ||
1306 | wxSize wxTextCtrl::DoGetBestSize() const | |
1307 | { | |
1308 | int wText = 100 ; | |
ef4a634b | 1309 | |
402679b0 | 1310 | int hText; |
29e4a190 RR |
1311 | if ( m_macUsesTXN ) |
1312 | { | |
1313 | hText = 17 ; | |
1314 | } | |
1315 | else | |
1316 | { | |
1317 | hText = 13 ; | |
1318 | } | |
72055702 SC |
1319 | /* |
1320 | int cx, cy; | |
1321 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
1322 | ||
1323 | int wText = DEFAULT_ITEM_WIDTH; | |
1324 | ||
1325 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
1326 | ||
1327 | return wxSize(wText, hText); | |
1328 | */ | |
1329 | if ( m_windowStyle & wxTE_MULTILINE ) | |
1330 | { | |
402679b0 | 1331 | hText *= 5 ; |
72055702 | 1332 | } |
402679b0 SC |
1333 | hText += 2 * m_macVerticalBorder ; |
1334 | wText += 2 * m_macHorizontalBorder ; | |
72055702 SC |
1335 | //else: for single line control everything is ok |
1336 | return wxSize(wText, hText); | |
1337 | } | |
1338 | ||
1339 | // ---------------------------------------------------------------------------- | |
1340 | // Undo/redo | |
1341 | // ---------------------------------------------------------------------------- | |
1342 | ||
1343 | void wxTextCtrl::Undo() | |
1344 | { | |
1345 | if (CanUndo()) | |
1346 | { | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | void wxTextCtrl::Redo() | |
1351 | { | |
1352 | if (CanRedo()) | |
1353 | { | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | bool wxTextCtrl::CanUndo() const | |
1358 | { | |
1359 | return FALSE ; | |
1360 | } | |
1361 | ||
1362 | bool wxTextCtrl::CanRedo() const | |
1363 | { | |
1364 | return FALSE ; | |
1365 | } | |
1366 | ||
1367 | // Makes 'unmodified' | |
1368 | void wxTextCtrl::DiscardEdits() | |
1369 | { | |
1370 | // TODO | |
1371 | } | |
1372 | ||
1373 | int wxTextCtrl::GetNumberOfLines() const | |
1374 | { | |
e40298d5 JS |
1375 | // TODO change this if possible to reflect real lines |
1376 | wxString content = GetValue() ; | |
ef4a634b | 1377 | |
32b5be3d | 1378 | int count = 1; |
49b45dcd | 1379 | for (size_t i = 0; i < content.Length() ; i++) |
29e4a190 RR |
1380 | { |
1381 | if (content[i] == '\r') count++; | |
32b5be3d | 1382 | } |
ef4a634b | 1383 | |
1b2b1638 | 1384 | return count; |
72055702 SC |
1385 | } |
1386 | ||
1387 | long wxTextCtrl::XYToPosition(long x, long y) const | |
1388 | { | |
1389 | // TODO | |
1390 | return 0; | |
1391 | } | |
1392 | ||
1393 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
1394 | { | |
1395 | return FALSE ; | |
1396 | } | |
1397 | ||
1398 | void wxTextCtrl::ShowPosition(long pos) | |
1399 | { | |
1400 | // TODO | |
1401 | } | |
1402 | ||
1403 | int wxTextCtrl::GetLineLength(long lineNo) const | |
1404 | { | |
e40298d5 JS |
1405 | // TODO change this if possible to reflect real lines |
1406 | wxString content = GetValue() ; | |
ef4a634b | 1407 | |
29e4a190 RR |
1408 | // Find line first |
1409 | int count = 0; | |
49b45dcd | 1410 | for (size_t i = 0; i < content.Length() ; i++) |
32b5be3d RR |
1411 | { |
1412 | if (count == lineNo) | |
1413 | { | |
1414 | // Count chars in line then | |
1415 | count = 0; | |
49b45dcd | 1416 | for (size_t j = i; j < content.Length(); j++) |
32b5be3d RR |
1417 | { |
1418 | count++; | |
29e4a190 RR |
1419 | if (content[j] == '\r') return count; |
1420 | } | |
ef4a634b | 1421 | |
29e4a190 RR |
1422 | return count; |
1423 | } | |
1424 | if (content[i] == '\r') count++; | |
1425 | } | |
72055702 SC |
1426 | return 0; |
1427 | } | |
1428 | ||
1429 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
1430 | { | |
e40298d5 JS |
1431 | // TODO change this if possible to reflect real lines |
1432 | wxString content = GetValue() ; | |
9c641c05 | 1433 | |
29e4a190 RR |
1434 | // Find line first |
1435 | int count = 0; | |
49b45dcd | 1436 | for (size_t i = 0; i < content.Length() ; i++) |
32b5be3d RR |
1437 | { |
1438 | if (count == lineNo) | |
1439 | { | |
1440 | // Add chars in line then | |
427ff662 | 1441 | wxString tmp; |
ef4a634b | 1442 | |
49b45dcd | 1443 | for (size_t j = i; j < content.Length(); j++) |
29e4a190 RR |
1444 | { |
1445 | if (content[j] == '\r') | |
1446 | return tmp; | |
ef4a634b | 1447 | |
29e4a190 RR |
1448 | tmp += content[j]; |
1449 | } | |
ef4a634b | 1450 | |
29e4a190 RR |
1451 | return tmp; |
1452 | } | |
1453 | if (content[i] == '\r') count++; | |
1454 | } | |
427ff662 | 1455 | return wxEmptyString ; |
72055702 SC |
1456 | } |
1457 | ||
1458 | /* | |
1459 | * Text item | |
1460 | */ | |
ef4a634b | 1461 | |
72055702 SC |
1462 | void wxTextCtrl::Command(wxCommandEvent & event) |
1463 | { | |
1464 | SetValue (event.GetString()); | |
1465 | ProcessCommand (event); | |
1466 | } | |
1467 | ||
1468 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
1469 | { | |
1470 | // By default, load the first file into the text window. | |
1471 | if (event.GetNumberOfFiles() > 0) | |
1472 | { | |
1473 | LoadFile(event.GetFiles()[0]); | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
1478 | { | |
1b2b1638 SC |
1479 | int key = event.GetKeyCode() ; |
1480 | bool eat_key = false ; | |
ef4a634b | 1481 | |
e600c175 SC |
1482 | if ( key == 'c' && event.MetaDown() ) |
1483 | { | |
1484 | if ( CanCopy() ) | |
1485 | Copy() ; | |
1486 | return ; | |
1487 | } | |
ef4a634b RD |
1488 | |
1489 | if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB && | |
1490 | !( key == WXK_RETURN && ( (m_windowStyle & wxPROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) ) | |
1b2b1638 SC |
1491 | /* && key != WXK_PRIOR && key != WXK_NEXT && key != WXK_HOME && key != WXK_END */ |
1492 | ) | |
1493 | { | |
1494 | // eat it | |
1495 | return ; | |
1496 | } | |
e600c175 SC |
1497 | if ( key == 'v' && event.MetaDown() ) |
1498 | { | |
1499 | if ( CanPaste() ) | |
1500 | Paste() ; | |
1501 | return ; | |
1502 | } | |
1503 | if ( key == 'x' && event.MetaDown() ) | |
1504 | { | |
1505 | if ( CanCut() ) | |
1506 | Cut() ; | |
1507 | return ; | |
1508 | } | |
1b2b1638 | 1509 | switch ( key ) |
72055702 SC |
1510 | { |
1511 | case WXK_RETURN: | |
ed8c2780 RR |
1512 | if (m_windowStyle & wxPROCESS_ENTER) |
1513 | { | |
72055702 SC |
1514 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); |
1515 | event.SetEventObject( this ); | |
1b2b1638 | 1516 | event.SetString( GetValue() ); |
72055702 SC |
1517 | if ( GetEventHandler()->ProcessEvent(event) ) |
1518 | return; | |
ef4a634b | 1519 | } |
72055702 SC |
1520 | if ( !(m_windowStyle & wxTE_MULTILINE) ) |
1521 | { | |
ed8c2780 | 1522 | wxWindow *parent = GetParent(); |
9c641c05 SC |
1523 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { |
1524 | parent = parent->GetParent() ; | |
ed8c2780 | 1525 | } |
9c641c05 | 1526 | if ( parent && parent->GetDefaultItem() ) |
ed8c2780 | 1527 | { |
9c641c05 | 1528 | wxButton *def = wxDynamicCast(parent->GetDefaultItem(), |
72055702 | 1529 | wxButton); |
ed8c2780 RR |
1530 | if ( def && def->IsEnabled() ) |
1531 | { | |
1532 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
1533 | event.SetEventObject(def); | |
1534 | def->Command(event); | |
1535 | return ; | |
72055702 | 1536 | } |
ed8c2780 | 1537 | } |
ef4a634b | 1538 | |
1b2b1638 SC |
1539 | // this will make wxWindows eat the ENTER key so that |
1540 | // we actually prevent line wrapping in a single line | |
1541 | // text control | |
1542 | eat_key = TRUE; | |
72055702 | 1543 | } |
72055702 SC |
1544 | |
1545 | break; | |
1546 | ||
1547 | case WXK_TAB: | |
1548 | // always produce navigation event - even if we process TAB | |
1549 | // ourselves the fact that we got here means that the user code | |
1550 | // decided to skip processing of this TAB - probably to let it | |
1551 | // do its default job. | |
1552 | { | |
1553 | wxNavigationKeyEvent eventNav; | |
1554 | eventNav.SetDirection(!event.ShiftDown()); | |
1555 | eventNav.SetWindowChange(event.ControlDown()); | |
1556 | eventNav.SetEventObject(this); | |
1557 | ||
1558 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) | |
1559 | return; | |
1b2b1638 | 1560 | |
ed8c2780 | 1561 | event.Skip() ; |
1b2b1638 | 1562 | return; |
72055702 SC |
1563 | } |
1564 | break; | |
1565 | } | |
ef4a634b | 1566 | |
1b2b1638 | 1567 | if (!eat_key) |
ed8c2780 | 1568 | { |
45c8d9e5 SC |
1569 | // default handling |
1570 | event.Skip() ; | |
1b2b1638 | 1571 | } |
e600c175 | 1572 | if ( ( key >= 0x20 && key < WXK_START ) || |
45c8d9e5 | 1573 | key == WXK_RETURN || |
ef4a634b | 1574 | key == WXK_DELETE || |
45c8d9e5 | 1575 | key == WXK_BACK) |
1b2b1638 SC |
1576 | { |
1577 | wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
1578 | event1.SetString( GetValue() ) ; | |
1579 | event1.SetEventObject( this ); | |
45c8d9e5 | 1580 | wxPostEvent(GetEventHandler(),event1); |
ed8c2780 | 1581 | } |
72055702 SC |
1582 | } |
1583 | ||
ef4a634b | 1584 | void wxTextCtrl::MacSuperShown( bool show ) |
fd4393b8 SC |
1585 | { |
1586 | bool former = m_macControlIsShown ; | |
1587 | wxControl::MacSuperShown( show ) ; | |
1588 | if ( (former != m_macControlIsShown) && m_macUsesTXN ) | |
1589 | { | |
1590 | if ( m_macControlIsShown ) | |
ef4a634b | 1591 | TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left, |
29e4a190 | 1592 | (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame); |
fd4393b8 | 1593 | else |
ef4a634b | 1594 | TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left, |
29e4a190 | 1595 | (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame); |
fd4393b8 SC |
1596 | } |
1597 | } | |
1598 | ||
ef4a634b | 1599 | bool wxTextCtrl::Show(bool show) |
fd4393b8 SC |
1600 | { |
1601 | bool former = m_macControlIsShown ; | |
ef4a634b | 1602 | |
fd4393b8 | 1603 | bool retval = wxControl::Show( show ) ; |
ef4a634b | 1604 | |
2eefc6d5 | 1605 | if ( former != m_macControlIsShown && m_macUsesTXN ) |
fd4393b8 SC |
1606 | { |
1607 | if ( m_macControlIsShown ) | |
ef4a634b | 1608 | TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left, |
29e4a190 | 1609 | (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom,(**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame); |
fd4393b8 | 1610 | else |
ef4a634b | 1611 | TXNSetFrameBounds( (TXNObject) m_macTXN, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.top + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.left, |
29e4a190 | 1612 | (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.bottom + 30000, (**(STPTextPaneVars **)m_macTXNvars).fRTextArea.right, (**(STPTextPaneVars **)m_macTXNvars).fTXNFrame); |
fd4393b8 | 1613 | } |
ef4a634b | 1614 | |
fd4393b8 SC |
1615 | return retval ; |
1616 | } | |
1617 | ||
72055702 SC |
1618 | // ---------------------------------------------------------------------------- |
1619 | // standard handlers for standard edit menu events | |
1620 | // ---------------------------------------------------------------------------- | |
1621 | ||
eb22f2a6 | 1622 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
72055702 SC |
1623 | { |
1624 | Cut(); | |
1625 | } | |
1626 | ||
eb22f2a6 | 1627 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) |
72055702 SC |
1628 | { |
1629 | Copy(); | |
1630 | } | |
1631 | ||
eb22f2a6 | 1632 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) |
72055702 SC |
1633 | { |
1634 | Paste(); | |
1635 | } | |
1636 | ||
eb22f2a6 | 1637 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) |
72055702 SC |
1638 | { |
1639 | Undo(); | |
1640 | } | |
1641 | ||
eb22f2a6 | 1642 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) |
72055702 SC |
1643 | { |
1644 | Redo(); | |
1645 | } | |
1646 | ||
1647 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
1648 | { | |
1649 | event.Enable( CanCut() ); | |
1650 | } | |
1651 | ||
1652 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
1653 | { | |
1654 | event.Enable( CanCopy() ); | |
1655 | } | |
1656 | ||
1657 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
1658 | { | |
1659 | event.Enable( CanPaste() ); | |
1660 | } | |
1661 | ||
1662 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
1663 | { | |
1664 | event.Enable( CanUndo() ); | |
1665 | } | |
1666 | ||
1667 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
1668 | { | |
1669 | event.Enable( CanRedo() ); | |
1670 | } | |
1671 | ||
2b5f62a0 VZ |
1672 | bool wxTextCtrl::MacSetupCursor( const wxPoint& pt ) |
1673 | { | |
1674 | if ( m_macUsesTXN ) | |
1675 | return true ; | |
1676 | else | |
1677 | return wxWindow::MacSetupCursor( pt ) ; | |
1678 | } | |
72055702 | 1679 | |
fedad417 GD |
1680 | #endif |
1681 | // wxUSE_TEXTCTRL |