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