]>
Commit | Line | Data |
---|---|---|
5d7836c4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
faa94f3e | 2 | // Name: src/richtext/richeditctrl.cpp |
5d7836c4 JS |
3 | // Purpose: A rich edit control |
4 | // Author: Julian Smart | |
7fe8059f | 5 | // Modified by: |
5d7836c4 | 6 | // Created: 2005-09-30 |
7fe8059f | 7 | // RCS-ID: $Id$ |
5d7836c4 JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
9eddec69 | 16 | #pragma hdrstop |
5d7836c4 JS |
17 | #endif |
18 | ||
b01ca8b6 JS |
19 | #if wxUSE_RICHTEXT |
20 | ||
21 | #include "wx/richtext/richtextctrl.h" | |
22 | ||
5d7836c4 | 23 | #ifndef WX_PRECOMP |
9eddec69 WS |
24 | #include "wx/wx.h" |
25 | #include "wx/settings.h" | |
5d7836c4 JS |
26 | #endif |
27 | ||
5d7836c4 JS |
28 | #include "wx/textfile.h" |
29 | #include "wx/ffile.h" | |
5d7836c4 JS |
30 | #include "wx/filename.h" |
31 | #include "wx/dcbuffer.h" | |
5d7836c4 JS |
32 | #include "wx/arrimpl.cpp" |
33 | ||
34 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED) | |
35 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED) | |
36 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK) | |
37 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK) | |
38 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK) | |
39 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK) | |
40 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN) | |
41 | ||
42 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
43 | IMPLEMENT_CLASS( wxRichTextCtrl, wxControl ) | |
44 | #else | |
45 | IMPLEMENT_CLASS( wxRichTextCtrl, wxScrolledWindow ) | |
46 | #endif | |
47 | ||
48 | IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent ) | |
49 | ||
50 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
51 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl ) | |
52 | #else | |
53 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxScrolledWindow ) | |
54 | #endif | |
55 | EVT_PAINT(wxRichTextCtrl::OnPaint) | |
56 | EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) | |
4d551ad5 | 57 | EVT_IDLE(wxRichTextCtrl::OnIdle) |
c59f6793 | 58 | EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) |
5d7836c4 JS |
59 | EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) |
60 | EVT_MOTION(wxRichTextCtrl::OnMoveMouse) | |
61 | EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) | |
62 | EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick) | |
63 | EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick) | |
64 | EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick) | |
65 | EVT_CHAR(wxRichTextCtrl::OnChar) | |
66 | EVT_SIZE(wxRichTextCtrl::OnSize) | |
67 | EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus) | |
68 | EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus) | |
69 | EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu) | |
70 | ||
71 | EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo) | |
72 | EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo) | |
73 | ||
74 | EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo) | |
75 | EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo) | |
76 | ||
77 | EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy) | |
78 | EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy) | |
79 | ||
80 | EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste) | |
81 | EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste) | |
82 | ||
83 | EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut) | |
84 | EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut) | |
85 | ||
86 | EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear) | |
87 | EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear) | |
88 | ||
89 | EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll) | |
90 | EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll) | |
91 | END_EVENT_TABLE() | |
92 | ||
93 | /*! | |
94 | * wxRichTextCtrl | |
95 | */ | |
96 | ||
97 | wxRichTextCtrl::wxRichTextCtrl() | |
98 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
99 | : wxScrollHelper(this) | |
100 | #endif | |
101 | { | |
102 | Init(); | |
103 | } | |
104 | ||
105 | wxRichTextCtrl::wxRichTextCtrl( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) | |
106 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
107 | : wxScrollHelper(this) | |
108 | #endif | |
109 | { | |
110 | Init(); | |
111 | Create(parent, id, pos, size, style); | |
112 | } | |
113 | ||
114 | /// Creation | |
115 | bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) | |
116 | { | |
117 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
118 | if (!wxTextCtrlBase::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE | |
119 | )) | |
120 | return false; | |
121 | #else | |
122 | if (!wxScrolledWindow::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE | |
123 | )) | |
124 | return false; | |
125 | #endif | |
126 | ||
127 | if (!GetFont().Ok()) | |
128 | { | |
129 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
130 | } | |
7fe8059f | 131 | |
5d7836c4 JS |
132 | GetBuffer().SetRichTextCtrl(this); |
133 | ||
134 | wxTextAttrEx attributes; | |
135 | attributes.SetFont(GetFont()); | |
136 | attributes.SetTextColour(*wxBLACK); | |
137 | attributes.SetBackgroundColour(*wxWHITE); | |
138 | attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT); | |
139 | attributes.SetFlags(wxTEXT_ATTR_ALL); | |
140 | ||
141 | SetDefaultStyle(attributes); | |
142 | SetBasicStyle(attributes); | |
143 | ||
7fe8059f | 144 | SetBackgroundColour(*wxWHITE); |
5d7836c4 JS |
145 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
146 | ||
7fe8059f | 147 | // Tell the sizers to use the given or best size |
5d7836c4 | 148 | SetBestFittingSize(size); |
7fe8059f | 149 | |
5d7836c4 JS |
150 | // Create a buffer |
151 | RecreateBuffer(size); | |
152 | ||
5d7836c4 | 153 | SetCursor(wxCursor(wxCURSOR_IBEAM)); |
7fe8059f | 154 | |
5d7836c4 JS |
155 | return true; |
156 | } | |
157 | ||
158 | wxRichTextCtrl::~wxRichTextCtrl() | |
159 | { | |
160 | delete m_contextMenu; | |
161 | } | |
162 | ||
163 | /// Member initialisation | |
164 | void wxRichTextCtrl::Init() | |
165 | { | |
166 | m_freezeCount = 0; | |
167 | m_contextMenu = NULL; | |
168 | m_caret = NULL; | |
169 | m_caretPosition = -1; | |
170 | m_selectionRange.SetRange(-2, -2); | |
171 | m_selectionAnchor = -2; | |
172 | m_editable = true; | |
173 | m_caretAtLineStart = false; | |
174 | m_dragging = false; | |
4d551ad5 JS |
175 | m_fullLayoutRequired = false; |
176 | m_fullLayoutTime = 0; | |
177 | m_fullLayoutSavedPosition = 0; | |
178 | m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD; | |
5d7836c4 JS |
179 | } |
180 | ||
181 | /// Call Freeze to prevent refresh | |
182 | void wxRichTextCtrl::Freeze() | |
183 | { | |
184 | m_freezeCount ++; | |
185 | } | |
186 | ||
187 | /// Call Thaw to refresh | |
0bab774b | 188 | void wxRichTextCtrl::Thaw() |
5d7836c4 JS |
189 | { |
190 | m_freezeCount --; | |
191 | ||
0bab774b | 192 | if (m_freezeCount == 0) |
5d7836c4 JS |
193 | { |
194 | SetupScrollbars(); | |
76bcd815 | 195 | Refresh(false); |
5d7836c4 JS |
196 | } |
197 | } | |
198 | ||
199 | /// Clear all text | |
200 | void wxRichTextCtrl::Clear() | |
201 | { | |
202 | m_buffer.Reset(); | |
203 | m_buffer.SetDirty(true); | |
204 | m_caretPosition = -1; | |
205 | m_caretAtLineStart = false; | |
206 | m_selectionRange.SetRange(-2, -2); | |
207 | ||
208 | if (m_freezeCount == 0) | |
209 | { | |
210 | SetupScrollbars(); | |
76bcd815 | 211 | Refresh(false); |
5d7836c4 JS |
212 | } |
213 | SendUpdateEvent(); | |
214 | } | |
215 | ||
216 | /// Painting | |
217 | void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
218 | { | |
d2142335 JS |
219 | if (GetCaret()) |
220 | GetCaret()->Hide(); | |
221 | ||
5d7836c4 | 222 | { |
c59f6793 JS |
223 | wxBufferedPaintDC dc(this, m_bufferBitmap); |
224 | //wxLogDebug(wxT("OnPaint")); | |
41e155b4 | 225 | |
c59f6793 | 226 | PrepareDC(dc); |
41e155b4 | 227 | |
c59f6793 JS |
228 | if (m_freezeCount > 0) |
229 | return; | |
41e155b4 | 230 | |
c59f6793 | 231 | dc.SetFont(GetFont()); |
41e155b4 | 232 | |
c59f6793 JS |
233 | // Paint the background |
234 | PaintBackground(dc); | |
41e155b4 | 235 | |
c59f6793 | 236 | wxRegion dirtyRegion = GetUpdateRegion(); |
41e155b4 | 237 | |
c59f6793 JS |
238 | wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); |
239 | wxRect availableSpace(GetClientSize()); | |
240 | if (GetBuffer().GetDirty()) | |
241 | { | |
242 | GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); | |
243 | GetBuffer().SetDirty(false); | |
244 | SetupScrollbars(); | |
245 | } | |
41e155b4 | 246 | |
c59f6793 | 247 | GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); |
5d7836c4 | 248 | } |
d2142335 JS |
249 | |
250 | if (GetCaret()) | |
251 | GetCaret()->Show(); | |
252 | ||
7383cf9d | 253 | PositionCaret(); |
5d7836c4 JS |
254 | } |
255 | ||
256 | // Empty implementation, to prevent flicker | |
257 | void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
258 | { | |
259 | } | |
260 | ||
261 | void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) | |
262 | { | |
c59f6793 JS |
263 | wxCaret* caret = new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16); |
264 | SetCaret(caret); | |
265 | caret->Show(); | |
266 | PositionCaret(); | |
267 | ||
5d7836c4 | 268 | if (!IsFrozen()) |
76bcd815 | 269 | Refresh(false); |
5d7836c4 JS |
270 | } |
271 | ||
272 | void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) | |
273 | { | |
c59f6793 JS |
274 | SetCaret(NULL); |
275 | ||
5d7836c4 | 276 | if (!IsFrozen()) |
76bcd815 | 277 | Refresh(false); |
5d7836c4 JS |
278 | } |
279 | ||
280 | /// Left-click | |
281 | void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event) | |
282 | { | |
283 | SetFocus(); | |
284 | ||
285 | wxClientDC dc(this); | |
286 | PrepareDC(dc); | |
287 | dc.SetFont(GetFont()); | |
7fe8059f | 288 | |
5d7836c4 JS |
289 | long position = 0; |
290 | int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position); | |
291 | ||
292 | if (hit != wxRICHTEXT_HITTEST_NONE) | |
293 | { | |
294 | m_dragStart = event.GetLogicalPosition(dc); | |
295 | m_dragging = true; | |
296 | CaptureMouse(); | |
297 | ||
298 | SelectNone(); | |
299 | ||
300 | bool caretAtLineStart = false; | |
301 | ||
302 | if (hit & wxRICHTEXT_HITTEST_BEFORE) | |
303 | { | |
304 | // If we're at the start of a line (but not first in para) | |
305 | // then we should keep the caret showing at the start of the line | |
306 | // by showing the m_caretAtLineStart flag. | |
307 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); | |
308 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); | |
309 | ||
1e967276 | 310 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) |
5d7836c4 JS |
311 | caretAtLineStart = true; |
312 | position --; | |
313 | } | |
314 | ||
315 | MoveCaret(position, caretAtLineStart); | |
316 | SetDefaultStyleToCursorStyle(); | |
317 | ||
318 | #if 0 | |
319 | wxWindow* p = GetParent(); | |
320 | while (p && !p->IsKindOf(CLASSINFO(wxFrame))) | |
321 | p = p->GetParent(); | |
322 | ||
323 | wxFrame* frame = wxDynamicCast(p, wxFrame); | |
324 | if (frame) | |
325 | { | |
326 | wxString msg = wxString::Format(wxT("Found position %ld"), position); | |
327 | frame->SetStatusText(msg, 1); | |
328 | } | |
329 | #endif | |
330 | } | |
331 | ||
332 | event.Skip(); | |
333 | } | |
334 | ||
335 | /// Left-up | |
336 | void wxRichTextCtrl::OnLeftUp(wxMouseEvent& WXUNUSED(event)) | |
337 | { | |
338 | if (m_dragging) | |
339 | { | |
340 | m_dragging = false; | |
341 | if (GetCapture() == this) | |
342 | ReleaseMouse(); | |
343 | } | |
344 | } | |
345 | ||
346 | /// Left-click | |
347 | void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) | |
348 | { | |
349 | if (!event.Dragging()) | |
350 | { | |
351 | event.Skip(); | |
352 | return; | |
353 | } | |
354 | ||
355 | wxClientDC dc(this); | |
356 | PrepareDC(dc); | |
357 | dc.SetFont(GetFont()); | |
7fe8059f | 358 | |
5d7836c4 JS |
359 | long position = 0; |
360 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
361 | int hit = GetBuffer().HitTest(dc, logicalPt, position); | |
362 | ||
363 | if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE) | |
364 | { | |
365 | // TODO: test closeness | |
366 | ||
367 | bool caretAtLineStart = false; | |
368 | ||
369 | if (hit & wxRICHTEXT_HITTEST_BEFORE) | |
370 | { | |
371 | // If we're at the start of a line (but not first in para) | |
372 | // then we should keep the caret showing at the start of the line | |
373 | // by showing the m_caretAtLineStart flag. | |
374 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); | |
375 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); | |
376 | ||
1e967276 | 377 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) |
5d7836c4 JS |
378 | caretAtLineStart = true; |
379 | position --; | |
380 | } | |
381 | ||
382 | if (m_caretPosition != position) | |
383 | { | |
384 | bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN); | |
7fe8059f | 385 | |
5d7836c4 JS |
386 | MoveCaret(position, caretAtLineStart); |
387 | SetDefaultStyleToCursorStyle(); | |
7fe8059f | 388 | |
5d7836c4 | 389 | if (extendSel) |
76bcd815 | 390 | Refresh(false); |
5d7836c4 JS |
391 | } |
392 | } | |
393 | } | |
394 | ||
395 | /// Right-click | |
396 | void wxRichTextCtrl::OnRightClick(wxMouseEvent& event) | |
397 | { | |
398 | SetFocus(); | |
399 | event.Skip(); | |
400 | } | |
401 | ||
402 | /// Left-double-click | |
403 | void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& event) | |
404 | { | |
405 | event.Skip(); | |
406 | } | |
407 | ||
408 | /// Middle-click | |
409 | void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event) | |
410 | { | |
411 | event.Skip(); | |
412 | } | |
413 | ||
414 | /// Key press | |
415 | void wxRichTextCtrl::OnChar(wxKeyEvent& event) | |
416 | { | |
417 | int flags = 0; | |
418 | if (event.ControlDown()) | |
419 | flags |= wxRICHTEXT_CTRL_DOWN; | |
420 | if (event.ShiftDown()) | |
421 | flags |= wxRICHTEXT_SHIFT_DOWN; | |
422 | if (event.AltDown()) | |
423 | flags |= wxRICHTEXT_ALT_DOWN; | |
424 | ||
425 | if (event.GetKeyCode() == WXK_LEFT || | |
426 | event.GetKeyCode() == WXK_RIGHT || | |
427 | event.GetKeyCode() == WXK_UP || | |
428 | event.GetKeyCode() == WXK_DOWN || | |
429 | event.GetKeyCode() == WXK_HOME || | |
430 | event.GetKeyCode() == WXK_PAGEUP || | |
431 | event.GetKeyCode() == WXK_PAGEDOWN || | |
5d7836c4 JS |
432 | event.GetKeyCode() == WXK_END) |
433 | { | |
0bab774b | 434 | KeyboardNavigate(event.GetKeyCode(), flags); |
99257cbe | 435 | return; |
5d7836c4 | 436 | } |
99257cbe VZ |
437 | |
438 | // all the other keys modify the controls contents which shouldn't be | |
439 | // possible if we're read-only | |
440 | if ( !IsEditable() ) | |
441 | { | |
442 | event.Skip(); | |
443 | return; | |
444 | } | |
445 | ||
446 | if (event.GetKeyCode() == WXK_RETURN) | |
5d7836c4 JS |
447 | { |
448 | BeginBatchUndo(_("Insert Text")); | |
449 | ||
450 | long newPos = m_caretPosition; | |
451 | ||
452 | DeleteSelectedContent(& newPos); | |
453 | ||
454 | GetBuffer().InsertNewlineWithUndo(newPos+1, this); | |
455 | ||
456 | wxRichTextEvent cmdEvent( | |
457 | wxEVT_COMMAND_RICHTEXT_RETURN, | |
458 | GetId()); | |
459 | cmdEvent.SetEventObject(this); | |
460 | cmdEvent.SetFlags(flags); | |
461 | GetEventHandler()->ProcessEvent(cmdEvent); | |
462 | ||
463 | EndBatchUndo(); | |
464 | SetDefaultStyleToCursorStyle(); | |
f9ed2d16 JS |
465 | |
466 | ScrollIntoView(m_caretPosition, WXK_RIGHT); | |
5d7836c4 JS |
467 | } |
468 | else if (event.GetKeyCode() == WXK_BACK) | |
469 | { | |
470 | BeginBatchUndo(_("Delete Text")); | |
471 | ||
472 | // Submit range in character positions, which are greater than caret positions, | |
473 | // so subtract 1 for deleted character and add 1 for conversion to character position. | |
474 | if (m_caretPosition > -1 && !HasSelection()) | |
475 | { | |
476 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), | |
477 | m_caretPosition, // Current caret position | |
478 | m_caretPosition-1, // New caret position | |
479 | this); | |
480 | } | |
481 | else | |
482 | DeleteSelectedContent(); | |
483 | ||
484 | EndBatchUndo(); | |
485 | ||
486 | // Shouldn't this be in Do()? | |
487 | if (GetLastPosition() == -1) | |
488 | { | |
489 | GetBuffer().Reset(); | |
490 | ||
491 | m_caretPosition = -1; | |
492 | PositionCaret(); | |
493 | SetDefaultStyleToCursorStyle(); | |
494 | } | |
495 | ||
f9ed2d16 | 496 | ScrollIntoView(m_caretPosition, WXK_LEFT); |
5d7836c4 JS |
497 | } |
498 | else if (event.GetKeyCode() == WXK_DELETE) | |
499 | { | |
500 | BeginBatchUndo(_("Delete Text")); | |
501 | ||
502 | // Submit range in character positions, which are greater than caret positions, | |
503 | if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection()) | |
504 | { | |
505 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1), | |
506 | m_caretPosition, // Current caret position | |
507 | m_caretPosition+1, // New caret position | |
508 | this); | |
509 | } | |
510 | else | |
511 | DeleteSelectedContent(); | |
512 | ||
513 | EndBatchUndo(); | |
514 | ||
515 | // Shouldn't this be in Do()? | |
516 | if (GetLastPosition() == -1) | |
517 | { | |
518 | GetBuffer().Reset(); | |
519 | ||
520 | m_caretPosition = -1; | |
521 | PositionCaret(); | |
522 | SetDefaultStyleToCursorStyle(); | |
523 | } | |
524 | } | |
525 | else | |
526 | { | |
527 | BeginBatchUndo(_("Insert Text")); | |
528 | ||
529 | long newPos = m_caretPosition; | |
530 | DeleteSelectedContent(& newPos); | |
531 | ||
532 | wxString str = (wxChar) event.GetKeyCode(); | |
533 | GetBuffer().InsertTextWithUndo(newPos+1, str, this); | |
534 | ||
535 | EndBatchUndo(); | |
536 | ||
537 | SetDefaultStyleToCursorStyle(); | |
f9ed2d16 | 538 | ScrollIntoView(m_caretPosition, WXK_RIGHT); |
5d7836c4 | 539 | } |
5d7836c4 JS |
540 | } |
541 | ||
542 | /// Delete content if there is a selection, e.g. when pressing a key. | |
543 | bool wxRichTextCtrl::DeleteSelectedContent(long* newPos) | |
544 | { | |
545 | if (HasSelection()) | |
546 | { | |
547 | long pos = m_selectionRange.GetStart(); | |
548 | GetBuffer().DeleteRangeWithUndo(m_selectionRange, | |
549 | m_caretPosition, // Current caret position | |
550 | pos, // New caret position | |
551 | this); | |
552 | m_selectionRange.SetRange(-2, -2); | |
553 | ||
554 | if (newPos) | |
555 | *newPos = pos-1; | |
556 | return true; | |
557 | } | |
558 | else | |
559 | return false; | |
560 | } | |
561 | ||
562 | /// Keyboard navigation | |
563 | ||
564 | /* | |
565 | ||
566 | Left: left one character | |
567 | Right: right one character | |
568 | Up: up one line | |
569 | Down: down one line | |
570 | Ctrl-Left: left one word | |
571 | Ctrl-Right: right one word | |
572 | Ctrl-Up: previous paragraph start | |
573 | Ctrl-Down: next start of paragraph | |
574 | Home: start of line | |
575 | End: end of line | |
576 | Ctrl-Home: start of document | |
577 | Ctrl-End: end of document | |
578 | Page-Up: Up a screen | |
579 | Page-Down: Down a screen | |
580 | ||
581 | Maybe: | |
582 | ||
583 | Ctrl-Alt-PgUp: Start of window | |
584 | Ctrl-Alt-PgDn: End of window | |
585 | F8: Start selection mode | |
586 | Esc: End selection mode | |
587 | ||
588 | Adding Shift does the above but starts/extends selection. | |
589 | ||
590 | ||
591 | */ | |
592 | ||
0bab774b | 593 | bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) |
5d7836c4 JS |
594 | { |
595 | bool success = false; | |
5d7836c4 JS |
596 | |
597 | if (keyCode == WXK_RIGHT) | |
598 | { | |
599 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
600 | success = WordRight(1, flags); | |
601 | else | |
602 | success = MoveRight(1, flags); | |
603 | } | |
604 | else if (keyCode == WXK_LEFT) | |
605 | { | |
606 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
607 | success = WordLeft(1, flags); | |
608 | else | |
609 | success = MoveLeft(1, flags); | |
610 | } | |
611 | else if (keyCode == WXK_UP) | |
612 | { | |
613 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
614 | success = MoveToParagraphStart(flags); | |
615 | else | |
616 | success = MoveUp(1, flags); | |
617 | } | |
618 | else if (keyCode == WXK_DOWN) | |
619 | { | |
620 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
621 | success = MoveToParagraphEnd(flags); | |
622 | else | |
623 | success = MoveDown(1, flags); | |
624 | } | |
faa94f3e | 625 | else if (keyCode == WXK_PAGEUP) |
5d7836c4 JS |
626 | { |
627 | success = PageUp(1, flags); | |
628 | } | |
faa94f3e | 629 | else if (keyCode == WXK_PAGEDOWN) |
5d7836c4 JS |
630 | { |
631 | success = PageDown(1, flags); | |
632 | } | |
633 | else if (keyCode == WXK_HOME) | |
634 | { | |
635 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
636 | success = MoveHome(flags); | |
637 | else | |
638 | success = MoveToLineStart(flags); | |
639 | } | |
640 | else if (keyCode == WXK_END) | |
641 | { | |
642 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
643 | success = MoveEnd(flags); | |
644 | else | |
645 | success = MoveToLineEnd(flags); | |
646 | } | |
647 | ||
648 | if (success) | |
649 | { | |
650 | ScrollIntoView(m_caretPosition, keyCode); | |
651 | SetDefaultStyleToCursorStyle(); | |
652 | } | |
653 | ||
5d7836c4 JS |
654 | return success; |
655 | } | |
656 | ||
657 | /// Extend the selection. Selections are in caret positions. | |
658 | bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags) | |
659 | { | |
660 | if (flags & wxRICHTEXT_SHIFT_DOWN) | |
661 | { | |
662 | // If not currently selecting, start selecting | |
663 | if (m_selectionRange.GetStart() == -2) | |
664 | { | |
665 | m_selectionAnchor = oldPos; | |
666 | ||
667 | if (oldPos > newPos) | |
668 | m_selectionRange.SetRange(newPos+1, oldPos); | |
669 | else | |
670 | m_selectionRange.SetRange(oldPos+1, newPos); | |
671 | } | |
672 | else | |
673 | { | |
674 | // Always ensure that the selection range start is greater than | |
675 | // the end. | |
676 | if (newPos > m_selectionAnchor) | |
677 | m_selectionRange.SetRange(m_selectionAnchor+1, newPos); | |
678 | else | |
679 | m_selectionRange.SetRange(newPos+1, m_selectionAnchor); | |
680 | } | |
681 | ||
682 | if (m_selectionRange.GetStart() > m_selectionRange.GetEnd()) | |
683 | { | |
684 | wxLogDebug(wxT("Strange selection range")); | |
685 | } | |
7fe8059f | 686 | |
5d7836c4 JS |
687 | return true; |
688 | } | |
689 | else | |
690 | return false; | |
691 | } | |
692 | ||
693 | /// Scroll into view, returning true if we scrolled. | |
694 | /// This takes a _caret_ position. | |
695 | bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) | |
696 | { | |
697 | wxRichTextLine* line = GetVisibleLineForCaretPosition(position); | |
698 | ||
699 | if (!line) | |
700 | return false; | |
701 | ||
702 | int ppuX, ppuY; | |
703 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
704 | ||
705 | int startX, startY; | |
706 | GetViewStart(& startX, & startY); | |
707 | startX = 0; | |
708 | startY = startY * ppuY; | |
709 | ||
8d7eaf91 | 710 | int sx = 0, sy = 0; |
5d7836c4 JS |
711 | GetVirtualSize(& sx, & sy); |
712 | sx = 0; | |
713 | if (ppuY != 0) | |
714 | sy = sy/ppuY; | |
715 | ||
716 | wxRect rect = line->GetRect(); | |
717 | ||
718 | bool scrolled = false; | |
719 | ||
720 | wxSize clientSize = GetClientSize(); | |
721 | ||
722 | // Going down | |
faa94f3e | 723 | if (keyCode == WXK_DOWN || keyCode == WXK_RIGHT || keyCode == WXK_END || keyCode == WXK_PAGEDOWN) |
5d7836c4 JS |
724 | { |
725 | if ((rect.y + rect.height) > (clientSize.y + startY)) | |
726 | { | |
727 | // Make it scroll so this item is at the bottom | |
728 | // of the window | |
729 | int y = rect.y - (clientSize.y - rect.height); | |
c59f6793 JS |
730 | y = (int) (0.5 + y/ppuY); |
731 | ||
732 | if (startY != y) | |
733 | { | |
734 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); | |
735 | scrolled = true; | |
736 | } | |
5d7836c4 JS |
737 | } |
738 | else if (rect.y < startY) | |
739 | { | |
740 | // Make it scroll so this item is at the top | |
741 | // of the window | |
742 | int y = rect.y ; | |
c59f6793 JS |
743 | y = (int) (0.5 + y/ppuY); |
744 | ||
745 | if (startY != y) | |
746 | { | |
747 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); | |
748 | scrolled = true; | |
749 | } | |
5d7836c4 | 750 | } |
5d7836c4 JS |
751 | } |
752 | // Going up | |
faa94f3e | 753 | else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PAGEUP ) |
5d7836c4 JS |
754 | { |
755 | if (rect.y < startY) | |
756 | { | |
757 | // Make it scroll so this item is at the top | |
758 | // of the window | |
759 | int y = rect.y ; | |
c59f6793 JS |
760 | y = (int) (0.5 + y/ppuY); |
761 | ||
762 | if (startY != y) | |
763 | { | |
764 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); | |
765 | scrolled = true; | |
766 | } | |
5d7836c4 JS |
767 | } |
768 | else if ((rect.y + rect.height) > (clientSize.y + startY)) | |
769 | { | |
770 | // Make it scroll so this item is at the bottom | |
771 | // of the window | |
772 | int y = rect.y - (clientSize.y - rect.height); | |
c59f6793 JS |
773 | y = (int) (0.5 + y/ppuY); |
774 | ||
775 | if (startY != y) | |
776 | { | |
777 | SetScrollbars(ppuX, ppuY, sx, sy, 0, y); | |
778 | scrolled = true; | |
779 | } | |
5d7836c4 | 780 | } |
5d7836c4 JS |
781 | } |
782 | PositionCaret(); | |
783 | ||
784 | return scrolled; | |
785 | } | |
786 | ||
787 | /// Is the given position visible on the screen? | |
788 | bool wxRichTextCtrl::IsPositionVisible(long pos) const | |
789 | { | |
790 | wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1); | |
791 | ||
792 | if (!line) | |
793 | return false; | |
794 | ||
795 | int ppuX, ppuY; | |
796 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
797 | ||
798 | int startX, startY; | |
799 | GetViewStart(& startX, & startY); | |
800 | startX = 0; | |
801 | startY = startY * ppuY; | |
802 | ||
8d7eaf91 | 803 | int sx = 0, sy = 0; |
5d7836c4 JS |
804 | GetVirtualSize(& sx, & sy); |
805 | sx = 0; | |
806 | if (ppuY != 0) | |
807 | sy = sy/ppuY; | |
808 | ||
809 | wxRect rect = line->GetRect(); | |
810 | ||
811 | wxSize clientSize = GetClientSize(); | |
812 | ||
813 | return !(((rect.y + rect.height) > (clientSize.y + startY)) || rect.y < startY); | |
814 | } | |
815 | ||
816 | void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) | |
817 | { | |
818 | m_caretPosition = position; | |
819 | m_caretAtLineStart = showAtLineStart; | |
820 | } | |
821 | ||
822 | /// Move caret one visual step forward: this may mean setting a flag | |
823 | /// and keeping the same position if we're going from the end of one line | |
824 | /// to the start of the next, which may be the exact same caret position. | |
825 | void wxRichTextCtrl::MoveCaretForward(long oldPosition) | |
826 | { | |
827 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); | |
828 | ||
829 | // Only do the check if we're not at the end of the paragraph (where things work OK | |
830 | // anyway) | |
831 | if (para && (oldPosition != para->GetRange().GetEnd() - 1)) | |
832 | { | |
833 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); | |
834 | ||
835 | if (line) | |
836 | { | |
1e967276 JS |
837 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
838 | ||
5d7836c4 JS |
839 | // We're at the end of a line. See whether we need to |
840 | // stay at the same actual caret position but change visual | |
841 | // position, or not. | |
1e967276 | 842 | if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
843 | { |
844 | if (m_caretAtLineStart) | |
845 | { | |
846 | // We're already at the start of the line, so actually move on now. | |
847 | m_caretPosition = oldPosition + 1; | |
848 | m_caretAtLineStart = false; | |
849 | } | |
850 | else | |
851 | { | |
852 | // We're showing at the end of the line, so keep to | |
853 | // the same position but indicate that we're to show | |
854 | // at the start of the next line. | |
855 | m_caretPosition = oldPosition; | |
7fe8059f | 856 | m_caretAtLineStart = true; |
5d7836c4 JS |
857 | } |
858 | SetDefaultStyleToCursorStyle(); | |
859 | return; | |
860 | } | |
861 | } | |
862 | } | |
863 | m_caretPosition ++; | |
864 | SetDefaultStyleToCursorStyle(); | |
865 | } | |
866 | ||
867 | /// Move caret one visual step backward: this may mean setting a flag | |
868 | /// and keeping the same position if we're going from the end of one line | |
869 | /// to the start of the next, which may be the exact same caret position. | |
870 | void wxRichTextCtrl::MoveCaretBack(long oldPosition) | |
871 | { | |
872 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); | |
873 | ||
874 | // Only do the check if we're not at the start of the paragraph (where things work OK | |
875 | // anyway) | |
876 | if (para && (oldPosition != para->GetRange().GetStart())) | |
877 | { | |
878 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); | |
879 | ||
880 | if (line) | |
881 | { | |
1e967276 JS |
882 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
883 | ||
5d7836c4 JS |
884 | // We're at the start of a line. See whether we need to |
885 | // stay at the same actual caret position but change visual | |
886 | // position, or not. | |
1e967276 | 887 | if (oldPosition == lineRange.GetStart()) |
5d7836c4 JS |
888 | { |
889 | m_caretPosition = oldPosition-1; | |
890 | m_caretAtLineStart = true; | |
891 | return; | |
892 | } | |
1e967276 | 893 | else if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
894 | { |
895 | if (m_caretAtLineStart) | |
896 | { | |
897 | // We're at the start of the line, so keep the same caret position | |
898 | // but clear the start-of-line flag. | |
899 | m_caretPosition = oldPosition; | |
900 | m_caretAtLineStart = false; | |
901 | } | |
902 | else | |
903 | { | |
904 | // We're showing at the end of the line, so go back | |
905 | // to the previous character position. | |
906 | m_caretPosition = oldPosition - 1; | |
907 | } | |
908 | SetDefaultStyleToCursorStyle(); | |
909 | return; | |
910 | } | |
911 | } | |
912 | } | |
913 | m_caretPosition --; | |
914 | SetDefaultStyleToCursorStyle(); | |
915 | } | |
916 | ||
917 | /// Move right | |
918 | bool wxRichTextCtrl::MoveRight(int noPositions, int flags) | |
919 | { | |
920 | long endPos = GetBuffer().GetRange().GetEnd(); | |
921 | ||
922 | if (m_caretPosition + noPositions < endPos) | |
923 | { | |
924 | long oldPos = m_caretPosition; | |
925 | long newPos = m_caretPosition + noPositions; | |
926 | ||
927 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
928 | if (!extendSel) | |
929 | SelectNone(); | |
930 | ||
931 | // Determine by looking at oldPos and m_caretPosition whether | |
932 | // we moved from the end of a line to the start of the next line, in which case | |
933 | // we want to adjust the caret position such that it is positioned at the | |
934 | // start of the next line, rather than jumping past the first character of the | |
935 | // line. | |
936 | if (noPositions == 1 && !extendSel) | |
937 | MoveCaretForward(oldPos); | |
938 | else | |
939 | SetCaretPosition(newPos); | |
940 | ||
941 | PositionCaret(); | |
942 | SetDefaultStyleToCursorStyle(); | |
943 | ||
c59f6793 | 944 | if (extendSel) |
76bcd815 | 945 | Refresh(false); |
5d7836c4 JS |
946 | return true; |
947 | } | |
948 | else | |
7fe8059f | 949 | return false; |
5d7836c4 JS |
950 | } |
951 | ||
952 | /// Move left | |
953 | bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) | |
954 | { | |
955 | long startPos = -1; | |
956 | ||
957 | if (m_caretPosition > startPos - noPositions + 1) | |
958 | { | |
959 | long oldPos = m_caretPosition; | |
960 | long newPos = m_caretPosition - noPositions; | |
961 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
962 | if (!extendSel) | |
963 | SelectNone(); | |
964 | ||
965 | if (noPositions == 1 && !extendSel) | |
966 | MoveCaretBack(oldPos); | |
967 | else | |
968 | SetCaretPosition(newPos); | |
969 | ||
970 | PositionCaret(); | |
971 | SetDefaultStyleToCursorStyle(); | |
972 | ||
c59f6793 | 973 | if (extendSel) |
76bcd815 | 974 | Refresh(false); |
5d7836c4 JS |
975 | return true; |
976 | } | |
977 | else | |
7fe8059f | 978 | return false; |
5d7836c4 JS |
979 | } |
980 | ||
981 | /// Move up | |
982 | bool wxRichTextCtrl::MoveUp(int noLines, int flags) | |
983 | { | |
984 | return MoveDown(- noLines, flags); | |
985 | } | |
986 | ||
987 | /// Move up | |
988 | bool wxRichTextCtrl::MoveDown(int noLines, int flags) | |
989 | { | |
c59f6793 JS |
990 | if (!GetCaret()) |
991 | return false; | |
992 | ||
5d7836c4 JS |
993 | long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); |
994 | wxPoint pt = GetCaret()->GetPosition(); | |
995 | long newLine = lineNumber + noLines; | |
996 | ||
997 | if (lineNumber != -1) | |
998 | { | |
999 | if (noLines > 0) | |
1000 | { | |
1001 | long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd()); | |
1002 | ||
1003 | if (newLine > lastLine) | |
1004 | return false; | |
1005 | } | |
1006 | else | |
1007 | { | |
1008 | if (newLine < 0) | |
1009 | return false; | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine); | |
1014 | if (lineObj) | |
7fe8059f WS |
1015 | { |
1016 | pt.y = lineObj->GetAbsolutePosition().y + 2; | |
5d7836c4 JS |
1017 | } |
1018 | else | |
1019 | return false; | |
1020 | ||
1021 | long newPos = 0; | |
1022 | wxClientDC dc(this); | |
1023 | PrepareDC(dc); | |
1024 | dc.SetFont(GetFont()); | |
7fe8059f | 1025 | |
5d7836c4 | 1026 | int hitTest = GetBuffer().HitTest(dc, pt, newPos); |
7fe8059f | 1027 | |
5d7836c4 JS |
1028 | if (hitTest != wxRICHTEXT_HITTEST_NONE) |
1029 | { | |
1030 | // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, | |
1031 | // we want to be at the end of the last line but with m_caretAtLineStart set to true, | |
1032 | // so we view the caret at the start of the line. | |
1033 | bool caretLineStart = false; | |
1034 | if (hitTest == wxRICHTEXT_HITTEST_BEFORE) | |
1035 | { | |
1036 | wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1); | |
1e967276 JS |
1037 | wxRichTextRange lineRange; |
1038 | if (thisLine) | |
1039 | lineRange = thisLine->GetAbsoluteRange(); | |
1040 | ||
1041 | if (thisLine && (newPos-1) == lineRange.GetEnd()) | |
5d7836c4 | 1042 | { |
1e967276 JS |
1043 | newPos --; |
1044 | caretLineStart = true; | |
5d7836c4 JS |
1045 | } |
1046 | else | |
1047 | { | |
1048 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos); | |
1049 | if (para && para->GetRange().GetStart() == newPos) | |
1050 | newPos --; | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | long newSelEnd = newPos; | |
1055 | ||
c59f6793 JS |
1056 | bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); |
1057 | if (!extendSel) | |
5d7836c4 JS |
1058 | SelectNone(); |
1059 | ||
1060 | SetCaretPosition(newPos, caretLineStart); | |
1061 | PositionCaret(); | |
1062 | SetDefaultStyleToCursorStyle(); | |
1063 | ||
c59f6793 | 1064 | if (extendSel) |
76bcd815 | 1065 | Refresh(false); |
5d7836c4 JS |
1066 | return true; |
1067 | } | |
41e155b4 WS |
1068 | |
1069 | return false; | |
5d7836c4 JS |
1070 | } |
1071 | ||
1072 | /// Move to the end of the paragraph | |
1073 | bool wxRichTextCtrl::MoveToParagraphEnd(int flags) | |
1074 | { | |
1075 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); | |
1076 | if (para) | |
1077 | { | |
1078 | long newPos = para->GetRange().GetEnd() - 1; | |
c59f6793 JS |
1079 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
1080 | if (!extendSel) | |
5d7836c4 JS |
1081 | SelectNone(); |
1082 | ||
1083 | SetCaretPosition(newPos); | |
1084 | PositionCaret(); | |
1085 | SetDefaultStyleToCursorStyle(); | |
1086 | ||
c59f6793 | 1087 | if (extendSel) |
76bcd815 | 1088 | Refresh(false); |
5d7836c4 JS |
1089 | return true; |
1090 | } | |
1091 | ||
1092 | return false; | |
1093 | } | |
1094 | ||
1095 | /// Move to the start of the paragraph | |
1096 | bool wxRichTextCtrl::MoveToParagraphStart(int flags) | |
1097 | { | |
1098 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); | |
1099 | if (para) | |
1100 | { | |
1101 | long newPos = para->GetRange().GetStart() - 1; | |
c59f6793 JS |
1102 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
1103 | if (!extendSel) | |
5d7836c4 JS |
1104 | SelectNone(); |
1105 | ||
1106 | SetCaretPosition(newPos); | |
1107 | PositionCaret(); | |
1108 | SetDefaultStyleToCursorStyle(); | |
1109 | ||
c59f6793 | 1110 | if (extendSel) |
76bcd815 | 1111 | Refresh(false); |
5d7836c4 JS |
1112 | return true; |
1113 | } | |
1114 | ||
1115 | return false; | |
1116 | } | |
1117 | ||
1118 | /// Move to the end of the line | |
1119 | bool wxRichTextCtrl::MoveToLineEnd(int flags) | |
1120 | { | |
1121 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1122 | ||
1123 | if (line) | |
1124 | { | |
1e967276 JS |
1125 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1126 | long newPos = lineRange.GetEnd(); | |
c59f6793 JS |
1127 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
1128 | if (!extendSel) | |
5d7836c4 JS |
1129 | SelectNone(); |
1130 | ||
1131 | SetCaretPosition(newPos); | |
1132 | PositionCaret(); | |
1133 | SetDefaultStyleToCursorStyle(); | |
1134 | ||
c59f6793 | 1135 | if (extendSel) |
76bcd815 | 1136 | Refresh(false); |
5d7836c4 JS |
1137 | return true; |
1138 | } | |
1139 | ||
1140 | return false; | |
1141 | } | |
1142 | ||
1143 | /// Move to the start of the line | |
1144 | bool wxRichTextCtrl::MoveToLineStart(int flags) | |
1145 | { | |
1146 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1147 | if (line) | |
1148 | { | |
1e967276 JS |
1149 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1150 | long newPos = lineRange.GetStart()-1; | |
5d7836c4 | 1151 | |
c59f6793 JS |
1152 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
1153 | if (!extendSel) | |
5d7836c4 JS |
1154 | SelectNone(); |
1155 | ||
1156 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line); | |
1157 | ||
1e967276 | 1158 | SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 JS |
1159 | PositionCaret(); |
1160 | SetDefaultStyleToCursorStyle(); | |
1161 | ||
c59f6793 | 1162 | if (extendSel) |
76bcd815 | 1163 | Refresh(false); |
5d7836c4 JS |
1164 | return true; |
1165 | } | |
1166 | ||
1167 | return false; | |
1168 | } | |
1169 | ||
1170 | /// Move to the start of the buffer | |
1171 | bool wxRichTextCtrl::MoveHome(int flags) | |
1172 | { | |
1173 | if (m_caretPosition != -1) | |
1174 | { | |
c59f6793 JS |
1175 | bool extendSel = ExtendSelection(m_caretPosition, -1, flags); |
1176 | if (!extendSel) | |
5d7836c4 JS |
1177 | SelectNone(); |
1178 | ||
1179 | SetCaretPosition(-1); | |
1180 | PositionCaret(); | |
1181 | SetDefaultStyleToCursorStyle(); | |
1182 | ||
c59f6793 | 1183 | if (extendSel) |
76bcd815 | 1184 | Refresh(false); |
5d7836c4 JS |
1185 | return true; |
1186 | } | |
1187 | else | |
7fe8059f | 1188 | return false; |
5d7836c4 JS |
1189 | } |
1190 | ||
1191 | /// Move to the end of the buffer | |
1192 | bool wxRichTextCtrl::MoveEnd(int flags) | |
1193 | { | |
1194 | long endPos = GetBuffer().GetRange().GetEnd()-1; | |
1195 | ||
1196 | if (m_caretPosition != endPos) | |
1197 | { | |
c59f6793 JS |
1198 | bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); |
1199 | if (!extendSel) | |
5d7836c4 JS |
1200 | SelectNone(); |
1201 | ||
1202 | SetCaretPosition(endPos); | |
1203 | PositionCaret(); | |
1204 | SetDefaultStyleToCursorStyle(); | |
1205 | ||
c59f6793 | 1206 | if (extendSel) |
76bcd815 | 1207 | Refresh(false); |
5d7836c4 JS |
1208 | return true; |
1209 | } | |
1210 | else | |
7fe8059f | 1211 | return false; |
5d7836c4 JS |
1212 | } |
1213 | ||
1214 | /// Move noPages pages up | |
1215 | bool wxRichTextCtrl::PageUp(int noPages, int flags) | |
1216 | { | |
1217 | return PageDown(- noPages, flags); | |
1218 | } | |
1219 | ||
1220 | /// Move noPages pages down | |
1221 | bool wxRichTextCtrl::PageDown(int noPages, int flags) | |
1222 | { | |
1223 | // Calculate which line occurs noPages * screen height further down. | |
1224 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1225 | if (line) | |
1226 | { | |
1227 | wxSize clientSize = GetClientSize(); | |
1228 | int newY = line->GetAbsolutePosition().y + noPages*clientSize.y; | |
1229 | ||
1230 | wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY); | |
1231 | if (newLine) | |
1232 | { | |
1e967276 JS |
1233 | wxRichTextRange lineRange = newLine->GetAbsoluteRange(); |
1234 | long pos = lineRange.GetStart()-1; | |
5d7836c4 JS |
1235 | if (pos != m_caretPosition) |
1236 | { | |
1237 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine); | |
1238 | ||
c59f6793 JS |
1239 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
1240 | if (!extendSel) | |
5d7836c4 JS |
1241 | SelectNone(); |
1242 | ||
1e967276 | 1243 | SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 JS |
1244 | PositionCaret(); |
1245 | SetDefaultStyleToCursorStyle(); | |
1246 | ||
c59f6793 | 1247 | if (extendSel) |
76bcd815 | 1248 | Refresh(false); |
5d7836c4 JS |
1249 | return true; |
1250 | } | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | return false; | |
1255 | } | |
1256 | ||
1257 | // Finds the caret position for the next word | |
1258 | long wxRichTextCtrl::FindNextWordPosition(int direction) const | |
7fe8059f | 1259 | { |
5d7836c4 JS |
1260 | long endPos = GetBuffer().GetRange().GetEnd(); |
1261 | ||
1262 | if (direction > 0) | |
1263 | { | |
1264 | long i = m_caretPosition+1+direction; // +1 for conversion to character pos | |
7fe8059f | 1265 | |
5d7836c4 JS |
1266 | // First skip current text to space |
1267 | while (i < endPos && i > -1) | |
1268 | { | |
1269 | // i is in character, not caret positions | |
1270 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
7fe8059f | 1271 | if (text != wxT(" ") && !text.empty()) |
5d7836c4 JS |
1272 | i += direction; |
1273 | else | |
1274 | { | |
1275 | break; | |
1276 | } | |
1277 | } | |
1278 | while (i < endPos && i > -1) | |
1279 | { | |
1280 | // i is in character, not caret positions | |
1281 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
7fe8059f | 1282 | if (text.empty()) // End of paragraph, or maybe an image |
5d7836c4 | 1283 | return wxMax(-1, i - 1); |
7fe8059f | 1284 | else if (text == wxT(" ") || text.empty()) |
5d7836c4 JS |
1285 | i += direction; |
1286 | else | |
1287 | { | |
1288 | // Convert to caret position | |
1289 | return wxMax(-1, i - 1); | |
1290 | } | |
1291 | } | |
1292 | if (i >= endPos) | |
1293 | return endPos-1; | |
1294 | return i-1; | |
1295 | } | |
1296 | else | |
1297 | { | |
1298 | long i = m_caretPosition; | |
1299 | ||
1300 | // First skip white space | |
1301 | while (i < endPos && i > -1) | |
1302 | { | |
1303 | // i is in character, not caret positions | |
1304 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
7fe8059f | 1305 | if (text.empty()) // End of paragraph, or maybe an image |
5d7836c4 | 1306 | break; |
7fe8059f | 1307 | else if (text == wxT(" ") || text.empty()) |
5d7836c4 JS |
1308 | i += direction; |
1309 | else | |
1310 | break; | |
1311 | } | |
1312 | // Next skip current text to space | |
1313 | while (i < endPos && i > -1) | |
1314 | { | |
1315 | // i is in character, not caret positions | |
1316 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
7fe8059f | 1317 | if (text != wxT(" ") /* && !text.empty() */) |
5d7836c4 JS |
1318 | i += direction; |
1319 | else | |
1320 | { | |
1321 | return i; | |
5d7836c4 JS |
1322 | } |
1323 | } | |
1324 | if (i < -1) | |
1325 | return -1; | |
1326 | return i; | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | /// Move n words left | |
1331 | bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) | |
1332 | { | |
1333 | long pos = FindNextWordPosition(-1); | |
1334 | if (pos != m_caretPosition) | |
1335 | { | |
1336 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); | |
7fe8059f | 1337 | |
c59f6793 JS |
1338 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
1339 | if (!extendSel) | |
5d7836c4 | 1340 | SelectNone(); |
7fe8059f | 1341 | |
5d7836c4 JS |
1342 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
1343 | PositionCaret(); | |
1344 | SetDefaultStyleToCursorStyle(); | |
1345 | ||
c59f6793 | 1346 | if (extendSel) |
76bcd815 | 1347 | Refresh(false); |
5d7836c4 JS |
1348 | return true; |
1349 | } | |
7fe8059f | 1350 | |
5d7836c4 JS |
1351 | return false; |
1352 | } | |
1353 | ||
1354 | /// Move n words right | |
1355 | bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) | |
1356 | { | |
1357 | long pos = FindNextWordPosition(1); | |
1358 | if (pos != m_caretPosition) | |
1359 | { | |
1360 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); | |
7fe8059f | 1361 | |
c59f6793 JS |
1362 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
1363 | if (!extendSel) | |
5d7836c4 | 1364 | SelectNone(); |
7fe8059f | 1365 | |
5d7836c4 JS |
1366 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
1367 | PositionCaret(); | |
1368 | SetDefaultStyleToCursorStyle(); | |
1369 | ||
c59f6793 | 1370 | if (extendSel) |
76bcd815 | 1371 | Refresh(false); |
5d7836c4 JS |
1372 | return true; |
1373 | } | |
7fe8059f | 1374 | |
5d7836c4 JS |
1375 | return false; |
1376 | } | |
1377 | ||
1378 | /// Sizing | |
1379 | void wxRichTextCtrl::OnSize(wxSizeEvent& event) | |
1380 | { | |
4d551ad5 JS |
1381 | // Only do sizing optimization for large buffers |
1382 | if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold) | |
1383 | { | |
1384 | m_fullLayoutRequired = true; | |
1385 | m_fullLayoutTime = wxGetLocalTimeMillis(); | |
1386 | m_fullLayoutSavedPosition = GetFirstVisiblePosition(); | |
2f36e8dc | 1387 | LayoutContent(true /* onlyVisibleRect */); |
4d551ad5 JS |
1388 | } |
1389 | else | |
1390 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
7fe8059f | 1391 | |
5d7836c4 JS |
1392 | RecreateBuffer(); |
1393 | ||
1394 | event.Skip(); | |
1395 | } | |
1396 | ||
4d551ad5 JS |
1397 | |
1398 | /// Idle-time processing | |
1399 | void wxRichTextCtrl::OnIdle(wxIdleEvent& event) | |
1400 | { | |
1401 | const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL; | |
1402 | ||
1403 | if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval))) | |
1404 | { | |
1405 | m_fullLayoutRequired = false; | |
1406 | m_fullLayoutTime = 0; | |
1407 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
1408 | ShowPosition(m_fullLayoutSavedPosition); | |
76bcd815 | 1409 | Refresh(false); |
4d551ad5 JS |
1410 | } |
1411 | event.Skip(); | |
1412 | } | |
1413 | ||
c59f6793 JS |
1414 | /// Scrolling |
1415 | void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) | |
1416 | { | |
1417 | // Not used | |
1418 | event.Skip(); | |
1419 | } | |
1420 | ||
5d7836c4 | 1421 | /// Set up scrollbars, e.g. after a resize |
169adfa9 | 1422 | void wxRichTextCtrl::SetupScrollbars(bool atTop) |
5d7836c4 JS |
1423 | { |
1424 | if (m_freezeCount) | |
1425 | return; | |
1426 | ||
1427 | if (GetBuffer().IsEmpty()) | |
1428 | { | |
1429 | SetScrollbars(0, 0, 0, 0, 0, 0); | |
1430 | return; | |
1431 | } | |
1432 | ||
1433 | // TODO: reimplement scrolling so we scroll by line, not by fixed number | |
1434 | // of pixels. See e.g. wxVScrolledWindow for ideas. | |
1435 | int pixelsPerUnit = 5; // 10; | |
1436 | wxSize clientSize = GetClientSize(); | |
1437 | ||
1438 | int maxHeight = GetBuffer().GetCachedSize().y; | |
1439 | ||
1440 | int unitsY = maxHeight/pixelsPerUnit; | |
1441 | ||
169adfa9 JS |
1442 | int startX = 0, startY = 0; |
1443 | if (!atTop) | |
1444 | GetViewStart(& startX, & startY); | |
7fe8059f | 1445 | |
5d7836c4 JS |
1446 | int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0); |
1447 | int maxPositionY = (wxMax(maxHeight - clientSize.y, 0))/pixelsPerUnit; | |
7fe8059f | 1448 | |
5d7836c4 JS |
1449 | // Move to previous scroll position if |
1450 | // possible | |
1451 | SetScrollbars(0, pixelsPerUnit, | |
1452 | 0, unitsY, | |
1453 | wxMin(maxPositionX, startX), wxMin(maxPositionY, startY)); | |
1454 | } | |
1455 | ||
1456 | /// Paint the background | |
1457 | void wxRichTextCtrl::PaintBackground(wxDC& dc) | |
1458 | { | |
1459 | wxColour backgroundColour = GetBackgroundColour(); | |
1460 | if (!backgroundColour.Ok()) | |
1461 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
1462 | ||
1463 | // Clear the background | |
1464 | dc.SetBrush(wxBrush(backgroundColour)); | |
1465 | dc.SetPen(*wxTRANSPARENT_PEN); | |
7fe8059f | 1466 | wxRect windowRect(GetClientSize()); |
5d7836c4 JS |
1467 | windowRect.x -= 2; windowRect.y -= 2; |
1468 | windowRect.width += 4; windowRect.height += 4; | |
7fe8059f | 1469 | |
5d7836c4 JS |
1470 | // We need to shift the rectangle to take into account |
1471 | // scrolling. Converting device to logical coordinates. | |
1472 | CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y); | |
1473 | dc.DrawRectangle(windowRect); | |
1474 | } | |
1475 | ||
1476 | /// Recreate buffer bitmap if necessary | |
1477 | bool wxRichTextCtrl::RecreateBuffer(const wxSize& size) | |
1478 | { | |
1479 | wxSize sz = size; | |
1480 | if (sz == wxDefaultSize) | |
1481 | sz = GetClientSize(); | |
7fe8059f | 1482 | |
5d7836c4 JS |
1483 | if (sz.x < 1 || sz.y < 1) |
1484 | return false; | |
7fe8059f | 1485 | |
5d7836c4 JS |
1486 | if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y) |
1487 | m_bufferBitmap = wxBitmap(sz.x, sz.y); | |
1488 | return m_bufferBitmap.Ok(); | |
1489 | } | |
1490 | ||
1491 | // ---------------------------------------------------------------------------- | |
1492 | // file IO functions | |
1493 | // ---------------------------------------------------------------------------- | |
1494 | ||
1495 | bool wxRichTextCtrl::LoadFile(const wxString& filename, int type) | |
1496 | { | |
1497 | bool success = GetBuffer().LoadFile(filename, type); | |
1498 | if (success) | |
1499 | m_filename = filename; | |
1500 | ||
1501 | DiscardEdits(); | |
1502 | SetInsertionPoint(0); | |
2f36e8dc | 1503 | LayoutContent(); |
5d7836c4 | 1504 | PositionCaret(); |
169adfa9 | 1505 | SetupScrollbars(true); |
76bcd815 | 1506 | Refresh(false); |
5d7836c4 JS |
1507 | SendUpdateEvent(); |
1508 | ||
1509 | if (success) | |
1510 | return true; | |
1511 | else | |
1512 | { | |
1513 | wxLogError(_("File couldn't be loaded.")); | |
7fe8059f | 1514 | |
5d7836c4 JS |
1515 | return false; |
1516 | } | |
1517 | } | |
1518 | ||
1519 | bool wxRichTextCtrl::SaveFile(const wxString& filename, int type) | |
1520 | { | |
1521 | wxString filenameToUse = filename.empty() ? m_filename : filename; | |
1522 | if ( filenameToUse.empty() ) | |
1523 | { | |
1524 | // what kind of message to give? is it an error or a program bug? | |
1525 | wxLogDebug(wxT("Can't save textctrl to file without filename.")); | |
1526 | ||
1527 | return false; | |
1528 | } | |
1529 | ||
1530 | if (GetBuffer().SaveFile(filenameToUse, type)) | |
1531 | { | |
1532 | m_filename = filenameToUse; | |
1533 | ||
1534 | DiscardEdits(); | |
1535 | ||
1536 | return true; | |
1537 | ||
1538 | } | |
1539 | ||
1540 | wxLogError(_("The text couldn't be saved.")); | |
1541 | ||
1542 | return false; | |
1543 | } | |
1544 | ||
1545 | // ---------------------------------------------------------------------------- | |
1546 | // wxRichTextCtrl specific functionality | |
1547 | // ---------------------------------------------------------------------------- | |
1548 | ||
1549 | /// Add a new paragraph of text to the end of the buffer | |
1550 | wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text) | |
1551 | { | |
1552 | return GetBuffer().AddParagraph(text); | |
1553 | } | |
1554 | ||
1555 | /// Add an image | |
1556 | wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image) | |
1557 | { | |
1558 | return GetBuffer().AddImage(image); | |
1559 | } | |
1560 | ||
1561 | // ---------------------------------------------------------------------------- | |
1562 | // selection and ranges | |
1563 | // ---------------------------------------------------------------------------- | |
1564 | ||
1565 | void wxRichTextCtrl::SelectAll() | |
1566 | { | |
1567 | SetSelection(0, GetLastPosition()); | |
1568 | m_selectionAnchor = -1; | |
1569 | } | |
1570 | ||
1571 | /// Select none | |
1572 | void wxRichTextCtrl::SelectNone() | |
1573 | { | |
c59f6793 JS |
1574 | if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) |
1575 | SetSelection(-2, -2); | |
5d7836c4 JS |
1576 | m_selectionAnchor = -2; |
1577 | } | |
1578 | ||
1579 | wxString wxRichTextCtrl::GetStringSelection() const | |
1580 | { | |
1581 | long from, to; | |
1582 | GetSelection(&from, &to); | |
1583 | ||
1584 | return GetRange(from, to); | |
1585 | } | |
1586 | ||
1587 | // do the window-specific processing after processing the update event | |
a3a4105d | 1588 | #if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
5d7836c4 JS |
1589 | void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event) |
1590 | { | |
a3a4105d VZ |
1591 | // call inherited |
1592 | wxWindowBase::DoUpdateWindowUI(event); | |
5d7836c4 | 1593 | |
a3a4105d | 1594 | // update text |
5d7836c4 JS |
1595 | if ( event.GetSetText() ) |
1596 | { | |
1597 | if ( event.GetText() != GetValue() ) | |
1598 | SetValue(event.GetText()); | |
1599 | } | |
1600 | } | |
a3a4105d | 1601 | #endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE |
5d7836c4 JS |
1602 | |
1603 | // ---------------------------------------------------------------------------- | |
1604 | // hit testing | |
1605 | // ---------------------------------------------------------------------------- | |
1606 | ||
1607 | wxTextCtrlHitTestResult | |
1608 | wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const | |
1609 | { | |
1610 | // implement in terms of the other overload as the native ports typically | |
1611 | // can get the position and not (x, y) pair directly (although wxUniv | |
1612 | // directly gets x and y -- and so overrides this method as well) | |
1613 | long pos; | |
1614 | wxTextCtrlHitTestResult rc = HitTest(pt, &pos); | |
1615 | ||
1616 | if ( rc != wxTE_HT_UNKNOWN ) | |
1617 | { | |
1618 | PositionToXY(pos, x, y); | |
1619 | } | |
1620 | ||
1621 | return rc; | |
1622 | } | |
1623 | ||
1624 | wxTextCtrlHitTestResult | |
1625 | wxRichTextCtrl::HitTest(const wxPoint& pt, | |
1626 | long * pos) const | |
1627 | { | |
1628 | wxClientDC dc((wxRichTextCtrl*) this); | |
1629 | ((wxRichTextCtrl*)this)->PrepareDC(dc); | |
563d25ba JS |
1630 | |
1631 | // Buffer uses logical position (relative to start of buffer) | |
1632 | // so convert | |
1633 | wxPoint pt2 = GetLogicalPoint(pt); | |
5d7836c4 | 1634 | |
563d25ba | 1635 | int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos); |
41e155b4 WS |
1636 | |
1637 | switch ( hit ) | |
1638 | { | |
1639 | case wxRICHTEXT_HITTEST_BEFORE: | |
1640 | return wxTE_HT_BEFORE; | |
1641 | ||
1642 | case wxRICHTEXT_HITTEST_AFTER: | |
1643 | return wxTE_HT_BEYOND; | |
1644 | ||
1645 | case wxRICHTEXT_HITTEST_ON: | |
1646 | return wxTE_HT_ON_TEXT; | |
1647 | } | |
1648 | ||
1649 | return wxTE_HT_UNKNOWN; | |
5d7836c4 JS |
1650 | } |
1651 | ||
1652 | // ---------------------------------------------------------------------------- | |
1653 | // set/get the controls text | |
1654 | // ---------------------------------------------------------------------------- | |
1655 | ||
1656 | wxString wxRichTextCtrl::GetValue() const | |
1657 | { | |
1658 | return GetBuffer().GetText(); | |
1659 | } | |
1660 | ||
1661 | wxString wxRichTextCtrl::GetRange(long from, long to) const | |
1662 | { | |
1663 | return GetBuffer().GetTextForRange(wxRichTextRange(from, to)); | |
1664 | } | |
1665 | ||
1666 | void wxRichTextCtrl::SetValue(const wxString& value) | |
1667 | { | |
1668 | Clear(); | |
1669 | ||
1670 | // if the text is long enough, it's faster to just set it instead of first | |
1671 | // comparing it with the old one (chances are that it will be different | |
1672 | // anyhow, this comparison is there to avoid flicker for small single-line | |
1673 | // edit controls mostly) | |
1674 | if ( (value.length() > 0x400) || (value != GetValue()) ) | |
1675 | { | |
1676 | DoWriteText(value, false /* not selection only */); | |
1677 | ||
1678 | // for compatibility, don't move the cursor when doing SetValue() | |
1679 | SetInsertionPoint(0); | |
1680 | } | |
1681 | else // same text | |
1682 | { | |
1683 | // still send an event for consistency | |
1684 | SendUpdateEvent(); | |
1685 | } | |
1686 | ||
1687 | // we should reset the modified flag even if the value didn't really change | |
1688 | ||
1689 | // mark the control as being not dirty - we changed its text, not the | |
1690 | // user | |
1691 | DiscardEdits(); | |
1692 | } | |
1693 | ||
1694 | void wxRichTextCtrl::WriteText(const wxString& value) | |
1695 | { | |
1696 | DoWriteText(value); | |
1697 | } | |
1698 | ||
1699 | void wxRichTextCtrl::DoWriteText(const wxString& value, bool WXUNUSED(selectionOnly)) | |
1700 | { | |
1701 | GetBuffer().InsertTextWithUndo(m_caretPosition+1, value, this); | |
1702 | } | |
1703 | ||
1704 | void wxRichTextCtrl::AppendText(const wxString& text) | |
1705 | { | |
1706 | SetInsertionPointEnd(); | |
1707 | ||
1708 | WriteText(text); | |
1709 | } | |
1710 | ||
1711 | /// Write an image at the current insertion point | |
1712 | bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType) | |
1713 | { | |
1714 | wxRichTextImageBlock imageBlock; | |
1715 | ||
1716 | wxImage image2 = image; | |
1717 | if (imageBlock.MakeImageBlock(image2, bitmapType)) | |
1718 | return WriteImage(imageBlock); | |
41e155b4 WS |
1719 | |
1720 | return false; | |
5d7836c4 JS |
1721 | } |
1722 | ||
1723 | bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) | |
1724 | { | |
1725 | wxRichTextImageBlock imageBlock; | |
1726 | ||
1727 | wxImage image; | |
1728 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) | |
1729 | return WriteImage(imageBlock); | |
41e155b4 WS |
1730 | |
1731 | return false; | |
5d7836c4 JS |
1732 | } |
1733 | ||
1734 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock) | |
1735 | { | |
1736 | return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this); | |
1737 | } | |
1738 | ||
1739 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType) | |
1740 | { | |
1741 | if (bitmap.Ok()) | |
1742 | { | |
1743 | wxRichTextImageBlock imageBlock; | |
7fe8059f | 1744 | |
5d7836c4 JS |
1745 | wxImage image = bitmap.ConvertToImage(); |
1746 | if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType)) | |
1747 | return WriteImage(imageBlock); | |
5d7836c4 | 1748 | } |
41e155b4 | 1749 | |
5d7836c4 JS |
1750 | return false; |
1751 | } | |
1752 | ||
1753 | /// Insert a newline (actually paragraph) at the current insertion point. | |
1754 | bool wxRichTextCtrl::Newline() | |
1755 | { | |
1756 | return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this); | |
1757 | } | |
1758 | ||
1759 | ||
1760 | // ---------------------------------------------------------------------------- | |
1761 | // Clipboard operations | |
1762 | // ---------------------------------------------------------------------------- | |
1763 | ||
1764 | void wxRichTextCtrl::Copy() | |
1765 | { | |
1766 | if (CanCopy()) | |
1767 | { | |
1768 | wxRichTextRange range = GetSelectionRange(); | |
1769 | GetBuffer().CopyToClipboard(range); | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | void wxRichTextCtrl::Cut() | |
1774 | { | |
1775 | if (CanCut()) | |
1776 | { | |
1777 | wxRichTextRange range = GetSelectionRange(); | |
1778 | GetBuffer().CopyToClipboard(range); | |
1779 | ||
1780 | DeleteSelectedContent(); | |
2f36e8dc | 1781 | LayoutContent(); |
76bcd815 | 1782 | Refresh(false); |
5d7836c4 JS |
1783 | } |
1784 | } | |
1785 | ||
1786 | void wxRichTextCtrl::Paste() | |
1787 | { | |
1788 | if (CanPaste()) | |
1789 | { | |
1790 | BeginBatchUndo(_("Paste")); | |
7fe8059f | 1791 | |
5d7836c4 JS |
1792 | long newPos = m_caretPosition; |
1793 | DeleteSelectedContent(& newPos); | |
7fe8059f | 1794 | |
5d7836c4 JS |
1795 | GetBuffer().PasteFromClipboard(newPos); |
1796 | ||
1797 | EndBatchUndo(); | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | void wxRichTextCtrl::DeleteSelection() | |
1802 | { | |
1803 | if (CanDeleteSelection()) | |
1804 | { | |
1805 | DeleteSelectedContent(); | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | bool wxRichTextCtrl::HasSelection() const | |
1810 | { | |
1811 | return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2; | |
1812 | } | |
1813 | ||
1814 | bool wxRichTextCtrl::CanCopy() const | |
1815 | { | |
1816 | // Can copy if there's a selection | |
1817 | return HasSelection(); | |
1818 | } | |
1819 | ||
1820 | bool wxRichTextCtrl::CanCut() const | |
1821 | { | |
1822 | return HasSelection() && IsEditable(); | |
1823 | } | |
1824 | ||
1825 | bool wxRichTextCtrl::CanPaste() const | |
1826 | { | |
1827 | if ( !IsEditable() ) | |
1828 | return false; | |
1829 | ||
1830 | return GetBuffer().CanPasteFromClipboard(); | |
1831 | } | |
1832 | ||
1833 | bool wxRichTextCtrl::CanDeleteSelection() const | |
1834 | { | |
1835 | return HasSelection() && IsEditable(); | |
1836 | } | |
1837 | ||
1838 | ||
1839 | // ---------------------------------------------------------------------------- | |
1840 | // Accessors | |
1841 | // ---------------------------------------------------------------------------- | |
1842 | ||
1843 | void wxRichTextCtrl::SetEditable(bool editable) | |
1844 | { | |
1845 | m_editable = editable; | |
1846 | } | |
1847 | ||
1848 | void wxRichTextCtrl::SetInsertionPoint(long pos) | |
1849 | { | |
1850 | SelectNone(); | |
1851 | ||
1852 | m_caretPosition = pos - 1; | |
1853 | } | |
1854 | ||
1855 | void wxRichTextCtrl::SetInsertionPointEnd() | |
1856 | { | |
1857 | long pos = GetLastPosition(); | |
1858 | SetInsertionPoint(pos); | |
1859 | } | |
1860 | ||
1861 | long wxRichTextCtrl::GetInsertionPoint() const | |
1862 | { | |
1863 | return m_caretPosition+1; | |
1864 | } | |
1865 | ||
1866 | wxTextPos wxRichTextCtrl::GetLastPosition() const | |
1867 | { | |
1868 | return GetBuffer().GetRange().GetEnd(); | |
1869 | } | |
1870 | ||
1871 | // If the return values from and to are the same, there is no | |
1872 | // selection. | |
1873 | void wxRichTextCtrl::GetSelection(long* from, long* to) const | |
1874 | { | |
1875 | *from = m_selectionRange.GetStart(); | |
1876 | *to = m_selectionRange.GetEnd(); | |
1877 | } | |
1878 | ||
1879 | bool wxRichTextCtrl::IsEditable() const | |
1880 | { | |
1881 | return m_editable; | |
1882 | } | |
1883 | ||
1884 | // ---------------------------------------------------------------------------- | |
1885 | // selection | |
1886 | // ---------------------------------------------------------------------------- | |
1887 | ||
1888 | void wxRichTextCtrl::SetSelection(long from, long to) | |
1889 | { | |
1890 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
1891 | // be selected. | |
1892 | if ( (from == -1) && (to == -1) ) | |
1893 | { | |
1894 | from = 0; | |
1895 | to = GetLastPosition(); | |
1896 | } | |
1897 | ||
1898 | DoSetSelection(from, to); | |
1899 | } | |
1900 | ||
1901 | void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret)) | |
1902 | { | |
1903 | m_selectionAnchor = from; | |
1904 | m_selectionRange.SetRange(from, to); | |
76bcd815 | 1905 | Refresh(false); |
5d7836c4 JS |
1906 | PositionCaret(); |
1907 | } | |
1908 | ||
1909 | // ---------------------------------------------------------------------------- | |
1910 | // Editing | |
1911 | // ---------------------------------------------------------------------------- | |
1912 | ||
1913 | void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), const wxString& value) | |
1914 | { | |
1915 | BeginBatchUndo(_("Replace")); | |
1916 | ||
1917 | DeleteSelectedContent(); | |
1918 | ||
1919 | DoWriteText(value, true /* selection only */); | |
1920 | ||
1921 | EndBatchUndo(); | |
1922 | } | |
1923 | ||
1924 | void wxRichTextCtrl::Remove(long from, long to) | |
1925 | { | |
1926 | SelectNone(); | |
1927 | ||
1928 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), | |
1929 | m_caretPosition, // Current caret position | |
1930 | from, // New caret position | |
1931 | this); | |
1932 | ||
2f36e8dc | 1933 | LayoutContent(); |
5d7836c4 | 1934 | if (!IsFrozen()) |
76bcd815 | 1935 | Refresh(false); |
5d7836c4 JS |
1936 | } |
1937 | ||
1938 | bool wxRichTextCtrl::IsModified() const | |
1939 | { | |
1940 | return m_buffer.IsModified(); | |
1941 | } | |
1942 | ||
1943 | void wxRichTextCtrl::MarkDirty() | |
1944 | { | |
1945 | m_buffer.Modify(true); | |
1946 | } | |
1947 | ||
1948 | void wxRichTextCtrl::DiscardEdits() | |
1949 | { | |
1950 | m_buffer.Modify(false); | |
1951 | m_buffer.GetCommandProcessor()->ClearCommands(); | |
1952 | } | |
1953 | ||
1954 | int wxRichTextCtrl::GetNumberOfLines() const | |
1955 | { | |
1956 | return GetBuffer().GetParagraphCount(); | |
1957 | } | |
1958 | ||
1959 | // ---------------------------------------------------------------------------- | |
1960 | // Positions <-> coords | |
1961 | // ---------------------------------------------------------------------------- | |
1962 | ||
1963 | long wxRichTextCtrl::XYToPosition(long x, long y) const | |
1964 | { | |
1965 | return GetBuffer().XYToPosition(x, y); | |
1966 | } | |
1967 | ||
1968 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
1969 | { | |
1970 | return GetBuffer().PositionToXY(pos, x, y); | |
1971 | } | |
1972 | ||
1973 | // ---------------------------------------------------------------------------- | |
1974 | // | |
1975 | // ---------------------------------------------------------------------------- | |
1976 | ||
1977 | void wxRichTextCtrl::ShowPosition(long pos) | |
1978 | { | |
1979 | if (!IsPositionVisible(pos)) | |
1980 | ScrollIntoView(pos-1, WXK_DOWN); | |
1981 | } | |
1982 | ||
1983 | int wxRichTextCtrl::GetLineLength(long lineNo) const | |
1984 | { | |
1985 | return GetBuffer().GetParagraphLength(lineNo); | |
1986 | } | |
1987 | ||
1988 | wxString wxRichTextCtrl::GetLineText(long lineNo) const | |
1989 | { | |
1990 | return GetBuffer().GetParagraphText(lineNo); | |
1991 | } | |
1992 | ||
1993 | // ---------------------------------------------------------------------------- | |
1994 | // Undo/redo | |
1995 | // ---------------------------------------------------------------------------- | |
1996 | ||
1997 | void wxRichTextCtrl::Undo() | |
1998 | { | |
1999 | if (CanUndo()) | |
2000 | { | |
2001 | GetCommandProcessor()->Undo(); | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | void wxRichTextCtrl::Redo() | |
2006 | { | |
2007 | if (CanRedo()) | |
2008 | { | |
2009 | GetCommandProcessor()->Redo(); | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | bool wxRichTextCtrl::CanUndo() const | |
2014 | { | |
2015 | return GetCommandProcessor()->CanUndo(); | |
2016 | } | |
2017 | ||
2018 | bool wxRichTextCtrl::CanRedo() const | |
2019 | { | |
2020 | return GetCommandProcessor()->CanRedo(); | |
2021 | } | |
2022 | ||
2023 | // ---------------------------------------------------------------------------- | |
563d25ba | 2024 | // implementation details |
5d7836c4 JS |
2025 | // ---------------------------------------------------------------------------- |
2026 | ||
563d25ba | 2027 | void wxRichTextCtrl::Command(wxCommandEvent& event) |
5d7836c4 JS |
2028 | { |
2029 | SetValue(event.GetString()); | |
2030 | GetEventHandler()->ProcessEvent(event); | |
2031 | } | |
2032 | ||
2033 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
2034 | { | |
2035 | // By default, load the first file into the text window. | |
2036 | if (event.GetNumberOfFiles() > 0) | |
2037 | { | |
2038 | LoadFile(event.GetFiles()[0]); | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | // ---------------------------------------------------------------------------- | |
2043 | // text control event processing | |
2044 | // ---------------------------------------------------------------------------- | |
2045 | ||
2046 | bool wxRichTextCtrl::SendUpdateEvent() | |
2047 | { | |
2048 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
2049 | InitCommandEvent(event); | |
2050 | ||
2051 | return GetEventHandler()->ProcessEvent(event); | |
2052 | } | |
2053 | ||
2054 | void wxRichTextCtrl::InitCommandEvent(wxCommandEvent& event) const | |
2055 | { | |
2056 | event.SetEventObject((wxControlBase *)this); // const_cast | |
2057 | ||
2058 | switch ( m_clientDataType ) | |
2059 | { | |
2060 | case wxClientData_Void: | |
2061 | event.SetClientData(GetClientData()); | |
2062 | break; | |
2063 | ||
2064 | case wxClientData_Object: | |
2065 | event.SetClientObject(GetClientObject()); | |
2066 | break; | |
2067 | ||
2068 | case wxClientData_None: | |
2069 | // nothing to do | |
2070 | ; | |
2071 | } | |
2072 | } | |
2073 | ||
2074 | ||
2075 | wxSize wxRichTextCtrl::DoGetBestSize() const | |
2076 | { | |
2077 | return wxSize(10, 10); | |
2078 | } | |
2079 | ||
2080 | // ---------------------------------------------------------------------------- | |
2081 | // standard handlers for standard edit menu events | |
2082 | // ---------------------------------------------------------------------------- | |
2083 | ||
2084 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
2085 | { | |
2086 | Cut(); | |
2087 | } | |
2088 | ||
2089 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) | |
2090 | { | |
2091 | DeleteSelection(); | |
2092 | } | |
2093 | ||
2094 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2095 | { | |
2096 | Copy(); | |
2097 | } | |
2098 | ||
2099 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2100 | { | |
2101 | Paste(); | |
2102 | } | |
2103 | ||
2104 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2105 | { | |
2106 | Undo(); | |
2107 | } | |
2108 | ||
2109 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2110 | { | |
2111 | Redo(); | |
2112 | } | |
2113 | ||
2114 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2115 | { | |
2116 | event.Enable( CanCut() ); | |
2117 | } | |
2118 | ||
2119 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2120 | { | |
2121 | event.Enable( CanCopy() ); | |
2122 | } | |
2123 | ||
2124 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) | |
2125 | { | |
2126 | event.Enable( CanDeleteSelection() ); | |
2127 | } | |
2128 | ||
2129 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2130 | { | |
2131 | event.Enable( CanPaste() ); | |
2132 | } | |
2133 | ||
2134 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2135 | { | |
2136 | event.Enable( CanUndo() ); | |
2137 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); | |
2138 | } | |
2139 | ||
2140 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2141 | { | |
2142 | event.Enable( CanRedo() ); | |
2143 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); | |
2144 | } | |
2145 | ||
2146 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
2147 | { | |
2148 | SelectAll(); | |
2149 | } | |
2150 | ||
2151 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
2152 | { | |
2153 | event.Enable(GetLastPosition() > 0); | |
2154 | } | |
2155 | ||
2156 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event)) | |
2157 | { | |
2158 | if (!m_contextMenu) | |
2159 | { | |
2160 | m_contextMenu = new wxMenu; | |
2161 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); | |
2162 | m_contextMenu->Append(wxID_REDO, _("&Redo")); | |
2163 | m_contextMenu->AppendSeparator(); | |
2164 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); | |
2165 | m_contextMenu->Append(wxID_COPY, _("&Copy")); | |
2166 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); | |
2167 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); | |
2168 | m_contextMenu->AppendSeparator(); | |
2169 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
2170 | } | |
2171 | PopupMenu(m_contextMenu); | |
2172 | return; | |
2173 | } | |
2174 | ||
2175 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) | |
2176 | { | |
2177 | return GetBuffer().SetStyle(wxRichTextRange(start, end), style); | |
2178 | } | |
2179 | ||
2180 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) | |
2181 | { | |
2182 | return GetBuffer().SetStyle(range, style); | |
2183 | } | |
2184 | ||
2185 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) | |
2186 | { | |
2187 | return GetBuffer().SetDefaultStyle(style); | |
2188 | } | |
2189 | ||
2190 | const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const | |
2191 | { | |
2192 | return GetBuffer().GetDefaultStyle(); | |
2193 | } | |
2194 | ||
2195 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) const | |
2196 | { | |
2197 | return GetBuffer().GetStyle(position, style); | |
2198 | } | |
2199 | ||
2200 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) const | |
2201 | { | |
2202 | return GetBuffer().GetStyle(position, style); | |
2203 | } | |
2204 | ||
2205 | /// Set font, and also the buffer attributes | |
2206 | bool wxRichTextCtrl::SetFont(const wxFont& font) | |
2207 | { | |
2208 | #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE | |
2209 | wxControl::SetFont(font); | |
2210 | #else | |
2211 | wxScrolledWindow::SetFont(font); | |
2212 | #endif | |
2213 | ||
2214 | wxTextAttrEx attr = GetBuffer().GetAttributes(); | |
2215 | attr.SetFont(font); | |
2216 | GetBuffer().SetBasicStyle(attr); | |
2217 | GetBuffer().SetDefaultStyle(attr); | |
2218 | ||
2219 | return true; | |
2220 | } | |
2221 | ||
4d551ad5 JS |
2222 | /// Transform logical to physical |
2223 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const | |
5d7836c4 JS |
2224 | { |
2225 | wxPoint pt; | |
2226 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); | |
2227 | ||
2228 | return pt; | |
2229 | } | |
2230 | ||
2231 | /// Transform physical to logical | |
4d551ad5 | 2232 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const |
5d7836c4 JS |
2233 | { |
2234 | wxPoint pt; | |
2235 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); | |
2236 | ||
2237 | return pt; | |
2238 | } | |
2239 | ||
2240 | /// Position the caret | |
2241 | void wxRichTextCtrl::PositionCaret() | |
2242 | { | |
c59f6793 JS |
2243 | if (!GetCaret()) |
2244 | return; | |
2245 | ||
2246 | //wxLogDebug(wxT("PositionCaret")); | |
2247 | ||
5d7836c4 JS |
2248 | wxRect caretRect; |
2249 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) | |
2250 | { | |
2251 | wxPoint originalPt = caretRect.GetPosition(); | |
2252 | wxPoint pt = GetPhysicalPoint(originalPt); | |
c59f6793 JS |
2253 | if (GetCaret()->GetPosition() != pt) |
2254 | { | |
2255 | GetCaret()->Move(pt); | |
2256 | GetCaret()->SetSize(caretRect.GetSize()); | |
2257 | } | |
5d7836c4 JS |
2258 | } |
2259 | } | |
2260 | ||
2261 | /// Get the caret height and position for the given character position | |
2262 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect) | |
2263 | { | |
2264 | wxClientDC dc(this); | |
2265 | dc.SetFont(GetFont()); | |
7fe8059f | 2266 | |
5d7836c4 JS |
2267 | PrepareDC(dc); |
2268 | ||
2269 | wxPoint pt; | |
2270 | int height = 0; | |
2271 | ||
2272 | if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart)) | |
2273 | { | |
2274 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); | |
2275 | return true; | |
2276 | } | |
41e155b4 WS |
2277 | |
2278 | return false; | |
5d7836c4 JS |
2279 | } |
2280 | ||
2281 | /// Gets the line for the visible caret position. If the caret is | |
2282 | /// shown at the very end of the line, it means the next character is actually | |
2283 | /// on the following line. So let's get the line we're expecting to find | |
2284 | /// if this is the case. | |
2285 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const | |
2286 | { | |
2287 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true); | |
2288 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true); | |
2289 | if (line) | |
2290 | { | |
1e967276 JS |
2291 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2292 | if (caretPosition == lineRange.GetStart()-1 && | |
2293 | (para->GetRange().GetStart() != lineRange.GetStart())) | |
5d7836c4 JS |
2294 | { |
2295 | if (!m_caretAtLineStart) | |
2296 | line = GetBuffer().GetLineAtPosition(caretPosition-1, true); | |
2297 | } | |
2298 | } | |
2299 | return line; | |
2300 | } | |
2301 | ||
2302 | ||
2303 | /// Move the caret to the given character position | |
2304 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) | |
2305 | { | |
2306 | if (GetBuffer().GetDirty()) | |
2f36e8dc | 2307 | LayoutContent(); |
5d7836c4 JS |
2308 | |
2309 | if (pos <= GetBuffer().GetRange().GetEnd()) | |
2310 | { | |
2311 | SetCaretPosition(pos, showAtLineStart); | |
2312 | ||
2313 | PositionCaret(); | |
2314 | ||
2315 | return true; | |
2316 | } | |
2317 | else | |
2318 | return false; | |
2319 | } | |
2320 | ||
2321 | /// Layout the buffer: which we must do before certain operations, such as | |
2322 | /// setting the caret position. | |
2f36e8dc | 2323 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) |
5d7836c4 | 2324 | { |
4d551ad5 | 2325 | if (GetBuffer().GetDirty() || onlyVisibleRect) |
1e967276 JS |
2326 | { |
2327 | wxRect availableSpace(GetClientSize()); | |
2328 | if (availableSpace.width == 0) | |
2329 | availableSpace.width = 10; | |
2330 | if (availableSpace.height == 0) | |
2331 | availableSpace.height = 10; | |
4d551ad5 JS |
2332 | |
2333 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; | |
2334 | if (onlyVisibleRect) | |
2335 | { | |
2336 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; | |
2337 | availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); | |
2338 | } | |
41e155b4 | 2339 | |
1e967276 JS |
2340 | wxClientDC dc(this); |
2341 | dc.SetFont(GetFont()); | |
41e155b4 | 2342 | |
1e967276 | 2343 | PrepareDC(dc); |
41e155b4 | 2344 | |
1e967276 JS |
2345 | GetBuffer().Defragment(); |
2346 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation | |
4d551ad5 | 2347 | GetBuffer().Layout(dc, availableSpace, flags); |
1e967276 | 2348 | GetBuffer().SetDirty(false); |
41e155b4 | 2349 | |
1e967276 JS |
2350 | if (!IsFrozen()) |
2351 | SetupScrollbars(); | |
2352 | } | |
5d7836c4 JS |
2353 | |
2354 | return true; | |
2355 | } | |
2356 | ||
2357 | /// Is all of the selection bold? | |
2358 | bool wxRichTextCtrl::IsSelectionBold() const | |
2359 | { | |
2360 | if (HasSelection()) | |
2361 | { | |
2362 | wxRichTextAttr attr; | |
2363 | wxRichTextRange range = GetSelectionRange(); | |
2364 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2365 | attr.SetFontWeight(wxBOLD); | |
7fe8059f | 2366 | |
5d7836c4 JS |
2367 | return HasCharacterAttributes(range, attr); |
2368 | } | |
2369 | else | |
2370 | { | |
2371 | // If no selection, then we need to combine current style with default style | |
2372 | // to see what the effect would be if we started typing. | |
2373 | wxRichTextAttr attr; | |
2374 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2375 | if (GetStyle(GetCaretPosition()+1, attr)) | |
2376 | { | |
2377 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2378 | return attr.GetFontWeight() == wxBOLD; | |
2379 | } | |
2380 | } | |
2381 | return false; | |
2382 | } | |
2383 | ||
2384 | /// Is all of the selection italics? | |
2385 | bool wxRichTextCtrl::IsSelectionItalics() const | |
2386 | { | |
2387 | if (HasSelection()) | |
2388 | { | |
2389 | wxRichTextRange range = GetSelectionRange(); | |
2390 | wxRichTextAttr attr; | |
2391 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2392 | attr.SetFontStyle(wxITALIC); | |
7fe8059f | 2393 | |
5d7836c4 JS |
2394 | return HasCharacterAttributes(range, attr); |
2395 | } | |
2396 | else | |
2397 | { | |
2398 | // If no selection, then we need to combine current style with default style | |
2399 | // to see what the effect would be if we started typing. | |
2400 | wxRichTextAttr attr; | |
2401 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2402 | if (GetStyle(GetCaretPosition()+1, attr)) | |
2403 | { | |
2404 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2405 | return attr.GetFontStyle() == wxITALIC; | |
2406 | } | |
2407 | } | |
2408 | return false; | |
2409 | } | |
2410 | ||
2411 | /// Is all of the selection underlined? | |
2412 | bool wxRichTextCtrl::IsSelectionUnderlined() const | |
2413 | { | |
2414 | if (HasSelection()) | |
2415 | { | |
2416 | wxRichTextRange range = GetSelectionRange(); | |
2417 | wxRichTextAttr attr; | |
2418 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2419 | attr.SetFontUnderlined(true); | |
7fe8059f | 2420 | |
5d7836c4 JS |
2421 | return HasCharacterAttributes(range, attr); |
2422 | } | |
2423 | else | |
2424 | { | |
2425 | // If no selection, then we need to combine current style with default style | |
2426 | // to see what the effect would be if we started typing. | |
2427 | wxRichTextAttr attr; | |
2428 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2429 | if (GetStyle(GetCaretPosition()+1, attr)) | |
2430 | { | |
2431 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2432 | return attr.GetFontUnderlined(); | |
2433 | } | |
2434 | } | |
2435 | return false; | |
2436 | } | |
2437 | ||
2438 | /// Apply bold to the selection | |
2439 | bool wxRichTextCtrl::ApplyBoldToSelection() | |
2440 | { | |
2441 | wxRichTextAttr attr; | |
2442 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2443 | attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD); | |
2444 | ||
2445 | if (HasSelection()) | |
2446 | return SetStyle(GetSelectionRange(), attr); | |
2447 | else | |
2448 | SetDefaultStyle(attr); | |
2449 | return true; | |
2450 | } | |
2451 | ||
2452 | /// Apply italic to the selection | |
2453 | bool wxRichTextCtrl::ApplyItalicToSelection() | |
2454 | { | |
2455 | wxRichTextAttr attr; | |
2456 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2457 | attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC); | |
2458 | ||
2459 | if (HasSelection()) | |
2460 | return SetStyle(GetSelectionRange(), attr); | |
2461 | else | |
2462 | SetDefaultStyle(attr); | |
2463 | return true; | |
2464 | } | |
2465 | ||
2466 | /// Apply underline to the selection | |
2467 | bool wxRichTextCtrl::ApplyUnderlineToSelection() | |
2468 | { | |
2469 | wxRichTextAttr attr; | |
2470 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
4d551ad5 | 2471 | attr.SetFontUnderlined(!IsSelectionUnderlined()); |
5d7836c4 JS |
2472 | |
2473 | if (HasSelection()) | |
2474 | return SetStyle(GetSelectionRange(), attr); | |
2475 | else | |
2476 | SetDefaultStyle(attr); | |
2477 | return true; | |
2478 | } | |
2479 | ||
2480 | /// Is all of the selection aligned according to the specified flag? | |
2481 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) const | |
2482 | { | |
2483 | if (HasSelection()) | |
2484 | { | |
2485 | wxRichTextRange range = GetSelectionRange(); | |
2486 | wxRichTextAttr attr; | |
2487 | attr.SetAlignment(alignment); | |
7fe8059f | 2488 | |
5d7836c4 JS |
2489 | return HasParagraphAttributes(range, attr); |
2490 | } | |
2491 | else | |
2492 | { | |
2493 | // If no selection, then we need to get information from the current paragraph. | |
2494 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); | |
2495 | if (para) | |
2496 | return para->GetAttributes().GetAlignment() == alignment; | |
2497 | } | |
2498 | return false; | |
2499 | } | |
2500 | ||
2501 | /// Apply alignment to the selection | |
2502 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) | |
2503 | { | |
2504 | wxRichTextAttr attr; | |
2505 | attr.SetAlignment(alignment); | |
2506 | if (HasSelection()) | |
2507 | return SetStyle(GetSelectionRange(), attr); | |
2508 | else | |
2509 | { | |
2510 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); | |
2511 | if (para) | |
2512 | return SetStyle(para->GetRange(), attr); | |
2513 | } | |
2514 | return true; | |
2515 | } | |
2516 | ||
2517 | /// Sets the default style to the style under the cursor | |
2518 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() | |
2519 | { | |
2520 | wxTextAttrEx attr; | |
2521 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); | |
2522 | ||
2523 | if (GetStyle(GetCaretPosition(), attr)) | |
2524 | { | |
2525 | SetDefaultStyle(attr); | |
2526 | return true; | |
2527 | } | |
41e155b4 WS |
2528 | |
2529 | return false; | |
5d7836c4 JS |
2530 | } |
2531 | ||
4d551ad5 JS |
2532 | /// Returns the first visible position in the current view |
2533 | long wxRichTextCtrl::GetFirstVisiblePosition() const | |
2534 | { | |
2535 | wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y); | |
2536 | if (line) | |
2537 | return line->GetAbsoluteRange().GetStart(); | |
2538 | else | |
2539 | return 0; | |
2540 | } | |
2541 | ||
5d7836c4 JS |
2542 | #endif |
2543 | // wxUSE_RICHTEXT |