forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEbookControls.cpp
More file actions
286 lines (246 loc) · 8.55 KB
/
Copy pathEbookControls.cpp
File metadata and controls
286 lines (246 loc) · 8.55 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
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
// utils
#include "BaseUtil.h"
#include "BitManip.h"
#include "HtmlParserLookup.h"
#include "Mui.h"
#include "SerializeTxt.h"
#include "StrSlice.h"
#include "Timer.h"
#include "TrivialHtmlParser.h"
#include "TxtParser.h"
#include "WinUtil.h"
// rendering engines
#include "EbookBase.h"
#include "HtmlFormatter.h"
// ui
#include "SumatraPDF.h"
#include "resource.h"
#include "EbookControls.h"
#include "MuiEbookPageDef.h"
#include "PagesLayoutDef.h"
#define NOLOG 1
#include "DebugLog.h"
PageControl::PageControl() : page(nullptr), cursorX(-1), cursorY(-1)
{
bit::Set(wantedInputBits, WantsMouseMoveBit, WantsMouseClickBit);
}
PageControl::~PageControl()
{
if (toolTip) {
// TODO: make Control's destructor clear the tooltip?
Control::NotifyMouseLeave();
}
}
void PageControl::SetPage(HtmlPage *newPage)
{
page = newPage;
RequestRepaint(this);
}
DrawInstr *PageControl::GetLinkAt(int x, int y) const
{
if (!page)
return nullptr;
PointF pt((REAL)(x - cachedStyle->padding.left), (REAL)(y - cachedStyle->padding.top));
for (DrawInstr& i : page->instructions) {
if (InstrLinkStart == i.type && !i.bbox.IsEmptyArea() && i.bbox.Contains(pt)) {
return &i;
}
}
return nullptr;
}
void PageControl::NotifyMouseMove(int x, int y)
{
DrawInstr *link = GetLinkAt(x, y);
if (!link) {
SetCursor(IDC_ARROW);
if (toolTip) {
Control::NotifyMouseLeave();
str::ReplacePtr(&toolTip, nullptr);
}
return;
}
SetCursor(IDC_HAND);
AutoFreeW url(str::conv::FromHtmlUtf8(link->str.s, link->str.len));
if (toolTip && (!url::IsAbsolute(url) || !str::Eq(toolTip, url))) {
Control::NotifyMouseLeave();
str::ReplacePtr(&toolTip, nullptr);
}
if (!toolTip && url::IsAbsolute(url)) {
toolTip = url.StealData();
Control::NotifyMouseEnter();
}
}
// size of the drawable area i.e. size minus padding
Size PageControl::GetDrawableSize() const
{
Size s;
pos.GetSize(&s);
Padding pad = cachedStyle->padding;
s.Width -= (pad.left + pad.right);
s.Height -= (pad.top + pad.bottom);
if ((s.Width <= 0) || (s.Height <= 0))
return Size();
return s;
}
void PageControl::Paint(Graphics *gfx, int offX, int offY)
{
CrashIf(!IsVisible());
Timer timerAll;
CachedStyle *s = cachedStyle;
Timer timerFill;
Rect r(offX, offY, pos.Width, pos.Height);
if (!s->bgColor->IsTransparent()) {
Brush *br = BrushFromColorData(s->bgColor, r);
gfx->FillRectangle(br, r);
}
double durFill = timerFill.Stop();
if (!page)
return;
// during resize the page we currently show might be bigger than
// our area. To avoid drawing outside our area we clip
Region origClipRegion;
gfx->GetClip(&origClipRegion);
r.X += s->padding.left;
r.Y += s->padding.top;
r.Width -= (s->padding.left + s->padding.right);
r.Height -= (s->padding.top + s->padding.bottom);
r.Inflate(1,0);
gfx->SetClip(r, CombineModeReplace);
COLORREF txtCol, bgCol;
GetEbookUiColors(txtCol, bgCol);
Color textColor, bgColor;
textColor.SetFromCOLORREF(txtCol);
ITextRender *textRender = CreateTextRender(GetTextRenderMethod(), gfx, pos.Width, pos.Height);
//ITextRender *textRender = CreateTextRender(TextRenderMethodHdc, gfx, pos.Width, pos.Height);
bgColor.SetFromCOLORREF(bgCol);
textRender->SetTextBgColor(bgColor);
Timer timerDrawHtml;
DrawHtmlPage(gfx, textRender, &page->instructions, (REAL)r.X, (REAL)r.Y, IsDebugPaint(), textColor);
double durDraw = timerDrawHtml.Stop();
gfx->SetClip(&origClipRegion, CombineModeReplace);
delete textRender;
double durAll = timerAll.Stop();
lf("all: %.2f, fill: %.2f, draw html: %.2f", durAll, durFill, durDraw);
}
Control *CreatePageControl(TxtNode *structDef)
{
CrashIf(!structDef->IsStructWithName("EbookPage"));
EbookPageDef *def = DeserializeEbookPageDef(structDef);
PageControl *c = new PageControl();
Style *style = StyleByName(def->style);
c->SetStyle(style);
if (def->name)
c->SetName(def->name);
FreeEbookPageDef(def);
return c;
}
ILayout *CreatePagesLayout(ParsedMui *parsedMui, TxtNode *structDef)
{
CrashIf(!structDef->IsStructWithName("PagesLayout"));
PagesLayoutDef *def = DeserializePagesLayoutDef(structDef);
CrashIf(!def->page1 || !def->page2);
PageControl *page1 = static_cast<PageControl*>(FindControlNamed(*parsedMui, def->page1));
PageControl *page2 = static_cast<PageControl*>(FindControlNamed(*parsedMui, def->page2));
CrashIf(!page1 || !page2);
PagesLayout *layout = new PagesLayout(page1, page2, def->spaceDx);
if (def->name)
layout->SetName(def->name);
FreePagesLayoutDef(def);
return layout;
}
void SetMainWndBgCol(EbookControls *ctrls)
{
COLORREF txtColor, bgColor;
GetEbookUiColors(txtColor, bgColor);
Style *styleMainWnd = StyleByName("styleMainWnd");
CrashIf(!styleMainWnd);
styleMainWnd->Set(Prop::AllocColorSolid(PropBgColor, GetRValueSafe(bgColor), GetGValueSafe(bgColor), GetBValueSafe(bgColor)));
ctrls->mainWnd->SetStyle(styleMainWnd);
Style *styleStatus = StyleByName("styleStatus");
styleStatus->Set(Prop::AllocColorSolid(PropBgColor, GetRValueSafe(bgColor), GetGValueSafe(bgColor), GetBValueSafe(bgColor)));
ctrls->status->SetStyle(styleStatus);
// TODO: should also allow to change text color
// TODO: also match the colors of progress bar to be based on background color
// TODO: update tab color
// note: callers are expected to update the background of tree control and
// other colors that are supposed to match background color
}
EbookControls *CreateEbookControls(HWND hwnd, FrameRateWnd *frameRateWnd)
{
static bool wasRegistered = false;
if (!wasRegistered) {
RegisterControlCreatorFor("EbookPage", &CreatePageControl);
RegisterLayoutCreatorFor("PagesLayout", &CreatePagesLayout);
wasRegistered = true;
}
ParsedMui *muiDef = new ParsedMui();
char *s = LoadTextResource(IDD_EBOOK_WIN_DESC);
MuiFromText(s, *muiDef);
free(s);
EbookControls *ctrls = new EbookControls;
ctrls->muiDef = muiDef;
CrashIf(!FindButtonVectorNamed(*muiDef, "nextButton"));
CrashIf(!FindButtonVectorNamed(*muiDef, "prevButton"));
ctrls->status = FindButtonNamed(*muiDef, "statusButton");
CrashIf(!ctrls->status);
ctrls->progress = FindScrollBarNamed(*muiDef, "progressScrollBar");
CrashIf(!ctrls->progress);
ctrls->progress->hCursor = GetCursor(IDC_HAND);
ctrls->topPart = FindLayoutNamed(*muiDef, "top");
CrashIf(!ctrls->topPart);
ctrls->pagesLayout = static_cast<PagesLayout*>(FindLayoutNamed(*muiDef, "pagesLayout"));
CrashIf(!ctrls->pagesLayout);
ctrls->mainWnd = new HwndWrapper(hwnd);
ctrls->mainWnd->frameRateWnd = frameRateWnd;
ctrls->mainWnd->SetMinSize(Size(320, 200));
SetMainWndBgCol(ctrls);
ctrls->mainWnd->layout = FindLayoutNamed(*muiDef, "mainLayout");
CrashIf(!ctrls->mainWnd->layout);
for (size_t i = 0; i < muiDef->allControls.Count(); i++) {
Control *c = muiDef->allControls.At(i);
ctrls->mainWnd->AddChild(c);
}
return ctrls;
}
void DestroyEbookControls(EbookControls* ctrls)
{
delete ctrls->mainWnd;
delete ctrls->topPart;
delete ctrls->pagesLayout;
delete ctrls->muiDef;
delete ctrls;
}
Size PagesLayout::Measure(const Size availableSize)
{
desiredSize = availableSize;
return desiredSize;
}
void PagesLayout::Arrange(const Rect finalRect)
{
// only page2 can be hidden
CrashIf(!page1->IsVisible());
// if only page1 visible, give it the whole area
if (!page2->IsVisible()) {
page1->Arrange(finalRect);
return;
}
// when both visible, give them equally sized areas
// with spaceDx between them
int dx = finalRect.Width;
if (page2->IsVisible()) {
dx = (dx / 2) - spaceDx;
// protect against excessive spaceDx values
if (dx <= 100) {
spaceDx = 0;
dx = dx /2;
CrashIf(dx < 10);
}
}
Rect r = finalRect;
r.Width = dx;
page1->Arrange(r);
r.X = r.X + dx + spaceDx;
page2->Arrange(r);
}