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