OLD | NEW |
| (Empty) |
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/core/cfwl_notedriver.h" | |
8 | |
9 #include <utility> | |
10 | |
11 #include "core/fxcrt/fx_ext.h" | |
12 #include "third_party/base/ptr_util.h" | |
13 #include "third_party/base/stl_util.h" | |
14 #include "xfa/fwl/core/cfwl_app.h" | |
15 #include "xfa/fwl/core/cfwl_eventtarget.h" | |
16 #include "xfa/fwl/core/cfwl_form.h" | |
17 #include "xfa/fwl/core/cfwl_msgkey.h" | |
18 #include "xfa/fwl/core/cfwl_msgkillfocus.h" | |
19 #include "xfa/fwl/core/cfwl_msgmouse.h" | |
20 #include "xfa/fwl/core/cfwl_msgmousewheel.h" | |
21 #include "xfa/fwl/core/cfwl_msgsetfocus.h" | |
22 #include "xfa/fwl/core/cfwl_noteloop.h" | |
23 #include "xfa/fwl/core/cfwl_widgetmgr.h" | |
24 | |
25 CFWL_NoteDriver::CFWL_NoteDriver() | |
26 : m_pHover(nullptr), | |
27 m_pFocus(nullptr), | |
28 m_pGrab(nullptr), | |
29 m_pNoteLoop(pdfium::MakeUnique<CFWL_NoteLoop>()) { | |
30 PushNoteLoop(m_pNoteLoop.get()); | |
31 } | |
32 | |
33 CFWL_NoteDriver::~CFWL_NoteDriver() { | |
34 ClearEventTargets(true); | |
35 } | |
36 | |
37 void CFWL_NoteDriver::SendEvent(CFWL_Event* pNote) { | |
38 if (m_eventTargets.empty()) | |
39 return; | |
40 | |
41 for (const auto& pair : m_eventTargets) { | |
42 CFWL_EventTarget* pEventTarget = pair.second; | |
43 if (pEventTarget && !pEventTarget->IsInvalid()) | |
44 pEventTarget->ProcessEvent(pNote); | |
45 } | |
46 } | |
47 | |
48 void CFWL_NoteDriver::RegisterEventTarget(CFWL_Widget* pListener, | |
49 CFWL_Widget* pEventSource) { | |
50 uint32_t key = pListener->GetEventKey(); | |
51 if (key == 0) { | |
52 do { | |
53 key = rand(); | |
54 } while (key == 0 || pdfium::ContainsKey(m_eventTargets, key)); | |
55 pListener->SetEventKey(key); | |
56 } | |
57 if (!m_eventTargets[key]) | |
58 m_eventTargets[key] = new CFWL_EventTarget(pListener); | |
59 | |
60 m_eventTargets[key]->SetEventSource(pEventSource); | |
61 } | |
62 | |
63 void CFWL_NoteDriver::UnregisterEventTarget(CFWL_Widget* pListener) { | |
64 uint32_t key = pListener->GetEventKey(); | |
65 if (key == 0) | |
66 return; | |
67 | |
68 auto it = m_eventTargets.find(key); | |
69 if (it != m_eventTargets.end()) | |
70 it->second->FlagInvalid(); | |
71 } | |
72 | |
73 void CFWL_NoteDriver::PushNoteLoop(CFWL_NoteLoop* pNoteLoop) { | |
74 m_noteLoopQueue.Add(pNoteLoop); | |
75 } | |
76 | |
77 CFWL_NoteLoop* CFWL_NoteDriver::PopNoteLoop() { | |
78 int32_t pos = m_noteLoopQueue.GetSize(); | |
79 if (pos <= 0) | |
80 return nullptr; | |
81 | |
82 CFWL_NoteLoop* p = m_noteLoopQueue.GetAt(pos - 1); | |
83 m_noteLoopQueue.RemoveAt(pos - 1); | |
84 return p; | |
85 } | |
86 | |
87 bool CFWL_NoteDriver::SetFocus(CFWL_Widget* pFocus) { | |
88 if (m_pFocus == pFocus) | |
89 return true; | |
90 | |
91 CFWL_Widget* pPrev = m_pFocus; | |
92 m_pFocus = pFocus; | |
93 if (pPrev) { | |
94 if (IFWL_WidgetDelegate* pDelegate = pPrev->GetDelegate()) { | |
95 CFWL_MsgKillFocus ms(pPrev, pPrev); | |
96 pDelegate->OnProcessMessage(&ms); | |
97 } | |
98 } | |
99 if (pFocus) { | |
100 CFWL_Widget* pWidget = | |
101 pFocus->GetOwnerApp()->GetWidgetMgr()->GetSystemFormWidget(pFocus); | |
102 CFWL_Form* pForm = static_cast<CFWL_Form*>(pWidget); | |
103 if (pForm) | |
104 pForm->SetSubFocus(pFocus); | |
105 | |
106 if (IFWL_WidgetDelegate* pDelegate = pFocus->GetDelegate()) { | |
107 CFWL_MsgSetFocus ms(nullptr, pFocus); | |
108 pDelegate->OnProcessMessage(&ms); | |
109 } | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 void CFWL_NoteDriver::Run() { | |
115 #if (_FX_OS_ == _FX_LINUX_DESKTOP_ || _FX_OS_ == _FX_WIN32_DESKTOP_ || \ | |
116 _FX_OS_ == _FX_WIN64_) | |
117 for (;;) { | |
118 CFWL_NoteLoop* pTopLoop = GetTopLoop(); | |
119 if (!pTopLoop || !pTopLoop->ContinueModal()) | |
120 break; | |
121 UnqueueMessage(pTopLoop); | |
122 } | |
123 #endif | |
124 } | |
125 | |
126 void CFWL_NoteDriver::NotifyTargetHide(CFWL_Widget* pNoteTarget) { | |
127 if (m_pFocus == pNoteTarget) | |
128 m_pFocus = nullptr; | |
129 if (m_pHover == pNoteTarget) | |
130 m_pHover = nullptr; | |
131 if (m_pGrab == pNoteTarget) | |
132 m_pGrab = nullptr; | |
133 } | |
134 | |
135 void CFWL_NoteDriver::NotifyTargetDestroy(CFWL_Widget* pNoteTarget) { | |
136 if (m_pFocus == pNoteTarget) | |
137 m_pFocus = nullptr; | |
138 if (m_pHover == pNoteTarget) | |
139 m_pHover = nullptr; | |
140 if (m_pGrab == pNoteTarget) | |
141 m_pGrab = nullptr; | |
142 | |
143 UnregisterEventTarget(pNoteTarget); | |
144 | |
145 for (int32_t nIndex = 0; nIndex < m_forms.GetSize(); nIndex++) { | |
146 CFWL_Form* pForm = static_cast<CFWL_Form*>(m_forms[nIndex]); | |
147 if (!pForm) | |
148 continue; | |
149 | |
150 CFWL_Widget* pSubFocus = pForm->GetSubFocus(); | |
151 if (!pSubFocus) | |
152 return; | |
153 if (pSubFocus == pNoteTarget) | |
154 pForm->SetSubFocus(nullptr); | |
155 } | |
156 } | |
157 | |
158 void CFWL_NoteDriver::RegisterForm(CFWL_Widget* pForm) { | |
159 if (!pForm || m_forms.Find(pForm) >= 0) | |
160 return; | |
161 | |
162 m_forms.Add(pForm); | |
163 if (m_forms.GetSize() != 1) | |
164 return; | |
165 | |
166 CFWL_NoteLoop* pLoop = m_noteLoopQueue.GetAt(0); | |
167 if (!pLoop) | |
168 return; | |
169 | |
170 pLoop->SetMainForm(pForm); | |
171 } | |
172 | |
173 void CFWL_NoteDriver::UnRegisterForm(CFWL_Widget* pForm) { | |
174 if (!pForm) | |
175 return; | |
176 | |
177 int32_t nIndex = m_forms.Find(pForm); | |
178 if (nIndex < 0) | |
179 return; | |
180 | |
181 m_forms.RemoveAt(nIndex); | |
182 } | |
183 | |
184 void CFWL_NoteDriver::QueueMessage(std::unique_ptr<CFWL_Message> pMessage) { | |
185 m_noteQueue.push_back(std::move(pMessage)); | |
186 } | |
187 | |
188 void CFWL_NoteDriver::UnqueueMessage(CFWL_NoteLoop* pNoteLoop) { | |
189 if (m_noteQueue.empty()) | |
190 return; | |
191 | |
192 std::unique_ptr<CFWL_Message> pMessage = std::move(m_noteQueue[0]); | |
193 m_noteQueue.pop_front(); | |
194 | |
195 if (!IsValidMessage(pMessage.get())) | |
196 return; | |
197 | |
198 ProcessMessage(pMessage.get()); | |
199 } | |
200 | |
201 CFWL_NoteLoop* CFWL_NoteDriver::GetTopLoop() const { | |
202 int32_t size = m_noteLoopQueue.GetSize(); | |
203 if (size <= 0) | |
204 return nullptr; | |
205 return m_noteLoopQueue[size - 1]; | |
206 } | |
207 | |
208 void CFWL_NoteDriver::ProcessMessage(CFWL_Message* pMessage) { | |
209 CFWL_WidgetMgr* pWidgetMgr = | |
210 pMessage->m_pDstTarget->GetOwnerApp()->GetWidgetMgr(); | |
211 CFWL_Widget* pMessageForm = pWidgetMgr->IsFormDisabled() | |
212 ? pMessage->m_pDstTarget | |
213 : GetMessageForm(pMessage->m_pDstTarget); | |
214 if (!pMessageForm) | |
215 return; | |
216 if (!DispatchMessage(pMessage, pMessageForm)) | |
217 return; | |
218 | |
219 if (pMessage->GetType() == CFWL_Message::Type::Mouse) | |
220 MouseSecondary(pMessage); | |
221 } | |
222 | |
223 bool CFWL_NoteDriver::DispatchMessage(CFWL_Message* pMessage, | |
224 CFWL_Widget* pMessageForm) { | |
225 switch (pMessage->GetType()) { | |
226 case CFWL_Message::Type::SetFocus: { | |
227 if (!DoSetFocus(pMessage, pMessageForm)) | |
228 return false; | |
229 break; | |
230 } | |
231 case CFWL_Message::Type::KillFocus: { | |
232 if (!DoKillFocus(pMessage, pMessageForm)) | |
233 return false; | |
234 break; | |
235 } | |
236 case CFWL_Message::Type::Key: { | |
237 if (!DoKey(pMessage, pMessageForm)) | |
238 return false; | |
239 break; | |
240 } | |
241 case CFWL_Message::Type::Mouse: { | |
242 if (!DoMouse(pMessage, pMessageForm)) | |
243 return false; | |
244 break; | |
245 } | |
246 case CFWL_Message::Type::MouseWheel: { | |
247 if (!DoWheel(pMessage, pMessageForm)) | |
248 return false; | |
249 break; | |
250 } | |
251 default: | |
252 break; | |
253 } | |
254 if (IFWL_WidgetDelegate* pDelegate = pMessage->m_pDstTarget->GetDelegate()) | |
255 pDelegate->OnProcessMessage(pMessage); | |
256 | |
257 return true; | |
258 } | |
259 | |
260 bool CFWL_NoteDriver::DoSetFocus(CFWL_Message* pMessage, | |
261 CFWL_Widget* pMessageForm) { | |
262 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
263 if (pWidgetMgr->IsFormDisabled()) { | |
264 m_pFocus = pMessage->m_pDstTarget; | |
265 return true; | |
266 } | |
267 | |
268 CFWL_Widget* pWidget = pMessage->m_pDstTarget; | |
269 if (!pWidget) | |
270 return false; | |
271 | |
272 CFWL_Form* pForm = static_cast<CFWL_Form*>(pWidget); | |
273 CFWL_Widget* pSubFocus = pForm->GetSubFocus(); | |
274 if (pSubFocus && ((pSubFocus->GetStates() & FWL_WGTSTATE_Focused) == 0)) { | |
275 pMessage->m_pDstTarget = pSubFocus; | |
276 if (m_pFocus != pMessage->m_pDstTarget) { | |
277 m_pFocus = pMessage->m_pDstTarget; | |
278 return true; | |
279 } | |
280 } | |
281 return false; | |
282 } | |
283 | |
284 bool CFWL_NoteDriver::DoKillFocus(CFWL_Message* pMessage, | |
285 CFWL_Widget* pMessageForm) { | |
286 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
287 if (pWidgetMgr->IsFormDisabled()) { | |
288 if (m_pFocus == pMessage->m_pDstTarget) | |
289 m_pFocus = nullptr; | |
290 return true; | |
291 } | |
292 | |
293 CFWL_Form* pForm = static_cast<CFWL_Form*>(pMessage->m_pDstTarget); | |
294 if (!pForm) | |
295 return false; | |
296 | |
297 CFWL_Widget* pSubFocus = pForm->GetSubFocus(); | |
298 if (pSubFocus && (pSubFocus->GetStates() & FWL_WGTSTATE_Focused)) { | |
299 pMessage->m_pDstTarget = pSubFocus; | |
300 if (m_pFocus == pMessage->m_pDstTarget) { | |
301 m_pFocus = nullptr; | |
302 return true; | |
303 } | |
304 } | |
305 return false; | |
306 } | |
307 | |
308 bool CFWL_NoteDriver::DoKey(CFWL_Message* pMessage, CFWL_Widget* pMessageForm) { | |
309 CFWL_MsgKey* pMsg = static_cast<CFWL_MsgKey*>(pMessage); | |
310 #if (_FX_OS_ != _FX_MACOSX_) | |
311 if (pMsg->m_dwCmd == FWL_KeyCommand::KeyDown && | |
312 pMsg->m_dwKeyCode == FWL_VKEY_Tab) { | |
313 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
314 CFWL_Widget* pForm = GetMessageForm(pMsg->m_pDstTarget); | |
315 CFWL_Widget* pFocus = m_pFocus; | |
316 if (m_pFocus && pWidgetMgr->GetSystemFormWidget(m_pFocus) != pForm) | |
317 pFocus = nullptr; | |
318 | |
319 bool bFind = false; | |
320 CFWL_Widget* pNextTabStop = pWidgetMgr->NextTab(pForm, pFocus, bFind); | |
321 if (!pNextTabStop) { | |
322 bFind = false; | |
323 pNextTabStop = pWidgetMgr->NextTab(pForm, nullptr, bFind); | |
324 } | |
325 if (pNextTabStop == pFocus) | |
326 return true; | |
327 if (pNextTabStop) | |
328 SetFocus(pNextTabStop); | |
329 return true; | |
330 } | |
331 #endif | |
332 | |
333 if (!m_pFocus) { | |
334 if (pMsg->m_dwCmd == FWL_KeyCommand::KeyDown && | |
335 pMsg->m_dwKeyCode == FWL_VKEY_Return) { | |
336 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
337 CFWL_Widget* defButton = pWidgetMgr->GetDefaultButton(pMessageForm); | |
338 if (defButton) { | |
339 pMsg->m_pDstTarget = defButton; | |
340 return true; | |
341 } | |
342 } | |
343 return false; | |
344 } | |
345 pMsg->m_pDstTarget = m_pFocus; | |
346 return true; | |
347 } | |
348 | |
349 bool CFWL_NoteDriver::DoMouse(CFWL_Message* pMessage, | |
350 CFWL_Widget* pMessageForm) { | |
351 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
352 if (pMsg->m_dwCmd == FWL_MouseCommand::Leave || | |
353 pMsg->m_dwCmd == FWL_MouseCommand::Hover || | |
354 pMsg->m_dwCmd == FWL_MouseCommand::Enter) { | |
355 return !!pMsg->m_pDstTarget; | |
356 } | |
357 if (pMsg->m_pDstTarget != pMessageForm) | |
358 pMsg->m_pDstTarget->TransformTo(pMessageForm, pMsg->m_fx, pMsg->m_fy); | |
359 if (!DoMouseEx(pMsg, pMessageForm)) | |
360 pMsg->m_pDstTarget = pMessageForm; | |
361 return true; | |
362 } | |
363 | |
364 bool CFWL_NoteDriver::DoWheel(CFWL_Message* pMessage, | |
365 CFWL_Widget* pMessageForm) { | |
366 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
367 if (!pWidgetMgr) | |
368 return false; | |
369 | |
370 CFWL_MsgMouseWheel* pMsg = static_cast<CFWL_MsgMouseWheel*>(pMessage); | |
371 CFWL_Widget* pDst = | |
372 pWidgetMgr->GetWidgetAtPoint(pMessageForm, pMsg->m_fx, pMsg->m_fy); | |
373 if (!pDst) | |
374 return false; | |
375 | |
376 pMessageForm->TransformTo(pDst, pMsg->m_fx, pMsg->m_fy); | |
377 pMsg->m_pDstTarget = pDst; | |
378 return true; | |
379 } | |
380 | |
381 bool CFWL_NoteDriver::DoMouseEx(CFWL_Message* pMessage, | |
382 CFWL_Widget* pMessageForm) { | |
383 CFWL_WidgetMgr* pWidgetMgr = pMessageForm->GetOwnerApp()->GetWidgetMgr(); | |
384 if (!pWidgetMgr) | |
385 return false; | |
386 CFWL_Widget* pTarget = nullptr; | |
387 if (m_pGrab) | |
388 pTarget = m_pGrab; | |
389 | |
390 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
391 if (!pTarget) { | |
392 pTarget = | |
393 pWidgetMgr->GetWidgetAtPoint(pMessageForm, pMsg->m_fx, pMsg->m_fy); | |
394 } | |
395 if (pTarget) { | |
396 if (pMessageForm != pTarget) | |
397 pMessageForm->TransformTo(pTarget, pMsg->m_fx, pMsg->m_fy); | |
398 } | |
399 if (!pTarget) | |
400 return false; | |
401 | |
402 pMsg->m_pDstTarget = pTarget; | |
403 return true; | |
404 } | |
405 | |
406 void CFWL_NoteDriver::MouseSecondary(CFWL_Message* pMessage) { | |
407 CFWL_Widget* pTarget = pMessage->m_pDstTarget; | |
408 if (pTarget == m_pHover) | |
409 return; | |
410 | |
411 CFWL_MsgMouse* pMsg = static_cast<CFWL_MsgMouse*>(pMessage); | |
412 if (m_pHover) { | |
413 CFWL_MsgMouse msLeave(nullptr, m_pHover); | |
414 msLeave.m_fx = pMsg->m_fx; | |
415 msLeave.m_fy = pMsg->m_fy; | |
416 pTarget->TransformTo(m_pHover, msLeave.m_fx, msLeave.m_fy); | |
417 | |
418 msLeave.m_dwFlags = 0; | |
419 msLeave.m_dwCmd = FWL_MouseCommand::Leave; | |
420 DispatchMessage(&msLeave, nullptr); | |
421 } | |
422 if (pTarget->GetClassID() == FWL_Type::Form) { | |
423 m_pHover = nullptr; | |
424 return; | |
425 } | |
426 m_pHover = pTarget; | |
427 | |
428 CFWL_MsgMouse msHover(nullptr, pTarget); | |
429 msHover.m_fx = pMsg->m_fx; | |
430 msHover.m_fy = pMsg->m_fy; | |
431 msHover.m_dwFlags = 0; | |
432 msHover.m_dwCmd = FWL_MouseCommand::Hover; | |
433 DispatchMessage(&msHover, nullptr); | |
434 } | |
435 | |
436 bool CFWL_NoteDriver::IsValidMessage(CFWL_Message* pMessage) { | |
437 for (int32_t i = 0; i < m_noteLoopQueue.GetSize(); i++) { | |
438 CFWL_NoteLoop* pNoteLoop = m_noteLoopQueue[i]; | |
439 CFWL_Widget* pForm = pNoteLoop->GetForm(); | |
440 if (pForm && (pForm == pMessage->m_pDstTarget)) | |
441 return true; | |
442 } | |
443 | |
444 for (int32_t j = 0; j < m_forms.GetSize(); j++) { | |
445 CFWL_Form* pForm = static_cast<CFWL_Form*>(m_forms[j]); | |
446 if (pForm == pMessage->m_pDstTarget) | |
447 return true; | |
448 } | |
449 return false; | |
450 } | |
451 | |
452 CFWL_Widget* CFWL_NoteDriver::GetMessageForm(CFWL_Widget* pDstTarget) { | |
453 int32_t iTrackLoop = m_noteLoopQueue.GetSize(); | |
454 if (iTrackLoop <= 0) | |
455 return nullptr; | |
456 | |
457 CFWL_Widget* pMessageForm = nullptr; | |
458 if (iTrackLoop > 1) | |
459 pMessageForm = m_noteLoopQueue[iTrackLoop - 1]->GetForm(); | |
460 else if (m_forms.Find(pDstTarget) < 0) | |
461 pMessageForm = pDstTarget; | |
462 if (!pMessageForm && pDstTarget) { | |
463 CFWL_WidgetMgr* pWidgetMgr = pDstTarget->GetOwnerApp()->GetWidgetMgr(); | |
464 if (!pWidgetMgr) | |
465 return nullptr; | |
466 pMessageForm = pWidgetMgr->GetSystemFormWidget(pDstTarget); | |
467 } | |
468 return pMessageForm; | |
469 } | |
470 | |
471 void CFWL_NoteDriver::ClearEventTargets(bool bRemoveAll) { | |
472 auto it = m_eventTargets.begin(); | |
473 while (it != m_eventTargets.end()) { | |
474 auto old = it++; | |
475 if (old->second && (bRemoveAll || old->second->IsInvalid())) { | |
476 delete old->second; | |
477 m_eventTargets.erase(old); | |
478 } | |
479 } | |
480 } | |
OLD | NEW |