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