-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
htmlwin.cpp
1949 lines (1640 loc) · 56.4 KB
/
htmlwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Name: src/html/htmlwin.cpp
// Purpose: wxHtmlWindow class for parsing & displaying HTML (implementation)
// Author: Vaclav Slavik
// Copyright: (c) 1999 Vaclav Slavik
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#if wxUSE_HTML && wxUSE_STREAMS
#ifndef WX_PRECOMP
#include "wx/list.h"
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/dcclient.h"
#include "wx/frame.h"
#include "wx/dcmemory.h"
#include "wx/timer.h"
#include "wx/settings.h"
#include "wx/dataobj.h"
#include "wx/statusbr.h"
#endif
#include "wx/html/htmlwin.h"
#include "wx/html/htmlproc.h"
#include "wx/clipbrd.h"
#include "wx/recguard.h"
#include <list>
// uncomment this line to visually show the extent of the selection
//#define DEBUG_HTML_SELECTION
// HTML events:
wxIMPLEMENT_DYNAMIC_CLASS(wxHtmlLinkEvent, wxCommandEvent);
wxIMPLEMENT_DYNAMIC_CLASS(wxHtmlCellEvent, wxCommandEvent);
wxDEFINE_EVENT( wxEVT_HTML_CELL_CLICKED, wxHtmlCellEvent );
wxDEFINE_EVENT( wxEVT_HTML_CELL_HOVER, wxHtmlCellEvent );
wxDEFINE_EVENT( wxEVT_HTML_LINK_CLICKED, wxHtmlLinkEvent );
#if wxUSE_CLIPBOARD
// ----------------------------------------------------------------------------
// wxHtmlWinAutoScrollTimer: the timer used to generate a stream of scroll
// events when a captured mouse is held outside the window
// ----------------------------------------------------------------------------
class wxHtmlWinAutoScrollTimer : public wxTimer
{
public:
wxHtmlWinAutoScrollTimer(wxScrolledWindow *win,
wxEventType eventTypeToSend,
int pos, int orient)
: m_eventType(eventTypeToSend)
{
m_win = win;
m_pos = pos;
m_orient = orient;
}
virtual void Notify() override;
private:
wxScrolledWindow *m_win;
wxEventType m_eventType;
int m_pos,
m_orient;
wxDECLARE_NO_COPY_CLASS(wxHtmlWinAutoScrollTimer);
};
void wxHtmlWinAutoScrollTimer::Notify()
{
// only do all this as long as the window is capturing the mouse
if ( wxWindow::GetCapture() != m_win )
{
Stop();
}
else // we still capture the mouse, continue generating events
{
// first scroll the window if we are allowed to do it
wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
event1.SetEventObject(m_win);
if ( m_win->GetEventHandler()->ProcessEvent(event1) )
{
// and then send a pseudo mouse-move event to refresh the selection
wxMouseEvent event2(wxEVT_MOTION);
wxGetMousePosition(&event2.m_x, &event2.m_y);
// the mouse event coordinates should be client, not screen as
// returned by wxGetMousePosition
wxWindow *parentTop = m_win;
while ( parentTop->GetParent() )
parentTop = parentTop->GetParent();
wxPoint ptOrig = parentTop->GetPosition();
event2.m_x -= ptOrig.x;
event2.m_y -= ptOrig.y;
event2.SetEventObject(m_win);
// FIXME: we don't fill in the other members - ok?
m_win->GetEventHandler()->ProcessEvent(event2);
}
else // can't scroll further, stop
{
Stop();
}
}
}
#endif // wxUSE_CLIPBOARD
//-----------------------------------------------------------------------------
// wxHtmlHistoryItem
//-----------------------------------------------------------------------------
// item of history list
class WXDLLIMPEXP_HTML wxHtmlHistoryItem
{
public:
wxHtmlHistoryItem(const wxString& p, const wxString& a) : m_Page(p), m_Anchor(a), m_Pos(0) { }
int GetPos() const {return m_Pos;}
void SetPos(int p) {m_Pos = p;}
const wxString& GetPage() const {return m_Page;}
const wxString& GetAnchor() const {return m_Anchor;}
private:
wxString m_Page;
wxString m_Anchor;
int m_Pos;
};
//-----------------------------------------------------------------------------
// our private containers: they have to be classes as they're forward-declared
//-----------------------------------------------------------------------------
class wxHtmlHistoryArray : public std::vector<wxHtmlHistoryItem>
{
public:
wxHtmlHistoryArray() = default;
};
class wxHtmlProcessorList : public std::list<std::unique_ptr<wxHtmlProcessor>>
{
public:
wxHtmlProcessorList() = default;
};
//-----------------------------------------------------------------------------
// wxHtmlWindowMouseHelper
//-----------------------------------------------------------------------------
wxHtmlWindowMouseHelper::wxHtmlWindowMouseHelper(wxHtmlWindowInterface *iface)
: m_tmpMouseMoved(false),
m_tmpLastLink(nullptr),
m_tmpLastCell(nullptr),
m_interface(iface)
{
}
void wxHtmlWindowMouseHelper::HandleMouseMoved()
{
m_tmpMouseMoved = true;
}
bool wxHtmlWindowMouseHelper::HandleMouseClick(wxHtmlCell *rootCell,
const wxPoint& pos,
const wxMouseEvent& event)
{
if (!rootCell)
return false;
wxHtmlCell *cell = rootCell->FindCellByPos(pos.x, pos.y);
// this check is needed because FindCellByPos returns terminal cell and
// containers may have empty borders -- in this case nullptr will be
// returned
if (!cell)
return false;
// adjust the coordinates to be relative to this cell:
wxPoint relpos = pos - cell->GetAbsPos(rootCell);
return OnCellClicked(cell, relpos.x, relpos.y, event);
}
void wxHtmlWindowMouseHelper::HandleIdle(wxHtmlCell *rootCell,
const wxPoint& pos)
{
wxHtmlCell *cell = rootCell ? rootCell->FindCellByPos(pos.x, pos.y) : nullptr;
if (cell != m_tmpLastCell)
{
wxHtmlLinkInfo *lnk = nullptr;
if (cell)
{
// adjust the coordinates to be relative to this cell:
wxPoint relpos = pos - cell->GetAbsPos(rootCell);
lnk = cell->GetLink(relpos.x, relpos.y);
}
wxCursor cur;
if (cell)
cur = cell->GetMouseCursorAt(m_interface, pos);
else
cur = m_interface->GetHTMLCursor(
wxHtmlWindowInterface::HTMLCursor_Default);
m_interface->GetHTMLWindow()->SetCursor(cur);
if (lnk != m_tmpLastLink)
{
if (lnk)
m_interface->SetHTMLStatusText(lnk->GetHref());
else
m_interface->SetHTMLStatusText(wxEmptyString);
m_tmpLastLink = lnk;
}
m_tmpLastCell = cell;
}
else // mouse moved but stayed in the same cell
{
if ( cell )
{
// A single cell can have different cursors for different positions,
// so update cursor for this case as well.
wxCursor cur = cell->GetMouseCursorAt(m_interface, pos);
m_interface->GetHTMLWindow()->SetCursor(cur);
OnCellMouseHover(cell, pos.x, pos.y);
}
}
m_tmpMouseMoved = false;
}
bool wxHtmlWindowMouseHelper::OnCellClicked(wxHtmlCell *cell,
wxCoord x, wxCoord y,
const wxMouseEvent& event)
{
wxHtmlCellEvent ev(wxEVT_HTML_CELL_CLICKED,
m_interface->GetHTMLWindow()->GetId(),
cell, wxPoint(x,y), event);
if (!m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev))
{
// if the event wasn't handled, do the default processing here:
wxASSERT_MSG( cell, wxT("can't be called with null cell") );
// If we don't return true, HTML listboxes will always think that they should take
// the focus
if (cell->ProcessMouseClick(m_interface, ev.GetPoint(), ev.GetMouseEvent()))
return true;
}
// true if a link was clicked, false otherwise
return ev.GetLinkClicked();
}
void wxHtmlWindowMouseHelper::OnCellMouseHover(wxHtmlCell * cell,
wxCoord x,
wxCoord y)
{
wxHtmlCellEvent ev(wxEVT_HTML_CELL_HOVER,
m_interface->GetHTMLWindow()->GetId(),
cell, wxPoint(x,y), wxMouseEvent());
m_interface->GetHTMLWindow()->GetEventHandler()->ProcessEvent(ev);
}
//-----------------------------------------------------------------------------
// wxHtmlWindow
//-----------------------------------------------------------------------------
wxList wxHtmlWindow::m_Filters;
wxHtmlFilter *wxHtmlWindow::m_DefaultFilter = nullptr;
wxHtmlProcessorList *wxHtmlWindow::m_GlobalProcessors = nullptr;
wxCursor *wxHtmlWindow::ms_cursorLink = nullptr;
wxCursor *wxHtmlWindow::ms_cursorText = nullptr;
wxCursor *wxHtmlWindow::ms_cursorDefault = nullptr;
void wxHtmlWindow::CleanUpStatics()
{
wxDELETE(m_DefaultFilter);
wxClearList(m_Filters);
wxDELETE(m_GlobalProcessors);
wxDELETE(ms_cursorLink);
wxDELETE(ms_cursorText);
wxDELETE(ms_cursorDefault);
}
void wxHtmlWindow::Init()
{
m_tmpCanDrawLocks = 0;
m_FS = new wxFileSystem();
#if wxUSE_STATUSBAR
m_RelatedStatusBar = nullptr;
m_RelatedStatusBarIndex = -1;
#endif // wxUSE_STATUSBAR
m_RelatedFrame = nullptr;
m_TitleFormat = wxT("%s");
m_OpenedPage.clear();
m_OpenedAnchor.clear();
m_OpenedPageTitle.clear();
m_Cell = nullptr;
m_Parser = new wxHtmlWinParser(this);
m_Parser->SetFS(m_FS);
m_HistoryPos = -1;
m_HistoryOn = true;
m_History = new wxHtmlHistoryArray;
m_Processors = nullptr;
SetBorders(10);
m_selection = nullptr;
m_makingSelection = false;
#if wxUSE_CLIPBOARD
m_timerAutoScroll = nullptr;
m_lastDoubleClick = 0;
#endif // wxUSE_CLIPBOARD
m_tmpSelFromCell = nullptr;
}
bool wxHtmlWindow::Create(wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size,
long style, const wxString& name)
{
if (!wxScrolledWindow::Create(parent, id, pos, size,
style | wxVSCROLL | wxHSCROLL,
name))
return false;
// We can't erase our background in EVT_ERASE_BACKGROUND handler and use
// double buffering in EVT_PAINT handler as this requires blitting back
// something already drawn on the window to the backing store bitmap when
// handling EVT_PAINT but blitting in this direction is simply not
// supported by OS X.
//
// So instead we use a hack with artificial EVT_ERASE_BACKGROUND generation
// from OnPaint() and this means that we never need the "real" erase event
// at all so disable it to avoid executing any user-defined handlers twice
// (and to avoid processing unnecessary event if no handlers are defined).
SetBackgroundStyle(wxBG_STYLE_PAINT);
SetPage(wxT("<html><body></body></html>"));
SetInitialSize(size);
if ( !HasFlag(wxHW_SCROLLBAR_NEVER) )
SetScrollRate(wxHTML_SCROLL_STEP, wxHTML_SCROLL_STEP);
return true;
}
wxHtmlWindow::~wxHtmlWindow()
{
#if wxUSE_CLIPBOARD
StopAutoScrolling();
#endif // wxUSE_CLIPBOARD
HistoryClear();
delete m_selection;
delete m_Cell;
delete m_Parser;
delete m_FS;
delete m_History;
delete m_Processors;
}
void wxHtmlWindow::SetRelatedFrame(wxFrame* frame, const wxString& format)
{
m_RelatedFrame = frame;
m_TitleFormat = format;
// Check if the format provided can actually be used: it's more
// user-friendly to do it here and now rather than triggering the same
// assert much later when it's really used.
// If you get an assert here, it means that the title doesn't contain
// exactly one "%s" format specifier, which is an error in the caller.
wxString::Format(m_TitleFormat, wxString());
}
#if wxUSE_STATUSBAR
void wxHtmlWindow::SetRelatedStatusBar(int index)
{
m_RelatedStatusBarIndex = index;
}
void wxHtmlWindow::SetRelatedStatusBar(wxStatusBar* statusbar, int index)
{
m_RelatedStatusBar = statusbar;
m_RelatedStatusBarIndex = index;
}
#endif // wxUSE_STATUSBAR
void wxHtmlWindow::SetFonts(const wxString& normal_face, const wxString& fixed_face, const int *sizes)
{
m_Parser->SetFonts(normal_face, fixed_face, sizes);
// re-layout the page after changing fonts:
DoSetPage(*(m_Parser->GetSource()));
}
void wxHtmlWindow::SetStandardFonts(int size,
const wxString& normal_face,
const wxString& fixed_face)
{
m_Parser->SetStandardFonts(size, normal_face, fixed_face);
// re-layout the page after changing fonts:
DoSetPage(*(m_Parser->GetSource()));
}
bool wxHtmlWindow::SetPage(const wxString& source)
{
m_OpenedPage.clear();
m_OpenedAnchor.clear();
m_OpenedPageTitle.clear();
return DoSetPage(source);
}
bool wxHtmlWindow::DoSetPage(const wxString& source)
{
wxString newsrc(source);
wxDELETE(m_selection);
// we will soon delete all the cells, so clear pointers to them:
m_tmpSelFromCell = nullptr;
// pass HTML through registered processors:
if (m_Processors || m_GlobalProcessors)
{
wxHtmlProcessorList::iterator nodeL, nodeG;
const auto isNodeLValid = [&nodeL, this]()
{
return m_Processors && nodeL != m_Processors->end();
};
const auto isNodeGValid = [&nodeG]()
{
return m_GlobalProcessors && nodeG != m_GlobalProcessors->end();
};
if ( m_Processors )
nodeL = m_Processors->begin();
if ( m_GlobalProcessors )
nodeG = m_GlobalProcessors->begin();
// VS: there are two lists, global and local, both of them sorted by
// priority. Since we have to go through _both_ lists with
// decreasing priority, we "merge-sort" the lists on-line by
// processing that one of the two heads that has higher priority
// in every iteration
while (isNodeLValid() || isNodeGValid())
{
int prL, prG;
prL = isNodeLValid() ? (*nodeL)->GetPriority() : -1;
prG = isNodeGValid() ? (*nodeG)->GetPriority() : -1;
if (prL > prG)
{
if ((*nodeL)->IsEnabled())
newsrc = (*nodeL)->Process(newsrc);
++nodeL;
}
else // prL <= prG
{
if ((*nodeG)->IsEnabled())
newsrc = (*nodeG)->Process(newsrc);
++nodeG;
}
}
}
// ...and run the parser on it:
wxClientDC dc(this);
dc.SetMapMode(wxMM_TEXT);
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
SetBackgroundImage(wxNullBitmap);
double pixelScale = 1.0;
#ifndef wxHAS_DPI_INDEPENDENT_PIXELS
pixelScale = GetDPIScaleFactor();
#endif
m_Parser->SetDC(&dc, pixelScale, 1.0);
// notice that it's important to set m_Cell to nullptr here before calling
// Parse() below, even if it will be overwritten by its return value as
// without this we may crash if it's used from inside Parse(), so use
// wxDELETE() and not just delete here
wxDELETE(m_Cell);
m_Cell = (wxHtmlContainerCell*) m_Parser->Parse(newsrc);
// The parser doesn't need the DC any more, so ensure it's not left with a
// dangling pointer after the DC object goes out of scope.
m_Parser->SetDC(nullptr);
m_Cell->SetIndent(m_Borders, wxHTML_INDENT_ALL, wxHTML_UNITS_PIXELS);
m_Cell->SetAlignHor(wxHTML_ALIGN_CENTER);
CreateLayout();
if (m_tmpCanDrawLocks == 0)
Refresh();
return true;
}
bool wxHtmlWindow::AppendToPage(const wxString& source)
{
return DoSetPage(*(GetParser()->GetSource()) + source);
}
bool wxHtmlWindow::LoadPage(const wxString& location)
{
wxCHECK_MSG( !location.empty(), false, "location must be non-empty" );
wxBusyCursor busyCursor;
bool rt_val;
bool needs_refresh = false;
m_tmpCanDrawLocks++;
if (m_HistoryOn && (m_HistoryPos != -1))
{
// store scroll position into history item:
int x, y;
GetViewStart(&x, &y);
(*m_History)[m_HistoryPos].SetPos(y);
}
// first check if we're moving to an anchor in the same page
size_t posLocalAnchor = location.Find('#');
if ( posLocalAnchor != wxString::npos && posLocalAnchor != 0 )
{
// check if the part before the anchor is the same as the (either
// relative or absolute) URI of the current page
const wxString beforeAnchor = location.substr(0, posLocalAnchor);
if ( beforeAnchor != m_OpenedPage &&
m_FS->GetPath() + beforeAnchor != m_OpenedPage )
{
// indicate that we're not moving to a local anchor
posLocalAnchor = wxString::npos;
}
}
if ( posLocalAnchor != wxString::npos )
{
m_tmpCanDrawLocks--;
rt_val = ScrollToAnchor(location.substr(posLocalAnchor + 1));
m_tmpCanDrawLocks++;
}
else // moving to another page
{
needs_refresh = true;
#if wxUSE_STATUSBAR
// load&display it:
if (m_RelatedStatusBarIndex != -1)
{
SetHTMLStatusText(_("Connecting..."));
Refresh(false);
}
#endif // wxUSE_STATUSBAR
wxFSFile *f = m_Parser->OpenURL(wxHTML_URL_PAGE, location);
// try to interpret 'location' as filename instead of URL:
if (f == nullptr)
{
wxFileName fn(location);
wxString location2 = wxFileSystem::FileNameToURL(fn);
f = m_Parser->OpenURL(wxHTML_URL_PAGE, location2);
}
if (f == nullptr)
{
wxLogError(_("Unable to open requested HTML document: %s"), location);
m_tmpCanDrawLocks--;
SetHTMLStatusText(wxEmptyString);
return false;
}
else
{
wxList::compatibility_iterator node;
wxString src;
#if wxUSE_STATUSBAR
if (m_RelatedStatusBarIndex != -1)
{
wxString msg = _("Loading : ") + location;
SetHTMLStatusText(msg);
Refresh(false);
}
#endif // wxUSE_STATUSBAR
node = m_Filters.GetFirst();
while (node)
{
wxHtmlFilter *h = (wxHtmlFilter*) node->GetData();
if (h->CanRead(*f))
{
src = h->ReadFile(*f);
break;
}
node = node->GetNext();
}
if (src.empty())
{
if (m_DefaultFilter == nullptr) m_DefaultFilter = GetDefaultFilter();
src = m_DefaultFilter->ReadFile(*f);
}
m_FS->ChangePathTo(f->GetLocation());
rt_val = SetPage(src);
m_OpenedPage = f->GetLocation();
if (!f->GetAnchor().empty())
{
ScrollToAnchor(f->GetAnchor());
}
else
{
// Without anchor, go to the top of the page, instead of
// possibly remaining at non-zero scroll position and scrolling
// the top out of view.
Scroll(0, 0);
}
delete f;
#if wxUSE_STATUSBAR
if (m_RelatedStatusBarIndex != -1)
{
SetHTMLStatusText(_("Done"));
}
#endif // wxUSE_STATUSBAR
}
}
if (m_HistoryOn) // add this page to history there:
{
int c = m_History->size() - (m_HistoryPos + 1);
if (m_HistoryPos < 0 ||
(*m_History)[m_HistoryPos].GetPage() != m_OpenedPage ||
(*m_History)[m_HistoryPos].GetAnchor() != m_OpenedAnchor)
{
m_HistoryPos++;
const auto first = m_History->begin() + m_HistoryPos;
m_History->erase(first, first + c);
m_History->emplace_back(m_OpenedPage, m_OpenedAnchor);
}
}
if (m_OpenedPageTitle.empty())
OnSetTitle(wxFileNameFromPath(m_OpenedPage));
if (needs_refresh)
{
m_tmpCanDrawLocks--;
Refresh();
}
else
m_tmpCanDrawLocks--;
return rt_val;
}
bool wxHtmlWindow::LoadFile(const wxFileName& filename)
{
wxString url = wxFileSystem::FileNameToURL(filename);
return LoadPage(url);
}
bool wxHtmlWindow::ScrollToAnchor(const wxString& anchor)
{
const wxHtmlCell *c = m_Cell->Find(wxHTML_COND_ISANCHOR, &anchor);
if (!c)
{
wxLogWarning(_("HTML anchor %s does not exist."), anchor);
return false;
}
else
{
// Go to next visible cell in current container, if it exists. This
// yields a bit better (even though still imperfect) results in that
// there's better chance of using a suitable cell for upper Y
// coordinate value. See bug #11406 for additional discussion.
const wxHtmlCell *c_save = c;
while ( c && c->IsFormattingCell() )
c = c->GetNext();
if ( !c )
c = c_save;
int y;
for (y = 0; c != nullptr; c = c->GetParent()) y += c->GetPosY();
Scroll(-1, y / wxHTML_SCROLL_STEP);
m_OpenedAnchor = anchor;
return true;
}
}
void wxHtmlWindow::OnSetTitle(const wxString& title)
{
if (m_RelatedFrame)
{
wxString tit;
tit.Printf(m_TitleFormat, title);
m_RelatedFrame->SetTitle(tit);
}
m_OpenedPageTitle = title;
}
void wxHtmlWindow::CreateLayout()
{
// ShowScrollbars() results in size change events -- and thus a nested
// CreateLayout() call -- on some platforms. Ignore nested calls, toplevel
// CreateLayout() will do the right thing eventually.
static wxRecursionGuardFlag s_flagReentrancy;
wxRecursionGuard guard(s_flagReentrancy);
if ( guard.IsInside() )
return;
if (!m_Cell)
return;
if ( HasFlag(wxHW_SCROLLBAR_NEVER) )
{
m_Cell->Layout(GetClientSize().GetWidth());
}
else // Do show scrollbars if necessary.
{
// Get client width if a vertical scrollbar is shown
// (which is usually the case).
ShowScrollbars(wxSHOW_SB_DEFAULT, wxSHOW_SB_ALWAYS);
const int widthWithVScrollbar = GetClientSize().GetWidth();
// Let wxScrolledWindow decide whether it needs to show the vertical
// scrollbar for the given contents size.
ShowScrollbars(wxSHOW_SB_DEFAULT, wxSHOW_SB_DEFAULT);
m_Cell->Layout(widthWithVScrollbar);
SetVirtualSize(m_Cell->GetWidth(), m_Cell->GetHeight());
// Check if the vertical scrollbar was hidden.
const int newClientWidth = GetClientSize().GetWidth();
if ( newClientWidth != widthWithVScrollbar )
{
m_Cell->Layout(newClientWidth);
SetVirtualSize(m_Cell->GetWidth(), m_Cell->GetHeight());
}
}
}
#if wxUSE_CONFIG
void wxHtmlWindow::ReadCustomization(wxConfigBase *cfg, wxString path)
{
wxString oldpath;
wxString tmp;
int p_fontsizes[7];
wxString p_fff, p_ffn;
if (!path.empty())
{
oldpath = cfg->GetPath();
cfg->SetPath(path);
}
m_Borders = cfg->Read(wxT("wxHtmlWindow/Borders"), m_Borders);
p_fff = cfg->Read(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
p_ffn = cfg->Read(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
for (int i = 0; i < 7; i++)
{
tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
p_fontsizes[i] = cfg->Read(tmp, m_Parser->m_FontsSizes[i]);
}
SetFonts(p_ffn, p_fff, p_fontsizes);
if (!path.empty())
cfg->SetPath(oldpath);
}
void wxHtmlWindow::WriteCustomization(wxConfigBase *cfg, wxString path)
{
wxString oldpath;
wxString tmp;
if (!path.empty())
{
oldpath = cfg->GetPath();
cfg->SetPath(path);
}
cfg->Write(wxT("wxHtmlWindow/Borders"), (long) m_Borders);
cfg->Write(wxT("wxHtmlWindow/FontFaceFixed"), m_Parser->m_FontFaceFixed);
cfg->Write(wxT("wxHtmlWindow/FontFaceNormal"), m_Parser->m_FontFaceNormal);
for (int i = 0; i < 7; i++)
{
tmp.Printf(wxT("wxHtmlWindow/FontsSize%i"), i);
cfg->Write(tmp, (long) m_Parser->m_FontsSizes[i]);
}
if (!path.empty())
cfg->SetPath(oldpath);
}
#endif // wxUSE_CONFIG
bool wxHtmlWindow::HistoryBack()
{
wxString a, l;
if (m_HistoryPos < 1) return false;
// store scroll position into history item:
int x, y;
GetViewStart(&x, &y);
(*m_History)[m_HistoryPos].SetPos(y);
// go to previous position:
m_HistoryPos--;
l = (*m_History)[m_HistoryPos].GetPage();
a = (*m_History)[m_HistoryPos].GetAnchor();
m_HistoryOn = false;
m_tmpCanDrawLocks++;
if (a.empty()) LoadPage(l);
else LoadPage(l + wxT("#") + a);
m_HistoryOn = true;
m_tmpCanDrawLocks--;
Scroll(0, (*m_History)[m_HistoryPos].GetPos());
Refresh();
return true;
}
bool wxHtmlWindow::HistoryCanBack()
{
if (m_HistoryPos < 1) return false;
return true ;
}
bool wxHtmlWindow::HistoryForward()
{
wxString a, l;
if (m_HistoryPos == -1) return false;
if (m_HistoryPos >= (int)m_History->size() - 1)return false;
m_OpenedPage.clear(); // this will disable adding new entry into history in LoadPage()
m_HistoryPos++;
l = (*m_History)[m_HistoryPos].GetPage();
a = (*m_History)[m_HistoryPos].GetAnchor();
m_HistoryOn = false;
m_tmpCanDrawLocks++;
if (a.empty()) LoadPage(l);
else LoadPage(l + wxT("#") + a);
m_HistoryOn = true;
m_tmpCanDrawLocks--;
Scroll(0, (*m_History)[m_HistoryPos].GetPos());
Refresh();
return true;
}
bool wxHtmlWindow::HistoryCanForward()
{
if (m_HistoryPos == -1) return false;
if (m_HistoryPos >= (int)m_History->size() - 1)return false;
return true ;
}
void wxHtmlWindow::HistoryClear()
{
m_History->clear();
m_HistoryPos = -1;
}
void wxHtmlWindow::AddProcessor(wxHtmlProcessor *processor)
{
if (!m_Processors)
{
m_Processors = new wxHtmlProcessorList;
}
std::unique_ptr<wxHtmlProcessor> processorPtr{processor};
wxHtmlProcessorList::iterator node;
for (node = m_Processors->begin(); node != m_Processors->end(); ++node)
{
if (processor->GetPriority() > (*node)->GetPriority())
{
m_Processors->insert(node, std::move(processorPtr));
return;
}
}
m_Processors->push_back(std::move(processorPtr));
}
/*static */ void wxHtmlWindow::AddGlobalProcessor(wxHtmlProcessor *processor)
{
if (!m_GlobalProcessors)
{
m_GlobalProcessors = new wxHtmlProcessorList;
}
std::unique_ptr<wxHtmlProcessor> processorPtr{processor};
wxHtmlProcessorList::iterator node;
for (node = m_GlobalProcessors->begin(); node != m_GlobalProcessors->end(); ++node)
{
if (processor->GetPriority() > (*node)->GetPriority())
{
m_GlobalProcessors->insert(node, std::move(processorPtr));
return;
}
}
m_GlobalProcessors->push_back(std::move(processorPtr));
}
void wxHtmlWindow::AddFilter(wxHtmlFilter *filter)
{
m_Filters.Append(filter);
}
bool wxHtmlWindow::IsSelectionEnabled() const
{
#if wxUSE_CLIPBOARD
return !HasFlag(wxHW_NO_SELECTION);
#else
return false;
#endif
}
#if wxUSE_CLIPBOARD
wxString wxHtmlWindow::DoSelectionToText(wxHtmlSelection *sel)
{
if ( !sel )
return wxEmptyString;
wxClientDC dc(this);
wxString text;
wxHtmlTerminalCellsInterator i(sel->GetFromCell(), sel->GetToCell());
const wxHtmlCell *prev = nullptr;
while ( i )
{
// When converting HTML content to plain text, the entire paragraph
// (container in wxHTML) goes on single line. A new paragraph (that
// should go on its own line) has its own container. Therefore, the
// simplest way of detecting where to insert newlines in plain text
// is to check if the parent container changed -- if it did, we moved
// to a new paragraph.
if ( prev && prev->GetParent() != i->GetParent() )
text << '\n';
// NB: we don't need to pass the selection to ConvertToText() in the
// middle of the selected text; it's only useful when only part of
// a cell is selected
text << i->ConvertToText(sel);
prev = *i;
++i;
}
return text;
}
wxString wxHtmlWindow::ToText()