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