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