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