Bug 1799258 - Fix constexpr issue on base toolchain builds. r=gfx-reviewers,lsalzman
[gecko.git] / widget / windows / nsWindow.cpp
blob703229c553b89671281fc3954668596033cafdbc
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sts=2 sw=2 et cin: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * nsWindow - Native window management and event handling.
10 * nsWindow is organized into a set of major blocks and
11 * block subsections. The layout is as follows:
13 * Includes
14 * Variables
15 * nsIWidget impl.
16 * nsIWidget methods and utilities
17 * nsSwitchToUIThread impl.
18 * nsSwitchToUIThread methods and utilities
19 * Moz events
20 * Event initialization
21 * Event dispatching
22 * Native events
23 * Wndproc(s)
24 * Event processing
25 * OnEvent event handlers
26 * IME management and accessibility
27 * Transparency
28 * Popup hook handling
29 * Misc. utilities
30 * Child window impl.
32 * Search for "BLOCK:" to find major blocks.
33 * Search for "SECTION:" to find specific sections.
35 * Blocks should be split out into separate files if they
36 * become unmanageable.
38 * Notable related sources:
40 * nsWindowDefs.h - Definitions, macros, structs, enums
41 * and general setup.
42 * nsWindowDbg.h/.cpp - Debug related code and directives.
43 * nsWindowGfx.h/.cpp - Graphics and painting.
47 /**************************************************************
48 **************************************************************
50 ** BLOCK: Includes
52 ** Include headers.
54 **************************************************************
55 **************************************************************/
57 #include "gfx2DGlue.h"
58 #include "gfxEnv.h"
59 #include "gfxPlatform.h"
61 #include "mozilla/AppShutdown.h"
62 #include "mozilla/AutoRestore.h"
63 #include "mozilla/PreXULSkeletonUI.h"
64 #include "mozilla/Logging.h"
65 #include "mozilla/MathAlgorithms.h"
66 #include "mozilla/MiscEvents.h"
67 #include "mozilla/MouseEvents.h"
68 #include "mozilla/PresShell.h"
69 #include "mozilla/ScopeExit.h"
70 #include "mozilla/StaticPrefs_browser.h"
71 #include "mozilla/SwipeTracker.h"
72 #include "mozilla/TouchEvents.h"
73 #include "mozilla/TimeStamp.h"
75 #include "mozilla/ipc/MessageChannel.h"
76 #include <algorithm>
77 #include <limits>
79 #include "nsWindow.h"
80 #include "nsWindowTaskbarConcealer.h"
81 #include "nsAppRunner.h"
83 #include <shellapi.h>
84 #include <windows.h>
85 #include <wtsapi32.h>
86 #include <process.h>
87 #include <commctrl.h>
88 #include <dbt.h>
89 #include <unknwn.h>
90 #include <psapi.h>
91 #include <rpc.h>
92 #include <propvarutil.h>
93 #include <propkey.h>
95 #include "mozilla/Logging.h"
96 #include "prtime.h"
97 #include "prenv.h"
99 #include "mozilla/WidgetTraceEvent.h"
100 #include "nsContentUtils.h"
101 #include "nsISupportsPrimitives.h"
102 #include "nsITheme.h"
103 #include "nsIObserverService.h"
104 #include "nsIScreenManager.h"
105 #include "imgIContainer.h"
106 #include "nsIFile.h"
107 #include "nsIRollupListener.h"
108 #include "nsIClipboard.h"
109 #include "WinMouseScrollHandler.h"
110 #include "nsFontMetrics.h"
111 #include "nsIFontEnumerator.h"
112 #include "nsFont.h"
113 #include "nsRect.h"
114 #include "nsThreadUtils.h"
115 #include "nsNativeCharsetUtils.h"
116 #include "nsGkAtoms.h"
117 #include "nsCRT.h"
118 #include "nsAppDirectoryServiceDefs.h"
119 #include "nsWidgetsCID.h"
120 #include "nsTHashtable.h"
121 #include "nsHashKeys.h"
122 #include "nsString.h"
123 #include "mozilla/Components.h"
124 #include "nsNativeThemeWin.h"
125 #include "nsWindowsDllInterceptor.h"
126 #include "nsLayoutUtils.h"
127 #include "nsView.h"
128 #include "nsWindowGfx.h"
129 #include "gfxWindowsPlatform.h"
130 #include "gfxDWriteFonts.h"
131 #include "nsPrintfCString.h"
132 #include "mozilla/Preferences.h"
133 #include "SystemTimeConverter.h"
134 #include "WinTaskbar.h"
135 #include "WidgetUtils.h"
136 #include "WinWindowOcclusionTracker.h"
137 #include "nsIWidgetListener.h"
138 #include "mozilla/dom/Document.h"
139 #include "mozilla/dom/MouseEventBinding.h"
140 #include "mozilla/dom/Touch.h"
141 #include "mozilla/gfx/2D.h"
142 #include "mozilla/gfx/GPUProcessManager.h"
143 #include "mozilla/intl/LocaleService.h"
144 #include "mozilla/layers/WebRenderLayerManager.h"
145 #include "mozilla/WindowsVersion.h"
146 #include "mozilla/TextEvents.h" // For WidgetKeyboardEvent
147 #include "mozilla/TextEventDispatcherListener.h"
148 #include "mozilla/widget/nsAutoRollup.h"
149 #include "mozilla/widget/PlatformWidgetTypes.h"
150 #include "mozilla/widget/Screen.h"
151 #include "nsStyleConsts.h"
152 #include "nsBidiKeyboard.h"
153 #include "nsStyleConsts.h"
154 #include "gfxConfig.h"
155 #include "InProcessWinCompositorWidget.h"
156 #include "InputDeviceUtils.h"
157 #include "ScreenHelperWin.h"
158 #include "mozilla/StaticPrefs_apz.h"
159 #include "mozilla/StaticPrefs_dom.h"
160 #include "mozilla/StaticPrefs_gfx.h"
161 #include "mozilla/StaticPrefs_layout.h"
162 #include "mozilla/StaticPrefs_widget.h"
163 #include "nsNativeAppSupportWin.h"
164 #include "mozilla/browser/NimbusFeatures.h"
166 #include "nsIGfxInfo.h"
167 #include "nsUXThemeConstants.h"
168 #include "KeyboardLayout.h"
169 #include "nsNativeDragTarget.h"
170 #include <mmsystem.h> // needed for WIN32_LEAN_AND_MEAN
171 #include <zmouse.h>
172 #include <richedit.h>
174 #if defined(ACCESSIBILITY)
176 # ifdef DEBUG
177 # include "mozilla/a11y/Logging.h"
178 # endif
180 # include "oleidl.h"
181 # include <winuser.h>
182 # include "nsAccessibilityService.h"
183 # include "mozilla/a11y/DocAccessible.h"
184 # include "mozilla/a11y/LazyInstantiator.h"
185 # include "mozilla/a11y/Platform.h"
186 # if !defined(WINABLEAPI)
187 # include <winable.h>
188 # endif // !defined(WINABLEAPI)
189 #endif // defined(ACCESSIBILITY)
191 #include "WindowsUIUtils.h"
193 #include "nsWindowDefs.h"
195 #include "nsCrashOnException.h"
197 #include "nsIContent.h"
199 #include "mozilla/BackgroundHangMonitor.h"
200 #include "WinIMEHandler.h"
202 #include "npapi.h"
204 #include <d3d11.h>
206 #include "InkCollector.h"
208 // ERROR from wingdi.h (below) gets undefined by some code.
209 // #define ERROR 0
210 // #define RGN_ERROR ERROR
211 #define ERROR 0
213 #if !defined(SM_CONVERTIBLESLATEMODE)
214 # define SM_CONVERTIBLESLATEMODE 0x2003
215 #endif
217 // Win 8.1+ (_WIN32_WINNT_WINBLUE)
218 #if !defined(WM_DPICHANGED)
219 # define WM_DPICHANGED 0x02E0
220 #endif
222 // Win 8+ (_WIN32_WINNT_WIN8)
223 #if !defined(EVENT_OBJECT_CLOAKED)
224 # define EVENT_OBJECT_CLOAKED 0x8017
225 # define EVENT_OBJECT_UNCLOAKED 0x8018
226 #endif
228 #include "mozilla/gfx/DeviceManagerDx.h"
229 #include "mozilla/layers/APZInputBridge.h"
230 #include "mozilla/layers/InputAPZContext.h"
231 #include "mozilla/layers/KnowsCompositor.h"
232 #include "InputData.h"
234 #include "mozilla/TaskController.h"
235 #include "mozilla/Telemetry.h"
236 #include "mozilla/webrender/WebRenderAPI.h"
237 #include "mozilla/layers/IAPZCTreeManager.h"
239 #include "DirectManipulationOwner.h"
241 using namespace mozilla;
242 using namespace mozilla::dom;
243 using namespace mozilla::gfx;
244 using namespace mozilla::layers;
245 using namespace mozilla::widget;
246 using namespace mozilla::plugins;
248 /**************************************************************
249 **************************************************************
251 ** BLOCK: Variables
253 ** nsWindow Class static initializations and global variables.
255 **************************************************************
256 **************************************************************/
258 /**************************************************************
260 * SECTION: nsWindow statics
262 **************************************************************/
263 static const wchar_t kUser32LibName[] = L"user32.dll";
265 uint32_t nsWindow::sInstanceCount = 0;
266 bool nsWindow::sIsOleInitialized = false;
267 nsIWidget::Cursor nsWindow::sCurrentCursor = {};
268 nsWindow* nsWindow::sCurrentWindow = nullptr;
269 bool nsWindow::sJustGotDeactivate = false;
270 bool nsWindow::sJustGotActivate = false;
271 bool nsWindow::sIsInMouseCapture = false;
273 // Hook Data Members for Dropdowns. sProcessHook Tells the
274 // hook methods whether they should be processing the hook
275 // messages.
276 HHOOK nsWindow::sMsgFilterHook = nullptr;
277 HHOOK nsWindow::sCallProcHook = nullptr;
278 HHOOK nsWindow::sCallMouseHook = nullptr;
279 bool nsWindow::sProcessHook = false;
280 UINT nsWindow::sRollupMsgId = 0;
281 HWND nsWindow::sRollupMsgWnd = nullptr;
282 UINT nsWindow::sHookTimerId = 0;
284 // Used to prevent dispatching mouse events that do not originate from user
285 // input.
286 POINT nsWindow::sLastMouseMovePoint = {0};
288 bool nsWindow::sIsRestoringSession = false;
290 bool nsWindow::sTouchInjectInitialized = false;
291 InjectTouchInputPtr nsWindow::sInjectTouchFuncPtr;
293 static SystemTimeConverter<DWORD>& TimeConverter() {
294 static SystemTimeConverter<DWORD> timeConverterSingleton;
295 return timeConverterSingleton;
298 // Global event hook for window cloaking. Never deregistered.
299 // - `Nothing` if not yet set.
300 // - `Some(nullptr)` if no attempt should be made to set it.
301 static mozilla::Maybe<HWINEVENTHOOK> sWinCloakEventHook =
302 IsWin8OrLater() ? Nothing() : Some(HWINEVENTHOOK(nullptr));
303 static mozilla::LazyLogModule sCloakingLog("DWMCloaking");
305 namespace mozilla {
307 class CurrentWindowsTimeGetter {
308 public:
309 explicit CurrentWindowsTimeGetter(HWND aWnd) : mWnd(aWnd) {}
311 DWORD GetCurrentTime() const { return ::GetTickCount(); }
313 void GetTimeAsyncForPossibleBackwardsSkew(const TimeStamp& aNow) {
314 DWORD currentTime = GetCurrentTime();
315 if (sBackwardsSkewStamp && currentTime == sLastPostTime) {
316 // There's already one inflight with this timestamp. Don't
317 // send a duplicate.
318 return;
320 sBackwardsSkewStamp = Some(aNow);
321 sLastPostTime = currentTime;
322 static_assert(sizeof(WPARAM) >= sizeof(DWORD),
323 "Can't fit a DWORD in a WPARAM");
324 ::PostMessage(mWnd, MOZ_WM_SKEWFIX, sLastPostTime, 0);
327 static bool GetAndClearBackwardsSkewStamp(DWORD aPostTime,
328 TimeStamp* aOutSkewStamp) {
329 if (aPostTime != sLastPostTime) {
330 // The SKEWFIX message is stale; we've sent a new one since then.
331 // Ignore this one.
332 return false;
334 MOZ_ASSERT(sBackwardsSkewStamp);
335 *aOutSkewStamp = sBackwardsSkewStamp.value();
336 sBackwardsSkewStamp = Nothing();
337 return true;
340 private:
341 static Maybe<TimeStamp> sBackwardsSkewStamp;
342 static DWORD sLastPostTime;
343 HWND mWnd;
346 Maybe<TimeStamp> CurrentWindowsTimeGetter::sBackwardsSkewStamp;
347 DWORD CurrentWindowsTimeGetter::sLastPostTime = 0;
349 } // namespace mozilla
351 /**************************************************************
353 * SECTION: globals variables
355 **************************************************************/
357 static const char* sScreenManagerContractID =
358 "@mozilla.org/gfx/screenmanager;1";
360 extern mozilla::LazyLogModule gWindowsLog;
362 static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
364 // General purpose user32.dll hook object
365 static WindowsDllInterceptor sUser32Intercept;
367 // 2 pixel offset for TransparencyMode::BorderlessGlass which equals the size of
368 // the default window border Windows paints. Glass will be extended inward
369 // this distance to remove the border.
370 static const int32_t kGlassMarginAdjustment = 2;
372 // When the client area is extended out into the default window frame area,
373 // this is the minimum amount of space along the edge of resizable windows
374 // we will always display a resize cursor in, regardless of the underlying
375 // content.
376 static const int32_t kResizableBorderMinSize = 3;
378 // Getting this object from the window server can be expensive. Keep it
379 // around, also get it off the main thread. (See bug 1640852)
380 StaticRefPtr<IVirtualDesktopManager> gVirtualDesktopManager;
381 static bool gInitializedVirtualDesktopManager = false;
383 // We should never really try to accelerate windows bigger than this. In some
384 // cases this might lead to no D3D9 acceleration where we could have had it
385 // but D3D9 does not reliably report when it supports bigger windows. 8192
386 // is as safe as we can get, we know at least D3D10 hardware always supports
387 // this, other hardware we expect to report correctly in D3D9.
388 #define MAX_ACCELERATED_DIMENSION 8192
390 // On window open (as well as after), Windows has an unfortunate habit of
391 // sending rather a lot of WM_NCHITTEST messages. Because we have to do point
392 // to DOM target conversions for these, we cache responses for a given
393 // coordinate this many milliseconds:
394 #define HITTEST_CACHE_LIFETIME_MS 50
396 #if defined(ACCESSIBILITY)
398 namespace mozilla {
401 * Windows touchscreen code works by setting a global WH_GETMESSAGE hook and
402 * injecting tiptsf.dll. The touchscreen process then posts registered messages
403 * to our main thread. The tiptsf hook picks up those registered messages and
404 * uses them as commands, some of which call into UIA, which then calls into
405 * MSAA, which then sends WM_GETOBJECT to us.
407 * We can get ahead of this by installing our own thread-local WH_GETMESSAGE
408 * hook. Since thread-local hooks are called ahead of global hooks, we will
409 * see these registered messages before tiptsf does. At this point we can then
410 * raise a flag that blocks a11y before invoking CallNextHookEx which will then
411 * invoke the global tiptsf hook. Then when we see WM_GETOBJECT, we check the
412 * flag by calling TIPMessageHandler::IsA11yBlocked().
414 * For Windows 8, we also hook tiptsf!ProcessCaretEvents, which is an a11y hook
415 * function that also calls into UIA.
417 class TIPMessageHandler {
418 public:
419 ~TIPMessageHandler() {
420 if (mHook) {
421 ::UnhookWindowsHookEx(mHook);
425 static void Initialize() {
426 if (!IsWin8OrLater()) {
427 return;
430 if (sInstance) {
431 return;
434 sInstance = new TIPMessageHandler();
435 ClearOnShutdown(&sInstance);
438 static bool IsA11yBlocked() {
439 if (!sInstance) {
440 return false;
443 return sInstance->mA11yBlockCount > 0;
446 private:
447 TIPMessageHandler() : mHook(nullptr), mA11yBlockCount(0) {
448 MOZ_ASSERT(NS_IsMainThread());
450 // Registered messages used by tiptsf
451 mMessages[0] = ::RegisterWindowMessage(L"ImmersiveFocusNotification");
452 mMessages[1] = ::RegisterWindowMessage(L"TipCloseMenus");
453 mMessages[2] = ::RegisterWindowMessage(L"TabletInputPanelOpening");
454 mMessages[3] = ::RegisterWindowMessage(L"IHM Pen or Touch Event noticed");
455 mMessages[4] = ::RegisterWindowMessage(L"ProgrammabilityCaretVisibility");
456 mMessages[5] = ::RegisterWindowMessage(L"CaretTrackingUpdateIPHidden");
457 mMessages[6] = ::RegisterWindowMessage(L"CaretTrackingUpdateIPInfo");
459 mHook = ::SetWindowsHookEx(WH_GETMESSAGE, &TIPHook, nullptr,
460 ::GetCurrentThreadId());
461 MOZ_ASSERT(mHook);
463 // On touchscreen devices, tiptsf.dll will have been loaded when STA COM was
464 // first initialized.
465 if (!IsWin10OrLater() && GetModuleHandle(L"tiptsf.dll") &&
466 !sProcessCaretEventsStub) {
467 sTipTsfInterceptor.Init("tiptsf.dll");
468 DebugOnly<bool> ok = sProcessCaretEventsStub.Set(
469 sTipTsfInterceptor, "ProcessCaretEvents", &ProcessCaretEventsHook);
470 MOZ_ASSERT(ok);
473 if (!sSendMessageTimeoutWStub) {
474 sUser32Intercept.Init("user32.dll");
475 DebugOnly<bool> hooked = sSendMessageTimeoutWStub.Set(
476 sUser32Intercept, "SendMessageTimeoutW", &SendMessageTimeoutWHook);
477 MOZ_ASSERT(hooked);
481 class MOZ_RAII A11yInstantiationBlocker {
482 public:
483 A11yInstantiationBlocker() {
484 if (!TIPMessageHandler::sInstance) {
485 return;
487 ++TIPMessageHandler::sInstance->mA11yBlockCount;
490 ~A11yInstantiationBlocker() {
491 if (!TIPMessageHandler::sInstance) {
492 return;
494 MOZ_ASSERT(TIPMessageHandler::sInstance->mA11yBlockCount > 0);
495 --TIPMessageHandler::sInstance->mA11yBlockCount;
499 friend class A11yInstantiationBlocker;
501 static LRESULT CALLBACK TIPHook(int aCode, WPARAM aWParam, LPARAM aLParam) {
502 if (aCode < 0 || !sInstance) {
503 return ::CallNextHookEx(nullptr, aCode, aWParam, aLParam);
506 MSG* msg = reinterpret_cast<MSG*>(aLParam);
507 UINT& msgCode = msg->message;
509 for (uint32_t i = 0; i < ArrayLength(sInstance->mMessages); ++i) {
510 if (msgCode == sInstance->mMessages[i]) {
511 A11yInstantiationBlocker block;
512 return ::CallNextHookEx(nullptr, aCode, aWParam, aLParam);
516 return ::CallNextHookEx(nullptr, aCode, aWParam, aLParam);
519 static void CALLBACK ProcessCaretEventsHook(HWINEVENTHOOK aWinEventHook,
520 DWORD aEvent, HWND aHwnd,
521 LONG aObjectId, LONG aChildId,
522 DWORD aGeneratingTid,
523 DWORD aEventTime) {
524 A11yInstantiationBlocker block;
525 sProcessCaretEventsStub(aWinEventHook, aEvent, aHwnd, aObjectId, aChildId,
526 aGeneratingTid, aEventTime);
529 static LRESULT WINAPI SendMessageTimeoutWHook(HWND aHwnd, UINT aMsgCode,
530 WPARAM aWParam, LPARAM aLParam,
531 UINT aFlags, UINT aTimeout,
532 PDWORD_PTR aMsgResult) {
533 // We don't want to handle this unless the message is a WM_GETOBJECT that we
534 // want to block, and the aHwnd is a nsWindow that belongs to the current
535 // (i.e., main) thread.
536 if (!aMsgResult || aMsgCode != WM_GETOBJECT ||
537 static_cast<LONG>(aLParam) != OBJID_CLIENT || !::NS_IsMainThread() ||
538 !WinUtils::GetNSWindowPtr(aHwnd) || !IsA11yBlocked()) {
539 return sSendMessageTimeoutWStub(aHwnd, aMsgCode, aWParam, aLParam, aFlags,
540 aTimeout, aMsgResult);
543 // In this case we want to fake the result that would happen if we had
544 // decided not to handle WM_GETOBJECT in our WndProc. We hand the message
545 // off to DefWindowProc to accomplish this.
546 *aMsgResult = static_cast<DWORD_PTR>(
547 ::DefWindowProcW(aHwnd, aMsgCode, aWParam, aLParam));
549 return static_cast<LRESULT>(TRUE);
552 static WindowsDllInterceptor sTipTsfInterceptor;
553 static WindowsDllInterceptor::FuncHookType<WINEVENTPROC>
554 sProcessCaretEventsStub;
555 static WindowsDllInterceptor::FuncHookType<decltype(&SendMessageTimeoutW)>
556 sSendMessageTimeoutWStub;
557 static StaticAutoPtr<TIPMessageHandler> sInstance;
559 HHOOK mHook;
560 UINT mMessages[7];
561 uint32_t mA11yBlockCount;
564 WindowsDllInterceptor TIPMessageHandler::sTipTsfInterceptor;
565 WindowsDllInterceptor::FuncHookType<WINEVENTPROC>
566 TIPMessageHandler::sProcessCaretEventsStub;
567 WindowsDllInterceptor::FuncHookType<decltype(&SendMessageTimeoutW)>
568 TIPMessageHandler::sSendMessageTimeoutWStub;
569 StaticAutoPtr<TIPMessageHandler> TIPMessageHandler::sInstance;
571 } // namespace mozilla
573 #endif // defined(ACCESSIBILITY)
575 namespace mozilla {
577 // This task will get the VirtualDesktopManager from the generic thread pool
578 // since doing this on the main thread on startup causes performance issues.
580 // See bug 1640852.
582 // This should be fine and should not require any locking, as when the main
583 // thread will access it, if it races with this function it will either find
584 // it to be null or to have a valid value.
585 class InitializeVirtualDesktopManagerTask : public Task {
586 public:
587 InitializeVirtualDesktopManagerTask() : Task(false, kDefaultPriorityValue) {}
589 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
590 bool GetName(nsACString& aName) override {
591 aName.AssignLiteral("InitializeVirtualDesktopManagerTask");
592 return true;
594 #endif
596 virtual bool Run() override {
597 if (!IsWin10OrLater()) {
598 return true;
601 RefPtr<IVirtualDesktopManager> desktopManager;
602 HRESULT hr = ::CoCreateInstance(
603 CLSID_VirtualDesktopManager, NULL, CLSCTX_INPROC_SERVER,
604 __uuidof(IVirtualDesktopManager), getter_AddRefs(desktopManager));
605 if (FAILED(hr)) {
606 return true;
609 gVirtualDesktopManager = desktopManager;
610 return true;
614 static bool GetMouseVanishSystemPref(bool aShouldUpdate) {
615 static Maybe<bool> sCachedMouseVanishSystemPref;
617 if (aShouldUpdate) {
618 sCachedMouseVanishSystemPref.reset();
621 if (sCachedMouseVanishSystemPref.isNothing()) {
622 BOOL mouseVanishSystemPref;
623 BOOL ok = ::SystemParametersInfo(SPI_GETMOUSEVANISH, 0,
624 &mouseVanishSystemPref, 0);
625 // If getting system pref failed, just use user pref.
626 sCachedMouseVanishSystemPref.emplace(
627 ok ? mouseVanishSystemPref
628 : StaticPrefs::widget_windows_hide_cursor_when_typing());
631 return *sCachedMouseVanishSystemPref;
634 static bool IsMouseVanishKey(WPARAM aVirtKey) {
635 switch (aVirtKey) {
636 case VK_SHIFT:
637 case VK_LSHIFT:
638 case VK_RSHIFT:
639 case VK_CONTROL:
640 case VK_LCONTROL:
641 case VK_RCONTROL:
642 case VK_MENU:
643 case VK_LMENU:
644 case VK_RMENU:
645 case VK_LWIN:
646 case VK_RWIN:
647 case VK_INSERT:
648 case VK_DELETE:
649 case VK_HOME:
650 case VK_END:
651 case VK_ESCAPE:
652 case VK_PRINT:
653 case VK_UP:
654 case VK_DOWN:
655 case VK_LEFT:
656 case VK_RIGHT:
657 case VK_PRIOR: // PgUp
658 case VK_NEXT: // PgDn
659 case 0xff: // Undefined. May be sent for Fn key.
660 return false;
661 default:
662 // Vanish unless Ctrl or Alt is also pressed, or if a key in
663 // a relevant range is pressed.
664 // The range between VK_F1 and VK_LAUNCH_APP2 includes control,
665 // function, browser, volume and media keys, all of which we ignore.
666 return (GetKeyState(VK_CONTROL) & 0x8000) != 0x8000 &&
667 (GetKeyState(VK_MENU) & 0x8000) != 0x8000 &&
668 (aVirtKey < VK_F1 || aVirtKey > VK_LAUNCH_APP2);
673 * Hide/unhide the cursor if the correct Windows and Firefox settings are set.
675 static void MaybeHideCursor(bool aShouldHide) {
676 static bool sMouseExists = [] {
677 // Before the first call to ShowCursor, the visibility count is 0
678 // if there is a mouse installed and -1 if not.
679 int count = ::ShowCursor(FALSE);
680 ::ShowCursor(TRUE);
681 return count == -1;
682 }();
684 if (!sMouseExists) {
685 return;
688 static bool sIsHidden = false;
689 bool shouldHide = aShouldHide &&
690 StaticPrefs::widget_windows_hide_cursor_when_typing() &&
691 GetMouseVanishSystemPref(false);
693 if (shouldHide != sIsHidden) {
694 [[maybe_unused]] int count = ::ShowCursor(aShouldHide ? FALSE : TRUE);
695 MOZ_ASSERT(count == (aShouldHide ? -1 : 0));
696 sIsHidden = shouldHide;
700 // Ground-truth query: does Windows claim the window is cloaked right now?
701 static bool IsCloaked(HWND hwnd) {
702 DWORD cloakedState;
703 HRESULT hr = ::DwmGetWindowAttribute(hwnd, DWMWA_CLOAKED, &cloakedState,
704 sizeof(cloakedState));
706 if (FAILED(hr)) {
707 MOZ_LOG(sCloakingLog, LogLevel::Warning,
708 ("failed (%08lX) to query cloaking state for HWND %p", hr, hwnd));
709 return false;
712 return cloakedState != 0;
715 } // namespace mozilla
717 /**************************************************************
718 **************************************************************
720 ** BLOCK: nsIWidget impl.
722 ** nsIWidget interface implementation, broken down into
723 ** sections.
725 **************************************************************
726 **************************************************************/
728 /**************************************************************
730 * SECTION: nsWindow construction and destruction
732 **************************************************************/
734 nsWindow::nsWindow(bool aIsChildWindow)
735 : nsBaseWidget(BorderStyle::Default),
736 mBrush(::CreateSolidBrush(NSRGB_2_COLOREF(::GetSysColor(COLOR_BTNFACE)))),
737 mFrameState(std::in_place, this),
738 mIsChildWindow(aIsChildWindow),
739 mLastPaintEndTime(TimeStamp::Now()),
740 mCachedHitTestTime(TimeStamp::Now()),
741 mSizeConstraintsScale(GetDefaultScale().scale),
742 mDesktopId("DesktopIdMutex") {
743 MOZ_ASSERT(mWindowType == WindowType::Child);
745 if (!gInitializedVirtualDesktopManager) {
746 TaskController::Get()->AddTask(
747 MakeAndAddRef<InitializeVirtualDesktopManagerTask>());
748 gInitializedVirtualDesktopManager = true;
751 // Global initialization
752 if (!sInstanceCount) {
753 // Global app registration id for Win7 and up. See
754 // WinTaskbar.cpp for details.
755 // MSIX packages explicitly do not support setting the appid from within
756 // the app, as it is set in the package manifest instead.
757 if (!WinUtils::HasPackageIdentity()) {
758 mozilla::widget::WinTaskbar::RegisterAppUserModelID();
760 KeyboardLayout::GetInstance()->OnLayoutChange(::GetKeyboardLayout(0));
761 #if defined(ACCESSIBILITY)
762 mozilla::TIPMessageHandler::Initialize();
763 #endif // defined(ACCESSIBILITY)
764 if (SUCCEEDED(::OleInitialize(nullptr))) {
765 sIsOleInitialized = true;
767 NS_ASSERTION(sIsOleInitialized, "***** OLE is not initialized!\n");
768 MouseScrollHandler::Initialize();
769 // Init theme data
770 nsUXThemeData::UpdateNativeThemeInfo();
771 RedirectedKeyDownMessageManager::Forget();
772 if (mPointerEvents.ShouldEnableInkCollector()) {
773 InkCollector::sInkCollector = new InkCollector();
775 } // !sInstanceCount
777 sInstanceCount++;
780 nsWindow::~nsWindow() {
781 mInDtor = true;
783 // If the widget was released without calling Destroy() then the native window
784 // still exists, and we need to destroy it. Destroy() will early-return if it
785 // was already called. In any case it is important to call it before
786 // destroying mPresentLock (cf. 1156182).
787 Destroy();
789 // Free app icon resources. This must happen after `OnDestroy` (see bug
790 // 708033).
791 if (mIconSmall) ::DestroyIcon(mIconSmall);
793 if (mIconBig) ::DestroyIcon(mIconBig);
795 sInstanceCount--;
797 // Global shutdown
798 if (sInstanceCount == 0) {
799 if (InkCollector::sInkCollector) {
800 InkCollector::sInkCollector->Shutdown();
801 InkCollector::sInkCollector = nullptr;
803 IMEHandler::Terminate();
804 sCurrentCursor = {};
805 if (sIsOleInitialized) {
806 ::OleFlushClipboard();
807 ::OleUninitialize();
808 sIsOleInitialized = false;
812 NS_IF_RELEASE(mNativeDragTarget);
815 /**************************************************************
817 * SECTION: nsIWidget::Create, nsIWidget::Destroy
819 * Creating and destroying windows for this widget.
821 **************************************************************/
823 // Allow Derived classes to modify the height that is passed
824 // when the window is created or resized.
825 int32_t nsWindow::GetHeight(int32_t aProposedHeight) { return aProposedHeight; }
827 static bool ShouldCacheTitleBarInfo(WindowType aWindowType,
828 BorderStyle aBorderStyle) {
829 return (aWindowType == WindowType::TopLevel) &&
830 (aBorderStyle == BorderStyle::Default ||
831 aBorderStyle == BorderStyle::All) &&
832 (!nsUXThemeData::sTitlebarInfoPopulatedThemed ||
833 !nsUXThemeData::sTitlebarInfoPopulatedAero);
836 void nsWindow::SendAnAPZEvent(InputData& aEvent) {
837 LRESULT popupHandlingResult;
838 if (DealWithPopups(mWnd, MOZ_WM_DMANIP, 0, 0, &popupHandlingResult)) {
839 // We need to consume the event after using it to roll up the popup(s).
840 return;
843 if (mSwipeTracker && aEvent.mInputType == PANGESTURE_INPUT) {
844 // Give the swipe tracker a first pass at the event. If a new pan gesture
845 // has been started since the beginning of the swipe, the swipe tracker
846 // will know to ignore the event.
847 nsEventStatus status =
848 mSwipeTracker->ProcessEvent(aEvent.AsPanGestureInput());
849 if (status == nsEventStatus_eConsumeNoDefault) {
850 return;
854 APZEventResult result;
855 if (mAPZC) {
856 result = mAPZC->InputBridge()->ReceiveInputEvent(aEvent);
858 if (result.GetStatus() == nsEventStatus_eConsumeNoDefault) {
859 return;
862 MOZ_ASSERT(aEvent.mInputType == PANGESTURE_INPUT ||
863 aEvent.mInputType == PINCHGESTURE_INPUT);
865 if (aEvent.mInputType == PANGESTURE_INPUT) {
866 PanGestureInput& panInput = aEvent.AsPanGestureInput();
867 WidgetWheelEvent event = panInput.ToWidgetEvent(this);
868 if (!mAPZC) {
869 if (MayStartSwipeForNonAPZ(panInput)) {
870 return;
872 } else {
873 event = MayStartSwipeForAPZ(panInput, result);
876 ProcessUntransformedAPZEvent(&event, result);
878 return;
881 PinchGestureInput& pinchInput = aEvent.AsPinchGestureInput();
882 WidgetWheelEvent event = pinchInput.ToWidgetEvent(this);
883 ProcessUntransformedAPZEvent(&event, result);
886 void nsWindow::RecreateDirectManipulationIfNeeded() {
887 DestroyDirectManipulation();
889 if (mWindowType != WindowType::TopLevel && mWindowType != WindowType::Popup) {
890 return;
893 if (!(StaticPrefs::apz_allow_zooming() ||
894 StaticPrefs::apz_windows_use_direct_manipulation()) ||
895 StaticPrefs::apz_windows_force_disable_direct_manipulation()) {
896 return;
899 if (!IsWin10OrLater()) {
900 // Chrome source said the Windows Direct Manipulation implementation had
901 // important bugs until Windows 10 (although IE on Windows 8.1 seems to use
902 // Direct Manipulation).
903 return;
906 mDmOwner = MakeUnique<DirectManipulationOwner>(this);
908 LayoutDeviceIntRect bounds(mBounds.X(), mBounds.Y(), mBounds.Width(),
909 GetHeight(mBounds.Height()));
910 mDmOwner->Init(bounds);
913 void nsWindow::ResizeDirectManipulationViewport() {
914 if (mDmOwner) {
915 LayoutDeviceIntRect bounds(mBounds.X(), mBounds.Y(), mBounds.Width(),
916 GetHeight(mBounds.Height()));
917 mDmOwner->ResizeViewport(bounds);
921 void nsWindow::DestroyDirectManipulation() {
922 if (mDmOwner) {
923 mDmOwner->Destroy();
924 mDmOwner.reset();
928 // Create the proper widget
929 nsresult nsWindow::Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
930 const LayoutDeviceIntRect& aRect,
931 widget::InitData* aInitData) {
932 // Historical note: there was once some belief and/or intent that nsWindows
933 // could be created on arbitrary threads, and this may still be reflected in
934 // some comments.
935 MOZ_ASSERT(NS_IsMainThread());
937 widget::InitData defaultInitData;
938 if (!aInitData) aInitData = &defaultInitData;
940 nsIWidget* baseParent =
941 aInitData->mWindowType == WindowType::Dialog ||
942 aInitData->mWindowType == WindowType::TopLevel ||
943 aInitData->mWindowType == WindowType::Invisible
944 ? nullptr
945 : aParent;
947 mIsTopWidgetWindow = (nullptr == baseParent);
948 mBounds = aRect;
950 // Ensure that the toolkit is created.
951 nsToolkit::GetToolkit();
953 BaseCreate(baseParent, aInitData);
955 HWND parent;
956 if (aParent) { // has a nsIWidget parent
957 parent = aParent ? (HWND)aParent->GetNativeData(NS_NATIVE_WINDOW) : nullptr;
958 mParent = aParent;
959 } else { // has a nsNative parent
960 parent = (HWND)aNativeParent;
961 mParent =
962 aNativeParent ? WinUtils::GetNSWindowPtr((HWND)aNativeParent) : nullptr;
965 mIsRTL = aInitData->mRTL;
966 mForMenupopupFrame = aInitData->mForMenupopupFrame;
967 mOpeningAnimationSuppressed = aInitData->mIsAnimationSuppressed;
968 mAlwaysOnTop = aInitData->mAlwaysOnTop;
969 mResizable = aInitData->mResizable;
971 DWORD style = WindowStyle();
972 DWORD extendedStyle = WindowExStyle();
974 // When window is PiP window on Windows7, WS_EX_COMPOSITED is set to suppress
975 // flickering during resizing with hardware acceleration.
976 bool isPIPWindow = aInitData && aInitData->mPIPWindow;
977 if (isPIPWindow && !IsWin8OrLater() &&
978 gfxConfig::IsEnabled(gfx::Feature::HW_COMPOSITING) &&
979 WidgetTypeSupportsAcceleration()) {
980 extendedStyle |= WS_EX_COMPOSITED;
983 if (mWindowType == WindowType::Popup) {
984 if (!aParent) {
985 parent = nullptr;
988 if (!IsWin8OrLater() && HasBogusPopupsDropShadowOnMultiMonitor() &&
989 ShouldUseOffMainThreadCompositing()) {
990 extendedStyle |= WS_EX_COMPOSITED;
992 } else if (mWindowType == WindowType::Invisible) {
993 // Make sure CreateWindowEx succeeds at creating a toplevel window
994 style &= ~0x40000000; // WS_CHILDWINDOW
995 } else {
996 // See if the caller wants to explictly set clip children and clip siblings
997 if (aInitData->mClipChildren) {
998 style |= WS_CLIPCHILDREN;
999 } else {
1000 style &= ~WS_CLIPCHILDREN;
1002 if (aInitData->mClipSiblings) {
1003 style |= WS_CLIPSIBLINGS;
1007 const wchar_t* className = ChooseWindowClass(mWindowType, mForMenupopupFrame);
1009 // Take specific actions when creating the first top-level window
1010 static bool sFirstTopLevelWindowCreated = false;
1011 if (aInitData->mWindowType == WindowType::TopLevel && !aParent &&
1012 !sFirstTopLevelWindowCreated) {
1013 sFirstTopLevelWindowCreated = true;
1014 mWnd = ConsumePreXULSkeletonUIHandle();
1015 auto skeletonUIError = GetPreXULSkeletonUIErrorReason();
1016 if (skeletonUIError) {
1017 nsAutoString errorString(
1018 GetPreXULSkeletonUIErrorString(skeletonUIError.value()));
1019 Telemetry::ScalarSet(
1020 Telemetry::ScalarID::STARTUP_SKELETON_UI_DISABLED_REASON,
1021 errorString);
1023 if (mWnd) {
1024 MOZ_ASSERT(style == kPreXULSkeletonUIWindowStyle,
1025 "The skeleton UI window style should match the expected "
1026 "style for the first window created");
1027 MOZ_ASSERT(extendedStyle == kPreXULSkeletonUIWindowStyleEx,
1028 "The skeleton UI window extended style should match the "
1029 "expected extended style for the first window created");
1030 MOZ_ASSERT(
1031 ::GetWindowThreadProcessId(mWnd, nullptr) == ::GetCurrentThreadId(),
1032 "The skeleton UI window should be created on the same thread as "
1033 "other windows");
1034 mIsShowingPreXULSkeletonUI = true;
1036 // If we successfully consumed the pre-XUL skeleton UI, just update
1037 // our internal state to match what is currently being displayed.
1038 mIsVisible = true;
1039 mIsCloaked = mozilla::IsCloaked(mWnd);
1040 mFrameState->ConsumePreXULSkeletonState(WasPreXULSkeletonUIMaximized());
1042 // These match the margins set in browser-tabsintitlebar.js with
1043 // default prefs on Windows. Bug 1673092 tracks lining this up with
1044 // that more correctly instead of hard-coding it.
1045 SetNonClientMargins(LayoutDeviceIntMargin(0, 2, 2, 2));
1047 // Reset the WNDPROC for this window and its whole class, as we had
1048 // to use our own WNDPROC when creating the the skeleton UI window.
1049 ::SetWindowLongPtrW(mWnd, GWLP_WNDPROC,
1050 reinterpret_cast<LONG_PTR>(
1051 WinUtils::NonClientDpiScalingDefWindowProcW));
1052 ::SetClassLongPtrW(mWnd, GCLP_WNDPROC,
1053 reinterpret_cast<LONG_PTR>(
1054 WinUtils::NonClientDpiScalingDefWindowProcW));
1058 if (!mWnd) {
1059 mWnd =
1060 ::CreateWindowExW(extendedStyle, className, L"", style, aRect.X(),
1061 aRect.Y(), aRect.Width(), GetHeight(aRect.Height()),
1062 parent, nullptr, nsToolkit::mDllInstance, nullptr);
1065 if (!mWnd) {
1066 NS_WARNING("nsWindow CreateWindowEx failed.");
1067 return NS_ERROR_FAILURE;
1070 if (!sWinCloakEventHook) {
1071 MOZ_LOG(sCloakingLog, LogLevel::Info, ("Registering cloaking event hook"));
1073 // C++03 lambda approximation until P2173R1 is available (-std=c++2b)
1074 struct StdcallLambda {
1075 static void CALLBACK OnCloakUncloakHook(HWINEVENTHOOK hWinEventHook,
1076 DWORD event, HWND hwnd,
1077 LONG idObject, LONG idChild,
1078 DWORD idEventThread,
1079 DWORD dwmsEventTime) {
1080 const bool isCloaked = event == EVENT_OBJECT_CLOAKED ? true : false;
1081 nsWindow::OnCloakEvent(hwnd, isCloaked);
1085 const HWINEVENTHOOK hook = ::SetWinEventHook(
1086 EVENT_OBJECT_CLOAKED, EVENT_OBJECT_UNCLOAKED, HMODULE(nullptr),
1087 &StdcallLambda::OnCloakUncloakHook, ::GetCurrentProcessId(),
1088 ::GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT);
1089 sWinCloakEventHook = Some(hook);
1091 if (!hook) {
1092 const DWORD err = ::GetLastError();
1093 MOZ_LOG(sCloakingLog, LogLevel::Error,
1094 ("Failed to register cloaking event hook! GLE = %lu (0x%lX)", err,
1095 err));
1099 if (aInitData->mIsPrivate) {
1100 if (NimbusFeatures::GetBool("majorRelease2022"_ns,
1101 "feltPrivacyWindowSeparation"_ns, true) &&
1102 // Although permanent Private Browsing mode is indeed Private Browsing,
1103 // we choose to make it look like regular Firefox in terms of the icon
1104 // it uses (which also means we shouldn't use the Private Browsing
1105 // AUMID).
1106 !StaticPrefs::browser_privatebrowsing_autostart()) {
1107 RefPtr<IPropertyStore> pPropStore;
1108 if (!FAILED(SHGetPropertyStoreForWindow(mWnd, IID_IPropertyStore,
1109 getter_AddRefs(pPropStore)))) {
1110 PROPVARIANT pv;
1111 nsAutoString aumid;
1112 // make sure we're using the private browsing AUMID so that taskbar
1113 // grouping works properly
1114 Unused << NS_WARN_IF(
1115 !mozilla::widget::WinTaskbar::GenerateAppUserModelID(aumid, true));
1116 if (!FAILED(InitPropVariantFromString(aumid.get(), &pv))) {
1117 if (!FAILED(pPropStore->SetValue(PKEY_AppUserModel_ID, pv))) {
1118 pPropStore->Commit();
1121 PropVariantClear(&pv);
1124 HICON icon = ::LoadIconW(::GetModuleHandleW(nullptr),
1125 MAKEINTRESOURCEW(IDI_PBMODE));
1126 SetBigIcon(icon);
1127 SetSmallIcon(icon);
1131 mDeviceNotifyHandle = InputDeviceUtils::RegisterNotification(mWnd);
1133 // If mDefaultScale is set before mWnd has been set, it will have the scale of
1134 // the primary monitor, rather than the monitor that the window is actually
1135 // on. For non-popup windows this gets corrected by the WM_DPICHANGED message
1136 // which resets mDefaultScale, but for popup windows we don't reset
1137 // mDefaultScale on that message. In order to ensure that popup windows
1138 // spawned on a non-primary monitor end up with the correct scale, we reset
1139 // mDefaultScale here so that it gets recomputed using the correct monitor now
1140 // that we have a mWnd.
1141 mDefaultScale = -1.0;
1143 if (mIsRTL) {
1144 DWORD dwAttribute = TRUE;
1145 DwmSetWindowAttribute(mWnd, DWMWA_NONCLIENT_RTL_LAYOUT, &dwAttribute,
1146 sizeof dwAttribute);
1149 UpdateDarkModeToolbar();
1151 if (mOpeningAnimationSuppressed) {
1152 SuppressAnimation(true);
1155 if (mAlwaysOnTop) {
1156 ::SetWindowPos(mWnd, HWND_TOPMOST, 0, 0, 0, 0,
1157 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
1160 if (mWindowType != WindowType::Invisible &&
1161 MouseScrollHandler::Device::IsFakeScrollableWindowNeeded()) {
1162 // Ugly Thinkpad Driver Hack (Bugs 507222 and 594977)
1164 // We create two zero-sized windows as descendants of the top-level window,
1165 // like so:
1167 // Top-level window (MozillaWindowClass)
1168 // FAKETRACKPOINTSCROLLCONTAINER (MozillaWindowClass)
1169 // FAKETRACKPOINTSCROLLABLE (MozillaWindowClass)
1171 // We need to have the middle window, otherwise the Trackpoint driver
1172 // will fail to deliver scroll messages. WM_MOUSEWHEEL messages are
1173 // sent to the FAKETRACKPOINTSCROLLABLE, which then propagate up the
1174 // window hierarchy until they are handled by nsWindow::WindowProc.
1175 // WM_HSCROLL messages are also sent to the FAKETRACKPOINTSCROLLABLE,
1176 // but these do not propagate automatically, so we have the window
1177 // procedure pretend that they were dispatched to the top-level window
1178 // instead.
1180 // The FAKETRACKPOINTSCROLLABLE needs to have the specific window styles it
1181 // is given below so that it catches the Trackpoint driver's heuristics.
1182 HWND scrollContainerWnd = ::CreateWindowW(
1183 className, L"FAKETRACKPOINTSCROLLCONTAINER", WS_CHILD | WS_VISIBLE, 0,
1184 0, 0, 0, mWnd, nullptr, nsToolkit::mDllInstance, nullptr);
1185 HWND scrollableWnd = ::CreateWindowW(
1186 className, L"FAKETRACKPOINTSCROLLABLE",
1187 WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | 0x30, 0, 0, 0, 0,
1188 scrollContainerWnd, nullptr, nsToolkit::mDllInstance, nullptr);
1190 // Give the FAKETRACKPOINTSCROLLABLE window a specific ID so that
1191 // WindowProcInternal can distinguish it from the top-level window
1192 // easily.
1193 ::SetWindowLongPtrW(scrollableWnd, GWLP_ID, eFakeTrackPointScrollableID);
1195 // Make FAKETRACKPOINTSCROLLABLE use nsWindow::WindowProc, and store the
1196 // old window procedure in its "user data".
1197 WNDPROC oldWndProc = (WNDPROC)::SetWindowLongPtrW(
1198 scrollableWnd, GWLP_WNDPROC, (LONG_PTR)nsWindow::WindowProc);
1199 ::SetWindowLongPtrW(scrollableWnd, GWLP_USERDATA, (LONG_PTR)oldWndProc);
1202 // We will start receiving native events after associating with our native
1203 // window. We will also become the output of WinUtils::GetNSWindowPtr for that
1204 // window.
1205 if (!AssociateWithNativeWindow()) {
1206 return NS_ERROR_FAILURE;
1209 // Starting with Windows XP, a process always runs within a terminal services
1210 // session. In order to play nicely with RDP, fast user switching, and the
1211 // lock screen, we should be handling WM_WTSSESSION_CHANGE. We must register
1212 // our HWND in order to receive this message.
1213 DebugOnly<BOOL> wtsRegistered =
1214 ::WTSRegisterSessionNotification(mWnd, NOTIFY_FOR_THIS_SESSION);
1215 NS_ASSERTION(wtsRegistered, "WTSRegisterSessionNotification failed!\n");
1217 mDefaultIMC.Init(this);
1218 IMEHandler::InitInputContext(this, mInputContext);
1220 // Query for command button metric data for rendering the titlebar. We
1221 // only do this once on the first window that has an actual titlebar
1222 if (ShouldCacheTitleBarInfo(mWindowType, mBorderStyle)) {
1223 nsUXThemeData::UpdateTitlebarInfo(mWnd);
1226 static bool a11yPrimed = false;
1227 if (!a11yPrimed && mWindowType == WindowType::TopLevel) {
1228 a11yPrimed = true;
1229 if (Preferences::GetInt("accessibility.force_disabled", 0) == -1) {
1230 ::PostMessage(mWnd, MOZ_WM_STARTA11Y, 0, 0);
1234 RecreateDirectManipulationIfNeeded();
1236 return NS_OK;
1239 void nsWindow::LocalesChanged() {
1240 bool isRTL = intl::LocaleService::GetInstance()->IsAppLocaleRTL();
1241 if (mIsRTL != isRTL) {
1242 DWORD dwAttribute = isRTL;
1243 DwmSetWindowAttribute(mWnd, DWMWA_NONCLIENT_RTL_LAYOUT, &dwAttribute,
1244 sizeof dwAttribute);
1245 mIsRTL = isRTL;
1249 // Close this nsWindow
1250 void nsWindow::Destroy() {
1251 // WM_DESTROY has already fired, avoid calling it twice
1252 if (mOnDestroyCalled) return;
1254 // Don't destroy windows that have file pickers open, we'll tear these down
1255 // later once the picker is closed.
1256 mDestroyCalled = true;
1257 if (mPickerDisplayCount) return;
1259 // During the destruction of all of our children, make sure we don't get
1260 // deleted.
1261 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
1263 DestroyDirectManipulation();
1266 * On windows the LayerManagerOGL destructor wants the widget to be around for
1267 * cleanup. It also would like to have the HWND intact, so we nullptr it here.
1269 DestroyLayerManager();
1271 InputDeviceUtils::UnregisterNotification(mDeviceNotifyHandle);
1272 mDeviceNotifyHandle = nullptr;
1274 // The DestroyWindow function destroys the specified window. The function
1275 // sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it
1276 // and remove the keyboard focus from it. The function also destroys the
1277 // window's menu, flushes the thread message queue, destroys timers, removes
1278 // clipboard ownership, and breaks the clipboard viewer chain (if the window
1279 // is at the top of the viewer chain).
1281 // If the specified window is a parent or owner window, DestroyWindow
1282 // automatically destroys the associated child or owned windows when it
1283 // destroys the parent or owner window. The function first destroys child or
1284 // owned windows, and then it destroys the parent or owner window.
1285 VERIFY(::DestroyWindow(mWnd));
1287 // Our windows can be subclassed which may prevent us receiving WM_DESTROY. If
1288 // OnDestroy() didn't get called, call it now.
1289 if (false == mOnDestroyCalled) {
1290 MSGResult msgResult;
1291 mWindowHook.Notify(mWnd, WM_DESTROY, 0, 0, msgResult);
1292 OnDestroy();
1296 /**************************************************************
1298 * SECTION: Window class utilities
1300 * Utilities for calculating the proper window class name for
1301 * Create window.
1303 **************************************************************/
1305 /* static */
1306 const wchar_t* nsWindow::RegisterWindowClass(const wchar_t* aClassName,
1307 UINT aExtraStyle, LPWSTR aIconID) {
1308 WNDCLASSW wc;
1309 if (::GetClassInfoW(nsToolkit::mDllInstance, aClassName, &wc)) {
1310 // already registered
1311 return aClassName;
1314 wc.style = CS_DBLCLKS | aExtraStyle;
1315 wc.lpfnWndProc = WinUtils::NonClientDpiScalingDefWindowProcW;
1316 wc.cbClsExtra = 0;
1317 wc.cbWndExtra = 0;
1318 wc.hInstance = nsToolkit::mDllInstance;
1319 wc.hIcon =
1320 aIconID ? ::LoadIconW(::GetModuleHandleW(nullptr), aIconID) : nullptr;
1321 wc.hCursor = nullptr;
1322 wc.hbrBackground = nullptr;
1323 wc.lpszMenuName = nullptr;
1324 wc.lpszClassName = aClassName;
1326 if (!::RegisterClassW(&wc)) {
1327 // For older versions of Win32 (i.e., not XP), the registration may
1328 // fail with aExtraStyle, so we have to re-register without it.
1329 wc.style = CS_DBLCLKS;
1330 ::RegisterClassW(&wc);
1332 return aClassName;
1335 static LPWSTR const gStockApplicationIcon = MAKEINTRESOURCEW(32512);
1337 /* static */
1338 const wchar_t* nsWindow::ChooseWindowClass(WindowType aWindowType,
1339 bool aForMenupopupFrame) {
1340 MOZ_ASSERT_IF(aForMenupopupFrame, aWindowType == WindowType::Popup);
1341 switch (aWindowType) {
1342 case WindowType::Invisible:
1343 return RegisterWindowClass(kClassNameHidden, 0, gStockApplicationIcon);
1344 case WindowType::Dialog:
1345 return RegisterWindowClass(kClassNameDialog, 0, 0);
1346 case WindowType::Popup:
1347 if (aForMenupopupFrame) {
1348 return RegisterWindowClass(kClassNameDropShadow, CS_DROPSHADOW,
1349 gStockApplicationIcon);
1351 [[fallthrough]];
1352 default:
1353 return RegisterWindowClass(GetMainWindowClass(), 0,
1354 gStockApplicationIcon);
1358 /**************************************************************
1360 * SECTION: Window styles utilities
1362 * Return the proper windows styles and extended styles.
1364 **************************************************************/
1366 // Return nsWindow styles
1367 DWORD nsWindow::WindowStyle() {
1368 DWORD style;
1370 switch (mWindowType) {
1371 case WindowType::Child:
1372 style = WS_OVERLAPPED;
1373 break;
1375 case WindowType::Dialog:
1376 style = WS_OVERLAPPED | WS_BORDER | WS_DLGFRAME | WS_SYSMENU | DS_3DLOOK |
1377 DS_MODALFRAME | WS_CLIPCHILDREN;
1378 if (mBorderStyle != BorderStyle::Default)
1379 style |= WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
1380 break;
1382 case WindowType::Popup:
1383 style = WS_POPUP;
1384 if (!HasGlass()) {
1385 style |= WS_OVERLAPPED;
1387 break;
1389 default:
1390 NS_ERROR("unknown border style");
1391 [[fallthrough]];
1393 case WindowType::TopLevel:
1394 case WindowType::Invisible:
1395 style = WS_OVERLAPPED | WS_BORDER | WS_DLGFRAME | WS_SYSMENU |
1396 WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPCHILDREN;
1397 break;
1400 if (mBorderStyle != BorderStyle::Default &&
1401 mBorderStyle != BorderStyle::All) {
1402 if (mBorderStyle == BorderStyle::None ||
1403 !(mBorderStyle & BorderStyle::Border))
1404 style &= ~WS_BORDER;
1406 if (mBorderStyle == BorderStyle::None ||
1407 !(mBorderStyle & BorderStyle::Title)) {
1408 style &= ~WS_DLGFRAME;
1409 style |= WS_POPUP;
1410 style &= ~WS_CHILD;
1413 if (mBorderStyle == BorderStyle::None ||
1414 !(mBorderStyle & BorderStyle::Close))
1415 style &= ~0;
1416 // XXX The close box can only be removed by changing the window class,
1417 // as far as I know --- roc+moz@cs.cmu.edu
1419 if (mBorderStyle == BorderStyle::None ||
1420 !(mBorderStyle & (BorderStyle::Menu | BorderStyle::Close)))
1421 style &= ~WS_SYSMENU;
1422 // Looks like getting rid of the system menu also does away with the
1423 // close box. So, we only get rid of the system menu if you want neither it
1424 // nor the close box. How does the Windows "Dialog" window class get just
1425 // closebox and no sysmenu? Who knows.
1427 if (mBorderStyle == BorderStyle::None ||
1428 !(mBorderStyle & BorderStyle::ResizeH))
1429 style &= ~WS_THICKFRAME;
1431 if (mBorderStyle == BorderStyle::None ||
1432 !(mBorderStyle & BorderStyle::Minimize))
1433 style &= ~WS_MINIMIZEBOX;
1435 if (mBorderStyle == BorderStyle::None ||
1436 !(mBorderStyle & BorderStyle::Maximize))
1437 style &= ~WS_MAXIMIZEBOX;
1439 if (IsPopupWithTitleBar()) {
1440 style |= WS_CAPTION;
1441 if (mBorderStyle & BorderStyle::Close) {
1442 style |= WS_SYSMENU;
1447 if (mIsChildWindow) {
1448 style |= WS_CLIPCHILDREN;
1449 if (!(style & WS_POPUP)) {
1450 style |= WS_CHILD; // WS_POPUP and WS_CHILD are mutually exclusive.
1454 VERIFY_WINDOW_STYLE(style);
1455 return style;
1458 // Return nsWindow extended styles
1459 DWORD nsWindow::WindowExStyle() {
1460 switch (mWindowType) {
1461 case WindowType::Child:
1462 return 0;
1464 case WindowType::Dialog:
1465 return WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME;
1467 case WindowType::Popup: {
1468 DWORD extendedStyle = WS_EX_TOOLWINDOW;
1469 if (mPopupLevel == PopupLevel::Top) extendedStyle |= WS_EX_TOPMOST;
1470 return extendedStyle;
1472 default:
1473 NS_ERROR("unknown border style");
1474 [[fallthrough]];
1476 case WindowType::TopLevel:
1477 case WindowType::Invisible:
1478 return WS_EX_WINDOWEDGE;
1482 /**************************************************************
1484 * SECTION: Native window association utilities
1486 * Used in Create and Destroy. A nsWindow can associate with its
1487 * underlying native window mWnd. Once a native window is
1488 * associated with a nsWindow, its native events will be handled
1489 * by the static member function nsWindow::WindowProc. Moreover,
1490 * the association will be registered in the WinUtils association
1491 * list, that is, calling WinUtils::GetNSWindowPtr on the native
1492 * window will return the associated nsWindow. This is used in
1493 * nsWindow::WindowProc to correctly dispatch native events to
1494 * the handler methods defined in nsWindow, even though it is a
1495 * static member function.
1497 * After dissociation, the native events of the native window will
1498 * no longer be handled by nsWindow::WindowProc, and will thus not
1499 * be dispatched to the nsWindow native event handler methods.
1500 * Moreover, the association will no longer be registered in the
1501 * WinUtils association list, so calling WinUtils::GetNSWindowPtr
1502 * on the native window will return nullptr.
1504 **************************************************************/
1506 bool nsWindow::AssociateWithNativeWindow() {
1507 if (!mWnd || !IsWindow(mWnd)) {
1508 NS_ERROR("Invalid window handle");
1509 return false;
1512 // Connect the this pointer to the native window handle.
1513 // This should be done before SetWindowLongPtrW, because nsWindow::WindowProc
1514 // uses WinUtils::GetNSWindowPtr internally.
1515 WinUtils::SetNSWindowPtr(mWnd, this);
1517 ::SetLastError(ERROR_SUCCESS);
1518 const auto prevWndProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtrW(
1519 mWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(nsWindow::WindowProc)));
1520 if (!prevWndProc && GetLastError() != ERROR_SUCCESS) {
1521 NS_ERROR("Failure in SetWindowLongPtrW");
1522 WinUtils::SetNSWindowPtr(mWnd, nullptr);
1523 return false;
1526 mPrevWndProc.emplace(prevWndProc);
1527 return true;
1530 void nsWindow::DissociateFromNativeWindow() {
1531 if (!mWnd || !IsWindow(mWnd) || mPrevWndProc.isNothing()) {
1532 return;
1535 DebugOnly<WNDPROC> wndProcBeforeDissociate =
1536 reinterpret_cast<WNDPROC>(::SetWindowLongPtrW(
1537 mWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(*mPrevWndProc)));
1538 NS_ASSERTION(wndProcBeforeDissociate == nsWindow::WindowProc,
1539 "Unstacked an unexpected native window procedure");
1541 WinUtils::SetNSWindowPtr(mWnd, nullptr);
1542 mPrevWndProc.reset();
1545 /**************************************************************
1547 * SECTION: nsIWidget::SetParent, nsIWidget::GetParent
1549 * Set or clear the parent widgets using window properties, and
1550 * handles calculating native parent handles.
1552 **************************************************************/
1554 // Get and set parent widgets
1555 void nsWindow::SetParent(nsIWidget* aNewParent) {
1556 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
1557 nsIWidget* parent = GetParent();
1558 if (parent) {
1559 parent->RemoveChild(this);
1562 mParent = aNewParent;
1564 if (aNewParent) {
1565 ReparentNativeWidget(aNewParent);
1566 aNewParent->AddChild(this);
1567 return;
1569 if (mWnd) {
1570 // If we have no parent, SetParent should return the desktop.
1571 VERIFY(::SetParent(mWnd, nullptr));
1572 RecreateDirectManipulationIfNeeded();
1576 void nsWindow::ReparentNativeWidget(nsIWidget* aNewParent) {
1577 MOZ_ASSERT(aNewParent, "null widget");
1579 mParent = aNewParent;
1580 if (mWindowType == WindowType::Popup) {
1581 return;
1583 HWND newParent = (HWND)aNewParent->GetNativeData(NS_NATIVE_WINDOW);
1584 NS_ASSERTION(newParent, "Parent widget has a null native window handle");
1585 if (newParent && mWnd) {
1586 ::SetParent(mWnd, newParent);
1587 RecreateDirectManipulationIfNeeded();
1591 nsIWidget* nsWindow::GetParent(void) {
1592 if (mIsTopWidgetWindow) {
1593 return nullptr;
1595 if (mInDtor || mOnDestroyCalled) {
1596 return nullptr;
1598 return mParent;
1601 static int32_t RoundDown(double aDouble) {
1602 return aDouble > 0 ? static_cast<int32_t>(floor(aDouble))
1603 : static_cast<int32_t>(ceil(aDouble));
1606 float nsWindow::GetDPI() {
1607 float dpi = 96.0f;
1608 nsCOMPtr<nsIScreen> screen = GetWidgetScreen();
1609 if (screen) {
1610 screen->GetDpi(&dpi);
1612 return dpi;
1615 double nsWindow::GetDefaultScaleInternal() {
1616 if (mDefaultScale <= 0.0) {
1617 mDefaultScale = WinUtils::LogToPhysFactor(mWnd);
1619 return mDefaultScale;
1622 int32_t nsWindow::LogToPhys(double aValue) {
1623 return WinUtils::LogToPhys(
1624 ::MonitorFromWindow(mWnd, MONITOR_DEFAULTTOPRIMARY), aValue);
1627 nsWindow* nsWindow::GetParentWindow(bool aIncludeOwner) {
1628 return static_cast<nsWindow*>(GetParentWindowBase(aIncludeOwner));
1631 nsWindow* nsWindow::GetParentWindowBase(bool aIncludeOwner) {
1632 if (mIsTopWidgetWindow) {
1633 // Must use a flag instead of mWindowType to tell if the window is the
1634 // owned by the topmost widget, because a child window can be embedded
1635 // inside a HWND which is not associated with a nsIWidget.
1636 return nullptr;
1639 // If this widget has already been destroyed, pretend we have no parent.
1640 // This corresponds to code in Destroy which removes the destroyed
1641 // widget from its parent's child list.
1642 if (mInDtor || mOnDestroyCalled) return nullptr;
1644 // aIncludeOwner set to true implies walking the parent chain to retrieve the
1645 // root owner. aIncludeOwner set to false implies the search will stop at the
1646 // true parent (default).
1647 nsWindow* widget = nullptr;
1648 if (mWnd) {
1649 HWND parent = nullptr;
1650 if (aIncludeOwner)
1651 parent = ::GetParent(mWnd);
1652 else
1653 parent = ::GetAncestor(mWnd, GA_PARENT);
1655 if (parent) {
1656 widget = WinUtils::GetNSWindowPtr(parent);
1657 if (widget) {
1658 // If the widget is in the process of being destroyed then
1659 // do NOT return it
1660 if (widget->mInDtor) {
1661 widget = nullptr;
1667 return widget;
1670 /**************************************************************
1672 * SECTION: nsIWidget::Show
1674 * Hide or show this component.
1676 **************************************************************/
1678 void nsWindow::Show(bool bState) {
1679 if (bState && mIsShowingPreXULSkeletonUI) {
1680 // The first time we decide to actually show the window is when we decide
1681 // that we've taken over the window from the skeleton UI, and we should
1682 // no longer treat resizes / moves specially.
1683 mIsShowingPreXULSkeletonUI = false;
1684 #if defined(ACCESSIBILITY)
1685 // If our HWND has focus and the a11y engine hasn't started yet, fire a
1686 // focus win event. Windows already did this when the skeleton UI appeared,
1687 // but a11y wouldn't have been able to start at that point even if a client
1688 // responded. Firing this now gives clients the chance to respond with
1689 // WM_GETOBJECT, which will trigger the a11y engine. We don't want to do
1690 // this if the a11y engine has already started because it has probably
1691 // already fired focus on a descendant.
1692 if (::GetFocus() == mWnd && !GetAccService()) {
1693 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, mWnd, OBJID_CLIENT, CHILDID_SELF);
1695 #endif // defined(ACCESSIBILITY)
1698 if (mForMenupopupFrame) {
1699 MOZ_ASSERT(ChooseWindowClass(mWindowType, mForMenupopupFrame) ==
1700 kClassNameDropShadow);
1701 const bool shouldUseDropShadow = [&] {
1702 if (mTransparencyMode == TransparencyMode::Transparent) {
1703 return false;
1705 if (HasBogusPopupsDropShadowOnMultiMonitor() &&
1706 WinUtils::GetMonitorCount() > 1 &&
1707 !gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
1708 // See bug 603793. When we try to draw D3D9/10 windows with a drop
1709 // shadow without the DWM on a secondary monitor, windows fails to
1710 // composite our windows correctly. We therefor switch off the drop
1711 // shadow for pop-up windows when the DWM is disabled and two monitors
1712 // are connected.
1713 return false;
1715 return true;
1716 }();
1718 static bool sShadowEnabled = true;
1719 if (sShadowEnabled != shouldUseDropShadow) {
1720 ::SetClassLongA(mWnd, GCL_STYLE, shouldUseDropShadow ? CS_DROPSHADOW : 0);
1721 sShadowEnabled = shouldUseDropShadow;
1724 // WS_EX_COMPOSITED conflicts with the WS_EX_LAYERED style and causes
1725 // some popup menus to become invisible.
1726 LONG_PTR exStyle = ::GetWindowLongPtrW(mWnd, GWL_EXSTYLE);
1727 if (exStyle & WS_EX_LAYERED) {
1728 ::SetWindowLongPtrW(mWnd, GWL_EXSTYLE, exStyle & ~WS_EX_COMPOSITED);
1732 bool syncInvalidate = false;
1734 bool wasVisible = mIsVisible;
1735 // Set the status now so that anyone asking during ShowWindow or
1736 // SetWindowPos would get the correct answer.
1737 mIsVisible = bState;
1739 // We may have cached an out of date visible state. This can happen
1740 // when session restore sets the full screen mode.
1741 if (mIsVisible)
1742 mOldStyle |= WS_VISIBLE;
1743 else
1744 mOldStyle &= ~WS_VISIBLE;
1746 if (mWnd) {
1747 if (bState) {
1748 if (!wasVisible && mWindowType == WindowType::TopLevel) {
1749 // speed up the initial paint after show for
1750 // top level windows:
1751 syncInvalidate = true;
1753 // Set the cursor before showing the window to avoid the default wait
1754 // cursor.
1755 SetCursor(Cursor{eCursor_standard});
1757 switch (mFrameState->GetSizeMode()) {
1758 case nsSizeMode_Fullscreen:
1759 ::ShowWindow(mWnd, SW_SHOW);
1760 break;
1761 case nsSizeMode_Maximized:
1762 ::ShowWindow(mWnd, SW_SHOWMAXIMIZED);
1763 break;
1764 case nsSizeMode_Minimized:
1765 ::ShowWindow(mWnd, SW_SHOWMINIMIZED);
1766 break;
1767 default:
1768 if (CanTakeFocus() && !mAlwaysOnTop) {
1769 ::ShowWindow(mWnd, SW_SHOWNORMAL);
1770 } else {
1771 ::ShowWindow(mWnd, SW_SHOWNOACTIVATE);
1772 // Don't flicker the window if we're restoring session
1773 if (!sIsRestoringSession) {
1774 Unused << GetAttention(2);
1777 break;
1779 } else {
1780 DWORD flags = SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW;
1781 if (wasVisible) flags |= SWP_NOZORDER;
1782 if (mAlwaysOnTop) flags |= SWP_NOACTIVATE;
1784 if (mWindowType == WindowType::Popup) {
1785 // ensure popups are the topmost of the TOPMOST
1786 // layer. Remember not to set the SWP_NOZORDER
1787 // flag as that might allow the taskbar to overlap
1788 // the popup.
1789 flags |= SWP_NOACTIVATE;
1790 HWND owner = ::GetWindow(mWnd, GW_OWNER);
1791 if (owner) {
1792 // PopupLevel::Top popups should be above all else. All other
1793 // types should be placed in front of their owner, without
1794 // changing the owner's z-level relative to other windows.
1795 if (mPopupLevel != PopupLevel::Top) {
1796 ::SetWindowPos(mWnd, owner, 0, 0, 0, 0, flags);
1797 ::SetWindowPos(owner, mWnd, 0, 0, 0, 0,
1798 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
1799 } else {
1800 ::SetWindowPos(mWnd, HWND_TOP, 0, 0, 0, 0, flags);
1802 } else {
1803 ::SetWindowPos(mWnd, HWND_TOPMOST, 0, 0, 0, 0, flags);
1805 } else {
1806 if (mWindowType == WindowType::Dialog && !CanTakeFocus())
1807 flags |= SWP_NOACTIVATE;
1809 ::SetWindowPos(mWnd, HWND_TOP, 0, 0, 0, 0, flags);
1812 } else {
1813 // Clear contents to avoid ghosting of old content if we display
1814 // this window again.
1815 if (wasVisible && mTransparencyMode == TransparencyMode::Transparent) {
1816 if (mCompositorWidgetDelegate) {
1817 mCompositorWidgetDelegate->ClearTransparentWindow();
1820 if (mWindowType != WindowType::Dialog) {
1821 ::ShowWindow(mWnd, SW_HIDE);
1822 } else {
1823 ::SetWindowPos(mWnd, 0, 0, 0, 0, 0,
1824 SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER |
1825 SWP_NOACTIVATE);
1830 if (!wasVisible && bState) {
1831 Invalidate();
1832 if (syncInvalidate && !mInDtor && !mOnDestroyCalled) {
1833 ::UpdateWindow(mWnd);
1837 if (mOpeningAnimationSuppressed) {
1838 SuppressAnimation(false);
1842 /**************************************************************
1844 * SECTION: nsIWidget::IsVisible
1846 * Returns the visibility state.
1848 **************************************************************/
1850 // Return true if the component is visible, false otherwise.
1852 // This does not take cloaking into account.
1853 bool nsWindow::IsVisible() const { return mIsVisible; }
1855 /**************************************************************
1857 * SECTION: Window clipping utilities
1859 * Used in Size and Move operations for setting the proper
1860 * window clipping regions for window transparency.
1862 **************************************************************/
1864 // XP and Vista visual styles sometimes require window clipping regions to be
1865 // applied for proper transparency. These routines are called on size and move
1866 // operations.
1867 // XXX this is apparently still needed in Windows 7 and later
1868 void nsWindow::ClearThemeRegion() {
1869 if (!HasGlass() &&
1870 (mWindowType == WindowType::Popup && !IsPopupWithTitleBar() &&
1871 (mPopupType == PopupType::Tooltip || mPopupType == PopupType::Panel))) {
1872 SetWindowRgn(mWnd, nullptr, false);
1876 /**************************************************************
1878 * SECTION: Touch and APZ-related functions
1880 **************************************************************/
1882 void nsWindow::RegisterTouchWindow() {
1883 mTouchWindow = true;
1884 ::RegisterTouchWindow(mWnd, TWF_WANTPALM);
1885 ::EnumChildWindows(mWnd, nsWindow::RegisterTouchForDescendants, 0);
1888 BOOL CALLBACK nsWindow::RegisterTouchForDescendants(HWND aWnd, LPARAM aMsg) {
1889 nsWindow* win = WinUtils::GetNSWindowPtr(aWnd);
1890 if (win) {
1891 ::RegisterTouchWindow(aWnd, TWF_WANTPALM);
1893 return TRUE;
1896 void nsWindow::LockAspectRatio(bool aShouldLock) {
1897 if (aShouldLock) {
1898 mAspectRatio = (float)mBounds.Width() / (float)mBounds.Height();
1899 } else {
1900 mAspectRatio = 0.0;
1904 /**************************************************************
1906 * SECTION: nsIWidget::SetInputRegion
1908 * Sets whether the window should ignore mouse events.
1910 **************************************************************/
1911 void nsWindow::SetInputRegion(const InputRegion& aInputRegion) {
1912 mInputRegion = aInputRegion;
1914 if (!mWnd) {
1915 return;
1918 const bool transparent = aInputRegion.mFullyTransparent;
1919 LONG_PTR oldStyle = ::GetWindowLongPtrW(mWnd, GWL_EXSTYLE);
1920 LONG_PTR newStyle = transparent ? (oldStyle | WS_EX_TRANSPARENT)
1921 : (oldStyle & ~WS_EX_TRANSPARENT);
1922 ::SetWindowLongPtrW(mWnd, GWL_EXSTYLE, newStyle);
1925 /**************************************************************
1927 * SECTION: nsIWidget::Move, nsIWidget::Resize, nsIWidget::Size
1929 * Repositioning and sizing a window.
1931 **************************************************************/
1933 void nsWindow::SetSizeConstraints(const SizeConstraints& aConstraints) {
1934 SizeConstraints c = aConstraints;
1936 if (mWindowType != WindowType::Popup && mResizable) {
1937 c.mMinSize.width =
1938 std::max(int32_t(::GetSystemMetrics(SM_CXMINTRACK)), c.mMinSize.width);
1939 c.mMinSize.height =
1940 std::max(int32_t(::GetSystemMetrics(SM_CYMINTRACK)), c.mMinSize.height);
1943 if (mMaxTextureSize > 0) {
1944 // We can't make ThebesLayers bigger than this anyway.. no point it letting
1945 // a window grow bigger as we won't be able to draw content there in
1946 // general.
1947 c.mMaxSize.width = std::min(c.mMaxSize.width, mMaxTextureSize);
1948 c.mMaxSize.height = std::min(c.mMaxSize.height, mMaxTextureSize);
1951 mSizeConstraintsScale = GetDefaultScale().scale;
1953 nsBaseWidget::SetSizeConstraints(c);
1956 const SizeConstraints nsWindow::GetSizeConstraints() {
1957 double scale = GetDefaultScale().scale;
1958 if (mSizeConstraintsScale == scale || mSizeConstraintsScale == 0.0) {
1959 return mSizeConstraints;
1961 scale /= mSizeConstraintsScale;
1962 SizeConstraints c = mSizeConstraints;
1963 if (c.mMinSize.width != NS_MAXSIZE) {
1964 c.mMinSize.width = NSToIntRound(c.mMinSize.width * scale);
1966 if (c.mMinSize.height != NS_MAXSIZE) {
1967 c.mMinSize.height = NSToIntRound(c.mMinSize.height * scale);
1969 if (c.mMaxSize.width != NS_MAXSIZE) {
1970 c.mMaxSize.width = NSToIntRound(c.mMaxSize.width * scale);
1972 if (c.mMaxSize.height != NS_MAXSIZE) {
1973 c.mMaxSize.height = NSToIntRound(c.mMaxSize.height * scale);
1975 return c;
1978 // Move this component
1979 void nsWindow::Move(double aX, double aY) {
1980 if (mWindowType == WindowType::TopLevel ||
1981 mWindowType == WindowType::Dialog) {
1982 SetSizeMode(nsSizeMode_Normal);
1985 // for top-level windows only, convert coordinates from desktop pixels
1986 // (the "parent" coordinate space) to the window's device pixel space
1987 double scale =
1988 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale : 1.0;
1989 int32_t x = NSToIntRound(aX * scale);
1990 int32_t y = NSToIntRound(aY * scale);
1992 // Check to see if window needs to be moved first
1993 // to avoid a costly call to SetWindowPos. This check
1994 // can not be moved to the calling code in nsView, because
1995 // some platforms do not position child windows correctly
1997 // Only perform this check for non-popup windows, since the positioning can
1998 // in fact change even when the x/y do not. We always need to perform the
1999 // check. See bug #97805 for details.
2000 if (mWindowType != WindowType::Popup && mBounds.IsEqualXY(x, y)) {
2001 // Nothing to do, since it is already positioned correctly.
2002 return;
2005 mBounds.MoveTo(x, y);
2007 if (mWnd) {
2008 #ifdef DEBUG
2009 // complain if a window is moved offscreen (legal, but potentially
2010 // worrisome)
2011 if (mIsTopWidgetWindow) { // only a problem for top-level windows
2012 // Make sure this window is actually on the screen before we move it
2013 // XXX: Needs multiple monitor support
2014 HDC dc = ::GetDC(mWnd);
2015 if (dc) {
2016 if (::GetDeviceCaps(dc, TECHNOLOGY) == DT_RASDISPLAY) {
2017 RECT workArea;
2018 ::SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
2019 // no annoying assertions. just mention the issue.
2020 if (x < 0 || x >= workArea.right || y < 0 || y >= workArea.bottom) {
2021 MOZ_LOG(gWindowsLog, LogLevel::Info,
2022 ("window moved to offscreen position\n"));
2025 ::ReleaseDC(mWnd, dc);
2028 #endif
2030 // Normally, when the skeleton UI is disabled, we resize+move the window
2031 // before showing it in order to ensure that it restores to the correct
2032 // position when the user un-maximizes it. However, when we are using the
2033 // skeleton UI, this results in the skeleton UI window being moved around
2034 // undesirably before being locked back into the maximized position. To
2035 // avoid this, we simply set the placement to restore to via
2036 // SetWindowPlacement. It's a little bit more of a dance, though, since we
2037 // need to convert the workspace coords that SetWindowPlacement uses to the
2038 // screen space coordinates we normally use with SetWindowPos.
2039 if (mIsShowingPreXULSkeletonUI && WasPreXULSkeletonUIMaximized()) {
2040 WINDOWPLACEMENT pl = {sizeof(WINDOWPLACEMENT)};
2041 VERIFY(::GetWindowPlacement(mWnd, &pl));
2043 HMONITOR monitor = ::MonitorFromWindow(mWnd, MONITOR_DEFAULTTONULL);
2044 if (NS_WARN_IF(!monitor)) {
2045 return;
2047 MONITORINFO mi = {sizeof(MONITORINFO)};
2048 VERIFY(::GetMonitorInfo(monitor, &mi));
2050 int32_t deltaX =
2051 x + mi.rcWork.left - mi.rcMonitor.left - pl.rcNormalPosition.left;
2052 int32_t deltaY =
2053 y + mi.rcWork.top - mi.rcMonitor.top - pl.rcNormalPosition.top;
2054 pl.rcNormalPosition.left += deltaX;
2055 pl.rcNormalPosition.right += deltaX;
2056 pl.rcNormalPosition.top += deltaY;
2057 pl.rcNormalPosition.bottom += deltaY;
2058 VERIFY(::SetWindowPlacement(mWnd, &pl));
2059 } else {
2060 ClearThemeRegion();
2062 UINT flags = SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE;
2063 double oldScale = mDefaultScale;
2064 mResizeState = IN_SIZEMOVE;
2065 VERIFY(::SetWindowPos(mWnd, nullptr, x, y, 0, 0, flags));
2066 mResizeState = NOT_RESIZING;
2067 if (WinUtils::LogToPhysFactor(mWnd) != oldScale) {
2068 ChangedDPI();
2072 ResizeDirectManipulationViewport();
2076 // Resize this component
2077 void nsWindow::Resize(double aWidth, double aHeight, bool aRepaint) {
2078 // for top-level windows only, convert coordinates from desktop pixels
2079 // (the "parent" coordinate space) to the window's device pixel space
2080 double scale =
2081 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale : 1.0;
2082 int32_t width = NSToIntRound(aWidth * scale);
2083 int32_t height = NSToIntRound(aHeight * scale);
2085 NS_ASSERTION((width >= 0), "Negative width passed to nsWindow::Resize");
2086 NS_ASSERTION((height >= 0), "Negative height passed to nsWindow::Resize");
2087 if (width < 0 || height < 0) {
2088 gfxCriticalNoteOnce << "Negative passed to Resize(" << width << ", "
2089 << height << ") repaint: " << aRepaint;
2092 ConstrainSize(&width, &height);
2094 // Avoid unnecessary resizing calls
2095 if (mBounds.IsEqualSize(width, height)) {
2096 if (aRepaint) {
2097 Invalidate();
2099 return;
2102 // Set cached value for lightweight and printing
2103 bool wasLocking = mAspectRatio != 0.0;
2104 mBounds.SizeTo(width, height);
2105 if (wasLocking) {
2106 LockAspectRatio(true); // This causes us to refresh the mAspectRatio value
2109 if (mWnd) {
2110 // Refer to the comment above a similar check in nsWindow::Move
2111 if (mIsShowingPreXULSkeletonUI && WasPreXULSkeletonUIMaximized()) {
2112 WINDOWPLACEMENT pl = {sizeof(WINDOWPLACEMENT)};
2113 VERIFY(::GetWindowPlacement(mWnd, &pl));
2114 pl.rcNormalPosition.right = pl.rcNormalPosition.left + width;
2115 pl.rcNormalPosition.bottom = pl.rcNormalPosition.top + GetHeight(height);
2116 mResizeState = RESIZING;
2117 VERIFY(::SetWindowPlacement(mWnd, &pl));
2118 mResizeState = NOT_RESIZING;
2119 } else {
2120 UINT flags = SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE;
2122 if (!aRepaint) {
2123 flags |= SWP_NOREDRAW;
2126 ClearThemeRegion();
2127 double oldScale = mDefaultScale;
2128 mResizeState = RESIZING;
2129 VERIFY(
2130 ::SetWindowPos(mWnd, nullptr, 0, 0, width, GetHeight(height), flags));
2132 mResizeState = NOT_RESIZING;
2133 if (WinUtils::LogToPhysFactor(mWnd) != oldScale) {
2134 ChangedDPI();
2138 ResizeDirectManipulationViewport();
2141 if (aRepaint) Invalidate();
2144 // Resize this component
2145 void nsWindow::Resize(double aX, double aY, double aWidth, double aHeight,
2146 bool aRepaint) {
2147 // for top-level windows only, convert coordinates from desktop pixels
2148 // (the "parent" coordinate space) to the window's device pixel space
2149 double scale =
2150 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale : 1.0;
2151 int32_t x = NSToIntRound(aX * scale);
2152 int32_t y = NSToIntRound(aY * scale);
2153 int32_t width = NSToIntRound(aWidth * scale);
2154 int32_t height = NSToIntRound(aHeight * scale);
2156 NS_ASSERTION((width >= 0), "Negative width passed to nsWindow::Resize");
2157 NS_ASSERTION((height >= 0), "Negative height passed to nsWindow::Resize");
2158 if (width < 0 || height < 0) {
2159 gfxCriticalNoteOnce << "Negative passed to Resize(" << x << " ," << y
2160 << ", " << width << ", " << height
2161 << ") repaint: " << aRepaint;
2164 ConstrainSize(&width, &height);
2166 // Avoid unnecessary resizing calls
2167 if (mBounds.IsEqualRect(x, y, width, height)) {
2168 if (aRepaint) {
2169 Invalidate();
2171 return;
2174 // Set cached value for lightweight and printing
2175 mBounds.SetRect(x, y, width, height);
2177 if (mWnd) {
2178 // Refer to the comment above a similar check in nsWindow::Move
2179 if (mIsShowingPreXULSkeletonUI && WasPreXULSkeletonUIMaximized()) {
2180 WINDOWPLACEMENT pl = {sizeof(WINDOWPLACEMENT)};
2181 VERIFY(::GetWindowPlacement(mWnd, &pl));
2183 HMONITOR monitor = ::MonitorFromWindow(mWnd, MONITOR_DEFAULTTONULL);
2184 if (NS_WARN_IF(!monitor)) {
2185 return;
2187 MONITORINFO mi = {sizeof(MONITORINFO)};
2188 VERIFY(::GetMonitorInfo(monitor, &mi));
2190 int32_t deltaX =
2191 x + mi.rcWork.left - mi.rcMonitor.left - pl.rcNormalPosition.left;
2192 int32_t deltaY =
2193 y + mi.rcWork.top - mi.rcMonitor.top - pl.rcNormalPosition.top;
2194 pl.rcNormalPosition.left += deltaX;
2195 pl.rcNormalPosition.right = pl.rcNormalPosition.left + width;
2196 pl.rcNormalPosition.top += deltaY;
2197 pl.rcNormalPosition.bottom = pl.rcNormalPosition.top + GetHeight(height);
2198 VERIFY(::SetWindowPlacement(mWnd, &pl));
2199 } else {
2200 UINT flags = SWP_NOZORDER | SWP_NOACTIVATE;
2201 if (!aRepaint) {
2202 flags |= SWP_NOREDRAW;
2205 ClearThemeRegion();
2207 double oldScale = mDefaultScale;
2208 mResizeState = RESIZING;
2209 VERIFY(
2210 ::SetWindowPos(mWnd, nullptr, x, y, width, GetHeight(height), flags));
2211 mResizeState = NOT_RESIZING;
2212 if (WinUtils::LogToPhysFactor(mWnd) != oldScale) {
2213 ChangedDPI();
2216 if (mTransitionWnd) {
2217 // If we have a fullscreen transition window, we need to make
2218 // it topmost again, otherwise the taskbar may be raised by
2219 // the system unexpectedly when we leave fullscreen state.
2220 ::SetWindowPos(mTransitionWnd, HWND_TOPMOST, 0, 0, 0, 0,
2221 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
2225 ResizeDirectManipulationViewport();
2228 if (aRepaint) Invalidate();
2231 mozilla::Maybe<bool> nsWindow::IsResizingNativeWidget() {
2232 if (mResizeState == RESIZING) {
2233 return Some(true);
2235 return Some(false);
2238 /**************************************************************
2240 * SECTION: Window Z-order and state.
2242 * nsIWidget::PlaceBehind, nsIWidget::SetSizeMode,
2243 * nsIWidget::ConstrainPosition
2245 * Z-order, positioning, restore, minimize, and maximize.
2247 **************************************************************/
2249 // Position the window behind the given window
2250 void nsWindow::PlaceBehind(nsTopLevelWidgetZPlacement aPlacement,
2251 nsIWidget* aWidget, bool aActivate) {
2252 HWND behind = HWND_TOP;
2253 if (aPlacement == eZPlacementBottom)
2254 behind = HWND_BOTTOM;
2255 else if (aPlacement == eZPlacementBelow && aWidget)
2256 behind = (HWND)aWidget->GetNativeData(NS_NATIVE_WINDOW);
2257 UINT flags = SWP_NOMOVE | SWP_NOREPOSITION | SWP_NOSIZE;
2258 if (!aActivate) flags |= SWP_NOACTIVATE;
2260 if (!CanTakeFocus() && behind == HWND_TOP) {
2261 // Can't place the window to top so place it behind the foreground window
2262 // (as long as it is not topmost)
2263 HWND wndAfter = ::GetForegroundWindow();
2264 if (!wndAfter)
2265 behind = HWND_BOTTOM;
2266 else if (!(GetWindowLongPtrW(wndAfter, GWL_EXSTYLE) & WS_EX_TOPMOST))
2267 behind = wndAfter;
2268 flags |= SWP_NOACTIVATE;
2271 ::SetWindowPos(mWnd, behind, 0, 0, 0, 0, flags);
2274 static UINT GetCurrentShowCmd(HWND aWnd) {
2275 WINDOWPLACEMENT pl;
2276 pl.length = sizeof(pl);
2277 ::GetWindowPlacement(aWnd, &pl);
2278 return pl.showCmd;
2281 // Maximize, minimize or restore the window.
2282 void nsWindow::SetSizeMode(nsSizeMode aMode) {
2283 // If we are still displaying a maximized pre-XUL skeleton UI, ignore the
2284 // noise of sizemode changes. Once we have "shown" the window for the first
2285 // time (called nsWindow::Show(true), even though the window is already
2286 // technically displayed), we will again accept sizemode changes.
2287 if (mIsShowingPreXULSkeletonUI && WasPreXULSkeletonUIMaximized()) {
2288 return;
2291 mFrameState->EnsureSizeMode(aMode);
2294 nsSizeMode nsWindow::SizeMode() { return mFrameState->GetSizeMode(); }
2296 void DoGetWorkspaceID(HWND aWnd, nsAString* aWorkspaceID) {
2297 RefPtr<IVirtualDesktopManager> desktopManager = gVirtualDesktopManager;
2298 if (!desktopManager || !aWnd) {
2299 return;
2302 GUID desktop;
2303 HRESULT hr = desktopManager->GetWindowDesktopId(aWnd, &desktop);
2304 if (FAILED(hr)) {
2305 return;
2308 RPC_WSTR workspaceIDStr = nullptr;
2309 if (UuidToStringW(&desktop, &workspaceIDStr) == RPC_S_OK) {
2310 aWorkspaceID->Assign((wchar_t*)workspaceIDStr);
2311 RpcStringFreeW(&workspaceIDStr);
2315 void nsWindow::GetWorkspaceID(nsAString& workspaceID) {
2316 // If we have a value cached, use that, but also make sure it is
2317 // scheduled to be updated. If we don't yet have a value, get
2318 // one synchronously.
2319 auto desktop = mDesktopId.Lock();
2320 if (desktop->mID.IsEmpty()) {
2321 DoGetWorkspaceID(mWnd, &desktop->mID);
2322 desktop->mUpdateIsQueued = false;
2323 } else {
2324 AsyncUpdateWorkspaceID(*desktop);
2327 workspaceID = desktop->mID;
2330 void nsWindow::AsyncUpdateWorkspaceID(Desktop& aDesktop) {
2331 struct UpdateWorkspaceIdTask : public Task {
2332 explicit UpdateWorkspaceIdTask(nsWindow* aSelf)
2333 : Task(false /* mainThread */, EventQueuePriority::Normal),
2334 mSelf(aSelf) {}
2336 bool Run() override {
2337 auto desktop = mSelf->mDesktopId.Lock();
2338 if (desktop->mUpdateIsQueued) {
2339 DoGetWorkspaceID(mSelf->mWnd, &desktop->mID);
2340 desktop->mUpdateIsQueued = false;
2342 return true;
2345 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
2346 bool GetName(nsACString& aName) override {
2347 aName.AssignLiteral("UpdateWorkspaceIdTask");
2348 return true;
2350 #endif
2352 RefPtr<nsWindow> mSelf;
2355 if (aDesktop.mUpdateIsQueued) {
2356 return;
2359 aDesktop.mUpdateIsQueued = true;
2360 TaskController::Get()->AddTask(MakeAndAddRef<UpdateWorkspaceIdTask>(this));
2363 void nsWindow::MoveToWorkspace(const nsAString& workspaceID) {
2364 RefPtr<IVirtualDesktopManager> desktopManager = gVirtualDesktopManager;
2365 if (!desktopManager) {
2366 return;
2369 GUID desktop;
2370 const nsString flat = PromiseFlatString(workspaceID);
2371 RPC_WSTR workspaceIDStr = reinterpret_cast<RPC_WSTR>((wchar_t*)flat.get());
2372 if (UuidFromStringW(workspaceIDStr, &desktop) == RPC_S_OK) {
2373 if (SUCCEEDED(desktopManager->MoveWindowToDesktop(mWnd, desktop))) {
2374 auto desktop = mDesktopId.Lock();
2375 desktop->mID = workspaceID;
2380 void nsWindow::SuppressAnimation(bool aSuppress) {
2381 DWORD dwAttribute = aSuppress ? TRUE : FALSE;
2382 DwmSetWindowAttribute(mWnd, DWMWA_TRANSITIONS_FORCEDISABLED, &dwAttribute,
2383 sizeof dwAttribute);
2386 // Constrain a potential move to fit onscreen
2387 // Position (aX, aY) is specified in Windows screen (logical) pixels,
2388 // except when using per-monitor DPI, in which case it's device pixels.
2389 void nsWindow::ConstrainPosition(bool aAllowSlop, int32_t* aX, int32_t* aY) {
2390 if (!mIsTopWidgetWindow) // only a problem for top-level windows
2391 return;
2393 double dpiScale = GetDesktopToDeviceScale().scale;
2395 // We need to use the window size in the kind of pixels used for window-
2396 // manipulation APIs.
2397 int32_t logWidth =
2398 std::max<int32_t>(NSToIntRound(mBounds.Width() / dpiScale), 1);
2399 int32_t logHeight =
2400 std::max<int32_t>(NSToIntRound(mBounds.Height() / dpiScale), 1);
2402 /* get our playing field. use the current screen, or failing that
2403 for any reason, use device caps for the default screen. */
2404 RECT screenRect;
2406 nsCOMPtr<nsIScreenManager> screenmgr =
2407 do_GetService(sScreenManagerContractID);
2408 if (!screenmgr) {
2409 return;
2411 nsCOMPtr<nsIScreen> screen;
2412 int32_t left, top, width, height;
2414 screenmgr->ScreenForRect(*aX, *aY, logWidth, logHeight,
2415 getter_AddRefs(screen));
2416 if (mFrameState->GetSizeMode() != nsSizeMode_Fullscreen) {
2417 // For normalized windows, use the desktop work area.
2418 nsresult rv = screen->GetAvailRectDisplayPix(&left, &top, &width, &height);
2419 if (NS_FAILED(rv)) {
2420 return;
2422 } else {
2423 // For full screen windows, use the desktop.
2424 nsresult rv = screen->GetRectDisplayPix(&left, &top, &width, &height);
2425 if (NS_FAILED(rv)) {
2426 return;
2429 screenRect.left = left;
2430 screenRect.right = left + width;
2431 screenRect.top = top;
2432 screenRect.bottom = top + height;
2434 if (aAllowSlop) {
2435 if (*aX < screenRect.left - logWidth + kWindowPositionSlop)
2436 *aX = screenRect.left - logWidth + kWindowPositionSlop;
2437 else if (*aX >= screenRect.right - kWindowPositionSlop)
2438 *aX = screenRect.right - kWindowPositionSlop;
2440 if (*aY < screenRect.top - logHeight + kWindowPositionSlop)
2441 *aY = screenRect.top - logHeight + kWindowPositionSlop;
2442 else if (*aY >= screenRect.bottom - kWindowPositionSlop)
2443 *aY = screenRect.bottom - kWindowPositionSlop;
2445 } else {
2446 if (*aX < screenRect.left)
2447 *aX = screenRect.left;
2448 else if (*aX >= screenRect.right - logWidth)
2449 *aX = screenRect.right - logWidth;
2451 if (*aY < screenRect.top)
2452 *aY = screenRect.top;
2453 else if (*aY >= screenRect.bottom - logHeight)
2454 *aY = screenRect.bottom - logHeight;
2458 /**************************************************************
2460 * SECTION: nsIWidget::Enable, nsIWidget::IsEnabled
2462 * Enabling and disabling the widget.
2464 **************************************************************/
2466 // Enable/disable this component
2467 void nsWindow::Enable(bool bState) {
2468 if (mWnd) {
2469 ::EnableWindow(mWnd, bState);
2473 // Return the current enable state
2474 bool nsWindow::IsEnabled() const {
2475 return !mWnd || (::IsWindowEnabled(mWnd) &&
2476 ::IsWindowEnabled(::GetAncestor(mWnd, GA_ROOT)));
2479 /**************************************************************
2481 * SECTION: nsIWidget::SetFocus
2483 * Give the focus to this widget.
2485 **************************************************************/
2487 void nsWindow::SetFocus(Raise aRaise, mozilla::dom::CallerType aCallerType) {
2488 if (mWnd) {
2489 #ifdef WINSTATE_DEBUG_OUTPUT
2490 if (mWnd == WinUtils::GetTopLevelHWND(mWnd)) {
2491 MOZ_LOG(gWindowsLog, LogLevel::Info,
2492 ("*** SetFocus: [ top] raise=%d\n", aRaise == Raise::Yes));
2493 } else {
2494 MOZ_LOG(gWindowsLog, LogLevel::Info,
2495 ("*** SetFocus: [child] raise=%d\n", aRaise == Raise::Yes));
2497 #endif
2498 // Uniconify, if necessary
2499 HWND toplevelWnd = WinUtils::GetTopLevelHWND(mWnd);
2500 if (aRaise == Raise::Yes && ::IsIconic(toplevelWnd)) {
2501 ::ShowWindow(toplevelWnd, SW_RESTORE);
2503 ::SetFocus(mWnd);
2507 /**************************************************************
2509 * SECTION: Bounds
2511 * GetBounds, GetClientBounds, GetScreenBounds,
2512 * GetRestoredBounds, GetClientOffset
2513 * SetDrawsInTitlebar, SetNonClientMargins
2515 * Bound calculations.
2517 **************************************************************/
2519 // Return the window's full dimensions in screen coordinates.
2520 // If the window has a parent, converts the origin to an offset
2521 // of the parent's screen origin.
2522 LayoutDeviceIntRect nsWindow::GetBounds() {
2523 if (!mWnd) {
2524 return mBounds;
2527 RECT r;
2528 VERIFY(::GetWindowRect(mWnd, &r));
2530 LayoutDeviceIntRect rect;
2532 // assign size
2533 rect.SizeTo(r.right - r.left, r.bottom - r.top);
2535 // popup window bounds' are in screen coordinates, not relative to parent
2536 // window
2537 if (mWindowType == WindowType::Popup) {
2538 rect.MoveTo(r.left, r.top);
2539 return rect;
2542 // chrome on parent:
2543 // ___ 5,5 (chrome start)
2544 // | ____ 10,10 (client start)
2545 // | | ____ 20,20 (child start)
2546 // | | |
2547 // 20,20 - 5,5 = 15,15 (??)
2548 // minus GetClientOffset:
2549 // 15,15 - 5,5 = 10,10
2551 // no chrome on parent:
2552 // ______ 10,10 (win start)
2553 // | ____ 20,20 (child start)
2554 // | |
2555 // 20,20 - 10,10 = 10,10
2557 // walking the chain:
2558 // ___ 5,5 (chrome start)
2559 // | ___ 10,10 (client start)
2560 // | | ___ 20,20 (child start)
2561 // | | | __ 30,30 (child start)
2562 // | | | |
2563 // 30,30 - 20,20 = 10,10 (offset from second child to first)
2564 // 20,20 - 5,5 = 15,15 + 10,10 = 25,25 (??)
2565 // minus GetClientOffset:
2566 // 25,25 - 5,5 = 20,20 (offset from second child to parent client)
2568 // convert coordinates if parent exists
2569 HWND parent = ::GetParent(mWnd);
2570 if (parent) {
2571 RECT pr;
2572 VERIFY(::GetWindowRect(parent, &pr));
2573 r.left -= pr.left;
2574 r.top -= pr.top;
2575 // adjust for chrome
2576 nsWindow* pWidget = static_cast<nsWindow*>(GetParent());
2577 if (pWidget && pWidget->IsTopLevelWidget()) {
2578 LayoutDeviceIntPoint clientOffset = pWidget->GetClientOffset();
2579 r.left -= clientOffset.x;
2580 r.top -= clientOffset.y;
2583 rect.MoveTo(r.left, r.top);
2584 if (mCompositorSession &&
2585 !wr::WindowSizeSanityCheck(rect.width, rect.height)) {
2586 gfxCriticalNoteOnce << "Invalid size" << rect << " size mode "
2587 << mFrameState->GetSizeMode();
2590 return rect;
2593 // Get this component dimension
2594 LayoutDeviceIntRect nsWindow::GetClientBounds() {
2595 if (!mWnd) {
2596 return LayoutDeviceIntRect(0, 0, 0, 0);
2599 RECT r;
2600 if (!::GetClientRect(mWnd, &r)) {
2601 MOZ_ASSERT_UNREACHABLE("unexpected to be called");
2602 gfxCriticalNoteOnce << "GetClientRect failed " << ::GetLastError();
2603 return mBounds;
2606 LayoutDeviceIntRect bounds = GetBounds();
2607 LayoutDeviceIntRect rect;
2608 rect.MoveTo(bounds.TopLeft() + GetClientOffset());
2609 rect.SizeTo(r.right - r.left, r.bottom - r.top);
2610 return rect;
2613 // Like GetBounds, but don't offset by the parent
2614 LayoutDeviceIntRect nsWindow::GetScreenBounds() {
2615 if (!mWnd) {
2616 return mBounds;
2619 RECT r;
2620 VERIFY(::GetWindowRect(mWnd, &r));
2622 return LayoutDeviceIntRect(r.left, r.top, r.right - r.left, r.bottom - r.top);
2625 nsresult nsWindow::GetRestoredBounds(LayoutDeviceIntRect& aRect) {
2626 if (SizeMode() == nsSizeMode_Normal) {
2627 aRect = GetScreenBounds();
2628 return NS_OK;
2630 if (!mWnd) {
2631 return NS_ERROR_FAILURE;
2634 WINDOWPLACEMENT pl = {sizeof(WINDOWPLACEMENT)};
2635 VERIFY(::GetWindowPlacement(mWnd, &pl));
2636 const RECT& r = pl.rcNormalPosition;
2638 HMONITOR monitor = ::MonitorFromWindow(mWnd, MONITOR_DEFAULTTONULL);
2639 if (!monitor) {
2640 return NS_ERROR_FAILURE;
2642 MONITORINFO mi = {sizeof(MONITORINFO)};
2643 VERIFY(::GetMonitorInfo(monitor, &mi));
2645 aRect.SetRect(r.left, r.top, r.right - r.left, r.bottom - r.top);
2646 aRect.MoveBy(mi.rcWork.left - mi.rcMonitor.left,
2647 mi.rcWork.top - mi.rcMonitor.top);
2648 return NS_OK;
2651 // Return the x,y offset of the client area from the origin of the window. If
2652 // the window is borderless returns (0,0).
2653 LayoutDeviceIntPoint nsWindow::GetClientOffset() {
2654 if (!mWnd) {
2655 return LayoutDeviceIntPoint(0, 0);
2658 RECT r1;
2659 GetWindowRect(mWnd, &r1);
2660 LayoutDeviceIntPoint pt = WidgetToScreenOffset();
2661 return LayoutDeviceIntPoint(pt.x - LayoutDeviceIntCoord(r1.left),
2662 pt.y - LayoutDeviceIntCoord(r1.top));
2665 void nsWindow::SetDrawsInTitlebar(bool aState) {
2666 nsWindow* window = GetTopLevelWindow(true);
2667 if (window && window != this) {
2668 return window->SetDrawsInTitlebar(aState);
2671 // top, right, bottom, left
2672 SetNonClientMargins(aState ? LayoutDeviceIntMargin(0, -1, -1, -1)
2673 : LayoutDeviceIntMargin(-1, -1, -1, -1));
2676 void nsWindow::ResetLayout() {
2677 // This will trigger a frame changed event, triggering
2678 // nc calc size and a sizemode gecko event.
2679 SetWindowPos(mWnd, 0, 0, 0, 0, 0,
2680 SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE |
2681 SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
2683 // If hidden, just send the frame changed event for now.
2684 if (!mIsVisible) {
2685 return;
2688 // Send a gecko size event to trigger reflow.
2689 RECT clientRc = {0};
2690 GetClientRect(mWnd, &clientRc);
2691 OnResize(WinUtils::ToIntRect(clientRc).Size());
2693 // Invalidate and update
2694 Invalidate();
2697 // Internally track the caption status via a window property. Required
2698 // due to our internal handling of WM_NCACTIVATE when custom client
2699 // margins are set.
2700 static const wchar_t kManageWindowInfoProperty[] = L"ManageWindowInfoProperty";
2701 typedef BOOL(WINAPI* GetWindowInfoPtr)(HWND hwnd, PWINDOWINFO pwi);
2702 static WindowsDllInterceptor::FuncHookType<GetWindowInfoPtr>
2703 sGetWindowInfoPtrStub;
2705 BOOL WINAPI GetWindowInfoHook(HWND hWnd, PWINDOWINFO pwi) {
2706 if (!sGetWindowInfoPtrStub) {
2707 NS_ASSERTION(FALSE, "Something is horribly wrong in GetWindowInfoHook!");
2708 return FALSE;
2710 int windowStatus =
2711 reinterpret_cast<LONG_PTR>(GetPropW(hWnd, kManageWindowInfoProperty));
2712 // No property set, return the default data.
2713 if (!windowStatus) return sGetWindowInfoPtrStub(hWnd, pwi);
2714 // Call GetWindowInfo and update dwWindowStatus with our
2715 // internally tracked value.
2716 BOOL result = sGetWindowInfoPtrStub(hWnd, pwi);
2717 if (result && pwi)
2718 pwi->dwWindowStatus = (windowStatus == 1 ? 0 : WS_ACTIVECAPTION);
2719 return result;
2722 void nsWindow::UpdateGetWindowInfoCaptionStatus(bool aActiveCaption) {
2723 if (!mWnd) return;
2725 sUser32Intercept.Init("user32.dll");
2726 sGetWindowInfoPtrStub.Set(sUser32Intercept, "GetWindowInfo",
2727 &GetWindowInfoHook);
2728 if (!sGetWindowInfoPtrStub) {
2729 return;
2732 // Update our internally tracked caption status
2733 SetPropW(mWnd, kManageWindowInfoProperty,
2734 reinterpret_cast<HANDLE>(static_cast<INT_PTR>(aActiveCaption) + 1));
2737 #define DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 19
2738 #define DWMWA_USE_IMMERSIVE_DARK_MODE 20
2740 void nsWindow::UpdateDarkModeToolbar() {
2741 if (!IsWin10OrLater()) {
2742 return;
2744 LookAndFeel::EnsureColorSchemesInitialized();
2745 BOOL dark = LookAndFeel::ColorSchemeForChrome() == ColorScheme::Dark;
2746 DwmSetWindowAttribute(mWnd, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &dark,
2747 sizeof dark);
2748 DwmSetWindowAttribute(mWnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &dark,
2749 sizeof dark);
2752 LayoutDeviceIntMargin nsWindow::NormalWindowNonClientOffset() const {
2753 bool glass = gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled();
2755 LayoutDeviceIntMargin nonClientOffset;
2757 // We're dealing with a "normal" window (not maximized, minimized, or
2758 // fullscreen), so process `mNonClientMargins` and set `mNonClientOffset`
2759 // accordingly.
2761 // Setting `mNonClientOffset` to 0 has the effect of leaving the default
2762 // frame intact. Setting it to a value greater than 0 reduces the frame
2763 // size by that amount.
2765 if (mNonClientMargins.top > 0 && glass) {
2766 nonClientOffset.top = std::min(mCaptionHeight, mNonClientMargins.top);
2767 } else if (mNonClientMargins.top == 0) {
2768 nonClientOffset.top = mCaptionHeight;
2769 } else {
2770 nonClientOffset.top = 0;
2773 if (mNonClientMargins.bottom > 0 && glass) {
2774 nonClientOffset.bottom =
2775 std::min(mVertResizeMargin, mNonClientMargins.bottom);
2776 } else if (mNonClientMargins.bottom == 0) {
2777 nonClientOffset.bottom = mVertResizeMargin;
2778 } else {
2779 nonClientOffset.bottom = 0;
2782 if (mNonClientMargins.left > 0 && glass) {
2783 nonClientOffset.left = std::min(mHorResizeMargin, mNonClientMargins.left);
2784 } else if (mNonClientMargins.left == 0) {
2785 nonClientOffset.left = mHorResizeMargin;
2786 } else {
2787 nonClientOffset.left = 0;
2790 if (mNonClientMargins.right > 0 && glass) {
2791 nonClientOffset.right = std::min(mHorResizeMargin, mNonClientMargins.right);
2792 } else if (mNonClientMargins.right == 0) {
2793 nonClientOffset.right = mHorResizeMargin;
2794 } else {
2795 nonClientOffset.right = 0;
2797 return nonClientOffset;
2801 * Called when the window layout changes: full screen mode transitions,
2802 * theme changes, and composition changes. Calculates the new non-client
2803 * margins and fires off a frame changed event, which triggers an nc calc
2804 * size windows event, kicking the changes in.
2806 * The offsets calculated here are based on the value of `mNonClientMargins`
2807 * which is specified in the "chromemargins" attribute of the window. For
2808 * each margin, the value specified has the following meaning:
2809 * -1 - leave the default frame in place
2810 * 0 - remove the frame
2811 * >0 - frame size equals min(0, (default frame size - margin value))
2813 * This function calculates and populates `mNonClientOffset`.
2814 * In our processing of `WM_NCCALCSIZE`, the frame size will be calculated
2815 * as (default frame size - offset). For example, if the left frame should
2816 * be 1 pixel narrower than the default frame size, `mNonClientOffset.left`
2817 * will equal 1.
2819 * For maximized, fullscreen, and minimized windows, the values stored in
2820 * `mNonClientMargins` are ignored, and special processing takes place.
2822 * For non-glass windows, we only allow frames to be their default size
2823 * or removed entirely.
2825 bool nsWindow::UpdateNonClientMargins(bool aReflowWindow) {
2826 if (!mCustomNonClient) {
2827 return false;
2830 const nsSizeMode sizeMode = mFrameState->GetSizeMode();
2832 bool hasCaption =
2833 bool(mBorderStyle & (BorderStyle::All | BorderStyle::Title |
2834 BorderStyle::Menu | BorderStyle::Default));
2836 float dpi = GetDPI();
2838 // mCaptionHeight is the default size of the NC area at
2839 // the top of the window. If the window has a caption,
2840 // the size is calculated as the sum of:
2841 // SM_CYFRAME - The thickness of the sizing border
2842 // around a resizable window
2843 // SM_CXPADDEDBORDER - The amount of border padding
2844 // for captioned windows
2845 // SM_CYCAPTION - The height of the caption area
2847 // If the window does not have a caption, mCaptionHeight will be equal to
2848 // `WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi)`
2849 mCaptionHeight =
2850 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi) +
2851 (hasCaption ? WinUtils::GetSystemMetricsForDpi(SM_CYCAPTION, dpi) +
2852 WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)
2853 : 0);
2854 if (!mUseResizeMarginOverrides) {
2855 // mHorResizeMargin is the size of the default NC areas on the
2856 // left and right sides of our window. It is calculated as
2857 // the sum of:
2858 // SM_CXFRAME - The thickness of the sizing border
2859 // SM_CXPADDEDBORDER - The amount of border padding
2860 // for captioned windows
2862 // If the window does not have a caption, mHorResizeMargin will be equal to
2863 // `WinUtils::GetSystemMetricsForDpi(SM_CXFRAME, dpi)`
2864 mHorResizeMargin =
2865 WinUtils::GetSystemMetricsForDpi(SM_CXFRAME, dpi) +
2866 (hasCaption ? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)
2867 : 0);
2869 // mVertResizeMargin is the size of the default NC area at the
2870 // bottom of the window. It is calculated as the sum of:
2871 // SM_CYFRAME - The thickness of the sizing border
2872 // SM_CXPADDEDBORDER - The amount of border padding
2873 // for captioned windows.
2875 // If the window does not have a caption, mVertResizeMargin will be equal to
2876 // `WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi)`
2877 mVertResizeMargin =
2878 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi) +
2879 (hasCaption ? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)
2880 : 0);
2883 if (sizeMode == nsSizeMode_Minimized) {
2884 // Use default frame size for minimized windows
2885 mNonClientOffset.top = 0;
2886 mNonClientOffset.left = 0;
2887 mNonClientOffset.right = 0;
2888 mNonClientOffset.bottom = 0;
2889 } else if (sizeMode == nsSizeMode_Fullscreen) {
2890 // Remove the default frame from the top of our fullscreen window. This
2891 // makes the whole caption part of our client area, allowing us to draw
2892 // in the whole caption area. Additionally remove the default frame from
2893 // the left, right, and bottom.
2894 mNonClientOffset.top = mCaptionHeight;
2895 mNonClientOffset.bottom = mVertResizeMargin;
2896 mNonClientOffset.left = mHorResizeMargin;
2897 mNonClientOffset.right = mHorResizeMargin;
2898 } else if (sizeMode == nsSizeMode_Maximized) {
2899 // On Windows 10+, we make the entire frame part of the client area.
2900 // We leave the default frame sizes for left, right and bottom since
2901 // Windows will automagically position the edges "offscreen" for maximized
2902 // windows.
2903 // On versions prior to Windows 10, we add padding to the widget to
2904 // circumvent a bug in DwmDefWindowProc (see
2905 // nsNativeThemeWin::GetWidgetPadding). We "undo" that padding in
2906 // WM_NCCALCSIZE by adding the caption (as well as the sizing frame) to the
2907 // client area.
2908 // The padding is not needed on Win10+ because we handle window buttons
2909 // non-natively in the theme. It also does not work on Win10+ -- it
2910 // exposes a new issue where widget edges would sometimes appear to bleed
2911 // into other displays (bug 1614218).
2912 int verticalResize = 0;
2913 if (IsWin10OrLater()) {
2914 verticalResize =
2915 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi) +
2916 (hasCaption ? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)
2917 : 0);
2920 mNonClientOffset.top = mCaptionHeight - verticalResize;
2921 mNonClientOffset.bottom = 0;
2922 mNonClientOffset.left = 0;
2923 mNonClientOffset.right = 0;
2925 APPBARDATA appBarData;
2926 appBarData.cbSize = sizeof(appBarData);
2927 UINT taskbarState = SHAppBarMessage(ABM_GETSTATE, &appBarData);
2928 if (ABS_AUTOHIDE & taskbarState) {
2929 UINT edge = -1;
2930 appBarData.hWnd = FindWindow(L"Shell_TrayWnd", nullptr);
2931 if (appBarData.hWnd) {
2932 HMONITOR taskbarMonitor =
2933 ::MonitorFromWindow(appBarData.hWnd, MONITOR_DEFAULTTOPRIMARY);
2934 HMONITOR windowMonitor =
2935 ::MonitorFromWindow(mWnd, MONITOR_DEFAULTTONEAREST);
2936 if (taskbarMonitor == windowMonitor) {
2937 SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData);
2938 edge = appBarData.uEdge;
2942 if (ABE_LEFT == edge) {
2943 mNonClientOffset.left -= 1;
2944 } else if (ABE_RIGHT == edge) {
2945 mNonClientOffset.right -= 1;
2946 } else if (ABE_BOTTOM == edge || ABE_TOP == edge) {
2947 mNonClientOffset.bottom -= 1;
2950 } else {
2951 mNonClientOffset = NormalWindowNonClientOffset();
2954 if (aReflowWindow) {
2955 // Force a reflow of content based on the new client
2956 // dimensions.
2957 ResetLayout();
2960 return true;
2963 nsresult nsWindow::SetNonClientMargins(const LayoutDeviceIntMargin& margins) {
2964 if (!mIsTopWidgetWindow || mBorderStyle == BorderStyle::None)
2965 return NS_ERROR_INVALID_ARG;
2967 if (mHideChrome) {
2968 mFutureMarginsOnceChromeShows = margins;
2969 mFutureMarginsToUse = true;
2970 return NS_OK;
2972 mFutureMarginsToUse = false;
2974 // Request for a reset
2975 if (margins.top == -1 && margins.left == -1 && margins.right == -1 &&
2976 margins.bottom == -1) {
2977 mCustomNonClient = false;
2978 mNonClientMargins = margins;
2979 // Force a reflow of content based on the new client
2980 // dimensions.
2981 ResetLayout();
2983 int windowStatus =
2984 reinterpret_cast<LONG_PTR>(GetPropW(mWnd, kManageWindowInfoProperty));
2985 if (windowStatus) {
2986 ::SendMessageW(mWnd, WM_NCACTIVATE, 1 != windowStatus, 0);
2989 return NS_OK;
2992 if (margins.top < -1 || margins.bottom < -1 || margins.left < -1 ||
2993 margins.right < -1)
2994 return NS_ERROR_INVALID_ARG;
2996 mNonClientMargins = margins;
2997 mCustomNonClient = true;
2998 if (!UpdateNonClientMargins()) {
2999 NS_WARNING("UpdateNonClientMargins failed!");
3000 return NS_OK;
3003 return NS_OK;
3006 void nsWindow::SetResizeMargin(mozilla::LayoutDeviceIntCoord aResizeMargin) {
3007 mUseResizeMarginOverrides = true;
3008 mHorResizeMargin = aResizeMargin;
3009 mVertResizeMargin = aResizeMargin;
3010 UpdateNonClientMargins();
3013 void nsWindow::InvalidateNonClientRegion() {
3014 // +-+-----------------------+-+
3015 // | | app non-client chrome | |
3016 // | +-----------------------+ |
3017 // | | app client chrome | | }
3018 // | +-----------------------+ | }
3019 // | | app content | | } area we don't want to invalidate
3020 // | +-----------------------+ | }
3021 // | | app client chrome | | }
3022 // | +-----------------------+ |
3023 // +---------------------------+ <
3024 // ^ ^ windows non-client chrome
3025 // client area = app *
3026 RECT rect;
3027 GetWindowRect(mWnd, &rect);
3028 MapWindowPoints(nullptr, mWnd, (LPPOINT)&rect, 2);
3029 HRGN winRgn = CreateRectRgnIndirect(&rect);
3031 // Subtract app client chrome and app content leaving
3032 // windows non-client chrome and app non-client chrome
3033 // in winRgn.
3034 GetWindowRect(mWnd, &rect);
3035 rect.top += mCaptionHeight;
3036 rect.right -= mHorResizeMargin;
3037 rect.bottom -= mVertResizeMargin;
3038 rect.left += mHorResizeMargin;
3039 MapWindowPoints(nullptr, mWnd, (LPPOINT)&rect, 2);
3040 HRGN clientRgn = CreateRectRgnIndirect(&rect);
3041 CombineRgn(winRgn, winRgn, clientRgn, RGN_DIFF);
3042 DeleteObject(clientRgn);
3044 // triggers ncpaint and paint events for the two areas
3045 RedrawWindow(mWnd, nullptr, winRgn, RDW_FRAME | RDW_INVALIDATE);
3046 DeleteObject(winRgn);
3049 HRGN nsWindow::ExcludeNonClientFromPaintRegion(HRGN aRegion) {
3050 RECT rect;
3051 HRGN rgn = nullptr;
3052 if (aRegion == (HRGN)1) { // undocumented value indicating a full refresh
3053 GetWindowRect(mWnd, &rect);
3054 rgn = CreateRectRgnIndirect(&rect);
3055 } else {
3056 rgn = aRegion;
3058 GetClientRect(mWnd, &rect);
3059 MapWindowPoints(mWnd, nullptr, (LPPOINT)&rect, 2);
3060 HRGN nonClientRgn = CreateRectRgnIndirect(&rect);
3061 CombineRgn(rgn, rgn, nonClientRgn, RGN_DIFF);
3062 DeleteObject(nonClientRgn);
3063 return rgn;
3066 /**************************************************************
3068 * SECTION: nsIWidget::SetBackgroundColor
3070 * Sets the window background paint color.
3072 **************************************************************/
3074 void nsWindow::SetBackgroundColor(const nscolor& aColor) {
3075 if (mBrush) ::DeleteObject(mBrush);
3077 mBrush = ::CreateSolidBrush(NSRGB_2_COLOREF(aColor));
3078 if (mWnd != nullptr) {
3079 ::SetClassLongPtrW(mWnd, GCLP_HBRBACKGROUND, (LONG_PTR)mBrush);
3083 /**************************************************************
3085 * SECTION: nsIWidget::SetCursor
3087 * SetCursor and related utilities for manging cursor state.
3089 **************************************************************/
3091 // Set this component cursor
3092 static HCURSOR CursorFor(nsCursor aCursor) {
3093 switch (aCursor) {
3094 case eCursor_select:
3095 return ::LoadCursor(nullptr, IDC_IBEAM);
3096 case eCursor_wait:
3097 return ::LoadCursor(nullptr, IDC_WAIT);
3098 case eCursor_hyperlink:
3099 return ::LoadCursor(nullptr, IDC_HAND);
3100 case eCursor_standard:
3101 case eCursor_context_menu: // XXX See bug 258960.
3102 return ::LoadCursor(nullptr, IDC_ARROW);
3104 case eCursor_n_resize:
3105 case eCursor_s_resize:
3106 return ::LoadCursor(nullptr, IDC_SIZENS);
3108 case eCursor_w_resize:
3109 case eCursor_e_resize:
3110 return ::LoadCursor(nullptr, IDC_SIZEWE);
3112 case eCursor_nw_resize:
3113 case eCursor_se_resize:
3114 return ::LoadCursor(nullptr, IDC_SIZENWSE);
3116 case eCursor_ne_resize:
3117 case eCursor_sw_resize:
3118 return ::LoadCursor(nullptr, IDC_SIZENESW);
3120 case eCursor_crosshair:
3121 return ::LoadCursor(nullptr, IDC_CROSS);
3123 case eCursor_move:
3124 return ::LoadCursor(nullptr, IDC_SIZEALL);
3126 case eCursor_help:
3127 return ::LoadCursor(nullptr, IDC_HELP);
3129 case eCursor_copy: // CSS3
3130 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_COPY));
3132 case eCursor_alias:
3133 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_ALIAS));
3135 case eCursor_cell:
3136 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_CELL));
3137 case eCursor_grab:
3138 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_GRAB));
3140 case eCursor_grabbing:
3141 return ::LoadCursor(nsToolkit::mDllInstance,
3142 MAKEINTRESOURCE(IDC_GRABBING));
3144 case eCursor_spinning:
3145 return ::LoadCursor(nullptr, IDC_APPSTARTING);
3147 case eCursor_zoom_in:
3148 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_ZOOMIN));
3150 case eCursor_zoom_out:
3151 return ::LoadCursor(nsToolkit::mDllInstance,
3152 MAKEINTRESOURCE(IDC_ZOOMOUT));
3154 case eCursor_not_allowed:
3155 case eCursor_no_drop:
3156 return ::LoadCursor(nullptr, IDC_NO);
3158 case eCursor_col_resize:
3159 return ::LoadCursor(nsToolkit::mDllInstance,
3160 MAKEINTRESOURCE(IDC_COLRESIZE));
3162 case eCursor_row_resize:
3163 return ::LoadCursor(nsToolkit::mDllInstance,
3164 MAKEINTRESOURCE(IDC_ROWRESIZE));
3166 case eCursor_vertical_text:
3167 return ::LoadCursor(nsToolkit::mDllInstance,
3168 MAKEINTRESOURCE(IDC_VERTICALTEXT));
3170 case eCursor_all_scroll:
3171 // XXX not 100% appropriate perhaps
3172 return ::LoadCursor(nullptr, IDC_SIZEALL);
3174 case eCursor_nesw_resize:
3175 return ::LoadCursor(nullptr, IDC_SIZENESW);
3177 case eCursor_nwse_resize:
3178 return ::LoadCursor(nullptr, IDC_SIZENWSE);
3180 case eCursor_ns_resize:
3181 return ::LoadCursor(nullptr, IDC_SIZENS);
3183 case eCursor_ew_resize:
3184 return ::LoadCursor(nullptr, IDC_SIZEWE);
3186 case eCursor_none:
3187 return ::LoadCursor(nsToolkit::mDllInstance, MAKEINTRESOURCE(IDC_NONE));
3189 default:
3190 NS_ERROR("Invalid cursor type");
3191 return nullptr;
3195 static HCURSOR CursorForImage(const nsIWidget::Cursor& aCursor,
3196 CSSToLayoutDeviceScale aScale) {
3197 if (!aCursor.IsCustom()) {
3198 return nullptr;
3201 nsIntSize size = nsIWidget::CustomCursorSize(aCursor);
3203 // Reject cursors greater than 128 pixels in either direction, to prevent
3204 // spoofing.
3205 // XXX ideally we should rescale. Also, we could modify the API to
3206 // allow trusted content to set larger cursors.
3207 if (size.width > 128 || size.height > 128) {
3208 return nullptr;
3211 LayoutDeviceIntSize layoutSize =
3212 RoundedToInt(CSSIntSize(size.width, size.height) * aScale);
3213 LayoutDeviceIntPoint hotspot =
3214 RoundedToInt(CSSIntPoint(aCursor.mHotspotX, aCursor.mHotspotY) * aScale);
3215 HCURSOR cursor;
3216 nsresult rv = nsWindowGfx::CreateIcon(aCursor.mContainer, true, hotspot,
3217 layoutSize, &cursor);
3218 if (NS_FAILED(rv)) {
3219 return nullptr;
3222 return cursor;
3225 void nsWindow::SetCursor(const Cursor& aCursor) {
3226 static HCURSOR sCurrentHCursor = nullptr;
3227 static bool sCurrentHCursorIsCustom = false;
3229 mCursor = aCursor;
3231 if (sCurrentCursor == aCursor && sCurrentHCursor && !mUpdateCursor) {
3232 // Cursors in windows are global, so even if our mUpdateCursor flag is
3233 // false we always need to make sure the Windows cursor is up-to-date,
3234 // since stuff like native drag and drop / resizers code can mutate it
3235 // outside of this method.
3236 ::SetCursor(sCurrentHCursor);
3237 return;
3240 mUpdateCursor = false;
3242 if (sCurrentHCursorIsCustom) {
3243 ::DestroyIcon(sCurrentHCursor);
3245 sCurrentHCursor = nullptr;
3246 sCurrentHCursorIsCustom = false;
3247 sCurrentCursor = aCursor;
3249 HCURSOR cursor = CursorForImage(aCursor, GetDefaultScale());
3250 bool custom = false;
3251 if (cursor) {
3252 custom = true;
3253 } else {
3254 cursor = CursorFor(aCursor.mDefaultCursor);
3257 if (!cursor) {
3258 return;
3261 sCurrentHCursor = cursor;
3262 sCurrentHCursorIsCustom = custom;
3263 ::SetCursor(cursor);
3266 /**************************************************************
3268 * SECTION: nsIWidget::Get/SetTransparencyMode
3270 * Manage the transparency mode of the window containing this
3271 * widget. Only works for popup and dialog windows when the
3272 * Desktop Window Manager compositor is not enabled.
3274 **************************************************************/
3276 TransparencyMode nsWindow::GetTransparencyMode() {
3277 return GetTopLevelWindow(true)->GetWindowTranslucencyInner();
3280 void nsWindow::SetTransparencyMode(TransparencyMode aMode) {
3281 nsWindow* window = GetTopLevelWindow(true);
3282 MOZ_ASSERT(window);
3284 if (!window || window->DestroyCalled()) {
3285 return;
3288 if (WindowType::TopLevel == window->mWindowType &&
3289 mTransparencyMode != aMode &&
3290 !gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
3291 NS_WARNING("Cannot set transparency mode on top-level windows.");
3292 return;
3295 window->SetWindowTranslucencyInner(aMode);
3298 void nsWindow::UpdateOpaqueRegion(const LayoutDeviceIntRegion& aOpaqueRegion) {
3299 if (!HasGlass() || GetParent()) return;
3301 // If there is no opaque region or hidechrome=true, set margins
3302 // to support a full sheet of glass. Comments in MSDN indicate
3303 // all values must be set to -1 to get a full sheet of glass.
3304 MARGINS margins = {-1, -1, -1, -1};
3305 if (!aOpaqueRegion.IsEmpty()) {
3306 LayoutDeviceIntRect clientBounds = GetClientBounds();
3307 // Find the largest rectangle and use that to calculate the inset.
3308 LayoutDeviceIntRect largest = aOpaqueRegion.GetLargestRectangle();
3309 margins.cxLeftWidth = largest.X();
3310 margins.cxRightWidth = clientBounds.Width() - largest.XMost();
3311 margins.cyBottomHeight = clientBounds.Height() - largest.YMost();
3312 if (mCustomNonClient) {
3313 // The minimum glass height must be the caption buttons height,
3314 // otherwise the buttons are drawn incorrectly.
3315 largest.MoveToY(std::max<uint32_t>(
3316 largest.Y(), nsUXThemeData::GetCommandButtonBoxMetrics().cy));
3318 margins.cyTopHeight = largest.Y();
3321 // Only update glass area if there are changes
3322 if (memcmp(&mGlassMargins, &margins, sizeof mGlassMargins)) {
3323 mGlassMargins = margins;
3324 UpdateGlass();
3328 /**************************************************************
3330 * SECTION: nsIWidget::UpdateWindowDraggingRegion
3332 * For setting the draggable titlebar region from CSS
3333 * with -moz-window-dragging: drag.
3335 **************************************************************/
3337 void nsWindow::UpdateWindowDraggingRegion(
3338 const LayoutDeviceIntRegion& aRegion) {
3339 if (mDraggableRegion != aRegion) {
3340 mDraggableRegion = aRegion;
3344 void nsWindow::UpdateGlass() {
3345 MARGINS margins = mGlassMargins;
3347 // DWMNCRP_USEWINDOWSTYLE - The non-client rendering area is
3348 // rendered based on the window style.
3349 // DWMNCRP_ENABLED - The non-client area rendering is
3350 // enabled; the window style is ignored.
3351 DWMNCRENDERINGPOLICY policy = DWMNCRP_USEWINDOWSTYLE;
3352 switch (mTransparencyMode) {
3353 case TransparencyMode::BorderlessGlass:
3354 // Only adjust if there is some opaque rectangle
3355 if (margins.cxLeftWidth >= 0) {
3356 margins.cxLeftWidth += kGlassMarginAdjustment;
3357 margins.cyTopHeight += kGlassMarginAdjustment;
3358 margins.cxRightWidth += kGlassMarginAdjustment;
3359 margins.cyBottomHeight += kGlassMarginAdjustment;
3361 policy = DWMNCRP_ENABLED;
3362 break;
3363 default:
3364 break;
3367 MOZ_LOG(gWindowsLog, LogLevel::Info,
3368 ("glass margins: left:%d top:%d right:%d bottom:%d\n",
3369 margins.cxLeftWidth, margins.cyTopHeight, margins.cxRightWidth,
3370 margins.cyBottomHeight));
3372 // Extends the window frame behind the client area
3373 if (gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
3374 DwmExtendFrameIntoClientArea(mWnd, &margins);
3375 DwmSetWindowAttribute(mWnd, DWMWA_NCRENDERING_POLICY, &policy,
3376 sizeof policy);
3380 /**************************************************************
3382 * SECTION: nsIWidget::HideWindowChrome
3384 * Show or hide window chrome.
3386 **************************************************************/
3388 void nsWindow::HideWindowChrome(bool aShouldHide) {
3389 HWND hwnd = WinUtils::GetTopLevelHWND(mWnd, true);
3390 if (!WinUtils::GetNSWindowPtr(hwnd)) {
3391 NS_WARNING("Trying to hide window decorations in an embedded context");
3392 return;
3395 if (mHideChrome == aShouldHide) return;
3397 DWORD_PTR style, exStyle;
3398 mHideChrome = aShouldHide;
3399 if (aShouldHide) {
3400 DWORD_PTR tempStyle = ::GetWindowLongPtrW(hwnd, GWL_STYLE);
3401 DWORD_PTR tempExStyle = ::GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
3403 style = tempStyle & ~(WS_CAPTION | WS_THICKFRAME);
3404 exStyle = tempExStyle & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE |
3405 WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
3407 mOldStyle = tempStyle;
3408 mOldExStyle = tempExStyle;
3409 } else {
3410 if (!mOldStyle || !mOldExStyle) {
3411 mOldStyle = ::GetWindowLongPtrW(hwnd, GWL_STYLE);
3412 mOldExStyle = ::GetWindowLongPtrW(hwnd, GWL_EXSTYLE);
3415 style = mOldStyle;
3416 exStyle = mOldExStyle;
3417 if (mFutureMarginsToUse) {
3418 SetNonClientMargins(mFutureMarginsOnceChromeShows);
3422 VERIFY_WINDOW_STYLE(style);
3423 ::SetWindowLongPtrW(hwnd, GWL_STYLE, style);
3424 ::SetWindowLongPtrW(hwnd, GWL_EXSTYLE, exStyle);
3427 /**************************************************************
3429 * SECTION: nsWindow::Invalidate
3431 * Invalidate an area of the client for painting.
3433 **************************************************************/
3435 // Invalidate this component visible area
3436 void nsWindow::Invalidate(bool aEraseBackground, bool aUpdateNCArea,
3437 bool aIncludeChildren) {
3438 if (!mWnd) {
3439 return;
3442 #ifdef WIDGET_DEBUG_OUTPUT
3443 debug_DumpInvalidate(stdout, this, nullptr, "noname", (int32_t)mWnd);
3444 #endif // WIDGET_DEBUG_OUTPUT
3446 DWORD flags = RDW_INVALIDATE;
3447 if (aEraseBackground) {
3448 flags |= RDW_ERASE;
3450 if (aUpdateNCArea) {
3451 flags |= RDW_FRAME;
3453 if (aIncludeChildren) {
3454 flags |= RDW_ALLCHILDREN;
3457 VERIFY(::RedrawWindow(mWnd, nullptr, nullptr, flags));
3460 // Invalidate this component visible area
3461 void nsWindow::Invalidate(const LayoutDeviceIntRect& aRect) {
3462 if (mWnd) {
3463 #ifdef WIDGET_DEBUG_OUTPUT
3464 debug_DumpInvalidate(stdout, this, &aRect, "noname", (int32_t)mWnd);
3465 #endif // WIDGET_DEBUG_OUTPUT
3467 RECT rect;
3469 rect.left = aRect.X();
3470 rect.top = aRect.Y();
3471 rect.right = aRect.XMost();
3472 rect.bottom = aRect.YMost();
3474 VERIFY(::InvalidateRect(mWnd, &rect, FALSE));
3478 static LRESULT CALLBACK FullscreenTransitionWindowProc(HWND hWnd, UINT uMsg,
3479 WPARAM wParam,
3480 LPARAM lParam) {
3481 switch (uMsg) {
3482 case WM_FULLSCREEN_TRANSITION_BEFORE:
3483 case WM_FULLSCREEN_TRANSITION_AFTER: {
3484 DWORD duration = (DWORD)lParam;
3485 DWORD flags = AW_BLEND;
3486 if (uMsg == WM_FULLSCREEN_TRANSITION_AFTER) {
3487 flags |= AW_HIDE;
3489 ::AnimateWindow(hWnd, duration, flags);
3490 // The message sender should have added ref for us.
3491 NS_DispatchToMainThread(
3492 already_AddRefed<nsIRunnable>((nsIRunnable*)wParam));
3493 break;
3495 case WM_DESTROY:
3496 ::PostQuitMessage(0);
3497 break;
3498 default:
3499 return ::DefWindowProcW(hWnd, uMsg, wParam, lParam);
3501 return 0;
3504 struct FullscreenTransitionInitData {
3505 LayoutDeviceIntRect mBounds;
3506 HANDLE mSemaphore;
3507 HANDLE mThread;
3508 HWND mWnd;
3510 FullscreenTransitionInitData()
3511 : mSemaphore(nullptr), mThread(nullptr), mWnd(nullptr) {}
3513 ~FullscreenTransitionInitData() {
3514 if (mSemaphore) {
3515 ::CloseHandle(mSemaphore);
3517 if (mThread) {
3518 ::CloseHandle(mThread);
3523 static DWORD WINAPI FullscreenTransitionThreadProc(LPVOID lpParam) {
3524 // Initialize window class
3525 static bool sInitialized = false;
3526 if (!sInitialized) {
3527 WNDCLASSW wc = {};
3528 wc.lpfnWndProc = ::FullscreenTransitionWindowProc;
3529 wc.hInstance = nsToolkit::mDllInstance;
3530 wc.hbrBackground = ::CreateSolidBrush(RGB(0, 0, 0));
3531 wc.lpszClassName = kClassNameTransition;
3532 ::RegisterClassW(&wc);
3533 sInitialized = true;
3536 auto data = static_cast<FullscreenTransitionInitData*>(lpParam);
3537 HWND wnd = ::CreateWindowW(kClassNameTransition, L"", 0, 0, 0, 0, 0, nullptr,
3538 nullptr, nsToolkit::mDllInstance, nullptr);
3539 if (!wnd) {
3540 ::ReleaseSemaphore(data->mSemaphore, 1, nullptr);
3541 return 0;
3544 // Since AnimateWindow blocks the thread of the transition window,
3545 // we need to hide the cursor for that window, otherwise the system
3546 // would show the busy pointer to the user.
3547 ::ShowCursor(false);
3548 ::SetWindowLongW(wnd, GWL_STYLE, 0);
3549 ::SetWindowLongW(
3550 wnd, GWL_EXSTYLE,
3551 WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE);
3552 ::SetWindowPos(wnd, HWND_TOPMOST, data->mBounds.X(), data->mBounds.Y(),
3553 data->mBounds.Width(), data->mBounds.Height(), 0);
3554 data->mWnd = wnd;
3555 ::ReleaseSemaphore(data->mSemaphore, 1, nullptr);
3556 // The initialization data may no longer be valid
3557 // after we release the semaphore.
3558 data = nullptr;
3560 MSG msg;
3561 while (::GetMessageW(&msg, nullptr, 0, 0)) {
3562 ::TranslateMessage(&msg);
3563 ::DispatchMessage(&msg);
3565 ::ShowCursor(true);
3566 ::DestroyWindow(wnd);
3567 return 0;
3570 class FullscreenTransitionData final : public nsISupports {
3571 public:
3572 NS_DECL_ISUPPORTS
3574 explicit FullscreenTransitionData(HWND aWnd) : mWnd(aWnd) {
3575 MOZ_ASSERT(NS_IsMainThread(),
3576 "FullscreenTransitionData "
3577 "should be constructed in the main thread");
3580 const HWND mWnd;
3582 private:
3583 ~FullscreenTransitionData() {
3584 MOZ_ASSERT(NS_IsMainThread(),
3585 "FullscreenTransitionData "
3586 "should be deconstructed in the main thread");
3587 ::PostMessageW(mWnd, WM_DESTROY, 0, 0);
3591 NS_IMPL_ISUPPORTS0(FullscreenTransitionData)
3593 /* virtual */
3594 bool nsWindow::PrepareForFullscreenTransition(nsISupports** aData) {
3595 // We don't support fullscreen transition when composition is not
3596 // enabled, which could make the transition broken and annoying.
3597 // See bug 1184201.
3598 if (!gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
3599 return false;
3602 FullscreenTransitionInitData initData;
3603 nsCOMPtr<nsIScreen> screen = GetWidgetScreen();
3604 const DesktopIntRect rect = screen->GetRectDisplayPix();
3605 MOZ_ASSERT(BoundsUseDesktopPixels(),
3606 "Should only be called on top-level window");
3607 initData.mBounds =
3608 LayoutDeviceIntRect::Round(rect * GetDesktopToDeviceScale());
3610 // Create a semaphore for synchronizing the window handle which will
3611 // be created by the transition thread and used by the main thread for
3612 // posting the transition messages.
3613 initData.mSemaphore = ::CreateSemaphore(nullptr, 0, 1, nullptr);
3614 if (initData.mSemaphore) {
3615 initData.mThread = ::CreateThread(
3616 nullptr, 0, FullscreenTransitionThreadProc, &initData, 0, nullptr);
3617 if (initData.mThread) {
3618 ::WaitForSingleObject(initData.mSemaphore, INFINITE);
3621 if (!initData.mWnd) {
3622 return false;
3625 mTransitionWnd = initData.mWnd;
3627 auto data = new FullscreenTransitionData(initData.mWnd);
3628 *aData = data;
3629 NS_ADDREF(data);
3630 return true;
3633 /* virtual */
3634 void nsWindow::PerformFullscreenTransition(FullscreenTransitionStage aStage,
3635 uint16_t aDuration,
3636 nsISupports* aData,
3637 nsIRunnable* aCallback) {
3638 auto data = static_cast<FullscreenTransitionData*>(aData);
3639 nsCOMPtr<nsIRunnable> callback = aCallback;
3640 UINT msg = aStage == eBeforeFullscreenToggle ? WM_FULLSCREEN_TRANSITION_BEFORE
3641 : WM_FULLSCREEN_TRANSITION_AFTER;
3642 WPARAM wparam = (WPARAM)callback.forget().take();
3643 ::PostMessage(data->mWnd, msg, wparam, (LPARAM)aDuration);
3646 /* virtual */
3647 void nsWindow::CleanupFullscreenTransition() {
3648 MOZ_ASSERT(NS_IsMainThread(),
3649 "CleanupFullscreenTransition "
3650 "should only run on the main thread");
3652 mTransitionWnd = nullptr;
3655 void nsWindow::OnFullscreenWillChange(bool aFullScreen) {
3656 if (mWidgetListener) {
3657 mWidgetListener->FullscreenWillChange(aFullScreen);
3661 void nsWindow::OnFullscreenChanged(nsSizeMode aOldSizeMode, bool aFullScreen) {
3662 // Hide chrome and reposition window. Note this will also cache dimensions for
3663 // restoration, so it should only be called once per fullscreen request.
3665 // Don't do this when minimized, since our bounds make no sense then, nor when
3666 // coming back from that state.
3667 const bool toOrFromMinimized =
3668 mFrameState->GetSizeMode() == nsSizeMode_Minimized ||
3669 aOldSizeMode == nsSizeMode_Minimized;
3670 if (!toOrFromMinimized) {
3671 InfallibleMakeFullScreen(aFullScreen);
3674 if (mWidgetListener) {
3675 mWidgetListener->FullscreenChanged(aFullScreen);
3678 // Possibly notify the taskbar that we have changed our fullscreen mode.
3679 TaskbarConcealer::OnFullscreenChanged(this, aFullScreen);
3682 nsresult nsWindow::MakeFullScreen(bool aFullScreen) {
3683 mFrameState->EnsureFullscreenMode(aFullScreen);
3684 return NS_OK;
3687 /**************************************************************
3689 * SECTION: Native data storage
3691 * nsIWidget::GetNativeData
3692 * nsIWidget::FreeNativeData
3694 * Set or clear native data based on a constant.
3696 **************************************************************/
3698 // Return some native data according to aDataType
3699 void* nsWindow::GetNativeData(uint32_t aDataType) {
3700 switch (aDataType) {
3701 case NS_NATIVE_TMP_WINDOW:
3702 return (void*)::CreateWindowExW(
3703 mIsRTL ? WS_EX_LAYOUTRTL : 0,
3704 ChooseWindowClass(mWindowType, /* aForMenupopupFrame = */ false), L"",
3705 WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
3706 mWnd, nullptr, nsToolkit::mDllInstance, nullptr);
3707 case NS_NATIVE_WIDGET:
3708 case NS_NATIVE_WINDOW:
3709 case NS_NATIVE_WINDOW_WEBRTC_DEVICE_ID:
3710 return (void*)mWnd;
3711 case NS_NATIVE_GRAPHIC:
3712 MOZ_ASSERT_UNREACHABLE("Not supported on Windows:");
3713 return nullptr;
3714 case NS_RAW_NATIVE_IME_CONTEXT: {
3715 void* pseudoIMEContext = GetPseudoIMEContext();
3716 if (pseudoIMEContext) {
3717 return pseudoIMEContext;
3719 [[fallthrough]];
3721 case NS_NATIVE_TSF_THREAD_MGR:
3722 case NS_NATIVE_TSF_CATEGORY_MGR:
3723 case NS_NATIVE_TSF_DISPLAY_ATTR_MGR:
3724 return IMEHandler::GetNativeData(this, aDataType);
3726 default:
3727 break;
3730 return nullptr;
3733 void nsWindow::SetNativeData(uint32_t aDataType, uintptr_t aVal) {
3734 NS_ERROR("SetNativeData called with unsupported data type.");
3737 // Free some native data according to aDataType
3738 void nsWindow::FreeNativeData(void* data, uint32_t aDataType) {
3739 switch (aDataType) {
3740 case NS_NATIVE_GRAPHIC:
3741 case NS_NATIVE_WIDGET:
3742 case NS_NATIVE_WINDOW:
3743 break;
3744 default:
3745 break;
3749 /**************************************************************
3751 * SECTION: nsIWidget::SetTitle
3753 * Set the main windows title text.
3755 **************************************************************/
3757 nsresult nsWindow::SetTitle(const nsAString& aTitle) {
3758 const nsString& strTitle = PromiseFlatString(aTitle);
3759 AutoRestore<bool> sendingText(mSendingSetText);
3760 mSendingSetText = true;
3761 ::SendMessageW(mWnd, WM_SETTEXT, (WPARAM)0, (LPARAM)(LPCWSTR)strTitle.get());
3762 return NS_OK;
3765 /**************************************************************
3767 * SECTION: nsIWidget::SetIcon
3769 * Set the main windows icon.
3771 **************************************************************/
3773 void nsWindow::SetBigIcon(HICON aIcon) {
3774 HICON icon =
3775 (HICON)::SendMessageW(mWnd, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)aIcon);
3776 if (icon) {
3777 ::DestroyIcon(icon);
3780 mIconBig = aIcon;
3783 void nsWindow::SetSmallIcon(HICON aIcon) {
3784 HICON icon = (HICON)::SendMessageW(mWnd, WM_SETICON, (WPARAM)ICON_SMALL,
3785 (LPARAM)aIcon);
3786 if (icon) {
3787 ::DestroyIcon(icon);
3790 mIconSmall = aIcon;
3793 void nsWindow::SetIcon(const nsAString& aIconSpec) {
3794 // Assume the given string is a local identifier for an icon file.
3796 nsCOMPtr<nsIFile> iconFile;
3797 ResolveIconName(aIconSpec, u".ico"_ns, getter_AddRefs(iconFile));
3798 if (!iconFile) return;
3800 nsAutoString iconPath;
3801 iconFile->GetPath(iconPath);
3803 // XXX this should use MZLU (see bug 239279)
3805 ::SetLastError(0);
3807 HICON bigIcon =
3808 (HICON)::LoadImageW(nullptr, (LPCWSTR)iconPath.get(), IMAGE_ICON,
3809 ::GetSystemMetrics(SM_CXICON),
3810 ::GetSystemMetrics(SM_CYICON), LR_LOADFROMFILE);
3811 HICON smallIcon =
3812 (HICON)::LoadImageW(nullptr, (LPCWSTR)iconPath.get(), IMAGE_ICON,
3813 ::GetSystemMetrics(SM_CXSMICON),
3814 ::GetSystemMetrics(SM_CYSMICON), LR_LOADFROMFILE);
3816 if (bigIcon) {
3817 SetBigIcon(bigIcon);
3819 #ifdef DEBUG_SetIcon
3820 else {
3821 NS_LossyConvertUTF16toASCII cPath(iconPath);
3822 MOZ_LOG(gWindowsLog, LogLevel::Info,
3823 ("\nIcon load error; icon=%s, rc=0x%08X\n\n", cPath.get(),
3824 ::GetLastError()));
3826 #endif
3827 if (smallIcon) {
3828 SetSmallIcon(smallIcon);
3830 #ifdef DEBUG_SetIcon
3831 else {
3832 NS_LossyConvertUTF16toASCII cPath(iconPath);
3833 MOZ_LOG(gWindowsLog, LogLevel::Info,
3834 ("\nSmall icon load error; icon=%s, rc=0x%08X\n\n", cPath.get(),
3835 ::GetLastError()));
3837 #endif
3840 void nsWindow::SetBigIconNoData() {
3841 HICON bigIcon =
3842 ::LoadIconW(::GetModuleHandleW(nullptr), gStockApplicationIcon);
3843 SetBigIcon(bigIcon);
3846 void nsWindow::SetSmallIconNoData() {
3847 HICON smallIcon =
3848 ::LoadIconW(::GetModuleHandleW(nullptr), gStockApplicationIcon);
3849 SetSmallIcon(smallIcon);
3852 /**************************************************************
3854 * SECTION: nsIWidget::WidgetToScreenOffset
3856 * Return this widget's origin in screen coordinates.
3858 **************************************************************/
3860 LayoutDeviceIntPoint nsWindow::WidgetToScreenOffset() {
3861 POINT point;
3862 point.x = 0;
3863 point.y = 0;
3864 ::ClientToScreen(mWnd, &point);
3865 return LayoutDeviceIntPoint(point.x, point.y);
3868 LayoutDeviceIntMargin nsWindow::ClientToWindowMargin() {
3869 if (mWindowType == WindowType::Popup && !IsPopupWithTitleBar()) {
3870 return {};
3873 if (mCustomNonClient) {
3874 return NonClientSizeMargin(NormalWindowNonClientOffset());
3877 // Just use a dummy 200x200 at (200, 200) client rect as the rect.
3878 RECT clientRect;
3879 clientRect.left = 200;
3880 clientRect.top = 200;
3881 clientRect.right = 400;
3882 clientRect.bottom = 400;
3884 auto ToRect = [](const RECT& aRect) -> LayoutDeviceIntRect {
3885 return {aRect.left, aRect.top, aRect.right - aRect.left,
3886 aRect.bottom - aRect.top};
3889 RECT windowRect = clientRect;
3890 ::AdjustWindowRectEx(&windowRect, WindowStyle(), false, WindowExStyle());
3892 return ToRect(windowRect) - ToRect(clientRect);
3895 /**************************************************************
3897 * SECTION: nsIWidget::EnableDragDrop
3899 * Enables/Disables drag and drop of files on this widget.
3901 **************************************************************/
3903 void nsWindow::EnableDragDrop(bool aEnable) {
3904 if (!mWnd) {
3905 // Return early if the window already closed
3906 return;
3909 if (aEnable) {
3910 if (!mNativeDragTarget) {
3911 mNativeDragTarget = new nsNativeDragTarget(this);
3912 mNativeDragTarget->AddRef();
3913 if (SUCCEEDED(::CoLockObjectExternal((LPUNKNOWN)mNativeDragTarget, TRUE,
3914 FALSE))) {
3915 ::RegisterDragDrop(mWnd, (LPDROPTARGET)mNativeDragTarget);
3918 } else {
3919 if (mWnd && mNativeDragTarget) {
3920 ::RevokeDragDrop(mWnd);
3921 ::CoLockObjectExternal((LPUNKNOWN)mNativeDragTarget, FALSE, TRUE);
3922 mNativeDragTarget->DragCancel();
3923 NS_RELEASE(mNativeDragTarget);
3928 /**************************************************************
3930 * SECTION: nsIWidget::CaptureMouse
3932 * Enables/Disables system mouse capture.
3934 **************************************************************/
3936 void nsWindow::CaptureMouse(bool aCapture) {
3937 TRACKMOUSEEVENT mTrack;
3938 mTrack.cbSize = sizeof(TRACKMOUSEEVENT);
3939 mTrack.dwHoverTime = 0;
3940 mTrack.hwndTrack = mWnd;
3941 if (aCapture) {
3942 mTrack.dwFlags = TME_CANCEL | TME_LEAVE;
3943 ::SetCapture(mWnd);
3944 } else {
3945 mTrack.dwFlags = TME_LEAVE;
3946 ::ReleaseCapture();
3948 sIsInMouseCapture = aCapture;
3949 TrackMouseEvent(&mTrack);
3952 /**************************************************************
3954 * SECTION: nsIWidget::CaptureRollupEvents
3956 * Dealing with event rollup on destroy for popups. Enables &
3957 * Disables system capture of any and all events that would
3958 * cause a dropdown to be rolled up.
3960 **************************************************************/
3962 void nsWindow::CaptureRollupEvents(bool aDoCapture) {
3963 if (aDoCapture) {
3964 if (!sMsgFilterHook && !sCallProcHook && !sCallMouseHook) {
3965 RegisterSpecialDropdownHooks();
3967 sProcessHook = true;
3968 } else {
3969 sProcessHook = false;
3970 UnregisterSpecialDropdownHooks();
3974 /**************************************************************
3976 * SECTION: nsIWidget::GetAttention
3978 * Bring this window to the user's attention.
3980 **************************************************************/
3982 // Draw user's attention to this window until it comes to foreground.
3983 nsresult nsWindow::GetAttention(int32_t aCycleCount) {
3984 // Got window?
3985 if (!mWnd) return NS_ERROR_NOT_INITIALIZED;
3987 HWND flashWnd = WinUtils::GetTopLevelHWND(mWnd, false, false);
3988 HWND fgWnd = ::GetForegroundWindow();
3989 // Don't flash if the flash count is 0 or if the foreground window is our
3990 // window handle or that of our owned-most window.
3991 if (aCycleCount == 0 || flashWnd == fgWnd ||
3992 flashWnd == WinUtils::GetTopLevelHWND(fgWnd, false, false)) {
3993 return NS_OK;
3996 DWORD defaultCycleCount = 0;
3997 ::SystemParametersInfo(SPI_GETFOREGROUNDFLASHCOUNT, 0, &defaultCycleCount, 0);
3999 FLASHWINFO flashInfo = {sizeof(FLASHWINFO), flashWnd, FLASHW_ALL,
4000 aCycleCount > 0 ? aCycleCount : defaultCycleCount, 0};
4001 ::FlashWindowEx(&flashInfo);
4003 return NS_OK;
4006 void nsWindow::StopFlashing() {
4007 HWND flashWnd = mWnd;
4008 while (HWND ownerWnd = ::GetWindow(flashWnd, GW_OWNER)) {
4009 flashWnd = ownerWnd;
4012 FLASHWINFO flashInfo = {sizeof(FLASHWINFO), flashWnd, FLASHW_STOP, 0, 0};
4013 ::FlashWindowEx(&flashInfo);
4016 /**************************************************************
4018 * SECTION: nsIWidget::HasPendingInputEvent
4020 * Ask whether there user input events pending. All input events are
4021 * included, including those not targeted at this nsIwidget instance.
4023 **************************************************************/
4025 bool nsWindow::HasPendingInputEvent() {
4026 // If there is pending input or the user is currently
4027 // moving the window then return true.
4028 // Note: When the user is moving the window WIN32 spins
4029 // a separate event loop and input events are not
4030 // reported to the application.
4031 if (HIWORD(GetQueueStatus(QS_INPUT))) return true;
4032 GUITHREADINFO guiInfo;
4033 guiInfo.cbSize = sizeof(GUITHREADINFO);
4034 if (!GetGUIThreadInfo(GetCurrentThreadId(), &guiInfo)) return false;
4035 return GUI_INMOVESIZE == (guiInfo.flags & GUI_INMOVESIZE);
4038 /**************************************************************
4040 * SECTION: nsIWidget::GetWindowRenderer
4042 * Get the window renderer associated with this widget.
4044 **************************************************************/
4046 WindowRenderer* nsWindow::GetWindowRenderer() {
4047 if (mWindowRenderer) {
4048 return mWindowRenderer;
4051 if (!mLocalesChangedObserver) {
4052 mLocalesChangedObserver = new LocalesChangedObserver(this);
4055 // Try OMTC first.
4056 if (!mWindowRenderer && ShouldUseOffMainThreadCompositing()) {
4057 gfxWindowsPlatform::GetPlatform()->UpdateRenderMode();
4058 CreateCompositor();
4061 if (!mWindowRenderer) {
4062 MOZ_ASSERT(!mCompositorSession && !mCompositorBridgeChild);
4063 MOZ_ASSERT(!mCompositorWidgetDelegate);
4065 // Ensure we have a widget proxy even if we're not using the compositor,
4066 // since all our transparent window handling lives there.
4067 WinCompositorWidgetInitData initData(
4068 reinterpret_cast<uintptr_t>(mWnd),
4069 reinterpret_cast<uintptr_t>(static_cast<nsIWidget*>(this)),
4070 mTransparencyMode, mFrameState->GetSizeMode());
4071 // If we're not using the compositor, the options don't actually matter.
4072 CompositorOptions options(false, false);
4073 mBasicLayersSurface =
4074 new InProcessWinCompositorWidget(initData, options, this);
4075 mCompositorWidgetDelegate = mBasicLayersSurface;
4076 mWindowRenderer = CreateFallbackRenderer();
4079 NS_ASSERTION(mWindowRenderer, "Couldn't provide a valid window renderer.");
4081 if (mWindowRenderer) {
4082 // Update the size constraints now that the layer manager has been
4083 // created.
4084 KnowsCompositor* knowsCompositor = mWindowRenderer->AsKnowsCompositor();
4085 if (knowsCompositor) {
4086 SizeConstraints c = mSizeConstraints;
4087 mMaxTextureSize = knowsCompositor->GetMaxTextureSize();
4088 c.mMaxSize.width = std::min(c.mMaxSize.width, mMaxTextureSize);
4089 c.mMaxSize.height = std::min(c.mMaxSize.height, mMaxTextureSize);
4090 nsBaseWidget::SetSizeConstraints(c);
4094 return mWindowRenderer;
4097 /**************************************************************
4099 * SECTION: nsBaseWidget::SetCompositorWidgetDelegate
4101 * Called to connect the nsWindow to the delegate providing
4102 * platform compositing API access.
4104 **************************************************************/
4106 void nsWindow::SetCompositorWidgetDelegate(CompositorWidgetDelegate* delegate) {
4107 if (delegate) {
4108 mCompositorWidgetDelegate = delegate->AsPlatformSpecificDelegate();
4109 MOZ_ASSERT(mCompositorWidgetDelegate,
4110 "nsWindow::SetCompositorWidgetDelegate called with a "
4111 "non-PlatformCompositorWidgetDelegate");
4112 } else {
4113 mCompositorWidgetDelegate = nullptr;
4117 /**************************************************************
4119 * SECTION: nsIWidget::OnDefaultButtonLoaded
4121 * Called after the dialog is loaded and it has a default button.
4123 **************************************************************/
4125 nsresult nsWindow::OnDefaultButtonLoaded(
4126 const LayoutDeviceIntRect& aButtonRect) {
4127 if (aButtonRect.IsEmpty()) return NS_OK;
4129 // Don't snap when we are not active.
4130 HWND activeWnd = ::GetActiveWindow();
4131 if (activeWnd != ::GetForegroundWindow() ||
4132 WinUtils::GetTopLevelHWND(mWnd, true) !=
4133 WinUtils::GetTopLevelHWND(activeWnd, true)) {
4134 return NS_OK;
4137 bool isAlwaysSnapCursor =
4138 Preferences::GetBool("ui.cursor_snapping.always_enabled", false);
4140 if (!isAlwaysSnapCursor) {
4141 BOOL snapDefaultButton;
4142 if (!::SystemParametersInfo(SPI_GETSNAPTODEFBUTTON, 0, &snapDefaultButton,
4143 0) ||
4144 !snapDefaultButton)
4145 return NS_OK;
4148 LayoutDeviceIntRect widgetRect = GetScreenBounds();
4149 LayoutDeviceIntRect buttonRect(aButtonRect + widgetRect.TopLeft());
4151 LayoutDeviceIntPoint centerOfButton(buttonRect.X() + buttonRect.Width() / 2,
4152 buttonRect.Y() + buttonRect.Height() / 2);
4153 // The center of the button can be outside of the widget.
4154 // E.g., it could be hidden by scrolling.
4155 if (!widgetRect.Contains(centerOfButton)) {
4156 return NS_OK;
4159 if (!::SetCursorPos(centerOfButton.x, centerOfButton.y)) {
4160 NS_ERROR("SetCursorPos failed");
4161 return NS_ERROR_FAILURE;
4163 return NS_OK;
4166 void nsWindow::UpdateThemeGeometries(
4167 const nsTArray<ThemeGeometry>& aThemeGeometries) {
4168 RefPtr<WebRenderLayerManager> layerManager =
4169 GetWindowRenderer() ? GetWindowRenderer()->AsWebRender() : nullptr;
4170 if (!layerManager) {
4171 return;
4174 if (!HasGlass() ||
4175 !gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
4176 return;
4179 mWindowButtonsRect = Nothing();
4181 if (!IsWin10OrLater()) {
4182 for (size_t i = 0; i < aThemeGeometries.Length(); i++) {
4183 if (aThemeGeometries[i].mType ==
4184 nsNativeThemeWin::eThemeGeometryTypeWindowButtons) {
4185 LayoutDeviceIntRect bounds = aThemeGeometries[i].mRect;
4186 // Extend the bounds by one pixel to the right, because that's how much
4187 // the actual window button shape extends past the client area of the
4188 // window (and overlaps the right window frame).
4189 bounds.SetWidth(bounds.Width() + 1);
4190 if (!mWindowButtonsRect) {
4191 mWindowButtonsRect = Some(bounds);
4198 void nsWindow::AddWindowOverlayWebRenderCommands(
4199 layers::WebRenderBridgeChild* aWrBridge, wr::DisplayListBuilder& aBuilder,
4200 wr::IpcResourceUpdateQueue& aResources) {
4201 if (mWindowButtonsRect) {
4202 wr::LayoutRect rect = wr::ToLayoutRect(*mWindowButtonsRect);
4203 aBuilder.PushClearRect(rect);
4207 uint32_t nsWindow::GetMaxTouchPoints() const {
4208 return WinUtils::GetMaxTouchPoints();
4211 void nsWindow::SetWindowClass(const nsAString& xulWinType,
4212 const nsAString& xulWinClass,
4213 const nsAString& xulWinName) {
4214 mIsEarlyBlankWindow = xulWinType.EqualsLiteral("navigator:blank");
4217 /**************************************************************
4218 **************************************************************
4220 ** BLOCK: Moz Events
4222 ** Moz GUI event management.
4224 **************************************************************
4225 **************************************************************/
4227 /**************************************************************
4229 * SECTION: Mozilla event initialization
4231 * Helpers for initializing moz events.
4233 **************************************************************/
4235 // Event initialization
4236 void nsWindow::InitEvent(WidgetGUIEvent& event, LayoutDeviceIntPoint* aPoint) {
4237 if (nullptr == aPoint) { // use the point from the event
4238 // get the message position in client coordinates
4239 if (mWnd != nullptr) {
4240 DWORD pos = ::GetMessagePos();
4241 POINT cpos;
4243 cpos.x = GET_X_LPARAM(pos);
4244 cpos.y = GET_Y_LPARAM(pos);
4246 ::ScreenToClient(mWnd, &cpos);
4247 event.mRefPoint = LayoutDeviceIntPoint(cpos.x, cpos.y);
4248 } else {
4249 event.mRefPoint = LayoutDeviceIntPoint(0, 0);
4251 } else {
4252 // use the point override if provided
4253 event.mRefPoint = *aPoint;
4256 event.AssignEventTime(CurrentMessageWidgetEventTime());
4259 WidgetEventTime nsWindow::CurrentMessageWidgetEventTime() const {
4260 LONG messageTime = ::GetMessageTime();
4261 return WidgetEventTime(GetMessageTimeStamp(messageTime));
4264 /**************************************************************
4266 * SECTION: Moz event dispatch helpers
4268 * Helpers for dispatching different types of moz events.
4270 **************************************************************/
4272 // Main event dispatch. Invokes callback and ProcessEvent method on
4273 // Event Listener object. Part of nsIWidget.
4274 nsresult nsWindow::DispatchEvent(WidgetGUIEvent* event,
4275 nsEventStatus& aStatus) {
4276 #ifdef WIDGET_DEBUG_OUTPUT
4277 debug_DumpEvent(stdout, event->mWidget, event, "something", (int32_t)mWnd);
4278 #endif // WIDGET_DEBUG_OUTPUT
4280 aStatus = nsEventStatus_eIgnore;
4282 // Top level windows can have a view attached which requires events be sent
4283 // to the underlying base window and the view. Added when we combined the
4284 // base chrome window with the main content child for nc client area (title
4285 // bar) rendering.
4286 if (mAttachedWidgetListener) {
4287 aStatus = mAttachedWidgetListener->HandleEvent(event, mUseAttachedEvents);
4288 } else if (mWidgetListener) {
4289 aStatus = mWidgetListener->HandleEvent(event, mUseAttachedEvents);
4292 // the window can be destroyed during processing of seemingly innocuous events
4293 // like, say, mousedowns due to the magic of scripting. mousedowns will return
4294 // nsEventStatus_eIgnore, which causes problems with the deleted window.
4295 // therefore:
4296 if (mOnDestroyCalled) aStatus = nsEventStatus_eConsumeNoDefault;
4297 return NS_OK;
4300 bool nsWindow::DispatchStandardEvent(EventMessage aMsg) {
4301 WidgetGUIEvent event(true, aMsg, this);
4302 InitEvent(event);
4304 bool result = DispatchWindowEvent(event);
4305 return result;
4308 bool nsWindow::DispatchKeyboardEvent(WidgetKeyboardEvent* event) {
4309 nsEventStatus status = DispatchInputEvent(event).mContentStatus;
4310 return ConvertStatus(status);
4313 bool nsWindow::DispatchContentCommandEvent(WidgetContentCommandEvent* aEvent) {
4314 nsEventStatus status;
4315 DispatchEvent(aEvent, status);
4316 return ConvertStatus(status);
4319 bool nsWindow::DispatchWheelEvent(WidgetWheelEvent* aEvent) {
4320 nsEventStatus status =
4321 DispatchInputEvent(aEvent->AsInputEvent()).mContentStatus;
4322 return ConvertStatus(status);
4325 // Recursively dispatch synchronous paints for nsIWidget
4326 // descendants with invalidated rectangles.
4327 BOOL CALLBACK nsWindow::DispatchStarvedPaints(HWND aWnd, LPARAM aMsg) {
4328 LONG_PTR proc = ::GetWindowLongPtrW(aWnd, GWLP_WNDPROC);
4329 if (proc == (LONG_PTR)&nsWindow::WindowProc) {
4330 // its one of our windows so check to see if it has a
4331 // invalidated rect. If it does. Dispatch a synchronous
4332 // paint.
4333 if (GetUpdateRect(aWnd, nullptr, FALSE)) VERIFY(::UpdateWindow(aWnd));
4335 return TRUE;
4338 // Check for pending paints and dispatch any pending paint
4339 // messages for any nsIWidget which is a descendant of the
4340 // top-level window that *this* window is embedded within.
4342 // Note: We do not dispatch pending paint messages for non
4343 // nsIWidget managed windows.
4344 void nsWindow::DispatchPendingEvents() {
4345 if (mPainting) {
4346 NS_WARNING(
4347 "We were asked to dispatch pending events during painting, "
4348 "denying since that's unsafe.");
4349 return;
4352 // We need to ensure that reflow events do not get starved.
4353 // At the same time, we don't want to recurse through here
4354 // as that would prevent us from dispatching starved paints.
4355 static int recursionBlocker = 0;
4356 if (recursionBlocker++ == 0) {
4357 NS_ProcessPendingEvents(nullptr, PR_MillisecondsToInterval(100));
4358 --recursionBlocker;
4361 // Quickly check to see if there are any paint events pending,
4362 // but only dispatch them if it has been long enough since the
4363 // last paint completed.
4364 if (::GetQueueStatus(QS_PAINT) &&
4365 ((TimeStamp::Now() - mLastPaintEndTime).ToMilliseconds() >= 50)) {
4366 // Find the top level window.
4367 HWND topWnd = WinUtils::GetTopLevelHWND(mWnd);
4369 // Dispatch pending paints for topWnd and all its descendant windows.
4370 // Note: EnumChildWindows enumerates all descendant windows not just
4371 // the children (but not the window itself).
4372 nsWindow::DispatchStarvedPaints(topWnd, 0);
4373 ::EnumChildWindows(topWnd, nsWindow::DispatchStarvedPaints, 0);
4377 void nsWindow::DispatchCustomEvent(const nsString& eventName) {
4378 if (Document* doc = GetDocument()) {
4379 if (nsPIDOMWindowOuter* win = doc->GetWindow()) {
4380 win->DispatchCustomEvent(eventName, ChromeOnlyDispatch::eYes);
4385 bool nsWindow::TouchEventShouldStartDrag(EventMessage aEventMessage,
4386 LayoutDeviceIntPoint aEventPoint) {
4387 // Allow users to start dragging by double-tapping.
4388 if (aEventMessage == eMouseDoubleClick) {
4389 return true;
4392 // In chrome UI, allow touchdownstartsdrag attributes
4393 // to cause any touchdown event to trigger a drag.
4394 if (aEventMessage == eMouseDown) {
4395 WidgetMouseEvent hittest(true, eMouseHitTest, this,
4396 WidgetMouseEvent::eReal);
4397 hittest.mRefPoint = aEventPoint;
4398 hittest.mIgnoreRootScrollFrame = true;
4399 hittest.mInputSource = MouseEvent_Binding::MOZ_SOURCE_TOUCH;
4400 DispatchInputEvent(&hittest);
4402 if (EventTarget* target = hittest.GetDOMEventTarget()) {
4403 if (nsIContent* content = nsIContent::FromEventTarget(target)) {
4404 // Check if the element or any parent element has the
4405 // attribute we're looking for.
4406 for (Element* element = content->GetAsElementOrParentElement(); element;
4407 element = element->GetParentElement()) {
4408 nsAutoString startDrag;
4409 element->GetAttribute(u"touchdownstartsdrag"_ns, startDrag);
4410 if (!startDrag.IsEmpty()) {
4411 return true;
4418 return false;
4421 // Deal with all sort of mouse event
4422 bool nsWindow::DispatchMouseEvent(EventMessage aEventMessage, WPARAM wParam,
4423 LPARAM lParam, bool aIsContextMenuKey,
4424 int16_t aButton, uint16_t aInputSource,
4425 WinPointerInfo* aPointerInfo,
4426 bool aIgnoreAPZ) {
4427 ContextMenuPreventer contextMenuPreventer(this);
4428 bool result = false;
4430 UserActivity();
4432 if (!mWidgetListener) {
4433 return result;
4436 LayoutDeviceIntPoint eventPoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
4437 LayoutDeviceIntPoint mpScreen = eventPoint + WidgetToScreenOffset();
4439 // Suppress mouse moves caused by widget creation. Make sure to do this early
4440 // so that we update sLastMouseMovePoint even for touch-induced mousemove
4441 // events.
4442 if (aEventMessage == eMouseMove) {
4443 if ((sLastMouseMovePoint.x == mpScreen.x.value) &&
4444 (sLastMouseMovePoint.y == mpScreen.y.value)) {
4445 return result;
4447 sLastMouseMovePoint.x = mpScreen.x;
4448 sLastMouseMovePoint.y = mpScreen.y;
4451 if (!aIgnoreAPZ && WinUtils::GetIsMouseFromTouch(aEventMessage)) {
4452 if (mTouchWindow) {
4453 // If mTouchWindow is true, then we must have APZ enabled and be
4454 // feeding it raw touch events. In that case we only want to
4455 // send touch-generated mouse events to content if they should
4456 // start a touch-based drag-and-drop gesture, such as on
4457 // double-tapping or when tapping elements marked with the
4458 // touchdownstartsdrag attribute in chrome UI.
4459 MOZ_ASSERT(mAPZC);
4460 if (TouchEventShouldStartDrag(aEventMessage, eventPoint)) {
4461 aEventMessage = eMouseTouchDrag;
4462 } else {
4463 return result;
4468 uint32_t pointerId =
4469 aPointerInfo ? aPointerInfo->pointerId : MOUSE_POINTERID();
4471 // Since it is unclear whether a user will use the digitizer,
4472 // Postpone initialization until first PEN message will be found.
4473 if (MouseEvent_Binding::MOZ_SOURCE_PEN == aInputSource
4474 // Messages should be only at topLevel window.
4475 && WindowType::TopLevel == mWindowType
4476 // Currently this scheme is used only when pointer events is enabled.
4477 && InkCollector::sInkCollector) {
4478 InkCollector::sInkCollector->SetTarget(mWnd);
4479 InkCollector::sInkCollector->SetPointerId(pointerId);
4482 switch (aEventMessage) {
4483 case eMouseDown:
4484 CaptureMouse(true);
4485 break;
4487 // eMouseMove and eMouseExitFromWidget are here because we need to make
4488 // sure capture flag isn't left on after a drag where we wouldn't see a
4489 // button up message (see bug 324131).
4490 case eMouseUp:
4491 case eMouseMove:
4492 case eMouseExitFromWidget:
4493 if (!(wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) &&
4494 sIsInMouseCapture)
4495 CaptureMouse(false);
4496 break;
4498 default:
4499 break;
4501 } // switch
4503 WidgetMouseEvent event(true, aEventMessage, this, WidgetMouseEvent::eReal,
4504 aIsContextMenuKey ? WidgetMouseEvent::eContextMenuKey
4505 : WidgetMouseEvent::eNormal);
4506 if (aEventMessage == eContextMenu && aIsContextMenuKey) {
4507 LayoutDeviceIntPoint zero(0, 0);
4508 InitEvent(event, &zero);
4509 } else {
4510 InitEvent(event, &eventPoint);
4513 ModifierKeyState modifierKeyState;
4514 modifierKeyState.InitInputEvent(event);
4516 // eContextMenu with Shift state is special. It won't fire "contextmenu"
4517 // event in the web content for blocking web content to prevent its default.
4518 // However, Shift+F10 is a standard shortcut key on Windows. Therefore,
4519 // this should not block web page to prevent its default. I.e., it should
4520 // behave same as ContextMenu key without Shift key.
4521 // XXX Should we allow to block web page to prevent its default with
4522 // Ctrl+Shift+F10 or Alt+Shift+F10 instead?
4523 if (aEventMessage == eContextMenu && aIsContextMenuKey && event.IsShift() &&
4524 NativeKey::LastKeyOrCharMSG().message == WM_SYSKEYDOWN &&
4525 NativeKey::LastKeyOrCharMSG().wParam == VK_F10) {
4526 event.mModifiers &= ~MODIFIER_SHIFT;
4529 event.mButton = aButton;
4530 event.mInputSource = aInputSource;
4531 if (aPointerInfo) {
4532 // Mouse events from Windows WM_POINTER*. Fill more information in
4533 // WidgetMouseEvent.
4534 event.AssignPointerHelperData(*aPointerInfo);
4535 event.mPressure = aPointerInfo->mPressure;
4536 event.mButtons = aPointerInfo->mButtons;
4537 } else {
4538 // If we get here the mouse events must be from non-touch sources, so
4539 // convert it to pointer events as well
4540 event.convertToPointer = true;
4541 event.pointerId = pointerId;
4544 // Static variables used to distinguish simple-, double- and triple-clicks.
4545 static POINT sLastMousePoint = {0};
4546 static LONG sLastMouseDownTime = 0L;
4547 static LONG sLastClickCount = 0L;
4548 static BYTE sLastMouseButton = 0;
4550 bool insideMovementThreshold =
4551 (DeprecatedAbs(sLastMousePoint.x - eventPoint.x.value) <
4552 (short)::GetSystemMetrics(SM_CXDOUBLECLK)) &&
4553 (DeprecatedAbs(sLastMousePoint.y - eventPoint.y.value) <
4554 (short)::GetSystemMetrics(SM_CYDOUBLECLK));
4556 BYTE eventButton;
4557 switch (aButton) {
4558 case MouseButton::ePrimary:
4559 eventButton = VK_LBUTTON;
4560 break;
4561 case MouseButton::eMiddle:
4562 eventButton = VK_MBUTTON;
4563 break;
4564 case MouseButton::eSecondary:
4565 eventButton = VK_RBUTTON;
4566 break;
4567 default:
4568 eventButton = 0;
4569 break;
4572 // Doubleclicks are used to set the click count, then changed to mousedowns
4573 // We're going to time double-clicks from mouse *up* to next mouse *down*
4574 LONG curMsgTime = ::GetMessageTime();
4576 switch (aEventMessage) {
4577 case eMouseDoubleClick:
4578 event.mMessage = eMouseDown;
4579 event.mButton = aButton;
4580 sLastClickCount = 2;
4581 sLastMouseDownTime = curMsgTime;
4582 break;
4583 case eMouseUp:
4584 // remember when this happened for the next mouse down
4585 sLastMousePoint.x = eventPoint.x;
4586 sLastMousePoint.y = eventPoint.y;
4587 sLastMouseButton = eventButton;
4588 break;
4589 case eMouseDown:
4590 // now look to see if we want to convert this to a double- or triple-click
4591 if (((curMsgTime - sLastMouseDownTime) < (LONG)::GetDoubleClickTime()) &&
4592 insideMovementThreshold && eventButton == sLastMouseButton) {
4593 sLastClickCount++;
4594 } else {
4595 // reset the click count, to count *this* click
4596 sLastClickCount = 1;
4598 // Set last Click time on MouseDown only
4599 sLastMouseDownTime = curMsgTime;
4600 break;
4601 case eMouseMove:
4602 if (!insideMovementThreshold) {
4603 sLastClickCount = 0;
4605 break;
4606 case eMouseExitFromWidget:
4607 event.mExitFrom =
4608 Some(IsTopLevelMouseExit(mWnd) ? WidgetMouseEvent::ePlatformTopLevel
4609 : WidgetMouseEvent::ePlatformChild);
4610 break;
4611 default:
4612 break;
4614 event.mClickCount = sLastClickCount;
4616 #ifdef NS_DEBUG_XX
4617 MOZ_LOG(gWindowsLog, LogLevel::Info,
4618 ("Msg Time: %d Click Count: %d\n", curMsgTime, event.mClickCount));
4619 #endif
4621 // call the event callback
4622 if (mWidgetListener) {
4623 if (aEventMessage == eMouseMove) {
4624 LayoutDeviceIntRect rect = GetBounds();
4625 rect.MoveTo(0, 0);
4627 if (rect.Contains(event.mRefPoint)) {
4628 if (sCurrentWindow == nullptr || sCurrentWindow != this) {
4629 if ((nullptr != sCurrentWindow) && (!sCurrentWindow->mInDtor)) {
4630 LPARAM pos = sCurrentWindow->lParamToClient(lParamToScreen(lParam));
4631 sCurrentWindow->DispatchMouseEvent(
4632 eMouseExitFromWidget, wParam, pos, false, MouseButton::ePrimary,
4633 aInputSource, aPointerInfo);
4635 sCurrentWindow = this;
4636 if (!mInDtor) {
4637 LPARAM pos = sCurrentWindow->lParamToClient(lParamToScreen(lParam));
4638 sCurrentWindow->DispatchMouseEvent(
4639 eMouseEnterIntoWidget, wParam, pos, false,
4640 MouseButton::ePrimary, aInputSource, aPointerInfo);
4644 } else if (aEventMessage == eMouseExitFromWidget) {
4645 if (sCurrentWindow == this) {
4646 sCurrentWindow = nullptr;
4650 nsIWidget::ContentAndAPZEventStatus eventStatus =
4651 DispatchInputEvent(&event);
4652 contextMenuPreventer.Update(event, eventStatus);
4653 return ConvertStatus(eventStatus.mContentStatus);
4656 return result;
4659 HWND nsWindow::GetTopLevelForFocus(HWND aCurWnd) {
4660 // retrieve the toplevel window or dialogue
4661 HWND toplevelWnd = nullptr;
4662 while (aCurWnd) {
4663 toplevelWnd = aCurWnd;
4664 nsWindow* win = WinUtils::GetNSWindowPtr(aCurWnd);
4665 if (win) {
4666 if (win->mWindowType == WindowType::TopLevel ||
4667 win->mWindowType == WindowType::Dialog) {
4668 break;
4672 aCurWnd = ::GetParent(aCurWnd); // Parent or owner (if has no parent)
4674 return toplevelWnd;
4677 void nsWindow::DispatchFocusToTopLevelWindow(bool aIsActivate) {
4678 if (aIsActivate) {
4679 sJustGotActivate = false;
4681 sJustGotDeactivate = false;
4682 mLastKillFocusWindow = nullptr;
4684 HWND toplevelWnd = GetTopLevelForFocus(mWnd);
4686 if (toplevelWnd) {
4687 nsWindow* win = WinUtils::GetNSWindowPtr(toplevelWnd);
4688 if (win && win->mWidgetListener) {
4689 if (aIsActivate) {
4690 win->mWidgetListener->WindowActivated();
4691 } else {
4692 win->mWidgetListener->WindowDeactivated();
4698 HWND nsWindow::WindowAtMouse() {
4699 DWORD pos = ::GetMessagePos();
4700 POINT mp;
4701 mp.x = GET_X_LPARAM(pos);
4702 mp.y = GET_Y_LPARAM(pos);
4703 return ::WindowFromPoint(mp);
4706 bool nsWindow::IsTopLevelMouseExit(HWND aWnd) {
4707 HWND mouseWnd = WindowAtMouse();
4709 // WinUtils::GetTopLevelHWND() will return a HWND for the window frame
4710 // (which includes the non-client area). If the mouse has moved into
4711 // the non-client area, we should treat it as a top-level exit.
4712 HWND mouseTopLevel = WinUtils::GetTopLevelHWND(mouseWnd);
4713 if (mouseWnd == mouseTopLevel) return true;
4715 return WinUtils::GetTopLevelHWND(aWnd) != mouseTopLevel;
4718 /**************************************************************
4720 * SECTION: IPC
4722 * IPC related helpers.
4724 **************************************************************/
4726 // static
4727 bool nsWindow::IsAsyncResponseEvent(UINT aMsg, LRESULT& aResult) {
4728 switch (aMsg) {
4729 case WM_SETFOCUS:
4730 case WM_KILLFOCUS:
4731 case WM_ENABLE:
4732 case WM_WINDOWPOSCHANGING:
4733 case WM_WINDOWPOSCHANGED:
4734 case WM_PARENTNOTIFY:
4735 case WM_ACTIVATEAPP:
4736 case WM_NCACTIVATE:
4737 case WM_ACTIVATE:
4738 case WM_CHILDACTIVATE:
4739 case WM_IME_SETCONTEXT:
4740 case WM_IME_NOTIFY:
4741 case WM_SHOWWINDOW:
4742 case WM_CANCELMODE:
4743 case WM_MOUSEACTIVATE:
4744 case WM_CONTEXTMENU:
4745 aResult = 0;
4746 return true;
4748 case WM_SETTINGCHANGE:
4749 case WM_SETCURSOR:
4750 return false;
4753 #ifdef DEBUG
4754 char szBuf[200];
4755 sprintf(szBuf,
4756 "An unhandled ISMEX_SEND message was received during spin loop! (%X)",
4757 aMsg);
4758 NS_WARNING(szBuf);
4759 #endif
4761 return false;
4764 void nsWindow::IPCWindowProcHandler(UINT& msg, WPARAM& wParam, LPARAM& lParam) {
4765 MOZ_ASSERT_IF(
4766 msg != WM_GETOBJECT,
4767 !mozilla::ipc::MessageChannel::IsPumpingMessages() ||
4768 mozilla::ipc::SuppressedNeuteringRegion::IsNeuteringSuppressed());
4770 // Modal UI being displayed in windowless plugins.
4771 if (mozilla::ipc::MessageChannel::IsSpinLoopActive() &&
4772 (InSendMessageEx(nullptr) & (ISMEX_REPLIED | ISMEX_SEND)) == ISMEX_SEND) {
4773 LRESULT res;
4774 if (IsAsyncResponseEvent(msg, res)) {
4775 ReplyMessage(res);
4777 return;
4780 // Handle certain sync plugin events sent to the parent which
4781 // trigger ipc calls that result in deadlocks.
4783 DWORD dwResult = 0;
4784 bool handled = false;
4786 switch (msg) {
4787 // Windowless flash sending WM_ACTIVATE events to the main window
4788 // via calls to ShowWindow.
4789 case WM_ACTIVATE:
4790 if (lParam != 0 && LOWORD(wParam) == WA_ACTIVE &&
4791 IsWindow((HWND)lParam)) {
4792 // Check for Adobe Reader X sync activate message from their
4793 // helper window and ignore. Fixes an annoying focus problem.
4794 if ((InSendMessageEx(nullptr) & (ISMEX_REPLIED | ISMEX_SEND)) ==
4795 ISMEX_SEND) {
4796 wchar_t szClass[10];
4797 HWND focusWnd = (HWND)lParam;
4798 if (IsWindowVisible(focusWnd) &&
4799 GetClassNameW(focusWnd, szClass,
4800 sizeof(szClass) / sizeof(char16_t)) &&
4801 !wcscmp(szClass, L"Edit") &&
4802 !WinUtils::IsOurProcessWindow(focusWnd)) {
4803 break;
4806 handled = true;
4808 break;
4809 // Plugins taking or losing focus triggering focus app messages.
4810 case WM_SETFOCUS:
4811 case WM_KILLFOCUS:
4812 // Windowed plugins that pass sys key events to defwndproc generate
4813 // WM_SYSCOMMAND events to the main window.
4814 case WM_SYSCOMMAND:
4815 // Windowed plugins that fire context menu selection events to parent
4816 // windows.
4817 case WM_CONTEXTMENU:
4818 // IME events fired as a result of synchronous focus changes
4819 case WM_IME_SETCONTEXT:
4820 handled = true;
4821 break;
4824 if (handled &&
4825 (InSendMessageEx(nullptr) & (ISMEX_REPLIED | ISMEX_SEND)) == ISMEX_SEND) {
4826 ReplyMessage(dwResult);
4830 /**************************************************************
4831 **************************************************************
4833 ** BLOCK: Native events
4835 ** Main Windows message handlers and OnXXX handlers for
4836 ** Windows event handling.
4838 **************************************************************
4839 **************************************************************/
4841 /**************************************************************
4843 * SECTION: Wind proc.
4845 * The main Windows event procedures and associated
4846 * message processing methods.
4848 **************************************************************/
4850 static bool DisplaySystemMenu(HWND hWnd, nsSizeMode sizeMode, bool isRtl,
4851 int32_t x, int32_t y) {
4852 HMENU hMenu = GetSystemMenu(hWnd, FALSE);
4853 if (hMenu) {
4854 MENUITEMINFO mii;
4855 mii.cbSize = sizeof(MENUITEMINFO);
4856 mii.fMask = MIIM_STATE;
4857 mii.fType = 0;
4859 // update the options
4860 mii.fState = MF_ENABLED;
4861 SetMenuItemInfo(hMenu, SC_RESTORE, FALSE, &mii);
4862 SetMenuItemInfo(hMenu, SC_SIZE, FALSE, &mii);
4863 SetMenuItemInfo(hMenu, SC_MOVE, FALSE, &mii);
4864 SetMenuItemInfo(hMenu, SC_MAXIMIZE, FALSE, &mii);
4865 SetMenuItemInfo(hMenu, SC_MINIMIZE, FALSE, &mii);
4867 mii.fState = MF_GRAYED;
4868 switch (sizeMode) {
4869 case nsSizeMode_Fullscreen:
4870 // intentional fall through
4871 case nsSizeMode_Maximized:
4872 SetMenuItemInfo(hMenu, SC_SIZE, FALSE, &mii);
4873 SetMenuItemInfo(hMenu, SC_MOVE, FALSE, &mii);
4874 SetMenuItemInfo(hMenu, SC_MAXIMIZE, FALSE, &mii);
4875 break;
4876 case nsSizeMode_Minimized:
4877 SetMenuItemInfo(hMenu, SC_MINIMIZE, FALSE, &mii);
4878 break;
4879 case nsSizeMode_Normal:
4880 SetMenuItemInfo(hMenu, SC_RESTORE, FALSE, &mii);
4881 break;
4882 case nsSizeMode_Invalid:
4883 NS_ASSERTION(false, "Did the argument come from invalid IPC?");
4884 break;
4885 default:
4886 MOZ_ASSERT_UNREACHABLE("Unhnalded nsSizeMode value detected");
4887 break;
4889 LPARAM cmd = TrackPopupMenu(
4890 hMenu,
4891 (TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_TOPALIGN |
4892 (isRtl ? TPM_RIGHTALIGN : TPM_LEFTALIGN)),
4893 x, y, 0, hWnd, nullptr);
4894 if (cmd) {
4895 PostMessage(hWnd, WM_SYSCOMMAND, cmd, 0);
4896 return true;
4899 return false;
4902 // The WndProc procedure for all nsWindows in this toolkit. This merely catches
4903 // exceptions and passes the real work to WindowProcInternal. See bug 587406
4904 // and http://msdn.microsoft.com/en-us/library/ms633573%28VS.85%29.aspx
4905 LRESULT CALLBACK nsWindow::WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
4906 LPARAM lParam) {
4907 mozilla::ipc::CancelCPOWs();
4909 BackgroundHangMonitor().NotifyActivity();
4911 return mozilla::CallWindowProcCrashProtected(WindowProcInternal, hWnd, msg,
4912 wParam, lParam);
4915 namespace geckoprofiler::markers {
4917 struct WindowProcMarker {
4918 static constexpr Span<const char> MarkerTypeName() {
4919 return MakeStringSpan("WindowProc");
4921 static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
4922 UINT aMsg, WPARAM aWParam, LPARAM aLParam) {
4923 aWriter.IntProperty("uMsg", aMsg);
4924 const char* name;
4925 if (aMsg < WM_USER) {
4926 const auto eventMsgInfo = mozilla::widget::gAllEvents.find(aMsg);
4927 if (eventMsgInfo != mozilla::widget::gAllEvents.end()) {
4928 name = eventMsgInfo->second.mStr;
4929 } else {
4930 name = "ui message";
4932 } else if (aMsg >= WM_USER && aMsg < WM_APP) {
4933 name = "WM_USER message";
4934 } else if (aMsg >= WM_APP && aMsg < 0xC000) {
4935 name = "WM_APP message";
4936 } else if (aMsg >= 0xC000 && aMsg < 0x10000) {
4937 name = "registered windows message";
4938 } else {
4939 name = "system message";
4941 aWriter.StringProperty("name", MakeStringSpan(name));
4943 if (aWParam) {
4944 aWriter.IntProperty("wParam", aWParam);
4946 if (aLParam) {
4947 aWriter.IntProperty("lParam", aLParam);
4951 static MarkerSchema MarkerTypeDisplay() {
4952 using MS = MarkerSchema;
4953 MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
4954 schema.AddKeyFormat("uMsg", MS::Format::Integer);
4955 schema.SetChartLabel("{marker.data.name} ({marker.data.uMsg})");
4956 schema.SetTableLabel(
4957 "{marker.name} - {marker.data.name} ({marker.data.uMsg})");
4958 schema.SetTooltipLabel("{marker.name} - {marker.data.name}");
4959 schema.AddKeyFormat("wParam", MS::Format::Integer);
4960 schema.AddKeyFormat("lParam", MS::Format::Integer);
4961 return schema;
4965 } // namespace geckoprofiler::markers
4967 class MOZ_RAII AutoProfilerMessageMarker {
4968 public:
4969 explicit AutoProfilerMessageMarker(HWND hWnd, UINT msg, WPARAM wParam,
4970 LPARAM lParam)
4971 : mMsg(msg), mWParam(wParam), mLParam(lParam) {
4972 if (profiler_thread_is_being_profiled_for_markers()) {
4973 mOptions.emplace(MarkerOptions(MarkerTiming::IntervalStart()));
4974 nsWindow* win = WinUtils::GetNSWindowPtr(hWnd);
4975 if (win) {
4976 nsIWidgetListener* wl = win->GetWidgetListener();
4977 if (wl) {
4978 PresShell* presShell = wl->GetPresShell();
4979 if (presShell) {
4980 Document* doc = presShell->GetDocument();
4981 if (doc) {
4982 mOptions->Set(MarkerInnerWindowId(doc->InnerWindowID()));
4990 ~AutoProfilerMessageMarker() {
4991 if (!profiler_thread_is_being_profiled_for_markers()) {
4992 return;
4995 if (mOptions) {
4996 mOptions->TimingRef().SetIntervalEnd();
4997 } else {
4998 mOptions.emplace(MarkerOptions(MarkerTiming::IntervalEnd()));
5000 profiler_add_marker("WindowProc", ::mozilla::baseprofiler::category::OTHER,
5001 std::move(*mOptions),
5002 geckoprofiler::markers::WindowProcMarker{}, mMsg,
5003 mWParam, mLParam);
5006 protected:
5007 Maybe<MarkerOptions> mOptions;
5008 UINT mMsg;
5009 WPARAM mWParam;
5010 LPARAM mLParam;
5013 LRESULT CALLBACK nsWindow::WindowProcInternal(HWND hWnd, UINT msg,
5014 WPARAM wParam, LPARAM lParam) {
5015 AutoProfilerMessageMarker marker(hWnd, msg, wParam, lParam);
5017 if (::GetWindowLongPtrW(hWnd, GWLP_ID) == eFakeTrackPointScrollableID) {
5018 // This message was sent to the FAKETRACKPOINTSCROLLABLE.
5019 if (msg == WM_HSCROLL) {
5020 // Route WM_HSCROLL messages to the main window.
5021 hWnd = ::GetParent(::GetParent(hWnd));
5022 } else {
5023 // Handle all other messages with its original window procedure.
5024 WNDPROC prevWindowProc = (WNDPROC)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
5025 return ::CallWindowProcW(prevWindowProc, hWnd, msg, wParam, lParam);
5029 if (msg == MOZ_WM_TRACE) {
5030 // This is a tracer event for measuring event loop latency.
5031 // See WidgetTraceEvent.cpp for more details.
5032 mozilla::SignalTracerThread();
5033 return 0;
5036 // Get the window which caused the event and ask it to process the message
5037 nsWindow* targetWindow = WinUtils::GetNSWindowPtr(hWnd);
5038 NS_ASSERTION(targetWindow, "nsWindow* is null!");
5039 if (!targetWindow) return ::DefWindowProcW(hWnd, msg, wParam, lParam);
5041 // Hold the window for the life of this method, in case it gets
5042 // destroyed during processing, unless we're in the dtor already.
5043 nsCOMPtr<nsIWidget> kungFuDeathGrip;
5044 if (!targetWindow->mInDtor) kungFuDeathGrip = targetWindow;
5046 targetWindow->IPCWindowProcHandler(msg, wParam, lParam);
5048 // Create this here so that we store the last rolled up popup until after
5049 // the event has been processed.
5050 nsAutoRollup autoRollup;
5052 LRESULT popupHandlingResult;
5053 if (DealWithPopups(hWnd, msg, wParam, lParam, &popupHandlingResult))
5054 return popupHandlingResult;
5056 // Call ProcessMessage
5057 LRESULT retValue;
5058 if (targetWindow->ProcessMessage(msg, wParam, lParam, &retValue)) {
5059 return retValue;
5062 LRESULT res = ::CallWindowProcW(targetWindow->GetPrevWindowProc(), hWnd, msg,
5063 wParam, lParam);
5065 return res;
5068 const char16_t* GetQuitType() {
5069 if (Preferences::GetBool(PREF_WIN_REGISTER_APPLICATION_RESTART, false)) {
5070 DWORD cchCmdLine = 0;
5071 HRESULT rc = ::GetApplicationRestartSettings(::GetCurrentProcess(), nullptr,
5072 &cchCmdLine, nullptr);
5073 if (rc == S_OK) {
5074 return u"os-restart";
5077 return nullptr;
5080 bool nsWindow::ExternalHandlerProcessMessage(UINT aMessage, WPARAM& aWParam,
5081 LPARAM& aLParam,
5082 MSGResult& aResult) {
5083 if (mWindowHook.Notify(mWnd, aMessage, aWParam, aLParam, aResult)) {
5084 return true;
5087 if (IMEHandler::ProcessMessage(this, aMessage, aWParam, aLParam, aResult)) {
5088 return true;
5091 if (MouseScrollHandler::ProcessMessage(this, aMessage, aWParam, aLParam,
5092 aResult)) {
5093 return true;
5096 return false;
5099 // The main windows message processing method. Wraps ProcessMessageInternal so
5100 // we can log aRetValue.
5101 bool nsWindow::ProcessMessage(UINT msg, WPARAM& wParam, LPARAM& lParam,
5102 LRESULT* aRetValue) {
5103 // For some events we might change the parameter values, so log
5104 // before and after we process them.
5105 PrintEvent printEvent(mWnd, msg, wParam, lParam);
5106 bool result = ProcessMessageInternal(msg, wParam, lParam, aRetValue);
5107 printEvent.SetResult(*aRetValue, result);
5109 return result;
5112 // The main windows message processing method. Called by ProcessMessage.
5113 bool nsWindow::ProcessMessageInternal(UINT msg, WPARAM& wParam, LPARAM& lParam,
5114 LRESULT* aRetValue) {
5115 MSGResult msgResult(aRetValue);
5116 if (ExternalHandlerProcessMessage(msg, wParam, lParam, msgResult)) {
5117 return (msgResult.mConsumed || !mWnd);
5120 bool result = false; // call the default nsWindow proc
5121 *aRetValue = 0;
5123 // Glass hit testing w/custom transparent margins
5124 LRESULT dwmHitResult;
5125 if (mCustomNonClient &&
5126 gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled() &&
5127 /* We don't do this for win10 glass with a custom titlebar,
5128 * in order to avoid the caption buttons breaking. */
5129 !(IsWin10OrLater() && HasGlass()) &&
5130 DwmDefWindowProc(mWnd, msg, wParam, lParam, &dwmHitResult)) {
5131 *aRetValue = dwmHitResult;
5132 return true;
5135 // The preference whether to use a different keyboard layout for each
5136 // window is cached, and updating it will not take effect until the
5137 // next restart. We read the preference here and not upon WM_ACTIVATE to make
5138 // sure that this behavior is consistent. Otherwise, if the user changed the
5139 // preference before having ever lowered the window, the preference would take
5140 // effect immediately.
5141 static const bool sSwitchKeyboardLayout =
5142 Preferences::GetBool("intl.keyboard.per_window_layout", false);
5143 static Maybe<bool> sCanQuit;
5145 // (Large blocks of code should be broken out into OnEvent handlers.)
5146 switch (msg) {
5147 // WM_QUERYENDSESSION must be handled by all windows.
5148 // Otherwise Windows thinks the window can just be killed at will.
5149 case WM_QUERYENDSESSION:
5150 if (sCanQuit.isNothing()) {
5151 // Ask if it's ok to quit, and store the answer until we
5152 // get WM_ENDSESSION signaling the round is complete.
5153 nsCOMPtr<nsIObserverService> obsServ =
5154 mozilla::services::GetObserverService();
5155 nsCOMPtr<nsISupportsPRBool> cancelQuitWrapper =
5156 do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
5157 cancelQuitWrapper->SetData(false);
5159 const char16_t* quitType = GetQuitType();
5160 obsServ->NotifyObservers(cancelQuitWrapper,
5161 "quit-application-requested", quitType);
5163 bool shouldCancelQuit;
5164 cancelQuitWrapper->GetData(&shouldCancelQuit);
5165 sCanQuit.emplace(!shouldCancelQuit);
5167 *aRetValue = *sCanQuit;
5168 result = true;
5169 break;
5171 case MOZ_WM_STARTA11Y:
5172 #if defined(ACCESSIBILITY)
5173 Unused << GetAccessible();
5174 result = true;
5175 #else
5176 result = false;
5177 #endif
5178 break;
5180 case WM_ENDSESSION:
5181 case MOZ_WM_APP_QUIT:
5182 // For WM_ENDSESSION, wParam indicates whether the session is being ended
5183 // (TRUE) or not (FALSE)
5184 if (msg == MOZ_WM_APP_QUIT || (wParam && sCanQuit.valueOr(false))) {
5185 // Let's fake a shutdown sequence without actually closing windows etc.
5186 // to avoid Windows killing us in the middle. A proper shutdown would
5187 // require having a chance to pump some messages. Unfortunately
5188 // Windows won't let us do that. Bug 212316.
5189 nsCOMPtr<nsIObserverService> obsServ =
5190 mozilla::services::GetObserverService();
5191 const char16_t* syncShutdown = u"syncShutdown";
5192 const char16_t* quitType = GetQuitType();
5194 AppShutdown::Init(AppShutdownMode::Normal, 0);
5196 obsServ->NotifyObservers(nullptr, "quit-application-granted",
5197 syncShutdown);
5198 obsServ->NotifyObservers(nullptr, "quit-application-forced", nullptr);
5200 AppShutdown::OnShutdownConfirmed();
5202 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownConfirmed,
5203 quitType);
5204 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown,
5205 nullptr);
5206 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown,
5207 nullptr);
5208 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown, nullptr);
5209 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM,
5210 nullptr);
5211 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry,
5212 nullptr);
5214 AppShutdown::DoImmediateExit();
5216 sCanQuit.reset();
5217 result = true;
5218 break;
5220 case WM_SYSCOLORCHANGE:
5221 // No need to invalidate layout for system color changes, but we need to
5222 // invalidate style.
5223 NotifyThemeChanged(widget::ThemeChangeKind::Style);
5224 break;
5226 case WM_THEMECHANGED: {
5227 // Update non-client margin offsets
5228 UpdateNonClientMargins();
5229 nsUXThemeData::UpdateNativeThemeInfo();
5231 // We assume pretty much everything could've changed here.
5232 NotifyThemeChanged(widget::ThemeChangeKind::StyleAndLayout);
5234 UpdateDarkModeToolbar();
5236 // Invalidate the window so that the repaint will
5237 // pick up the new theme.
5238 Invalidate(true, true, true);
5239 } break;
5241 case WM_WTSSESSION_CHANGE: {
5242 switch (wParam) {
5243 case WTS_CONSOLE_CONNECT:
5244 case WTS_REMOTE_CONNECT:
5245 case WTS_SESSION_UNLOCK:
5246 // When a session becomes visible, we should invalidate.
5247 Invalidate(true, true, true);
5248 break;
5249 default:
5250 break;
5252 } break;
5254 case WM_FONTCHANGE: {
5255 // We only handle this message for the hidden window,
5256 // as we only need to update the (global) font list once
5257 // for any given change, not once per window!
5258 if (mWindowType != WindowType::Invisible) {
5259 break;
5262 // update the global font list
5263 gfxPlatform::GetPlatform()->UpdateFontList();
5264 } break;
5266 case WM_SETTINGCHANGE: {
5267 if (wParam == SPI_SETCLIENTAREAANIMATION ||
5268 wParam == SPI_SETKEYBOARDDELAY) {
5269 // These need to update LookAndFeel cached values.
5270 // They affect reduced motion settings / caret blink count / and
5271 // keyboard cues, so no need to invalidate style / layout.
5272 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
5273 break;
5275 if (wParam == SPI_SETFONTSMOOTHING ||
5276 wParam == SPI_SETFONTSMOOTHINGTYPE) {
5277 gfxDWriteFont::UpdateSystemTextVars();
5278 break;
5280 if (wParam == SPI_SETWORKAREA) {
5281 // NB: We also refresh screens on WM_DISPLAYCHANGE but the rcWork
5282 // values are sometimes wrong at that point. This message then
5283 // arrives soon afterward, when we can get the right rcWork values.
5284 ScreenHelperWin::RefreshScreens();
5285 break;
5287 if (auto lParamString = reinterpret_cast<const wchar_t*>(lParam)) {
5288 if (!wcscmp(lParamString, L"ImmersiveColorSet")) {
5289 // This affects system colors (-moz-win-accentcolor), so gotta pass
5290 // the style flag.
5291 NotifyThemeChanged(widget::ThemeChangeKind::Style);
5292 break;
5295 // UserInteractionMode, ConvertibleSlateMode, SystemDockMode may cause
5296 // @media(pointer) queries to change, which layout needs to know about
5298 // (WM_SETTINGCHANGE will be sent to all top-level windows, so we
5299 // only respond to the hidden top-level window to avoid hammering
5300 // layout with a bunch of NotifyThemeChanged() calls)
5302 if (mWindowType == WindowType::Invisible) {
5303 if (!wcscmp(lParamString, L"UserInteractionMode") ||
5304 !wcscmp(lParamString, L"ConvertibleSlateMode") ||
5305 !wcscmp(lParamString, L"SystemDockMode")) {
5306 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
5307 WindowsUIUtils::UpdateInTabletMode();
5312 // SPI_GETMOUSEVANISH sends WM_SETTINGCHANGE when changed but does
5313 // not include identifiers in the parameters. WM_SETTINGCHANGE docs
5314 // recommend updating all cached settings when this message is received
5315 // anyway.
5316 GetMouseVanishSystemPref(true);
5317 } break;
5319 case WM_DEVICECHANGE: {
5320 if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
5321 DEV_BROADCAST_HDR* hdr = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam);
5322 // Check dbch_devicetype explicitly since we will get other device types
5323 // (e.g. DBT_DEVTYP_VOLUME) for some reasons even if we specify
5324 // DBT_DEVTYP_DEVICEINTERFACE in the filter for
5325 // RegisterDeviceNotification.
5326 if (hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
5327 // This can only change media queries (any-hover/any-pointer).
5328 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
5331 } break;
5333 case WM_NCCALCSIZE: {
5334 // NOTE: the following block is mirrored in PreXULSkeletonUI.cpp, and
5335 // will need to be kept in sync.
5336 if (mCustomNonClient) {
5337 // If `wParam` is `FALSE`, `lParam` points to a `RECT` that contains
5338 // the proposed window rectangle for our window. During our
5339 // processing of the `WM_NCCALCSIZE` message, we are expected to
5340 // modify the `RECT` that `lParam` points to, so that its value upon
5341 // our return is the new client area. We must return 0 if `wParam`
5342 // is `FALSE`.
5344 // If `wParam` is `TRUE`, `lParam` points to a `NCCALCSIZE_PARAMS`
5345 // struct. This struct contains an array of 3 `RECT`s, the first of
5346 // which has the exact same meaning as the `RECT` that is pointed to
5347 // by `lParam` when `wParam` is `FALSE`. The remaining `RECT`s, in
5348 // conjunction with our return value, can
5349 // be used to specify portions of the source and destination window
5350 // rectangles that are valid and should be preserved. We opt not to
5351 // implement an elaborate client-area preservation technique, and
5352 // simply return 0, which means "preserve the entire old client area
5353 // and align it with the upper-left corner of our new client area".
5354 RECT* clientRect =
5355 wParam ? &(reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam))->rgrc[0]
5356 : (reinterpret_cast<RECT*>(lParam));
5357 auto margin = NonClientSizeMargin();
5358 clientRect->top += margin.top;
5359 clientRect->left += margin.left;
5360 clientRect->right -= margin.right;
5361 clientRect->bottom -= margin.bottom;
5362 // Make client rect's width and height more than 0 to
5363 // avoid problems of webrender and angle.
5364 clientRect->right = std::max(clientRect->right, clientRect->left + 1);
5365 clientRect->bottom = std::max(clientRect->bottom, clientRect->top + 1);
5367 result = true;
5368 *aRetValue = 0;
5370 break;
5373 case WM_NCHITTEST: {
5374 if (mInputRegion.mFullyTransparent) {
5375 // Treat this window as transparent.
5376 *aRetValue = HTTRANSPARENT;
5377 result = true;
5378 break;
5381 if (mInputRegion.mMargin) {
5382 const LayoutDeviceIntPoint screenPoint(GET_X_LPARAM(lParam),
5383 GET_Y_LPARAM(lParam));
5384 LayoutDeviceIntRect screenRect = GetScreenBounds();
5385 screenRect.Deflate(mInputRegion.mMargin);
5386 if (!screenRect.Contains(screenPoint)) {
5387 *aRetValue = HTTRANSPARENT;
5388 result = true;
5389 break;
5394 * If an nc client area margin has been moved, we are responsible
5395 * for calculating where the resize margins are and returning the
5396 * appropriate set of hit test constants. DwmDefWindowProc (above)
5397 * will handle hit testing on it's command buttons if we are on a
5398 * composited desktop.
5401 if (!mCustomNonClient) {
5402 break;
5405 *aRetValue =
5406 ClientMarginHitTestPoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
5407 result = true;
5408 break;
5411 case WM_SETTEXT:
5413 * WM_SETTEXT paints the titlebar area. Avoid this if we have a
5414 * custom titlebar we paint ourselves, or if we're the ones
5415 * sending the message with an updated title
5418 if ((mSendingSetText &&
5419 gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) ||
5420 !mCustomNonClient || mNonClientMargins.top == -1)
5421 break;
5424 // From msdn, the way around this is to disable the visible state
5425 // temporarily. We need the text to be set but we don't want the
5426 // redraw to occur. However, we need to make sure that we don't
5427 // do this at the same time that a Present is happening.
5429 // To do this we take mPresentLock in nsWindow::PreRender and
5430 // if that lock is taken we wait before doing WM_SETTEXT
5431 if (mCompositorWidgetDelegate) {
5432 mCompositorWidgetDelegate->EnterPresentLock();
5434 DWORD style = GetWindowLong(mWnd, GWL_STYLE);
5435 SetWindowLong(mWnd, GWL_STYLE, style & ~WS_VISIBLE);
5436 *aRetValue =
5437 CallWindowProcW(GetPrevWindowProc(), mWnd, msg, wParam, lParam);
5438 SetWindowLong(mWnd, GWL_STYLE, style);
5439 if (mCompositorWidgetDelegate) {
5440 mCompositorWidgetDelegate->LeavePresentLock();
5443 return true;
5446 case WM_NCACTIVATE: {
5448 * WM_NCACTIVATE paints nc areas. Avoid this and re-route painting
5449 * through WM_NCPAINT via InvalidateNonClientRegion.
5451 UpdateGetWindowInfoCaptionStatus(FALSE != wParam);
5453 if (!mCustomNonClient) {
5454 break;
5457 // There is a case that rendered result is not kept. Bug 1237617
5458 if (wParam == TRUE && !gfxEnv::MOZ_DISABLE_FORCE_PRESENT() &&
5459 gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
5460 NS_DispatchToMainThread(NewRunnableMethod(
5461 "nsWindow::ForcePresent", this, &nsWindow::ForcePresent));
5464 // let the dwm handle nc painting on glass
5465 // Never allow native painting if we are on fullscreen
5466 if (mFrameState->GetSizeMode() != nsSizeMode_Fullscreen &&
5467 gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled())
5468 break;
5470 if (wParam == TRUE) {
5471 // going active
5472 *aRetValue = FALSE; // ignored
5473 result = true;
5474 // invalidate to trigger a paint
5475 InvalidateNonClientRegion();
5476 break;
5477 } else {
5478 // going inactive
5479 *aRetValue = TRUE; // go ahead and deactive
5480 result = true;
5481 // invalidate to trigger a paint
5482 InvalidateNonClientRegion();
5483 break;
5487 case WM_NCPAINT: {
5489 * ClearType changes often don't send a WM_SETTINGCHANGE message. But they
5490 * do seem to always send a WM_NCPAINT message, so let's update on that.
5492 gfxDWriteFont::UpdateSystemTextVars();
5495 * Reset the non-client paint region so that it excludes the
5496 * non-client areas we paint manually. Then call defwndproc
5497 * to do the actual painting.
5500 if (!mCustomNonClient) break;
5502 // let the dwm handle nc painting on glass
5503 if (gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) break;
5505 HRGN paintRgn = ExcludeNonClientFromPaintRegion((HRGN)wParam);
5506 LRESULT res = CallWindowProcW(GetPrevWindowProc(), mWnd, msg,
5507 (WPARAM)paintRgn, lParam);
5508 if (paintRgn != (HRGN)wParam) DeleteObject(paintRgn);
5509 *aRetValue = res;
5510 result = true;
5511 } break;
5513 case WM_POWERBROADCAST:
5514 switch (wParam) {
5515 case PBT_APMSUSPEND:
5516 PostSleepWakeNotification(true);
5517 break;
5518 case PBT_APMRESUMEAUTOMATIC:
5519 case PBT_APMRESUMECRITICAL:
5520 case PBT_APMRESUMESUSPEND:
5521 PostSleepWakeNotification(false);
5522 break;
5524 break;
5526 case WM_CLOSE: // close request
5527 if (mWidgetListener) mWidgetListener->RequestWindowClose(this);
5528 result = true; // abort window closure
5529 break;
5531 case WM_DESTROY:
5532 // clean up.
5533 DestroyLayerManager();
5534 OnDestroy();
5535 result = true;
5536 break;
5538 case WM_PAINT:
5539 *aRetValue = (int)OnPaint(nullptr, 0);
5540 result = true;
5541 break;
5543 case WM_PRINTCLIENT:
5544 result = OnPaint((HDC)wParam, 0);
5545 break;
5547 case WM_HOTKEY:
5548 result = OnHotKey(wParam, lParam);
5549 break;
5551 case WM_SYSCHAR:
5552 case WM_CHAR: {
5553 MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
5554 result = ProcessCharMessage(nativeMsg, nullptr);
5555 DispatchPendingEvents();
5556 } break;
5558 case WM_SYSKEYUP:
5559 case WM_KEYUP: {
5560 MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
5561 nativeMsg.time = ::GetMessageTime();
5562 result = ProcessKeyUpMessage(nativeMsg, nullptr);
5563 DispatchPendingEvents();
5564 } break;
5566 case WM_SYSKEYDOWN:
5567 case WM_KEYDOWN: {
5568 if (IsMouseVanishKey(wParam)) {
5569 MaybeHideCursor(true);
5572 MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
5573 result = ProcessKeyDownMessage(nativeMsg, nullptr);
5574 DispatchPendingEvents();
5575 } break;
5577 // say we've dealt with erase background if widget does
5578 // not need auto-erasing
5579 case WM_ERASEBKGND:
5580 if (!AutoErase((HDC)wParam)) {
5581 *aRetValue = 1;
5582 result = true;
5584 break;
5586 case WM_MOUSEMOVE: {
5587 MaybeHideCursor(false);
5589 LPARAM lParamScreen = lParamToScreen(lParam);
5590 mSimulatedClientArea = IsSimulatedClientArea(GET_X_LPARAM(lParamScreen),
5591 GET_Y_LPARAM(lParamScreen));
5593 if (!mMousePresent && !sIsInMouseCapture) {
5594 // First MOUSEMOVE over the client area. Ask for MOUSELEAVE
5595 TRACKMOUSEEVENT mTrack;
5596 mTrack.cbSize = sizeof(TRACKMOUSEEVENT);
5597 mTrack.dwFlags = TME_LEAVE;
5598 mTrack.dwHoverTime = 0;
5599 mTrack.hwndTrack = mWnd;
5600 TrackMouseEvent(&mTrack);
5602 mMousePresent = true;
5604 // Suppress dispatch of pending events
5605 // when mouse moves are generated by widget
5606 // creation instead of user input.
5607 POINT mp;
5608 mp.x = GET_X_LPARAM(lParamScreen);
5609 mp.y = GET_Y_LPARAM(lParamScreen);
5610 bool userMovedMouse = false;
5611 if ((sLastMouseMovePoint.x != mp.x) || (sLastMouseMovePoint.y != mp.y)) {
5612 userMovedMouse = true;
5615 if (userMovedMouse) {
5616 result = DispatchMouseEvent(
5617 eMouseMove, wParam, lParam, false, MouseButton::ePrimary,
5618 MOUSE_INPUT_SOURCE(),
5619 mPointerEvents.GetCachedPointerInfo(msg, wParam));
5620 DispatchPendingEvents();
5622 } break;
5624 case WM_NCMOUSEMOVE: {
5625 MaybeHideCursor(false);
5627 LPARAM lParamClient = lParamToClient(lParam);
5628 if (IsSimulatedClientArea(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))) {
5629 if (!sIsInMouseCapture) {
5630 TRACKMOUSEEVENT mTrack;
5631 mTrack.cbSize = sizeof(TRACKMOUSEEVENT);
5632 mTrack.dwFlags = TME_LEAVE | TME_NONCLIENT;
5633 mTrack.dwHoverTime = 0;
5634 mTrack.hwndTrack = mWnd;
5635 TrackMouseEvent(&mTrack);
5637 // If we noticed the mouse moving in our draggable region, forward the
5638 // message as a normal WM_MOUSEMOVE.
5639 SendMessage(mWnd, WM_MOUSEMOVE, 0, lParamClient);
5640 } else {
5641 // We've transitioned from a draggable area to somewhere else within
5642 // the non-client area - perhaps one of the edges of the window for
5643 // resizing.
5644 mSimulatedClientArea = false;
5647 if (mMousePresent && !sIsInMouseCapture && !mSimulatedClientArea) {
5648 SendMessage(mWnd, WM_MOUSELEAVE, 0, 0);
5650 } break;
5652 case WM_LBUTTONDOWN: {
5653 MaybeHideCursor(false);
5655 result =
5656 DispatchMouseEvent(eMouseDown, wParam, lParam, false,
5657 MouseButton::ePrimary, MOUSE_INPUT_SOURCE(),
5658 mPointerEvents.GetCachedPointerInfo(msg, wParam));
5659 DispatchPendingEvents();
5660 } break;
5662 case WM_LBUTTONUP: {
5663 result =
5664 DispatchMouseEvent(eMouseUp, wParam, lParam, false,
5665 MouseButton::ePrimary, MOUSE_INPUT_SOURCE(),
5666 mPointerEvents.GetCachedPointerInfo(msg, wParam));
5667 DispatchPendingEvents();
5668 } break;
5670 case WM_NCMOUSELEAVE: {
5671 mSimulatedClientArea = false;
5673 if (EventIsInsideWindow(this)) {
5674 // If we're handling WM_NCMOUSELEAVE and the mouse is still over the
5675 // window, then by process of elimination, the mouse has moved from the
5676 // non-client to client area, so no need to fall-through to the
5677 // WM_MOUSELEAVE handler. We also need to re-register for the
5678 // WM_MOUSELEAVE message, since according to the documentation at [1],
5679 // all tracking requested via TrackMouseEvent is cleared once
5680 // WM_NCMOUSELEAVE or WM_MOUSELEAVE fires.
5681 // [1]:
5682 // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-trackmouseevent
5683 TRACKMOUSEEVENT mTrack;
5684 mTrack.cbSize = sizeof(TRACKMOUSEEVENT);
5685 mTrack.dwFlags = TME_LEAVE;
5686 mTrack.dwHoverTime = 0;
5687 mTrack.hwndTrack = mWnd;
5688 TrackMouseEvent(&mTrack);
5689 break;
5691 // We've transitioned from non-client to outside of the window, so
5692 // fall-through to the WM_MOUSELEAVE handler.
5693 [[fallthrough]];
5695 case WM_MOUSELEAVE: {
5696 if (!mMousePresent) break;
5697 if (mSimulatedClientArea) break;
5698 mMousePresent = false;
5700 // Check if the mouse is over the fullscreen transition window, if so
5701 // clear sLastMouseMovePoint. This way the WM_MOUSEMOVE we get after the
5702 // transition window disappears will not be ignored, even if the mouse
5703 // hasn't moved.
5704 if (mTransitionWnd && WindowAtMouse() == mTransitionWnd) {
5705 sLastMouseMovePoint = {0};
5708 // We need to check mouse button states and put them in for
5709 // wParam.
5710 WPARAM mouseState = (GetKeyState(VK_LBUTTON) ? MK_LBUTTON : 0) |
5711 (GetKeyState(VK_MBUTTON) ? MK_MBUTTON : 0) |
5712 (GetKeyState(VK_RBUTTON) ? MK_RBUTTON : 0);
5713 // Synthesize an event position because we don't get one from
5714 // WM_MOUSELEAVE.
5715 LPARAM pos = lParamToClient(::GetMessagePos());
5716 DispatchMouseEvent(eMouseExitFromWidget, mouseState, pos, false,
5717 MouseButton::ePrimary, MOUSE_INPUT_SOURCE());
5718 } break;
5720 case MOZ_WM_PEN_LEAVES_HOVER_OF_DIGITIZER: {
5721 LPARAM pos = lParamToClient(::GetMessagePos());
5722 MOZ_ASSERT(InkCollector::sInkCollector);
5723 uint16_t pointerId = InkCollector::sInkCollector->GetPointerId();
5724 if (pointerId != 0) {
5725 WinPointerInfo pointerInfo;
5726 pointerInfo.pointerId = pointerId;
5727 DispatchMouseEvent(eMouseExitFromWidget, wParam, pos, false,
5728 MouseButton::ePrimary,
5729 MouseEvent_Binding::MOZ_SOURCE_PEN, &pointerInfo);
5730 InkCollector::sInkCollector->ClearTarget();
5731 InkCollector::sInkCollector->ClearPointerId();
5733 } break;
5735 case WM_CONTEXTMENU: {
5736 // If the context menu is brought up by a touch long-press, then
5737 // the APZ code is responsible for dealing with this, so we don't
5738 // need to do anything.
5739 if (mTouchWindow &&
5740 MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_TOUCH) {
5741 MOZ_ASSERT(mAPZC); // since mTouchWindow is true, APZ must be enabled
5742 result = true;
5743 break;
5746 // If this WM_CONTEXTMENU is triggered by a mouse's secondary button up
5747 // event in overscroll gutter, we shouldn't open context menu.
5748 if (MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_MOUSE &&
5749 mNeedsToPreventContextMenu) {
5750 result = true;
5751 break;
5754 // if the context menu is brought up from the keyboard, |lParam|
5755 // will be -1.
5756 LPARAM pos;
5757 bool contextMenukey = false;
5758 if (lParam == -1) {
5759 contextMenukey = true;
5760 pos = lParamToClient(GetMessagePos());
5761 } else {
5762 pos = lParamToClient(lParam);
5765 result = DispatchMouseEvent(
5766 eContextMenu, wParam, pos, contextMenukey,
5767 contextMenukey ? MouseButton::ePrimary : MouseButton::eSecondary,
5768 MOUSE_INPUT_SOURCE());
5769 if (lParam != -1 && !result && mCustomNonClient &&
5770 mDraggableRegion.Contains(GET_X_LPARAM(pos), GET_Y_LPARAM(pos))) {
5771 // Blank area hit, throw up the system menu.
5772 DisplaySystemMenu(mWnd, mFrameState->GetSizeMode(), mIsRTL,
5773 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
5774 result = true;
5776 } break;
5778 case WM_POINTERLEAVE:
5779 case WM_POINTERDOWN:
5780 case WM_POINTERUP:
5781 case WM_POINTERUPDATE:
5782 MaybeHideCursor(false);
5783 result = OnPointerEvents(msg, wParam, lParam);
5784 if (result) {
5785 DispatchPendingEvents();
5787 break;
5789 case DM_POINTERHITTEST:
5790 if (mDmOwner) {
5791 UINT contactId = GET_POINTERID_WPARAM(wParam);
5792 POINTER_INPUT_TYPE pointerType;
5793 if (mPointerEvents.GetPointerType(contactId, &pointerType) &&
5794 pointerType == PT_TOUCHPAD) {
5795 mDmOwner->SetContact(contactId);
5798 break;
5800 case WM_LBUTTONDBLCLK:
5801 MaybeHideCursor(false);
5802 result = DispatchMouseEvent(eMouseDoubleClick, wParam, lParam, false,
5803 MouseButton::ePrimary, MOUSE_INPUT_SOURCE());
5804 DispatchPendingEvents();
5805 break;
5807 case WM_MBUTTONDOWN:
5808 MaybeHideCursor(false);
5809 result = DispatchMouseEvent(eMouseDown, wParam, lParam, false,
5810 MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5811 DispatchPendingEvents();
5812 break;
5814 case WM_MBUTTONUP:
5815 result = DispatchMouseEvent(eMouseUp, wParam, lParam, false,
5816 MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5817 DispatchPendingEvents();
5818 break;
5820 case WM_MBUTTONDBLCLK:
5821 MaybeHideCursor(false);
5822 result = DispatchMouseEvent(eMouseDoubleClick, wParam, lParam, false,
5823 MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5824 DispatchPendingEvents();
5825 break;
5827 case WM_NCMBUTTONDOWN:
5828 MaybeHideCursor(false);
5829 result = DispatchMouseEvent(eMouseDown, 0, lParamToClient(lParam), false,
5830 MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5831 DispatchPendingEvents();
5832 break;
5834 case WM_NCMBUTTONUP:
5835 result = DispatchMouseEvent(eMouseUp, 0, lParamToClient(lParam), false,
5836 MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5837 DispatchPendingEvents();
5838 break;
5840 case WM_NCMBUTTONDBLCLK:
5841 MaybeHideCursor(false);
5842 result =
5843 DispatchMouseEvent(eMouseDoubleClick, 0, lParamToClient(lParam),
5844 false, MouseButton::eMiddle, MOUSE_INPUT_SOURCE());
5845 DispatchPendingEvents();
5846 break;
5848 case WM_RBUTTONDOWN:
5849 MaybeHideCursor(false);
5850 result =
5851 DispatchMouseEvent(eMouseDown, wParam, lParam, false,
5852 MouseButton::eSecondary, MOUSE_INPUT_SOURCE(),
5853 mPointerEvents.GetCachedPointerInfo(msg, wParam));
5854 DispatchPendingEvents();
5855 break;
5857 case WM_RBUTTONUP:
5858 result =
5859 DispatchMouseEvent(eMouseUp, wParam, lParam, false,
5860 MouseButton::eSecondary, MOUSE_INPUT_SOURCE(),
5861 mPointerEvents.GetCachedPointerInfo(msg, wParam));
5862 DispatchPendingEvents();
5863 break;
5865 case WM_RBUTTONDBLCLK:
5866 MaybeHideCursor(false);
5867 result =
5868 DispatchMouseEvent(eMouseDoubleClick, wParam, lParam, false,
5869 MouseButton::eSecondary, MOUSE_INPUT_SOURCE());
5870 DispatchPendingEvents();
5871 break;
5873 case WM_NCRBUTTONDOWN:
5874 MaybeHideCursor(false);
5875 result =
5876 DispatchMouseEvent(eMouseDown, 0, lParamToClient(lParam), false,
5877 MouseButton::eSecondary, MOUSE_INPUT_SOURCE());
5878 DispatchPendingEvents();
5879 break;
5881 case WM_NCRBUTTONUP:
5882 result =
5883 DispatchMouseEvent(eMouseUp, 0, lParamToClient(lParam), false,
5884 MouseButton::eSecondary, MOUSE_INPUT_SOURCE());
5885 DispatchPendingEvents();
5886 break;
5888 case WM_NCRBUTTONDBLCLK:
5889 MaybeHideCursor(false);
5890 result = DispatchMouseEvent(eMouseDoubleClick, 0, lParamToClient(lParam),
5891 false, MouseButton::eSecondary,
5892 MOUSE_INPUT_SOURCE());
5893 DispatchPendingEvents();
5894 break;
5896 // Windows doesn't provide to customize the behavior of 4th nor 5th button
5897 // of mouse. If 5-button mouse works with standard mouse deriver of
5898 // Windows, users cannot disable 4th button (browser back) nor 5th button
5899 // (browser forward). We should allow to do it with our prefs since we can
5900 // prevent Windows to generate WM_APPCOMMAND message if WM_XBUTTONUP
5901 // messages are not sent to DefWindowProc.
5902 case WM_XBUTTONDOWN:
5903 case WM_XBUTTONUP:
5904 case WM_NCXBUTTONDOWN:
5905 case WM_NCXBUTTONUP:
5906 MaybeHideCursor(false);
5908 *aRetValue = TRUE;
5909 switch (GET_XBUTTON_WPARAM(wParam)) {
5910 case XBUTTON1:
5911 result = !Preferences::GetBool("mousebutton.4th.enabled", true);
5912 break;
5913 case XBUTTON2:
5914 result = !Preferences::GetBool("mousebutton.5th.enabled", true);
5915 break;
5916 default:
5917 break;
5919 break;
5921 case WM_SIZING: {
5922 if (mAspectRatio > 0) {
5923 LPRECT rect = (LPRECT)lParam;
5924 int32_t newWidth, newHeight;
5926 // The following conditions and switch statement borrow heavily from the
5927 // Chromium source code from
5928 // https://chromium.googlesource.com/chromium/src/+/456d6e533cfb4531995e0ef52c279d4b5aa8a352/ui/views/window/window_resize_utils.cc#45
5929 if (wParam == WMSZ_LEFT || wParam == WMSZ_RIGHT ||
5930 wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT) {
5931 newWidth = rect->right - rect->left;
5932 newHeight = newWidth / mAspectRatio;
5933 if (newHeight < mSizeConstraints.mMinSize.height) {
5934 newHeight = mSizeConstraints.mMinSize.height;
5935 newWidth = newHeight * mAspectRatio;
5936 } else if (newHeight > mSizeConstraints.mMaxSize.height) {
5937 newHeight = mSizeConstraints.mMaxSize.height;
5938 newWidth = newHeight * mAspectRatio;
5940 } else {
5941 newHeight = rect->bottom - rect->top;
5942 newWidth = newHeight * mAspectRatio;
5943 if (newWidth < mSizeConstraints.mMinSize.width) {
5944 newWidth = mSizeConstraints.mMinSize.width;
5945 newHeight = newWidth / mAspectRatio;
5946 } else if (newWidth > mSizeConstraints.mMaxSize.width) {
5947 newWidth = mSizeConstraints.mMaxSize.width;
5948 newHeight = newWidth / mAspectRatio;
5952 switch (wParam) {
5953 case WMSZ_RIGHT:
5954 case WMSZ_BOTTOM:
5955 rect->right = newWidth + rect->left;
5956 rect->bottom = rect->top + newHeight;
5957 break;
5958 case WMSZ_TOP:
5959 rect->right = newWidth + rect->left;
5960 rect->top = rect->bottom - newHeight;
5961 break;
5962 case WMSZ_LEFT:
5963 case WMSZ_TOPLEFT:
5964 rect->left = rect->right - newWidth;
5965 rect->top = rect->bottom - newHeight;
5966 break;
5967 case WMSZ_TOPRIGHT:
5968 rect->right = rect->left + newWidth;
5969 rect->top = rect->bottom - newHeight;
5970 break;
5971 case WMSZ_BOTTOMLEFT:
5972 rect->left = rect->right - newWidth;
5973 rect->bottom = rect->top + newHeight;
5974 break;
5975 case WMSZ_BOTTOMRIGHT:
5976 rect->right = rect->left + newWidth;
5977 rect->bottom = rect->top + newHeight;
5978 break;
5982 // When we get WM_ENTERSIZEMOVE we don't know yet if we're in a live
5983 // resize or move event. Instead we wait for first VM_SIZING message
5984 // within a ENTERSIZEMOVE to consider this a live resize event.
5985 if (mResizeState == IN_SIZEMOVE) {
5986 mResizeState = RESIZING;
5987 NotifyLiveResizeStarted();
5989 break;
5992 case WM_MOVING:
5993 FinishLiveResizing(MOVING);
5994 if (WinUtils::IsPerMonitorDPIAware()) {
5995 // Sometimes, we appear to miss a WM_DPICHANGED message while moving
5996 // a window around. Therefore, call ChangedDPI and ResetLayout here
5997 // if it appears that the window's scaling is not what we expect.
5998 // This causes the prescontext and appshell window management code to
5999 // check the appUnitsPerDevPixel value and current widget size, and
6000 // refresh them if necessary. If nothing has changed, these calls will
6001 // return without actually triggering any extra reflow or painting.
6002 if (WinUtils::LogToPhysFactor(mWnd) != mDefaultScale) {
6003 ChangedDPI();
6004 ResetLayout();
6005 if (mWidgetListener) {
6006 mWidgetListener->UIResolutionChanged();
6010 break;
6012 case WM_ENTERSIZEMOVE: {
6013 if (mResizeState == NOT_RESIZING) {
6014 mResizeState = IN_SIZEMOVE;
6016 break;
6019 case WM_EXITSIZEMOVE: {
6020 FinishLiveResizing(NOT_RESIZING);
6022 if (!sIsInMouseCapture) {
6023 NotifySizeMoveDone();
6026 break;
6029 case WM_DISPLAYCHANGE: {
6030 ScreenHelperWin::RefreshScreens();
6031 if (mWidgetListener) {
6032 mWidgetListener->UIResolutionChanged();
6034 break;
6037 case WM_NCLBUTTONDBLCLK:
6038 DispatchMouseEvent(eMouseDoubleClick, 0, lParamToClient(lParam), false,
6039 MouseButton::ePrimary, MOUSE_INPUT_SOURCE());
6040 result = DispatchMouseEvent(eMouseUp, 0, lParamToClient(lParam), false,
6041 MouseButton::ePrimary, MOUSE_INPUT_SOURCE());
6042 DispatchPendingEvents();
6043 break;
6045 case WM_NCLBUTTONDOWN: {
6046 // Dispatch a custom event when this happens in the draggable region, so
6047 // that non-popup-based panels can react to it. This doesn't send an
6048 // actual mousedown event because that would break dragging or interfere
6049 // with other mousedown handling in the caption area.
6050 if (ClientMarginHitTestPoint(GET_X_LPARAM(lParam),
6051 GET_Y_LPARAM(lParam)) == HTCAPTION) {
6052 DispatchCustomEvent(u"draggableregionleftmousedown"_ns);
6055 if (IsWindowButton(wParam) && mCustomNonClient && !mWindowButtonsRect) {
6056 DispatchMouseEvent(eMouseDown, wParamFromGlobalMouseState(),
6057 lParamToClient(lParam), false, MouseButton::ePrimary,
6058 MOUSE_INPUT_SOURCE(), nullptr, true);
6059 DispatchPendingEvents();
6060 result = true;
6062 break;
6065 case WM_APPCOMMAND: {
6066 MSG nativeMsg = WinUtils::InitMSG(msg, wParam, lParam, mWnd);
6067 result = HandleAppCommandMsg(nativeMsg, aRetValue);
6068 break;
6071 // The WM_ACTIVATE event is fired when a window is raised or lowered,
6072 // and the loword of wParam specifies which. But we don't want to tell
6073 // the focus system about this until the WM_SETFOCUS or WM_KILLFOCUS
6074 // events are fired. Instead, set either the sJustGotActivate or
6075 // gJustGotDeactivate flags and activate/deactivate once the focus
6076 // events arrive.
6077 case WM_ACTIVATE: {
6078 int32_t fActive = LOWORD(wParam);
6079 if (!fActive) {
6080 MaybeHideCursor(false);
6083 if (mWidgetListener) {
6084 if (WA_INACTIVE == fActive) {
6085 // when minimizing a window, the deactivation and focus events will
6086 // be fired in the reverse order. Instead, just deactivate right away.
6087 // This can also happen when a modal system dialog is opened, so check
6088 // if the last window to receive the WM_KILLFOCUS message was this one
6089 // or a child of this one.
6090 if (HIWORD(wParam) ||
6091 (mLastKillFocusWindow &&
6092 (GetTopLevelForFocus(mLastKillFocusWindow) == mWnd))) {
6093 DispatchFocusToTopLevelWindow(false);
6094 } else {
6095 sJustGotDeactivate = true;
6097 if (mIsTopWidgetWindow) {
6098 mLastKeyboardLayout = KeyboardLayout::GetInstance()->GetLayout();
6100 } else {
6101 StopFlashing();
6103 sJustGotActivate = true;
6104 WidgetMouseEvent event(true, eMouseActivate, this,
6105 WidgetMouseEvent::eReal);
6106 InitEvent(event);
6107 ModifierKeyState modifierKeyState;
6108 modifierKeyState.InitInputEvent(event);
6109 DispatchInputEvent(&event);
6110 if (sSwitchKeyboardLayout && mLastKeyboardLayout)
6111 ActivateKeyboardLayout(mLastKeyboardLayout, 0);
6114 } break;
6116 case WM_ACTIVATEAPP: {
6117 GPUProcessManager::Get()->SetAppInForeground(wParam);
6118 } break;
6120 case WM_MOUSEACTIVATE:
6121 // A popup with a parent owner should not be activated when clicked but
6122 // should still allow the mouse event to be fired, so the return value
6123 // is set to MA_NOACTIVATE. But if the owner isn't the frontmost window,
6124 // just use default processing so that the window is activated.
6125 if (IsPopup() && IsOwnerForegroundWindow()) {
6126 *aRetValue = MA_NOACTIVATE;
6127 result = true;
6129 break;
6131 case WM_WINDOWPOSCHANGING: {
6132 LPWINDOWPOS info = (LPWINDOWPOS)lParam;
6133 OnWindowPosChanging(info);
6134 result = true;
6135 } break;
6137 case WM_GETMINMAXINFO: {
6138 MINMAXINFO* mmi = (MINMAXINFO*)lParam;
6139 // Set the constraints. The minimum size should also be constrained to the
6140 // default window maximum size so that it fits on screen.
6141 mmi->ptMinTrackSize.x =
6142 std::min((int32_t)mmi->ptMaxTrackSize.x,
6143 std::max((int32_t)mmi->ptMinTrackSize.x,
6144 mSizeConstraints.mMinSize.width));
6145 mmi->ptMinTrackSize.y =
6146 std::min((int32_t)mmi->ptMaxTrackSize.y,
6147 std::max((int32_t)mmi->ptMinTrackSize.y,
6148 mSizeConstraints.mMinSize.height));
6149 mmi->ptMaxTrackSize.x = std::min((int32_t)mmi->ptMaxTrackSize.x,
6150 mSizeConstraints.mMaxSize.width);
6151 mmi->ptMaxTrackSize.y = std::min((int32_t)mmi->ptMaxTrackSize.y,
6152 mSizeConstraints.mMaxSize.height);
6153 } break;
6155 case WM_SETFOCUS:
6156 // If previous focused window isn't ours, it must have received the
6157 // redirected message. So, we should forget it.
6158 if (!WinUtils::IsOurProcessWindow(HWND(wParam))) {
6159 RedirectedKeyDownMessageManager::Forget();
6161 if (sJustGotActivate) {
6162 DispatchFocusToTopLevelWindow(true);
6164 TaskbarConcealer::OnFocusAcquired(this);
6165 break;
6167 case WM_KILLFOCUS:
6168 if (sJustGotDeactivate) {
6169 DispatchFocusToTopLevelWindow(false);
6170 } else {
6171 mLastKillFocusWindow = mWnd;
6173 break;
6175 case WM_WINDOWPOSCHANGED: {
6176 WINDOWPOS* wp = (LPWINDOWPOS)lParam;
6177 OnWindowPosChanged(wp);
6178 TaskbarConcealer::OnWindowPosChanged(this);
6179 result = true;
6180 } break;
6182 case WM_INPUTLANGCHANGEREQUEST:
6183 *aRetValue = TRUE;
6184 result = false;
6185 break;
6187 case WM_INPUTLANGCHANGE:
6188 KeyboardLayout::GetInstance()->OnLayoutChange(
6189 reinterpret_cast<HKL>(lParam));
6190 nsBidiKeyboard::OnLayoutChange();
6191 result = false; // always pass to child window
6192 break;
6194 case WM_DESTROYCLIPBOARD: {
6195 nsIClipboard* clipboard;
6196 nsresult rv = CallGetService(kCClipboardCID, &clipboard);
6197 if (NS_SUCCEEDED(rv)) {
6198 clipboard->EmptyClipboard(nsIClipboard::kGlobalClipboard);
6199 NS_RELEASE(clipboard);
6201 } break;
6203 #ifdef ACCESSIBILITY
6204 case WM_GETOBJECT: {
6205 *aRetValue = 0;
6206 // Do explicit casting to make it working on 64bit systems (see bug 649236
6207 // for details).
6208 int32_t objId = static_cast<DWORD>(lParam);
6209 if (objId == OBJID_CLIENT) { // oleacc.dll will be loaded dynamically
6210 RefPtr<IAccessible> root(
6211 a11y::LazyInstantiator::GetRootAccessible(mWnd));
6212 if (root) {
6213 *aRetValue = LresultFromObject(IID_IAccessible, wParam, root);
6214 a11y::LazyInstantiator::EnableBlindAggregation(mWnd);
6215 result = true;
6218 } break;
6219 #endif
6221 case WM_SYSCOMMAND: {
6222 WPARAM filteredWParam = (wParam & 0xFFF0);
6223 if (mFrameState->GetSizeMode() == nsSizeMode_Fullscreen &&
6224 filteredWParam == SC_RESTORE &&
6225 GetCurrentShowCmd(mWnd) != SW_SHOWMINIMIZED) {
6226 mFrameState->EnsureFullscreenMode(false);
6227 result = true;
6230 // Handle the system menu manually when we're in full screen mode
6231 // so we can set the appropriate options.
6232 if (filteredWParam == SC_KEYMENU && lParam == VK_SPACE &&
6233 mFrameState->GetSizeMode() == nsSizeMode_Fullscreen) {
6234 DisplaySystemMenu(mWnd, mFrameState->GetSizeMode(), mIsRTL,
6235 MOZ_SYSCONTEXT_X_POS, MOZ_SYSCONTEXT_Y_POS);
6236 result = true;
6238 } break;
6240 case WM_DWMCOMPOSITIONCHANGED:
6241 // Every window will get this message, but gfxVars only broadcasts
6242 // updates when the value actually changes
6243 if (XRE_IsParentProcess()) {
6244 BOOL dwmEnabled = FALSE;
6245 if (FAILED(::DwmIsCompositionEnabled(&dwmEnabled)) || !dwmEnabled) {
6246 gfxVars::SetDwmCompositionEnabled(false);
6247 } else {
6248 gfxVars::SetDwmCompositionEnabled(true);
6252 UpdateNonClientMargins();
6253 BroadcastMsg(mWnd, WM_DWMCOMPOSITIONCHANGED);
6254 // TODO: Why is NotifyThemeChanged needed, what does it affect? And can we
6255 // make it more granular by tweaking the ChangeKind we pass?
6256 NotifyThemeChanged(widget::ThemeChangeKind::StyleAndLayout);
6257 UpdateGlass();
6258 Invalidate(true, true, true);
6259 break;
6261 case WM_DPICHANGED: {
6262 LPRECT rect = (LPRECT)lParam;
6263 OnDPIChanged(rect->left, rect->top, rect->right - rect->left,
6264 rect->bottom - rect->top);
6265 break;
6268 /* Gesture support events */
6269 case WM_TABLET_QUERYSYSTEMGESTURESTATUS:
6270 // According to MS samples, this must be handled to enable
6271 // rotational support in multi-touch drivers.
6272 result = true;
6273 *aRetValue = TABLET_ROTATE_GESTURE_ENABLE;
6274 break;
6276 case WM_TOUCH:
6277 result = OnTouch(wParam, lParam);
6278 if (result) {
6279 *aRetValue = 0;
6281 break;
6283 case WM_GESTURE:
6284 result = OnGesture(wParam, lParam);
6285 break;
6287 case WM_GESTURENOTIFY: {
6288 if (mWindowType != WindowType::Invisible) {
6289 // A GestureNotify event is dispatched to decide which single-finger
6290 // panning direction should be active (including none) and if pan
6291 // feedback should be displayed. Java and plugin windows can make their
6292 // own calls.
6294 GESTURENOTIFYSTRUCT* gestureinfo = (GESTURENOTIFYSTRUCT*)lParam;
6295 nsPointWin touchPoint;
6296 touchPoint = gestureinfo->ptsLocation;
6297 touchPoint.ScreenToClient(mWnd);
6298 WidgetGestureNotifyEvent gestureNotifyEvent(true, eGestureNotify, this);
6299 gestureNotifyEvent.mRefPoint =
6300 LayoutDeviceIntPoint::FromUnknownPoint(touchPoint);
6301 nsEventStatus status;
6302 DispatchEvent(&gestureNotifyEvent, status);
6303 mDisplayPanFeedback = gestureNotifyEvent.mDisplayPanFeedback;
6304 if (!mTouchWindow)
6305 mGesture.SetWinGestureSupport(mWnd, gestureNotifyEvent.mPanDirection);
6307 result = false; // should always bubble to DefWindowProc
6308 } break;
6310 case WM_CLEAR: {
6311 WidgetContentCommandEvent command(true, eContentCommandDelete, this);
6312 DispatchWindowEvent(command);
6313 result = true;
6314 } break;
6316 case WM_CUT: {
6317 WidgetContentCommandEvent command(true, eContentCommandCut, this);
6318 DispatchWindowEvent(command);
6319 result = true;
6320 } break;
6322 case WM_COPY: {
6323 WidgetContentCommandEvent command(true, eContentCommandCopy, this);
6324 DispatchWindowEvent(command);
6325 result = true;
6326 } break;
6328 case WM_PASTE: {
6329 WidgetContentCommandEvent command(true, eContentCommandPaste, this);
6330 DispatchWindowEvent(command);
6331 result = true;
6332 } break;
6334 case EM_UNDO: {
6335 WidgetContentCommandEvent command(true, eContentCommandUndo, this);
6336 DispatchWindowEvent(command);
6337 *aRetValue = (LRESULT)(command.mSucceeded && command.mIsEnabled);
6338 result = true;
6339 } break;
6341 case EM_REDO: {
6342 WidgetContentCommandEvent command(true, eContentCommandRedo, this);
6343 DispatchWindowEvent(command);
6344 *aRetValue = (LRESULT)(command.mSucceeded && command.mIsEnabled);
6345 result = true;
6346 } break;
6348 case EM_CANPASTE: {
6349 // Support EM_CANPASTE message only when wParam isn't specified or
6350 // is plain text format.
6351 if (wParam == 0 || wParam == CF_TEXT || wParam == CF_UNICODETEXT) {
6352 WidgetContentCommandEvent command(true, eContentCommandPaste, this,
6353 true);
6354 DispatchWindowEvent(command);
6355 *aRetValue = (LRESULT)(command.mSucceeded && command.mIsEnabled);
6356 result = true;
6358 } break;
6360 case EM_CANUNDO: {
6361 WidgetContentCommandEvent command(true, eContentCommandUndo, this, true);
6362 DispatchWindowEvent(command);
6363 *aRetValue = (LRESULT)(command.mSucceeded && command.mIsEnabled);
6364 result = true;
6365 } break;
6367 case EM_CANREDO: {
6368 WidgetContentCommandEvent command(true, eContentCommandRedo, this, true);
6369 DispatchWindowEvent(command);
6370 *aRetValue = (LRESULT)(command.mSucceeded && command.mIsEnabled);
6371 result = true;
6372 } break;
6374 case MOZ_WM_SKEWFIX: {
6375 TimeStamp skewStamp;
6376 if (CurrentWindowsTimeGetter::GetAndClearBackwardsSkewStamp(wParam,
6377 &skewStamp)) {
6378 TimeConverter().CompensateForBackwardsSkew(::GetMessageTime(),
6379 skewStamp);
6381 } break;
6383 default: {
6384 if (msg == nsAppShell::GetTaskbarButtonCreatedMessage()) {
6385 SetHasTaskbarIconBeenCreated();
6387 } break;
6390 //*aRetValue = result;
6391 if (mWnd) {
6392 return result;
6393 } else {
6394 // Events which caused mWnd destruction and aren't consumed
6395 // will crash during the Windows default processing.
6396 return true;
6400 void nsWindow::FinishLiveResizing(ResizeState aNewState) {
6401 if (mResizeState == RESIZING) {
6402 NotifyLiveResizeStopped();
6404 mResizeState = aNewState;
6405 ForcePresent();
6408 /**************************************************************
6410 * SECTION: Broadcast messaging
6412 * Broadcast messages to all windows.
6414 **************************************************************/
6416 // Enumerate all child windows sending aMsg to each of them
6417 BOOL CALLBACK nsWindow::BroadcastMsgToChildren(HWND aWnd, LPARAM aMsg) {
6418 WNDPROC winProc = (WNDPROC)::GetWindowLongPtrW(aWnd, GWLP_WNDPROC);
6419 if (winProc == &nsWindow::WindowProc) {
6420 // it's one of our windows so go ahead and send a message to it
6421 ::CallWindowProcW(winProc, aWnd, aMsg, 0, 0);
6423 return TRUE;
6426 // Enumerate all top level windows specifying that the children of each
6427 // top level window should be enumerated. Do *not* send the message to
6428 // each top level window since it is assumed that the toolkit will send
6429 // aMsg to them directly.
6430 BOOL CALLBACK nsWindow::BroadcastMsg(HWND aTopWindow, LPARAM aMsg) {
6431 // Iterate each of aTopWindows child windows sending the aMsg
6432 // to each of them.
6433 ::EnumChildWindows(aTopWindow, nsWindow::BroadcastMsgToChildren, aMsg);
6434 return TRUE;
6437 /**************************************************************
6439 * SECTION: Event processing helpers
6441 * Special processing for certain event types and
6442 * synthesized events.
6444 **************************************************************/
6446 LayoutDeviceIntMargin nsWindow::NonClientSizeMargin(
6447 const LayoutDeviceIntMargin& aNonClientOffset) const {
6448 return LayoutDeviceIntMargin(mCaptionHeight - aNonClientOffset.top,
6449 mHorResizeMargin - aNonClientOffset.right,
6450 mVertResizeMargin - aNonClientOffset.bottom,
6451 mHorResizeMargin - aNonClientOffset.left);
6454 int32_t nsWindow::ClientMarginHitTestPoint(int32_t aX, int32_t aY) {
6455 const nsSizeMode sizeMode = mFrameState->GetSizeMode();
6456 if (sizeMode == nsSizeMode_Minimized || sizeMode == nsSizeMode_Fullscreen) {
6457 return HTCLIENT;
6460 // Calculations are done in screen coords
6461 const LayoutDeviceIntRect winRect = GetScreenBounds();
6462 const LayoutDeviceIntPoint point(aX, aY);
6464 // hit return constants:
6465 // HTBORDER - non-resizable border
6466 // HTBOTTOM, HTLEFT, HTRIGHT, HTTOP - resizable border
6467 // HTBOTTOMLEFT, HTBOTTOMRIGHT - resizable corner
6468 // HTTOPLEFT, HTTOPRIGHT - resizable corner
6469 // HTCAPTION - general title bar area
6470 // HTCLIENT - area considered the client
6471 // HTCLOSE - hovering over the close button
6472 // HTMAXBUTTON - maximize button
6473 // HTMINBUTTON - minimize button
6475 int32_t testResult = HTCLIENT;
6476 const bool isResizable =
6477 sizeMode != nsSizeMode_Maximized &&
6478 (mBorderStyle &
6479 (BorderStyle::All | BorderStyle::ResizeH | BorderStyle::Default));
6481 LayoutDeviceIntMargin nonClientSizeMargin = NonClientSizeMargin();
6483 // Ensure being accessible to borders of window. Even if contents are in
6484 // this area, the area must behave as border.
6485 nonClientSizeMargin.EnsureAtLeast(
6486 LayoutDeviceIntMargin(kResizableBorderMinSize, kResizableBorderMinSize,
6487 kResizableBorderMinSize, kResizableBorderMinSize));
6489 LayoutDeviceIntRect clientRect = winRect;
6490 clientRect.Deflate(nonClientSizeMargin);
6492 const bool allowContentOverride =
6493 sizeMode == nsSizeMode_Maximized || clientRect.Contains(point);
6495 // The border size. If there is no content under mouse cursor, the border
6496 // size should be larger than the values in system settings. Otherwise,
6497 // contents under the mouse cursor should be able to override the behavior.
6498 // E.g., user must expect that Firefox button always opens the popup menu
6499 // even when the user clicks on the above edge of it.
6500 LayoutDeviceIntMargin borderSize = nonClientSizeMargin;
6501 borderSize.EnsureAtLeast(
6502 LayoutDeviceIntMargin(mVertResizeMargin, mHorResizeMargin,
6503 mVertResizeMargin, mHorResizeMargin));
6505 bool top = false;
6506 bool bottom = false;
6507 bool left = false;
6508 bool right = false;
6510 if (point.y >= winRect.y && point.y < winRect.y + borderSize.top) {
6511 top = true;
6512 } else if (point.y <= winRect.YMost() &&
6513 point.y > winRect.YMost() - borderSize.bottom) {
6514 bottom = true;
6517 // (the 2x case here doubles the resize area for corners)
6518 int multiplier = (top || bottom) ? 2 : 1;
6519 if (point.x >= winRect.x &&
6520 point.x < winRect.x + (multiplier * borderSize.left)) {
6521 left = true;
6522 } else if (point.x <= winRect.XMost() &&
6523 point.x > winRect.XMost() - (multiplier * borderSize.right)) {
6524 right = true;
6527 bool inResizeRegion = false;
6528 if (isResizable) {
6529 if (top) {
6530 testResult = HTTOP;
6531 if (left) {
6532 testResult = HTTOPLEFT;
6533 } else if (right) {
6534 testResult = HTTOPRIGHT;
6536 } else if (bottom) {
6537 testResult = HTBOTTOM;
6538 if (left) {
6539 testResult = HTBOTTOMLEFT;
6540 } else if (right) {
6541 testResult = HTBOTTOMRIGHT;
6543 } else {
6544 if (left) {
6545 testResult = HTLEFT;
6547 if (right) {
6548 testResult = HTRIGHT;
6551 inResizeRegion = (testResult != HTCLIENT);
6552 } else {
6553 if (top) {
6554 testResult = HTCAPTION;
6555 } else if (bottom || left || right) {
6556 testResult = HTBORDER;
6560 if (!sIsInMouseCapture && allowContentOverride) {
6562 POINT pt = {aX, aY};
6563 ::ScreenToClient(mWnd, &pt);
6565 if (pt.x == mCachedHitTestPoint.x.value &&
6566 pt.y == mCachedHitTestPoint.y.value &&
6567 TimeStamp::Now() - mCachedHitTestTime <
6568 TimeDuration::FromMilliseconds(HITTEST_CACHE_LIFETIME_MS)) {
6569 return mCachedHitTestResult;
6572 mCachedHitTestPoint = {pt.x, pt.y};
6573 mCachedHitTestTime = TimeStamp::Now();
6576 auto pt = mCachedHitTestPoint;
6578 if (mWindowBtnRect[WindowButtonType::Minimize].Contains(pt)) {
6579 testResult = HTMINBUTTON;
6580 } else if (mWindowBtnRect[WindowButtonType::Maximize].Contains(pt)) {
6581 testResult = HTMAXBUTTON;
6582 } else if (mWindowBtnRect[WindowButtonType::Close].Contains(pt)) {
6583 testResult = HTCLOSE;
6584 } else if (!inResizeRegion) {
6585 // If we're in the resize region, avoid overriding that with either a
6586 // drag or a client result; resize takes priority over either (but not
6587 // over the window controls, which is why we check this after those).
6588 if (mDraggableRegion.Contains(pt)) {
6589 testResult = HTCAPTION;
6590 } else {
6591 testResult = HTCLIENT;
6595 mCachedHitTestResult = testResult;
6598 return testResult;
6601 bool nsWindow::IsSimulatedClientArea(int32_t screenX, int32_t screenY) {
6602 int32_t testResult = ClientMarginHitTestPoint(screenX, screenY);
6603 return testResult == HTCAPTION || IsWindowButton(testResult);
6606 bool nsWindow::IsWindowButton(int32_t hitTestResult) {
6607 return hitTestResult == HTMINBUTTON || hitTestResult == HTMAXBUTTON ||
6608 hitTestResult == HTCLOSE;
6611 TimeStamp nsWindow::GetMessageTimeStamp(LONG aEventTime) const {
6612 CurrentWindowsTimeGetter getCurrentTime(mWnd);
6613 return TimeConverter().GetTimeStampFromSystemTime(aEventTime, getCurrentTime);
6616 void nsWindow::PostSleepWakeNotification(const bool aIsSleepMode) {
6617 // Retain the previous mode that was notified to observers
6618 static bool sWasSleepMode = false;
6620 // Only notify observers if mode changed
6621 if (aIsSleepMode == sWasSleepMode) return;
6623 sWasSleepMode = aIsSleepMode;
6625 nsCOMPtr<nsIObserverService> observerService =
6626 mozilla::services::GetObserverService();
6627 if (observerService)
6628 observerService->NotifyObservers(nullptr,
6629 aIsSleepMode
6630 ? NS_WIDGET_SLEEP_OBSERVER_TOPIC
6631 : NS_WIDGET_WAKE_OBSERVER_TOPIC,
6632 nullptr);
6635 LRESULT nsWindow::ProcessCharMessage(const MSG& aMsg, bool* aEventDispatched) {
6636 if (IMEHandler::IsComposingOn(this)) {
6637 IMEHandler::NotifyIME(this, REQUEST_TO_COMMIT_COMPOSITION);
6639 // These must be checked here too as a lone WM_CHAR could be received
6640 // if a child window didn't handle it (for example Alt+Space in a content
6641 // window)
6642 ModifierKeyState modKeyState;
6643 NativeKey nativeKey(this, aMsg, modKeyState);
6644 return static_cast<LRESULT>(nativeKey.HandleCharMessage(aEventDispatched));
6647 LRESULT nsWindow::ProcessKeyUpMessage(const MSG& aMsg, bool* aEventDispatched) {
6648 ModifierKeyState modKeyState;
6649 NativeKey nativeKey(this, aMsg, modKeyState);
6650 bool result = nativeKey.HandleKeyUpMessage(aEventDispatched);
6651 if (aMsg.wParam == VK_F10) {
6652 // Bug 1382199: Windows default behavior will trigger the System menu bar
6653 // when F10 is released. Among other things, this causes the System menu bar
6654 // to appear when a web page overrides the contextmenu event. We *never*
6655 // want this default behavior, so eat this key (never pass it to Windows).
6656 return true;
6658 return result;
6661 LRESULT nsWindow::ProcessKeyDownMessage(const MSG& aMsg,
6662 bool* aEventDispatched) {
6663 // If this method doesn't call NativeKey::HandleKeyDownMessage(), this method
6664 // must clean up the redirected message information itself. For more
6665 // information, see above comment of
6666 // RedirectedKeyDownMessageManager::AutoFlusher class definition in
6667 // KeyboardLayout.h.
6668 RedirectedKeyDownMessageManager::AutoFlusher redirectedMsgFlusher(this, aMsg);
6670 ModifierKeyState modKeyState;
6672 NativeKey nativeKey(this, aMsg, modKeyState);
6673 LRESULT result =
6674 static_cast<LRESULT>(nativeKey.HandleKeyDownMessage(aEventDispatched));
6675 // HandleKeyDownMessage cleaned up the redirected message information
6676 // itself, so, we should do nothing.
6677 redirectedMsgFlusher.Cancel();
6679 if (aMsg.wParam == VK_MENU ||
6680 (aMsg.wParam == VK_F10 && !modKeyState.IsShift())) {
6681 // We need to let Windows handle this keypress,
6682 // by returning false, if there's a native menu
6683 // bar somewhere in our containing window hierarchy.
6684 // Otherwise we handle the keypress and don't pass
6685 // it on to Windows, by returning true.
6686 bool hasNativeMenu = false;
6687 HWND hWnd = mWnd;
6688 while (hWnd) {
6689 if (::GetMenu(hWnd)) {
6690 hasNativeMenu = true;
6691 break;
6693 hWnd = ::GetParent(hWnd);
6695 result = !hasNativeMenu;
6698 return result;
6701 nsresult nsWindow::SynthesizeNativeKeyEvent(
6702 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
6703 uint32_t aModifierFlags, const nsAString& aCharacters,
6704 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) {
6705 AutoObserverNotifier notifier(aObserver, "keyevent");
6707 KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
6708 return keyboardLayout->SynthesizeNativeKeyEvent(
6709 this, aNativeKeyboardLayout, aNativeKeyCode, aModifierFlags, aCharacters,
6710 aUnmodifiedCharacters);
6713 nsresult nsWindow::SynthesizeNativeMouseEvent(
6714 LayoutDeviceIntPoint aPoint, NativeMouseMessage aNativeMessage,
6715 MouseButton aButton, nsIWidget::Modifiers aModifierFlags,
6716 nsIObserver* aObserver) {
6717 AutoObserverNotifier notifier(aObserver, "mouseevent");
6719 INPUT input;
6720 memset(&input, 0, sizeof(input));
6722 // TODO (bug 1693240):
6723 // Now, we synthesize native mouse events asynchronously since we want to
6724 // synthesize the event on the front window at the point. However, Windows
6725 // does not provide a way to set modifier only while a mouse message is
6726 // being handled, and MOUSEEVENTF_MOVE may be coalesced by Windows. So, we
6727 // need a trick for handling it.
6729 switch (aNativeMessage) {
6730 case NativeMouseMessage::Move:
6731 input.mi.dwFlags = MOUSEEVENTF_MOVE;
6732 // Reset sLastMouseMovePoint so that even if we're moving the mouse
6733 // to the position it's already at, we still dispatch a mousemove
6734 // event, because the callers of this function expect that.
6735 sLastMouseMovePoint = {0};
6736 break;
6737 case NativeMouseMessage::ButtonDown:
6738 case NativeMouseMessage::ButtonUp: {
6739 const bool isDown = aNativeMessage == NativeMouseMessage::ButtonDown;
6740 switch (aButton) {
6741 case MouseButton::ePrimary:
6742 input.mi.dwFlags = isDown ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP;
6743 break;
6744 case MouseButton::eMiddle:
6745 input.mi.dwFlags =
6746 isDown ? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP;
6747 break;
6748 case MouseButton::eSecondary:
6749 input.mi.dwFlags =
6750 isDown ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP;
6751 break;
6752 case MouseButton::eX1:
6753 input.mi.dwFlags = isDown ? MOUSEEVENTF_XDOWN : MOUSEEVENTF_XUP;
6754 input.mi.mouseData = XBUTTON1;
6755 break;
6756 case MouseButton::eX2:
6757 input.mi.dwFlags = isDown ? MOUSEEVENTF_XDOWN : MOUSEEVENTF_XUP;
6758 input.mi.mouseData = XBUTTON2;
6759 break;
6760 default:
6761 return NS_ERROR_INVALID_ARG;
6763 break;
6765 case NativeMouseMessage::EnterWindow:
6766 case NativeMouseMessage::LeaveWindow:
6767 MOZ_ASSERT_UNREACHABLE("Non supported mouse event on Windows");
6768 return NS_ERROR_INVALID_ARG;
6771 input.type = INPUT_MOUSE;
6772 ::SetCursorPos(aPoint.x, aPoint.y);
6773 ::SendInput(1, &input, sizeof(INPUT));
6775 return NS_OK;
6778 nsresult nsWindow::SynthesizeNativeMouseScrollEvent(
6779 LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage, double aDeltaX,
6780 double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
6781 uint32_t aAdditionalFlags, nsIObserver* aObserver) {
6782 AutoObserverNotifier notifier(aObserver, "mousescrollevent");
6783 return MouseScrollHandler::SynthesizeNativeMouseScrollEvent(
6784 this, aPoint, aNativeMessage,
6785 (aNativeMessage == WM_MOUSEWHEEL || aNativeMessage == WM_VSCROLL)
6786 ? static_cast<int32_t>(aDeltaY)
6787 : static_cast<int32_t>(aDeltaX),
6788 aModifierFlags, aAdditionalFlags);
6791 nsresult nsWindow::SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase,
6792 LayoutDeviceIntPoint aPoint,
6793 double aDeltaX, double aDeltaY,
6794 int32_t aModifierFlags,
6795 nsIObserver* aObserver) {
6796 AutoObserverNotifier notifier(aObserver, "touchpadpanevent");
6797 DirectManipulationOwner::SynthesizeNativeTouchpadPan(
6798 this, aEventPhase, aPoint, aDeltaX, aDeltaY, aModifierFlags);
6799 return NS_OK;
6802 static void MaybeLogPosChanged(HWND aWnd, WINDOWPOS* wp) {
6803 #ifdef WINSTATE_DEBUG_OUTPUT
6804 if (aWnd == WinUtils::GetTopLevelHWND(aWnd)) {
6805 MOZ_LOG(gWindowsLog, LogLevel::Info, ("*** OnWindowPosChanged: [ top] "));
6806 } else {
6807 MOZ_LOG(gWindowsLog, LogLevel::Info, ("*** OnWindowPosChanged: [child] "));
6809 MOZ_LOG(gWindowsLog, LogLevel::Info, ("WINDOWPOS flags:"));
6810 if (wp->flags & SWP_FRAMECHANGED) {
6811 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_FRAMECHANGED "));
6813 if (wp->flags & SWP_SHOWWINDOW) {
6814 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_SHOWWINDOW "));
6816 if (wp->flags & SWP_NOSIZE) {
6817 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_NOSIZE "));
6819 if (wp->flags & SWP_HIDEWINDOW) {
6820 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_HIDEWINDOW "));
6822 if (wp->flags & SWP_NOZORDER) {
6823 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_NOZORDER "));
6825 if (wp->flags & SWP_NOACTIVATE) {
6826 MOZ_LOG(gWindowsLog, LogLevel::Info, ("SWP_NOACTIVATE "));
6828 MOZ_LOG(gWindowsLog, LogLevel::Info, ("\n"));
6829 #endif
6832 /**************************************************************
6834 * SECTION: OnXXX message handlers
6836 * For message handlers that need to be broken out or
6837 * implemented in specific platform code.
6839 **************************************************************/
6841 void nsWindow::OnWindowPosChanged(WINDOWPOS* wp) {
6842 if (!wp) {
6843 return;
6846 MaybeLogPosChanged(mWnd, wp);
6848 // Handle window size mode changes
6849 if (wp->flags & SWP_FRAMECHANGED) {
6850 // Bug 566135 - Windows theme code calls show window on SW_SHOWMINIMIZED
6851 // windows when fullscreen games disable desktop composition. If we're
6852 // minimized and not being activated, ignore the event and let windows
6853 // handle it.
6854 if (mFrameState->GetSizeMode() == nsSizeMode_Minimized &&
6855 (wp->flags & SWP_NOACTIVATE)) {
6856 return;
6859 mFrameState->OnFrameChanged();
6861 if (mFrameState->GetSizeMode() == nsSizeMode_Minimized) {
6862 // Skip window size change events below on minimization.
6863 return;
6867 // Notify visibility change when window is activated.
6868 if (!(wp->flags & SWP_NOACTIVATE) && NeedsToTrackWindowOcclusionState()) {
6869 WinWindowOcclusionTracker::Get()->OnWindowVisibilityChanged(
6870 this, mFrameState->GetSizeMode() != nsSizeMode_Minimized);
6873 // Handle window position changes
6874 if (!(wp->flags & SWP_NOMOVE)) {
6875 mBounds.MoveTo(wp->x, wp->y);
6876 NotifyWindowMoved(wp->x, wp->y);
6879 // Handle window size changes
6880 if (!(wp->flags & SWP_NOSIZE)) {
6881 RECT r;
6882 int32_t newWidth, newHeight;
6884 ::GetWindowRect(mWnd, &r);
6886 newWidth = r.right - r.left;
6887 newHeight = r.bottom - r.top;
6889 if (newWidth > mLastSize.width) {
6890 RECT drect;
6892 // getting wider
6893 drect.left = wp->x + mLastSize.width;
6894 drect.top = wp->y;
6895 drect.right = drect.left + (newWidth - mLastSize.width);
6896 drect.bottom = drect.top + newHeight;
6898 ::RedrawWindow(mWnd, &drect, nullptr,
6899 RDW_INVALIDATE | RDW_NOERASE | RDW_NOINTERNALPAINT |
6900 RDW_ERASENOW | RDW_ALLCHILDREN);
6902 if (newHeight > mLastSize.height) {
6903 RECT drect;
6905 // getting taller
6906 drect.left = wp->x;
6907 drect.top = wp->y + mLastSize.height;
6908 drect.right = drect.left + newWidth;
6909 drect.bottom = drect.top + (newHeight - mLastSize.height);
6911 ::RedrawWindow(mWnd, &drect, nullptr,
6912 RDW_INVALIDATE | RDW_NOERASE | RDW_NOINTERNALPAINT |
6913 RDW_ERASENOW | RDW_ALLCHILDREN);
6916 mBounds.SizeTo(newWidth, newHeight);
6917 mLastSize.width = newWidth;
6918 mLastSize.height = newHeight;
6920 #ifdef WINSTATE_DEBUG_OUTPUT
6921 MOZ_LOG(gWindowsLog, LogLevel::Info,
6922 ("*** Resize window: %d x %d x %d x %d\n", wp->x, wp->y, newWidth,
6923 newHeight));
6924 #endif
6926 if (mAspectRatio > 0) {
6927 // It's possible (via Windows Aero Snap) that the size of the window
6928 // has changed such that it violates the aspect ratio constraint. If so,
6929 // queue up an event to enforce the aspect ratio constraint and repaint.
6930 // When resized with Windows Aero Snap, we are in the NOT_RESIZING state.
6931 float newAspectRatio = (float)newWidth / newHeight;
6932 if (mResizeState == NOT_RESIZING && mAspectRatio != newAspectRatio) {
6933 // Hold a reference to self alive and pass it into the lambda to make
6934 // sure this nsIWidget stays alive long enough to run this function.
6935 nsCOMPtr<nsIWidget> self(this);
6936 NS_DispatchToMainThread(NS_NewRunnableFunction(
6937 "EnforceAspectRatio", [self, this, newWidth]() -> void {
6938 if (mWnd) {
6939 Resize(newWidth, newWidth / mAspectRatio, true);
6941 }));
6945 // If a maximized window is resized, recalculate the non-client margins.
6946 if (mFrameState->GetSizeMode() == nsSizeMode_Maximized) {
6947 if (UpdateNonClientMargins(true)) {
6948 // gecko resize event already sent by UpdateNonClientMargins.
6949 return;
6954 // Notify the widget listener for size change of client area for gecko
6955 // events. This needs to be done when either window size is changed,
6956 // or window frame is changed. They may not happen together.
6957 // However, we don't invoke that for popup when window frame changes,
6958 // because popups may trigger frame change before size change via
6959 // {Set,Clear}ThemeRegion they invoke in Resize. That would make the
6960 // code below call OnResize with a wrong client size first, which can
6961 // lead to flickerling for some popups.
6962 if (!(wp->flags & SWP_NOSIZE) ||
6963 ((wp->flags & SWP_FRAMECHANGED) && !IsPopup())) {
6964 RECT r;
6965 LayoutDeviceIntSize clientSize;
6966 if (::GetClientRect(mWnd, &r)) {
6967 clientSize = WinUtils::ToIntRect(r).Size();
6968 } else {
6969 clientSize = mBounds.Size();
6971 // Send a gecko resize event
6972 OnResize(clientSize);
6976 void nsWindow::OnWindowPosChanging(WINDOWPOS* info) {
6977 // Update non-client margins if the frame size is changing, and let the
6978 // browser know we are changing size modes, so alternative css can kick in.
6979 // If we're going into fullscreen mode, ignore this, since it'll reset
6980 // margins to normal mode.
6981 if (info->flags & SWP_FRAMECHANGED && !(info->flags & SWP_NOSIZE)) {
6982 mFrameState->OnFrameChanging();
6985 // Force fullscreen. This works around a bug in Windows 10 1809 where
6986 // using fullscreen when a window is "snapped" causes a spurious resize
6987 // smaller than the full screen, see bug 1482920.
6988 if (mFrameState->GetSizeMode() == nsSizeMode_Fullscreen &&
6989 !(info->flags & SWP_NOMOVE) && !(info->flags & SWP_NOSIZE)) {
6990 nsCOMPtr<nsIScreenManager> screenmgr =
6991 do_GetService(sScreenManagerContractID);
6992 if (screenmgr) {
6993 LayoutDeviceIntRect bounds(info->x, info->y, info->cx, info->cy);
6994 DesktopIntRect deskBounds =
6995 RoundedToInt(bounds / GetDesktopToDeviceScale());
6996 nsCOMPtr<nsIScreen> screen;
6997 screenmgr->ScreenForRect(deskBounds.X(), deskBounds.Y(),
6998 deskBounds.Width(), deskBounds.Height(),
6999 getter_AddRefs(screen));
7001 if (screen) {
7002 auto rect = screen->GetRect();
7003 info->x = rect.x;
7004 info->y = rect.y;
7005 info->cx = rect.width;
7006 info->cy = rect.height;
7011 // enforce local z-order rules
7012 if (!(info->flags & SWP_NOZORDER)) {
7013 HWND hwndAfter = info->hwndInsertAfter;
7015 nsWindow* aboveWindow = 0;
7016 nsWindowZ placement;
7018 if (hwndAfter == HWND_BOTTOM)
7019 placement = nsWindowZBottom;
7020 else if (hwndAfter == HWND_TOP || hwndAfter == HWND_TOPMOST ||
7021 hwndAfter == HWND_NOTOPMOST)
7022 placement = nsWindowZTop;
7023 else {
7024 placement = nsWindowZRelative;
7025 aboveWindow = WinUtils::GetNSWindowPtr(hwndAfter);
7028 if (mWidgetListener) {
7029 nsCOMPtr<nsIWidget> actualBelow = nullptr;
7030 if (mWidgetListener->ZLevelChanged(false, &placement, aboveWindow,
7031 getter_AddRefs(actualBelow))) {
7032 if (placement == nsWindowZBottom)
7033 info->hwndInsertAfter = HWND_BOTTOM;
7034 else if (placement == nsWindowZTop)
7035 info->hwndInsertAfter = HWND_TOP;
7036 else {
7037 info->hwndInsertAfter =
7038 (HWND)actualBelow->GetNativeData(NS_NATIVE_WINDOW);
7043 // prevent rude external programs from making hidden window visible
7044 if (mWindowType == WindowType::Invisible) info->flags &= ~SWP_SHOWWINDOW;
7046 // When waking from sleep or switching out of tablet mode, Windows 10
7047 // Version 1809 will reopen popup windows that should be hidden. Detect
7048 // this case and refuse to show the window.
7049 static bool sDWMUnhidesPopups = IsWin10Sep2018UpdateOrLater();
7050 if (sDWMUnhidesPopups && (info->flags & SWP_SHOWWINDOW) &&
7051 mWindowType == WindowType::Popup && mWidgetListener &&
7052 mWidgetListener->ShouldNotBeVisible()) {
7053 info->flags &= ~SWP_SHOWWINDOW;
7057 void nsWindow::UserActivity() {
7058 // Check if we have the idle service, if not we try to get it.
7059 if (!mIdleService) {
7060 mIdleService = do_GetService("@mozilla.org/widget/useridleservice;1");
7063 // Check that we now have the idle service.
7064 if (mIdleService) {
7065 mIdleService->ResetIdleTimeOut(0);
7069 // Helper function for TouchDeviceNeedsPanGestureConversion(PTOUCHINPUT,
7070 // uint32_t).
7071 static bool TouchDeviceNeedsPanGestureConversion(HANDLE aSource) {
7072 std::string deviceName;
7073 UINT dataSize = 0;
7074 // The first call just queries how long the name string will be.
7075 GetRawInputDeviceInfoA(aSource, RIDI_DEVICENAME, nullptr, &dataSize);
7076 if (!dataSize || dataSize > 0x10000) {
7077 return false;
7079 deviceName.resize(dataSize);
7080 // The second call actually populates the string.
7081 UINT result = GetRawInputDeviceInfoA(aSource, RIDI_DEVICENAME, &deviceName[0],
7082 &dataSize);
7083 if (result == UINT_MAX) {
7084 return false;
7086 // The affected device name is "\\?\VIRTUAL_DIGITIZER", but each backslash
7087 // needs to be escaped with another one.
7088 std::string expectedDeviceName = "\\\\?\\VIRTUAL_DIGITIZER";
7089 // For some reason, the dataSize returned by the first call is double the
7090 // actual length of the device name (as if it were returning the size of a
7091 // wide-character string in bytes) even though we are using the narrow
7092 // version of the API. For the comparison against the expected device name
7093 // to pass, we truncate the buffer to be no longer tha the expected device
7094 // name.
7095 if (deviceName.substr(0, expectedDeviceName.length()) != expectedDeviceName) {
7096 return false;
7099 RID_DEVICE_INFO deviceInfo;
7100 deviceInfo.cbSize = sizeof(deviceInfo);
7101 dataSize = sizeof(deviceInfo);
7102 result =
7103 GetRawInputDeviceInfoA(aSource, RIDI_DEVICEINFO, &deviceInfo, &dataSize);
7104 if (result == UINT_MAX) {
7105 return false;
7107 // The device identifiers that we check for here come from bug 1355162
7108 // comment 1 (see also bug 1511901 comment 35).
7109 return deviceInfo.dwType == RIM_TYPEHID && deviceInfo.hid.dwVendorId == 0 &&
7110 deviceInfo.hid.dwProductId == 0 &&
7111 deviceInfo.hid.dwVersionNumber == 1 &&
7112 deviceInfo.hid.usUsagePage == 13 && deviceInfo.hid.usUsage == 4;
7115 // Determine if the touch device that originated |aOSEvent| needs to have
7116 // touch events representing a two-finger gesture converted to pan
7117 // gesture events.
7118 // We only do this for touch devices with a specific name and identifiers.
7119 static bool TouchDeviceNeedsPanGestureConversion(PTOUCHINPUT aOSEvent,
7120 uint32_t aTouchCount) {
7121 if (!StaticPrefs::apz_windows_check_for_pan_gesture_conversion()) {
7122 return false;
7124 if (aTouchCount == 0) {
7125 return false;
7127 HANDLE source = aOSEvent[0].hSource;
7129 // Cache the result of this computation for each touch device.
7130 // Touch devices are identified by the HANDLE stored in the hSource
7131 // field of TOUCHINPUT.
7132 static std::map<HANDLE, bool> sResultCache;
7133 auto [iter, inserted] = sResultCache.emplace(source, false);
7134 if (inserted) {
7135 iter->second = TouchDeviceNeedsPanGestureConversion(source);
7137 return iter->second;
7140 Maybe<PanGestureInput> nsWindow::ConvertTouchToPanGesture(
7141 const MultiTouchInput& aTouchInput, PTOUCHINPUT aOSEvent) {
7142 // Checks if the touch device that originated the touch event is one
7143 // for which we want to convert the touch events to pang gesture events.
7144 bool shouldConvert = TouchDeviceNeedsPanGestureConversion(
7145 aOSEvent, aTouchInput.mTouches.Length());
7146 if (!shouldConvert) {
7147 return Nothing();
7150 // Only two-finger gestures need conversion.
7151 if (aTouchInput.mTouches.Length() != 2) {
7152 return Nothing();
7155 PanGestureInput::PanGestureType eventType = PanGestureInput::PANGESTURE_PAN;
7156 if (aTouchInput.mType == MultiTouchInput::MULTITOUCH_START) {
7157 eventType = PanGestureInput::PANGESTURE_START;
7158 } else if (aTouchInput.mType == MultiTouchInput::MULTITOUCH_END) {
7159 eventType = PanGestureInput::PANGESTURE_END;
7160 } else if (aTouchInput.mType == MultiTouchInput::MULTITOUCH_CANCEL) {
7161 eventType = PanGestureInput::PANGESTURE_CANCELLED;
7164 // Use the midpoint of the two touches as the start point of the pan gesture.
7165 ScreenPoint focusPoint = (aTouchInput.mTouches[0].mScreenPoint +
7166 aTouchInput.mTouches[1].mScreenPoint) /
7168 // To compute the displacement of the pan gesture, we keep track of the
7169 // location of the previous event.
7170 ScreenPoint displacement = (eventType == PanGestureInput::PANGESTURE_START)
7171 ? ScreenPoint(0, 0)
7172 : (focusPoint - mLastPanGestureFocus);
7173 mLastPanGestureFocus = focusPoint;
7175 // We need to negate the displacement because for a touch event, moving the
7176 // fingers down results in scrolling up, but for a touchpad gesture, we want
7177 // moving the fingers down to result in scrolling down.
7178 PanGestureInput result(eventType, aTouchInput.mTimeStamp, focusPoint,
7179 -displacement, aTouchInput.modifiers);
7180 result.mSimulateMomentum = true;
7182 return Some(result);
7185 // Dispatch an event that originated as an OS touch event.
7186 // Usually, we want to dispatch it as a touch event, but some touchpads
7187 // produce touch events for two-finger scrolling, which need to be converted
7188 // to pan gesture events for correct behaviour.
7189 void nsWindow::DispatchTouchOrPanGestureInput(MultiTouchInput& aTouchInput,
7190 PTOUCHINPUT aOSEvent) {
7191 if (Maybe<PanGestureInput> panInput =
7192 ConvertTouchToPanGesture(aTouchInput, aOSEvent)) {
7193 DispatchPanGestureInput(*panInput);
7194 return;
7197 DispatchTouchInput(aTouchInput);
7200 bool nsWindow::OnTouch(WPARAM wParam, LPARAM lParam) {
7201 uint32_t cInputs = LOWORD(wParam);
7202 PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs];
7204 if (GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, pInputs,
7205 sizeof(TOUCHINPUT))) {
7206 MultiTouchInput touchInput, touchEndInput;
7208 // Walk across the touch point array processing each contact point.
7209 for (uint32_t i = 0; i < cInputs; i++) {
7210 bool addToEvent = false, addToEndEvent = false;
7212 // N.B.: According with MS documentation
7213 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd317334(v=vs.85).aspx
7214 // TOUCHEVENTF_DOWN cannot be combined with TOUCHEVENTF_MOVE or
7215 // TOUCHEVENTF_UP. Possibly, it means that TOUCHEVENTF_MOVE and
7216 // TOUCHEVENTF_UP can be combined together.
7218 if (pInputs[i].dwFlags & (TOUCHEVENTF_DOWN | TOUCHEVENTF_MOVE)) {
7219 if (touchInput.mTimeStamp.IsNull()) {
7220 // Initialize a touch event to send.
7221 touchInput.mType = MultiTouchInput::MULTITOUCH_MOVE;
7222 touchInput.mTimeStamp = GetMessageTimeStamp(::GetMessageTime());
7223 ModifierKeyState modifierKeyState;
7224 touchInput.modifiers = modifierKeyState.GetModifiers();
7226 // Pres shell expects this event to be a eTouchStart
7227 // if any new contact points have been added since the last event sent.
7228 if (pInputs[i].dwFlags & TOUCHEVENTF_DOWN) {
7229 touchInput.mType = MultiTouchInput::MULTITOUCH_START;
7231 addToEvent = true;
7233 if (pInputs[i].dwFlags & TOUCHEVENTF_UP) {
7234 // Pres shell expects removed contacts points to be delivered in a
7235 // separate eTouchEnd event containing only the contact points that were
7236 // removed.
7237 if (touchEndInput.mTimeStamp.IsNull()) {
7238 // Initialize a touch event to send.
7239 touchEndInput.mType = MultiTouchInput::MULTITOUCH_END;
7240 touchEndInput.mTimeStamp = GetMessageTimeStamp(::GetMessageTime());
7241 ModifierKeyState modifierKeyState;
7242 touchEndInput.modifiers = modifierKeyState.GetModifiers();
7244 addToEndEvent = true;
7246 if (!addToEvent && !addToEndEvent) {
7247 // Filter out spurious Windows events we don't understand, like palm
7248 // contact.
7249 continue;
7252 // Setup the touch point we'll append to the touch event array.
7253 nsPointWin touchPoint;
7254 touchPoint.x = TOUCH_COORD_TO_PIXEL(pInputs[i].x);
7255 touchPoint.y = TOUCH_COORD_TO_PIXEL(pInputs[i].y);
7256 touchPoint.ScreenToClient(mWnd);
7258 // Initialize the touch data.
7259 SingleTouchData touchData(
7260 pInputs[i].dwID, // aIdentifier
7261 ScreenIntPoint::FromUnknownPoint(touchPoint), // aScreenPoint
7262 // The contact area info cannot be trusted even when
7263 // TOUCHINPUTMASKF_CONTACTAREA is set when the input source is pen,
7264 // which somehow violates the API docs. (bug 1710509) Ultimately the
7265 // dwFlags check will become redundant since we want to migrate to
7266 // WM_POINTER for pens. (bug 1707075)
7267 (pInputs[i].dwMask & TOUCHINPUTMASKF_CONTACTAREA) &&
7268 !(pInputs[i].dwFlags & TOUCHEVENTF_PEN)
7269 ? ScreenSize(TOUCH_COORD_TO_PIXEL(pInputs[i].cxContact) / 2,
7270 TOUCH_COORD_TO_PIXEL(pInputs[i].cyContact) / 2)
7271 : ScreenSize(1, 1), // aRadius
7272 0.0f, // aRotationAngle
7273 0.0f); // aForce
7275 // Append touch data to the appropriate event.
7276 if (addToEvent) {
7277 touchInput.mTouches.AppendElement(touchData);
7279 if (addToEndEvent) {
7280 touchEndInput.mTouches.AppendElement(touchData);
7284 // Dispatch touch start and touch move event if we have one.
7285 if (!touchInput.mTimeStamp.IsNull()) {
7286 DispatchTouchOrPanGestureInput(touchInput, pInputs);
7288 // Dispatch touch end event if we have one.
7289 if (!touchEndInput.mTimeStamp.IsNull()) {
7290 DispatchTouchOrPanGestureInput(touchEndInput, pInputs);
7294 delete[] pInputs;
7295 CloseTouchInputHandle((HTOUCHINPUT)lParam);
7296 return true;
7299 // Gesture event processing. Handles WM_GESTURE events.
7300 bool nsWindow::OnGesture(WPARAM wParam, LPARAM lParam) {
7301 // Treatment for pan events which translate into scroll events:
7302 if (mGesture.IsPanEvent(lParam)) {
7303 if (!mGesture.ProcessPanMessage(mWnd, wParam, lParam))
7304 return false; // ignore
7306 nsEventStatus status;
7308 WidgetWheelEvent wheelEvent(true, eWheel, this);
7310 ModifierKeyState modifierKeyState;
7311 modifierKeyState.InitInputEvent(wheelEvent);
7313 wheelEvent.mButton = 0;
7314 wheelEvent.mTimeStamp = GetMessageTimeStamp(::GetMessageTime());
7315 wheelEvent.mInputSource = MouseEvent_Binding::MOZ_SOURCE_TOUCH;
7317 bool endFeedback = true;
7319 if (mGesture.PanDeltaToPixelScroll(wheelEvent)) {
7320 DispatchEvent(&wheelEvent, status);
7323 if (mDisplayPanFeedback) {
7324 mGesture.UpdatePanFeedbackX(
7325 mWnd, DeprecatedAbs(RoundDown(wheelEvent.mOverflowDeltaX)),
7326 endFeedback);
7327 mGesture.UpdatePanFeedbackY(
7328 mWnd, DeprecatedAbs(RoundDown(wheelEvent.mOverflowDeltaY)),
7329 endFeedback);
7330 mGesture.PanFeedbackFinalize(mWnd, endFeedback);
7333 CloseGestureInfoHandle((HGESTUREINFO)lParam);
7335 return true;
7338 // Other gestures translate into simple gesture events:
7339 WidgetSimpleGestureEvent event(true, eVoidEvent, this);
7340 if (!mGesture.ProcessGestureMessage(mWnd, wParam, lParam, event)) {
7341 return false; // fall through to DefWndProc
7344 // Polish up and send off the new event
7345 ModifierKeyState modifierKeyState;
7346 modifierKeyState.InitInputEvent(event);
7347 event.mButton = 0;
7348 event.mTimeStamp = GetMessageTimeStamp(::GetMessageTime());
7349 event.mInputSource = MouseEvent_Binding::MOZ_SOURCE_TOUCH;
7351 nsEventStatus status;
7352 DispatchEvent(&event, status);
7353 if (status == nsEventStatus_eIgnore) {
7354 return false; // Ignored, fall through
7357 // Only close this if we process and return true.
7358 CloseGestureInfoHandle((HGESTUREINFO)lParam);
7360 return true; // Handled
7363 // WM_DESTROY event handler
7364 void nsWindow::OnDestroy() {
7365 mOnDestroyCalled = true;
7367 // If this is a toplevel window, notify the taskbar concealer to clean up any
7368 // relevant state.
7369 if (!mParent) {
7370 TaskbarConcealer::OnWindowDestroyed(mWnd);
7373 // Make sure we don't get destroyed in the process of tearing down.
7374 nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
7376 // Dispatch the destroy notification.
7377 if (!mInDtor) NotifyWindowDestroyed();
7379 // Prevent the widget from sending additional events.
7380 mWidgetListener = nullptr;
7381 mAttachedWidgetListener = nullptr;
7383 DestroyDirectManipulation();
7385 if (mWnd == mLastKillFocusWindow) {
7386 mLastKillFocusWindow = nullptr;
7388 // Unregister notifications from terminal services
7389 ::WTSUnRegisterSessionNotification(mWnd);
7391 // We will stop receiving native events after dissociating from our native
7392 // window. We will also disappear from the output of WinUtils::GetNSWindowPtr
7393 // for that window.
7394 DissociateFromNativeWindow();
7396 // Once mWidgetListener is cleared and the subclass is reset, sCurrentWindow
7397 // can be cleared. (It's used in tracking windows for mouse events.)
7398 if (sCurrentWindow == this) sCurrentWindow = nullptr;
7400 // Disconnects us from our parent, will call our GetParent().
7401 nsBaseWidget::Destroy();
7403 // Release references to children, device context, toolkit, and app shell.
7404 nsBaseWidget::OnDestroy();
7406 // Clear our native parent handle.
7407 // XXX Windows will take care of this in the proper order, and
7408 // SetParent(nullptr)'s remove child on the parent already took place in
7409 // nsBaseWidget's Destroy call above.
7410 // SetParent(nullptr);
7411 mParent = nullptr;
7413 // We have to destroy the native drag target before we null out our window
7414 // pointer.
7415 EnableDragDrop(false);
7417 // If we're going away and for some reason we're still the rollup widget,
7418 // rollup and turn off capture.
7419 nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
7420 nsCOMPtr<nsIWidget> rollupWidget;
7421 if (rollupListener) {
7422 rollupWidget = rollupListener->GetRollupWidget();
7424 if (this == rollupWidget) {
7425 rollupListener->Rollup({});
7426 CaptureRollupEvents(false);
7429 IMEHandler::OnDestroyWindow(this);
7431 // Free GDI window class objects
7432 if (mBrush) {
7433 VERIFY(::DeleteObject(mBrush));
7434 mBrush = nullptr;
7437 // Destroy any custom cursor resources.
7438 if (mCursor.IsCustom()) {
7439 SetCursor(Cursor{eCursor_standard});
7442 if (mCompositorWidgetDelegate) {
7443 mCompositorWidgetDelegate->OnDestroyWindow();
7445 mBasicLayersSurface = nullptr;
7447 // Finalize panning feedback to possibly restore window displacement
7448 mGesture.PanFeedbackFinalize(mWnd, true);
7450 // Clear the main HWND.
7451 mWnd = nullptr;
7454 // Send a resize message to the listener
7455 bool nsWindow::OnResize(const LayoutDeviceIntSize& aSize) {
7456 if (mCompositorWidgetDelegate &&
7457 !mCompositorWidgetDelegate->OnWindowResize(aSize)) {
7458 return false;
7461 bool result = false;
7462 if (mWidgetListener) {
7463 result = mWidgetListener->WindowResized(this, aSize.width, aSize.height);
7466 // If there is an attached view, inform it as well as the normal widget
7467 // listener.
7468 if (mAttachedWidgetListener) {
7469 return mAttachedWidgetListener->WindowResized(this, aSize.width,
7470 aSize.height);
7473 return result;
7476 void nsWindow::OnSizeModeChange() {
7477 const nsSizeMode mode = mFrameState->GetSizeMode();
7479 MOZ_LOG(gWindowsLog, LogLevel::Info,
7480 ("nsWindow::OnSizeModeChange() sizeMode %d", mode));
7482 UpdateNonClientMargins(false);
7484 if (NeedsToTrackWindowOcclusionState()) {
7485 WinWindowOcclusionTracker::Get()->OnWindowVisibilityChanged(
7486 this, mode != nsSizeMode_Minimized);
7488 wr::DebugFlags flags{0};
7489 flags.bits = gfx::gfxVars::WebRenderDebugFlags();
7490 bool debugEnabled = bool(flags & wr::DebugFlags::WINDOW_VISIBILITY_DBG);
7491 if (debugEnabled && mCompositorWidgetDelegate) {
7492 mCompositorWidgetDelegate->NotifyVisibilityUpdated(mode,
7493 mIsFullyOccluded);
7497 if (mCompositorWidgetDelegate) {
7498 mCompositorWidgetDelegate->OnWindowModeChange(mode);
7501 if (mWidgetListener) {
7502 mWidgetListener->SizeModeChanged(mode);
7506 bool nsWindow::OnHotKey(WPARAM wParam, LPARAM lParam) { return true; }
7508 // Can be overriden. Controls auto-erase of background.
7509 bool nsWindow::AutoErase(HDC dc) { return false; }
7511 bool nsWindow::IsPopup() { return mWindowType == WindowType::Popup; }
7513 bool nsWindow::ShouldUseOffMainThreadCompositing() {
7514 if (mWindowType == WindowType::Popup && mPopupType == PopupType::Tooltip) {
7515 return false;
7518 // Content rendering of popup is always done by child window.
7519 // See nsDocumentViewer::ShouldAttachToTopLevel().
7520 if (mWindowType == WindowType::Popup && !mIsChildWindow) {
7521 MOZ_ASSERT(!mParent);
7522 return false;
7525 return nsBaseWidget::ShouldUseOffMainThreadCompositing();
7528 void nsWindow::WindowUsesOMTC() {
7529 ULONG_PTR style = ::GetClassLongPtr(mWnd, GCL_STYLE);
7530 if (!style) {
7531 NS_WARNING("Could not get window class style");
7532 return;
7534 style |= CS_HREDRAW | CS_VREDRAW;
7535 DebugOnly<ULONG_PTR> result = ::SetClassLongPtr(mWnd, GCL_STYLE, style);
7536 NS_WARNING_ASSERTION(result, "Could not reset window class style");
7539 // See bug 603793
7540 bool nsWindow::HasBogusPopupsDropShadowOnMultiMonitor() {
7541 static const bool sHasBogusPopupsDropShadowOnMultiMonitor = [] {
7542 // Since any change in the preferences requires a restart, this can be
7543 // done just once.
7544 // Check for Direct2D first.
7545 if (gfxWindowsPlatform::GetPlatform()->IsDirect2DBackend()) {
7546 return true;
7548 // Otherwise check if Direct3D 9 may be used.
7549 if (gfxConfig::IsEnabled(gfx::Feature::HW_COMPOSITING) &&
7550 !gfxConfig::IsEnabled(gfx::Feature::OPENGL_COMPOSITING)) {
7551 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
7552 if (gfxInfo) {
7553 int32_t status;
7554 nsCString discardFailureId;
7555 if (NS_SUCCEEDED(
7556 gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS,
7557 discardFailureId, &status))) {
7558 if (status == nsIGfxInfo::FEATURE_STATUS_OK ||
7559 gfxConfig::IsForcedOnByUser(gfx::Feature::HW_COMPOSITING)) {
7560 return true;
7565 return false;
7566 }();
7567 return sHasBogusPopupsDropShadowOnMultiMonitor;
7570 void nsWindow::OnDPIChanged(int32_t x, int32_t y, int32_t width,
7571 int32_t height) {
7572 // Don't try to handle WM_DPICHANGED for popup windows (see bug 1239353);
7573 // they remain tied to their original parent's resolution.
7574 if (mWindowType == WindowType::Popup) {
7575 return;
7577 if (StaticPrefs::layout_css_devPixelsPerPx() > 0.0) {
7578 return;
7580 mDefaultScale = -1.0; // force recomputation of scale factor
7582 if (mResizeState != RESIZING &&
7583 mFrameState->GetSizeMode() == nsSizeMode_Normal) {
7584 // Limit the position (if not in the middle of a drag-move) & size,
7585 // if it would overflow the destination screen
7586 nsCOMPtr<nsIScreenManager> sm = do_GetService(sScreenManagerContractID);
7587 if (sm) {
7588 nsCOMPtr<nsIScreen> screen;
7589 sm->ScreenForRect(x, y, width, height, getter_AddRefs(screen));
7590 if (screen) {
7591 int32_t availLeft, availTop, availWidth, availHeight;
7592 screen->GetAvailRect(&availLeft, &availTop, &availWidth, &availHeight);
7593 if (mResizeState != MOVING) {
7594 x = std::max(x, availLeft);
7595 y = std::max(y, availTop);
7597 width = std::min(width, availWidth);
7598 height = std::min(height, availHeight);
7602 Resize(x, y, width, height, true);
7604 UpdateNonClientMargins();
7605 ChangedDPI();
7606 ResetLayout();
7609 // Callback to generate OnCloakChanged pseudo-events.
7610 /* static */
7611 void nsWindow::OnCloakEvent(HWND aWnd, bool aCloaked) {
7612 MOZ_ASSERT(NS_IsMainThread());
7613 MOZ_ASSERT(IsWin8OrLater());
7615 const char* const kEventName = aCloaked ? "CLOAKED" : "UNCLOAKED";
7616 nsWindow* pWin = WinUtils::GetNSWindowPtr(aWnd);
7617 if (!pWin) {
7618 MOZ_LOG(
7619 sCloakingLog, LogLevel::Debug,
7620 ("Received %s event for HWND %p (not an nsWindow)", kEventName, aWnd));
7621 return;
7624 const char* const kWasCloakedStr = pWin->mIsCloaked ? "cloaked" : "uncloaked";
7625 if (mozilla::IsCloaked(aWnd) == pWin->mIsCloaked) {
7626 MOZ_LOG(sCloakingLog, LogLevel::Debug,
7627 ("Received redundant %s event for %s HWND %p; discarding",
7628 kEventName, kWasCloakedStr, aWnd));
7629 return;
7632 MOZ_LOG(
7633 sCloakingLog, LogLevel::Info,
7634 ("Received %s event for %s HWND %p", kEventName, kWasCloakedStr, aWnd));
7636 // Cloaking events like the one we've just received are sent asynchronously.
7637 // Rather than process them one-by-one, we jump the gun a bit and perform
7638 // updates on all newly cloaked/uncloaked nsWindows at once. This also lets us
7639 // batch operations that consider more than one window's state.
7640 struct Item {
7641 nsWindow* win;
7642 bool nowCloaked;
7644 nsTArray<Item> changedWindows;
7646 mozilla::EnumerateThreadWindows([&](HWND hwnd) {
7647 nsWindow* pWin = WinUtils::GetNSWindowPtr(hwnd);
7648 if (!pWin) {
7649 return;
7652 const bool isCloaked = mozilla::IsCloaked(hwnd);
7653 if (isCloaked != pWin->mIsCloaked) {
7654 changedWindows.AppendElement(Item{pWin, isCloaked});
7658 if (changedWindows.IsEmpty()) {
7659 return;
7662 for (const Item& item : changedWindows) {
7663 item.win->OnCloakChanged(item.nowCloaked);
7666 nsWindow::TaskbarConcealer::OnCloakChanged();
7669 void nsWindow::OnCloakChanged(bool aCloaked) {
7670 MOZ_LOG(sCloakingLog, LogLevel::Info,
7671 ("Calling OnCloakChanged(): HWND %p, aCloaked %s", mWnd,
7672 aCloaked ? "true" : "false"));
7673 mIsCloaked = aCloaked;
7676 /**************************************************************
7677 **************************************************************
7679 ** BLOCK: IME management and accessibility
7681 ** Handles managing IME input and accessibility.
7683 **************************************************************
7684 **************************************************************/
7686 void nsWindow::SetInputContext(const InputContext& aContext,
7687 const InputContextAction& aAction) {
7688 InputContext newInputContext = aContext;
7689 IMEHandler::SetInputContext(this, newInputContext, aAction);
7690 mInputContext = newInputContext;
7693 InputContext nsWindow::GetInputContext() {
7694 mInputContext.mIMEState.mOpen = IMEState::CLOSED;
7695 if (WinUtils::IsIMEEnabled(mInputContext) && IMEHandler::GetOpenState(this)) {
7696 mInputContext.mIMEState.mOpen = IMEState::OPEN;
7697 } else {
7698 mInputContext.mIMEState.mOpen = IMEState::CLOSED;
7700 return mInputContext;
7703 TextEventDispatcherListener* nsWindow::GetNativeTextEventDispatcherListener() {
7704 return IMEHandler::GetNativeTextEventDispatcherListener();
7707 #ifdef ACCESSIBILITY
7708 # ifdef DEBUG
7709 # define NS_LOG_WMGETOBJECT(aWnd, aHwnd, aAcc) \
7710 if (a11y::logging::IsEnabled(a11y::logging::ePlatforms)) { \
7711 printf( \
7712 "Get the window:\n {\n HWND: %p, parent HWND: %p, wndobj: " \
7713 "%p,\n", \
7714 aHwnd, ::GetParent(aHwnd), aWnd); \
7715 printf(" acc: %p", aAcc); \
7716 if (aAcc) { \
7717 nsAutoString name; \
7718 aAcc->Name(name); \
7719 printf(", accname: %s", NS_ConvertUTF16toUTF8(name).get()); \
7721 printf("\n }\n"); \
7724 # else
7725 # define NS_LOG_WMGETOBJECT(aWnd, aHwnd, aAcc)
7726 # endif
7728 a11y::LocalAccessible* nsWindow::GetAccessible() {
7729 // If the pref was ePlatformIsDisabled, return null here, disabling a11y.
7730 if (a11y::PlatformDisabledState() == a11y::ePlatformIsDisabled)
7731 return nullptr;
7733 if (mInDtor || mOnDestroyCalled || mWindowType == WindowType::Invisible) {
7734 return nullptr;
7737 // In case of popup window return a popup accessible.
7738 nsView* view = nsView::GetViewFor(this);
7739 if (view) {
7740 nsIFrame* frame = view->GetFrame();
7741 if (frame && nsLayoutUtils::IsPopup(frame)) {
7742 nsAccessibilityService* accService = GetOrCreateAccService();
7743 if (accService) {
7744 a11y::DocAccessible* docAcc =
7745 GetAccService()->GetDocAccessible(frame->PresShell());
7746 if (docAcc) {
7747 NS_LOG_WMGETOBJECT(
7748 this, mWnd,
7749 docAcc->GetAccessibleOrDescendant(frame->GetContent()));
7750 return docAcc->GetAccessibleOrDescendant(frame->GetContent());
7756 // otherwise root document accessible.
7757 NS_LOG_WMGETOBJECT(this, mWnd, GetRootAccessible());
7758 return GetRootAccessible();
7760 #endif
7762 /**************************************************************
7763 **************************************************************
7765 ** BLOCK: Transparency
7767 ** Window transparency helpers.
7769 **************************************************************
7770 **************************************************************/
7772 void nsWindow::SetWindowTranslucencyInner(TransparencyMode aMode) {
7773 if (aMode == mTransparencyMode) return;
7775 // stop on dialogs and popups!
7776 HWND hWnd = WinUtils::GetTopLevelHWND(mWnd, true);
7777 nsWindow* parent = WinUtils::GetNSWindowPtr(hWnd);
7779 if (!parent) {
7780 NS_WARNING("Trying to use transparent chrome in an embedded context");
7781 return;
7784 if (parent != this) {
7785 NS_WARNING(
7786 "Setting SetWindowTranslucencyInner on a parent this is not us!");
7789 if (aMode == TransparencyMode::Transparent) {
7790 // If we're switching to the use of a transparent window, hide the chrome
7791 // on our parent.
7792 HideWindowChrome(true);
7793 } else if (mHideChrome &&
7794 mTransparencyMode == TransparencyMode::Transparent) {
7795 // if we're switching out of transparent, re-enable our parent's chrome.
7796 HideWindowChrome(false);
7799 LONG_PTR style = ::GetWindowLongPtrW(hWnd, GWL_STYLE),
7800 exStyle = ::GetWindowLongPtr(hWnd, GWL_EXSTYLE);
7802 if (parent->mIsVisible) {
7803 style |= WS_VISIBLE;
7804 if (parent->mFrameState->GetSizeMode() == nsSizeMode_Maximized) {
7805 style |= WS_MAXIMIZE;
7806 } else if (parent->mFrameState->GetSizeMode() == nsSizeMode_Minimized) {
7807 style |= WS_MINIMIZE;
7811 if (aMode == TransparencyMode::Transparent)
7812 exStyle |= WS_EX_LAYERED;
7813 else
7814 exStyle &= ~WS_EX_LAYERED;
7816 VERIFY_WINDOW_STYLE(style);
7817 ::SetWindowLongPtrW(hWnd, GWL_STYLE, style);
7818 ::SetWindowLongPtrW(hWnd, GWL_EXSTYLE, exStyle);
7820 if (HasGlass()) memset(&mGlassMargins, 0, sizeof mGlassMargins);
7821 mTransparencyMode = aMode;
7823 if (mCompositorWidgetDelegate) {
7824 mCompositorWidgetDelegate->UpdateTransparency(aMode);
7826 UpdateGlass();
7828 // Clear window by transparent black when compositor window is used in GPU
7829 // process and non-client area rendering by DWM is enabled.
7830 // It is for showing non-client area rendering. See nsWindow::UpdateGlass().
7831 if (HasGlass() && GetWindowRenderer()->AsKnowsCompositor() &&
7832 GetWindowRenderer()->AsKnowsCompositor()->GetUseCompositorWnd()) {
7833 HDC hdc;
7834 RECT rect;
7835 hdc = ::GetWindowDC(mWnd);
7836 ::GetWindowRect(mWnd, &rect);
7837 ::MapWindowPoints(nullptr, mWnd, (LPPOINT)&rect, 2);
7838 ::FillRect(hdc, &rect,
7839 reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
7840 ReleaseDC(mWnd, hdc);
7844 /**************************************************************
7845 **************************************************************
7847 ** BLOCK: Popup rollup hooks
7849 ** Deals with CaptureRollup on popup windows.
7851 **************************************************************
7852 **************************************************************/
7854 // Schedules a timer for a window, so we can rollup after processing the hook
7855 // event
7856 void nsWindow::ScheduleHookTimer(HWND aWnd, UINT aMsgId) {
7857 // In some cases multiple hooks may be scheduled
7858 // so ignore any other requests once one timer is scheduled
7859 if (sHookTimerId == 0) {
7860 // Remember the window handle and the message ID to be used later
7861 sRollupMsgId = aMsgId;
7862 sRollupMsgWnd = aWnd;
7863 // Schedule native timer for doing the rollup after
7864 // this event is done being processed
7865 sHookTimerId = ::SetTimer(nullptr, 0, 0, (TIMERPROC)HookTimerForPopups);
7866 NS_ASSERTION(sHookTimerId, "Timer couldn't be created.");
7870 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7871 int gLastMsgCode = 0;
7872 extern MSGFEventMsgInfo gMSGFEvents[];
7873 #endif
7875 // Process Menu messages, rollup when popup is clicked.
7876 LRESULT CALLBACK nsWindow::MozSpecialMsgFilter(int code, WPARAM wParam,
7877 LPARAM lParam) {
7878 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7879 if (sProcessHook) {
7880 MSG* pMsg = (MSG*)lParam;
7882 int inx = 0;
7883 while (gMSGFEvents[inx].mId != code && gMSGFEvents[inx].mStr != nullptr) {
7884 inx++;
7886 if (code != gLastMsgCode) {
7887 if (gMSGFEvents[inx].mId == code) {
7888 # ifdef DEBUG
7889 MOZ_LOG(gWindowsLog, LogLevel::Info,
7890 ("MozSpecialMessageProc - code: 0x%X - %s hw: %p\n", code,
7891 gMSGFEvents[inx].mStr, pMsg->hwnd));
7892 # endif
7893 } else {
7894 # ifdef DEBUG
7895 MOZ_LOG(gWindowsLog, LogLevel::Info,
7896 ("MozSpecialMessageProc - code: 0x%X - %d hw: %p\n", code,
7897 gMSGFEvents[inx].mId, pMsg->hwnd));
7898 # endif
7900 gLastMsgCode = code;
7902 PrintEvent(pMsg->message, FALSE, FALSE);
7904 #endif // #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7906 if (sProcessHook && code == MSGF_MENU) {
7907 MSG* pMsg = (MSG*)lParam;
7908 ScheduleHookTimer(pMsg->hwnd, pMsg->message);
7911 return ::CallNextHookEx(sMsgFilterHook, code, wParam, lParam);
7914 // Process all mouse messages. Roll up when a click is in a native window
7915 // that doesn't have an nsIWidget.
7916 LRESULT CALLBACK nsWindow::MozSpecialMouseProc(int code, WPARAM wParam,
7917 LPARAM lParam) {
7918 if (sProcessHook) {
7919 switch (WinUtils::GetNativeMessage(wParam)) {
7920 case WM_LBUTTONDOWN:
7921 case WM_RBUTTONDOWN:
7922 case WM_MBUTTONDOWN:
7923 case WM_MOUSEWHEEL:
7924 case WM_MOUSEHWHEEL: {
7925 MOUSEHOOKSTRUCT* ms = (MOUSEHOOKSTRUCT*)lParam;
7926 nsIWidget* mozWin = WinUtils::GetNSWindowPtr(ms->hwnd);
7927 if (!mozWin) {
7928 ScheduleHookTimer(ms->hwnd, (UINT)wParam);
7930 break;
7934 return ::CallNextHookEx(sCallMouseHook, code, wParam, lParam);
7937 // Process all messages. Roll up when the window is moving, or
7938 // is resizing or when maximized or mininized.
7939 LRESULT CALLBACK nsWindow::MozSpecialWndProc(int code, WPARAM wParam,
7940 LPARAM lParam) {
7941 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7942 if (sProcessHook) {
7943 CWPSTRUCT* cwpt = (CWPSTRUCT*)lParam;
7944 PrintEvent(cwpt->message, FALSE, FALSE);
7946 #endif
7948 if (sProcessHook) {
7949 CWPSTRUCT* cwpt = (CWPSTRUCT*)lParam;
7950 if (cwpt->message == WM_MOVING || cwpt->message == WM_SIZING ||
7951 cwpt->message == WM_GETMINMAXINFO) {
7952 ScheduleHookTimer(cwpt->hwnd, (UINT)cwpt->message);
7956 return ::CallNextHookEx(sCallProcHook, code, wParam, lParam);
7959 // Register the special "hooks" for dropdown processing.
7960 void nsWindow::RegisterSpecialDropdownHooks() {
7961 NS_ASSERTION(!sMsgFilterHook, "sMsgFilterHook must be NULL!");
7962 NS_ASSERTION(!sCallProcHook, "sCallProcHook must be NULL!");
7964 DISPLAY_NMM_PRT("***************** Installing Msg Hooks ***************\n");
7966 // Install msg hook for moving the window and resizing
7967 if (!sMsgFilterHook) {
7968 DISPLAY_NMM_PRT("***** Hooking sMsgFilterHook!\n");
7969 sMsgFilterHook = SetWindowsHookEx(WH_MSGFILTER, MozSpecialMsgFilter,
7970 nullptr, GetCurrentThreadId());
7971 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7972 if (!sMsgFilterHook) {
7973 MOZ_LOG(gWindowsLog, LogLevel::Info,
7974 ("***** SetWindowsHookEx is NOT installed for WH_MSGFILTER!\n"));
7976 #endif
7979 // Install msg hook for menus
7980 if (!sCallProcHook) {
7981 DISPLAY_NMM_PRT("***** Hooking sCallProcHook!\n");
7982 sCallProcHook = SetWindowsHookEx(WH_CALLWNDPROC, MozSpecialWndProc, nullptr,
7983 GetCurrentThreadId());
7984 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7985 if (!sCallProcHook) {
7986 MOZ_LOG(
7987 gWindowsLog, LogLevel::Info,
7988 ("***** SetWindowsHookEx is NOT installed for WH_CALLWNDPROC!\n"));
7990 #endif
7993 // Install msg hook for the mouse
7994 if (!sCallMouseHook) {
7995 DISPLAY_NMM_PRT("***** Hooking sCallMouseHook!\n");
7996 sCallMouseHook = SetWindowsHookEx(WH_MOUSE, MozSpecialMouseProc, nullptr,
7997 GetCurrentThreadId());
7998 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7999 if (!sCallMouseHook) {
8000 MOZ_LOG(gWindowsLog, LogLevel::Info,
8001 ("***** SetWindowsHookEx is NOT installed for WH_MOUSE!\n"));
8003 #endif
8007 // Unhook special message hooks for dropdowns.
8008 void nsWindow::UnregisterSpecialDropdownHooks() {
8009 DISPLAY_NMM_PRT(
8010 "***************** De-installing Msg Hooks ***************\n");
8012 if (sCallProcHook) {
8013 DISPLAY_NMM_PRT("***** Unhooking sCallProcHook!\n");
8014 if (!::UnhookWindowsHookEx(sCallProcHook)) {
8015 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sCallProcHook!\n");
8017 sCallProcHook = nullptr;
8020 if (sMsgFilterHook) {
8021 DISPLAY_NMM_PRT("***** Unhooking sMsgFilterHook!\n");
8022 if (!::UnhookWindowsHookEx(sMsgFilterHook)) {
8023 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sMsgFilterHook!\n");
8025 sMsgFilterHook = nullptr;
8028 if (sCallMouseHook) {
8029 DISPLAY_NMM_PRT("***** Unhooking sCallMouseHook!\n");
8030 if (!::UnhookWindowsHookEx(sCallMouseHook)) {
8031 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sCallMouseHook!\n");
8033 sCallMouseHook = nullptr;
8037 // This timer is designed to only fire one time at most each time a "hook"
8038 // function is used to rollup the dropdown. In some cases, the timer may be
8039 // scheduled from the hook, but that hook event or a subsequent event may roll
8040 // up the dropdown before this timer function is executed.
8042 // For example, if an MFC control takes focus, the combobox will lose focus and
8043 // rollup before this function fires.
8044 VOID CALLBACK nsWindow::HookTimerForPopups(HWND hwnd, UINT uMsg, UINT idEvent,
8045 DWORD dwTime) {
8046 if (sHookTimerId != 0) {
8047 // if the window is nullptr then we need to use the ID to kill the timer
8048 DebugOnly<BOOL> status = ::KillTimer(nullptr, sHookTimerId);
8049 NS_ASSERTION(status, "Hook Timer was not killed.");
8050 sHookTimerId = 0;
8053 if (sRollupMsgId != 0) {
8054 // Note: DealWithPopups does the check to make sure that the rollup widget
8055 // is set.
8056 LRESULT popupHandlingResult;
8057 nsAutoRollup autoRollup;
8058 DealWithPopups(sRollupMsgWnd, sRollupMsgId, 0, 0, &popupHandlingResult);
8059 sRollupMsgId = 0;
8060 sRollupMsgWnd = nullptr;
8064 static bool IsDifferentThreadWindow(HWND aWnd) {
8065 return ::GetCurrentThreadId() != ::GetWindowThreadProcessId(aWnd, nullptr);
8068 // static
8069 bool nsWindow::EventIsInsideWindow(nsWindow* aWindow,
8070 Maybe<POINT> aEventPoint) {
8071 RECT r;
8072 ::GetWindowRect(aWindow->mWnd, &r);
8073 POINT mp;
8074 if (aEventPoint) {
8075 mp = *aEventPoint;
8076 } else {
8077 DWORD pos = ::GetMessagePos();
8078 mp.x = GET_X_LPARAM(pos);
8079 mp.y = GET_Y_LPARAM(pos);
8082 auto margin = aWindow->mInputRegion.mMargin;
8083 if (margin > 0) {
8084 r.top += margin;
8085 r.bottom -= margin;
8086 r.left += margin;
8087 r.right -= margin;
8090 // was the event inside this window?
8091 return static_cast<bool>(::PtInRect(&r, mp));
8094 // static
8095 bool nsWindow::GetPopupsToRollup(nsIRollupListener* aRollupListener,
8096 uint32_t* aPopupsToRollup,
8097 Maybe<POINT> aEventPoint) {
8098 // If we're dealing with menus, we probably have submenus and we don't want
8099 // to rollup some of them if the click is in a parent menu of the current
8100 // submenu.
8101 *aPopupsToRollup = UINT32_MAX;
8102 AutoTArray<nsIWidget*, 5> widgetChain;
8103 uint32_t sameTypeCount = aRollupListener->GetSubmenuWidgetChain(&widgetChain);
8104 for (uint32_t i = 0; i < widgetChain.Length(); ++i) {
8105 nsIWidget* widget = widgetChain[i];
8106 if (EventIsInsideWindow(static_cast<nsWindow*>(widget), aEventPoint)) {
8107 // Don't roll up if the mouse event occurred within a menu of the
8108 // same type. If the mouse event occurred in a menu higher than that,
8109 // roll up, but pass the number of popups to Rollup so that only those
8110 // of the same type close up.
8111 if (i < sameTypeCount) {
8112 return false;
8115 *aPopupsToRollup = sameTypeCount;
8116 break;
8119 return true;
8122 // static
8123 bool nsWindow::NeedsToHandleNCActivateDelayed(HWND aWnd) {
8124 // While popup is open, popup window might be activated by other application.
8125 // At this time, we need to take back focus to the previous window but it
8126 // causes flickering its nonclient area because WM_NCACTIVATE comes before
8127 // WM_ACTIVATE and we cannot know which window will take focus at receiving
8128 // WM_NCACTIVATE. Therefore, we need a hack for preventing the flickerling.
8130 // If non-popup window receives WM_NCACTIVATE at deactivating, default
8131 // wndproc shouldn't handle it as deactivating. Instead, at receiving
8132 // WM_ACTIVIATE after that, WM_NCACTIVATE should be sent again manually.
8133 // This returns true if the window needs to handle WM_NCACTIVATE later.
8135 nsWindow* window = WinUtils::GetNSWindowPtr(aWnd);
8136 return window && !window->IsPopup();
8139 static bool IsTouchSupportEnabled(HWND aWnd) {
8140 nsWindow* topWindow =
8141 WinUtils::GetNSWindowPtr(WinUtils::GetTopLevelHWND(aWnd, true));
8142 return topWindow ? topWindow->IsTouchWindow() : false;
8145 static Maybe<POINT> GetSingleTouch(WPARAM wParam, LPARAM lParam) {
8146 Maybe<POINT> ret;
8147 uint32_t cInputs = LOWORD(wParam);
8148 if (cInputs != 1) {
8149 return ret;
8151 TOUCHINPUT input;
8152 if (GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, &input,
8153 sizeof(TOUCHINPUT))) {
8154 ret.emplace();
8155 ret->x = TOUCH_COORD_TO_PIXEL(input.x);
8156 ret->y = TOUCH_COORD_TO_PIXEL(input.y);
8158 // Note that we don't call CloseTouchInputHandle here because we need
8159 // to read the touch input info again in OnTouch later.
8160 return ret;
8163 // static
8164 bool nsWindow::DealWithPopups(HWND aWnd, UINT aMessage, WPARAM aWParam,
8165 LPARAM aLParam, LRESULT* aResult) {
8166 NS_ASSERTION(aResult, "Bad outResult");
8168 // XXX Why do we use the return value of WM_MOUSEACTIVATE for all messages?
8169 *aResult = MA_NOACTIVATE;
8171 if (!::IsWindowVisible(aWnd)) {
8172 return false;
8175 nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
8176 NS_ENSURE_TRUE(rollupListener, false);
8178 nsCOMPtr<nsIWidget> popup = rollupListener->GetRollupWidget();
8179 if (!popup) {
8180 return false;
8183 static bool sSendingNCACTIVATE = false;
8184 static bool sPendingNCACTIVATE = false;
8185 uint32_t popupsToRollup = UINT32_MAX;
8187 bool consumeRollupEvent = false;
8188 Maybe<POINT> touchPoint; // In screen coords.
8190 nsWindow* popupWindow = static_cast<nsWindow*>(popup.get());
8191 UINT nativeMessage = WinUtils::GetNativeMessage(aMessage);
8192 switch (nativeMessage) {
8193 case WM_TOUCH:
8194 if (!IsTouchSupportEnabled(aWnd)) {
8195 // If APZ is disabled, don't allow touch inputs to dismiss popups. The
8196 // compatibility mouse events will do it instead.
8197 return false;
8199 touchPoint = GetSingleTouch(aWParam, aLParam);
8200 if (!touchPoint) {
8201 return false;
8203 [[fallthrough]];
8204 case WM_LBUTTONDOWN:
8205 case WM_RBUTTONDOWN:
8206 case WM_MBUTTONDOWN:
8207 case WM_NCLBUTTONDOWN:
8208 case WM_NCRBUTTONDOWN:
8209 case WM_NCMBUTTONDOWN:
8210 if (nativeMessage != WM_TOUCH && IsTouchSupportEnabled(aWnd) &&
8211 MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_TOUCH) {
8212 // If any of these mouse events are really compatibility events that
8213 // Windows is sending for touch inputs, then don't allow them to dismiss
8214 // popups when APZ is enabled (instead we do the dismissing as part of
8215 // WM_TOUCH handling which is more correct).
8216 // If we don't do this, then when the user lifts their finger after a
8217 // long-press, the WM_RBUTTONDOWN compatibility event that Windows sends
8218 // us will dismiss the contextmenu popup that we displayed as part of
8219 // handling the long-tap-up.
8220 return false;
8222 if (!EventIsInsideWindow(popupWindow, touchPoint) &&
8223 GetPopupsToRollup(rollupListener, &popupsToRollup, touchPoint)) {
8224 break;
8226 return false;
8227 case WM_POINTERDOWN: {
8228 WinPointerEvents pointerEvents;
8229 if (!pointerEvents.ShouldRollupOnPointerEvent(nativeMessage, aWParam)) {
8230 return false;
8232 POINT pt;
8233 pt.x = GET_X_LPARAM(aLParam);
8234 pt.y = GET_Y_LPARAM(aLParam);
8235 if (!GetPopupsToRollup(rollupListener, &popupsToRollup, Some(pt))) {
8236 return false;
8238 if (EventIsInsideWindow(popupWindow, Some(pt))) {
8239 // Don't roll up if the event is inside the popup window.
8240 return false;
8242 } break;
8243 case MOZ_WM_DMANIP: {
8244 POINT pt;
8245 ::GetCursorPos(&pt);
8246 if (!GetPopupsToRollup(rollupListener, &popupsToRollup, Some(pt))) {
8247 return false;
8249 if (EventIsInsideWindow(popupWindow, Some(pt))) {
8250 // Don't roll up if the event is inside the popup window
8251 return false;
8253 } break;
8254 case WM_MOUSEWHEEL:
8255 case WM_MOUSEHWHEEL:
8256 // We need to check if the popup thinks that it should cause closing
8257 // itself when mouse wheel events are fired outside the rollup widget.
8258 if (!EventIsInsideWindow(popupWindow)) {
8259 // Check if we should consume this event even if we don't roll-up:
8260 consumeRollupEvent = rollupListener->ShouldConsumeOnMouseWheelEvent();
8261 *aResult = MA_ACTIVATE;
8262 if (rollupListener->ShouldRollupOnMouseWheelEvent() &&
8263 GetPopupsToRollup(rollupListener, &popupsToRollup)) {
8264 break;
8267 return consumeRollupEvent;
8269 case WM_ACTIVATEAPP:
8270 break;
8272 case WM_ACTIVATE:
8273 // NOTE: Don't handle WA_INACTIVE for preventing popup taking focus
8274 // because we cannot distinguish it's caused by mouse or not.
8275 if (LOWORD(aWParam) == WA_ACTIVE && aLParam) {
8276 nsWindow* window = WinUtils::GetNSWindowPtr(aWnd);
8277 if (window && window->IsPopup()) {
8278 // Cancel notifying widget listeners of deactivating the previous
8279 // active window (see WM_KILLFOCUS case in ProcessMessage()).
8280 sJustGotDeactivate = false;
8281 // Reactivate the window later.
8282 ::PostMessageW(aWnd, MOZ_WM_REACTIVATE, aWParam, aLParam);
8283 return true;
8285 // Don't rollup the popup when focus moves back to the parent window
8286 // from a popup because such case is caused by strange mouse drivers.
8287 nsWindow* prevWindow =
8288 WinUtils::GetNSWindowPtr(reinterpret_cast<HWND>(aLParam));
8289 if (prevWindow && prevWindow->IsPopup()) {
8290 // Consume this message here since previous window must not have
8291 // been inactivated since we've already stopped accepting the
8292 // inactivation below.
8293 return true;
8295 } else if (LOWORD(aWParam) == WA_INACTIVE) {
8296 nsWindow* activeWindow =
8297 WinUtils::GetNSWindowPtr(reinterpret_cast<HWND>(aLParam));
8298 if (sPendingNCACTIVATE && NeedsToHandleNCActivateDelayed(aWnd)) {
8299 // If focus moves to non-popup widget or focusable popup, the window
8300 // needs to update its nonclient area.
8301 if (!activeWindow || !activeWindow->IsPopup()) {
8302 sSendingNCACTIVATE = true;
8303 ::SendMessageW(aWnd, WM_NCACTIVATE, false, 0);
8304 sSendingNCACTIVATE = false;
8306 sPendingNCACTIVATE = false;
8308 // If focus moves from/to popup, we don't need to rollup the popup
8309 // because such case is caused by strange mouse drivers. And in
8310 // such case, we should consume the message here since we need to
8311 // hide this odd focus move from our content. (If we didn't consume
8312 // the message here, ProcessMessage() will notify widget listener of
8313 // inactivation and that causes unnecessary reflow for supporting
8314 // -moz-window-inactive pseudo class.
8315 if (activeWindow) {
8316 if (activeWindow->IsPopup()) {
8317 return true;
8319 nsWindow* deactiveWindow = WinUtils::GetNSWindowPtr(aWnd);
8320 if (deactiveWindow && deactiveWindow->IsPopup()) {
8321 return true;
8324 } else if (LOWORD(aWParam) == WA_CLICKACTIVE) {
8325 // If the WM_ACTIVATE message is caused by a click in a popup,
8326 // we should not rollup any popups.
8327 nsWindow* window = WinUtils::GetNSWindowPtr(aWnd);
8328 if ((window && window->IsPopup()) ||
8329 !GetPopupsToRollup(rollupListener, &popupsToRollup)) {
8330 return false;
8333 break;
8335 case MOZ_WM_REACTIVATE:
8336 // The previous active window should take back focus.
8337 if (::IsWindow(reinterpret_cast<HWND>(aLParam))) {
8338 // FYI: Even without this API call, you see expected result (e.g., the
8339 // owner window of the popup keeps active without flickering
8340 // the non-client area). And also this causes initializing
8341 // TSF and it causes using CPU time a lot. However, even if we
8342 // consume WM_ACTIVE messages, native focus change has already
8343 // been occurred. I.e., a popup window is active now. Therefore,
8344 // you'll see some odd behavior if we don't reactivate the owner
8345 // window here. For example, if you do:
8346 // 1. Turn wheel on a bookmark panel.
8347 // 2. Turn wheel on another window.
8348 // then, you'll see that the another window becomes active but the
8349 // owner window of the bookmark panel looks still active and the
8350 // bookmark panel keeps open. The reason is that the first wheel
8351 // operation gives focus to the bookmark panel. Therefore, when
8352 // the next operation gives focus to the another window, previous
8353 // focus window is the bookmark panel (i.e., a popup window).
8354 // So, in this case, our hack around here prevents to inactivate
8355 // the owner window and roll up the bookmark panel.
8356 ::SetForegroundWindow(reinterpret_cast<HWND>(aLParam));
8358 return true;
8360 case WM_NCACTIVATE:
8361 if (!aWParam && !sSendingNCACTIVATE &&
8362 NeedsToHandleNCActivateDelayed(aWnd)) {
8363 // Don't just consume WM_NCACTIVATE. It doesn't handle only the
8364 // nonclient area state change.
8365 ::DefWindowProcW(aWnd, aMessage, TRUE, aLParam);
8366 // Accept the deactivating because it's necessary to receive following
8367 // WM_ACTIVATE.
8368 *aResult = TRUE;
8369 sPendingNCACTIVATE = true;
8370 return true;
8372 return false;
8374 case WM_MOUSEACTIVATE:
8375 if (!EventIsInsideWindow(popupWindow) &&
8376 GetPopupsToRollup(rollupListener, &popupsToRollup)) {
8377 // WM_MOUSEACTIVATE may be caused by moving the mouse (e.g., X-mouse
8378 // of TweakUI is enabled. Then, check if the popup should be rolled up
8379 // with rollup listener. If not, just consume the message.
8380 if (HIWORD(aLParam) == WM_MOUSEMOVE &&
8381 !rollupListener->ShouldRollupOnMouseActivate()) {
8382 return true;
8384 // Otherwise, it should be handled by wndproc.
8385 return false;
8388 // Prevent the click inside the popup from causing a change in window
8389 // activation. Since the popup is shown non-activated, we need to eat any
8390 // requests to activate the window while it is displayed. Windows will
8391 // automatically activate the popup on the mousedown otherwise.
8392 return true;
8394 case WM_SHOWWINDOW:
8395 // If the window is being minimized, close popups.
8396 if (aLParam == SW_PARENTCLOSING) {
8397 break;
8399 return false;
8401 case WM_KILLFOCUS:
8402 // If focus moves to other window created in different process/thread,
8403 // e.g., a plugin window, popups should be rolled up.
8404 if (IsDifferentThreadWindow(reinterpret_cast<HWND>(aWParam))) {
8405 break;
8407 return false;
8409 case WM_MOVING:
8410 case WM_MENUSELECT:
8411 break;
8413 default:
8414 return false;
8417 // Only need to deal with the last rollup for left mouse down events.
8418 NS_ASSERTION(!nsAutoRollup::GetLastRollup(), "last rollup is null");
8420 nsIRollupListener::RollupOptions rollupOptions{
8421 popupsToRollup,
8422 nsIRollupListener::FlushViews::Yes,
8425 if (nativeMessage == WM_TOUCH || nativeMessage == WM_LBUTTONDOWN ||
8426 nativeMessage == WM_POINTERDOWN) {
8427 LayoutDeviceIntPoint pos;
8428 if (nativeMessage == WM_TOUCH) {
8429 pos.x = touchPoint->x;
8430 pos.y = touchPoint->y;
8431 } else {
8432 POINT pt;
8433 pt.x = GET_X_LPARAM(aLParam);
8434 pt.y = GET_Y_LPARAM(aLParam);
8435 // POINTERDOWN is already in screen coords.
8436 if (nativeMessage == WM_LBUTTONDOWN) {
8437 ::ClientToScreen(aWnd, &pt);
8439 pos = LayoutDeviceIntPoint(pt.x, pt.y);
8442 rollupOptions.mPoint = &pos;
8443 nsIContent* lastRollup = nullptr;
8444 consumeRollupEvent = rollupListener->Rollup(rollupOptions, &lastRollup);
8445 nsAutoRollup::SetLastRollup(lastRollup);
8446 } else {
8447 consumeRollupEvent = rollupListener->Rollup(rollupOptions);
8450 // Tell hook to stop processing messages
8451 sProcessHook = false;
8452 sRollupMsgId = 0;
8453 sRollupMsgWnd = nullptr;
8455 // If we are NOT supposed to be consuming events, let it go through
8456 if (consumeRollupEvent && nativeMessage != WM_RBUTTONDOWN) {
8457 *aResult = MA_ACTIVATE;
8458 return true;
8461 return false;
8464 /**************************************************************
8465 **************************************************************
8467 ** BLOCK: Misc. utility methods and functions.
8469 ** General use.
8471 **************************************************************
8472 **************************************************************/
8474 // Note that the result of GetTopLevelWindow method can be different from the
8475 // result of WinUtils::GetTopLevelHWND(). The result can be non-floating
8476 // window. Because our top level window may be contained in another window
8477 // which is not managed by us.
8478 nsWindow* nsWindow::GetTopLevelWindow(bool aStopOnDialogOrPopup) {
8479 nsWindow* curWindow = this;
8481 while (true) {
8482 if (aStopOnDialogOrPopup) {
8483 switch (curWindow->mWindowType) {
8484 case WindowType::Dialog:
8485 case WindowType::Popup:
8486 return curWindow;
8487 default:
8488 break;
8492 // Retrieve the top level parent or owner window
8493 nsWindow* parentWindow = curWindow->GetParentWindow(true);
8495 if (!parentWindow) return curWindow;
8497 curWindow = parentWindow;
8501 // Set a flag if hwnd is a (non-popup) visible window from this process,
8502 // and bail out of the enumeration. Otherwise leave the flag unmodified
8503 // and continue the enumeration.
8504 // lParam must be a bool* pointing at the flag to be set.
8505 static BOOL CALLBACK EnumVisibleWindowsProc(HWND hwnd, LPARAM lParam) {
8506 DWORD pid;
8507 ::GetWindowThreadProcessId(hwnd, &pid);
8508 if (pid == ::GetCurrentProcessId() && ::IsWindowVisible(hwnd)) {
8509 // Don't count popups as visible windows, since they don't take focus,
8510 // in case we only have a popup visible (see bug 1554490 where the gfx
8511 // test window is an offscreen popup).
8512 nsWindow* window = WinUtils::GetNSWindowPtr(hwnd);
8513 if (!window || !window->IsPopup()) {
8514 bool* windowsVisible = reinterpret_cast<bool*>(lParam);
8515 *windowsVisible = true;
8516 return FALSE;
8519 return TRUE;
8522 // Determine if it would be ok to activate a window, taking focus.
8523 // We want to avoid stealing focus from another app (bug 225305).
8524 bool nsWindow::CanTakeFocus() {
8525 HWND fgWnd = ::GetForegroundWindow();
8526 if (!fgWnd) {
8527 // There is no foreground window, so don't worry about stealing focus.
8528 return true;
8530 // We can take focus if the current foreground window is already from
8531 // this process.
8532 DWORD pid;
8533 ::GetWindowThreadProcessId(fgWnd, &pid);
8534 if (pid == ::GetCurrentProcessId()) {
8535 return true;
8538 bool windowsVisible = false;
8539 ::EnumWindows(EnumVisibleWindowsProc,
8540 reinterpret_cast<LPARAM>(&windowsVisible));
8542 if (!windowsVisible) {
8543 // We're probably creating our first visible window, allow that to
8544 // take focus.
8545 return true;
8547 return false;
8550 /* static */ const wchar_t* nsWindow::GetMainWindowClass() {
8551 static const wchar_t* sMainWindowClass = nullptr;
8552 if (!sMainWindowClass) {
8553 nsAutoString className;
8554 Preferences::GetString("ui.window_class_override", className);
8555 if (!className.IsEmpty()) {
8556 sMainWindowClass = wcsdup(className.get());
8557 } else {
8558 sMainWindowClass = kClassNameGeneral;
8561 return sMainWindowClass;
8564 LPARAM nsWindow::lParamToScreen(LPARAM lParam) {
8565 POINT pt;
8566 pt.x = GET_X_LPARAM(lParam);
8567 pt.y = GET_Y_LPARAM(lParam);
8568 ::ClientToScreen(mWnd, &pt);
8569 return MAKELPARAM(pt.x, pt.y);
8572 LPARAM nsWindow::lParamToClient(LPARAM lParam) {
8573 POINT pt;
8574 pt.x = GET_X_LPARAM(lParam);
8575 pt.y = GET_Y_LPARAM(lParam);
8576 ::ScreenToClient(mWnd, &pt);
8577 return MAKELPARAM(pt.x, pt.y);
8580 WPARAM nsWindow::wParamFromGlobalMouseState() {
8581 WPARAM result = 0;
8583 if (!!::GetKeyState(VK_CONTROL)) {
8584 result |= MK_CONTROL;
8587 if (!!::GetKeyState(VK_SHIFT)) {
8588 result |= MK_SHIFT;
8591 if (!!::GetKeyState(VK_LBUTTON)) {
8592 result |= MK_LBUTTON;
8595 if (!!::GetKeyState(VK_MBUTTON)) {
8596 result |= MK_MBUTTON;
8599 if (!!::GetKeyState(VK_RBUTTON)) {
8600 result |= MK_RBUTTON;
8603 if (!!::GetKeyState(VK_XBUTTON1)) {
8604 result |= MK_XBUTTON1;
8607 if (!!::GetKeyState(VK_XBUTTON2)) {
8608 result |= MK_XBUTTON2;
8611 return result;
8614 void nsWindow::PickerOpen() { mPickerDisplayCount++; }
8616 void nsWindow::PickerClosed() {
8617 NS_ASSERTION(mPickerDisplayCount > 0, "mPickerDisplayCount out of sync!");
8618 if (!mPickerDisplayCount) return;
8619 mPickerDisplayCount--;
8620 if (!mPickerDisplayCount && mDestroyCalled) {
8621 Destroy();
8625 bool nsWindow::WidgetTypeSupportsAcceleration() {
8626 // We don't currently support using an accelerated layer manager with
8627 // transparent windows so don't even try. I'm also not sure if we even
8628 // want to support this case. See bug 593471.
8630 // Windows' support for transparent accelerated surfaces isn't great.
8631 // Some possible approaches:
8632 // - Readback the data and update it using
8633 // UpdateLayeredWindow/UpdateLayeredWindowIndirect
8634 // This is what WPF does. See
8635 // CD3DDeviceLevel1::PresentWithGDI/CD3DSwapChainWithSwDC in WpfGfx. The
8636 // rationale for not using IDirect3DSurface9::GetDC is explained here:
8637 // https://web.archive.org/web/20160521191104/https://blogs.msdn.microsoft.com/dwayneneed/2008/09/08/transparent-windows-in-wpf/
8638 // - Use D3D11_RESOURCE_MISC_GDI_COMPATIBLE, IDXGISurface1::GetDC(),
8639 // and UpdateLayeredWindowIndirect.
8640 // This is suggested here:
8641 // https://docs.microsoft.com/en-us/archive/msdn-magazine/2009/december/windows-with-c-layered-windows-with-direct2d
8642 // but might have the same problem that IDirect3DSurface9::GetDC has.
8643 // - Creating the window with the WS_EX_NOREDIRECTIONBITMAP flag and use
8644 // DirectComposition.
8645 // Not supported on Win7.
8646 // - Using DwmExtendFrameIntoClientArea with negative margins and something
8647 // to turn off the glass effect.
8648 // This doesn't work when the DWM is not running (Win7)
8650 // Also see bug 1150376, D3D11 composition can cause issues on some devices
8651 // on Windows 7 where presentation fails randomly for windows with drop
8652 // shadows.
8653 return mTransparencyMode != TransparencyMode::Transparent &&
8654 !(IsPopup() && DeviceManagerDx::Get()->IsWARP());
8657 bool nsWindow::DispatchTouchEventFromWMPointer(
8658 UINT msg, LPARAM aLParam, const WinPointerInfo& aPointerInfo,
8659 mozilla::MouseButton aButton) {
8660 MultiTouchInput::MultiTouchType touchType;
8661 switch (msg) {
8662 case WM_POINTERDOWN:
8663 touchType = MultiTouchInput::MULTITOUCH_START;
8664 break;
8665 case WM_POINTERUPDATE:
8666 if (aPointerInfo.mPressure == 0) {
8667 return false; // hover
8669 touchType = MultiTouchInput::MULTITOUCH_MOVE;
8670 break;
8671 case WM_POINTERUP:
8672 touchType = MultiTouchInput::MULTITOUCH_END;
8673 break;
8674 default:
8675 return false;
8678 nsPointWin touchPoint;
8679 touchPoint.x = GET_X_LPARAM(aLParam);
8680 touchPoint.y = GET_Y_LPARAM(aLParam);
8681 touchPoint.ScreenToClient(mWnd);
8683 SingleTouchData touchData(static_cast<int32_t>(aPointerInfo.pointerId),
8684 ScreenIntPoint::FromUnknownPoint(touchPoint),
8685 ScreenSize(1, 1), // pixel size radius for pen
8686 0.0f, // no radius rotation
8687 aPointerInfo.mPressure);
8688 touchData.mTiltX = aPointerInfo.tiltX;
8689 touchData.mTiltY = aPointerInfo.tiltY;
8690 touchData.mTwist = aPointerInfo.twist;
8692 MultiTouchInput touchInput;
8693 touchInput.mType = touchType;
8694 touchInput.mTimeStamp = GetMessageTimeStamp(::GetMessageTime());
8695 touchInput.mTouches.AppendElement(touchData);
8696 touchInput.mButton = aButton;
8697 touchInput.mButtons = aPointerInfo.mButtons;
8699 // POINTER_INFO.dwKeyStates can't be used as it only supports Shift and Ctrl
8700 ModifierKeyState modifierKeyState;
8701 touchInput.modifiers = modifierKeyState.GetModifiers();
8703 DispatchTouchInput(touchInput, MouseEvent_Binding::MOZ_SOURCE_PEN);
8704 return true;
8707 static MouseButton PenFlagsToMouseButton(PEN_FLAGS aPenFlags) {
8708 // Theoretically flags can be set together but they do not
8709 if (aPenFlags & PEN_FLAG_BARREL) {
8710 return MouseButton::eSecondary;
8712 if (aPenFlags & PEN_FLAG_ERASER) {
8713 return MouseButton::eEraser;
8715 return MouseButton::ePrimary;
8718 bool nsWindow::OnPointerEvents(UINT msg, WPARAM aWParam, LPARAM aLParam) {
8719 if (!mAPZC) {
8720 // APZ is not available on context menu. Follow the behavior of touch input
8721 // which fallbacks to WM_LBUTTON* and WM_GESTURE, to keep consistency.
8722 return false;
8724 if (!mPointerEvents.ShouldHandleWinPointerMessages(msg, aWParam)) {
8725 return false;
8727 if (!mPointerEvents.ShouldFirePointerEventByWinPointerMessages()) {
8728 // We have to handle WM_POINTER* to fetch and cache pen related information
8729 // and fire WidgetMouseEvent with the cached information the WM_*BUTTONDOWN
8730 // handler. This is because Windows doesn't support ::DoDragDrop in the
8731 // touch or pen message handlers.
8732 mPointerEvents.ConvertAndCachePointerInfo(msg, aWParam);
8733 // Don't consume the Windows WM_POINTER* messages
8734 return false;
8737 uint32_t pointerId = mPointerEvents.GetPointerId(aWParam);
8738 POINTER_PEN_INFO penInfo{};
8739 if (!mPointerEvents.GetPointerPenInfo(pointerId, &penInfo)) {
8740 return false;
8743 // When dispatching mouse events with pen, there may be some
8744 // WM_POINTERUPDATE messages between WM_POINTERDOWN and WM_POINTERUP with
8745 // small movements. Those events will reset sLastMousePoint and reset
8746 // sLastClickCount. To prevent that, we keep the last pen down position
8747 // and compare it with the subsequent WM_POINTERUPDATE. If the movement is
8748 // smaller than GetSystemMetrics(SM_CXDRAG), then we suppress firing
8749 // eMouseMove for WM_POINTERUPDATE.
8750 static POINT sLastPointerDownPoint = {0};
8752 // We don't support chorded buttons for pen. Keep the button at
8753 // WM_POINTERDOWN.
8754 static mozilla::MouseButton sLastPenDownButton = MouseButton::ePrimary;
8755 static bool sPointerDown = false;
8757 EventMessage message;
8758 mozilla::MouseButton button = MouseButton::ePrimary;
8759 switch (msg) {
8760 case WM_POINTERDOWN: {
8761 LayoutDeviceIntPoint eventPoint(GET_X_LPARAM(aLParam),
8762 GET_Y_LPARAM(aLParam));
8763 sLastPointerDownPoint.x = eventPoint.x;
8764 sLastPointerDownPoint.y = eventPoint.y;
8765 message = eMouseDown;
8766 button = PenFlagsToMouseButton(penInfo.penFlags);
8767 sLastPenDownButton = button;
8768 sPointerDown = true;
8769 } break;
8770 case WM_POINTERUP:
8771 message = eMouseUp;
8772 MOZ_ASSERT(sPointerDown, "receive WM_POINTERUP w/o WM_POINTERDOWN");
8773 button = sPointerDown ? sLastPenDownButton : MouseButton::ePrimary;
8774 sPointerDown = false;
8775 break;
8776 case WM_POINTERUPDATE:
8777 message = eMouseMove;
8778 if (sPointerDown) {
8779 LayoutDeviceIntPoint eventPoint(GET_X_LPARAM(aLParam),
8780 GET_Y_LPARAM(aLParam));
8781 int32_t movementX = sLastPointerDownPoint.x > eventPoint.x
8782 ? sLastPointerDownPoint.x - eventPoint.x.value
8783 : eventPoint.x.value - sLastPointerDownPoint.x;
8784 int32_t movementY = sLastPointerDownPoint.y > eventPoint.y
8785 ? sLastPointerDownPoint.y - eventPoint.y.value
8786 : eventPoint.y.value - sLastPointerDownPoint.y;
8787 bool insideMovementThreshold =
8788 movementX < (int32_t)::GetSystemMetrics(SM_CXDRAG) &&
8789 movementY < (int32_t)::GetSystemMetrics(SM_CYDRAG);
8791 if (insideMovementThreshold) {
8792 // Suppress firing eMouseMove for WM_POINTERUPDATE if the movement
8793 // from last WM_POINTERDOWN is smaller than SM_CXDRAG / SM_CYDRAG
8794 return false;
8796 button = sLastPenDownButton;
8798 break;
8799 case WM_POINTERLEAVE:
8800 message = eMouseExitFromWidget;
8801 break;
8802 default:
8803 return false;
8806 // Windows defines the pen pressure is normalized to a range between 0 and
8807 // 1024. Convert it to float.
8808 float pressure = penInfo.pressure ? (float)penInfo.pressure / 1024 : 0;
8809 int16_t buttons = sPointerDown
8810 ? nsContentUtils::GetButtonsFlagForButton(button)
8811 : MouseButtonsFlag::eNoButtons;
8812 WinPointerInfo pointerInfo(pointerId, penInfo.tiltX, penInfo.tiltY, pressure,
8813 buttons);
8814 // Per
8815 // https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-pointer_pen_info,
8816 // the rotation is normalized in a range of 0 to 359.
8817 MOZ_ASSERT(penInfo.rotation <= 359);
8818 pointerInfo.twist = (int32_t)penInfo.rotation;
8820 // Fire touch events but not when the barrel button is pressed.
8821 if (button != MouseButton::eSecondary &&
8822 StaticPrefs::dom_w3c_pointer_events_scroll_by_pen_enabled() &&
8823 DispatchTouchEventFromWMPointer(msg, aLParam, pointerInfo, button)) {
8824 return true;
8827 // The aLParam of WM_POINTER* is the screen location. Convert it to client
8828 // location
8829 LPARAM newLParam = lParamToClient(aLParam);
8830 DispatchMouseEvent(message, aWParam, newLParam, false, button,
8831 MouseEvent_Binding::MOZ_SOURCE_PEN, &pointerInfo);
8833 if (button == MouseButton::eSecondary && message == eMouseUp) {
8834 // Fire eContextMenu manually since consuming WM_POINTER* blocks
8835 // WM_CONTEXTMENU
8836 DispatchMouseEvent(eContextMenu, aWParam, newLParam, false, button,
8837 MouseEvent_Binding::MOZ_SOURCE_PEN, &pointerInfo);
8839 // Consume WM_POINTER* to stop Windows fires WM_*BUTTONDOWN / WM_*BUTTONUP
8840 // WM_MOUSEMOVE.
8841 return true;
8844 void nsWindow::GetCompositorWidgetInitData(
8845 mozilla::widget::CompositorWidgetInitData* aInitData) {
8846 *aInitData = WinCompositorWidgetInitData(
8847 reinterpret_cast<uintptr_t>(mWnd),
8848 reinterpret_cast<uintptr_t>(static_cast<nsIWidget*>(this)),
8849 mTransparencyMode, mFrameState->GetSizeMode());
8852 bool nsWindow::SynchronouslyRepaintOnResize() {
8853 return !gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled();
8856 void nsWindow::MaybeDispatchInitialFocusEvent() {
8857 if (mIsShowingPreXULSkeletonUI && ::GetActiveWindow() == mWnd) {
8858 DispatchFocusToTopLevelWindow(true);
8862 already_AddRefed<nsIWidget> nsIWidget::CreateTopLevelWindow() {
8863 nsCOMPtr<nsIWidget> window = new nsWindow();
8864 return window.forget();
8867 already_AddRefed<nsIWidget> nsIWidget::CreateChildWindow() {
8868 nsCOMPtr<nsIWidget> window = new nsWindow(true);
8869 return window.forget();
8872 // static
8873 bool nsWindow::InitTouchInjection() {
8874 if (!sTouchInjectInitialized) {
8875 // Initialize touch injection on the first call
8876 HMODULE hMod = LoadLibraryW(kUser32LibName);
8877 if (!hMod) {
8878 return false;
8881 InitializeTouchInjectionPtr func =
8882 (InitializeTouchInjectionPtr)GetProcAddress(hMod,
8883 "InitializeTouchInjection");
8884 if (!func) {
8885 WinUtils::Log("InitializeTouchInjection not available.");
8886 return false;
8889 if (!func(TOUCH_INJECT_MAX_POINTS, TOUCH_FEEDBACK_DEFAULT)) {
8890 WinUtils::Log("InitializeTouchInjection failure. GetLastError=%d",
8891 GetLastError());
8892 return false;
8895 sInjectTouchFuncPtr =
8896 (InjectTouchInputPtr)GetProcAddress(hMod, "InjectTouchInput");
8897 if (!sInjectTouchFuncPtr) {
8898 WinUtils::Log("InjectTouchInput not available.");
8899 return false;
8901 sTouchInjectInitialized = true;
8903 return true;
8906 bool nsWindow::InjectTouchPoint(uint32_t aId, LayoutDeviceIntPoint& aPoint,
8907 POINTER_FLAGS aFlags, uint32_t aPressure,
8908 uint32_t aOrientation) {
8909 if (aId > TOUCH_INJECT_MAX_POINTS) {
8910 WinUtils::Log("Pointer ID exceeds maximum. See TOUCH_INJECT_MAX_POINTS.");
8911 return false;
8914 POINTER_TOUCH_INFO info{};
8916 info.touchFlags = TOUCH_FLAG_NONE;
8917 info.touchMask =
8918 TOUCH_MASK_CONTACTAREA | TOUCH_MASK_ORIENTATION | TOUCH_MASK_PRESSURE;
8919 info.pressure = aPressure;
8920 info.orientation = aOrientation;
8922 info.pointerInfo.pointerFlags = aFlags;
8923 info.pointerInfo.pointerType = PT_TOUCH;
8924 info.pointerInfo.pointerId = aId;
8925 info.pointerInfo.ptPixelLocation.x = aPoint.x;
8926 info.pointerInfo.ptPixelLocation.y = aPoint.y;
8928 info.rcContact.top = info.pointerInfo.ptPixelLocation.y - 2;
8929 info.rcContact.bottom = info.pointerInfo.ptPixelLocation.y + 2;
8930 info.rcContact.left = info.pointerInfo.ptPixelLocation.x - 2;
8931 info.rcContact.right = info.pointerInfo.ptPixelLocation.x + 2;
8933 for (int i = 0; i < 3; i++) {
8934 if (sInjectTouchFuncPtr(1, &info)) {
8935 break;
8937 DWORD error = GetLastError();
8938 if (error == ERROR_NOT_READY && i < 2) {
8939 // We sent it too quickly after the previous injection (see bug 1535140
8940 // comment 10). On the first loop iteration we just yield (via Sleep(0))
8941 // and try again. If it happens again on the second loop iteration we
8942 // explicitly Sleep(1) and try again. If that doesn't work either we just
8943 // error out.
8944 ::Sleep(i);
8945 continue;
8947 WinUtils::Log("InjectTouchInput failure. GetLastError=%d", error);
8948 return false;
8950 return true;
8953 void nsWindow::ChangedDPI() {
8954 if (mWidgetListener) {
8955 if (PresShell* presShell = mWidgetListener->GetPresShell()) {
8956 presShell->BackingScaleFactorChanged();
8961 static Result<POINTER_FLAGS, nsresult> PointerStateToFlag(
8962 nsWindow::TouchPointerState aPointerState, bool isUpdate) {
8963 bool hover = aPointerState & nsWindow::TOUCH_HOVER;
8964 bool contact = aPointerState & nsWindow::TOUCH_CONTACT;
8965 bool remove = aPointerState & nsWindow::TOUCH_REMOVE;
8966 bool cancel = aPointerState & nsWindow::TOUCH_CANCEL;
8968 POINTER_FLAGS flags;
8969 if (isUpdate) {
8970 // We know about this pointer, send an update
8971 flags = POINTER_FLAG_UPDATE;
8972 if (hover) {
8973 flags |= POINTER_FLAG_INRANGE;
8974 } else if (contact) {
8975 flags |= POINTER_FLAG_INCONTACT | POINTER_FLAG_INRANGE;
8976 } else if (remove) {
8977 flags = POINTER_FLAG_UP;
8980 if (cancel) {
8981 flags |= POINTER_FLAG_CANCELED;
8983 } else {
8984 // Missing init state, error out
8985 if (remove || cancel) {
8986 return Err(NS_ERROR_INVALID_ARG);
8989 // Create a new pointer
8990 flags = POINTER_FLAG_INRANGE;
8991 if (contact) {
8992 flags |= POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN;
8995 return flags;
8998 nsresult nsWindow::SynthesizeNativeTouchPoint(
8999 uint32_t aPointerId, nsIWidget::TouchPointerState aPointerState,
9000 LayoutDeviceIntPoint aPoint, double aPointerPressure,
9001 uint32_t aPointerOrientation, nsIObserver* aObserver) {
9002 AutoObserverNotifier notifier(aObserver, "touchpoint");
9004 if (StaticPrefs::apz_test_fails_with_native_injection() ||
9005 !InitTouchInjection()) {
9006 // If we don't have touch injection from the OS, or if we are running a test
9007 // that cannot properly inject events to satisfy the OS requirements (see
9008 // bug 1313170) we can just fake it and synthesize the events from here.
9009 MOZ_ASSERT(NS_IsMainThread());
9010 if (aPointerState == TOUCH_HOVER) {
9011 return NS_ERROR_UNEXPECTED;
9014 if (!mSynthesizedTouchInput) {
9015 mSynthesizedTouchInput = MakeUnique<MultiTouchInput>();
9018 WidgetEventTime time = CurrentMessageWidgetEventTime();
9019 LayoutDeviceIntPoint pointInWindow = aPoint - WidgetToScreenOffset();
9020 MultiTouchInput inputToDispatch = UpdateSynthesizedTouchState(
9021 mSynthesizedTouchInput.get(), time.mTimeStamp, aPointerId,
9022 aPointerState, pointInWindow, aPointerPressure, aPointerOrientation);
9023 DispatchTouchInput(inputToDispatch);
9024 return NS_OK;
9027 // win api expects a value from 0 to 1024. aPointerPressure is a value
9028 // from 0.0 to 1.0.
9029 uint32_t pressure = (uint32_t)ceil(aPointerPressure * 1024);
9031 // If we already know about this pointer id get it's record
9032 return mActivePointers.WithEntryHandle(aPointerId, [&](auto&& entry) {
9033 POINTER_FLAGS flags;
9034 // Can't use MOZ_TRY_VAR because it confuses WithEntryHandle
9035 auto result = PointerStateToFlag(aPointerState, !!entry);
9036 if (result.isOk()) {
9037 flags = result.unwrap();
9038 } else {
9039 return result.unwrapErr();
9042 if (!entry) {
9043 entry.Insert(MakeUnique<PointerInfo>(aPointerId, aPoint,
9044 PointerInfo::PointerType::TOUCH));
9045 } else {
9046 if (entry.Data()->mType != PointerInfo::PointerType::TOUCH) {
9047 return NS_ERROR_UNEXPECTED;
9049 if (aPointerState & TOUCH_REMOVE) {
9050 // Remove the pointer from our tracking list. This is UniquePtr wrapped,
9051 // so shouldn't leak.
9052 entry.Remove();
9056 return !InjectTouchPoint(aPointerId, aPoint, flags, pressure,
9057 aPointerOrientation)
9058 ? NS_ERROR_UNEXPECTED
9059 : NS_OK;
9063 nsresult nsWindow::ClearNativeTouchSequence(nsIObserver* aObserver) {
9064 AutoObserverNotifier notifier(aObserver, "cleartouch");
9065 if (!sTouchInjectInitialized) {
9066 return NS_OK;
9069 // cancel all input points
9070 for (auto iter = mActivePointers.Iter(); !iter.Done(); iter.Next()) {
9071 auto* info = iter.UserData();
9072 if (info->mType != PointerInfo::PointerType::TOUCH) {
9073 continue;
9075 InjectTouchPoint(info->mPointerId, info->mPosition, POINTER_FLAG_CANCELED);
9076 iter.Remove();
9079 nsBaseWidget::ClearNativeTouchSequence(nullptr);
9081 return NS_OK;
9084 #if !defined(NTDDI_WIN10_RS5) || (NTDDI_VERSION < NTDDI_WIN10_RS5)
9085 static CreateSyntheticPointerDevicePtr CreateSyntheticPointerDevice;
9086 static DestroySyntheticPointerDevicePtr DestroySyntheticPointerDevice;
9087 static InjectSyntheticPointerInputPtr InjectSyntheticPointerInput;
9088 #endif
9089 static HSYNTHETICPOINTERDEVICE sSyntheticPenDevice;
9091 static bool InitPenInjection() {
9092 if (sSyntheticPenDevice) {
9093 return true;
9095 #if !defined(NTDDI_WIN10_RS5) || (NTDDI_VERSION < NTDDI_WIN10_RS5)
9096 HMODULE hMod = LoadLibraryW(kUser32LibName);
9097 if (!hMod) {
9098 return false;
9100 CreateSyntheticPointerDevice =
9101 (CreateSyntheticPointerDevicePtr)GetProcAddress(
9102 hMod, "CreateSyntheticPointerDevice");
9103 if (!CreateSyntheticPointerDevice) {
9104 WinUtils::Log("CreateSyntheticPointerDevice not available.");
9105 return false;
9107 DestroySyntheticPointerDevice =
9108 (DestroySyntheticPointerDevicePtr)GetProcAddress(
9109 hMod, "DestroySyntheticPointerDevice");
9110 if (!DestroySyntheticPointerDevice) {
9111 WinUtils::Log("DestroySyntheticPointerDevice not available.");
9112 return false;
9114 InjectSyntheticPointerInput = (InjectSyntheticPointerInputPtr)GetProcAddress(
9115 hMod, "InjectSyntheticPointerInput");
9116 if (!InjectSyntheticPointerInput) {
9117 WinUtils::Log("InjectSyntheticPointerInput not available.");
9118 return false;
9120 #endif
9121 sSyntheticPenDevice =
9122 CreateSyntheticPointerDevice(PT_PEN, 1, POINTER_FEEDBACK_DEFAULT);
9123 return !!sSyntheticPenDevice;
9126 nsresult nsWindow::SynthesizeNativePenInput(
9127 uint32_t aPointerId, nsIWidget::TouchPointerState aPointerState,
9128 LayoutDeviceIntPoint aPoint, double aPressure, uint32_t aRotation,
9129 int32_t aTiltX, int32_t aTiltY, int32_t aButton, nsIObserver* aObserver) {
9130 AutoObserverNotifier notifier(aObserver, "peninput");
9131 if (!InitPenInjection()) {
9132 return NS_ERROR_UNEXPECTED;
9135 // win api expects a value from 0 to 1024. aPointerPressure is a value
9136 // from 0.0 to 1.0.
9137 uint32_t pressure = (uint32_t)ceil(aPressure * 1024);
9139 // If we already know about this pointer id get it's record
9140 return mActivePointers.WithEntryHandle(aPointerId, [&](auto&& entry) {
9141 POINTER_FLAGS flags;
9142 // Can't use MOZ_TRY_VAR because it confuses WithEntryHandle
9143 auto result = PointerStateToFlag(aPointerState, !!entry);
9144 if (result.isOk()) {
9145 flags = result.unwrap();
9146 } else {
9147 return result.unwrapErr();
9150 if (!entry) {
9151 entry.Insert(MakeUnique<PointerInfo>(aPointerId, aPoint,
9152 PointerInfo::PointerType::PEN));
9153 } else {
9154 if (entry.Data()->mType != PointerInfo::PointerType::PEN) {
9155 return NS_ERROR_UNEXPECTED;
9157 if (aPointerState & TOUCH_REMOVE) {
9158 // Remove the pointer from our tracking list. This is UniquePtr wrapped,
9159 // so shouldn't leak.
9160 entry.Remove();
9164 POINTER_TYPE_INFO info{};
9166 info.type = PT_PEN;
9167 info.penInfo.pointerInfo.pointerType = PT_PEN;
9168 info.penInfo.pointerInfo.pointerFlags = flags;
9169 info.penInfo.pointerInfo.pointerId = aPointerId;
9170 info.penInfo.pointerInfo.ptPixelLocation.x = aPoint.x;
9171 info.penInfo.pointerInfo.ptPixelLocation.y = aPoint.y;
9173 info.penInfo.penFlags = PEN_FLAG_NONE;
9174 // PEN_FLAG_ERASER is not supported this way, unfortunately.
9175 if (aButton == 2) {
9176 info.penInfo.penFlags |= PEN_FLAG_BARREL;
9178 info.penInfo.penMask = PEN_MASK_PRESSURE | PEN_MASK_ROTATION |
9179 PEN_MASK_TILT_X | PEN_MASK_TILT_Y;
9180 info.penInfo.pressure = pressure;
9181 info.penInfo.rotation = aRotation;
9182 info.penInfo.tiltX = aTiltX;
9183 info.penInfo.tiltY = aTiltY;
9185 return InjectSyntheticPointerInput(sSyntheticPenDevice, &info, 1)
9186 ? NS_OK
9187 : NS_ERROR_UNEXPECTED;
9191 bool nsWindow::HandleAppCommandMsg(const MSG& aAppCommandMsg,
9192 LRESULT* aRetValue) {
9193 ModifierKeyState modKeyState;
9194 NativeKey nativeKey(this, aAppCommandMsg, modKeyState);
9195 bool consumed = nativeKey.HandleAppCommandMessage();
9196 *aRetValue = consumed ? 1 : 0;
9197 return consumed;
9200 #ifdef DEBUG
9201 nsresult nsWindow::SetHiDPIMode(bool aHiDPI) {
9202 return WinUtils::SetHiDPIMode(aHiDPI);
9205 nsresult nsWindow::RestoreHiDPIMode() { return WinUtils::RestoreHiDPIMode(); }
9206 #endif
9208 static nsSizeMode GetSizeModeForWindowFrame(HWND aWnd, bool aFullscreenMode) {
9209 WINDOWPLACEMENT pl;
9210 pl.length = sizeof(pl);
9211 ::GetWindowPlacement(aWnd, &pl);
9213 if (pl.showCmd == SW_SHOWMINIMIZED) {
9214 return nsSizeMode_Minimized;
9215 } else if (aFullscreenMode) {
9216 return nsSizeMode_Fullscreen;
9217 } else if (pl.showCmd == SW_SHOWMAXIMIZED) {
9218 return nsSizeMode_Maximized;
9219 } else {
9220 return nsSizeMode_Normal;
9224 static void ShowWindowWithMode(HWND aWnd, nsSizeMode aMode) {
9225 // This will likely cause a callback to
9226 // nsWindow::FrameState::{OnFrameChanging() and OnFrameChanged()}
9227 switch (aMode) {
9228 case nsSizeMode_Fullscreen:
9229 ::ShowWindow(aWnd, SW_SHOW);
9230 break;
9232 case nsSizeMode_Maximized:
9233 ::ShowWindow(aWnd, SW_MAXIMIZE);
9234 break;
9236 case nsSizeMode_Minimized:
9237 ::ShowWindow(aWnd, SW_MINIMIZE);
9238 break;
9240 default:
9241 // Don't call ::ShowWindow if we're trying to "restore" a window that is
9242 // already in a normal state. Prevents a bug where snapping to one side
9243 // of the screen and then minimizing would cause Windows to forget our
9244 // window's correct restored position/size.
9245 if (GetCurrentShowCmd(aWnd) != SW_SHOWNORMAL) {
9246 ::ShowWindow(aWnd, SW_RESTORE);
9251 nsWindow::FrameState::FrameState(nsWindow* aWindow) : mWindow(aWindow) {}
9253 nsSizeMode nsWindow::FrameState::GetSizeMode() const { return mSizeMode; }
9255 void nsWindow::FrameState::CheckInvariant() const {
9256 MOZ_ASSERT(mSizeMode >= 0 && mSizeMode < nsSizeMode_Invalid);
9257 MOZ_ASSERT(mLastSizeMode >= 0 && mLastSizeMode < nsSizeMode_Invalid);
9258 MOZ_ASSERT(mPreFullscreenSizeMode >= 0 &&
9259 mPreFullscreenSizeMode < nsSizeMode_Invalid);
9260 MOZ_ASSERT(mWindow);
9262 // We should never observe fullscreen sizemode unless fullscreen is enabled
9263 MOZ_ASSERT_IF(mSizeMode == nsSizeMode_Fullscreen, mFullscreenMode);
9264 MOZ_ASSERT_IF(!mFullscreenMode, mSizeMode != nsSizeMode_Fullscreen);
9266 // Something went wrong if we somehow saved fullscreen mode when we are
9267 // changing into fullscreen mode
9268 MOZ_ASSERT(mPreFullscreenSizeMode != nsSizeMode_Fullscreen);
9271 void nsWindow::FrameState::ConsumePreXULSkeletonState(bool aWasMaximized) {
9272 mSizeMode = aWasMaximized ? nsSizeMode_Maximized : nsSizeMode_Normal;
9275 void nsWindow::FrameState::EnsureSizeMode(nsSizeMode aMode,
9276 DoShowWindow aDoShowWindow) {
9277 if (mSizeMode == aMode) {
9278 return;
9281 if (aMode == nsSizeMode_Fullscreen) {
9282 EnsureFullscreenMode(true, aDoShowWindow);
9283 MOZ_ASSERT(mSizeMode == nsSizeMode_Fullscreen);
9284 } else if (mSizeMode == nsSizeMode_Fullscreen && aMode == nsSizeMode_Normal) {
9285 // If we are in fullscreen mode, minimize should work like normal and
9286 // return us to fullscreen mode when unminimized. Maximize isn't really
9287 // available and won't do anything. "Restore" should do the same thing as
9288 // requesting to end fullscreen.
9289 EnsureFullscreenMode(false, aDoShowWindow);
9290 } else {
9291 SetSizeModeInternal(aMode, aDoShowWindow);
9295 void nsWindow::FrameState::EnsureFullscreenMode(bool aFullScreen,
9296 DoShowWindow aDoShowWindow) {
9297 const bool changed = aFullScreen != mFullscreenMode;
9298 if (changed && aFullScreen) {
9299 // Save the size mode from before fullscreen.
9300 mPreFullscreenSizeMode = mSizeMode;
9302 mFullscreenMode = aFullScreen;
9303 if (changed || aFullScreen) {
9304 // NOTE(emilio): When minimizing a fullscreen window we remain with
9305 // mFullscreenMode = true, but mSizeMode = nsSizeMode_Minimized. We need to
9306 // make sure to call SetSizeModeInternal even if mFullscreenMode didn't
9307 // change, to ensure we actually end up with a fullscreen sizemode when
9308 // restoring a window from that state.
9309 SetSizeModeInternal(
9310 aFullScreen ? nsSizeMode_Fullscreen : mPreFullscreenSizeMode,
9311 aDoShowWindow);
9315 void nsWindow::FrameState::OnFrameChanging() {
9316 const nsSizeMode newSizeMode =
9317 GetSizeModeForWindowFrame(mWindow->mWnd, mFullscreenMode);
9318 EnsureSizeMode(newSizeMode);
9319 mWindow->UpdateNonClientMargins(false);
9322 void nsWindow::FrameState::OnFrameChanged() {
9323 // We don't want to perform the ShowWindow ourselves if we're on the frame
9324 // changed message. Windows has done the frame change for us, and we take care
9325 // of activating as needed. We also don't want to potentially trigger
9326 // more focus / restore. Among other things, this addresses a bug on Win7
9327 // related to window docking. (bug 489258)
9328 const auto newSizeMode =
9329 GetSizeModeForWindowFrame(mWindow->mWnd, mFullscreenMode);
9330 EnsureSizeMode(newSizeMode, DoShowWindow::No);
9332 // If window was restored, activate the window now to get correct attributes.
9333 if (mWindow->mIsVisible && mLastSizeMode != mSizeMode &&
9334 mSizeMode != nsSizeMode_Minimized) {
9335 mWindow->DispatchFocusToTopLevelWindow(true);
9337 mLastSizeMode = mSizeMode;
9340 static void MaybeLogSizeMode(nsSizeMode aMode) {
9341 #ifdef WINSTATE_DEBUG_OUTPUT
9342 MOZ_LOG(gWindowsLog, LogLevel::Info, ("*** SizeMode: %d\n", int(aMode)));
9343 #endif
9346 void nsWindow::FrameState::SetSizeModeInternal(nsSizeMode aMode,
9347 DoShowWindow aDoShowWindow) {
9348 if (mSizeMode == aMode) {
9349 return;
9352 const auto oldSizeMode = mSizeMode;
9353 const bool fullscreenChange =
9354 mSizeMode == nsSizeMode_Fullscreen || aMode == nsSizeMode_Fullscreen;
9355 const bool fullscreen = aMode == nsSizeMode_Fullscreen;
9357 if (fullscreenChange) {
9358 mWindow->OnFullscreenWillChange(fullscreen);
9361 mLastSizeMode = mSizeMode;
9362 mSizeMode = aMode;
9364 MaybeLogSizeMode(mSizeMode);
9366 if (bool(aDoShowWindow) && mWindow->mIsVisible) {
9367 ShowWindowWithMode(mWindow->mWnd, aMode);
9370 mWindow->OnSizeModeChange();
9372 if (fullscreenChange) {
9373 mWindow->OnFullscreenChanged(oldSizeMode, fullscreen);
9377 void nsWindow::ContextMenuPreventer::Update(
9378 const WidgetMouseEvent& aEvent,
9379 const nsIWidget::ContentAndAPZEventStatus& aEventStatus) {
9380 mNeedsToPreventContextMenu =
9381 aEvent.mMessage == eMouseUp &&
9382 aEvent.mButton == MouseButton::eSecondary &&
9383 aEvent.mInputSource == MouseEvent_Binding::MOZ_SOURCE_MOUSE &&
9384 aEventStatus.mApzStatus == nsEventStatus_eConsumeNoDefault;