Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fwl / src / basewidget / fwl_pictureboximp.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "../../../foxitlib.h"\r
8 #include "../core/include/fwl_targetimp.h"\r
9 #include "../core/include/fwl_noteimp.h"\r
10 #include "../core/include/fwl_widgetimp.h"\r
11 #include "include/fwl_pictureboximp.h"\r
12 IFWL_PictureBox* IFWL_PictureBox::Create()\r
13 {\r
14     return new IFWL_PictureBox;\r
15 }\r
16 IFWL_PictureBox::IFWL_PictureBox()\r
17 {\r
18     m_pData = NULL;\r
19 }\r
20 IFWL_PictureBox::~IFWL_PictureBox()\r
21 {\r
22     if (m_pData) {\r
23         delete (CFWL_PictureBoxImp*)m_pData;\r
24         m_pData = NULL;\r
25     }\r
26 }\r
27 FWL_ERR IFWL_PictureBox::Initialize(IFWL_Widget *pOuter)\r
28 {\r
29     m_pData = FX_NEW CFWL_PictureBoxImp(pOuter);\r
30     ((CFWL_PictureBoxImp*)m_pData)->SetInterface(this);\r
31     return ((CFWL_PictureBoxImp*)m_pData)->Initialize();\r
32 }\r
33 FWL_ERR IFWL_PictureBox::Initialize(const CFWL_WidgetImpProperties &properties, IFWL_Widget *pOuter)\r
34 {\r
35     m_pData = FX_NEW CFWL_PictureBoxImp(properties, pOuter);\r
36     ((CFWL_PictureBoxImp*)m_pData)->SetInterface(this);\r
37     return ((CFWL_PictureBoxImp*)m_pData)->Initialize();\r
38 }\r
39 CFWL_PictureBoxImp::CFWL_PictureBoxImp(IFWL_Widget *pOuter )\r
40     : CFWL_WidgetImp(pOuter)\r
41     , m_bTop(FALSE)\r
42     , m_bVCenter(FALSE)\r
43     , m_bButton(FALSE)\r
44 {\r
45     m_rtClient.Reset();\r
46     m_rtImage.Reset();\r
47     m_matrix.Reset();\r
48 }\r
49 CFWL_PictureBoxImp::CFWL_PictureBoxImp(const CFWL_WidgetImpProperties &properties, IFWL_Widget *pOuter )\r
50     : CFWL_WidgetImp(properties, pOuter)\r
51     , m_bTop(FALSE)\r
52     , m_bVCenter(FALSE)\r
53     , m_bButton(FALSE)\r
54 {\r
55     m_rtClient.Reset();\r
56     m_rtImage.Reset();\r
57     m_matrix.Reset();\r
58 }\r
59 CFWL_PictureBoxImp::~CFWL_PictureBoxImp()\r
60 {\r
61 }\r
62 FWL_ERR CFWL_PictureBoxImp::GetClassName(CFX_WideString &wsClass) const\r
63 {\r
64     wsClass = FWL_CLASS_PictureBox;\r
65     return FWL_ERR_Succeeded;\r
66 }\r
67 FX_DWORD CFWL_PictureBoxImp::GetClassID() const\r
68 {\r
69     return FWL_CLASSHASH_PictureBox;\r
70 }\r
71 FWL_ERR CFWL_PictureBoxImp::Initialize()\r
72 {\r
73     _FWL_ERR_CHECK_RETURN_VALUE_IF_FAIL(CFWL_WidgetImp::Initialize(), FWL_ERR_Indefinite);\r
74     m_pDelegate = (IFWL_WidgetDelegate*) FX_NEW CFWL_PictureBoxImpDelegate(this);\r
75     return FWL_ERR_Succeeded;\r
76 }\r
77 FWL_ERR CFWL_PictureBoxImp::Finalize()\r
78 {\r
79     if ( m_pDelegate) {\r
80         delete (CFWL_PictureBoxImpDelegate*)m_pDelegate;\r
81         m_pDelegate = NULL;\r
82     }\r
83     return CFWL_WidgetImp::Finalize();\r
84 }\r
85 FWL_ERR CFWL_PictureBoxImp::GetWidgetRect(CFX_RectF &rect, FX_BOOL bAutoSize )\r
86 {\r
87     if (bAutoSize) {\r
88         rect.Set(0, 0, 0, 0);\r
89         _FWL_RETURN_VALUE_IF_FAIL(m_pProperties->m_pDataProvider, FWL_ERR_Indefinite);\r
90         CFX_DIBitmap *pBitmap = ((IFWL_PictureBoxDP*)m_pProperties->m_pDataProvider)->GetPicture(m_pInterface);\r
91         if (pBitmap) {\r
92             rect.Set(0, 0, (FX_FLOAT)pBitmap->GetWidth(), (FX_FLOAT)pBitmap->GetHeight());\r
93         }\r
94         CFWL_WidgetImp::GetWidgetRect(rect, TRUE);\r
95     } else {\r
96         rect = m_pProperties->m_rtWidget;\r
97     }\r
98     return FWL_ERR_Succeeded;\r
99 }\r
100 FWL_ERR CFWL_PictureBoxImp::Update()\r
101 {\r
102     if (IsLocked()) {\r
103         return FWL_ERR_Succeeded;\r
104     }\r
105     if (!m_pProperties->m_pThemeProvider) {\r
106         m_pProperties->m_pThemeProvider = GetAvailableTheme();\r
107     }\r
108     GetClientRect(m_rtClient);\r
109     return FWL_ERR_Succeeded;\r
110 }\r
111 FWL_ERR CFWL_PictureBoxImp::DrawWidget(CFX_Graphics *pGraphics, const CFX_Matrix *pMatrix )\r
112 {\r
113     _FWL_RETURN_VALUE_IF_FAIL(pGraphics, FWL_ERR_Indefinite);\r
114     _FWL_RETURN_VALUE_IF_FAIL(m_pProperties->m_pThemeProvider, FWL_ERR_Indefinite);\r
115     IFWL_ThemeProvider *pTheme = GetAvailableTheme();\r
116     if (HasBorder()) {\r
117         DrawBorder(pGraphics, FWL_PART_PTB_Border, pTheme, pMatrix );\r
118     }\r
119     if (HasEdge()) {\r
120         DrawEdge(pGraphics, FWL_PART_PTB_Edge, pTheme, pMatrix);\r
121     }\r
122     DrawBkground(pGraphics, pTheme, pMatrix);\r
123     return FWL_ERR_Succeeded;\r
124 }\r
125 void CFWL_PictureBoxImp::DrawBkground(CFX_Graphics *pGraphics, IFWL_ThemeProvider *pTheme, const CFX_Matrix *pMatrix)\r
126 {\r
127     _FWL_RETURN_IF_FAIL(m_pProperties->m_pDataProvider);\r
128     CFX_DIBitmap *pPicture = ((IFWL_PictureBoxDP*)m_pProperties->m_pDataProvider)->GetPicture(m_pInterface);\r
129     CFX_Matrix matrix;\r
130     ((IFWL_PictureBoxDP*)m_pProperties->m_pDataProvider)->GetMatrix(m_pInterface, matrix);\r
131     if (!pPicture) {\r
132         return;\r
133     }\r
134     matrix.Concat(*pMatrix);\r
135     FX_FLOAT fx = (FX_FLOAT)pPicture->GetWidth();\r
136     FX_FLOAT fy = (FX_FLOAT)pPicture->GetHeight();\r
137     if (fx > m_rtClient.width) {\r
138         fx = m_rtClient.width;\r
139     }\r
140     if (fy > m_rtClient.height) {\r
141         fy = m_rtClient.height;\r
142     }\r
143     CFX_PointF pt;\r
144     pt.Set((m_rtClient.width - fx) / 2, (m_rtClient.height - fy) / 2);\r
145     pGraphics->DrawImage(pPicture, pt, &matrix);\r
146 }\r
147 FX_BOOL CFWL_PictureBoxImp::VStyle(FX_BOOL dwStyle)\r
148 {\r
149     switch(dwStyle & FWL_STYLEEXT_PTB_VAlignMask) {\r
150         case FWL_STYLEEXT_PTB_Top: {\r
151                 return m_bTop = TRUE;\r
152                 break;\r
153             }\r
154         case FWL_STYLEEXT_PTB_Vcenter: {\r
155                 return m_bVCenter = TRUE;\r
156                 break;\r
157             }\r
158         case FWL_STYLEEXT_PTB_Bottom: {\r
159                 return m_bButton = TRUE;\r
160                 break;\r
161             }\r
162     }\r
163     return FALSE;\r
164 }\r
165 CFWL_PictureBoxImpDelegate::CFWL_PictureBoxImpDelegate(CFWL_PictureBoxImp *pOwner)\r
166     : m_pOwner(pOwner)\r
167 {\r
168 }\r
169 FWL_ERR CFWL_PictureBoxImpDelegate::OnDrawWidget(CFX_Graphics *pGraphics, const CFX_Matrix *pMatrix)\r
170 {\r
171     return m_pOwner->DrawWidget(pGraphics, pMatrix);\r
172 }\r