]>
Commit | Line | Data |
---|---|---|
524c47aa SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/textctrl_osx.cpp | |
3 | // Purpose: wxTextCtrl | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText) | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_TEXTCTRL | |
15 | ||
16 | #include "wx/textctrl.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/intl.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/utils.h" | |
22 | #include "wx/dc.h" | |
23 | #include "wx/button.h" | |
24 | #include "wx/menu.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/msgdlg.h" | |
27 | #include "wx/toplevel.h" | |
28 | #endif | |
29 | ||
30 | #ifdef __DARWIN__ | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #else | |
34 | #include <stat.h> | |
35 | #endif | |
36 | ||
37 | #if wxUSE_STD_IOSTREAM | |
38 | #if wxUSE_IOSTREAMH | |
39 | #include <fstream.h> | |
40 | #else | |
41 | #include <fstream> | |
42 | #endif | |
43 | #endif | |
44 | ||
45 | #include "wx/filefn.h" | |
46 | #include "wx/sysopt.h" | |
47 | #include "wx/thread.h" | |
48 | ||
49 | #include "wx/osx/private.h" | |
524c47aa SC |
50 | |
51 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) | |
52 | ||
53 | BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) | |
54 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
55 | EVT_CHAR(wxTextCtrl::OnChar) | |
56 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
57 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
58 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
59 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
60 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
61 | EVT_MENU(wxID_CLEAR, wxTextCtrl::OnDelete) | |
62 | EVT_MENU(wxID_SELECTALL, wxTextCtrl::OnSelectAll) | |
63 | ||
64 | EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu) | |
65 | ||
66 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
67 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
68 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
69 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
70 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
71 | EVT_UPDATE_UI(wxID_CLEAR, wxTextCtrl::OnUpdateDelete) | |
72 | EVT_UPDATE_UI(wxID_SELECTALL, wxTextCtrl::OnUpdateSelectAll) | |
73 | END_EVENT_TABLE() | |
74 | ||
75 | ||
76 | void wxTextCtrl::Init() | |
77 | { | |
78 | m_editable = true ; | |
79 | m_dirty = false; | |
80 | ||
81 | m_maxLength = 0; | |
82 | m_privateContextMenu = NULL; | |
d9d551f6 | 83 | m_triggerUpdateEvents = true ; |
524c47aa SC |
84 | } |
85 | ||
86 | wxTextCtrl::~wxTextCtrl() | |
87 | { | |
88 | #if wxUSE_MENUS | |
89 | delete m_privateContextMenu; | |
90 | #endif | |
91 | } | |
92 | ||
93 | bool wxTextCtrl::Create( wxWindow *parent, | |
94 | wxWindowID id, | |
95 | const wxString& str, | |
96 | const wxPoint& pos, | |
97 | const wxSize& size, | |
98 | long style, | |
99 | const wxValidator& validator, | |
100 | const wxString& name ) | |
101 | { | |
102 | m_macIsUserPane = false ; | |
103 | m_editable = true ; | |
104 | ||
105 | if ( ! (style & wxNO_BORDER) ) | |
106 | style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ; | |
107 | ||
108 | if ( !wxTextCtrlBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) ) | |
109 | return false; | |
110 | ||
111 | if ( m_windowStyle & wxTE_MULTILINE ) | |
112 | { | |
113 | // always turn on this style for multi-line controls | |
114 | m_windowStyle |= wxTE_PROCESS_ENTER; | |
115 | style |= wxTE_PROCESS_ENTER ; | |
116 | } | |
117 | ||
524c47aa | 118 | |
1e181c7a | 119 | m_peer = wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str, pos, size, style, GetExtraStyle() ); |
524c47aa SC |
120 | |
121 | MacPostControlCreate(pos, size) ; | |
122 | ||
123 | // only now the embedding is correct and we can do a positioning update | |
124 | ||
125 | MacSuperChangedPosition() ; | |
126 | ||
127 | if ( m_windowStyle & wxTE_READONLY) | |
128 | SetEditable( false ) ; | |
129 | ||
130 | SetCursor( wxCursor( wxCURSOR_IBEAM ) ) ; | |
131 | ||
132 | return true; | |
133 | } | |
134 | ||
1e181c7a | 135 | wxTextWidgetImpl* wxTextCtrl::GetTextPeer() const |
524c47aa | 136 | { |
1e181c7a | 137 | return dynamic_cast<wxTextWidgetImpl*> (m_peer); |
524c47aa SC |
138 | } |
139 | ||
140 | void wxTextCtrl::MacSuperChangedPosition() | |
141 | { | |
142 | wxWindow::MacSuperChangedPosition() ; | |
143 | #if wxOSX_USE_CARBON | |
144 | GetPeer()->SuperChangedPosition() ; | |
145 | #endif | |
146 | } | |
147 | ||
148 | void wxTextCtrl::MacVisibilityChanged() | |
149 | { | |
150 | #if wxOSX_USE_CARBON | |
151 | GetPeer()->VisibilityChanged( GetPeer()->IsVisible() ); | |
152 | #endif | |
153 | } | |
154 | ||
155 | void wxTextCtrl::MacCheckSpelling(bool check) | |
156 | { | |
1e181c7a | 157 | GetTextPeer()->CheckSpelling(check); |
524c47aa SC |
158 | } |
159 | ||
33b3ade5 | 160 | wxString wxTextCtrl::DoGetValue() const |
524c47aa | 161 | { |
1e181c7a | 162 | return GetTextPeer()->GetStringValue() ; |
524c47aa SC |
163 | } |
164 | ||
165 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
166 | { | |
1e181c7a | 167 | GetTextPeer()->GetSelection( from , to ) ; |
524c47aa SC |
168 | } |
169 | ||
524c47aa SC |
170 | void wxTextCtrl::SetMaxLength(unsigned long len) |
171 | { | |
172 | m_maxLength = len ; | |
173 | } | |
174 | ||
175 | bool wxTextCtrl::SetFont( const wxFont& font ) | |
176 | { | |
177 | if ( !wxTextCtrlBase::SetFont( font ) ) | |
178 | return false ; | |
179 | ||
1e181c7a | 180 | GetPeer()->SetFont( font , GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ; |
524c47aa SC |
181 | |
182 | return true ; | |
183 | } | |
184 | ||
185 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
186 | { | |
1e181c7a | 187 | GetTextPeer()->SetStyle( start , end , style ) ; |
524c47aa SC |
188 | |
189 | return true ; | |
190 | } | |
191 | ||
192 | bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) | |
193 | { | |
194 | wxTextCtrlBase::SetDefaultStyle( style ) ; | |
195 | SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ; | |
196 | ||
197 | return true ; | |
198 | } | |
199 | ||
200 | // Clipboard operations | |
201 | ||
202 | void wxTextCtrl::Copy() | |
203 | { | |
204 | if (CanCopy()) | |
1e181c7a | 205 | GetTextPeer()->Copy() ; |
524c47aa SC |
206 | } |
207 | ||
208 | void wxTextCtrl::Cut() | |
209 | { | |
210 | if (CanCut()) | |
211 | { | |
1e181c7a | 212 | GetTextPeer()->Cut() ; |
524c47aa | 213 | |
d9d551f6 SC |
214 | SendTextUpdatedEvent(); |
215 | } | |
524c47aa SC |
216 | } |
217 | ||
218 | void wxTextCtrl::Paste() | |
219 | { | |
220 | if (CanPaste()) | |
221 | { | |
1e181c7a | 222 | GetTextPeer()->Paste() ; |
524c47aa SC |
223 | |
224 | // TODO: eventually we should add setting the default style again | |
d9d551f6 | 225 | SendTextUpdatedEvent(); |
524c47aa SC |
226 | } |
227 | } | |
228 | ||
229 | bool wxTextCtrl::CanCopy() const | |
230 | { | |
231 | // Can copy if there's a selection | |
232 | long from, to; | |
233 | GetSelection( &from, &to ); | |
234 | ||
235 | return (from != to); | |
236 | } | |
237 | ||
238 | bool wxTextCtrl::CanCut() const | |
239 | { | |
240 | if ( !IsEditable() ) | |
241 | return false; | |
242 | ||
243 | // Can cut if there's a selection | |
244 | long from, to; | |
245 | GetSelection( &from, &to ); | |
246 | ||
247 | return (from != to); | |
248 | } | |
249 | ||
250 | bool wxTextCtrl::CanPaste() const | |
251 | { | |
252 | if (!IsEditable()) | |
253 | return false; | |
254 | ||
1e181c7a | 255 | return GetTextPeer()->CanPaste() ; |
524c47aa SC |
256 | } |
257 | ||
258 | void wxTextCtrl::SetEditable(bool editable) | |
259 | { | |
260 | if ( editable != m_editable ) | |
261 | { | |
262 | m_editable = editable ; | |
1e181c7a | 263 | GetTextPeer()->SetEditable( editable ) ; |
524c47aa SC |
264 | } |
265 | } | |
266 | ||
267 | void wxTextCtrl::SetInsertionPoint(long pos) | |
268 | { | |
269 | SetSelection( pos , pos ) ; | |
270 | } | |
271 | ||
272 | void wxTextCtrl::SetInsertionPointEnd() | |
273 | { | |
0b6a49c2 | 274 | long pos = GetLastPosition(); |
524c47aa SC |
275 | SetInsertionPoint( pos ); |
276 | } | |
277 | ||
278 | long wxTextCtrl::GetInsertionPoint() const | |
279 | { | |
280 | long begin, end ; | |
281 | GetSelection( &begin , &end ) ; | |
282 | ||
283 | return begin ; | |
284 | } | |
285 | ||
286 | wxTextPos wxTextCtrl::GetLastPosition() const | |
287 | { | |
1e181c7a | 288 | return GetTextPeer()->GetLastPosition() ; |
524c47aa SC |
289 | } |
290 | ||
524c47aa SC |
291 | void wxTextCtrl::Remove(long from, long to) |
292 | { | |
1e181c7a | 293 | GetTextPeer()->Remove( from , to ) ; |
d9d551f6 SC |
294 | if ( m_triggerUpdateEvents ) |
295 | SendTextUpdatedEvent(); | |
524c47aa SC |
296 | } |
297 | ||
298 | void wxTextCtrl::SetSelection(long from, long to) | |
299 | { | |
1e181c7a | 300 | GetTextPeer()->SetSelection( from , to ) ; |
524c47aa SC |
301 | } |
302 | ||
303 | void wxTextCtrl::WriteText(const wxString& str) | |
304 | { | |
1e181c7a | 305 | GetTextPeer()->WriteText( str ) ; |
d9d551f6 SC |
306 | if ( m_triggerUpdateEvents ) |
307 | SendTextUpdatedEvent(); | |
524c47aa SC |
308 | } |
309 | ||
310 | void wxTextCtrl::Clear() | |
311 | { | |
1e181c7a | 312 | GetTextPeer()->Clear() ; |
d9d551f6 | 313 | SendTextUpdatedEvent(); |
524c47aa SC |
314 | } |
315 | ||
316 | bool wxTextCtrl::IsModified() const | |
317 | { | |
318 | return m_dirty; | |
319 | } | |
320 | ||
321 | bool wxTextCtrl::IsEditable() const | |
322 | { | |
323 | return IsEnabled() && m_editable ; | |
324 | } | |
325 | ||
326 | bool wxTextCtrl::AcceptsFocus() const | |
327 | { | |
328 | // we don't want focus if we can't be edited | |
329 | return /*IsEditable() && */ wxControl::AcceptsFocus(); | |
330 | } | |
331 | ||
332 | wxSize wxTextCtrl::DoGetBestSize() const | |
333 | { | |
334 | int wText, hText; | |
335 | ||
336 | // these are the numbers from the HIG: | |
337 | // we reduce them by the borders first | |
338 | wText = 100 ; | |
339 | ||
340 | switch ( m_windowVariant ) | |
341 | { | |
342 | case wxWINDOW_VARIANT_NORMAL : | |
343 | hText = 22 - 6 ; | |
344 | break ; | |
345 | ||
346 | case wxWINDOW_VARIANT_SMALL : | |
347 | hText = 19 - 6 ; | |
348 | break ; | |
349 | ||
350 | case wxWINDOW_VARIANT_MINI : | |
351 | hText = 15 - 6 ; | |
352 | break ; | |
353 | ||
354 | default : | |
355 | hText = 22 - 6; | |
356 | break ; | |
357 | } | |
358 | ||
359 | // as the above numbers have some free space around the text | |
360 | // we get 5 lines like this anyway | |
361 | if ( m_windowStyle & wxTE_MULTILINE ) | |
362 | hText *= 5 ; | |
363 | ||
364 | if ( !HasFlag(wxNO_BORDER) ) | |
365 | hText += 6 ; | |
366 | ||
367 | return wxSize(wText, hText); | |
368 | } | |
369 | ||
370 | // ---------------------------------------------------------------------------- | |
371 | // Undo/redo | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
374 | void wxTextCtrl::Undo() | |
375 | { | |
376 | if (CanUndo()) | |
1e181c7a | 377 | GetTextPeer()->Undo() ; |
524c47aa SC |
378 | } |
379 | ||
380 | void wxTextCtrl::Redo() | |
381 | { | |
382 | if (CanRedo()) | |
1e181c7a | 383 | GetTextPeer()->Redo() ; |
524c47aa SC |
384 | } |
385 | ||
386 | bool wxTextCtrl::CanUndo() const | |
387 | { | |
388 | if ( !IsEditable() ) | |
389 | return false ; | |
390 | ||
1e181c7a | 391 | return GetTextPeer()->CanUndo() ; |
524c47aa SC |
392 | } |
393 | ||
394 | bool wxTextCtrl::CanRedo() const | |
395 | { | |
396 | if ( !IsEditable() ) | |
397 | return false ; | |
398 | ||
1e181c7a | 399 | return GetTextPeer()->CanRedo() ; |
524c47aa SC |
400 | } |
401 | ||
402 | void wxTextCtrl::MarkDirty() | |
403 | { | |
404 | m_dirty = true; | |
405 | } | |
406 | ||
407 | void wxTextCtrl::DiscardEdits() | |
408 | { | |
409 | m_dirty = false; | |
410 | } | |
411 | ||
412 | int wxTextCtrl::GetNumberOfLines() const | |
413 | { | |
1e181c7a | 414 | return GetTextPeer()->GetNumberOfLines() ; |
524c47aa SC |
415 | } |
416 | ||
417 | long wxTextCtrl::XYToPosition(long x, long y) const | |
418 | { | |
1e181c7a | 419 | return GetTextPeer()->XYToPosition( x , y ) ; |
524c47aa SC |
420 | } |
421 | ||
422 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
423 | { | |
1e181c7a | 424 | return GetTextPeer()->PositionToXY( pos , x , y ) ; |
524c47aa SC |
425 | } |
426 | ||
427 | void wxTextCtrl::ShowPosition(long pos) | |
428 | { | |
1e181c7a | 429 | return GetTextPeer()->ShowPosition(pos) ; |
524c47aa SC |
430 | } |
431 | ||
432 | int wxTextCtrl::GetLineLength(long lineNo) const | |
433 | { | |
1e181c7a | 434 | return GetTextPeer()->GetLineLength(lineNo) ; |
524c47aa SC |
435 | } |
436 | ||
437 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
438 | { | |
1e181c7a | 439 | return GetTextPeer()->GetLineText(lineNo) ; |
524c47aa SC |
440 | } |
441 | ||
442 | void wxTextCtrl::Command(wxCommandEvent & event) | |
443 | { | |
444 | SetValue(event.GetString()); | |
445 | ProcessCommand(event); | |
446 | } | |
447 | ||
448 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
449 | { | |
450 | // By default, load the first file into the text window. | |
451 | if (event.GetNumberOfFiles() > 0) | |
452 | LoadFile( event.GetFiles()[0] ); | |
453 | } | |
454 | ||
455 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
456 | { | |
457 | int key = event.GetKeyCode() ; | |
458 | bool eat_key = false ; | |
459 | long from, to; | |
460 | ||
461 | if ( key == 'a' && event.MetaDown() ) | |
462 | { | |
463 | SelectAll() ; | |
464 | ||
465 | return ; | |
466 | } | |
467 | ||
468 | if ( key == 'c' && event.MetaDown() ) | |
469 | { | |
470 | if ( CanCopy() ) | |
471 | Copy() ; | |
472 | ||
473 | return ; | |
474 | } | |
475 | ||
476 | if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB && | |
477 | !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) ) | |
478 | // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END | |
479 | ) | |
480 | { | |
481 | // eat it | |
482 | return ; | |
483 | } | |
484 | ||
485 | // Check if we have reached the max # of chars (if it is set), but still | |
486 | // allow navigation and deletion | |
487 | GetSelection( &from, &to ); | |
488 | if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength && | |
489 | key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && key != WXK_UP && key != WXK_DOWN && | |
490 | key != WXK_BACK && key != WXK_DELETE && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) && | |
491 | from == to ) | |
492 | { | |
493 | // eat it, we don't want to add more than allowed # of characters | |
494 | ||
495 | // TODO: generate EVT_TEXT_MAXLEN() | |
496 | return; | |
497 | } | |
498 | ||
499 | // assume that any key not processed yet is going to modify the control | |
500 | m_dirty = true; | |
501 | ||
502 | if ( key == 'v' && event.MetaDown() ) | |
503 | { | |
504 | if ( CanPaste() ) | |
505 | Paste() ; | |
506 | ||
507 | return ; | |
508 | } | |
509 | ||
510 | if ( key == 'x' && event.MetaDown() ) | |
511 | { | |
512 | if ( CanCut() ) | |
513 | Cut() ; | |
514 | ||
515 | return ; | |
516 | } | |
517 | ||
518 | switch ( key ) | |
519 | { | |
520 | case WXK_RETURN: | |
521 | if (m_windowStyle & wxTE_PROCESS_ENTER) | |
522 | { | |
523 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
524 | event.SetEventObject( this ); | |
525 | event.SetString( GetValue() ); | |
526 | if ( HandleWindowEvent(event) ) | |
527 | return; | |
528 | } | |
529 | ||
530 | if ( !(m_windowStyle & wxTE_MULTILINE) ) | |
531 | { | |
532 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); | |
533 | if ( tlw && tlw->GetDefaultItem() ) | |
534 | { | |
535 | wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); | |
536 | if ( def && def->IsEnabled() ) | |
537 | { | |
538 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
539 | event.SetEventObject(def); | |
540 | def->Command(event); | |
541 | ||
542 | return ; | |
543 | } | |
544 | } | |
545 | ||
546 | // this will make wxWidgets eat the ENTER key so that | |
547 | // we actually prevent line wrapping in a single line text control | |
548 | eat_key = true; | |
549 | } | |
550 | break; | |
551 | ||
552 | case WXK_TAB: | |
553 | if ( !(m_windowStyle & wxTE_PROCESS_TAB)) | |
554 | { | |
555 | int flags = 0; | |
556 | if (!event.ShiftDown()) | |
557 | flags |= wxNavigationKeyEvent::IsForward ; | |
558 | if (event.ControlDown()) | |
559 | flags |= wxNavigationKeyEvent::WinChange ; | |
560 | Navigate(flags); | |
561 | ||
562 | return; | |
563 | } | |
564 | else | |
565 | { | |
566 | // This is necessary (don't know why); | |
567 | // otherwise the tab will not be inserted. | |
568 | WriteText(wxT("\t")); | |
569 | eat_key = true; | |
570 | } | |
571 | break; | |
572 | ||
573 | default: | |
574 | break; | |
575 | } | |
576 | ||
577 | if (!eat_key) | |
578 | { | |
579 | // perform keystroke handling | |
580 | event.Skip(true) ; | |
581 | } | |
582 | ||
583 | if ( ( key >= 0x20 && key < WXK_START ) || | |
584 | ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) || | |
585 | key == WXK_RETURN || | |
586 | key == WXK_DELETE || | |
587 | key == WXK_BACK) | |
588 | { | |
589 | wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); | |
590 | event1.SetEventObject( this ); | |
591 | wxPostEvent( GetEventHandler(), event1 ); | |
592 | } | |
593 | } | |
594 | ||
595 | // ---------------------------------------------------------------------------- | |
596 | // standard handlers for standard edit menu events | |
597 | // ---------------------------------------------------------------------------- | |
598 | ||
599 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
600 | { | |
601 | Cut(); | |
602 | } | |
603 | ||
604 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
605 | { | |
606 | Copy(); | |
607 | } | |
608 | ||
609 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
610 | { | |
611 | Paste(); | |
612 | } | |
613 | ||
614 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
615 | { | |
616 | Undo(); | |
617 | } | |
618 | ||
619 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
620 | { | |
621 | Redo(); | |
622 | } | |
623 | ||
624 | void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
625 | { | |
626 | long from, to; | |
627 | ||
628 | GetSelection( &from, &to ); | |
629 | if (from != -1 && to != -1) | |
630 | Remove( from, to ); | |
631 | } | |
632 | ||
633 | void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
634 | { | |
635 | SetSelection(-1, -1); | |
636 | } | |
637 | ||
638 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
639 | { | |
640 | event.Enable( CanCut() ); | |
641 | } | |
642 | ||
643 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
644 | { | |
645 | event.Enable( CanCopy() ); | |
646 | } | |
647 | ||
648 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
649 | { | |
650 | event.Enable( CanPaste() ); | |
651 | } | |
652 | ||
653 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
654 | { | |
655 | event.Enable( CanUndo() ); | |
656 | } | |
657 | ||
658 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
659 | { | |
660 | event.Enable( CanRedo() ); | |
661 | } | |
662 | ||
663 | void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent& event) | |
664 | { | |
665 | long from, to; | |
666 | ||
667 | GetSelection( &from, &to ); | |
668 | event.Enable( from != -1 && to != -1 && from != to && IsEditable() ) ; | |
669 | } | |
670 | ||
671 | void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
672 | { | |
673 | event.Enable(GetLastPosition() > 0); | |
674 | } | |
675 | ||
676 | // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment | |
677 | ||
678 | void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event) | |
679 | { | |
1e181c7a | 680 | if ( GetTextPeer()->HasOwnContextMenu() ) |
524c47aa SC |
681 | { |
682 | event.Skip() ; | |
683 | return ; | |
684 | } | |
685 | ||
686 | #if wxUSE_MENUS | |
687 | if (m_privateContextMenu == NULL) | |
688 | { | |
689 | m_privateContextMenu = new wxMenu; | |
690 | m_privateContextMenu->Append(wxID_UNDO, _("&Undo")); | |
691 | m_privateContextMenu->Append(wxID_REDO, _("&Redo")); | |
692 | m_privateContextMenu->AppendSeparator(); | |
693 | m_privateContextMenu->Append(wxID_CUT, _("Cu&t")); | |
694 | m_privateContextMenu->Append(wxID_COPY, _("&Copy")); | |
695 | m_privateContextMenu->Append(wxID_PASTE, _("&Paste")); | |
696 | m_privateContextMenu->Append(wxID_CLEAR, _("&Delete")); | |
697 | m_privateContextMenu->AppendSeparator(); | |
698 | m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
699 | } | |
700 | ||
701 | if (m_privateContextMenu != NULL) | |
702 | PopupMenu(m_privateContextMenu); | |
703 | #endif | |
704 | } | |
705 | ||
706 | bool wxTextCtrl::MacSetupCursor( const wxPoint& pt ) | |
707 | { | |
1e181c7a | 708 | if ( !GetTextPeer()->SetupCursor( pt ) ) |
524c47aa SC |
709 | return wxWindow::MacSetupCursor( pt ) ; |
710 | else | |
711 | return true ; | |
712 | } | |
713 | ||
714 | // ---------------------------------------------------------------------------- | |
715 | // implementation base class | |
716 | // ---------------------------------------------------------------------------- | |
717 | ||
1e181c7a | 718 | void wxTextWidgetImpl::SetStyle(long WXUNUSED(start), |
524c47aa SC |
719 | long WXUNUSED(end), |
720 | const wxTextAttr& WXUNUSED(style)) | |
721 | { | |
722 | } | |
723 | ||
1e181c7a | 724 | void wxTextWidgetImpl::Copy() |
524c47aa SC |
725 | { |
726 | } | |
727 | ||
1e181c7a | 728 | void wxTextWidgetImpl::Cut() |
524c47aa SC |
729 | { |
730 | } | |
731 | ||
1e181c7a | 732 | void wxTextWidgetImpl::Paste() |
524c47aa SC |
733 | { |
734 | } | |
735 | ||
1e181c7a | 736 | bool wxTextWidgetImpl::CanPaste() const |
524c47aa SC |
737 | { |
738 | return false ; | |
739 | } | |
740 | ||
1e181c7a | 741 | void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable)) |
524c47aa SC |
742 | { |
743 | } | |
744 | ||
0b6a49c2 | 745 | long wxTextWidgetImpl::GetLastPosition() const |
524c47aa SC |
746 | { |
747 | return GetStringValue().length() ; | |
748 | } | |
749 | ||
1e181c7a | 750 | void wxTextWidgetImpl::Replace( long from , long to , const wxString &val ) |
524c47aa SC |
751 | { |
752 | SetSelection( from , to ) ; | |
753 | WriteText( val ) ; | |
754 | } | |
755 | ||
1e181c7a | 756 | void wxTextWidgetImpl::Remove( long from , long to ) |
524c47aa SC |
757 | { |
758 | SetSelection( from , to ) ; | |
759 | WriteText( wxEmptyString) ; | |
760 | } | |
761 | ||
1e181c7a | 762 | void wxTextWidgetImpl::Clear() |
524c47aa SC |
763 | { |
764 | SetStringValue( wxEmptyString ) ; | |
765 | } | |
766 | ||
1e181c7a | 767 | bool wxTextWidgetImpl::CanUndo() const |
524c47aa SC |
768 | { |
769 | return false ; | |
770 | } | |
771 | ||
1e181c7a | 772 | void wxTextWidgetImpl::Undo() |
524c47aa SC |
773 | { |
774 | } | |
775 | ||
1e181c7a | 776 | bool wxTextWidgetImpl::CanRedo() const |
524c47aa SC |
777 | { |
778 | return false ; | |
779 | } | |
780 | ||
1e181c7a | 781 | void wxTextWidgetImpl::Redo() |
524c47aa SC |
782 | { |
783 | } | |
784 | ||
1e181c7a | 785 | long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x), long WXUNUSED(y)) const |
524c47aa SC |
786 | { |
787 | return 0 ; | |
788 | } | |
789 | ||
1e181c7a | 790 | bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos), |
524c47aa SC |
791 | long *WXUNUSED(x), |
792 | long *WXUNUSED(y)) const | |
793 | { | |
794 | return false ; | |
795 | } | |
796 | ||
1e181c7a | 797 | void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos) ) |
524c47aa SC |
798 | { |
799 | } | |
800 | ||
1e181c7a | 801 | int wxTextWidgetImpl::GetNumberOfLines() const |
524c47aa SC |
802 | { |
803 | ItemCount lines = 0 ; | |
804 | wxString content = GetStringValue() ; | |
805 | lines = 1; | |
806 | ||
807 | for (size_t i = 0; i < content.length() ; i++) | |
808 | { | |
809 | if (content[i] == '\r') | |
810 | lines++; | |
811 | } | |
812 | ||
813 | return lines ; | |
814 | } | |
815 | ||
1e181c7a | 816 | wxString wxTextWidgetImpl::GetLineText(long lineNo) const |
524c47aa SC |
817 | { |
818 | // TODO: change this if possible to reflect real lines | |
819 | wxString content = GetStringValue() ; | |
820 | ||
821 | // Find line first | |
822 | int count = 0; | |
823 | for (size_t i = 0; i < content.length() ; i++) | |
824 | { | |
825 | if (count == lineNo) | |
826 | { | |
827 | // Add chars in line then | |
828 | wxString tmp; | |
829 | ||
830 | for (size_t j = i; j < content.length(); j++) | |
831 | { | |
832 | if (content[j] == '\n') | |
833 | return tmp; | |
834 | ||
835 | tmp += content[j]; | |
836 | } | |
837 | ||
838 | return tmp; | |
839 | } | |
840 | ||
841 | if (content[i] == '\n') | |
842 | count++; | |
843 | } | |
844 | ||
845 | return wxEmptyString ; | |
846 | } | |
847 | ||
1e181c7a | 848 | int wxTextWidgetImpl::GetLineLength(long lineNo) const |
524c47aa SC |
849 | { |
850 | // TODO: change this if possible to reflect real lines | |
851 | wxString content = GetStringValue() ; | |
852 | ||
853 | // Find line first | |
854 | int count = 0; | |
855 | for (size_t i = 0; i < content.length() ; i++) | |
856 | { | |
857 | if (count == lineNo) | |
858 | { | |
859 | // Count chars in line then | |
860 | count = 0; | |
861 | for (size_t j = i; j < content.length(); j++) | |
862 | { | |
863 | count++; | |
864 | if (content[j] == '\n') | |
865 | return count; | |
866 | } | |
867 | ||
868 | return count; | |
869 | } | |
870 | ||
871 | if (content[i] == '\n') | |
872 | count++; | |
873 | } | |
874 | ||
875 | return 0 ; | |
876 | } | |
877 | ||
524c47aa | 878 | #endif // wxUSE_TEXTCTRL |