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