summaryrefslogtreecommitdiff
path: root/xfa/fwl/lightwidget/cfwl_widget.cpp
blob: 20c9a3b92f7e5e9a5bf7bdb78e2998fbfa520951 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fwl/lightwidget/cfwl_widget.h"

#include "xfa/fde/tto/fde_textout.h"
#include "xfa/fwl/core/cfwl_themetext.h"
#include "xfa/fwl/core/fwl_noteimp.h"
#include "xfa/fwl/core/fwl_noteimp.h"
#include "xfa/fwl/core/fwl_widgetimp.h"
#include "xfa/fwl/core/fwl_widgetmgrimp.h"
#include "xfa/fwl/core/ifwl_app.h"
#include "xfa/fwl/core/ifwl_themeprovider.h"

#define FWL_WGT_CalcHeight 2048
#define FWL_WGT_CalcWidth 2048
#define FWL_WGT_CalcMultiLineDefWidth 120.0f

IFWL_Widget* CFWL_Widget::GetWidget() {
  return m_pIface;
}

FWL_Error CFWL_Widget::GetClassName(CFX_WideString& wsClass) const {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->GetClassName(wsClass);
}

FWL_Type CFWL_Widget::GetClassID() const {
  if (!m_pIface)
    return FWL_Type::Unknown;
  return m_pIface->GetClassID();
}

FX_BOOL CFWL_Widget::IsInstance(const CFX_WideStringC& wsClass) const {
  if (!m_pIface)
    return FALSE;
  return m_pIface->IsInstance(wsClass);
}

static void* gs_pFWLWidget = (void*)FXBSTR_ID('l', 'i', 'g', 't');

FWL_Error CFWL_Widget::Initialize(const CFWL_WidgetProperties* pProperties) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->SetPrivateData(gs_pFWLWidget, this, nullptr);
}

FWL_Error CFWL_Widget::GetWidgetRect(CFX_RectF& rect, FX_BOOL bAutoSize) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->GetWidgetRect(rect, bAutoSize);
}

FWL_Error CFWL_Widget::GetGlobalRect(CFX_RectF& rect) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->GetGlobalRect(rect);
}

FWL_Error CFWL_Widget::SetWidgetRect(const CFX_RectF& rect) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->SetWidgetRect(rect);
}

FWL_Error CFWL_Widget::GetClientRect(CFX_RectF& rect) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->GetClientRect(rect);
}

CFWL_Widget* CFWL_Widget::GetParent() {
  if (!m_pIface)
    return nullptr;

  IFWL_Widget* parent = m_pIface->GetParent();
  if (parent)
    return static_cast<CFWL_Widget*>(parent->GetPrivateData(gs_pFWLWidget));
  return nullptr;
}

FWL_Error CFWL_Widget::SetParent(CFWL_Widget* pParent) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->SetParent(pParent ? pParent->GetWidget() : nullptr);
}

CFWL_Widget* CFWL_Widget::GetOwner() {
  return nullptr;
}

FWL_Error CFWL_Widget::SetOwner(CFWL_Widget* pOwner) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return FWL_Error::Succeeded;
}

uint32_t CFWL_Widget::GetStyles() {
  if (!m_pIface)
    return 0;
  return m_pIface->GetStyles();
}

FWL_Error CFWL_Widget::ModifyStyles(uint32_t dwStylesAdded,
                                    uint32_t dwStylesRemoved) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->ModifyStyles(dwStylesAdded, dwStylesRemoved);
}

uint32_t CFWL_Widget::GetStylesEx() {
  if (!m_pIface)
    return 0;
  return m_pIface->GetStylesEx();
}

FWL_Error CFWL_Widget::ModifyStylesEx(uint32_t dwStylesExAdded,
                                      uint32_t dwStylesExRemoved) {
  return m_pIface->ModifyStylesEx(dwStylesExAdded, dwStylesExRemoved);
}

uint32_t CFWL_Widget::GetStates() {
  if (!m_pIface)
    return 0;
  return m_pIface->GetStates();
}

void CFWL_Widget::SetStates(uint32_t dwStates, FX_BOOL bSet) {
  if (m_pIface)
    m_pIface->SetStates(dwStates, bSet);
}

FWL_Error CFWL_Widget::SetPrivateData(void* module_id,
                                      void* pData,
                                      PD_CALLBACK_FREEDATA callback) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->SetPrivateData(module_id, pData, callback);
}

void* CFWL_Widget::GetPrivateData(void* module_id) {
  if (!m_pIface)
    return nullptr;
  return m_pIface->GetPrivateData(module_id);
}

FWL_Error CFWL_Widget::Update() {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->Update();
}

FWL_Error CFWL_Widget::LockUpdate() {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->LockUpdate();
}

FWL_Error CFWL_Widget::UnlockUpdate() {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->UnlockUpdate();
}

FWL_WidgetHit CFWL_Widget::HitTest(FX_FLOAT fx, FX_FLOAT fy) {
  if (!m_pIface)
    return FWL_WidgetHit::Unknown;
  return m_pIface->HitTest(fx, fy);
}

FWL_Error CFWL_Widget::TransformTo(CFWL_Widget* pWidget,
                                   FX_FLOAT& fx,
                                   FX_FLOAT& fy) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->TransformTo(pWidget ? pWidget->GetWidget() : nullptr, fx,
                               fy);
}

FWL_Error CFWL_Widget::TransformTo(CFWL_Widget* pWidget, CFX_RectF& rt) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->TransformTo(pWidget ? pWidget->GetWidget() : nullptr, rt);
}

FWL_Error CFWL_Widget::GetMatrix(CFX_Matrix& matrix, FX_BOOL bGlobal) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->GetMatrix(matrix, bGlobal);
}

FWL_Error CFWL_Widget::SetMatrix(const CFX_Matrix& matrix) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->SetMatrix(matrix);
}

FWL_Error CFWL_Widget::DrawWidget(CFX_Graphics* pGraphics,
                                  const CFX_Matrix* pMatrix) {
  if (!m_pIface)
    return FWL_Error::Indefinite;
  return m_pIface->DrawWidget(pGraphics, pMatrix);
}

IFWL_WidgetDelegate* CFWL_Widget::SetDelegate(IFWL_WidgetDelegate* pDelegate) {
  if (!m_pIface)
    return nullptr;

  m_pDelegate = m_pIface->SetDelegate(pDelegate);
  return m_pDelegate;
}

CFWL_Widget::CFWL_Widget()
    : m_pIface(nullptr), m_pDelegate(nullptr), m_pProperties(nullptr) {
  m_pProperties = new CFWL_WidgetProperties;
  m_pWidgetMgr = CFWL_WidgetMgr::GetInstance();
  ASSERT(m_pWidgetMgr);
}

CFWL_Widget::~CFWL_Widget() {
  delete m_pProperties;
  if (m_pIface) {
    m_pIface->Finalize();
    delete m_pIface;
  }
}

FWL_Error CFWL_Widget::Repaint(const CFX_RectF* pRect) {
  if (!m_pIface)
    return FWL_Error::Indefinite;

  CFX_RectF rect;
  if (pRect) {
    rect = *pRect;
  } else {
    m_pIface->GetWidgetRect(rect);
    rect.left = rect.top = 0;
  }
  return m_pWidgetMgr->RepaintWidget(m_pIface, &rect);
}

FWL_Error CFWL_Widget::SetFocus(FX_BOOL bFocus) {
  if (!m_pIface)
    return FWL_Error::Indefinite;

  IFWL_App* pApp = m_pIface->GetOwnerApp();
  if (!pApp)
    return FWL_Error::Indefinite;

  CFWL_NoteDriver* pDriver = pApp->GetNoteDriver();
  if (!pDriver)
    return FWL_Error::Indefinite;

  if (bFocus) {
    pDriver->SetFocus(m_pIface);
  } else {
    if (pDriver->GetFocus() == m_pIface) {
      pDriver->SetFocus(nullptr);
    }
  }
  return FWL_Error::Succeeded;
}

FWL_Error CFWL_Widget::SetGrab(FX_BOOL bSet) {
  if (!m_pIface)
    return FWL_Error::Indefinite;

  IFWL_App* pApp = m_pIface->GetOwnerApp();
  if (!pApp)
    return FWL_Error::Indefinite;

  CFWL_NoteDriver* pDriver = pApp->GetNoteDriver();
  if (!pDriver)
    return FWL_Error::Indefinite;

  pDriver->SetGrab(m_pIface, bSet);
  return FWL_Error::Succeeded;
}

void CFWL_Widget::RegisterEventTarget(CFWL_Widget* pEventSource,
                                      uint32_t dwFilter) {
  if (!m_pIface)
    return;

  IFWL_App* pApp = m_pIface->GetOwnerApp();
  if (!pApp)
    return;

  CFWL_NoteDriver* pNoteDriver = pApp->GetNoteDriver();
  if (!pNoteDriver)
    return;

  IFWL_Widget* pEventSourceImp =
      !pEventSource ? nullptr : pEventSource->GetWidget();
  pNoteDriver->RegisterEventTarget(GetWidget(), pEventSourceImp, dwFilter);
}

void CFWL_Widget::DispatchEvent(CFWL_Event* pEvent) {
  if (!m_pIface)
    return;
  if (m_pIface->GetOuter())
    return;

  IFWL_App* pApp = m_pIface->GetOwnerApp();
  if (!pApp)
    return;

  CFWL_NoteDriver* pNoteDriver = pApp->GetNoteDriver();
  if (!pNoteDriver)
    return;

  pNoteDriver->SendEvent(pEvent);
}

CFX_SizeF CFWL_Widget::CalcTextSize(const CFX_WideString& wsText,
                                    FX_BOOL bMultiLine,
                                    int32_t iLineWidth) {
  if (!m_pIface)
    return CFX_SizeF();

  IFWL_ThemeProvider* pTheme = m_pIface->GetThemeProvider();
  if (!pTheme)
    return CFX_SizeF();

  CFWL_ThemeText calPart;
  calPart.m_pWidget = m_pIface;
  calPart.m_wsText = wsText;
  calPart.m_dwTTOStyles =
      bMultiLine ? FDE_TTOSTYLE_LineWrap : FDE_TTOSTYLE_SingleLine;
  calPart.m_iTTOAlign = FDE_TTOALIGNMENT_TopLeft;
  CFX_RectF rect;
  FX_FLOAT fWidth = bMultiLine
                        ? (iLineWidth > 0 ? (FX_FLOAT)iLineWidth
                                          : FWL_WGT_CalcMultiLineDefWidth)
                        : FWL_WGT_CalcWidth;
  rect.Set(0, 0, fWidth, FWL_WGT_CalcHeight);
  pTheme->CalcTextRect(&calPart, rect);
  return CFX_SizeF(rect.width, rect.height);
}