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