]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: htmlwin.cpp | |
3 | // Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation) | |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 1999 Vaclav Slavik | |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "htmlwin.h" | |
13 | #pragma implementation "htmlproc.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #include "wx/defs.h" | |
19 | #if wxUSE_HTML && wxUSE_STREAMS | |
20 | ||
21 | #ifdef __BORDLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WXPRECOMP | |
26 | #include "wx/log.h" | |
27 | #include "wx/intl.h" | |
28 | #include "wx/dcclient.h" | |
29 | #include "wx/frame.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/html/htmlwin.h" | |
33 | #include "wx/html/htmlproc.h" | |
34 | #include "wx/list.h" | |
35 | ||
36 | #include "wx/arrimpl.cpp" | |
37 | #include "wx/listimpl.cpp" | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // wxHtmlHistoryItem | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | // item of history list | |
44 | class WXDLLEXPORT wxHtmlHistoryItem : public wxObject | |
45 | { | |
46 | public: | |
47 | wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;} | |
48 | int GetPos() const {return m_Pos;} | |
49 | void SetPos(int p) {m_Pos = p;} | |
50 | const wxString& GetPage() const {return m_Page;} | |
51 | const wxString& GetAnchor() const {return m_Anchor;} | |
52 | ||
53 | private: | |
54 | wxString m_Page; | |
55 | wxString m_Anchor; | |
56 | int m_Pos; | |
57 | }; | |
58 | ||
59 | ||
60 | //----------------------------------------------------------------------------- | |
61 | // our private arrays: | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
64 | WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray); | |
65 | WX_DEFINE_OBJARRAY(wxHtmlHistoryArray); | |
66 | ||
67 | WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList); | |
68 | WX_DEFINE_LIST(wxHtmlProcessorList); | |
69 | ||
70 | //----------------------------------------------------------------------------- | |
71 | // wxHtmlWindow | |
72 | //----------------------------------------------------------------------------- | |
73 | ||
74 | ||
75 | void wxHtmlWindow::Init() | |
76 | { | |
77 | m_tmpMouseMoved = FALSE; | |
78 | m_tmpLastLink = NULL; | |
79 | m_tmpLastCell = NULL; | |
80 | m_tmpCanDrawLocks = 0; | |
81 | m_FS = new wxFileSystem(); | |
82 | m_RelatedStatusBar = -1; | |
83 | m_RelatedFrame = NULL; | |
84 | m_TitleFormat = wxT("%s"); | |
85 | m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString; | |
86 | m_Cell = NULL; | |
87 | m_Parser = new wxHtmlWinParser(this); | |
88 | m_Parser->SetFS(m_FS); | |
89 | m_HistoryPos = -1; | |
90 | m_HistoryOn = TRUE; | |
91 | m_History = new wxHtmlHistoryArray; | |
92 | m_Processors = NULL; | |
93 | m_Style = 0; | |
94 | SetBorders(10); | |
95 | } | |
96 | ||
97 | bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id, | |
98 | const wxPoint& pos, const wxSize& size, | |
99 | long style, const wxString& name) | |
100 | { | |
101 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
102 | style | wxVSCROLL | wxHSCROLL, name)) | |
103 | return FALSE; | |
104 | ||
105 | m_Style = style; | |
106 | SetPage(wxT("<html><body></body></html>")); | |
107 | return TRUE; | |
108 | } | |
109 | ||
110 | ||
111 | wxHtmlWindow::~wxHtmlWindow() | |
112 | { | |
113 | HistoryClear(); | |
114 | ||
115 | if (m_Cell) delete m_Cell; | |
116 | ||
117 | delete m_Parser; | |
118 | delete m_FS; | |
119 | delete m_History; | |
120 | delete m_Processors; | |
121 | } | |
122 | ||
123 | ||
124 | ||
125 | void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format) | |
126 | { | |
127 | m_RelatedFrame = frame; | |
128 | m_TitleFormat = format; | |
129 | } | |
130 | ||
131 | ||
132 | ||
133 | void wxHtmlWindow::SetRelatedStatusBar(int bar) | |
134 | { | |
135 | m_RelatedStatusBar = bar; | |
136 | } | |
137 | ||
138 | ||
139 | ||
140 | void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes) | |
141 | { | |
142 | wxString op = m_OpenedPage; | |
143 | ||
144 | m_Parser->SetFonts(normal_face, fixed_face, sizes); | |
145 | // fonts changed => contents invalid, so reload the page: | |
146 | SetPage(wxT("<html><body></body></html>")); | |
147 | if (!op.IsEmpty()) LoadPage(op); | |
148 | } | |
149 | ||
150 | ||
151 | ||
152 | bool wxHtmlWindow::SetPage(const wxString& source) | |
153 | { | |
154 | wxString newsrc(source); | |
155 | ||
156 | // pass HTML through registered processors: | |
157 | if (m_Processors || m_GlobalProcessors) | |
158 | { | |
159 | wxHtmlProcessorList::Node *nodeL, *nodeG; | |
160 | int prL, prG; | |
161 | ||
162 | nodeL = (m_Processors) ? m_Processors->GetFirst() : NULL; | |
163 | nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : NULL; | |
164 | ||
165 | // VS: there are two lists, global and local, both of them sorted by | |
166 | // priority. Since we have to go through _both_ lists with | |
167 | // decreasing priority, we "merge-sort" the lists on-line by | |
168 | // processing that one of the two heads that has higher priority | |
169 | // in every iteration | |
170 | while (nodeL || nodeG) | |
171 | { | |
172 | prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1; | |
173 | prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1; | |
174 | if (prL > prG) | |
175 | { | |
176 | if (nodeL->GetData()->IsEnabled()) | |
177 | newsrc = nodeL->GetData()->Process(newsrc); | |
178 | nodeL = nodeL->GetNext(); | |
179 | } | |
180 | else // prL <= prG | |
181 | { | |
182 | if (nodeG->GetData()->IsEnabled()) | |
183 | newsrc = nodeG->GetData()->Process(newsrc); | |
184 | nodeG = nodeG->GetNext(); | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | // ...and run the parser on it: | |
190 | wxClientDC *dc = new wxClientDC(this); | |
191 | dc->SetMapMode(wxMM_TEXT); | |
192 | SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF)); | |
193 | m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString; | |
194 | m_Parser->SetDC(dc); | |
195 | if (m_Cell) | |
196 | { | |
197 | delete m_Cell; | |
198 | m_Cell = NULL; | |
199 | } | |
200 | m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc); | |
201 | delete dc; | |
202 | m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); | |
203 | m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER); | |
204 | CreateLayout(); | |
205 | if (m_tmpCanDrawLocks == 0) | |
206 | Refresh(); | |
207 | return TRUE; | |
208 | } | |
209 | ||
210 | bool wxHtmlWindow::AppendToPage(const wxString& source) | |
211 | { | |
212 | return SetPage(*(GetParser()->GetSource()) + source); | |
213 | } | |
214 | ||
215 | bool wxHtmlWindow::LoadPage(const wxString& location) | |
216 | { | |
217 | wxBusyCursor busyCursor; | |
218 | ||
219 | wxFSFile *f; | |
220 | bool rt_val; | |
221 | bool needs_refresh = FALSE; | |
222 | ||
223 | m_tmpCanDrawLocks++; | |
224 | if (m_HistoryOn && (m_HistoryPos != -1)) | |
225 | { | |
226 | // store scroll position into history item: | |
227 | int x, y; | |
228 | GetViewStart(&x, &y); | |
229 | (*m_History)[m_HistoryPos].SetPos(y); | |
230 | } | |
231 | ||
232 | if (location[0] == wxT('#')) | |
233 | { | |
234 | // local anchor: | |
235 | wxString anch = location.Mid(1) /*1 to end*/; | |
236 | m_tmpCanDrawLocks--; | |
237 | rt_val = ScrollToAnchor(anch); | |
238 | m_tmpCanDrawLocks++; | |
239 | } | |
240 | else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage) | |
241 | { | |
242 | wxString anch = location.AfterFirst(wxT('#')); | |
243 | m_tmpCanDrawLocks--; | |
244 | rt_val = ScrollToAnchor(anch); | |
245 | m_tmpCanDrawLocks++; | |
246 | } | |
247 | else if (location.Find(wxT('#')) != wxNOT_FOUND && | |
248 | (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage) | |
249 | { | |
250 | wxString anch = location.AfterFirst(wxT('#')); | |
251 | m_tmpCanDrawLocks--; | |
252 | rt_val = ScrollToAnchor(anch); | |
253 | m_tmpCanDrawLocks++; | |
254 | } | |
255 | ||
256 | else | |
257 | { | |
258 | needs_refresh = TRUE; | |
259 | // load&display it: | |
260 | if (m_RelatedStatusBar != -1) | |
261 | { | |
262 | m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar); | |
263 | Refresh(FALSE); | |
264 | } | |
265 | ||
266 | f = m_FS->OpenFile(location); | |
267 | ||
268 | if (f == NULL) | |
269 | { | |
270 | wxLogError(_("Unable to open requested HTML document: %s"), location.c_str()); | |
271 | m_tmpCanDrawLocks--; | |
272 | return FALSE; | |
273 | } | |
274 | ||
275 | else | |
276 | { | |
277 | wxNode *node; | |
278 | wxString src = wxEmptyString; | |
279 | ||
280 | if (m_RelatedStatusBar != -1) | |
281 | { | |
282 | wxString msg = _("Loading : ") + location; | |
283 | m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar); | |
284 | Refresh(FALSE); | |
285 | } | |
286 | ||
287 | node = m_Filters.GetFirst(); | |
288 | while (node) | |
289 | { | |
290 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
291 | if (h->CanRead(*f)) | |
292 | { | |
293 | src = h->ReadFile(*f); | |
294 | break; | |
295 | } | |
296 | node = node->GetNext(); | |
297 | } | |
298 | if (src == wxEmptyString) | |
299 | { | |
300 | if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter(); | |
301 | src = m_DefaultFilter->ReadFile(*f); | |
302 | } | |
303 | ||
304 | m_FS->ChangePathTo(f->GetLocation()); | |
305 | rt_val = SetPage(src); | |
306 | m_OpenedPage = f->GetLocation(); | |
307 | if (f->GetAnchor() != wxEmptyString) | |
308 | { | |
309 | wxYield(); | |
310 | ScrollToAnchor(f->GetAnchor()); | |
311 | } | |
312 | ||
313 | delete f; | |
314 | ||
315 | if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar); | |
316 | } | |
317 | } | |
318 | ||
319 | if (m_HistoryOn) // add this page to history there: | |
320 | { | |
321 | int c = m_History->GetCount() - (m_HistoryPos + 1); | |
322 | ||
323 | if (m_HistoryPos < 0 || | |
324 | (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage || | |
325 | (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor) | |
326 | { | |
327 | m_HistoryPos++; | |
328 | for (int i = 0; i < c; i++) | |
329 | m_History->RemoveAt(m_HistoryPos); | |
330 | m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor)); | |
331 | } | |
332 | } | |
333 | ||
334 | if (m_OpenedPageTitle == wxEmptyString) | |
335 | OnSetTitle(wxFileNameFromPath(m_OpenedPage)); | |
336 | ||
337 | if (needs_refresh) | |
338 | { | |
339 | wxYield(); | |
340 | m_tmpCanDrawLocks--; | |
341 | Refresh(); | |
342 | } | |
343 | else | |
344 | m_tmpCanDrawLocks--; | |
345 | ||
346 | return rt_val; | |
347 | } | |
348 | ||
349 | ||
350 | ||
351 | bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor) | |
352 | { | |
353 | const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor); | |
354 | if (!c) | |
355 | { | |
356 | wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str()); | |
357 | return FALSE; | |
358 | } | |
359 | else | |
360 | { | |
361 | int y; | |
362 | ||
363 | for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY(); | |
364 | Scroll(-1, y / wxHTML_SCROLL_STEP); | |
365 | m_OpenedAnchor = anchor; | |
366 | return TRUE; | |
367 | } | |
368 | } | |
369 | ||
370 | ||
371 | void wxHtmlWindow::OnSetTitle(const wxString& title) | |
372 | { | |
373 | if (m_RelatedFrame) | |
374 | { | |
375 | wxString tit; | |
376 | tit.Printf(m_TitleFormat, title.c_str()); | |
377 | m_RelatedFrame->SetTitle(tit); | |
378 | } | |
379 | m_OpenedPageTitle = title; | |
380 | } | |
381 | ||
382 | ||
383 | ||
384 | ||
385 | ||
386 | void wxHtmlWindow::CreateLayout() | |
387 | { | |
388 | int ClientWidth, ClientHeight; | |
389 | ||
390 | if (!m_Cell) return; | |
391 | ||
392 | if (m_Style & wxHW_SCROLLBAR_NEVER) | |
393 | { | |
394 | SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off | |
395 | GetClientSize(&ClientWidth, &ClientHeight); | |
396 | m_Cell->Layout(ClientWidth); | |
397 | } | |
398 | ||
399 | else { | |
400 | GetClientSize(&ClientWidth, &ClientHeight); | |
401 | m_Cell->Layout(ClientWidth); | |
402 | if (ClientHeight < m_Cell->GetHeight() + GetCharHeight()) | |
403 | { | |
404 | SetScrollbars( | |
405 | wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP, | |
406 | m_Cell->GetWidth() / wxHTML_SCROLL_STEP, | |
407 | (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP | |
408 | /*cheat: top-level frag is always container*/); | |
409 | } | |
410 | else /* we fit into window, no need for scrollbars */ | |
411 | { | |
412 | SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable... | |
413 | GetClientSize(&ClientWidth, &ClientHeight); | |
414 | m_Cell->Layout(ClientWidth); // ...and relayout | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | ||
420 | ||
421 | void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path) | |
422 | { | |
423 | wxString oldpath; | |
424 | wxString tmp; | |
425 | int p_fontsizes[7]; | |
426 | wxString p_fff, p_ffn; | |
427 | ||
428 | if (path != wxEmptyString) | |
429 | { | |
430 | oldpath = cfg->GetPath(); | |
431 | cfg->SetPath(path); | |
432 | } | |
433 | ||
434 | m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders); | |
435 | p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
436 | p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
437 | for (int i = 0; i < 7; i++) | |
438 | { | |
439 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); | |
440 | p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]); | |
441 | } | |
442 | SetFonts(p_ffn, p_fff, p_fontsizes); | |
443 | ||
444 | if (path != wxEmptyString) | |
445 | cfg->SetPath(oldpath); | |
446 | } | |
447 | ||
448 | ||
449 | ||
450 | void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path) | |
451 | { | |
452 | wxString oldpath; | |
453 | wxString tmp; | |
454 | ||
455 | if (path != wxEmptyString) | |
456 | { | |
457 | oldpath = cfg->GetPath(); | |
458 | cfg->SetPath(path); | |
459 | } | |
460 | ||
461 | cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders); | |
462 | cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
463 | cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
464 | for (int i = 0; i < 7; i++) | |
465 | { | |
466 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); | |
467 | cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]); | |
468 | } | |
469 | ||
470 | if (path != wxEmptyString) | |
471 | cfg->SetPath(oldpath); | |
472 | } | |
473 | ||
474 | ||
475 | ||
476 | bool wxHtmlWindow::HistoryBack() | |
477 | { | |
478 | wxString a, l; | |
479 | ||
480 | if (m_HistoryPos < 1) return FALSE; | |
481 | ||
482 | // store scroll position into history item: | |
483 | int x, y; | |
484 | GetViewStart(&x, &y); | |
485 | (*m_History)[m_HistoryPos].SetPos(y); | |
486 | ||
487 | // go to previous position: | |
488 | m_HistoryPos--; | |
489 | ||
490 | l = (*m_History)[m_HistoryPos].GetPage(); | |
491 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
492 | m_HistoryOn = FALSE; | |
493 | m_tmpCanDrawLocks++; | |
494 | if (a == wxEmptyString) LoadPage(l); | |
495 | else LoadPage(l + wxT("#") + a); | |
496 | m_HistoryOn = TRUE; | |
497 | wxYield(); | |
498 | m_tmpCanDrawLocks--; | |
499 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); | |
500 | Refresh(); | |
501 | return TRUE; | |
502 | } | |
503 | ||
504 | bool wxHtmlWindow::HistoryCanBack() | |
505 | { | |
506 | if (m_HistoryPos < 1) return FALSE; | |
507 | return TRUE ; | |
508 | } | |
509 | ||
510 | ||
511 | bool wxHtmlWindow::HistoryForward() | |
512 | { | |
513 | wxString a, l; | |
514 | ||
515 | if (m_HistoryPos == -1) return FALSE; | |
516 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE; | |
517 | ||
518 | m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage() | |
519 | ||
520 | m_HistoryPos++; | |
521 | l = (*m_History)[m_HistoryPos].GetPage(); | |
522 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
523 | m_HistoryOn = FALSE; | |
524 | m_tmpCanDrawLocks++; | |
525 | if (a == wxEmptyString) LoadPage(l); | |
526 | else LoadPage(l + wxT("#") + a); | |
527 | m_HistoryOn = TRUE; | |
528 | wxYield(); | |
529 | m_tmpCanDrawLocks--; | |
530 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); | |
531 | Refresh(); | |
532 | return TRUE; | |
533 | } | |
534 | ||
535 | bool wxHtmlWindow::HistoryCanForward() | |
536 | { | |
537 | if (m_HistoryPos == -1) return FALSE; | |
538 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE; | |
539 | return TRUE ; | |
540 | } | |
541 | ||
542 | ||
543 | void wxHtmlWindow::HistoryClear() | |
544 | { | |
545 | m_History->Empty(); | |
546 | m_HistoryPos = -1; | |
547 | } | |
548 | ||
549 | void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor) | |
550 | { | |
551 | if (!m_Processors) | |
552 | { | |
553 | m_Processors = new wxHtmlProcessorList; | |
554 | m_Processors->DeleteContents(TRUE); | |
555 | } | |
556 | wxHtmlProcessorList::Node *node; | |
557 | ||
558 | for (node = m_Processors->GetFirst(); node; node = node->GetNext()) | |
559 | { | |
560 | if (processor->GetPriority() > node->GetData()->GetPriority()) | |
561 | { | |
562 | m_Processors->Insert(node, processor); | |
563 | return; | |
564 | } | |
565 | } | |
566 | m_Processors->Append(processor); | |
567 | } | |
568 | ||
569 | /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor) | |
570 | { | |
571 | if (!m_GlobalProcessors) | |
572 | { | |
573 | m_GlobalProcessors = new wxHtmlProcessorList; | |
574 | m_GlobalProcessors->DeleteContents(TRUE); | |
575 | } | |
576 | wxHtmlProcessorList::Node *node; | |
577 | ||
578 | for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext()) | |
579 | { | |
580 | if (processor->GetPriority() > node->GetData()->GetPriority()) | |
581 | { | |
582 | m_GlobalProcessors->Insert(node, processor); | |
583 | return; | |
584 | } | |
585 | } | |
586 | m_GlobalProcessors->Append(processor); | |
587 | } | |
588 | ||
589 | ||
590 | ||
591 | wxList wxHtmlWindow::m_Filters; | |
592 | wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL; | |
593 | wxCursor *wxHtmlWindow::s_cur_hand = NULL; | |
594 | wxCursor *wxHtmlWindow::s_cur_arrow = NULL; | |
595 | wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL; | |
596 | ||
597 | void wxHtmlWindow::CleanUpStatics() | |
598 | { | |
599 | delete m_DefaultFilter; | |
600 | m_DefaultFilter = NULL; | |
601 | m_Filters.DeleteContents(TRUE); | |
602 | m_Filters.Clear(); | |
603 | delete m_GlobalProcessors; | |
604 | m_GlobalProcessors = NULL; | |
605 | delete s_cur_hand; | |
606 | delete s_cur_arrow; | |
607 | } | |
608 | ||
609 | ||
610 | ||
611 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) | |
612 | { | |
613 | m_Filters.Append(filter); | |
614 | } | |
615 | ||
616 | ||
617 | ||
618 | ||
619 | void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link) | |
620 | { | |
621 | LoadPage(link.GetHref()); | |
622 | } | |
623 | ||
624 | void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell, | |
625 | wxCoord x, wxCoord y, | |
626 | const wxMouseEvent& event) | |
627 | { | |
628 | wxCHECK_RET( cell, _T("can't be called with NULL cell") ); | |
629 | ||
630 | cell->OnMouseClick(this, x, y, event); | |
631 | } | |
632 | ||
633 | void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell), | |
634 | wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) | |
635 | { | |
636 | // do nothing here | |
637 | } | |
638 | ||
639 | void wxHtmlWindow::OnDraw(wxDC& dc) | |
640 | { | |
641 | if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return; | |
642 | ||
643 | int x, y; | |
644 | wxRect rect = GetUpdateRegion().GetBox(); | |
645 | ||
646 | dc.SetMapMode(wxMM_TEXT); | |
647 | dc.SetBackgroundMode(wxTRANSPARENT); | |
648 | GetViewStart(&x, &y); | |
649 | ||
650 | m_Cell->Draw(dc, 0, 0, | |
651 | y * wxHTML_SCROLL_STEP + rect.GetTop(), | |
652 | y * wxHTML_SCROLL_STEP + rect.GetBottom()); | |
653 | } | |
654 | ||
655 | ||
656 | ||
657 | ||
658 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
659 | { | |
660 | wxScrolledWindow::OnSize(event); | |
661 | CreateLayout(); | |
662 | Refresh(); | |
663 | } | |
664 | ||
665 | ||
666 | void wxHtmlWindow::OnMouseEvent(wxMouseEvent& event) | |
667 | { | |
668 | m_tmpMouseMoved = TRUE; | |
669 | ||
670 | if (event.ButtonDown()) | |
671 | { | |
672 | if ( m_Cell ) | |
673 | { | |
674 | int sx, sy; | |
675 | GetViewStart(&sx, &sy); | |
676 | sx *= wxHTML_SCROLL_STEP; | |
677 | sy *= wxHTML_SCROLL_STEP; | |
678 | ||
679 | wxPoint pos = event.GetPosition(); | |
680 | pos.x += sx; | |
681 | pos.y += sy; | |
682 | ||
683 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); | |
684 | ||
685 | // VZ: is it possible that we don't find anything at all? | |
686 | // VS: yes. FindCellByPos returns terminal cell and | |
687 | // containers may have empty borders | |
688 | if ( cell ) | |
689 | OnCellClicked(cell, pos.x, pos.y, event); | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | ||
695 | ||
696 | void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
697 | { | |
698 | if (s_cur_hand == NULL) | |
699 | { | |
700 | s_cur_hand = new wxCursor(wxCURSOR_HAND); | |
701 | s_cur_arrow = new wxCursor(wxCURSOR_ARROW); | |
702 | } | |
703 | ||
704 | if (m_tmpMouseMoved && (m_Cell != NULL)) | |
705 | { | |
706 | int sx, sy; | |
707 | GetViewStart(&sx, &sy); | |
708 | sx *= wxHTML_SCROLL_STEP; | |
709 | sy *= wxHTML_SCROLL_STEP; | |
710 | ||
711 | int x, y; | |
712 | wxGetMousePosition(&x, &y); | |
713 | ScreenToClient(&x, &y); | |
714 | x += sx; | |
715 | y += sy; | |
716 | ||
717 | wxHtmlCell *cell = m_Cell->FindCellByPos(x, y); | |
718 | if ( cell != m_tmpLastCell ) | |
719 | { | |
720 | wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL; | |
721 | ||
722 | if (lnk != m_tmpLastLink) | |
723 | { | |
724 | if (lnk == NULL) | |
725 | { | |
726 | SetCursor(*s_cur_arrow); | |
727 | if (m_RelatedStatusBar != -1) | |
728 | m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar); | |
729 | } | |
730 | else | |
731 | { | |
732 | SetCursor(*s_cur_hand); | |
733 | if (m_RelatedStatusBar != -1) | |
734 | m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar); | |
735 | } | |
736 | m_tmpLastLink = lnk; | |
737 | } | |
738 | ||
739 | m_tmpLastCell = cell; | |
740 | } | |
741 | else // mouse moved but stayed in the same cell | |
742 | { | |
743 | if ( cell ) | |
744 | OnCellMouseHover(cell, x, y); | |
745 | } | |
746 | ||
747 | m_tmpMouseMoved = FALSE; | |
748 | } | |
749 | } | |
750 | ||
751 | ||
752 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject) | |
753 | ||
754 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow) | |
755 | ||
756 | BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow) | |
757 | EVT_SIZE(wxHtmlWindow::OnSize) | |
758 | EVT_LEFT_DOWN(wxHtmlWindow::OnMouseEvent) | |
759 | EVT_RIGHT_DOWN(wxHtmlWindow::OnMouseEvent) | |
760 | EVT_MOTION(wxHtmlWindow::OnMouseEvent) | |
761 | EVT_IDLE(wxHtmlWindow::OnIdle) | |
762 | END_EVENT_TABLE() | |
763 | ||
764 | ||
765 | ||
766 | ||
767 | ||
768 | // A module to allow initialization/cleanup | |
769 | // without calling these functions from app.cpp or from | |
770 | // the user's application. | |
771 | ||
772 | class wxHtmlWinModule: public wxModule | |
773 | { | |
774 | DECLARE_DYNAMIC_CLASS(wxHtmlWinModule) | |
775 | public: | |
776 | wxHtmlWinModule() : wxModule() {} | |
777 | bool OnInit() { return TRUE; } | |
778 | void OnExit() { wxHtmlWindow::CleanUpStatics(); } | |
779 | }; | |
780 | ||
781 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule) | |
782 | ||
783 | ||
784 | // This hack forces the linker to always link in m_* files | |
785 | // (wxHTML doesn't work without handlers from these files) | |
786 | #include "wx/html/forcelnk.h" | |
787 | FORCE_WXHTML_MODULES() | |
788 | ||
789 | #endif |