]>
Commit | Line | Data |
---|---|---|
5d7836c4 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/richtext/richtextctrl.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 | ||
a188ac29 | 29 | #include "wx/timer.h" |
5d7836c4 JS |
30 | #include "wx/textfile.h" |
31 | #include "wx/ffile.h" | |
5d7836c4 JS |
32 | #include "wx/filename.h" |
33 | #include "wx/dcbuffer.h" | |
5d7836c4 | 34 | #include "wx/arrimpl.cpp" |
dadd4f55 | 35 | #include "wx/fontenum.h" |
addcf76c | 36 | #include "wx/accel.h" |
5d7836c4 | 37 | |
5dec941a JS |
38 | #if defined (__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) |
39 | #define wxHAVE_PRIMARY_SELECTION 1 | |
40 | #else | |
41 | #define wxHAVE_PRIMARY_SELECTION 0 | |
42 | #endif | |
43 | ||
44 | #if wxUSE_CLIPBOARD && wxHAVE_PRIMARY_SELECTION | |
45 | #include "wx/clipbrd.h" | |
46 | #endif | |
47 | ||
27e20452 JS |
48 | // DLL options compatibility check: |
49 | #include "wx/app.h" | |
50 | WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") | |
51 | ||
9b11752c VZ |
52 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, wxRichTextEvent ); |
53 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, wxRichTextEvent ); | |
54 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, wxRichTextEvent ); | |
55 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, wxRichTextEvent ); | |
56 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_RETURN, wxRichTextEvent ); | |
57 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CHARACTER, wxRichTextEvent ); | |
58 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_DELETE, wxRichTextEvent ); | |
5912d19e | 59 | |
9b11752c VZ |
60 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACING, wxRichTextEvent ); |
61 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_REPLACED, wxRichTextEvent ); | |
62 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGING, wxRichTextEvent ); | |
63 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLESHEET_CHANGED, wxRichTextEvent ); | |
5d7836c4 | 64 | |
9b11752c VZ |
65 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_INSERTED, wxRichTextEvent ); |
66 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_CONTENT_DELETED, wxRichTextEvent ); | |
67 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_STYLE_CHANGED, wxRichTextEvent ); | |
68 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_SELECTION_CHANGED, wxRichTextEvent ); | |
69 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_BUFFER_RESET, wxRichTextEvent ); | |
603f702b | 70 | wxDEFINE_EVENT( wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED, wxRichTextEvent ); |
5912d19e | 71 | |
1c13f06e JS |
72 | #if wxRICHTEXT_USE_OWN_CARET |
73 | ||
74 | /*! | |
75 | * wxRichTextCaret | |
76 | * | |
77 | * This implements a non-flashing cursor in case there | |
78 | * are platform-specific problems with the generic caret. | |
79 | * wxRICHTEXT_USE_OWN_CARET is set in richtextbuffer.h. | |
80 | */ | |
81 | ||
a188ac29 JS |
82 | class wxRichTextCaret; |
83 | class wxRichTextCaretTimer: public wxTimer | |
84 | { | |
85 | public: | |
86 | wxRichTextCaretTimer(wxRichTextCaret* caret) | |
87 | { | |
88 | m_caret = caret; | |
89 | } | |
90 | virtual void Notify(); | |
91 | wxRichTextCaret* m_caret; | |
92 | }; | |
93 | ||
1c13f06e JS |
94 | class wxRichTextCaret: public wxCaret |
95 | { | |
96 | public: | |
97 | // ctors | |
98 | // ----- | |
99 | // default - use Create() | |
a188ac29 | 100 | wxRichTextCaret(): m_timer(this) { Init(); } |
1c13f06e JS |
101 | // creates a block caret associated with the given window |
102 | wxRichTextCaret(wxRichTextCtrl *window, int width, int height) | |
a188ac29 | 103 | : wxCaret(window, width, height), m_timer(this) { Init(); m_richTextCtrl = window; } |
1c13f06e | 104 | wxRichTextCaret(wxRichTextCtrl *window, const wxSize& size) |
a188ac29 | 105 | : wxCaret(window, size), m_timer(this) { Init(); m_richTextCtrl = window; } |
1c13f06e JS |
106 | |
107 | virtual ~wxRichTextCaret(); | |
108 | ||
109 | // implementation | |
110 | // -------------- | |
111 | ||
112 | // called by wxWindow (not using the event tables) | |
113 | virtual void OnSetFocus(); | |
114 | virtual void OnKillFocus(); | |
115 | ||
116 | // draw the caret on the given DC | |
117 | void DoDraw(wxDC *dc); | |
118 | ||
119 | // get the visible count | |
120 | int GetVisibleCount() const { return m_countVisible; } | |
121 | ||
122 | // delay repositioning | |
123 | bool GetNeedsUpdate() const { return m_needsUpdate; } | |
124 | void SetNeedsUpdate(bool needsUpdate = true ) { m_needsUpdate = needsUpdate; } | |
125 | ||
a188ac29 JS |
126 | void Notify(); |
127 | ||
cd45ed36 JS |
128 | bool GetRefreshEnabled() const { return m_refreshEnabled; } |
129 | void EnableRefresh(bool b) { m_refreshEnabled = b; } | |
130 | ||
1c13f06e JS |
131 | protected: |
132 | virtual void DoShow(); | |
133 | virtual void DoHide(); | |
134 | virtual void DoMove(); | |
135 | virtual void DoSize(); | |
136 | ||
137 | // refresh the caret | |
138 | void Refresh(); | |
139 | ||
140 | private: | |
141 | void Init(); | |
142 | ||
143 | int m_xOld, | |
144 | m_yOld; | |
145 | bool m_hasFocus; // true => our window has focus | |
146 | bool m_needsUpdate; // must be repositioned | |
a188ac29 JS |
147 | bool m_flashOn; |
148 | wxRichTextCaretTimer m_timer; | |
1c13f06e | 149 | wxRichTextCtrl* m_richTextCtrl; |
cd45ed36 | 150 | bool m_refreshEnabled; |
1c13f06e JS |
151 | }; |
152 | #endif | |
153 | ||
24777478 | 154 | IMPLEMENT_DYNAMIC_CLASS( wxRichTextCtrl, wxControl ) |
5d7836c4 | 155 | |
d0960f6d | 156 | IMPLEMENT_DYNAMIC_CLASS( wxRichTextEvent, wxNotifyEvent ) |
5d7836c4 | 157 | |
24777478 | 158 | BEGIN_EVENT_TABLE( wxRichTextCtrl, wxControl ) |
5d7836c4 JS |
159 | EVT_PAINT(wxRichTextCtrl::OnPaint) |
160 | EVT_ERASE_BACKGROUND(wxRichTextCtrl::OnEraseBackground) | |
4d551ad5 | 161 | EVT_IDLE(wxRichTextCtrl::OnIdle) |
c59f6793 | 162 | EVT_SCROLLWIN(wxRichTextCtrl::OnScroll) |
5d7836c4 JS |
163 | EVT_LEFT_DOWN(wxRichTextCtrl::OnLeftClick) |
164 | EVT_MOTION(wxRichTextCtrl::OnMoveMouse) | |
165 | EVT_LEFT_UP(wxRichTextCtrl::OnLeftUp) | |
166 | EVT_RIGHT_DOWN(wxRichTextCtrl::OnRightClick) | |
167 | EVT_MIDDLE_DOWN(wxRichTextCtrl::OnMiddleClick) | |
168 | EVT_LEFT_DCLICK(wxRichTextCtrl::OnLeftDClick) | |
169 | EVT_CHAR(wxRichTextCtrl::OnChar) | |
c80e8cbe | 170 | EVT_KEY_DOWN(wxRichTextCtrl::OnChar) |
5d7836c4 JS |
171 | EVT_SIZE(wxRichTextCtrl::OnSize) |
172 | EVT_SET_FOCUS(wxRichTextCtrl::OnSetFocus) | |
173 | EVT_KILL_FOCUS(wxRichTextCtrl::OnKillFocus) | |
61f84e24 | 174 | EVT_MOUSE_CAPTURE_LOST(wxRichTextCtrl::OnCaptureLost) |
5d7836c4 | 175 | EVT_CONTEXT_MENU(wxRichTextCtrl::OnContextMenu) |
52170c5b | 176 | EVT_SYS_COLOUR_CHANGED(wxRichTextCtrl::OnSysColourChanged) |
5d7836c4 JS |
177 | |
178 | EVT_MENU(wxID_UNDO, wxRichTextCtrl::OnUndo) | |
179 | EVT_UPDATE_UI(wxID_UNDO, wxRichTextCtrl::OnUpdateUndo) | |
180 | ||
181 | EVT_MENU(wxID_REDO, wxRichTextCtrl::OnRedo) | |
182 | EVT_UPDATE_UI(wxID_REDO, wxRichTextCtrl::OnUpdateRedo) | |
183 | ||
184 | EVT_MENU(wxID_COPY, wxRichTextCtrl::OnCopy) | |
185 | EVT_UPDATE_UI(wxID_COPY, wxRichTextCtrl::OnUpdateCopy) | |
186 | ||
187 | EVT_MENU(wxID_PASTE, wxRichTextCtrl::OnPaste) | |
188 | EVT_UPDATE_UI(wxID_PASTE, wxRichTextCtrl::OnUpdatePaste) | |
189 | ||
190 | EVT_MENU(wxID_CUT, wxRichTextCtrl::OnCut) | |
191 | EVT_UPDATE_UI(wxID_CUT, wxRichTextCtrl::OnUpdateCut) | |
192 | ||
193 | EVT_MENU(wxID_CLEAR, wxRichTextCtrl::OnClear) | |
194 | EVT_UPDATE_UI(wxID_CLEAR, wxRichTextCtrl::OnUpdateClear) | |
195 | ||
196 | EVT_MENU(wxID_SELECTALL, wxRichTextCtrl::OnSelectAll) | |
197 | EVT_UPDATE_UI(wxID_SELECTALL, wxRichTextCtrl::OnUpdateSelectAll) | |
603f702b JS |
198 | |
199 | EVT_MENU(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnProperties) | |
200 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES1, wxRichTextCtrl::OnUpdateProperties) | |
201 | ||
202 | EVT_MENU(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnProperties) | |
203 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES2, wxRichTextCtrl::OnUpdateProperties) | |
204 | ||
205 | EVT_MENU(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnProperties) | |
206 | EVT_UPDATE_UI(wxID_RICHTEXT_PROPERTIES3, wxRichTextCtrl::OnUpdateProperties) | |
207 | ||
5d7836c4 JS |
208 | END_EVENT_TABLE() |
209 | ||
210 | /*! | |
211 | * wxRichTextCtrl | |
212 | */ | |
213 | ||
dadd4f55 JS |
214 | wxArrayString wxRichTextCtrl::sm_availableFontNames; |
215 | ||
5d7836c4 | 216 | wxRichTextCtrl::wxRichTextCtrl() |
343ae70d | 217 | : wxScrollHelper(this) |
5d7836c4 JS |
218 | { |
219 | Init(); | |
220 | } | |
221 | ||
343ae70d VZ |
222 | wxRichTextCtrl::wxRichTextCtrl(wxWindow* parent, |
223 | wxWindowID id, | |
224 | const wxString& value, | |
225 | const wxPoint& pos, | |
226 | const wxSize& size, | |
4e8d9558 JS |
227 | long style, |
228 | const wxValidator& validator, | |
229 | const wxString& name) | |
343ae70d | 230 | : wxScrollHelper(this) |
5d7836c4 JS |
231 | { |
232 | Init(); | |
4e8d9558 | 233 | Create(parent, id, value, pos, size, style, validator, name); |
0c0e063e JS |
234 | |
235 | SetDropTarget(new wxRichTextDropTarget(this)); | |
5d7836c4 JS |
236 | } |
237 | ||
238 | /// Creation | |
4e8d9558 JS |
239 | bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, |
240 | const wxValidator& validator, const wxString& name) | |
5d7836c4 | 241 | { |
6d595f3d JS |
242 | style |= wxVSCROLL; |
243 | ||
24777478 | 244 | if (!wxControl::Create(parent, id, pos, size, |
0ec1179b VZ |
245 | style|wxFULL_REPAINT_ON_RESIZE, |
246 | validator, name)) | |
5d7836c4 | 247 | return false; |
5d7836c4 | 248 | |
a1b806b9 | 249 | if (!GetFont().IsOk()) |
5d7836c4 JS |
250 | { |
251 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
252 | } | |
7fe8059f | 253 | |
e9f10004 JS |
254 | // No physical scrolling, so we can preserve margins |
255 | EnableScrolling(false, false); | |
256 | ||
dadd4f55 JS |
257 | if (style & wxTE_READONLY) |
258 | SetEditable(false); | |
259 | ||
42688aea | 260 | // The base attributes must all have default values |
24777478 | 261 | wxRichTextAttr attributes; |
5d7836c4 | 262 | attributes.SetFont(GetFont()); |
79f955ec | 263 | attributes.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); |
5d7836c4 | 264 | attributes.SetAlignment(wxTEXT_ALIGNMENT_LEFT); |
3e3a754f JS |
265 | attributes.SetLineSpacing(10); |
266 | attributes.SetParagraphSpacingAfter(10); | |
267 | attributes.SetParagraphSpacingBefore(0); | |
4fe83b93 | 268 | |
5d7836c4 JS |
269 | SetBasicStyle(attributes); |
270 | ||
603f702b JS |
271 | int margin = 5; |
272 | SetMargins(margin, margin); | |
273 | ||
fe5aa22c JS |
274 | // The default attributes will be merged with base attributes, so |
275 | // can be empty to begin with | |
24777478 | 276 | wxRichTextAttr defaultAttributes; |
fe5aa22c JS |
277 | SetDefaultStyle(defaultAttributes); |
278 | ||
79f955ec | 279 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
5d7836c4 JS |
280 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
281 | ||
cd8ba0d9 JS |
282 | GetBuffer().Reset(); |
283 | GetBuffer().SetRichTextCtrl(this); | |
284 | ||
1c13f06e JS |
285 | #if wxRICHTEXT_USE_OWN_CARET |
286 | SetCaret(new wxRichTextCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16)); | |
287 | #else | |
cd8ba0d9 | 288 | SetCaret(new wxCaret(this, wxRICHTEXT_DEFAULT_CARET_WIDTH, 16)); |
1c13f06e | 289 | #endif |
cd8ba0d9 | 290 | |
7fe8059f | 291 | // Tell the sizers to use the given or best size |
170acdc9 | 292 | SetInitialSize(size); |
7fe8059f | 293 | |
3e3a754f | 294 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 JS |
295 | // Create a buffer |
296 | RecreateBuffer(size); | |
3e3a754f | 297 | #endif |
5d7836c4 | 298 | |
d2d0adc7 JS |
299 | m_textCursor = wxCursor(wxCURSOR_IBEAM); |
300 | m_urlCursor = wxCursor(wxCURSOR_HAND); | |
301 | ||
302 | SetCursor(m_textCursor); | |
61399247 | 303 | |
27e20452 JS |
304 | if (!value.IsEmpty()) |
305 | SetValue(value); | |
7fe8059f | 306 | |
d2d0adc7 JS |
307 | GetBuffer().AddEventHandler(this); |
308 | ||
addcf76c | 309 | // Accelerators |
0951fe0a | 310 | wxAcceleratorEntry entries[6]; |
addcf76c | 311 | |
675ec665 SC |
312 | entries[0].Set(wxACCEL_CTRL, (int) 'C', wxID_COPY); |
313 | entries[1].Set(wxACCEL_CTRL, (int) 'X', wxID_CUT); | |
314 | entries[2].Set(wxACCEL_CTRL, (int) 'V', wxID_PASTE); | |
315 | entries[3].Set(wxACCEL_CTRL, (int) 'A', wxID_SELECTALL); | |
316 | entries[4].Set(wxACCEL_CTRL, (int) 'Z', wxID_UNDO); | |
317 | entries[5].Set(wxACCEL_CTRL, (int) 'Y', wxID_REDO); | |
addcf76c | 318 | |
0951fe0a | 319 | wxAcceleratorTable accel(6, entries); |
addcf76c JS |
320 | SetAcceleratorTable(accel); |
321 | ||
8e43fc77 JS |
322 | m_contextMenu = new wxMenu; |
323 | m_contextMenu->Append(wxID_UNDO, _("&Undo")); | |
324 | m_contextMenu->Append(wxID_REDO, _("&Redo")); | |
325 | m_contextMenu->AppendSeparator(); | |
326 | m_contextMenu->Append(wxID_CUT, _("Cu&t")); | |
327 | m_contextMenu->Append(wxID_COPY, _("&Copy")); | |
328 | m_contextMenu->Append(wxID_PASTE, _("&Paste")); | |
329 | m_contextMenu->Append(wxID_CLEAR, _("&Delete")); | |
330 | m_contextMenu->AppendSeparator(); | |
331 | m_contextMenu->Append(wxID_SELECTALL, _("Select &All")); | |
cdaed652 | 332 | m_contextMenu->AppendSeparator(); |
603f702b | 333 | m_contextMenu->Append(wxID_RICHTEXT_PROPERTIES1, _("&Properties")); |
cdaed652 | 334 | |
5d7836c4 JS |
335 | return true; |
336 | } | |
337 | ||
338 | wxRichTextCtrl::~wxRichTextCtrl() | |
339 | { | |
603f702b | 340 | SetFocusObject(& GetBuffer(), false); |
d2d0adc7 | 341 | GetBuffer().RemoveEventHandler(this); |
88bee7e6 | 342 | |
5d7836c4 JS |
343 | delete m_contextMenu; |
344 | } | |
345 | ||
346 | /// Member initialisation | |
347 | void wxRichTextCtrl::Init() | |
348 | { | |
5d7836c4 JS |
349 | m_contextMenu = NULL; |
350 | m_caret = NULL; | |
351 | m_caretPosition = -1; | |
5d7836c4 | 352 | m_selectionAnchor = -2; |
603f702b JS |
353 | m_selectionAnchorObject = NULL; |
354 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 JS |
355 | m_editable = true; |
356 | m_caretAtLineStart = false; | |
357 | m_dragging = false; | |
0c0e063e | 358 | m_preDrag = false; |
4d551ad5 JS |
359 | m_fullLayoutRequired = false; |
360 | m_fullLayoutTime = 0; | |
361 | m_fullLayoutSavedPosition = 0; | |
362 | m_delayedLayoutThreshold = wxRICHTEXT_DEFAULT_DELAYED_LAYOUT_THRESHOLD; | |
ab14c7aa | 363 | m_caretPositionForDefaultStyle = -2; |
603f702b | 364 | m_focusObject = & m_buffer; |
5d7836c4 JS |
365 | } |
366 | ||
17808a75 | 367 | void wxRichTextCtrl::DoThaw() |
5d7836c4 | 368 | { |
603f702b | 369 | if (GetBuffer().IsDirty()) |
17808a75 VZ |
370 | LayoutContent(); |
371 | else | |
372 | SetupScrollbars(); | |
8251c171 | 373 | |
6a8e61e0 | 374 | wxWindow::DoThaw(); |
5d7836c4 JS |
375 | } |
376 | ||
377 | /// Clear all text | |
378 | void wxRichTextCtrl::Clear() | |
379 | { | |
603f702b JS |
380 | if (GetFocusObject() == & GetBuffer()) |
381 | { | |
382 | m_buffer.ResetAndClearCommands(); | |
383 | m_buffer.Invalidate(wxRICHTEXT_ALL); | |
384 | } | |
385 | else | |
386 | { | |
387 | GetFocusObject()->Reset(); | |
388 | } | |
389 | ||
5d7836c4 | 390 | m_caretPosition = -1; |
ab14c7aa | 391 | m_caretPositionForDefaultStyle = -2; |
5d7836c4 | 392 | m_caretAtLineStart = false; |
603f702b JS |
393 | m_selection.Reset(); |
394 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 | 395 | |
495a248e JS |
396 | Scroll(0,0); |
397 | ||
17808a75 | 398 | if (!IsFrozen()) |
5d7836c4 | 399 | { |
cecf8eee | 400 | LayoutContent(); |
76bcd815 | 401 | Refresh(false); |
5d7836c4 | 402 | } |
0ec1179b VZ |
403 | |
404 | wxTextCtrl::SendTextUpdatedEvent(this); | |
5d7836c4 JS |
405 | } |
406 | ||
407 | /// Painting | |
408 | void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
409 | { | |
1c13f06e JS |
410 | #if !wxRICHTEXT_USE_OWN_CARET |
411 | if (GetCaret() && !IsFrozen()) | |
d2142335 | 412 | GetCaret()->Hide(); |
cd45ed36 JS |
413 | #else |
414 | // Stop the caret refreshing the control from within the | |
415 | // paint handler | |
416 | if (GetCaret()) | |
417 | ((wxRichTextCaret*) GetCaret())->EnableRefresh(false); | |
1c13f06e | 418 | #endif |
d2142335 | 419 | |
5d7836c4 | 420 | { |
3e3a754f | 421 | #if wxRICHTEXT_BUFFERED_PAINTING |
c59f6793 | 422 | wxBufferedPaintDC dc(this, m_bufferBitmap); |
3e3a754f JS |
423 | #else |
424 | wxPaintDC dc(this); | |
425 | #endif | |
41e155b4 | 426 | |
17808a75 | 427 | if (IsFrozen()) |
c59f6793 | 428 | return; |
41e155b4 | 429 | |
17808a75 VZ |
430 | PrepareDC(dc); |
431 | ||
c59f6793 | 432 | dc.SetFont(GetFont()); |
41e155b4 | 433 | |
c59f6793 JS |
434 | // Paint the background |
435 | PaintBackground(dc); | |
41e155b4 | 436 | |
ea160b2e JS |
437 | // wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); |
438 | ||
439 | wxRect drawingArea(GetUpdateRegion().GetBox()); | |
440 | drawingArea.SetPosition(GetLogicalPoint(drawingArea.GetPosition())); | |
441 | ||
c59f6793 | 442 | wxRect availableSpace(GetClientSize()); |
603f702b | 443 | if (GetBuffer().IsDirty()) |
c59f6793 JS |
444 | { |
445 | GetBuffer().Layout(dc, availableSpace, wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT); | |
603f702b | 446 | GetBuffer().Invalidate(wxRICHTEXT_NONE); |
c59f6793 JS |
447 | SetupScrollbars(); |
448 | } | |
41e155b4 | 449 | |
e9f10004 JS |
450 | wxRect clipRect(availableSpace); |
451 | clipRect.x += GetBuffer().GetLeftMargin(); | |
452 | clipRect.y += GetBuffer().GetTopMargin(); | |
453 | clipRect.width -= (GetBuffer().GetLeftMargin() + GetBuffer().GetRightMargin()); | |
454 | clipRect.height -= (GetBuffer().GetTopMargin() + GetBuffer().GetBottomMargin()); | |
455 | clipRect.SetPosition(GetLogicalPoint(clipRect.GetPosition())); | |
456 | dc.SetClippingRegion(clipRect); | |
457 | ||
603f702b JS |
458 | int flags = 0; |
459 | if ((GetExtraStyle() & wxRICHTEXT_EX_NO_GUIDELINES) == 0) | |
460 | flags |= wxRICHTEXT_DRAW_GUIDELINES; | |
4fe83b93 | 461 | |
603f702b | 462 | GetBuffer().Draw(dc, GetBuffer().GetOwnRange(), GetSelection(), drawingArea, 0 /* descent */, flags); |
e9f10004 JS |
463 | |
464 | dc.DestroyClippingRegion(); | |
465 | ||
35bdbae5 JS |
466 | // Other user defined painting after everything else (i.e. all text) is painted |
467 | PaintAboveContent(dc); | |
468 | ||
1c13f06e JS |
469 | #if wxRICHTEXT_USE_OWN_CARET |
470 | if (GetCaret()->IsVisible()) | |
471 | { | |
cd45ed36 | 472 | PositionCaret(); |
1c13f06e JS |
473 | ((wxRichTextCaret*) GetCaret())->DoDraw(& dc); |
474 | } | |
475 | #endif | |
5d7836c4 | 476 | } |
d2142335 | 477 | |
1c13f06e JS |
478 | #if !wxRICHTEXT_USE_OWN_CARET |
479 | if (GetCaret()) | |
d2142335 | 480 | GetCaret()->Show(); |
7383cf9d | 481 | PositionCaret(); |
5219cf05 | 482 | #else |
cd45ed36 JS |
483 | if (GetCaret()) |
484 | ((wxRichTextCaret*) GetCaret())->EnableRefresh(true); | |
1c13f06e | 485 | #endif |
5d7836c4 JS |
486 | } |
487 | ||
488 | // Empty implementation, to prevent flicker | |
489 | void wxRichTextCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
490 | { | |
491 | } | |
492 | ||
493 | void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) | |
494 | { | |
ea160b2e JS |
495 | if (GetCaret()) |
496 | { | |
1c13f06e | 497 | #if !wxRICHTEXT_USE_OWN_CARET |
ea160b2e | 498 | PositionCaret(); |
1c13f06e JS |
499 | #endif |
500 | GetCaret()->Show(); | |
ea160b2e | 501 | } |
c59f6793 | 502 | |
1c13f06e JS |
503 | #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET |
504 | // Work around dropouts when control is focused | |
505 | if (!IsFrozen()) | |
506 | { | |
507 | Refresh(false); | |
508 | } | |
509 | #endif | |
5d7836c4 JS |
510 | } |
511 | ||
512 | void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) | |
513 | { | |
1c13f06e | 514 | if (GetCaret()) |
ea160b2e | 515 | GetCaret()->Hide(); |
c59f6793 | 516 | |
1c13f06e JS |
517 | #if defined(__WXGTK__) && !wxRICHTEXT_USE_OWN_CARET |
518 | // Work around dropouts when control is focused | |
519 | if (!IsFrozen()) | |
520 | { | |
521 | Refresh(false); | |
522 | } | |
523 | #endif | |
5d7836c4 JS |
524 | } |
525 | ||
61f84e24 JS |
526 | void wxRichTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) |
527 | { | |
88bee7e6 | 528 | m_dragging = false; |
61f84e24 JS |
529 | } |
530 | ||
603f702b JS |
531 | // Set up the caret for the given position and container, after a mouse click |
532 | bool wxRichTextCtrl::SetCaretPositionAfterClick(wxRichTextParagraphLayoutBox* container, long position, int hitTestFlags, bool extendSelection) | |
533 | { | |
534 | bool caretAtLineStart = false; | |
535 | ||
536 | if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE) | |
537 | { | |
538 | // If we're at the start of a line (but not first in para) | |
539 | // then we should keep the caret showing at the start of the line | |
540 | // by showing the m_caretAtLineStart flag. | |
541 | wxRichTextParagraph* para = container->GetParagraphAtPosition(position); | |
542 | wxRichTextLine* line = container->GetLineAtPosition(position); | |
543 | ||
544 | if (line && para && line->GetAbsoluteRange().GetStart() == position && para->GetRange().GetStart() != position) | |
545 | caretAtLineStart = true; | |
546 | position --; | |
547 | } | |
548 | ||
549 | if (extendSelection && (m_caretPosition != position)) | |
550 | ExtendSelection(m_caretPosition, position, wxRICHTEXT_SHIFT_DOWN); | |
551 | ||
552 | MoveCaret(position, caretAtLineStart); | |
553 | SetDefaultStyleToCursorStyle(); | |
4fe83b93 | 554 | |
603f702b JS |
555 | return true; |
556 | } | |
557 | ||
5d7836c4 JS |
558 | /// Left-click |
559 | void wxRichTextCtrl::OnLeftClick(wxMouseEvent& event) | |
560 | { | |
561 | SetFocus(); | |
562 | ||
563 | wxClientDC dc(this); | |
564 | PrepareDC(dc); | |
565 | dc.SetFont(GetFont()); | |
7fe8059f | 566 | |
603f702b | 567 | // TODO: detect change of focus object |
5d7836c4 | 568 | long position = 0; |
603f702b JS |
569 | wxRichTextObject* hitObj = NULL; |
570 | wxRichTextObject* contextObj = NULL; | |
571 | int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position, & hitObj, & contextObj); | |
5d7836c4 | 572 | |
0c0e063e JS |
573 | #if wxUSE_DRAG_AND_DROP |
574 | // If there's no selection, or we're not inside it, this isn't an attempt to initiate Drag'n'Drop | |
b33e0c2c | 575 | if (IsEditable() && HasSelection() && GetSelectionRange().ToInternal().Contains(position)) |
0c0e063e JS |
576 | { |
577 | // This might be an attempt at initiating Drag'n'Drop. So set the time & flags | |
578 | m_preDrag = true; | |
579 | m_dragStartPoint = event.GetPosition(); // No need to worry about logical positions etc, we only care about the distance from the original pt | |
580 | ||
581 | #if wxUSE_DATETIME | |
582 | m_dragStartTime = wxDateTime::UNow(); | |
583 | #endif // wxUSE_DATETIME | |
584 | ||
585 | // Preserve behaviour of clicking on an object within the selection | |
586 | if (hit != wxRICHTEXT_HITTEST_NONE && hitObj) | |
587 | m_dragging = true; | |
588 | ||
589 | return; // Don't skip the event, else the selection will be lost | |
590 | } | |
591 | #endif // wxUSE_DRAG_AND_DROP | |
592 | ||
603f702b | 593 | if (hit != wxRICHTEXT_HITTEST_NONE && hitObj) |
5d7836c4 | 594 | { |
603f702b JS |
595 | wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject(); |
596 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
597 | if (container && container != GetFocusObject() && container->AcceptsFocus()) | |
598 | { | |
599 | SetFocusObject(container, false /* don't set caret position yet */); | |
600 | } | |
4fe83b93 | 601 | |
5d7836c4 JS |
602 | m_dragging = true; |
603 | CaptureMouse(); | |
604 | ||
f7222f4c JS |
605 | long oldCaretPos = m_caretPosition; |
606 | ||
603f702b | 607 | SetCaretPositionAfterClick(container, position, hit); |
f7222f4c | 608 | |
603f702b JS |
609 | // For now, don't handle shift-click when we're selecting multiple objects. |
610 | if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
77ea6c48 | 611 | ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN); |
f7222f4c JS |
612 | else |
613 | SelectNone(); | |
5d7836c4 JS |
614 | } |
615 | ||
616 | event.Skip(); | |
617 | } | |
618 | ||
619 | /// Left-up | |
d2d0adc7 | 620 | void wxRichTextCtrl::OnLeftUp(wxMouseEvent& event) |
5d7836c4 JS |
621 | { |
622 | if (m_dragging) | |
623 | { | |
624 | m_dragging = false; | |
625 | if (GetCapture() == this) | |
626 | ReleaseMouse(); | |
d2d0adc7 JS |
627 | |
628 | // See if we clicked on a URL | |
629 | wxClientDC dc(this); | |
630 | PrepareDC(dc); | |
631 | dc.SetFont(GetFont()); | |
632 | ||
633 | long position = 0; | |
634 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
603f702b JS |
635 | wxRichTextObject* hitObj = NULL; |
636 | wxRichTextObject* contextObj = NULL; | |
637 | // Only get objects at this level, not nested, because otherwise we couldn't swipe text at a single level. | |
638 | int hit = GetFocusObject()->HitTest(dc, logicalPt, position, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS); | |
88bee7e6 | 639 | |
0c0e063e JS |
640 | #if wxUSE_DRAG_AND_DROP |
641 | if (m_preDrag) | |
642 | { | |
643 | // Preserve the behaviour that would have happened without drag-and-drop detection, in OnLeftClick | |
644 | m_preDrag = false; // Tell DnD not to happen now: we are processing Left Up ourselves. | |
645 | ||
646 | // Do the actions that would have been done in OnLeftClick if we hadn't tried to drag | |
647 | long position = 0; | |
648 | wxRichTextObject* hitObj = NULL; | |
649 | wxRichTextObject* contextObj = NULL; | |
650 | int hit = GetBuffer().HitTest(dc, event.GetLogicalPosition(dc), position, & hitObj, & contextObj); | |
651 | wxRichTextParagraphLayoutBox* oldFocusObject = GetFocusObject(); | |
652 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
653 | if (container && container != GetFocusObject() && container->AcceptsFocus()) | |
654 | { | |
655 | SetFocusObject(container, false /* don't set caret position yet */); | |
656 | } | |
657 | ||
658 | long oldCaretPos = m_caretPosition; | |
659 | ||
660 | SetCaretPositionAfterClick(container, position, hit); | |
661 | ||
662 | // For now, don't handle shift-click when we're selecting multiple objects. | |
663 | if (event.ShiftDown() && GetFocusObject() == oldFocusObject && m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
664 | ExtendSelection(oldCaretPos, m_caretPosition, wxRICHTEXT_SHIFT_DOWN); | |
665 | else | |
666 | SelectNone(); | |
667 | } | |
668 | #endif | |
669 | ||
62381daa | 670 | if ((hit != wxRICHTEXT_HITTEST_NONE) && !(hit & wxRICHTEXT_HITTEST_OUTSIDE)) |
d2d0adc7 | 671 | { |
db6634e0 JS |
672 | wxRichTextEvent cmdEvent( |
673 | wxEVT_COMMAND_RICHTEXT_LEFT_CLICK, | |
674 | GetId()); | |
675 | cmdEvent.SetEventObject(this); | |
603f702b JS |
676 | cmdEvent.SetPosition(position); |
677 | if (hitObj) | |
678 | cmdEvent.SetContainer(hitObj->GetContainer()); | |
db6634e0 JS |
679 | |
680 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) | |
d2d0adc7 | 681 | { |
24777478 | 682 | wxRichTextAttr attr; |
db6634e0 | 683 | if (GetStyle(position, attr)) |
d2d0adc7 | 684 | { |
db6634e0 | 685 | if (attr.HasFlag(wxTEXT_ATTR_URL)) |
d2d0adc7 | 686 | { |
db6634e0 JS |
687 | wxString urlTarget = attr.GetURL(); |
688 | if (!urlTarget.IsEmpty()) | |
d2d0adc7 | 689 | { |
db6634e0 | 690 | wxMouseEvent mouseEvent(event); |
1a75935d | 691 | |
db6634e0 | 692 | long startPos = 0, endPos = 0; |
603f702b | 693 | wxRichTextObject* obj = GetFocusObject()->GetLeafObjectAtPosition(position); |
db6634e0 JS |
694 | if (obj) |
695 | { | |
696 | startPos = obj->GetRange().GetStart(); | |
697 | endPos = obj->GetRange().GetEnd(); | |
698 | } | |
1a75935d | 699 | |
db6634e0 JS |
700 | wxTextUrlEvent urlEvent(GetId(), mouseEvent, startPos, endPos); |
701 | InitCommandEvent(urlEvent); | |
1a75935d | 702 | |
db6634e0 | 703 | urlEvent.SetString(urlTarget); |
1a75935d | 704 | |
db6634e0 | 705 | GetEventHandler()->ProcessEvent(urlEvent); |
88bee7e6 | 706 | } |
d2d0adc7 JS |
707 | } |
708 | } | |
709 | } | |
710 | } | |
5d7836c4 | 711 | } |
5dec941a | 712 | |
0c0e063e JS |
713 | #if wxUSE_DRAG_AND_DROP |
714 | m_preDrag = false; | |
715 | #endif // wxUSE_DRAG_AND_DROP | |
716 | ||
5dec941a JS |
717 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION |
718 | if (HasSelection() && GetFocusObject() && GetFocusObject()->GetBuffer()) | |
719 | { | |
720 | // Put the selection in PRIMARY, if it exists | |
721 | wxTheClipboard->UsePrimarySelection(true); | |
722 | ||
723 | wxRichTextRange range = GetInternalSelectionRange(); | |
724 | GetFocusObject()->GetBuffer()->CopyToClipboard(range); | |
725 | ||
726 | wxTheClipboard->UsePrimarySelection(false); | |
727 | } | |
728 | #endif | |
5d7836c4 JS |
729 | } |
730 | ||
0c0e063e | 731 | /// Mouse-movements |
5d7836c4 JS |
732 | void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) |
733 | { | |
0c0e063e JS |
734 | #if wxUSE_DRAG_AND_DROP |
735 | // See if we're starting Drag'n'Drop | |
736 | if (m_preDrag) | |
737 | { | |
738 | int x = m_dragStartPoint.x - event.GetPosition().x; | |
739 | int y = m_dragStartPoint.y - event.GetPosition().y; | |
740 | size_t distance = abs(x) + abs(y); | |
741 | #if wxUSE_DATETIME | |
742 | wxTimeSpan diff = wxDateTime::UNow() - m_dragStartTime; | |
743 | #endif | |
744 | if ((distance > 10) | |
745 | #if wxUSE_DATETIME | |
746 | && (diff.GetMilliseconds() > 100) | |
747 | #endif | |
748 | ) | |
749 | { | |
750 | m_dragging = false; | |
751 | ||
752 | // Start drag'n'drop | |
753 | wxRichTextRange range = GetInternalSelectionRange(); | |
754 | if (range == wxRICHTEXT_NONE) | |
755 | { | |
756 | // Don't try to drag an empty range | |
757 | m_preDrag = false; | |
758 | return; | |
759 | } | |
760 | ||
761 | // Cache the current situation, to be restored if Drag'n'Drop is cancelled | |
762 | long oldPos = GetCaretPosition(); | |
763 | wxRichTextParagraphLayoutBox* oldFocus = GetFocusObject(); | |
764 | ||
765 | wxDataObjectComposite* compositeObject = new wxDataObjectComposite(); | |
766 | wxString text = GetFocusObject()->GetTextForRange(range); | |
767 | #ifdef __WXMSW__ | |
768 | text = wxTextFile::Translate(text, wxTextFileType_Dos); | |
769 | #endif | |
770 | compositeObject->Add(new wxTextDataObject(text), false /* not preferred */); | |
771 | ||
772 | wxRichTextBuffer* richTextBuf = new wxRichTextBuffer; | |
773 | GetFocusObject()->CopyFragment(range, *richTextBuf); | |
774 | compositeObject->Add(new wxRichTextBufferDataObject(richTextBuf), true /* preferred */); | |
775 | ||
776 | wxRichTextDropSource source(*compositeObject, this); | |
777 | // Use wxDrag_DefaultMove, not because it's the likelier choice but because pressing Ctrl for Copy obeys the principle of least surprise | |
778 | // The alternative, wxDrag_DefaultCopy, requires the user to know that Move needs the Shift key pressed | |
779 | BeginBatchUndo(_("Drag")); | |
780 | switch (source.DoDragDrop(wxDrag_AllowMove | wxDrag_DefaultMove)) | |
781 | { | |
782 | case wxDragMove: | |
783 | case wxDragCopy: break; | |
784 | ||
785 | case wxDragError: | |
786 | wxLogError(wxT("An error occurred during drag and drop operation")); | |
787 | case wxDragNone: | |
788 | case wxDragCancel: | |
789 | Refresh(); // This is needed in wxMSW, otherwise resetting the position doesn't 'take' | |
790 | SetCaretPosition(oldPos); | |
791 | SetFocusObject(oldFocus, false); | |
792 | default: break; | |
793 | } | |
794 | EndBatchUndo(); | |
795 | ||
796 | m_preDrag = false; | |
797 | return; | |
798 | } | |
799 | } | |
800 | #endif // wxUSE_DRAG_AND_DROP | |
801 | ||
5d7836c4 JS |
802 | wxClientDC dc(this); |
803 | PrepareDC(dc); | |
804 | dc.SetFont(GetFont()); | |
7fe8059f | 805 | |
5d7836c4 JS |
806 | long position = 0; |
807 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
603f702b JS |
808 | wxRichTextObject* hitObj = NULL; |
809 | wxRichTextObject* contextObj = NULL; | |
4fe83b93 | 810 | |
603f702b | 811 | int flags = 0; |
4fe83b93 | 812 | |
603f702b JS |
813 | // If we're dragging, let's only consider positions at this level; otherwise |
814 | // selecting a range is not going to work. | |
815 | wxRichTextParagraphLayoutBox* container = & GetBuffer(); | |
816 | if (m_dragging) | |
817 | { | |
818 | flags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS; | |
819 | container = GetFocusObject(); | |
820 | } | |
821 | int hit = container->HitTest(dc, logicalPt, position, & hitObj, & contextObj, flags); | |
4fe83b93 | 822 | |
d2d0adc7 | 823 | // See if we need to change the cursor |
88bee7e6 | 824 | |
d2d0adc7 | 825 | { |
603f702b | 826 | if (hit != wxRICHTEXT_HITTEST_NONE && !(hit & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj) |
d2d0adc7 | 827 | { |
603f702b | 828 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
24777478 | 829 | wxRichTextAttr attr; |
603f702b | 830 | if (actualContainer && GetStyle(position, attr, actualContainer)) |
d2d0adc7 JS |
831 | { |
832 | if (attr.HasFlag(wxTEXT_ATTR_URL)) | |
833 | { | |
e4ef49c7 | 834 | SetCursor(m_urlCursor); |
d2d0adc7 JS |
835 | } |
836 | else if (!attr.HasFlag(wxTEXT_ATTR_URL)) | |
837 | { | |
e4ef49c7 | 838 | SetCursor(m_textCursor); |
d2d0adc7 JS |
839 | } |
840 | } | |
841 | } | |
f262b25c JS |
842 | else |
843 | SetCursor(m_textCursor); | |
d2d0adc7 JS |
844 | } |
845 | ||
846 | if (!event.Dragging()) | |
847 | { | |
848 | event.Skip(); | |
849 | return; | |
850 | } | |
4fe83b93 | 851 | |
0c0e063e JS |
852 | if (m_dragging |
853 | #if wxUSE_DRAG_AND_DROP | |
854 | && !m_preDrag | |
855 | #endif | |
856 | ) | |
5d7836c4 | 857 | { |
603f702b JS |
858 | wxRichTextParagraphLayoutBox* commonAncestor = NULL; |
859 | wxRichTextParagraphLayoutBox* otherContainer = NULL; | |
860 | wxRichTextParagraphLayoutBox* firstContainer = NULL; | |
861 | ||
862 | // Check for dragging across multiple containers | |
863 | long position2 = 0; | |
864 | wxRichTextObject* hitObj2 = NULL, *contextObj2 = NULL; | |
865 | int hit2 = GetBuffer().HitTest(dc, logicalPt, position2, & hitObj2, & contextObj2, 0); | |
866 | if (hit2 != wxRICHTEXT_HITTEST_NONE && !(hit2 & wxRICHTEXT_HITTEST_OUTSIDE) && hitObj2 && hitObj != hitObj2) | |
867 | { | |
868 | // See if we can find a common ancestor | |
869 | if (m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
870 | { | |
871 | firstContainer = GetFocusObject(); | |
872 | commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox); | |
873 | } | |
874 | else | |
875 | { | |
876 | firstContainer = wxDynamicCast(m_selectionAnchorObject, wxRichTextParagraphLayoutBox); | |
877 | //commonAncestor = GetFocusObject(); // when the selection state is not normal, the focus object (e.g. table) | |
878 | // is the common ancestor. | |
879 | commonAncestor = wxDynamicCast(firstContainer->GetParent(), wxRichTextParagraphLayoutBox); | |
880 | } | |
4fe83b93 | 881 | |
603f702b JS |
882 | if (commonAncestor && commonAncestor->HandlesChildSelections()) |
883 | { | |
884 | wxRichTextObject* p = hitObj2; | |
885 | while (p) | |
886 | { | |
887 | if (p->GetParent() == commonAncestor) | |
888 | { | |
889 | otherContainer = wxDynamicCast(p, wxRichTextParagraphLayoutBox); | |
890 | break; | |
891 | } | |
892 | p = p->GetParent(); | |
893 | } | |
894 | } | |
5d7836c4 | 895 | |
603f702b JS |
896 | if (commonAncestor && firstContainer && otherContainer) |
897 | { | |
898 | // We have now got a second container that shares a parent with the current or anchor object. | |
899 | if (m_selectionState == wxRichTextCtrlSelectionState_Normal) | |
900 | { | |
901 | // Don't go into common-ancestor selection mode if we still have the same | |
902 | // container. | |
903 | if (otherContainer != firstContainer) | |
904 | { | |
905 | m_selectionState = wxRichTextCtrlSelectionState_CommonAncestor; | |
906 | m_selectionAnchorObject = firstContainer; | |
907 | m_selectionAnchor = firstContainer->GetRange().GetStart(); | |
5d7836c4 | 908 | |
603f702b JS |
909 | // The common ancestor, such as a table, returns the cell selection |
910 | // between the anchor and current position. | |
911 | m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart()); | |
912 | } | |
913 | } | |
914 | else | |
915 | { | |
916 | m_selection = commonAncestor->GetSelection(m_selectionAnchor, otherContainer->GetRange().GetStart()); | |
917 | } | |
5d7836c4 | 918 | |
603f702b | 919 | Refresh(); |
7fe8059f | 920 | |
603f702b JS |
921 | if (otherContainer->AcceptsFocus()) |
922 | SetFocusObject(otherContainer, false /* don't set caret and clear selection */); | |
4fe83b93 | 923 | MoveCaret(-1, false); |
603f702b JS |
924 | SetDefaultStyleToCursorStyle(); |
925 | } | |
5d7836c4 JS |
926 | } |
927 | } | |
603f702b | 928 | |
0c0e063e JS |
929 | if (hitObj && m_dragging && hit != wxRICHTEXT_HITTEST_NONE && m_selectionState == wxRichTextCtrlSelectionState_Normal |
930 | #if wxUSE_DRAG_AND_DROP | |
931 | && !m_preDrag | |
932 | #endif | |
933 | ) | |
603f702b JS |
934 | { |
935 | // TODO: test closeness | |
936 | SetCaretPositionAfterClick(container, position, hit, true /* extend selection */); | |
937 | } | |
5d7836c4 JS |
938 | } |
939 | ||
940 | /// Right-click | |
52170c5b | 941 | void wxRichTextCtrl::OnRightClick(wxMouseEvent& event) |
5d7836c4 JS |
942 | { |
943 | SetFocus(); | |
4fe83b93 | 944 | |
603f702b JS |
945 | wxClientDC dc(this); |
946 | PrepareDC(dc); | |
947 | dc.SetFont(GetFont()); | |
948 | ||
949 | long position = 0; | |
950 | wxPoint logicalPt = event.GetLogicalPosition(dc); | |
951 | wxRichTextObject* hitObj = NULL; | |
952 | wxRichTextObject* contextObj = NULL; | |
953 | int hit = GetFocusObject()->HitTest(dc, logicalPt, position, & hitObj, & contextObj); | |
954 | ||
955 | if (hitObj && hitObj->GetContainer() != GetFocusObject()) | |
956 | { | |
957 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
958 | if (actualContainer && actualContainer->AcceptsFocus()) | |
959 | { | |
960 | SetFocusObject(actualContainer, false /* don't set caret position yet */); | |
961 | SetCaretPositionAfterClick(actualContainer, position, hit); | |
962 | } | |
963 | } | |
db6634e0 JS |
964 | |
965 | wxRichTextEvent cmdEvent( | |
966 | wxEVT_COMMAND_RICHTEXT_RIGHT_CLICK, | |
967 | GetId()); | |
968 | cmdEvent.SetEventObject(this); | |
603f702b JS |
969 | cmdEvent.SetPosition(position); |
970 | if (hitObj) | |
971 | cmdEvent.SetContainer(hitObj->GetContainer()); | |
1a75935d | 972 | |
52170c5b JS |
973 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
974 | event.Skip(); | |
5d7836c4 JS |
975 | } |
976 | ||
977 | /// Left-double-click | |
db6634e0 | 978 | void wxRichTextCtrl::OnLeftDClick(wxMouseEvent& WXUNUSED(event)) |
5d7836c4 | 979 | { |
db6634e0 JS |
980 | wxRichTextEvent cmdEvent( |
981 | wxEVT_COMMAND_RICHTEXT_LEFT_DCLICK, | |
982 | GetId()); | |
983 | cmdEvent.SetEventObject(this); | |
984 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 985 | cmdEvent.SetContainer(GetFocusObject()); |
1a75935d | 986 | |
db6634e0 JS |
987 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
988 | { | |
989 | SelectWord(GetCaretPosition()+1); | |
990 | } | |
5d7836c4 JS |
991 | } |
992 | ||
993 | /// Middle-click | |
994 | void wxRichTextCtrl::OnMiddleClick(wxMouseEvent& event) | |
995 | { | |
db6634e0 JS |
996 | wxRichTextEvent cmdEvent( |
997 | wxEVT_COMMAND_RICHTEXT_MIDDLE_CLICK, | |
998 | GetId()); | |
999 | cmdEvent.SetEventObject(this); | |
1000 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1001 | cmdEvent.SetContainer(GetFocusObject()); |
1a75935d | 1002 | |
db6634e0 JS |
1003 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1004 | event.Skip(); | |
5dec941a JS |
1005 | |
1006 | #if wxUSE_CLIPBOARD && wxUSE_DATAOBJ && wxHAVE_PRIMARY_SELECTION | |
1007 | // Paste any PRIMARY selection, if it exists | |
1008 | wxTheClipboard->UsePrimarySelection(true); | |
1009 | Paste(); | |
1010 | wxTheClipboard->UsePrimarySelection(false); | |
1011 | #endif | |
5d7836c4 JS |
1012 | } |
1013 | ||
1014 | /// Key press | |
1015 | void wxRichTextCtrl::OnChar(wxKeyEvent& event) | |
1016 | { | |
1017 | int flags = 0; | |
3e3a754f | 1018 | if (event.CmdDown()) |
5d7836c4 JS |
1019 | flags |= wxRICHTEXT_CTRL_DOWN; |
1020 | if (event.ShiftDown()) | |
1021 | flags |= wxRICHTEXT_SHIFT_DOWN; | |
1022 | if (event.AltDown()) | |
1023 | flags |= wxRICHTEXT_ALT_DOWN; | |
1024 | ||
c80e8cbe JS |
1025 | if (event.GetEventType() == wxEVT_KEY_DOWN) |
1026 | { | |
7a34307e | 1027 | if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION)) |
8c9e72f9 JS |
1028 | { |
1029 | KeyboardNavigate(event.GetKeyCode(), flags); | |
1030 | return; | |
1031 | } | |
1032 | ||
1033 | long keycode = event.GetKeyCode(); | |
1034 | switch ( keycode ) | |
1035 | { | |
1036 | case WXK_ESCAPE: | |
1037 | case WXK_START: | |
1038 | case WXK_LBUTTON: | |
1039 | case WXK_RBUTTON: | |
1040 | case WXK_CANCEL: | |
1041 | case WXK_MBUTTON: | |
1042 | case WXK_CLEAR: | |
1043 | case WXK_SHIFT: | |
1044 | case WXK_ALT: | |
1045 | case WXK_CONTROL: | |
1046 | case WXK_MENU: | |
1047 | case WXK_PAUSE: | |
1048 | case WXK_CAPITAL: | |
1049 | case WXK_END: | |
1050 | case WXK_HOME: | |
1051 | case WXK_LEFT: | |
1052 | case WXK_UP: | |
1053 | case WXK_RIGHT: | |
1054 | case WXK_DOWN: | |
1055 | case WXK_SELECT: | |
1056 | case WXK_PRINT: | |
1057 | case WXK_EXECUTE: | |
1058 | case WXK_SNAPSHOT: | |
1059 | case WXK_INSERT: | |
1060 | case WXK_HELP: | |
1061 | case WXK_F1: | |
1062 | case WXK_F2: | |
1063 | case WXK_F3: | |
1064 | case WXK_F4: | |
1065 | case WXK_F5: | |
1066 | case WXK_F6: | |
1067 | case WXK_F7: | |
1068 | case WXK_F8: | |
1069 | case WXK_F9: | |
1070 | case WXK_F10: | |
1071 | case WXK_F11: | |
1072 | case WXK_F12: | |
1073 | case WXK_F13: | |
1074 | case WXK_F14: | |
1075 | case WXK_F15: | |
1076 | case WXK_F16: | |
1077 | case WXK_F17: | |
1078 | case WXK_F18: | |
1079 | case WXK_F19: | |
1080 | case WXK_F20: | |
1081 | case WXK_F21: | |
1082 | case WXK_F22: | |
1083 | case WXK_F23: | |
1084 | case WXK_F24: | |
1085 | case WXK_NUMLOCK: | |
1086 | case WXK_SCROLL: | |
1087 | case WXK_PAGEUP: | |
1088 | case WXK_PAGEDOWN: | |
1089 | case WXK_NUMPAD_F1: | |
1090 | case WXK_NUMPAD_F2: | |
1091 | case WXK_NUMPAD_F3: | |
1092 | case WXK_NUMPAD_F4: | |
1093 | case WXK_NUMPAD_HOME: | |
1094 | case WXK_NUMPAD_LEFT: | |
1095 | case WXK_NUMPAD_UP: | |
1096 | case WXK_NUMPAD_RIGHT: | |
1097 | case WXK_NUMPAD_DOWN: | |
1098 | case WXK_NUMPAD_PAGEUP: | |
1099 | case WXK_NUMPAD_PAGEDOWN: | |
1100 | case WXK_NUMPAD_END: | |
1101 | case WXK_NUMPAD_BEGIN: | |
1102 | case WXK_NUMPAD_INSERT: | |
8c9e72f9 JS |
1103 | case WXK_WINDOWS_LEFT: |
1104 | { | |
1105 | return; | |
1106 | } | |
1107 | default: | |
1108 | { | |
1109 | } | |
1110 | } | |
1111 | ||
c80e8cbe JS |
1112 | // Must process this before translation, otherwise it's translated into a WXK_DELETE event. |
1113 | if (event.CmdDown() && event.GetKeyCode() == WXK_BACK) | |
1114 | { | |
1115 | BeginBatchUndo(_("Delete Text")); | |
1116 | ||
1117 | long newPos = m_caretPosition; | |
1118 | ||
29dbface | 1119 | bool processed = DeleteSelectedContent(& newPos); |
c80e8cbe JS |
1120 | |
1121 | // Submit range in character positions, which are greater than caret positions, | |
1122 | // so subtract 1 for deleted character and add 1 for conversion to character position. | |
1123 | if (newPos > -1) | |
1124 | { | |
c80e8cbe JS |
1125 | if (event.CmdDown()) |
1126 | { | |
1127 | long pos = wxRichTextCtrl::FindNextWordPosition(-1); | |
8251c171 | 1128 | if (pos < newPos) |
c80e8cbe | 1129 | { |
603f702b | 1130 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this, & GetBuffer()); |
c80e8cbe JS |
1131 | processed = true; |
1132 | } | |
1133 | } | |
1134 | ||
1135 | if (!processed) | |
603f702b | 1136 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this, & GetBuffer()); |
c80e8cbe JS |
1137 | } |
1138 | ||
1139 | EndBatchUndo(); | |
1140 | ||
1141 | if (GetLastPosition() == -1) | |
1142 | { | |
603f702b | 1143 | GetFocusObject()->Reset(); |
c80e8cbe JS |
1144 | |
1145 | m_caretPosition = -1; | |
1146 | PositionCaret(); | |
1147 | SetDefaultStyleToCursorStyle(); | |
1148 | } | |
1149 | ||
1150 | ScrollIntoView(m_caretPosition, WXK_LEFT); | |
1151 | ||
1152 | wxRichTextEvent cmdEvent( | |
1153 | wxEVT_COMMAND_RICHTEXT_DELETE, | |
1154 | GetId()); | |
1155 | cmdEvent.SetEventObject(this); | |
1156 | cmdEvent.SetFlags(flags); | |
1157 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1158 | cmdEvent.SetContainer(GetFocusObject()); |
c80e8cbe JS |
1159 | GetEventHandler()->ProcessEvent(cmdEvent); |
1160 | ||
1161 | Update(); | |
1162 | } | |
1163 | else | |
1164 | event.Skip(); | |
1165 | ||
1166 | return; | |
1167 | } | |
1168 | ||
99257cbe VZ |
1169 | // all the other keys modify the controls contents which shouldn't be |
1170 | // possible if we're read-only | |
1171 | if ( !IsEditable() ) | |
1172 | { | |
1173 | event.Skip(); | |
1174 | return; | |
1175 | } | |
1176 | ||
1177 | if (event.GetKeyCode() == WXK_RETURN) | |
5d7836c4 JS |
1178 | { |
1179 | BeginBatchUndo(_("Insert Text")); | |
1180 | ||
1181 | long newPos = m_caretPosition; | |
1182 | ||
1183 | DeleteSelectedContent(& newPos); | |
1184 | ||
ff76711f JS |
1185 | if (event.ShiftDown()) |
1186 | { | |
1187 | wxString text; | |
1188 | text = wxRichTextLineBreakChar; | |
4e63bfb9 | 1189 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, text, this); |
4bf9e5bc | 1190 | m_caretAtLineStart = true; |
043c0d58 | 1191 | PositionCaret(); |
ff76711f JS |
1192 | } |
1193 | else | |
4e63bfb9 | 1194 | GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), newPos+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE|wxRICHTEXT_INSERT_INTERACTIVE); |
ff76711f | 1195 | |
d2d0adc7 | 1196 | EndBatchUndo(); |
0a21bea0 | 1197 | SetDefaultStyleToCursorStyle(); |
d2d0adc7 JS |
1198 | |
1199 | ScrollIntoView(m_caretPosition, WXK_RIGHT); | |
5d7836c4 JS |
1200 | |
1201 | wxRichTextEvent cmdEvent( | |
1202 | wxEVT_COMMAND_RICHTEXT_RETURN, | |
1203 | GetId()); | |
1204 | cmdEvent.SetEventObject(this); | |
1205 | cmdEvent.SetFlags(flags); | |
5912d19e | 1206 | cmdEvent.SetPosition(newPos+1); |
603f702b | 1207 | cmdEvent.SetContainer(GetFocusObject()); |
5912d19e | 1208 | |
d2d0adc7 JS |
1209 | if (!GetEventHandler()->ProcessEvent(cmdEvent)) |
1210 | { | |
1211 | // Generate conventional event | |
1212 | wxCommandEvent textEvent(wxEVT_COMMAND_TEXT_ENTER, GetId()); | |
1213 | InitCommandEvent(textEvent); | |
88bee7e6 | 1214 | |
d2d0adc7 JS |
1215 | GetEventHandler()->ProcessEvent(textEvent); |
1216 | } | |
621ae374 | 1217 | Update(); |
5d7836c4 JS |
1218 | } |
1219 | else if (event.GetKeyCode() == WXK_BACK) | |
1220 | { | |
1221 | BeginBatchUndo(_("Delete Text")); | |
88bee7e6 | 1222 | |
c80e8cbe JS |
1223 | long newPos = m_caretPosition; |
1224 | ||
dc5de0dd | 1225 | bool processed = DeleteSelectedContent(& newPos); |
c80e8cbe | 1226 | |
5d7836c4 JS |
1227 | // Submit range in character positions, which are greater than caret positions, |
1228 | // so subtract 1 for deleted character and add 1 for conversion to character position. | |
c80e8cbe | 1229 | if (newPos > -1) |
5d7836c4 | 1230 | { |
62381daa JS |
1231 | if (event.CmdDown()) |
1232 | { | |
1233 | long pos = wxRichTextCtrl::FindNextWordPosition(-1); | |
8251c171 | 1234 | if (pos < newPos) |
62381daa | 1235 | { |
603f702b | 1236 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(pos+1, newPos), this, & GetBuffer()); |
62381daa JS |
1237 | processed = true; |
1238 | } | |
1239 | } | |
1240 | ||
1241 | if (!processed) | |
603f702b | 1242 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos, newPos), this, & GetBuffer()); |
5d7836c4 | 1243 | } |
5d7836c4 JS |
1244 | |
1245 | EndBatchUndo(); | |
1246 | ||
5d7836c4 JS |
1247 | if (GetLastPosition() == -1) |
1248 | { | |
603f702b | 1249 | GetFocusObject()->Reset(); |
5d7836c4 JS |
1250 | |
1251 | m_caretPosition = -1; | |
1252 | PositionCaret(); | |
0a21bea0 | 1253 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1254 | } |
1255 | ||
f9ed2d16 | 1256 | ScrollIntoView(m_caretPosition, WXK_LEFT); |
5912d19e JS |
1257 | |
1258 | wxRichTextEvent cmdEvent( | |
1259 | wxEVT_COMMAND_RICHTEXT_DELETE, | |
1260 | GetId()); | |
1261 | cmdEvent.SetEventObject(this); | |
1262 | cmdEvent.SetFlags(flags); | |
1263 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1264 | cmdEvent.SetContainer(GetFocusObject()); |
5912d19e JS |
1265 | GetEventHandler()->ProcessEvent(cmdEvent); |
1266 | ||
621ae374 | 1267 | Update(); |
5d7836c4 JS |
1268 | } |
1269 | else if (event.GetKeyCode() == WXK_DELETE) | |
1270 | { | |
1271 | BeginBatchUndo(_("Delete Text")); | |
1272 | ||
c80e8cbe JS |
1273 | long newPos = m_caretPosition; |
1274 | ||
dc5de0dd | 1275 | bool processed = DeleteSelectedContent(& newPos); |
c80e8cbe | 1276 | |
5d7836c4 | 1277 | // Submit range in character positions, which are greater than caret positions, |
603f702b | 1278 | if (newPos < GetFocusObject()->GetOwnRange().GetEnd()+1) |
5d7836c4 | 1279 | { |
c80e8cbe JS |
1280 | if (event.CmdDown()) |
1281 | { | |
1282 | long pos = wxRichTextCtrl::FindNextWordPosition(1); | |
1283 | if (pos != -1 && (pos > newPos)) | |
1284 | { | |
603f702b | 1285 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos+1, pos), this, & GetBuffer()); |
c80e8cbe JS |
1286 | processed = true; |
1287 | } | |
1288 | } | |
1289 | ||
fc6d9d0d | 1290 | if (!processed && newPos < (GetLastPosition()-1)) |
603f702b | 1291 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(newPos+1, newPos+1), this, & GetBuffer()); |
5d7836c4 | 1292 | } |
5d7836c4 JS |
1293 | |
1294 | EndBatchUndo(); | |
1295 | ||
5d7836c4 JS |
1296 | if (GetLastPosition() == -1) |
1297 | { | |
603f702b | 1298 | GetFocusObject()->Reset(); |
5d7836c4 JS |
1299 | |
1300 | m_caretPosition = -1; | |
1301 | PositionCaret(); | |
0a21bea0 | 1302 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 1303 | } |
5912d19e | 1304 | |
575be976 JS |
1305 | ScrollIntoView(m_caretPosition, WXK_LEFT); |
1306 | ||
5912d19e JS |
1307 | wxRichTextEvent cmdEvent( |
1308 | wxEVT_COMMAND_RICHTEXT_DELETE, | |
1309 | GetId()); | |
1310 | cmdEvent.SetEventObject(this); | |
1311 | cmdEvent.SetFlags(flags); | |
1312 | cmdEvent.SetPosition(m_caretPosition+1); | |
603f702b | 1313 | cmdEvent.SetContainer(GetFocusObject()); |
5912d19e JS |
1314 | GetEventHandler()->ProcessEvent(cmdEvent); |
1315 | ||
621ae374 | 1316 | Update(); |
5d7836c4 JS |
1317 | } |
1318 | else | |
1319 | { | |
3e3a754f JS |
1320 | long keycode = event.GetKeyCode(); |
1321 | switch ( keycode ) | |
1322 | { | |
1323 | case WXK_ESCAPE: | |
3e3a754f JS |
1324 | { |
1325 | event.Skip(); | |
1326 | return; | |
1327 | } | |
1807a1f3 | 1328 | |
3e3a754f JS |
1329 | default: |
1330 | { | |
cfceb4a8 | 1331 | #ifdef __WXMAC__ |
91babb9d | 1332 | if (event.CmdDown()) |
cfceb4a8 | 1333 | #else |
d2bb72ff JS |
1334 | // Fixes AltGr+key with European input languages on Windows |
1335 | if ((event.CmdDown() && !event.AltDown()) || (event.AltDown() && !event.CmdDown())) | |
cfceb4a8 | 1336 | #endif |
3e3a754f JS |
1337 | { |
1338 | event.Skip(); | |
1339 | return; | |
1340 | } | |
41a85215 | 1341 | |
5912d19e JS |
1342 | wxRichTextEvent cmdEvent( |
1343 | wxEVT_COMMAND_RICHTEXT_CHARACTER, | |
1344 | GetId()); | |
1345 | cmdEvent.SetEventObject(this); | |
1346 | cmdEvent.SetFlags(flags); | |
0b14a7bd JS |
1347 | #if wxUSE_UNICODE |
1348 | cmdEvent.SetCharacter(event.GetUnicodeKey()); | |
1349 | #else | |
5912d19e | 1350 | cmdEvent.SetCharacter((wxChar) keycode); |
0b14a7bd | 1351 | #endif |
5912d19e | 1352 | cmdEvent.SetPosition(m_caretPosition+1); |
603f702b | 1353 | cmdEvent.SetContainer(GetFocusObject()); |
88bee7e6 | 1354 | |
dadd4f55 JS |
1355 | if (keycode == wxT('\t')) |
1356 | { | |
1357 | // See if we need to promote or demote the selection or paragraph at the cursor | |
1358 | // position, instead of inserting a tab. | |
1359 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
603f702b | 1360 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos); |
dadd4f55 JS |
1361 | if (para && para->GetRange().GetStart() == pos && para->GetAttributes().HasListStyleName()) |
1362 | { | |
1363 | wxRichTextRange range; | |
1364 | if (HasSelection()) | |
1365 | range = GetSelectionRange(); | |
1366 | else | |
1367 | range = para->GetRange().FromInternal(); | |
41a85215 | 1368 | |
dadd4f55 JS |
1369 | int promoteBy = event.ShiftDown() ? 1 : -1; |
1370 | ||
1371 | PromoteList(promoteBy, range, NULL); | |
d2d0adc7 | 1372 | |
5912d19e JS |
1373 | GetEventHandler()->ProcessEvent(cmdEvent); |
1374 | ||
dadd4f55 JS |
1375 | return; |
1376 | } | |
1377 | } | |
5d7836c4 | 1378 | |
3e3a754f | 1379 | BeginBatchUndo(_("Insert Text")); |
5d7836c4 | 1380 | |
3e3a754f JS |
1381 | long newPos = m_caretPosition; |
1382 | DeleteSelectedContent(& newPos); | |
5d7836c4 | 1383 | |
0b14a7bd JS |
1384 | #if wxUSE_UNICODE |
1385 | wxString str = event.GetUnicodeKey(); | |
1386 | #else | |
3e3a754f | 1387 | wxString str = (wxChar) event.GetKeyCode(); |
0b14a7bd | 1388 | #endif |
4e63bfb9 | 1389 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), newPos+1, str, this, 0); |
5d7836c4 | 1390 | |
3e3a754f JS |
1391 | EndBatchUndo(); |
1392 | ||
0a21bea0 | 1393 | SetDefaultStyleToCursorStyle(); |
3e3a754f | 1394 | ScrollIntoView(m_caretPosition, WXK_RIGHT); |
88bee7e6 | 1395 | |
c1dd924d | 1396 | cmdEvent.SetPosition(m_caretPosition); |
5912d19e JS |
1397 | GetEventHandler()->ProcessEvent(cmdEvent); |
1398 | ||
621ae374 | 1399 | Update(); |
3e3a754f JS |
1400 | } |
1401 | } | |
5d7836c4 | 1402 | } |
5d7836c4 JS |
1403 | } |
1404 | ||
1405 | /// Delete content if there is a selection, e.g. when pressing a key. | |
1406 | bool wxRichTextCtrl::DeleteSelectedContent(long* newPos) | |
1407 | { | |
1408 | if (HasSelection()) | |
1409 | { | |
603f702b JS |
1410 | long pos = m_selection.GetRange().GetStart(); |
1411 | wxRichTextRange range = m_selection.GetRange(); | |
205a1ad9 JS |
1412 | |
1413 | // SelectAll causes more to be selected than doing it interactively, | |
1414 | // and causes a new paragraph to be inserted. So for multiline buffers, | |
1415 | // don't delete the final position. | |
1416 | if (range.GetEnd() == GetLastPosition() && GetNumberOfLines() > 0) | |
1417 | range.SetEnd(range.GetEnd()-1); | |
1418 | ||
603f702b JS |
1419 | GetFocusObject()->DeleteRangeWithUndo(range, this, & GetBuffer()); |
1420 | m_selection.Reset(); | |
1421 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 JS |
1422 | |
1423 | if (newPos) | |
1424 | *newPos = pos-1; | |
1425 | return true; | |
1426 | } | |
1427 | else | |
1428 | return false; | |
1429 | } | |
1430 | ||
1431 | /// Keyboard navigation | |
1432 | ||
1433 | /* | |
1434 | ||
1435 | Left: left one character | |
1436 | Right: right one character | |
1437 | Up: up one line | |
1438 | Down: down one line | |
1439 | Ctrl-Left: left one word | |
1440 | Ctrl-Right: right one word | |
1441 | Ctrl-Up: previous paragraph start | |
1442 | Ctrl-Down: next start of paragraph | |
1443 | Home: start of line | |
1444 | End: end of line | |
1445 | Ctrl-Home: start of document | |
1446 | Ctrl-End: end of document | |
1447 | Page-Up: Up a screen | |
1448 | Page-Down: Down a screen | |
1449 | ||
1450 | Maybe: | |
1451 | ||
1452 | Ctrl-Alt-PgUp: Start of window | |
1453 | Ctrl-Alt-PgDn: End of window | |
1454 | F8: Start selection mode | |
1455 | Esc: End selection mode | |
1456 | ||
1457 | Adding Shift does the above but starts/extends selection. | |
1458 | ||
1459 | ||
1460 | */ | |
1461 | ||
0bab774b | 1462 | bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) |
5d7836c4 JS |
1463 | { |
1464 | bool success = false; | |
5d7836c4 | 1465 | |
3e3a754f | 1466 | if (keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT) |
5d7836c4 JS |
1467 | { |
1468 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1469 | success = WordRight(1, flags); | |
1470 | else | |
1471 | success = MoveRight(1, flags); | |
1472 | } | |
3e3a754f | 1473 | else if (keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT) |
5d7836c4 JS |
1474 | { |
1475 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1476 | success = WordLeft(1, flags); | |
1477 | else | |
1478 | success = MoveLeft(1, flags); | |
1479 | } | |
3e3a754f | 1480 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP) |
5d7836c4 JS |
1481 | { |
1482 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1483 | success = MoveToParagraphStart(flags); | |
1484 | else | |
1485 | success = MoveUp(1, flags); | |
1486 | } | |
3e3a754f | 1487 | else if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN) |
5d7836c4 JS |
1488 | { |
1489 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1490 | success = MoveToParagraphEnd(flags); | |
1491 | else | |
1492 | success = MoveDown(1, flags); | |
1493 | } | |
3e3a754f | 1494 | else if (keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP) |
5d7836c4 JS |
1495 | { |
1496 | success = PageUp(1, flags); | |
1497 | } | |
3e3a754f | 1498 | else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) |
5d7836c4 JS |
1499 | { |
1500 | success = PageDown(1, flags); | |
1501 | } | |
3e3a754f | 1502 | else if (keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME) |
5d7836c4 JS |
1503 | { |
1504 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1505 | success = MoveHome(flags); | |
1506 | else | |
1507 | success = MoveToLineStart(flags); | |
1508 | } | |
3e3a754f | 1509 | else if (keyCode == WXK_END || keyCode == WXK_NUMPAD_END) |
5d7836c4 JS |
1510 | { |
1511 | if (flags & wxRICHTEXT_CTRL_DOWN) | |
1512 | success = MoveEnd(flags); | |
1513 | else | |
1514 | success = MoveToLineEnd(flags); | |
1515 | } | |
1516 | ||
1517 | if (success) | |
1518 | { | |
1519 | ScrollIntoView(m_caretPosition, keyCode); | |
0a21bea0 | 1520 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1521 | } |
1522 | ||
5d7836c4 JS |
1523 | return success; |
1524 | } | |
1525 | ||
1526 | /// Extend the selection. Selections are in caret positions. | |
1527 | bool wxRichTextCtrl::ExtendSelection(long oldPos, long newPos, int flags) | |
1528 | { | |
1529 | if (flags & wxRICHTEXT_SHIFT_DOWN) | |
1530 | { | |
e2d0875a JS |
1531 | if (oldPos == newPos) |
1532 | return false; | |
1533 | ||
603f702b | 1534 | wxRichTextSelection oldSelection = m_selection; |
4fe83b93 | 1535 | |
603f702b | 1536 | m_selection.SetContainer(GetFocusObject()); |
4fe83b93 | 1537 | |
603f702b JS |
1538 | wxRichTextRange oldRange; |
1539 | if (m_selection.IsValid()) | |
1540 | oldRange = m_selection.GetRange(); | |
1541 | else | |
1542 | oldRange = wxRICHTEXT_NO_SELECTION; | |
1543 | wxRichTextRange newRange; | |
7051fa41 | 1544 | |
5d7836c4 | 1545 | // If not currently selecting, start selecting |
603f702b | 1546 | if (oldRange.GetStart() == -2) |
5d7836c4 JS |
1547 | { |
1548 | m_selectionAnchor = oldPos; | |
1549 | ||
1550 | if (oldPos > newPos) | |
603f702b | 1551 | newRange.SetRange(newPos+1, oldPos); |
5d7836c4 | 1552 | else |
603f702b | 1553 | newRange.SetRange(oldPos+1, newPos); |
5d7836c4 JS |
1554 | } |
1555 | else | |
1556 | { | |
1557 | // Always ensure that the selection range start is greater than | |
1558 | // the end. | |
1559 | if (newPos > m_selectionAnchor) | |
603f702b | 1560 | newRange.SetRange(m_selectionAnchor+1, newPos); |
e2d0875a | 1561 | else if (newPos == m_selectionAnchor) |
603f702b | 1562 | newRange = wxRichTextRange(-2, -2); |
5d7836c4 | 1563 | else |
603f702b | 1564 | newRange.SetRange(newPos+1, m_selectionAnchor); |
5d7836c4 | 1565 | } |
4fe83b93 | 1566 | |
603f702b | 1567 | m_selection.SetRange(newRange); |
5d7836c4 | 1568 | |
603f702b | 1569 | RefreshForSelectionChange(oldSelection, m_selection); |
7051fa41 | 1570 | |
603f702b | 1571 | if (newRange.GetStart() > newRange.GetEnd()) |
5d7836c4 JS |
1572 | { |
1573 | wxLogDebug(wxT("Strange selection range")); | |
1574 | } | |
7fe8059f | 1575 | |
5d7836c4 JS |
1576 | return true; |
1577 | } | |
1578 | else | |
1579 | return false; | |
1580 | } | |
1581 | ||
1582 | /// Scroll into view, returning true if we scrolled. | |
1583 | /// This takes a _caret_ position. | |
1584 | bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) | |
1585 | { | |
1586 | wxRichTextLine* line = GetVisibleLineForCaretPosition(position); | |
1587 | ||
1588 | if (!line) | |
1589 | return false; | |
1590 | ||
1591 | int ppuX, ppuY; | |
1592 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
1593 | ||
3956d02a JS |
1594 | int startXUnits, startYUnits; |
1595 | GetViewStart(& startXUnits, & startYUnits); | |
1596 | int startY = startYUnits * ppuY; | |
5d7836c4 | 1597 | |
8d7eaf91 | 1598 | int sx = 0, sy = 0; |
5d7836c4 | 1599 | GetVirtualSize(& sx, & sy); |
3956d02a JS |
1600 | int sxUnits = 0; |
1601 | int syUnits = 0; | |
5d7836c4 | 1602 | if (ppuY != 0) |
3956d02a | 1603 | syUnits = sy/ppuY; |
5d7836c4 JS |
1604 | |
1605 | wxRect rect = line->GetRect(); | |
1606 | ||
1607 | bool scrolled = false; | |
1608 | ||
1609 | wxSize clientSize = GetClientSize(); | |
4fe83b93 | 1610 | |
603f702b JS |
1611 | int leftMargin, rightMargin, topMargin, bottomMargin; |
1612 | ||
1613 | { | |
1614 | wxClientDC dc(this); | |
1615 | wxRichTextObject::GetTotalMargin(dc, & GetBuffer(), GetBuffer().GetAttributes(), leftMargin, rightMargin, | |
1616 | topMargin, bottomMargin); | |
1617 | } | |
1618 | // clientSize.y -= GetBuffer().GetBottomMargin(); | |
1619 | clientSize.y -= bottomMargin; | |
e9f10004 JS |
1620 | |
1621 | if (GetWindowStyle() & wxRE_CENTRE_CARET) | |
1622 | { | |
1623 | int y = rect.y - GetClientSize().y/2; | |
1624 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); | |
1625 | if (y >= 0 && (y + clientSize.y) < GetBuffer().GetCachedSize().y) | |
1626 | { | |
1627 | if (startYUnits != yUnits) | |
1628 | { | |
1629 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); | |
1630 | scrolled = true; | |
1631 | } | |
1632 | #if !wxRICHTEXT_USE_OWN_CARET | |
1633 | if (scrolled) | |
1634 | #endif | |
1635 | PositionCaret(); | |
1636 | ||
1637 | return scrolled; | |
1638 | } | |
1639 | } | |
5d7836c4 JS |
1640 | |
1641 | // Going down | |
3e3a754f | 1642 | if (keyCode == WXK_DOWN || keyCode == WXK_NUMPAD_DOWN || |
ec36b155 | 1643 | keyCode == WXK_RIGHT || keyCode == WXK_NUMPAD_RIGHT || |
3e3a754f JS |
1644 | keyCode == WXK_END || keyCode == WXK_NUMPAD_END || |
1645 | keyCode == WXK_PAGEDOWN || keyCode == WXK_NUMPAD_PAGEDOWN) | |
5d7836c4 JS |
1646 | { |
1647 | if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1648 | { | |
1649 | // Make it scroll so this item is at the bottom | |
1650 | // of the window | |
1651 | int y = rect.y - (clientSize.y - rect.height); | |
3956d02a | 1652 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1653 | |
3956d02a JS |
1654 | // If we're still off the screen, scroll another line down |
1655 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1656 | yUnits ++; | |
1657 | ||
1658 | if (startYUnits != yUnits) | |
c59f6793 | 1659 | { |
3956d02a | 1660 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1661 | scrolled = true; |
1662 | } | |
5d7836c4 | 1663 | } |
e9f10004 | 1664 | else if (rect.y < (startY + GetBuffer().GetTopMargin())) |
5d7836c4 JS |
1665 | { |
1666 | // Make it scroll so this item is at the top | |
1667 | // of the window | |
e9f10004 | 1668 | int y = rect.y - GetBuffer().GetTopMargin(); |
3956d02a | 1669 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1670 | |
3956d02a | 1671 | if (startYUnits != yUnits) |
c59f6793 | 1672 | { |
3956d02a | 1673 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1674 | scrolled = true; |
1675 | } | |
5d7836c4 | 1676 | } |
5d7836c4 JS |
1677 | } |
1678 | // Going up | |
3e3a754f JS |
1679 | else if (keyCode == WXK_UP || keyCode == WXK_NUMPAD_UP || |
1680 | keyCode == WXK_LEFT || keyCode == WXK_NUMPAD_LEFT || | |
1681 | keyCode == WXK_HOME || keyCode == WXK_NUMPAD_HOME || | |
1682 | keyCode == WXK_PAGEUP || keyCode == WXK_NUMPAD_PAGEUP ) | |
5d7836c4 | 1683 | { |
e9f10004 | 1684 | if (rect.y < (startY + GetBuffer().GetBottomMargin())) |
5d7836c4 JS |
1685 | { |
1686 | // Make it scroll so this item is at the top | |
1687 | // of the window | |
e9f10004 | 1688 | int y = rect.y - GetBuffer().GetTopMargin(); |
3956d02a | 1689 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
c59f6793 | 1690 | |
3956d02a | 1691 | if (startYUnits != yUnits) |
c59f6793 | 1692 | { |
3956d02a | 1693 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1694 | scrolled = true; |
1695 | } | |
5d7836c4 JS |
1696 | } |
1697 | else if ((rect.y + rect.height) > (clientSize.y + startY)) | |
1698 | { | |
1699 | // Make it scroll so this item is at the bottom | |
1700 | // of the window | |
1701 | int y = rect.y - (clientSize.y - rect.height); | |
3956d02a JS |
1702 | int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); |
1703 | ||
1704 | // If we're still off the screen, scroll another line down | |
1705 | if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) | |
1706 | yUnits ++; | |
c59f6793 | 1707 | |
3956d02a | 1708 | if (startYUnits != yUnits) |
c59f6793 | 1709 | { |
3956d02a | 1710 | SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); |
c59f6793 JS |
1711 | scrolled = true; |
1712 | } | |
5d7836c4 | 1713 | } |
5d7836c4 | 1714 | } |
109bfc88 | 1715 | |
1c13f06e | 1716 | #if !wxRICHTEXT_USE_OWN_CARET |
109bfc88 | 1717 | if (scrolled) |
1c13f06e | 1718 | #endif |
109bfc88 | 1719 | PositionCaret(); |
5d7836c4 JS |
1720 | |
1721 | return scrolled; | |
1722 | } | |
1723 | ||
1724 | /// Is the given position visible on the screen? | |
1725 | bool wxRichTextCtrl::IsPositionVisible(long pos) const | |
1726 | { | |
1727 | wxRichTextLine* line = GetVisibleLineForCaretPosition(pos-1); | |
1728 | ||
1729 | if (!line) | |
1730 | return false; | |
1731 | ||
1732 | int ppuX, ppuY; | |
1733 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
1734 | ||
1735 | int startX, startY; | |
1736 | GetViewStart(& startX, & startY); | |
1737 | startX = 0; | |
1738 | startY = startY * ppuY; | |
1739 | ||
5d7836c4 | 1740 | wxRect rect = line->GetRect(); |
5d7836c4 | 1741 | wxSize clientSize = GetClientSize(); |
e9f10004 | 1742 | clientSize.y -= GetBuffer().GetBottomMargin(); |
5d7836c4 | 1743 | |
1437c7fb | 1744 | return (rect.GetTop() >= (startY + GetBuffer().GetTopMargin())) && (rect.GetBottom() <= (startY + clientSize.y)); |
5d7836c4 JS |
1745 | } |
1746 | ||
1747 | void wxRichTextCtrl::SetCaretPosition(long position, bool showAtLineStart) | |
1748 | { | |
1749 | m_caretPosition = position; | |
1750 | m_caretAtLineStart = showAtLineStart; | |
1751 | } | |
1752 | ||
1753 | /// Move caret one visual step forward: this may mean setting a flag | |
1754 | /// and keeping the same position if we're going from the end of one line | |
1755 | /// to the start of the next, which may be the exact same caret position. | |
1756 | void wxRichTextCtrl::MoveCaretForward(long oldPosition) | |
1757 | { | |
603f702b | 1758 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition); |
5d7836c4 JS |
1759 | |
1760 | // Only do the check if we're not at the end of the paragraph (where things work OK | |
1761 | // anyway) | |
1762 | if (para && (oldPosition != para->GetRange().GetEnd() - 1)) | |
1763 | { | |
603f702b | 1764 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition); |
5d7836c4 JS |
1765 | |
1766 | if (line) | |
1767 | { | |
1e967276 JS |
1768 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1769 | ||
5d7836c4 JS |
1770 | // We're at the end of a line. See whether we need to |
1771 | // stay at the same actual caret position but change visual | |
1772 | // position, or not. | |
1e967276 | 1773 | if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
1774 | { |
1775 | if (m_caretAtLineStart) | |
1776 | { | |
1777 | // We're already at the start of the line, so actually move on now. | |
1778 | m_caretPosition = oldPosition + 1; | |
1779 | m_caretAtLineStart = false; | |
1780 | } | |
1781 | else | |
1782 | { | |
1783 | // We're showing at the end of the line, so keep to | |
1784 | // the same position but indicate that we're to show | |
1785 | // at the start of the next line. | |
1786 | m_caretPosition = oldPosition; | |
7fe8059f | 1787 | m_caretAtLineStart = true; |
5d7836c4 | 1788 | } |
0a21bea0 | 1789 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1790 | return; |
1791 | } | |
1792 | } | |
1793 | } | |
1794 | m_caretPosition ++; | |
0a21bea0 | 1795 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1796 | } |
1797 | ||
1798 | /// Move caret one visual step backward: this may mean setting a flag | |
1799 | /// and keeping the same position if we're going from the end of one line | |
1800 | /// to the start of the next, which may be the exact same caret position. | |
1801 | void wxRichTextCtrl::MoveCaretBack(long oldPosition) | |
1802 | { | |
603f702b | 1803 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(oldPosition); |
5d7836c4 JS |
1804 | |
1805 | // Only do the check if we're not at the start of the paragraph (where things work OK | |
1806 | // anyway) | |
1807 | if (para && (oldPosition != para->GetRange().GetStart())) | |
1808 | { | |
603f702b | 1809 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(oldPosition); |
5d7836c4 JS |
1810 | |
1811 | if (line) | |
1812 | { | |
1e967276 JS |
1813 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
1814 | ||
5d7836c4 JS |
1815 | // We're at the start of a line. See whether we need to |
1816 | // stay at the same actual caret position but change visual | |
1817 | // position, or not. | |
1e967276 | 1818 | if (oldPosition == lineRange.GetStart()) |
5d7836c4 JS |
1819 | { |
1820 | m_caretPosition = oldPosition-1; | |
1821 | m_caretAtLineStart = true; | |
1822 | return; | |
1823 | } | |
1e967276 | 1824 | else if (oldPosition == lineRange.GetEnd()) |
5d7836c4 JS |
1825 | { |
1826 | if (m_caretAtLineStart) | |
1827 | { | |
1828 | // We're at the start of the line, so keep the same caret position | |
1829 | // but clear the start-of-line flag. | |
1830 | m_caretPosition = oldPosition; | |
1831 | m_caretAtLineStart = false; | |
1832 | } | |
1833 | else | |
1834 | { | |
1835 | // We're showing at the end of the line, so go back | |
1836 | // to the previous character position. | |
1837 | m_caretPosition = oldPosition - 1; | |
1838 | } | |
0a21bea0 | 1839 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1840 | return; |
1841 | } | |
1842 | } | |
1843 | } | |
1844 | m_caretPosition --; | |
0a21bea0 | 1845 | SetDefaultStyleToCursorStyle(); |
5d7836c4 JS |
1846 | } |
1847 | ||
1848 | /// Move right | |
1849 | bool wxRichTextCtrl::MoveRight(int noPositions, int flags) | |
1850 | { | |
603f702b | 1851 | long endPos = GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
1852 | |
1853 | if (m_caretPosition + noPositions < endPos) | |
1854 | { | |
1855 | long oldPos = m_caretPosition; | |
1856 | long newPos = m_caretPosition + noPositions; | |
1857 | ||
1858 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1859 | if (!extendSel) | |
1860 | SelectNone(); | |
1861 | ||
1862 | // Determine by looking at oldPos and m_caretPosition whether | |
1863 | // we moved from the end of a line to the start of the next line, in which case | |
1864 | // we want to adjust the caret position such that it is positioned at the | |
1865 | // start of the next line, rather than jumping past the first character of the | |
1866 | // line. | |
1867 | if (noPositions == 1 && !extendSel) | |
1868 | MoveCaretForward(oldPos); | |
1869 | else | |
1870 | SetCaretPosition(newPos); | |
1871 | ||
1872 | PositionCaret(); | |
0a21bea0 | 1873 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 1874 | |
5d7836c4 JS |
1875 | return true; |
1876 | } | |
1877 | else | |
7fe8059f | 1878 | return false; |
5d7836c4 JS |
1879 | } |
1880 | ||
1881 | /// Move left | |
1882 | bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) | |
1883 | { | |
1884 | long startPos = -1; | |
1885 | ||
1886 | if (m_caretPosition > startPos - noPositions + 1) | |
1887 | { | |
1888 | long oldPos = m_caretPosition; | |
1889 | long newPos = m_caretPosition - noPositions; | |
1890 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); | |
1891 | if (!extendSel) | |
1892 | SelectNone(); | |
1893 | ||
1894 | if (noPositions == 1 && !extendSel) | |
1895 | MoveCaretBack(oldPos); | |
1896 | else | |
1897 | SetCaretPosition(newPos); | |
1898 | ||
1899 | PositionCaret(); | |
0a21bea0 | 1900 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 1901 | |
5d7836c4 JS |
1902 | return true; |
1903 | } | |
1904 | else | |
7fe8059f | 1905 | return false; |
5d7836c4 JS |
1906 | } |
1907 | ||
603f702b JS |
1908 | // Find the caret position for the combination of hit-test flags and character position. |
1909 | // Returns the caret position and also an indication of where to place the caret (caretLineStart) | |
1910 | // since this is ambiguous (same position used for end of line and start of next). | |
1911 | long wxRichTextCtrl::FindCaretPositionForCharacterPosition(long position, int hitTestFlags, wxRichTextParagraphLayoutBox* container, | |
1912 | bool& caretLineStart) | |
1913 | { | |
1914 | // If end of previous line, and hitTest is wxRICHTEXT_HITTEST_BEFORE, | |
1915 | // we want to be at the end of the last line but with m_caretAtLineStart set to true, | |
1916 | // so we view the caret at the start of the line. | |
1917 | caretLineStart = false; | |
1918 | long caretPosition = position; | |
1919 | ||
1920 | if (hitTestFlags & wxRICHTEXT_HITTEST_BEFORE) | |
1921 | { | |
1922 | wxRichTextLine* thisLine = container->GetLineAtPosition(position-1); | |
1923 | wxRichTextRange lineRange; | |
1924 | if (thisLine) | |
1925 | lineRange = thisLine->GetAbsoluteRange(); | |
1926 | ||
1927 | if (thisLine && (position-1) == lineRange.GetEnd()) | |
1928 | { | |
1929 | caretPosition --; | |
1930 | caretLineStart = true; | |
1931 | } | |
1932 | else | |
1933 | { | |
1934 | wxRichTextParagraph* para = container->GetParagraphAtPosition(position); | |
1935 | if (para && para->GetRange().GetStart() == position) | |
1936 | caretPosition --; | |
1937 | } | |
1938 | } | |
1939 | return caretPosition; | |
1940 | } | |
1941 | ||
5d7836c4 JS |
1942 | /// Move up |
1943 | bool wxRichTextCtrl::MoveUp(int noLines, int flags) | |
1944 | { | |
1945 | return MoveDown(- noLines, flags); | |
1946 | } | |
1947 | ||
1948 | /// Move up | |
1949 | bool wxRichTextCtrl::MoveDown(int noLines, int flags) | |
1950 | { | |
c59f6793 JS |
1951 | if (!GetCaret()) |
1952 | return false; | |
1953 | ||
603f702b | 1954 | long lineNumber = GetFocusObject()->GetVisibleLineNumber(m_caretPosition, true, m_caretAtLineStart); |
5d7836c4 | 1955 | wxPoint pt = GetCaret()->GetPosition(); |
4fe83b93 | 1956 | long newLine = lineNumber + noLines; |
603f702b | 1957 | bool notInThisObject = false; |
5d7836c4 JS |
1958 | |
1959 | if (lineNumber != -1) | |
1960 | { | |
1961 | if (noLines > 0) | |
1962 | { | |
603f702b | 1963 | long lastLine = GetFocusObject()->GetVisibleLineNumber(GetFocusObject()->GetOwnRange().GetEnd()); |
5d7836c4 | 1964 | if (newLine > lastLine) |
603f702b | 1965 | notInThisObject = true; |
5d7836c4 JS |
1966 | } |
1967 | else | |
1968 | { | |
1969 | if (newLine < 0) | |
603f702b | 1970 | notInThisObject = true; |
5d7836c4 JS |
1971 | } |
1972 | } | |
4fe83b93 | 1973 | |
603f702b | 1974 | wxRichTextParagraphLayoutBox* container = GetFocusObject(); |
343ef639 | 1975 | int hitTestFlags = wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS|wxRICHTEXT_HITTEST_NO_FLOATING_OBJECTS; |
5d7836c4 | 1976 | |
603f702b | 1977 | if (notInThisObject) |
7fe8059f | 1978 | { |
603f702b JS |
1979 | // If we know we're navigating out of the current object, |
1980 | // try to find an object anywhere in the buffer at the new position (up or down a bit) | |
1981 | container = & GetBuffer(); | |
343ef639 | 1982 | hitTestFlags &= ~wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS; |
4fe83b93 | 1983 | |
603f702b JS |
1984 | if (noLines > 0) // going down |
1985 | { | |
1986 | pt.y = GetFocusObject()->GetPosition().y + GetFocusObject()->GetCachedSize().y + 2; | |
1987 | } | |
1988 | else // going up | |
1989 | { | |
1990 | pt.y = GetFocusObject()->GetPosition().y - 2; | |
1991 | } | |
5d7836c4 JS |
1992 | } |
1993 | else | |
603f702b JS |
1994 | { |
1995 | wxRichTextLine* lineObj = GetFocusObject()->GetLineForVisibleLineNumber(newLine); | |
1996 | if (lineObj) | |
1997 | pt.y = lineObj->GetAbsolutePosition().y + 2; | |
1998 | else | |
1999 | return false; | |
2000 | } | |
5d7836c4 JS |
2001 | |
2002 | long newPos = 0; | |
2003 | wxClientDC dc(this); | |
2004 | PrepareDC(dc); | |
2005 | dc.SetFont(GetFont()); | |
7fe8059f | 2006 | |
603f702b JS |
2007 | wxRichTextObject* hitObj = NULL; |
2008 | wxRichTextObject* contextObj = NULL; | |
2009 | int hitTest = container->HitTest(dc, pt, newPos, & hitObj, & contextObj, hitTestFlags); | |
7fe8059f | 2010 | |
343ef639 JS |
2011 | if (hitObj && |
2012 | ((hitTest & wxRICHTEXT_HITTEST_NONE) == 0) && | |
2013 | (! (hitObj == (& m_buffer) && ((hitTest & wxRICHTEXT_HITTEST_OUTSIDE) != 0))) // outside the buffer counts as 'do nothing' | |
2014 | ) | |
5d7836c4 | 2015 | { |
603f702b | 2016 | if (notInThisObject) |
5d7836c4 | 2017 | { |
603f702b JS |
2018 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
2019 | if (actualContainer && actualContainer != GetFocusObject() && actualContainer->AcceptsFocus()) | |
5d7836c4 | 2020 | { |
603f702b | 2021 | SetFocusObject(actualContainer, false /* don't set caret position yet */); |
4fe83b93 | 2022 | |
603f702b | 2023 | container = actualContainer; |
5d7836c4 JS |
2024 | } |
2025 | } | |
4fe83b93 | 2026 | |
603f702b JS |
2027 | bool caretLineStart = true; |
2028 | long caretPosition = FindCaretPositionForCharacterPosition(newPos, hitTest, container, caretLineStart); | |
2029 | long newSelEnd = caretPosition; | |
2030 | bool extendSel; | |
2031 | ||
2032 | if (notInThisObject) | |
2033 | extendSel = false; | |
2034 | else | |
2035 | extendSel = ExtendSelection(m_caretPosition, newSelEnd, flags); | |
5d7836c4 | 2036 | |
c59f6793 | 2037 | if (!extendSel) |
5d7836c4 JS |
2038 | SelectNone(); |
2039 | ||
603f702b | 2040 | SetCaretPosition(caretPosition, caretLineStart); |
5d7836c4 | 2041 | PositionCaret(); |
0a21bea0 | 2042 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2043 | |
5d7836c4 JS |
2044 | return true; |
2045 | } | |
41e155b4 WS |
2046 | |
2047 | return false; | |
5d7836c4 JS |
2048 | } |
2049 | ||
2050 | /// Move to the end of the paragraph | |
2051 | bool wxRichTextCtrl::MoveToParagraphEnd(int flags) | |
2052 | { | |
603f702b | 2053 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true); |
5d7836c4 JS |
2054 | if (para) |
2055 | { | |
2056 | long newPos = para->GetRange().GetEnd() - 1; | |
c59f6793 JS |
2057 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2058 | if (!extendSel) | |
5d7836c4 JS |
2059 | SelectNone(); |
2060 | ||
2061 | SetCaretPosition(newPos); | |
2062 | PositionCaret(); | |
0a21bea0 | 2063 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2064 | |
5d7836c4 JS |
2065 | return true; |
2066 | } | |
2067 | ||
2068 | return false; | |
2069 | } | |
2070 | ||
2071 | /// Move to the start of the paragraph | |
2072 | bool wxRichTextCtrl::MoveToParagraphStart(int flags) | |
2073 | { | |
603f702b | 2074 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(m_caretPosition, true); |
5d7836c4 JS |
2075 | if (para) |
2076 | { | |
2077 | long newPos = para->GetRange().GetStart() - 1; | |
c59f6793 JS |
2078 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2079 | if (!extendSel) | |
5d7836c4 JS |
2080 | SelectNone(); |
2081 | ||
2082 | SetCaretPosition(newPos); | |
2083 | PositionCaret(); | |
0a21bea0 | 2084 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2085 | |
5d7836c4 JS |
2086 | return true; |
2087 | } | |
2088 | ||
2089 | return false; | |
2090 | } | |
2091 | ||
2092 | /// Move to the end of the line | |
2093 | bool wxRichTextCtrl::MoveToLineEnd(int flags) | |
2094 | { | |
2095 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2096 | ||
2097 | if (line) | |
2098 | { | |
1e967276 JS |
2099 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2100 | long newPos = lineRange.GetEnd(); | |
c59f6793 JS |
2101 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2102 | if (!extendSel) | |
5d7836c4 JS |
2103 | SelectNone(); |
2104 | ||
2105 | SetCaretPosition(newPos); | |
2106 | PositionCaret(); | |
0a21bea0 | 2107 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2108 | |
5d7836c4 JS |
2109 | return true; |
2110 | } | |
2111 | ||
2112 | return false; | |
2113 | } | |
2114 | ||
2115 | /// Move to the start of the line | |
2116 | bool wxRichTextCtrl::MoveToLineStart(int flags) | |
2117 | { | |
2118 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2119 | if (line) | |
2120 | { | |
1e967276 JS |
2121 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
2122 | long newPos = lineRange.GetStart()-1; | |
5d7836c4 | 2123 | |
c59f6793 JS |
2124 | bool extendSel = ExtendSelection(m_caretPosition, newPos, flags); |
2125 | if (!extendSel) | |
5d7836c4 JS |
2126 | SelectNone(); |
2127 | ||
603f702b | 2128 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(line); |
5d7836c4 | 2129 | |
1e967276 | 2130 | SetCaretPosition(newPos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 | 2131 | PositionCaret(); |
0a21bea0 | 2132 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2133 | |
5d7836c4 JS |
2134 | return true; |
2135 | } | |
2136 | ||
2137 | return false; | |
2138 | } | |
2139 | ||
2140 | /// Move to the start of the buffer | |
2141 | bool wxRichTextCtrl::MoveHome(int flags) | |
2142 | { | |
2143 | if (m_caretPosition != -1) | |
2144 | { | |
c59f6793 JS |
2145 | bool extendSel = ExtendSelection(m_caretPosition, -1, flags); |
2146 | if (!extendSel) | |
5d7836c4 JS |
2147 | SelectNone(); |
2148 | ||
2149 | SetCaretPosition(-1); | |
2150 | PositionCaret(); | |
0a21bea0 | 2151 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2152 | |
5d7836c4 JS |
2153 | return true; |
2154 | } | |
2155 | else | |
7fe8059f | 2156 | return false; |
5d7836c4 JS |
2157 | } |
2158 | ||
2159 | /// Move to the end of the buffer | |
2160 | bool wxRichTextCtrl::MoveEnd(int flags) | |
2161 | { | |
603f702b | 2162 | long endPos = GetFocusObject()->GetOwnRange().GetEnd()-1; |
5d7836c4 JS |
2163 | |
2164 | if (m_caretPosition != endPos) | |
2165 | { | |
c59f6793 JS |
2166 | bool extendSel = ExtendSelection(m_caretPosition, endPos, flags); |
2167 | if (!extendSel) | |
5d7836c4 JS |
2168 | SelectNone(); |
2169 | ||
2170 | SetCaretPosition(endPos); | |
2171 | PositionCaret(); | |
0a21bea0 | 2172 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2173 | |
5d7836c4 JS |
2174 | return true; |
2175 | } | |
2176 | else | |
7fe8059f | 2177 | return false; |
5d7836c4 JS |
2178 | } |
2179 | ||
2180 | /// Move noPages pages up | |
2181 | bool wxRichTextCtrl::PageUp(int noPages, int flags) | |
2182 | { | |
2183 | return PageDown(- noPages, flags); | |
2184 | } | |
2185 | ||
2186 | /// Move noPages pages down | |
2187 | bool wxRichTextCtrl::PageDown(int noPages, int flags) | |
2188 | { | |
2189 | // Calculate which line occurs noPages * screen height further down. | |
2190 | wxRichTextLine* line = GetVisibleLineForCaretPosition(m_caretPosition); | |
2191 | if (line) | |
2192 | { | |
2193 | wxSize clientSize = GetClientSize(); | |
2194 | int newY = line->GetAbsolutePosition().y + noPages*clientSize.y; | |
2195 | ||
603f702b | 2196 | wxRichTextLine* newLine = GetFocusObject()->GetLineAtYPosition(newY); |
5d7836c4 JS |
2197 | if (newLine) |
2198 | { | |
1e967276 JS |
2199 | wxRichTextRange lineRange = newLine->GetAbsoluteRange(); |
2200 | long pos = lineRange.GetStart()-1; | |
5d7836c4 JS |
2201 | if (pos != m_caretPosition) |
2202 | { | |
603f702b | 2203 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphForLine(newLine); |
5d7836c4 | 2204 | |
c59f6793 JS |
2205 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2206 | if (!extendSel) | |
5d7836c4 JS |
2207 | SelectNone(); |
2208 | ||
1e967276 | 2209 | SetCaretPosition(pos, para->GetRange().GetStart() != lineRange.GetStart()); |
5d7836c4 | 2210 | PositionCaret(); |
0a21bea0 | 2211 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2212 | |
5d7836c4 JS |
2213 | return true; |
2214 | } | |
2215 | } | |
2216 | } | |
2217 | ||
2218 | return false; | |
2219 | } | |
2220 | ||
0871b343 JS |
2221 | static bool wxRichTextCtrlIsWhitespace(const wxString& str) |
2222 | { | |
2223 | return str == wxT(" ") || str == wxT("\t"); | |
2224 | } | |
2225 | ||
5d7836c4 JS |
2226 | // Finds the caret position for the next word |
2227 | long wxRichTextCtrl::FindNextWordPosition(int direction) const | |
7fe8059f | 2228 | { |
603f702b | 2229 | long endPos = GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
2230 | |
2231 | if (direction > 0) | |
2232 | { | |
2233 | long i = m_caretPosition+1+direction; // +1 for conversion to character pos | |
7fe8059f | 2234 | |
5d7836c4 JS |
2235 | // First skip current text to space |
2236 | while (i < endPos && i > -1) | |
2237 | { | |
2238 | // i is in character, not caret positions | |
603f702b JS |
2239 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2240 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2241 | if (line && (i == line->GetAbsoluteRange().GetEnd())) |
2242 | { | |
2243 | break; | |
2244 | } | |
2245 | else if (!wxRichTextCtrlIsWhitespace(text) && !text.empty()) | |
5d7836c4 JS |
2246 | i += direction; |
2247 | else | |
2248 | { | |
2249 | break; | |
2250 | } | |
2251 | } | |
2252 | while (i < endPos && i > -1) | |
2253 | { | |
2254 | // i is in character, not caret positions | |
603f702b JS |
2255 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2256 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2257 | if (line && (i == line->GetAbsoluteRange().GetEnd())) |
2258 | return wxMax(-1, i); | |
2259 | ||
7fe8059f | 2260 | if (text.empty()) // End of paragraph, or maybe an image |
5d7836c4 | 2261 | return wxMax(-1, i - 1); |
0871b343 | 2262 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) |
5d7836c4 JS |
2263 | i += direction; |
2264 | else | |
2265 | { | |
2266 | // Convert to caret position | |
2267 | return wxMax(-1, i - 1); | |
2268 | } | |
2269 | } | |
2270 | if (i >= endPos) | |
2271 | return endPos-1; | |
2272 | return i-1; | |
2273 | } | |
2274 | else | |
2275 | { | |
2276 | long i = m_caretPosition; | |
2277 | ||
2278 | // First skip white space | |
2279 | while (i < endPos && i > -1) | |
2280 | { | |
2281 | // i is in character, not caret positions | |
603f702b JS |
2282 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2283 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2284 | |
2285 | if (text.empty() || (line && (i == line->GetAbsoluteRange().GetStart()))) // End of paragraph, or maybe an image | |
5d7836c4 | 2286 | break; |
0871b343 | 2287 | else if (wxRichTextCtrlIsWhitespace(text) || text.empty()) |
5d7836c4 JS |
2288 | i += direction; |
2289 | else | |
2290 | break; | |
2291 | } | |
2292 | // Next skip current text to space | |
2293 | while (i < endPos && i > -1) | |
2294 | { | |
2295 | // i is in character, not caret positions | |
603f702b JS |
2296 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(i, i)); |
2297 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(i, false); | |
0871b343 JS |
2298 | if (line && line->GetAbsoluteRange().GetStart() == i) |
2299 | return i-1; | |
2300 | ||
2301 | if (!wxRichTextCtrlIsWhitespace(text) /* && !text.empty() */) | |
5d7836c4 JS |
2302 | i += direction; |
2303 | else | |
2304 | { | |
2305 | return i; | |
5d7836c4 JS |
2306 | } |
2307 | } | |
2308 | if (i < -1) | |
2309 | return -1; | |
2310 | return i; | |
2311 | } | |
2312 | } | |
2313 | ||
2314 | /// Move n words left | |
2315 | bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) | |
2316 | { | |
2317 | long pos = FindNextWordPosition(-1); | |
2318 | if (pos != m_caretPosition) | |
2319 | { | |
603f702b | 2320 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true); |
7fe8059f | 2321 | |
c59f6793 JS |
2322 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2323 | if (!extendSel) | |
5d7836c4 | 2324 | SelectNone(); |
7fe8059f | 2325 | |
5d7836c4 JS |
2326 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
2327 | PositionCaret(); | |
0a21bea0 | 2328 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2329 | |
5d7836c4 JS |
2330 | return true; |
2331 | } | |
7fe8059f | 2332 | |
5d7836c4 JS |
2333 | return false; |
2334 | } | |
2335 | ||
2336 | /// Move n words right | |
2337 | bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) | |
2338 | { | |
2339 | long pos = FindNextWordPosition(1); | |
2340 | if (pos != m_caretPosition) | |
2341 | { | |
603f702b | 2342 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos, true); |
7fe8059f | 2343 | |
c59f6793 JS |
2344 | bool extendSel = ExtendSelection(m_caretPosition, pos, flags); |
2345 | if (!extendSel) | |
5d7836c4 | 2346 | SelectNone(); |
7fe8059f | 2347 | |
5d7836c4 JS |
2348 | SetCaretPosition(pos, para->GetRange().GetStart() != pos); |
2349 | PositionCaret(); | |
0a21bea0 | 2350 | SetDefaultStyleToCursorStyle(); |
5d7836c4 | 2351 | |
5d7836c4 JS |
2352 | return true; |
2353 | } | |
7fe8059f | 2354 | |
5d7836c4 JS |
2355 | return false; |
2356 | } | |
2357 | ||
2358 | /// Sizing | |
2359 | void wxRichTextCtrl::OnSize(wxSizeEvent& event) | |
2360 | { | |
4d551ad5 | 2361 | // Only do sizing optimization for large buffers |
603f702b | 2362 | if (GetBuffer().GetOwnRange().GetEnd() > m_delayedLayoutThreshold) |
4d551ad5 JS |
2363 | { |
2364 | m_fullLayoutRequired = true; | |
2365 | m_fullLayoutTime = wxGetLocalTimeMillis(); | |
2366 | m_fullLayoutSavedPosition = GetFirstVisiblePosition(); | |
2f36e8dc | 2367 | LayoutContent(true /* onlyVisibleRect */); |
4d551ad5 JS |
2368 | } |
2369 | else | |
2370 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
7fe8059f | 2371 | |
3e3a754f | 2372 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 | 2373 | RecreateBuffer(); |
3e3a754f | 2374 | #endif |
5d7836c4 JS |
2375 | |
2376 | event.Skip(); | |
2377 | } | |
2378 | ||
98769552 JS |
2379 | // Force any pending layout due to large buffer |
2380 | void wxRichTextCtrl::ForceDelayedLayout() | |
2381 | { | |
2382 | if (m_fullLayoutRequired) | |
2383 | { | |
2384 | m_fullLayoutRequired = false; | |
2385 | m_fullLayoutTime = 0; | |
2386 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
2387 | ShowPosition(m_fullLayoutSavedPosition); | |
2388 | Refresh(false); | |
2389 | Update(); | |
2390 | } | |
2391 | } | |
4d551ad5 JS |
2392 | |
2393 | /// Idle-time processing | |
2394 | void wxRichTextCtrl::OnIdle(wxIdleEvent& event) | |
2395 | { | |
1c13f06e JS |
2396 | #if wxRICHTEXT_USE_OWN_CARET |
2397 | if (((wxRichTextCaret*) GetCaret())->GetNeedsUpdate()) | |
2398 | { | |
2399 | ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(false); | |
2400 | PositionCaret(); | |
2401 | GetCaret()->Show(); | |
2402 | } | |
2403 | #endif | |
2404 | ||
4d551ad5 JS |
2405 | const int layoutInterval = wxRICHTEXT_DEFAULT_LAYOUT_INTERVAL; |
2406 | ||
2407 | if (m_fullLayoutRequired && (wxGetLocalTimeMillis() > (m_fullLayoutTime + layoutInterval))) | |
2408 | { | |
2409 | m_fullLayoutRequired = false; | |
2410 | m_fullLayoutTime = 0; | |
2411 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
2412 | ShowPosition(m_fullLayoutSavedPosition); | |
76bcd815 | 2413 | Refresh(false); |
4d551ad5 | 2414 | } |
ab14c7aa JS |
2415 | |
2416 | if (m_caretPositionForDefaultStyle != -2) | |
2417 | { | |
2418 | // If the caret position has changed, no longer reflect the default style | |
2419 | // in the UI. | |
2420 | if (GetCaretPosition() != m_caretPositionForDefaultStyle) | |
2421 | m_caretPositionForDefaultStyle = -2; | |
2422 | } | |
2423 | ||
4d551ad5 JS |
2424 | event.Skip(); |
2425 | } | |
2426 | ||
c59f6793 JS |
2427 | /// Scrolling |
2428 | void wxRichTextCtrl::OnScroll(wxScrollWinEvent& event) | |
2429 | { | |
1c13f06e JS |
2430 | #if wxRICHTEXT_USE_OWN_CARET |
2431 | if (!((wxRichTextCaret*) GetCaret())->GetNeedsUpdate()) | |
2432 | { | |
2433 | GetCaret()->Hide(); | |
2434 | ((wxRichTextCaret*) GetCaret())->SetNeedsUpdate(); | |
2435 | } | |
2436 | #endif | |
2437 | ||
c59f6793 JS |
2438 | event.Skip(); |
2439 | } | |
2440 | ||
5d7836c4 | 2441 | /// Set up scrollbars, e.g. after a resize |
169adfa9 | 2442 | void wxRichTextCtrl::SetupScrollbars(bool atTop) |
5d7836c4 | 2443 | { |
17808a75 | 2444 | if (IsFrozen()) |
5d7836c4 JS |
2445 | return; |
2446 | ||
2447 | if (GetBuffer().IsEmpty()) | |
2448 | { | |
2449 | SetScrollbars(0, 0, 0, 0, 0, 0); | |
2450 | return; | |
2451 | } | |
2452 | ||
2453 | // TODO: reimplement scrolling so we scroll by line, not by fixed number | |
2454 | // of pixels. See e.g. wxVScrolledWindow for ideas. | |
3956d02a | 2455 | int pixelsPerUnit = 5; |
5d7836c4 JS |
2456 | wxSize clientSize = GetClientSize(); |
2457 | ||
e9f10004 | 2458 | int maxHeight = GetBuffer().GetCachedSize().y + GetBuffer().GetTopMargin(); |
5d7836c4 | 2459 | |
3956d02a JS |
2460 | // Round up so we have at least maxHeight pixels |
2461 | int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5); | |
5d7836c4 | 2462 | |
169adfa9 JS |
2463 | int startX = 0, startY = 0; |
2464 | if (!atTop) | |
2465 | GetViewStart(& startX, & startY); | |
7fe8059f | 2466 | |
7051fa41 | 2467 | int maxPositionX = 0; |
3956d02a | 2468 | int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5); |
7fe8059f | 2469 | |
7051fa41 JS |
2470 | int newStartX = wxMin(maxPositionX, startX); |
2471 | int newStartY = wxMin(maxPositionY, startY); | |
2472 | ||
2473 | int oldPPUX, oldPPUY; | |
2474 | int oldStartX, oldStartY; | |
2475 | int oldVirtualSizeX = 0, oldVirtualSizeY = 0; | |
2476 | GetScrollPixelsPerUnit(& oldPPUX, & oldPPUY); | |
2477 | GetViewStart(& oldStartX, & oldStartY); | |
2478 | GetVirtualSize(& oldVirtualSizeX, & oldVirtualSizeY); | |
2479 | if (oldPPUY > 0) | |
2480 | oldVirtualSizeY /= oldPPUY; | |
2481 | ||
2482 | if (oldPPUX == 0 && oldPPUY == pixelsPerUnit && oldVirtualSizeY == unitsY && oldStartX == newStartX && oldStartY == newStartY) | |
2483 | return; | |
2484 | ||
2485 | // Don't set scrollbars if there were none before, and there will be none now. | |
205a1ad9 | 2486 | if (oldPPUY != 0 && (oldVirtualSizeY*oldPPUY < clientSize.y) && (unitsY*pixelsPerUnit < clientSize.y)) |
7051fa41 JS |
2487 | return; |
2488 | ||
5d7836c4 JS |
2489 | // Move to previous scroll position if |
2490 | // possible | |
7051fa41 | 2491 | SetScrollbars(0, pixelsPerUnit, 0, unitsY, newStartX, newStartY); |
5d7836c4 JS |
2492 | } |
2493 | ||
2494 | /// Paint the background | |
2495 | void wxRichTextCtrl::PaintBackground(wxDC& dc) | |
2496 | { | |
2497 | wxColour backgroundColour = GetBackgroundColour(); | |
a1b806b9 | 2498 | if (!backgroundColour.IsOk()) |
5d7836c4 JS |
2499 | backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
2500 | ||
2501 | // Clear the background | |
2502 | dc.SetBrush(wxBrush(backgroundColour)); | |
2503 | dc.SetPen(*wxTRANSPARENT_PEN); | |
7fe8059f | 2504 | wxRect windowRect(GetClientSize()); |
5d7836c4 JS |
2505 | windowRect.x -= 2; windowRect.y -= 2; |
2506 | windowRect.width += 4; windowRect.height += 4; | |
7fe8059f | 2507 | |
5d7836c4 JS |
2508 | // We need to shift the rectangle to take into account |
2509 | // scrolling. Converting device to logical coordinates. | |
2510 | CalcUnscrolledPosition(windowRect.x, windowRect.y, & windowRect.x, & windowRect.y); | |
2511 | dc.DrawRectangle(windowRect); | |
2512 | } | |
2513 | ||
3e3a754f | 2514 | #if wxRICHTEXT_BUFFERED_PAINTING |
5d7836c4 JS |
2515 | /// Recreate buffer bitmap if necessary |
2516 | bool wxRichTextCtrl::RecreateBuffer(const wxSize& size) | |
2517 | { | |
2518 | wxSize sz = size; | |
2519 | if (sz == wxDefaultSize) | |
2520 | sz = GetClientSize(); | |
7fe8059f | 2521 | |
5d7836c4 JS |
2522 | if (sz.x < 1 || sz.y < 1) |
2523 | return false; | |
7fe8059f | 2524 | |
a1b806b9 | 2525 | if (!m_bufferBitmap.IsOk() || m_bufferBitmap.GetWidth() < sz.x || m_bufferBitmap.GetHeight() < sz.y) |
5d7836c4 | 2526 | m_bufferBitmap = wxBitmap(sz.x, sz.y); |
a1b806b9 | 2527 | return m_bufferBitmap.IsOk(); |
5d7836c4 | 2528 | } |
3e3a754f | 2529 | #endif |
5d7836c4 JS |
2530 | |
2531 | // ---------------------------------------------------------------------------- | |
2532 | // file IO functions | |
2533 | // ---------------------------------------------------------------------------- | |
2534 | ||
3306aec1 JS |
2535 | bool wxRichTextCtrl::DoLoadFile(const wxString& filename, int fileType) |
2536 | { | |
d75a69e8 | 2537 | bool success = GetBuffer().LoadFile(filename, (wxRichTextFileType)fileType); |
5d7836c4 JS |
2538 | if (success) |
2539 | m_filename = filename; | |
2540 | ||
2541 | DiscardEdits(); | |
2542 | SetInsertionPoint(0); | |
2f36e8dc | 2543 | LayoutContent(); |
5d7836c4 | 2544 | PositionCaret(); |
169adfa9 | 2545 | SetupScrollbars(true); |
76bcd815 | 2546 | Refresh(false); |
0ec1179b | 2547 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 JS |
2548 | |
2549 | if (success) | |
2550 | return true; | |
2551 | else | |
2552 | { | |
2553 | wxLogError(_("File couldn't be loaded.")); | |
7fe8059f | 2554 | |
5d7836c4 JS |
2555 | return false; |
2556 | } | |
2557 | } | |
2558 | ||
3306aec1 | 2559 | bool wxRichTextCtrl::DoSaveFile(const wxString& filename, int fileType) |
5d7836c4 | 2560 | { |
d75a69e8 | 2561 | if (GetBuffer().SaveFile(filename, (wxRichTextFileType)fileType)) |
5d7836c4 | 2562 | { |
3306aec1 | 2563 | m_filename = filename; |
5d7836c4 JS |
2564 | |
2565 | DiscardEdits(); | |
2566 | ||
2567 | return true; | |
5d7836c4 JS |
2568 | } |
2569 | ||
2570 | wxLogError(_("The text couldn't be saved.")); | |
2571 | ||
2572 | return false; | |
2573 | } | |
2574 | ||
2575 | // ---------------------------------------------------------------------------- | |
2576 | // wxRichTextCtrl specific functionality | |
2577 | // ---------------------------------------------------------------------------- | |
2578 | ||
2579 | /// Add a new paragraph of text to the end of the buffer | |
2580 | wxRichTextRange wxRichTextCtrl::AddParagraph(const wxString& text) | |
2581 | { | |
603f702b | 2582 | wxRichTextRange range = GetFocusObject()->AddParagraph(text); |
4fe83b93 | 2583 | GetBuffer().Invalidate(); |
62381daa JS |
2584 | LayoutContent(); |
2585 | return range; | |
5d7836c4 JS |
2586 | } |
2587 | ||
2588 | /// Add an image | |
2589 | wxRichTextRange wxRichTextCtrl::AddImage(const wxImage& image) | |
2590 | { | |
603f702b | 2591 | wxRichTextRange range = GetFocusObject()->AddImage(image); |
4fe83b93 | 2592 | GetBuffer().Invalidate(); |
62381daa JS |
2593 | LayoutContent(); |
2594 | return range; | |
5d7836c4 JS |
2595 | } |
2596 | ||
2597 | // ---------------------------------------------------------------------------- | |
2598 | // selection and ranges | |
2599 | // ---------------------------------------------------------------------------- | |
2600 | ||
2601 | void wxRichTextCtrl::SelectAll() | |
2602 | { | |
76d7fc29 | 2603 | SetSelection(-1, -1); |
5d7836c4 JS |
2604 | } |
2605 | ||
2606 | /// Select none | |
2607 | void wxRichTextCtrl::SelectNone() | |
2608 | { | |
603f702b | 2609 | if (m_selection.IsValid()) |
c1aec196 | 2610 | { |
603f702b | 2611 | wxRichTextSelection oldSelection = m_selection; |
7051fa41 | 2612 | |
603f702b | 2613 | m_selection.Reset(); |
7051fa41 | 2614 | |
603f702b | 2615 | RefreshForSelectionChange(oldSelection, m_selection); |
c1aec196 | 2616 | } |
5d7836c4 | 2617 | m_selectionAnchor = -2; |
603f702b JS |
2618 | m_selectionAnchorObject = NULL; |
2619 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
5d7836c4 JS |
2620 | } |
2621 | ||
0ca07313 JS |
2622 | static bool wxIsWordDelimiter(const wxString& text) |
2623 | { | |
1c07e924 | 2624 | return !text.IsEmpty() && !wxIsalnum(text[0]); |
0ca07313 JS |
2625 | } |
2626 | ||
2627 | /// Select the word at the given character position | |
2628 | bool wxRichTextCtrl::SelectWord(long position) | |
2629 | { | |
603f702b | 2630 | if (position < 0 || position > GetFocusObject()->GetOwnRange().GetEnd()) |
0ca07313 | 2631 | return false; |
1807a1f3 | 2632 | |
603f702b | 2633 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(position); |
0ca07313 JS |
2634 | if (!para) |
2635 | return false; | |
2636 | ||
62381daa JS |
2637 | if (position == para->GetRange().GetEnd()) |
2638 | position --; | |
2639 | ||
0ca07313 JS |
2640 | long positionStart = position; |
2641 | long positionEnd = position; | |
1807a1f3 | 2642 | |
0ca07313 JS |
2643 | for (positionStart = position; positionStart >= para->GetRange().GetStart(); positionStart --) |
2644 | { | |
603f702b | 2645 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionStart, positionStart)); |
0ca07313 JS |
2646 | if (wxIsWordDelimiter(text)) |
2647 | { | |
2648 | positionStart ++; | |
2649 | break; | |
2650 | } | |
2651 | } | |
2652 | if (positionStart < para->GetRange().GetStart()) | |
2653 | positionStart = para->GetRange().GetStart(); | |
1807a1f3 | 2654 | |
0ca07313 JS |
2655 | for (positionEnd = position; positionEnd < para->GetRange().GetEnd(); positionEnd ++) |
2656 | { | |
603f702b | 2657 | wxString text = GetFocusObject()->GetTextForRange(wxRichTextRange(positionEnd, positionEnd)); |
0ca07313 JS |
2658 | if (wxIsWordDelimiter(text)) |
2659 | { | |
2660 | positionEnd --; | |
2661 | break; | |
2662 | } | |
2663 | } | |
2664 | if (positionEnd >= para->GetRange().GetEnd()) | |
2665 | positionEnd = para->GetRange().GetEnd(); | |
1807a1f3 | 2666 | |
62381daa JS |
2667 | if (positionEnd < positionStart) |
2668 | return false; | |
2669 | ||
0ca07313 JS |
2670 | SetSelection(positionStart, positionEnd+1); |
2671 | ||
2672 | if (positionStart >= 0) | |
2673 | { | |
2674 | MoveCaret(positionStart-1, true); | |
0a21bea0 | 2675 | SetDefaultStyleToCursorStyle(); |
0ca07313 JS |
2676 | } |
2677 | ||
2678 | return true; | |
2679 | } | |
2680 | ||
5d7836c4 JS |
2681 | wxString wxRichTextCtrl::GetStringSelection() const |
2682 | { | |
2683 | long from, to; | |
2684 | GetSelection(&from, &to); | |
2685 | ||
2686 | return GetRange(from, to); | |
2687 | } | |
2688 | ||
5d7836c4 JS |
2689 | // ---------------------------------------------------------------------------- |
2690 | // hit testing | |
2691 | // ---------------------------------------------------------------------------- | |
2692 | ||
2693 | wxTextCtrlHitTestResult | |
2694 | wxRichTextCtrl::HitTest(const wxPoint& pt, wxTextCoord *x, wxTextCoord *y) const | |
2695 | { | |
2696 | // implement in terms of the other overload as the native ports typically | |
2697 | // can get the position and not (x, y) pair directly (although wxUniv | |
2698 | // directly gets x and y -- and so overrides this method as well) | |
2699 | long pos; | |
2700 | wxTextCtrlHitTestResult rc = HitTest(pt, &pos); | |
2701 | ||
2702 | if ( rc != wxTE_HT_UNKNOWN ) | |
2703 | { | |
2704 | PositionToXY(pos, x, y); | |
2705 | } | |
2706 | ||
2707 | return rc; | |
2708 | } | |
2709 | ||
2710 | wxTextCtrlHitTestResult | |
2711 | wxRichTextCtrl::HitTest(const wxPoint& pt, | |
2712 | long * pos) const | |
2713 | { | |
2714 | wxClientDC dc((wxRichTextCtrl*) this); | |
2715 | ((wxRichTextCtrl*)this)->PrepareDC(dc); | |
61399247 | 2716 | |
563d25ba JS |
2717 | // Buffer uses logical position (relative to start of buffer) |
2718 | // so convert | |
2719 | wxPoint pt2 = GetLogicalPoint(pt); | |
5d7836c4 | 2720 | |
603f702b JS |
2721 | wxRichTextObject* hitObj = NULL; |
2722 | wxRichTextObject* contextObj = NULL; | |
2723 | int hit = ((wxRichTextCtrl*)this)->GetFocusObject()->HitTest(dc, pt2, *pos, & hitObj, & contextObj, wxRICHTEXT_HITTEST_NO_NESTED_OBJECTS); | |
41e155b4 | 2724 | |
f262b25c JS |
2725 | if ((hit & wxRICHTEXT_HITTEST_BEFORE) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) |
2726 | return wxTE_HT_BEFORE; | |
2727 | else if ((hit & wxRICHTEXT_HITTEST_AFTER) && (hit & wxRICHTEXT_HITTEST_OUTSIDE)) | |
2728 | return wxTE_HT_BEYOND; | |
25d6eb3a | 2729 | else if (hit & (wxRICHTEXT_HITTEST_BEFORE|wxRICHTEXT_HITTEST_AFTER)) |
f262b25c | 2730 | return wxTE_HT_ON_TEXT; |
41e155b4 WS |
2731 | |
2732 | return wxTE_HT_UNKNOWN; | |
5d7836c4 JS |
2733 | } |
2734 | ||
0c0e063e JS |
2735 | wxRichTextParagraphLayoutBox* |
2736 | wxRichTextCtrl::FindContainerAtPoint(const wxPoint pt, long& position, int& hit, wxRichTextObject* hitObj, int flags/* = 0*/) | |
2737 | { | |
2738 | wxClientDC dc(this); | |
2739 | PrepareDC(dc); | |
2740 | dc.SetFont(GetFont()); | |
2741 | ||
2742 | wxPoint logicalPt = GetLogicalPoint(pt); | |
2743 | ||
2744 | wxRichTextObject* contextObj = NULL; | |
2745 | hit = GetBuffer().HitTest(dc, logicalPt, position, &hitObj, &contextObj, flags); | |
2746 | wxRichTextParagraphLayoutBox* container = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
2747 | ||
2748 | return container; | |
2749 | } | |
2750 | ||
2751 | ||
5d7836c4 JS |
2752 | // ---------------------------------------------------------------------------- |
2753 | // set/get the controls text | |
2754 | // ---------------------------------------------------------------------------- | |
2755 | ||
805c8d92 | 2756 | wxString wxRichTextCtrl::DoGetValue() const |
5d7836c4 JS |
2757 | { |
2758 | return GetBuffer().GetText(); | |
2759 | } | |
2760 | ||
2761 | wxString wxRichTextCtrl::GetRange(long from, long to) const | |
2762 | { | |
96c9f0f6 | 2763 | // Public API for range is different from internals |
603f702b | 2764 | return GetFocusObject()->GetTextForRange(wxRichTextRange(from, to-1)); |
5d7836c4 JS |
2765 | } |
2766 | ||
343ae70d | 2767 | void wxRichTextCtrl::DoSetValue(const wxString& value, int flags) |
5d7836c4 | 2768 | { |
d081739d JS |
2769 | // Don't call Clear here, since it always sends a text updated event |
2770 | m_buffer.ResetAndClearCommands(); | |
603f702b | 2771 | m_buffer.Invalidate(wxRICHTEXT_ALL); |
d081739d JS |
2772 | m_caretPosition = -1; |
2773 | m_caretPositionForDefaultStyle = -2; | |
2774 | m_caretAtLineStart = false; | |
603f702b JS |
2775 | m_selection.Reset(); |
2776 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
d081739d JS |
2777 | |
2778 | Scroll(0,0); | |
2779 | ||
2780 | if (!IsFrozen()) | |
2781 | { | |
2782 | LayoutContent(); | |
2783 | Refresh(false); | |
2784 | } | |
5d7836c4 | 2785 | |
1a75935d | 2786 | if (!value.IsEmpty()) |
5d7836c4 | 2787 | { |
1a75935d JS |
2788 | // Remove empty paragraph |
2789 | GetBuffer().Clear(); | |
d081739d | 2790 | DoWriteText(value, flags); |
5d7836c4 JS |
2791 | |
2792 | // for compatibility, don't move the cursor when doing SetValue() | |
2793 | SetInsertionPoint(0); | |
2794 | } | |
1a75935d | 2795 | else |
5d7836c4 | 2796 | { |
1a75935d JS |
2797 | // still send an event for consistency |
2798 | if (flags & SetValue_SendEvent) | |
0ec1179b | 2799 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 | 2800 | } |
5d7836c4 JS |
2801 | DiscardEdits(); |
2802 | } | |
2803 | ||
2804 | void wxRichTextCtrl::WriteText(const wxString& value) | |
2805 | { | |
2806 | DoWriteText(value); | |
2807 | } | |
2808 | ||
343ae70d | 2809 | void wxRichTextCtrl::DoWriteText(const wxString& value, int flags) |
5d7836c4 | 2810 | { |
0ca07313 | 2811 | wxString valueUnix = wxTextFile::Translate(value, wxTextFileType_Unix); |
fe5aa22c | 2812 | |
4e63bfb9 | 2813 | GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, valueUnix, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
343ae70d VZ |
2814 | |
2815 | if ( flags & SetValue_SendEvent ) | |
0ec1179b | 2816 | wxTextCtrl::SendTextUpdatedEvent(this); |
5d7836c4 JS |
2817 | } |
2818 | ||
2819 | void wxRichTextCtrl::AppendText(const wxString& text) | |
2820 | { | |
2821 | SetInsertionPointEnd(); | |
2822 | ||
2823 | WriteText(text); | |
2824 | } | |
2825 | ||
2826 | /// Write an image at the current insertion point | |
24777478 | 2827 | bool wxRichTextCtrl::WriteImage(const wxImage& image, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 JS |
2828 | { |
2829 | wxRichTextImageBlock imageBlock; | |
2830 | ||
2831 | wxImage image2 = image; | |
2832 | if (imageBlock.MakeImageBlock(image2, bitmapType)) | |
24777478 | 2833 | return WriteImage(imageBlock, textAttr); |
41e155b4 WS |
2834 | |
2835 | return false; | |
5d7836c4 JS |
2836 | } |
2837 | ||
24777478 | 2838 | bool wxRichTextCtrl::WriteImage(const wxString& filename, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 JS |
2839 | { |
2840 | wxRichTextImageBlock imageBlock; | |
2841 | ||
2842 | wxImage image; | |
2843 | if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) | |
24777478 | 2844 | return WriteImage(imageBlock, textAttr); |
41e155b4 WS |
2845 | |
2846 | return false; | |
5d7836c4 JS |
2847 | } |
2848 | ||
24777478 | 2849 | bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock, const wxRichTextAttr& textAttr) |
5d7836c4 | 2850 | { |
4e63bfb9 | 2851 | return GetFocusObject()->InsertImageWithUndo(& GetBuffer(), m_caretPosition+1, imageBlock, this, 0, textAttr); |
5d7836c4 JS |
2852 | } |
2853 | ||
24777478 | 2854 | bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, wxBitmapType bitmapType, const wxRichTextAttr& textAttr) |
5d7836c4 | 2855 | { |
a1b806b9 | 2856 | if (bitmap.IsOk()) |
5d7836c4 JS |
2857 | { |
2858 | wxRichTextImageBlock imageBlock; | |
7fe8059f | 2859 | |
5d7836c4 | 2860 | wxImage image = bitmap.ConvertToImage(); |
a1b806b9 | 2861 | if (image.IsOk() && imageBlock.MakeImageBlock(image, bitmapType)) |
24777478 | 2862 | return WriteImage(imageBlock, textAttr); |
5d7836c4 | 2863 | } |
41e155b4 | 2864 | |
5d7836c4 JS |
2865 | return false; |
2866 | } | |
2867 | ||
603f702b JS |
2868 | // Write a text box at the current insertion point. |
2869 | wxRichTextBox* wxRichTextCtrl::WriteTextBox(const wxRichTextAttr& textAttr) | |
2870 | { | |
2871 | wxRichTextBox* textBox = new wxRichTextBox; | |
2872 | textBox->SetAttributes(textAttr); | |
2873 | textBox->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style | |
2874 | textBox->AddParagraph(wxEmptyString); | |
2875 | textBox->SetParent(NULL); | |
2876 | ||
2877 | // The object returned is the one actually inserted into the buffer, | |
2878 | // while the original one is deleted. | |
4e63bfb9 | 2879 | wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, textBox, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
603f702b JS |
2880 | wxRichTextBox* box = wxDynamicCast(obj, wxRichTextBox); |
2881 | return box; | |
2882 | } | |
2883 | ||
2884 | // Write a table at the current insertion point, returning the table. | |
2885 | wxRichTextTable* wxRichTextCtrl::WriteTable(int rows, int cols, const wxRichTextAttr& tableAttr, const wxRichTextAttr& cellAttr) | |
2886 | { | |
2887 | wxASSERT(rows > 0 && cols > 0); | |
2888 | ||
2889 | if (rows <= 0 || cols <= 0) | |
2890 | return NULL; | |
2891 | ||
2892 | wxRichTextTable* table = new wxRichTextTable; | |
2893 | table->SetAttributes(tableAttr); | |
2894 | table->SetParent(& GetBuffer()); // set parent temporarily for AddParagraph to use correct style | |
4fe83b93 | 2895 | |
603f702b JS |
2896 | table->CreateTable(rows, cols); |
2897 | ||
2898 | table->SetParent(NULL); | |
2899 | ||
2900 | int i, j; | |
2901 | for (j = 0; j < rows; j++) | |
2902 | { | |
2903 | for (i = 0; i < cols; i++) | |
2904 | { | |
2905 | table->GetCell(j, i)->GetAttributes() = cellAttr; | |
2906 | } | |
2907 | } | |
2908 | ||
2909 | // The object returned is the one actually inserted into the buffer, | |
2910 | // while the original one is deleted. | |
4e63bfb9 | 2911 | wxRichTextObject* obj = GetFocusObject()->InsertObjectWithUndo(& GetBuffer(), m_caretPosition+1, table, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
603f702b JS |
2912 | wxRichTextTable* tableResult = wxDynamicCast(obj, wxRichTextTable); |
2913 | return tableResult; | |
2914 | } | |
2915 | ||
2916 | ||
5d7836c4 JS |
2917 | /// Insert a newline (actually paragraph) at the current insertion point. |
2918 | bool wxRichTextCtrl::Newline() | |
2919 | { | |
4e63bfb9 | 2920 | return GetFocusObject()->InsertNewlineWithUndo(& GetBuffer(), m_caretPosition+1, this, wxRICHTEXT_INSERT_WITH_PREVIOUS_PARAGRAPH_STYLE); |
5d7836c4 JS |
2921 | } |
2922 | ||
ff76711f JS |
2923 | /// Insert a line break at the current insertion point. |
2924 | bool wxRichTextCtrl::LineBreak() | |
2925 | { | |
2926 | wxString text; | |
2927 | text = wxRichTextLineBreakChar; | |
4e63bfb9 | 2928 | return GetFocusObject()->InsertTextWithUndo(& GetBuffer(), m_caretPosition+1, text, this); |
ff76711f | 2929 | } |
5d7836c4 JS |
2930 | |
2931 | // ---------------------------------------------------------------------------- | |
2932 | // Clipboard operations | |
2933 | // ---------------------------------------------------------------------------- | |
2934 | ||
2935 | void wxRichTextCtrl::Copy() | |
2936 | { | |
2937 | if (CanCopy()) | |
2938 | { | |
fe5aa22c | 2939 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
2940 | GetBuffer().CopyToClipboard(range); |
2941 | } | |
2942 | } | |
2943 | ||
2944 | void wxRichTextCtrl::Cut() | |
2945 | { | |
2946 | if (CanCut()) | |
2947 | { | |
fe5aa22c | 2948 | wxRichTextRange range = GetInternalSelectionRange(); |
5d7836c4 JS |
2949 | GetBuffer().CopyToClipboard(range); |
2950 | ||
2951 | DeleteSelectedContent(); | |
2f36e8dc | 2952 | LayoutContent(); |
76bcd815 | 2953 | Refresh(false); |
5d7836c4 JS |
2954 | } |
2955 | } | |
2956 | ||
2957 | void wxRichTextCtrl::Paste() | |
2958 | { | |
2959 | if (CanPaste()) | |
2960 | { | |
2961 | BeginBatchUndo(_("Paste")); | |
7fe8059f | 2962 | |
5d7836c4 JS |
2963 | long newPos = m_caretPosition; |
2964 | DeleteSelectedContent(& newPos); | |
7fe8059f | 2965 | |
5d7836c4 JS |
2966 | GetBuffer().PasteFromClipboard(newPos); |
2967 | ||
2968 | EndBatchUndo(); | |
2969 | } | |
2970 | } | |
2971 | ||
2972 | void wxRichTextCtrl::DeleteSelection() | |
2973 | { | |
2974 | if (CanDeleteSelection()) | |
2975 | { | |
2976 | DeleteSelectedContent(); | |
2977 | } | |
2978 | } | |
2979 | ||
2980 | bool wxRichTextCtrl::HasSelection() const | |
2981 | { | |
603f702b JS |
2982 | return (m_selection.IsValid() && m_selection.GetContainer() == GetFocusObject()); |
2983 | } | |
2984 | ||
2985 | bool wxRichTextCtrl::HasUnfocusedSelection() const | |
2986 | { | |
2987 | return m_selection.IsValid(); | |
5d7836c4 JS |
2988 | } |
2989 | ||
2990 | bool wxRichTextCtrl::CanCopy() const | |
2991 | { | |
2992 | // Can copy if there's a selection | |
2993 | return HasSelection(); | |
2994 | } | |
2995 | ||
2996 | bool wxRichTextCtrl::CanCut() const | |
2997 | { | |
2998 | return HasSelection() && IsEditable(); | |
2999 | } | |
3000 | ||
3001 | bool wxRichTextCtrl::CanPaste() const | |
3002 | { | |
3003 | if ( !IsEditable() ) | |
3004 | return false; | |
3005 | ||
3006 | return GetBuffer().CanPasteFromClipboard(); | |
3007 | } | |
3008 | ||
3009 | bool wxRichTextCtrl::CanDeleteSelection() const | |
3010 | { | |
3011 | return HasSelection() && IsEditable(); | |
3012 | } | |
3013 | ||
3014 | ||
3015 | // ---------------------------------------------------------------------------- | |
3016 | // Accessors | |
3017 | // ---------------------------------------------------------------------------- | |
3018 | ||
8e43fc77 JS |
3019 | void wxRichTextCtrl::SetContextMenu(wxMenu* menu) |
3020 | { | |
3021 | if (m_contextMenu && m_contextMenu != menu) | |
3022 | delete m_contextMenu; | |
3023 | m_contextMenu = menu; | |
3024 | } | |
3025 | ||
5d7836c4 JS |
3026 | void wxRichTextCtrl::SetEditable(bool editable) |
3027 | { | |
3028 | m_editable = editable; | |
3029 | } | |
3030 | ||
3031 | void wxRichTextCtrl::SetInsertionPoint(long pos) | |
3032 | { | |
3033 | SelectNone(); | |
3034 | ||
3035 | m_caretPosition = pos - 1; | |
6f5d63bc JS |
3036 | |
3037 | PositionCaret(); | |
4427f297 JS |
3038 | |
3039 | SetDefaultStyleToCursorStyle(); | |
5d7836c4 JS |
3040 | } |
3041 | ||
3042 | void wxRichTextCtrl::SetInsertionPointEnd() | |
3043 | { | |
3044 | long pos = GetLastPosition(); | |
3045 | SetInsertionPoint(pos); | |
3046 | } | |
3047 | ||
3048 | long wxRichTextCtrl::GetInsertionPoint() const | |
3049 | { | |
3050 | return m_caretPosition+1; | |
3051 | } | |
3052 | ||
3053 | wxTextPos wxRichTextCtrl::GetLastPosition() const | |
3054 | { | |
603f702b | 3055 | return GetFocusObject()->GetOwnRange().GetEnd(); |
5d7836c4 JS |
3056 | } |
3057 | ||
3058 | // If the return values from and to are the same, there is no | |
3059 | // selection. | |
3060 | void wxRichTextCtrl::GetSelection(long* from, long* to) const | |
3061 | { | |
603f702b JS |
3062 | if (m_selection.IsValid()) |
3063 | { | |
3064 | *from = m_selection.GetRange().GetStart(); | |
3065 | *to = m_selection.GetRange().GetEnd(); | |
96c9f0f6 | 3066 | (*to) ++; |
603f702b JS |
3067 | } |
3068 | else | |
3069 | { | |
3070 | *from = -2; | |
3071 | *to = -2; | |
3072 | } | |
5d7836c4 JS |
3073 | } |
3074 | ||
3075 | bool wxRichTextCtrl::IsEditable() const | |
3076 | { | |
3077 | return m_editable; | |
3078 | } | |
3079 | ||
3080 | // ---------------------------------------------------------------------------- | |
3081 | // selection | |
3082 | // ---------------------------------------------------------------------------- | |
3083 | ||
3084 | void wxRichTextCtrl::SetSelection(long from, long to) | |
3085 | { | |
3086 | // if from and to are both -1, it means (in wxWidgets) that all text should | |
3087 | // be selected. | |
3088 | if ( (from == -1) && (to == -1) ) | |
3089 | { | |
3090 | from = 0; | |
96c9f0f6 | 3091 | to = GetLastPosition()+1; |
5d7836c4 JS |
3092 | } |
3093 | ||
c1aec196 JS |
3094 | if (from == to) |
3095 | { | |
3096 | SelectNone(); | |
3097 | } | |
3098 | else | |
3099 | { | |
603f702b JS |
3100 | wxRichTextSelection oldSelection = m_selection; |
3101 | ||
205a1ad9 | 3102 | m_selectionAnchor = from-1; |
603f702b JS |
3103 | m_selectionAnchorObject = NULL; |
3104 | m_selection.Set(wxRichTextRange(from, to-1), GetFocusObject()); | |
76d7fc29 | 3105 | |
a216dfa2 | 3106 | m_caretPosition = wxMax(-1, to-1); |
0ca07313 | 3107 | |
603f702b | 3108 | RefreshForSelectionChange(oldSelection, m_selection); |
c1aec196 JS |
3109 | PositionCaret(); |
3110 | } | |
5d7836c4 JS |
3111 | } |
3112 | ||
3113 | // ---------------------------------------------------------------------------- | |
3114 | // Editing | |
3115 | // ---------------------------------------------------------------------------- | |
3116 | ||
e3bb4c54 | 3117 | void wxRichTextCtrl::Replace(long from, long to, |
343ae70d | 3118 | const wxString& value) |
5d7836c4 JS |
3119 | { |
3120 | BeginBatchUndo(_("Replace")); | |
3121 | ||
e3bb4c54 JS |
3122 | SetSelection(from, to); |
3123 | ||
3124 | wxRichTextAttr attr = GetDefaultStyle(); | |
3125 | ||
5d7836c4 JS |
3126 | DeleteSelectedContent(); |
3127 | ||
e3bb4c54 JS |
3128 | SetDefaultStyle(attr); |
3129 | ||
343ae70d | 3130 | DoWriteText(value, SetValue_SelectionOnly); |
5d7836c4 JS |
3131 | |
3132 | EndBatchUndo(); | |
3133 | } | |
3134 | ||
3135 | void wxRichTextCtrl::Remove(long from, long to) | |
3136 | { | |
3137 | SelectNone(); | |
3138 | ||
603f702b | 3139 | GetFocusObject()->DeleteRangeWithUndo(wxRichTextRange(from, to-1), this, & GetBuffer()); |
5d7836c4 | 3140 | |
2f36e8dc | 3141 | LayoutContent(); |
5d7836c4 | 3142 | if (!IsFrozen()) |
76bcd815 | 3143 | Refresh(false); |
5d7836c4 JS |
3144 | } |
3145 | ||
3146 | bool wxRichTextCtrl::IsModified() const | |
3147 | { | |
3148 | return m_buffer.IsModified(); | |
3149 | } | |
3150 | ||
3151 | void wxRichTextCtrl::MarkDirty() | |
3152 | { | |
3153 | m_buffer.Modify(true); | |
3154 | } | |
3155 | ||
3156 | void wxRichTextCtrl::DiscardEdits() | |
3157 | { | |
ab14c7aa | 3158 | m_caretPositionForDefaultStyle = -2; |
5d7836c4 JS |
3159 | m_buffer.Modify(false); |
3160 | m_buffer.GetCommandProcessor()->ClearCommands(); | |
3161 | } | |
3162 | ||
3163 | int wxRichTextCtrl::GetNumberOfLines() const | |
3164 | { | |
603f702b | 3165 | return GetFocusObject()->GetParagraphCount(); |
5d7836c4 JS |
3166 | } |
3167 | ||
3168 | // ---------------------------------------------------------------------------- | |
3169 | // Positions <-> coords | |
3170 | // ---------------------------------------------------------------------------- | |
3171 | ||
3172 | long wxRichTextCtrl::XYToPosition(long x, long y) const | |
3173 | { | |
603f702b | 3174 | return GetFocusObject()->XYToPosition(x, y); |
5d7836c4 JS |
3175 | } |
3176 | ||
3177 | bool wxRichTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
3178 | { | |
603f702b | 3179 | return GetFocusObject()->PositionToXY(pos, x, y); |
5d7836c4 JS |
3180 | } |
3181 | ||
3182 | // ---------------------------------------------------------------------------- | |
3183 | // | |
3184 | // ---------------------------------------------------------------------------- | |
3185 | ||
3186 | void wxRichTextCtrl::ShowPosition(long pos) | |
3187 | { | |
3188 | if (!IsPositionVisible(pos)) | |
3189 | ScrollIntoView(pos-1, WXK_DOWN); | |
3190 | } | |
3191 | ||
3192 | int wxRichTextCtrl::GetLineLength(long lineNo) const | |
3193 | { | |
603f702b | 3194 | return GetFocusObject()->GetParagraphLength(lineNo); |
5d7836c4 JS |
3195 | } |
3196 | ||
3197 | wxString wxRichTextCtrl::GetLineText(long lineNo) const | |
3198 | { | |
603f702b | 3199 | return GetFocusObject()->GetParagraphText(lineNo); |
5d7836c4 JS |
3200 | } |
3201 | ||
3202 | // ---------------------------------------------------------------------------- | |
3203 | // Undo/redo | |
3204 | // ---------------------------------------------------------------------------- | |
3205 | ||
3206 | void wxRichTextCtrl::Undo() | |
3207 | { | |
3208 | if (CanUndo()) | |
3209 | { | |
3210 | GetCommandProcessor()->Undo(); | |
3211 | } | |
3212 | } | |
3213 | ||
3214 | void wxRichTextCtrl::Redo() | |
3215 | { | |
3216 | if (CanRedo()) | |
3217 | { | |
3218 | GetCommandProcessor()->Redo(); | |
3219 | } | |
3220 | } | |
3221 | ||
3222 | bool wxRichTextCtrl::CanUndo() const | |
3223 | { | |
3a4a38c1 | 3224 | return GetCommandProcessor()->CanUndo() && IsEditable(); |
5d7836c4 JS |
3225 | } |
3226 | ||
3227 | bool wxRichTextCtrl::CanRedo() const | |
3228 | { | |
3a4a38c1 | 3229 | return GetCommandProcessor()->CanRedo() && IsEditable(); |
5d7836c4 JS |
3230 | } |
3231 | ||
3232 | // ---------------------------------------------------------------------------- | |
563d25ba | 3233 | // implementation details |
5d7836c4 JS |
3234 | // ---------------------------------------------------------------------------- |
3235 | ||
563d25ba | 3236 | void wxRichTextCtrl::Command(wxCommandEvent& event) |
5d7836c4 JS |
3237 | { |
3238 | SetValue(event.GetString()); | |
3239 | GetEventHandler()->ProcessEvent(event); | |
3240 | } | |
3241 | ||
3242 | void wxRichTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
3243 | { | |
3244 | // By default, load the first file into the text window. | |
3245 | if (event.GetNumberOfFiles() > 0) | |
3246 | { | |
3247 | LoadFile(event.GetFiles()[0]); | |
3248 | } | |
3249 | } | |
3250 | ||
5d7836c4 JS |
3251 | wxSize wxRichTextCtrl::DoGetBestSize() const |
3252 | { | |
3253 | return wxSize(10, 10); | |
3254 | } | |
3255 | ||
3256 | // ---------------------------------------------------------------------------- | |
3257 | // standard handlers for standard edit menu events | |
3258 | // ---------------------------------------------------------------------------- | |
3259 | ||
3260 | void wxRichTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
3261 | { | |
3262 | Cut(); | |
3263 | } | |
3264 | ||
3265 | void wxRichTextCtrl::OnClear(wxCommandEvent& WXUNUSED(event)) | |
3266 | { | |
3267 | DeleteSelection(); | |
3268 | } | |
3269 | ||
3270 | void wxRichTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
3271 | { | |
3272 | Copy(); | |
3273 | } | |
3274 | ||
3275 | void wxRichTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
3276 | { | |
3277 | Paste(); | |
3278 | } | |
3279 | ||
3280 | void wxRichTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
3281 | { | |
3282 | Undo(); | |
3283 | } | |
3284 | ||
3285 | void wxRichTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
3286 | { | |
3287 | Redo(); | |
3288 | } | |
3289 | ||
3290 | void wxRichTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
3291 | { | |
3292 | event.Enable( CanCut() ); | |
3293 | } | |
3294 | ||
3295 | void wxRichTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
3296 | { | |
3297 | event.Enable( CanCopy() ); | |
3298 | } | |
3299 | ||
3300 | void wxRichTextCtrl::OnUpdateClear(wxUpdateUIEvent& event) | |
3301 | { | |
3302 | event.Enable( CanDeleteSelection() ); | |
3303 | } | |
3304 | ||
3305 | void wxRichTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
3306 | { | |
3307 | event.Enable( CanPaste() ); | |
3308 | } | |
3309 | ||
3310 | void wxRichTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
3311 | { | |
3312 | event.Enable( CanUndo() ); | |
3313 | event.SetText( GetCommandProcessor()->GetUndoMenuLabel() ); | |
3314 | } | |
3315 | ||
3316 | void wxRichTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
3317 | { | |
3318 | event.Enable( CanRedo() ); | |
3319 | event.SetText( GetCommandProcessor()->GetRedoMenuLabel() ); | |
3320 | } | |
3321 | ||
3322 | void wxRichTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
3323 | { | |
b30661fd JS |
3324 | if (GetLastPosition() > 0) |
3325 | SelectAll(); | |
5d7836c4 JS |
3326 | } |
3327 | ||
3328 | void wxRichTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) | |
3329 | { | |
b30661fd | 3330 | event.Enable(GetLastPosition() > 0); |
5d7836c4 JS |
3331 | } |
3332 | ||
603f702b | 3333 | void wxRichTextCtrl::OnProperties(wxCommandEvent& event) |
cdaed652 | 3334 | { |
603f702b JS |
3335 | int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1; |
3336 | if (idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount()) | |
3337 | { | |
3338 | wxRichTextObject* obj = m_contextMenuPropertiesInfo.GetObject(idx); | |
3339 | if (obj && obj->CanEditProperties()) | |
3340 | obj->EditProperties(this, & GetBuffer()); | |
3341 | ||
3342 | m_contextMenuPropertiesInfo.Clear(); | |
3343 | } | |
cdaed652 VZ |
3344 | } |
3345 | ||
603f702b | 3346 | void wxRichTextCtrl::OnUpdateProperties(wxUpdateUIEvent& event) |
cdaed652 | 3347 | { |
603f702b JS |
3348 | int idx = event.GetId() - wxID_RICHTEXT_PROPERTIES1; |
3349 | event.Enable(idx >= 0 && idx < m_contextMenuPropertiesInfo.GetCount()); | |
cdaed652 VZ |
3350 | } |
3351 | ||
b4ae0def | 3352 | void wxRichTextCtrl::OnContextMenu(wxContextMenuEvent& event) |
5d7836c4 | 3353 | { |
b4ae0def JS |
3354 | if (event.GetEventObject() != this) |
3355 | { | |
3356 | event.Skip(); | |
3357 | return; | |
3358 | } | |
3359 | ||
9b794421 JS |
3360 | ShowContextMenu(m_contextMenu, event.GetPosition()); |
3361 | } | |
3362 | ||
3363 | // Prepares the context menu, adding appropriate property-editing commands. | |
3364 | // Returns the number of property commands added. | |
3365 | int wxRichTextCtrl::PrepareContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands) | |
3366 | { | |
cdaed652 VZ |
3367 | wxClientDC dc(this); |
3368 | PrepareDC(dc); | |
3369 | dc.SetFont(GetFont()); | |
3370 | ||
9b794421 JS |
3371 | m_contextMenuPropertiesInfo.Clear(); |
3372 | ||
cdaed652 | 3373 | long position = 0; |
603f702b JS |
3374 | wxRichTextObject* hitObj = NULL; |
3375 | wxRichTextObject* contextObj = NULL; | |
9b794421 JS |
3376 | if (pt != wxDefaultPosition) |
3377 | { | |
3378 | wxPoint logicalPt = GetLogicalPoint(ScreenToClient(pt)); | |
3379 | int hit = GetBuffer().HitTest(dc, logicalPt, position, & hitObj, & contextObj); | |
4fe83b93 | 3380 | |
9b794421 JS |
3381 | if (hit == wxRICHTEXT_HITTEST_ON || hit == wxRICHTEXT_HITTEST_BEFORE || hit == wxRICHTEXT_HITTEST_AFTER) |
3382 | { | |
3383 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); | |
3384 | if (hitObj && actualContainer) | |
3385 | { | |
3386 | if (actualContainer->AcceptsFocus()) | |
3387 | { | |
3388 | SetFocusObject(actualContainer, false /* don't set caret position yet */); | |
3389 | SetCaretPositionAfterClick(actualContainer, position, hit); | |
3390 | } | |
4fe83b93 | 3391 | |
9b794421 JS |
3392 | if (addPropertyCommands) |
3393 | m_contextMenuPropertiesInfo.AddItems(actualContainer, hitObj); | |
3394 | } | |
3395 | else | |
3396 | { | |
3397 | if (addPropertyCommands) | |
3398 | m_contextMenuPropertiesInfo.AddItems(GetFocusObject(), NULL); | |
3399 | } | |
3400 | } | |
3401 | else | |
3402 | { | |
3403 | if (addPropertyCommands) | |
3404 | m_contextMenuPropertiesInfo.AddItems(GetFocusObject(), NULL); | |
3405 | } | |
3406 | } | |
3407 | else | |
cdaed652 | 3408 | { |
9b794421 JS |
3409 | // Invoked from the keyboard, so don't set the caret position and don't use the event |
3410 | // position | |
3411 | hitObj = GetFocusObject()->GetLeafObjectAtPosition(m_caretPosition+1); | |
3412 | if (hitObj) | |
3413 | contextObj = hitObj->GetParentContainer(); | |
3414 | else | |
3415 | contextObj = GetFocusObject(); | |
3416 | ||
603f702b JS |
3417 | wxRichTextParagraphLayoutBox* actualContainer = wxDynamicCast(contextObj, wxRichTextParagraphLayoutBox); |
3418 | if (hitObj && actualContainer) | |
3419 | { | |
9b794421 JS |
3420 | if (addPropertyCommands) |
3421 | m_contextMenuPropertiesInfo.AddItems(actualContainer, hitObj); | |
603f702b JS |
3422 | } |
3423 | else | |
9b794421 JS |
3424 | { |
3425 | if (addPropertyCommands) | |
3426 | m_contextMenuPropertiesInfo.AddItems(GetFocusObject(), NULL); | |
3427 | } | |
cdaed652 | 3428 | } |
9b794421 JS |
3429 | |
3430 | if (menu) | |
cdaed652 | 3431 | { |
9b794421 JS |
3432 | if (addPropertyCommands) |
3433 | m_contextMenuPropertiesInfo.AddMenuItems(menu); | |
3434 | return m_contextMenuPropertiesInfo.GetCount(); | |
cdaed652 | 3435 | } |
9b794421 JS |
3436 | else |
3437 | return 0; | |
3438 | } | |
cdaed652 | 3439 | |
9b794421 JS |
3440 | // Shows the context menu, adding appropriate property-editing commands |
3441 | bool wxRichTextCtrl::ShowContextMenu(wxMenu* menu, const wxPoint& pt, bool addPropertyCommands) | |
3442 | { | |
3443 | if (menu) | |
603f702b | 3444 | { |
9b794421 JS |
3445 | PrepareContextMenu(menu, pt, addPropertyCommands); |
3446 | PopupMenu(menu); | |
3447 | return true; | |
603f702b | 3448 | } |
9b794421 JS |
3449 | else |
3450 | return false; | |
5d7836c4 JS |
3451 | } |
3452 | ||
27e20452 JS |
3453 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) |
3454 | { | |
603f702b | 3455 | return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), wxRichTextAttr(style)); |
24777478 JS |
3456 | } |
3457 | ||
3458 | bool wxRichTextCtrl::SetStyle(long start, long end, const wxRichTextAttr& style) | |
3459 | { | |
603f702b | 3460 | return GetFocusObject()->SetStyle(wxRichTextRange(start, end-1), style); |
27e20452 JS |
3461 | } |
3462 | ||
44cc96a8 | 3463 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxTextAttr& style) |
24777478 | 3464 | { |
603f702b | 3465 | return GetFocusObject()->SetStyle(range.ToInternal(), wxRichTextAttr(style)); |
24777478 JS |
3466 | } |
3467 | ||
3468 | bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) | |
5d7836c4 | 3469 | { |
603f702b | 3470 | return GetFocusObject()->SetStyle(range.ToInternal(), style); |
5d7836c4 JS |
3471 | } |
3472 | ||
603f702b | 3473 | void wxRichTextCtrl::SetStyle(wxRichTextObject *obj, const wxRichTextAttr& textAttr) |
cdaed652 | 3474 | { |
603f702b | 3475 | GetFocusObject()->SetStyle(obj, textAttr); |
cdaed652 VZ |
3476 | } |
3477 | ||
3e3a754f JS |
3478 | // extended style setting operation with flags including: |
3479 | // wxRICHTEXT_SETSTYLE_WITH_UNDO, wxRICHTEXT_SETSTYLE_OPTIMIZE, wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY. | |
3480 | // see richtextbuffer.h for more details. | |
3e3a754f | 3481 | |
24777478 | 3482 | bool wxRichTextCtrl::SetStyleEx(const wxRichTextRange& range, const wxRichTextAttr& style, int flags) |
3e3a754f | 3483 | { |
603f702b | 3484 | return GetFocusObject()->SetStyle(range.ToInternal(), style, flags); |
3e3a754f JS |
3485 | } |
3486 | ||
27e20452 JS |
3487 | bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style) |
3488 | { | |
24777478 | 3489 | return GetBuffer().SetDefaultStyle(style); |
5d7836c4 JS |
3490 | } |
3491 | ||
24777478 JS |
3492 | bool wxRichTextCtrl::SetDefaultStyle(const wxRichTextAttr& style) |
3493 | { | |
603f702b JS |
3494 | wxRichTextAttr attr1(style); |
3495 | attr1.GetTextBoxAttr().Reset(); | |
3496 | return GetBuffer().SetDefaultStyle(attr1); | |
24777478 JS |
3497 | } |
3498 | ||
3499 | const wxRichTextAttr& wxRichTextCtrl::GetDefaultStyleEx() const | |
27e20452 | 3500 | { |
61399247 | 3501 | return GetBuffer().GetDefaultStyle(); |
27e20452 JS |
3502 | } |
3503 | ||
3966a9f4 | 3504 | bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style) |
24777478 JS |
3505 | { |
3506 | wxRichTextAttr attr; | |
603f702b | 3507 | if (GetFocusObject()->GetStyle(position, attr)) |
24777478 JS |
3508 | { |
3509 | style = attr; | |
3510 | return true; | |
3511 | } | |
3512 | else | |
3513 | return false; | |
3514 | } | |
3515 | ||
3516 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style) | |
5d7836c4 | 3517 | { |
603f702b JS |
3518 | return GetFocusObject()->GetStyle(position, style); |
3519 | } | |
3520 | ||
3521 | bool wxRichTextCtrl::GetStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3522 | { | |
3523 | wxRichTextAttr attr; | |
3524 | if (container->GetStyle(position, attr)) | |
3525 | { | |
3526 | style = attr; | |
3527 | return true; | |
3528 | } | |
3529 | else | |
3530 | return false; | |
5d7836c4 JS |
3531 | } |
3532 | ||
a7ed48a5 | 3533 | // get the common set of styles for the range |
44cc96a8 | 3534 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxTextAttr& style) |
24777478 JS |
3535 | { |
3536 | wxRichTextAttr attr; | |
603f702b | 3537 | if (GetFocusObject()->GetStyleForRange(range.ToInternal(), attr)) |
24777478 JS |
3538 | { |
3539 | style = attr; | |
3540 | return true; | |
3541 | } | |
3542 | else | |
3543 | return false; | |
3544 | } | |
3545 | ||
3546 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style) | |
a7ed48a5 | 3547 | { |
603f702b JS |
3548 | return GetFocusObject()->GetStyleForRange(range.ToInternal(), style); |
3549 | } | |
3550 | ||
3551 | bool wxRichTextCtrl::GetStyleForRange(const wxRichTextRange& range, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3552 | { | |
3553 | return container->GetStyleForRange(range.ToInternal(), style); | |
a7ed48a5 JS |
3554 | } |
3555 | ||
fe5aa22c | 3556 | /// Get the content (uncombined) attributes for this position. |
24777478 | 3557 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style) |
fe5aa22c | 3558 | { |
603f702b JS |
3559 | return GetFocusObject()->GetUncombinedStyle(position, style); |
3560 | } | |
3561 | ||
3562 | /// Get the content (uncombined) attributes for this position. | |
3563 | bool wxRichTextCtrl::GetUncombinedStyle(long position, wxRichTextAttr& style, wxRichTextParagraphLayoutBox* container) | |
3564 | { | |
3565 | return container->GetUncombinedStyle(position, style); | |
fe5aa22c JS |
3566 | } |
3567 | ||
5d7836c4 JS |
3568 | /// Set font, and also the buffer attributes |
3569 | bool wxRichTextCtrl::SetFont(const wxFont& font) | |
3570 | { | |
24777478 | 3571 | wxControl::SetFont(font); |
5d7836c4 | 3572 | |
24777478 | 3573 | wxRichTextAttr attr = GetBuffer().GetAttributes(); |
5d7836c4 JS |
3574 | attr.SetFont(font); |
3575 | GetBuffer().SetBasicStyle(attr); | |
fe5aa22c | 3576 | |
fe5aa22c JS |
3577 | GetBuffer().Invalidate(wxRICHTEXT_ALL); |
3578 | Refresh(false); | |
5d7836c4 JS |
3579 | |
3580 | return true; | |
3581 | } | |
3582 | ||
4d551ad5 JS |
3583 | /// Transform logical to physical |
3584 | wxPoint wxRichTextCtrl::GetPhysicalPoint(const wxPoint& ptLogical) const | |
5d7836c4 JS |
3585 | { |
3586 | wxPoint pt; | |
3587 | CalcScrolledPosition(ptLogical.x, ptLogical.y, & pt.x, & pt.y); | |
3588 | ||
3589 | return pt; | |
3590 | } | |
3591 | ||
3592 | /// Transform physical to logical | |
4d551ad5 | 3593 | wxPoint wxRichTextCtrl::GetLogicalPoint(const wxPoint& ptPhysical) const |
5d7836c4 JS |
3594 | { |
3595 | wxPoint pt; | |
3596 | CalcUnscrolledPosition(ptPhysical.x, ptPhysical.y, & pt.x, & pt.y); | |
3597 | ||
3598 | return pt; | |
3599 | } | |
3600 | ||
3601 | /// Position the caret | |
603f702b | 3602 | void wxRichTextCtrl::PositionCaret(wxRichTextParagraphLayoutBox* container) |
5d7836c4 | 3603 | { |
c59f6793 JS |
3604 | if (!GetCaret()) |
3605 | return; | |
3606 | ||
3607 | //wxLogDebug(wxT("PositionCaret")); | |
3608 | ||
5d7836c4 | 3609 | wxRect caretRect; |
603f702b | 3610 | if (GetCaretPositionForIndex(GetCaretPosition(), caretRect, container)) |
5d7836c4 | 3611 | { |
7e4fe193 JS |
3612 | wxPoint newPt = caretRect.GetPosition(); |
3613 | wxSize newSz = caretRect.GetSize(); | |
3614 | wxPoint pt = GetPhysicalPoint(newPt); | |
3615 | if (GetCaret()->GetPosition() != pt || GetCaret()->GetSize() != newSz) | |
c59f6793 | 3616 | { |
1c13f06e JS |
3617 | GetCaret()->Hide(); |
3618 | if (GetCaret()->GetSize() != newSz) | |
3619 | GetCaret()->SetSize(newSz); | |
e9f10004 | 3620 | |
423b9ba5 JS |
3621 | // Adjust size so the caret size and position doesn't appear in the margins |
3622 | if (((pt.y + newSz.y) <= GetBuffer().GetTopMargin()) || (pt.y >= (GetClientSize().y - GetBuffer().GetBottomMargin()))) | |
603f702b JS |
3623 | { |
3624 | pt.x = -200; | |
e9f10004 | 3625 | pt.y = -200; |
603f702b | 3626 | } |
423b9ba5 JS |
3627 | else if (pt.y < GetBuffer().GetTopMargin() && (pt.y + newSz.y) > GetBuffer().GetTopMargin()) |
3628 | { | |
3629 | newSz.y -= (GetBuffer().GetTopMargin() - pt.y); | |
3630 | if (newSz.y > 0) | |
3631 | { | |
3632 | pt.y = GetBuffer().GetTopMargin(); | |
3633 | GetCaret()->SetSize(newSz); | |
3634 | } | |
3635 | } | |
3636 | else if (pt.y < (GetClientSize().y - GetBuffer().GetBottomMargin()) && (pt.y + newSz.y) > (GetClientSize().y - GetBuffer().GetBottomMargin())) | |
3637 | { | |
3638 | newSz.y = GetClientSize().y - GetBuffer().GetBottomMargin() - pt.y; | |
3639 | GetCaret()->SetSize(newSz); | |
3640 | } | |
e9f10004 | 3641 | |
c59f6793 | 3642 | GetCaret()->Move(pt); |
1c13f06e | 3643 | GetCaret()->Show(); |
c59f6793 | 3644 | } |
5d7836c4 JS |
3645 | } |
3646 | } | |
3647 | ||
3648 | /// Get the caret height and position for the given character position | |
603f702b | 3649 | bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect, wxRichTextParagraphLayoutBox* container) |
5d7836c4 JS |
3650 | { |
3651 | wxClientDC dc(this); | |
3652 | dc.SetFont(GetFont()); | |
7fe8059f | 3653 | |
5d7836c4 JS |
3654 | PrepareDC(dc); |
3655 | ||
3656 | wxPoint pt; | |
3657 | int height = 0; | |
4fe83b93 | 3658 | |
603f702b JS |
3659 | if (!container) |
3660 | container = GetFocusObject(); | |
5d7836c4 | 3661 | |
603f702b | 3662 | if (container->FindPosition(dc, position, pt, & height, m_caretAtLineStart)) |
5d7836c4 | 3663 | { |
88bee7e6 JS |
3664 | // Caret height can't be zero |
3665 | if (height == 0) | |
3666 | height = dc.GetCharHeight(); | |
3667 | ||
5d7836c4 JS |
3668 | rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); |
3669 | return true; | |
3670 | } | |
41e155b4 WS |
3671 | |
3672 | return false; | |
5d7836c4 JS |
3673 | } |
3674 | ||
3675 | /// Gets the line for the visible caret position. If the caret is | |
3676 | /// shown at the very end of the line, it means the next character is actually | |
3677 | /// on the following line. So let's get the line we're expecting to find | |
3678 | /// if this is the case. | |
3679 | wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPosition) const | |
3680 | { | |
603f702b JS |
3681 | wxRichTextLine* line = GetFocusObject()->GetLineAtPosition(caretPosition, true); |
3682 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPosition, true); | |
5d7836c4 JS |
3683 | if (line) |
3684 | { | |
1e967276 JS |
3685 | wxRichTextRange lineRange = line->GetAbsoluteRange(); |
3686 | if (caretPosition == lineRange.GetStart()-1 && | |
3687 | (para->GetRange().GetStart() != lineRange.GetStart())) | |
5d7836c4 JS |
3688 | { |
3689 | if (!m_caretAtLineStart) | |
603f702b | 3690 | line = GetFocusObject()->GetLineAtPosition(caretPosition-1, true); |
5d7836c4 JS |
3691 | } |
3692 | } | |
3693 | return line; | |
3694 | } | |
3695 | ||
3696 | ||
3697 | /// Move the caret to the given character position | |
603f702b | 3698 | bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart, wxRichTextParagraphLayoutBox* container) |
5d7836c4 | 3699 | { |
603f702b | 3700 | if (GetBuffer().IsDirty()) |
2f36e8dc | 3701 | LayoutContent(); |
5d7836c4 | 3702 | |
603f702b JS |
3703 | if (!container) |
3704 | container = GetFocusObject(); | |
3705 | ||
3706 | if (pos <= container->GetOwnRange().GetEnd()) | |
5d7836c4 JS |
3707 | { |
3708 | SetCaretPosition(pos, showAtLineStart); | |
3709 | ||
603f702b | 3710 | PositionCaret(container); |
5d7836c4 JS |
3711 | |
3712 | return true; | |
3713 | } | |
3714 | else | |
3715 | return false; | |
3716 | } | |
3717 | ||
3718 | /// Layout the buffer: which we must do before certain operations, such as | |
3719 | /// setting the caret position. | |
2f36e8dc | 3720 | bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) |
5d7836c4 | 3721 | { |
603f702b | 3722 | if (GetBuffer().IsDirty() || onlyVisibleRect) |
1e967276 JS |
3723 | { |
3724 | wxRect availableSpace(GetClientSize()); | |
3725 | if (availableSpace.width == 0) | |
3726 | availableSpace.width = 10; | |
3727 | if (availableSpace.height == 0) | |
3728 | availableSpace.height = 10; | |
4d551ad5 JS |
3729 | |
3730 | int flags = wxRICHTEXT_FIXED_WIDTH|wxRICHTEXT_VARIABLE_HEIGHT; | |
3731 | if (onlyVisibleRect) | |
3732 | { | |
3733 | flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; | |
3734 | availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); | |
3735 | } | |
41e155b4 | 3736 | |
1e967276 JS |
3737 | wxClientDC dc(this); |
3738 | dc.SetFont(GetFont()); | |
41e155b4 | 3739 | |
1e967276 | 3740 | PrepareDC(dc); |
41e155b4 | 3741 | |
1e967276 JS |
3742 | GetBuffer().Defragment(); |
3743 | GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation | |
4d551ad5 | 3744 | GetBuffer().Layout(dc, availableSpace, flags); |
603f702b | 3745 | GetBuffer().Invalidate(wxRICHTEXT_NONE); |
41e155b4 | 3746 | |
1e967276 JS |
3747 | if (!IsFrozen()) |
3748 | SetupScrollbars(); | |
3749 | } | |
5d7836c4 JS |
3750 | |
3751 | return true; | |
3752 | } | |
3753 | ||
75936ec6 | 3754 | /// Is all of the selection, or the current caret position, bold? |
3966a9f4 | 3755 | bool wxRichTextCtrl::IsSelectionBold() |
5d7836c4 JS |
3756 | { |
3757 | if (HasSelection()) | |
3758 | { | |
24777478 | 3759 | wxRichTextAttr attr; |
a7ed48a5 | 3760 | wxRichTextRange range = GetSelectionRange(); |
5d7836c4 | 3761 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
7d76fbd5 | 3762 | attr.SetFontWeight(wxFONTWEIGHT_BOLD); |
7fe8059f | 3763 | |
5d7836c4 JS |
3764 | return HasCharacterAttributes(range, attr); |
3765 | } | |
3766 | else | |
3767 | { | |
3768 | // If no selection, then we need to combine current style with default style | |
3769 | // to see what the effect would be if we started typing. | |
24777478 | 3770 | wxRichTextAttr attr; |
5d7836c4 | 3771 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
ab14c7aa JS |
3772 | |
3773 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3774 | if (GetStyle(pos, attr)) | |
5d7836c4 | 3775 | { |
ab14c7aa JS |
3776 | if (IsDefaultStyleShowing()) |
3777 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
7d76fbd5 | 3778 | return attr.GetFontWeight() == wxFONTWEIGHT_BOLD; |
5d7836c4 JS |
3779 | } |
3780 | } | |
3781 | return false; | |
3782 | } | |
3783 | ||
75936ec6 | 3784 | /// Is all of the selection, or the current caret position, italics? |
3966a9f4 | 3785 | bool wxRichTextCtrl::IsSelectionItalics() |
5d7836c4 JS |
3786 | { |
3787 | if (HasSelection()) | |
3788 | { | |
a7ed48a5 | 3789 | wxRichTextRange range = GetSelectionRange(); |
24777478 | 3790 | wxRichTextAttr attr; |
5d7836c4 | 3791 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
7d76fbd5 | 3792 | attr.SetFontStyle(wxFONTSTYLE_ITALIC); |
7fe8059f | 3793 | |
5d7836c4 JS |
3794 | return HasCharacterAttributes(range, attr); |
3795 | } | |
3796 | else | |
3797 | { | |
3798 | // If no selection, then we need to combine current style with default style | |
3799 | // to see what the effect would be if we started typing. | |
24777478 | 3800 | wxRichTextAttr attr; |
5d7836c4 | 3801 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
ab14c7aa JS |
3802 | |
3803 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3804 | if (GetStyle(pos, attr)) | |
5d7836c4 | 3805 | { |
ab14c7aa JS |
3806 | if (IsDefaultStyleShowing()) |
3807 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
7d76fbd5 | 3808 | return attr.GetFontStyle() == wxFONTSTYLE_ITALIC; |
5d7836c4 JS |
3809 | } |
3810 | } | |
3811 | return false; | |
3812 | } | |
3813 | ||
75936ec6 | 3814 | /// Is all of the selection, or the current caret position, underlined? |
3966a9f4 | 3815 | bool wxRichTextCtrl::IsSelectionUnderlined() |
5d7836c4 JS |
3816 | { |
3817 | if (HasSelection()) | |
3818 | { | |
a7ed48a5 | 3819 | wxRichTextRange range = GetSelectionRange(); |
24777478 | 3820 | wxRichTextAttr attr; |
5d7836c4 JS |
3821 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
3822 | attr.SetFontUnderlined(true); | |
7fe8059f | 3823 | |
5d7836c4 JS |
3824 | return HasCharacterAttributes(range, attr); |
3825 | } | |
3826 | else | |
3827 | { | |
3828 | // If no selection, then we need to combine current style with default style | |
3829 | // to see what the effect would be if we started typing. | |
24777478 | 3830 | wxRichTextAttr attr; |
5d7836c4 | 3831 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
ab14c7aa | 3832 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
fe5aa22c | 3833 | |
ab14c7aa | 3834 | if (GetStyle(pos, attr)) |
5d7836c4 | 3835 | { |
ab14c7aa JS |
3836 | if (IsDefaultStyleShowing()) |
3837 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
5d7836c4 JS |
3838 | return attr.GetFontUnderlined(); |
3839 | } | |
3840 | } | |
3841 | return false; | |
3842 | } | |
3843 | ||
75936ec6 JS |
3844 | /// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)? |
3845 | bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag) | |
3846 | { | |
3847 | wxRichTextAttr attr; | |
3848 | attr.SetFlags(wxTEXT_ATTR_EFFECTS); | |
3849 | attr.SetTextEffectFlags(flag); | |
3850 | attr.SetTextEffects(flag); | |
3851 | ||
3852 | if (HasSelection()) | |
3853 | { | |
3854 | return HasCharacterAttributes(GetSelectionRange(), attr); | |
3855 | } | |
3856 | else | |
3857 | { | |
3858 | // If no selection, then we need to combine current style with default style | |
3859 | // to see what the effect would be if we started typing. | |
3860 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
3861 | if (GetStyle(pos, attr)) | |
3862 | { | |
3863 | if (IsDefaultStyleShowing()) | |
3864 | wxRichTextApplyStyle(attr, GetDefaultStyleEx()); | |
dabf7342 | 3865 | return (attr.GetTextEffectFlags() & flag) != 0; |
75936ec6 JS |
3866 | } |
3867 | } | |
3868 | return false; | |
3869 | } | |
3870 | ||
5d7836c4 JS |
3871 | /// Apply bold to the selection |
3872 | bool wxRichTextCtrl::ApplyBoldToSelection() | |
3873 | { | |
24777478 | 3874 | wxRichTextAttr attr; |
5d7836c4 | 3875 | attr.SetFlags(wxTEXT_ATTR_FONT_WEIGHT); |
7d76fbd5 | 3876 | attr.SetFontWeight(IsSelectionBold() ? wxFONTWEIGHT_NORMAL : wxFONTWEIGHT_BOLD); |
5d7836c4 JS |
3877 | |
3878 | if (HasSelection()) | |
3e3a754f | 3879 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 3880 | else |
063dbace JS |
3881 | { |
3882 | wxRichTextAttr current = GetDefaultStyleEx(); | |
3883 | current.Apply(attr); | |
3884 | SetAndShowDefaultStyle(current); | |
3885 | } | |
5d7836c4 JS |
3886 | return true; |
3887 | } | |
3888 | ||
3889 | /// Apply italic to the selection | |
3890 | bool wxRichTextCtrl::ApplyItalicToSelection() | |
3891 | { | |
24777478 | 3892 | wxRichTextAttr attr; |
5d7836c4 | 3893 | attr.SetFlags(wxTEXT_ATTR_FONT_ITALIC); |
7d76fbd5 | 3894 | attr.SetFontStyle(IsSelectionItalics() ? wxFONTSTYLE_NORMAL : wxFONTSTYLE_ITALIC); |
5d7836c4 JS |
3895 | |
3896 | if (HasSelection()) | |
3e3a754f | 3897 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 3898 | else |
063dbace JS |
3899 | { |
3900 | wxRichTextAttr current = GetDefaultStyleEx(); | |
3901 | current.Apply(attr); | |
3902 | SetAndShowDefaultStyle(current); | |
3903 | } | |
5d7836c4 JS |
3904 | return true; |
3905 | } | |
3906 | ||
3907 | /// Apply underline to the selection | |
3908 | bool wxRichTextCtrl::ApplyUnderlineToSelection() | |
3909 | { | |
24777478 | 3910 | wxRichTextAttr attr; |
5d7836c4 | 3911 | attr.SetFlags(wxTEXT_ATTR_FONT_UNDERLINE); |
4d551ad5 | 3912 | attr.SetFontUnderlined(!IsSelectionUnderlined()); |
5d7836c4 | 3913 | |
75936ec6 JS |
3914 | if (HasSelection()) |
3915 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); | |
3916 | else | |
3917 | { | |
3918 | wxRichTextAttr current = GetDefaultStyleEx(); | |
3919 | current.Apply(attr); | |
3920 | SetAndShowDefaultStyle(current); | |
3921 | } | |
3922 | return true; | |
3923 | } | |
3924 | ||
3925 | /// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection | |
3926 | bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags) | |
3927 | { | |
3928 | wxRichTextAttr attr; | |
3929 | attr.SetFlags(wxTEXT_ATTR_EFFECTS); | |
3930 | attr.SetTextEffectFlags(flags); | |
3931 | if (!DoesSelectionHaveTextEffectFlag(flags)) | |
3932 | attr.SetTextEffects(flags); | |
3933 | else | |
3934 | attr.SetTextEffects(attr.GetTextEffectFlags() & ~flags); | |
3935 | ||
5d7836c4 | 3936 | if (HasSelection()) |
3e3a754f | 3937 | return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY); |
5d7836c4 | 3938 | else |
063dbace JS |
3939 | { |
3940 | wxRichTextAttr current = GetDefaultStyleEx(); | |
3941 | current.Apply(attr); | |
3942 | SetAndShowDefaultStyle(current); | |
3943 | } | |
5d7836c4 JS |
3944 | return true; |
3945 | } | |
3946 | ||
3947 | /// Is all of the selection aligned according to the specified flag? | |
3966a9f4 | 3948 | bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment) |
5d7836c4 | 3949 | { |
0ca07313 | 3950 | wxRichTextRange range; |
5d7836c4 | 3951 | if (HasSelection()) |
a7ed48a5 | 3952 | range = GetSelectionRange(); |
5d7836c4 | 3953 | else |
a7ed48a5 | 3954 | range = wxRichTextRange(GetCaretPosition()+1, GetCaretPosition()+2); |
0ca07313 | 3955 | |
24777478 | 3956 | wxRichTextAttr attr; |
0ca07313 JS |
3957 | attr.SetAlignment(alignment); |
3958 | ||
3959 | return HasParagraphAttributes(range, attr); | |
5d7836c4 JS |
3960 | } |
3961 | ||
3962 | /// Apply alignment to the selection | |
3963 | bool wxRichTextCtrl::ApplyAlignmentToSelection(wxTextAttrAlignment alignment) | |
3964 | { | |
24777478 | 3965 | wxRichTextAttr attr; |
5d7836c4 JS |
3966 | attr.SetAlignment(alignment); |
3967 | if (HasSelection()) | |
3968 | return SetStyle(GetSelectionRange(), attr); | |
3969 | else | |
3970 | { | |
603f702b | 3971 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(GetCaretPosition()+1); |
5d7836c4 | 3972 | if (para) |
3e3a754f | 3973 | return SetStyleEx(para->GetRange().FromInternal(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY); |
5d7836c4 JS |
3974 | } |
3975 | return true; | |
3976 | } | |
3977 | ||
ab14c7aa | 3978 | /// Apply a named style to the selection |
1807a1f3 | 3979 | bool wxRichTextCtrl::ApplyStyle(wxRichTextStyleDefinition* def) |
ab14c7aa JS |
3980 | { |
3981 | // Flags are defined within each definition, so only certain | |
3982 | // attributes are applied. | |
24777478 | 3983 | wxRichTextAttr attr(GetStyleSheet() ? def->GetStyleMergedWithBase(GetStyleSheet()) : def->GetStyle()); |
1807a1f3 | 3984 | |
f2ab7bf3 | 3985 | int flags = wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_RESET; |
ab14c7aa | 3986 | |
38f833b1 JS |
3987 | if (def->IsKindOf(CLASSINFO(wxRichTextListStyleDefinition))) |
3988 | { | |
3989 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
41a85215 | 3990 | |
38f833b1 | 3991 | wxRichTextRange range; |
41a85215 | 3992 | |
38f833b1 JS |
3993 | if (HasSelection()) |
3994 | range = GetSelectionRange(); | |
3995 | else | |
3996 | { | |
41a85215 | 3997 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); |
38f833b1 JS |
3998 | range = wxRichTextRange(pos, pos+1); |
3999 | } | |
41a85215 WS |
4000 | |
4001 | return SetListStyle(range, (wxRichTextListStyleDefinition*) def, flags); | |
4002 | } | |
4003 | ||
edae63b0 JS |
4004 | bool isPara = false; |
4005 | ||
ab14c7aa JS |
4006 | // Make sure the attr has the style name |
4007 | if (def->IsKindOf(CLASSINFO(wxRichTextParagraphStyleDefinition))) | |
3e3a754f | 4008 | { |
edae63b0 | 4009 | isPara = true; |
ab14c7aa | 4010 | attr.SetParagraphStyleName(def->GetName()); |
1807a1f3 | 4011 | |
3e3a754f | 4012 | // If applying a paragraph style, we only want the paragraph nodes to adopt these |
38f833b1 | 4013 | // attributes, and not the leaf nodes. This will allow the content (e.g. text) |
3e3a754f JS |
4014 | // to change its style independently. |
4015 | flags |= wxRICHTEXT_SETSTYLE_PARAGRAPHS_ONLY; | |
4016 | } | |
b9433288 | 4017 | else if (def->IsKindOf(CLASSINFO(wxRichTextCharacterStyleDefinition))) |
ab14c7aa | 4018 | attr.SetCharacterStyleName(def->GetName()); |
b9433288 JS |
4019 | else if (def->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition))) |
4020 | attr.GetTextBoxAttr().SetBoxStyleName(def->GetName()); | |
ab14c7aa | 4021 | |
b9433288 JS |
4022 | if (def->IsKindOf(CLASSINFO(wxRichTextBoxStyleDefinition))) |
4023 | { | |
4024 | if (GetFocusObject() && (GetFocusObject() != & GetBuffer())) | |
4025 | { | |
4026 | SetStyle(GetFocusObject(), attr); | |
4027 | return true; | |
4028 | } | |
4029 | else | |
4030 | return false; | |
4031 | } | |
4032 | else if (HasSelection()) | |
1807a1f3 | 4033 | return SetStyleEx(GetSelectionRange(), attr, flags); |
ab14c7aa | 4034 | else |
1807a1f3 | 4035 | { |
24777478 JS |
4036 | wxRichTextAttr current = GetDefaultStyleEx(); |
4037 | wxRichTextAttr defaultStyle(attr); | |
edae63b0 JS |
4038 | if (isPara) |
4039 | { | |
4040 | // Don't apply extra character styles since they are already implied | |
4041 | // in the paragraph style | |
4042 | defaultStyle.SetFlags(defaultStyle.GetFlags() & ~wxTEXT_ATTR_CHARACTER); | |
4043 | } | |
4044 | current.Apply(defaultStyle); | |
063dbace | 4045 | SetAndShowDefaultStyle(current); |
edae63b0 JS |
4046 | |
4047 | // If it's a paragraph style, we want to apply the style to the | |
4048 | // current paragraph even if we didn't select any text. | |
4049 | if (isPara) | |
4050 | { | |
4051 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
603f702b | 4052 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(pos); |
edae63b0 JS |
4053 | if (para) |
4054 | { | |
4055 | return SetStyleEx(para->GetRange().FromInternal(), attr, flags); | |
4056 | } | |
4057 | } | |
1807a1f3 JS |
4058 | return true; |
4059 | } | |
ab14c7aa JS |
4060 | } |
4061 | ||
fe5aa22c JS |
4062 | /// Apply the style sheet to the buffer, for example if the styles have changed. |
4063 | bool wxRichTextCtrl::ApplyStyleSheet(wxRichTextStyleSheet* styleSheet) | |
4064 | { | |
4065 | if (!styleSheet) | |
4066 | styleSheet = GetBuffer().GetStyleSheet(); | |
4067 | if (!styleSheet) | |
4068 | return false; | |
4069 | ||
4070 | if (GetBuffer().ApplyStyleSheet(styleSheet)) | |
4071 | { | |
4072 | GetBuffer().Invalidate(wxRICHTEXT_ALL); | |
4073 | Refresh(false); | |
4074 | return true; | |
4075 | } | |
4076 | else | |
4077 | return false; | |
4078 | } | |
4079 | ||
5d7836c4 JS |
4080 | /// Sets the default style to the style under the cursor |
4081 | bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() | |
4082 | { | |
24777478 | 4083 | wxRichTextAttr attr; |
5d7836c4 JS |
4084 | attr.SetFlags(wxTEXT_ATTR_CHARACTER); |
4085 | ||
ab14c7aa JS |
4086 | // If at the start of a paragraph, use the next position. |
4087 | long pos = GetAdjustedCaretPosition(GetCaretPosition()); | |
4088 | ||
fe5aa22c | 4089 | if (GetUncombinedStyle(pos, attr)) |
5d7836c4 JS |
4090 | { |
4091 | SetDefaultStyle(attr); | |
4092 | return true; | |
4093 | } | |
41e155b4 WS |
4094 | |
4095 | return false; | |
5d7836c4 JS |
4096 | } |
4097 | ||
4d551ad5 JS |
4098 | /// Returns the first visible position in the current view |
4099 | long wxRichTextCtrl::GetFirstVisiblePosition() const | |
4100 | { | |
603f702b | 4101 | wxRichTextLine* line = GetFocusObject()->GetLineAtYPosition(GetLogicalPoint(wxPoint(0, 0)).y); |
4d551ad5 JS |
4102 | if (line) |
4103 | return line->GetAbsoluteRange().GetStart(); | |
4104 | else | |
4105 | return 0; | |
4106 | } | |
4107 | ||
ea160b2e JS |
4108 | /// Get the first visible point in the window |
4109 | wxPoint wxRichTextCtrl::GetFirstVisiblePoint() const | |
4110 | { | |
4111 | int ppuX, ppuY; | |
4112 | int startXUnits, startYUnits; | |
4113 | ||
4114 | GetScrollPixelsPerUnit(& ppuX, & ppuY); | |
4115 | GetViewStart(& startXUnits, & startYUnits); | |
4116 | ||
4117 | return wxPoint(startXUnits * ppuX, startYUnits * ppuY); | |
4118 | } | |
4119 | ||
ab14c7aa JS |
4120 | /// The adjusted caret position is the character position adjusted to take |
4121 | /// into account whether we're at the start of a paragraph, in which case | |
4122 | /// style information should be taken from the next position, not current one. | |
4123 | long wxRichTextCtrl::GetAdjustedCaretPosition(long caretPos) const | |
4124 | { | |
603f702b | 4125 | wxRichTextParagraph* para = GetFocusObject()->GetParagraphAtPosition(caretPos+1); |
ab14c7aa JS |
4126 | |
4127 | if (para && (caretPos+1 == para->GetRange().GetStart())) | |
4128 | caretPos ++; | |
4129 | return caretPos; | |
4130 | } | |
4131 | ||
96c9f0f6 JS |
4132 | /// Get/set the selection range in character positions. -1, -1 means no selection. |
4133 | /// The range is in API convention, i.e. a single character selection is denoted | |
4134 | /// by (n, n+1) | |
4135 | wxRichTextRange wxRichTextCtrl::GetSelectionRange() const | |
4136 | { | |
4137 | wxRichTextRange range = GetInternalSelectionRange(); | |
4138 | if (range != wxRichTextRange(-2,-2) && range != wxRichTextRange(-1,-1)) | |
4139 | range.SetEnd(range.GetEnd() + 1); | |
4140 | return range; | |
4141 | } | |
4142 | ||
4143 | void wxRichTextCtrl::SetSelectionRange(const wxRichTextRange& range) | |
4144 | { | |
76d7fc29 | 4145 | SetSelection(range.GetStart(), range.GetEnd()); |
96c9f0f6 JS |
4146 | } |
4147 | ||
38f833b1 JS |
4148 | /// Set list style |
4149 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
4150 | { | |
603f702b | 4151 | return GetFocusObject()->SetListStyle(range.ToInternal(), def, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4152 | } |
4153 | ||
4154 | bool wxRichTextCtrl::SetListStyle(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
4155 | { | |
603f702b | 4156 | return GetFocusObject()->SetListStyle(range.ToInternal(), defName, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4157 | } |
4158 | ||
4159 | /// Clear list for given range | |
4160 | bool wxRichTextCtrl::ClearListStyle(const wxRichTextRange& range, int flags) | |
4161 | { | |
603f702b | 4162 | return GetFocusObject()->ClearListStyle(range.ToInternal(), flags); |
38f833b1 JS |
4163 | } |
4164 | ||
4165 | /// Number/renumber any list elements in the given range | |
4166 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int startFrom, int specifiedLevel) | |
4167 | { | |
603f702b | 4168 | return GetFocusObject()->NumberList(range.ToInternal(), def, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4169 | } |
4170 | ||
4171 | bool wxRichTextCtrl::NumberList(const wxRichTextRange& range, const wxString& defName, int flags, int startFrom, int specifiedLevel) | |
4172 | { | |
603f702b | 4173 | return GetFocusObject()->NumberList(range.ToInternal(), defName, flags, startFrom, specifiedLevel); |
38f833b1 JS |
4174 | } |
4175 | ||
4176 | /// Promote the list items within the given range. promoteBy can be a positive or negative number, e.g. 1 or -1 | |
4177 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, wxRichTextListStyleDefinition* def, int flags, int specifiedLevel) | |
4178 | { | |
603f702b | 4179 | return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), def, flags, specifiedLevel); |
38f833b1 JS |
4180 | } |
4181 | ||
4182 | bool wxRichTextCtrl::PromoteList(int promoteBy, const wxRichTextRange& range, const wxString& defName, int flags, int specifiedLevel) | |
4183 | { | |
603f702b | 4184 | return GetFocusObject()->PromoteList(promoteBy, range.ToInternal(), defName, flags, specifiedLevel); |
38f833b1 JS |
4185 | } |
4186 | ||
12cc29c5 JS |
4187 | /// Deletes the content in the given range |
4188 | bool wxRichTextCtrl::Delete(const wxRichTextRange& range) | |
4189 | { | |
603f702b | 4190 | return GetFocusObject()->DeleteRangeWithUndo(range.ToInternal(), this, & GetBuffer()); |
12cc29c5 JS |
4191 | } |
4192 | ||
dadd4f55 JS |
4193 | const wxArrayString& wxRichTextCtrl::GetAvailableFontNames() |
4194 | { | |
4195 | if (sm_availableFontNames.GetCount() == 0) | |
4196 | { | |
4197 | sm_availableFontNames = wxFontEnumerator::GetFacenames(); | |
4198 | sm_availableFontNames.Sort(); | |
4199 | } | |
4200 | return sm_availableFontNames; | |
4201 | } | |
4202 | ||
4203 | void wxRichTextCtrl::ClearAvailableFontNames() | |
4204 | { | |
4205 | sm_availableFontNames.Clear(); | |
4206 | } | |
4207 | ||
c0a5145a | 4208 | void wxRichTextCtrl::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
52170c5b JS |
4209 | { |
4210 | //wxLogDebug(wxT("wxRichTextCtrl::OnSysColourChanged")); | |
4211 | ||
4212 | wxTextAttrEx basicStyle = GetBasicStyle(); | |
4213 | basicStyle.SetTextColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT)); | |
4214 | SetBasicStyle(basicStyle); | |
4215 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
4216 | ||
4217 | Refresh(); | |
4218 | } | |
4219 | ||
7051fa41 | 4220 | // Refresh the area affected by a selection change |
603f702b JS |
4221 | bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection& oldSelection, const wxRichTextSelection& newSelection) |
4222 | { | |
4223 | // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that | |
4224 | // the selection contains whole containers rather than just text, so refresh everything | |
4225 | // for now as it would be hard to compute the rectangle bounding all selections. | |
4226 | // TODO: improve on this. | |
4227 | if ((oldSelection.IsValid() && (oldSelection.GetContainer() != GetFocusObject() || oldSelection.GetCount() > 1)) || | |
4228 | (newSelection.IsValid() && (newSelection.GetContainer() != GetFocusObject() || newSelection.GetCount() > 1))) | |
4229 | { | |
4230 | Refresh(false); | |
4231 | return true; | |
4232 | } | |
4fe83b93 | 4233 | |
603f702b JS |
4234 | wxRichTextRange oldRange, newRange; |
4235 | if (oldSelection.IsValid()) | |
4236 | oldRange = oldSelection.GetRange(); | |
4237 | else | |
4238 | oldRange = wxRICHTEXT_NO_SELECTION; | |
4239 | if (newSelection.IsValid()) | |
4240 | newRange = newSelection.GetRange(); | |
4241 | else | |
4242 | newRange = wxRICHTEXT_NO_SELECTION; | |
4fe83b93 | 4243 | |
7051fa41 JS |
4244 | // Calculate the refresh rectangle - just the affected lines |
4245 | long firstPos, lastPos; | |
603f702b | 4246 | if (oldRange.GetStart() == -2 && newRange.GetStart() != -2) |
7051fa41 | 4247 | { |
603f702b JS |
4248 | firstPos = newRange.GetStart(); |
4249 | lastPos = newRange.GetEnd(); | |
7051fa41 | 4250 | } |
603f702b | 4251 | else if (oldRange.GetStart() != -2 && newRange.GetStart() == -2) |
7051fa41 | 4252 | { |
603f702b JS |
4253 | firstPos = oldRange.GetStart(); |
4254 | lastPos = oldRange.GetEnd(); | |
7051fa41 | 4255 | } |
603f702b | 4256 | else if (oldRange.GetStart() == -2 && newRange.GetStart() == -2) |
7051fa41 JS |
4257 | { |
4258 | return false; | |
4259 | } | |
4260 | else | |
4261 | { | |
603f702b JS |
4262 | firstPos = wxMin(oldRange.GetStart(), newRange.GetStart()); |
4263 | lastPos = wxMax(oldRange.GetEnd(), newRange.GetEnd()); | |
7051fa41 JS |
4264 | } |
4265 | ||
603f702b JS |
4266 | wxRichTextLine* firstLine = GetFocusObject()->GetLineAtPosition(firstPos); |
4267 | wxRichTextLine* lastLine = GetFocusObject()->GetLineAtPosition(lastPos); | |
7051fa41 JS |
4268 | |
4269 | if (firstLine && lastLine) | |
4270 | { | |
4271 | wxSize clientSize = GetClientSize(); | |
4272 | wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition()); | |
4273 | wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y); | |
4274 | ||
4275 | pt1.x = 0; | |
4276 | pt1.y = wxMax(0, pt1.y); | |
4277 | pt2.x = 0; | |
4278 | pt2.y = wxMin(clientSize.y, pt2.y); | |
4279 | ||
4280 | wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y)); | |
4281 | RefreshRect(rect, false); | |
4282 | } | |
4283 | else | |
4284 | Refresh(false); | |
4285 | ||
4286 | return true; | |
4287 | } | |
4288 | ||
603f702b JS |
4289 | // margins functions |
4290 | bool wxRichTextCtrl::DoSetMargins(const wxPoint& pt) | |
4291 | { | |
4292 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS); | |
4293 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS); | |
4294 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS); | |
4295 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS); | |
4fe83b93 | 4296 | |
603f702b JS |
4297 | return true; |
4298 | } | |
4299 | ||
4300 | wxPoint wxRichTextCtrl::DoGetMargins() const | |
4301 | { | |
4302 | return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(), | |
4303 | GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue()); | |
4304 | } | |
4305 | ||
4306 | bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox* obj, bool setCaretPosition) | |
4307 | { | |
4308 | if (obj && !obj->AcceptsFocus()) | |
4309 | return false; | |
4310 | ||
4311 | wxRichTextParagraphLayoutBox* oldContainer = GetFocusObject(); | |
4312 | bool changingContainer = (m_focusObject != obj); | |
4313 | ||
eadb039e JS |
4314 | if (changingContainer && HasSelection()) |
4315 | SelectNone(); | |
4316 | ||
603f702b | 4317 | m_focusObject = obj; |
4fe83b93 | 4318 | |
603f702b JS |
4319 | if (!obj) |
4320 | m_focusObject = & m_buffer; | |
4321 | ||
4322 | if (setCaretPosition && changingContainer) | |
4323 | { | |
4324 | m_selection.Reset(); | |
4325 | m_selectionAnchor = -2; | |
4326 | m_selectionAnchorObject = NULL; | |
4327 | m_selectionState = wxRichTextCtrlSelectionState_Normal; | |
4328 | ||
4329 | long pos = -1; | |
4fe83b93 | 4330 | |
603f702b JS |
4331 | m_caretAtLineStart = false; |
4332 | MoveCaret(pos, m_caretAtLineStart); | |
4333 | SetDefaultStyleToCursorStyle(); | |
4334 | ||
4335 | wxRichTextEvent cmdEvent( | |
4336 | wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED, | |
4337 | GetId()); | |
4338 | cmdEvent.SetEventObject(this); | |
4339 | cmdEvent.SetPosition(m_caretPosition+1); | |
4340 | cmdEvent.SetOldContainer(oldContainer); | |
4341 | cmdEvent.SetContainer(m_focusObject); | |
4342 | ||
4343 | GetEventHandler()->ProcessEvent(cmdEvent); | |
4344 | } | |
4345 | return true; | |
4346 | } | |
4347 | ||
0c0e063e JS |
4348 | #if wxUSE_DRAG_AND_DROP |
4349 | void wxRichTextCtrl::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def, wxDataObject* DataObj) | |
4350 | { | |
4351 | m_preDrag = false; | |
4352 | ||
4353 | if ((def != wxDragCopy) && (def != wxDragMove)) | |
4354 | { | |
4355 | return; | |
4356 | } | |
4357 | ||
4358 | if (!GetSelection().IsValid()) | |
4359 | { | |
4360 | return; | |
4361 | } | |
4362 | ||
4363 | wxRichTextParagraphLayoutBox* originContainer = GetSelection().GetContainer(); | |
4364 | wxRichTextParagraphLayoutBox* destContainer = GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one | |
4365 | ||
4366 | ||
4367 | wxRichTextBuffer* richTextBuffer = ((wxRichTextBufferDataObject*)DataObj)->GetRichTextBuffer(); | |
4368 | if (richTextBuffer) | |
4369 | { | |
4370 | long position = GetCaretPosition(); | |
4371 | wxRichTextRange selectionrange = GetInternalSelectionRange(); | |
4372 | if (selectionrange.Contains(position) && (def == wxDragMove)) | |
4373 | { | |
4374 | // It doesn't make sense to move onto itself | |
4375 | return; | |
4376 | } | |
4377 | ||
4378 | // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection | |
4379 | // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway | |
4380 | bool DeleteAfter = (def == wxDragMove) && (position > selectionrange.GetEnd()); | |
4381 | if ((def == wxDragMove) && !DeleteAfter) | |
4382 | { | |
4383 | // We can't use e.g. DeleteSelectedContent() as it uses the focus container | |
4384 | originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer()); | |
4385 | } | |
4386 | ||
4e63bfb9 | 4387 | destContainer->InsertParagraphsWithUndo(&GetBuffer(), position+1, *richTextBuffer, this, 0); |
0c0e063e JS |
4388 | ShowPosition(position + richTextBuffer->GetOwnRange().GetEnd()); |
4389 | ||
4390 | delete richTextBuffer; | |
4391 | ||
4392 | if (DeleteAfter) | |
4393 | { | |
4394 | // We can't use e.g. DeleteSelectedContent() as it uses the focus container | |
4395 | originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer()); | |
4396 | } | |
4397 | ||
4398 | ||
4399 | SelectNone(); | |
4400 | Refresh(); | |
4401 | } | |
4402 | } | |
4403 | #endif // wxUSE_DRAG_AND_DROP | |
4404 | ||
4405 | ||
4406 | #if wxUSE_DRAG_AND_DROP | |
4407 | bool wxRichTextDropSource::GiveFeedback(wxDragResult WXUNUSED(effect)) | |
4408 | { | |
4409 | wxCHECK_MSG(m_rtc, false, wxT("NULL m_rtc")); | |
4410 | ||
4411 | long position = 0; | |
4412 | int hit = 0; | |
4413 | wxRichTextObject* hitObj = NULL; | |
4414 | wxRichTextParagraphLayoutBox* container = m_rtc->FindContainerAtPoint(m_rtc->ScreenToClient(wxGetMousePosition()), position, hit, hitObj); | |
4415 | ||
4416 | if (!(hit & wxRICHTEXT_HITTEST_NONE) && container && container->AcceptsFocus()) | |
4417 | { | |
4418 | m_rtc->StoreFocusObject(container); | |
4419 | m_rtc->SetCaretPositionAfterClick(container, position, hit); | |
4420 | } | |
4421 | ||
4422 | return false; // so that the base-class sets a cursor | |
4423 | } | |
4424 | #endif // wxUSE_DRAG_AND_DROP | |
4425 | ||
4426 | ||
1c13f06e JS |
4427 | #if wxRICHTEXT_USE_OWN_CARET |
4428 | ||
4429 | // ---------------------------------------------------------------------------- | |
4430 | // initialization and destruction | |
4431 | // ---------------------------------------------------------------------------- | |
4432 | ||
4433 | void wxRichTextCaret::Init() | |
4434 | { | |
4435 | m_hasFocus = true; | |
cd45ed36 | 4436 | m_refreshEnabled = true; |
1c13f06e JS |
4437 | |
4438 | m_xOld = | |
4439 | m_yOld = -1; | |
4440 | m_richTextCtrl = NULL; | |
4441 | m_needsUpdate = false; | |
a188ac29 | 4442 | m_flashOn = true; |
1c13f06e JS |
4443 | } |
4444 | ||
4445 | wxRichTextCaret::~wxRichTextCaret() | |
4446 | { | |
a188ac29 JS |
4447 | if (m_timer.IsRunning()) |
4448 | m_timer.Stop(); | |
1c13f06e JS |
4449 | } |
4450 | ||
4451 | // ---------------------------------------------------------------------------- | |
4452 | // showing/hiding/moving the caret (base class interface) | |
4453 | // ---------------------------------------------------------------------------- | |
4454 | ||
4455 | void wxRichTextCaret::DoShow() | |
4456 | { | |
a188ac29 | 4457 | m_flashOn = true; |
ab52bac8 | 4458 | |
a188ac29 JS |
4459 | if (!m_timer.IsRunning()) |
4460 | m_timer.Start(GetBlinkTime()); | |
4461 | ||
1c13f06e JS |
4462 | Refresh(); |
4463 | } | |
4464 | ||
4465 | void wxRichTextCaret::DoHide() | |
4466 | { | |
a188ac29 JS |
4467 | if (m_timer.IsRunning()) |
4468 | m_timer.Stop(); | |
4469 | ||
1c13f06e JS |
4470 | Refresh(); |
4471 | } | |
4472 | ||
4473 | void wxRichTextCaret::DoMove() | |
4474 | { | |
4475 | if (IsVisible()) | |
4476 | { | |
4477 | Refresh(); | |
4478 | ||
4479 | if (m_xOld != -1 && m_yOld != -1) | |
4480 | { | |
cd45ed36 | 4481 | if (m_richTextCtrl && m_refreshEnabled) |
1c13f06e JS |
4482 | { |
4483 | wxRect rect(GetPosition(), GetSize()); | |
4484 | m_richTextCtrl->RefreshRect(rect, false); | |
4485 | } | |
4486 | } | |
4487 | } | |
4488 | ||
4489 | m_xOld = m_x; | |
4490 | m_yOld = m_y; | |
4491 | } | |
4492 | ||
4493 | void wxRichTextCaret::DoSize() | |
4494 | { | |
4495 | int countVisible = m_countVisible; | |
4496 | if (countVisible > 0) | |
4497 | { | |
4498 | m_countVisible = 0; | |
4499 | DoHide(); | |
4500 | } | |
4501 | ||
4502 | if (countVisible > 0) | |
4503 | { | |
4504 | m_countVisible = countVisible; | |
4505 | DoShow(); | |
4506 | } | |
4507 | } | |
4508 | ||
4509 | // ---------------------------------------------------------------------------- | |
4510 | // handling the focus | |
4511 | // ---------------------------------------------------------------------------- | |
4512 | ||
4513 | void wxRichTextCaret::OnSetFocus() | |
4514 | { | |
4515 | m_hasFocus = true; | |
4516 | ||
4517 | if ( IsVisible() ) | |
4518 | Refresh(); | |
4519 | } | |
4520 | ||
4521 | void wxRichTextCaret::OnKillFocus() | |
4522 | { | |
4523 | m_hasFocus = false; | |
4524 | } | |
4525 | ||
4526 | // ---------------------------------------------------------------------------- | |
4527 | // drawing the caret | |
4528 | // ---------------------------------------------------------------------------- | |
4529 | ||
4530 | void wxRichTextCaret::Refresh() | |
4531 | { | |
cd45ed36 | 4532 | if (m_richTextCtrl && m_refreshEnabled) |
1c13f06e JS |
4533 | { |
4534 | wxRect rect(GetPosition(), GetSize()); | |
4535 | m_richTextCtrl->RefreshRect(rect, false); | |
4536 | } | |
4537 | } | |
4538 | ||
4539 | void wxRichTextCaret::DoDraw(wxDC *dc) | |
4540 | { | |
4541 | dc->SetPen( *wxBLACK_PEN ); | |
4542 | ||
4543 | dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH)); | |
4544 | dc->SetPen(*wxBLACK_PEN); | |
4545 | ||
1c13f06e JS |
4546 | wxPoint pt(m_x, m_y); |
4547 | ||
4548 | if (m_richTextCtrl) | |
4549 | { | |
4550 | pt = m_richTextCtrl->GetLogicalPoint(pt); | |
4551 | } | |
a188ac29 JS |
4552 | if (IsVisible() && m_flashOn) |
4553 | dc->DrawRectangle(pt.x, pt.y, m_width, m_height); | |
4554 | } | |
4555 | ||
4556 | void wxRichTextCaret::Notify() | |
4557 | { | |
4558 | m_flashOn = !m_flashOn; | |
4559 | Refresh(); | |
4560 | } | |
4561 | ||
4562 | void wxRichTextCaretTimer::Notify() | |
4563 | { | |
4564 | m_caret->Notify(); | |
1c13f06e JS |
4565 | } |
4566 | #endif | |
4567 | // wxRICHTEXT_USE_OWN_CARET | |
4568 | ||
603f702b JS |
4569 | // Add an item |
4570 | bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString& label, wxRichTextObject* obj) | |
4571 | { | |
4572 | if (GetCount() < 3) | |
4573 | { | |
4574 | m_labels.Add(label); | |
4575 | m_objects.Add(obj); | |
4576 | return true; | |
4577 | } | |
4578 | else | |
4579 | return false; | |
4580 | } | |
4581 | ||
4582 | // Returns number of menu items were added. | |
4583 | int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu* menu, int startCmd) const | |
4584 | { | |
4585 | wxMenuItem* item = menu->FindItem(startCmd); | |
9b794421 JS |
4586 | // If none of the standard properties identifiers are in the menu, add them if necessary. |
4587 | // If no items to add, just set the text to something generic | |
4588 | if (GetCount() == 0) | |
603f702b | 4589 | { |
9b794421 | 4590 | if (item) |
603f702b JS |
4591 | { |
4592 | menu->SetLabel(startCmd, _("&Properties")); | |
4fe83b93 | 4593 | |
603f702b JS |
4594 | // Delete the others if necessary |
4595 | int i; | |
4596 | for (i = startCmd+1; i < startCmd+3; i++) | |
4597 | { | |
4598 | if (menu->FindItem(i)) | |
4599 | { | |
4600 | menu->Delete(i); | |
4601 | } | |
4602 | } | |
4603 | } | |
9b794421 JS |
4604 | } |
4605 | else | |
4606 | { | |
4607 | int i; | |
4608 | int pos = -1; | |
4609 | // Find the position of the first properties item | |
4610 | for (i = 0; i < (int) menu->GetMenuItemCount(); i++) | |
603f702b | 4611 | { |
9b794421 JS |
4612 | wxMenuItem* item = menu->FindItemByPosition(i); |
4613 | if (item && item->GetId() == startCmd) | |
603f702b | 4614 | { |
9b794421 JS |
4615 | pos = i; |
4616 | break; | |
603f702b | 4617 | } |
9b794421 | 4618 | } |
4fe83b93 | 4619 | |
9b794421 JS |
4620 | if (pos != -1) |
4621 | { | |
4622 | int insertBefore = pos+1; | |
4623 | for (i = startCmd; i < startCmd+GetCount(); i++) | |
603f702b | 4624 | { |
9b794421 | 4625 | if (menu->FindItem(i)) |
603f702b | 4626 | { |
9b794421 JS |
4627 | menu->SetLabel(i, m_labels[i - startCmd]); |
4628 | } | |
4629 | else | |
4630 | { | |
4631 | if (insertBefore >= (int) menu->GetMenuItemCount()) | |
4632 | menu->Append(i, m_labels[i - startCmd]); | |
603f702b | 4633 | else |
9b794421 | 4634 | menu->Insert(insertBefore, i, m_labels[i - startCmd]); |
603f702b | 4635 | } |
9b794421 JS |
4636 | insertBefore ++; |
4637 | } | |
4fe83b93 | 4638 | |
9b794421 JS |
4639 | // Delete any old items still left on the menu |
4640 | for (i = startCmd + GetCount(); i < startCmd+3; i++) | |
4641 | { | |
4642 | if (menu->FindItem(i)) | |
603f702b | 4643 | { |
9b794421 | 4644 | menu->Delete(i); |
603f702b JS |
4645 | } |
4646 | } | |
4647 | } | |
9b794421 JS |
4648 | else |
4649 | { | |
4650 | // No existing property identifiers were found, so append to the end of the menu. | |
4651 | menu->AppendSeparator(); | |
4652 | for (i = startCmd; i < startCmd+GetCount(); i++) | |
4653 | { | |
4654 | menu->Append(i, m_labels[i - startCmd]); | |
4655 | } | |
4656 | } | |
603f702b JS |
4657 | } |
4658 | ||
4659 | return GetCount(); | |
4660 | } | |
4661 | ||
4662 | // Add appropriate menu items for the current container and clicked on object | |
4663 | // (and container's parent, if appropriate). | |
4664 | int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextObject* container, wxRichTextObject* obj) | |
4665 | { | |
4666 | Clear(); | |
4667 | if (obj && obj->CanEditProperties()) | |
4668 | AddItem(obj->GetPropertiesMenuLabel(), obj); | |
4669 | ||
4670 | if (container && container != obj && container->CanEditProperties() && m_labels.Index(container->GetPropertiesMenuLabel()) == wxNOT_FOUND) | |
4671 | AddItem(container->GetPropertiesMenuLabel(), container); | |
4672 | ||
4673 | if (container && container->GetParent() && container->GetParent()->CanEditProperties() && m_labels.Index(container->GetParent()->GetPropertiesMenuLabel()) == wxNOT_FOUND) | |
4674 | AddItem(container->GetParent()->GetPropertiesMenuLabel(), container->GetParent()); | |
4675 | ||
4676 | return GetCount(); | |
4677 | } | |
4678 | ||
5d7836c4 JS |
4679 | #endif |
4680 | // wxUSE_RICHTEXT |