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/fxfa/cxfa_ffimageedit.h"
8
9 #include <utility>
10
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fwl/cfwl_app.h"
13 #include "xfa/fwl/cfwl_messagemouse.h"
14 #include "xfa/fwl/cfwl_notedriver.h"
15 #include "xfa/fwl/cfwl_picturebox.h"
16 #include "xfa/fxfa/cxfa_ffdoc.h"
17 #include "xfa/fxfa/cxfa_ffdocview.h"
18 #include "xfa/fxfa/cxfa_fffield.h"
19 #include "xfa/fxfa/cxfa_ffpageview.h"
20 #include "xfa/fxfa/cxfa_ffwidget.h"
21 #include "xfa/fxfa/parser/cxfa_border.h"
22 #include "xfa/fxfa/parser/cxfa_image.h"
23 #include "xfa/fxfa/parser/cxfa_para.h"
24 #include "xfa/fxfa/parser/cxfa_value.h"
25
CXFA_FFImageEdit(CXFA_Node * pNode)26 CXFA_FFImageEdit::CXFA_FFImageEdit(CXFA_Node* pNode)
27 : CXFA_FFField(pNode), m_pOldDelegate(nullptr) {}
28
~CXFA_FFImageEdit()29 CXFA_FFImageEdit::~CXFA_FFImageEdit() {
30 CXFA_FFImageEdit::UnloadWidget();
31 }
32
LoadWidget()33 bool CXFA_FFImageEdit::LoadWidget() {
34 auto pNew = pdfium::MakeUnique<CFWL_PictureBox>(GetFWLApp());
35 CFWL_PictureBox* pPictureBox = pNew.get();
36 m_pNormalWidget = std::move(pNew);
37 m_pNormalWidget->SetLayoutItem(this);
38
39 CFWL_NoteDriver* pNoteDriver =
40 m_pNormalWidget->GetOwnerApp()->GetNoteDriver();
41 pNoteDriver->RegisterEventTarget(m_pNormalWidget.get(),
42 m_pNormalWidget.get());
43 m_pOldDelegate = pPictureBox->GetDelegate();
44 pPictureBox->SetDelegate(this);
45
46 CXFA_FFField::LoadWidget();
47 if (!m_pNode->GetWidgetAcc()->GetImageEditImage())
48 UpdateFWLData();
49
50 return true;
51 }
52
UnloadWidget()53 void CXFA_FFImageEdit::UnloadWidget() {
54 m_pNode->GetWidgetAcc()->SetImageEditImage(nullptr);
55 CXFA_FFField::UnloadWidget();
56 }
57
RenderWidget(CXFA_Graphics * pGS,const CFX_Matrix & matrix,uint32_t dwStatus)58 void CXFA_FFImageEdit::RenderWidget(CXFA_Graphics* pGS,
59 const CFX_Matrix& matrix,
60 uint32_t dwStatus) {
61 if (!IsMatchVisibleStatus(dwStatus))
62 return;
63
64 CFX_Matrix mtRotate = GetRotateMatrix();
65 mtRotate.Concat(matrix);
66
67 CXFA_FFWidget::RenderWidget(pGS, mtRotate, dwStatus);
68 DrawBorder(pGS, m_pNode->GetWidgetAcc()->GetUIBorder(), m_rtUI, mtRotate);
69 RenderCaption(pGS, &mtRotate);
70 RetainPtr<CFX_DIBitmap> pDIBitmap =
71 m_pNode->GetWidgetAcc()->GetImageEditImage();
72 if (!pDIBitmap)
73 return;
74
75 CFX_RectF rtImage = m_pNormalWidget->GetWidgetRect();
76 XFA_AttributeEnum iHorzAlign = XFA_AttributeEnum::Left;
77 XFA_AttributeEnum iVertAlign = XFA_AttributeEnum::Top;
78 CXFA_Para* para = m_pNode->GetParaIfExists();
79 if (para) {
80 iHorzAlign = para->GetHorizontalAlign();
81 iVertAlign = para->GetVerticalAlign();
82 }
83
84 XFA_AttributeEnum iAspect = XFA_AttributeEnum::Fit;
85 CXFA_Value* value = m_pNode->GetFormValueIfExists();
86 if (value) {
87 CXFA_Image* image = value->GetImageIfExists();
88 if (image)
89 iAspect = image->GetAspect();
90 }
91
92 int32_t iImageXDpi = 0;
93 int32_t iImageYDpi = 0;
94 m_pNode->GetWidgetAcc()->GetImageEditDpi(iImageXDpi, iImageYDpi);
95 XFA_DrawImage(pGS, rtImage, mtRotate, pDIBitmap, iAspect, iImageXDpi,
96 iImageYDpi, iHorzAlign, iVertAlign);
97 }
98
OnLButtonDown(uint32_t dwFlags,const CFX_PointF & point)99 bool CXFA_FFImageEdit::OnLButtonDown(uint32_t dwFlags,
100 const CFX_PointF& point) {
101 if (!m_pNode->IsOpenAccess())
102 return false;
103 if (!PtInActiveRect(point))
104 return false;
105
106 SetButtonDown(true);
107
108 CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
109 ms.m_dwCmd = FWL_MouseCommand::LeftButtonDown;
110 ms.m_dwFlags = dwFlags;
111 ms.m_pos = FWLToClient(point);
112 TranslateFWLMessage(&ms);
113 return true;
114 }
115
SetFWLRect()116 void CXFA_FFImageEdit::SetFWLRect() {
117 if (!m_pNormalWidget)
118 return;
119
120 CFX_RectF rtUIMargin = m_pNode->GetWidgetAcc()->GetUIMargin();
121 CFX_RectF rtImage(m_rtUI);
122 rtImage.Deflate(rtUIMargin.left, rtUIMargin.top, rtUIMargin.width,
123 rtUIMargin.height);
124 m_pNormalWidget->SetWidgetRect(rtImage);
125 }
126
CommitData()127 bool CXFA_FFImageEdit::CommitData() {
128 return true;
129 }
130
UpdateFWLData()131 bool CXFA_FFImageEdit::UpdateFWLData() {
132 m_pNode->GetWidgetAcc()->SetImageEditImage(nullptr);
133 m_pNode->GetWidgetAcc()->LoadImageEditImage(GetDoc());
134 return true;
135 }
136
OnProcessMessage(CFWL_Message * pMessage)137 void CXFA_FFImageEdit::OnProcessMessage(CFWL_Message* pMessage) {
138 m_pOldDelegate->OnProcessMessage(pMessage);
139 }
140
OnProcessEvent(CFWL_Event * pEvent)141 void CXFA_FFImageEdit::OnProcessEvent(CFWL_Event* pEvent) {
142 CXFA_FFField::OnProcessEvent(pEvent);
143 m_pOldDelegate->OnProcessEvent(pEvent);
144 }
145
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)146 void CXFA_FFImageEdit::OnDrawWidget(CXFA_Graphics* pGraphics,
147 const CFX_Matrix& matrix) {
148 m_pOldDelegate->OnDrawWidget(pGraphics, matrix);
149 }
150
GetFormFieldType()151 FormFieldType CXFA_FFImageEdit::GetFormFieldType() {
152 return FormFieldType::kXFA_ImageField;
153 }
154