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