]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/html/htmlwin.cpp |
5526e819 VS |
3 | // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation) |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 | 6 | // Copyright: (c) 1999 Vaclav Slavik |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
3096bd2f | 10 | #include "wx/wxprec.h" |
5526e819 | 11 | |
2b5f62a0 | 12 | #ifdef __BORLANDC__ |
93763ad5 | 13 | #pragma hdrstop |
5526e819 VS |
14 | #endif |
15 | ||
93763ad5 WS |
16 | #if wxUSE_HTML && wxUSE_STREAMS |
17 | ||
b4f4d3dd | 18 | #ifndef WX_PRECOMP |
8ecff181 | 19 | #include "wx/list.h" |
04dbb646 VZ |
20 | #include "wx/log.h" |
21 | #include "wx/intl.h" | |
22 | #include "wx/dcclient.h" | |
23 | #include "wx/frame.h" | |
f38924e8 | 24 | #include "wx/dcmemory.h" |
c0badb70 | 25 | #include "wx/timer.h" |
9eddec69 | 26 | #include "wx/settings.h" |
28f92d74 | 27 | #include "wx/dataobj.h" |
82a46104 | 28 | #include "wx/statusbr.h" |
5526e819 VS |
29 | #endif |
30 | ||
69941f05 | 31 | #include "wx/html/htmlwin.h" |
892aeafc | 32 | #include "wx/html/htmlproc.h" |
e3774124 | 33 | #include "wx/clipbrd.h" |
a7b2c092 | 34 | #include "wx/recguard.h" |
04dbb646 VZ |
35 | |
36 | #include "wx/arrimpl.cpp" | |
892aeafc VS |
37 | #include "wx/listimpl.cpp" |
38 | ||
03a187cc VZ |
39 | // uncomment this line to visually show the extent of the selection |
40 | //#define DEBUG_HTML_SELECTION | |
41 | ||
a1c3cdc4 VS |
42 | // HTML events: |
43 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlLinkEvent, wxCommandEvent) | |
44 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellEvent, wxCommandEvent) | |
45 | ||
ce7fe42e VZ |
46 | wxDEFINE_EVENT( wxEVT_HTML_CELL_CLICKED, wxHtmlCellEvent ); |
47 | wxDEFINE_EVENT( wxEVT_HTML_CELL_HOVER, wxHtmlCellEvent ); | |
48 | wxDEFINE_EVENT( wxEVT_HTML_LINK_CLICKED, wxHtmlLinkEvent ); | |
a1c3cdc4 | 49 | |
1338c59a | 50 | |
1338c59a VS |
51 | #if wxUSE_CLIPBOARD |
52 | // ---------------------------------------------------------------------------- | |
53 | // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll | |
54 | // events when a captured mouse is held outside the window | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | class wxHtmlWinAutoScrollTimer : public wxTimer | |
58 | { | |
59 | public: | |
60 | wxHtmlWinAutoScrollTimer(wxScrolledWindow *win, | |
61 | wxEventType eventTypeToSend, | |
62 | int pos, int orient) | |
63 | { | |
64 | m_win = win; | |
65 | m_eventType = eventTypeToSend; | |
66 | m_pos = pos; | |
67 | m_orient = orient; | |
68 | } | |
69 | ||
70 | virtual void Notify(); | |
71 | ||
72 | private: | |
73 | wxScrolledWindow *m_win; | |
74 | wxEventType m_eventType; | |
75 | int m_pos, | |
76 | m_orient; | |
77 | ||
c0c133e1 | 78 | wxDECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer); |
1338c59a VS |
79 | }; |
80 | ||
81 | void wxHtmlWinAutoScrollTimer::Notify() | |
82 | { | |
83 | // only do all this as long as the window is capturing the mouse | |
84 | if ( wxWindow::GetCapture() != m_win ) | |
85 | { | |
86 | Stop(); | |
87 | } | |
88 | else // we still capture the mouse, continue generating events | |
89 | { | |
90 | // first scroll the window if we are allowed to do it | |
91 | wxScrollWinEvent event1(m_eventType, m_pos, m_orient); | |
92 | event1.SetEventObject(m_win); | |
93 | if ( m_win->GetEventHandler()->ProcessEvent(event1) ) | |
94 | { | |
95 | // and then send a pseudo mouse-move event to refresh the selection | |
96 | wxMouseEvent event2(wxEVT_MOTION); | |
97 | wxGetMousePosition(&event2.m_x, &event2.m_y); | |
98 | ||
99 | // the mouse event coordinates should be client, not screen as | |
100 | // returned by wxGetMousePosition | |
101 | wxWindow *parentTop = m_win; | |
102 | while ( parentTop->GetParent() ) | |
103 | parentTop = parentTop->GetParent(); | |
104 | wxPoint ptOrig = parentTop->GetPosition(); | |
105 | event2.m_x -= ptOrig.x; | |
106 | event2.m_y -= ptOrig.y; | |
107 | ||
108 | event2.SetEventObject(m_win); | |
109 | ||
110 | // FIXME: we don't fill in the other members - ok? | |
111 | m_win->GetEventHandler()->ProcessEvent(event2); | |
112 | } | |
113 | else // can't scroll further, stop | |
114 | { | |
115 | Stop(); | |
116 | } | |
117 | } | |
118 | } | |
d659d703 VZ |
119 | |
120 | #endif // wxUSE_CLIPBOARD | |
1338c59a VS |
121 | |
122 | ||
123 | ||
892aeafc VS |
124 | //----------------------------------------------------------------------------- |
125 | // wxHtmlHistoryItem | |
126 | //----------------------------------------------------------------------------- | |
127 | ||
128 | // item of history list | |
4460b6c4 | 129 | class WXDLLIMPEXP_HTML wxHtmlHistoryItem |
892aeafc VS |
130 | { |
131 | public: | |
132 | wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;} | |
133 | int GetPos() const {return m_Pos;} | |
134 | void SetPos(int p) {m_Pos = p;} | |
135 | const wxString& GetPage() const {return m_Page;} | |
136 | const wxString& GetAnchor() const {return m_Anchor;} | |
137 | ||
138 | private: | |
139 | wxString m_Page; | |
140 | wxString m_Anchor; | |
141 | int m_Pos; | |
142 | }; | |
5526e819 | 143 | |
5526e819 VS |
144 | |
145 | //----------------------------------------------------------------------------- | |
892aeafc | 146 | // our private arrays: |
5526e819 VS |
147 | //----------------------------------------------------------------------------- |
148 | ||
892aeafc | 149 | WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray); |
17a1ebd1 | 150 | WX_DEFINE_OBJARRAY(wxHtmlHistoryArray) |
5526e819 | 151 | |
892aeafc | 152 | WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList); |
17a1ebd1 | 153 | WX_DEFINE_LIST(wxHtmlProcessorList) |
5526e819 | 154 | |
bc55e31b VS |
155 | //----------------------------------------------------------------------------- |
156 | // wxHtmlWindowMouseHelper | |
157 | //----------------------------------------------------------------------------- | |
158 | ||
159 | wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface *iface) | |
160 | : m_tmpMouseMoved(false), | |
161 | m_tmpLastLink(NULL), | |
162 | m_tmpLastCell(NULL), | |
163 | m_interface(iface) | |
164 | { | |
165 | } | |
166 | ||
167 | void wxHtmlWindowMouseHelper::HandleMouseMoved() | |
168 | { | |
169 | m_tmpMouseMoved = true; | |
170 | } | |
171 | ||
172 | bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell *rootCell, | |
173 | const wxPoint& pos, | |
174 | const wxMouseEvent& event) | |
175 | { | |
176 | if (!rootCell) | |
177 | return false; | |
178 | ||
179 | wxHtmlCell *cell = rootCell->FindCellByPos(pos.x, pos.y); | |
180 | // this check is needed because FindCellByPos returns terminal cell and | |
181 | // containers may have empty borders -- in this case NULL will be | |
182 | // returned | |
183 | if (!cell) | |
184 | return false; | |
185 | ||
186 | // adjust the coordinates to be relative to this cell: | |
187 | wxPoint relpos = pos - cell->GetAbsPos(rootCell); | |
188 | ||
189 | return OnCellClicked(cell, relpos.x, relpos.y, event); | |
190 | } | |
191 | ||
192 | void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell *rootCell, | |
193 | const wxPoint& pos) | |
194 | { | |
195 | wxHtmlCell *cell = rootCell ? rootCell->FindCellByPos(pos.x, pos.y) : NULL; | |
196 | ||
197 | if (cell != m_tmpLastCell) | |
198 | { | |
199 | wxHtmlLinkInfo *lnk = NULL; | |
200 | if (cell) | |
201 | { | |
202 | // adjust the coordinates to be relative to this cell: | |
203 | wxPoint relpos = pos - cell->GetAbsPos(rootCell); | |
204 | lnk = cell->GetLink(relpos.x, relpos.y); | |
205 | } | |
206 | ||
207 | wxCursor cur; | |
208 | if (cell) | |
88a1b648 | 209 | cur = cell->GetMouseCursor(m_interface); |
bc55e31b | 210 | else |
88a1b648 VS |
211 | cur = m_interface->GetHTMLCursor( |
212 | wxHtmlWindowInterface::HTMLCursor_Default); | |
213 | ||
bc55e31b VS |
214 | m_interface->GetHTMLWindow()->SetCursor(cur); |
215 | ||
216 | if (lnk != m_tmpLastLink) | |
217 | { | |
218 | if (lnk) | |
219 | m_interface->SetHTMLStatusText(lnk->GetHref()); | |
220 | else | |
221 | m_interface->SetHTMLStatusText(wxEmptyString); | |
222 | ||
223 | m_tmpLastLink = lnk; | |
224 | } | |
225 | ||
226 | m_tmpLastCell = cell; | |
227 | } | |
228 | else // mouse moved but stayed in the same cell | |
229 | { | |
230 | if ( cell ) | |
231 | { | |
232 | OnCellMouseHover(cell, pos.x, pos.y); | |
233 | } | |
234 | } | |
235 | ||
236 | m_tmpMouseMoved = false; | |
237 | } | |
238 | ||
239 | bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell *cell, | |
240 | wxCoord x, wxCoord y, | |
241 | const wxMouseEvent& event) | |
242 | { | |
ce7fe42e | 243 | wxHtmlCellEvent ev(wxEVT_HTML_CELL_CLICKED, |
a1c3cdc4 VS |
244 | m_interface->GetHTMLWindow()->GetId(), |
245 | cell, wxPoint(x,y), event); | |
246 | ||
247 | if (!m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev)) | |
248 | { | |
249 | // if the event wasn't handled, do the default processing here: | |
250 | ||
9a83f860 | 251 | wxASSERT_MSG( cell, wxT("can't be called with NULL cell") ); |
a1c3cdc4 VS |
252 | |
253 | cell->ProcessMouseClick(m_interface, ev.GetPoint(), ev.GetMouseEvent()); | |
254 | } | |
bc55e31b | 255 | |
a1c3cdc4 VS |
256 | // true if a link was clicked, false otherwise |
257 | return ev.GetLinkClicked(); | |
bc55e31b VS |
258 | } |
259 | ||
a1c3cdc4 VS |
260 | void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell * cell, |
261 | wxCoord x, | |
262 | wxCoord y) | |
bc55e31b | 263 | { |
ce7fe42e | 264 | wxHtmlCellEvent ev(wxEVT_HTML_CELL_HOVER, |
a1c3cdc4 VS |
265 | m_interface->GetHTMLWindow()->GetId(), |
266 | cell, wxPoint(x,y), wxMouseEvent()); | |
267 | m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev); | |
bc55e31b VS |
268 | } |
269 | ||
a1c3cdc4 VS |
270 | |
271 | ||
272 | ||
892aeafc VS |
273 | //----------------------------------------------------------------------------- |
274 | // wxHtmlWindow | |
275 | //----------------------------------------------------------------------------- | |
5526e819 | 276 | |
88a1b648 VS |
277 | wxList wxHtmlWindow::m_Filters; |
278 | wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL; | |
279 | wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL; | |
280 | wxCursor *wxHtmlWindow::ms_cursorLink = NULL; | |
281 | wxCursor *wxHtmlWindow::ms_cursorText = NULL; | |
282 | ||
283 | void wxHtmlWindow::CleanUpStatics() | |
284 | { | |
285 | wxDELETE(m_DefaultFilter); | |
286 | WX_CLEAR_LIST(wxList, m_Filters); | |
287 | if (m_GlobalProcessors) | |
288 | WX_CLEAR_LIST(wxHtmlProcessorList, *m_GlobalProcessors); | |
289 | wxDELETE(m_GlobalProcessors); | |
290 | wxDELETE(ms_cursorLink); | |
291 | wxDELETE(ms_cursorText); | |
292 | } | |
5526e819 | 293 | |
4f417130 | 294 | void wxHtmlWindow::Init() |
5526e819 | 295 | { |
89de9af3 | 296 | m_tmpCanDrawLocks = 0; |
5526e819 | 297 | m_FS = new wxFileSystem(); |
67a99992 | 298 | #if wxUSE_STATUSBAR |
37146d33 VS |
299 | m_RelatedStatusBar = NULL; |
300 | m_RelatedStatusBarIndex = -1; | |
67a99992 | 301 | #endif // wxUSE_STATUSBAR |
5526e819 | 302 | m_RelatedFrame = NULL; |
892aeafc | 303 | m_TitleFormat = wxT("%s"); |
d5db80c2 | 304 | m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString; |
5526e819 VS |
305 | m_Cell = NULL; |
306 | m_Parser = new wxHtmlWinParser(this); | |
4f9297b0 | 307 | m_Parser->SetFS(m_FS); |
5526e819 | 308 | m_HistoryPos = -1; |
d1da8872 | 309 | m_HistoryOn = true; |
892aeafc VS |
310 | m_History = new wxHtmlHistoryArray; |
311 | m_Processors = NULL; | |
4f417130 | 312 | SetBorders(10); |
adf2eb2d VS |
313 | m_selection = NULL; |
314 | m_makingSelection = false; | |
1338c59a VS |
315 | #if wxUSE_CLIPBOARD |
316 | m_timerAutoScroll = NULL; | |
6ce51985 | 317 | m_lastDoubleClick = 0; |
d659d703 | 318 | #endif // wxUSE_CLIPBOARD |
b3c03420 | 319 | m_tmpSelFromCell = NULL; |
4f417130 VS |
320 | } |
321 | ||
790dbce3 | 322 | bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id, |
4f417130 | 323 | const wxPoint& pos, const wxSize& size, |
790dbce3 | 324 | long style, const wxString& name) |
4f417130 | 325 | { |
790dbce3 | 326 | if (!wxScrolledWindow::Create(parent, id, pos, size, |
2a536376 VS |
327 | style | wxVSCROLL | wxHSCROLL, |
328 | name)) | |
d1da8872 | 329 | return false; |
4f417130 | 330 | |
03a187cc VZ |
331 | // We can't erase our background in EVT_ERASE_BACKGROUND handler and use |
332 | // double buffering in EVT_PAINT handler as this requires blitting back | |
333 | // something already drawn on the window to the backing store bitmap when | |
334 | // handling EVT_PAINT but blitting in this direction is simply not | |
335 | // supported by OS X. | |
336 | // | |
337 | // So instead we use a hack with artificial EVT_ERASE_BACKGROUND generation | |
338 | // from OnPaint() and this means that we never need the "real" erase event | |
339 | // at all so disable it to avoid executing any user-defined handlers twice | |
340 | // (and to avoid processing unnecessary event if no handlers are defined). | |
341 | SetBackgroundStyle(wxBG_STYLE_PAINT); | |
4f9297b0 | 342 | SetPage(wxT("<html><body></body></html>")); |
8e494db9 JS |
343 | |
344 | SetInitialSize(size); | |
d1da8872 | 345 | return true; |
5526e819 VS |
346 | } |
347 | ||
348 | ||
5526e819 VS |
349 | wxHtmlWindow::~wxHtmlWindow() |
350 | { | |
1338c59a VS |
351 | #if wxUSE_CLIPBOARD |
352 | StopAutoScrolling(); | |
d659d703 | 353 | #endif // wxUSE_CLIPBOARD |
5526e819 VS |
354 | HistoryClear(); |
355 | ||
689c4829 VZ |
356 | delete m_selection; |
357 | ||
358 | delete m_Cell; | |
5526e819 | 359 | |
d80096a2 VZ |
360 | if ( m_Processors ) |
361 | { | |
362 | WX_CLEAR_LIST(wxHtmlProcessorList, *m_Processors); | |
363 | } | |
222ed1d6 | 364 | |
5526e819 VS |
365 | delete m_Parser; |
366 | delete m_FS; | |
892aeafc VS |
367 | delete m_History; |
368 | delete m_Processors; | |
5526e819 VS |
369 | } |
370 | ||
371 | ||
372 | ||
373 | void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format) | |
374 | { | |
375 | m_RelatedFrame = frame; | |
376 | m_TitleFormat = format; | |
377 | } | |
378 | ||
379 | ||
380 | ||
67a99992 | 381 | #if wxUSE_STATUSBAR |
37146d33 | 382 | void wxHtmlWindow::SetRelatedStatusBar(int index) |
5526e819 | 383 | { |
37146d33 | 384 | m_RelatedStatusBarIndex = index; |
5526e819 | 385 | } |
37146d33 VS |
386 | |
387 | void wxHtmlWindow::SetRelatedStatusBar(wxStatusBar* statusbar, int index) | |
388 | { | |
389 | m_RelatedStatusBar = statusbar; | |
390 | m_RelatedStatusBarIndex = index; | |
391 | } | |
392 | ||
67a99992 | 393 | #endif // wxUSE_STATUSBAR |
269e8200 RD |
394 | |
395 | ||
396 | ||
fbfb8bcc | 397 | void wxHtmlWindow::SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes) |
5526e819 | 398 | { |
4f9297b0 | 399 | m_Parser->SetFonts(normal_face, fixed_face, sizes); |
73de5077 VS |
400 | |
401 | // re-layout the page after changing fonts: | |
402 | DoSetPage(*(m_Parser->GetSource())); | |
5526e819 VS |
403 | } |
404 | ||
10e5c7ea VS |
405 | void wxHtmlWindow::SetStandardFonts(int size, |
406 | const wxString& normal_face, | |
407 | const wxString& fixed_face) | |
7acd3625 | 408 | { |
10e5c7ea | 409 | m_Parser->SetStandardFonts(size, normal_face, fixed_face); |
5526e819 | 410 | |
73de5077 VS |
411 | // re-layout the page after changing fonts: |
412 | DoSetPage(*(m_Parser->GetSource())); | |
413 | } | |
5526e819 VS |
414 | |
415 | bool wxHtmlWindow::SetPage(const wxString& source) | |
73de5077 VS |
416 | { |
417 | m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString; | |
418 | return DoSetPage(source); | |
419 | } | |
420 | ||
421 | bool wxHtmlWindow::DoSetPage(const wxString& source) | |
5526e819 | 422 | { |
892aeafc VS |
423 | wxString newsrc(source); |
424 | ||
adf2eb2d VS |
425 | wxDELETE(m_selection); |
426 | ||
b3c03420 VS |
427 | // we will soon delete all the cells, so clear pointers to them: |
428 | m_tmpSelFromCell = NULL; | |
429 | ||
892aeafc | 430 | // pass HTML through registered processors: |
960ba969 | 431 | if (m_Processors || m_GlobalProcessors) |
892aeafc | 432 | { |
222ed1d6 | 433 | wxHtmlProcessorList::compatibility_iterator nodeL, nodeG; |
960ba969 | 434 | int prL, prG; |
892aeafc | 435 | |
0cc70962 VZ |
436 | if ( m_Processors ) |
437 | nodeL = m_Processors->GetFirst(); | |
438 | if ( m_GlobalProcessors ) | |
439 | nodeG = m_GlobalProcessors->GetFirst(); | |
960ba969 VS |
440 | |
441 | // VS: there are two lists, global and local, both of them sorted by | |
e421922f | 442 | // priority. Since we have to go through _both_ lists with |
960ba969 VS |
443 | // decreasing priority, we "merge-sort" the lists on-line by |
444 | // processing that one of the two heads that has higher priority | |
445 | // in every iteration | |
446 | while (nodeL || nodeG) | |
892aeafc VS |
447 | { |
448 | prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1; | |
960ba969 VS |
449 | prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1; |
450 | if (prL > prG) | |
892aeafc | 451 | { |
73348d09 VS |
452 | if (nodeL->GetData()->IsEnabled()) |
453 | newsrc = nodeL->GetData()->Process(newsrc); | |
892aeafc VS |
454 | nodeL = nodeL->GetNext(); |
455 | } | |
960ba969 | 456 | else // prL <= prG |
892aeafc | 457 | { |
73348d09 VS |
458 | if (nodeG->GetData()->IsEnabled()) |
459 | newsrc = nodeG->GetData()->Process(newsrc); | |
960ba969 | 460 | nodeG = nodeG->GetNext(); |
892aeafc VS |
461 | } |
462 | } | |
463 | } | |
5526e819 | 464 | |
892aeafc VS |
465 | // ...and run the parser on it: |
466 | wxClientDC *dc = new wxClientDC(this); | |
4f9297b0 | 467 | dc->SetMapMode(wxMM_TEXT); |
5526e819 | 468 | SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF)); |
97e490f8 | 469 | SetBackgroundImage(wxNullBitmap); |
73de5077 | 470 | |
4f9297b0 | 471 | m_Parser->SetDC(dc); |
5276b0a5 VZ |
472 | |
473 | // notice that it's important to set m_Cell to NULL here before calling | |
474 | // Parse() below, even if it will be overwritten by its return value as | |
475 | // without this we may crash if it's used from inside Parse(), so use | |
476 | // wxDELETE() and not just delete here | |
477 | wxDELETE(m_Cell); | |
478 | ||
892aeafc | 479 | m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc); |
5526e819 | 480 | delete dc; |
4f9297b0 VS |
481 | m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); |
482 | m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER); | |
5526e819 | 483 | CreateLayout(); |
bfb9ee96 | 484 | if (m_tmpCanDrawLocks == 0) |
892aeafc | 485 | Refresh(); |
d1da8872 | 486 | return true; |
5526e819 VS |
487 | } |
488 | ||
39029898 VS |
489 | bool wxHtmlWindow::AppendToPage(const wxString& source) |
490 | { | |
73de5077 | 491 | return DoSetPage(*(GetParser()->GetSource()) + source); |
39029898 | 492 | } |
5526e819 VS |
493 | |
494 | bool wxHtmlWindow::LoadPage(const wxString& location) | |
495 | { | |
179a30e2 VZ |
496 | wxCHECK_MSG( !location.empty(), false, "location must be non-empty" ); |
497 | ||
1ccabb81 | 498 | wxBusyCursor busyCursor; |
790dbce3 | 499 | |
5526e819 | 500 | bool rt_val; |
d1da8872 | 501 | bool needs_refresh = false; |
33ac7e6f | 502 | |
89de9af3 | 503 | m_tmpCanDrawLocks++; |
e421922f | 504 | if (m_HistoryOn && (m_HistoryPos != -1)) |
4f9297b0 | 505 | { |
960ba969 | 506 | // store scroll position into history item: |
5526e819 | 507 | int x, y; |
e421922f | 508 | GetViewStart(&x, &y); |
892aeafc | 509 | (*m_History)[m_HistoryPos].SetPos(y); |
5526e819 VS |
510 | } |
511 | ||
e4e487e2 VZ |
512 | // first check if we're moving to an anchor in the same page |
513 | size_t posLocalAnchor = location.Find('#'); | |
514 | if ( posLocalAnchor != wxString::npos && posLocalAnchor != 0 ) | |
4f9297b0 | 515 | { |
e4e487e2 VZ |
516 | // check if the part before the anchor is the same as the (either |
517 | // relative or absolute) URI of the current page | |
518 | const wxString beforeAnchor = location.substr(0, posLocalAnchor); | |
519 | if ( beforeAnchor != m_OpenedPage && | |
520 | m_FS->GetPath() + beforeAnchor != m_OpenedPage ) | |
521 | { | |
522 | // indicate that we're not moving to a local anchor | |
523 | posLocalAnchor = wxString::npos; | |
524 | } | |
fc7dfaf8 | 525 | } |
e4e487e2 VZ |
526 | |
527 | if ( posLocalAnchor != wxString::npos ) | |
e421922f | 528 | { |
fc7dfaf8 | 529 | m_tmpCanDrawLocks--; |
e4e487e2 | 530 | rt_val = ScrollToAnchor(location.substr(posLocalAnchor + 1)); |
fc7dfaf8 | 531 | m_tmpCanDrawLocks++; |
5526e819 | 532 | } |
e4e487e2 | 533 | else // moving to another page |
4f9297b0 | 534 | { |
67a99992 WS |
535 | needs_refresh = true; |
536 | #if wxUSE_STATUSBAR | |
5526e819 | 537 | // load&display it: |
37146d33 | 538 | if (m_RelatedStatusBarIndex != -1) |
e421922f | 539 | { |
37146d33 | 540 | SetHTMLStatusText(_("Connecting...")); |
67a99992 | 541 | Refresh(false); |
5526e819 | 542 | } |
67a99992 | 543 | #endif // wxUSE_STATUSBAR |
790dbce3 | 544 | |
e4e487e2 | 545 | wxFSFile *f = m_Parser->OpenURL(wxHTML_URL_PAGE, location); |
33ac7e6f | 546 | |
7cb9cf89 VS |
547 | // try to interpret 'location' as filename instead of URL: |
548 | if (f == NULL) | |
549 | { | |
550 | wxFileName fn(location); | |
551 | wxString location2 = wxFileSystem::FileNameToURL(fn); | |
552 | f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2); | |
553 | } | |
554 | ||
33ac7e6f | 555 | if (f == NULL) |
e421922f | 556 | { |
f6bcfd97 | 557 | wxLogError(_("Unable to open requested HTML document: %s"), location.c_str()); |
89de9af3 | 558 | m_tmpCanDrawLocks--; |
f1edd54c | 559 | SetHTMLStatusText(wxEmptyString); |
67a99992 | 560 | return false; |
5526e819 VS |
561 | } |
562 | ||
33ac7e6f | 563 | else |
e421922f | 564 | { |
222ed1d6 | 565 | wxList::compatibility_iterator node; |
5526e819 VS |
566 | wxString src = wxEmptyString; |
567 | ||
67a99992 | 568 | #if wxUSE_STATUSBAR |
37146d33 | 569 | if (m_RelatedStatusBarIndex != -1) |
e421922f | 570 | { |
5526e819 | 571 | wxString msg = _("Loading : ") + location; |
37146d33 | 572 | SetHTMLStatusText(msg); |
67a99992 | 573 | Refresh(false); |
5526e819 | 574 | } |
67a99992 | 575 | #endif // wxUSE_STATUSBAR |
5526e819 VS |
576 | |
577 | node = m_Filters.GetFirst(); | |
4f9297b0 | 578 | while (node) |
e421922f | 579 | { |
4f9297b0 VS |
580 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); |
581 | if (h->CanRead(*f)) | |
e421922f | 582 | { |
4f9297b0 | 583 | src = h->ReadFile(*f); |
5526e819 VS |
584 | break; |
585 | } | |
4f9297b0 | 586 | node = node->GetNext(); |
5526e819 | 587 | } |
33ac7e6f | 588 | if (src == wxEmptyString) |
e421922f | 589 | { |
89de9af3 | 590 | if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter(); |
4f9297b0 | 591 | src = m_DefaultFilter->ReadFile(*f); |
89de9af3 | 592 | } |
5526e819 | 593 | |
4f9297b0 | 594 | m_FS->ChangePathTo(f->GetLocation()); |
5526e819 | 595 | rt_val = SetPage(src); |
4f9297b0 | 596 | m_OpenedPage = f->GetLocation(); |
33ac7e6f | 597 | if (f->GetAnchor() != wxEmptyString) |
e421922f | 598 | { |
4f9297b0 | 599 | ScrollToAnchor(f->GetAnchor()); |
5526e819 VS |
600 | } |
601 | ||
602 | delete f; | |
603 | ||
67a99992 | 604 | #if wxUSE_STATUSBAR |
37146d33 VS |
605 | if (m_RelatedStatusBarIndex != -1) |
606 | { | |
607 | SetHTMLStatusText(_("Done")); | |
608 | } | |
67a99992 | 609 | #endif // wxUSE_STATUSBAR |
5526e819 VS |
610 | } |
611 | } | |
612 | ||
4f9297b0 VS |
613 | if (m_HistoryOn) // add this page to history there: |
614 | { | |
892aeafc | 615 | int c = m_History->GetCount() - (m_HistoryPos + 1); |
5526e819 | 616 | |
0cb9cfb2 | 617 | if (m_HistoryPos < 0 || |
ee19c324 VS |
618 | (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage || |
619 | (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor) | |
620 | { | |
621 | m_HistoryPos++; | |
622 | for (int i = 0; i < c; i++) | |
b54e41c5 | 623 | m_History->RemoveAt(m_HistoryPos); |
ee19c324 VS |
624 | m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor)); |
625 | } | |
5526e819 VS |
626 | } |
627 | ||
096824d7 VS |
628 | if (m_OpenedPageTitle == wxEmptyString) |
629 | OnSetTitle(wxFileNameFromPath(m_OpenedPage)); | |
fc7dfaf8 | 630 | |
33ac7e6f | 631 | if (needs_refresh) |
4f9297b0 | 632 | { |
fc7dfaf8 VS |
633 | m_tmpCanDrawLocks--; |
634 | Refresh(); | |
635 | } | |
636 | else | |
637 | m_tmpCanDrawLocks--; | |
638 | ||
5526e819 VS |
639 | return rt_val; |
640 | } | |
641 | ||
642 | ||
7cb9cf89 VS |
643 | bool wxHtmlWindow::LoadFile(const wxFileName& filename) |
644 | { | |
645 | wxString url = wxFileSystem::FileNameToURL(filename); | |
646 | return LoadPage(url); | |
647 | } | |
648 | ||
5526e819 VS |
649 | |
650 | bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor) | |
651 | { | |
4f9297b0 | 652 | const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor); |
f3c82859 VS |
653 | if (!c) |
654 | { | |
f6bcfd97 | 655 | wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str()); |
d1da8872 | 656 | return false; |
f3c82859 | 657 | } |
33ac7e6f | 658 | else |
4f9297b0 | 659 | { |
8e6c2840 VS |
660 | // Go to next visible cell in current container, if it exists. This |
661 | // yields a bit better (even though still imperfect) results in that | |
662 | // there's better chance of using a suitable cell for upper Y | |
663 | // coordinate value. See bug #11406 for additional discussion. | |
664 | const wxHtmlCell *c_save = c; | |
665 | while ( c && c->IsFormattingCell() ) | |
666 | c = c->GetNext(); | |
667 | if ( !c ) | |
668 | c = c_save; | |
669 | ||
5526e819 | 670 | int y; |
269e8200 | 671 | |
4f9297b0 | 672 | for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY(); |
efba2b89 | 673 | Scroll(-1, y / wxHTML_SCROLL_STEP); |
5526e819 | 674 | m_OpenedAnchor = anchor; |
d1da8872 | 675 | return true; |
5526e819 VS |
676 | } |
677 | } | |
678 | ||
679 | ||
d5db80c2 | 680 | void wxHtmlWindow::OnSetTitle(const wxString& title) |
5526e819 | 681 | { |
33ac7e6f | 682 | if (m_RelatedFrame) |
4f9297b0 | 683 | { |
5526e819 VS |
684 | wxString tit; |
685 | tit.Printf(m_TitleFormat, title.c_str()); | |
4f9297b0 | 686 | m_RelatedFrame->SetTitle(tit); |
5526e819 | 687 | } |
d5db80c2 | 688 | m_OpenedPageTitle = title; |
5526e819 VS |
689 | } |
690 | ||
691 | ||
a7b2c092 VS |
692 | // return scroll steps such that a) scrollbars aren't shown needlessly |
693 | // and b) entire content is viewable (i.e. round up) | |
694 | static int ScrollSteps(int size, int available) | |
695 | { | |
696 | if ( size <= available ) | |
697 | return 0; | |
698 | else | |
699 | return (size + wxHTML_SCROLL_STEP - 1) / wxHTML_SCROLL_STEP; | |
700 | } | |
5526e819 VS |
701 | |
702 | ||
703 | void wxHtmlWindow::CreateLayout() | |
704 | { | |
a7b2c092 VS |
705 | // SetScrollbars() results in size change events -- and thus a nested |
706 | // CreateLayout() call -- on some platforms. Ignore nested calls, toplevel | |
707 | // CreateLayout() will do the right thing eventually. | |
708 | static wxRecursionGuardFlag s_flagReentrancy; | |
709 | wxRecursionGuard guard(s_flagReentrancy); | |
710 | if ( guard.IsInside() ) | |
711 | return; | |
712 | ||
713 | if (!m_Cell) | |
714 | return; | |
5526e819 | 715 | |
a7b2c092 VS |
716 | int clientWidth, clientHeight; |
717 | GetClientSize(&clientWidth, &clientHeight); | |
718 | ||
719 | const int vscrollbar = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
720 | const int hscrollbar = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y); | |
721 | ||
722 | if ( HasScrollbar(wxHORIZONTAL) ) | |
723 | clientHeight += hscrollbar; | |
724 | ||
725 | if ( HasScrollbar(wxVERTICAL) ) | |
726 | clientWidth += vscrollbar; | |
a547ebff | 727 | |
cc87fbed | 728 | if ( HasFlag(wxHW_SCROLLBAR_NEVER) ) |
4f9297b0 | 729 | { |
689f42f4 | 730 | SetScrollbars(1, 1, 0, 0); // always off |
a7b2c092 | 731 | m_Cell->Layout(clientWidth); |
a547ebff | 732 | } |
cc87fbed VZ |
733 | else // !wxHW_SCROLLBAR_NEVER |
734 | { | |
a7b2c092 VS |
735 | // Lay the content out with the assumption that it's too large to fit |
736 | // in the window (this is likely to be the case): | |
737 | m_Cell->Layout(clientWidth - vscrollbar); | |
738 | ||
739 | // If the layout is wider than the window, horizontal scrollbar will | |
740 | // certainly be shown. Account for it here for subsequent computations. | |
741 | if ( m_Cell->GetWidth() > clientWidth ) | |
742 | clientHeight -= hscrollbar; | |
743 | ||
744 | if ( m_Cell->GetHeight() <= clientHeight ) | |
e421922f | 745 | { |
a7b2c092 VS |
746 | // we fit into the window, hide vertical scrollbar: |
747 | SetScrollbars | |
748 | ( | |
749 | wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP, | |
750 | ScrollSteps(m_Cell->GetWidth(), clientWidth - vscrollbar), | |
751 | 0 | |
752 | ); | |
753 | // ...and redo the layout to use the extra space | |
754 | m_Cell->Layout(clientWidth); | |
a547ebff | 755 | } |
a7b2c092 | 756 | else |
e421922f | 757 | { |
a7b2c092 VS |
758 | // If the content doesn't fit into the window by only a small |
759 | // margin, chances are that it may fit fully with scrollbar turned | |
760 | // off. It's something worth trying but on the other hand, we don't | |
761 | // want to waste too much time redoing the layout (twice!) for | |
762 | // long -- and thus expensive to layout -- pages. The cut-off value | |
763 | // is an arbitrary heuristics. | |
764 | static const int SMALL_OVERLAP = 60; | |
765 | if ( m_Cell->GetHeight() <= clientHeight + SMALL_OVERLAP ) | |
766 | { | |
767 | m_Cell->Layout(clientWidth); | |
768 | ||
769 | if ( m_Cell->GetHeight() <= clientHeight ) | |
770 | { | |
771 | // Great, we fit in. Hide the scrollbar. | |
772 | SetScrollbars | |
773 | ( | |
774 | wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP, | |
775 | ScrollSteps(m_Cell->GetWidth(), clientWidth), | |
776 | 0 | |
777 | ); | |
778 | return; | |
779 | } | |
780 | else | |
781 | { | |
782 | // That didn't work out, go back to previous layout. Note | |
783 | // that redoing the layout once again here isn't as bad as | |
784 | // it looks -- thanks to the small cut-off value, it's a | |
785 | // reasonably small page. | |
786 | m_Cell->Layout(clientWidth - vscrollbar); | |
787 | } | |
788 | } | |
789 | // else: the page is very long, it will certainly need scrollbar | |
790 | ||
791 | SetScrollbars | |
792 | ( | |
793 | wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP, | |
794 | ScrollSteps(m_Cell->GetWidth(), clientWidth - vscrollbar), | |
795 | ScrollSteps(m_Cell->GetHeight(), clientHeight) | |
796 | ); | |
89de9af3 | 797 | } |
a547ebff | 798 | } |
5526e819 VS |
799 | } |
800 | ||
b4246849 | 801 | #if wxUSE_CONFIG |
5526e819 VS |
802 | void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path) |
803 | { | |
804 | wxString oldpath; | |
805 | wxString tmp; | |
d5db80c2 VS |
806 | int p_fontsizes[7]; |
807 | wxString p_fff, p_ffn; | |
5526e819 | 808 | |
33ac7e6f | 809 | if (path != wxEmptyString) |
4f9297b0 VS |
810 | { |
811 | oldpath = cfg->GetPath(); | |
812 | cfg->SetPath(path); | |
5526e819 VS |
813 | } |
814 | ||
892aeafc VS |
815 | m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders); |
816 | p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
817 | p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
bfb9ee96 | 818 | for (int i = 0; i < 7; i++) |
4f9297b0 | 819 | { |
66a77a74 | 820 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); |
4f9297b0 | 821 | p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]); |
5526e819 | 822 | } |
8eb2940f | 823 | SetFonts(p_ffn, p_fff, p_fontsizes); |
5526e819 VS |
824 | |
825 | if (path != wxEmptyString) | |
4f9297b0 | 826 | cfg->SetPath(oldpath); |
5526e819 VS |
827 | } |
828 | ||
829 | ||
830 | ||
831 | void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path) | |
832 | { | |
833 | wxString oldpath; | |
834 | wxString tmp; | |
835 | ||
33ac7e6f | 836 | if (path != wxEmptyString) |
4f9297b0 VS |
837 | { |
838 | oldpath = cfg->GetPath(); | |
839 | cfg->SetPath(path); | |
5526e819 VS |
840 | } |
841 | ||
892aeafc VS |
842 | cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders); |
843 | cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
844 | cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
bfb9ee96 | 845 | for (int i = 0; i < 7; i++) |
4f9297b0 | 846 | { |
66a77a74 | 847 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); |
4f9297b0 | 848 | cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]); |
5526e819 VS |
849 | } |
850 | ||
851 | if (path != wxEmptyString) | |
4f9297b0 | 852 | cfg->SetPath(oldpath); |
5526e819 | 853 | } |
b4246849 | 854 | #endif // wxUSE_CONFIG |
5526e819 VS |
855 | |
856 | bool wxHtmlWindow::HistoryBack() | |
857 | { | |
858 | wxString a, l; | |
859 | ||
d1da8872 | 860 | if (m_HistoryPos < 1) return false; |
5526e819 | 861 | |
bbda1088 VS |
862 | // store scroll position into history item: |
863 | int x, y; | |
e421922f | 864 | GetViewStart(&x, &y); |
892aeafc | 865 | (*m_History)[m_HistoryPos].SetPos(y); |
bbda1088 VS |
866 | |
867 | // go to previous position: | |
5526e819 VS |
868 | m_HistoryPos--; |
869 | ||
892aeafc VS |
870 | l = (*m_History)[m_HistoryPos].GetPage(); |
871 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
d1da8872 | 872 | m_HistoryOn = false; |
89de9af3 | 873 | m_tmpCanDrawLocks++; |
5526e819 | 874 | if (a == wxEmptyString) LoadPage(l); |
fc7dfaf8 | 875 | else LoadPage(l + wxT("#") + a); |
d1da8872 | 876 | m_HistoryOn = true; |
89de9af3 | 877 | m_tmpCanDrawLocks--; |
892aeafc | 878 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); |
5526e819 | 879 | Refresh(); |
d1da8872 | 880 | return true; |
5526e819 VS |
881 | } |
882 | ||
1b113a81 VS |
883 | bool wxHtmlWindow::HistoryCanBack() |
884 | { | |
d1da8872 WS |
885 | if (m_HistoryPos < 1) return false; |
886 | return true ; | |
1b113a81 | 887 | } |
5526e819 VS |
888 | |
889 | ||
890 | bool wxHtmlWindow::HistoryForward() | |
891 | { | |
892 | wxString a, l; | |
893 | ||
d1da8872 WS |
894 | if (m_HistoryPos == -1) return false; |
895 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false; | |
5526e819 VS |
896 | |
897 | m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage() | |
898 | ||
899 | m_HistoryPos++; | |
892aeafc VS |
900 | l = (*m_History)[m_HistoryPos].GetPage(); |
901 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
d1da8872 | 902 | m_HistoryOn = false; |
89de9af3 | 903 | m_tmpCanDrawLocks++; |
5526e819 | 904 | if (a == wxEmptyString) LoadPage(l); |
fc7dfaf8 | 905 | else LoadPage(l + wxT("#") + a); |
d1da8872 | 906 | m_HistoryOn = true; |
89de9af3 | 907 | m_tmpCanDrawLocks--; |
892aeafc | 908 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); |
5526e819 | 909 | Refresh(); |
d1da8872 | 910 | return true; |
5526e819 VS |
911 | } |
912 | ||
1b113a81 VS |
913 | bool wxHtmlWindow::HistoryCanForward() |
914 | { | |
d1da8872 WS |
915 | if (m_HistoryPos == -1) return false; |
916 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return false; | |
917 | return true ; | |
1b113a81 | 918 | } |
5526e819 VS |
919 | |
920 | ||
921 | void wxHtmlWindow::HistoryClear() | |
922 | { | |
892aeafc | 923 | m_History->Empty(); |
5526e819 VS |
924 | m_HistoryPos = -1; |
925 | } | |
926 | ||
892aeafc VS |
927 | void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor) |
928 | { | |
929 | if (!m_Processors) | |
930 | { | |
931 | m_Processors = new wxHtmlProcessorList; | |
892aeafc | 932 | } |
222ed1d6 | 933 | wxHtmlProcessorList::compatibility_iterator node; |
bfb9ee96 | 934 | |
892aeafc VS |
935 | for (node = m_Processors->GetFirst(); node; node = node->GetNext()) |
936 | { | |
bfb9ee96 | 937 | if (processor->GetPriority() > node->GetData()->GetPriority()) |
892aeafc VS |
938 | { |
939 | m_Processors->Insert(node, processor); | |
960ba969 | 940 | return; |
892aeafc VS |
941 | } |
942 | } | |
960ba969 | 943 | m_Processors->Append(processor); |
892aeafc VS |
944 | } |
945 | ||
960ba969 | 946 | /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor) |
892aeafc | 947 | { |
960ba969 | 948 | if (!m_GlobalProcessors) |
892aeafc | 949 | { |
960ba969 | 950 | m_GlobalProcessors = new wxHtmlProcessorList; |
892aeafc | 951 | } |
222ed1d6 | 952 | wxHtmlProcessorList::compatibility_iterator node; |
e421922f | 953 | |
960ba969 | 954 | for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext()) |
892aeafc | 955 | { |
bfb9ee96 | 956 | if (processor->GetPriority() > node->GetData()->GetPriority()) |
892aeafc | 957 | { |
960ba969 VS |
958 | m_GlobalProcessors->Insert(node, processor); |
959 | return; | |
892aeafc VS |
960 | } |
961 | } | |
960ba969 | 962 | m_GlobalProcessors->Append(processor); |
892aeafc VS |
963 | } |
964 | ||
5526e819 VS |
965 | |
966 | ||
5526e819 VS |
967 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) |
968 | { | |
5526e819 VS |
969 | m_Filters.Append(filter); |
970 | } | |
971 | ||
972 | ||
36c4ff4d VS |
973 | bool wxHtmlWindow::IsSelectionEnabled() const |
974 | { | |
975 | #if wxUSE_CLIPBOARD | |
cc87fbed | 976 | return !HasFlag(wxHW_NO_SELECTION); |
36c4ff4d VS |
977 | #else |
978 | return false; | |
979 | #endif | |
980 | } | |
d659d703 | 981 | |
e3774124 | 982 | |
1338c59a | 983 | #if wxUSE_CLIPBOARD |
977b867e | 984 | wxString wxHtmlWindow::DoSelectionToText(wxHtmlSelection *sel) |
e3774124 | 985 | { |
977b867e | 986 | if ( !sel ) |
e3774124 VS |
987 | return wxEmptyString; |
988 | ||
f30e67db | 989 | wxClientDC dc(this); |
e3774124 | 990 | wxString text; |
7f8f381c VS |
991 | |
992 | wxHtmlTerminalCellsInterator i(sel->GetFromCell(), sel->GetToCell()); | |
993 | const wxHtmlCell *prev = NULL; | |
994 | ||
e3774124 VS |
995 | while ( i ) |
996 | { | |
7f8f381c VS |
997 | // When converting HTML content to plain text, the entire paragraph |
998 | // (container in wxHTML) goes on single line. A new paragraph (that | |
999 | // should go on its own line) has its own container. Therefore, the | |
1000 | // simplest way of detecting where to insert newlines in plain text | |
1001 | // is to check if the parent container changed -- if it did, we moved | |
1002 | // to a new paragraph. | |
1003 | if ( prev && prev->GetParent() != i->GetParent() ) | |
1004 | text << '\n'; | |
1005 | ||
1006 | // NB: we don't need to pass the selection to ConvertToText() in the | |
1007 | // middle of the selected text; it's only useful when only part of | |
1008 | // a cell is selected | |
1009 | text << i->ConvertToText(sel); | |
1010 | ||
e3774124 VS |
1011 | prev = *i; |
1012 | ++i; | |
1013 | } | |
1014 | return text; | |
1015 | } | |
1016 | ||
977b867e VS |
1017 | wxString wxHtmlWindow::ToText() |
1018 | { | |
1019 | if (m_Cell) | |
1020 | { | |
1021 | wxHtmlSelection sel; | |
1022 | sel.Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal()); | |
1023 | return DoSelectionToText(&sel); | |
1024 | } | |
1025 | else | |
1026 | return wxEmptyString; | |
1027 | } | |
1028 | ||
d659d703 VZ |
1029 | #endif // wxUSE_CLIPBOARD |
1030 | ||
8feaa81f | 1031 | bool wxHtmlWindow::CopySelection(ClipboardType t) |
e3774124 | 1032 | { |
d659d703 | 1033 | #if wxUSE_CLIPBOARD |
e3774124 VS |
1034 | if ( m_selection ) |
1035 | { | |
28b2ac5b | 1036 | #if defined(__UNIX__) && !defined(__WXMAC__) |
e3774124 | 1037 | wxTheClipboard->UsePrimarySelection(t == Primary); |
d659d703 VZ |
1038 | #else // !__UNIX__ |
1039 | // Primary selection exists only under X11, so don't do anything under | |
1040 | // the other platforms when we try to access it | |
1041 | // | |
1042 | // TODO: this should be abstracted at wxClipboard level! | |
1043 | if ( t == Primary ) | |
8feaa81f | 1044 | return false; |
d659d703 VZ |
1045 | #endif // __UNIX__/!__UNIX__ |
1046 | ||
e3774124 VS |
1047 | if ( wxTheClipboard->Open() ) |
1048 | { | |
d659d703 | 1049 | const wxString txt(SelectionToText()); |
e3774124 VS |
1050 | wxTheClipboard->SetData(new wxTextDataObject(txt)); |
1051 | wxTheClipboard->Close(); | |
9a83f860 | 1052 | wxLogTrace(wxT("wxhtmlselection"), |
e3774124 | 1053 | _("Copied to clipboard:\"%s\""), txt.c_str()); |
8feaa81f VZ |
1054 | |
1055 | return true; | |
e3774124 VS |
1056 | } |
1057 | } | |
caf448e3 WS |
1058 | #else |
1059 | wxUnusedVar(t); | |
d659d703 | 1060 | #endif // wxUSE_CLIPBOARD |
8feaa81f VZ |
1061 | |
1062 | return false; | |
e3774124 | 1063 | } |
5526e819 VS |
1064 | |
1065 | ||
0b2dadd3 | 1066 | void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link) |
5526e819 | 1067 | { |
a1c3cdc4 | 1068 | wxHtmlLinkEvent event(GetId(), link); |
33c2a4b7 | 1069 | event.SetEventObject(this); |
a1c3cdc4 VS |
1070 | if (!GetEventHandler()->ProcessEvent(event)) |
1071 | { | |
1072 | // the default behaviour is to load the URL in this window | |
1073 | const wxMouseEvent *e = event.GetLinkInfo().GetEvent(); | |
1074 | if (e == NULL || e->LeftUp()) | |
1075 | LoadPage(event.GetLinkInfo().GetHref()); | |
1076 | } | |
5526e819 VS |
1077 | } |
1078 | ||
03a187cc | 1079 | void wxHtmlWindow::DoEraseBackground(wxDC& dc) |
1338c59a | 1080 | { |
03a187cc VZ |
1081 | // if we don't have any background bitmap we just fill it with background |
1082 | // colour and we also must do it if the background bitmap is not fully | |
1083 | // opaque as otherwise junk could be left there | |
1084 | if ( !m_bmpBg.IsOk() || m_bmpBg.GetMask() ) | |
97e490f8 | 1085 | { |
03a187cc | 1086 | dc.SetBackground(GetBackgroundColour()); |
97e490f8 VZ |
1087 | dc.Clear(); |
1088 | } | |
1089 | ||
03a187cc | 1090 | if ( m_bmpBg.IsOk() ) |
97e490f8 | 1091 | { |
03a187cc | 1092 | // draw the background bitmap tiling it over the entire window area |
4340a145 | 1093 | const wxSize sz = GetVirtualSize(); |
03a187cc VZ |
1094 | const wxSize sizeBmp(m_bmpBg.GetWidth(), m_bmpBg.GetHeight()); |
1095 | for ( wxCoord x = 0; x < sz.x; x += sizeBmp.x ) | |
97e490f8 | 1096 | { |
03a187cc VZ |
1097 | for ( wxCoord y = 0; y < sz.y; y += sizeBmp.y ) |
1098 | { | |
1099 | dc.DrawBitmap(m_bmpBg, x, y, true /* use mask */); | |
1100 | } | |
97e490f8 VZ |
1101 | } |
1102 | } | |
1338c59a VS |
1103 | } |
1104 | ||
49b489e8 VZ |
1105 | void wxHtmlWindow::OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
1106 | { | |
1107 | // We never get real erase background events as we changed our background | |
1108 | // style to wxBG_STYLE_PAINT in our ctor so the only time when we get here | |
1109 | // is when an artificial wxEraseEvent is generated by our own OnPaint() | |
1110 | // below. This handler only exists to stop the event from propagating | |
1111 | // downwards to wxWindow which may erase the background itself when it gets | |
1112 | // it in some ports (currently this happens in wxUniv), so we simply stop | |
1113 | // processing here and set a special flag allowing OnPaint() to see that | |
1114 | // the event hadn't been really processed. | |
1115 | m_isBgReallyErased = false; | |
1116 | } | |
1117 | ||
1338c59a | 1118 | void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) |
5526e819 | 1119 | { |
03a187cc | 1120 | wxPaintDC dcPaint(this); |
1338c59a | 1121 | |
518ba663 VZ |
1122 | if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) |
1123 | return; | |
5526e819 | 1124 | |
b835e9bf | 1125 | int x, y; |
e421922f | 1126 | GetViewStart(&x, &y); |
03a187cc VZ |
1127 | const wxRect rect = GetUpdateRegion().GetBox(); |
1128 | const wxSize sz = GetClientSize(); | |
1129 | ||
1130 | // set up the DC we're drawing on: if the window is already double buffered | |
1131 | // we do it directly on wxPaintDC, otherwise we allocate a backing store | |
1132 | // buffer and compose the drawing there and then blit it to screen all at | |
1133 | // once | |
1134 | wxDC *dc; | |
1338c59a | 1135 | wxMemoryDC dcm; |
03a187cc | 1136 | if ( IsDoubleBuffered() ) |
518ba663 | 1137 | { |
03a187cc VZ |
1138 | dc = &dcPaint; |
1139 | } | |
1140 | else // window is not double buffered by the system, do it ourselves | |
1141 | { | |
1142 | if ( !m_backBuffer.IsOk() ) | |
1143 | m_backBuffer.Create(sz.x, sz.y); | |
1144 | dcm.SelectObject(m_backBuffer); | |
1145 | dc = &dcm; | |
518ba663 | 1146 | } |
03a187cc VZ |
1147 | |
1148 | PrepareDC(*dc); | |
1149 | ||
49b489e8 VZ |
1150 | // Erase the background: for compatibility, we must generate the event to |
1151 | // allow the user-defined handlers to do it, hence this hack with sending | |
1152 | // an artificial wxEraseEvent to trigger the execution of such handlers. | |
03a187cc VZ |
1153 | wxEraseEvent eraseEvent(GetId(), dc); |
1154 | eraseEvent.SetEventObject(this); | |
49b489e8 VZ |
1155 | |
1156 | // Hack inside a hack: the background wasn't really erased if our own | |
1157 | // OnEraseBackground() was executed, so we need to check for the flag set | |
1158 | // by it whenever it's called. | |
1159 | m_isBgReallyErased = true; // Initially assume it wasn't. | |
1160 | if ( !ProcessWindowEvent(eraseEvent) || !m_isBgReallyErased ) | |
518ba663 | 1161 | { |
03a187cc VZ |
1162 | // erase background ourselves |
1163 | DoEraseBackground(*dc); | |
518ba663 | 1164 | } |
03a187cc | 1165 | //else: background erased by the user-defined handler |
a03ae172 | 1166 | |
03a187cc VZ |
1167 | |
1168 | // draw the HTML window contents | |
1169 | dc->SetMapMode(wxMM_TEXT); | |
3ee9771b | 1170 | dc->SetBackgroundMode(wxTRANSPARENT); |
88ed20a2 | 1171 | dc->SetLayoutDirection(GetLayoutDirection()); |
d659d703 | 1172 | |
f30e67db VS |
1173 | wxHtmlRenderingInfo rinfo; |
1174 | wxDefaultHtmlRenderingStyle rstyle; | |
1175 | rinfo.SetSelection(m_selection); | |
1176 | rinfo.SetStyle(&rstyle); | |
03a187cc | 1177 | m_Cell->Draw(*dc, 0, 0, |
790dbce3 | 1178 | y * wxHTML_SCROLL_STEP + rect.GetTop(), |
36c4ff4d | 1179 | y * wxHTML_SCROLL_STEP + rect.GetBottom(), |
f30e67db | 1180 | rinfo); |
d1da8872 | 1181 | |
03693319 VS |
1182 | #ifdef DEBUG_HTML_SELECTION |
1183 | { | |
1184 | int xc, yc, x, y; | |
1185 | wxGetMousePosition(&xc, &yc); | |
1186 | ScreenToClient(&xc, &yc); | |
1187 | CalcUnscrolledPosition(xc, yc, &x, &y); | |
1188 | wxHtmlCell *at = m_Cell->FindCellByPos(x, y); | |
d1da8872 | 1189 | wxHtmlCell *before = |
03693319 | 1190 | m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_BEFORE); |
d1da8872 | 1191 | wxHtmlCell *after = |
03693319 | 1192 | m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_AFTER); |
d1da8872 | 1193 | |
03a187cc VZ |
1194 | dc->SetBrush(*wxTRANSPARENT_BRUSH); |
1195 | dc->SetPen(*wxBLACK_PEN); | |
03693319 | 1196 | if (at) |
03a187cc | 1197 | dc->DrawRectangle(at->GetAbsPos(), |
03693319 | 1198 | wxSize(at->GetWidth(),at->GetHeight())); |
03a187cc | 1199 | dc->SetPen(*wxGREEN_PEN); |
03693319 | 1200 | if (before) |
03a187cc | 1201 | dc->DrawRectangle(before->GetAbsPos().x+1, before->GetAbsPos().y+1, |
03693319 | 1202 | before->GetWidth()-2,before->GetHeight()-2); |
03a187cc | 1203 | dc->SetPen(*wxRED_PEN); |
03693319 | 1204 | if (after) |
03a187cc | 1205 | dc->DrawRectangle(after->GetAbsPos().x+2, after->GetAbsPos().y+2, |
03693319 VS |
1206 | after->GetWidth()-4,after->GetHeight()-4); |
1207 | } | |
03a187cc | 1208 | #endif // DEBUG_HTML_SELECTION |
d1da8872 | 1209 | |
03a187cc VZ |
1210 | if ( dc != &dcPaint ) |
1211 | { | |
1212 | dc->SetDeviceOrigin(0,0); | |
1213 | dcPaint.Blit(0, rect.GetTop(), | |
1214 | sz.x, rect.GetBottom() - rect.GetTop() + 1, | |
1215 | dc, | |
1216 | 0, rect.GetTop()); | |
1217 | } | |
5526e819 VS |
1218 | } |
1219 | ||
1220 | ||
1221 | ||
1222 | ||
1223 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
1224 | { | |
6528a7f1 VZ |
1225 | event.Skip(); |
1226 | ||
03a187cc | 1227 | m_backBuffer = wxNullBitmap; |
1338c59a | 1228 | |
5526e819 | 1229 | CreateLayout(); |
1338c59a VS |
1230 | |
1231 | // Recompute selection if necessary: | |
1232 | if ( m_selection ) | |
1233 | { | |
1234 | m_selection->Set(m_selection->GetFromCell(), | |
1235 | m_selection->GetToCell()); | |
2f0bebe6 | 1236 | m_selection->ClearFromToCharacterPos(); |
1338c59a | 1237 | } |
d659d703 | 1238 | |
f6bcfd97 | 1239 | Refresh(); |
5526e819 VS |
1240 | } |
1241 | ||
1242 | ||
fc7a2a60 | 1243 | void wxHtmlWindow::OnMouseMove(wxMouseEvent& WXUNUSED(event)) |
5526e819 | 1244 | { |
bc55e31b | 1245 | wxHtmlWindowMouseHelper::HandleMouseMoved(); |
31d8b4ad | 1246 | } |
5526e819 | 1247 | |
adf2eb2d | 1248 | void wxHtmlWindow::OnMouseDown(wxMouseEvent& event) |
31d8b4ad | 1249 | { |
0994d968 | 1250 | #if wxUSE_CLIPBOARD |
adf2eb2d | 1251 | if ( event.LeftDown() && IsSelectionEnabled() ) |
4f9297b0 | 1252 | { |
0994d968 VS |
1253 | const long TRIPLECLICK_LEN = 200; // 0.2 sec after doubleclick |
1254 | if ( wxGetLocalTimeMillis() - m_lastDoubleClick <= TRIPLECLICK_LEN ) | |
adf2eb2d | 1255 | { |
0994d968 | 1256 | SelectLine(CalcUnscrolledPosition(event.GetPosition())); |
d659d703 | 1257 | |
5de65c69 | 1258 | (void) CopySelection(); |
adf2eb2d | 1259 | } |
0994d968 | 1260 | else |
d659d703 | 1261 | { |
0994d968 | 1262 | m_makingSelection = true; |
d659d703 | 1263 | |
0994d968 VS |
1264 | if ( m_selection ) |
1265 | { | |
1266 | wxDELETE(m_selection); | |
1267 | Refresh(); | |
1268 | } | |
1269 | m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition()); | |
1270 | m_tmpSelFromCell = NULL; | |
5526e819 | 1271 | |
0994d968 VS |
1272 | CaptureMouse(); |
1273 | } | |
adf2eb2d | 1274 | } |
d659d703 | 1275 | #endif // wxUSE_CLIPBOARD |
bc0e68eb VZ |
1276 | |
1277 | // in any case, let the default handler set focus to this window | |
1278 | event.Skip(); | |
adf2eb2d | 1279 | } |
5526e819 | 1280 | |
adf2eb2d VS |
1281 | void wxHtmlWindow::OnMouseUp(wxMouseEvent& event) |
1282 | { | |
1338c59a | 1283 | #if wxUSE_CLIPBOARD |
adf2eb2d VS |
1284 | if ( m_makingSelection ) |
1285 | { | |
1286 | ReleaseMouse(); | |
1287 | m_makingSelection = false; | |
1288 | ||
907f2fab VS |
1289 | // if m_selection=NULL, the user didn't move the mouse far enough from |
1290 | // starting point and the mouse up event is part of a click, the user | |
1291 | // is not selecting text: | |
1292 | if ( m_selection ) | |
adf2eb2d | 1293 | { |
907f2fab VS |
1294 | CopySelection(Primary); |
1295 | ||
adf2eb2d VS |
1296 | // we don't want mouse up event that ended selecting to be |
1297 | // handled as mouse click and e.g. follow hyperlink: | |
1298 | return; | |
1299 | } | |
1300 | } | |
d659d703 VZ |
1301 | #endif // wxUSE_CLIPBOARD |
1302 | ||
bc55e31b | 1303 | wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); |
615f68c6 VZ |
1304 | if ( !wxHtmlWindowMouseHelper::HandleMouseClick(m_Cell, pos, event) ) |
1305 | event.Skip(); | |
5526e819 VS |
1306 | } |
1307 | ||
63e819f2 VS |
1308 | #if wxUSE_CLIPBOARD |
1309 | void wxHtmlWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) | |
1310 | { | |
1311 | if ( !m_makingSelection ) | |
1312 | return; | |
1313 | ||
1314 | // discard the selecting operation | |
1315 | m_makingSelection = false; | |
1316 | wxDELETE(m_selection); | |
1317 | m_tmpSelFromCell = NULL; | |
1318 | Refresh(); | |
1319 | } | |
1320 | #endif // wxUSE_CLIPBOARD | |
5526e819 VS |
1321 | |
1322 | ||
5180055b JS |
1323 | void wxHtmlWindow::OnInternalIdle() |
1324 | { | |
1325 | wxWindow::OnInternalIdle(); | |
d1da8872 | 1326 | |
bc55e31b | 1327 | if (m_Cell != NULL && DidMouseMove()) |
4f9297b0 | 1328 | { |
03693319 VS |
1329 | #ifdef DEBUG_HTML_SELECTION |
1330 | Refresh(); | |
1331 | #endif | |
adf2eb2d VS |
1332 | int xc, yc, x, y; |
1333 | wxGetMousePosition(&xc, &yc); | |
1334 | ScreenToClient(&xc, &yc); | |
1335 | CalcUnscrolledPosition(xc, yc, &x, &y); | |
0cb9cfb2 | 1336 | |
f6010d8f | 1337 | wxHtmlCell *cell = m_Cell->FindCellByPos(x, y); |
adf2eb2d VS |
1338 | |
1339 | // handle selection update: | |
1340 | if ( m_makingSelection ) | |
1341 | { | |
adf2eb2d | 1342 | if ( !m_tmpSelFromCell ) |
748418c0 VS |
1343 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1344 | m_tmpSelFromPos.x,m_tmpSelFromPos.y); | |
d1da8872 | 1345 | |
03693319 VS |
1346 | // NB: a trick - we adjust selFromPos to be upper left or bottom |
1347 | // right corner of the first cell of the selection depending | |
1348 | // on whether the mouse is moving to the right or to the left. | |
1349 | // This gives us more "natural" behaviour when selecting | |
1350 | // a line (specifically, first cell of the next line is not | |
1351 | // included if you drag selection from left to right over | |
1352 | // entire line): | |
1353 | wxPoint dirFromPos; | |
1354 | if ( !m_tmpSelFromCell ) | |
1355 | { | |
1356 | dirFromPos = m_tmpSelFromPos; | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | dirFromPos = m_tmpSelFromCell->GetAbsPos(); | |
1361 | if ( x < m_tmpSelFromPos.x ) | |
1362 | { | |
1363 | dirFromPos.x += m_tmpSelFromCell->GetWidth(); | |
1364 | dirFromPos.y += m_tmpSelFromCell->GetHeight(); | |
1365 | } | |
1366 | } | |
d1da8872 | 1367 | bool goingDown = dirFromPos.y < y || |
03693319 VS |
1368 | (dirFromPos.y == y && dirFromPos.x < x); |
1369 | ||
1370 | // determine selection span: | |
748418c0 | 1371 | if ( /*still*/ !m_tmpSelFromCell ) |
adf2eb2d VS |
1372 | { |
1373 | if (goingDown) | |
1374 | { | |
5a1597e9 VS |
1375 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1376 | m_tmpSelFromPos.x,m_tmpSelFromPos.y, | |
1377 | wxHTML_FIND_NEAREST_AFTER); | |
adf2eb2d VS |
1378 | if (!m_tmpSelFromCell) |
1379 | m_tmpSelFromCell = m_Cell->GetFirstTerminal(); | |
1380 | } | |
1381 | else | |
1382 | { | |
5a1597e9 VS |
1383 | m_tmpSelFromCell = m_Cell->FindCellByPos( |
1384 | m_tmpSelFromPos.x,m_tmpSelFromPos.y, | |
1385 | wxHTML_FIND_NEAREST_BEFORE); | |
adf2eb2d VS |
1386 | if (!m_tmpSelFromCell) |
1387 | m_tmpSelFromCell = m_Cell->GetLastTerminal(); | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | wxHtmlCell *selcell = cell; | |
1392 | if (!selcell) | |
1393 | { | |
1394 | if (goingDown) | |
1395 | { | |
1396 | selcell = m_Cell->FindCellByPos(x, y, | |
03693319 | 1397 | wxHTML_FIND_NEAREST_BEFORE); |
adf2eb2d VS |
1398 | if (!selcell) |
1399 | selcell = m_Cell->GetLastTerminal(); | |
1400 | } | |
1401 | else | |
1402 | { | |
1403 | selcell = m_Cell->FindCellByPos(x, y, | |
03693319 | 1404 | wxHTML_FIND_NEAREST_AFTER); |
adf2eb2d VS |
1405 | if (!selcell) |
1406 | selcell = m_Cell->GetFirstTerminal(); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | // NB: it may *rarely* happen that the code above didn't find one | |
1411 | // of the cells, e.g. if wxHtmlWindow doesn't contain any | |
d659d703 | 1412 | // visible cells. |
adf2eb2d | 1413 | if ( selcell && m_tmpSelFromCell ) |
d659d703 | 1414 | { |
adf2eb2d VS |
1415 | if ( !m_selection ) |
1416 | { | |
1417 | // start selecting only if mouse movement was big enough | |
1418 | // (otherwise it was meant as mouse click, not selection): | |
1419 | const int PRECISION = 2; | |
1420 | wxPoint diff = m_tmpSelFromPos - wxPoint(x,y); | |
1421 | if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION) | |
1422 | { | |
1423 | m_selection = new wxHtmlSelection(); | |
1424 | } | |
1425 | } | |
1426 | if ( m_selection ) | |
1427 | { | |
e3774124 | 1428 | if ( m_tmpSelFromCell->IsBefore(selcell) ) |
adf2eb2d VS |
1429 | { |
1430 | m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell, | |
a1c3cdc4 VS |
1431 | wxPoint(x,y), selcell); |
1432 | } | |
adf2eb2d VS |
1433 | else |
1434 | { | |
1435 | m_selection->Set(wxPoint(x,y), selcell, | |
1436 | m_tmpSelFromPos, m_tmpSelFromCell); | |
1437 | } | |
2f0bebe6 | 1438 | m_selection->ClearFromToCharacterPos(); |
1338c59a | 1439 | Refresh(); |
adf2eb2d VS |
1440 | } |
1441 | } | |
1442 | } | |
d659d703 | 1443 | |
adf2eb2d | 1444 | // handle cursor and status bar text changes: |
f6010d8f | 1445 | |
bc55e31b VS |
1446 | // NB: because we're passing in 'cell' and not 'm_Cell' (so that the |
1447 | // leaf cell lookup isn't done twice), we need to adjust the | |
1448 | // position for the new root: | |
1449 | wxPoint posInCell(x, y); | |
1450 | if (cell) | |
1451 | posInCell -= cell->GetAbsPos(); | |
1452 | wxHtmlWindowMouseHelper::HandleIdle(cell, posInCell); | |
5526e819 VS |
1453 | } |
1454 | } | |
1455 | ||
e3774124 | 1456 | #if wxUSE_CLIPBOARD |
1338c59a VS |
1457 | void wxHtmlWindow::StopAutoScrolling() |
1458 | { | |
1459 | if ( m_timerAutoScroll ) | |
1460 | { | |
1461 | wxDELETE(m_timerAutoScroll); | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event) | |
1466 | { | |
1467 | StopAutoScrolling(); | |
1468 | event.Skip(); | |
1469 | } | |
1470 | ||
1471 | void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event) | |
1472 | { | |
1473 | // don't prevent the usual processing of the event from taking place | |
1474 | event.Skip(); | |
1475 | ||
1476 | // when a captured mouse leave a scrolled window we start generate | |
1477 | // scrolling events to allow, for example, extending selection beyond the | |
1478 | // visible area in some controls | |
1479 | if ( wxWindow::GetCapture() == this ) | |
1480 | { | |
1481 | // where is the mouse leaving? | |
1482 | int pos, orient; | |
1483 | wxPoint pt = event.GetPosition(); | |
1484 | if ( pt.x < 0 ) | |
1485 | { | |
1486 | orient = wxHORIZONTAL; | |
1487 | pos = 0; | |
1488 | } | |
1489 | else if ( pt.y < 0 ) | |
1490 | { | |
1491 | orient = wxVERTICAL; | |
1492 | pos = 0; | |
1493 | } | |
1494 | else // we're lower or to the right of the window | |
1495 | { | |
1496 | wxSize size = GetClientSize(); | |
1497 | if ( pt.x > size.x ) | |
1498 | { | |
1499 | orient = wxHORIZONTAL; | |
1500 | pos = GetVirtualSize().x / wxHTML_SCROLL_STEP; | |
1501 | } | |
1502 | else if ( pt.y > size.y ) | |
1503 | { | |
1504 | orient = wxVERTICAL; | |
1505 | pos = GetVirtualSize().y / wxHTML_SCROLL_STEP; | |
1506 | } | |
1507 | else // this should be impossible | |
1508 | { | |
1509 | // but seems to happen sometimes under wxMSW - maybe it's a bug | |
1510 | // there but for now just ignore it | |
1511 | ||
9a83f860 | 1512 | //wxFAIL_MSG( wxT("can't understand where has mouse gone") ); |
1338c59a VS |
1513 | |
1514 | return; | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | // only start the auto scroll timer if the window can be scrolled in | |
1519 | // this direction | |
1520 | if ( !HasScrollbar(orient) ) | |
1521 | return; | |
1522 | ||
1523 | delete m_timerAutoScroll; | |
1524 | m_timerAutoScroll = new wxHtmlWinAutoScrollTimer | |
1525 | ( | |
1526 | this, | |
1527 | pos == 0 ? wxEVT_SCROLLWIN_LINEUP | |
1528 | : wxEVT_SCROLLWIN_LINEDOWN, | |
1529 | pos, | |
1530 | orient | |
1531 | ); | |
1532 | m_timerAutoScroll->Start(50); // FIXME: make configurable | |
1533 | } | |
1534 | } | |
1535 | ||
e3774124 VS |
1536 | void wxHtmlWindow::OnKeyUp(wxKeyEvent& event) |
1537 | { | |
0f11c233 VZ |
1538 | if ( IsSelectionEnabled() && |
1539 | (event.GetKeyCode() == 'C' && event.CmdDown()) ) | |
e3774124 | 1540 | { |
ce7fe42e | 1541 | wxClipboardTextEvent evt(wxEVT_TEXT_COPY, GetId()); |
0f11c233 VZ |
1542 | |
1543 | evt.SetEventObject(this); | |
1544 | ||
1545 | GetEventHandler()->ProcessEvent(evt); | |
e3774124 | 1546 | } |
fca74306 VS |
1547 | else |
1548 | { | |
1549 | event.Skip(); | |
1550 | } | |
e3774124 VS |
1551 | } |
1552 | ||
fc7a2a60 | 1553 | void wxHtmlWindow::OnCopy(wxCommandEvent& WXUNUSED(event)) |
e3774124 | 1554 | { |
5de65c69 | 1555 | (void) CopySelection(); |
e3774124 | 1556 | } |
d659d703 | 1557 | |
0f11c233 VZ |
1558 | void wxHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event)) |
1559 | { | |
1560 | (void) CopySelection(); | |
1561 | } | |
1562 | ||
31eefb99 VS |
1563 | void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event) |
1564 | { | |
1565 | // select word under cursor: | |
1566 | if ( IsSelectionEnabled() ) | |
1567 | { | |
0994d968 | 1568 | SelectWord(CalcUnscrolledPosition(event.GetPosition())); |
d659d703 | 1569 | |
5de65c69 | 1570 | (void) CopySelection(Primary); |
d659d703 | 1571 | |
0994d968 | 1572 | m_lastDoubleClick = wxGetLocalTimeMillis(); |
31eefb99 VS |
1573 | } |
1574 | else | |
1575 | event.Skip(); | |
1576 | } | |
0994d968 VS |
1577 | |
1578 | void wxHtmlWindow::SelectWord(const wxPoint& pos) | |
1579 | { | |
2a536376 | 1580 | if ( m_Cell ) |
0994d968 | 1581 | { |
2a536376 VS |
1582 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); |
1583 | if ( cell ) | |
1584 | { | |
1585 | delete m_selection; | |
1586 | m_selection = new wxHtmlSelection(); | |
1587 | m_selection->Set(cell, cell); | |
1588 | RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()), | |
1589 | wxSize(cell->GetWidth(), cell->GetHeight()))); | |
1590 | } | |
0994d968 VS |
1591 | } |
1592 | } | |
1593 | ||
1594 | void wxHtmlWindow::SelectLine(const wxPoint& pos) | |
1595 | { | |
2a536376 | 1596 | if ( m_Cell ) |
0994d968 | 1597 | { |
2a536376 VS |
1598 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); |
1599 | if ( cell ) | |
0994d968 | 1600 | { |
2a536376 VS |
1601 | // We use following heuristic to find a "line": let the line be all |
1602 | // cells in same container as the cell under mouse cursor that are | |
4c51a665 | 1603 | // neither completely above nor completely below the clicked cell |
2a536376 VS |
1604 | // (i.e. are likely to be words positioned on same line of text). |
1605 | ||
1606 | int y1 = cell->GetAbsPos().y; | |
1607 | int y2 = y1 + cell->GetHeight(); | |
1608 | int y; | |
1609 | const wxHtmlCell *c; | |
1610 | const wxHtmlCell *before = NULL; | |
1611 | const wxHtmlCell *after = NULL; | |
1612 | ||
1613 | // find last cell of line: | |
1614 | for ( c = cell->GetNext(); c; c = c->GetNext()) | |
1615 | { | |
1616 | y = c->GetAbsPos().y; | |
1617 | if ( y + c->GetHeight() > y1 && y < y2 ) | |
1618 | after = c; | |
1619 | else | |
1620 | break; | |
1621 | } | |
1622 | if ( !after ) | |
1623 | after = cell; | |
0994d968 | 1624 | |
2a536376 VS |
1625 | // find first cell of line: |
1626 | for ( c = cell->GetParent()->GetFirstChild(); | |
1627 | c && c != cell; c = c->GetNext()) | |
0994d968 | 1628 | { |
2a536376 VS |
1629 | y = c->GetAbsPos().y; |
1630 | if ( y + c->GetHeight() > y1 && y < y2 ) | |
1631 | { | |
1632 | if ( ! before ) | |
1633 | before = c; | |
1634 | } | |
1635 | else | |
1636 | before = NULL; | |
0994d968 | 1637 | } |
2a536376 VS |
1638 | if ( !before ) |
1639 | before = cell; | |
1640 | ||
1641 | delete m_selection; | |
1642 | m_selection = new wxHtmlSelection(); | |
1643 | m_selection->Set(before, after); | |
1644 | ||
1645 | Refresh(); | |
0994d968 | 1646 | } |
2a536376 VS |
1647 | } |
1648 | } | |
d659d703 | 1649 | |
2a536376 VS |
1650 | void wxHtmlWindow::SelectAll() |
1651 | { | |
1652 | if ( m_Cell ) | |
1653 | { | |
0994d968 VS |
1654 | delete m_selection; |
1655 | m_selection = new wxHtmlSelection(); | |
2a536376 | 1656 | m_selection->Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal()); |
0994d968 VS |
1657 | Refresh(); |
1658 | } | |
1659 | } | |
2a536376 | 1660 | |
d659d703 | 1661 | #endif // wxUSE_CLIPBOARD |
e3774124 VS |
1662 | |
1663 | ||
5526e819 | 1664 | |
bfb9ee96 | 1665 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject) |
5526e819 | 1666 | |
459f2add | 1667 | wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow) |
786a2425 | 1668 | /* |
d1da8872 WS |
1669 | TODO PROPERTIES |
1670 | style , wxHW_SCROLLBAR_AUTO | |
1671 | borders , (dimension) | |
1672 | url , string | |
1673 | htmlcode , string | |
786a2425 | 1674 | */ |
459f2add | 1675 | wxEND_PROPERTIES_TABLE() |
786a2425 | 1676 | |
459f2add SC |
1677 | wxBEGIN_HANDLERS_TABLE(wxHtmlWindow) |
1678 | wxEND_HANDLERS_TABLE() | |
786a2425 | 1679 | |
d1da8872 | 1680 | wxCONSTRUCTOR_5( wxHtmlWindow , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
28953245 SC |
1681 | |
1682 | wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow, wxScrolledWindow,"wx/html/htmlwin.h") | |
5526e819 VS |
1683 | |
1684 | BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow) | |
1685 | EVT_SIZE(wxHtmlWindow::OnSize) | |
adf2eb2d VS |
1686 | EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown) |
1687 | EVT_LEFT_UP(wxHtmlWindow::OnMouseUp) | |
1688 | EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp) | |
31d8b4ad | 1689 | EVT_MOTION(wxHtmlWindow::OnMouseMove) |
1338c59a | 1690 | EVT_PAINT(wxHtmlWindow::OnPaint) |
49b489e8 | 1691 | EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground) |
e3774124 | 1692 | #if wxUSE_CLIPBOARD |
31eefb99 | 1693 | EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick) |
1338c59a VS |
1694 | EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter) |
1695 | EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave) | |
63e819f2 | 1696 | EVT_MOUSE_CAPTURE_LOST(wxHtmlWindow::OnMouseCaptureLost) |
e3774124 VS |
1697 | EVT_KEY_UP(wxHtmlWindow::OnKeyUp) |
1698 | EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy) | |
0f11c233 | 1699 | EVT_TEXT_COPY(wxID_ANY, wxHtmlWindow::OnClipboardEvent) |
d659d703 | 1700 | #endif // wxUSE_CLIPBOARD |
5526e819 VS |
1701 | END_EVENT_TABLE() |
1702 | ||
bc55e31b VS |
1703 | //----------------------------------------------------------------------------- |
1704 | // wxHtmlWindowInterface implementation in wxHtmlWindow | |
1705 | //----------------------------------------------------------------------------- | |
1706 | ||
1707 | void wxHtmlWindow::SetHTMLWindowTitle(const wxString& title) | |
1708 | { | |
1709 | OnSetTitle(title); | |
1710 | } | |
1711 | ||
1712 | void wxHtmlWindow::OnHTMLLinkClicked(const wxHtmlLinkInfo& link) | |
1713 | { | |
1714 | OnLinkClicked(link); | |
1715 | } | |
1716 | ||
1717 | wxHtmlOpeningStatus wxHtmlWindow::OnHTMLOpeningURL(wxHtmlURLType type, | |
1718 | const wxString& url, | |
1719 | wxString *redirect) const | |
1720 | { | |
1721 | return OnOpeningURL(type, url, redirect); | |
1722 | } | |
1723 | ||
1724 | wxPoint wxHtmlWindow::HTMLCoordsToWindow(wxHtmlCell *WXUNUSED(cell), | |
1725 | const wxPoint& pos) const | |
1726 | { | |
1727 | return CalcScrolledPosition(pos); | |
1728 | } | |
1729 | ||
1730 | wxWindow* wxHtmlWindow::GetHTMLWindow() | |
1731 | { | |
1732 | return this; | |
1733 | } | |
1734 | ||
1735 | wxColour wxHtmlWindow::GetHTMLBackgroundColour() const | |
1736 | { | |
1737 | return GetBackgroundColour(); | |
1738 | } | |
1739 | ||
1740 | void wxHtmlWindow::SetHTMLBackgroundColour(const wxColour& clr) | |
1741 | { | |
1742 | SetBackgroundColour(clr); | |
1743 | } | |
1744 | ||
1745 | void wxHtmlWindow::SetHTMLBackgroundImage(const wxBitmap& bmpBg) | |
1746 | { | |
1747 | SetBackgroundImage(bmpBg); | |
1748 | } | |
5526e819 | 1749 | |
bc55e31b VS |
1750 | void wxHtmlWindow::SetHTMLStatusText(const wxString& text) |
1751 | { | |
1752 | #if wxUSE_STATUSBAR | |
37146d33 VS |
1753 | if (m_RelatedStatusBarIndex != -1) |
1754 | { | |
1755 | if (m_RelatedStatusBar) | |
1756 | { | |
1757 | m_RelatedStatusBar->SetStatusText(text, m_RelatedStatusBarIndex); | |
1758 | } | |
1759 | else if (m_RelatedFrame) | |
1760 | { | |
1761 | m_RelatedFrame->SetStatusText(text, m_RelatedStatusBarIndex); | |
1762 | } | |
1763 | } | |
80d99525 WS |
1764 | #else |
1765 | wxUnusedVar(text); | |
bc55e31b VS |
1766 | #endif // wxUSE_STATUSBAR |
1767 | } | |
5526e819 | 1768 | |
88a1b648 VS |
1769 | /*static*/ |
1770 | wxCursor wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor type) | |
1771 | { | |
1772 | switch (type) | |
1773 | { | |
1774 | case HTMLCursor_Link: | |
1775 | if ( !ms_cursorLink ) | |
1776 | ms_cursorLink = new wxCursor(wxCURSOR_HAND); | |
1777 | return *ms_cursorLink; | |
1778 | ||
1779 | case HTMLCursor_Text: | |
1780 | if ( !ms_cursorText ) | |
1781 | ms_cursorText = new wxCursor(wxCURSOR_IBEAM); | |
1782 | return *ms_cursorText; | |
1783 | ||
1784 | case HTMLCursor_Default: | |
1785 | default: | |
1786 | return *wxSTANDARD_CURSOR; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | wxCursor wxHtmlWindow::GetHTMLCursor(HTMLCursor type) const | |
1791 | { | |
1792 | return GetDefaultHTMLCursor(type); | |
1793 | } | |
1794 | ||
5526e819 | 1795 | |
bc55e31b VS |
1796 | //----------------------------------------------------------------------------- |
1797 | // wxHtmlWinModule | |
1798 | //----------------------------------------------------------------------------- | |
5526e819 | 1799 | |
a76015e6 VS |
1800 | // A module to allow initialization/cleanup |
1801 | // without calling these functions from app.cpp or from | |
1802 | // the user's application. | |
1803 | ||
1804 | class wxHtmlWinModule: public wxModule | |
1805 | { | |
1806 | DECLARE_DYNAMIC_CLASS(wxHtmlWinModule) | |
1807 | public: | |
1808 | wxHtmlWinModule() : wxModule() {} | |
d1da8872 | 1809 | bool OnInit() { return true; } |
a76015e6 VS |
1810 | void OnExit() { wxHtmlWindow::CleanUpStatics(); } |
1811 | }; | |
1812 | ||
1813 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule) | |
1814 | ||
5526e819 | 1815 | |
0cecad31 VS |
1816 | // This hack forces the linker to always link in m_* files |
1817 | // (wxHTML doesn't work without handlers from these files) | |
1818 | #include "wx/html/forcelnk.h" | |
1819 | FORCE_WXHTML_MODULES() | |
5526e819 | 1820 | |
d659d703 | 1821 | #endif // wxUSE_HTML |