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