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