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