]>
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()); | |
79f955ec | 154 | attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
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 | ||
79f955ec | 169 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
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 | 411 | wxMouseEvent mouseEvent(event); |
1a75935d | 412 | |
db6634e0 JS |
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 | } | |
1a75935d | 420 | |
db6634e0 JS |
421 | wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos); |
422 | InitCommandEvent(urlEvent); | |
1a75935d | 423 | |
db6634e0 | 424 | urlEvent.SetString(urlTarget); |
1a75935d | 425 | |
db6634e0 | 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); | |
1a75935d | 516 | |
db6634e0 | 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); | |
1a75935d | 528 | |
db6634e0 JS |
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); | |
1a75935d | 543 | |
db6634e0 JS |
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 | 1049 | if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN || |
ec36b155 | 1050 | keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT || |
3e3a754f JS |
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; | |
25d6eb3a | 2042 | else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER)) |
f262b25c | 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 | ||
1a75935d | 2067 | if (!value.IsEmpty()) |
5d7836c4 | 2068 | { |
1a75935d JS |
2069 | // Remove empty paragraph |
2070 | GetBuffer().Clear(); | |
343ae70d | 2071 | DoWriteText(value); |
5d7836c4 JS |
2072 | |
2073 | // for compatibility, don't move the cursor when doing SetValue() | |
2074 | SetInsertionPoint(0); | |
2075 | } | |
1a75935d | 2076 | else |
5d7836c4 | 2077 | { |
1a75935d JS |
2078 | // still send an event for consistency |
2079 | if (flags & SetValue_SendEvent) | |
343ae70d | 2080 | SendTextUpdatedEvent(); |
5d7836c4 | 2081 | } |
5d7836c4 JS |
2082 | DiscardEdits(); |
2083 | } | |
2084 | ||
2085 | void wxRichTextCtrl::WriteText(const wxString& value) | |
2086 | { | |
2087 | DoWriteText(value); | |
2088 | } | |
2089 | ||
343ae70d | 2090 | void wxRichTextCtrl::DoWriteText(const wxString& value, int flags) |
5d7836c4 | 2091 | { |
0ca07313 | 2092 | wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix); |
fe5aa22c | 2093 | |
33dddf58 | 2094 | GetBuffer().InsertTextWithUndo(m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
343ae70d VZ |
2095 | |
2096 | if ( flags & SetValue_SendEvent ) | |
2097 | SendTextUpdatedEvent(); | |
5d7836c4 JS |
2098 | } |
2099 | ||
2100 | void wxRichTextCtrl::AppendText(const wxString& text) | |
2101 | { | |
2102 | SetInsertionPointEnd(); | |
2103 | ||
2104 | WriteText(text); | |
2105 | } | |
2106 | ||
2107 | /// Write an image at the current insertion point | |
2108 | bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType) | |
2109 | { | |
2110 | wxRichTextImageBlock imageBlock; | |
2111 | ||
2112 | wxImage image2 = image; | |
2113 | if (imageBlock.MakeImageBlock(image2, bitmapType)) | |
2114 | return WriteImage(imageBlock); | |
41e155b4 WS |
2115 | |
2116 | return false; | |
5d7836c4 JS |
2117 | } |
2118 | ||
2119 | bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) | |
2120 | { | |
2121 | wxRichTextImageBlock imageBlock; | |
2122 | ||
2123 | wxImage image; | |
2124 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) | |
2125 | return WriteImage(imageBlock); | |
41e155b4 WS |
2126 | |
2127 | return false; | |
5d7836c4 JS |
2128 | } |
2129 | ||
2130 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock) | |
2131 | { | |
2132 | return GetBuffer().InsertImageWithUndo(m_caretPosition+1, imageBlock, this); | |
2133 | } | |
2134 | ||
2135 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType) | |
2136 | { | |
2137 | if (bitmap.Ok()) | |
2138 | { | |
2139 | wxRichTextImageBlock imageBlock; | |
7fe8059f | 2140 | |
5d7836c4 JS |
2141 | wxImage image = bitmap.ConvertToImage(); |
2142 | if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType)) | |
2143 | return WriteImage(imageBlock); | |
5d7836c4 | 2144 | } |
41e155b4 | 2145 | |
5d7836c4 JS |
2146 | return false; |
2147 | } | |
2148 | ||
2149 | /// Insert a newline (actually paragraph) at the current insertion point. | |
2150 | bool wxRichTextCtrl::Newline() | |
2151 | { | |
2152 | return GetBuffer().InsertNewlineWithUndo(m_caretPosition+1, this); | |
2153 | } | |
2154 | ||
ff76711f JS |
2155 | /// Insert a line break at the current insertion point. |
2156 | bool wxRichTextCtrl::LineBreak() | |
2157 | { | |
2158 | wxString text; | |
2159 | text = wxRichTextLineBreakChar; | |
2160 | return GetBuffer().InsertTextWithUndo(m_caretPosition+1, text, this); | |
2161 | } | |
5d7836c4 JS |
2162 | |
2163 | // ---------------------------------------------------------------------------- | |
2164 | // Clipboard operations | |
2165 | // ---------------------------------------------------------------------------- | |
2166 | ||
2167 | void wxRichTextCtrl::Copy() | |
2168 | { | |
2169 | if (CanCopy()) | |
2170 | { | |
fe5aa22c | 2171 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
2172 | GetBuffer().CopyToClipboard(range); |
2173 | } | |
2174 | } | |
2175 | ||
2176 | void wxRichTextCtrl::Cut() | |
2177 | { | |
2178 | if (CanCut()) | |
2179 | { | |
fe5aa22c | 2180 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
2181 | GetBuffer().CopyToClipboard(range); |
2182 | ||
2183 | DeleteSelectedContent(); | |
2f36e8dc | 2184 | LayoutContent(); |
76bcd815 | 2185 | Refresh(false); |
5d7836c4 JS |
2186 | } |
2187 | } | |
2188 | ||
2189 | void wxRichTextCtrl::Paste() | |
2190 | { | |
2191 | if (CanPaste()) | |
2192 | { | |
2193 | BeginBatchUndo(_("Paste")); | |
7fe8059f | 2194 | |
5d7836c4 JS |
2195 | long newPos = m_caretPosition; |
2196 | DeleteSelectedContent(& newPos); | |
7fe8059f | 2197 | |
5d7836c4 JS |
2198 | GetBuffer().PasteFromClipboard(newPos); |
2199 | ||
2200 | EndBatchUndo(); | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | void wxRichTextCtrl::DeleteSelection() | |
2205 | { | |
2206 | if (CanDeleteSelection()) | |
2207 | { | |
2208 | DeleteSelectedContent(); | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | bool wxRichTextCtrl::HasSelection() const | |
2213 | { | |
2214 | return m_selectionRange.GetStart() != -2 && m_selectionRange.GetEnd() != -2; | |
2215 | } | |
2216 | ||
2217 | bool wxRichTextCtrl::CanCopy() const | |
2218 | { | |
2219 | // Can copy if there's a selection | |
2220 | return HasSelection(); | |
2221 | } | |
2222 | ||
2223 | bool wxRichTextCtrl::CanCut() const | |
2224 | { | |
2225 | return HasSelection() && IsEditable(); | |
2226 | } | |
2227 | ||
2228 | bool wxRichTextCtrl::CanPaste() const | |
2229 | { | |
2230 | if ( !IsEditable() ) | |
2231 | return false; | |
2232 | ||
2233 | return GetBuffer().CanPasteFromClipboard(); | |
2234 | } | |
2235 | ||
2236 | bool wxRichTextCtrl::CanDeleteSelection() const | |
2237 | { | |
2238 | return HasSelection() && IsEditable(); | |
2239 | } | |
2240 | ||
2241 | ||
2242 | // ---------------------------------------------------------------------------- | |
2243 | // Accessors | |
2244 | // ---------------------------------------------------------------------------- | |
2245 | ||
2246 | void wxRichTextCtrl::SetEditable(bool editable) | |
2247 | { | |
2248 | m_editable = editable; | |
2249 | } | |
2250 | ||
2251 | void wxRichTextCtrl::SetInsertionPoint(long pos) | |
2252 | { | |
2253 | SelectNone(); | |
2254 | ||
2255 | m_caretPosition = pos - 1; | |
2256 | } | |
2257 | ||
2258 | void wxRichTextCtrl::SetInsertionPointEnd() | |
2259 | { | |
2260 | long pos = GetLastPosition(); | |
2261 | SetInsertionPoint(pos); | |
2262 | } | |
2263 | ||
2264 | long wxRichTextCtrl::GetInsertionPoint() const | |
2265 | { | |
2266 | return m_caretPosition+1; | |
2267 | } | |
2268 | ||
2269 | wxTextPos wxRichTextCtrl::GetLastPosition() const | |
2270 | { | |
2271 | return GetBuffer().GetRange().GetEnd(); | |
2272 | } | |
2273 | ||
2274 | // If the return values from and to are the same, there is no | |
2275 | // selection. | |
2276 | void wxRichTextCtrl::GetSelection(long* from, long* to) const | |
2277 | { | |
2278 | *from = m_selectionRange.GetStart(); | |
2279 | *to = m_selectionRange.GetEnd(); | |
96c9f0f6 JS |
2280 | if ((*to) != -1 && (*to) != -2) |
2281 | (*to) ++; | |
5d7836c4 JS |
2282 | } |
2283 | ||
2284 | bool wxRichTextCtrl::IsEditable() const | |
2285 | { | |
2286 | return m_editable; | |
2287 | } | |
2288 | ||
2289 | // ---------------------------------------------------------------------------- | |
2290 | // selection | |
2291 | // ---------------------------------------------------------------------------- | |
2292 | ||
2293 | void wxRichTextCtrl::SetSelection(long from, long to) | |
2294 | { | |
2295 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
2296 | // be selected. | |
2297 | if ( (from == -1) && (to == -1) ) | |
2298 | { | |
2299 | from = 0; | |
96c9f0f6 | 2300 | to = GetLastPosition()+1; |
5d7836c4 JS |
2301 | } |
2302 | ||
2303 | DoSetSelection(from, to); | |
2304 | } | |
2305 | ||
2306 | void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCaret)) | |
2307 | { | |
2308 | m_selectionAnchor = from; | |
96c9f0f6 | 2309 | m_selectionRange.SetRange(from, to-1); |
ff76711f JS |
2310 | if (from > -2) |
2311 | m_caretPosition = from-1; | |
0ca07313 | 2312 | |
76bcd815 | 2313 | Refresh(false); |
5d7836c4 JS |
2314 | PositionCaret(); |
2315 | } | |
2316 | ||
2317 | // ---------------------------------------------------------------------------- | |
2318 | // Editing | |
2319 | // ---------------------------------------------------------------------------- | |
2320 | ||
343ae70d VZ |
2321 | void wxRichTextCtrl::Replace(long WXUNUSED(from), long WXUNUSED(to), |
2322 | const wxString& value) | |
5d7836c4 JS |
2323 | { |
2324 | BeginBatchUndo(_("Replace")); | |
2325 | ||
2326 | DeleteSelectedContent(); | |
2327 | ||
343ae70d | 2328 | DoWriteText(value, SetValue_SelectionOnly); |
5d7836c4 JS |
2329 | |
2330 | EndBatchUndo(); | |
2331 | } | |
2332 | ||
2333 | void wxRichTextCtrl::Remove(long from, long to) | |
2334 | { | |
2335 | SelectNone(); | |
2336 | ||
f52eff54 | 2337 | GetBuffer().DeleteRangeWithUndo(wxRichTextRange(from, to), this); |
5d7836c4 | 2338 | |
2f36e8dc | 2339 | LayoutContent(); |
5d7836c4 | 2340 | if (!IsFrozen()) |
76bcd815 | 2341 | Refresh(false); |
5d7836c4 JS |
2342 | } |
2343 | ||
2344 | bool wxRichTextCtrl::IsModified() const | |
2345 | { | |
2346 | return m_buffer.IsModified(); | |
2347 | } | |
2348 | ||
2349 | void wxRichTextCtrl::MarkDirty() | |
2350 | { | |
2351 | m_buffer.Modify(true); | |
2352 | } | |
2353 | ||
2354 | void wxRichTextCtrl::DiscardEdits() | |
2355 | { | |
ab14c7aa | 2356 | m_caretPositionForDefaultStyle = -2; |
5d7836c4 JS |
2357 | m_buffer.Modify(false); |
2358 | m_buffer.GetCommandProcessor()->ClearCommands(); | |
2359 | } | |
2360 | ||
2361 | int wxRichTextCtrl::GetNumberOfLines() const | |
2362 | { | |
2363 | return GetBuffer().GetParagraphCount(); | |
2364 | } | |
2365 | ||
2366 | // ---------------------------------------------------------------------------- | |
2367 | // Positions <-> coords | |
2368 | // ---------------------------------------------------------------------------- | |
2369 | ||
2370 | long wxRichTextCtrl::XYToPosition(long x, long y) const | |
2371 | { | |
2372 | return GetBuffer().XYToPosition(x, y); | |
2373 | } | |
2374 | ||
2375 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
2376 | { | |
2377 | return GetBuffer().PositionToXY(pos, x, y); | |
2378 | } | |
2379 | ||
2380 | // ---------------------------------------------------------------------------- | |
2381 | // | |
2382 | // ---------------------------------------------------------------------------- | |
2383 | ||
2384 | void wxRichTextCtrl::ShowPosition(long pos) | |
2385 | { | |
2386 | if (!IsPositionVisible(pos)) | |
2387 | ScrollIntoView(pos-1, WXK_DOWN); | |
2388 | } | |
2389 | ||
2390 | int wxRichTextCtrl::GetLineLength(long lineNo) const | |
2391 | { | |
2392 | return GetBuffer().GetParagraphLength(lineNo); | |
2393 | } | |
2394 | ||
2395 | wxString wxRichTextCtrl::GetLineText(long lineNo) const | |
2396 | { | |
2397 | return GetBuffer().GetParagraphText(lineNo); | |
2398 | } | |
2399 | ||
2400 | // ---------------------------------------------------------------------------- | |
2401 | // Undo/redo | |
2402 | // ---------------------------------------------------------------------------- | |
2403 | ||
2404 | void wxRichTextCtrl::Undo() | |
2405 | { | |
2406 | if (CanUndo()) | |
2407 | { | |
2408 | GetCommandProcessor()->Undo(); | |
2409 | } | |
2410 | } | |
2411 | ||
2412 | void wxRichTextCtrl::Redo() | |
2413 | { | |
2414 | if (CanRedo()) | |
2415 | { | |
2416 | GetCommandProcessor()->Redo(); | |
2417 | } | |
2418 | } | |
2419 | ||
2420 | bool wxRichTextCtrl::CanUndo() const | |
2421 | { | |
2422 | return GetCommandProcessor()->CanUndo(); | |
2423 | } | |
2424 | ||
2425 | bool wxRichTextCtrl::CanRedo() const | |
2426 | { | |
2427 | return GetCommandProcessor()->CanRedo(); | |
2428 | } | |
2429 | ||
2430 | // ---------------------------------------------------------------------------- | |
563d25ba | 2431 | // implementation details |
5d7836c4 JS |
2432 | // ---------------------------------------------------------------------------- |
2433 | ||
563d25ba | 2434 | void wxRichTextCtrl::Command(wxCommandEvent& event) |
5d7836c4 JS |
2435 | { |
2436 | SetValue(event.GetString()); | |
2437 | GetEventHandler()->ProcessEvent(event); | |
2438 | } | |
2439 | ||
2440 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
2441 | { | |
2442 | // By default, load the first file into the text window. | |
2443 | if (event.GetNumberOfFiles() > 0) | |
2444 | { | |
2445 | LoadFile(event.GetFiles()[0]); | |
2446 | } | |
2447 | } | |
2448 | ||
5d7836c4 JS |
2449 | wxSize wxRichTextCtrl::DoGetBestSize() const |
2450 | { | |
2451 | return wxSize(10, 10); | |
2452 | } | |
2453 | ||
2454 | // ---------------------------------------------------------------------------- | |
2455 | // standard handlers for standard edit menu events | |
2456 | // ---------------------------------------------------------------------------- | |
2457 | ||
2458 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
2459 | { | |
2460 | Cut(); | |
2461 | } | |
2462 | ||
2463 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) | |
2464 | { | |
2465 | DeleteSelection(); | |
2466 | } | |
2467 | ||
2468 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
2469 | { | |
2470 | Copy(); | |
2471 | } | |
2472 | ||
2473 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
2474 | { | |
2475 | Paste(); | |
2476 | } | |
2477 | ||
2478 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
2479 | { | |
2480 | Undo(); | |
2481 | } | |
2482 | ||
2483 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
2484 | { | |
2485 | Redo(); | |
2486 | } | |
2487 | ||
2488 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
2489 | { | |
2490 | event.Enable( CanCut() ); | |
2491 | } | |
2492 | ||
2493 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
2494 | { | |
2495 | event.Enable( CanCopy() ); | |
2496 | } | |
2497 | ||
2498 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) | |
2499 | { | |
2500 | event.Enable( CanDeleteSelection() ); | |
2501 | } | |
2502 | ||
2503 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
2504 | { | |
2505 | event.Enable( CanPaste() ); | |
2506 | } | |
2507 | ||
2508 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
2509 | { | |
2510 | event.Enable( CanUndo() ); | |
2511 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); | |
2512 | } | |
2513 | ||
2514 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
2515 | { | |
2516 | event.Enable( CanRedo() ); | |
2517 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); | |
2518 | } | |
2519 | ||
2520 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
2521 | { | |
2522 | SelectAll(); | |
2523 | } | |
2524 | ||
2525 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
2526 | { | |
2527 | event.Enable(GetLastPosition() > 0); | |
2528 | } | |
2529 | ||
b4ae0def | 2530 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event) |
5d7836c4 | 2531 | { |
b4ae0def JS |
2532 | if (event.GetEventObject() != this) |
2533 | { | |
2534 | event.Skip(); | |
2535 | return; | |
2536 | } | |
2537 | ||
5d7836c4 JS |
2538 | if (!m_contextMenu) |
2539 | { | |
2540 | m_contextMenu = new wxMenu; | |
2541 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); | |
2542 | m_contextMenu->Append(wxID_REDO, _("&Redo")); | |
2543 | m_contextMenu->AppendSeparator(); | |
2544 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); | |
2545 | m_contextMenu->Append(wxID_COPY, _("&Copy")); | |
2546 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); | |
2547 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); | |
2548 | m_contextMenu->AppendSeparator(); | |
2549 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
2550 | } | |
2551 | PopupMenu(m_contextMenu); | |
2552 | return; | |
2553 | } | |
2554 | ||
2555 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) | |
2556 | { | |
96c9f0f6 | 2557 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style); |
5d7836c4 JS |
2558 | } |
2559 | ||
27e20452 JS |
2560 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) |
2561 | { | |
96c9f0f6 | 2562 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), wxTextAttrEx(style)); |
27e20452 JS |
2563 | } |
2564 | ||
5d7836c4 JS |
2565 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) |
2566 | { | |
96c9f0f6 | 2567 | return GetBuffer().SetStyle(range.ToInternal(), style); |
5d7836c4 JS |
2568 | } |
2569 | ||
3e3a754f JS |
2570 | // extended style setting operation with flags including: |
2571 | // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. | |
2572 | // see richtextbuffer.h for more details. | |
2573 | bool wxRichTextCtrl::SetStyleEx(long start, long end, const wxTextAttrEx& style, int flags) | |
2574 | { | |
2575 | return GetBuffer().SetStyle(wxRichTextRange(start, end-1), style, flags); | |
2576 | } | |
2577 | ||
2578 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxTextAttrEx& style, int flags) | |
2579 | { | |
2580 | return GetBuffer().SetStyle(range.ToInternal(), style, flags); | |
2581 | } | |
2582 | ||
2583 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags) | |
2584 | { | |
2585 | return GetBuffer().SetStyle(range.ToInternal(), style, flags); | |
2586 | } | |
2587 | ||
5d7836c4 JS |
2588 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) |
2589 | { | |
2590 | return GetBuffer().SetDefaultStyle(style); | |
2591 | } | |
2592 | ||
27e20452 JS |
2593 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
2594 | { | |
2595 | return GetBuffer().SetDefaultStyle(wxTextAttrEx(style)); | |
2596 | } | |
2597 | ||
5d7836c4 JS |
2598 | const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const |
2599 | { | |
2600 | return GetBuffer().GetDefaultStyle(); | |
2601 | } | |
2602 | ||
27e20452 JS |
2603 | const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const |
2604 | { | |
61399247 | 2605 | return GetBuffer().GetDefaultStyle(); |
27e20452 JS |
2606 | } |
2607 | ||
3966a9f4 | 2608 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style) |
27e20452 | 2609 | { |
fe5aa22c | 2610 | wxTextAttrEx attr(style); |
27e20452 JS |
2611 | if (GetBuffer().GetStyle(position, attr)) |
2612 | { | |
2613 | style = attr; | |
2614 | return true; | |
2615 | } | |
2616 | else | |
2617 | return false; | |
2618 | } | |
2619 | ||
3966a9f4 | 2620 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) |
5d7836c4 JS |
2621 | { |
2622 | return GetBuffer().GetStyle(position, style); | |
2623 | } | |
2624 | ||
3966a9f4 | 2625 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) |
5d7836c4 JS |
2626 | { |
2627 | return GetBuffer().GetStyle(position, style); | |
2628 | } | |
2629 | ||
a7ed48a5 JS |
2630 | // get the common set of styles for the range |
2631 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style) | |
2632 | { | |
2633 | wxTextAttrEx styleEx; | |
2634 | if (GetBuffer().GetStyleForRange(range.ToInternal(), styleEx)) | |
2635 | { | |
2636 | style = styleEx; | |
2637 | return true; | |
2638 | } | |
2639 | else | |
2640 | return false; | |
2641 | } | |
2642 | ||
2643 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttrEx& style) | |
2644 | { | |
2645 | return GetBuffer().GetStyleForRange(range.ToInternal(), style); | |
2646 | } | |
2647 | ||
fe5aa22c JS |
2648 | /// Get the content (uncombined) attributes for this position. |
2649 | ||
2650 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttr& style) | |
2651 | { | |
2652 | wxTextAttrEx attr(style); | |
2653 | if (GetBuffer().GetUncombinedStyle(position, attr)) | |
2654 | { | |
2655 | style = attr; | |
2656 | return true; | |
2657 | } | |
2658 | else | |
2659 | return false; | |
2660 | } | |
2661 | ||
2662 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxTextAttrEx& style) | |
2663 | { | |
2664 | return GetBuffer().GetUncombinedStyle(position, style); | |
2665 | } | |
2666 | ||
2667 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style) | |
2668 | { | |
2669 | return GetBuffer().GetUncombinedStyle(position, style); | |
2670 | } | |
2671 | ||
5d7836c4 JS |
2672 | /// Set font, and also the buffer attributes |
2673 | bool wxRichTextCtrl::SetFont(const wxFont& font) | |
2674 | { | |
5d7836c4 | 2675 | wxControl::SetFont(font); |
5d7836c4 JS |
2676 | |
2677 | wxTextAttrEx attr = GetBuffer().GetAttributes(); | |
2678 | attr.SetFont(font); | |
2679 | GetBuffer().SetBasicStyle(attr); | |
fe5aa22c | 2680 | |
fe5aa22c JS |
2681 | GetBuffer().Invalidate(wxRICHTEXT_ALL); |
2682 | Refresh(false); | |
5d7836c4 JS |
2683 | |
2684 | return true; | |
2685 | } | |
2686 | ||
4d551ad5 JS |
2687 | /// Transform logical to physical |
2688 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const | |
5d7836c4 JS |
2689 | { |
2690 | wxPoint pt; | |
2691 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); | |
2692 | ||
2693 | return pt; | |
2694 | } | |
2695 | ||
2696 | /// Transform physical to logical | |
4d551ad5 | 2697 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const |
5d7836c4 JS |
2698 | { |
2699 | wxPoint pt; | |
2700 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); | |
2701 | ||
2702 | return pt; | |
2703 | } | |
2704 | ||
2705 | /// Position the caret | |
2706 | void wxRichTextCtrl::PositionCaret() | |
2707 | { | |
c59f6793 JS |
2708 | if (!GetCaret()) |
2709 | return; | |
2710 | ||
2711 | //wxLogDebug(wxT("PositionCaret")); | |
2712 | ||
5d7836c4 JS |
2713 | wxRect caretRect; |
2714 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect)) | |
2715 | { | |
7e4fe193 JS |
2716 | wxPoint newPt = caretRect.GetPosition(); |
2717 | wxSize newSz = caretRect.GetSize(); | |
2718 | wxPoint pt = GetPhysicalPoint(newPt); | |
2719 | if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz) | |
c59f6793 JS |
2720 | { |
2721 | GetCaret()->Move(pt); | |
7e4fe193 | 2722 | GetCaret()->SetSize(newSz); |
c59f6793 | 2723 | } |
5d7836c4 JS |
2724 | } |
2725 | } | |
2726 | ||
2727 | /// Get the caret height and position for the given character position | |
2728 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect) | |
2729 | { | |
2730 | wxClientDC dc(this); | |
2731 | dc.SetFont(GetFont()); | |
7fe8059f | 2732 | |
5d7836c4 JS |
2733 | PrepareDC(dc); |
2734 | ||
2735 | wxPoint pt; | |
2736 | int height = 0; | |
2737 | ||
2738 | if (GetBuffer().FindPosition(dc, position, pt, & height, m_caretAtLineStart)) | |
2739 | { | |
88bee7e6 JS |
2740 | // Caret height can't be zero |
2741 | if (height == 0) | |
2742 | height = dc.GetCharHeight(); | |
2743 | ||
5d7836c4 JS |
2744 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); |
2745 | return true; | |
2746 | } | |
41e155b4 WS |
2747 | |
2748 | return false; | |
5d7836c4 JS |
2749 | } |
2750 | ||
2751 | /// Gets the line for the visible caret position. If the caret is | |
2752 | /// shown at the very end of the line, it means the next character is actually | |
2753 | /// on the following line. So let's get the line we're expecting to find | |
2754 | /// if this is the case. | |
2755 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const | |
2756 | { | |
2757 | wxRichTextLine* line = GetBuffer().GetLineAtPosition(caretPosition, true); | |
2758 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPosition, true); | |
2759 | if (line) | |
2760 | { | |
1e967276 JS |
2761 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2762 | if (caretPosition == lineRange.GetStart()-1 && | |
2763 | (para->GetRange().GetStart() != lineRange.GetStart())) | |
5d7836c4 JS |
2764 | { |
2765 | if (!m_caretAtLineStart) | |
2766 | line = GetBuffer().GetLineAtPosition(caretPosition-1, true); | |
2767 | } | |
2768 | } | |
2769 | return line; | |
2770 | } | |
2771 | ||
2772 | ||
2773 | /// Move the caret to the given character position | |
2774 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) | |
2775 | { | |
2776 | if (GetBuffer().GetDirty()) | |
2f36e8dc | 2777 | LayoutContent(); |
5d7836c4 JS |
2778 | |
2779 | if (pos <= GetBuffer().GetRange().GetEnd()) | |
2780 | { | |
2781 | SetCaretPosition(pos, showAtLineStart); | |
2782 | ||
2783 | PositionCaret(); | |
2784 | ||
2785 | return true; | |
2786 | } | |
2787 | else | |
2788 | return false; | |
2789 | } | |
2790 | ||
2791 | /// Layout the buffer: which we must do before certain operations, such as | |
2792 | /// setting the caret position. | |
2f36e8dc | 2793 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) |
5d7836c4 | 2794 | { |
4d551ad5 | 2795 | if (GetBuffer().GetDirty() || onlyVisibleRect) |
1e967276 JS |
2796 | { |
2797 | wxRect availableSpace(GetClientSize()); | |
2798 | if (availableSpace.width == 0) | |
2799 | availableSpace.width = 10; | |
2800 | if (availableSpace.height == 0) | |
2801 | availableSpace.height = 10; | |
4d551ad5 JS |
2802 | |
2803 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; | |
2804 | if (onlyVisibleRect) | |
2805 | { | |
2806 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; | |
2807 | availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); | |
2808 | } | |
41e155b4 | 2809 | |
1e967276 JS |
2810 | wxClientDC dc(this); |
2811 | dc.SetFont(GetFont()); | |
41e155b4 | 2812 | |
1e967276 | 2813 | PrepareDC(dc); |
41e155b4 | 2814 | |
1e967276 JS |
2815 | GetBuffer().Defragment(); |
2816 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation | |
4d551ad5 | 2817 | GetBuffer().Layout(dc, availableSpace, flags); |
1e967276 | 2818 | GetBuffer().SetDirty(false); |
41e155b4 | 2819 | |
1e967276 JS |
2820 | if (!IsFrozen()) |
2821 | SetupScrollbars(); | |
2822 | } | |
5d7836c4 JS |
2823 | |
2824 | return true; | |
2825 | } | |
2826 | ||
2827 | /// Is all of the selection bold? | |
3966a9f4 | 2828 | bool wxRichTextCtrl::IsSelectionBold() |
5d7836c4 JS |
2829 | { |
2830 | if (HasSelection()) | |
2831 | { | |
2832 | wxRichTextAttr attr; | |
a7ed48a5 | 2833 | wxRichTextRange range = GetSelectionRange(); |
5d7836c4 JS |
2834 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
2835 | attr.SetFontWeight(wxBOLD); | |
7fe8059f | 2836 | |
5d7836c4 JS |
2837 | return HasCharacterAttributes(range, attr); |
2838 | } | |
2839 | else | |
2840 | { | |
2841 | // If no selection, then we need to combine current style with default style | |
2842 | // to see what the effect would be if we started typing. | |
2843 | wxRichTextAttr attr; | |
2844 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
ab14c7aa JS |
2845 | |
2846 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2847 | if (GetStyle(pos, attr)) | |
5d7836c4 | 2848 | { |
ab14c7aa JS |
2849 | if (IsDefaultStyleShowing()) |
2850 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
5d7836c4 JS |
2851 | return attr.GetFontWeight() == wxBOLD; |
2852 | } | |
2853 | } | |
2854 | return false; | |
2855 | } | |
2856 | ||
2857 | /// Is all of the selection italics? | |
3966a9f4 | 2858 | bool wxRichTextCtrl::IsSelectionItalics() |
5d7836c4 JS |
2859 | { |
2860 | if (HasSelection()) | |
2861 | { | |
a7ed48a5 | 2862 | wxRichTextRange range = GetSelectionRange(); |
5d7836c4 JS |
2863 | wxRichTextAttr attr; |
2864 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2865 | attr.SetFontStyle(wxITALIC); | |
7fe8059f | 2866 | |
5d7836c4 JS |
2867 | return HasCharacterAttributes(range, attr); |
2868 | } | |
2869 | else | |
2870 | { | |
2871 | // If no selection, then we need to combine current style with default style | |
2872 | // to see what the effect would be if we started typing. | |
2873 | wxRichTextAttr attr; | |
2874 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
ab14c7aa JS |
2875 | |
2876 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
2877 | if (GetStyle(pos, attr)) | |
5d7836c4 | 2878 | { |
ab14c7aa JS |
2879 | if (IsDefaultStyleShowing()) |
2880 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
5d7836c4 JS |
2881 | return attr.GetFontStyle() == wxITALIC; |
2882 | } | |
2883 | } | |
2884 | return false; | |
2885 | } | |
2886 | ||
2887 | /// Is all of the selection underlined? | |
3966a9f4 | 2888 | bool wxRichTextCtrl::IsSelectionUnderlined() |
5d7836c4 JS |
2889 | { |
2890 | if (HasSelection()) | |
2891 | { | |
a7ed48a5 | 2892 | wxRichTextRange range = GetSelectionRange(); |
5d7836c4 JS |
2893 | wxRichTextAttr attr; |
2894 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
2895 | attr.SetFontUnderlined(true); | |
7fe8059f | 2896 | |
5d7836c4 JS |
2897 | return HasCharacterAttributes(range, attr); |
2898 | } | |
2899 | else | |
2900 | { | |
2901 | // If no selection, then we need to combine current style with default style | |
2902 | // to see what the effect would be if we started typing. | |
2903 | wxRichTextAttr attr; | |
2904 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
ab14c7aa | 2905 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
fe5aa22c | 2906 | |
ab14c7aa | 2907 | if (GetStyle(pos, attr)) |
5d7836c4 | 2908 | { |
ab14c7aa JS |
2909 | if (IsDefaultStyleShowing()) |
2910 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
5d7836c4 JS |
2911 | return attr.GetFontUnderlined(); |
2912 | } | |
2913 | } | |
2914 | return false; | |
2915 | } | |
2916 | ||
2917 | /// Apply bold to the selection | |
2918 | bool wxRichTextCtrl::ApplyBoldToSelection() | |
2919 | { | |
2920 | wxRichTextAttr attr; | |
2921 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); | |
2922 | attr.SetFontWeight(IsSelectionBold() ? wxNORMAL : wxBOLD); | |
2923 | ||
2924 | if (HasSelection()) | |
3e3a754f | 2925 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 2926 | else |
ab14c7aa | 2927 | SetAndShowDefaultStyle(attr); |
5d7836c4 JS |
2928 | return true; |
2929 | } | |
2930 | ||
2931 | /// Apply italic to the selection | |
2932 | bool wxRichTextCtrl::ApplyItalicToSelection() | |
2933 | { | |
2934 | wxRichTextAttr attr; | |
2935 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); | |
2936 | attr.SetFontStyle(IsSelectionItalics() ? wxNORMAL : wxITALIC); | |
2937 | ||
2938 | if (HasSelection()) | |
3e3a754f | 2939 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 2940 | else |
ab14c7aa | 2941 | SetAndShowDefaultStyle(attr); |
5d7836c4 JS |
2942 | return true; |
2943 | } | |
2944 | ||
2945 | /// Apply underline to the selection | |
2946 | bool wxRichTextCtrl::ApplyUnderlineToSelection() | |
2947 | { | |
2948 | wxRichTextAttr attr; | |
2949 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); | |
4d551ad5 | 2950 | attr.SetFontUnderlined(!IsSelectionUnderlined()); |
5d7836c4 JS |
2951 | |
2952 | if (HasSelection()) | |
3e3a754f | 2953 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 2954 | else |
ab14c7aa | 2955 | SetAndShowDefaultStyle(attr); |
5d7836c4 JS |
2956 | return true; |
2957 | } | |
2958 | ||
2959 | /// Is all of the selection aligned according to the specified flag? | |
3966a9f4 | 2960 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) |
5d7836c4 | 2961 | { |
0ca07313 | 2962 | wxRichTextRange range; |
5d7836c4 | 2963 | if (HasSelection()) |
a7ed48a5 | 2964 | range = GetSelectionRange(); |
5d7836c4 | 2965 | else |
a7ed48a5 | 2966 | range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); |
0ca07313 JS |
2967 | |
2968 | wxRichTextAttr attr; | |
2969 | attr.SetAlignment(alignment); | |
2970 | ||
2971 | return HasParagraphAttributes(range, attr); | |
5d7836c4 JS |
2972 | } |
2973 | ||
2974 | /// Apply alignment to the selection | |
2975 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) | |
2976 | { | |
2977 | wxRichTextAttr attr; | |
2978 | attr.SetAlignment(alignment); | |
2979 | if (HasSelection()) | |
2980 | return SetStyle(GetSelectionRange(), attr); | |
2981 | else | |
2982 | { | |
2983 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(GetCaretPosition()+1); | |
2984 | if (para) | |
3e3a754f | 2985 | return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY); |
5d7836c4 JS |
2986 | } |
2987 | return true; | |
2988 | } | |
2989 | ||
ab14c7aa | 2990 | /// Apply a named style to the selection |
1807a1f3 | 2991 | bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def) |
ab14c7aa JS |
2992 | { |
2993 | // Flags are defined within each definition, so only certain | |
2994 | // attributes are applied. | |
336d8ae9 | 2995 | wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle()); |
1807a1f3 | 2996 | |
3e3a754f | 2997 | int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE; |
ab14c7aa | 2998 | |
38f833b1 JS |
2999 | if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition))) |
3000 | { | |
3001 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
41a85215 | 3002 | |
38f833b1 | 3003 | wxRichTextRange range; |
41a85215 | 3004 | |
38f833b1 JS |
3005 | if (HasSelection()) |
3006 | range = GetSelectionRange(); | |
3007 | else | |
3008 | { | |
41a85215 | 3009 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
38f833b1 JS |
3010 | range = wxRichTextRange(pos, pos+1); |
3011 | } | |
41a85215 WS |
3012 | |
3013 | return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags); | |
3014 | } | |
3015 | ||
ab14c7aa JS |
3016 | // Make sure the attr has the style name |
3017 | if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition))) | |
3e3a754f | 3018 | { |
ab14c7aa | 3019 | attr.SetParagraphStyleName(def->GetName()); |
1807a1f3 | 3020 | |
3e3a754f | 3021 | // If applying a paragraph style, we only want the paragraph nodes to adopt these |
38f833b1 | 3022 | // attributes, and not the leaf nodes. This will allow the content (e.g. text) |
3e3a754f JS |
3023 | // to change its style independently. |
3024 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
3025 | } | |
ab14c7aa JS |
3026 | else |
3027 | attr.SetCharacterStyleName(def->GetName()); | |
3028 | ||
3029 | if (HasSelection()) | |
1807a1f3 | 3030 | return SetStyleEx(GetSelectionRange(), attr, flags); |
ab14c7aa | 3031 | else |
1807a1f3 | 3032 | { |
ab14c7aa | 3033 | SetAndShowDefaultStyle(attr); |
1807a1f3 JS |
3034 | return true; |
3035 | } | |
ab14c7aa JS |
3036 | } |
3037 | ||
fe5aa22c JS |
3038 | /// Apply the style sheet to the buffer, for example if the styles have changed. |
3039 | bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet) | |
3040 | { | |
3041 | if (!styleSheet) | |
3042 | styleSheet = GetBuffer().GetStyleSheet(); | |
3043 | if (!styleSheet) | |
3044 | return false; | |
3045 | ||
3046 | if (GetBuffer().ApplyStyleSheet(styleSheet)) | |
3047 | { | |
3048 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
3049 | Refresh(false); | |
3050 | return true; | |
3051 | } | |
3052 | else | |
3053 | return false; | |
3054 | } | |
3055 | ||
5d7836c4 JS |
3056 | /// Sets the default style to the style under the cursor |
3057 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() | |
3058 | { | |
3059 | wxTextAttrEx attr; | |
3060 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); | |
3061 | ||
ab14c7aa JS |
3062 | // If at the start of a paragraph, use the next position. |
3063 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3064 | ||
fe5aa22c | 3065 | if (GetUncombinedStyle(pos, attr)) |
5d7836c4 JS |
3066 | { |
3067 | SetDefaultStyle(attr); | |
3068 | return true; | |
3069 | } | |
41e155b4 WS |
3070 | |
3071 | return false; | |
5d7836c4 JS |
3072 | } |
3073 | ||
4d551ad5 JS |
3074 | /// Returns the first visible position in the current view |
3075 | long wxRichTextCtrl::GetFirstVisiblePosition() const | |
3076 | { | |
3077 | wxRichTextLine* line = GetBuffer().GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y); | |
3078 | if (line) | |
3079 | return line->GetAbsoluteRange().GetStart(); | |
3080 | else | |
3081 | return 0; | |
3082 | } | |
3083 | ||
ea160b2e JS |
3084 | /// Get the first visible point in the window |
3085 | wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const | |
3086 | { | |
3087 | int ppuX, ppuY; | |
3088 | int startXUnits, startYUnits; | |
3089 | ||
3090 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
3091 | GetViewStart(& startXUnits, & startYUnits); | |
3092 | ||
3093 | return wxPoint(startXUnits * ppuX, startYUnits * ppuY); | |
3094 | } | |
3095 | ||
ab14c7aa JS |
3096 | /// The adjusted caret position is the character position adjusted to take |
3097 | /// into account whether we're at the start of a paragraph, in which case | |
3098 | /// style information should be taken from the next position, not current one. | |
3099 | long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const | |
3100 | { | |
3101 | wxRichTextParagraph* para = GetBuffer().GetParagraphAtPosition(caretPos+1); | |
3102 | ||
3103 | if (para && (caretPos+1 == para->GetRange().GetStart())) | |
3104 | caretPos ++; | |
3105 | return caretPos; | |
3106 | } | |
3107 | ||
96c9f0f6 JS |
3108 | /// Get/set the selection range in character positions. -1, -1 means no selection. |
3109 | /// The range is in API convention, i.e. a single character selection is denoted | |
3110 | /// by (n, n+1) | |
3111 | wxRichTextRange wxRichTextCtrl::GetSelectionRange() const | |
3112 | { | |
3113 | wxRichTextRange range = GetInternalSelectionRange(); | |
3114 | if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1)) | |
3115 | range.SetEnd(range.GetEnd() + 1); | |
3116 | return range; | |
3117 | } | |
3118 | ||
3119 | void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range) | |
3120 | { | |
3121 | wxRichTextRange range1(range); | |
3122 | if (range1 != wxRichTextRange(-2,-2) && range1 != wxRichTextRange(-1,-1) ) | |
3123 | range1.SetEnd(range1.GetEnd() - 1); | |
3124 | ||
3125 | wxASSERT( range1.GetStart() > range1.GetEnd() ); | |
fe5aa22c | 3126 | |
96c9f0f6 JS |
3127 | SetInternalSelectionRange(range1); |
3128 | } | |
3129 | ||
38f833b1 JS |
3130 | /// Set list style |
3131 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
3132 | { | |
3133 | return GetBuffer().SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel); | |
3134 | } | |
3135 | ||
3136 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
3137 | { | |
3138 | return GetBuffer().SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel); | |
3139 | } | |
3140 | ||
3141 | /// Clear list for given range | |
3142 | bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags) | |
3143 | { | |
3144 | return GetBuffer().ClearListStyle(range.ToInternal(), flags); | |
3145 | } | |
3146 | ||
3147 | /// Number/renumber any list elements in the given range | |
3148 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
3149 | { | |
41a85215 | 3150 | return GetBuffer().NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel); |
38f833b1 JS |
3151 | } |
3152 | ||
3153 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
3154 | { | |
41a85215 | 3155 | return GetBuffer().NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel); |
38f833b1 JS |
3156 | } |
3157 | ||
3158 | /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 | |
3159 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel) | |
3160 | { | |
3161 | return GetBuffer().PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel); | |
3162 | } | |
3163 | ||
3164 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel) | |
3165 | { | |
3166 | return GetBuffer().PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel); | |
3167 | } | |
3168 | ||
12cc29c5 JS |
3169 | /// Deletes the content in the given range |
3170 | bool wxRichTextCtrl::Delete(const wxRichTextRange& range) | |
3171 | { | |
3172 | return GetBuffer().DeleteRangeWithUndo(range.ToInternal(), this); | |
3173 | } | |
3174 | ||
dadd4f55 JS |
3175 | const wxArrayString& wxRichTextCtrl::GetAvailableFontNames() |
3176 | { | |
3177 | if (sm_availableFontNames.GetCount() == 0) | |
3178 | { | |
3179 | sm_availableFontNames = wxFontEnumerator::GetFacenames(); | |
3180 | sm_availableFontNames.Sort(); | |
3181 | } | |
3182 | return sm_availableFontNames; | |
3183 | } | |
3184 | ||
3185 | void wxRichTextCtrl::ClearAvailableFontNames() | |
3186 | { | |
3187 | sm_availableFontNames.Clear(); | |
3188 | } | |
3189 | ||
5d7836c4 JS |
3190 | #endif |
3191 | // wxUSE_RICHTEXT |