]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "textctrl.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/textctrl.h" | |
15 | #include "wx/utils.h" | |
16 | #include "wx/intl.h" | |
17 | #include "wx/log.h" | |
18 | #include "wx/settings.h" | |
19 | #include "wx/panel.h" | |
20 | #include "wx/clipbrd.h" | |
21 | #include "wx/tokenzr.h" | |
22 | #include "wx/dcclient.h" | |
23 | ||
24 | #include "wx/univ/inphand.h" | |
25 | #include "wx/univ/renderer.h" | |
26 | #include "wx/univ/colschem.h" | |
27 | #include "wx/univ/theme.h" | |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // helpers | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type, int y1, int y2, wxTextCtrl *owner ) | |
34 | { | |
35 | m_type = type; | |
36 | m_y1 = y1; | |
37 | m_y2 = y2; | |
38 | m_owner = owner; | |
39 | ||
40 | m_cursorX = m_owner->GetCursorX(); | |
41 | m_cursorY = m_owner->GetCursorY(); | |
42 | ||
43 | if (m_type == wxSOURCE_UNDO_LINE) | |
44 | { | |
45 | m_text = m_owner->m_lines[m_y1].m_text; | |
46 | } else | |
47 | if (m_type == wxSOURCE_UNDO_ENTER) | |
48 | { | |
49 | m_text = m_owner->m_lines[m_y1].m_text; | |
50 | } else | |
51 | if (m_type == wxSOURCE_UNDO_BACK) | |
52 | { | |
53 | for (int i = m_y1; i < m_y2+2; i++) | |
54 | { | |
55 | if (i >= (int)m_owner->m_lines.GetCount()) | |
56 | m_lines.Add( wxT("") ); | |
57 | else | |
58 | m_lines.Add( m_owner->m_lines[i].m_text ); | |
59 | } | |
60 | } else | |
61 | if (m_type == wxSOURCE_UNDO_DELETE) | |
62 | { | |
63 | for (int i = m_y1; i < m_y2+1; i++) | |
64 | { | |
65 | m_lines.Add( m_owner->m_lines[i].m_text ); | |
66 | } | |
67 | } else | |
68 | if (m_type == wxSOURCE_UNDO_PASTE) | |
69 | { | |
70 | m_text = m_owner->m_lines[m_y1].m_text; | |
71 | } | |
72 | } | |
73 | ||
74 | void wxSourceUndoStep::Undo() | |
75 | { | |
76 | if (m_type == wxSOURCE_UNDO_LINE) | |
77 | { | |
78 | m_owner->m_lines[m_y1].m_text = m_text; | |
79 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
80 | m_owner->RefreshLine( m_y1 ); | |
81 | } else | |
82 | if (m_type == wxSOURCE_UNDO_ENTER) | |
83 | { | |
84 | m_owner->m_lines[m_y1].m_text = m_text; | |
85 | m_owner->m_lines.RemoveAt( m_y1+1 ); | |
86 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
87 | m_owner->RefreshDown( m_y1 ); | |
88 | } else | |
89 | if (m_type == wxSOURCE_UNDO_BACK) | |
90 | { | |
91 | m_owner->m_lines[m_y1].m_text = m_lines[0]; | |
92 | m_owner->m_lines.Insert( new wxSourceLine( m_lines[1] ), m_y1+1 ); | |
93 | m_owner->MyAdjustScrollbars(); | |
94 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
95 | m_owner->RefreshDown( m_y1 ); | |
96 | } else | |
97 | if (m_type == wxSOURCE_UNDO_DELETE) | |
98 | { | |
99 | m_owner->m_lines[m_y1].m_text = m_lines[0]; | |
100 | for (int i = 1; i < (int)m_lines.GetCount(); i++) | |
101 | m_owner->m_lines.Insert( new wxSourceLine( m_lines[i] ), m_y1+i ); | |
102 | m_owner->MyAdjustScrollbars(); | |
103 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
104 | m_owner->RefreshDown( m_y1 ); | |
105 | } else | |
106 | if (m_type == wxSOURCE_UNDO_PASTE) | |
107 | { | |
108 | m_owner->m_lines[m_y1].m_text = m_text; | |
109 | for (int i = 0; i < m_y2-m_y1; i++) | |
110 | m_owner->m_lines.RemoveAt( m_y1+1 ); | |
111 | m_owner->MyAdjustScrollbars(); | |
112 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
113 | m_owner->RefreshDown( m_y1 ); | |
114 | } else | |
115 | if (m_type == wxSOURCE_UNDO_INSERT_LINE) | |
116 | { | |
117 | m_owner->m_lines.RemoveAt( m_y1 ); | |
118 | m_owner->MyAdjustScrollbars(); | |
119 | m_owner->MoveCursor( 0, m_y1 ); | |
120 | m_owner->RefreshDown( m_y1 ); | |
121 | } | |
122 | } | |
123 | ||
124 | #include "wx/arrimpl.cpp" | |
125 | WX_DEFINE_OBJARRAY(wxSourceLineArray); | |
126 | ||
127 | //----------------------------------------------------------------------------- | |
128 | // wxTextCtrl | |
129 | //----------------------------------------------------------------------------- | |
130 | ||
131 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
132 | ||
133 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
134 | EVT_PAINT(wxTextCtrl::OnPaint) | |
135 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
136 | EVT_CHAR(wxTextCtrl::OnChar) | |
137 | EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse) | |
138 | EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus) | |
139 | EVT_SET_FOCUS(wxTextCtrl::OnSetFocus) | |
140 | ||
141 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
142 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
143 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
144 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
145 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
146 | ||
147 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
148 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
149 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
150 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
151 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
152 | END_EVENT_TABLE() | |
153 | ||
154 | void wxTextCtrl::Init() | |
155 | { | |
156 | m_editable = TRUE; | |
157 | m_modified = FALSE; | |
158 | ||
159 | m_undos.DeleteContents( TRUE ); | |
160 | ||
161 | m_lang = wxSOURCE_LANG_NONE; | |
162 | ||
163 | m_capturing = FALSE; | |
164 | ||
165 | m_cursorX = 0; | |
166 | m_cursorY = 0; | |
167 | ||
168 | m_longestLine = 0; | |
169 | ||
170 | m_bracketX = -1; | |
171 | m_bracketY = -1; | |
172 | ||
173 | m_overwrite = FALSE; | |
174 | m_ignoreInput = FALSE; | |
175 | ||
176 | ClearSelection(); | |
177 | ||
178 | m_keywordColour = wxColour( 10, 140, 10 ); | |
179 | ||
180 | m_defineColour = *wxRED; | |
181 | ||
182 | m_variableColour = wxColour( 50, 120, 150 ); | |
183 | ||
184 | m_commentColour = wxColour( 130, 130, 130 ); | |
185 | ||
186 | m_stringColour = wxColour( 10, 140, 10 ); | |
187 | } | |
188 | ||
189 | wxTextCtrl::wxTextCtrl( wxWindow *parent, | |
190 | wxWindowID id, | |
191 | const wxString &value, | |
192 | const wxPoint &pos, | |
193 | const wxSize &size, | |
194 | long style, | |
195 | const wxValidator& validator, | |
196 | const wxString &name ) | |
197 | : wxScrollHelper(this) | |
198 | { | |
199 | Init(); | |
200 | ||
201 | Create( parent, id, value, pos, size, style, validator, name ); | |
202 | } | |
203 | ||
204 | bool wxTextCtrl::Create( wxWindow *parent, | |
205 | wxWindowID id, | |
206 | const wxString &value, | |
207 | const wxPoint &pos, | |
208 | const wxSize &size, | |
209 | long style, | |
210 | const wxValidator& validator, | |
211 | const wxString &name ) | |
212 | { | |
213 | if ((style & wxBORDER_MASK) == 0) | |
214 | style |= wxBORDER_SUNKEN; | |
215 | ||
216 | if ((style & wxTE_MULTILINE) != 0) | |
217 | style |= wxALWAYS_SHOW_SB; | |
218 | ||
219 | wxTextCtrlBase::Create( parent, id, pos /* wxDefaultPosition */, size, | |
220 | style|wxVSCROLL|wxHSCROLL|wxNO_FULL_REPAINT_ON_RESIZE ); | |
221 | ||
222 | SetBackgroundColour( *wxWHITE ); | |
223 | ||
224 | SetCursor( wxCursor( wxCURSOR_IBEAM ) ); | |
225 | ||
226 | m_editable = ((m_windowStyle & wxTE_READONLY) == 0); | |
227 | ||
228 | if (HasFlag(wxTE_PASSWORD)) | |
229 | m_sourceFont = wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL ); | |
230 | else | |
231 | m_sourceFont = GetFont(); | |
232 | ||
233 | wxClientDC dc(this); | |
234 | dc.SetFont( m_sourceFont ); | |
235 | m_lineHeight = dc.GetCharHeight(); | |
236 | m_charWidth = dc.GetCharWidth(); | |
237 | ||
238 | SetValue( value ); | |
239 | ||
240 | wxSize size_best( DoGetBestSize() ); | |
241 | wxSize new_size( size ); | |
242 | if (new_size.x == -1) | |
243 | new_size.x = size_best.x; | |
244 | if (new_size.y == -1) | |
245 | new_size.y = size_best.y; | |
246 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
247 | SetSize( new_size.x, new_size.y ); | |
248 | ||
249 | // We create an input handler since it might be useful | |
250 | CreateInputHandler(wxINP_HANDLER_TEXTCTRL); | |
251 | ||
252 | MyAdjustScrollbars(); | |
253 | ||
254 | return TRUE; | |
255 | } | |
256 | ||
257 | //----------------------------------------------------------------------------- | |
258 | // public methods | |
259 | //----------------------------------------------------------------------------- | |
260 | ||
261 | wxString wxTextCtrl::GetValue() const | |
262 | { | |
263 | wxString ret; | |
264 | for (size_t i = 0; i < m_lines.GetCount(); i++) | |
265 | { | |
266 | ret += m_lines[i].m_text; | |
267 | if (i+1 < m_lines.GetCount()) | |
268 | ret += wxT('\n'); | |
269 | } | |
270 | ||
271 | return ret; | |
272 | } | |
273 | ||
274 | void wxTextCtrl::SetValue(const wxString& value) | |
275 | { | |
276 | m_modified = FALSE; | |
277 | ||
278 | wxString oldValue = GetValue(); | |
279 | ||
280 | m_cursorX = 0; | |
281 | m_cursorY = 0; | |
282 | ClearSelection(); | |
283 | m_lines.Clear(); | |
284 | m_longestLine = 0; | |
285 | ||
286 | if (value.IsEmpty()) | |
287 | { | |
288 | m_lines.Add( new wxSourceLine( wxT("") ) ); | |
289 | } | |
290 | else | |
291 | { | |
292 | int begin = 0; | |
293 | int pos = 0; | |
294 | for (;;) | |
295 | { | |
296 | pos = value.find( wxT('\n'), begin ); | |
297 | if (pos < 0) | |
298 | { | |
299 | wxSourceLine *sl = new wxSourceLine( value.Mid( begin, value.Len()-begin ) ); | |
300 | m_lines.Add( sl ); | |
301 | ||
302 | // if (sl->m_text.Len() > m_longestLine) | |
303 | // m_longestLine = sl->m_text.Len(); | |
304 | int ww = 0; | |
305 | GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL ); | |
306 | ww /= m_charWidth; | |
307 | if (ww > m_longestLine) | |
308 | m_longestLine = ww; | |
309 | ||
310 | break; | |
311 | } | |
312 | else | |
313 | { | |
314 | wxSourceLine *sl = new wxSourceLine( value.Mid( begin, pos-begin ) ); | |
315 | m_lines.Add( sl ); | |
316 | ||
317 | // if (sl->m_text.Len() > m_longestLine) | |
318 | // m_longestLine = sl->m_text.Len(); | |
319 | int ww = 0; | |
320 | GetTextExtent( sl->m_text, &ww, NULL, NULL, NULL ); | |
321 | ww /= m_charWidth; | |
322 | if (ww > m_longestLine) | |
323 | m_longestLine = ww; | |
324 | ||
325 | begin = pos+1; | |
326 | } | |
327 | } | |
328 | } | |
329 | ||
330 | // Don't need to refresh if the value hasn't changed | |
331 | if ((GetWindowStyle() & wxTE_MULTILINE) == 0) | |
332 | { | |
333 | if (value == oldValue) | |
334 | return; | |
335 | } | |
336 | ||
337 | MyAdjustScrollbars(); | |
338 | ||
339 | Refresh(); | |
340 | } | |
341 | ||
342 | int wxTextCtrl::GetLineLength(long lineNo) const | |
343 | { | |
344 | if (lineNo >= (long)m_lines.GetCount()) | |
345 | return 0; | |
346 | ||
347 | return m_lines[lineNo].m_text.Len(); | |
348 | } | |
349 | ||
350 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
351 | { | |
352 | if (lineNo >= (long)m_lines.GetCount()) | |
353 | return wxT(""); | |
354 | ||
355 | return m_lines[lineNo].m_text; | |
356 | } | |
357 | ||
358 | int wxTextCtrl::GetNumberOfLines() const | |
359 | { | |
360 | return m_lines.GetCount(); | |
361 | } | |
362 | ||
363 | bool wxTextCtrl::IsModified() const | |
364 | { | |
365 | return m_modified; | |
366 | } | |
367 | ||
368 | bool wxTextCtrl::IsEditable() const | |
369 | { | |
370 | return m_editable; | |
371 | } | |
372 | ||
373 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
374 | { | |
375 | if (m_selStartX == -1 || m_selStartY == -1 || | |
376 | m_selEndX == -1 || m_selEndY == -1) | |
377 | { | |
378 | *from = GetInsertionPoint(); | |
379 | *to = GetInsertionPoint(); | |
380 | } | |
381 | else | |
382 | { | |
383 | *from = XYToPosition(m_selStartX, m_selStartY); | |
384 | *to = XYToPosition(m_selEndX, m_selEndY); | |
385 | } | |
386 | } | |
387 | ||
388 | void wxTextCtrl::Clear() | |
389 | { | |
390 | m_modified = TRUE; | |
391 | m_cursorX = 0; | |
392 | m_cursorY = 0; | |
393 | ClearSelection(); | |
394 | ||
395 | m_lines.Clear(); | |
396 | m_lines.Add( new wxSourceLine( wxT("") ) ); | |
397 | ||
398 | SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 ); | |
399 | Refresh(); | |
400 | m_undos.Clear(); | |
401 | } | |
402 | ||
403 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
404 | { | |
405 | } | |
406 | ||
407 | void wxTextCtrl::Remove(long from, long to) | |
408 | { | |
409 | ||
410 | } | |
411 | ||
412 | void wxTextCtrl::DiscardEdits() | |
413 | { | |
414 | ClearSelection(); | |
415 | Refresh(); | |
416 | } | |
417 | ||
418 | void wxTextCtrl::SetMaxLength(unsigned long len) | |
419 | { | |
420 | } | |
421 | ||
422 | int wxTextCtrl::PosToPixel( int line, int pos ) | |
423 | { | |
424 | // TODO add support for Tabs | |
425 | ||
426 | if (line >= (int)m_lines.GetCount()) return 0; | |
427 | if (pos < 0) return 0; | |
428 | ||
429 | wxString text = m_lines[line].m_text; | |
430 | ||
431 | if (text.IsEmpty()) return 0; | |
432 | ||
433 | if (pos < (int)text.Len()) | |
434 | text.Remove( pos, text.Len()-pos ); | |
435 | ||
436 | int w = 0; | |
437 | ||
438 | GetTextExtent( text, &w, NULL, NULL, NULL ); | |
439 | ||
440 | return w; | |
441 | } | |
442 | ||
443 | int wxTextCtrl::PixelToPos( int line, int pixel ) | |
444 | { | |
445 | if (pixel < 2) return 0; | |
446 | ||
447 | if (line >= (int)m_lines.GetCount()) return 0; | |
448 | ||
449 | wxString text = m_lines[line].m_text; | |
450 | ||
451 | int w = 0; | |
452 | int res = text.Len(); | |
453 | while (res > 0) | |
454 | { | |
455 | GetTextExtent( text, &w, NULL, NULL, NULL ); | |
456 | ||
457 | if (w < pixel) | |
458 | return res; | |
459 | ||
460 | res--; | |
461 | text.Remove( res,1 ); | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | void wxTextCtrl::SetLanguage( wxSourceLanguage lang ) | |
468 | { | |
469 | m_lang = lang; | |
470 | ||
471 | m_keywords.Clear(); | |
472 | } | |
473 | ||
474 | void wxTextCtrl::WriteText(const wxString& text2) | |
475 | { | |
476 | if (text2.IsEmpty()) return; | |
477 | ||
478 | m_modified = TRUE; | |
479 | ||
480 | wxString text( text2 ); | |
481 | wxArrayString lines; | |
482 | int pos; | |
483 | while ( (pos = text.Find('\n')) != -1 ) | |
484 | { | |
485 | lines.Add( text.Left( pos ) ); | |
486 | text.Remove( 0, pos+1 ); | |
487 | } | |
488 | lines.Add( text ); | |
489 | int count = (int)lines.GetCount(); | |
490 | ||
491 | wxString tmp1( m_lines[m_cursorY].m_text ); | |
492 | wxString tmp2( tmp1 ); | |
493 | int len = (int)tmp1.Len(); | |
494 | ||
495 | if (len < m_cursorX) | |
496 | { | |
497 | wxString tmp; | |
498 | for (int i = 0; i < m_cursorX-len; i++) | |
499 | tmp.Append( ' ' ); | |
500 | m_lines[m_cursorY].m_text.Append( tmp ); | |
501 | tmp1.Append( tmp ); | |
502 | tmp2.Append( tmp ); | |
503 | } | |
504 | ||
505 | tmp1.Remove( m_cursorX ); | |
506 | tmp2.Remove( 0, m_cursorX ); | |
507 | tmp1.Append( lines[0] ); | |
508 | ||
509 | if (count == 1) | |
510 | { | |
511 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) ); | |
512 | ||
513 | tmp1.Append( tmp2 ); | |
514 | m_lines[m_cursorY].m_text = tmp1; | |
515 | RefreshLine( m_cursorY ); | |
516 | } | |
517 | else | |
518 | { | |
519 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, m_cursorY, m_cursorY+count-1, this ) ); | |
520 | ||
521 | m_lines[m_cursorY].m_text = tmp1; | |
522 | int i; | |
523 | for (i = 1; i < count; i++) | |
524 | m_lines.Insert( new wxSourceLine( lines[i] ), m_cursorY+i ); | |
525 | m_lines[m_cursorY+i-1].m_text.Append( tmp2 ); | |
526 | ||
527 | MyAdjustScrollbars(); | |
528 | RefreshDown( m_cursorY ); | |
529 | } | |
530 | } | |
531 | ||
532 | void wxTextCtrl::AppendText(const wxString& text2) | |
533 | { | |
534 | if (text2.IsEmpty()) return; | |
535 | ||
536 | m_modified = TRUE; | |
537 | ||
538 | wxString text( text2 ); | |
539 | wxArrayString lines; | |
540 | int pos; | |
541 | while ( (pos = text.Find('\n')) != -1 ) | |
542 | { | |
543 | lines.Add( text.Left( pos ) ); | |
544 | text.Remove( 0, pos+1 ); | |
545 | } | |
546 | lines.Add( text ); | |
547 | int count = (int)lines.GetCount(); | |
548 | ||
549 | size_t y = m_lines.GetCount()-1; | |
550 | ||
551 | wxString tmp( m_lines[y].m_text ); | |
552 | tmp.Append( lines[0] ); | |
553 | ||
554 | if (count == 1) | |
555 | { | |
556 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, y, y, this ) ); | |
557 | ||
558 | m_lines[y].m_text = tmp; | |
559 | RefreshLine( y ); | |
560 | } | |
561 | else | |
562 | { | |
563 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, y, y+count-1, this ) ); | |
564 | ||
565 | m_lines[y].m_text = tmp; | |
566 | int i; | |
567 | for (i = 1; i < count; i++) | |
568 | m_lines.Insert( new wxSourceLine( lines[i] ), y+i ); | |
569 | ||
570 | MyAdjustScrollbars(); | |
571 | RefreshDown( y ); | |
572 | } | |
573 | } | |
574 | ||
575 | bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
576 | { | |
577 | return FALSE; | |
578 | } | |
579 | ||
580 | long wxTextCtrl::XYToPosition(long x, long y) const | |
581 | { | |
582 | long ret = 0; | |
583 | ||
584 | for (size_t i = 0; i < m_lines.GetCount(); i++) | |
585 | { | |
586 | if (i < (size_t)y) | |
587 | { | |
588 | // Add one for the end-of-line character | |
589 | ret += m_lines[i].m_text.Len() + 1; | |
590 | continue; | |
591 | } | |
592 | ||
593 | if ((size_t)x < (m_lines[i].m_text.Len()+1)) | |
594 | return (ret + x); | |
595 | else | |
596 | return (ret + m_lines[i].m_text.Len() + 1); | |
597 | } | |
598 | ||
599 | return ret; | |
600 | } | |
601 | ||
602 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
603 | { | |
604 | if (m_lines.GetCount() == 0) | |
605 | { | |
606 | if (x) *x = 0; | |
607 | if (y) *y = 0; | |
608 | ||
609 | return (pos == 0); | |
610 | } | |
611 | ||
612 | long xx = 0; | |
613 | long yy = 0; | |
614 | ||
615 | for (size_t i = 0; i < m_lines.GetCount(); i++) | |
616 | { | |
617 | //pos -= m_lines[i].m_text.Len(); | |
618 | //if (pos <= 0) | |
619 | ||
620 | // Add one for the end-of-line character. (In Windows, | |
621 | // there are _two_ positions for each end of line.) | |
622 | if (pos <= ((int)m_lines[i].m_text.Len())) | |
623 | { | |
624 | xx = pos; | |
625 | if (x) *x = xx; | |
626 | if (y) *y = yy; | |
627 | return TRUE; | |
628 | } | |
629 | pos -= (m_lines[i].m_text.Len() + 1); | |
630 | yy++; | |
631 | } | |
632 | ||
633 | // Last pos | |
634 | //xx = m_lines[ m_lines.GetCount()-1 ].m_text.Len(); | |
635 | xx = pos; | |
636 | if (x) *x = xx; | |
637 | if (y) *y = yy; | |
638 | ||
639 | return FALSE; | |
640 | } | |
641 | ||
642 | void wxTextCtrl::ShowPosition(long pos) | |
643 | { | |
644 | } | |
645 | ||
646 | void wxTextCtrl::Copy() | |
647 | { | |
648 | if (!HasSelection()) return; | |
649 | ||
650 | wxString sel; | |
651 | ||
652 | int selStartY = m_selStartY; | |
653 | int selEndY = m_selEndY; | |
654 | int selStartX = m_selStartX; | |
655 | int selEndX = m_selEndX; | |
656 | ||
657 | if ((selStartY > selEndY) || | |
658 | ((selStartY == selEndY) && (selStartX > selEndX))) | |
659 | { | |
660 | int tmp = selStartX; | |
661 | selStartX = selEndX; | |
662 | selEndX = tmp; | |
663 | tmp = selStartY; | |
664 | selStartY = selEndY; | |
665 | selEndY = tmp; | |
666 | } | |
667 | ||
668 | if (selStartY == selEndY) | |
669 | { | |
670 | sel = m_lines[selStartY].m_text; | |
671 | ||
672 | if (selStartX >= (int)sel.Len()) return; | |
673 | if (selEndX > (int)sel.Len()) | |
674 | selEndX = sel.Len(); | |
675 | ||
676 | sel.Remove( selEndX, sel.Len()-selEndX ); | |
677 | sel.Remove( 0, selStartX ); | |
678 | } | |
679 | else | |
680 | { | |
681 | wxString tmp( m_lines[selStartY].m_text ); | |
682 | ||
683 | if (selStartX < (int)tmp.Len()) | |
684 | { | |
685 | tmp.Remove( 0, selStartX ); | |
686 | sel = tmp; | |
687 | sel.Append( wxT("\n") ); | |
688 | } | |
689 | for (int i = selStartY+1; i < selEndY; i++) | |
690 | { | |
691 | sel.Append( m_lines[i].m_text ); | |
692 | sel.Append( wxT("\n") ); | |
693 | } | |
694 | tmp = m_lines[selEndY].m_text; | |
695 | if (selEndX > (int)tmp.Len()) | |
696 | selEndX = tmp.Len(); | |
697 | if (selEndX > 0) | |
698 | { | |
699 | tmp.Remove( selEndX, tmp.Len()-selEndX ); | |
700 | sel.Append( tmp ); | |
701 | } | |
702 | } | |
703 | ||
704 | if (wxTheClipboard->Open()) | |
705 | { | |
706 | wxTheClipboard->SetData( new wxTextDataObject( sel ) ); | |
707 | wxTheClipboard->Close(); | |
708 | } | |
709 | } | |
710 | ||
711 | void wxTextCtrl::Cut() | |
712 | { | |
713 | Copy(); | |
714 | ||
715 | Delete(); | |
716 | } | |
717 | ||
718 | void wxTextCtrl::Paste() | |
719 | { | |
720 | Delete(); | |
721 | ||
722 | if (!wxTheClipboard->Open()) return; | |
723 | ||
724 | if (!wxTheClipboard->IsSupported( wxDF_TEXT )) | |
725 | { | |
726 | wxTheClipboard->Close(); | |
727 | ||
728 | return; | |
729 | } | |
730 | ||
731 | wxTextDataObject data; | |
732 | ||
733 | bool ret = wxTheClipboard->GetData( data ); | |
734 | ||
735 | wxTheClipboard->Close(); | |
736 | ||
737 | if (!ret) return; | |
738 | ||
739 | m_modified = TRUE; | |
740 | ||
741 | wxString text( data.GetText() ); | |
742 | wxArrayString lines; | |
743 | int pos; | |
744 | while ( (pos = text.Find('\n')) != -1 ) | |
745 | { | |
746 | lines.Add( text.Left( pos ) ); | |
747 | text.Remove( 0, pos+1 ); | |
748 | } | |
749 | lines.Add( text ); | |
750 | int count = (int)lines.GetCount(); | |
751 | ||
752 | wxString tmp1( m_lines[m_cursorY].m_text ); | |
753 | wxString tmp2( tmp1 ); | |
754 | int len = (int)tmp1.Len(); | |
755 | ||
756 | if (len < m_cursorX) | |
757 | { | |
758 | wxString tmp; | |
759 | for (int i = 0; i < m_cursorX-len; i++) | |
760 | tmp.Append( ' ' ); | |
761 | m_lines[m_cursorY].m_text.Append( tmp ); | |
762 | tmp1.Append( tmp ); | |
763 | tmp2.Append( tmp ); | |
764 | } | |
765 | ||
766 | tmp1.Remove( m_cursorX ); | |
767 | tmp2.Remove( 0, m_cursorX ); | |
768 | tmp1.Append( lines[0] ); | |
769 | ||
770 | if (count == 1) | |
771 | { | |
772 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) ); | |
773 | ||
774 | tmp1.Append( tmp2 ); | |
775 | m_lines[m_cursorY].m_text = tmp1; | |
776 | RefreshLine( m_cursorY ); | |
777 | } | |
778 | else | |
779 | { | |
780 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_PASTE, m_cursorY, m_cursorY+count-1, this ) ); | |
781 | ||
782 | m_lines[m_cursorY].m_text = tmp1; | |
783 | int i; | |
784 | for (i = 1; i < count; i++) | |
785 | m_lines.Insert( new wxSourceLine( lines[i] ), m_cursorY+i ); | |
786 | m_lines[m_cursorY+i-1].m_text.Append( tmp2 ); | |
787 | ||
788 | MyAdjustScrollbars(); | |
789 | RefreshDown( m_cursorY ); | |
790 | } | |
791 | } | |
792 | ||
793 | void wxTextCtrl::Undo() | |
794 | { | |
795 | if (m_undos.GetCount() == 0) return; | |
796 | ||
797 | wxList::Node *node = m_undos.Item( m_undos.GetCount()-1 ); | |
798 | wxSourceUndoStep *undo = (wxSourceUndoStep*) node->GetData(); | |
799 | ||
800 | undo->Undo(); | |
801 | ||
802 | delete node; | |
803 | ||
804 | m_modified = TRUE; | |
805 | } | |
806 | ||
807 | void wxTextCtrl::SetInsertionPoint(long pos) | |
808 | { | |
809 | ClearSelection(); | |
810 | long x, y; | |
811 | PositionToXY(pos, & x, & y); | |
812 | m_cursorX = x; | |
813 | m_cursorY = y; | |
814 | // TODO: scroll to this position if necessary | |
815 | Refresh(); | |
816 | } | |
817 | ||
818 | void wxTextCtrl::SetInsertionPointEnd() | |
819 | { | |
820 | SetInsertionPoint(GetLastPosition()); | |
821 | } | |
822 | ||
823 | long wxTextCtrl::GetInsertionPoint() const | |
824 | { | |
825 | return XYToPosition( m_cursorX, m_cursorY ); | |
826 | } | |
827 | ||
828 | long wxTextCtrl::GetLastPosition() const | |
829 | { | |
830 | size_t lineCount = m_lines.GetCount() - 1; | |
831 | // It's the length of the line, not the length - 1, | |
832 | // because there's a position after the last character. | |
833 | return XYToPosition( m_lines[lineCount].m_text.Len(), lineCount ); | |
834 | } | |
835 | ||
836 | void wxTextCtrl::SetSelection(long from, long to) | |
837 | { | |
838 | } | |
839 | ||
840 | void wxTextCtrl::SetEditable(bool editable) | |
841 | { | |
842 | m_editable = editable; | |
843 | } | |
844 | ||
845 | bool wxTextCtrl::Enable( bool enable ) | |
846 | { | |
847 | return FALSE; | |
848 | } | |
849 | ||
850 | bool wxTextCtrl::SetFont(const wxFont& font) | |
851 | { | |
852 | wxTextCtrlBase::SetFont( font ); | |
853 | ||
854 | m_sourceFont = font; | |
855 | ||
856 | wxClientDC dc(this); | |
857 | dc.SetFont( m_sourceFont ); | |
858 | m_lineHeight = dc.GetCharHeight(); | |
859 | m_charWidth = dc.GetCharWidth(); | |
860 | ||
861 | // TODO: recalc longest lines | |
862 | ||
863 | MyAdjustScrollbars(); | |
864 | ||
865 | return TRUE; | |
866 | } | |
867 | ||
868 | bool wxTextCtrl::SetForegroundColour(const wxColour& colour) | |
869 | { | |
870 | return wxWindow::SetForegroundColour( colour ); | |
871 | } | |
872 | ||
873 | bool wxTextCtrl::SetBackgroundColour(const wxColour& colour) | |
874 | { | |
875 | return wxWindow::SetBackgroundColour( colour ); | |
876 | } | |
877 | ||
878 | //----------------------------------------------------------------------------- | |
879 | // private code and handlers | |
880 | //----------------------------------------------------------------------------- | |
881 | ||
882 | void wxTextCtrl::SearchForBrackets() | |
883 | { | |
884 | int oldBracketY = m_bracketY; | |
885 | int oldBracketX = m_bracketX; | |
886 | ||
887 | if (m_cursorY < 0 || m_cursorY >= (int)m_lines.GetCount()) return; | |
888 | ||
889 | wxString current = m_lines[m_cursorY].m_text; | |
890 | ||
891 | // reverse search first | |
892 | ||
893 | char bracket = ' '; | |
894 | ||
895 | if (m_cursorX > 0) | |
896 | bracket = current[(size_t) (m_cursorX-1)]; | |
897 | ||
898 | if (bracket == ')' || bracket == ']' || bracket == '}') | |
899 | { | |
900 | char antibracket = '('; | |
901 | if (bracket == ']') antibracket = '['; | |
902 | if (bracket == '}') antibracket = '{'; | |
903 | ||
904 | int count = 1; | |
905 | ||
906 | int endY = m_cursorY-60; | |
907 | if (endY < 0) endY = 0; | |
908 | for (int y = m_cursorY; y >= endY; y--) | |
909 | { | |
910 | current = m_lines[y].m_text; | |
911 | if (y == m_cursorY) | |
912 | current.erase(m_cursorX-1,current.Len()-m_cursorX+1); | |
913 | ||
914 | for (int n = current.Len()-1; n >= 0; n--) | |
915 | { | |
916 | // ignore chars | |
917 | if (current[(size_t) (n)] == '\'') | |
918 | { | |
919 | for (int m = n-1; m >= 0; m--) | |
920 | { | |
921 | if (current[(size_t) (m)] == '\'') | |
922 | { | |
923 | if (m == 0 || current[(size_t) (m-1)] != '\\') | |
924 | break; | |
925 | } | |
926 | n = m-1; | |
927 | } | |
928 | continue; | |
929 | } | |
930 | ||
931 | // ignore strings | |
932 | if (current[(size_t) (n)] == '\"') | |
933 | { | |
934 | for (int m = n-1; m >= 0; m--) | |
935 | { | |
936 | if (current[(size_t) (m)] == '\"') | |
937 | { | |
938 | if (m == 0 || current[(size_t) (m-1)] != '\\') | |
939 | break; | |
940 | } | |
941 | n = m-1; | |
942 | } | |
943 | continue; | |
944 | } | |
945 | ||
946 | if (current[(size_t) (n)] == antibracket) | |
947 | { | |
948 | count--; | |
949 | if (count == 0) | |
950 | { | |
951 | m_bracketY = y; | |
952 | m_bracketX = n; | |
953 | if (oldBracketY != m_bracketY && oldBracketY != -1) | |
954 | RefreshLine( oldBracketY ); | |
955 | if (m_bracketY != oldBracketY || m_bracketX != oldBracketX) | |
956 | RefreshLine( m_bracketY ); | |
957 | return; | |
958 | } | |
959 | } | |
960 | else if (current[(size_t) (n)] == bracket) | |
961 | { | |
962 | count++; | |
963 | } | |
964 | } | |
965 | } | |
966 | } | |
967 | ||
968 | // then forward | |
969 | ||
970 | bracket = ' '; | |
971 | if ((int)current.Len() > m_cursorX) | |
972 | bracket = current[(size_t) (m_cursorX)]; | |
973 | if (bracket == '(' || bracket == '[' || bracket == '{') | |
974 | { | |
975 | char antibracket = ')'; | |
976 | if (bracket == '[') antibracket = ']'; | |
977 | if (bracket == '{') antibracket = '}'; | |
978 | ||
979 | int count = 1; | |
980 | ||
981 | int endY = m_cursorY+60; | |
982 | if (endY > (int)(m_lines.GetCount()-1)) endY = m_lines.GetCount()-1; | |
983 | for (int y = m_cursorY; y <= endY; y++) | |
984 | { | |
985 | current = m_lines[y].m_text; | |
986 | int start = 0; | |
987 | if (y == m_cursorY) | |
988 | start = m_cursorX+1; | |
989 | ||
990 | for (int n = start; n < (int)current.Len(); n++) | |
991 | { | |
992 | // ignore chars | |
993 | if (current[(size_t) (n)] == '\'') | |
994 | { | |
995 | for (int m = n+1; m < (int)current.Len(); m++) | |
996 | { | |
997 | if (current[(size_t) (m)] == '\'') | |
998 | { | |
999 | if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\')) | |
1000 | break; | |
1001 | } | |
1002 | n = m+1; | |
1003 | } | |
1004 | continue; | |
1005 | } | |
1006 | ||
1007 | // ignore strings | |
1008 | if (current[(size_t) (n)] == '\"') | |
1009 | { | |
1010 | for (int m = n+1; m < (int)current.Len(); m++) | |
1011 | { | |
1012 | if (current[(size_t) (m)] == '\"') | |
1013 | { | |
1014 | if (m == 0 || (current[(size_t) (m-1)] != '\\') || (m >= 2 && current[(size_t) (m-2)] == '\\')) | |
1015 | break; | |
1016 | } | |
1017 | n = m+1; | |
1018 | } | |
1019 | continue; | |
1020 | } | |
1021 | ||
1022 | if (current[(size_t) (n)] == antibracket) | |
1023 | { | |
1024 | count--; | |
1025 | if (count == 0) | |
1026 | { | |
1027 | m_bracketY = y; | |
1028 | m_bracketX = n; | |
1029 | if (oldBracketY != m_bracketY && oldBracketY != -1) | |
1030 | RefreshLine( oldBracketY ); | |
1031 | if (m_bracketY != oldBracketY || m_bracketX != oldBracketX) | |
1032 | RefreshLine( m_bracketY ); | |
1033 | return; | |
1034 | } | |
1035 | } | |
1036 | else if (current[(size_t) (n)] == bracket) | |
1037 | { | |
1038 | count++; | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | if (oldBracketY != -1) | |
1045 | { | |
1046 | m_bracketY = -1; | |
1047 | RefreshLine( oldBracketY ); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | void wxTextCtrl::Delete() | |
1052 | { | |
1053 | if (!HasSelection()) return; | |
1054 | ||
1055 | m_modified = TRUE; | |
1056 | ||
1057 | int selStartY = m_selStartY; | |
1058 | int selEndY = m_selEndY; | |
1059 | int selStartX = m_selStartX; | |
1060 | int selEndX = m_selEndX; | |
1061 | ||
1062 | if ((selStartY > selEndY) || | |
1063 | ((selStartY == selEndY) && (selStartX > selEndX))) | |
1064 | { | |
1065 | int tmp = selStartX; | |
1066 | selStartX = selEndX; | |
1067 | selEndX = tmp; | |
1068 | tmp = selStartY; | |
1069 | selStartY = selEndY; | |
1070 | selEndY = tmp; | |
1071 | } | |
1072 | ||
1073 | int len = (int)m_lines[selStartY].m_text.Len(); | |
1074 | ||
1075 | if (selStartY == selEndY) | |
1076 | { | |
1077 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, selStartY, selStartY, this ) ); | |
1078 | ||
1079 | wxString tmp( m_lines[selStartY].m_text ); | |
1080 | if (selStartX < len) | |
1081 | { | |
1082 | if (selEndX > len) | |
1083 | selEndX = len; | |
1084 | tmp.Remove( selStartX, selEndX-selStartX ); | |
1085 | m_lines[selStartY].m_text = tmp; | |
1086 | } | |
1087 | ClearSelection(); | |
1088 | m_cursorX = selStartX; | |
1089 | RefreshLine( selStartY ); | |
1090 | } | |
1091 | else | |
1092 | { | |
1093 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, selStartY, selEndY, this ) ); | |
1094 | ||
1095 | if (selStartX < len) | |
1096 | m_lines[selStartY].m_text.Remove( selStartX ); | |
1097 | ||
1098 | for (int i = 0; i < selEndY-selStartY-1; i++) | |
1099 | m_lines.RemoveAt( selStartY+1 ); | |
1100 | ||
1101 | if (selEndX < (int)m_lines[selStartY+1].m_text.Len()) | |
1102 | m_lines[selStartY+1].m_text.Remove( 0, selEndX ); | |
1103 | else | |
1104 | m_lines[selStartY+1].m_text.Remove( 0 ); | |
1105 | ||
1106 | m_lines[selStartY].m_text.Append( m_lines[selStartY+1].m_text ); | |
1107 | m_lines.RemoveAt( selStartY+1 ); | |
1108 | ||
1109 | ClearSelection(); | |
1110 | MoveCursor( selStartX, selStartY ); | |
1111 | MyAdjustScrollbars(); | |
1112 | ||
1113 | RefreshDown( selStartY ); | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | void wxTextCtrl::DeleteLine() | |
1118 | { | |
1119 | if (HasSelection()) return; | |
1120 | ||
1121 | if (m_cursorY < 0 || m_cursorY >= (int)m_lines.GetCount()-1) return; // TODO | |
1122 | ||
1123 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, m_cursorY, m_cursorY+1, this ) ); | |
1124 | ||
1125 | m_lines.RemoveAt( m_cursorY ); | |
1126 | m_cursorX = 0; | |
1127 | if (m_cursorY >= (int)m_lines.GetCount()) m_cursorY--; | |
1128 | ||
1129 | MyAdjustScrollbars(); | |
1130 | RefreshDown( m_cursorY ); | |
1131 | } | |
1132 | ||
1133 | void wxTextCtrl::DoChar( char c ) | |
1134 | { | |
1135 | m_modified = TRUE; | |
1136 | ||
1137 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) ); | |
1138 | ||
1139 | wxString tmp( m_lines[m_cursorY].m_text ); | |
1140 | tmp.Trim(); | |
1141 | if (m_cursorX >= (int)tmp.Len()) | |
1142 | { | |
1143 | int len = tmp.Len(); | |
1144 | for (int i = 0; i < m_cursorX - len; i++) | |
1145 | tmp.Append( ' ' ); | |
1146 | tmp.Append( c ); | |
1147 | } | |
1148 | else | |
1149 | { | |
1150 | if (m_overwrite) | |
1151 | tmp.SetChar( m_cursorX, c ); | |
1152 | else | |
1153 | tmp.insert( m_cursorX, 1, c ); | |
1154 | } | |
1155 | ||
1156 | m_lines[m_cursorY].m_text = tmp; | |
1157 | ||
1158 | // if (tmp.Len() > m_longestLine) | |
1159 | // { | |
1160 | // m_longestLine = tmp.Len(); | |
1161 | // MyAdjustScrollbars(); | |
1162 | // } | |
1163 | ||
1164 | int ww = 0; | |
1165 | GetTextExtent( tmp, &ww, NULL, NULL, NULL ); | |
1166 | ww /= m_charWidth; | |
1167 | if (ww > m_longestLine) | |
1168 | { | |
1169 | m_longestLine = ww; | |
1170 | MyAdjustScrollbars(); | |
1171 | } | |
1172 | ||
1173 | m_cursorX++; | |
1174 | ||
1175 | int y = m_cursorY*m_lineHeight; | |
1176 | // int x = (m_cursorX-1)*m_charWidth; | |
1177 | int x = PosToPixel( m_cursorY, m_cursorX-1 ); | |
1178 | CalcScrolledPosition( x, y, &x, &y ); | |
1179 | wxRect rect( x+2, y+2, 10000, m_lineHeight ); | |
1180 | Refresh( TRUE, &rect ); | |
1181 | // refresh whole line for syntax colour highlighting | |
1182 | rect.x = 0; | |
1183 | Refresh( FALSE, &rect ); | |
1184 | ||
1185 | int size_x = 0; | |
1186 | int size_y = 0; | |
1187 | GetClientSize( &size_x, &size_y ); | |
1188 | size_x /= m_charWidth; | |
1189 | ||
1190 | int view_x = 0; | |
1191 | int view_y = 0; | |
1192 | GetViewStart( &view_x, &view_y ); | |
1193 | ||
1194 | //int xx = m_cursorX; | |
1195 | int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth; | |
1196 | ||
1197 | if (xx < view_x) | |
1198 | Scroll( xx, -1 ); | |
1199 | else if (xx > view_x+size_x-1) | |
1200 | Scroll( xx-size_x+1, -1 ); | |
1201 | } | |
1202 | ||
1203 | void wxTextCtrl::DoBack() | |
1204 | { | |
1205 | m_modified = TRUE; | |
1206 | ||
1207 | if (m_cursorX == 0) | |
1208 | { | |
1209 | if (m_cursorY == 0) return; | |
1210 | ||
1211 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_BACK, m_cursorY-1, m_cursorY, this ) ); | |
1212 | ||
1213 | wxString tmp1( m_lines[m_cursorY-1].m_text ); | |
1214 | tmp1.Trim(); | |
1215 | wxString tmp2( m_lines[m_cursorY].m_text ); | |
1216 | tmp2.Trim(); | |
1217 | m_cursorX = tmp1.Len(); | |
1218 | m_cursorY--; | |
1219 | tmp1.Append( tmp2 ); | |
1220 | m_lines[m_cursorY].m_text = tmp1; | |
1221 | m_lines.RemoveAt( m_cursorY+1 ); | |
1222 | ||
1223 | MyAdjustScrollbars(); | |
1224 | RefreshDown( m_cursorY-1 ); | |
1225 | } | |
1226 | else | |
1227 | { | |
1228 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) ); | |
1229 | ||
1230 | if (m_cursorX <= (int)m_lines[m_cursorY].m_text.Len()) | |
1231 | m_lines[m_cursorY].m_text.Remove( m_cursorX-1, 1 ); | |
1232 | m_cursorX--; | |
1233 | ||
1234 | int y = m_cursorY*m_lineHeight; | |
1235 | // int x = m_cursorX*m_charWidth; | |
1236 | int x = PosToPixel( m_cursorY, m_cursorX ); | |
1237 | CalcScrolledPosition( x, y, &x, &y ); | |
1238 | wxRect rect( x+2, y+2, 10000, m_lineHeight ); | |
1239 | Refresh( TRUE, &rect ); | |
1240 | // refresh whole line for syntax colour highlighting | |
1241 | rect.x = 0; | |
1242 | Refresh( FALSE, &rect ); | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | void wxTextCtrl::DoDelete() | |
1247 | { | |
1248 | m_modified = TRUE; | |
1249 | ||
1250 | wxString tmp( m_lines[m_cursorY].m_text ); | |
1251 | tmp.Trim(); | |
1252 | int len = (int)tmp.Len(); | |
1253 | if (m_cursorX >= len) | |
1254 | { | |
1255 | if (m_cursorY == (int)m_lines.GetCount()-1) return; | |
1256 | ||
1257 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_DELETE, m_cursorY, m_cursorY+1, this ) ); | |
1258 | ||
1259 | for (int i = 0; i < (m_cursorX-len); i++) | |
1260 | tmp += ' '; | |
1261 | ||
1262 | tmp += m_lines[m_cursorY+1].m_text; | |
1263 | ||
1264 | m_lines[m_cursorY] = tmp; | |
1265 | m_lines.RemoveAt( m_cursorY+1 ); | |
1266 | ||
1267 | MyAdjustScrollbars(); | |
1268 | RefreshDown( m_cursorY ); | |
1269 | } | |
1270 | else | |
1271 | { | |
1272 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, m_cursorY, m_cursorY, this ) ); | |
1273 | ||
1274 | tmp.Remove( m_cursorX, 1 ); | |
1275 | m_lines[m_cursorY].m_text = tmp; | |
1276 | ||
1277 | int y = m_cursorY*m_lineHeight; | |
1278 | // int x = m_cursorX*m_charWidth; | |
1279 | int x = PosToPixel( m_cursorY, m_cursorX ); | |
1280 | CalcScrolledPosition( x, y, &x, &y ); | |
1281 | wxRect rect( x+2, y+2, 10000, m_lineHeight ); | |
1282 | Refresh( TRUE, &rect ); | |
1283 | // refresh whole line for syntax colour highlighting | |
1284 | rect.x = 0; | |
1285 | Refresh( FALSE, &rect ); | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | void wxTextCtrl::DoReturn() | |
1290 | { | |
1291 | m_modified = TRUE; | |
1292 | ||
1293 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_ENTER, m_cursorY, m_cursorY, this ) ); | |
1294 | ||
1295 | wxString tmp( m_lines[m_cursorY].m_text ); | |
1296 | size_t indent = tmp.find_first_not_of( ' ' ); | |
1297 | if (indent == wxSTRING_MAXLEN) indent = 0; | |
1298 | tmp.Trim(); | |
1299 | if (m_cursorX >= (int)tmp.Len()) | |
1300 | { | |
1301 | int cursorX = indent; | |
1302 | int cursorY = m_cursorY + 1; | |
1303 | ||
1304 | wxString new_tmp; | |
1305 | for (size_t i = 0; i < indent; i++) new_tmp.Append( ' ' ); | |
1306 | m_lines.Insert( new wxSourceLine( new_tmp ), cursorY ); | |
1307 | ||
1308 | MyAdjustScrollbars(); | |
1309 | MoveCursor( cursorX, cursorY ); | |
1310 | RefreshDown( m_cursorY ); | |
1311 | } | |
1312 | else | |
1313 | { | |
1314 | wxString tmp1( tmp ); | |
1315 | tmp1.Remove( m_cursorX, tmp.Len()-m_cursorX ); | |
1316 | m_lines[m_cursorY].m_text = tmp1; | |
1317 | ||
1318 | wxString tmp2( tmp ); | |
1319 | tmp2.Remove( 0, m_cursorX ); | |
1320 | ||
1321 | int cursorX = indent; | |
1322 | int cursorY = m_cursorY + 1; | |
1323 | ||
1324 | wxString new_tmp; | |
1325 | for (size_t i = 0; i < indent; i++) new_tmp.Append( ' ' ); | |
1326 | new_tmp.Append( tmp2 ); | |
1327 | m_lines.Insert( new wxSourceLine( new_tmp ), cursorY ); | |
1328 | ||
1329 | MyAdjustScrollbars(); | |
1330 | MoveCursor( cursorX, cursorY ); | |
1331 | RefreshDown( m_cursorY-1 ); | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | void wxTextCtrl::DoDClick() | |
1336 | { | |
1337 | wxString line( m_lines[ m_cursorY ].m_text ); | |
1338 | if (m_cursorX >= (int)line.Len()) return; | |
1339 | int p = m_cursorX; | |
1340 | char ch = line[(size_t) (p)]; | |
1341 | if (((ch >= 'a') && (ch <= 'z')) || | |
1342 | ((ch >= 'A') && (ch <= 'Z')) || | |
1343 | ((ch >= '0') && (ch <= '9')) || | |
1344 | (ch == '_')) | |
1345 | { | |
1346 | m_selStartY = m_cursorY; | |
1347 | m_selEndY = m_cursorY; | |
1348 | if (p > 0) | |
1349 | { | |
1350 | ch = line[(size_t) (p-1)]; | |
1351 | while (((ch >= 'a') && (ch <= 'z')) || | |
1352 | ((ch >= 'A') && (ch <= 'Z')) || | |
1353 | ((ch >= '0') && (ch <= '9')) || | |
1354 | (ch == '_')) | |
1355 | { | |
1356 | p--; | |
1357 | if (p == 0) break; | |
1358 | ch = line[(size_t) (p-1)]; | |
1359 | } | |
1360 | } | |
1361 | m_selStartX = p; | |
1362 | ||
1363 | p = m_cursorX; | |
1364 | if (p < (int)line.Len()) | |
1365 | { | |
1366 | ch = line[(size_t) (p)]; | |
1367 | while (((ch >= 'a') && (ch <= 'z')) || | |
1368 | ((ch >= 'A') && (ch <= 'Z')) || | |
1369 | ((ch >= '0') && (ch <= '9')) || | |
1370 | (ch == '_')) | |
1371 | { | |
1372 | if (p >= (int)line.Len()) break; | |
1373 | p++; | |
1374 | ch = line[(size_t) (p)]; | |
1375 | } | |
1376 | } | |
1377 | m_selEndX = p; | |
1378 | RefreshLine( m_cursorY ); | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | wxString wxTextCtrl::GetNextToken( wxString &line, size_t &pos ) | |
1383 | { | |
1384 | wxString ret; | |
1385 | size_t len = line.Len(); | |
1386 | for (size_t p = pos; p < len; p++) | |
1387 | { | |
1388 | if ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL)) | |
1389 | { | |
1390 | if (line[p] == '#') | |
1391 | { | |
1392 | for (size_t q = p; q < len; q++) | |
1393 | ret.Append( line[q] ); | |
1394 | pos = p; | |
1395 | return ret; | |
1396 | } | |
1397 | } | |
1398 | else | |
1399 | { | |
1400 | if ((line[p] == '/') && (p+1 < len) && (line[(size_t) (p+1)] == '/')) | |
1401 | { | |
1402 | for (size_t q = p; q < len; q++) | |
1403 | ret.Append( line[q] ); | |
1404 | pos = p; | |
1405 | return ret; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | if (line[p] == '"') | |
1410 | { | |
1411 | ret.Append( line[p] ); | |
1412 | for (size_t q = p+1; q < len; q++) | |
1413 | { | |
1414 | ret.Append( line[q] ); | |
1415 | if ((line[q] == '"') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\'))) | |
1416 | break; | |
1417 | } | |
1418 | pos = p; | |
1419 | return ret; | |
1420 | } | |
1421 | ||
1422 | if (line[p] == '\'') | |
1423 | { | |
1424 | ret.Append( line[p] ); | |
1425 | for (size_t q = p+1; q < len; q++) | |
1426 | { | |
1427 | ret.Append( line[q] ); | |
1428 | if ((line[q] == '\'') && ((line[(size_t) (q-1)] != '\\') || (q >= 2 && line[(size_t) (q-2)] == '\\'))) | |
1429 | break; | |
1430 | } | |
1431 | pos = p; | |
1432 | return ret; | |
1433 | } | |
1434 | ||
1435 | if (((line[p] >= 'a') && (line[p] <= 'z')) || | |
1436 | ((line[p] >= 'A') && (line[p] <= 'Z')) || | |
1437 | (line[p] == '_') || | |
1438 | (line[p] == '#')) | |
1439 | { | |
1440 | ret.Append( line[p] ); | |
1441 | for (size_t q = p+1; q < len; q++) | |
1442 | { | |
1443 | if (((line[q] >= 'a') && (line[q] <= 'z')) || | |
1444 | ((line[q] >= 'A') && (line[q] <= 'Z')) || | |
1445 | ((line[q] >= '0') && (line[q] <= '9')) || | |
1446 | (line[q] == '_')) | |
1447 | { | |
1448 | ret.Append( line[q] ); | |
1449 | continue; | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | pos = p; | |
1454 | return ret; | |
1455 | } | |
1456 | } | |
1457 | pos = p; | |
1458 | return ret; | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | return ret; | |
1463 | } | |
1464 | ||
1465 | void wxTextCtrl::OnEraseBackground( wxEraseEvent &event ) | |
1466 | { | |
1467 | event.Skip(); | |
1468 | } | |
1469 | ||
1470 | void wxTextCtrl::DrawLinePart( wxDC &dc, int x, int y, const wxString &toDraw, const wxString &origin, const wxColour &colour ) | |
1471 | { | |
1472 | size_t pos = 0; | |
1473 | size_t len = origin.Len(); | |
1474 | dc.SetTextForeground( colour ); | |
1475 | while (pos < len) | |
1476 | { | |
1477 | while (toDraw[pos] == wxT(' ')) | |
1478 | { | |
1479 | pos++; | |
1480 | if (pos == len) return; | |
1481 | } | |
1482 | ||
1483 | size_t start = pos; | |
1484 | ||
1485 | wxString current; | |
1486 | current += toDraw[pos]; | |
1487 | pos++; | |
1488 | while ( (toDraw[pos] == origin[pos]) && (pos < len)) | |
1489 | { | |
1490 | current += toDraw[pos]; | |
1491 | pos++; | |
1492 | } | |
1493 | ||
1494 | int xx = 0; | |
1495 | wxString tmp = origin.Left( start ); | |
1496 | GetTextExtent( tmp, &xx, NULL, NULL, NULL ); | |
1497 | xx += x; | |
1498 | int yy = y; | |
1499 | dc.DrawText( current, xx, yy ); | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | void wxTextCtrl::DrawLine( wxDC &dc, int x, int y, const wxString &line2, int lineNum ) | |
1504 | { | |
1505 | int selStartY = m_selStartY; | |
1506 | int selEndY = m_selEndY; | |
1507 | int selStartX = m_selStartX; | |
1508 | int selEndX = m_selEndX; | |
1509 | ||
1510 | if ((selStartY > selEndY) || | |
1511 | ((selStartY == selEndY) && (selStartX > selEndX))) | |
1512 | { | |
1513 | int tmp = selStartX; | |
1514 | selStartX = selEndX; | |
1515 | selEndX = tmp; | |
1516 | tmp = selStartY; | |
1517 | selStartY = selEndY; | |
1518 | selEndY = tmp; | |
1519 | } | |
1520 | ||
1521 | wxString line( line2 ); | |
1522 | if (HasFlag(wxTE_PASSWORD)) | |
1523 | { | |
1524 | size_t len = line.Len(); | |
1525 | line = wxString( wxT('*'), len ); | |
1526 | } | |
1527 | ||
1528 | wxString keyword( ' ', line.Len() ); | |
1529 | wxString define( ' ', line.Len() ); | |
1530 | wxString variable( ' ', line.Len() ); | |
1531 | wxString comment( ' ', line.Len() ); | |
1532 | wxString my_string( ' ', line.Len() ); | |
1533 | wxString selection( ' ', line.Len() ); | |
1534 | ||
1535 | if (m_lang != wxSOURCE_LANG_NONE) | |
1536 | { | |
1537 | if (lineNum == m_bracketY) | |
1538 | { | |
1539 | wxString red( ' ', line.Len() ); | |
1540 | if (m_bracketX < (int)line.Len()) | |
1541 | { | |
1542 | red.SetChar( m_bracketX, line[(size_t) (m_bracketX)] ); | |
1543 | line.SetChar( m_bracketX, ' ' ); | |
1544 | dc.SetTextForeground( *wxRED ); | |
1545 | dc.DrawText( red, x, y ); | |
1546 | dc.SetTextForeground( *wxBLACK ); | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | size_t pos = 0; | |
1551 | wxString token( GetNextToken( line, pos ) ); | |
1552 | while (!token.IsNull()) | |
1553 | { | |
1554 | if (m_keywords.Index( token ) != wxNOT_FOUND) | |
1555 | { | |
1556 | size_t end_pos = pos + token.Len(); | |
1557 | for (size_t i = pos; i < end_pos; i++) | |
1558 | { | |
1559 | keyword[i] = line[i]; | |
1560 | line[i] = ' '; | |
1561 | } | |
1562 | } else | |
1563 | if (m_defines.Index( token ) != wxNOT_FOUND) | |
1564 | { | |
1565 | size_t end_pos = pos + token.Len(); | |
1566 | for (size_t i = pos; i < end_pos; i++) | |
1567 | { | |
1568 | define[i] = line[i]; | |
1569 | line[i] = ' '; | |
1570 | } | |
1571 | } else | |
1572 | if ((m_variables.Index( token ) != wxNOT_FOUND) || | |
1573 | ((token.Len() > 2) && (token[(size_t) (0)] == 'w') && (token[(size_t) (1)] == 'x'))) | |
1574 | { | |
1575 | size_t end_pos = pos + token.Len(); | |
1576 | for (size_t i = pos; i < end_pos; i++) | |
1577 | { | |
1578 | variable[i] = line[i]; | |
1579 | line[i] = ' '; | |
1580 | } | |
1581 | } else | |
1582 | if ((token.Len() >= 2) && (token[(size_t) (0)] == '/') && (token[(size_t) (1)] == '/') && (m_lang == wxSOURCE_LANG_CPP)) | |
1583 | { | |
1584 | size_t end_pos = pos + token.Len(); | |
1585 | for (size_t i = pos; i < end_pos; i++) | |
1586 | { | |
1587 | comment[i] = line[i]; | |
1588 | line[i] = ' '; | |
1589 | } | |
1590 | } else | |
1591 | if ((token[(size_t) (0)] == '#') && | |
1592 | ((m_lang == wxSOURCE_LANG_PYTHON) || (m_lang == wxSOURCE_LANG_PERL))) | |
1593 | { | |
1594 | size_t end_pos = pos + token.Len(); | |
1595 | for (size_t i = pos; i < end_pos; i++) | |
1596 | { | |
1597 | comment[i] = line[i]; | |
1598 | line[i] = ' '; | |
1599 | } | |
1600 | } else | |
1601 | if ((token[(size_t) (0)] == '"') || (token[(size_t) (0)] == '\'')) | |
1602 | { | |
1603 | size_t end_pos = pos + token.Len(); | |
1604 | for (size_t i = pos; i < end_pos; i++) | |
1605 | { | |
1606 | my_string[i] = line[i]; | |
1607 | line[i] = ' '; | |
1608 | } | |
1609 | } | |
1610 | pos += token.Len(); | |
1611 | token = GetNextToken( line, pos ); | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | if ((lineNum < selStartY) || (lineNum > selEndY)) | |
1616 | { | |
1617 | DrawLinePart( dc, x, y, line, line2, *wxBLACK ); | |
1618 | DrawLinePart( dc, x, y, selection, line2, *wxWHITE ); | |
1619 | DrawLinePart( dc, x, y, keyword, line2, m_keywordColour ); | |
1620 | DrawLinePart( dc, x, y, define, line2, m_defineColour ); | |
1621 | DrawLinePart( dc, x, y, variable, line2, m_variableColour ); | |
1622 | DrawLinePart( dc, x, y, comment, line2, m_commentColour ); | |
1623 | DrawLinePart( dc, x, y, my_string, line2, m_stringColour ); | |
1624 | return; | |
1625 | } | |
1626 | ||
1627 | if (selStartY == selEndY) | |
1628 | { | |
1629 | // int xx = selStartX*m_charWidth; | |
1630 | int xx = PosToPixel( lineNum, selStartX ); | |
1631 | // int ww = (selEndX-selStartX)*m_charWidth; | |
1632 | int ww = PosToPixel( lineNum, selEndX ) - xx; | |
1633 | dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); | |
1634 | ||
1635 | for (size_t i = (size_t)selStartX; i < (size_t)selEndX; i++) | |
1636 | { | |
1637 | selection[i] = line[i]; | |
1638 | line[i] = ' '; | |
1639 | } | |
1640 | } else | |
1641 | if ((lineNum > selStartY) && (lineNum < selEndY)) | |
1642 | { | |
1643 | dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); | |
1644 | ||
1645 | for (size_t i = 0; i < line.Len(); i++) | |
1646 | { | |
1647 | selection[i] = line[i]; | |
1648 | line[i] = ' '; | |
1649 | } | |
1650 | } else | |
1651 | if (lineNum == selStartY) | |
1652 | { | |
1653 | // int xx = selStartX*m_charWidth; | |
1654 | int xx = PosToPixel( lineNum, selStartX ); | |
1655 | dc.DrawRectangle( xx+2, lineNum*m_lineHeight+2, 10000, m_lineHeight ); | |
1656 | ||
1657 | for (size_t i = (size_t)selStartX; i < line.Len(); i++) | |
1658 | { | |
1659 | selection[i] = line[i]; | |
1660 | line[i] = ' '; | |
1661 | } | |
1662 | } else | |
1663 | if (lineNum == selEndY) | |
1664 | { | |
1665 | // int ww = selEndX*m_charWidth; | |
1666 | int ww = PosToPixel( lineNum, selEndX ); | |
1667 | dc.DrawRectangle( 0+2, lineNum*m_lineHeight+2, ww, m_lineHeight ); | |
1668 | ||
1669 | for (size_t i = 0; i < (size_t)selEndX; i++) | |
1670 | { | |
1671 | selection[i] = line[i]; | |
1672 | line[i] = ' '; | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | DrawLinePart( dc, x, y, line, line2, *wxBLACK ); | |
1677 | DrawLinePart( dc, x, y, selection, line2, *wxWHITE ); | |
1678 | DrawLinePart( dc, x, y, keyword, line2, m_keywordColour ); | |
1679 | DrawLinePart( dc, x, y, define, line2, m_defineColour ); | |
1680 | DrawLinePart( dc, x, y, variable, line2, m_variableColour ); | |
1681 | DrawLinePart( dc, x, y, comment, line2, m_commentColour ); | |
1682 | DrawLinePart( dc, x, y, my_string, line2, m_stringColour ); | |
1683 | } | |
1684 | ||
1685 | void wxTextCtrl::OnPaint( wxPaintEvent &event ) | |
1686 | { | |
1687 | wxPaintDC dc(this); | |
1688 | ||
1689 | if (m_lines.GetCount() == 0) return; | |
1690 | ||
1691 | PrepareDC( dc ); | |
1692 | ||
1693 | dc.SetFont( m_sourceFont ); | |
1694 | ||
1695 | int scroll_y = 0; | |
1696 | GetViewStart( NULL, &scroll_y ); | |
1697 | ||
1698 | // We have a inner border of two pixels | |
1699 | // around the text, so scroll units do | |
1700 | // not correspond to lines. | |
1701 | if (scroll_y > 0) scroll_y--; | |
1702 | ||
1703 | int size_x = 0; | |
1704 | int size_y = 0; | |
1705 | GetClientSize( &size_x, &size_y ); | |
1706 | ||
1707 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1708 | dc.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT), wxSOLID ) ); | |
1709 | int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+2 ); | |
1710 | for (int i = scroll_y; i < upper; i++) | |
1711 | { | |
1712 | int x = 0+2; | |
1713 | int y = i*m_lineHeight+2; | |
1714 | int w = 10000; | |
1715 | int h = m_lineHeight; | |
1716 | CalcScrolledPosition( x,y,&x,&y ); | |
1717 | if (IsExposed(x,y,w,h)) | |
1718 | DrawLine( dc, 0+2, i*m_lineHeight+2, m_lines[i].m_text, i ); | |
1719 | } | |
1720 | ||
1721 | if (m_editable && (FindFocus() == this)) | |
1722 | { | |
1723 | ///dc.SetBrush( *wxRED_BRUSH ); | |
1724 | dc.SetBrush( *wxBLACK_BRUSH ); | |
1725 | // int xx = m_cursorX*m_charWidth; | |
1726 | int xx = PosToPixel( m_cursorY, m_cursorX ); | |
1727 | dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight ); | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | void wxTextCtrl::OnMouse( wxMouseEvent &event ) | |
1732 | { | |
1733 | if (m_lines.GetCount() == 0) return; | |
1734 | ||
1735 | ||
1736 | #if 0 // there is no middle button on iPAQs | |
1737 | if (event.MiddleDown()) | |
1738 | { | |
1739 | Paste( TRUE ); | |
1740 | return; | |
1741 | } | |
1742 | #endif | |
1743 | ||
1744 | if (event.LeftDClick()) | |
1745 | { | |
1746 | DoDClick(); | |
1747 | return; | |
1748 | } | |
1749 | ||
1750 | if (event.LeftDown()) | |
1751 | { | |
1752 | m_capturing = TRUE; | |
1753 | CaptureMouse(); | |
1754 | } | |
1755 | ||
1756 | if (event.LeftUp()) | |
1757 | { | |
1758 | m_capturing = FALSE; | |
1759 | ReleaseMouse(); | |
1760 | } | |
1761 | ||
1762 | if (event.LeftDown() || | |
1763 | (event.LeftIsDown() && m_capturing)) | |
1764 | { | |
1765 | int x = event.GetX(); | |
1766 | int y = event.GetY(); | |
1767 | CalcUnscrolledPosition( x, y, &x, &y ); | |
1768 | y /= m_lineHeight; | |
1769 | // x /= m_charWidth; | |
1770 | x = PixelToPos( y, x ); | |
1771 | MoveCursor( | |
1772 | wxMin( 1000, wxMax( 0, x ) ), | |
1773 | wxMin( (int)m_lines.GetCount()-1, wxMax( 0, y ) ), | |
1774 | event.ShiftDown() || !event.LeftDown() ); | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | void wxTextCtrl::OnChar( wxKeyEvent &event ) | |
1779 | { | |
1780 | if (m_lines.GetCount() == 0) return; | |
1781 | ||
1782 | if (!m_editable) return; | |
1783 | ||
1784 | int size_x = 0; | |
1785 | int size_y = 0; | |
1786 | GetClientSize( &size_x, &size_y ); | |
1787 | size_x /= m_charWidth; | |
1788 | size_y /= m_lineHeight; | |
1789 | size_y--; | |
1790 | ||
1791 | if (event.ShiftDown()) | |
1792 | { | |
1793 | switch (event.GetKeyCode()) | |
1794 | { | |
1795 | case '4': event.m_keyCode = WXK_LEFT; break; | |
1796 | case '8': event.m_keyCode = WXK_UP; break; | |
1797 | case '6': event.m_keyCode = WXK_RIGHT; break; | |
1798 | case '2': event.m_keyCode = WXK_DOWN; break; | |
1799 | case '9': event.m_keyCode = WXK_PRIOR; break; | |
1800 | case '3': event.m_keyCode = WXK_NEXT; break; | |
1801 | case '7': event.m_keyCode = WXK_HOME; break; | |
1802 | case '1': event.m_keyCode = WXK_END; break; | |
1803 | case '0': event.m_keyCode = WXK_INSERT; break; | |
1804 | } | |
1805 | } | |
1806 | ||
1807 | switch (event.GetKeyCode()) | |
1808 | { | |
1809 | case WXK_UP: | |
1810 | { | |
1811 | if (m_ignoreInput) return; | |
1812 | if (m_cursorY > 0) | |
1813 | MoveCursor( m_cursorX, m_cursorY-1, event.ShiftDown() ); | |
1814 | m_ignoreInput = TRUE; | |
1815 | return; | |
1816 | } | |
1817 | case WXK_DOWN: | |
1818 | { | |
1819 | if (m_ignoreInput) return; | |
1820 | if (m_cursorY < (int)(m_lines.GetCount()-1)) | |
1821 | MoveCursor( m_cursorX, m_cursorY+1, event.ShiftDown() ); | |
1822 | m_ignoreInput = TRUE; | |
1823 | return; | |
1824 | } | |
1825 | case WXK_LEFT: | |
1826 | { | |
1827 | if (m_ignoreInput) return; | |
1828 | if (m_cursorX > 0) | |
1829 | { | |
1830 | MoveCursor( m_cursorX-1, m_cursorY, event.ShiftDown() ); | |
1831 | } | |
1832 | else | |
1833 | { | |
1834 | if (m_cursorY > 0) | |
1835 | MoveCursor( m_lines[m_cursorY-1].m_text.Len(), m_cursorY-1, event.ShiftDown() ); | |
1836 | } | |
1837 | m_ignoreInput = TRUE; | |
1838 | return; | |
1839 | } | |
1840 | case WXK_RIGHT: | |
1841 | { | |
1842 | if (m_ignoreInput) return; | |
1843 | if (m_cursorX < 1000) | |
1844 | MoveCursor( m_cursorX+1, m_cursorY, event.ShiftDown() ); | |
1845 | m_ignoreInput = TRUE; | |
1846 | return; | |
1847 | } | |
1848 | case WXK_HOME: | |
1849 | { | |
1850 | if (event.ControlDown()) | |
1851 | MoveCursor( 0, 0, event.ShiftDown() ); | |
1852 | else | |
1853 | MoveCursor( 0, m_cursorY, event.ShiftDown() ); | |
1854 | return; | |
1855 | } | |
1856 | case WXK_END: | |
1857 | { | |
1858 | if (event.ControlDown()) | |
1859 | MoveCursor( 0, m_lines.GetCount()-1, event.ShiftDown() ); | |
1860 | else | |
1861 | MoveCursor( m_lines[m_cursorY].m_text.Len(), m_cursorY, event.ShiftDown() ); | |
1862 | return; | |
1863 | } | |
1864 | case WXK_NEXT: | |
1865 | { | |
1866 | if (m_ignoreInput) return; | |
1867 | MoveCursor( m_cursorX, wxMin( (int)(m_lines.GetCount()-1), m_cursorY+size_y ), event.ShiftDown() ); | |
1868 | m_ignoreInput = TRUE; | |
1869 | return; | |
1870 | } | |
1871 | case WXK_PRIOR: | |
1872 | { | |
1873 | if (m_ignoreInput) return; | |
1874 | MoveCursor( m_cursorX, wxMax( 0, m_cursorY-size_y ), event.ShiftDown() ); | |
1875 | m_ignoreInput = TRUE; | |
1876 | return; | |
1877 | } | |
1878 | case WXK_INSERT: | |
1879 | { | |
1880 | if (event.ShiftDown()) | |
1881 | Paste(); | |
1882 | else if (event.ControlDown()) | |
1883 | Copy(); | |
1884 | else | |
1885 | m_overwrite = !m_overwrite; | |
1886 | return; | |
1887 | } | |
1888 | case WXK_RETURN: | |
1889 | { | |
1890 | if (m_windowStyle & wxPROCESS_ENTER) | |
1891 | { | |
1892 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); | |
1893 | event.SetEventObject(this); | |
1894 | event.SetString(GetValue()); | |
1895 | if (GetEventHandler()->ProcessEvent(event)) return; | |
1896 | } | |
1897 | ||
1898 | if (IsSingleLine()) | |
1899 | { | |
1900 | event.Skip(); | |
1901 | return; | |
1902 | } | |
1903 | ||
1904 | if (HasSelection()) | |
1905 | Delete(); | |
1906 | DoReturn(); | |
1907 | return; | |
1908 | } | |
1909 | case WXK_TAB: | |
1910 | { | |
1911 | if (HasSelection()) | |
1912 | Delete(); | |
1913 | bool save_overwrite = m_overwrite; | |
1914 | m_overwrite = FALSE; | |
1915 | int i = 4-(m_cursorX % 4); | |
1916 | if (i == 0) i = 4; | |
1917 | for (int c = 0; c < i; c++) | |
1918 | DoChar( ' ' ); | |
1919 | m_overwrite = save_overwrite; | |
1920 | return; | |
1921 | } | |
1922 | case WXK_BACK: | |
1923 | { | |
1924 | if (HasSelection()) | |
1925 | Delete(); | |
1926 | else | |
1927 | DoBack(); | |
1928 | return; | |
1929 | } | |
1930 | case WXK_DELETE: | |
1931 | { | |
1932 | if (HasSelection()) | |
1933 | Delete(); | |
1934 | else | |
1935 | DoDelete(); | |
1936 | return; | |
1937 | } | |
1938 | default: | |
1939 | { | |
1940 | if ( (event.GetKeyCode() >= 'a') && | |
1941 | (event.GetKeyCode() <= 'z') && | |
1942 | (event.AltDown()) ) | |
1943 | { | |
1944 | // Alt-F etc. | |
1945 | event.Skip(); | |
1946 | return; | |
1947 | } | |
1948 | ||
1949 | if ( (event.GetKeyCode() >= 32) && | |
1950 | (event.GetKeyCode() <= 255) && | |
1951 | !(event.ControlDown() && !event.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr | |
1952 | { | |
1953 | if (HasSelection()) | |
1954 | Delete(); | |
1955 | DoChar( (char) event.GetKeyCode() ); | |
1956 | return; | |
1957 | } | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | event.Skip(); | |
1962 | } | |
1963 | ||
1964 | void wxTextCtrl::OnInternalIdle() | |
1965 | { | |
1966 | m_ignoreInput = FALSE; | |
1967 | ||
1968 | if (m_lang != wxSOURCE_LANG_NONE) | |
1969 | SearchForBrackets(); | |
1970 | } | |
1971 | ||
1972 | void wxTextCtrl::Indent() | |
1973 | { | |
1974 | int startY = m_cursorY; | |
1975 | int endY = m_cursorY; | |
1976 | if (HasSelection()) | |
1977 | { | |
1978 | startY = m_selStartY; | |
1979 | endY = m_selEndY; | |
1980 | if (endY < startY) | |
1981 | { | |
1982 | int tmp = startY; | |
1983 | startY = endY; | |
1984 | endY = tmp; | |
1985 | } | |
1986 | } | |
1987 | ||
1988 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) ); | |
1989 | ||
1990 | for (int i = startY; i <= endY; i++) | |
1991 | { | |
1992 | m_lines[i].m_text.insert( 0u, wxT(" ") ); | |
1993 | RefreshLine( i ); | |
1994 | } | |
1995 | } | |
1996 | ||
1997 | void wxTextCtrl::Unindent() | |
1998 | { | |
1999 | int startY = m_cursorY; | |
2000 | int endY = m_cursorY; | |
2001 | if (HasSelection()) | |
2002 | { | |
2003 | startY = m_selStartY; | |
2004 | endY = m_selEndY; | |
2005 | if (endY < startY) | |
2006 | { | |
2007 | int tmp = startY; | |
2008 | startY = endY; | |
2009 | endY = tmp; | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) ); | |
2014 | ||
2015 | for (int i = startY; i <= endY; i++) | |
2016 | { | |
2017 | for (int n = 0; n < 4; n++) | |
2018 | { | |
2019 | if (m_lines[i].m_text[0u] == wxT(' ')) | |
2020 | m_lines[i].m_text.erase(0u,1u); | |
2021 | } | |
2022 | RefreshLine( i ); | |
2023 | } | |
2024 | } | |
2025 | bool wxTextCtrl::HasSelection() | |
2026 | { | |
2027 | return ((m_selStartY != m_selEndY) || (m_selStartX != m_selEndX)); | |
2028 | } | |
2029 | ||
2030 | void wxTextCtrl::ClearSelection() | |
2031 | { | |
2032 | m_selStartX = -1; | |
2033 | m_selStartY = -1; | |
2034 | m_selEndX = -1; | |
2035 | m_selEndY = -1; | |
2036 | } | |
2037 | ||
2038 | void wxTextCtrl::RefreshLine( int n ) | |
2039 | { | |
2040 | int y = n*m_lineHeight; | |
2041 | int x = 0; | |
2042 | CalcScrolledPosition( x, y, &x, &y ); | |
2043 | wxRect rect( 0+2, y+2, 10000, m_lineHeight ); | |
2044 | Refresh( TRUE, &rect ); | |
2045 | } | |
2046 | ||
2047 | void wxTextCtrl::RefreshDown( int n ) | |
2048 | { | |
2049 | int size_x = 0; | |
2050 | int size_y = 0; | |
2051 | GetClientSize( &size_x, &size_y ); | |
2052 | ||
2053 | int view_x = 0; | |
2054 | int view_y = 0; | |
2055 | GetViewStart( &view_x, &view_y ); | |
2056 | ||
2057 | if (n < view_y) | |
2058 | { | |
2059 | Refresh(); | |
2060 | } | |
2061 | else | |
2062 | { | |
2063 | int y = n*m_lineHeight; | |
2064 | int x = 0; | |
2065 | CalcScrolledPosition( x, y, &x, &y ); | |
2066 | ||
2067 | wxRect rect( 0+2, y+2, 10000, size_y ); | |
2068 | Refresh( TRUE, &rect ); | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) | |
2073 | { | |
2074 | if (!m_editable) return; | |
2075 | ||
2076 | // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE)) | |
2077 | { | |
2078 | if (new_x > (int) (m_lines[new_y].m_text.Len())) | |
2079 | new_x = m_lines[new_y].m_text.Len(); | |
2080 | } | |
2081 | ||
2082 | if ((new_x == m_cursorX) && (new_y == m_cursorY)) return; | |
2083 | ||
2084 | bool no_cursor_refresh = FALSE; | |
2085 | bool has_selection = HasSelection(); | |
2086 | ||
2087 | if (shift) | |
2088 | { | |
2089 | int x,y,w,h; | |
2090 | bool erase_background = TRUE; | |
2091 | ||
2092 | if (!has_selection) | |
2093 | { | |
2094 | m_selStartX = m_cursorX; | |
2095 | m_selStartY = m_cursorY; | |
2096 | ||
2097 | x = 0; | |
2098 | w = 10000; | |
2099 | if (new_y > m_selStartY) | |
2100 | { | |
2101 | y = m_selStartY*m_lineHeight; | |
2102 | h = (new_y-m_selStartY+1)*m_lineHeight; | |
2103 | } | |
2104 | else if (new_y == m_selStartY) | |
2105 | { | |
2106 | x = PosToPixel( new_y, m_selStartX ); | |
2107 | w = PosToPixel( new_y, new_x ) - x; | |
2108 | if (w < 0) | |
2109 | { | |
2110 | x += w; | |
2111 | w = -w + 2; // +2 for the cursor | |
2112 | } | |
2113 | y = m_selStartY*m_lineHeight; | |
2114 | h = m_lineHeight; | |
2115 | } | |
2116 | else | |
2117 | { | |
2118 | y = new_y*m_lineHeight; | |
2119 | h = (-new_y+m_selStartY+1)*m_lineHeight; | |
2120 | } | |
2121 | ||
2122 | no_cursor_refresh = TRUE; | |
2123 | m_cursorX = new_x; | |
2124 | m_cursorY = new_y; | |
2125 | } | |
2126 | else | |
2127 | { | |
2128 | if (new_y == m_selEndY) | |
2129 | { | |
2130 | y = new_y *m_lineHeight; | |
2131 | h = m_lineHeight; | |
2132 | if (m_selEndX > new_x) | |
2133 | { | |
2134 | // x = new_x*m_charWidth; | |
2135 | x = PosToPixel( new_y, new_x ); | |
2136 | // w = (m_selEndX-new_x)*m_charWidth; | |
2137 | w = PosToPixel( new_y, m_selEndX ) - x; | |
2138 | } | |
2139 | else | |
2140 | { | |
2141 | // x = m_selEndX*m_charWidth; | |
2142 | x = PosToPixel( new_y, m_selEndX ); | |
2143 | // w = (-m_selEndX+new_x)*m_charWidth; | |
2144 | w = PosToPixel( new_y, new_x ) - x; | |
2145 | } | |
2146 | } | |
2147 | else | |
2148 | { | |
2149 | x = 0; | |
2150 | w = 10000; | |
2151 | if (new_y > m_selEndY) | |
2152 | { | |
2153 | y = m_selEndY*m_lineHeight; | |
2154 | h = (new_y-m_selEndY+1) * m_lineHeight; | |
2155 | ||
2156 | erase_background = ((m_selEndY < m_selStartY) || | |
2157 | ((m_selEndY == m_selStartY) && (m_selEndX < m_selStartX))); | |
2158 | } | |
2159 | else | |
2160 | { | |
2161 | y = new_y*m_lineHeight; | |
2162 | h = (-new_y+m_selEndY+1) * m_lineHeight; | |
2163 | ||
2164 | erase_background = ((m_selEndY > m_selStartY) || | |
2165 | ((m_selEndY == m_selStartY) && (m_selEndX > m_selStartX))); | |
2166 | } | |
2167 | no_cursor_refresh = TRUE; | |
2168 | m_cursorX = new_x; | |
2169 | m_cursorY = new_y; | |
2170 | } | |
2171 | } | |
2172 | ||
2173 | m_selEndX = new_x; | |
2174 | m_selEndY = new_y; | |
2175 | ||
2176 | CalcScrolledPosition( x, y, &x, &y ); | |
2177 | wxRect rect( x+2, y+2, w, h ); | |
2178 | Refresh( erase_background, &rect ); | |
2179 | } | |
2180 | else | |
2181 | { | |
2182 | if (has_selection) | |
2183 | { | |
2184 | int ry1 = m_selEndY; | |
2185 | int ry2 = m_selStartY; | |
2186 | m_selEndX = -1; | |
2187 | m_selEndY = -1; | |
2188 | m_selStartX = -1; | |
2189 | m_selStartY = -1; | |
2190 | ||
2191 | if (ry1 > ry2) | |
2192 | { | |
2193 | int tmp = ry2; | |
2194 | ry2 = ry1; | |
2195 | ry1 = tmp; | |
2196 | } | |
2197 | ||
2198 | int x = 0; | |
2199 | int y = ry1*m_lineHeight; | |
2200 | CalcScrolledPosition( x, y, &x, &y ); | |
2201 | wxRect rect( 0, y+2, 10000, (ry2-ry1+1)*m_lineHeight ); | |
2202 | ||
2203 | Refresh( TRUE, &rect ); | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | /* | |
2208 | printf( "startx %d starty %d endx %d endy %d\n", | |
2209 | m_selStartX, m_selStartY, m_selEndX, m_selEndY ); | |
2210 | ||
2211 | printf( "has %d\n", (int)HasSelection() ); | |
2212 | */ | |
2213 | ||
2214 | if (!no_cursor_refresh) | |
2215 | { | |
2216 | // int x = m_cursorX*m_charWidth; | |
2217 | int x = PosToPixel( m_cursorY, m_cursorX ); | |
2218 | int y = m_cursorY*m_lineHeight; | |
2219 | CalcScrolledPosition( x, y, &x, &y ); | |
2220 | wxRect rect( x+2, y+2, 4, m_lineHeight+2 ); | |
2221 | ||
2222 | m_cursorX = new_x; | |
2223 | m_cursorY = new_y; | |
2224 | ||
2225 | Refresh( TRUE, &rect ); | |
2226 | ||
2227 | if (FindFocus() == this) | |
2228 | { | |
2229 | wxClientDC dc(this); | |
2230 | PrepareDC( dc ); | |
2231 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
2232 | //dc.SetBrush( *wxRED_BRUSH ); | |
2233 | dc.SetBrush( *wxBLACK_BRUSH ); | |
2234 | // int xx = m_cursorX*m_charWidth; | |
2235 | int xx = PosToPixel( m_cursorY, m_cursorX ); | |
2236 | dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight ); | |
2237 | } | |
2238 | } | |
2239 | ||
2240 | int size_x = 0; | |
2241 | int size_y = 0; | |
2242 | GetClientSize( &size_x, &size_y ); | |
2243 | size_x /= m_charWidth; | |
2244 | size_y /= m_lineHeight; | |
2245 | ||
2246 | int view_x = 0; | |
2247 | int view_y = 0; | |
2248 | GetViewStart( &view_x, &view_y ); | |
2249 | ||
2250 | if (centre) | |
2251 | { | |
2252 | int sy = m_cursorY - (size_y/2); | |
2253 | if (sy < 0) sy = 0; | |
2254 | Scroll( -1, sy ); | |
2255 | } | |
2256 | else | |
2257 | { | |
2258 | if (m_cursorY < view_y) | |
2259 | Scroll( -1, m_cursorY ); | |
2260 | else if (m_cursorY > view_y+size_y-1) | |
2261 | Scroll( -1, m_cursorY-size_y+1 ); | |
2262 | } | |
2263 | ||
2264 | //int xx = m_cursorX; | |
2265 | int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth; | |
2266 | ||
2267 | if (xx < view_x) | |
2268 | Scroll( xx, -1 ); | |
2269 | else if (xx > view_x+size_x-1) | |
2270 | Scroll( xx-size_x+1, -1 ); | |
2271 | } | |
2272 | ||
2273 | void wxTextCtrl::MyAdjustScrollbars() | |
2274 | { | |
2275 | if (IsSingleLine()) | |
2276 | return; | |
2277 | ||
2278 | int y_range = m_lines.GetCount(); | |
2279 | ||
2280 | int height = 0; | |
2281 | GetClientSize( NULL, &height ); | |
2282 | height -= 4; | |
2283 | if (height >= (int)m_lines.GetCount() *m_lineHeight) | |
2284 | y_range = 0; | |
2285 | ||
2286 | int view_x = 0; | |
2287 | int view_y = 0; | |
2288 | GetViewStart( &view_x, &view_y ); | |
2289 | ||
2290 | SetScrollbars( m_charWidth, m_lineHeight, m_longestLine+2, y_range, view_x, view_y ); | |
2291 | } | |
2292 | ||
2293 | //----------------------------------------------------------------------------- | |
2294 | // clipboard handlers | |
2295 | //----------------------------------------------------------------------------- | |
2296 | ||
2297 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
2298 | { | |
2299 | Cut(); | |
2300 | } | |
2301 | ||
2302 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2303 | { | |
2304 | Copy(); | |
2305 | } | |
2306 | ||
2307 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2308 | { | |
2309 | Paste(); | |
2310 | } | |
2311 | ||
2312 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2313 | { | |
2314 | Undo(); | |
2315 | } | |
2316 | ||
2317 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2318 | { | |
2319 | Redo(); | |
2320 | } | |
2321 | ||
2322 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2323 | { | |
2324 | event.Enable( CanCut() ); | |
2325 | } | |
2326 | ||
2327 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2328 | { | |
2329 | event.Enable( CanCopy() ); | |
2330 | } | |
2331 | ||
2332 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2333 | { | |
2334 | event.Enable( CanPaste() ); | |
2335 | } | |
2336 | ||
2337 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2338 | { | |
2339 | event.Enable( CanUndo() ); | |
2340 | } | |
2341 | ||
2342 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2343 | { | |
2344 | event.Enable( CanRedo() ); | |
2345 | } | |
2346 | ||
2347 | wxSize wxTextCtrl::DoGetBestSize() const | |
2348 | { | |
2349 | if (IsSingleLine()) | |
2350 | { | |
2351 | wxSize ret(80, m_lineHeight + 4); | |
2352 | ||
2353 | if (HasFlag(wxBORDER_SUNKEN) || HasFlag(wxBORDER_RAISED)) | |
2354 | ret.y += 4; | |
2355 | ||
2356 | if (HasFlag(wxBORDER_SIMPLE)) | |
2357 | ret.y += 2; | |
2358 | ||
2359 | return ret; | |
2360 | } | |
2361 | else | |
2362 | { | |
2363 | return wxSize(80, 60); | |
2364 | } | |
2365 | } | |
2366 | ||
2367 | // ---------------------------------------------------------------------------- | |
2368 | // freeze/thaw | |
2369 | // ---------------------------------------------------------------------------- | |
2370 | ||
2371 | void wxTextCtrl::Freeze() | |
2372 | { | |
2373 | } | |
2374 | ||
2375 | void wxTextCtrl::Thaw() | |
2376 | { | |
2377 | } | |
2378 | ||
2379 | void wxTextCtrl::OnSetFocus( wxFocusEvent& event ) | |
2380 | { | |
2381 | // To hide or show caret, as appropriate | |
2382 | Refresh(); | |
2383 | } | |
2384 | ||
2385 | void wxTextCtrl::OnKillFocus( wxFocusEvent& event ) | |
2386 | { | |
2387 | // To hide or show caret, as appropriate | |
2388 | Refresh(); | |
2389 | } | |
2390 | ||
2391 | // ---------------------------------------------------------------------------- | |
2392 | // text control scrolling | |
2393 | // ---------------------------------------------------------------------------- | |
2394 | ||
2395 | bool wxTextCtrl::ScrollLines(int lines) | |
2396 | { | |
2397 | wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented"); | |
2398 | ||
2399 | return FALSE; | |
2400 | } | |
2401 | ||
2402 | bool wxTextCtrl::ScrollPages(int pages) | |
2403 | { | |
2404 | wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented"); | |
2405 | ||
2406 | return FALSE; | |
2407 | } | |
2408 |