]>
Commit | Line | Data |
---|---|---|
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 | #include <fstream.h> | |
27 | ||
28 | #include "wx/app.h" | |
29 | #include "wx/dc.h" | |
30 | #include "wx/button.h" | |
31 | #include "wx/panel.h" | |
32 | #include "wx/textctrl.h" | |
33 | #include "wx/notebook.h" | |
34 | #include "wx/tabctrl.h" | |
35 | #include "wx/settings.h" | |
36 | #include "wx/filefn.h" | |
37 | #include "wx/utils.h" | |
38 | ||
39 | #if defined(__BORLANDC__) && !defined(__WIN32__) | |
40 | #include <alloc.h> | |
41 | #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__) | |
42 | #include <malloc.h> | |
43 | #endif | |
44 | ||
45 | #include "wx/mac/uma.h" | |
46 | ||
47 | #define wxUSE_MLTE 0 | |
48 | ||
49 | #if wxUSE_MLTE == 0 // old textctrl implementation | |
50 | ||
51 | #if !USE_SHARED_LIBRARY | |
52 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
53 | ||
54 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
55 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
56 | EVT_CHAR(wxTextCtrl::OnChar) | |
57 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
58 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
59 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
60 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
61 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
62 | ||
63 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
64 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
65 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
66 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
67 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
68 | END_EVENT_TABLE() | |
69 | #endif | |
70 | ||
71 | // Text item | |
72 | wxTextCtrl::wxTextCtrl() | |
73 | { | |
74 | } | |
75 | ||
76 | const short kVerticalMargin = 2 ; | |
77 | const short kHorizontalMargin = 2 ; | |
78 | ||
79 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | |
80 | const wxString& st, | |
81 | const wxPoint& pos, | |
82 | const wxSize& size, long style, | |
83 | const wxValidator& validator, | |
84 | const wxString& name) | |
85 | { | |
86 | // base initialization | |
87 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) | |
88 | return FALSE; | |
89 | ||
90 | wxSize mySize = size ; | |
91 | if ( UMAHasAppearance() ) | |
92 | { | |
93 | m_macHorizontalBorder = 5 ; // additional pixels around the real control | |
94 | m_macVerticalBorder = 5 ; | |
95 | } | |
96 | else | |
97 | { | |
98 | m_macHorizontalBorder = 0 ; // additional pixels around the real control | |
99 | m_macVerticalBorder = 0 ; | |
100 | } | |
101 | ||
102 | ||
103 | Rect bounds ; | |
104 | Str255 title ; | |
105 | ||
106 | if ( mySize.y == -1 ) | |
107 | { | |
108 | if ( UMAHasAppearance() ) | |
109 | mySize.y = 13 ; | |
110 | else | |
111 | mySize.y = 24 ; | |
112 | ||
113 | mySize.y += 2 * m_macVerticalBorder ; | |
114 | } | |
115 | ||
116 | MacPreControlCreate( parent , id , "" , pos , mySize ,style, validator , name , &bounds , title ) ; | |
117 | ||
118 | if ( m_windowStyle & wxTE_MULTILINE ) | |
119 | { | |
120 | wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER), | |
121 | wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") ); | |
122 | ||
123 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
124 | } | |
125 | ||
126 | ||
127 | m_macControl = ::NewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1, | |
128 | ( style & wxTE_PASSWORD ) ? kControlEditTextPasswordProc : kControlEditTextProc , (long) this ) ; | |
129 | MacPostControlCreate() ; | |
130 | ||
131 | wxString value ; | |
132 | ||
133 | { | |
134 | TEHandle teH ; | |
135 | long size ; | |
136 | ||
137 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
138 | (*teH)->lineHeight = -1 ; | |
139 | } | |
140 | ||
141 | if( wxApp::s_macDefaultEncodingIsPC ) | |
142 | value = wxMacMakeMacStringFromPC( st ) ; | |
143 | else | |
144 | value = st ; | |
145 | ::SetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ; | |
146 | ||
147 | return TRUE; | |
148 | } | |
149 | ||
150 | wxString wxTextCtrl::GetValue() const | |
151 | { | |
152 | Size actualsize; | |
153 | ::GetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ; | |
154 | wxBuffer[actualsize] = 0 ; | |
155 | if( wxApp::s_macDefaultEncodingIsPC ) | |
156 | return wxMacMakePCStringFromMac( wxBuffer ) ; | |
157 | else | |
158 | return wxString(wxBuffer); | |
159 | } | |
160 | ||
161 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
162 | { | |
163 | ControlEditTextSelectionRec selection ; | |
164 | TEHandle teH ; | |
165 | long size ; | |
166 | ||
167 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
168 | ||
169 | *from = (**teH).selStart; | |
170 | *to = (**teH).selEnd; | |
171 | } | |
172 | ||
173 | void wxTextCtrl::SetValue(const wxString& st) | |
174 | { | |
175 | wxString value ; | |
176 | ||
177 | if( wxApp::s_macDefaultEncodingIsPC ) | |
178 | value = wxMacMakeMacStringFromPC( st ) ; | |
179 | else | |
180 | value = st ; | |
181 | ::SetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , value.Length() , (char*) ((const char*)value) ) ; | |
182 | WindowRef window = GetMacRootWindow() ; | |
183 | if ( window ) | |
184 | { | |
185 | wxWindow* win = wxFindWinFromMacWindow( window ) ; | |
186 | if ( win ) | |
187 | { | |
188 | wxMacDrawingHelper help( win ) ; | |
189 | // the mac control manager always assumes to have the origin at 0,0 | |
190 | SetOrigin( 0 , 0 ) ; | |
191 | ||
192 | bool hasTabBehind = false ; | |
193 | wxWindow* parent = GetParent() ; | |
194 | while ( parent ) | |
195 | { | |
196 | if( parent->MacGetWindowData() ) | |
197 | { | |
198 | ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ; | |
199 | break ; | |
200 | } | |
201 | ||
202 | if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )) | |
203 | { | |
204 | if ( ((wxControl*)parent)->GetMacControl() ) | |
205 | SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ; | |
206 | break ; | |
207 | } | |
208 | ||
209 | parent = parent->GetParent() ; | |
210 | } | |
211 | ||
212 | UMADrawControl( m_macControl ) ; | |
213 | ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ; | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | // Clipboard operations | |
219 | void wxTextCtrl::Copy() | |
220 | { | |
221 | if (CanCopy()) | |
222 | { | |
223 | TEHandle teH ; | |
224 | long size ; | |
225 | ||
226 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
227 | TECopy( teH ) ; | |
228 | #if TARGET_CARBON | |
229 | OSStatus err ; | |
230 | err = ClearCurrentScrap( ); | |
231 | #else | |
232 | OSErr err ; | |
233 | err = ZeroScrap( ); | |
234 | #endif | |
235 | TEToScrap() ; | |
236 | } | |
237 | } | |
238 | ||
239 | void wxTextCtrl::Cut() | |
240 | { | |
241 | if (CanCut()) | |
242 | { | |
243 | TEHandle teH ; | |
244 | long size ; | |
245 | ||
246 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
247 | TECut( teH ) ; | |
248 | #if TARGET_CARBON | |
249 | OSStatus err ; | |
250 | err = ClearCurrentScrap( ); | |
251 | #else | |
252 | OSErr err ; | |
253 | err = ZeroScrap( ); | |
254 | #endif | |
255 | TEToScrap() ; | |
256 | // MacInvalidateControl() ; | |
257 | } | |
258 | } | |
259 | ||
260 | void wxTextCtrl::Paste() | |
261 | { | |
262 | if (CanPaste()) | |
263 | { | |
264 | TEHandle teH ; | |
265 | long size ; | |
266 | ||
267 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
268 | TEFromScrap() ; | |
269 | TEPaste( teH ) ; | |
270 | WindowRef window = GetMacRootWindow() ; | |
271 | if ( window ) | |
272 | { | |
273 | wxWindow* win = wxFindWinFromMacWindow( window ) ; | |
274 | if ( win ) | |
275 | { | |
276 | wxMacDrawingHelper help( win ) ; | |
277 | // the mac control manager always assumes to have the origin at 0,0 | |
278 | SetOrigin( 0 , 0 ) ; | |
279 | ||
280 | bool hasTabBehind = false ; | |
281 | wxWindow* parent = GetParent() ; | |
282 | while ( parent ) | |
283 | { | |
284 | if( parent->MacGetWindowData() ) | |
285 | { | |
286 | ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ; | |
287 | break ; | |
288 | } | |
289 | ||
290 | if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )) | |
291 | { | |
292 | if ( ((wxControl*)parent)->GetMacControl() ) | |
293 | SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ; | |
294 | break ; | |
295 | } | |
296 | ||
297 | parent = parent->GetParent() ; | |
298 | } | |
299 | ||
300 | UMADrawControl( m_macControl ) ; | |
301 | ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ; | |
302 | } | |
303 | } | |
304 | } | |
305 | } | |
306 | ||
307 | bool wxTextCtrl::CanCopy() const | |
308 | { | |
309 | // Can copy if there's a selection | |
310 | long from, to; | |
311 | GetSelection(& from, & to); | |
312 | return (from != to); | |
313 | } | |
314 | ||
315 | bool wxTextCtrl::CanCut() const | |
316 | { | |
317 | // Can cut if there's a selection | |
318 | long from, to; | |
319 | GetSelection(& from, & to); | |
320 | return (from != to); | |
321 | } | |
322 | ||
323 | bool wxTextCtrl::CanPaste() const | |
324 | { | |
325 | if (!IsEditable()) | |
326 | return FALSE; | |
327 | ||
328 | long offset ; | |
329 | #if TARGET_CARBON | |
330 | OSStatus err = noErr; | |
331 | ScrapRef scrapRef; | |
332 | ||
333 | err = GetCurrentScrap( &scrapRef ); | |
334 | if ( err != noTypeErr && err != memFullErr ) | |
335 | { | |
336 | ScrapFlavorFlags flavorFlags; | |
337 | Size byteCount; | |
338 | ||
339 | if (( err = GetScrapFlavorFlags( scrapRef, 'TEXT', &flavorFlags )) == noErr) | |
340 | { | |
341 | if (( err = GetScrapFlavorSize( scrapRef, 'TEXT', &byteCount )) == noErr) | |
342 | { | |
343 | return TRUE ; | |
344 | } | |
345 | } | |
346 | } | |
347 | return FALSE; | |
348 | ||
349 | #else | |
350 | if ( GetScrap( NULL , 'TEXT' , &offset ) > 0 ) | |
351 | { | |
352 | return TRUE ; | |
353 | } | |
354 | #endif | |
355 | return FALSE ; | |
356 | } | |
357 | ||
358 | void wxTextCtrl::SetEditable(bool editable) | |
359 | { | |
360 | if ( editable ) | |
361 | UMAActivateControl( m_macControl ) ; | |
362 | else | |
363 | UMADeactivateControl( m_macControl ) ; | |
364 | } | |
365 | ||
366 | void wxTextCtrl::SetInsertionPoint(long pos) | |
367 | { | |
368 | SetSelection( pos , pos ) ; | |
369 | } | |
370 | ||
371 | void wxTextCtrl::SetInsertionPointEnd() | |
372 | { | |
373 | long pos = GetLastPosition(); | |
374 | SetInsertionPoint(pos); | |
375 | } | |
376 | ||
377 | long wxTextCtrl::GetInsertionPoint() const | |
378 | { | |
379 | ControlEditTextSelectionRec selection ; | |
380 | TEHandle teH ; | |
381 | long size ; | |
382 | ||
383 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
384 | // ::GetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ; | |
385 | return (**teH).selStart ; | |
386 | } | |
387 | ||
388 | long wxTextCtrl::GetLastPosition() const | |
389 | { | |
390 | ControlEditTextSelectionRec selection ; | |
391 | TEHandle teH ; | |
392 | long size ; | |
393 | ||
394 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
395 | ||
396 | // ::GetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection , &size ) ; | |
397 | return (**teH).teLength ; | |
398 | } | |
399 | ||
400 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
401 | { | |
402 | TEHandle teH ; | |
403 | long size ; | |
404 | ||
405 | ControlEditTextSelectionRec selection ; | |
406 | ||
407 | selection.selStart = from ; | |
408 | selection.selEnd = to ; | |
409 | ::SetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
410 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
411 | TESetSelect( from , to , teH ) ; | |
412 | TEDelete( teH ) ; | |
413 | TEInsert( value , value.Length() , teH ) ; | |
414 | Refresh() ; | |
415 | } | |
416 | ||
417 | void wxTextCtrl::Remove(long from, long to) | |
418 | { | |
419 | TEHandle teH ; | |
420 | long size ; | |
421 | ||
422 | ControlEditTextSelectionRec selection ; | |
423 | ||
424 | selection.selStart = from ; | |
425 | selection.selEnd = to ; | |
426 | ::SetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
427 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
428 | TEDelete( teH ) ; | |
429 | Refresh() ; | |
430 | } | |
431 | ||
432 | void wxTextCtrl::SetSelection(long from, long to) | |
433 | { | |
434 | ControlEditTextSelectionRec selection ; | |
435 | TEHandle teH ; | |
436 | long size ; | |
437 | ||
438 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
439 | ||
440 | selection.selStart = from ; | |
441 | selection.selEnd = to ; | |
442 | ||
443 | ::SetControlData( m_macControl , 0, kControlEditTextSelectionTag , sizeof( selection ) , (char*) &selection ) ; | |
444 | TESetSelect( selection.selStart , selection.selEnd , teH ) ; | |
445 | } | |
446 | ||
447 | bool wxTextCtrl::LoadFile(const wxString& file) | |
448 | { | |
449 | if ( wxTextCtrlBase::LoadFile(file) ) | |
450 | { | |
451 | return TRUE; | |
452 | } | |
453 | ||
454 | return FALSE; | |
455 | } | |
456 | ||
457 | void wxTextCtrl::WriteText(const wxString& text) | |
458 | { | |
459 | TEHandle teH ; | |
460 | long size ; | |
461 | ||
462 | memcpy( wxBuffer, text , text.Length() ) ; | |
463 | wxBuffer[text.Length() ] = 0 ; | |
464 | // wxMacConvertNewlines( wxBuffer , wxBuffer ) ; | |
465 | ||
466 | ::GetControlData( m_macControl , 0, kControlEditTextTEHandleTag , sizeof( TEHandle ) , (char*) &teH , &size ) ; | |
467 | ||
468 | TEInsert( wxBuffer , strlen( wxBuffer) , teH ) ; | |
469 | Refresh() ; | |
470 | } | |
471 | ||
472 | void wxTextCtrl::AppendText(const wxString& text) | |
473 | { | |
474 | SetInsertionPointEnd(); | |
475 | WriteText(text); | |
476 | } | |
477 | ||
478 | void wxTextCtrl::Clear() | |
479 | { | |
480 | ::SetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 0 , (char*) ((const char*)NULL) ) ; | |
481 | Refresh() ; | |
482 | } | |
483 | ||
484 | bool wxTextCtrl::IsModified() const | |
485 | { | |
486 | return TRUE; | |
487 | } | |
488 | ||
489 | bool wxTextCtrl::IsEditable() const | |
490 | { | |
491 | return IsEnabled(); | |
492 | } | |
493 | ||
494 | bool wxTextCtrl::AcceptsFocus() const | |
495 | { | |
496 | // we don't want focus if we can't be edited | |
497 | return IsEditable() && wxControl::AcceptsFocus(); | |
498 | } | |
499 | ||
500 | wxSize wxTextCtrl::DoGetBestSize() const | |
501 | { | |
502 | int wText = 100 ; | |
503 | ||
504 | int hText ; | |
505 | if ( UMAHasAppearance() ) | |
506 | hText = 13 ; | |
507 | else | |
508 | hText = 24 ; | |
509 | hText += 2 * m_macHorizontalBorder ; | |
510 | /* | |
511 | int cx, cy; | |
512 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
513 | ||
514 | int wText = DEFAULT_ITEM_WIDTH; | |
515 | ||
516 | int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); | |
517 | ||
518 | return wxSize(wText, hText); | |
519 | */ | |
520 | if ( m_windowStyle & wxTE_MULTILINE ) | |
521 | { | |
522 | hText *= wxMin(GetNumberOfLines(), 5); | |
523 | } | |
524 | //else: for single line control everything is ok | |
525 | return wxSize(wText, hText); | |
526 | } | |
527 | ||
528 | // ---------------------------------------------------------------------------- | |
529 | // Undo/redo | |
530 | // ---------------------------------------------------------------------------- | |
531 | ||
532 | void wxTextCtrl::Undo() | |
533 | { | |
534 | if (CanUndo()) | |
535 | { | |
536 | } | |
537 | } | |
538 | ||
539 | void wxTextCtrl::Redo() | |
540 | { | |
541 | if (CanRedo()) | |
542 | { | |
543 | } | |
544 | } | |
545 | ||
546 | bool wxTextCtrl::CanUndo() const | |
547 | { | |
548 | return FALSE ; | |
549 | } | |
550 | ||
551 | bool wxTextCtrl::CanRedo() const | |
552 | { | |
553 | return FALSE ; | |
554 | } | |
555 | ||
556 | // Makes 'unmodified' | |
557 | void wxTextCtrl::DiscardEdits() | |
558 | { | |
559 | // TODO | |
560 | } | |
561 | ||
562 | int wxTextCtrl::GetNumberOfLines() const | |
563 | { | |
564 | Size actualsize; | |
565 | ::GetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ; | |
566 | ||
567 | int count = 1; | |
568 | for (int i = 0; i < actualsize; i++) | |
569 | { | |
570 | if (wxBuffer[i] == '\r') count++; | |
571 | } | |
572 | ||
573 | return count; | |
574 | } | |
575 | ||
576 | long wxTextCtrl::XYToPosition(long x, long y) const | |
577 | { | |
578 | // TODO | |
579 | return 0; | |
580 | } | |
581 | ||
582 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
583 | { | |
584 | return FALSE ; | |
585 | } | |
586 | ||
587 | void wxTextCtrl::ShowPosition(long pos) | |
588 | { | |
589 | // TODO | |
590 | } | |
591 | ||
592 | int wxTextCtrl::GetLineLength(long lineNo) const | |
593 | { | |
594 | Size actualsize; | |
595 | ::GetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ; | |
596 | ||
597 | // Find line first | |
598 | int count = 0; | |
599 | for (int i = 0; i < actualsize; i++) | |
600 | { | |
601 | if (count == lineNo) | |
602 | { | |
603 | // Count chars in line then | |
604 | count = 0; | |
605 | for (int j = i; j < actualsize; j++) | |
606 | { | |
607 | count++; | |
608 | if (wxBuffer[j] == '\r') return count; | |
609 | } | |
610 | ||
611 | return count; | |
612 | } | |
613 | if (wxBuffer[i] == '\r') count++; | |
614 | } | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
619 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
620 | { | |
621 | Size actualsize; | |
622 | ::GetControlData( m_macControl, 0, ( m_windowStyle & wxTE_PASSWORD ) ? kControlEditTextPasswordTag : kControlEditTextTextTag , 32767 , wxBuffer , &actualsize) ; | |
623 | ||
624 | // Find line first | |
625 | int count = 0; | |
626 | for (int i = 0; i < actualsize; i++) | |
627 | { | |
628 | if (count == lineNo) | |
629 | { | |
630 | // Add chars in line then | |
631 | wxString tmp(""); | |
632 | ||
633 | for (int j = i; j < actualsize; j++) | |
634 | { | |
635 | if (wxBuffer[j] == '\r') | |
636 | return tmp; | |
637 | ||
638 | tmp += wxBuffer[j]; | |
639 | } | |
640 | ||
641 | return tmp; | |
642 | } | |
643 | if (wxBuffer[i] == '\r') count++; | |
644 | } | |
645 | ||
646 | return wxString(""); | |
647 | } | |
648 | ||
649 | /* | |
650 | * Text item | |
651 | */ | |
652 | ||
653 | void wxTextCtrl::Command(wxCommandEvent & event) | |
654 | { | |
655 | SetValue (event.GetString()); | |
656 | ProcessCommand (event); | |
657 | } | |
658 | ||
659 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
660 | { | |
661 | // By default, load the first file into the text window. | |
662 | if (event.GetNumberOfFiles() > 0) | |
663 | { | |
664 | LoadFile(event.GetFiles()[0]); | |
665 | } | |
666 | } | |
667 | ||
668 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
669 | { | |
670 | switch ( event.KeyCode() ) | |
671 | { | |
672 | case WXK_RETURN: | |
673 | if (m_windowStyle & wxPROCESS_ENTER) | |
674 | { | |
675 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
676 | event.SetEventObject( this ); | |
677 | if ( GetEventHandler()->ProcessEvent(event) ) | |
678 | return; | |
679 | } | |
680 | if ( !(m_windowStyle & wxTE_MULTILINE) ) | |
681 | { | |
682 | wxWindow *parent = GetParent(); | |
683 | wxPanel *panel = wxDynamicCast(parent, wxPanel); | |
684 | while ( parent != NULL && panel == NULL ) | |
685 | { | |
686 | parent = parent->GetParent() ; | |
687 | panel = wxDynamicCast(parent, wxPanel); | |
688 | } | |
689 | if ( panel && panel->GetDefaultItem() ) | |
690 | { | |
691 | wxButton *def = wxDynamicCast(panel->GetDefaultItem(), | |
692 | wxButton); | |
693 | if ( def && def->IsEnabled() ) | |
694 | { | |
695 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
696 | event.SetEventObject(def); | |
697 | def->Command(event); | |
698 | return ; | |
699 | } | |
700 | } | |
701 | } | |
702 | //else: multiline controls need Enter for themselves | |
703 | ||
704 | break; | |
705 | ||
706 | case WXK_TAB: | |
707 | // always produce navigation event - even if we process TAB | |
708 | // ourselves the fact that we got here means that the user code | |
709 | // decided to skip processing of this TAB - probably to let it | |
710 | // do its default job. | |
711 | { | |
712 | wxNavigationKeyEvent eventNav; | |
713 | eventNav.SetDirection(!event.ShiftDown()); | |
714 | eventNav.SetWindowChange(event.ControlDown()); | |
715 | eventNav.SetEventObject(this); | |
716 | ||
717 | if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) | |
718 | return; | |
719 | event.Skip() ; | |
720 | return ; | |
721 | } | |
722 | break; | |
723 | } | |
724 | ||
725 | EventRecord *ev = wxTheApp->MacGetCurrentEvent() ; | |
726 | short keycode ; | |
727 | short keychar ; | |
728 | keychar = short(ev->message & charCodeMask); | |
729 | keycode = short(ev->message & keyCodeMask) >> 8 ; | |
730 | ::HandleControlKey( m_macControl , keycode , keychar , ev->modifiers ) ; | |
731 | if ( keychar >= 0x20 || event.KeyCode() == WXK_RETURN || event.KeyCode() == WXK_DELETE || event.KeyCode() == WXK_BACK) | |
732 | { | |
733 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
734 | event.SetString( GetValue() ) ; | |
735 | event.SetEventObject( this ); | |
736 | GetEventHandler()->ProcessEvent(event); | |
737 | } | |
738 | ||
739 | } | |
740 | ||
741 | // ---------------------------------------------------------------------------- | |
742 | // standard handlers for standard edit menu events | |
743 | // ---------------------------------------------------------------------------- | |
744 | ||
745 | void wxTextCtrl::OnCut(wxCommandEvent& event) | |
746 | { | |
747 | Cut(); | |
748 | } | |
749 | ||
750 | void wxTextCtrl::OnCopy(wxCommandEvent& event) | |
751 | { | |
752 | Copy(); | |
753 | } | |
754 | ||
755 | void wxTextCtrl::OnPaste(wxCommandEvent& event) | |
756 | { | |
757 | Paste(); | |
758 | } | |
759 | ||
760 | void wxTextCtrl::OnUndo(wxCommandEvent& event) | |
761 | { | |
762 | Undo(); | |
763 | } | |
764 | ||
765 | void wxTextCtrl::OnRedo(wxCommandEvent& event) | |
766 | { | |
767 | Redo(); | |
768 | } | |
769 | ||
770 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
771 | { | |
772 | event.Enable( CanCut() ); | |
773 | } | |
774 | ||
775 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
776 | { | |
777 | event.Enable( CanCopy() ); | |
778 | } | |
779 | ||
780 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
781 | { | |
782 | event.Enable( CanPaste() ); | |
783 | } | |
784 | ||
785 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
786 | { | |
787 | event.Enable( CanUndo() ); | |
788 | } | |
789 | ||
790 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
791 | { | |
792 | event.Enable( CanRedo() ); | |
793 | } | |
794 | ||
795 | #else | |
796 | ||
797 | #if !TARGET_CARBON | |
798 | #define GetControlOwner( control ) (**control).contrlOwner | |
799 | #endif | |
800 | ||
801 | //todo add access to global event record | |
802 | ||
803 | EventRecord event ; | |
804 | ||
805 | static EventRecord *GetCurrentEventRecord() | |
806 | { | |
807 | return &event ; | |
808 | } | |
809 | ||
810 | // CS:We will replace the TextEdit by using the MultiLanguageTextEngine based on the following code written by apple | |
811 | ||
812 | /* | |
813 | File: mUPControl.c | |
814 | ||
815 | Description: | |
816 | mUPControl implementation. | |
817 | ||
818 | Copyright: | |
819 |