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