Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfwl_scrollbar.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "xfa/fwl/cfwl_scrollbar.h"
8
9#include <algorithm>
10#include <memory>
11#include <utility>
12
13#include "xfa/fwl/cfwl_app.h"
14#include "xfa/fwl/cfwl_messagemouse.h"
15#include "xfa/fwl/cfwl_messagemousewheel.h"
16#include "xfa/fwl/cfwl_notedriver.h"
17#include "xfa/fwl/cfwl_themebackground.h"
18#include "xfa/fwl/cfwl_themepart.h"
19#include "xfa/fwl/ifwl_themeprovider.h"
20
21namespace {
22
23constexpr int kScrollbarElapsedMsecs = 500;
24constexpr float kMinThumbSize = 5.0f;
25
26} // namespace
27
28CFWL_ScrollBar::CFWL_ScrollBar(CFWL_App* app,
29 const Properties& properties,
30 CFWL_Widget* pOuter)
31 : CFWL_Widget(app, properties, pOuter) {}
32
33CFWL_ScrollBar::~CFWL_ScrollBar() = default;
34
35FWL_Type CFWL_ScrollBar::GetClassID() const {
37}
38
39void CFWL_ScrollBar::Update() {
40 if (IsLocked())
41 return;
42
43 Layout();
44}
45
46void CFWL_ScrollBar::DrawWidget(CFGAS_GEGraphics* pGraphics,
47 const CFX_Matrix& matrix) {
48 if (!pGraphics)
49 return;
50
51 if (HasBorder())
52 DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
53
54 DrawLowerTrack(pGraphics, matrix);
55 DrawUpperTrack(pGraphics, matrix);
56 DrawMinArrowBtn(pGraphics, matrix);
57 DrawMaxArrowBtn(pGraphics, matrix);
58 DrawThumb(pGraphics, matrix);
59}
60
61void CFWL_ScrollBar::SetTrackPos(float fTrackPos) {
62 m_fTrackPos = fTrackPos;
63 m_ThumbRect = CalcThumbButtonRect(m_ThumbRect);
64 m_MinTrackRect = CalcMinTrackRect(m_MinTrackRect);
65 m_MaxTrackRect = CalcMaxTrackRect(m_MaxTrackRect);
66}
67
68bool CFWL_ScrollBar::DoScroll(CFWL_EventScroll::Code dwCode, float fPos) {
69 if (dwCode == CFWL_EventScroll::Code::None)
70 return false;
71 return OnScroll(dwCode, fPos);
72}
73
74void CFWL_ScrollBar::DrawUpperTrack(CFGAS_GEGraphics* pGraphics,
75 const CFX_Matrix& mtMatrix) {
76 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kUpperTrack, this,
77 pGraphics);
80 : m_iMaxTrackState;
81 param.m_matrix = mtMatrix;
82 param.m_PartRect = m_MaxTrackRect;
83 GetThemeProvider()->DrawBackground(param);
84}
85
86void CFWL_ScrollBar::DrawLowerTrack(CFGAS_GEGraphics* pGraphics,
87 const CFX_Matrix& mtMatrix) {
88 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kLowerTrack, this,
89 pGraphics);
92 : m_iMinTrackState;
93 param.m_matrix = mtMatrix;
94 param.m_PartRect = m_MinTrackRect;
95 GetThemeProvider()->DrawBackground(param);
96}
97
98void CFWL_ScrollBar::DrawMaxArrowBtn(CFGAS_GEGraphics* pGraphics,
99 const CFX_Matrix& mtMatrix) {
100 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kBackArrow, this, pGraphics);
103 : m_iMaxButtonState;
104 param.m_matrix = mtMatrix;
105 param.m_PartRect = m_MaxBtnRect;
106 if (param.m_PartRect.height > 0 && param.m_PartRect.width > 0)
107 GetThemeProvider()->DrawBackground(param);
108}
109
110void CFWL_ScrollBar::DrawMinArrowBtn(CFGAS_GEGraphics* pGraphics,
111 const CFX_Matrix& mtMatrix) {
112 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kForeArrow, this, pGraphics);
115 : m_iMinButtonState;
116 param.m_matrix = mtMatrix;
117 param.m_PartRect = m_MinBtnRect;
118 if (param.m_PartRect.height > 0 && param.m_PartRect.width > 0)
119 GetThemeProvider()->DrawBackground(param);
120}
121
122void CFWL_ScrollBar::DrawThumb(CFGAS_GEGraphics* pGraphics,
123 const CFX_Matrix& mtMatrix) {
124 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kThumb, this, pGraphics);
127 : m_iThumbButtonState;
128 param.m_matrix = mtMatrix;
129 param.m_PartRect = m_ThumbRect;
130 GetThemeProvider()->DrawBackground(param);
131}
132
133void CFWL_ScrollBar::Layout() {
134 m_ClientRect = GetClientRect();
135
136 CalcButtonLen();
137 m_MinBtnRect = CalcMinButtonRect();
138 m_MaxBtnRect = CalcMaxButtonRect();
139 m_ThumbRect = CalcThumbButtonRect(m_ThumbRect);
140 m_MinTrackRect = CalcMinTrackRect(m_MinTrackRect);
141 m_MaxTrackRect = CalcMaxTrackRect(m_MaxTrackRect);
142}
143
144void CFWL_ScrollBar::CalcButtonLen() {
145 m_fButtonLen = IsVertical() ? m_ClientRect.width : m_ClientRect.height;
146 float fLength = IsVertical() ? m_ClientRect.height : m_ClientRect.width;
147 if (fLength < m_fButtonLen * 2) {
148 m_fButtonLen = fLength / 2;
149 m_bMinSize = true;
150 } else {
151 m_bMinSize = false;
152 }
153}
154
155CFX_RectF CFWL_ScrollBar::CalcMinButtonRect() {
156 if (IsVertical())
157 return CFX_RectF(m_ClientRect.TopLeft(), m_ClientRect.width, m_fButtonLen);
158 return CFX_RectF(m_ClientRect.TopLeft(), m_fButtonLen, m_ClientRect.height);
159}
160
161CFX_RectF CFWL_ScrollBar::CalcMaxButtonRect() {
162 if (IsVertical()) {
163 return CFX_RectF(m_ClientRect.left, m_ClientRect.bottom() - m_fButtonLen,
164 m_ClientRect.width, m_fButtonLen);
165 }
166 return CFX_RectF(m_ClientRect.right() - m_fButtonLen, m_ClientRect.top,
167 m_fButtonLen, m_ClientRect.height);
168}
169
170CFX_RectF CFWL_ScrollBar::CalcThumbButtonRect(const CFX_RectF& rtThumb) {
171 CFX_RectF rect;
172 if (!IsEnabled())
173 return rect;
174
175 if (m_bMinSize) {
176 rect.left = rtThumb.left;
177 rect.top = rtThumb.top;
178 return rect;
179 }
180
181 float fRange = m_fRangeMax - m_fRangeMin;
182 if (fRange < 0) {
183 if (IsVertical()) {
184 return CFX_RectF(m_ClientRect.left, m_MaxBtnRect.bottom(),
185 m_ClientRect.width, 0);
186 }
187 return CFX_RectF(m_MaxBtnRect.right(), m_ClientRect.top, 0,
188 m_ClientRect.height);
189 }
190
191 CFX_RectF rtClient = m_ClientRect;
192 float fLength = IsVertical() ? rtClient.height : rtClient.width;
193 float fSize = m_fButtonLen;
194 fLength -= fSize * 2.0f;
195 if (fLength < fSize)
196 fLength = 0.0f;
197
198 float fThumbSize = fLength * fLength / (fRange + fLength);
199 fThumbSize = std::max(fThumbSize, kMinThumbSize);
200
201 float fDiff = std::max(fLength - fThumbSize, 0.0f);
202 float fTrackPos = std::clamp(m_fTrackPos, m_fRangeMin, m_fRangeMax);
203 if (!fRange)
204 return rect;
205
206 float iPos = fSize + fDiff * (fTrackPos - m_fRangeMin) / fRange;
207 rect.left = rtClient.left;
208 rect.top = rtClient.top;
209 if (IsVertical()) {
210 rect.top += iPos;
211 rect.width = rtClient.width;
212 rect.height = fThumbSize;
213 } else {
214 rect.left += iPos;
215 rect.width = fThumbSize;
216 rect.height = rtClient.height;
217 }
218 return rect;
219}
220
221CFX_RectF CFWL_ScrollBar::CalcMinTrackRect(const CFX_RectF& rtMinRect) {
222 CFX_RectF rect;
223 if (m_bMinSize) {
224 rect.left = rtMinRect.left;
225 rect.top = rtMinRect.top;
226 return rect;
227 }
228
229 rect.left = m_ClientRect.left;
230 rect.top = m_ClientRect.top;
231 if (IsVertical()) {
232 rect.width = m_ClientRect.width;
233 rect.height = (m_ThumbRect.top + m_ThumbRect.bottom()) / 2;
234 } else {
235 rect.width = (m_ThumbRect.left + m_ThumbRect.right()) / 2;
236 rect.height = m_ClientRect.height;
237 }
238 return rect;
239}
240
241CFX_RectF CFWL_ScrollBar::CalcMaxTrackRect(const CFX_RectF& rtMaxRect) {
242 if (m_bMinSize)
243 return CFX_RectF(rtMaxRect.TopLeft(), 0, 0);
244
245 if (IsVertical()) {
246 float iy = (m_ThumbRect.top + m_ThumbRect.bottom()) / 2;
247 return CFX_RectF(m_ClientRect.left, iy, m_ClientRect.width,
248 m_ClientRect.bottom() - iy);
249 }
250
251 float ix = (m_ThumbRect.left + m_ThumbRect.right()) / 2;
252 return CFX_RectF(ix, m_ClientRect.top, m_ClientRect.height - ix,
253 m_ClientRect.height);
254}
255
256float CFWL_ScrollBar::GetTrackPointPos(const CFX_PointF& point) {
257 CFX_PointF diff = point - m_cpTrackPoint;
258 float fRange = m_fRangeMax - m_fRangeMin;
259 float fPos;
260
261 if (IsVertical()) {
262 fPos = fRange * diff.y /
263 (m_MaxBtnRect.top - m_MinBtnRect.bottom() - m_ThumbRect.height);
264 } else {
265 fPos = fRange * diff.x /
266 (m_MaxBtnRect.left - m_MinBtnRect.right() - m_ThumbRect.width);
267 }
268
269 fPos += m_fLastTrackPos;
270 return std::clamp(fPos, m_fRangeMin, m_fRangeMax);
271}
272
273bool CFWL_ScrollBar::SendEvent() {
274 if (m_iMinButtonState == CFWL_PartState::kPressed) {
275 DoScroll(CFWL_EventScroll::Code::StepBackward, m_fTrackPos);
276 return false;
277 }
278 if (m_iMaxButtonState == CFWL_PartState::kPressed) {
279 DoScroll(CFWL_EventScroll::Code::StepForward, m_fTrackPos);
280 return false;
281 }
282 if (m_iMinTrackState == CFWL_PartState::kPressed) {
283 DoScroll(CFWL_EventScroll::Code::PageBackward, m_fTrackPos);
284 return m_ThumbRect.Contains(m_cpTrackPoint);
285 }
286 if (m_iMaxTrackState == CFWL_PartState::kPressed) {
287 DoScroll(CFWL_EventScroll::Code::PageForward, m_fTrackPos);
288 return m_ThumbRect.Contains(m_cpTrackPoint);
289 }
290 if (m_iMouseWheel) {
291 CFWL_EventScroll::Code dwCode = m_iMouseWheel < 0
292 ? CFWL_EventScroll::Code::StepForward
293 : CFWL_EventScroll::Code::StepBackward;
294 DoScroll(dwCode, m_fTrackPos);
295 }
296 return true;
297}
298
299bool CFWL_ScrollBar::OnScroll(CFWL_EventScroll::Code dwCode, float fPos) {
300 CFWL_EventScroll ev(this, dwCode, fPos);
301 DispatchEvent(&ev);
302 return true;
303}
304
305void CFWL_ScrollBar::OnProcessMessage(CFWL_Message* pMessage) {
306 CFWL_Message::Type type = pMessage->GetType();
307 if (type == CFWL_Message::Type::kMouse) {
308 CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
309 switch (pMsg->m_dwCmd) {
310 case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
311 OnLButtonDown(pMsg->m_pos);
312 break;
313 case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
314 OnLButtonUp(pMsg->m_pos);
315 break;
316 case CFWL_MessageMouse::MouseCommand::kMove:
317 OnMouseMove(pMsg->m_pos);
318 break;
319 case CFWL_MessageMouse::MouseCommand::kLeave:
320 OnMouseLeave();
321 break;
322 default:
323 break;
324 }
325 } else if (type == CFWL_Message::Type::kMouseWheel) {
326 auto* pMsg = static_cast<CFWL_MessageMouseWheel*>(pMessage);
327 OnMouseWheel(pMsg->delta());
328 }
329}
330
331void CFWL_ScrollBar::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
332 const CFX_Matrix& matrix) {
333 DrawWidget(pGraphics, matrix);
334}
335
336void CFWL_ScrollBar::OnLButtonDown(const CFX_PointF& point) {
337 if (!IsEnabled())
338 return;
339
340 m_bMouseDown = true;
341 SetGrab(true);
342
343 m_cpTrackPoint = point;
344 m_fLastTrackPos = m_fTrackPos;
345 if (m_MinBtnRect.Contains(point))
346 DoMouseDown(0, m_MinBtnRect, &m_iMinButtonState, point);
347 else if (m_ThumbRect.Contains(point))
348 DoMouseDown(1, m_ThumbRect, &m_iThumbButtonState, point);
349 else if (m_MaxBtnRect.Contains(point))
350 DoMouseDown(2, m_MaxBtnRect, &m_iMaxButtonState, point);
351 else if (m_MinTrackRect.Contains(point))
352 DoMouseDown(3, m_MinTrackRect, &m_iMinTrackState, point);
353 else
354 DoMouseDown(4, m_MaxTrackRect, &m_iMaxTrackState, point);
355
356 if (!SendEvent()) {
357 m_pTimer = std::make_unique<CFX_Timer>(GetFWLApp()->GetTimerHandler(), this,
358 kScrollbarElapsedMsecs);
359 }
360}
361
362void CFWL_ScrollBar::OnLButtonUp(const CFX_PointF& point) {
363 m_pTimer.reset();
364 m_bMouseDown = false;
365 DoMouseUp(0, m_MinBtnRect, &m_iMinButtonState, point);
366 DoMouseUp(1, m_ThumbRect, &m_iThumbButtonState, point);
367 DoMouseUp(2, m_MaxBtnRect, &m_iMaxButtonState, point);
368 DoMouseUp(3, m_MinTrackRect, &m_iMinTrackState, point);
369 DoMouseUp(4, m_MaxTrackRect, &m_iMaxTrackState, point);
370 SetGrab(false);
371}
372
373void CFWL_ScrollBar::OnMouseMove(const CFX_PointF& point) {
374 DoMouseMove(0, m_MinBtnRect, &m_iMinButtonState, point);
375 DoMouseMove(1, m_ThumbRect, &m_iThumbButtonState, point);
376 DoMouseMove(2, m_MaxBtnRect, &m_iMaxButtonState, point);
377 DoMouseMove(3, m_MinTrackRect, &m_iMinTrackState, point);
378 DoMouseMove(4, m_MaxTrackRect, &m_iMaxTrackState, point);
379}
380
381void CFWL_ScrollBar::OnMouseLeave() {
382 DoMouseLeave(0, m_MinBtnRect, &m_iMinButtonState);
383 DoMouseLeave(1, m_ThumbRect, &m_iThumbButtonState);
384 DoMouseLeave(2, m_MaxBtnRect, &m_iMaxButtonState);
385 DoMouseLeave(3, m_MinTrackRect, &m_iMinTrackState);
386 DoMouseLeave(4, m_MaxTrackRect, &m_iMaxTrackState);
387}
388
389void CFWL_ScrollBar::OnMouseWheel(const CFX_Vector& delta) {
390 m_iMouseWheel = delta.y;
391 SendEvent();
392 m_iMouseWheel = 0;
393}
394
395void CFWL_ScrollBar::DoMouseDown(int32_t iItem,
396 const CFX_RectF& rtItem,
397 CFWL_PartState* pState,
398 const CFX_PointF& point) {
399 if (!rtItem.Contains(point))
400 return;
401 if (*pState == CFWL_PartState::kPressed)
402 return;
403
404 *pState = CFWL_PartState::kPressed;
405 RepaintRect(rtItem);
406}
407
408void CFWL_ScrollBar::DoMouseUp(int32_t iItem,
409 const CFX_RectF& rtItem,
410 CFWL_PartState* pState,
411 const CFX_PointF& point) {
414 if (*pState == iNewState)
415 return;
416
417 *pState = iNewState;
418 RepaintRect(rtItem);
419 OnScroll(CFWL_EventScroll::Code::EndScroll, m_fTrackPos);
420}
421
422void CFWL_ScrollBar::DoMouseMove(int32_t iItem,
423 const CFX_RectF& rtItem,
424 CFWL_PartState* pState,
425 const CFX_PointF& point) {
426 if (!m_bMouseDown) {
429 if (*pState == iNewState)
430 return;
431
432 *pState = iNewState;
433 RepaintRect(rtItem);
434 } else if ((2 == iItem) &&
435 (m_iThumbButtonState == CFWL_PartState::kPressed)) {
436 m_fTrackPos = GetTrackPointPos(point);
437 OnScroll(CFWL_EventScroll::Code::TrackPos, m_fTrackPos);
438 }
439}
440
441void CFWL_ScrollBar::DoMouseLeave(int32_t iItem,
442 const CFX_RectF& rtItem,
443 CFWL_PartState* pState) {
444 if (*pState == CFWL_PartState::kNormal)
445 return;
446
447 *pState = CFWL_PartState::kNormal;
448 RepaintRect(rtItem);
449}
450
451void CFWL_ScrollBar::DoMouseHover(int32_t iItem,
452 const CFX_RectF& rtItem,
453 CFWL_PartState* pState) {
454 if (*pState == CFWL_PartState::kHovered)
455 return;
456
457 *pState = CFWL_PartState::kHovered;
458 RepaintRect(rtItem);
459}
460
461void CFWL_ScrollBar::OnTimerFired() {
462 m_pTimer.reset();
463 if (!SendEvent()) {
464 m_pTimer =
465 std::make_unique<CFX_Timer>(GetFWLApp()->GetTimerHandler(), this, 0);
466 }
467}
CFWL_PartState
FWL_Type
Definition cfwl_widget.h:46
#define FWL_STATE_WGT_Disabled
Definition cfwl_widget.h:41
const CFX_Vector & delta() const
const MouseCommand m_dwCmd
Type GetType() const
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void OnTimerFired() override
void OnProcessMessage(CFWL_Message *pMessage) override
void Update() override
FWL_Type GetClassID() const override
~CFWL_ScrollBar() override
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void SetTrackPos(float fTrackPos)
CFX_RectF m_PartRect
CFX_Matrix m_matrix
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
Properties m_Properties
bool HasBorder() const
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
void RepaintRect(const CFX_RectF &pRect)
IFWL_ThemeProvider * GetThemeProvider() const
bool IsEnabled() const
void SetGrab(bool bSet)
virtual CFX_RectF GetClientRect()
void DispatchEvent(CFWL_Event *pEvent)
bool IsLocked() const
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_RectF & operator=(const CFX_RectF &other)=default
float bottom() const
bool Contains(const PointType &p) const
float right() const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)