]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlwin.cpp
for built-in dialogs, there is no m_peer instance, and still we might have a Refresh...
[wxWidgets.git] / src / html / htmlwin.cpp
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/clipbrd.h"
36 #include "wx/dataobj.h"
37 #include "wx/timer.h"
38 #include "wx/dcmemory.h"
39
40 #include "wx/arrimpl.cpp"
41 #include "wx/listimpl.cpp"
42
43
44
45 #if wxUSE_CLIPBOARD
46 // ----------------------------------------------------------------------------
47 // wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
48 // events when a captured mouse is held outside the window
49 // ----------------------------------------------------------------------------
50
51 class wxHtmlWinAutoScrollTimer : public wxTimer
52 {
53 public:
54 wxHtmlWinAutoScrollTimer(wxScrolledWindow *win,
55 wxEventType eventTypeToSend,
56 int pos, int orient)
57 {
58 m_win = win;
59 m_eventType = eventTypeToSend;
60 m_pos = pos;
61 m_orient = orient;
62 }
63
64 virtual void Notify();
65
66 private:
67 wxScrolledWindow *m_win;
68 wxEventType m_eventType;
69 int m_pos,
70 m_orient;
71
72 DECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer)
73 };
74
75 void wxHtmlWinAutoScrollTimer::Notify()
76 {
77 // only do all this as long as the window is capturing the mouse
78 if ( wxWindow::GetCapture() != m_win )
79 {
80 Stop();
81 }
82 else // we still capture the mouse, continue generating events
83 {
84 // first scroll the window if we are allowed to do it
85 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
86 event1.SetEventObject(m_win);
87 if ( m_win->GetEventHandler()->ProcessEvent(event1) )
88 {
89 // and then send a pseudo mouse-move event to refresh the selection
90 wxMouseEvent event2(wxEVT_MOTION);
91 wxGetMousePosition(&event2.m_x, &event2.m_y);
92
93 // the mouse event coordinates should be client, not screen as
94 // returned by wxGetMousePosition
95 wxWindow *parentTop = m_win;
96 while ( parentTop->GetParent() )
97 parentTop = parentTop->GetParent();
98 wxPoint ptOrig = parentTop->GetPosition();
99 event2.m_x -= ptOrig.x;
100 event2.m_y -= ptOrig.y;
101
102 event2.SetEventObject(m_win);
103
104 // FIXME: we don't fill in the other members - ok?
105 m_win->GetEventHandler()->ProcessEvent(event2);
106 }
107 else // can't scroll further, stop
108 {
109 Stop();
110 }
111 }
112 }
113
114 #endif // wxUSE_CLIPBOARD
115
116
117
118 //-----------------------------------------------------------------------------
119 // wxHtmlHistoryItem
120 //-----------------------------------------------------------------------------
121
122 // item of history list
123 class WXDLLIMPEXP_HTML wxHtmlHistoryItem
124 {
125 public:
126 wxHtmlHistoryItem(const wxString& p, const wxString& a) {m_Page = p, m_Anchor = a, m_Pos = 0;}
127 int GetPos() const {return m_Pos;}
128 void SetPos(int p) {m_Pos = p;}
129 const wxString& GetPage() const {return m_Page;}
130 const wxString& GetAnchor() const {return m_Anchor;}
131
132 private:
133 wxString m_Page;
134 wxString m_Anchor;
135 int m_Pos;
136 };
137
138
139 //-----------------------------------------------------------------------------
140 // our private arrays:
141 //-----------------------------------------------------------------------------
142
143 WX_DECLARE_OBJARRAY(wxHtmlHistoryItem, wxHtmlHistoryArray);
144 WX_DEFINE_OBJARRAY(wxHtmlHistoryArray);
145
146 WX_DECLARE_LIST(wxHtmlProcessor, wxHtmlProcessorList);
147 WX_DEFINE_LIST(wxHtmlProcessorList);
148
149 //-----------------------------------------------------------------------------
150 // wxHtmlWindow
151 //-----------------------------------------------------------------------------
152
153
154 void wxHtmlWindow::Init()
155 {
156 m_tmpMouseMoved = FALSE;
157 m_tmpLastLink = NULL;
158 m_tmpLastCell = NULL;
159 m_tmpCanDrawLocks = 0;
160 m_FS = new wxFileSystem();
161 #if wxUSE_STATUSBAR
162 m_RelatedStatusBar = -1;
163 #endif // wxUSE_STATUSBAR
164 m_RelatedFrame = NULL;
165 m_TitleFormat = wxT("%s");
166 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
167 m_Cell = NULL;
168 m_Parser = new wxHtmlWinParser(this);
169 m_Parser->SetFS(m_FS);
170 m_HistoryPos = -1;
171 m_HistoryOn = TRUE;
172 m_History = new wxHtmlHistoryArray;
173 m_Processors = NULL;
174 m_Style = 0;
175 SetBorders(10);
176 m_selection = NULL;
177 m_makingSelection = false;
178 #if wxUSE_CLIPBOARD
179 m_timerAutoScroll = NULL;
180 m_lastDoubleClick = 0;
181 #endif // wxUSE_CLIPBOARD
182 m_backBuffer = NULL;
183 }
184
185 bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
186 const wxPoint& pos, const wxSize& size,
187 long style, const wxString& name)
188 {
189 if (!wxScrolledWindow::Create(parent, id, pos, size,
190 style | wxVSCROLL | wxHSCROLL,
191 name))
192 return FALSE;
193
194 m_Style = style;
195 SetPage(wxT("<html><body></body></html>"));
196 return TRUE;
197 }
198
199
200 wxHtmlWindow::~wxHtmlWindow()
201 {
202 #if wxUSE_CLIPBOARD
203 StopAutoScrolling();
204 #endif // wxUSE_CLIPBOARD
205 HistoryClear();
206
207 delete m_selection;
208
209 delete m_Cell;
210
211 if ( m_Processors )
212 {
213 WX_CLEAR_LIST(wxHtmlProcessorList, *m_Processors);
214 }
215
216 delete m_Parser;
217 delete m_FS;
218 delete m_History;
219 delete m_Processors;
220 delete m_backBuffer;
221 }
222
223
224
225 void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
226 {
227 m_RelatedFrame = frame;
228 m_TitleFormat = format;
229 }
230
231
232
233 #if wxUSE_STATUSBAR
234 void wxHtmlWindow::SetRelatedStatusBar(int bar)
235 {
236 m_RelatedStatusBar = bar;
237 }
238 #endif // wxUSE_STATUSBAR
239
240
241
242 void wxHtmlWindow::SetFonts(wxString normal_face, wxString fixed_face, const int *sizes)
243 {
244 wxString op = m_OpenedPage;
245
246 m_Parser->SetFonts(normal_face, fixed_face, sizes);
247 // fonts changed => contents invalid, so reload the page:
248 SetPage(wxT("<html><body></body></html>"));
249 if (!op.IsEmpty()) LoadPage(op);
250 }
251
252
253
254 bool wxHtmlWindow::SetPage(const wxString& source)
255 {
256 wxString newsrc(source);
257
258 wxDELETE(m_selection);
259
260 // pass HTML through registered processors:
261 if (m_Processors || m_GlobalProcessors)
262 {
263 wxHtmlProcessorList::compatibility_iterator nodeL, nodeG;
264 int prL, prG;
265
266 nodeL = (m_Processors) ? m_Processors->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
267 nodeG = (m_GlobalProcessors) ? m_GlobalProcessors->GetFirst() : wxHtmlProcessorList::compatibility_iterator();
268
269 // VS: there are two lists, global and local, both of them sorted by
270 // priority. Since we have to go through _both_ lists with
271 // decreasing priority, we "merge-sort" the lists on-line by
272 // processing that one of the two heads that has higher priority
273 // in every iteration
274 while (nodeL || nodeG)
275 {
276 prL = (nodeL) ? nodeL->GetData()->GetPriority() : -1;
277 prG = (nodeG) ? nodeG->GetData()->GetPriority() : -1;
278 if (prL > prG)
279 {
280 if (nodeL->GetData()->IsEnabled())
281 newsrc = nodeL->GetData()->Process(newsrc);
282 nodeL = nodeL->GetNext();
283 }
284 else // prL <= prG
285 {
286 if (nodeG->GetData()->IsEnabled())
287 newsrc = nodeG->GetData()->Process(newsrc);
288 nodeG = nodeG->GetNext();
289 }
290 }
291 }
292
293 // ...and run the parser on it:
294 wxClientDC *dc = new wxClientDC(this);
295 dc->SetMapMode(wxMM_TEXT);
296 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
297 m_OpenedPage = m_OpenedAnchor = m_OpenedPageTitle = wxEmptyString;
298 m_Parser->SetDC(dc);
299 if (m_Cell)
300 {
301 delete m_Cell;
302 m_Cell = NULL;
303 }
304 m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
305 delete dc;
306 m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
307 m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
308 CreateLayout();
309 if (m_tmpCanDrawLocks == 0)
310 Refresh();
311 return TRUE;
312 }
313
314 bool wxHtmlWindow::AppendToPage(const wxString& source)
315 {
316 return SetPage(*(GetParser()->GetSource()) + source);
317 }
318
319 bool wxHtmlWindow::LoadPage(const wxString& location)
320 {
321 wxBusyCursor busyCursor;
322
323 wxFSFile *f;
324 bool rt_val;
325 bool needs_refresh = FALSE;
326
327 m_tmpCanDrawLocks++;
328 if (m_HistoryOn && (m_HistoryPos != -1))
329 {
330 // store scroll position into history item:
331 int x, y;
332 GetViewStart(&x, &y);
333 (*m_History)[m_HistoryPos].SetPos(y);
334 }
335
336 if (location[0] == wxT('#'))
337 {
338 // local anchor:
339 wxString anch = location.Mid(1) /*1 to end*/;
340 m_tmpCanDrawLocks--;
341 rt_val = ScrollToAnchor(anch);
342 m_tmpCanDrawLocks++;
343 }
344 else if (location.Find(wxT('#')) != wxNOT_FOUND && location.BeforeFirst(wxT('#')) == m_OpenedPage)
345 {
346 wxString anch = location.AfterFirst(wxT('#'));
347 m_tmpCanDrawLocks--;
348 rt_val = ScrollToAnchor(anch);
349 m_tmpCanDrawLocks++;
350 }
351 else if (location.Find(wxT('#')) != wxNOT_FOUND &&
352 (m_FS->GetPath() + location.BeforeFirst(wxT('#'))) == m_OpenedPage)
353 {
354 wxString anch = location.AfterFirst(wxT('#'));
355 m_tmpCanDrawLocks--;
356 rt_val = ScrollToAnchor(anch);
357 m_tmpCanDrawLocks++;
358 }
359
360 else
361 {
362 needs_refresh = true;
363 #if wxUSE_STATUSBAR
364 // load&display it:
365 if (m_RelatedStatusBar != -1)
366 {
367 m_RelatedFrame->SetStatusText(_("Connecting..."), m_RelatedStatusBar);
368 Refresh(false);
369 }
370 #endif // wxUSE_STATUSBAR
371
372 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
373
374 // try to interpret 'location' as filename instead of URL:
375 if (f == NULL)
376 {
377 wxFileName fn(location);
378 wxString location2 = wxFileSystem::FileNameToURL(fn);
379 f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
380 }
381
382 if (f == NULL)
383 {
384 wxLogError(_("Unable to open requested HTML document: %s"), location.c_str());
385 m_tmpCanDrawLocks--;
386 return false;
387 }
388
389 else
390 {
391 wxList::compatibility_iterator node;
392 wxString src = wxEmptyString;
393
394 #if wxUSE_STATUSBAR
395 if (m_RelatedStatusBar != -1)
396 {
397 wxString msg = _("Loading : ") + location;
398 m_RelatedFrame->SetStatusText(msg, m_RelatedStatusBar);
399 Refresh(false);
400 }
401 #endif // wxUSE_STATUSBAR
402
403 node = m_Filters.GetFirst();
404 while (node)
405 {
406 wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
407 if (h->CanRead(*f))
408 {
409 src = h->ReadFile(*f);
410 break;
411 }
412 node = node->GetNext();
413 }
414 if (src == wxEmptyString)
415 {
416 if (m_DefaultFilter == NULL) m_DefaultFilter = GetDefaultFilter();
417 src = m_DefaultFilter->ReadFile(*f);
418 }
419
420 m_FS->ChangePathTo(f->GetLocation());
421 rt_val = SetPage(src);
422 m_OpenedPage = f->GetLocation();
423 if (f->GetAnchor() != wxEmptyString)
424 {
425 ScrollToAnchor(f->GetAnchor());
426 }
427
428 delete f;
429
430 #if wxUSE_STATUSBAR
431 if (m_RelatedStatusBar != -1)
432 m_RelatedFrame->SetStatusText(_("Done"), m_RelatedStatusBar);
433 #endif // wxUSE_STATUSBAR
434 }
435 }
436
437 if (m_HistoryOn) // add this page to history there:
438 {
439 int c = m_History->GetCount() - (m_HistoryPos + 1);
440
441 if (m_HistoryPos < 0 ||
442 (*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
443 (*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
444 {
445 m_HistoryPos++;
446 for (int i = 0; i < c; i++)
447 m_History->RemoveAt(m_HistoryPos);
448 m_History->Add(new wxHtmlHistoryItem(m_OpenedPage, m_OpenedAnchor));
449 }
450 }
451
452 if (m_OpenedPageTitle == wxEmptyString)
453 OnSetTitle(wxFileNameFromPath(m_OpenedPage));
454
455 if (needs_refresh)
456 {
457 m_tmpCanDrawLocks--;
458 Refresh();
459 }
460 else
461 m_tmpCanDrawLocks--;
462
463 return rt_val;
464 }
465
466
467 bool wxHtmlWindow::LoadFile(const wxFileName& filename)
468 {
469 wxString url = wxFileSystem::FileNameToURL(filename);
470 return LoadPage(url);
471 }
472
473
474 bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
475 {
476 const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
477 if (!c)
478 {
479 wxLogWarning(_("HTML anchor %s does not exist."), anchor.c_str());
480 return FALSE;
481 }
482 else
483 {
484 int y;
485
486 for (y = 0; c != NULL; c = c->GetParent()) y += c->GetPosY();
487 Scroll(-1, y / wxHTML_SCROLL_STEP);
488 m_OpenedAnchor = anchor;
489 return TRUE;
490 }
491 }
492
493
494 void wxHtmlWindow::OnSetTitle(const wxString& title)
495 {
496 if (m_RelatedFrame)
497 {
498 wxString tit;
499 tit.Printf(m_TitleFormat, title.c_str());
500 m_RelatedFrame->SetTitle(tit);
501 }
502 m_OpenedPageTitle = title;
503 }
504
505
506
507
508
509 void wxHtmlWindow::CreateLayout()
510 {
511 int ClientWidth, ClientHeight;
512
513 if (!m_Cell) return;
514
515 if (m_Style & wxHW_SCROLLBAR_NEVER)
516 {
517 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // always off
518 GetClientSize(&ClientWidth, &ClientHeight);
519 m_Cell->Layout(ClientWidth);
520 }
521
522 else {
523 GetClientSize(&ClientWidth, &ClientHeight);
524 m_Cell->Layout(ClientWidth);
525 if (ClientHeight < m_Cell->GetHeight() + GetCharHeight())
526 {
527 SetScrollbars(
528 wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP,
529 m_Cell->GetWidth() / wxHTML_SCROLL_STEP,
530 (m_Cell->GetHeight() + GetCharHeight()) / wxHTML_SCROLL_STEP
531 /*cheat: top-level frag is always container*/);
532 }
533 else /* we fit into window, no need for scrollbars */
534 {
535 SetScrollbars(wxHTML_SCROLL_STEP, 1, m_Cell->GetWidth() / wxHTML_SCROLL_STEP, 0); // disable...
536 GetClientSize(&ClientWidth, &ClientHeight);
537 m_Cell->Layout(ClientWidth); // ...and relayout
538 }
539 }
540 }
541
542
543
544 void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
545 {
546 wxString oldpath;
547 wxString tmp;
548 int p_fontsizes[7];
549 wxString p_fff, p_ffn;
550
551 if (path != wxEmptyString)
552 {
553 oldpath = cfg->GetPath();
554 cfg->SetPath(path);
555 }
556
557 m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
558 p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
559 p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
560 for (int i = 0; i < 7; i++)
561 {
562 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
563 p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
564 }
565 SetFonts(p_ffn, p_fff, p_fontsizes);
566
567 if (path != wxEmptyString)
568 cfg->SetPath(oldpath);
569 }
570
571
572
573 void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
574 {
575 wxString oldpath;
576 wxString tmp;
577
578 if (path != wxEmptyString)
579 {
580 oldpath = cfg->GetPath();
581 cfg->SetPath(path);
582 }
583
584 cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
585 cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
586 cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
587 for (int i = 0; i < 7; i++)
588 {
589 tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
590 cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
591 }
592
593 if (path != wxEmptyString)
594 cfg->SetPath(oldpath);
595 }
596
597
598
599 bool wxHtmlWindow::HistoryBack()
600 {
601 wxString a, l;
602
603 if (m_HistoryPos < 1) return FALSE;
604
605 // store scroll position into history item:
606 int x, y;
607 GetViewStart(&x, &y);
608 (*m_History)[m_HistoryPos].SetPos(y);
609
610 // go to previous position:
611 m_HistoryPos--;
612
613 l = (*m_History)[m_HistoryPos].GetPage();
614 a = (*m_History)[m_HistoryPos].GetAnchor();
615 m_HistoryOn = FALSE;
616 m_tmpCanDrawLocks++;
617 if (a == wxEmptyString) LoadPage(l);
618 else LoadPage(l + wxT("#") + a);
619 m_HistoryOn = TRUE;
620 m_tmpCanDrawLocks--;
621 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
622 Refresh();
623 return TRUE;
624 }
625
626 bool wxHtmlWindow::HistoryCanBack()
627 {
628 if (m_HistoryPos < 1) return FALSE;
629 return TRUE ;
630 }
631
632
633 bool wxHtmlWindow::HistoryForward()
634 {
635 wxString a, l;
636
637 if (m_HistoryPos == -1) return FALSE;
638 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
639
640 m_OpenedPage = wxEmptyString; // this will disable adding new entry into history in LoadPage()
641
642 m_HistoryPos++;
643 l = (*m_History)[m_HistoryPos].GetPage();
644 a = (*m_History)[m_HistoryPos].GetAnchor();
645 m_HistoryOn = FALSE;
646 m_tmpCanDrawLocks++;
647 if (a == wxEmptyString) LoadPage(l);
648 else LoadPage(l + wxT("#") + a);
649 m_HistoryOn = TRUE;
650 m_tmpCanDrawLocks--;
651 Scroll(0, (*m_History)[m_HistoryPos].GetPos());
652 Refresh();
653 return TRUE;
654 }
655
656 bool wxHtmlWindow::HistoryCanForward()
657 {
658 if (m_HistoryPos == -1) return FALSE;
659 if (m_HistoryPos >= (int)m_History->GetCount() - 1)return FALSE;
660 return TRUE ;
661 }
662
663
664 void wxHtmlWindow::HistoryClear()
665 {
666 m_History->Empty();
667 m_HistoryPos = -1;
668 }
669
670 void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
671 {
672 if (!m_Processors)
673 {
674 m_Processors = new wxHtmlProcessorList;
675 }
676 wxHtmlProcessorList::compatibility_iterator node;
677
678 for (node = m_Processors->GetFirst(); node; node = node->GetNext())
679 {
680 if (processor->GetPriority() > node->GetData()->GetPriority())
681 {
682 m_Processors->Insert(node, processor);
683 return;
684 }
685 }
686 m_Processors->Append(processor);
687 }
688
689 /*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
690 {
691 if (!m_GlobalProcessors)
692 {
693 m_GlobalProcessors = new wxHtmlProcessorList;
694 }
695 wxHtmlProcessorList::compatibility_iterator node;
696
697 for (node = m_GlobalProcessors->GetFirst(); node; node = node->GetNext())
698 {
699 if (processor->GetPriority() > node->GetData()->GetPriority())
700 {
701 m_GlobalProcessors->Insert(node, processor);
702 return;
703 }
704 }
705 m_GlobalProcessors->Append(processor);
706 }
707
708
709
710 wxList wxHtmlWindow::m_Filters;
711 wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = NULL;
712 wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = NULL;
713
714 void wxHtmlWindow::CleanUpStatics()
715 {
716 wxDELETE(m_DefaultFilter);
717 WX_CLEAR_LIST(wxList, m_Filters);
718 if (m_GlobalProcessors)
719 WX_CLEAR_LIST(wxHtmlProcessorList, *m_GlobalProcessors);
720 wxDELETE(m_GlobalProcessors);
721 }
722
723
724
725 void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
726 {
727 m_Filters.Append(filter);
728 }
729
730
731 bool wxHtmlWindow::IsSelectionEnabled() const
732 {
733 #if wxUSE_CLIPBOARD
734 return !(m_Style & wxHW_NO_SELECTION);
735 #else
736 return false;
737 #endif
738 }
739
740
741 #if wxUSE_CLIPBOARD
742 wxString wxHtmlWindow::DoSelectionToText(wxHtmlSelection *sel)
743 {
744 if ( !sel )
745 return wxEmptyString;
746
747 wxClientDC dc(this);
748
749 const wxHtmlCell *end = sel->GetToCell();
750 wxString text;
751 wxHtmlTerminalCellsInterator i(sel->GetFromCell(), end);
752 if ( i )
753 {
754 text << i->ConvertToText(sel);
755 ++i;
756 }
757 const wxHtmlCell *prev = *i;
758 while ( i )
759 {
760 if ( prev->GetParent() != i->GetParent() )
761 text << _T('\n');
762 text << i->ConvertToText(*i == end ? sel : NULL);
763 prev = *i;
764 ++i;
765 }
766 return text;
767 }
768
769 wxString wxHtmlWindow::ToText()
770 {
771 if (m_Cell)
772 {
773 wxHtmlSelection sel;
774 sel.Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
775 return DoSelectionToText(&sel);
776 }
777 else
778 return wxEmptyString;
779 }
780
781 #endif // wxUSE_CLIPBOARD
782
783 bool wxHtmlWindow::CopySelection(ClipboardType t)
784 {
785 #if wxUSE_CLIPBOARD
786 if ( m_selection )
787 {
788 #ifdef __UNIX__
789 wxTheClipboard->UsePrimarySelection(t == Primary);
790 #else // !__UNIX__
791 // Primary selection exists only under X11, so don't do anything under
792 // the other platforms when we try to access it
793 //
794 // TODO: this should be abstracted at wxClipboard level!
795 if ( t == Primary )
796 return false;
797 #endif // __UNIX__/!__UNIX__
798
799 if ( wxTheClipboard->Open() )
800 {
801 const wxString txt(SelectionToText());
802 wxTheClipboard->SetData(new wxTextDataObject(txt));
803 wxTheClipboard->Close();
804 wxLogTrace(_T("wxhtmlselection"),
805 _("Copied to clipboard:\"%s\""), txt.c_str());
806
807 return true;
808 }
809 }
810 #endif // wxUSE_CLIPBOARD
811
812 return false;
813 }
814
815
816 void wxHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
817 {
818 const wxMouseEvent *e = link.GetEvent();
819 if (e == NULL || e->LeftUp())
820 LoadPage(link.GetHref());
821 }
822
823 void wxHtmlWindow::OnCellClicked(wxHtmlCell *cell,
824 wxCoord x, wxCoord y,
825 const wxMouseEvent& event)
826 {
827 wxCHECK_RET( cell, _T("can't be called with NULL cell") );
828
829 cell->OnMouseClick(this, x, y, event);
830 }
831
832 void wxHtmlWindow::OnCellMouseHover(wxHtmlCell * WXUNUSED(cell),
833 wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
834 {
835 // do nothing here
836 }
837
838 void wxHtmlWindow::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
839 {
840 }
841
842 void wxHtmlWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
843 {
844 wxPaintDC dc(this);
845
846 if (m_tmpCanDrawLocks > 0 || m_Cell == NULL) return;
847
848 int x, y;
849 GetViewStart(&x, &y);
850 wxRect rect = GetUpdateRegion().GetBox();
851 wxSize sz = GetSize();
852
853 wxMemoryDC dcm;
854 if ( !m_backBuffer )
855 m_backBuffer = new wxBitmap(sz.x, sz.y);
856 dcm.SelectObject(*m_backBuffer);
857 dcm.SetBackground(wxBrush(GetBackgroundColour(), wxSOLID));
858 dcm.Clear();
859 PrepareDC(dcm);
860 dcm.SetMapMode(wxMM_TEXT);
861 dcm.SetBackgroundMode(wxTRANSPARENT);
862
863 wxHtmlRenderingInfo rinfo;
864 wxDefaultHtmlRenderingStyle rstyle;
865 rinfo.SetSelection(m_selection);
866 rinfo.SetStyle(&rstyle);
867 m_Cell->Draw(dcm, 0, 0,
868 y * wxHTML_SCROLL_STEP + rect.GetTop(),
869 y * wxHTML_SCROLL_STEP + rect.GetBottom(),
870 rinfo);
871
872 //#define DEBUG_HTML_SELECTION
873 #ifdef DEBUG_HTML_SELECTION
874 {
875 int xc, yc, x, y;
876 wxGetMousePosition(&xc, &yc);
877 ScreenToClient(&xc, &yc);
878 CalcUnscrolledPosition(xc, yc, &x, &y);
879 wxHtmlCell *at = m_Cell->FindCellByPos(x, y);
880 wxHtmlCell *before =
881 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_BEFORE);
882 wxHtmlCell *after =
883 m_Cell->FindCellByPos(x, y, wxHTML_FIND_NEAREST_AFTER);
884
885 dcm.SetBrush(*wxTRANSPARENT_BRUSH);
886 dcm.SetPen(*wxBLACK_PEN);
887 if (at)
888 dcm.DrawRectangle(at->GetAbsPos(),
889 wxSize(at->GetWidth(),at->GetHeight()));
890 dcm.SetPen(*wxGREEN_PEN);
891 if (before)
892 dcm.DrawRectangle(before->GetAbsPos().x+1, before->GetAbsPos().y+1,
893 before->GetWidth()-2,before->GetHeight()-2);
894 dcm.SetPen(*wxRED_PEN);
895 if (after)
896 dcm.DrawRectangle(after->GetAbsPos().x+2, after->GetAbsPos().y+2,
897 after->GetWidth()-4,after->GetHeight()-4);
898 }
899 #endif
900
901 dcm.SetDeviceOrigin(0,0);
902 dc.Blit(0, rect.GetTop(),
903 sz.x, rect.GetBottom() - rect.GetTop() + 1,
904 &dcm,
905 0, rect.GetTop());
906 }
907
908
909
910
911 void wxHtmlWindow::OnSize(wxSizeEvent& event)
912 {
913 wxDELETE(m_backBuffer);
914
915 wxScrolledWindow::OnSize(event);
916 CreateLayout();
917
918 // Recompute selection if necessary:
919 if ( m_selection )
920 {
921 m_selection->Set(m_selection->GetFromCell(),
922 m_selection->GetToCell());
923 m_selection->ClearPrivPos();
924 }
925
926 Refresh();
927 }
928
929
930 void wxHtmlWindow::OnMouseMove(wxMouseEvent& WXUNUSED(event))
931 {
932 m_tmpMouseMoved = true;
933 }
934
935 void wxHtmlWindow::OnMouseDown(wxMouseEvent& event)
936 {
937 #if wxUSE_CLIPBOARD
938 if ( event.LeftDown() && IsSelectionEnabled() )
939 {
940 const long TRIPLECLICK_LEN = 200; // 0.2 sec after doubleclick
941 if ( wxGetLocalTimeMillis() - m_lastDoubleClick <= TRIPLECLICK_LEN )
942 {
943 SelectLine(CalcUnscrolledPosition(event.GetPosition()));
944
945 (void) CopySelection();
946 }
947 else
948 {
949 m_makingSelection = true;
950
951 if ( m_selection )
952 {
953 wxDELETE(m_selection);
954 Refresh();
955 }
956 m_tmpSelFromPos = CalcUnscrolledPosition(event.GetPosition());
957 m_tmpSelFromCell = NULL;
958
959 CaptureMouse();
960 }
961 }
962 #endif // wxUSE_CLIPBOARD
963 }
964
965 void wxHtmlWindow::OnMouseUp(wxMouseEvent& event)
966 {
967 #if wxUSE_CLIPBOARD
968 if ( m_makingSelection )
969 {
970 ReleaseMouse();
971 m_makingSelection = false;
972
973 // did the user move the mouse far enough from starting point?
974 if ( CopySelection(Primary) )
975 {
976 // we don't want mouse up event that ended selecting to be
977 // handled as mouse click and e.g. follow hyperlink:
978 return;
979 }
980 }
981 #endif // wxUSE_CLIPBOARD
982
983 SetFocus();
984 if ( m_Cell )
985 {
986 wxPoint pos = CalcUnscrolledPosition(event.GetPosition());
987 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
988
989 // check is needed because FindCellByPos returns terminal cell and
990 // containers may have empty borders -- in this case NULL will be
991 // returned
992 if ( cell )
993 OnCellClicked(cell, pos.x, pos.y, event);
994 }
995 }
996
997
998
999 void wxHtmlWindow::OnInternalIdle()
1000 {
1001 wxWindow::OnInternalIdle();
1002
1003 if (m_tmpMouseMoved && (m_Cell != NULL))
1004 {
1005 #ifdef DEBUG_HTML_SELECTION
1006 Refresh();
1007 #endif
1008 int xc, yc, x, y;
1009 wxGetMousePosition(&xc, &yc);
1010 ScreenToClient(&xc, &yc);
1011 CalcUnscrolledPosition(xc, yc, &x, &y);
1012
1013 wxHtmlCell *cell = m_Cell->FindCellByPos(x, y);
1014
1015 // handle selection update:
1016 if ( m_makingSelection )
1017 {
1018 if ( !m_tmpSelFromCell )
1019 m_tmpSelFromCell = m_Cell->FindCellByPos(
1020 m_tmpSelFromPos.x,m_tmpSelFromPos.y);
1021
1022 // NB: a trick - we adjust selFromPos to be upper left or bottom
1023 // right corner of the first cell of the selection depending
1024 // on whether the mouse is moving to the right or to the left.
1025 // This gives us more "natural" behaviour when selecting
1026 // a line (specifically, first cell of the next line is not
1027 // included if you drag selection from left to right over
1028 // entire line):
1029 wxPoint dirFromPos;
1030 if ( !m_tmpSelFromCell )
1031 {
1032 dirFromPos = m_tmpSelFromPos;
1033 }
1034 else
1035 {
1036 dirFromPos = m_tmpSelFromCell->GetAbsPos();
1037 if ( x < m_tmpSelFromPos.x )
1038 {
1039 dirFromPos.x += m_tmpSelFromCell->GetWidth();
1040 dirFromPos.y += m_tmpSelFromCell->GetHeight();
1041 }
1042 }
1043 bool goingDown = dirFromPos.y < y ||
1044 (dirFromPos.y == y && dirFromPos.x < x);
1045
1046 // determine selection span:
1047 if ( /*still*/ !m_tmpSelFromCell )
1048 {
1049 if (goingDown)
1050 {
1051 m_tmpSelFromCell = m_Cell->FindCellByPos(
1052 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1053 wxHTML_FIND_NEAREST_AFTER);
1054 if (!m_tmpSelFromCell)
1055 m_tmpSelFromCell = m_Cell->GetFirstTerminal();
1056 }
1057 else
1058 {
1059 m_tmpSelFromCell = m_Cell->FindCellByPos(
1060 m_tmpSelFromPos.x,m_tmpSelFromPos.y,
1061 wxHTML_FIND_NEAREST_BEFORE);
1062 if (!m_tmpSelFromCell)
1063 m_tmpSelFromCell = m_Cell->GetLastTerminal();
1064 }
1065 }
1066
1067 wxHtmlCell *selcell = cell;
1068 if (!selcell)
1069 {
1070 if (goingDown)
1071 {
1072 selcell = m_Cell->FindCellByPos(x, y,
1073 wxHTML_FIND_NEAREST_BEFORE);
1074 if (!selcell)
1075 selcell = m_Cell->GetLastTerminal();
1076 }
1077 else
1078 {
1079 selcell = m_Cell->FindCellByPos(x, y,
1080 wxHTML_FIND_NEAREST_AFTER);
1081 if (!selcell)
1082 selcell = m_Cell->GetFirstTerminal();
1083 }
1084 }
1085
1086 // NB: it may *rarely* happen that the code above didn't find one
1087 // of the cells, e.g. if wxHtmlWindow doesn't contain any
1088 // visible cells.
1089 if ( selcell && m_tmpSelFromCell )
1090 {
1091 if ( !m_selection )
1092 {
1093 // start selecting only if mouse movement was big enough
1094 // (otherwise it was meant as mouse click, not selection):
1095 const int PRECISION = 2;
1096 wxPoint diff = m_tmpSelFromPos - wxPoint(x,y);
1097 if (abs(diff.x) > PRECISION || abs(diff.y) > PRECISION)
1098 {
1099 m_selection = new wxHtmlSelection();
1100 }
1101 }
1102 if ( m_selection )
1103 {
1104 if ( m_tmpSelFromCell->IsBefore(selcell) )
1105 {
1106 m_selection->Set(m_tmpSelFromPos, m_tmpSelFromCell,
1107 wxPoint(x,y), selcell); }
1108 else
1109 {
1110 m_selection->Set(wxPoint(x,y), selcell,
1111 m_tmpSelFromPos, m_tmpSelFromCell);
1112 }
1113 m_selection->ClearPrivPos();
1114 Refresh();
1115 }
1116 }
1117 }
1118
1119 // handle cursor and status bar text changes:
1120 if ( cell != m_tmpLastCell )
1121 {
1122 wxHtmlLinkInfo *lnk = cell ? cell->GetLink(x, y) : NULL;
1123 wxCursor cur;
1124 if (cell)
1125 cur = cell->GetCursor();
1126 else
1127 cur = *wxSTANDARD_CURSOR;
1128 SetCursor(cur);
1129
1130 if (lnk != m_tmpLastLink)
1131 {
1132 #if wxUSE_STATUSBAR
1133 if (lnk == NULL)
1134 {
1135 if (m_RelatedStatusBar != -1)
1136 m_RelatedFrame->SetStatusText(wxEmptyString,
1137 m_RelatedStatusBar);
1138 }
1139 else
1140 {
1141 if (m_RelatedStatusBar != -1)
1142 m_RelatedFrame->SetStatusText(lnk->GetHref(),
1143 m_RelatedStatusBar);
1144 }
1145 #endif // wxUSE_STATUSBAR
1146 m_tmpLastLink = lnk;
1147 }
1148
1149 m_tmpLastCell = cell;
1150 }
1151 else // mouse moved but stayed in the same cell
1152 {
1153 if ( cell )
1154 OnCellMouseHover(cell, x, y);
1155 }
1156
1157 m_tmpMouseMoved = FALSE;
1158 }
1159 }
1160
1161 #if wxUSE_CLIPBOARD
1162 void wxHtmlWindow::StopAutoScrolling()
1163 {
1164 if ( m_timerAutoScroll )
1165 {
1166 wxDELETE(m_timerAutoScroll);
1167 }
1168 }
1169
1170 void wxHtmlWindow::OnMouseEnter(wxMouseEvent& event)
1171 {
1172 StopAutoScrolling();
1173 event.Skip();
1174 }
1175
1176 void wxHtmlWindow::OnMouseLeave(wxMouseEvent& event)
1177 {
1178 // don't prevent the usual processing of the event from taking place
1179 event.Skip();
1180
1181 // when a captured mouse leave a scrolled window we start generate
1182 // scrolling events to allow, for example, extending selection beyond the
1183 // visible area in some controls
1184 if ( wxWindow::GetCapture() == this )
1185 {
1186 // where is the mouse leaving?
1187 int pos, orient;
1188 wxPoint pt = event.GetPosition();
1189 if ( pt.x < 0 )
1190 {
1191 orient = wxHORIZONTAL;
1192 pos = 0;
1193 }
1194 else if ( pt.y < 0 )
1195 {
1196 orient = wxVERTICAL;
1197 pos = 0;
1198 }
1199 else // we're lower or to the right of the window
1200 {
1201 wxSize size = GetClientSize();
1202 if ( pt.x > size.x )
1203 {
1204 orient = wxHORIZONTAL;
1205 pos = GetVirtualSize().x / wxHTML_SCROLL_STEP;
1206 }
1207 else if ( pt.y > size.y )
1208 {
1209 orient = wxVERTICAL;
1210 pos = GetVirtualSize().y / wxHTML_SCROLL_STEP;
1211 }
1212 else // this should be impossible
1213 {
1214 // but seems to happen sometimes under wxMSW - maybe it's a bug
1215 // there but for now just ignore it
1216
1217 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1218
1219 return;
1220 }
1221 }
1222
1223 // only start the auto scroll timer if the window can be scrolled in
1224 // this direction
1225 if ( !HasScrollbar(orient) )
1226 return;
1227
1228 delete m_timerAutoScroll;
1229 m_timerAutoScroll = new wxHtmlWinAutoScrollTimer
1230 (
1231 this,
1232 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1233 : wxEVT_SCROLLWIN_LINEDOWN,
1234 pos,
1235 orient
1236 );
1237 m_timerAutoScroll->Start(50); // FIXME: make configurable
1238 }
1239 }
1240
1241 void wxHtmlWindow::OnKeyUp(wxKeyEvent& event)
1242 {
1243 if ( IsSelectionEnabled() &&
1244 event.GetKeyCode() == 'C' && event.ControlDown() )
1245 {
1246 (void) CopySelection();
1247 }
1248 }
1249
1250 void wxHtmlWindow::OnCopy(wxCommandEvent& WXUNUSED(event))
1251 {
1252 (void) CopySelection();
1253 }
1254
1255 void wxHtmlWindow::OnDoubleClick(wxMouseEvent& event)
1256 {
1257 // select word under cursor:
1258 if ( IsSelectionEnabled() )
1259 {
1260 SelectWord(CalcUnscrolledPosition(event.GetPosition()));
1261
1262 (void) CopySelection(Primary);
1263
1264 m_lastDoubleClick = wxGetLocalTimeMillis();
1265 }
1266 else
1267 event.Skip();
1268 }
1269
1270 void wxHtmlWindow::SelectWord(const wxPoint& pos)
1271 {
1272 if ( m_Cell )
1273 {
1274 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1275 if ( cell )
1276 {
1277 delete m_selection;
1278 m_selection = new wxHtmlSelection();
1279 m_selection->Set(cell, cell);
1280 RefreshRect(wxRect(CalcScrolledPosition(cell->GetAbsPos()),
1281 wxSize(cell->GetWidth(), cell->GetHeight())));
1282 }
1283 }
1284 }
1285
1286 void wxHtmlWindow::SelectLine(const wxPoint& pos)
1287 {
1288 if ( m_Cell )
1289 {
1290 wxHtmlCell *cell = m_Cell->FindCellByPos(pos.x, pos.y);
1291 if ( cell )
1292 {
1293 // We use following heuristic to find a "line": let the line be all
1294 // cells in same container as the cell under mouse cursor that are
1295 // neither completely above nor completely bellow the clicked cell
1296 // (i.e. are likely to be words positioned on same line of text).
1297
1298 int y1 = cell->GetAbsPos().y;
1299 int y2 = y1 + cell->GetHeight();
1300 int y;
1301 const wxHtmlCell *c;
1302 const wxHtmlCell *before = NULL;
1303 const wxHtmlCell *after = NULL;
1304
1305 // find last cell of line:
1306 for ( c = cell->GetNext(); c; c = c->GetNext())
1307 {
1308 y = c->GetAbsPos().y;
1309 if ( y + c->GetHeight() > y1 && y < y2 )
1310 after = c;
1311 else
1312 break;
1313 }
1314 if ( !after )
1315 after = cell;
1316
1317 // find first cell of line:
1318 for ( c = cell->GetParent()->GetFirstChild();
1319 c && c != cell; c = c->GetNext())
1320 {
1321 y = c->GetAbsPos().y;
1322 if ( y + c->GetHeight() > y1 && y < y2 )
1323 {
1324 if ( ! before )
1325 before = c;
1326 }
1327 else
1328 before = NULL;
1329 }
1330 if ( !before )
1331 before = cell;
1332
1333 delete m_selection;
1334 m_selection = new wxHtmlSelection();
1335 m_selection->Set(before, after);
1336
1337 Refresh();
1338 }
1339 }
1340 }
1341
1342 void wxHtmlWindow::SelectAll()
1343 {
1344 if ( m_Cell )
1345 {
1346 delete m_selection;
1347 m_selection = new wxHtmlSelection();
1348 m_selection->Set(m_Cell->GetFirstTerminal(), m_Cell->GetLastTerminal());
1349 Refresh();
1350 }
1351 }
1352
1353 #endif // wxUSE_CLIPBOARD
1354
1355
1356
1357 IMPLEMENT_ABSTRACT_CLASS(wxHtmlProcessor,wxObject)
1358
1359 #if wxUSE_EXTENDED_RTTI
1360 IMPLEMENT_DYNAMIC_CLASS_XTI(wxHtmlWindow, wxScrolledWindow,"wx/html/htmlwin.h")
1361
1362 wxBEGIN_PROPERTIES_TABLE(wxHtmlWindow)
1363 /*
1364 TODO PROPERTIES
1365 style , wxHW_SCROLLBAR_AUTO
1366 borders , (dimension)
1367 url , string
1368 htmlcode , string
1369 */
1370 wxEND_PROPERTIES_TABLE()
1371
1372 wxBEGIN_HANDLERS_TABLE(wxHtmlWindow)
1373 wxEND_HANDLERS_TABLE()
1374
1375 wxCONSTRUCTOR_5( wxHtmlWindow , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
1376 #else
1377 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWindow,wxScrolledWindow)
1378 #endif
1379
1380 BEGIN_EVENT_TABLE(wxHtmlWindow, wxScrolledWindow)
1381 EVT_SIZE(wxHtmlWindow::OnSize)
1382 EVT_LEFT_DOWN(wxHtmlWindow::OnMouseDown)
1383 EVT_LEFT_UP(wxHtmlWindow::OnMouseUp)
1384 EVT_RIGHT_UP(wxHtmlWindow::OnMouseUp)
1385 EVT_MOTION(wxHtmlWindow::OnMouseMove)
1386 EVT_ERASE_BACKGROUND(wxHtmlWindow::OnEraseBackground)
1387 EVT_PAINT(wxHtmlWindow::OnPaint)
1388 #if wxUSE_CLIPBOARD
1389 EVT_LEFT_DCLICK(wxHtmlWindow::OnDoubleClick)
1390 EVT_ENTER_WINDOW(wxHtmlWindow::OnMouseEnter)
1391 EVT_LEAVE_WINDOW(wxHtmlWindow::OnMouseLeave)
1392 EVT_KEY_UP(wxHtmlWindow::OnKeyUp)
1393 EVT_MENU(wxID_COPY, wxHtmlWindow::OnCopy)
1394 #endif // wxUSE_CLIPBOARD
1395 END_EVENT_TABLE()
1396
1397
1398
1399
1400
1401 // A module to allow initialization/cleanup
1402 // without calling these functions from app.cpp or from
1403 // the user's application.
1404
1405 class wxHtmlWinModule: public wxModule
1406 {
1407 DECLARE_DYNAMIC_CLASS(wxHtmlWinModule)
1408 public:
1409 wxHtmlWinModule() : wxModule() {}
1410 bool OnInit() { return TRUE; }
1411 void OnExit() { wxHtmlWindow::CleanUpStatics(); }
1412 };
1413
1414 IMPLEMENT_DYNAMIC_CLASS(wxHtmlWinModule, wxModule)
1415
1416
1417 // This hack forces the linker to always link in m_* files
1418 // (wxHTML doesn't work without handlers from these files)
1419 #include "wx/html/forcelnk.h"
1420 FORCE_WXHTML_MODULES()
1421
1422 #endif // wxUSE_HTML
1423