]>
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 __BORLANDC__ | |
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 | |
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 | m_selection = NULL; | |
96 | m_makingSelection = false; | |
97 | } | |
98 | ||
99 | bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id, | |
100 | const wxPoint& pos, const wxSize& size, | |
101 | long style, const wxString& name) | |
102 | { | |
103 | if (!wxScrolledWindow::Create(parent, id, pos, size, | |
104 | style | wxVSCROLL | wxHSCROLL, name)) | |
105 | return FALSE; | |
106 | ||
107 | m_Style = style; | |
108 | SetPage(wxT("<html><body></body></html>")); | |
109 | return TRUE; | |
110 | } | |
111 | ||
112 | ||
113 | wxHtmlWindow::~wxHtmlWindow() | |
114 | { | |
115 | HistoryClear(); | |
116 | ||
117 | if (m_Cell) delete m_Cell; | |
118 | ||
119 | delete m_Parser; | |
120 | delete m_FS; | |
121 | delete m_History; | |
122 | delete m_Processors; | |
123 | } | |
124 | ||
125 | ||
126 | ||
127 | void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format) | |
128 | { | |
129 | m_RelatedFrame = frame; | |
130 | m_TitleFormat = format; | |
131 | } | |
132 | ||
133 | ||
134 | ||
135 | void wxHtmlWindow::SetRelatedStatusBar(int bar) | |
136 | { | |
137 | m_RelatedStatusBar = bar; | |
138 | } | |
139 | ||
140 | ||
141 | ||
142 | void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes) | |
143 | { | |
144 | wxString op = m_OpenedPage; | |
145 | ||
146 | m_Parser->SetFonts(normal_face, fixed_face, sizes); | |
147 | // fonts changed => contents invalid, so reload the page: | |
148 | SetPage(wxT("<html><body></body></html>")); | |
149 | if (!op.IsEmpty()) LoadPage(op); | |
150 | } | |
151 | ||
152 | ||
153 | ||
154 | bool wxHtmlWindow::SetPage(const wxString& source) | |
155 | { | |
156 | wxString newsrc(source); | |
157 | ||
158 | wxDELETE(m_selection); | |
159 | ||
160 | // pass HTML through registered processors: | |
161 | if (m_Processors || m_GlobalProcessors) | |
162 | { | |
163 | wxHtmlProcessorList::Node *nodeL, *nodeG; | |
164 | int prL, prG; | |
165 | ||
166 | nodeL = (m_Processors) ? m_Processors->GetFirst() : NULL; | |
167 | nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : NULL; | |
168 | ||
169 | // VS: there are two lists, global and local, both of them sorted by | |
170 | // priority. Since we have to go through _both_ lists with | |
171 | // decreasing priority, we "merge-sort" the lists on-line by | |
172 | // processing that one of the two heads that has higher priority | |
173 | // in every iteration | |
174 | while (nodeL || nodeG) | |
175 | { | |
176 | prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1; | |
177 | prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1; | |
178 | if (prL > prG) | |
179 | { | |
180 | if (nodeL->GetData()->IsEnabled()) | |
181 | newsrc = nodeL->GetData()->Process(newsrc); | |
182 | nodeL = nodeL->GetNext(); | |
183 | } | |
184 | else // prL <= prG | |
185 | { | |
186 | if (nodeG->GetData()->IsEnabled()) | |
187 | newsrc = nodeG->GetData()->Process(newsrc); | |
188 | nodeG = nodeG->GetNext(); | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | // ...and run the parser on it: | |
194 | wxClientDC *dc = new wxClientDC(this); | |
195 | dc->SetMapMode(wxMM_TEXT); | |
196 | SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF)); | |
197 | m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString; | |
198 | m_Parser->SetDC(dc); | |
199 | if (m_Cell) | |
200 | { | |
201 | delete m_Cell; | |
202 | m_Cell = NULL; | |
203 | } | |
204 | m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc); | |
205 | delete dc; | |
206 | m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS); | |
207 | m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER); | |
208 | CreateLayout(); | |
209 | if (m_tmpCanDrawLocks == 0) | |
210 | Refresh(); | |
211 | return TRUE; | |
212 | } | |
213 | ||
214 | bool wxHtmlWindow::AppendToPage(const wxString& source) | |
215 | { | |
216 | return SetPage(*(GetParser()->GetSource()) + source); | |
217 | } | |
218 | ||
219 | bool wxHtmlWindow::LoadPage(const wxString& location) | |
220 | { | |
221 | wxBusyCursor busyCursor; | |
222 | ||
223 | wxFSFile *f; | |
224 | bool rt_val; | |
225 | bool needs_refresh = FALSE; | |
226 | ||
227 | m_tmpCanDrawLocks++; | |
228 | if (m_HistoryOn && (m_HistoryPos != -1)) | |
229 | { | |
230 | // store scroll position into history item: | |
231 | int x, y; | |
232 | GetViewStart(&x, &y); | |
233 | (*m_History)[m_HistoryPos].SetPos(y); | |
234 | } | |
235 | ||
236 | if (location[0] == wxT('#')) | |
237 | { | |
238 | // local anchor: | |
239 | wxString anch = location.Mid(1) /*1 to end*/; | |
240 | m_tmpCanDrawLocks--; | |
241 | rt_val = ScrollToAnchor(anch); | |
242 | m_tmpCanDrawLocks++; | |
243 | } | |
244 | else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage) | |
245 | { | |
246 | wxString anch = location.AfterFirst(wxT('#')); | |
247 | m_tmpCanDrawLocks--; | |
248 | rt_val = ScrollToAnchor(anch); | |
249 | m_tmpCanDrawLocks++; | |
250 | } | |
251 | else if (location.Find(wxT('#')) != wxNOT_FOUND && | |
252 | (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage) | |
253 | { | |
254 | wxString anch = location.AfterFirst(wxT('#')); | |
255 | m_tmpCanDrawLocks--; | |
256 | rt_val = ScrollToAnchor(anch); | |
257 | m_tmpCanDrawLocks++; | |
258 | } | |
259 | ||
260 | else | |
261 | { | |
262 | needs_refresh = TRUE; | |
263 | // load&display it: | |
264 | if (m_RelatedStatusBar != -1) | |
265 | { | |
266 | m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar); | |
267 | Refresh(FALSE); | |
268 | } | |
269 | ||
270 | f = m_Parser->OpenURL(wxHTML_URL_PAGE, location); | |
271 | ||
272 | // try to interpret 'location' as filename instead of URL: | |
273 | if (f == NULL) | |
274 | { | |
275 | wxFileName fn(location); | |
276 | wxString location2 = wxFileSystem::FileNameToURL(fn); | |
277 | f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2); | |
278 | } | |
279 | ||
280 | if (f == NULL) | |
281 | { | |
282 | wxLogError(_("Unable to open requested HTML document: %s"), location.c_str()); | |
283 | m_tmpCanDrawLocks--; | |
284 | return FALSE; | |
285 | } | |
286 | ||
287 | else | |
288 | { | |
289 | wxNode *node; | |
290 | wxString src = wxEmptyString; | |
291 | ||
292 | if (m_RelatedStatusBar != -1) | |
293 | { | |
294 | wxString msg = _("Loading : ") + location; | |
295 | m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar); | |
296 | Refresh(FALSE); | |
297 | } | |
298 | ||
299 | node = m_Filters.GetFirst(); | |
300 | while (node) | |
301 | { | |
302 | wxHtmlFilter *h = (wxHtmlFilter*) node->GetData(); | |
303 | if (h->CanRead(*f)) | |
304 | { | |
305 | src = h->ReadFile(*f); | |
306 | break; | |
307 | } | |
308 | node = node->GetNext(); | |
309 | } | |
310 | if (src == wxEmptyString) | |
311 | { | |
312 | if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter(); | |
313 | src = m_DefaultFilter->ReadFile(*f); | |
314 | } | |
315 | ||
316 | m_FS->ChangePathTo(f->GetLocation()); | |
317 | rt_val = SetPage(src); | |
318 | m_OpenedPage = f->GetLocation(); | |
319 | if (f->GetAnchor() != wxEmptyString) | |
320 | { | |
321 | ScrollToAnchor(f->GetAnchor()); | |
322 | } | |
323 | ||
324 | delete f; | |
325 | ||
326 | if (m_RelatedStatusBar != -1) m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar); | |
327 | } | |
328 | } | |
329 | ||
330 | if (m_HistoryOn) // add this page to history there: | |
331 | { | |
332 | int c = m_History->GetCount() - (m_HistoryPos + 1); | |
333 | ||
334 | if (m_HistoryPos < 0 || | |
335 | (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage || | |
336 | (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor) | |
337 | { | |
338 | m_HistoryPos++; | |
339 | for (int i = 0; i < c; i++) | |
340 | m_History->RemoveAt(m_HistoryPos); | |
341 | m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor)); | |
342 | } | |
343 | } | |
344 | ||
345 | if (m_OpenedPageTitle == wxEmptyString) | |
346 | OnSetTitle(wxFileNameFromPath(m_OpenedPage)); | |
347 | ||
348 | if (needs_refresh) | |
349 | { | |
350 | m_tmpCanDrawLocks--; | |
351 | Refresh(); | |
352 | } | |
353 | else | |
354 | m_tmpCanDrawLocks--; | |
355 | ||
356 | return rt_val; | |
357 | } | |
358 | ||
359 | ||
360 | bool wxHtmlWindow::LoadFile(const wxFileName& filename) | |
361 | { | |
362 | wxString url = wxFileSystem::FileNameToURL(filename); | |
363 | return LoadPage(url); | |
364 | } | |
365 | ||
366 | ||
367 | bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor) | |
368 | { | |
369 | const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor); | |
370 | if (!c) | |
371 | { | |
372 | wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str()); | |
373 | return FALSE; | |
374 | } | |
375 | else | |
376 | { | |
377 | int y; | |
378 | ||
379 | for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY(); | |
380 | Scroll(-1, y / wxHTML_SCROLL_STEP); | |
381 | m_OpenedAnchor = anchor; | |
382 | return TRUE; | |
383 | } | |
384 | } | |
385 | ||
386 | ||
387 | void wxHtmlWindow::OnSetTitle(const wxString& title) | |
388 | { | |
389 | if (m_RelatedFrame) | |
390 | { | |
391 | wxString tit; | |
392 | tit.Printf(m_TitleFormat, title.c_str()); | |
393 | m_RelatedFrame->SetTitle(tit); | |
394 | } | |
395 | m_OpenedPageTitle = title; | |
396 | } | |
397 | ||
398 | ||
399 | ||
400 | ||
401 | ||
402 | void wxHtmlWindow::CreateLayout() | |
403 | { | |
404 | int ClientWidth, ClientHeight; | |
405 | ||
406 | if (!m_Cell) return; | |
407 | ||
408 | if (m_Style & wxHW_SCROLLBAR_NEVER) | |
409 | { | |
410 | SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off | |
411 | GetClientSize(&ClientWidth, &ClientHeight); | |
412 | m_Cell->Layout(ClientWidth); | |
413 | } | |
414 | ||
415 | else { | |
416 | GetClientSize(&ClientWidth, &ClientHeight); | |
417 | m_Cell->Layout(ClientWidth); | |
418 | if (ClientHeight < m_Cell->GetHeight() + GetCharHeight()) | |
419 | { | |
420 | SetScrollbars( | |
421 | wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP, | |
422 | m_Cell->GetWidth() / wxHTML_SCROLL_STEP, | |
423 | (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP | |
424 | /*cheat: top-level frag is always container*/); | |
425 | } | |
426 | else /* we fit into window, no need for scrollbars */ | |
427 | { | |
428 | SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable... | |
429 | GetClientSize(&ClientWidth, &ClientHeight); | |
430 | m_Cell->Layout(ClientWidth); // ...and relayout | |
431 | } | |
432 | } | |
433 | } | |
434 | ||
435 | ||
436 | ||
437 | void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path) | |
438 | { | |
439 | wxString oldpath; | |
440 | wxString tmp; | |
441 | int p_fontsizes[7]; | |
442 | wxString p_fff, p_ffn; | |
443 | ||
444 | if (path != wxEmptyString) | |
445 | { | |
446 | oldpath = cfg->GetPath(); | |
447 | cfg->SetPath(path); | |
448 | } | |
449 | ||
450 | m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders); | |
451 | p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
452 | p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
453 | for (int i = 0; i < 7; i++) | |
454 | { | |
455 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); | |
456 | p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]); | |
457 | } | |
458 | SetFonts(p_ffn, p_fff, p_fontsizes); | |
459 | ||
460 | if (path != wxEmptyString) | |
461 | cfg->SetPath(oldpath); | |
462 | } | |
463 | ||
464 | ||
465 | ||
466 | void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path) | |
467 | { | |
468 | wxString oldpath; | |
469 | wxString tmp; | |
470 | ||
471 | if (path != wxEmptyString) | |
472 | { | |
473 | oldpath = cfg->GetPath(); | |
474 | cfg->SetPath(path); | |
475 | } | |
476 | ||
477 | cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders); | |
478 | cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed); | |
479 | cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal); | |
480 | for (int i = 0; i < 7; i++) | |
481 | { | |
482 | tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i); | |
483 | cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]); | |
484 | } | |
485 | ||
486 | if (path != wxEmptyString) | |
487 | cfg->SetPath(oldpath); | |
488 | } | |
489 | ||
490 | ||
491 | ||
492 | bool wxHtmlWindow::HistoryBack() | |
493 | { | |
494 | wxString a, l; | |
495 | ||
496 | if (m_HistoryPos < 1) return FALSE; | |
497 | ||
498 | // store scroll position into history item: | |
499 | int x, y; | |
500 | GetViewStart(&x, &y); | |
501 | (*m_History)[m_HistoryPos].SetPos(y); | |
502 | ||
503 | // go to previous position: | |
504 | m_HistoryPos--; | |
505 | ||
506 | l = (*m_History)[m_HistoryPos].GetPage(); | |
507 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
508 | m_HistoryOn = FALSE; | |
509 | m_tmpCanDrawLocks++; | |
510 | if (a == wxEmptyString) LoadPage(l); | |
511 | else LoadPage(l + wxT("#") + a); | |
512 | m_HistoryOn = TRUE; | |
513 | m_tmpCanDrawLocks--; | |
514 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); | |
515 | Refresh(); | |
516 | return TRUE; | |
517 | } | |
518 | ||
519 | bool wxHtmlWindow::HistoryCanBack() | |
520 | { | |
521 | if (m_HistoryPos < 1) return FALSE; | |
522 | return TRUE ; | |
523 | } | |
524 | ||
525 | ||
526 | bool wxHtmlWindow::HistoryForward() | |
527 | { | |
528 | wxString a, l; | |
529 | ||
530 | if (m_HistoryPos == -1) return FALSE; | |
531 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE; | |
532 | ||
533 | m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage() | |
534 | ||
535 | m_HistoryPos++; | |
536 | l = (*m_History)[m_HistoryPos].GetPage(); | |
537 | a = (*m_History)[m_HistoryPos].GetAnchor(); | |
538 | m_HistoryOn = FALSE; | |
539 | m_tmpCanDrawLocks++; | |
540 | if (a == wxEmptyString) LoadPage(l); | |
541 | else LoadPage(l + wxT("#") + a); | |
542 | m_HistoryOn = TRUE; | |
543 | m_tmpCanDrawLocks--; | |
544 | Scroll(0, (*m_History)[m_HistoryPos].GetPos()); | |
545 | Refresh(); | |
546 | return TRUE; | |
547 | } | |
548 | ||
549 | bool wxHtmlWindow::HistoryCanForward() | |
550 | { | |
551 | if (m_HistoryPos == -1) return FALSE; | |
552 | if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE; | |
553 | return TRUE ; | |
554 | } | |
555 | ||
556 | ||
557 | void wxHtmlWindow::HistoryClear() | |
558 | { | |
559 | m_History->Empty(); | |
560 | m_HistoryPos = -1; | |
561 | } | |
562 | ||
563 | void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor) | |
564 | { | |
565 | if (!m_Processors) | |
566 | { | |
567 | m_Processors = new wxHtmlProcessorList; | |
568 | m_Processors->DeleteContents(TRUE); | |
569 | } | |
570 | wxHtmlProcessorList::Node *node; | |
571 | ||
572 | for (node = m_Processors->GetFirst(); node; node = node->GetNext()) | |
573 | { | |
574 | if (processor->GetPriority() > node->GetData()->GetPriority()) | |
575 | { | |
576 | m_Processors->Insert(node, processor); | |
577 | return; | |
578 | } | |
579 | } | |
580 | m_Processors->Append(processor); | |
581 | } | |
582 | ||
583 | /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor) | |
584 | { | |
585 | if (!m_GlobalProcessors) | |
586 | { | |
587 | m_GlobalProcessors = new wxHtmlProcessorList; | |
588 | m_GlobalProcessors->DeleteContents(TRUE); | |
589 | } | |
590 | wxHtmlProcessorList::Node *node; | |
591 | ||
592 | for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext()) | |
593 | { | |
594 | if (processor->GetPriority() > node->GetData()->GetPriority()) | |
595 | { | |
596 | m_GlobalProcessors->Insert(node, processor); | |
597 | return; | |
598 | } | |
599 | } | |
600 | m_GlobalProcessors->Append(processor); | |
601 | } | |
602 | ||
603 | ||
604 | ||
605 | wxList wxHtmlWindow::m_Filters; | |
606 | wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL; | |
607 | wxCursor *wxHtmlWindow::s_cur_hand = NULL; | |
608 | wxCursor *wxHtmlWindow::s_cur_arrow = NULL; | |
609 | wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL; | |
610 | ||
611 | void wxHtmlWindow::CleanUpStatics() | |
612 | { | |
613 | wxDELETE(m_DefaultFilter); | |
614 | m_Filters.DeleteContents(TRUE); | |
615 | m_Filters.Clear(); | |
616 | wxDELETE(m_GlobalProcessors); | |
617 | wxDELETE(s_cur_hand); | |
618 | wxDELETE(s_cur_arrow); | |
619 | } | |
620 | ||
621 | ||
622 | ||
623 | void wxHtmlWindow::AddFilter(wxHtmlFilter *filter) | |
624 | { | |
625 | m_Filters.Append(filter); | |
626 | } | |
627 | ||
628 | ||
629 | bool wxHtmlWindow::IsSelectionEnabled() const | |
630 | { | |
631 | #if wxUSE_CLIPBOARD | |
632 | return !(m_Style & wxHW_NO_SELECTION); | |
633 | #else | |
634 | return false; | |
635 | #endif | |
636 | } | |
637 | ||
638 | ||
639 | void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link) | |
640 | { | |
641 | const wxMouseEvent *e = link.GetEvent(); | |
642 | if (e == NULL || e->LeftUp()) | |
643 | LoadPage(link.GetHref()); | |
644 | } | |
645 | ||
646 | void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell, | |
647 | wxCoord x, wxCoord y, | |
648 | const wxMouseEvent& event) | |
649 | { | |
650 | wxCHECK_RET( cell, _T("can't be called with NULL cell") ); | |
651 | ||
652 | cell->OnMouseClick(this, x, y, event); | |
653 | } | |
654 | ||
655 | void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell), | |
656 | wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) | |
657 | { | |
658 | // do nothing here | |
659 | } | |
660 | ||
661 | void wxHtmlWindow::OnDraw(wxDC& dc) | |
662 | { | |
663 | if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return; | |
664 | ||
665 | int x, y; | |
666 | wxRect rect = GetUpdateRegion().GetBox(); | |
667 | ||
668 | dc.SetMapMode(wxMM_TEXT); | |
669 | dc.SetBackgroundMode(wxTRANSPARENT); | |
670 | GetViewStart(&x, &y); | |
671 | ||
672 | wxHtmlRenderingState rstate(m_selection); | |
673 | m_Cell->Draw(dc, 0, 0, | |
674 | y * wxHTML_SCROLL_STEP + rect.GetTop(), | |
675 | y * wxHTML_SCROLL_STEP + rect.GetBottom(), | |
676 | rstate); | |
677 | } | |
678 | ||
679 | ||
680 | ||
681 | ||
682 | void wxHtmlWindow::OnSize(wxSizeEvent& event) | |
683 | { | |
684 | wxScrolledWindow::OnSize(event); | |
685 | CreateLayout(); | |
686 | Refresh(); | |
687 | } | |
688 | ||
689 | ||
690 | void wxHtmlWindow::OnMouseMove(wxMouseEvent& event) | |
691 | { | |
692 | m_tmpMouseMoved = true; | |
693 | } | |
694 | ||
695 | void wxHtmlWindow::OnMouseDown(wxMouseEvent& event) | |
696 | { | |
697 | if ( event.LeftDown() && IsSelectionEnabled() ) | |
698 | { | |
699 | m_makingSelection = true; | |
700 | ||
701 | if ( m_selection ) | |
702 | { | |
703 | wxDELETE(m_selection); | |
704 | Refresh(); | |
705 | } | |
706 | m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition()); | |
707 | m_tmpSelFromCell = NULL; | |
708 | ||
709 | CaptureMouse(); | |
710 | } | |
711 | } | |
712 | ||
713 | void wxHtmlWindow::OnMouseUp(wxMouseEvent& event) | |
714 | { | |
715 | if ( m_makingSelection ) | |
716 | { | |
717 | ReleaseMouse(); | |
718 | m_makingSelection = false; | |
719 | ||
720 | // did the user move the mouse far enough from starting point? | |
721 | if ( m_selection ) | |
722 | { | |
723 | // we don't want mouse up event that ended selecting to be | |
724 | // handled as mouse click and e.g. follow hyperlink: | |
725 | return; | |
726 | } | |
727 | } | |
728 | ||
729 | SetFocus(); | |
730 | if ( m_Cell ) | |
731 | { | |
732 | wxPoint pos = CalcUnscrolledPosition(event.GetPosition()); | |
733 | wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y); | |
734 | ||
735 | // VZ: is it possible that we don't find anything at all? | |
736 | // VS: yes. FindCellByPos returns terminal cell and | |
737 | // containers may have empty borders | |
738 | if ( cell ) | |
739 | OnCellClicked(cell, pos.x, pos.y, event); | |
740 | } | |
741 | } | |
742 | ||
743 | ||
744 | ||
745 | void wxHtmlWindow::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
746 | { | |
747 | if (s_cur_hand == NULL) | |
748 | { | |
749 | s_cur_hand = new wxCursor(wxCURSOR_HAND); | |
750 | s_cur_arrow = new wxCursor(wxCURSOR_ARROW); | |
751 | } | |
752 | ||
753 | if (m_tmpMouseMoved && (m_Cell != NULL)) | |
754 | { | |
755 | int xc, yc, x, y; | |
756 | wxGetMousePosition(&xc, &yc); | |
757 | ScreenToClient(&xc, &yc); | |
758 | CalcUnscrolledPosition(xc, yc, &x, &y); | |
759 | ||
760 | wxHtmlCell *cell = m_Cell->FindCellByPos(x, y); | |
761 | ||
762 | // handle selection update: | |
763 | if ( m_makingSelection ) | |
764 | { | |
765 | bool goingDown = m_tmpSelFromPos.y < y || | |
766 | m_tmpSelFromPos.y == y && m_tmpSelFromPos.x < x; | |
767 | ||
768 | if ( !m_tmpSelFromCell ) | |
769 | { | |
770 | if (goingDown) | |
771 | { | |
772 | m_tmpSelFromCell = m_Cell->FindCellByPos(x, y, | |
773 | wxHTML_FIND_NEAREST_BEFORE); | |
774 | if (!m_tmpSelFromCell) | |
775 | m_tmpSelFromCell = m_Cell->GetFirstTerminal(); | |
776 | } | |
777 | else | |
778 | { | |
779 | m_tmpSelFromCell = m_Cell->FindCellByPos(x, y, | |
780 | wxHTML_FIND_NEAREST_AFTER); | |
781 | if (!m_tmpSelFromCell) | |
782 | m_tmpSelFromCell = m_Cell->GetLastTerminal(); | |
783 | } | |
784 | } | |
785 | ||
786 | wxHtmlCell *selcell = cell; | |
787 | if (!selcell) | |
788 | { | |
789 | if (goingDown) | |
790 | { | |
791 | selcell = m_Cell->FindCellByPos(x, y, | |
792 | wxHTML_FIND_NEAREST_AFTER); | |
793 | if (!selcell) | |
794 | selcell = m_Cell->GetLastTerminal(); | |
795 | } | |
796 | else | |
797 | { | |
798 | selcell = m_Cell->FindCellByPos(x, y, | |
799 | wxHTML_FIND_NEAREST_BEFORE); | |
800 | if (!selcell) | |
801 | selcell = m_Cell->GetFirstTerminal(); | |
802 | } | |
803 | } | |
804 | ||
805 | // NB: it may *rarely* happen that the code above didn't find one | |
806 | // of the cells, e.g. if wxHtmlWindow doesn't contain any | |
807 | // visible cells. | |
808 | if ( selcell && m_tmpSelFromCell ) | |
809 | { | |
810 | if ( !m_selection ) | |
811 | { | |
812 | // start selecting only if mouse movement was big enough | |
813 | // (otherwise it was meant as mouse click, not selection): | |
814 | const int PRECISION = 2; | |
815 | wxPoint diff = m_tmpSelFromPos - wxPoint(x,y); | |
816 | if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION) | |
817 | { | |
818 | m_selection = new wxHtmlSelection(); | |
819 | } | |
820 | } | |
821 | if ( m_selection ) | |
822 | { | |
823 | if (goingDown) | |
824 | { | |
825 | m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell, | |
826 | wxPoint(x,y), selcell); | |
827 | } | |
828 | else | |
829 | { | |
830 | m_selection->Set(wxPoint(x,y), selcell, | |
831 | m_tmpSelFromPos, m_tmpSelFromCell); | |
832 | } | |
833 | Refresh(); // FIXME - optimize! | |
834 | } | |
835 | } | |
836 | } | |
837 | ||
838 | // handle cursor and status bar text changes: | |
839 | if ( cell != m_tmpLastCell ) | |
840 | { | |
841 | wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL; | |
842 | ||
843 | if (lnk != m_tmpLastLink) | |
844 | { | |
845 | if (lnk == NULL) | |
846 | { | |
847 | SetCursor(*s_cur_arrow); | |
848 | if (m_RelatedStatusBar != -1) | |
849 | m_RelatedFrame->SetStatusText(wxEmptyString, m_RelatedStatusBar); | |
850 | } | |
851 | else | |
852 | { | |
853 | SetCursor(*s_cur_hand); | |
854 | if (m_RelatedStatusBar != -1) | |
855 | m_RelatedFrame->SetStatusText(lnk->GetHref(), m_RelatedStatusBar); | |
856 | } | |
857 | m_tmpLastLink = lnk; | |
858 | } | |
859 | ||
860 | m_tmpLastCell = cell; | |
861 | } | |
862 | else // mouse moved but stayed in the same cell | |
863 | { | |
864 | if ( cell ) | |
865 | OnCellMouseHover(cell, x, y); | |
866 | } | |
867 | ||
868 | m_tmpMouseMoved = FALSE; | |
869 | } | |
870 | } | |
871 | ||
872 | ||
873 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject) | |
874 | ||
875 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow) | |
876 | ||
877 | BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow) | |
878 | EVT_SIZE(wxHtmlWindow::OnSize) | |
879 | EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown) | |
880 | EVT_LEFT_UP(wxHtmlWindow::OnMouseUp) | |
881 | EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp) | |
882 | EVT_MOTION(wxHtmlWindow::OnMouseMove) | |
883 | EVT_IDLE(wxHtmlWindow::OnIdle) | |
884 | END_EVENT_TABLE() | |
885 | ||
886 | ||
887 | ||
888 | ||
889 | ||
890 | // A module to allow initialization/cleanup | |
891 | // without calling these functions from app.cpp or from | |
892 | // the user's application. | |
893 | ||
894 | class wxHtmlWinModule: public wxModule | |
895 | { | |
896 | DECLARE_DYNAMIC_CLASS(wxHtmlWinModule) | |
897 | public: | |
898 | wxHtmlWinModule() : wxModule() {} | |
899 | bool OnInit() { return TRUE; } | |
900 | void OnExit() { wxHtmlWindow::CleanUpStatics(); } | |
901 | }; | |
902 | ||
903 | IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule) | |
904 | ||
905 | ||
906 | // This hack forces the linker to always link in m_* files | |
907 | // (wxHTML doesn't work without handlers from these files) | |
908 | #include "wx/html/forcelnk.h" | |
909 | FORCE_WXHTML_MODULES() | |
910 | ||
911 | #endif |