]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/richtext/richeditctrl.cpp | |
3 | // Purpose: A rich edit control | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 2005-09-30 | |
7 | // RCS-ID: $Id$ | |
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 | #if wxUSE_RICHTEXT | |
20 | ||
21 | #include "wx/richtext/richtextctrl.h" | |
22 | #include "wx/richtext/richtextstyles.h" | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #include "wx/settings.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/textfile.h" | |
30 | #include "wx/ffile.h" | |
31 | #include "wx/filename.h" | |
32 | #include "wx/dcbuffer.h" | |
33 | #include "wx/arrimpl.cpp" | |
34 | #include "wx/fontenum.h" | |
35 | ||
36 | // DLL options compatibility check: | |
37 | #include "wx/app.h" | |
38 | WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") | |
39 | ||
40 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK) | |
41 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK) | |
42 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK) | |
43 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK) | |
44 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_RETURN) | |
45 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CHARACTER) | |
46 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_DELETE) | |
47 | ||
48 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING) | |
49 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED) | |
50 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING) | |
51 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED) | |
52 | ||
53 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED) | |
54 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED) | |
55 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED) | |
56 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED) | |
57 | ||
58 | IMPLEMENT_CLASS( wxRichTextCtrl, wxControl ) | |
59 | ||
60 | IMPLEMENT_CLASS( wxRichTextEvent, wxNotifyEvent ) | |
61 | ||
62 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl ) | |
63 | EVT_PAINT(wxRichTextCtrl::OnPaint) | |
64 | EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) | |
65 | EVT_IDLE(wxRichTextCtrl::OnIdle) | |
66 | EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) | |
67 | EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) | |
68 | EVT_MOTION(wxRichTextCtrl::OnMoveMouse) | |
69 | EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) | |
70 | EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick) | |
71 | EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick) | |
72 | EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick) | |
73 | EVT_CHAR(wxRichTextCtrl::OnChar) | |
74 | EVT_SIZE(wxRichTextCtrl::OnSize) | |
75 | EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus) | |
76 | EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus) | |
77 | EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost) | |
78 | EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu) | |
79 | ||
80 | EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo) | |
81 | EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo) | |
82 | ||
83 | EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo) | |
84 | EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo) | |
85 | ||
86 | EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy) | |
87 | EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy) | |
88 | ||
89 | EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste) | |
90 | EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste) | |
91 | ||
92 | EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut) | |
93 | EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut) | |
94 | ||
95 | EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear) | |
96 | EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear) | |
97 | ||
98 | EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll) | |
99 | EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll) | |
100 | END_EVENT_TABLE() | |
101 | ||
102 | /*! | |
103 | * wxRichTextCtrl | |
104 | */ | |
105 | ||
106 | wxArrayString wxRichTextCtrl::sm_availableFontNames; | |
107 | ||
108 | wxRichTextCtrl::wxRichTextCtrl() | |
109 | : wxScrollHelper(this) | |
110 | { | |
111 | Init(); | |
112 | } | |
113 | ||
114 | wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent, | |
115 | wxWindowID id, | |
116 | const wxString& value, | |
117 | const wxPoint& pos, | |
118 | const wxSize& size, | |
119 | long style, | |
120 | const wxValidator& validator, | |
121 | const wxString& name) | |
122 | : wxScrollHelper(this) | |
123 | { | |
124 | Init(); | |
125 | Create(parent, id, value, pos, size, style, validator, name); | |
126 | } | |
127 | ||
128 | /// Creation | |
129 | bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, | |
130 | const wxValidator& validator, const wxString& name) | |
131 | { | |
132 | if (!wxTextCtrlBase::Create(parent, id, pos, size, | |
133 | style|wxFULL_REPAINT_ON_RESIZE, | |
134 | validator, name)) | |
135 | return false; | |
136 | ||
137 | if (!GetFont().Ok()) | |
138 | { | |
139 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
140 | } | |
141 | ||
142 | GetBuffer().Reset(); | |
143 | GetBuffer().SetRichTextCtrl(this); | |
144 | ||
145 | SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16)); | |
146 | GetCaret()->Show(); | |
147 | ||
148 | if (style & wxTE_READONLY) | |
149 | SetEditable(false); | |
150 | ||
151 | // The base attributes must all have default values | |
152 | wxTextAttrEx attributes; | |
153 | attributes.SetFont(GetFont()); | |
154 | attributes.SetTextColour(*wxBLACK); | |
155 | attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT); | |
156 | attributes.SetLineSpacing(10); | |
157 | attributes.SetParagraphSpacingAfter(10); | |
158 | attributes.SetParagraphSpacingBefore(0); | |
159 | attributes.SetTextEffects(0); | |
160 | attributes.SetTextEffectFlags(wxTEXT_ATTR_EFFECT_STRIKETHROUGH|wxTEXT_ATTR_EFFECT_CAPITALS); | |
161 | ||
162 | SetBasicStyle(attributes); | |
163 | ||
164 | // The default attributes will be merged with base attributes, so | |
165 | // can be empty to begin with | |
166 | wxTextAttrEx defaultAttributes; | |
167 | SetDefaultStyle(defaultAttributes); | |
168 | ||
169 | SetBackgroundColour(*wxWHITE); | |
170 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
171 | ||
172 | // Tell the sizers to use the given or best size | |
173 | SetInitialSize(size); | |
174 | ||
175 | #if wxRICHTEXT_BUFFERED_PAINTING | |
176 | // Create a buffer | |
177 | RecreateBuffer(size); | |
178 | #endif | |
179 | ||
180 | m_textCursor = wxCursor(wxCURSOR_IBEAM); | |
181 | m_urlCursor = wxCursor(wxCURSOR_HAND); | |
182 | ||
183 | SetCursor(m_textCursor); | |
184 | ||
185 | if (!value.IsEmpty()) | |
186 | SetValue(value); | |
187 | ||
188 | GetBuffer().AddEventHandler(this); | |
189 | ||
190 | return true; | |
191 | } | |
192 | ||
193 | wxRichTextCtrl::~wxRichTextCtrl() | |
194 | { | |
195 | GetBuffer().RemoveEventHandler(this); | |
196 | ||
197 | delete m_contextMenu; | |
198 | } | |
199 | ||
200 | /// Member initialisation | |
201 | void wxRichTextCtrl::Init() | |
202 | { | |
203 | m_freezeCount = 0; | |
204 | m_contextMenu = NULL; | |
205 | m_caret = NULL; | |
206 | m_caretPosition = -1; | |
207 | m_selectionRange.SetRange(-2, -2); | |
208 | m_selectionAnchor = -2; | |
209 | m_editable = true; | |
210 | m_caretAtLineStart = false; | |
211 | m_dragging = false; | |
212 | m_fullLayoutRequired = false; | |
213 | m_fullLayoutTime = 0; | |
214 | m_fullLayoutSavedPosition = 0; | |
215 | m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD; | |
216 | m_caretPositionForDefaultStyle = -2; | |
217 | } | |
218 | ||
219 | /// Call Freeze to prevent refresh | |
220 | void wxRichTextCtrl::Freeze() | |
221 | { | |
222 | m_freezeCount ++; | |
223 | } | |
224 | ||
225 | /// Call Thaw to refresh | |
226 | void wxRichTextCtrl::Thaw() | |
227 | { | |
228 | m_freezeCount --; | |
229 | ||
230 | if (m_freezeCount == 0) | |
231 | { | |
232 | SetupScrollbars(); | |
233 | Refresh(false); | |
234 | } | |
235 | } | |
236 | ||
237 | /// Clear all text | |
238 | void wxRichTextCtrl::Clear() | |
239 | { | |
240 | m_buffer.ResetAndClearCommands(); | |
241 | m_buffer.SetDirty(true); | |
242 | m_caretPosition = -1; | |
243 | m_caretPositionForDefaultStyle = -2; | |
244 | m_caretAtLineStart = false; | |
245 | m_selectionRange.SetRange(-2, -2); | |
246 | ||
247 | SetScrollbars(0, 0, 0, 0, 0, 0); | |
248 | ||
249 | if (m_freezeCount == 0) | |
250 | { | |
251 | SetupScrollbars(); | |
252 | Refresh(false); | |
253 | } | |
254 | SendTextUpdatedEvent(); | |
255 | } | |
256 | ||
257 | /// Painting | |
258 | void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
259 | { | |
260 | if (GetCaret() && GetCaret()->IsVisible()) | |
261 | GetCaret()->Hide(); | |
262 | ||
263 | { | |
264 | #if wxRICHTEXT_BUFFERED_PAINTING | |
265 | wxBufferedPaintDC dc(this, m_bufferBitmap); | |
266 | #else | |
267 | wxPaintDC dc(this); | |
268 | #endif | |
269 | PrepareDC(dc); | |
270 | ||
271 | if (m_freezeCount > 0) | |
272 | return; | |
273 | ||
274 | dc.SetFont(GetFont()); | |
275 | ||
276 | // Paint the background | |
277 | PaintBackground(dc); | |
278 | ||
279 | // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); | |
280 | ||
281 | wxRect drawingArea(GetUpdateRegion().GetBox()); | |
282 | drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition())); | |
283 | ||
284 | wxRect availableSpace(GetClientSize()); | |
285 | if (GetBuffer().GetDirty()) | |
286 | { | |
287 | GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); | |
288 | GetBuffer().SetDirty(false); | |
289 | SetupScrollbars(); | |
290 | } | |
291 | ||
292 | GetBuffer().Draw(dc, GetBuffer().GetRange(), GetInternalSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); | |
293 | } | |
294 | ||
295 | if (GetCaret() && !GetCaret()->IsVisible()) | |
296 | GetCaret()->Show(); | |
297 | ||
298 | PositionCaret(); | |
299 | } | |
300 | ||
301 | // Empty implementation, to prevent flicker | |
302 | void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
303 | { | |
304 | } | |
305 | ||
306 | void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) | |
307 | { | |
308 | if (GetCaret()) | |
309 | { | |
310 | if (!GetCaret()->IsVisible()) | |
311 | GetCaret()->Show(); | |
312 | PositionCaret(); | |
313 | } | |
314 | ||
315 | // if (!IsFrozen()) | |
316 | // Refresh(false); | |
317 | } | |
318 | ||
319 | void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) | |
320 | { | |
321 | if (GetCaret() && GetCaret()->IsVisible()) | |
322 | GetCaret()->Hide(); | |
323 | ||
324 | // if (!IsFrozen()) | |
325 | // Refresh(false); | |
326 | } | |
327 | ||
328 | void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) | |
329 | { | |
330 | m_dragging = false; | |
331 | } | |
332 | ||
333 | /// Left-click | |
334 | void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event) | |
335 | { | |
336 | SetFocus(); | |
337 | ||
338 | wxClientDC dc(this); | |
339 | PrepareDC(dc); | |
340 | dc.SetFont(GetFont()); | |
341 | ||
342 | long position = 0; | |
343 | int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position); | |
344 | ||
345 | if (hit != wxRICHTEXT_HITTEST_NONE) | |
346 | { | |
347 | m_dragStart = event.GetLogicalPosition(dc); | |
348 | m_dragging = true; | |
349 | CaptureMouse(); | |
350 | ||
351 | SelectNone(); | |
352 | ||
353 | bool caretAtLineStart = false; | |
354 | ||
355 | if (hit & wxRICHTEXT_HITTEST_BEFORE) | |
356 | { | |
357 | // If we're at the start of a line (but not first in para) | |
358 | // then we should keep the caret showing at the start of the line | |
359 | // by showing the m_caretAtLineStart flag. | |
360 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); | |
361 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); | |
362 | ||
363 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) | |
364 | caretAtLineStart = true; | |
365 | position --; | |
366 | } | |
367 | ||
368 | MoveCaret(position, caretAtLineStart); | |
369 | SetDefaultStyleToCursorStyle(); | |
370 | } | |
371 | ||
372 | event.Skip(); | |
373 | } | |
374 | ||
375 | /// Left-up | |
376 | void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event) | |
377 | { | |
378 | if (m_dragging) | |
379 | { | |
380 | m_dragging = false; | |
381 | if (GetCapture() == this) | |
382 | ReleaseMouse(); | |
383 | ||
384 | // See if we clicked on a URL | |
385 | wxClientDC dc(this); | |
386 | PrepareDC(dc); | |
387 | dc.SetFont(GetFont()); | |
388 | ||
389 | long position = 0; | |
390 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
391 | int hit = GetBuffer().HitTest(dc, logicalPt, position); | |
392 | ||
393 | if (hit != wxRICHTEXT_HITTEST_NONE) | |
394 | { | |
395 | wxTextAttrEx attr; | |
396 | if (GetStyle(position, attr)) | |
397 | { | |
398 | if (attr.HasFlag(wxTEXT_ATTR_URL)) | |
399 | { | |
400 | wxString urlTarget = attr.GetURL(); | |
401 | if (!urlTarget.IsEmpty()) | |
402 | { | |
403 | wxMouseEvent mouseEvent(event); | |
404 | ||
405 | long startPos = 0, endPos = 0; | |
406 | wxRichTextObject* obj = GetBuffer().GetLeafObjectAtPosition(position); | |
407 | if (obj) | |
408 | { | |
409 | startPos = obj->GetRange().GetStart(); | |
410 | endPos = obj->GetRange().GetEnd(); | |
411 | } | |
412 | ||
413 | wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos); | |
414 | InitCommandEvent(urlEvent); | |
415 | ||
416 | urlEvent.SetString(urlTarget); | |
417 | ||
418 | GetEventHandler()->ProcessEvent(urlEvent); | |
419 | } | |
420 | } | |
421 | } | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | /// Left-click | |
427 | void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) | |
428 | { | |
429 | wxClientDC dc(this); | |
430 | PrepareDC(dc); | |
431 | dc.SetFont(GetFont()); | |
432 | ||
433 | long position = 0; | |
434 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
435 | int hit = GetBuffer().HitTest(dc, logicalPt, position); | |
436 | ||
437 | // See if we need to change the cursor | |
438 | ||
439 | { | |
440 | if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE)) | |
441 | { | |
442 | wxTextAttrEx attr; | |
443 | if (GetStyle(position, attr)) | |
444 | { | |
445 | if (attr.HasFlag(wxTEXT_ATTR_URL)) | |
446 | { | |
447 | SetCursor(m_urlCursor); | |
448 | } | |
449 | else if (!attr.HasFlag(wxTEXT_ATTR_URL)) | |
450 | { | |
451 | SetCursor(m_textCursor); | |
452 | } | |
453 | } | |
454 | } | |
455 | else | |
456 | SetCursor(m_textCursor); | |
457 | } | |
458 | ||
459 | if (!event.Dragging()) | |
460 | { | |
461 | event.Skip(); | |
462 | return; | |
463 | } | |
464 | ||
465 | if (m_dragging && hit != wxRICHTEXT_HITTEST_NONE) | |
466 | { | |
467 | // TODO: test closeness | |
468 | ||
469 | bool caretAtLineStart = false; | |
470 | ||
471 | if (hit & wxRICHTEXT_HITTEST_BEFORE) | |
472 | { | |
473 | // If we're at the start of a line (but not first in para) | |
474 | // then we should keep the caret showing at the start of the line | |
475 | // by showing the m_caretAtLineStart flag. | |
476 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); | |
477 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(position); | |
478 | ||
479 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) | |
480 | caretAtLineStart = true; | |
481 | position --; | |
482 | } | |
483 | ||
484 | if (m_caretPosition != position) | |
485 | { | |
486 | bool extendSel = ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN); | |
487 | ||
488 | MoveCaret(position, caretAtLineStart); | |
489 | SetDefaultStyleToCursorStyle(); | |
490 | ||
491 | if (extendSel) | |
492 | Refresh(false); | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
497 | /// Right-click | |
498 | void wxRichTextCtrl::OnRightClick(wxMouseEvent& event) | |
499 | { | |
500 | SetFocus(); | |
501 | event.Skip(); | |
502 | } | |
503 | ||
504 | /// Left-double-click | |
505 | void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& event) | |
506 | { | |
507 | SelectWord(GetCaretPosition()+1); | |
508 | event.Skip(); | |
509 | } | |
510 | ||
511 | /// Middle-click | |
512 | void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event) | |
513 | { | |
514 | event.Skip(); | |
515 | } | |
516 | ||
517 | /// Key press | |
518 | void wxRichTextCtrl::OnChar(wxKeyEvent& event) | |
519 | { | |
520 | int flags = 0; | |
521 | if (event.CmdDown()) | |
522 | flags |= wxRICHTEXT_CTRL_DOWN; | |
523 | if (event.ShiftDown()) | |
524 | flags |= wxRICHTEXT_SHIFT_DOWN; | |
525 | if (event.AltDown()) | |
526 | flags |= wxRICHTEXT_ALT_DOWN; | |
527 | ||
528 | if (event.GetKeyCode() == WXK_LEFT || | |
529 | event.GetKeyCode() == WXK_RIGHT || | |
530 | event.GetKeyCode() == WXK_UP || | |
531 | event.GetKeyCode() == WXK_DOWN || | |
532 | event.GetKeyCode() == WXK_HOME || | |
533 | event.GetKeyCode() == WXK_PAGEUP || | |
534 | event.GetKeyCode() == WXK_PAGEDOWN || | |
535 | event.GetKeyCode() == WXK_END || | |
536 | ||
537 | event.GetKeyCode() == WXK_NUMPAD_LEFT || | |
538 | event.GetKeyCode() == WXK_NUMPAD_RIGHT || | |
539 | event.GetKeyCode() == WXK_NUMPAD_UP || | |
540 | event.GetKeyCode() == WXK_NUMPAD_DOWN || | |
541 | event.GetKeyCode() == WXK_NUMPAD_HOME || | |
542 | event.GetKeyCode() == WXK_NUMPAD_PAGEUP || | |
543 | event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN || | |
544 | event.GetKeyCode() == WXK_NUMPAD_END) | |
545 | { | |
546 | KeyboardNavigate(event.GetKeyCode(), flags); | |
547 | return; | |
548 | } | |
549 | ||
550 | // all the other keys modify the controls contents which shouldn't be | |
551 | // possible if we're read-only | |
552 | if ( !IsEditable() ) | |
553 | { | |
554 | event.Skip(); | |
555 | return; | |
556 | } | |
557 | ||
558 | if (event.GetKeyCode() == WXK_RETURN) | |
559 | { | |
560 | BeginBatchUndo(_("Insert Text")); | |
561 | ||
562 | long newPos = m_caretPosition; | |
563 | ||
564 | DeleteSelectedContent(& newPos); | |
565 | ||
566 | if (event.ShiftDown()) | |
567 | { | |
568 | wxString text; | |
569 | text = wxRichTextLineBreakChar; | |
570 | GetBuffer().InsertTextWithUndo(newPos+1, text, this); | |
571 | } | |
572 | else | |
573 | GetBuffer().InsertNewlineWithUndo(newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); | |
574 | ||
575 | EndBatchUndo(); | |
576 | SetDefaultStyleToCursorStyle(); | |
577 | ||
578 | ScrollIntoView(m_caretPosition, WXK_RIGHT); | |
579 | ||
580 | wxRichTextEvent cmdEvent( | |
581 | wxEVT_COMMAND_RICHTEXT_RETURN, | |
582 | GetId()); | |
583 | cmdEvent.SetEventObject(this); | |
584 | cmdEvent.SetFlags(flags); | |
585 | cmdEvent.SetPosition(newPos+1); | |
586 | ||
587 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) | |
588 | { | |
589 | // Generate conventional event | |
590 | wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
591 | InitCommandEvent(textEvent); | |
592 | ||
593 | GetEventHandler()->ProcessEvent(textEvent); | |
594 | } | |
595 | Update(); | |
596 | } | |
597 | else if (event.GetKeyCode() == WXK_BACK) | |
598 | { | |
599 | BeginBatchUndo(_("Delete Text")); | |
600 | ||
601 | // Submit range in character positions, which are greater than caret positions, | |
602 | // so subtract 1 for deleted character and add 1 for conversion to character position. | |
603 | if (m_caretPosition > -1 && !HasSelection()) | |
604 | { | |
605 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition, m_caretPosition), this); | |
606 | } | |
607 | else | |
608 | DeleteSelectedContent(); | |
609 | ||
610 | EndBatchUndo(); | |
611 | ||
612 | if (GetLastPosition() == -1) | |
613 | { | |
614 | GetBuffer().Reset(); | |
615 | ||
616 | m_caretPosition = -1; | |
617 | PositionCaret(); | |
618 | SetDefaultStyleToCursorStyle(); | |
619 | } | |
620 | ||
621 | ScrollIntoView(m_caretPosition, WXK_LEFT); | |
622 | ||
623 | wxRichTextEvent cmdEvent( | |
624 | wxEVT_COMMAND_RICHTEXT_DELETE, | |
625 | GetId()); | |
626 | cmdEvent.SetEventObject(this); | |
627 | cmdEvent.SetFlags(flags); | |
628 | cmdEvent.SetPosition(m_caretPosition+1); | |
629 | GetEventHandler()->ProcessEvent(cmdEvent); | |
630 | ||
631 | Update(); | |
632 | } | |
633 | else if (event.GetKeyCode() == WXK_DELETE) | |
634 | { | |
635 | BeginBatchUndo(_("Delete Text")); | |
636 | ||
637 | // Submit range in character positions, which are greater than caret positions, | |
638 | if (m_caretPosition < GetBuffer().GetRange().GetEnd()+1 && !HasSelection()) | |
639 | { | |
640 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(m_caretPosition+1, m_caretPosition+1), this); | |
641 | } | |
642 | else | |
643 | DeleteSelectedContent(); | |
644 | ||
645 | EndBatchUndo(); | |
646 | ||
647 | if (GetLastPosition() == -1) | |
648 | { | |
649 | GetBuffer().Reset(); | |
650 | ||
651 | m_caretPosition = -1; | |
652 | PositionCaret(); | |
653 | SetDefaultStyleToCursorStyle(); | |
654 | } | |
655 | ||
656 | wxRichTextEvent cmdEvent( | |
657 | wxEVT_COMMAND_RICHTEXT_DELETE, | |
658 | GetId()); | |
659 | cmdEvent.SetEventObject(this); | |
660 | cmdEvent.SetFlags(flags); | |
661 | cmdEvent.SetPosition(m_caretPosition+1); | |
662 | GetEventHandler()->ProcessEvent(cmdEvent); | |
663 | ||
664 | Update(); | |
665 | } | |
666 | else | |
667 | { | |
668 | long keycode = event.GetKeyCode(); | |
669 | switch ( keycode ) | |
670 | { | |
671 | case WXK_ESCAPE: | |
672 | case WXK_DELETE: | |
673 | case WXK_START: | |
674 | case WXK_LBUTTON: | |
675 | case WXK_RBUTTON: | |
676 | case WXK_CANCEL: | |
677 | case WXK_MBUTTON: | |
678 | case WXK_CLEAR: | |
679 | case WXK_SHIFT: | |
680 | case WXK_ALT: | |
681 | case WXK_CONTROL: | |
682 | case WXK_MENU: | |
683 | case WXK_PAUSE: | |
684 | case WXK_CAPITAL: | |
685 | case WXK_END: | |
686 | case WXK_HOME: | |
687 | case WXK_LEFT: | |
688 | case WXK_UP: | |
689 | case WXK_RIGHT: | |
690 | case WXK_DOWN: | |
691 | case WXK_SELECT: | |
692 | case WXK_PRINT: | |
693 | case WXK_EXECUTE: | |
694 | case WXK_SNAPSHOT: | |
695 | case WXK_INSERT: | |
696 | case WXK_HELP: | |
697 | case WXK_NUMPAD0: | |
698 | case WXK_NUMPAD1: | |
699 | case WXK_NUMPAD2: | |
700 | case WXK_NUMPAD3: | |
701 | case WXK_NUMPAD4: | |
702 | case WXK_NUMPAD5: | |
703 | case WXK_NUMPAD6: | |
704 | case WXK_NUMPAD7: | |
705 | case WXK_NUMPAD8: | |
706 | case WXK_NUMPAD9: | |
707 | case WXK_MULTIPLY: | |
708 | case WXK_ADD: | |
709 | case WXK_SEPARATOR: | |
710 | case WXK_SUBTRACT: | |
711 | case WXK_DECIMAL: | |
712 | case WXK_DIVIDE: | |
713 | case WXK_F1: | |
714 | case WXK_F2: | |
715 | case WXK_F3: | |
716 | case WXK_F4: | |
717 | case WXK_F5: | |
718 | case WXK_F6: | |
719 | case WXK_F7: | |
720 | case WXK_F8: | |
721 | case WXK_F9: | |
722 | case WXK_F10: | |
723 | case WXK_F11: | |
724 | case WXK_F12: | |
725 | case WXK_F13: | |
726 | case WXK_F14: | |
727 | case WXK_F15: | |
728 | case WXK_F16: | |
729 | case WXK_F17: | |
730 | case WXK_F18: | |
731 | case WXK_F19: | |
732 | case WXK_F20: | |
733 | case WXK_F21: | |
734 | case WXK_F22: | |
735 | case WXK_F23: | |
736 | case WXK_F24: | |
737 | case WXK_NUMLOCK: | |
738 | case WXK_SCROLL: | |
739 | case WXK_PAGEUP: | |
740 | case WXK_PAGEDOWN: | |
741 | case WXK_NUMPAD_SPACE: | |
742 | case WXK_NUMPAD_TAB: | |
743 | case WXK_NUMPAD_ENTER: | |
744 | case WXK_NUMPAD_F1: | |
745 | case WXK_NUMPAD_F2: | |
746 | case WXK_NUMPAD_F3: | |
747 | case WXK_NUMPAD_F4: | |
748 | case WXK_NUMPAD_HOME: | |
749 | case WXK_NUMPAD_LEFT: | |
750 | case WXK_NUMPAD_UP: | |
751 | case WXK_NUMPAD_RIGHT: | |
752 | case WXK_NUMPAD_DOWN: | |
753 | case WXK_NUMPAD_PAGEUP: | |
754 | case WXK_NUMPAD_PAGEDOWN: | |
755 | case WXK_NUMPAD_END: | |
756 | case WXK_NUMPAD_BEGIN: | |
757 | case WXK_NUMPAD_INSERT: | |
758 | case WXK_NUMPAD_DELETE: | |
759 | case WXK_NUMPAD_EQUAL: | |
760 | case WXK_NUMPAD_MULTIPLY: | |
761 | case WXK_NUMPAD_ADD: | |
762 | case WXK_NUMPAD_SEPARATOR: | |
763 | case WXK_NUMPAD_SUBTRACT: | |
764 | case WXK_NUMPAD_DECIMAL: | |
765 | case WXK_WINDOWS_LEFT: | |
766 | { | |
767 | event.Skip(); | |
768 | return; | |
769 | } | |
770 | ||
771 | default: | |
772 | { | |
773 | if (event.CmdDown() || event.AltDown()) | |
774 | { | |
775 | event.Skip(); | |
776 | return; | |
777 | } | |
778 | ||
779 | wxRichTextEvent cmdEvent( | |
780 | wxEVT_COMMAND_RICHTEXT_CHARACTER, | |
781 | GetId()); | |
782 | cmdEvent.SetEventObject(this); | |
783 | cmdEvent.SetFlags(flags); | |
784 | cmdEvent.SetCharacter((wxChar) keycode); | |
785 | cmdEvent.SetPosition(m_caretPosition+1); | |
786 | ||
787 | if (keycode == wxT('\t')) | |
788 | { | |
789 | // See if we need to promote or demote the selection or paragraph at the cursor | |
790 | // position, instead of inserting a tab. | |
791 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
792 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos); | |
793 | if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName()) | |
794 | { | |
795 | wxRichTextRange range; | |
796 | if (HasSelection()) | |
797 | range = GetSelectionRange(); | |
798 | else | |
799 | range = para->GetRange().FromInternal(); | |
800 | ||
801 | int promoteBy = event.ShiftDown() ? 1 : -1; | |
802 | ||
803 | PromoteList(promoteBy, range, NULL); | |
804 | ||
805 | GetEventHandler()->ProcessEvent(cmdEvent); | |
806 | ||
807 | return; | |
808 | } | |
809 | } | |
810 | ||
811 | BeginBatchUndo(_("Insert Text")); | |
812 | ||
813 | long newPos = m_caretPosition; | |
814 | DeleteSelectedContent(& newPos); | |
815 | ||
816 | wxString str = (wxChar) event.GetKeyCode(); | |
817 | GetBuffer().InsertTextWithUndo(newPos+1, str, this, 0); | |
818 | ||
819 | EndBatchUndo(); | |
820 | ||
821 | SetDefaultStyleToCursorStyle(); | |
822 | ScrollIntoView(m_caretPosition, WXK_RIGHT); | |
823 | ||
824 | GetEventHandler()->ProcessEvent(cmdEvent); | |
825 | ||
826 | Update(); | |
827 | } | |
828 | } | |
829 | } | |
830 | } | |
831 | ||
832 | /// Delete content if there is a selection, e.g. when pressing a key. | |
833 | bool wxRichTextCtrl::DeleteSelectedContent(long* newPos) | |
834 | { | |
835 | if (HasSelection()) | |
836 | { | |
837 | long pos = m_selectionRange.GetStart(); | |
838 | GetBuffer().DeleteRangeWithUndo(m_selectionRange, this); | |
839 | m_selectionRange.SetRange(-2, -2); | |
840 | ||
841 | if (newPos) | |
842 | *newPos = pos-1; | |
843 | return true; | |
844 | } | |
845 | else | |
846 | return false; | |
847 | } | |
848 | ||
849 | /// Keyboard navigation | |
850 | ||
851 | /* | |
852 | ||
853 | Left: left one character | |
854 | Right: right one character | |
855 | Up: up one line | |
856 | Down: down one line | |
857 | Ctrl-Left: left one word | |
858 | Ctrl-Right: right one word | |
859 | Ctrl-Up: previous paragraph start | |
860 | Ctrl-Down: next start of paragraph | |
861 | Home: start of line | |
862 | End: end of line | |
863 | Ctrl-Home: start of document | |
864 | Ctrl-End: end of document | |
865 | Page-Up: Up a screen | |
866 | Page-Down: Down a screen | |
867 | ||
868 | Maybe: | |
869 | ||
870 | Ctrl-Alt-PgUp: Start of window | |
871 | Ctrl-Alt-PgDn: End of window | |
872 | F8: Start selection mode | |
873 | Esc: End selection mode | |
874 | ||
875 | Adding Shift does the above but starts/extends selection. | |
876 | ||
877 | ||
878 | */ | |
879 | ||
880 | bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) | |
881 | { | |
882 | bool success = false; | |
883 | ||
884 | if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT) | |
885 | { | |
886 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
887 | success = WordRight(1, flags); | |
888 | else | |
889 | success = MoveRight(1, flags); | |
890 | } | |
891 | else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT) | |
892 | { | |
893 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
894 | success = WordLeft(1, flags); | |
895 | else | |
896 | success = MoveLeft(1, flags); | |
897 | } | |
898 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP) | |
899 | { | |
900 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
901 | success = MoveToParagraphStart(flags); | |
902 | else | |
903 | success = MoveUp(1, flags); | |
904 | } | |
905 | else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN) | |
906 | { | |
907 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
908 | success = MoveToParagraphEnd(flags); | |
909 | else | |
910 | success = MoveDown(1, flags); | |
911 | } | |
912 | else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP) | |
913 | { | |
914 | success = PageUp(1, flags); | |
915 | } | |
916 | else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) | |
917 | { | |
918 | success = PageDown(1, flags); | |
919 | } | |
920 | else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME) | |
921 | { | |
922 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
923 | success = MoveHome(flags); | |
924 | else | |
925 | success = MoveToLineStart(flags); | |
926 | } | |
927 | else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END) | |
928 | { | |
929 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
930 | success = MoveEnd(flags); | |
931 | else | |
932 | success = MoveToLineEnd(flags); | |
933 | } | |
934 | ||
935 | if (success) | |
936 | { | |
937 | ScrollIntoView(m_caretPosition, keyCode); | |
938 | SetDefaultStyleToCursorStyle(); | |
939 | } | |
940 | ||
941 | return success; | |
942 | } | |
943 | ||
944 | /// Extend the selection. Selections are in caret positions. | |
945 | bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags) | |
946 | { | |
947 | if (flags & wxRICHTEXT_SHIFT_DOWN) | |
948 | { | |
949 | // If not currently selecting, start selecting | |
950 | if (m_selectionRange.GetStart() == -2) | |
951 | { | |
952 | m_selectionAnchor = oldPos; | |
953 | ||
954 | if (oldPos > newPos) | |
955 | m_selectionRange.SetRange(newPos+1, oldPos); | |
956 | else | |
957 | m_selectionRange.SetRange(oldPos+1, newPos); | |
958 | } | |
959 | else | |
960 | { | |
961 | // Always ensure that the selection range start is greater than | |
962 | // the end. | |
963 | if (newPos > m_selectionAnchor) | |
964 | m_selectionRange.SetRange(m_selectionAnchor+1, newPos); | |
965 | else | |
966 | m_selectionRange.SetRange(newPos+1, m_selectionAnchor); | |
967 | } | |
968 | ||
969 | if (m_selectionRange.GetStart() > m_selectionRange.GetEnd()) | |
970 | { | |
971 | wxLogDebug(wxT("Strange selection range")); | |
972 | } | |
973 | ||
974 | return true; | |
975 | } | |
976 | else | |
977 | return false; | |
978 | } | |
979 | ||
980 | /// Scroll into view, returning true if we scrolled. | |
981 | /// This takes a _caret_ position. | |
982 | bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) | |
983 | { | |
984 | wxRichTextLine* line = GetVisibleLineForCaretPosition(position); | |
985 | ||
986 | if (!line) | |
987 | return false; | |
988 | ||
989 | int ppuX, ppuY; | |
990 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
991 | ||
992 | int startXUnits, startYUnits; | |
993 | GetViewStart(& startXUnits, & startYUnits); | |
994 | int startY = startYUnits * ppuY; | |
995 | ||
996 | int sx = 0, sy = 0; | |
997 | GetVirtualSize(& sx, & sy); | |
998 | int sxUnits = 0; | |
999 | int syUnits = 0; | |
1000 | if (ppuY != 0) | |
1001 | syUnits = sy/ppuY; | |
1002 | ||
1003 | wxRect rect = line->GetRect(); | |
1004 | ||
1005 | bool scrolled = false; | |
1006 | ||
1007 | wxSize clientSize = GetClientSize(); | |
1008 | ||
1009 | // Going down | |
1010 | if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN || | |
1011 | keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_DOWN || | |
1012 | keyCode == WXK_END || keyCode == WXK_NUMPAD_END || | |
1013 | keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) | |
1014 | { | |
1015 | if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1016 | { | |
1017 | // Make it scroll so this item is at the bottom | |
1018 | // of the window | |
1019 | int y = rect.y - (clientSize.y - rect.height); | |
1020 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
1021 | ||
1022 | // If we're still off the screen, scroll another line down | |
1023 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1024 | yUnits ++; | |
1025 | ||
1026 | if (startYUnits != yUnits) | |
1027 | { | |
1028 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1029 | scrolled = true; | |
1030 | } | |
1031 | } | |
1032 | else if (rect.y < startY) | |
1033 | { | |
1034 | // Make it scroll so this item is at the top | |
1035 | // of the window | |
1036 | int y = rect.y ; | |
1037 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
1038 | ||
1039 | if (startYUnits != yUnits) | |
1040 | { | |
1041 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1042 | scrolled = true; | |
1043 | } | |
1044 | } | |
1045 | } | |
1046 | // Going up | |
1047 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP || | |
1048 | keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT || | |
1049 | keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME || | |
1050 | keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP ) | |
1051 | { | |
1052 | if (rect.y < startY) | |
1053 | { | |
1054 | // Make it scroll so this item is at the top | |
1055 | // of the window | |
1056 | int y = rect.y ; | |
1057 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
1058 | ||
1059 | if (startYUnits != yUnits) | |
1060 | { | |
1061 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1062 | scrolled = true; | |
1063 | } | |
1064 | } | |
1065 | else if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1066 | { | |
1067 | // Make it scroll so this item is at the bottom | |
1068 | // of the window | |
1069 | int y = rect.y - (clientSize.y - rect.height); | |
1070 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
1071 | ||
1072 | // If we're still off the screen, scroll another line down | |
1073 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1074 | yUnits ++; | |
1075 | ||
1076 | if (startYUnits != yUnits) | |
1077 | { | |
1078 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1079 | scrolled = true; | |
1080 | } | |
1081 | } | |
1082 | } | |
1083 | PositionCaret(); | |
1084 | ||
1085 | return scrolled; | |
1086 | } | |
1087 | ||
1088 | /// Is the given position visible on the screen? | |
1089 | bool wxRichTextCtrl::IsPositionVisible(long pos) const | |
1090 | { | |
1091 | wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1); | |
1092 | ||
1093 | if (!line) | |
1094 | return false; | |
1095 | ||
1096 | int ppuX, ppuY; | |
1097 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
1098 | ||
1099 | int startX, startY; | |
1100 | GetViewStart(& startX, & startY); | |
1101 | startX = 0; | |
1102 | startY = startY * ppuY; | |
1103 | ||
1104 | int sx = 0, sy = 0; | |
1105 | GetVirtualSize(& sx, & sy); | |
1106 | sx = 0; | |
1107 | if (ppuY != 0) | |
1108 | sy = sy/ppuY; | |
1109 | ||
1110 | wxRect rect = line->GetRect(); | |
1111 | ||
1112 | wxSize clientSize = GetClientSize(); | |
1113 | ||
1114 | return !(((rect.y + rect.height) > (clientSize.y + startY)) || rect.y < startY); | |
1115 | } | |
1116 | ||
1117 | void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) | |
1118 | { | |
1119 | m_caretPosition = position; | |
1120 | m_caretAtLineStart = showAtLineStart; | |
1121 | } | |
1122 | ||
1123 | /// Move caret one visual step forward: this may mean setting a flag | |
1124 | /// and keeping the same position if we're going from the end of one line | |
1125 | /// to the start of the next, which may be the exact same caret position. | |
1126 | void wxRichTextCtrl::MoveCaretForward(long oldPosition) | |
1127 | { | |
1128 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); | |
1129 | ||
1130 | // Only do the check if we're not at the end of the paragraph (where things work OK | |
1131 | // anyway) | |
1132 | if (para && (oldPosition != para->GetRange().GetEnd() - 1)) | |
1133 | { | |
1134 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); | |
1135 | ||
1136 | if (line) | |
1137 | { | |
1138 | wxRichTextRange lineRange = line->GetAbsoluteRange(); | |
1139 | ||
1140 | // We're at the end of a line. See whether we need to | |
1141 | // stay at the same actual caret position but change visual | |
1142 | // position, or not. | |
1143 | if (oldPosition == lineRange.GetEnd()) | |
1144 | { | |
1145 | if (m_caretAtLineStart) | |
1146 | { | |
1147 | // We're already at the start of the line, so actually move on now. | |
1148 | m_caretPosition = oldPosition + 1; | |
1149 | m_caretAtLineStart = false; | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | // We're showing at the end of the line, so keep to | |
1154 | // the same position but indicate that we're to show | |
1155 | // at the start of the next line. | |
1156 | m_caretPosition = oldPosition; | |
1157 | m_caretAtLineStart = true; | |
1158 | } | |
1159 | SetDefaultStyleToCursorStyle(); | |
1160 | return; | |
1161 | } | |
1162 | } | |
1163 | } | |
1164 | m_caretPosition ++; | |
1165 | SetDefaultStyleToCursorStyle(); | |
1166 | } | |
1167 | ||
1168 | /// Move caret one visual step backward: this may mean setting a flag | |
1169 | /// and keeping the same position if we're going from the end of one line | |
1170 | /// to the start of the next, which may be the exact same caret position. | |
1171 | void wxRichTextCtrl::MoveCaretBack(long oldPosition) | |
1172 | { | |
1173 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(oldPosition); | |
1174 | ||
1175 | // Only do the check if we're not at the start of the paragraph (where things work OK | |
1176 | // anyway) | |
1177 | if (para && (oldPosition != para->GetRange().GetStart())) | |
1178 | { | |
1179 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(oldPosition); | |
1180 | ||
1181 | if (line) | |
1182 | { | |
1183 | wxRichTextRange lineRange = line->GetAbsoluteRange(); | |
1184 | ||
1185 | // We're at the start of a line. See whether we need to | |
1186 | // stay at the same actual caret position but change visual | |
1187 | // position, or not. | |
1188 | if (oldPosition == lineRange.GetStart()) | |
1189 | { | |
1190 | m_caretPosition = oldPosition-1; | |
1191 | m_caretAtLineStart = true; | |
1192 | return; | |
1193 | } | |
1194 | else if (oldPosition == lineRange.GetEnd()) | |
1195 | { | |
1196 | if (m_caretAtLineStart) | |
1197 | { | |
1198 | // We're at the start of the line, so keep the same caret position | |
1199 | // but clear the start-of-line flag. | |
1200 | m_caretPosition = oldPosition; | |
1201 | m_caretAtLineStart = false; | |
1202 | } | |
1203 | else | |
1204 | { | |
1205 | // We're showing at the end of the line, so go back | |
1206 | // to the previous character position. | |
1207 | m_caretPosition = oldPosition - 1; | |
1208 | } | |
1209 | SetDefaultStyleToCursorStyle(); | |
1210 | return; | |
1211 | } | |
1212 | } | |
1213 | } | |
1214 | m_caretPosition --; | |
1215 | SetDefaultStyleToCursorStyle(); | |
1216 | } | |
1217 | ||
1218 | /// Move right | |
1219 | bool wxRichTextCtrl::MoveRight(int noPositions, int flags) | |
1220 | { | |
1221 | long endPos = GetBuffer().GetRange().GetEnd(); | |
1222 | ||
1223 | if (m_caretPosition + noPositions < endPos) | |
1224 | { | |
1225 | long oldPos = m_caretPosition; | |
1226 | long newPos = m_caretPosition + noPositions; | |
1227 | ||
1228 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1229 | if (!extendSel) | |
1230 | SelectNone(); | |
1231 | ||
1232 | // Determine by looking at oldPos and m_caretPosition whether | |
1233 | // we moved from the end of a line to the start of the next line, in which case | |
1234 | // we want to adjust the caret position such that it is positioned at the | |
1235 | // start of the next line, rather than jumping past the first character of the | |
1236 | // line. | |
1237 | if (noPositions == 1 && !extendSel) | |
1238 | MoveCaretForward(oldPos); | |
1239 | else | |
1240 | SetCaretPosition(newPos); | |
1241 | ||
1242 | PositionCaret(); | |
1243 | SetDefaultStyleToCursorStyle(); | |
1244 | ||
1245 | if (extendSel) | |
1246 | Refresh(false); | |
1247 | return true; | |
1248 | } | |
1249 | else | |
1250 | return false; | |
1251 | } | |
1252 | ||
1253 | /// Move left | |
1254 | bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) | |
1255 | { | |
1256 | long startPos = -1; | |
1257 | ||
1258 | if (m_caretPosition > startPos - noPositions + 1) | |
1259 | { | |
1260 | long oldPos = m_caretPosition; | |
1261 | long newPos = m_caretPosition - noPositions; | |
1262 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1263 | if (!extendSel) | |
1264 | SelectNone(); | |
1265 | ||
1266 | if (noPositions == 1 && !extendSel) | |
1267 | MoveCaretBack(oldPos); | |
1268 | else | |
1269 | SetCaretPosition(newPos); | |
1270 | ||
1271 | PositionCaret(); | |
1272 | SetDefaultStyleToCursorStyle(); | |
1273 | ||
1274 | if (extendSel) | |
1275 | Refresh(false); | |
1276 | return true; | |
1277 | } | |
1278 | else | |
1279 | return false; | |
1280 | } | |
1281 | ||
1282 | /// Move up | |
1283 | bool wxRichTextCtrl::MoveUp(int noLines, int flags) | |
1284 | { | |
1285 | return MoveDown(- noLines, flags); | |
1286 | } | |
1287 | ||
1288 | /// Move up | |
1289 | bool wxRichTextCtrl::MoveDown(int noLines, int flags) | |
1290 | { | |
1291 | if (!GetCaret()) | |
1292 | return false; | |
1293 | ||
1294 | long lineNumber = GetBuffer().GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); | |
1295 | wxPoint pt = GetCaret()->GetPosition(); | |
1296 | long newLine = lineNumber + noLines; | |
1297 | ||
1298 | if (lineNumber != -1) | |
1299 | { | |
1300 | if (noLines > 0) | |
1301 | { | |
1302 | long lastLine = GetBuffer().GetVisibleLineNumber(GetBuffer().GetRange().GetEnd()); | |
1303 | ||
1304 | if (newLine > lastLine) | |
1305 | return false; | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | if (newLine < 0) | |
1310 | return false; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | wxRichTextLine* lineObj = GetBuffer().GetLineForVisibleLineNumber(newLine); | |
1315 | if (lineObj) | |
1316 | { | |
1317 | pt.y = lineObj->GetAbsolutePosition().y + 2; | |
1318 | } | |
1319 | else | |
1320 | return false; | |
1321 | ||
1322 | long newPos = 0; | |
1323 | wxClientDC dc(this); | |
1324 | PrepareDC(dc); | |
1325 | dc.SetFont(GetFont()); | |
1326 | ||
1327 | int hitTest = GetBuffer().HitTest(dc, pt, newPos); | |
1328 | ||
1329 | if (hitTest != wxRICHTEXT_HITTEST_NONE) | |
1330 | { | |
1331 | // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, | |
1332 | // we want to be at the end of the last line but with m_caretAtLineStart set to true, | |
1333 | // so we view the caret at the start of the line. | |
1334 | bool caretLineStart = false; | |
1335 | if (hitTest & wxRICHTEXT_HITTEST_BEFORE) | |
1336 | { | |
1337 | wxRichTextLine* thisLine = GetBuffer().GetLineAtPosition(newPos-1); | |
1338 | wxRichTextRange lineRange; | |
1339 | if (thisLine) | |
1340 | lineRange = thisLine->GetAbsoluteRange(); | |
1341 | ||
1342 | if (thisLine && (newPos-1) == lineRange.GetEnd()) | |
1343 | { | |
1344 | newPos --; | |
1345 | caretLineStart = true; | |
1346 | } | |
1347 | else | |
1348 | { | |
1349 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(newPos); | |
1350 | if (para && para->GetRange().GetStart() == newPos) | |
1351 | newPos --; | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | long newSelEnd = newPos; | |
1356 | ||
1357 | bool extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); | |
1358 | if (!extendSel) | |
1359 | SelectNone(); | |
1360 | ||
1361 | SetCaretPosition(newPos, caretLineStart); | |
1362 | PositionCaret(); | |
1363 | SetDefaultStyleToCursorStyle(); | |
1364 | ||
1365 | if (extendSel) | |
1366 | Refresh(false); | |
1367 | return true; | |
1368 | } | |
1369 | ||
1370 | return false; | |
1371 | } | |
1372 | ||
1373 | /// Move to the end of the paragraph | |
1374 | bool wxRichTextCtrl::MoveToParagraphEnd(int flags) | |
1375 | { | |
1376 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); | |
1377 | if (para) | |
1378 | { | |
1379 | long newPos = para->GetRange().GetEnd() - 1; | |
1380 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1381 | if (!extendSel) | |
1382 | SelectNone(); | |
1383 | ||
1384 | SetCaretPosition(newPos); | |
1385 | PositionCaret(); | |
1386 | SetDefaultStyleToCursorStyle(); | |
1387 | ||
1388 | if (extendSel) | |
1389 | Refresh(false); | |
1390 | return true; | |
1391 | } | |
1392 | ||
1393 | return false; | |
1394 | } | |
1395 | ||
1396 | /// Move to the start of the paragraph | |
1397 | bool wxRichTextCtrl::MoveToParagraphStart(int flags) | |
1398 | { | |
1399 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(m_caretPosition, true); | |
1400 | if (para) | |
1401 | { | |
1402 | long newPos = para->GetRange().GetStart() - 1; | |
1403 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1404 | if (!extendSel) | |
1405 | SelectNone(); | |
1406 | ||
1407 | SetCaretPosition(newPos); | |
1408 | PositionCaret(); | |
1409 | SetDefaultStyleToCursorStyle(); | |
1410 | ||
1411 | if (extendSel) | |
1412 | Refresh(false); | |
1413 | return true; | |
1414 | } | |
1415 | ||
1416 | return false; | |
1417 | } | |
1418 | ||
1419 | /// Move to the end of the line | |
1420 | bool wxRichTextCtrl::MoveToLineEnd(int flags) | |
1421 | { | |
1422 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1423 | ||
1424 | if (line) | |
1425 | { | |
1426 | wxRichTextRange lineRange = line->GetAbsoluteRange(); | |
1427 | long newPos = lineRange.GetEnd(); | |
1428 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1429 | if (!extendSel) | |
1430 | SelectNone(); | |
1431 | ||
1432 | SetCaretPosition(newPos); | |
1433 | PositionCaret(); | |
1434 | SetDefaultStyleToCursorStyle(); | |
1435 | ||
1436 | if (extendSel) | |
1437 | Refresh(false); | |
1438 | return true; | |
1439 | } | |
1440 | ||
1441 | return false; | |
1442 | } | |
1443 | ||
1444 | /// Move to the start of the line | |
1445 | bool wxRichTextCtrl::MoveToLineStart(int flags) | |
1446 | { | |
1447 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1448 | if (line) | |
1449 | { | |
1450 | wxRichTextRange lineRange = line->GetAbsoluteRange(); | |
1451 | long newPos = lineRange.GetStart()-1; | |
1452 | ||
1453 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1454 | if (!extendSel) | |
1455 | SelectNone(); | |
1456 | ||
1457 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(line); | |
1458 | ||
1459 | SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart()); | |
1460 | PositionCaret(); | |
1461 | SetDefaultStyleToCursorStyle(); | |
1462 | ||
1463 | if (extendSel) | |
1464 | Refresh(false); | |
1465 | return true; | |
1466 | } | |
1467 | ||
1468 | return false; | |
1469 | } | |
1470 | ||
1471 | /// Move to the start of the buffer | |
1472 | bool wxRichTextCtrl::MoveHome(int flags) | |
1473 | { | |
1474 | if (m_caretPosition != -1) | |
1475 | { | |
1476 | bool extendSel = ExtendSelection(m_caretPosition, -1, flags); | |
1477 | if (!extendSel) | |
1478 | SelectNone(); | |
1479 | ||
1480 | SetCaretPosition(-1); | |
1481 | PositionCaret(); | |
1482 | SetDefaultStyleToCursorStyle(); | |
1483 | ||
1484 | if (extendSel) | |
1485 | Refresh(false); | |
1486 | return true; | |
1487 | } | |
1488 | else | |
1489 | return false; | |
1490 | } | |
1491 | ||
1492 | /// Move to the end of the buffer | |
1493 | bool wxRichTextCtrl::MoveEnd(int flags) | |
1494 | { | |
1495 | long endPos = GetBuffer().GetRange().GetEnd()-1; | |
1496 | ||
1497 | if (m_caretPosition != endPos) | |
1498 | { | |
1499 | bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); | |
1500 | if (!extendSel) | |
1501 | SelectNone(); | |
1502 | ||
1503 | SetCaretPosition(endPos); | |
1504 | PositionCaret(); | |
1505 | SetDefaultStyleToCursorStyle(); | |
1506 | ||
1507 | if (extendSel) | |
1508 | Refresh(false); | |
1509 | return true; | |
1510 | } | |
1511 | else | |
1512 | return false; | |
1513 | } | |
1514 | ||
1515 | /// Move noPages pages up | |
1516 | bool wxRichTextCtrl::PageUp(int noPages, int flags) | |
1517 | { | |
1518 | return PageDown(- noPages, flags); | |
1519 | } | |
1520 | ||
1521 | /// Move noPages pages down | |
1522 | bool wxRichTextCtrl::PageDown(int noPages, int flags) | |
1523 | { | |
1524 | // Calculate which line occurs noPages * screen height further down. | |
1525 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
1526 | if (line) | |
1527 | { | |
1528 | wxSize clientSize = GetClientSize(); | |
1529 | int newY = line->GetAbsolutePosition().y + noPages*clientSize.y; | |
1530 | ||
1531 | wxRichTextLine* newLine = GetBuffer().GetLineAtYPosition(newY); | |
1532 | if (newLine) | |
1533 | { | |
1534 | wxRichTextRange lineRange = newLine->GetAbsoluteRange(); | |
1535 | long pos = lineRange.GetStart()-1; | |
1536 | if (pos != m_caretPosition) | |
1537 | { | |
1538 | wxRichTextParagraph* para = GetBuffer().GetParagraphForLine(newLine); | |
1539 | ||
1540 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); | |
1541 | if (!extendSel) | |
1542 | SelectNone(); | |
1543 | ||
1544 | SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); | |
1545 | PositionCaret(); | |
1546 | SetDefaultStyleToCursorStyle(); | |
1547 | ||
1548 | if (extendSel) | |
1549 | Refresh(false); | |
1550 | return true; | |
1551 | } | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | return false; | |
1556 | } | |
1557 | ||
1558 | static bool wxRichTextCtrlIsWhitespace(const wxString& str) | |
1559 | { | |
1560 | return str == wxT(" ") || str == wxT("\t"); | |
1561 | } | |
1562 | ||
1563 | // Finds the caret position for the next word | |
1564 | long wxRichTextCtrl::FindNextWordPosition(int direction) const | |
1565 | { | |
1566 | long endPos = GetBuffer().GetRange().GetEnd(); | |
1567 | ||
1568 | if (direction > 0) | |
1569 | { | |
1570 | long i = m_caretPosition+1+direction; // +1 for conversion to character pos | |
1571 | ||
1572 | // First skip current text to space | |
1573 | while (i < endPos && i > -1) | |
1574 | { | |
1575 | // i is in character, not caret positions | |
1576 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
1577 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false); | |
1578 | if (line && (i == line->GetAbsoluteRange().GetEnd())) | |
1579 | { | |
1580 | break; | |
1581 | } | |
1582 | else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty()) | |
1583 | i += direction; | |
1584 | else | |
1585 | { | |
1586 | break; | |
1587 | } | |
1588 | } | |
1589 | while (i < endPos && i > -1) | |
1590 | { | |
1591 | // i is in character, not caret positions | |
1592 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
1593 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false); | |
1594 | if (line && (i == line->GetAbsoluteRange().GetEnd())) | |
1595 | return wxMax(-1, i); | |
1596 | ||
1597 | if (text.empty()) // End of paragraph, or maybe an image | |
1598 | return wxMax(-1, i - 1); | |
1599 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) | |
1600 | i += direction; | |
1601 | else | |
1602 | { | |
1603 | // Convert to caret position | |
1604 | return wxMax(-1, i - 1); | |
1605 | } | |
1606 | } | |
1607 | if (i >= endPos) | |
1608 | return endPos-1; | |
1609 | return i-1; | |
1610 | } | |
1611 | else | |
1612 | { | |
1613 | long i = m_caretPosition; | |
1614 | ||
1615 | // First skip white space | |
1616 | while (i < endPos && i > -1) | |
1617 | { | |
1618 | // i is in character, not caret positions | |
1619 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
1620 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false); | |
1621 | ||
1622 | if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image | |
1623 | break; | |
1624 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) | |
1625 | i += direction; | |
1626 | else | |
1627 | break; | |
1628 | } | |
1629 | // Next skip current text to space | |
1630 | while (i < endPos && i > -1) | |
1631 | { | |
1632 | // i is in character, not caret positions | |
1633 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(i, i)); | |
1634 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(i, false); | |
1635 | if (line && line->GetAbsoluteRange().GetStart() == i) | |
1636 | return i-1; | |
1637 | ||
1638 | if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */) | |
1639 | i += direction; | |
1640 | else | |
1641 | { | |
1642 | return i; | |
1643 | } | |
1644 | } | |
1645 | if (i < -1) | |
1646 | return -1; | |
1647 | return i; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | /// Move n words left | |
1652 | bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) | |
1653 | { | |
1654 | long pos = FindNextWordPosition(-1); | |
1655 | if (pos != m_caretPosition) | |
1656 | { | |
1657 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); | |
1658 | ||
1659 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); | |
1660 | if (!extendSel) | |
1661 | SelectNone(); | |
1662 | ||
1663 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); | |
1664 | PositionCaret(); | |
1665 | SetDefaultStyleToCursorStyle(); | |
1666 | ||
1667 | if (extendSel) | |
1668 | Refresh(false); | |
1669 | return true; | |
1670 | } | |
1671 | ||
1672 | return false; | |
1673 | } | |
1674 | ||
1675 | /// Move n words right | |
1676 | bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) | |
1677 | { | |
1678 | long pos = FindNextWordPosition(1); | |
1679 | if (pos != m_caretPosition) | |
1680 | { | |
1681 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(pos, true); | |
1682 | ||
1683 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); | |
1684 | if (!extendSel) | |
1685 | SelectNone(); | |
1686 | ||
1687 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); | |
1688 | PositionCaret(); | |
1689 | SetDefaultStyleToCursorStyle(); | |
1690 | ||
1691 | if (extendSel) | |
1692 | Refresh(false); | |
1693 | return true; | |
1694 | } | |
1695 | ||
1696 | return false; | |
1697 | } | |
1698 | ||
1699 | /// Sizing | |
1700 | void wxRichTextCtrl::OnSize(wxSizeEvent& event) | |
1701 | { | |
1702 | // Only do sizing optimization for large buffers | |
1703 | if (GetBuffer().GetRange().GetEnd() > m_delayedLayoutThreshold) | |
1704 | { | |
1705 | m_fullLayoutRequired = true; | |
1706 | m_fullLayoutTime = wxGetLocalTimeMillis(); | |
1707 | m_fullLayoutSavedPosition = GetFirstVisiblePosition(); | |
1708 | LayoutContent(true /* onlyVisibleRect */); | |
1709 | } | |
1710 | else | |
1711 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
1712 | ||
1713 | #if wxRICHTEXT_BUFFERED_PAINTING | |
1714 | RecreateBuffer(); | |
1715 | #endif | |
1716 | ||
1717 | event.Skip(); | |
1718 | } | |
1719 | ||
1720 | ||
1721 | /// Idle-time processing | |
1722 | void wxRichTextCtrl::OnIdle(wxIdleEvent& event) | |
1723 | { | |
1724 | const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL; | |
1725 | ||
1726 | if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval))) | |
1727 | { | |
1728 | m_fullLayoutRequired = false; | |
1729 | m_fullLayoutTime = 0; | |
1730 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
1731 | ShowPosition(m_fullLayoutSavedPosition); | |
1732 | Refresh(false); | |
1733 | } | |
1734 | ||
1735 | if (m_caretPositionForDefaultStyle != -2) | |
1736 | { | |
1737 | // If the caret position has changed, no longer reflect the default style | |
1738 | // in the UI. | |
1739 | if (GetCaretPosition() != m_caretPositionForDefaultStyle) | |
1740 | m_caretPositionForDefaultStyle = -2; | |
1741 | } | |
1742 | ||
1743 | event.Skip(); | |
1744 | } | |
1745 | ||
1746 | /// Scrolling | |
1747 | void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) | |
1748 | { | |
1749 | // Not used | |
1750 | event.Skip(); | |
1751 | } | |
1752 | ||
1753 | /// Set up scrollbars, e.g. after a resize | |
1754 | void wxRichTextCtrl::SetupScrollbars(bool atTop) | |
1755 | { | |
1756 | if (m_freezeCount) | |
1757 | return; | |
1758 | ||
1759 | if (GetBuffer().IsEmpty()) | |
1760 | { | |
1761 | SetScrollbars(0, 0, 0, 0, 0, 0); | |
1762 | return; | |
1763 | } | |
1764 | ||
1765 | // TODO: reimplement scrolling so we scroll by line, not by fixed number | |
1766 | // of pixels. See e.g. wxVScrolledWindow for ideas. | |
1767 | int pixelsPerUnit = 5; | |
1768 | wxSize clientSize = GetClientSize(); | |
1769 | ||
1770 | int maxHeight = GetBuffer().GetCachedSize().y; | |
1771 | ||
1772 | // Round up so we have at least maxHeight pixels | |
1773 | int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5); | |
1774 | ||
1775 | int startX = 0, startY = 0; | |
1776 | if (!atTop) | |
1777 | GetViewStart(& startX, & startY); | |
1778 | ||
1779 | int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0); | |
1780 | int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5); | |
1781 | ||
1782 | // Move to previous scroll position if | |
1783 | // possible | |
1784 | SetScrollbars(0, pixelsPerUnit, | |
1785 | 0, unitsY, | |
1786 | wxMin(maxPositionX, startX), wxMin(maxPositionY, startY)); | |
1787 | } | |
1788 | ||
1789 | /// Paint the background | |
1790 | void wxRichTextCtrl::PaintBackground(wxDC& dc) | |
1791 | { | |
1792 | wxColour backgroundColour = GetBackgroundColour(); | |
1793 | if (!backgroundColour.Ok()) | |
1794 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
1795 | ||
1796 | // Clear the background | |
1797 | dc.SetBrush(wxBrush(backgroundColour)); | |
1798 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1799 | wxRect windowRect(GetClientSize()); | |
1800 | windowRect.x -= 2; windowRect.y -= 2; | |
1801 | windowRect.width += 4; windowRect.height += 4; | |
1802 | ||
1803 | // We need to shift the rectangle to take into account | |
1804 | // scrolling. Converting device to logical coordinates. | |
1805 | CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y); | |
1806 | dc.DrawRectangle(windowRect); | |
1807 | } | |
1808 | ||
1809 | #if wxRICHTEXT_BUFFERED_PAINTING | |
1810 | /// Recreate buffer bitmap if necessary | |
1811 | bool wxRichTextCtrl::RecreateBuffer(const wxSize& size) | |
1812 | { | |
1813 | wxSize sz = size; | |
1814 | if (sz == wxDefaultSize) | |
1815 | sz = GetClientSize(); | |
1816 | ||
1817 | if (sz.x < 1 || sz.y < 1) | |
1818 | return false; | |
1819 | ||
1820 | if (!m_bufferBitmap.Ok() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y) | |
1821 | m_bufferBitmap = wxBitmap(sz.x, sz.y); | |
1822 | return m_bufferBitmap.Ok(); | |
1823 | } | |
1824 | #endif | |
1825 | ||
1826 | // ---------------------------------------------------------------------------- | |
1827 | // file IO functions | |
1828 | // ---------------------------------------------------------------------------- | |
1829 | ||
1830 | bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType) | |
1831 | { | |
1832 | bool success = GetBuffer().LoadFile(filename, fileType); | |
1833 | if (success) | |
1834 | m_filename = filename; | |
1835 | ||
1836 | DiscardEdits(); | |
1837 | SetInsertionPoint(0); | |
1838 | LayoutContent(); | |
1839 | PositionCaret(); | |
1840 | SetupScrollbars(true); | |
1841 | Refresh(false); | |
1842 | SendTextUpdatedEvent(); | |
1843 | ||
1844 | if (success) | |
1845 | return true; | |
1846 | else | |
1847 | { | |
1848 | wxLogError(_("File couldn't be loaded.")); | |
1849 | ||
1850 | return false; | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType) | |
1855 | { | |
1856 | if (GetBuffer().SaveFile(filename, fileType)) | |
1857 | { | |
1858 | m_filename = filename; | |
1859 | ||
1860 | DiscardEdits(); | |
1861 | ||
1862 | return true; | |
1863 | } | |
1864 | ||
1865 | wxLogError(_("The text couldn't be saved.")); | |
1866 | ||
1867 | return false; | |
1868 | } | |
1869 | ||
1870 | // ---------------------------------------------------------------------------- | |
1871 | // wxRichTextCtrl specific functionality | |
1872 | // ---------------------------------------------------------------------------- | |
1873 | ||
1874 | /// Add a new paragraph of text to the end of the buffer | |
1875 | wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text) | |
1876 | { | |
1877 | return GetBuffer().AddParagraph(text); | |
1878 | } | |
1879 | ||
1880 | /// Add an image | |
1881 | wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image) | |
1882 | { | |
1883 | return GetBuffer().AddImage(image); | |
1884 | } | |
1885 | ||
1886 | // ---------------------------------------------------------------------------- | |
1887 | // selection and ranges | |
1888 | // ---------------------------------------------------------------------------- | |
1889 | ||
1890 | void wxRichTextCtrl::SelectAll() | |
1891 | { | |
1892 | SetSelection(0, GetLastPosition()+1); | |
1893 | m_selectionAnchor = -1; | |
1894 | } | |
1895 | ||
1896 | /// Select none | |
1897 | void wxRichTextCtrl::SelectNone() | |
1898 | { | |
1899 | if (!(GetSelectionRange() == wxRichTextRange(-2, -2))) | |
1900 | SetSelection(-2, -2); | |
1901 | m_selectionAnchor = -2; | |
1902 | } | |
1903 | ||
1904 | static bool wxIsWordDelimiter(const wxString& text) | |
1905 | { | |
1906 | return !text.IsEmpty() && !wxIsalnum(text[0]); | |
1907 | } | |
1908 | ||
1909 | /// Select the word at the given character position | |
1910 | bool wxRichTextCtrl::SelectWord(long position) | |
1911 | { | |
1912 | if (position < 0 || position > GetBuffer().GetRange().GetEnd()) | |
1913 | return false; | |
1914 | ||
1915 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(position); | |
1916 | if (!para) | |
1917 | return false; | |
1918 | ||
1919 | long positionStart = position; | |
1920 | long positionEnd = position; | |
1921 | ||
1922 | for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --) | |
1923 | { | |
1924 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionStart, positionStart)); | |
1925 | if (wxIsWordDelimiter(text)) | |
1926 | { | |
1927 | positionStart ++; | |
1928 | break; | |
1929 | } | |
1930 | } | |
1931 | if (positionStart < para->GetRange().GetStart()) | |
1932 | positionStart = para->GetRange().GetStart(); | |
1933 | ||
1934 | for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++) | |
1935 | { | |
1936 | wxString text = GetBuffer().GetTextForRange(wxRichTextRange(positionEnd, positionEnd)); | |
1937 | if (wxIsWordDelimiter(text)) | |
1938 | { | |
1939 | positionEnd --; | |
1940 | break; | |
1941 | } | |
1942 | } | |
1943 | if (positionEnd >= para->GetRange().GetEnd()) | |
1944 | positionEnd = para->GetRange().GetEnd(); | |
1945 | ||
1946 | SetSelection(positionStart, positionEnd+1); | |
1947 | ||
1948 | if (positionStart >= 0) | |
1949 | { | |
1950 | MoveCaret(positionStart-1, true); | |
1951 | SetDefaultStyleToCursorStyle(); | |
1952 | } | |
1953 | ||
1954 | return true; | |
1955 | } | |
1956 | ||
1957 | wxString wxRichTextCtrl::GetStringSelection() const | |
1958 | { | |
1959 | long from, to; | |
1960 | GetSelection(&from, &to); | |
1961 | ||
1962 | return GetRange(from, to); | |
1963 | } | |
1964 | ||
1965 | // ---------------------------------------------------------------------------- | |
1966 | // hit testing | |
1967 | // ---------------------------------------------------------------------------- | |
1968 | ||
1969 | wxTextCtrlHitTestResult | |
1970 | wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const | |
1971 | { | |
1972 | // implement in terms of the other overload as the native ports typically | |
1973 | // can get the position and not (x, y) pair directly (although wxUniv | |
1974 | // directly gets x and y -- and so overrides this method as well) | |
1975 | long pos; | |
1976 | wxTextCtrlHitTestResult rc = HitTest(pt, &pos); | |
1977 | ||
1978 | if ( rc != wxTE_HT_UNKNOWN ) | |
1979 | { | |
1980 | PositionToXY(pos, x, y); | |
1981 | } | |
1982 | ||
1983 | return rc; | |
1984 | } | |
1985 | ||
1986 | wxTextCtrlHitTestResult | |
1987 | wxRichTextCtrl::HitTest(const wxPoint& pt, | |
1988 | long * pos) const | |
1989 | { | |
1990 | wxClientDC dc((wxRichTextCtrl*) this); | |
1991 | ((wxRichTextCtrl*)this)->PrepareDC(dc); | |
1992 | ||
1993 | // Buffer uses logical position (relative to start of buffer) | |
1994 | // so convert | |
1995 | wxPoint pt2 = GetLogicalPoint(pt); | |
1996 | ||
1997 | int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos); | |
1998 | ||
1999 | if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) | |
2000 | return wxTE_HT_BEFORE; | |
2001 | else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) | |
2002 | return wxTE_HT_BEYOND; | |
2003 | else if (hit & wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER) | |
2004 | return wxTE_HT_ON_TEXT; | |
2005 | ||
2006 | return wxTE_HT_UNKNOWN; | |
2007 | } | |
2008 | ||
2009 | // ---------------------------------------------------------------------------- | |
2010 | // set/get the controls text | |
2011 | // ---------------------------------------------------------------------------- | |
2012 | ||
2013 | wxString wxRichTextCtrl::GetValue() const | |
2014 | { | |
2015 | return GetBuffer().GetText(); | |
2016 | } | |
2017 | ||
2018 | wxString wxRichTextCtrl::GetRange(long from, long to) const | |
2019 | { | |
2020 | // Public API for range is different from internals | |
2021 | return GetBuffer().GetTextForRange(wxRichTextRange(from, to-1)); | |
2022 | } | |
2023 | ||
2024 | void wxRichTextCtrl::DoSetValue(const wxString& value, int flags) | |
2025 | { | |
2026 | Clear(); | |
2027 | ||
2028 | // if the text is long enough, it's faster to just set it instead of first | |
2029 | // comparing it with the old one (chances are that it will be different | |
2030 | // anyhow, this comparison is there to avoid flicker for small single-line | |
2031 | // edit controls mostly) | |
2032 | if ( (value.length() > 0x400) || (value != GetValue()) ) | |
2033 | { | |
2034 | DoWriteText(value); | |
2035 | ||
2036 | // for compatibility, don't move the cursor when doing SetValue() | |
2037 | SetInsertionPoint(0); | |
2038 | } | |
2039 | else // same text | |
2040 | { | |
2041 | if ( flags & SetValue_SendEvent ) | |
2042 | { | |
2043 | // still send an event for consistency | |
2044 | SendTextUpdatedEvent(); | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | // we should reset the modified flag even if the value didn't really change | |
2049 | ||
2050 | // mark the control as being not dirty - we changed its text, not the | |
2051 | // user | |
2052 | DiscardEdits(); | |
2053 | } | |
2054 | ||
2055 | void wxRichTextCtrl::WriteText(const wxString& value) | |
2056 | { | |
2057 | DoWriteText(value); | |
2058 | } | |
2059 | ||
2060 | void wxRichTextCtrl::DoWriteText(const wxString& value, int flags) | |
2061 | { | |
2062 | wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix); | |
2063 | ||
2064 | GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); | |
2065 | ||
2066 | if ( flags & SetValue_SendEvent ) | |
2067 | SendTextUpdatedEvent(); | |
2068 | } | |
2069 | ||
2070 | void wxRichTextCtrl::AppendText(const wxString& text) | |
2071 | { | |
2072 | SetInsertionPointEnd(); | |
2073 | ||
2074 | WriteText(text); | |
2075 | } | |
2076 | ||
2077 | /// Write an image at the current insertion point | |
2078 | bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType) | |
2079 | { | |
2080 | wxRichTextImageBlock imageBlock; | |
2081 | ||
2082 | wxImage image2 = image; | |
2083 | if (imageBlock.MakeImageBlock(image2, bitmapType)) | |
2084 | return WriteImage(imageBlock); | |
2085 | ||
2086 | return false; | |
2087 | } | |
2088 | ||
2089 | bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) | |
2090 | { | |
2091 | wxRichTextImageBlock imageBlock; | |
2092 | ||
2093 | wxImage image; | |
2094 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) | |
2095 | return WriteImage(imageBlock); | |
2096 | ||
2097 | return false; | |
2098 | } | |
2099 | ||
2100 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock) | |
2101 | { | |
2102 | return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this); | |
2103 | } | |
2104 | ||
2105 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType) | |
2106 | { | |
2107 | if (bitmap.Ok()) | |
2108 | { | |
2109 | wxRichTextImageBlock imageBlock; | |
2110 | ||
2111 | wxImage image = bitmap.ConvertToImage(); | |
2112 | if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType)) | |
2113 | return WriteImage(imageBlock); | |
2114 | } | |
2115 | ||
2116 | return false; | |
2117 | } | |
2118 | ||
2119 | /// Insert a newline (actually paragraph) at the current insertion point. | |
2120 | bool wxRichTextCtrl::Newline() | |
2121 | { | |
2122 | return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this); | |
2123 | } | |
2124 | ||
2125 | /// Insert a line break at the current insertion point. | |
2126 | bool wxRichTextCtrl::LineBreak() | |
2127 | { | |
2128 | wxString text; | |
2129 | text = wxRichTextLineBreakChar; | |
2130 | return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this); | |
2131 | } | |
2132 | ||
2133 | // ---------------------------------------------------------------------------- | |
2134 | // Clipboard operations | |
2135 | // ---------------------------------------------------------------------------- | |
2136 | ||
2137 | void wxRichTextCtrl::Copy() | |
2138 | { | |
2139 | if (CanCopy()) | |
2140 | { | |
2141 | wxRichTextRange range = GetInternalSelectionRange(); | |
2142 | GetBuffer().CopyToClipboard(range); | |
2143 | } | |
2144 | } | |
2145 | ||
2146 | void wxRichTextCtrl::Cut() | |
2147 | { | |
2148 | if (CanCut()) | |
2149 | { | |
2150 | wxRichTextRange range = GetInternalSelectionRange(); | |
2151 | GetBuffer().CopyToClipboard(range); | |
2152 | ||
2153 | DeleteSelectedContent(); | |
2154 | LayoutContent(); | |
2155 | Refresh(false); | |
2156 | } | |
2157 | } | |
2158 | ||
2159 | void wxRichTextCtrl::Paste() | |
2160 | { | |
2161 | if (CanPaste()) | |
2162 | { | |
2163 | BeginBatchUndo(_("Paste")); | |
2164 | ||
2165 | long newPos = m_caretPosition; | |
2166 | DeleteSelectedContent(& newPos); | |
2167 | ||
2168 | GetBuffer().PasteFromClipboard(newPos); | |
2169 | ||
2170 | EndBatchUndo(); | |
2171 | } | |
2172 | } | |
2173 | ||
2174 | void wxRichTextCtrl::DeleteSelection() | |
2175 | { | |
2176 | if (CanDeleteSelection()) | |
2177 | { | |
2178 | DeleteSelectedContent(); | |
2179 | } | |
2180 | } | |
2181 | ||
2182 | bool wxRichTextCtrl::HasSelection() const | |
2183 | { | |
2184 | return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2; | |
2185 | } | |
2186 | ||
2187 | bool wxRichTextCtrl::CanCopy() const | |
2188 | { | |
2189 | // Can copy if there's a selection | |
2190 | return HasSelection(); | |
2191 | } | |
2192 | ||
2193 | bool wxRichTextCtrl::CanCut() const | |
2194 | { | |
2195 | return HasSelection() && IsEditable(); | |
2196 | } | |
2197 | ||
2198 | bool wxRichTextCtrl::CanPaste() const | |
2199 | { | |
2200 | if ( !IsEditable() ) | |
2201 | return false; | |
2202 | ||
2203 | return GetBuffer().CanPasteFromClipboard(); | |
2204 | } | |
2205 | ||
2206 | bool wxRichTextCtrl::CanDeleteSelection() const | |
2207 | { | |
2208 | return HasSelection() && IsEditable(); | |
2209 | } | |
2210 | ||
2211 | ||
2212 | // ---------------------------------------------------------------------------- | |
2213 | // Accessors | |
2214 | // ---------------------------------------------------------------------------- | |
2215 | ||
2216 | void wxRichTextCtrl::SetEditable(bool editable) | |
2217 | { | |
2218 | m_editable = editable; | |
2219 | } | |
2220 | ||
2221 | void wxRichTextCtrl::SetInsertionPoint(long pos) | |
2222 | { | |
2223 | SelectNone(); | |
2224 | ||
2225 | m_caretPosition = pos - 1; | |
2226 | } | |
2227 | ||
2228 | void wxRichTextCtrl::SetInsertionPointEnd() | |
2229 | { | |
2230 | long pos = GetLastPosition(); | |
2231 | SetInsertionPoint(pos); | |
2232 | } | |
2233 | ||
2234 | long wxRichTextCtrl::GetInsertionPoint() const | |
2235 | { | |
2236 | return m_caretPosition+1; | |
2237 | } | |
2238 | ||
2239 | wxTextPos wxRichTextCtrl::GetLastPosition() const | |
2240 | { | |
2241 | return GetBuffer().GetRange().GetEnd(); | |
2242 | } | |
2243 | ||
2244 | // If the return values from and to are the same, there is no | |
2245 | // selection. | |
2246 | void wxRichTextCtrl::GetSelection(long* from, long* to) const | |
2247 | { | |
2248 | *from = m_selectionRange.GetStart(); | |
2249 | *to = m_selectionRange.GetEnd(); | |
2250 | if ((*to) != -1 && (*to) != -2) | |
2251 | (*to) ++; | |
2252 | } | |
2253 | ||
2254 | bool wxRichTextCtrl::IsEditable() const | |
2255 | { | |
2256 | return m_editable; | |
2257 | } | |
2258 | ||
2259 | // ---------------------------------------------------------------------------- | |
2260 | // selection | |
2261 | // ---------------------------------------------------------------------------- | |
2262 | ||
2263 | void wxRichTextCtrl::SetSelection(long from, long to) | |
2264 | { | |
2265 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
2266 | // be selected. | |
2267 | if ( (from == -1) && (to == -1) ) | |
2268 | { | |
2269 | from = 0; | |
2270 | to = GetLastPosition()+1; | |
2271 | } | |
2272 | ||
2273 | DoSetSelection(from, to); | |
2274 | } | |
2275 | ||
2276 | void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret)) | |
2277 | { | |
2278 | m_selectionAnchor = from; | |
2279 | m_selectionRange.SetRange(from, to-1); | |
2280 | if (from > -2) | |
2281 | m_caretPosition = from-1; | |
2282 | ||
2283 | Refresh(false); | |
2284 | PositionCaret(); | |
2285 | } | |
2286 | ||
2287 | // ---------------------------------------------------------------------------- | |
2288 | // Editing | |
2289 | // ---------------------------------------------------------------------------- | |
2290 | ||
2291 | void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), | |
2292 | const wxString& value) | |
2293 | { | |
2294 | BeginBatchUndo(_("Replace")); | |
2295 | ||
2296 | DeleteSelectedContent(); | |
2297 | ||
2298 | DoWriteText(value, SetValue_SelectionOnly); | |
2299 | ||
2300 | EndBatchUndo(); | |
2301 | } | |
2302 | ||
2303 | void wxRichTextCtrl::Remove(long from, long to) | |
2304 | { | |
2305 | SelectNone(); | |
2306 | ||
2307 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), this); | |
2308 | ||
2309 | LayoutContent(); | |
2310 | if (!IsFrozen()) | |
2311 | Refresh(false); | |
2312 | } | |
2313 | ||
2314 | bool wxRichTextCtrl::IsModified() const | |
2315 | { | |
2316 | return m_buffer.IsModified(); | |
2317 | } | |
2318 | ||
2319 | void wxRichTextCtrl::MarkDirty() | |
2320 | { | |
2321 | m_buffer.Modify(true); | |
2322 | } | |
2323 | ||
2324 | void wxRichTextCtrl::DiscardEdits() | |
2325 | { | |
2326 | m_caretPositionForDefaultStyle = -2; | |
2327 | m_buffer.Modify(false); | |
2328 | m_buffer.GetCommandProcessor()->ClearCommands(); | |
2329 | } | |
2330 | ||
2331 | int wxRichTextCtrl::GetNumberOfLines() const | |
2332 | { | |
2333 | return GetBuffer().GetParagraphCount(); | |
2334 | } | |
2335 | ||
2336 | // ---------------------------------------------------------------------------- | |
2337 | // Positions <-> coords | |
2338 | // ---------------------------------------------------------------------------- | |
2339 | ||
2340 | long wxRichTextCtrl::XYToPosition(long x, long y) const | |
2341 | { | |
2342 | return GetBuffer().XYToPosition(x, y); | |
2343 | } | |
2344 | ||
2345 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
2346 | { | |
2347 | return GetBuffer().PositionToXY(pos, x, y); | |
2348 | } | |
2349 | ||
2350 | // ---------------------------------------------------------------------------- | |
2351 | // | |
2352 | // ---------------------------------------------------------------------------- | |
2353 | ||
2354 | void wxRichTextCtrl::ShowPosition(long pos) | |
2355 | { | |
2356 | if (!IsPositionVisible(pos)) | |
2357 | ScrollIntoView(pos-1, WXK_DOWN); | |
2358 | } | |
2359 | ||
2360 | int wxRichTextCtrl::GetLineLength(long lineNo) const | |
2361 | { | |
2362 | return GetBuffer().GetParagraphLength(lineNo); | |
2363 | } | |
2364 | ||
2365 | wxString wxRichTextCtrl::GetLineText(long lineNo) const | |
2366 | { | |
2367 | return GetBuffer().GetParagraphText(lineNo); | |
2368 | } | |
2369 | ||
2370 | // ---------------------------------------------------------------------------- | |
2371 | // Undo/redo | |
2372 | // ---------------------------------------------------------------------------- | |
2373 | ||
2374 | void wxRichTextCtrl::Undo() | |
2375 | { | |
2376 | if (CanUndo()) | |
2377 | { | |
2378 | GetCommandProcessor()->Undo(); | |
2379 | } | |
2380 | } | |
2381 | ||
2382 | void wxRichTextCtrl::Redo() | |
2383 | { | |
2384 | if (CanRedo()) | |
2385 | { | |
2386 | GetCommandProcessor()->Redo(); | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | bool wxRichTextCtrl::CanUndo() const | |
2391 | { | |
2392 | return GetCommandProcessor()->CanUndo(); | |
2393 | } | |
2394 | ||
2395 | bool wxRichTextCtrl::CanRedo() const | |
2396 | { | |
2397 | return GetCommandProcessor()->CanRedo(); | |
2398 | } | |
2399 | ||
2400 | // ---------------------------------------------------------------------------- | |
2401 | // implementation details | |
2402 | // ---------------------------------------------------------------------------- | |
2403 | ||
2404 | void wxRichTextCtrl::Command(wxCommandEvent& event) | |
2405 | { | |
2406 | SetValue(event.GetString()); | |
2407 | GetEventHandler()->ProcessEvent(event); | |
2408 | } | |
2409 | ||
2410 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
2411 | { | |
2412 | // By default, load the first file into the text window. | |
2413 | if (event.GetNumberOfFiles() > 0) | |
2414 | { | |
2415 | LoadFile(event.GetFiles()[0]); | |
2416 | } | |
2417 | } | |
2418 | ||
2419 | wxSize wxRichTextCtrl::DoGetBestSize() const | |
2420 | { | |
2421 | return wxSize(10, 10); | |
2422 | } | |
2423 | ||
2424 | // ---------------------------------------------------------------------------- | |
2425 | // standard handlers for standard edit menu events | |
2426 | // ---------------------------------------------------------------------------- | |
2427 | ||
2428 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
2429 | { | |
2430 | Cut(); | |
2431 | } | |
2432 | ||
2433 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) | |
2434 | { | |
2435 | DeleteSelection(); | |
2436 | } | |
2437 | ||
2438 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2439 | { | |
2440 | Copy(); | |
2441 | } | |
2442 | ||
2443 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2444 | { | |
2445 | Paste(); | |
2446 | } | |
2447 | ||
2448 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2449 | { | |
2450 | Undo(); | |
2451 | } | |
2452 | ||
2453 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2454 | { | |
2455 | Redo(); | |
2456 | } | |
2457 | ||
2458 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2459 | { | |
2460 | event.Enable( CanCut() ); | |
2461 | } | |
2462 | ||
2463 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2464 | { | |
2465 | event.Enable( CanCopy() ); | |
2466 | } | |
2467 | ||
2468 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) | |
2469 | { | |
2470 | event.Enable( CanDeleteSelection() ); | |
2471 | } | |
2472 | ||
2473 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2474 | { | |
2475 | event.Enable( CanPaste() ); | |
2476 | } | |
2477 | ||
2478 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2479 | { | |
2480 | event.Enable( CanUndo() ); | |
2481 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); | |
2482 | } | |
2483 | ||
2484 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2485 | { | |
2486 | event.Enable( CanRedo() ); | |
2487 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); | |
2488 | } | |
2489 | ||
2490 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
2491 | { | |
2492 | SelectAll(); | |
2493 | } | |
2494 | ||
2495 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
2496 | { | |
2497 | event.Enable(GetLastPosition() > 0); | |
2498 | } | |
2499 | ||
2500 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& WXUNUSED(event)) | |
2501 | { | |
2502 | if (!m_contextMenu) | |
2503 | { | |
2504 | m_contextMenu = new wxMenu; | |
2505 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); | |
2506 | m_contextMenu->Append(wxID_REDO, _("&Redo")); | |
2507 | m_contextMenu->AppendSeparator(); | |
2508 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); | |
2509 | m_contextMenu->Append(wxID_COPY, _("&Copy")); | |
2510 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); | |
2511 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); | |
2512 | m_contextMenu->AppendSeparator(); | |
2513 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
2514 | } | |
2515 | PopupMenu(m_contextMenu); | |
2516 | return; | |
2517 | } | |
2518 | ||
2519 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) | |
2520 | { | |
2521 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style); | |
2522 | } | |
2523 | ||
2524 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) | |
2525 | { | |
2526 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttrEx(style)); | |
2527 | } | |
2528 | ||
2529 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) | |
2530 | { | |
2531 | return GetBuffer().SetStyle(range.ToInternal(), style); | |
2532 | } | |
2533 | ||
2534 | // extended style setting operation with flags including: | |
2535 | // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. | |
2536 | // see richtextbuffer.h for more details. | |
2537 | bool wxRichTextCtrl::SetStyleEx(long start, long end, const wxTextAttrEx& style, int flags) | |
2538 | { | |
2539 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style, flags); | |
2540 | } | |
2541 | ||
2542 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttrEx& style, int flags) | |
2543 | { | |
2544 | return GetBuffer().SetStyle(range.ToInternal(), style, flags); | |
2545 | } | |
2546 | ||
2547 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags) | |
2548 | { | |
2549 | return GetBuffer().SetStyle(range.ToInternal(), style, flags); | |
2550 | } | |
2551 | ||
2552 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) | |
2553 | { | |
2554 | return GetBuffer().SetDefaultStyle(style); | |
2555 | } | |
2556 | ||
2557 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style) | |
2558 | { | |
2559 | return GetBuffer().SetDefaultStyle(wxTextAttrEx(style)); | |
2560 | } | |
2561 | ||
2562 | const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const | |
2563 | { | |
2564 | return GetBuffer().GetDefaultStyle(); | |
2565 | } | |
2566 | ||
2567 | const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const | |
2568 | { | |
2569 | return GetBuffer().GetDefaultStyle(); | |
2570 | } | |
2571 | ||
2572 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style) | |
2573 | { | |
2574 | wxTextAttrEx attr(style); | |
2575 | if (GetBuffer().GetStyle(position, attr)) | |
2576 | { | |
2577 | style = attr; | |
2578 | return true; | |
2579 | } | |
2580 | else | |
2581 | return false; | |
2582 | } | |
2583 | ||
2584 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) | |
2585 | { | |
2586 | return GetBuffer().GetStyle(position, style); | |
2587 | } | |
2588 | ||
2589 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) | |
2590 | { | |
2591 | return GetBuffer().GetStyle(position, style); | |
2592 | } | |
2593 | ||
2594 | // get the common set of styles for the range | |
2595 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style) | |
2596 | { | |
2597 | wxTextAttrEx styleEx; | |
2598 | if (GetBuffer().GetStyleForRange(range.ToInternal(), styleEx)) | |
2599 | { | |
2600 | style = styleEx; | |
2601 | return true; | |
2602 | } | |
2603 | else | |
2604 | return false; | |
2605 | } | |
2606 | ||
2607 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttrEx& style) | |
2608 | { | |
2609 | return GetBuffer().GetStyleForRange(range.ToInternal(), style); | |
2610 | } | |
2611 | ||
2612 | /// Get the content (uncombined) attributes for this position. | |
2613 | ||
2614 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style) | |
2615 | { | |
2616 | wxTextAttrEx attr(style); | |
2617 | if (GetBuffer().GetUncombinedStyle(position, attr)) | |
2618 | { | |
2619 | style = attr; | |
2620 | return true; | |
2621 | } | |
2622 | else | |
2623 | return false; | |
2624 | } | |
2625 | ||
2626 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttrEx& style) | |
2627 | { | |
2628 | return GetBuffer().GetUncombinedStyle(position, style); | |
2629 | } | |
2630 | ||
2631 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style) | |
2632 | { | |
2633 | return GetBuffer().GetUncombinedStyle(position, style); | |
2634 | } | |
2635 | ||
2636 | /// Set font, and also the buffer attributes | |
2637 | bool wxRichTextCtrl::SetFont(const wxFont& font) | |
2638 | { | |
2639 | wxControl::SetFont(font); | |
2640 | ||
2641 | wxTextAttrEx attr = GetBuffer().GetAttributes(); | |
2642 | attr.SetFont(font); | |
2643 | GetBuffer().SetBasicStyle(attr); | |
2644 | ||
2645 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
2646 | Refresh(false); | |
2647 | ||
2648 | return true; | |
2649 | } | |
2650 | ||
2651 | /// Transform logical to physical | |
2652 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const | |
2653 | { | |
2654 | wxPoint pt; | |
2655 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); | |
2656 | ||
2657 | return pt; | |
2658 | } | |
2659 | ||
2660 | /// Transform physical to logical | |
2661 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const | |
2662 | { | |
2663 | wxPoint pt; | |
2664 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); | |
2665 | ||
2666 | return pt; | |
2667 | } | |
2668 | ||
2669 | /// Position the caret | |
2670 | void wxRichTextCtrl::PositionCaret() | |
2671 | { | |
2672 | if (!GetCaret()) | |
2673 | return; | |
2674 | ||
2675 | //wxLogDebug(wxT("PositionCaret")); | |
2676 | ||
2677 | wxRect caretRect; | |
2678 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) | |
2679 | { | |
2680 | wxPoint originalPt = caretRect.GetPosition(); | |
2681 | wxPoint pt = GetPhysicalPoint(originalPt); | |
2682 | if (GetCaret()->GetPosition() != pt) | |
2683 | { | |
2684 | GetCaret()->Move(pt); | |
2685 | GetCaret()->SetSize(caretRect.GetSize()); | |
2686 | } | |
2687 | } | |
2688 | } | |
2689 | ||
2690 | /// Get the caret height and position for the given character position | |
2691 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect) | |
2692 | { | |
2693 | wxClientDC dc(this); | |
2694 | dc.SetFont(GetFont()); | |
2695 | ||
2696 | PrepareDC(dc); | |
2697 | ||
2698 | wxPoint pt; | |
2699 | int height = 0; | |
2700 | ||
2701 | if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart)) | |
2702 | { | |
2703 | // Caret height can't be zero | |
2704 | if (height == 0) | |
2705 | height = dc.GetCharHeight(); | |
2706 | ||
2707 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); | |
2708 | return true; | |
2709 | } | |
2710 | ||
2711 | return false; | |
2712 | } | |
2713 | ||
2714 | /// Gets the line for the visible caret position. If the caret is | |
2715 | /// shown at the very end of the line, it means the next character is actually | |
2716 | /// on the following line. So let's get the line we're expecting to find | |
2717 | /// if this is the case. | |
2718 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const | |
2719 | { | |
2720 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true); | |
2721 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true); | |
2722 | if (line) | |
2723 | { | |
2724 | wxRichTextRange lineRange = line->GetAbsoluteRange(); | |
2725 | if (caretPosition == lineRange.GetStart()-1 && | |
2726 | (para->GetRange().GetStart() != lineRange.GetStart())) | |
2727 | { | |
2728 | if (!m_caretAtLineStart) | |
2729 | line = GetBuffer().GetLineAtPosition(caretPosition-1, true); | |
2730 | } | |
2731 | } | |
2732 | return line; | |
2733 | } | |
2734 | ||
2735 | ||
2736 | /// Move the caret to the given character position | |
2737 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) | |
2738 | { | |
2739 | if (GetBuffer().GetDirty()) | |
2740 | LayoutContent(); | |
2741 | ||
2742 | if (pos <= GetBuffer().GetRange().GetEnd()) | |
2743 | { | |
2744 | SetCaretPosition(pos, showAtLineStart); | |
2745 | ||
2746 | PositionCaret(); | |
2747 | ||
2748 | return true; | |
2749 | } | |
2750 | else | |
2751 | return false; | |
2752 | } | |
2753 | ||
2754 | /// Layout the buffer: which we must do before certain operations, such as | |
2755 | /// setting the caret position. | |
2756 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) | |
2757 | { | |
2758 | if (GetBuffer().GetDirty() || onlyVisibleRect) | |
2759 | { | |
2760 | wxRect availableSpace(GetClientSize()); | |
2761 | if (availableSpace.width == 0) | |
2762 | availableSpace.width = 10; | |
2763 | if (availableSpace.height == 0) | |
2764 | availableSpace.height = 10; | |
2765 | ||
2766 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; | |
2767 | if (onlyVisibleRect) | |
2768 | { | |
2769 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; | |
2770 | availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); | |
2771 | } | |
2772 | ||
2773 | wxClientDC dc(this); | |
2774 | dc.SetFont(GetFont()); | |
2775 | ||
2776 | PrepareDC(dc); | |
2777 | ||
2778 | GetBuffer().Defragment(); | |
2779 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation | |
2780 | GetBuffer().Layout(dc, availableSpace, flags); | |
2781 | GetBuffer().SetDirty(false); | |
2782 | ||
2783 | if (!IsFrozen()) | |
2784 | SetupScrollbars(); | |
2785 | } | |
2786 | ||
2787 | return true; | |
2788 | } | |
2789 | ||
2790 | /// Is all of the selection bold? | |
2791 | bool wxRichTextCtrl::IsSelectionBold() | |
2792 | { | |
2793 | if (HasSelection()) | |
2794 | { | |
2795 | wxRichTextAttr attr; | |
2796 | wxRichTextRange range = GetSelectionRange(); | |
2797 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2798 | attr.SetFontWeight(wxBOLD); | |
2799 | ||
2800 | return HasCharacterAttributes(range, attr); | |
2801 | } | |
2802 | else | |
2803 | { | |
2804 | // If no selection, then we need to combine current style with default style | |
2805 | // to see what the effect would be if we started typing. | |
2806 | wxRichTextAttr attr; | |
2807 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2808 | ||
2809 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2810 | if (GetStyle(pos, attr)) | |
2811 | { | |
2812 | if (IsDefaultStyleShowing()) | |
2813 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2814 | return attr.GetFontWeight() == wxBOLD; | |
2815 | } | |
2816 | } | |
2817 | return false; | |
2818 | } | |
2819 | ||
2820 | /// Is all of the selection italics? | |
2821 | bool wxRichTextCtrl::IsSelectionItalics() | |
2822 | { | |
2823 | if (HasSelection()) | |
2824 | { | |
2825 | wxRichTextRange range = GetSelectionRange(); | |
2826 | wxRichTextAttr attr; | |
2827 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2828 | attr.SetFontStyle(wxITALIC); | |
2829 | ||
2830 | return HasCharacterAttributes(range, attr); | |
2831 | } | |
2832 | else | |
2833 | { | |
2834 | // If no selection, then we need to combine current style with default style | |
2835 | // to see what the effect would be if we started typing. | |
2836 | wxRichTextAttr attr; | |
2837 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2838 | ||
2839 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2840 | if (GetStyle(pos, attr)) | |
2841 | { | |
2842 | if (IsDefaultStyleShowing()) | |
2843 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2844 | return attr.GetFontStyle() == wxITALIC; | |
2845 | } | |
2846 | } | |
2847 | return false; | |
2848 | } | |
2849 | ||
2850 | /// Is all of the selection underlined? | |
2851 | bool wxRichTextCtrl::IsSelectionUnderlined() | |
2852 | { | |
2853 | if (HasSelection()) | |
2854 | { | |
2855 | wxRichTextRange range = GetSelectionRange(); | |
2856 | wxRichTextAttr attr; | |
2857 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2858 | attr.SetFontUnderlined(true); | |
2859 | ||
2860 | return HasCharacterAttributes(range, attr); | |
2861 | } | |
2862 | else | |
2863 | { | |
2864 | // If no selection, then we need to combine current style with default style | |
2865 | // to see what the effect would be if we started typing. | |
2866 | wxRichTextAttr attr; | |
2867 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2868 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2869 | ||
2870 | if (GetStyle(pos, attr)) | |
2871 | { | |
2872 | if (IsDefaultStyleShowing()) | |
2873 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
2874 | return attr.GetFontUnderlined(); | |
2875 | } | |
2876 | } | |
2877 | return false; | |
2878 | } | |
2879 | ||
2880 | /// Apply bold to the selection | |
2881 | bool wxRichTextCtrl::ApplyBoldToSelection() | |
2882 | { | |
2883 | wxRichTextAttr attr; | |
2884 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2885 | attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD); | |
2886 | ||
2887 | if (HasSelection()) | |
2888 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); | |
2889 | else | |
2890 | SetAndShowDefaultStyle(attr); | |
2891 | return true; | |
2892 | } | |
2893 | ||
2894 | /// Apply italic to the selection | |
2895 | bool wxRichTextCtrl::ApplyItalicToSelection() | |
2896 | { | |
2897 | wxRichTextAttr attr; | |
2898 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2899 | attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC); | |
2900 | ||
2901 | if (HasSelection()) | |
2902 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); | |
2903 | else | |
2904 | SetAndShowDefaultStyle(attr); | |
2905 | return true; | |
2906 | } | |
2907 | ||
2908 | /// Apply underline to the selection | |
2909 | bool wxRichTextCtrl::ApplyUnderlineToSelection() | |
2910 | { | |
2911 | wxRichTextAttr attr; | |
2912 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2913 | attr.SetFontUnderlined(!IsSelectionUnderlined()); | |
2914 | ||
2915 | if (HasSelection()) | |
2916 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); | |
2917 | else | |
2918 | SetAndShowDefaultStyle(attr); | |
2919 | return true; | |
2920 | } | |
2921 | ||
2922 | /// Is all of the selection aligned according to the specified flag? | |
2923 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) | |
2924 | { | |
2925 | wxRichTextRange range; | |
2926 | if (HasSelection()) | |
2927 | range = GetSelectionRange(); | |
2928 | else | |
2929 | range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); | |
2930 | ||
2931 | wxRichTextAttr attr; | |
2932 | attr.SetAlignment(alignment); | |
2933 | ||
2934 | return HasParagraphAttributes(range, attr); | |
2935 | } | |
2936 | ||
2937 | /// Apply alignment to the selection | |
2938 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) | |
2939 | { | |
2940 | wxRichTextAttr attr; | |
2941 | attr.SetAlignment(alignment); | |
2942 | if (HasSelection()) | |
2943 | return SetStyle(GetSelectionRange(), attr); | |
2944 | else | |
2945 | { | |
2946 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); | |
2947 | if (para) | |
2948 | return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY); | |
2949 | } | |
2950 | return true; | |
2951 | } | |
2952 | ||
2953 | /// Apply a named style to the selection | |
2954 | bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def) | |
2955 | { | |
2956 | // Flags are defined within each definition, so only certain | |
2957 | // attributes are applied. | |
2958 | wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle()); | |
2959 | ||
2960 | int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE; | |
2961 | ||
2962 | if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition))) | |
2963 | { | |
2964 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
2965 | ||
2966 | wxRichTextRange range; | |
2967 | ||
2968 | if (HasSelection()) | |
2969 | range = GetSelectionRange(); | |
2970 | else | |
2971 | { | |
2972 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2973 | range = wxRichTextRange(pos, pos+1); | |
2974 | } | |
2975 | ||
2976 | return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags); | |
2977 | } | |
2978 | ||
2979 | // Make sure the attr has the style name | |
2980 | if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition))) | |
2981 | { | |
2982 | attr.SetParagraphStyleName(def->GetName()); | |
2983 | ||
2984 | // If applying a paragraph style, we only want the paragraph nodes to adopt these | |
2985 | // attributes, and not the leaf nodes. This will allow the content (e.g. text) | |
2986 | // to change its style independently. | |
2987 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
2988 | } | |
2989 | else | |
2990 | attr.SetCharacterStyleName(def->GetName()); | |
2991 | ||
2992 | if (HasSelection()) | |
2993 | return SetStyleEx(GetSelectionRange(), attr, flags); | |
2994 | else | |
2995 | { | |
2996 | SetAndShowDefaultStyle(attr); | |
2997 | return true; | |
2998 | } | |
2999 | } | |
3000 | ||
3001 | /// Apply the style sheet to the buffer, for example if the styles have changed. | |
3002 | bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet) | |
3003 | { | |
3004 | if (!styleSheet) | |
3005 | styleSheet = GetBuffer().GetStyleSheet(); | |
3006 | if (!styleSheet) | |
3007 | return false; | |
3008 | ||
3009 | if (GetBuffer().ApplyStyleSheet(styleSheet)) | |
3010 | { | |
3011 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
3012 | Refresh(false); | |
3013 | return true; | |
3014 | } | |
3015 | else | |
3016 | return false; | |
3017 | } | |
3018 | ||
3019 | /// Sets the default style to the style under the cursor | |
3020 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() | |
3021 | { | |
3022 | wxTextAttrEx attr; | |
3023 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); | |
3024 | ||
3025 | // If at the start of a paragraph, use the next position. | |
3026 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3027 | ||
3028 | if (GetUncombinedStyle(pos, attr)) | |
3029 | { | |
3030 | SetDefaultStyle(attr); | |
3031 | return true; | |
3032 | } | |
3033 | ||
3034 | return false; | |
3035 | } | |
3036 | ||
3037 | /// Returns the first visible position in the current view | |
3038 | long wxRichTextCtrl::GetFirstVisiblePosition() const | |
3039 | { | |
3040 | wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y); | |
3041 | if (line) | |
3042 | return line->GetAbsoluteRange().GetStart(); | |
3043 | else | |
3044 | return 0; | |
3045 | } | |
3046 | ||
3047 | /// Get the first visible point in the window | |
3048 | wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const | |
3049 | { | |
3050 | int ppuX, ppuY; | |
3051 | int startXUnits, startYUnits; | |
3052 | ||
3053 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
3054 | GetViewStart(& startXUnits, & startYUnits); | |
3055 | ||
3056 | return wxPoint(startXUnits * ppuX, startYUnits * ppuY); | |
3057 | } | |
3058 | ||
3059 | /// The adjusted caret position is the character position adjusted to take | |
3060 | /// into account whether we're at the start of a paragraph, in which case | |
3061 | /// style information should be taken from the next position, not current one. | |
3062 | long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const | |
3063 | { | |
3064 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1); | |
3065 | ||
3066 | if (para && (caretPos+1 == para->GetRange().GetStart())) | |
3067 | caretPos ++; | |
3068 | return caretPos; | |
3069 | } | |
3070 | ||
3071 | /// Get/set the selection range in character positions. -1, -1 means no selection. | |
3072 | /// The range is in API convention, i.e. a single character selection is denoted | |
3073 | /// by (n, n+1) | |
3074 | wxRichTextRange wxRichTextCtrl::GetSelectionRange() const | |
3075 | { | |
3076 | wxRichTextRange range = GetInternalSelectionRange(); | |
3077 | if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1)) | |
3078 | range.SetEnd(range.GetEnd() + 1); | |
3079 | return range; | |
3080 | } | |
3081 | ||
3082 | void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range) | |
3083 | { | |
3084 | wxRichTextRange range1(range); | |
3085 | if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) ) | |
3086 | range1.SetEnd(range1.GetEnd() - 1); | |
3087 | ||
3088 | wxASSERT( range1.GetStart() > range1.GetEnd() ); | |
3089 | ||
3090 | SetInternalSelectionRange(range1); | |
3091 | } | |
3092 | ||
3093 | /// Set list style | |
3094 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
3095 | { | |
3096 | return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel); | |
3097 | } | |
3098 | ||
3099 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
3100 | { | |
3101 | return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel); | |
3102 | } | |
3103 | ||
3104 | /// Clear list for given range | |
3105 | bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags) | |
3106 | { | |
3107 | return GetBuffer().ClearListStyle(range.ToInternal(), flags); | |
3108 | } | |
3109 | ||
3110 | /// Number/renumber any list elements in the given range | |
3111 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
3112 | { | |
3113 | return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel); | |
3114 | } | |
3115 | ||
3116 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
3117 | { | |
3118 | return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel); | |
3119 | } | |
3120 | ||
3121 | /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 | |
3122 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel) | |
3123 | { | |
3124 | return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel); | |
3125 | } | |
3126 | ||
3127 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel) | |
3128 | { | |
3129 | return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel); | |
3130 | } | |
3131 | ||
3132 | /// Deletes the content in the given range | |
3133 | bool wxRichTextCtrl::Delete(const wxRichTextRange& range) | |
3134 | { | |
3135 | return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this); | |
3136 | } | |
3137 | ||
3138 | const wxArrayString& wxRichTextCtrl::GetAvailableFontNames() | |
3139 | { | |
3140 | if (sm_availableFontNames.GetCount() == 0) | |
3141 | { | |
3142 | sm_availableFontNames = wxFontEnumerator::GetFacenames(); | |
3143 | sm_availableFontNames.Sort(); | |
3144 | } | |
3145 | return sm_availableFontNames; | |
3146 | } | |
3147 | ||
3148 | void wxRichTextCtrl::ClearAvailableFontNames() | |
3149 | { | |
3150 | sm_availableFontNames.Clear(); | |
3151 | } | |
3152 | ||
3153 | #endif | |
3154 | // wxUSE_RICHTEXT |