]>
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 | ||
23 | #include "wx/univ/inphand.h" | |
24 | #include "wx/univ/renderer.h" | |
25 | #include "wx/univ/colschem.h" | |
26 | #include "wx/univ/theme.h" | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // helpers | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | wxSourceUndoStep::wxSourceUndoStep( wxSourceUndo type, int y1, int y2, wxTextCtrl *owner ) | |
33 | { | |
34 | m_type = type; | |
35 | m_y1 = y1; | |
36 | m_y2 = y2; | |
37 | m_owner = owner; | |
38 | ||
39 | m_cursorX = m_owner->GetCursorX(); | |
40 | m_cursorY = m_owner->GetCursorY(); | |
41 | ||
42 | if (m_type == wxSOURCE_UNDO_LINE) | |
43 | { | |
44 | m_text = m_owner->m_lines[m_y1].m_text; | |
45 | } else | |
46 | if (m_type == wxSOURCE_UNDO_ENTER) | |
47 | { | |
48 | m_text = m_owner->m_lines[m_y1].m_text; | |
49 | } else | |
50 | if (m_type == wxSOURCE_UNDO_BACK) | |
51 | { | |
52 | for (int i = m_y1; i < m_y2+2; i++) | |
53 | { | |
54 | if (i >= (int)m_owner->m_lines.GetCount()) | |
55 | m_lines.Add( wxT("") ); | |
56 | else | |
57 | m_lines.Add( m_owner->m_lines[i].m_text ); | |
58 | } | |
59 | } else | |
60 | if (m_type == wxSOURCE_UNDO_DELETE) | |
61 | { | |
62 | for (int i = m_y1; i < m_y2+1; i++) | |
63 | { | |
64 | m_lines.Add( m_owner->m_lines[i].m_text ); | |
65 | } | |
66 | } else | |
67 | if (m_type == wxSOURCE_UNDO_PASTE) | |
68 | { | |
69 | m_text = m_owner->m_lines[m_y1].m_text; | |
70 | } | |
71 | } | |
72 | ||
73 | void wxSourceUndoStep::Undo() | |
74 | { | |
75 | if (m_type == wxSOURCE_UNDO_LINE) | |
76 | { | |
77 | m_owner->m_lines[m_y1].m_text = m_text; | |
78 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
79 | m_owner->RefreshLine( m_y1 ); | |
80 | } else | |
81 | if (m_type == wxSOURCE_UNDO_ENTER) | |
82 | { | |
83 | m_owner->m_lines[m_y1].m_text = m_text; | |
84 | m_owner->m_lines.RemoveAt( m_y1+1 ); | |
85 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
86 | m_owner->RefreshDown( m_y1 ); | |
87 | } else | |
88 | if (m_type == wxSOURCE_UNDO_BACK) | |
89 | { | |
90 | m_owner->m_lines[m_y1].m_text = m_lines[0]; | |
91 | m_owner->m_lines.Insert( new wxSourceLine( m_lines[1] ), m_y1+1 ); | |
92 | m_owner->MyAdjustScrollbars(); | |
93 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
94 | m_owner->RefreshDown( m_y1 ); | |
95 | } else | |
96 | if (m_type == wxSOURCE_UNDO_DELETE) | |
97 | { | |
98 | m_owner->m_lines[m_y1].m_text = m_lines[0]; | |
99 | for (int i = 1; i < (int)m_lines.GetCount(); i++) | |
100 | m_owner->m_lines.Insert( new wxSourceLine( m_lines[i] ), m_y1+i ); | |
101 | m_owner->MyAdjustScrollbars(); | |
102 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
103 | m_owner->RefreshDown( m_y1 ); | |
104 | } else | |
105 | if (m_type == wxSOURCE_UNDO_PASTE) | |
106 | { | |
107 | m_owner->m_lines[m_y1].m_text = m_text; | |
108 | for (int i = 0; i < m_y2-m_y1; i++) | |
109 | m_owner->m_lines.RemoveAt( m_y1+1 ); | |
110 | m_owner->MyAdjustScrollbars(); | |
111 | m_owner->MoveCursor( m_cursorX, m_cursorY ); | |
112 | m_owner->RefreshDown( m_y1 ); | |
113 | } else | |
114 | if (m_type == wxSOURCE_UNDO_INSERT_LINE) | |
115 | { | |
116 | m_owner->m_lines.RemoveAt( m_y1 ); | |
117 | m_owner->MyAdjustScrollbars(); | |
118 | m_owner->MoveCursor( 0, m_y1 ); | |
119 | m_owner->RefreshDown( m_y1 ); | |
120 | } | |
121 | } | |
122 | ||
123 | #include "wx/arrimpl.cpp" | |
124 | WX_DEFINE_OBJARRAY(wxSourceLineArray); | |
125 | ||
126 | //----------------------------------------------------------------------------- | |
127 | // wxTextCtrl | |
128 | //----------------------------------------------------------------------------- | |
129 | ||
130 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
131 | ||
132 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
133 | EVT_PAINT(wxTextCtrl::OnPaint) | |
134 | EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) | |
135 | EVT_CHAR(wxTextCtrl::OnChar) | |
136 | EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse) | |
137 | EVT_IDLE(wxTextCtrl::OnIdle) | |
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 | wxNode *node = m_undos.Nth( m_undos.GetCount()-1 ); | |
798 | wxSourceUndoStep *undo = (wxSourceUndoStep*) node->Data(); | |
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.KeyCode() >= 'a') && | |
1941 | (event.KeyCode() <= 'z') && | |
1942 | (event.AltDown()) ) | |
1943 | { | |
1944 | // Alt-F etc. | |
1945 | event.Skip(); | |
1946 | return; | |
1947 | } | |
1948 | ||
1949 | if ( (event.KeyCode() >= 32) && | |
1950 | (event.KeyCode() <= 255) && | |
1951 | !(event.ControlDown() && !event.AltDown()) ) // filters out Ctrl-X but leaves Alt-Gr | |
1952 | { | |
1953 | if (HasSelection()) | |
1954 | Delete(); | |
1955 | DoChar( (char) event.KeyCode() ); | |
1956 | return; | |
1957 | } | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | event.Skip(); | |
1962 | } | |
1963 | ||
1964 | void wxTextCtrl::OnIdle( wxIdleEvent &event ) | |
1965 | { | |
1966 | m_ignoreInput = FALSE; | |
1967 | ||
1968 | if (m_lang != wxSOURCE_LANG_NONE) | |
1969 | SearchForBrackets(); | |
1970 | ||
1971 | event.Skip( TRUE ); | |
1972 | } | |
1973 | ||
1974 | void wxTextCtrl::Indent() | |
1975 | { | |
1976 | int startY = m_cursorY; | |
1977 | int endY = m_cursorY; | |
1978 | if (HasSelection()) | |
1979 | { | |
1980 | startY = m_selStartY; | |
1981 | endY = m_selEndY; | |
1982 | if (endY < startY) | |
1983 | { | |
1984 | int tmp = startY; | |
1985 | startY = endY; | |
1986 | endY = tmp; | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) ); | |
1991 | ||
1992 | for (int i = startY; i <= endY; i++) | |
1993 | { | |
1994 | m_lines[i].m_text.insert( 0u, wxT(" ") ); | |
1995 | RefreshLine( i ); | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | void wxTextCtrl::Unindent() | |
2000 | { | |
2001 | int startY = m_cursorY; | |
2002 | int endY = m_cursorY; | |
2003 | if (HasSelection()) | |
2004 | { | |
2005 | startY = m_selStartY; | |
2006 | endY = m_selEndY; | |
2007 | if (endY < startY) | |
2008 | { | |
2009 | int tmp = startY; | |
2010 | startY = endY; | |
2011 | endY = tmp; | |
2012 | } | |
2013 | } | |
2014 | ||
2015 | m_undos.Append( new wxSourceUndoStep( wxSOURCE_UNDO_LINE, startY, endY, this ) ); | |
2016 | ||
2017 | for (int i = startY; i <= endY; i++) | |
2018 | { | |
2019 | for (int n = 0; n < 4; n++) | |
2020 | { | |
2021 | if (m_lines[i].m_text[0u] == wxT(' ')) | |
2022 | m_lines[i].m_text.erase(0u,1u); | |
2023 | } | |
2024 | RefreshLine( i ); | |
2025 | } | |
2026 | } | |
2027 | bool wxTextCtrl::HasSelection() | |
2028 | { | |
2029 | return ((m_selStartY != m_selEndY) || (m_selStartX != m_selEndX)); | |
2030 | } | |
2031 | ||
2032 | void wxTextCtrl::ClearSelection() | |
2033 | { | |
2034 | m_selStartX = -1; | |
2035 | m_selStartY = -1; | |
2036 | m_selEndX = -1; | |
2037 | m_selEndY = -1; | |
2038 | } | |
2039 | ||
2040 | void wxTextCtrl::RefreshLine( int n ) | |
2041 | { | |
2042 | int y = n*m_lineHeight; | |
2043 | int x = 0; | |
2044 | CalcScrolledPosition( x, y, &x, &y ); | |
2045 | wxRect rect( 0+2, y+2, 10000, m_lineHeight ); | |
2046 | Refresh( TRUE, &rect ); | |
2047 | } | |
2048 | ||
2049 | void wxTextCtrl::RefreshDown( int n ) | |
2050 | { | |
2051 | int size_x = 0; | |
2052 | int size_y = 0; | |
2053 | GetClientSize( &size_x, &size_y ); | |
2054 | ||
2055 | int view_x = 0; | |
2056 | int view_y = 0; | |
2057 | GetViewStart( &view_x, &view_y ); | |
2058 | ||
2059 | if (n < view_y) | |
2060 | { | |
2061 | Refresh(); | |
2062 | } | |
2063 | else | |
2064 | { | |
2065 | int y = n*m_lineHeight; | |
2066 | int x = 0; | |
2067 | CalcScrolledPosition( x, y, &x, &y ); | |
2068 | ||
2069 | wxRect rect( 0+2, y+2, 10000, size_y ); | |
2070 | Refresh( TRUE, &rect ); | |
2071 | } | |
2072 | } | |
2073 | ||
2074 | void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre ) | |
2075 | { | |
2076 | if (!m_editable) return; | |
2077 | ||
2078 | // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE)) | |
2079 | { | |
2080 | if (new_x > (int) (m_lines[new_y].m_text.Len())) | |
2081 | new_x = m_lines[new_y].m_text.Len(); | |
2082 | } | |
2083 | ||
2084 | if ((new_x == m_cursorX) && (new_y == m_cursorY)) return; | |
2085 | ||
2086 | bool no_cursor_refresh = FALSE; | |
2087 | bool has_selection = HasSelection(); | |
2088 | ||
2089 | if (shift) | |
2090 | { | |
2091 | int x,y,w,h; | |
2092 | bool erase_background = TRUE; | |
2093 | ||
2094 | if (!has_selection) | |
2095 | { | |
2096 | m_selStartX = m_cursorX; | |
2097 | m_selStartY = m_cursorY; | |
2098 | ||
2099 | x = 0; | |
2100 | w = 10000; | |
2101 | if (new_y > m_selStartY) | |
2102 | { | |
2103 | y = m_selStartY*m_lineHeight; | |
2104 | h = (new_y-m_selStartY+1)*m_lineHeight; | |
2105 | } | |
2106 | else if (new_y == m_selStartY) | |
2107 | { | |
2108 | x = PosToPixel( new_y, m_selStartX ); | |
2109 | w = PosToPixel( new_y, new_x ) - x; | |
2110 | if (w < 0) | |
2111 | { | |
2112 | x += w; | |
2113 | w = -w + 2; // +2 for the cursor | |
2114 | } | |
2115 | y = m_selStartY*m_lineHeight; | |
2116 | h = m_lineHeight; | |
2117 | } | |
2118 | else | |
2119 | { | |
2120 | y = new_y*m_lineHeight; | |
2121 | h = (-new_y+m_selStartY+1)*m_lineHeight; | |
2122 | } | |
2123 | ||
2124 | no_cursor_refresh = TRUE; | |
2125 | m_cursorX = new_x; | |
2126 | m_cursorY = new_y; | |
2127 | } | |
2128 | else | |
2129 | { | |
2130 | if (new_y == m_selEndY) | |
2131 | { | |
2132 | y = new_y *m_lineHeight; | |
2133 | h = m_lineHeight; | |
2134 | if (m_selEndX > new_x) | |
2135 | { | |
2136 | // x = new_x*m_charWidth; | |
2137 | x = PosToPixel( new_y, new_x ); | |
2138 | // w = (m_selEndX-new_x)*m_charWidth; | |
2139 | w = PosToPixel( new_y, m_selEndX ) - x; | |
2140 | } | |
2141 | else | |
2142 | { | |
2143 | // x = m_selEndX*m_charWidth; | |
2144 | x = PosToPixel( new_y, m_selEndX ); | |
2145 | // w = (-m_selEndX+new_x)*m_charWidth; | |
2146 | w = PosToPixel( new_y, new_x ) - x; | |
2147 | } | |
2148 | } | |
2149 | else | |
2150 | { | |
2151 | x = 0; | |
2152 | w = 10000; | |
2153 | if (new_y > m_selEndY) | |
2154 | { | |
2155 | y = m_selEndY*m_lineHeight; | |
2156 | h = (new_y-m_selEndY+1) * m_lineHeight; | |
2157 | ||
2158 | erase_background = ((m_selEndY < m_selStartY) || | |
2159 | ((m_selEndY == m_selStartY) && (m_selEndX < m_selStartX))); | |
2160 | } | |
2161 | else | |
2162 | { | |
2163 | y = new_y*m_lineHeight; | |
2164 | h = (-new_y+m_selEndY+1) * m_lineHeight; | |
2165 | ||
2166 | erase_background = ((m_selEndY > m_selStartY) || | |
2167 | ((m_selEndY == m_selStartY) && (m_selEndX > m_selStartX))); | |
2168 | } | |
2169 | no_cursor_refresh = TRUE; | |
2170 | m_cursorX = new_x; | |
2171 | m_cursorY = new_y; | |
2172 | } | |
2173 | } | |
2174 | ||
2175 | m_selEndX = new_x; | |
2176 | m_selEndY = new_y; | |
2177 | ||
2178 | CalcScrolledPosition( x, y, &x, &y ); | |
2179 | wxRect rect( x+2, y+2, w, h ); | |
2180 | Refresh( erase_background, &rect ); | |
2181 | } | |
2182 | else | |
2183 | { | |
2184 | if (has_selection) | |
2185 | { | |
2186 | int ry1 = m_selEndY; | |
2187 | int ry2 = m_selStartY; | |
2188 | m_selEndX = -1; | |
2189 | m_selEndY = -1; | |
2190 | m_selStartX = -1; | |
2191 | m_selStartY = -1; | |
2192 | ||
2193 | if (ry1 > ry2) | |
2194 | { | |
2195 | int tmp = ry2; | |
2196 | ry2 = ry1; | |
2197 | ry1 = tmp; | |
2198 | } | |
2199 | ||
2200 | int x = 0; | |
2201 | int y = ry1*m_lineHeight; | |
2202 | CalcScrolledPosition( x, y, &x, &y ); | |
2203 | wxRect rect( 0, y+2, 10000, (ry2-ry1+1)*m_lineHeight ); | |
2204 | ||
2205 | Refresh( TRUE, &rect ); | |
2206 | } | |
2207 | } | |
2208 | ||
2209 | /* | |
2210 | printf( "startx %d starty %d endx %d endy %d\n", | |
2211 | m_selStartX, m_selStartY, m_selEndX, m_selEndY ); | |
2212 | ||
2213 | printf( "has %d\n", (int)HasSelection() ); | |
2214 | */ | |
2215 | ||
2216 | if (!no_cursor_refresh) | |
2217 | { | |
2218 | // int x = m_cursorX*m_charWidth; | |
2219 | int x = PosToPixel( m_cursorY, m_cursorX ); | |
2220 | int y = m_cursorY*m_lineHeight; | |
2221 | CalcScrolledPosition( x, y, &x, &y ); | |
2222 | wxRect rect( x+2, y+2, 4, m_lineHeight+2 ); | |
2223 | ||
2224 | m_cursorX = new_x; | |
2225 | m_cursorY = new_y; | |
2226 | ||
2227 | Refresh( TRUE, &rect ); | |
2228 | ||
2229 | if (FindFocus() == this) | |
2230 | { | |
2231 | wxClientDC dc(this); | |
2232 | PrepareDC( dc ); | |
2233 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
2234 | //dc.SetBrush( *wxRED_BRUSH ); | |
2235 | dc.SetBrush( *wxBLACK_BRUSH ); | |
2236 | // int xx = m_cursorX*m_charWidth; | |
2237 | int xx = PosToPixel( m_cursorY, m_cursorX ); | |
2238 | dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight ); | |
2239 | } | |
2240 | } | |
2241 | ||
2242 | int size_x = 0; | |
2243 | int size_y = 0; | |
2244 | GetClientSize( &size_x, &size_y ); | |
2245 | size_x /= m_charWidth; | |
2246 | size_y /= m_lineHeight; | |
2247 | ||
2248 | int view_x = 0; | |
2249 | int view_y = 0; | |
2250 | GetViewStart( &view_x, &view_y ); | |
2251 | ||
2252 | if (centre) | |
2253 | { | |
2254 | int sy = m_cursorY - (size_y/2); | |
2255 | if (sy < 0) sy = 0; | |
2256 | Scroll( -1, sy ); | |
2257 | } | |
2258 | else | |
2259 | { | |
2260 | if (m_cursorY < view_y) | |
2261 | Scroll( -1, m_cursorY ); | |
2262 | else if (m_cursorY > view_y+size_y-1) | |
2263 | Scroll( -1, m_cursorY-size_y+1 ); | |
2264 | } | |
2265 | ||
2266 | //int xx = m_cursorX; | |
2267 | int xx = PosToPixel( m_cursorY, m_cursorX ) / m_charWidth; | |
2268 | ||
2269 | if (xx < view_x) | |
2270 | Scroll( xx, -1 ); | |
2271 | else if (xx > view_x+size_x-1) | |
2272 | Scroll( xx-size_x+1, -1 ); | |
2273 | } | |
2274 | ||
2275 | void wxTextCtrl::MyAdjustScrollbars() | |
2276 | { | |
2277 | if (IsSingleLine()) | |
2278 | return; | |
2279 | ||
2280 | int y_range = m_lines.GetCount(); | |
2281 | ||
2282 | int height = 0; | |
2283 | GetClientSize( NULL, &height ); | |
2284 | height -= 4; | |
2285 | if (height >= (int)m_lines.GetCount() *m_lineHeight) | |
2286 | y_range = 0; | |
2287 | ||
2288 | int view_x = 0; | |
2289 | int view_y = 0; | |
2290 | GetViewStart( &view_x, &view_y ); | |
2291 | ||
2292 | SetScrollbars( m_charWidth, m_lineHeight, m_longestLine+2, y_range, view_x, view_y ); | |
2293 | } | |
2294 | ||
2295 | //----------------------------------------------------------------------------- | |
2296 | // clipboard handlers | |
2297 | //----------------------------------------------------------------------------- | |
2298 | ||
2299 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
2300 | { | |
2301 | Cut(); | |
2302 | } | |
2303 | ||
2304 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2305 | { | |
2306 | Copy(); | |
2307 | } | |
2308 | ||
2309 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2310 | { | |
2311 | Paste(); | |
2312 | } | |
2313 | ||
2314 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2315 | { | |
2316 | Undo(); | |
2317 | } | |
2318 | ||
2319 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2320 | { | |
2321 | Redo(); | |
2322 | } | |
2323 | ||
2324 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2325 | { | |
2326 | event.Enable( CanCut() ); | |
2327 | } | |
2328 | ||
2329 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2330 | { | |
2331 | event.Enable( CanCopy() ); | |
2332 | } | |
2333 | ||
2334 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2335 | { | |
2336 | event.Enable( CanPaste() ); | |
2337 | } | |
2338 | ||
2339 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2340 | { | |
2341 | event.Enable( CanUndo() ); | |
2342 | } | |
2343 | ||
2344 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2345 | { | |
2346 | event.Enable( CanRedo() ); | |
2347 | } | |
2348 | ||
2349 | wxSize wxTextCtrl::DoGetBestSize() const | |
2350 | { | |
2351 | if (IsSingleLine()) | |
2352 | { | |
2353 | wxSize ret(80, m_lineHeight + 4); | |
2354 | ||
2355 | if (HasFlag(wxBORDER_SUNKEN) || HasFlag(wxBORDER_RAISED)) | |
2356 | ret.y += 4; | |
2357 | ||
2358 | if (HasFlag(wxBORDER_SIMPLE)) | |
2359 | ret.y += 2; | |
2360 | ||
2361 | return ret; | |
2362 | } | |
2363 | else | |
2364 | { | |
2365 | return wxSize(80, 60); | |
2366 | } | |
2367 | } | |
2368 | ||
2369 | // ---------------------------------------------------------------------------- | |
2370 | // freeze/thaw | |
2371 | // ---------------------------------------------------------------------------- | |
2372 | ||
2373 | void wxTextCtrl::Freeze() | |
2374 | { | |
2375 | } | |
2376 | ||
2377 | void wxTextCtrl::Thaw() | |
2378 | { | |
2379 | } | |
2380 | ||
2381 | void wxTextCtrl::OnSetFocus( wxFocusEvent& event ) | |
2382 | { | |
2383 | // To hide or show caret, as appropriate | |
2384 | Refresh(); | |
2385 | } | |
2386 | ||
2387 | void wxTextCtrl::OnKillFocus( wxFocusEvent& event ) | |
2388 | { | |
2389 | // To hide or show caret, as appropriate | |
2390 | Refresh(); | |
2391 | } | |
2392 | ||
2393 | // ---------------------------------------------------------------------------- | |
2394 | // text control scrolling | |
2395 | // ---------------------------------------------------------------------------- | |
2396 | ||
2397 | bool wxTextCtrl::ScrollLines(int lines) | |
2398 | { | |
2399 | wxFAIL_MSG( "wxTextCtrl::ScrollLines not implemented"); | |
2400 | ||
2401 | return FALSE; | |
2402 | } | |
2403 | ||
2404 | bool wxTextCtrl::ScrollPages(int pages) | |
2405 | { | |
2406 | wxFAIL_MSG( "wxTextCtrl::ScrollPages not implemented"); | |
2407 | ||
2408 | return FALSE; | |
2409 | } | |
2410 |