• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
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 "third_party/base/ptr_util.h"
14 #include "third_party/base/stl_util.h"
15 #include "xfa/fwl/cfwl_app.h"
16 #include "xfa/fwl/cfwl_messagemouse.h"
17 #include "xfa/fwl/cfwl_messagemousewheel.h"
18 #include "xfa/fwl/cfwl_notedriver.h"
19 #include "xfa/fwl/cfwl_themebackground.h"
20 #include "xfa/fwl/cfwl_themepart.h"
21 #include "xfa/fwl/ifwl_themeprovider.h"
22 
23 #define FWL_SCROLLBAR_Elapse 500
24 
25 namespace {
26 
27 const float kMinThumbSize = 5.0f;
28 
29 }  // namespace
30 
CFWL_ScrollBar(const CFWL_App * app,std::unique_ptr<CFWL_WidgetProperties> properties,CFWL_Widget * pOuter)31 CFWL_ScrollBar::CFWL_ScrollBar(
32     const CFWL_App* app,
33     std::unique_ptr<CFWL_WidgetProperties> properties,
34     CFWL_Widget* pOuter)
35     : CFWL_Widget(app, std::move(properties), pOuter) {}
36 
37 CFWL_ScrollBar::~CFWL_ScrollBar() = default;
38 
GetClassID() const39 FWL_Type CFWL_ScrollBar::GetClassID() const {
40   return FWL_Type::ScrollBar;
41 }
42 
Update()43 void CFWL_ScrollBar::Update() {
44   if (IsLocked())
45     return;
46   if (!m_pProperties->m_pThemeProvider)
47     m_pProperties->m_pThemeProvider = GetAvailableTheme();
48 
49   Layout();
50 }
51 
DrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)52 void CFWL_ScrollBar::DrawWidget(CXFA_Graphics* pGraphics,
53                                 const CFX_Matrix& matrix) {
54   if (!pGraphics)
55     return;
56   if (!m_pProperties->m_pThemeProvider)
57     return;
58 
59   IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider.Get();
60   if (HasBorder())
61     DrawBorder(pGraphics, CFWL_Part::Border, pTheme, matrix);
62   DrawTrack(pGraphics, pTheme, true, &matrix);
63   DrawTrack(pGraphics, pTheme, false, &matrix);
64   DrawArrowBtn(pGraphics, pTheme, true, &matrix);
65   DrawArrowBtn(pGraphics, pTheme, false, &matrix);
66   DrawThumb(pGraphics, pTheme, &matrix);
67 }
68 
SetTrackPos(float fTrackPos)69 void CFWL_ScrollBar::SetTrackPos(float fTrackPos) {
70   m_fTrackPos = fTrackPos;
71   m_rtThumb = CalcThumbButtonRect(m_rtThumb);
72   m_rtMinTrack = CalcMinTrackRect(m_rtMinTrack);
73   m_rtMaxTrack = CalcMaxTrackRect(m_rtMaxTrack);
74 }
75 
DoScroll(CFWL_EventScroll::Code dwCode,float fPos)76 bool CFWL_ScrollBar::DoScroll(CFWL_EventScroll::Code dwCode, float fPos) {
77   if (dwCode == CFWL_EventScroll::Code::None)
78     return false;
79   return OnScroll(dwCode, fPos);
80 }
81 
DrawTrack(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,bool bLower,const CFX_Matrix * pMatrix)82 void CFWL_ScrollBar::DrawTrack(CXFA_Graphics* pGraphics,
83                                IFWL_ThemeProvider* pTheme,
84                                bool bLower,
85                                const CFX_Matrix* pMatrix) {
86   CFWL_ThemeBackground param;
87   param.m_pWidget = this;
88   param.m_iPart = bLower ? CFWL_Part::LowerTrack : CFWL_Part::UpperTrack;
89   param.m_dwStates = (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)
90                          ? CFWL_PartState_Disabled
91                          : (bLower ? m_iMinTrackState : m_iMaxTrackState);
92   param.m_pGraphics = pGraphics;
93   param.m_matrix.Concat(*pMatrix);
94   param.m_rtPart = bLower ? m_rtMinTrack : m_rtMaxTrack;
95   pTheme->DrawBackground(param);
96 }
97 
DrawArrowBtn(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,bool bMinBtn,const CFX_Matrix * pMatrix)98 void CFWL_ScrollBar::DrawArrowBtn(CXFA_Graphics* pGraphics,
99                                   IFWL_ThemeProvider* pTheme,
100                                   bool bMinBtn,
101                                   const CFX_Matrix* pMatrix) {
102   CFWL_ThemeBackground param;
103   param.m_pWidget = this;
104   param.m_iPart = bMinBtn ? CFWL_Part::ForeArrow : CFWL_Part::BackArrow;
105   param.m_dwStates = (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)
106                          ? CFWL_PartState_Disabled
107                          : (bMinBtn ? m_iMinButtonState : m_iMaxButtonState);
108   param.m_pGraphics = pGraphics;
109   param.m_matrix.Concat(*pMatrix);
110   param.m_rtPart = bMinBtn ? m_rtMinBtn : m_rtMaxBtn;
111   if (param.m_rtPart.height > 0 && param.m_rtPart.width > 0)
112     pTheme->DrawBackground(param);
113 }
114 
DrawThumb(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)115 void CFWL_ScrollBar::DrawThumb(CXFA_Graphics* pGraphics,
116                                IFWL_ThemeProvider* pTheme,
117                                const CFX_Matrix* pMatrix) {
118   CFWL_ThemeBackground param;
119   param.m_pWidget = this;
120   param.m_iPart = CFWL_Part::Thumb;
121   param.m_dwStates = (m_pProperties->m_dwStates & FWL_WGTSTATE_Disabled)
122                          ? CFWL_PartState_Disabled
123                          : m_iThumbButtonState;
124   param.m_pGraphics = pGraphics;
125   param.m_matrix.Concat(*pMatrix);
126   param.m_rtPart = m_rtThumb;
127   pTheme->DrawBackground(param);
128 }
129 
Layout()130 void CFWL_ScrollBar::Layout() {
131   m_rtClient = GetClientRect();
132 
133   CalcButtonLen();
134   m_rtMinBtn = CalcMinButtonRect();
135   m_rtMaxBtn = CalcMaxButtonRect();
136   m_rtThumb = CalcThumbButtonRect(m_rtThumb);
137   m_rtMinTrack = CalcMinTrackRect(m_rtMinTrack);
138   m_rtMaxTrack = CalcMaxTrackRect(m_rtMaxTrack);
139 }
140 
CalcButtonLen()141 void CFWL_ScrollBar::CalcButtonLen() {
142   m_fButtonLen = IsVertical() ? m_rtClient.width : m_rtClient.height;
143   float fLength = IsVertical() ? m_rtClient.height : m_rtClient.width;
144   if (fLength < m_fButtonLen * 2) {
145     m_fButtonLen = fLength / 2;
146     m_bMinSize = true;
147   } else {
148     m_bMinSize = false;
149   }
150 }
151 
CalcMinButtonRect()152 CFX_RectF CFWL_ScrollBar::CalcMinButtonRect() {
153   if (IsVertical())
154     return CFX_RectF(m_rtClient.TopLeft(), m_rtClient.width, m_fButtonLen);
155   return CFX_RectF(m_rtClient.TopLeft(), m_fButtonLen, m_rtClient.height);
156 }
157 
CalcMaxButtonRect()158 CFX_RectF CFWL_ScrollBar::CalcMaxButtonRect() {
159   if (IsVertical()) {
160     return CFX_RectF(m_rtClient.left, m_rtClient.bottom() - m_fButtonLen,
161                      m_rtClient.width, m_fButtonLen);
162   }
163   return CFX_RectF(m_rtClient.right() - m_fButtonLen, m_rtClient.top,
164                    m_fButtonLen, m_rtClient.height);
165 }
166 
CalcThumbButtonRect(const CFX_RectF & rtThumb)167 CFX_RectF CFWL_ScrollBar::CalcThumbButtonRect(const CFX_RectF& rtThumb) {
168   CFX_RectF rect;
169   if (!IsEnabled())
170     return rect;
171 
172   if (m_bMinSize) {
173     rect.left = rtThumb.left;
174     rect.top = rtThumb.top;
175     return rect;
176   }
177 
178   float fRange = m_fRangeMax - m_fRangeMin;
179   if (fRange < 0) {
180     if (IsVertical()) {
181       return CFX_RectF(m_rtClient.left, m_rtMaxBtn.bottom(), m_rtClient.width,
182                        0);
183     }
184     return CFX_RectF(m_rtMaxBtn.right(), m_rtClient.top, 0, m_rtClient.height);
185   }
186 
187   CFX_RectF rtClient = m_rtClient;
188   float fLength = IsVertical() ? rtClient.height : rtClient.width;
189   float fSize = m_fButtonLen;
190   fLength -= fSize * 2.0f;
191   if (fLength < fSize)
192     fLength = 0.0f;
193 
194   float fThumbSize = fLength * fLength / (fRange + fLength);
195   fThumbSize = std::max(fThumbSize, kMinThumbSize);
196 
197   float fDiff = std::max(fLength - fThumbSize, 0.0f);
198   float fTrackPos = pdfium::clamp(m_fTrackPos, m_fRangeMin, m_fRangeMax);
199   if (!fRange)
200     return rect;
201 
202   float iPos = fSize + fDiff * (fTrackPos - m_fRangeMin) / fRange;
203   rect.left = rtClient.left;
204   rect.top = rtClient.top;
205   if (IsVertical()) {
206     rect.top += iPos;
207     rect.width = rtClient.width;
208     rect.height = fThumbSize;
209   } else {
210     rect.left += iPos;
211     rect.width = fThumbSize;
212     rect.height = rtClient.height;
213   }
214   return rect;
215 }
216 
CalcMinTrackRect(const CFX_RectF & rtMinRect)217 CFX_RectF CFWL_ScrollBar::CalcMinTrackRect(const CFX_RectF& rtMinRect) {
218   CFX_RectF rect;
219   if (m_bMinSize) {
220     rect.left = rtMinRect.left;
221     rect.top = rtMinRect.top;
222     return rect;
223   }
224 
225   rect.left = m_rtClient.left;
226   rect.top = m_rtClient.top;
227   if (IsVertical()) {
228     rect.width = m_rtClient.width;
229     rect.height = (m_rtThumb.top + m_rtThumb.bottom()) / 2;
230   } else {
231     rect.width = (m_rtThumb.left + m_rtThumb.right()) / 2;
232     rect.height = m_rtClient.height;
233   }
234   return rect;
235 }
236 
CalcMaxTrackRect(const CFX_RectF & rtMaxRect)237 CFX_RectF CFWL_ScrollBar::CalcMaxTrackRect(const CFX_RectF& rtMaxRect) {
238   if (m_bMinSize)
239     return CFX_RectF(rtMaxRect.TopLeft(), 0, 0);
240 
241   if (IsVertical()) {
242     float iy = (m_rtThumb.top + m_rtThumb.bottom()) / 2;
243     return CFX_RectF(m_rtClient.left, iy, m_rtClient.width,
244                      m_rtClient.bottom() - iy);
245   }
246 
247   float ix = (m_rtThumb.left + m_rtThumb.right()) / 2;
248   return CFX_RectF(ix, m_rtClient.top, m_rtClient.height - ix,
249                    m_rtClient.height);
250 }
251 
GetTrackPointPos(const CFX_PointF & point)252 float CFWL_ScrollBar::GetTrackPointPos(const CFX_PointF& point) {
253   CFX_PointF diff = point - m_cpTrackPoint;
254   float fRange = m_fRangeMax - m_fRangeMin;
255   float fPos;
256 
257   if (IsVertical()) {
258     fPos = fRange * diff.y /
259            (m_rtMaxBtn.top - m_rtMinBtn.bottom() - m_rtThumb.height);
260   } else {
261     fPos = fRange * diff.x /
262            (m_rtMaxBtn.left - m_rtMinBtn.right() - m_rtThumb.width);
263   }
264 
265   fPos += m_fLastTrackPos;
266   return pdfium::clamp(fPos, m_fRangeMin, m_fRangeMax);
267 }
268 
SendEvent()269 bool CFWL_ScrollBar::SendEvent() {
270   if (m_iMinButtonState == CFWL_PartState_Pressed) {
271     DoScroll(CFWL_EventScroll::Code::StepBackward, m_fTrackPos);
272     return false;
273   }
274   if (m_iMaxButtonState == CFWL_PartState_Pressed) {
275     DoScroll(CFWL_EventScroll::Code::StepForward, m_fTrackPos);
276     return false;
277   }
278   if (m_iMinTrackState == CFWL_PartState_Pressed) {
279     DoScroll(CFWL_EventScroll::Code::PageBackward, m_fTrackPos);
280     return m_rtThumb.Contains(m_cpTrackPoint);
281   }
282   if (m_iMaxTrackState == CFWL_PartState_Pressed) {
283     DoScroll(CFWL_EventScroll::Code::PageForward, m_fTrackPos);
284     return m_rtThumb.Contains(m_cpTrackPoint);
285   }
286   if (m_iMouseWheel) {
287     CFWL_EventScroll::Code dwCode = m_iMouseWheel < 0
288                                         ? CFWL_EventScroll::Code::StepForward
289                                         : CFWL_EventScroll::Code::StepBackward;
290     DoScroll(dwCode, m_fTrackPos);
291   }
292   return true;
293 }
294 
OnScroll(CFWL_EventScroll::Code dwCode,float fPos)295 bool CFWL_ScrollBar::OnScroll(CFWL_EventScroll::Code dwCode, float fPos) {
296   CFWL_EventScroll ev(this);
297   ev.m_iScrollCode = dwCode;
298   ev.m_fPos = fPos;
299   DispatchEvent(&ev);
300   return true;
301 }
302 
OnProcessMessage(CFWL_Message * pMessage)303 void CFWL_ScrollBar::OnProcessMessage(CFWL_Message* pMessage) {
304   if (!pMessage)
305     return;
306 
307   CFWL_Message::Type type = pMessage->GetType();
308   if (type == CFWL_Message::Type::Mouse) {
309     CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
310     switch (pMsg->m_dwCmd) {
311       case FWL_MouseCommand::LeftButtonDown:
312         OnLButtonDown(pMsg->m_pos);
313         break;
314       case FWL_MouseCommand::LeftButtonUp:
315         OnLButtonUp(pMsg->m_pos);
316         break;
317       case FWL_MouseCommand::Move:
318         OnMouseMove(pMsg->m_pos);
319         break;
320       case FWL_MouseCommand::Leave:
321         OnMouseLeave();
322         break;
323       default:
324         break;
325     }
326   } else if (type == CFWL_Message::Type::MouseWheel) {
327     CFWL_MessageMouseWheel* pMsg =
328         static_cast<CFWL_MessageMouseWheel*>(pMessage);
329     OnMouseWheel(pMsg->m_delta);
330   }
331 }
332 
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)333 void CFWL_ScrollBar::OnDrawWidget(CXFA_Graphics* pGraphics,
334                                   const CFX_Matrix& matrix) {
335   DrawWidget(pGraphics, matrix);
336 }
337 
OnLButtonDown(const CFX_PointF & point)338 void CFWL_ScrollBar::OnLButtonDown(const CFX_PointF& point) {
339   if (!IsEnabled())
340     return;
341 
342   m_bMouseDown = true;
343   SetGrab(true);
344 
345   m_cpTrackPoint = point;
346   m_fLastTrackPos = m_fTrackPos;
347   if (m_rtMinBtn.Contains(point))
348     DoMouseDown(0, m_rtMinBtn, m_iMinButtonState, point);
349   else if (m_rtThumb.Contains(point))
350     DoMouseDown(1, m_rtThumb, m_iThumbButtonState, point);
351   else if (m_rtMaxBtn.Contains(point))
352     DoMouseDown(2, m_rtMaxBtn, m_iMaxButtonState, point);
353   else if (m_rtMinTrack.Contains(point))
354     DoMouseDown(3, m_rtMinTrack, m_iMinTrackState, point);
355   else
356     DoMouseDown(4, m_rtMaxTrack, m_iMaxTrackState, point);
357 
358   if (!SendEvent()) {
359     m_pTimer = pdfium::MakeUnique<CFX_Timer>(
360         GetOwnerApp()->GetAdapterNative()->GetTimerHandler(), this,
361         FWL_SCROLLBAR_Elapse);
362   }
363 }
364 
OnLButtonUp(const CFX_PointF & point)365 void CFWL_ScrollBar::OnLButtonUp(const CFX_PointF& point) {
366   m_pTimer.reset();
367   m_bMouseDown = false;
368   DoMouseUp(0, m_rtMinBtn, m_iMinButtonState, point);
369   DoMouseUp(1, m_rtThumb, m_iThumbButtonState, point);
370   DoMouseUp(2, m_rtMaxBtn, m_iMaxButtonState, point);
371   DoMouseUp(3, m_rtMinTrack, m_iMinTrackState, point);
372   DoMouseUp(4, m_rtMaxTrack, m_iMaxTrackState, point);
373   SetGrab(false);
374 }
375 
OnMouseMove(const CFX_PointF & point)376 void CFWL_ScrollBar::OnMouseMove(const CFX_PointF& point) {
377   DoMouseMove(0, m_rtMinBtn, m_iMinButtonState, point);
378   DoMouseMove(1, m_rtThumb, m_iThumbButtonState, point);
379   DoMouseMove(2, m_rtMaxBtn, m_iMaxButtonState, point);
380   DoMouseMove(3, m_rtMinTrack, m_iMinTrackState, point);
381   DoMouseMove(4, m_rtMaxTrack, m_iMaxTrackState, point);
382 }
383 
OnMouseLeave()384 void CFWL_ScrollBar::OnMouseLeave() {
385   DoMouseLeave(0, m_rtMinBtn, m_iMinButtonState);
386   DoMouseLeave(1, m_rtThumb, m_iThumbButtonState);
387   DoMouseLeave(2, m_rtMaxBtn, m_iMaxButtonState);
388   DoMouseLeave(3, m_rtMinTrack, m_iMinTrackState);
389   DoMouseLeave(4, m_rtMaxTrack, m_iMaxTrackState);
390 }
391 
OnMouseWheel(const CFX_PointF & delta)392 void CFWL_ScrollBar::OnMouseWheel(const CFX_PointF& delta) {
393   m_iMouseWheel = static_cast<int32_t>(delta.x);
394   SendEvent();
395   m_iMouseWheel = 0;
396 }
397 
DoMouseDown(int32_t iItem,const CFX_RectF & rtItem,int32_t & iState,const CFX_PointF & point)398 void CFWL_ScrollBar::DoMouseDown(int32_t iItem,
399                                  const CFX_RectF& rtItem,
400                                  int32_t& iState,
401                                  const CFX_PointF& point) {
402   if (!rtItem.Contains(point))
403     return;
404   if (iState == CFWL_PartState_Pressed)
405     return;
406 
407   iState = CFWL_PartState_Pressed;
408   RepaintRect(rtItem);
409 }
410 
DoMouseUp(int32_t iItem,const CFX_RectF & rtItem,int32_t & iState,const CFX_PointF & point)411 void CFWL_ScrollBar::DoMouseUp(int32_t iItem,
412                                const CFX_RectF& rtItem,
413                                int32_t& iState,
414                                const CFX_PointF& point) {
415   int32_t iNewState =
416       rtItem.Contains(point) ? CFWL_PartState_Hovered : CFWL_PartState_Normal;
417   if (iState == iNewState)
418     return;
419 
420   iState = iNewState;
421   RepaintRect(rtItem);
422   OnScroll(CFWL_EventScroll::Code::EndScroll, m_fTrackPos);
423 }
424 
DoMouseMove(int32_t iItem,const CFX_RectF & rtItem,int32_t & iState,const CFX_PointF & point)425 void CFWL_ScrollBar::DoMouseMove(int32_t iItem,
426                                  const CFX_RectF& rtItem,
427                                  int32_t& iState,
428                                  const CFX_PointF& point) {
429   if (!m_bMouseDown) {
430     int32_t iNewState =
431         rtItem.Contains(point) ? CFWL_PartState_Hovered : CFWL_PartState_Normal;
432     if (iState == iNewState)
433       return;
434 
435     iState = iNewState;
436     RepaintRect(rtItem);
437   } else if ((2 == iItem) && (m_iThumbButtonState == CFWL_PartState_Pressed)) {
438     m_fTrackPos = GetTrackPointPos(point);
439     OnScroll(CFWL_EventScroll::Code::TrackPos, m_fTrackPos);
440   }
441 }
442 
DoMouseLeave(int32_t iItem,const CFX_RectF & rtItem,int32_t & iState)443 void CFWL_ScrollBar::DoMouseLeave(int32_t iItem,
444                                   const CFX_RectF& rtItem,
445                                   int32_t& iState) {
446   if (iState == CFWL_PartState_Normal)
447     return;
448 
449   iState = CFWL_PartState_Normal;
450   RepaintRect(rtItem);
451 }
452 
DoMouseHover(int32_t iItem,const CFX_RectF & rtItem,int32_t & iState)453 void CFWL_ScrollBar::DoMouseHover(int32_t iItem,
454                                   const CFX_RectF& rtItem,
455                                   int32_t& iState) {
456   if (iState == CFWL_PartState_Hovered)
457     return;
458 
459   iState = CFWL_PartState_Hovered;
460   RepaintRect(rtItem);
461 }
462 
OnTimerFired()463 void CFWL_ScrollBar::OnTimerFired() {
464   m_pTimer.reset();
465   if (!SendEvent()) {
466     m_pTimer = pdfium::MakeUnique<CFX_Timer>(
467         GetOwnerApp()->GetAdapterNative()->GetTimerHandler(), this, 0);
468   }
469 }
470