-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathPDFViewImpl.cpp
More file actions
1624 lines (1401 loc) · 40.5 KB
/
PDFViewImpl.cpp
File metadata and controls
1624 lines (1401 loc) · 40.5 KB
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/PDFViewImpl.cpp
// Purpose: wxPDFViewImpl implementation
// Author: Tobias Taschner
// Created: 2014-08-07
// Copyright: (c) 2014 Tobias Taschner
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include "private/PDFViewImpl.h"
#include "private/PDFViewPrintout.h"
#include <wx/dcbuffer.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include "fpdf_ext.h"
#include "fpdf_text.h"
#include "fpdf_fwlevent.h"
#include "v8.h"
#include "libplatform/libplatform.h"
// See Table 3.20 in
// http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf
#define PDF_PERMISSION_PRINT_LOW_QUALITY 1 << 2
#define PDF_PERMISSION_PRINT_HIGH_QUALITY 1 << 11
#define PDF_PERMISSION_COPY 1 << 4
#define PDF_PERMISSION_COPY_ACCESSIBLE 1 << 9
std::map<FPDF_FORMFILLINFO*, wxPDFViewImpl*> g_pdfFormMap;
void LogPDFError(unsigned long error)
{
wxString errorMsg;
switch (error)
{
case FPDF_ERR_UNKNOWN:
errorMsg = _("Unknown Error");
break;
case FPDF_ERR_FILE:
errorMsg = _("File not found or could not be opened.");
break;
case FPDF_ERR_FORMAT:
errorMsg = _("File not in PDF format or corrupted.");
break;
case FPDF_ERR_PASSWORD:
errorMsg = _("Password required or incorrect password.");
break;
case FPDF_ERR_SECURITY:
errorMsg = _("Unsupported security scheme.");
break;
case FPDF_ERR_PAGE:
errorMsg = _("Page not found or content error.");
break;
default:
errorMsg = wxString::Format(_("Unknown Error (%d)"), error);
break;
};
if (error != FPDF_ERR_SUCCESS)
wxLogError("PDF Error: %s", errorMsg);
}
int Form_Alert(IPDF_JSPLATFORM* WXUNUSED(pThis), FPDF_WIDESTRING Msg, FPDF_WIDESTRING Title, int Type, int Icon)
{
long msgBoxStyle = wxCENTRE;
switch (Icon)
{
case 0:
msgBoxStyle |= wxICON_ERROR;
break;
case 1:
msgBoxStyle |= wxICON_WARNING;
break;
case 2:
msgBoxStyle |= wxICON_QUESTION;
break;
case 3:
msgBoxStyle |= wxICON_INFORMATION;
break;
default:
break;
}
switch (Type)
{
case 0:
msgBoxStyle |= wxOK;
break;
case 1:
msgBoxStyle |= wxOK | wxCANCEL;
break;
case 2:
msgBoxStyle |= wxYES_NO;
break;
case 3:
msgBoxStyle |= wxYES_NO | wxCANCEL;
break;
}
wxMBConvUTF16 conv;
wxString msgTitle = conv.cMB2WC((char*) Title);
wxString msgMsg = conv.cMB2WC((char*) Msg);
int msgBoxRes = wxMessageBox(msgMsg, msgTitle, msgBoxStyle);
int retVal = 0;
switch (msgBoxRes)
{
case wxOK:
retVal = 1;
break;
case wxCANCEL:
retVal = 2;
break;
case wxNO:
retVal = 3;
break;
case wxYES:
retVal = 4;
break;
};
return retVal;
}
void Form_GotoPage(IPDF_JSPLATFORM* pThis, int pageNumber)
{
wxPDFViewImpl* impl = g_pdfFormMap[(FPDF_FORMFILLINFO*) pThis->m_pFormfillinfo];
if (!impl)
return;
impl->GoToPage(pageNumber);
}
void Form_Print(IPDF_JSPLATFORM* pThis,
FPDF_BOOL bUI,
int nStart,
int nEnd,
FPDF_BOOL bSilent,
FPDF_BOOL bShrinkToFit,
FPDF_BOOL bPrintAsImage,
FPDF_BOOL bReverse,
FPDF_BOOL bAnnotations)
{
wxPDFViewImpl* impl = g_pdfFormMap[(FPDF_FORMFILLINFO*) pThis->m_pFormfillinfo];
if (!impl)
return;
// TODO: use parameters
impl->Print();
}
wxString g_formSelectedFilePath;
int Form_Browse(IPDF_JSPLATFORM* pThis,
void* filePath,
int length)
{
if (length == 0)
{
wxFileDialog dlg(NULL, _("Open File"), "", "", "All Files|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (dlg.ShowModal() == wxID_OK)
{
g_formSelectedFilePath = dlg.GetPath();
return g_formSelectedFilePath.length();
}
}
else if (filePath != NULL)
{
memcpy(filePath, g_formSelectedFilePath.utf8_str(), g_formSelectedFilePath.length());
return g_formSelectedFilePath.length();
}
return 0;
}
void Form_OpenDoc(IPDF_JSPLATFORM* pThis,
FPDF_WIDESTRING Path)
{
wxPDFViewImpl* impl = g_pdfFormMap[(FPDF_FORMFILLINFO*) pThis->m_pFormfillinfo];
if (!impl)
return;
wxMBConvUTF16 conv;
wxString filePath = conv.cMB2WC((char*) Path);
wxLogDebug("Form_OpenDoc: %s", filePath);
impl->GoToRemote(filePath);
}
wxPDFViewImpl* g_unsupportedHandlerPDFViewImpl = NULL;
void Unsupported_Handler(UNSUPPORT_INFO*, int type)
{
if (g_unsupportedHandlerPDFViewImpl)
g_unsupportedHandlerPDFViewImpl->HandleUnsupportedFeature(type);
}
UNSUPPORT_INFO g_unsupported_info = {
1,
Unsupported_Handler
};
int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
unsigned long size)
{
wxPDFViewImpl* impl = (wxPDFViewImpl*) param;
std::istream* pIstr = impl->GetStream();
pIstr->seekg(pos);
pIstr->read((char*) pBuf, size);
if (pIstr->gcount() == size && !pIstr->fail())
return 1;
else
return 0;
}
FPDF_BOOL Is_Data_Avail(_FX_FILEAVAIL* WXUNUSED(pThis), size_t WXUNUSED(offset), size_t WXUNUSED(size))
{
return true;
}
void Add_Segment(FX_DOWNLOADHINTS* WXUNUSED(pThis), size_t WXUNUSED(offset), size_t WXUNUSED(size))
{
}
void FFI_SetCursor(FPDF_FORMFILLINFO* pThis, int nCursorType)
{
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (!impl)
return;
wxStockCursor cursorType = wxCURSOR_ARROW;
switch (nCursorType) {
case FXCT_NESW:
cursorType = wxCURSOR_SIZENESW;
break;
case FXCT_NWSE:
cursorType = wxCURSOR_SIZENWSE;
break;
case FXCT_VBEAM:
case FXCT_HBEAM:
cursorType = wxCURSOR_CHAR;
break;
case FXCT_HAND:
cursorType = wxCURSOR_HAND;
break;
}
wxLogDebug("FFI_SetCursor: %d", nCursorType);
impl->SetDefaultCursor(cursorType);
}
void FFI_Invalidate(FPDF_FORMFILLINFO* pThis,
FPDF_PAGE page,
double left,
double top,
double right,
double bottom)
{
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (impl)
{
int mostVisible = impl->GetMostVisiblePage();
if (mostVisible >= 0 && (*impl->GetPages())[mostVisible].GetPage() == page)
{
wxRect rect(left, top, 0, 0);
rect.SetBottom(bottom);
rect.SetRight(right);
impl->InvalidatePage(mostVisible, &rect);
}
}
}
FPDF_PAGE FFI_GetPage(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document, int nPageIndex)
{
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (impl)
return (*impl->GetPages())[nPageIndex].GetPage();
return NULL;
}
FPDF_PAGE FFI_GetCurrentPage(FPDF_FORMFILLINFO* pThis, FPDF_DOCUMENT document)
{
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (impl)
{
int mostVisible = impl->GetMostVisiblePage();
if (mostVisible >= 0)
return (*impl->GetPages())[mostVisible].GetPage();
}
return NULL;
}
void FFI_DoGoToAction(FPDF_FORMFILLINFO* pThis,
int nPageIndex,
int zoomMode,
float* fPosArray,
int sizeofArray)
{
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (impl)
impl->DoGoToAction(nPageIndex);
}
void FFI_ExecuteNamedAction(FPDF_FORMFILLINFO* pThis,
FPDF_BYTESTRING namedAction)
{
wxString action(namedAction);
wxPDFViewImpl* impl = g_pdfFormMap[pThis];
if (impl)
impl->ExecuteNamedAction(action);
}
static int wxConvertModifiersToPdf(int modifiers)
{
int pdf_mods = 0;
if (modifiers & wxMOD_CONTROL)
pdf_mods |= FWL_EVENTFLAG_ControlKey;
if (modifiers & wxMOD_ALT)
pdf_mods |= FWL_EVENTFLAG_AltKey;
if (modifiers & wxMOD_SHIFT)
pdf_mods |= FWL_EVENTFLAG_ShiftKey;
if (modifiers & wxMOD_META)
pdf_mods |= FWL_EVENTFLAG_MetaKey;
return pdf_mods;
}
static int wxConvertKeyCodeToPdf(int keyCode)
{
switch (keyCode)
{
case WXK_LEFT:
return FWL_VKEY_Left;
case WXK_UP:
return FWL_VKEY_Up;
case WXK_RIGHT:
return FWL_VKEY_Right;
case WXK_DOWN:
return FWL_VKEY_Down;
case WXK_PAGEUP:
return FWL_VKEY_Prior;
case WXK_PAGEDOWN:
return FWL_VKEY_Next;
case WXK_END:
return FWL_VKEY_End;
case WXK_HOME:
return FWL_VKEY_Home;
case WXK_INSERT:
return FWL_VKEY_Insert;
case WXK_DELETE:
return FWL_VKEY_Delete;
default:
return keyCode;
}
}
//
// wxPDFViewActivity
//
class wxPDFViewActivity
{
public:
wxPDFViewActivity(wxPDFViewImpl* pdfViewImpl, const wxString& description):
m_impl(pdfViewImpl)
{
m_impl->SendActivity(description);
}
~wxPDFViewActivity()
{
m_impl->SendActivity("");
}
private:
wxPDFViewImpl* m_impl;
};
//
// wxPDFViewImpl
//
wxAtomicInt wxPDFViewImpl::ms_pdfSDKRefCount = 0;
v8::Platform* wxPDFViewImpl::ms_platform = 0;
wxPDFViewImpl::wxPDFViewImpl(wxPDFView* ctrl):
m_ctrl(ctrl),
m_handCursor(wxCURSOR_HAND),
m_defaultCursor(wxCURSOR_ARROW)
{
AcquireSDK();
SetPages(&m_pages);
m_printValidator = NULL;
m_zoomType = wxPDFVIEW_ZOOM_TYPE_FREE;
m_pagePadding = 16;
m_scrollStepX = 20;
m_scrollStepY = 20;
m_zoom = 1.0;
m_minZoom = 0.1;
m_maxZoom = 10.0;
m_pDataStream.reset();
m_pdfDoc = NULL;
m_pdfForm = NULL;
m_pdfAvail = NULL;
m_mostVisiblePage = -1;
m_bookmarks = NULL;
m_currentFindIndex = -1;
m_docPermissions = 0;
m_linearized = false;
m_backPage = -1;
// PDF SDK structures
memset(&m_pdfFileAccess, '\0', sizeof(m_pdfFileAccess));
m_pdfFileAccess.m_FileLen = 0;
m_pdfFileAccess.m_GetBlock = Get_Block;
m_pdfFileAccess.m_Param = this;
memset(&m_pdfFileAvail, '\0', sizeof(m_pdfFileAvail));
m_pdfFileAvail.version = 1;
m_pdfFileAvail.IsDataAvail = Is_Data_Avail;
memset(&m_hints, '\0', sizeof(m_hints));
m_hints.version = 1;
m_hints.AddSegment = Add_Segment;
m_ctrl->Bind(wxEVT_PAINT, &wxPDFViewImpl::OnPaint, this);
m_ctrl->Bind(wxEVT_SIZE, &wxPDFViewImpl::OnSize, this);
m_ctrl->Bind(wxEVT_MOUSEWHEEL, &wxPDFViewImpl::OnMouseWheel, this);
m_ctrl->Bind(wxEVT_MOTION, &wxPDFViewImpl::OnMouseMotion, this);
m_ctrl->Bind(wxEVT_LEFT_UP, &wxPDFViewImpl::OnMouseLeftUp, this);
m_ctrl->Bind(wxEVT_LEFT_DOWN, &wxPDFViewImpl::OnMouseLeftDown, this);
m_ctrl->Bind(wxEVT_KEY_DOWN, &wxPDFViewImpl::OnKeyDown, this);
m_ctrl->Bind(wxEVT_KEY_UP, &wxPDFViewImpl::OnKeyUp, this);
m_ctrl->Bind(wxEVT_CHAR, &wxPDFViewImpl::OnKeyChar, this);
}
wxPDFViewImpl::~wxPDFViewImpl()
{
CloseDocument();
ReleaseSDK();
}
void wxPDFViewImpl::NavigateToPage(wxPDFViewPageNavigation pageNavigation)
{
switch (pageNavigation)
{
case wxPDFVIEW_PAGE_NAV_NEXT:
{
int nextPage = GetMostVisiblePage();
if (GetPagePosition(nextPage) == wxPDFVIEW_PAGE_POS_LEFT)
nextPage++;
GoToPage(nextPage + 1);
break;
}
case wxPDFVIEW_PAGE_NAV_PREV:
GoToPage(GetMostVisiblePage() - 1);
break;
case wxPDFVIEW_PAGE_NAV_FIRST:
GoToPage(0);
break;
case wxPDFVIEW_PAGE_NAV_LAST:
GoToPage(GetPageCount() - 1);
break;
}
}
void wxPDFViewImpl::UpdateDocumentInfo()
{
UpdateVirtualSize();
CalcZoomLevel();
wxCommandEvent readyEvent(wxEVT_PDFVIEW_DOCUMENT_READY);
m_ctrl->ProcessEvent(readyEvent);
wxCommandEvent pgEvent(wxEVT_PDFVIEW_PAGE_CHANGED);
pgEvent.SetInt(0);
m_ctrl->ProcessEvent(pgEvent);
}
void wxPDFViewImpl::RecalculatePageRects()
{
m_pageRects.clear();
m_pageRects.reserve(GetPageCount());
wxSize defaultPageSize = wxDefaultSize;
m_maxPageHeight = 0;
#ifdef __WXMSW__
HDC desktopDc = ::GetDC(NULL);
int dpiX = ::GetDeviceCaps(desktopDc, LOGPIXELSX);
double screenScale = dpiX / (double)96;
#endif
m_docSize.Set(0, 0);
wxRect pageRect;
wxRect prevPageRect;
for (int i = 0; i < GetPageCount(); ++i)
{
bool pageAvail = !m_linearized || FPDFAvail_IsPageAvail(m_pdfAvail, i, &m_hints) != 0;
wxPDFViewPagePosition pagePos = GetPagePosition(i);
wxSize pageSize;
double width;
double height;
if (pageAvail && FPDF_GetPageSizeByIndex(m_pdfDoc, i, &width, &height))
{
pageSize = wxSize(width, height);
if (!defaultPageSize.IsFullySpecified())
defaultPageSize = pageSize;
} else
pageSize = defaultPageSize;
#ifdef __WXMSW__
pageSize *= screenScale;
#endif
if (pagePos != wxPDFVIEW_PAGE_POS_RIGHT)
pageRect.y += m_pagePadding / 2;
pageRect.SetSize(pageSize);
m_pageRects.push_back(pageRect);
int pageWidth = pageSize.x;
if (pagePos != wxPDFVIEW_PAGE_POS_CENTER && i > 0)
pageWidth *= 2;
if (pageWidth > m_docSize.x)
m_docSize.x = pageWidth;
if (pageSize.y > m_maxPageHeight)
m_maxPageHeight = pageSize.y;
if (pagePos != wxPDFVIEW_PAGE_POS_LEFT)
{
pageRect.x = 0;
if (prevPageRect.height > pageSize.y)
pageRect.y += prevPageRect.height;
else
pageRect.y += pageSize.y;
pageRect.y += m_pagePadding / 2;
// Make sure every page top is pixel exact scrollable
int pageDisplayHeight = pageRect.height + m_pagePadding;
int scrollMod = pageDisplayHeight % m_scrollStepY;
if (scrollMod)
pageRect.y += m_scrollStepY - scrollMod;
}
else
pageRect.x += pageSize.x;
prevPageRect = pageRect;
}
m_docSize.SetHeight(pageRect.y - (m_pagePadding / 2));
AlignPageRects();
}
void wxPDFViewImpl::AlignPageRects()
{
int ctrlWidth = m_ctrl->GetVirtualSize().GetWidth() / m_ctrl->GetScaleX();
int pageIndex = 0;
for (auto it = m_pageRects.begin(); it != m_pageRects.end(); ++it, ++pageIndex)
{
switch (GetPagePosition(pageIndex))
{
case wxPDFVIEW_PAGE_POS_CENTER:
it->x = (ctrlWidth - it->width) / 2;
break;
case wxPDFVIEW_PAGE_POS_LEFT:
it->x = (ctrlWidth / 2) - it->width;
break;
case wxPDFVIEW_PAGE_POS_RIGHT:
it->x = (ctrlWidth / 2);
break;
}
}
}
void wxPDFViewImpl::OnPaint(wxPaintEvent& WXUNUSED(event))
{
wxAutoBufferedPaintDC dc(m_ctrl);
m_ctrl->PrepareDC(dc);
wxSharedPtr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
wxRect rectUpdate = m_ctrl->GetUpdateClientRect();
rectUpdate.SetPosition(m_ctrl->CalcUnscrolledPosition(rectUpdate.GetPosition()));
rectUpdate = ScaledToUnscaled(rectUpdate);
dc.SetBackground(m_ctrl->GetBackgroundColour());
dc.Clear();
// Draw visible pages
if (GetFirstVisiblePage() < 0)
return;
for (int pageIndex = GetFirstVisiblePage(); pageIndex <= GetLastVisiblePage(); ++pageIndex)
{
wxRect pageRect = m_pageRects[pageIndex];
if (pageRect.Intersects(rectUpdate))
{
m_pages[pageIndex].Draw(this, dc, *gc, pageRect);
if (GetPagePosition(pageIndex) == wxPDFVIEW_PAGE_POS_RIGHT)
{
// Draw line between left and right page
gc->SetPen(*wxLIGHT_GREY_PEN);
wxPoint2DDouble linePoints[2] = {
{ (wxDouble)pageRect.x, (wxDouble)pageRect.y },
{ (wxDouble)pageRect.x, (wxDouble)pageRect.y + pageRect.height }
};
gc->DrawLines(2, linePoints);
}
}
}
// Draw text selections
gc->SetBrush(wxColor(0, 0, 200, 50));
gc->SetPen(*wxTRANSPARENT_PEN);
for (auto it = m_selection.begin(); it != m_selection.end(); ++it)
{
int pageIndex = it->GetPage()->GetIndex();
if (IsPageVisible(pageIndex))
{
wxRect pageRect = m_pageRects[pageIndex];
if (pageRect.Intersects(rectUpdate))
{
// Screen rects are relative to the page
wxVector<wxRect> screenRects = it->GetScreenRects(m_pageRects[pageIndex]);
for (auto sr = screenRects.begin(); sr != screenRects.end(); ++sr)
{
sr->Offset(m_pageRects[pageIndex].GetPosition());
gc->DrawRectangle(sr->x, sr->y, sr->width, sr->height);
}
}
}
}
}
void wxPDFViewImpl::OnSize(wxSizeEvent& event)
{
AlignPageRects();
CalcZoomLevel();
CalcVisiblePages();
event.Skip();
}
void wxPDFViewImpl::OnMouseWheel(wxMouseEvent& event)
{
if (event.ControlDown() && event.GetWheelRotation() != 0)
{
double currentZoom = m_zoom;
double delta;
if ( currentZoom < 100 )
delta = 0.05;
else if ( currentZoom <= 120 )
delta = 0.1;
else
delta = 0.5;
if ( event.GetWheelRotation() < 0 )
delta = -delta;
SetZoom(currentZoom + delta);
} else
event.Skip();
}
void wxPDFViewImpl::OnMouseMotion(wxMouseEvent& event)
{
if (EvaluateLinkTargetPageAtClientPos(event.GetPosition(), event.GetEventType()))
m_ctrl->SetCursor(m_handCursor);
else
m_ctrl->SetCursor(m_defaultCursor);
event.Skip();
}
void wxPDFViewImpl::OnMouseLeftDown(wxMouseEvent& event)
{
EvaluateLinkTargetPageAtClientPos(event.GetPosition(), event.GetEventType());
event.Skip();
}
void wxPDFViewImpl::OnMouseLeftUp(wxMouseEvent& event)
{
if (EvaluateLinkTargetPageAtClientPos(event.GetPosition(), event.GetEventType()))
m_ctrl->SetCursor(m_defaultCursor);
event.Skip();
}
void wxPDFViewImpl::OnKeyUp(wxKeyEvent& event)
{
if (!FORM_OnKeyUp(m_pdfForm, m_pages[GetMostVisiblePage()].GetPage(),
wxConvertKeyCodeToPdf(event.GetKeyCode()), wxConvertModifiersToPdf(event.GetModifiers())))
event.Skip();
}
void wxPDFViewImpl::OnKeyDown(wxKeyEvent& event)
{
int keyCode = wxConvertKeyCodeToPdf(event.GetKeyCode());
switch (keyCode)
{
case WXK_ESCAPE:
case WXK_BACK:
case WXK_RETURN:
case WXK_SPACE:
event.Skip();
break;
default:
if (!FORM_OnKeyDown(m_pdfForm, m_pages[GetMostVisiblePage()].GetPage(), keyCode,
wxConvertModifiersToPdf(event.GetModifiers())))
event.Skip();
break;
}
}
void wxPDFViewImpl::OnKeyChar(wxKeyEvent& event)
{
if (!FORM_OnChar(m_pdfForm, m_pages[GetMostVisiblePage()].GetPage(),
wxConvertKeyCodeToPdf(event.GetKeyCode()), wxConvertModifiersToPdf(event.GetModifiers())))
event.Skip();
}
void wxPDFViewImpl::GoToPage(int pageIndex, const wxRect* centerRect)
{
if (pageIndex < 0)
pageIndex = 0;
else if (pageIndex >= GetPageCount())
pageIndex = GetPageCount() - 1;
m_backPage = GetMostVisiblePage();
wxRect pageRect = m_pageRects[pageIndex];
int scrollTop = pageRect.GetTop() - m_pagePadding / 2;
int pixelsPerUnitY;
m_ctrl->GetScrollPixelsPerUnit(NULL, &pixelsPerUnitY);
int scrollPosY = (scrollTop * m_ctrl->GetScaleY()) / pixelsPerUnitY;
m_ctrl->Scroll(-1, scrollPosY);
}
void wxPDFViewImpl::GoToPage(int pageIndex)
{
GoToPage(pageIndex, NULL);
}
void wxPDFViewImpl::GoToRemote(const wxString& path)
{
wxCommandEvent gotoEvt(wxEVT_PDFVIEW_REMOTE_GOTO);
gotoEvt.SetString(path);
m_ctrl->AddPendingEvent(gotoEvt);
}
void wxPDFViewImpl::DoGoToAction(int pageIndex)
{
CallAfter(&wxPDFViewImpl::GoToPage, pageIndex);
}
void wxPDFViewImpl::UpdateVirtualSize()
{
int scrollSizeX = m_docSize.x * m_ctrl->GetScaleX();
m_ctrl->SetVirtualSize(scrollSizeX, m_docSize.y * m_ctrl->GetScaleY());
m_ctrl->SetScrollRate(wxRound(m_scrollStepX * m_ctrl->GetScaleX()), wxRound(m_scrollStepY * m_ctrl->GetScaleY()));
int pixelsPerUnitX;
m_ctrl->GetScrollPixelsPerUnit(&pixelsPerUnitX, NULL);
wxSize clientSize = m_ctrl->GetClientSize();
int scrollPosX = (scrollSizeX - clientSize.x) / 2;
scrollPosX /= pixelsPerUnitX;
m_ctrl->Scroll(scrollPosX, -1);
}
void wxPDFViewImpl::SetZoom(double zoom)
{
if (zoom < m_minZoom)
zoom = m_minZoom;
else if (zoom > m_maxZoom)
zoom = m_maxZoom;
if (zoom == m_zoom)
return;
m_zoom = zoom;
m_ctrl->SetScale(m_zoom, m_zoom);
UpdateVirtualSize();
AlignPageRects();
CalcVisiblePages();
m_ctrl->Refresh();
wxCommandEvent zoomEvent(wxEVT_PDFVIEW_ZOOM_CHANGED);
m_ctrl->ProcessEvent(zoomEvent);
}
void wxPDFViewImpl::SetZoomType(wxPDFViewZoomType zoomType)
{
if (m_zoomType == zoomType)
return;
m_zoomType = zoomType;
RecalculatePageRects();
CalcZoomLevel();
CalcVisiblePages();
m_ctrl->Refresh();
wxCommandEvent zoomEvent(wxEVT_PDFVIEW_ZOOM_TYPE_CHANGED);
m_ctrl->ProcessEvent(zoomEvent);
}
void wxPDFViewImpl::SetDisplayFlags(int flags)
{
if (m_displayFlags == flags)
return;
m_displayFlags = flags;
RecalculatePageRects();
CalcZoomLevel();
CalcVisiblePages();
m_ctrl->Refresh();
}
int wxPDFViewImpl::GetDisplayFlags() const
{
return m_displayFlags;
}
void wxPDFViewImpl::StopFind()
{
m_selection.clear();
m_findResults.clear();
m_nextPageToSearch = -1;
m_lastPageToSearch = -1;
m_lastCharacterIndexToSearch = -1;
m_currentFindIndex = -1;
m_findText.clear();
m_ctrl->Refresh();
}
long wxPDFViewImpl::Find(const wxString& text, int flags)
{
if (m_pages.empty())
return wxNOT_FOUND;
bool firstSearch = false;
int characterToStartSearchingFrom = 0;
if (m_findText != text) // First time we search for this text.
{
firstSearch = true;
wxVector<wxPDFViewTextRange> oldSelection = m_selection;
StopFind();
m_findText = text;
if (m_findText.empty())
return wxNOT_FOUND;
if (oldSelection.empty()) {
// Start searching from the beginning of the document.
m_nextPageToSearch = -1;
m_lastPageToSearch = GetPageCount() - 1;
m_lastCharacterIndexToSearch = -1;
} else {
// There's a current selection, so start from it.
m_nextPageToSearch = oldSelection[0].GetPage()->GetIndex();
m_lastCharacterIndexToSearch = oldSelection[0].GetCharIndex();
characterToStartSearchingFrom = oldSelection[0].GetCharIndex();
m_lastPageToSearch = m_nextPageToSearch;
}
}
if (m_findText.empty())
return wxNOT_FOUND;
bool caseSensitive = flags & wxPDFVIEW_FIND_MATCH_CASE;
bool forward = (flags & wxPDFVIEW_FIND_BACKWARDS) == 0;
// Move the find index
if (forward)
++m_currentFindIndex;
else
--m_currentFindIndex;
// Determine if we need more results
bool needMoreResults = true;
if (m_currentFindIndex == static_cast<int>(m_findResults.size()))
m_nextPageToSearch++;
else if (m_currentFindIndex < 0)
m_nextPageToSearch--;
else
needMoreResults = false;
while (needMoreResults
&& m_nextPageToSearch < GetPageCount()
&& m_nextPageToSearch >= 0)
{
int resultCount = FindOnPage(m_nextPageToSearch, caseSensitive, firstSearch, characterToStartSearchingFrom);
if (resultCount)
needMoreResults = false;
else if (forward)
++m_nextPageToSearch;
else
--m_nextPageToSearch;
}
if (m_findResults.empty())
return wxNOT_FOUND;
// Wrap find index
if (m_currentFindIndex < 0)
m_currentFindIndex = m_findResults.size() - 1;
else if (m_currentFindIndex >= (int) m_findResults.size())
m_currentFindIndex = 0;
// Select result
m_selection.clear();
wxPDFViewTextRange result = m_findResults[m_currentFindIndex];
m_selection.push_back(result);
int resultPageIndex = result.GetPage()->GetIndex();
// Make selection visible
if (!IsPageVisible(resultPageIndex))
GoToPage(resultPageIndex); // TODO: center selection rect
else
m_ctrl->Refresh();
return m_findResults.size();
}
int wxPDFViewImpl::FindOnPage(int pageIndex, bool caseSensitive, bool firstSearch, int WXUNUSED(characterToStartSearchingFrom))
{
// Find all the matches in the current page.
unsigned long flags = caseSensitive ? FPDF_MATCHCASE : 0;
wxMBConvUTF16LE conv;
FPDF_SCHHANDLE find = FPDFText_FindStart(
m_pages[pageIndex].GetTextPage(),
#ifdef __WXMSW__
reinterpret_cast<FPDF_WIDESTRING>(m_findText.wc_str(conv)),
#else
reinterpret_cast<FPDF_WIDESTRING>((const char*)m_findText.mb_str(conv)),
#endif
flags, 0);
wxPDFViewPage& page = m_pages[pageIndex];
int resultCount = 0;
while (FPDFText_FindNext(find))
{
wxPDFViewTextRange result(&page,
FPDFText_GetSchResultIndex(find),
FPDFText_GetSchCount(find));
if (!firstSearch &&
m_lastCharacterIndexToSearch != -1 &&
result.GetPage()->GetIndex() == m_lastPageToSearch &&
result.GetCharIndex() >= m_lastCharacterIndexToSearch)
{
break;
}
AddFindResult(result);
++resultCount;
}
FPDFText_FindClose(find);
return resultCount;
}
void wxPDFViewImpl::AddFindResult(const wxPDFViewTextRange& result)
{
// Figure out where to insert the new location, since we could have
// started searching midway and now we wrapped.
size_t i;
int pageIndex = result.GetPage()->GetIndex();
int charIndex = result.GetCharIndex();
for (i = 0; i < m_findResults.size(); ++i)
{
if (m_findResults[i].GetPage()->GetIndex() > pageIndex ||
(m_findResults[i].GetPage()->GetIndex() == pageIndex &&
m_findResults[i].GetCharIndex() > charIndex))
{
break;
}
}
m_findResults.insert(m_findResults.begin() + i, result);
}
bool wxPDFViewImpl::IsPrintAllowed() const
{
if (m_printValidator)
{
switch (m_printValidator->GetPrintPermission())
{
case wxPDFViewPrintValidator::Print_Allow: