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