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