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/. */
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:
16 * nsIWidget methods and utilities
17 * nsSwitchToUIThread impl.
18 * nsSwitchToUIThread methods and utilities
20 * Event initialization
25 * OnEvent event handlers
26 * IME management and accessibility
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
42 * nsWindowDbg.h/.cpp - Debug related code and directives.
43 * nsWindowGfx.h/.cpp - Graphics and painting.
47 /**************************************************************
48 **************************************************************
54 **************************************************************
55 **************************************************************/
57 #include "gfx2DGlue.h"
59 #include "gfxPlatform.h"
61 #include "mozilla/AppShutdown.h"
62 #include "mozilla/AutoRestore.h"
63 #include "mozilla/Likely.h"
64 #include "mozilla/PreXULSkeletonUI.h"
65 #include "mozilla/Logging.h"
66 #include "mozilla/MathAlgorithms.h"
67 #include "mozilla/MiscEvents.h"
68 #include "mozilla/MouseEvents.h"
69 #include "mozilla/PresShell.h"
70 #include "mozilla/ScopeExit.h"
71 #include "mozilla/StaticPrefs_browser.h"
72 #include "mozilla/SwipeTracker.h"
73 #include "mozilla/TouchEvents.h"
74 #include "mozilla/TimeStamp.h"
76 #include "mozilla/ipc/MessageChannel.h"
80 #include "mozilla/widget/WinMessages.h"
82 #include "nsWindowTaskbarConcealer.h"
83 #include "nsAppRunner.h"
94 #include <propvarutil.h>
97 #include "mozilla/Logging.h"
101 #include "mozilla/WidgetTraceEvent.h"
102 #include "nsContentUtils.h"
103 #include "nsISupportsPrimitives.h"
104 #include "nsITheme.h"
105 #include "nsIObserverService.h"
106 #include "nsIScreenManager.h"
107 #include "imgIContainer.h"
109 #include "nsIRollupListener.h"
110 #include "nsIClipboard.h"
111 #include "WinMouseScrollHandler.h"
112 #include "nsFontMetrics.h"
113 #include "nsIFontEnumerator.h"
116 #include "nsThreadUtils.h"
117 #include "nsNativeCharsetUtils.h"
118 #include "nsGkAtoms.h"
120 #include "nsAppDirectoryServiceDefs.h"
121 #include "nsWidgetsCID.h"
122 #include "nsTHashtable.h"
123 #include "nsHashKeys.h"
124 #include "nsString.h"
125 #include "mozilla/Components.h"
126 #include "nsNativeThemeWin.h"
127 #include "nsXULPopupManager.h"
128 #include "nsWindowsDllInterceptor.h"
129 #include "nsLayoutUtils.h"
131 #include "nsWindowGfx.h"
132 #include "gfxWindowsPlatform.h"
133 #include "gfxDWriteFonts.h"
134 #include "nsPrintfCString.h"
135 #include "mozilla/Preferences.h"
136 #include "SystemTimeConverter.h"
137 #include "WinTaskbar.h"
138 #include "WidgetUtils.h"
139 #include "WinWindowOcclusionTracker.h"
140 #include "nsIWidgetListener.h"
141 #include "mozilla/dom/Document.h"
142 #include "mozilla/dom/MouseEventBinding.h"
143 #include "mozilla/dom/Touch.h"
144 #include "mozilla/gfx/2D.h"
145 #include "mozilla/gfx/GPUProcessManager.h"
146 #include "mozilla/intl/LocaleService.h"
147 #include "mozilla/layers/WebRenderLayerManager.h"
148 #include "mozilla/WindowsVersion.h"
149 #include "mozilla/TextEvents.h" // For WidgetKeyboardEvent
150 #include "mozilla/TextEventDispatcherListener.h"
151 #include "mozilla/widget/nsAutoRollup.h"
152 #include "mozilla/widget/PlatformWidgetTypes.h"
153 #include "mozilla/widget/Screen.h"
154 #include "nsStyleConsts.h"
155 #include "nsBidiKeyboard.h"
156 #include "nsStyleConsts.h"
157 #include "gfxConfig.h"
158 #include "InProcessWinCompositorWidget.h"
159 #include "InputDeviceUtils.h"
160 #include "ScreenHelperWin.h"
161 #include "mozilla/StaticPrefs_apz.h"
162 #include "mozilla/StaticPrefs_dom.h"
163 #include "mozilla/StaticPrefs_gfx.h"
164 #include "mozilla/StaticPrefs_layout.h"
165 #include "mozilla/StaticPrefs_ui.h"
166 #include "mozilla/StaticPrefs_widget.h"
167 #include "nsNativeAppSupportWin.h"
168 #include "mozilla/browser/NimbusFeatures.h"
170 #include "nsIGfxInfo.h"
171 #include "nsUXThemeConstants.h"
172 #include "KeyboardLayout.h"
173 #include "nsNativeDragTarget.h"
174 #include <mmsystem.h> // needed for WIN32_LEAN_AND_MEAN
176 #include <richedit.h>
178 #if defined(ACCESSIBILITY)
181 # include "mozilla/a11y/Logging.h"
185 # include <winuser.h>
186 # include "nsAccessibilityService.h"
187 # include "mozilla/a11y/DocAccessible.h"
188 # include "mozilla/a11y/LazyInstantiator.h"
189 # include "mozilla/a11y/Platform.h"
190 # if !defined(WINABLEAPI)
191 # include <winable.h>
192 # endif // !defined(WINABLEAPI)
193 #endif // defined(ACCESSIBILITY)
195 #include "WindowsUIUtils.h"
197 #include "nsWindowDefs.h"
199 #include "nsCrashOnException.h"
201 #include "nsIContent.h"
203 #include "mozilla/BackgroundHangMonitor.h"
204 #include "WinIMEHandler.h"
210 // ERROR from wingdi.h (below) gets undefined by some code.
212 // #define RGN_ERROR ERROR
215 #if !defined(SM_CONVERTIBLESLATEMODE)
216 # define SM_CONVERTIBLESLATEMODE 0x2003
219 #include "mozilla/gfx/DeviceManagerDx.h"
220 #include "mozilla/layers/APZInputBridge.h"
221 #include "mozilla/layers/InputAPZContext.h"
222 #include "mozilla/layers/KnowsCompositor.h"
223 #include "InputData.h"
225 #include "mozilla/TaskController.h"
226 #include "mozilla/Telemetry.h"
227 #include "mozilla/webrender/WebRenderAPI.h"
228 #include "mozilla/layers/IAPZCTreeManager.h"
230 #include "DirectManipulationOwner.h"
232 using namespace mozilla
;
233 using namespace mozilla::dom
;
234 using namespace mozilla::gfx
;
235 using namespace mozilla::layers
;
236 using namespace mozilla::widget
;
237 using namespace mozilla::plugins
;
239 /**************************************************************
240 **************************************************************
244 ** nsWindow Class static initializations and global variables.
246 **************************************************************
247 **************************************************************/
249 /**************************************************************
251 * SECTION: nsWindow statics
253 **************************************************************/
254 static const wchar_t kUser32LibName
[] = L
"user32.dll";
256 uint32_t nsWindow::sInstanceCount
= 0;
257 bool nsWindow::sIsOleInitialized
= false;
258 nsIWidget::Cursor
nsWindow::sCurrentCursor
= {};
259 nsWindow
* nsWindow::sCurrentWindow
= nullptr;
260 bool nsWindow::sJustGotDeactivate
= false;
261 bool nsWindow::sJustGotActivate
= false;
262 bool nsWindow::sIsInMouseCapture
= false;
264 // Urgent-message reentrancy depth for the static `WindowProc` callback.
266 // Three unfortunate facts collide:
268 // 𝛼) Some messages must be processed promptly. If not, Windows will leave the
269 // receiving window in an intermediate, and potentially unusable, state until
270 // the WindowProc invocation that is handling it returns.
272 // 𝛽) Some messages have indefinitely long processing time. These are mostly
273 // messages which may cause us to enter a nested modal loop (via
274 // `SpinEventLoopUntil` or similar).
276 // 𝛾) Sometimes, messages skip the queue entirely. Our `WindowProc` may be
277 // reentrantly reinvoked from the kernel while we're blocking _on_ the
278 // kernel, even briefly, during processing of other messages. (Relevant
279 // search term: `KeUserModeCallback`.)
281 // The nightmare scenario, then, is that during processing of an 𝛼-message, we
282 // briefly become blocked (e.g., by calling `::SendMessageW()`), and the kernel
283 // takes that opportunity to use 𝛾 to hand us a 𝛽-message. (Concretely, see
286 // There is little we can do to prevent the first half of this scenario. 𝛼) and
287 // 𝛾) are effectively immutable facts of Windows, and we sometimes legitimately
288 // need to make blocking calls to process 𝛼-messages. (We may not even be aware
289 // that we're making such calls, if they're undocumented implementation details
292 // In an ideal world, WindowProc would always return promptly (or at least in
293 // bounded time), and 𝛽-messages would not _per se_ exist; long-running modal
294 // states would instead be implemented in async fashion. In practice, that's far
295 // easier said than done -- replacing existing uses of `SpinEventLoopUntil` _et
296 // al._ with asynchronous mechanisms is a collection of mostly-unrelated cross-
297 // cutting architectural tasks, each of potentially unbounded scope. For now,
298 // and for the foreseeable future, we're stuck with them.
300 // We therefore simply punt. More specifically: if a known 𝛽-message jumps the
301 // queue to come in while we're in the middle of processing a known 𝛼-message,
303 // * properly queue the message for processing later;
304 // * respond to the 𝛽-message as though we actually had processed it; and
305 // * just hope that it can wait until we get around to it.
307 // The word "known" requires a bit of justification. There is no canonical set
308 // of 𝛼-messages, nor is the set of 𝛽-messages fixed (or even demarcable). We
309 // can't safely assume that all messages are 𝛼-messages, as that could cause
310 // 𝛽-messages to be arbitrarily and surprisingly delayed whenever any nested
311 // event loop is active. We also can't assume all messages are 𝛽-messages,
312 // since one 𝛼-message jumping the queue while processing another 𝛼-message is
313 // part of normal and required operation for windowed Windows applications.
315 // So we simply add messages to those sets as we identify them. (Or, preferably,
316 // rework the 𝛽-message's handling to make it no longer 𝛽. But see above.)
320 // The actual value of `sDepth` is the number of active invocations of
321 // `WindowProc` that are processing known 𝛼-messages.
322 size_t nsWindow::WndProcUrgentInvocation::sDepth
= 0;
324 // Hook Data Members for Dropdowns. sProcessHook Tells the
325 // hook methods whether they should be processing the hook
327 HHOOK
nsWindow::sMsgFilterHook
= nullptr;
328 HHOOK
nsWindow::sCallProcHook
= nullptr;
329 HHOOK
nsWindow::sCallMouseHook
= nullptr;
330 bool nsWindow::sProcessHook
= false;
331 UINT
nsWindow::sRollupMsgId
= 0;
332 HWND
nsWindow::sRollupMsgWnd
= nullptr;
333 UINT
nsWindow::sHookTimerId
= 0;
335 // Used to prevent dispatching mouse events that do not originate from user
337 POINT
nsWindow::sLastMouseMovePoint
= {0};
339 bool nsWindow::sIsRestoringSession
= false;
341 bool nsWindow::sTouchInjectInitialized
= false;
342 InjectTouchInputPtr
nsWindow::sInjectTouchFuncPtr
;
344 static SystemTimeConverter
<DWORD
>& TimeConverter() {
345 static SystemTimeConverter
<DWORD
> timeConverterSingleton
;
346 return timeConverterSingleton
;
349 // Global event hook for window cloaking. Never deregistered.
350 // - `Nothing` if not yet set.
351 // - `Some(nullptr)` if no attempt should be made to set it.
352 static mozilla::Maybe
<HWINEVENTHOOK
> sWinCloakEventHook
= Nothing();
353 static mozilla::LazyLogModule
sCloakingLog("DWMCloaking");
357 class CurrentWindowsTimeGetter
{
359 explicit CurrentWindowsTimeGetter(HWND aWnd
) : mWnd(aWnd
) {}
361 DWORD
GetCurrentTime() const { return ::GetTickCount(); }
363 void GetTimeAsyncForPossibleBackwardsSkew(const TimeStamp
& aNow
) {
364 DWORD currentTime
= GetCurrentTime();
365 if (sBackwardsSkewStamp
&& currentTime
== sLastPostTime
) {
366 // There's already one inflight with this timestamp. Don't
370 sBackwardsSkewStamp
= Some(aNow
);
371 sLastPostTime
= currentTime
;
372 static_assert(sizeof(WPARAM
) >= sizeof(DWORD
),
373 "Can't fit a DWORD in a WPARAM");
374 ::PostMessage(mWnd
, MOZ_WM_SKEWFIX
, sLastPostTime
, 0);
377 static bool GetAndClearBackwardsSkewStamp(DWORD aPostTime
,
378 TimeStamp
* aOutSkewStamp
) {
379 if (aPostTime
!= sLastPostTime
) {
380 // The SKEWFIX message is stale; we've sent a new one since then.
384 MOZ_ASSERT(sBackwardsSkewStamp
);
385 *aOutSkewStamp
= sBackwardsSkewStamp
.value();
386 sBackwardsSkewStamp
= Nothing();
391 static Maybe
<TimeStamp
> sBackwardsSkewStamp
;
392 static DWORD sLastPostTime
;
396 Maybe
<TimeStamp
> CurrentWindowsTimeGetter::sBackwardsSkewStamp
;
397 DWORD
CurrentWindowsTimeGetter::sLastPostTime
= 0;
399 } // namespace mozilla
401 /**************************************************************
403 * SECTION: globals variables
405 **************************************************************/
407 static const char* sScreenManagerContractID
=
408 "@mozilla.org/gfx/screenmanager;1";
410 extern mozilla::LazyLogModule gWindowsLog
;
412 static NS_DEFINE_CID(kCClipboardCID
, NS_CLIPBOARD_CID
);
414 // General purpose user32.dll hook object
415 static WindowsDllInterceptor sUser32Intercept
;
417 // When the client area is extended out into the default window frame area,
418 // this is the minimum amount of space along the edge of resizable windows
419 // we will always display a resize cursor in, regardless of the underlying
421 static const int32_t kResizableBorderMinSize
= 3;
423 // Getting this object from the window server can be expensive. Keep it
424 // around, also get it off the main thread. (See bug 1640852)
425 StaticRefPtr
<IVirtualDesktopManager
> gVirtualDesktopManager
;
426 static bool gInitializedVirtualDesktopManager
= false;
428 // We should never really try to accelerate windows bigger than this. In some
429 // cases this might lead to no D3D9 acceleration where we could have had it
430 // but D3D9 does not reliably report when it supports bigger windows. 8192
431 // is as safe as we can get, we know at least D3D10 hardware always supports
432 // this, other hardware we expect to report correctly in D3D9.
433 #define MAX_ACCELERATED_DIMENSION 8192
435 // On window open (as well as after), Windows has an unfortunate habit of
436 // sending rather a lot of WM_NCHITTEST messages. Because we have to do point
437 // to DOM target conversions for these, we cache responses for a given
438 // coordinate this many milliseconds:
439 #define HITTEST_CACHE_LIFETIME_MS 50
441 #if defined(ACCESSIBILITY)
446 * Windows touchscreen code works by setting a global WH_GETMESSAGE hook and
447 * injecting tiptsf.dll. The touchscreen process then posts registered messages
448 * to our main thread. The tiptsf hook picks up those registered messages and
449 * uses them as commands, some of which call into UIA, which then calls into
450 * MSAA, which then sends WM_GETOBJECT to us.
452 * We can get ahead of this by installing our own thread-local WH_GETMESSAGE
453 * hook. Since thread-local hooks are called ahead of global hooks, we will
454 * see these registered messages before tiptsf does. At this point we can then
455 * raise a flag that blocks a11y before invoking CallNextHookEx which will then
456 * invoke the global tiptsf hook. Then when we see WM_GETOBJECT, we check the
457 * flag by calling TIPMessageHandler::IsA11yBlocked().
459 * For Windows 8, we also hook tiptsf!ProcessCaretEvents, which is an a11y hook
460 * function that also calls into UIA.
462 class TIPMessageHandler
{
464 ~TIPMessageHandler() {
466 ::UnhookWindowsHookEx(mHook
);
470 static void Initialize() {
475 sInstance
= new TIPMessageHandler();
476 ClearOnShutdown(&sInstance
);
479 static bool IsA11yBlocked() {
484 return sInstance
->mA11yBlockCount
> 0;
488 TIPMessageHandler() : mHook(nullptr), mA11yBlockCount(0) {
489 MOZ_ASSERT(NS_IsMainThread());
491 // Registered messages used by tiptsf
492 mMessages
[0] = ::RegisterWindowMessage(L
"ImmersiveFocusNotification");
493 mMessages
[1] = ::RegisterWindowMessage(L
"TipCloseMenus");
494 mMessages
[2] = ::RegisterWindowMessage(L
"TabletInputPanelOpening");
495 mMessages
[3] = ::RegisterWindowMessage(L
"IHM Pen or Touch Event noticed");
496 mMessages
[4] = ::RegisterWindowMessage(L
"ProgrammabilityCaretVisibility");
497 mMessages
[5] = ::RegisterWindowMessage(L
"CaretTrackingUpdateIPHidden");
498 mMessages
[6] = ::RegisterWindowMessage(L
"CaretTrackingUpdateIPInfo");
500 mHook
= ::SetWindowsHookEx(WH_GETMESSAGE
, &TIPHook
, nullptr,
501 ::GetCurrentThreadId());
504 if (!sSendMessageTimeoutWStub
) {
505 sUser32Intercept
.Init("user32.dll");
506 DebugOnly
<bool> hooked
= sSendMessageTimeoutWStub
.Set(
507 sUser32Intercept
, "SendMessageTimeoutW", &SendMessageTimeoutWHook
);
512 class MOZ_RAII A11yInstantiationBlocker
{
514 A11yInstantiationBlocker() {
515 if (!TIPMessageHandler::sInstance
) {
518 ++TIPMessageHandler::sInstance
->mA11yBlockCount
;
521 ~A11yInstantiationBlocker() {
522 if (!TIPMessageHandler::sInstance
) {
525 MOZ_ASSERT(TIPMessageHandler::sInstance
->mA11yBlockCount
> 0);
526 --TIPMessageHandler::sInstance
->mA11yBlockCount
;
530 friend class A11yInstantiationBlocker
;
532 static LRESULT CALLBACK
TIPHook(int aCode
, WPARAM aWParam
, LPARAM aLParam
) {
533 if (aCode
< 0 || !sInstance
) {
534 return ::CallNextHookEx(nullptr, aCode
, aWParam
, aLParam
);
537 MSG
* msg
= reinterpret_cast<MSG
*>(aLParam
);
538 UINT
& msgCode
= msg
->message
;
540 for (uint32_t i
= 0; i
< ArrayLength(sInstance
->mMessages
); ++i
) {
541 if (msgCode
== sInstance
->mMessages
[i
]) {
542 A11yInstantiationBlocker block
;
543 return ::CallNextHookEx(nullptr, aCode
, aWParam
, aLParam
);
547 return ::CallNextHookEx(nullptr, aCode
, aWParam
, aLParam
);
550 static LRESULT WINAPI
SendMessageTimeoutWHook(HWND aHwnd
, UINT aMsgCode
,
551 WPARAM aWParam
, LPARAM aLParam
,
552 UINT aFlags
, UINT aTimeout
,
553 PDWORD_PTR aMsgResult
) {
554 // We don't want to handle this unless the message is a WM_GETOBJECT that we
555 // want to block, and the aHwnd is a nsWindow that belongs to the current
556 // (i.e., main) thread.
557 if (!aMsgResult
|| aMsgCode
!= WM_GETOBJECT
||
558 static_cast<LONG
>(aLParam
) != OBJID_CLIENT
|| !::NS_IsMainThread() ||
559 !WinUtils::GetNSWindowPtr(aHwnd
) || !IsA11yBlocked()) {
560 return sSendMessageTimeoutWStub(aHwnd
, aMsgCode
, aWParam
, aLParam
, aFlags
,
561 aTimeout
, aMsgResult
);
564 // In this case we want to fake the result that would happen if we had
565 // decided not to handle WM_GETOBJECT in our WndProc. We hand the message
566 // off to DefWindowProc to accomplish this.
567 *aMsgResult
= static_cast<DWORD_PTR
>(
568 ::DefWindowProcW(aHwnd
, aMsgCode
, aWParam
, aLParam
));
570 return static_cast<LRESULT
>(TRUE
);
573 static WindowsDllInterceptor::FuncHookType
<decltype(&SendMessageTimeoutW
)>
574 sSendMessageTimeoutWStub
;
575 static StaticAutoPtr
<TIPMessageHandler
> sInstance
;
579 uint32_t mA11yBlockCount
;
582 WindowsDllInterceptor::FuncHookType
<decltype(&SendMessageTimeoutW
)>
583 TIPMessageHandler::sSendMessageTimeoutWStub
;
584 StaticAutoPtr
<TIPMessageHandler
> TIPMessageHandler::sInstance
;
586 } // namespace mozilla
588 #endif // defined(ACCESSIBILITY)
592 // This task will get the VirtualDesktopManager from the generic thread pool
593 // since doing this on the main thread on startup causes performance issues.
597 // This should be fine and should not require any locking, as when the main
598 // thread will access it, if it races with this function it will either find
599 // it to be null or to have a valid value.
600 class InitializeVirtualDesktopManagerTask
: public Task
{
602 InitializeVirtualDesktopManagerTask()
603 : Task(Kind::OffMainThreadOnly
, kDefaultPriorityValue
) {}
605 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
606 bool GetName(nsACString
& aName
) override
{
607 aName
.AssignLiteral("InitializeVirtualDesktopManagerTask");
612 virtual TaskResult
Run() override
{
613 RefPtr
<IVirtualDesktopManager
> desktopManager
;
614 HRESULT hr
= ::CoCreateInstance(
615 CLSID_VirtualDesktopManager
, NULL
, CLSCTX_INPROC_SERVER
,
616 __uuidof(IVirtualDesktopManager
), getter_AddRefs(desktopManager
));
618 return TaskResult::Complete
;
621 gVirtualDesktopManager
= desktopManager
;
622 return TaskResult::Complete
;
626 // Ground-truth query: does Windows claim the window is cloaked right now?
627 static bool IsCloaked(HWND hwnd
) {
629 HRESULT hr
= ::DwmGetWindowAttribute(hwnd
, DWMWA_CLOAKED
, &cloakedState
,
630 sizeof(cloakedState
));
633 MOZ_LOG(sCloakingLog
, LogLevel::Warning
,
634 ("failed (%08lX) to query cloaking state for HWND %p", hr
, hwnd
));
638 return cloakedState
!= 0;
641 } // namespace mozilla
643 /**************************************************************
644 **************************************************************
646 ** BLOCK: nsIWidget impl.
648 ** nsIWidget interface implementation, broken down into
651 **************************************************************
652 **************************************************************/
654 /**************************************************************
656 * SECTION: nsWindow construction and destruction
658 **************************************************************/
660 nsWindow::nsWindow(bool aIsChildWindow
)
661 : nsBaseWidget(BorderStyle::Default
),
662 mBrush(::CreateSolidBrush(NSRGB_2_COLOREF(::GetSysColor(COLOR_BTNFACE
)))),
663 mFrameState(std::in_place
, this),
664 mIsChildWindow(aIsChildWindow
),
665 mLastPaintEndTime(TimeStamp::Now()),
666 mCachedHitTestTime(TimeStamp::Now()),
667 mSizeConstraintsScale(GetDefaultScale().scale
),
668 mDesktopId("DesktopIdMutex") {
669 MOZ_ASSERT(mWindowType
== WindowType::Child
);
671 if (!gInitializedVirtualDesktopManager
) {
672 TaskController::Get()->AddTask(
673 MakeAndAddRef
<InitializeVirtualDesktopManagerTask
>());
674 gInitializedVirtualDesktopManager
= true;
677 // Global initialization
678 if (!sInstanceCount
) {
679 // Global app registration id for Win7 and up. See
680 // WinTaskbar.cpp for details.
681 // MSIX packages explicitly do not support setting the appid from within
682 // the app, as it is set in the package manifest instead.
683 if (!WinUtils::HasPackageIdentity()) {
684 mozilla::widget::WinTaskbar::RegisterAppUserModelID();
686 if (!StaticPrefs::ui_key_layout_load_when_first_needed()) {
687 KeyboardLayout::GetInstance()->OnLayoutChange(::GetKeyboardLayout(0));
689 #if defined(ACCESSIBILITY)
690 mozilla::TIPMessageHandler::Initialize();
691 #endif // defined(ACCESSIBILITY)
692 if (SUCCEEDED(::OleInitialize(nullptr))) {
693 sIsOleInitialized
= true;
695 NS_ASSERTION(sIsOleInitialized
, "***** OLE is not initialized!\n");
696 MouseScrollHandler::Initialize();
698 nsUXThemeData::UpdateNativeThemeInfo();
699 RedirectedKeyDownMessageManager::Forget();
705 nsWindow::~nsWindow() {
708 // If the widget was released without calling Destroy() then the native window
709 // still exists, and we need to destroy it. Destroy() will early-return if it
710 // was already called. In any case it is important to call it before
711 // destroying mPresentLock (cf. 1156182).
714 // Free app icon resources. This must happen after `OnDestroy` (see bug
716 if (mIconSmall
) ::DestroyIcon(mIconSmall
);
718 if (mIconBig
) ::DestroyIcon(mIconBig
);
723 if (sInstanceCount
== 0) {
724 IMEHandler::Terminate();
726 if (sIsOleInitialized
) {
727 ::OleFlushClipboard();
729 sIsOleInitialized
= false;
733 NS_IF_RELEASE(mNativeDragTarget
);
736 /**************************************************************
738 * SECTION: nsIWidget::Create, nsIWidget::Destroy
740 * Creating and destroying windows for this widget.
742 **************************************************************/
744 // Allow Derived classes to modify the height that is passed
745 // when the window is created or resized.
746 int32_t nsWindow::GetHeight(int32_t aProposedHeight
) { return aProposedHeight
; }
748 void nsWindow::SendAnAPZEvent(InputData
& aEvent
) {
749 LRESULT popupHandlingResult
;
750 if (DealWithPopups(mWnd
, MOZ_WM_DMANIP
, 0, 0, &popupHandlingResult
)) {
751 // We need to consume the event after using it to roll up the popup(s).
755 if (mSwipeTracker
&& aEvent
.mInputType
== PANGESTURE_INPUT
) {
756 // Give the swipe tracker a first pass at the event. If a new pan gesture
757 // has been started since the beginning of the swipe, the swipe tracker
758 // will know to ignore the event.
759 nsEventStatus status
=
760 mSwipeTracker
->ProcessEvent(aEvent
.AsPanGestureInput());
761 if (status
== nsEventStatus_eConsumeNoDefault
) {
766 APZEventResult result
;
768 result
= mAPZC
->InputBridge()->ReceiveInputEvent(aEvent
);
770 if (result
.GetStatus() == nsEventStatus_eConsumeNoDefault
) {
774 MOZ_ASSERT(aEvent
.mInputType
== PANGESTURE_INPUT
||
775 aEvent
.mInputType
== PINCHGESTURE_INPUT
);
777 if (aEvent
.mInputType
== PANGESTURE_INPUT
) {
778 PanGestureInput
& panInput
= aEvent
.AsPanGestureInput();
779 WidgetWheelEvent event
= panInput
.ToWidgetEvent(this);
781 if (MayStartSwipeForNonAPZ(panInput
)) {
785 event
= MayStartSwipeForAPZ(panInput
, result
);
788 ProcessUntransformedAPZEvent(&event
, result
);
793 PinchGestureInput
& pinchInput
= aEvent
.AsPinchGestureInput();
794 WidgetWheelEvent event
= pinchInput
.ToWidgetEvent(this);
795 ProcessUntransformedAPZEvent(&event
, result
);
798 void nsWindow::RecreateDirectManipulationIfNeeded() {
799 DestroyDirectManipulation();
801 if (mWindowType
!= WindowType::TopLevel
&& mWindowType
!= WindowType::Popup
) {
805 if (!(StaticPrefs::apz_allow_zooming() ||
806 StaticPrefs::apz_windows_use_direct_manipulation()) ||
807 StaticPrefs::apz_windows_force_disable_direct_manipulation()) {
811 mDmOwner
= MakeUnique
<DirectManipulationOwner
>(this);
813 LayoutDeviceIntRect
bounds(mBounds
.X(), mBounds
.Y(), mBounds
.Width(),
814 GetHeight(mBounds
.Height()));
815 mDmOwner
->Init(bounds
);
818 void nsWindow::ResizeDirectManipulationViewport() {
820 LayoutDeviceIntRect
bounds(mBounds
.X(), mBounds
.Y(), mBounds
.Width(),
821 GetHeight(mBounds
.Height()));
822 mDmOwner
->ResizeViewport(bounds
);
826 void nsWindow::DestroyDirectManipulation() {
833 // Create the proper widget
834 nsresult
nsWindow::Create(nsIWidget
* aParent
, nsNativeWidget aNativeParent
,
835 const LayoutDeviceIntRect
& aRect
,
836 widget::InitData
* aInitData
) {
837 // Historical note: there was once some belief and/or intent that nsWindows
838 // could be created on arbitrary threads, and this may still be reflected in
840 MOZ_ASSERT(NS_IsMainThread());
842 widget::InitData defaultInitData
;
843 if (!aInitData
) aInitData
= &defaultInitData
;
845 nsIWidget
* baseParent
=
846 aInitData
->mWindowType
== WindowType::Dialog
||
847 aInitData
->mWindowType
== WindowType::TopLevel
||
848 aInitData
->mWindowType
== WindowType::Invisible
852 mIsTopWidgetWindow
= (nullptr == baseParent
);
855 // Ensure that the toolkit is created.
856 nsToolkit::GetToolkit();
858 BaseCreate(baseParent
, aInitData
);
861 if (aParent
) { // has a nsIWidget parent
862 parent
= aParent
? (HWND
)aParent
->GetNativeData(NS_NATIVE_WINDOW
) : nullptr;
864 } else { // has a nsNative parent
865 parent
= (HWND
)aNativeParent
;
867 aNativeParent
? WinUtils::GetNSWindowPtr((HWND
)aNativeParent
) : nullptr;
870 mIsRTL
= aInitData
->mRTL
;
871 mOpeningAnimationSuppressed
= aInitData
->mIsAnimationSuppressed
;
872 mAlwaysOnTop
= aInitData
->mAlwaysOnTop
;
873 mIsAlert
= aInitData
->mIsAlert
;
874 mResizable
= aInitData
->mResizable
;
876 DWORD style
= WindowStyle();
877 DWORD extendedStyle
= WindowExStyle();
879 if (mWindowType
== WindowType::Popup
) {
883 } else if (mWindowType
== WindowType::Invisible
) {
884 // Make sure CreateWindowEx succeeds at creating a toplevel window
885 style
&= ~0x40000000; // WS_CHILDWINDOW
887 // See if the caller wants to explictly set clip children and clip siblings
888 if (aInitData
->mClipChildren
) {
889 style
|= WS_CLIPCHILDREN
;
891 style
&= ~WS_CLIPCHILDREN
;
893 if (aInitData
->mClipSiblings
) {
894 style
|= WS_CLIPSIBLINGS
;
898 const wchar_t* className
= ChooseWindowClass(mWindowType
);
900 // Take specific actions when creating the first top-level window
901 static bool sFirstTopLevelWindowCreated
= false;
902 if (aInitData
->mWindowType
== WindowType::TopLevel
&& !aParent
&&
903 !sFirstTopLevelWindowCreated
) {
904 sFirstTopLevelWindowCreated
= true;
905 mWnd
= ConsumePreXULSkeletonUIHandle();
906 auto skeletonUIError
= GetPreXULSkeletonUIErrorReason();
907 if (skeletonUIError
) {
908 nsAutoString
errorString(
909 GetPreXULSkeletonUIErrorString(skeletonUIError
.value()));
910 Telemetry::ScalarSet(
911 Telemetry::ScalarID::STARTUP_SKELETON_UI_DISABLED_REASON
,
915 MOZ_ASSERT(style
== kPreXULSkeletonUIWindowStyle
,
916 "The skeleton UI window style should match the expected "
917 "style for the first window created");
918 MOZ_ASSERT(extendedStyle
== kPreXULSkeletonUIWindowStyleEx
,
919 "The skeleton UI window extended style should match the "
920 "expected extended style for the first window created");
922 ::GetWindowThreadProcessId(mWnd
, nullptr) == ::GetCurrentThreadId(),
923 "The skeleton UI window should be created on the same thread as "
925 mIsShowingPreXULSkeletonUI
= true;
927 // If we successfully consumed the pre-XUL skeleton UI, just update
928 // our internal state to match what is currently being displayed.
930 mIsCloaked
= mozilla::IsCloaked(mWnd
);
931 mFrameState
->ConsumePreXULSkeletonState(WasPreXULSkeletonUIMaximized());
933 // These match the margins set in browser-tabsintitlebar.js with
934 // default prefs on Windows. Bug 1673092 tracks lining this up with
935 // that more correctly instead of hard-coding it.
936 SetNonClientMargins(LayoutDeviceIntMargin(0, 2, 2, 2));
938 // Reset the WNDPROC for this window and its whole class, as we had
939 // to use our own WNDPROC when creating the the skeleton UI window.
940 ::SetWindowLongPtrW(mWnd
, GWLP_WNDPROC
,
941 reinterpret_cast<LONG_PTR
>(
942 WinUtils::NonClientDpiScalingDefWindowProcW
));
943 ::SetClassLongPtrW(mWnd
, GCLP_WNDPROC
,
944 reinterpret_cast<LONG_PTR
>(
945 WinUtils::NonClientDpiScalingDefWindowProcW
));
951 ::CreateWindowExW(extendedStyle
, className
, L
"", style
, aRect
.X(),
952 aRect
.Y(), aRect
.Width(), GetHeight(aRect
.Height()),
953 parent
, nullptr, nsToolkit::mDllInstance
, nullptr);
957 NS_WARNING("nsWindow CreateWindowEx failed.");
958 return NS_ERROR_FAILURE
;
961 if (!sWinCloakEventHook
) {
962 MOZ_LOG(sCloakingLog
, LogLevel::Info
, ("Registering cloaking event hook"));
964 // C++03 lambda approximation until P2173R1 is available (-std=c++2b)
965 struct StdcallLambda
{
966 static void CALLBACK
OnCloakUncloakHook(HWINEVENTHOOK hWinEventHook
,
967 DWORD event
, HWND hwnd
,
968 LONG idObject
, LONG idChild
,
970 DWORD dwmsEventTime
) {
971 const bool isCloaked
= event
== EVENT_OBJECT_CLOAKED
? true : false;
972 nsWindow::OnCloakEvent(hwnd
, isCloaked
);
976 const HWINEVENTHOOK hook
= ::SetWinEventHook(
977 EVENT_OBJECT_CLOAKED
, EVENT_OBJECT_UNCLOAKED
, HMODULE(nullptr),
978 &StdcallLambda::OnCloakUncloakHook
, ::GetCurrentProcessId(),
979 ::GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT
);
980 sWinCloakEventHook
= Some(hook
);
983 const DWORD err
= ::GetLastError();
984 MOZ_LOG(sCloakingLog
, LogLevel::Error
,
985 ("Failed to register cloaking event hook! GLE = %lu (0x%lX)", err
,
990 if (aInitData
->mIsPrivate
) {
991 if (NimbusFeatures::GetBool("majorRelease2022"_ns
,
992 "feltPrivacyWindowSeparation"_ns
, true) &&
993 // Although permanent Private Browsing mode is indeed Private Browsing,
994 // we choose to make it look like regular Firefox in terms of the icon
995 // it uses (which also means we shouldn't use the Private Browsing
997 !StaticPrefs::browser_privatebrowsing_autostart()) {
998 RefPtr
<IPropertyStore
> pPropStore
;
999 if (!FAILED(SHGetPropertyStoreForWindow(mWnd
, IID_IPropertyStore
,
1000 getter_AddRefs(pPropStore
)))) {
1003 // make sure we're using the private browsing AUMID so that taskbar
1004 // grouping works properly
1005 Unused
<< NS_WARN_IF(
1006 !mozilla::widget::WinTaskbar::GenerateAppUserModelID(aumid
, true));
1007 if (!FAILED(InitPropVariantFromString(aumid
.get(), &pv
))) {
1008 if (!FAILED(pPropStore
->SetValue(PKEY_AppUserModel_ID
, pv
))) {
1009 pPropStore
->Commit();
1012 PropVariantClear(&pv
);
1015 HICON icon
= ::LoadIconW(::GetModuleHandleW(nullptr),
1016 MAKEINTRESOURCEW(IDI_PBMODE
));
1022 mDeviceNotifyHandle
= InputDeviceUtils::RegisterNotification(mWnd
);
1024 // If mDefaultScale is set before mWnd has been set, it will have the scale of
1025 // the primary monitor, rather than the monitor that the window is actually
1026 // on. For non-popup windows this gets corrected by the WM_DPICHANGED message
1027 // which resets mDefaultScale, but for popup windows we don't reset
1028 // mDefaultScale on that message. In order to ensure that popup windows
1029 // spawned on a non-primary monitor end up with the correct scale, we reset
1030 // mDefaultScale here so that it gets recomputed using the correct monitor now
1031 // that we have a mWnd.
1032 mDefaultScale
= -1.0;
1035 DWORD dwAttribute
= TRUE
;
1036 DwmSetWindowAttribute(mWnd
, DWMWA_NONCLIENT_RTL_LAYOUT
, &dwAttribute
,
1037 sizeof dwAttribute
);
1040 UpdateDarkModeToolbar();
1042 if (mOpeningAnimationSuppressed
) {
1043 SuppressAnimation(true);
1047 ::SetWindowPos(mWnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1048 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
1051 if (mWindowType
!= WindowType::Invisible
&&
1052 MouseScrollHandler::Device::IsFakeScrollableWindowNeeded()) {
1053 // Ugly Thinkpad Driver Hack (Bugs 507222 and 594977)
1055 // We create two zero-sized windows as descendants of the top-level window,
1058 // Top-level window (MozillaWindowClass)
1059 // FAKETRACKPOINTSCROLLCONTAINER (MozillaWindowClass)
1060 // FAKETRACKPOINTSCROLLABLE (MozillaWindowClass)
1062 // We need to have the middle window, otherwise the Trackpoint driver
1063 // will fail to deliver scroll messages. WM_MOUSEWHEEL messages are
1064 // sent to the FAKETRACKPOINTSCROLLABLE, which then propagate up the
1065 // window hierarchy until they are handled by nsWindow::WindowProc.
1066 // WM_HSCROLL messages are also sent to the FAKETRACKPOINTSCROLLABLE,
1067 // but these do not propagate automatically, so we have the window
1068 // procedure pretend that they were dispatched to the top-level window
1071 // The FAKETRACKPOINTSCROLLABLE needs to have the specific window styles it
1072 // is given below so that it catches the Trackpoint driver's heuristics.
1073 HWND scrollContainerWnd
= ::CreateWindowW(
1074 className
, L
"FAKETRACKPOINTSCROLLCONTAINER", WS_CHILD
| WS_VISIBLE
, 0,
1075 0, 0, 0, mWnd
, nullptr, nsToolkit::mDllInstance
, nullptr);
1076 HWND scrollableWnd
= ::CreateWindowW(
1077 className
, L
"FAKETRACKPOINTSCROLLABLE",
1078 WS_CHILD
| WS_VISIBLE
| WS_VSCROLL
| WS_TABSTOP
| 0x30, 0, 0, 0, 0,
1079 scrollContainerWnd
, nullptr, nsToolkit::mDllInstance
, nullptr);
1081 // Give the FAKETRACKPOINTSCROLLABLE window a specific ID so that
1082 // WindowProcInternal can distinguish it from the top-level window
1084 ::SetWindowLongPtrW(scrollableWnd
, GWLP_ID
, eFakeTrackPointScrollableID
);
1086 // Make FAKETRACKPOINTSCROLLABLE use nsWindow::WindowProc, and store the
1087 // old window procedure in its "user data".
1088 WNDPROC oldWndProc
= (WNDPROC
)::SetWindowLongPtrW(
1089 scrollableWnd
, GWLP_WNDPROC
, (LONG_PTR
)nsWindow::WindowProc
);
1090 ::SetWindowLongPtrW(scrollableWnd
, GWLP_USERDATA
, (LONG_PTR
)oldWndProc
);
1093 // We will start receiving native events after associating with our native
1094 // window. We will also become the output of WinUtils::GetNSWindowPtr for that
1096 if (!AssociateWithNativeWindow()) {
1097 return NS_ERROR_FAILURE
;
1100 // Starting with Windows XP, a process always runs within a terminal services
1101 // session. In order to play nicely with RDP, fast user switching, and the
1102 // lock screen, we should be handling WM_WTSSESSION_CHANGE. We must register
1103 // our HWND in order to receive this message.
1104 DebugOnly
<BOOL
> wtsRegistered
=
1105 ::WTSRegisterSessionNotification(mWnd
, NOTIFY_FOR_THIS_SESSION
);
1106 NS_ASSERTION(wtsRegistered
, "WTSRegisterSessionNotification failed!\n");
1108 mDefaultIMC
.Init(this);
1109 IMEHandler::InitInputContext(this, mInputContext
);
1111 static bool a11yPrimed
= false;
1112 if (!a11yPrimed
&& mWindowType
== WindowType::TopLevel
) {
1114 if (Preferences::GetInt("accessibility.force_disabled", 0) == -1) {
1115 ::PostMessage(mWnd
, MOZ_WM_STARTA11Y
, 0, 0);
1119 RecreateDirectManipulationIfNeeded();
1124 void nsWindow::LocalesChanged() {
1125 bool isRTL
= intl::LocaleService::GetInstance()->IsAppLocaleRTL();
1126 if (mIsRTL
!= isRTL
) {
1127 DWORD dwAttribute
= isRTL
;
1128 DwmSetWindowAttribute(mWnd
, DWMWA_NONCLIENT_RTL_LAYOUT
, &dwAttribute
,
1129 sizeof dwAttribute
);
1134 // Close this nsWindow
1135 void nsWindow::Destroy() {
1136 // WM_DESTROY has already fired, avoid calling it twice
1137 if (mOnDestroyCalled
) return;
1139 // Don't destroy windows that have file pickers open, we'll tear these down
1140 // later once the picker is closed.
1141 mDestroyCalled
= true;
1142 if (mPickerDisplayCount
) return;
1144 // During the destruction of all of our children, make sure we don't get
1146 nsCOMPtr
<nsIWidget
> kungFuDeathGrip(this);
1148 DestroyDirectManipulation();
1151 * On windows the LayerManagerOGL destructor wants the widget to be around for
1152 * cleanup. It also would like to have the HWND intact, so we nullptr it here.
1154 DestroyLayerManager();
1156 InputDeviceUtils::UnregisterNotification(mDeviceNotifyHandle
);
1157 mDeviceNotifyHandle
= nullptr;
1159 // The DestroyWindow function destroys the specified window. The function
1160 // sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it
1161 // and remove the keyboard focus from it. The function also destroys the
1162 // window's menu, flushes the thread message queue, destroys timers, removes
1163 // clipboard ownership, and breaks the clipboard viewer chain (if the window
1164 // is at the top of the viewer chain).
1166 // If the specified window is a parent or owner window, DestroyWindow
1167 // automatically destroys the associated child or owned windows when it
1168 // destroys the parent or owner window. The function first destroys child or
1169 // owned windows, and then it destroys the parent or owner window.
1170 VERIFY(::DestroyWindow(mWnd
));
1172 // Our windows can be subclassed which may prevent us receiving WM_DESTROY. If
1173 // OnDestroy() didn't get called, call it now.
1174 if (false == mOnDestroyCalled
) {
1175 MSGResult msgResult
;
1176 mWindowHook
.Notify(mWnd
, WM_DESTROY
, 0, 0, msgResult
);
1181 /**************************************************************
1183 * SECTION: Window class utilities
1185 * Utilities for calculating the proper window class name for
1188 **************************************************************/
1191 const wchar_t* nsWindow::RegisterWindowClass(const wchar_t* aClassName
,
1192 UINT aExtraStyle
, LPWSTR aIconID
) {
1194 if (::GetClassInfoW(nsToolkit::mDllInstance
, aClassName
, &wc
)) {
1195 // already registered
1199 wc
.style
= CS_DBLCLKS
| aExtraStyle
;
1200 wc
.lpfnWndProc
= WinUtils::NonClientDpiScalingDefWindowProcW
;
1203 wc
.hInstance
= nsToolkit::mDllInstance
;
1205 aIconID
? ::LoadIconW(::GetModuleHandleW(nullptr), aIconID
) : nullptr;
1206 wc
.hCursor
= nullptr;
1207 wc
.hbrBackground
= nullptr;
1208 wc
.lpszMenuName
= nullptr;
1209 wc
.lpszClassName
= aClassName
;
1211 if (!::RegisterClassW(&wc
)) {
1212 // For older versions of Win32 (i.e., not XP), the registration may
1213 // fail with aExtraStyle, so we have to re-register without it.
1214 wc
.style
= CS_DBLCLKS
;
1215 ::RegisterClassW(&wc
);
1220 static LPWSTR
const gStockApplicationIcon
= MAKEINTRESOURCEW(32512);
1223 const wchar_t* nsWindow::ChooseWindowClass(WindowType aWindowType
) {
1224 switch (aWindowType
) {
1225 case WindowType::Invisible
:
1226 return RegisterWindowClass(kClassNameHidden
, 0, gStockApplicationIcon
);
1227 case WindowType::Dialog
:
1228 return RegisterWindowClass(kClassNameDialog
, 0, 0);
1229 case WindowType::Popup
:
1230 return RegisterWindowClass(kClassNameDropShadow
, CS_DROPSHADOW
,
1231 gStockApplicationIcon
);
1233 return RegisterWindowClass(GetMainWindowClass(), 0,
1234 gStockApplicationIcon
);
1238 /**************************************************************
1240 * SECTION: Window styles utilities
1242 * Return the proper windows styles and extended styles.
1244 **************************************************************/
1246 // Return nsWindow styles
1247 DWORD
nsWindow::WindowStyle() {
1250 switch (mWindowType
) {
1251 case WindowType::Child
:
1252 style
= WS_OVERLAPPED
;
1255 case WindowType::Dialog
:
1256 style
= WS_OVERLAPPED
| WS_BORDER
| WS_DLGFRAME
| WS_SYSMENU
| DS_3DLOOK
|
1257 DS_MODALFRAME
| WS_CLIPCHILDREN
;
1258 if (mBorderStyle
!= BorderStyle::Default
)
1259 style
|= WS_THICKFRAME
| WS_MINIMIZEBOX
| WS_MAXIMIZEBOX
;
1262 case WindowType::Popup
:
1263 style
= WS_POPUP
| WS_OVERLAPPED
;
1267 NS_ERROR("unknown border style");
1270 case WindowType::TopLevel
:
1271 case WindowType::Invisible
:
1272 style
= WS_OVERLAPPED
| WS_BORDER
| WS_DLGFRAME
| WS_SYSMENU
|
1273 WS_THICKFRAME
| WS_MINIMIZEBOX
| WS_MAXIMIZEBOX
| WS_CLIPCHILDREN
;
1277 if (mBorderStyle
!= BorderStyle::Default
&&
1278 mBorderStyle
!= BorderStyle::All
) {
1279 if (mBorderStyle
== BorderStyle::None
||
1280 !(mBorderStyle
& BorderStyle::Border
))
1281 style
&= ~WS_BORDER
;
1283 if (mBorderStyle
== BorderStyle::None
||
1284 !(mBorderStyle
& BorderStyle::Title
)) {
1285 style
&= ~WS_DLGFRAME
;
1288 if (mBorderStyle
== BorderStyle::None
||
1289 !(mBorderStyle
& BorderStyle::Close
))
1291 // XXX The close box can only be removed by changing the window class,
1292 // as far as I know --- roc+moz@cs.cmu.edu
1294 if (mBorderStyle
== BorderStyle::None
||
1295 !(mBorderStyle
& (BorderStyle::Menu
| BorderStyle::Close
)))
1296 style
&= ~WS_SYSMENU
;
1297 // Looks like getting rid of the system menu also does away with the
1298 // close box. So, we only get rid of the system menu if you want neither it
1299 // nor the close box. How does the Windows "Dialog" window class get just
1300 // closebox and no sysmenu? Who knows.
1302 if (mBorderStyle
== BorderStyle::None
||
1303 !(mBorderStyle
& BorderStyle::ResizeH
))
1304 style
&= ~WS_THICKFRAME
;
1306 if (mBorderStyle
== BorderStyle::None
||
1307 !(mBorderStyle
& BorderStyle::Minimize
))
1308 style
&= ~WS_MINIMIZEBOX
;
1310 if (mBorderStyle
== BorderStyle::None
||
1311 !(mBorderStyle
& BorderStyle::Maximize
))
1312 style
&= ~WS_MAXIMIZEBOX
;
1314 if (IsPopupWithTitleBar()) {
1315 style
|= WS_CAPTION
;
1316 if (mBorderStyle
& BorderStyle::Close
) {
1317 style
|= WS_SYSMENU
;
1322 if (mIsChildWindow
) {
1323 style
|= WS_CLIPCHILDREN
;
1324 if (!(style
& WS_POPUP
)) {
1325 style
|= WS_CHILD
; // WS_POPUP and WS_CHILD are mutually exclusive.
1329 VERIFY_WINDOW_STYLE(style
);
1333 // Return nsWindow extended styles
1334 DWORD
nsWindow::WindowExStyle() {
1335 switch (mWindowType
) {
1336 case WindowType::Child
:
1339 case WindowType::Popup
: {
1340 DWORD extendedStyle
= WS_EX_TOOLWINDOW
;
1341 if (mPopupLevel
== PopupLevel::Top
) {
1342 extendedStyle
|= WS_EX_TOPMOST
;
1344 return extendedStyle
;
1347 case WindowType::Sheet
:
1348 MOZ_FALLTHROUGH_ASSERT("Sheets are macOS specific");
1349 case WindowType::Dialog
:
1350 case WindowType::TopLevel
:
1351 case WindowType::Invisible
:
1355 MOZ_ASSERT(mWindowType
== WindowType::Dialog
,
1356 "Expect alert windows to have type=dialog");
1357 return WS_EX_TOOLWINDOW
;
1359 return WS_EX_WINDOWEDGE
;
1362 /**************************************************************
1364 * SECTION: Native window association utilities
1366 * Used in Create and Destroy. A nsWindow can associate with its
1367 * underlying native window mWnd. Once a native window is
1368 * associated with a nsWindow, its native events will be handled
1369 * by the static member function nsWindow::WindowProc. Moreover,
1370 * the association will be registered in the WinUtils association
1371 * list, that is, calling WinUtils::GetNSWindowPtr on the native
1372 * window will return the associated nsWindow. This is used in
1373 * nsWindow::WindowProc to correctly dispatch native events to
1374 * the handler methods defined in nsWindow, even though it is a
1375 * static member function.
1377 * After dissociation, the native events of the native window will
1378 * no longer be handled by nsWindow::WindowProc, and will thus not
1379 * be dispatched to the nsWindow native event handler methods.
1380 * Moreover, the association will no longer be registered in the
1381 * WinUtils association list, so calling WinUtils::GetNSWindowPtr
1382 * on the native window will return nullptr.
1384 **************************************************************/
1386 bool nsWindow::AssociateWithNativeWindow() {
1387 if (!mWnd
|| !IsWindow(mWnd
)) {
1388 NS_ERROR("Invalid window handle");
1392 // Connect the this pointer to the native window handle.
1393 // This should be done before SetWindowLongPtrW, because nsWindow::WindowProc
1394 // uses WinUtils::GetNSWindowPtr internally.
1395 WinUtils::SetNSWindowPtr(mWnd
, this);
1397 ::SetLastError(ERROR_SUCCESS
);
1398 const auto prevWndProc
= reinterpret_cast<WNDPROC
>(::SetWindowLongPtrW(
1399 mWnd
, GWLP_WNDPROC
, reinterpret_cast<LONG_PTR
>(nsWindow::WindowProc
)));
1400 if (!prevWndProc
&& GetLastError() != ERROR_SUCCESS
) {
1401 NS_ERROR("Failure in SetWindowLongPtrW");
1402 WinUtils::SetNSWindowPtr(mWnd
, nullptr);
1406 mPrevWndProc
.emplace(prevWndProc
);
1410 void nsWindow::DissociateFromNativeWindow() {
1411 if (!mWnd
|| !IsWindow(mWnd
) || mPrevWndProc
.isNothing()) {
1415 DebugOnly
<WNDPROC
> wndProcBeforeDissociate
=
1416 reinterpret_cast<WNDPROC
>(::SetWindowLongPtrW(
1417 mWnd
, GWLP_WNDPROC
, reinterpret_cast<LONG_PTR
>(*mPrevWndProc
)));
1418 NS_ASSERTION(wndProcBeforeDissociate
== nsWindow::WindowProc
,
1419 "Unstacked an unexpected native window procedure");
1421 WinUtils::SetNSWindowPtr(mWnd
, nullptr);
1422 mPrevWndProc
.reset();
1425 /**************************************************************
1427 * SECTION: nsIWidget::SetParent, nsIWidget::GetParent
1429 * Set or clear the parent widgets using window properties, and
1430 * handles calculating native parent handles.
1432 **************************************************************/
1434 // Get and set parent widgets
1435 void nsWindow::SetParent(nsIWidget
* aNewParent
) {
1436 nsCOMPtr
<nsIWidget
> kungFuDeathGrip(this);
1437 nsIWidget
* parent
= GetParent();
1439 parent
->RemoveChild(this);
1442 mParent
= aNewParent
;
1445 ReparentNativeWidget(aNewParent
);
1446 aNewParent
->AddChild(this);
1450 // If we have no parent, SetParent should return the desktop.
1451 VERIFY(::SetParent(mWnd
, nullptr));
1452 RecreateDirectManipulationIfNeeded();
1456 void nsWindow::ReparentNativeWidget(nsIWidget
* aNewParent
) {
1457 MOZ_ASSERT(aNewParent
, "null widget");
1459 mParent
= aNewParent
;
1460 if (mWindowType
== WindowType::Popup
) {
1463 HWND newParent
= (HWND
)aNewParent
->GetNativeData(NS_NATIVE_WINDOW
);
1464 NS_ASSERTION(newParent
, "Parent widget has a null native window handle");
1465 if (newParent
&& mWnd
) {
1466 ::SetParent(mWnd
, newParent
);
1467 RecreateDirectManipulationIfNeeded();
1471 nsIWidget
* nsWindow::GetParent(void) {
1472 if (mIsTopWidgetWindow
) {
1475 if (mInDtor
|| mOnDestroyCalled
) {
1481 static int32_t RoundDown(double aDouble
) {
1482 return aDouble
> 0 ? static_cast<int32_t>(floor(aDouble
))
1483 : static_cast<int32_t>(ceil(aDouble
));
1486 float nsWindow::GetDPI() {
1488 nsCOMPtr
<nsIScreen
> screen
= GetWidgetScreen();
1490 screen
->GetDpi(&dpi
);
1495 double nsWindow::GetDefaultScaleInternal() {
1496 if (mDefaultScale
<= 0.0) {
1497 mDefaultScale
= WinUtils::LogToPhysFactor(mWnd
);
1499 return mDefaultScale
;
1502 int32_t nsWindow::LogToPhys(double aValue
) {
1503 return WinUtils::LogToPhys(
1504 ::MonitorFromWindow(mWnd
, MONITOR_DEFAULTTOPRIMARY
), aValue
);
1507 nsWindow
* nsWindow::GetParentWindow(bool aIncludeOwner
) {
1508 return static_cast<nsWindow
*>(GetParentWindowBase(aIncludeOwner
));
1511 nsWindow
* nsWindow::GetParentWindowBase(bool aIncludeOwner
) {
1512 if (mIsTopWidgetWindow
) {
1513 // Must use a flag instead of mWindowType to tell if the window is the
1514 // owned by the topmost widget, because a child window can be embedded
1515 // inside a HWND which is not associated with a nsIWidget.
1519 // If this widget has already been destroyed, pretend we have no parent.
1520 // This corresponds to code in Destroy which removes the destroyed
1521 // widget from its parent's child list.
1522 if (mInDtor
|| mOnDestroyCalled
) return nullptr;
1524 // aIncludeOwner set to true implies walking the parent chain to retrieve the
1525 // root owner. aIncludeOwner set to false implies the search will stop at the
1526 // true parent (default).
1527 nsWindow
* widget
= nullptr;
1529 HWND parent
= nullptr;
1531 parent
= ::GetParent(mWnd
);
1533 parent
= ::GetAncestor(mWnd
, GA_PARENT
);
1536 widget
= WinUtils::GetNSWindowPtr(parent
);
1538 // If the widget is in the process of being destroyed then
1540 if (widget
->mInDtor
) {
1550 /**************************************************************
1552 * SECTION: nsIWidget::Show
1554 * Hide or show this component.
1556 **************************************************************/
1558 void nsWindow::Show(bool bState
) {
1559 if (bState
&& mIsShowingPreXULSkeletonUI
) {
1560 // The first time we decide to actually show the window is when we decide
1561 // that we've taken over the window from the skeleton UI, and we should
1562 // no longer treat resizes / moves specially.
1563 mIsShowingPreXULSkeletonUI
= false;
1564 #if defined(ACCESSIBILITY)
1565 // If our HWND has focus and the a11y engine hasn't started yet, fire a
1566 // focus win event. Windows already did this when the skeleton UI appeared,
1567 // but a11y wouldn't have been able to start at that point even if a client
1568 // responded. Firing this now gives clients the chance to respond with
1569 // WM_GETOBJECT, which will trigger the a11y engine. We don't want to do
1570 // this if the a11y engine has already started because it has probably
1571 // already fired focus on a descendant.
1572 if (::GetFocus() == mWnd
&& !GetAccService()) {
1573 ::NotifyWinEvent(EVENT_OBJECT_FOCUS
, mWnd
, OBJID_CLIENT
, CHILDID_SELF
);
1575 #endif // defined(ACCESSIBILITY)
1578 if (mWindowType
== WindowType::Popup
) {
1579 MOZ_ASSERT(ChooseWindowClass(mWindowType
) == kClassNameDropShadow
);
1580 const bool shouldUseDropShadow
=
1581 mTransparencyMode
!= TransparencyMode::Transparent
;
1583 static bool sShadowEnabled
= true;
1584 if (sShadowEnabled
!= shouldUseDropShadow
) {
1585 ::SetClassLongA(mWnd
, GCL_STYLE
, shouldUseDropShadow
? CS_DROPSHADOW
: 0);
1586 sShadowEnabled
= shouldUseDropShadow
;
1589 // WS_EX_COMPOSITED conflicts with the WS_EX_LAYERED style and causes
1590 // some popup menus to become invisible.
1591 LONG_PTR exStyle
= ::GetWindowLongPtrW(mWnd
, GWL_EXSTYLE
);
1592 if (exStyle
& WS_EX_LAYERED
) {
1593 ::SetWindowLongPtrW(mWnd
, GWL_EXSTYLE
, exStyle
& ~WS_EX_COMPOSITED
);
1597 bool syncInvalidate
= false;
1599 bool wasVisible
= mIsVisible
;
1600 // Set the status now so that anyone asking during ShowWindow or
1601 // SetWindowPos would get the correct answer.
1602 mIsVisible
= bState
;
1604 // We may have cached an out of date visible state. This can happen
1605 // when session restore sets the full screen mode.
1607 mOldStyle
|= WS_VISIBLE
;
1609 mOldStyle
&= ~WS_VISIBLE
;
1613 if (!wasVisible
&& mWindowType
== WindowType::TopLevel
) {
1614 // speed up the initial paint after show for
1615 // top level windows:
1616 syncInvalidate
= true;
1618 // Set the cursor before showing the window to avoid the default wait
1620 SetCursor(Cursor
{eCursor_standard
});
1622 switch (mFrameState
->GetSizeMode()) {
1623 case nsSizeMode_Fullscreen
:
1624 ::ShowWindow(mWnd
, SW_SHOW
);
1626 case nsSizeMode_Maximized
:
1627 ::ShowWindow(mWnd
, SW_SHOWMAXIMIZED
);
1629 case nsSizeMode_Minimized
:
1630 ::ShowWindow(mWnd
, SW_SHOWMINIMIZED
);
1633 if (CanTakeFocus() && !mAlwaysOnTop
) {
1634 ::ShowWindow(mWnd
, SW_SHOWNORMAL
);
1636 ::ShowWindow(mWnd
, SW_SHOWNOACTIVATE
);
1637 // Don't flicker the window if we're restoring session
1638 if (!sIsRestoringSession
) {
1639 Unused
<< GetAttention(2);
1645 DWORD flags
= SWP_NOSIZE
| SWP_NOMOVE
| SWP_SHOWWINDOW
;
1647 flags
|= SWP_NOZORDER
;
1649 if (mAlwaysOnTop
|| mIsAlert
) {
1650 flags
|= SWP_NOACTIVATE
;
1653 if (mWindowType
== WindowType::Popup
) {
1654 // ensure popups are the topmost of the TOPMOST
1655 // layer. Remember not to set the SWP_NOZORDER
1656 // flag as that might allow the taskbar to overlap
1658 flags
|= SWP_NOACTIVATE
;
1659 HWND owner
= ::GetWindow(mWnd
, GW_OWNER
);
1661 // PopupLevel::Top popups should be above all else. All other
1662 // types should be placed in front of their owner, without
1663 // changing the owner's z-level relative to other windows.
1664 if (mPopupLevel
!= PopupLevel::Top
) {
1665 ::SetWindowPos(mWnd
, owner
, 0, 0, 0, 0, flags
);
1666 ::SetWindowPos(owner
, mWnd
, 0, 0, 0, 0,
1667 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
1669 ::SetWindowPos(mWnd
, HWND_TOP
, 0, 0, 0, 0, flags
);
1672 ::SetWindowPos(mWnd
, HWND_TOPMOST
, 0, 0, 0, 0, flags
);
1675 if (mWindowType
== WindowType::Dialog
&& !CanTakeFocus())
1676 flags
|= SWP_NOACTIVATE
;
1678 ::SetWindowPos(mWnd
, HWND_TOP
, 0, 0, 0, 0, flags
);
1682 // Clear contents to avoid ghosting of old content if we display
1683 // this window again.
1684 if (wasVisible
&& mTransparencyMode
== TransparencyMode::Transparent
) {
1685 if (mCompositorWidgetDelegate
) {
1686 mCompositorWidgetDelegate
->ClearTransparentWindow();
1689 if (mWindowType
!= WindowType::Dialog
) {
1690 ::ShowWindow(mWnd
, SW_HIDE
);
1692 ::SetWindowPos(mWnd
, 0, 0, 0, 0, 0,
1693 SWP_HIDEWINDOW
| SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOZORDER
|
1699 if (!wasVisible
&& bState
) {
1701 if (syncInvalidate
&& !mInDtor
&& !mOnDestroyCalled
) {
1702 ::UpdateWindow(mWnd
);
1706 if (mOpeningAnimationSuppressed
) {
1707 SuppressAnimation(false);
1711 /**************************************************************
1713 * SECTION: nsIWidget::IsVisible
1715 * Returns the visibility state.
1717 **************************************************************/
1719 // Return true if the component is visible, false otherwise.
1721 // This does not take cloaking into account.
1722 bool nsWindow::IsVisible() const { return mIsVisible
; }
1724 /**************************************************************
1726 * SECTION: Window clipping utilities
1728 * Used in Size and Move operations for setting the proper
1729 * window clipping regions for window transparency.
1731 **************************************************************/
1733 // XP and Vista visual styles sometimes require window clipping regions to be
1734 // applied for proper transparency. These routines are called on size and move
1736 // XXX this is apparently still needed in Windows 7 and later
1737 void nsWindow::ClearThemeRegion() {
1738 if (mWindowType
== WindowType::Popup
&& !IsPopupWithTitleBar() &&
1739 (mPopupType
== PopupType::Tooltip
|| mPopupType
== PopupType::Panel
)) {
1740 SetWindowRgn(mWnd
, nullptr, false);
1744 /**************************************************************
1746 * SECTION: Touch and APZ-related functions
1748 **************************************************************/
1750 void nsWindow::RegisterTouchWindow() {
1751 mTouchWindow
= true;
1752 ::RegisterTouchWindow(mWnd
, TWF_WANTPALM
);
1753 ::EnumChildWindows(mWnd
, nsWindow::RegisterTouchForDescendants
, 0);
1756 BOOL CALLBACK
nsWindow::RegisterTouchForDescendants(HWND aWnd
, LPARAM aMsg
) {
1757 nsWindow
* win
= WinUtils::GetNSWindowPtr(aWnd
);
1759 ::RegisterTouchWindow(aWnd
, TWF_WANTPALM
);
1764 void nsWindow::LockAspectRatio(bool aShouldLock
) {
1766 mAspectRatio
= (float)mBounds
.Width() / (float)mBounds
.Height();
1772 /**************************************************************
1774 * SECTION: nsIWidget::SetInputRegion
1776 * Sets whether the window should ignore mouse events.
1778 **************************************************************/
1779 void nsWindow::SetInputRegion(const InputRegion
& aInputRegion
) {
1780 mInputRegion
= aInputRegion
;
1783 /**************************************************************
1785 * SECTION: nsIWidget::Move, nsIWidget::Resize, nsIWidget::Size
1787 * Repositioning and sizing a window.
1789 **************************************************************/
1791 void nsWindow::SetSizeConstraints(const SizeConstraints
& aConstraints
) {
1792 SizeConstraints c
= aConstraints
;
1794 if (mWindowType
!= WindowType::Popup
&& mResizable
) {
1796 std::max(int32_t(::GetSystemMetrics(SM_CXMINTRACK
)), c
.mMinSize
.width
);
1798 std::max(int32_t(::GetSystemMetrics(SM_CYMINTRACK
)), c
.mMinSize
.height
);
1801 if (mMaxTextureSize
> 0) {
1802 // We can't make ThebesLayers bigger than this anyway.. no point it letting
1803 // a window grow bigger as we won't be able to draw content there in
1805 c
.mMaxSize
.width
= std::min(c
.mMaxSize
.width
, mMaxTextureSize
);
1806 c
.mMaxSize
.height
= std::min(c
.mMaxSize
.height
, mMaxTextureSize
);
1809 mSizeConstraintsScale
= GetDefaultScale().scale
;
1811 nsBaseWidget::SetSizeConstraints(c
);
1814 const SizeConstraints
nsWindow::GetSizeConstraints() {
1815 double scale
= GetDefaultScale().scale
;
1816 if (mSizeConstraintsScale
== scale
|| mSizeConstraintsScale
== 0.0) {
1817 return mSizeConstraints
;
1819 scale
/= mSizeConstraintsScale
;
1820 SizeConstraints c
= mSizeConstraints
;
1821 if (c
.mMinSize
.width
!= NS_MAXSIZE
) {
1822 c
.mMinSize
.width
= NSToIntRound(c
.mMinSize
.width
* scale
);
1824 if (c
.mMinSize
.height
!= NS_MAXSIZE
) {
1825 c
.mMinSize
.height
= NSToIntRound(c
.mMinSize
.height
* scale
);
1827 if (c
.mMaxSize
.width
!= NS_MAXSIZE
) {
1828 c
.mMaxSize
.width
= NSToIntRound(c
.mMaxSize
.width
* scale
);
1830 if (c
.mMaxSize
.height
!= NS_MAXSIZE
) {
1831 c
.mMaxSize
.height
= NSToIntRound(c
.mMaxSize
.height
* scale
);
1836 // Move this component
1837 void nsWindow::Move(double aX
, double aY
) {
1838 if (mWindowType
== WindowType::TopLevel
||
1839 mWindowType
== WindowType::Dialog
) {
1840 SetSizeMode(nsSizeMode_Normal
);
1843 // for top-level windows only, convert coordinates from desktop pixels
1844 // (the "parent" coordinate space) to the window's device pixel space
1846 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale
: 1.0;
1847 int32_t x
= NSToIntRound(aX
* scale
);
1848 int32_t y
= NSToIntRound(aY
* scale
);
1850 // Check to see if window needs to be moved first
1851 // to avoid a costly call to SetWindowPos. This check
1852 // can not be moved to the calling code in nsView, because
1853 // some platforms do not position child windows correctly
1855 // Only perform this check for non-popup windows, since the positioning can
1856 // in fact change even when the x/y do not. We always need to perform the
1857 // check. See bug #97805 for details.
1858 if (mWindowType
!= WindowType::Popup
&& mBounds
.IsEqualXY(x
, y
)) {
1859 // Nothing to do, since it is already positioned correctly.
1863 mBounds
.MoveTo(x
, y
);
1867 // complain if a window is moved offscreen (legal, but potentially
1869 if (mIsTopWidgetWindow
) { // only a problem for top-level windows
1870 // Make sure this window is actually on the screen before we move it
1871 // XXX: Needs multiple monitor support
1872 HDC dc
= ::GetDC(mWnd
);
1874 if (::GetDeviceCaps(dc
, TECHNOLOGY
) == DT_RASDISPLAY
) {
1876 ::SystemParametersInfo(SPI_GETWORKAREA
, 0, &workArea
, 0);
1877 // no annoying assertions. just mention the issue.
1878 if (x
< 0 || x
>= workArea
.right
|| y
< 0 || y
>= workArea
.bottom
) {
1879 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
1880 ("window moved to offscreen position\n"));
1883 ::ReleaseDC(mWnd
, dc
);
1888 // Normally, when the skeleton UI is disabled, we resize+move the window
1889 // before showing it in order to ensure that it restores to the correct
1890 // position when the user un-maximizes it. However, when we are using the
1891 // skeleton UI, this results in the skeleton UI window being moved around
1892 // undesirably before being locked back into the maximized position. To
1893 // avoid this, we simply set the placement to restore to via
1894 // SetWindowPlacement. It's a little bit more of a dance, though, since we
1895 // need to convert the workspace coords that SetWindowPlacement uses to the
1896 // screen space coordinates we normally use with SetWindowPos.
1897 if (mIsShowingPreXULSkeletonUI
&& WasPreXULSkeletonUIMaximized()) {
1898 WINDOWPLACEMENT pl
= {sizeof(WINDOWPLACEMENT
)};
1899 VERIFY(::GetWindowPlacement(mWnd
, &pl
));
1901 HMONITOR monitor
= ::MonitorFromWindow(mWnd
, MONITOR_DEFAULTTONULL
);
1902 if (NS_WARN_IF(!monitor
)) {
1905 MONITORINFO mi
= {sizeof(MONITORINFO
)};
1906 VERIFY(::GetMonitorInfo(monitor
, &mi
));
1909 x
+ mi
.rcWork
.left
- mi
.rcMonitor
.left
- pl
.rcNormalPosition
.left
;
1911 y
+ mi
.rcWork
.top
- mi
.rcMonitor
.top
- pl
.rcNormalPosition
.top
;
1912 pl
.rcNormalPosition
.left
+= deltaX
;
1913 pl
.rcNormalPosition
.right
+= deltaX
;
1914 pl
.rcNormalPosition
.top
+= deltaY
;
1915 pl
.rcNormalPosition
.bottom
+= deltaY
;
1916 VERIFY(::SetWindowPlacement(mWnd
, &pl
));
1920 UINT flags
= SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOSIZE
;
1921 double oldScale
= mDefaultScale
;
1922 mResizeState
= IN_SIZEMOVE
;
1923 VERIFY(::SetWindowPos(mWnd
, nullptr, x
, y
, 0, 0, flags
));
1924 mResizeState
= NOT_RESIZING
;
1925 if (WinUtils::LogToPhysFactor(mWnd
) != oldScale
) {
1930 ResizeDirectManipulationViewport();
1934 // Resize this component
1935 void nsWindow::Resize(double aWidth
, double aHeight
, bool aRepaint
) {
1936 // for top-level windows only, convert coordinates from desktop pixels
1937 // (the "parent" coordinate space) to the window's device pixel space
1939 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale
: 1.0;
1940 int32_t width
= NSToIntRound(aWidth
* scale
);
1941 int32_t height
= NSToIntRound(aHeight
* scale
);
1943 NS_ASSERTION((width
>= 0), "Negative width passed to nsWindow::Resize");
1944 NS_ASSERTION((height
>= 0), "Negative height passed to nsWindow::Resize");
1945 if (width
< 0 || height
< 0) {
1946 gfxCriticalNoteOnce
<< "Negative passed to Resize(" << width
<< ", "
1947 << height
<< ") repaint: " << aRepaint
;
1950 ConstrainSize(&width
, &height
);
1952 // Avoid unnecessary resizing calls
1953 if (mBounds
.IsEqualSize(width
, height
)) {
1960 // Set cached value for lightweight and printing
1961 bool wasLocking
= mAspectRatio
!= 0.0;
1962 mBounds
.SizeTo(width
, height
);
1964 LockAspectRatio(true); // This causes us to refresh the mAspectRatio value
1968 // Refer to the comment above a similar check in nsWindow::Move
1969 if (mIsShowingPreXULSkeletonUI
&& WasPreXULSkeletonUIMaximized()) {
1970 WINDOWPLACEMENT pl
= {sizeof(WINDOWPLACEMENT
)};
1971 VERIFY(::GetWindowPlacement(mWnd
, &pl
));
1972 pl
.rcNormalPosition
.right
= pl
.rcNormalPosition
.left
+ width
;
1973 pl
.rcNormalPosition
.bottom
= pl
.rcNormalPosition
.top
+ GetHeight(height
);
1974 mResizeState
= RESIZING
;
1975 VERIFY(::SetWindowPlacement(mWnd
, &pl
));
1976 mResizeState
= NOT_RESIZING
;
1978 UINT flags
= SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOMOVE
;
1981 flags
|= SWP_NOREDRAW
;
1985 double oldScale
= mDefaultScale
;
1986 mResizeState
= RESIZING
;
1988 ::SetWindowPos(mWnd
, nullptr, 0, 0, width
, GetHeight(height
), flags
));
1990 mResizeState
= NOT_RESIZING
;
1991 if (WinUtils::LogToPhysFactor(mWnd
) != oldScale
) {
1996 ResizeDirectManipulationViewport();
1999 if (aRepaint
) Invalidate();
2002 // Resize this component
2003 void nsWindow::Resize(double aX
, double aY
, double aWidth
, double aHeight
,
2005 // for top-level windows only, convert coordinates from desktop pixels
2006 // (the "parent" coordinate space) to the window's device pixel space
2008 BoundsUseDesktopPixels() ? GetDesktopToDeviceScale().scale
: 1.0;
2009 int32_t x
= NSToIntRound(aX
* scale
);
2010 int32_t y
= NSToIntRound(aY
* scale
);
2011 int32_t width
= NSToIntRound(aWidth
* scale
);
2012 int32_t height
= NSToIntRound(aHeight
* scale
);
2014 NS_ASSERTION((width
>= 0), "Negative width passed to nsWindow::Resize");
2015 NS_ASSERTION((height
>= 0), "Negative height passed to nsWindow::Resize");
2016 if (width
< 0 || height
< 0) {
2017 gfxCriticalNoteOnce
<< "Negative passed to Resize(" << x
<< " ," << y
2018 << ", " << width
<< ", " << height
2019 << ") repaint: " << aRepaint
;
2022 ConstrainSize(&width
, &height
);
2024 // Avoid unnecessary resizing calls
2025 if (mBounds
.IsEqualRect(x
, y
, width
, height
)) {
2032 // Set cached value for lightweight and printing
2033 mBounds
.SetRect(x
, y
, width
, height
);
2036 // Refer to the comment above a similar check in nsWindow::Move
2037 if (mIsShowingPreXULSkeletonUI
&& WasPreXULSkeletonUIMaximized()) {
2038 WINDOWPLACEMENT pl
= {sizeof(WINDOWPLACEMENT
)};
2039 VERIFY(::GetWindowPlacement(mWnd
, &pl
));
2041 HMONITOR monitor
= ::MonitorFromWindow(mWnd
, MONITOR_DEFAULTTONULL
);
2042 if (NS_WARN_IF(!monitor
)) {
2045 MONITORINFO mi
= {sizeof(MONITORINFO
)};
2046 VERIFY(::GetMonitorInfo(monitor
, &mi
));
2049 x
+ mi
.rcWork
.left
- mi
.rcMonitor
.left
- pl
.rcNormalPosition
.left
;
2051 y
+ mi
.rcWork
.top
- mi
.rcMonitor
.top
- pl
.rcNormalPosition
.top
;
2052 pl
.rcNormalPosition
.left
+= deltaX
;
2053 pl
.rcNormalPosition
.right
= pl
.rcNormalPosition
.left
+ width
;
2054 pl
.rcNormalPosition
.top
+= deltaY
;
2055 pl
.rcNormalPosition
.bottom
= pl
.rcNormalPosition
.top
+ GetHeight(height
);
2056 VERIFY(::SetWindowPlacement(mWnd
, &pl
));
2058 UINT flags
= SWP_NOZORDER
| SWP_NOACTIVATE
;
2060 flags
|= SWP_NOREDRAW
;
2065 double oldScale
= mDefaultScale
;
2066 mResizeState
= RESIZING
;
2068 ::SetWindowPos(mWnd
, nullptr, x
, y
, width
, GetHeight(height
), flags
));
2069 mResizeState
= NOT_RESIZING
;
2070 if (WinUtils::LogToPhysFactor(mWnd
) != oldScale
) {
2074 if (mTransitionWnd
) {
2075 // If we have a fullscreen transition window, we need to make
2076 // it topmost again, otherwise the taskbar may be raised by
2077 // the system unexpectedly when we leave fullscreen state.
2078 ::SetWindowPos(mTransitionWnd
, HWND_TOPMOST
, 0, 0, 0, 0,
2079 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
);
2083 ResizeDirectManipulationViewport();
2086 if (aRepaint
) Invalidate();
2089 mozilla::Maybe
<bool> nsWindow::IsResizingNativeWidget() {
2090 if (mResizeState
== RESIZING
) {
2096 /**************************************************************
2098 * SECTION: Window Z-order and state.
2100 * nsIWidget::PlaceBehind, nsIWidget::SetSizeMode,
2101 * nsIWidget::ConstrainPosition
2103 * Z-order, positioning, restore, minimize, and maximize.
2105 **************************************************************/
2107 // Position the window behind the given window
2108 void nsWindow::PlaceBehind(nsTopLevelWidgetZPlacement aPlacement
,
2109 nsIWidget
* aWidget
, bool aActivate
) {
2110 HWND behind
= HWND_TOP
;
2111 if (aPlacement
== eZPlacementBottom
)
2112 behind
= HWND_BOTTOM
;
2113 else if (aPlacement
== eZPlacementBelow
&& aWidget
)
2114 behind
= (HWND
)aWidget
->GetNativeData(NS_NATIVE_WINDOW
);
2115 UINT flags
= SWP_NOMOVE
| SWP_NOREPOSITION
| SWP_NOSIZE
;
2116 if (!aActivate
) flags
|= SWP_NOACTIVATE
;
2118 if (!CanTakeFocus() && behind
== HWND_TOP
) {
2119 // Can't place the window to top so place it behind the foreground window
2120 // (as long as it is not topmost)
2121 HWND wndAfter
= ::GetForegroundWindow();
2123 behind
= HWND_BOTTOM
;
2124 else if (!(GetWindowLongPtrW(wndAfter
, GWL_EXSTYLE
) & WS_EX_TOPMOST
))
2126 flags
|= SWP_NOACTIVATE
;
2129 ::SetWindowPos(mWnd
, behind
, 0, 0, 0, 0, flags
);
2132 static UINT
GetCurrentShowCmd(HWND aWnd
) {
2134 pl
.length
= sizeof(pl
);
2135 ::GetWindowPlacement(aWnd
, &pl
);
2139 // Maximize, minimize or restore the window.
2140 void nsWindow::SetSizeMode(nsSizeMode aMode
) {
2141 // If we are still displaying a maximized pre-XUL skeleton UI, ignore the
2142 // noise of sizemode changes. Once we have "shown" the window for the first
2143 // time (called nsWindow::Show(true), even though the window is already
2144 // technically displayed), we will again accept sizemode changes.
2145 if (mIsShowingPreXULSkeletonUI
&& WasPreXULSkeletonUIMaximized()) {
2149 mFrameState
->EnsureSizeMode(aMode
);
2152 nsSizeMode
nsWindow::SizeMode() { return mFrameState
->GetSizeMode(); }
2154 void DoGetWorkspaceID(HWND aWnd
, nsAString
* aWorkspaceID
) {
2155 RefPtr
<IVirtualDesktopManager
> desktopManager
= gVirtualDesktopManager
;
2156 if (!desktopManager
|| !aWnd
) {
2161 HRESULT hr
= desktopManager
->GetWindowDesktopId(aWnd
, &desktop
);
2166 RPC_WSTR workspaceIDStr
= nullptr;
2167 if (UuidToStringW(&desktop
, &workspaceIDStr
) == RPC_S_OK
) {
2168 aWorkspaceID
->Assign((wchar_t*)workspaceIDStr
);
2169 RpcStringFreeW(&workspaceIDStr
);
2173 void nsWindow::GetWorkspaceID(nsAString
& workspaceID
) {
2174 // If we have a value cached, use that, but also make sure it is
2175 // scheduled to be updated. If we don't yet have a value, get
2176 // one synchronously.
2177 auto desktop
= mDesktopId
.Lock();
2178 if (desktop
->mID
.IsEmpty()) {
2179 DoGetWorkspaceID(mWnd
, &desktop
->mID
);
2180 desktop
->mUpdateIsQueued
= false;
2182 AsyncUpdateWorkspaceID(*desktop
);
2185 workspaceID
= desktop
->mID
;
2188 void nsWindow::AsyncUpdateWorkspaceID(Desktop
& aDesktop
) {
2189 struct UpdateWorkspaceIdTask
: public Task
{
2190 explicit UpdateWorkspaceIdTask(nsWindow
* aSelf
)
2191 : Task(Kind::OffMainThreadOnly
, EventQueuePriority::Normal
),
2194 TaskResult
Run() override
{
2195 auto desktop
= mSelf
->mDesktopId
.Lock();
2196 if (desktop
->mUpdateIsQueued
) {
2197 DoGetWorkspaceID(mSelf
->mWnd
, &desktop
->mID
);
2198 desktop
->mUpdateIsQueued
= false;
2200 return TaskResult::Complete
;
2203 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
2204 bool GetName(nsACString
& aName
) override
{
2205 aName
.AssignLiteral("UpdateWorkspaceIdTask");
2210 RefPtr
<nsWindow
> mSelf
;
2213 if (aDesktop
.mUpdateIsQueued
) {
2217 aDesktop
.mUpdateIsQueued
= true;
2218 TaskController::Get()->AddTask(MakeAndAddRef
<UpdateWorkspaceIdTask
>(this));
2221 void nsWindow::MoveToWorkspace(const nsAString
& workspaceID
) {
2222 RefPtr
<IVirtualDesktopManager
> desktopManager
= gVirtualDesktopManager
;
2223 if (!desktopManager
) {
2228 const nsString flat
= PromiseFlatString(workspaceID
);
2229 RPC_WSTR workspaceIDStr
= reinterpret_cast<RPC_WSTR
>((wchar_t*)flat
.get());
2230 if (UuidFromStringW(workspaceIDStr
, &desktop
) == RPC_S_OK
) {
2231 if (SUCCEEDED(desktopManager
->MoveWindowToDesktop(mWnd
, desktop
))) {
2232 auto desktop
= mDesktopId
.Lock();
2233 desktop
->mID
= workspaceID
;
2238 void nsWindow::SuppressAnimation(bool aSuppress
) {
2239 DWORD dwAttribute
= aSuppress
? TRUE
: FALSE
;
2240 DwmSetWindowAttribute(mWnd
, DWMWA_TRANSITIONS_FORCEDISABLED
, &dwAttribute
,
2241 sizeof dwAttribute
);
2244 // Constrain a potential move to fit onscreen
2245 // Position (aX, aY) is specified in Windows screen (logical) pixels,
2246 // except when using per-monitor DPI, in which case it's device pixels.
2247 void nsWindow::ConstrainPosition(DesktopIntPoint
& aPoint
) {
2248 if (!mIsTopWidgetWindow
) // only a problem for top-level windows
2251 double dpiScale
= GetDesktopToDeviceScale().scale
;
2253 // We need to use the window size in the kind of pixels used for window-
2254 // manipulation APIs.
2256 std::max
<int32_t>(NSToIntRound(mBounds
.Width() / dpiScale
), 1);
2258 std::max
<int32_t>(NSToIntRound(mBounds
.Height() / dpiScale
), 1);
2260 /* get our playing field. use the current screen, or failing that
2261 for any reason, use device caps for the default screen. */
2264 nsCOMPtr
<nsIScreenManager
> screenmgr
=
2265 do_GetService(sScreenManagerContractID
);
2269 nsCOMPtr
<nsIScreen
> screen
;
2270 int32_t left
, top
, width
, height
;
2272 screenmgr
->ScreenForRect(aPoint
.x
, aPoint
.y
, logWidth
, logHeight
,
2273 getter_AddRefs(screen
));
2274 if (mFrameState
->GetSizeMode() != nsSizeMode_Fullscreen
) {
2275 // For normalized windows, use the desktop work area.
2276 nsresult rv
= screen
->GetAvailRectDisplayPix(&left
, &top
, &width
, &height
);
2277 if (NS_FAILED(rv
)) {
2281 // For full screen windows, use the desktop.
2282 nsresult rv
= screen
->GetRectDisplayPix(&left
, &top
, &width
, &height
);
2283 if (NS_FAILED(rv
)) {
2287 screenRect
.left
= left
;
2288 screenRect
.right
= left
+ width
;
2289 screenRect
.top
= top
;
2290 screenRect
.bottom
= top
+ height
;
2292 if (aPoint
.x
< screenRect
.left
)
2293 aPoint
.x
= screenRect
.left
;
2294 else if (aPoint
.x
>= screenRect
.right
- logWidth
)
2295 aPoint
.x
= screenRect
.right
- logWidth
;
2297 if (aPoint
.y
< screenRect
.top
)
2298 aPoint
.y
= screenRect
.top
;
2299 else if (aPoint
.y
>= screenRect
.bottom
- logHeight
)
2300 aPoint
.y
= screenRect
.bottom
- logHeight
;
2303 /**************************************************************
2305 * SECTION: nsIWidget::Enable, nsIWidget::IsEnabled
2307 * Enabling and disabling the widget.
2309 **************************************************************/
2311 // Enable/disable this component
2312 void nsWindow::Enable(bool bState
) {
2314 ::EnableWindow(mWnd
, bState
);
2318 // Return the current enable state
2319 bool nsWindow::IsEnabled() const {
2320 return !mWnd
|| (::IsWindowEnabled(mWnd
) &&
2321 ::IsWindowEnabled(::GetAncestor(mWnd
, GA_ROOT
)));
2324 /**************************************************************
2326 * SECTION: nsIWidget::SetFocus
2328 * Give the focus to this widget.
2330 **************************************************************/
2332 void nsWindow::SetFocus(Raise aRaise
, mozilla::dom::CallerType aCallerType
) {
2334 #ifdef WINSTATE_DEBUG_OUTPUT
2335 if (mWnd
== WinUtils::GetTopLevelHWND(mWnd
)) {
2336 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
2337 ("*** SetFocus: [ top] raise=%d\n", aRaise
== Raise::Yes
));
2339 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
2340 ("*** SetFocus: [child] raise=%d\n", aRaise
== Raise::Yes
));
2343 // Uniconify, if necessary
2344 HWND toplevelWnd
= WinUtils::GetTopLevelHWND(mWnd
);
2345 if (aRaise
== Raise::Yes
&& ::IsIconic(toplevelWnd
)) {
2346 ::ShowWindow(toplevelWnd
, SW_RESTORE
);
2352 /**************************************************************
2356 * GetBounds, GetClientBounds, GetScreenBounds,
2357 * GetRestoredBounds, GetClientOffset, SetNonClientMargins
2359 * Bound calculations.
2361 **************************************************************/
2363 // Return the window's full dimensions in screen coordinates.
2364 // If the window has a parent, converts the origin to an offset
2365 // of the parent's screen origin.
2366 LayoutDeviceIntRect
nsWindow::GetBounds() {
2372 VERIFY(::GetWindowRect(mWnd
, &r
));
2374 LayoutDeviceIntRect rect
;
2377 rect
.SizeTo(r
.right
- r
.left
, r
.bottom
- r
.top
);
2379 // popup window bounds' are in screen coordinates, not relative to parent
2381 if (mWindowType
== WindowType::Popup
) {
2382 rect
.MoveTo(r
.left
, r
.top
);
2386 // chrome on parent:
2387 // ___ 5,5 (chrome start)
2388 // | ____ 10,10 (client start)
2389 // | | ____ 20,20 (child start)
2391 // 20,20 - 5,5 = 15,15 (??)
2392 // minus GetClientOffset:
2393 // 15,15 - 5,5 = 10,10
2395 // no chrome on parent:
2396 // ______ 10,10 (win start)
2397 // | ____ 20,20 (child start)
2399 // 20,20 - 10,10 = 10,10
2401 // walking the chain:
2402 // ___ 5,5 (chrome start)
2403 // | ___ 10,10 (client start)
2404 // | | ___ 20,20 (child start)
2405 // | | | __ 30,30 (child start)
2407 // 30,30 - 20,20 = 10,10 (offset from second child to first)
2408 // 20,20 - 5,5 = 15,15 + 10,10 = 25,25 (??)
2409 // minus GetClientOffset:
2410 // 25,25 - 5,5 = 20,20 (offset from second child to parent client)
2412 // convert coordinates if parent exists
2413 HWND parent
= ::GetParent(mWnd
);
2416 VERIFY(::GetWindowRect(parent
, &pr
));
2419 // adjust for chrome
2420 nsWindow
* pWidget
= static_cast<nsWindow
*>(GetParent());
2421 if (pWidget
&& pWidget
->IsTopLevelWidget()) {
2422 LayoutDeviceIntPoint clientOffset
= pWidget
->GetClientOffset();
2423 r
.left
-= clientOffset
.x
;
2424 r
.top
-= clientOffset
.y
;
2427 rect
.MoveTo(r
.left
, r
.top
);
2428 if (mCompositorSession
&&
2429 !wr::WindowSizeSanityCheck(rect
.width
, rect
.height
)) {
2430 gfxCriticalNoteOnce
<< "Invalid size" << rect
<< " size mode "
2431 << mFrameState
->GetSizeMode();
2437 // Get this component dimension
2438 LayoutDeviceIntRect
nsWindow::GetClientBounds() {
2440 return LayoutDeviceIntRect(0, 0, 0, 0);
2444 if (!::GetClientRect(mWnd
, &r
)) {
2445 MOZ_ASSERT_UNREACHABLE("unexpected to be called");
2446 gfxCriticalNoteOnce
<< "GetClientRect failed " << ::GetLastError();
2450 LayoutDeviceIntRect bounds
= GetBounds();
2451 LayoutDeviceIntRect rect
;
2452 rect
.MoveTo(bounds
.TopLeft() + GetClientOffset());
2453 rect
.SizeTo(r
.right
- r
.left
, r
.bottom
- r
.top
);
2457 // Like GetBounds, but don't offset by the parent
2458 LayoutDeviceIntRect
nsWindow::GetScreenBounds() {
2464 VERIFY(::GetWindowRect(mWnd
, &r
));
2466 return LayoutDeviceIntRect(r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
);
2469 nsresult
nsWindow::GetRestoredBounds(LayoutDeviceIntRect
& aRect
) {
2470 if (SizeMode() == nsSizeMode_Normal
) {
2471 aRect
= GetScreenBounds();
2475 return NS_ERROR_FAILURE
;
2478 WINDOWPLACEMENT pl
= {sizeof(WINDOWPLACEMENT
)};
2479 VERIFY(::GetWindowPlacement(mWnd
, &pl
));
2480 const RECT
& r
= pl
.rcNormalPosition
;
2482 HMONITOR monitor
= ::MonitorFromWindow(mWnd
, MONITOR_DEFAULTTONULL
);
2484 return NS_ERROR_FAILURE
;
2486 MONITORINFO mi
= {sizeof(MONITORINFO
)};
2487 VERIFY(::GetMonitorInfo(monitor
, &mi
));
2489 aRect
.SetRect(r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
);
2490 aRect
.MoveBy(mi
.rcWork
.left
- mi
.rcMonitor
.left
,
2491 mi
.rcWork
.top
- mi
.rcMonitor
.top
);
2495 // Return the x,y offset of the client area from the origin of the window. If
2496 // the window is borderless returns (0,0).
2497 LayoutDeviceIntPoint
nsWindow::GetClientOffset() {
2499 return LayoutDeviceIntPoint(0, 0);
2503 GetWindowRect(mWnd
, &r1
);
2504 LayoutDeviceIntPoint pt
= WidgetToScreenOffset();
2505 return LayoutDeviceIntPoint(pt
.x
- LayoutDeviceIntCoord(r1
.left
),
2506 pt
.y
- LayoutDeviceIntCoord(r1
.top
));
2509 void nsWindow::ResetLayout() {
2510 // This will trigger a frame changed event, triggering
2511 // nc calc size and a sizemode gecko event.
2512 SetWindowPos(mWnd
, 0, 0, 0, 0, 0,
2513 SWP_FRAMECHANGED
| SWP_NOACTIVATE
| SWP_NOMOVE
|
2514 SWP_NOOWNERZORDER
| SWP_NOSIZE
| SWP_NOZORDER
);
2516 // If hidden, just send the frame changed event for now.
2521 // Send a gecko size event to trigger reflow.
2522 RECT clientRc
= {0};
2523 GetClientRect(mWnd
, &clientRc
);
2524 OnResize(WinUtils::ToIntRect(clientRc
).Size());
2526 // Invalidate and update
2530 // Internally track the caption status via a window property. Required
2531 // due to our internal handling of WM_NCACTIVATE when custom client
2533 static const wchar_t kManageWindowInfoProperty
[] = L
"ManageWindowInfoProperty";
2534 typedef BOOL(WINAPI
* GetWindowInfoPtr
)(HWND hwnd
, PWINDOWINFO pwi
);
2535 static WindowsDllInterceptor::FuncHookType
<GetWindowInfoPtr
>
2536 sGetWindowInfoPtrStub
;
2538 BOOL WINAPI
GetWindowInfoHook(HWND hWnd
, PWINDOWINFO pwi
) {
2539 if (!sGetWindowInfoPtrStub
) {
2540 NS_ASSERTION(FALSE
, "Something is horribly wrong in GetWindowInfoHook!");
2544 reinterpret_cast<LONG_PTR
>(GetPropW(hWnd
, kManageWindowInfoProperty
));
2545 // No property set, return the default data.
2546 if (!windowStatus
) return sGetWindowInfoPtrStub(hWnd
, pwi
);
2547 // Call GetWindowInfo and update dwWindowStatus with our
2548 // internally tracked value.
2549 BOOL result
= sGetWindowInfoPtrStub(hWnd
, pwi
);
2551 pwi
->dwWindowStatus
= (windowStatus
== 1 ? 0 : WS_ACTIVECAPTION
);
2555 void nsWindow::UpdateGetWindowInfoCaptionStatus(bool aActiveCaption
) {
2558 sUser32Intercept
.Init("user32.dll");
2559 sGetWindowInfoPtrStub
.Set(sUser32Intercept
, "GetWindowInfo",
2560 &GetWindowInfoHook
);
2561 if (!sGetWindowInfoPtrStub
) {
2565 // Update our internally tracked caption status
2566 SetPropW(mWnd
, kManageWindowInfoProperty
,
2567 reinterpret_cast<HANDLE
>(static_cast<INT_PTR
>(aActiveCaption
) + 1));
2570 #define DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 19
2571 #define DWMWA_USE_IMMERSIVE_DARK_MODE 20
2573 void nsWindow::UpdateDarkModeToolbar() {
2574 PreferenceSheet::EnsureInitialized();
2575 BOOL dark
= PreferenceSheet::ColorSchemeForChrome() == ColorScheme::Dark
;
2576 DwmSetWindowAttribute(mWnd
, DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1
, &dark
,
2578 DwmSetWindowAttribute(mWnd
, DWMWA_USE_IMMERSIVE_DARK_MODE
, &dark
,
2582 LayoutDeviceIntMargin
nsWindow::NormalWindowNonClientOffset() const {
2583 LayoutDeviceIntMargin nonClientOffset
;
2585 // We're dealing with a "normal" window (not maximized, minimized, or
2586 // fullscreen), so process `mNonClientMargins` and set `mNonClientOffset`
2589 // Setting `mNonClientOffset` to 0 has the effect of leaving the default
2590 // frame intact. Setting it to a value greater than 0 reduces the frame
2591 // size by that amount.
2593 if (mNonClientMargins
.top
> 0) {
2594 nonClientOffset
.top
= std::min(mCaptionHeight
, mNonClientMargins
.top
);
2595 } else if (mNonClientMargins
.top
== 0) {
2596 nonClientOffset
.top
= mCaptionHeight
;
2598 nonClientOffset
.top
= 0;
2601 if (mNonClientMargins
.bottom
> 0) {
2602 nonClientOffset
.bottom
=
2603 std::min(mVertResizeMargin
, mNonClientMargins
.bottom
);
2604 } else if (mNonClientMargins
.bottom
== 0) {
2605 nonClientOffset
.bottom
= mVertResizeMargin
;
2607 nonClientOffset
.bottom
= 0;
2610 if (mNonClientMargins
.left
> 0) {
2611 nonClientOffset
.left
= std::min(mHorResizeMargin
, mNonClientMargins
.left
);
2612 } else if (mNonClientMargins
.left
== 0) {
2613 nonClientOffset
.left
= mHorResizeMargin
;
2615 nonClientOffset
.left
= 0;
2618 if (mNonClientMargins
.right
> 0) {
2619 nonClientOffset
.right
= std::min(mHorResizeMargin
, mNonClientMargins
.right
);
2620 } else if (mNonClientMargins
.right
== 0) {
2621 nonClientOffset
.right
= mHorResizeMargin
;
2623 nonClientOffset
.right
= 0;
2625 return nonClientOffset
;
2629 * Called when the window layout changes: full screen mode transitions,
2630 * theme changes, and composition changes. Calculates the new non-client
2631 * margins and fires off a frame changed event, which triggers an nc calc
2632 * size windows event, kicking the changes in.
2634 * The offsets calculated here are based on the value of `mNonClientMargins`
2635 * which is specified in the "chromemargins" attribute of the window. For
2636 * each margin, the value specified has the following meaning:
2637 * -1 - leave the default frame in place
2638 * 0 - remove the frame
2639 * >0 - frame size equals min(0, (default frame size - margin value))
2641 * This function calculates and populates `mNonClientOffset`.
2642 * In our processing of `WM_NCCALCSIZE`, the frame size will be calculated
2643 * as (default frame size - offset). For example, if the left frame should
2644 * be 1 pixel narrower than the default frame size, `mNonClientOffset.left`
2647 * For maximized, fullscreen, and minimized windows, the values stored in
2648 * `mNonClientMargins` are ignored, and special processing takes place.
2650 * For non-glass windows, we only allow frames to be their default size
2651 * or removed entirely.
2653 bool nsWindow::UpdateNonClientMargins(bool aReflowWindow
) {
2654 if (!mCustomNonClient
) {
2658 const nsSizeMode sizeMode
= mFrameState
->GetSizeMode();
2661 bool(mBorderStyle
& (BorderStyle::All
| BorderStyle::Title
|
2662 BorderStyle::Menu
| BorderStyle::Default
));
2664 float dpi
= GetDPI();
2666 // mCaptionHeight is the default size of the NC area at
2667 // the top of the window. If the window has a caption,
2668 // the size is calculated as the sum of:
2669 // SM_CYFRAME - The thickness of the sizing border
2670 // around a resizable window
2671 // SM_CXPADDEDBORDER - The amount of border padding
2672 // for captioned windows
2673 // SM_CYCAPTION - The height of the caption area
2675 // If the window does not have a caption, mCaptionHeight will be equal to
2676 // `WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi)`
2678 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME
, dpi
) +
2679 (hasCaption
? WinUtils::GetSystemMetricsForDpi(SM_CYCAPTION
, dpi
) +
2680 WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER
, dpi
)
2682 if (!mUseResizeMarginOverrides
) {
2683 // mHorResizeMargin is the size of the default NC areas on the
2684 // left and right sides of our window. It is calculated as
2686 // SM_CXFRAME - The thickness of the sizing border
2687 // SM_CXPADDEDBORDER - The amount of border padding
2688 // for captioned windows
2690 // If the window does not have a caption, mHorResizeMargin will be equal to
2691 // `WinUtils::GetSystemMetricsForDpi(SM_CXFRAME, dpi)`
2693 WinUtils::GetSystemMetricsForDpi(SM_CXFRAME
, dpi
) +
2694 (hasCaption
? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER
, dpi
)
2697 // mVertResizeMargin is the size of the default NC area at the
2698 // bottom of the window. It is calculated as the sum of:
2699 // SM_CYFRAME - The thickness of the sizing border
2700 // SM_CXPADDEDBORDER - The amount of border padding
2701 // for captioned windows.
2703 // If the window does not have a caption, mVertResizeMargin will be equal to
2704 // `WinUtils::GetSystemMetricsForDpi(SM_CYFRAME, dpi)`
2706 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME
, dpi
) +
2707 (hasCaption
? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER
, dpi
)
2711 if (sizeMode
== nsSizeMode_Minimized
) {
2712 // Use default frame size for minimized windows
2713 mNonClientOffset
.top
= 0;
2714 mNonClientOffset
.left
= 0;
2715 mNonClientOffset
.right
= 0;
2716 mNonClientOffset
.bottom
= 0;
2717 } else if (sizeMode
== nsSizeMode_Fullscreen
) {
2718 // Remove the default frame from the top of our fullscreen window. This
2719 // makes the whole caption part of our client area, allowing us to draw
2720 // in the whole caption area. Additionally remove the default frame from
2721 // the left, right, and bottom.
2722 mNonClientOffset
.top
= mCaptionHeight
;
2723 mNonClientOffset
.bottom
= mVertResizeMargin
;
2724 mNonClientOffset
.left
= mHorResizeMargin
;
2725 mNonClientOffset
.right
= mHorResizeMargin
;
2726 } else if (sizeMode
== nsSizeMode_Maximized
) {
2727 // We make the entire frame part of the client area. We leave the default
2728 // frame sizes for left, right and bottom since Windows will automagically
2729 // position the edges "offscreen" for maximized windows.
2730 int verticalResize
=
2731 WinUtils::GetSystemMetricsForDpi(SM_CYFRAME
, dpi
) +
2732 (hasCaption
? WinUtils::GetSystemMetricsForDpi(SM_CXPADDEDBORDER
, dpi
)
2735 mNonClientOffset
.top
= mCaptionHeight
- verticalResize
;
2736 mNonClientOffset
.bottom
= 0;
2737 mNonClientOffset
.left
= 0;
2738 mNonClientOffset
.right
= 0;
2740 mozilla::Maybe
<UINT
> maybeEdge
= GetHiddenTaskbarEdge();
2742 auto edge
= maybeEdge
.value();
2743 if (ABE_LEFT
== edge
) {
2744 mNonClientOffset
.left
-= kHiddenTaskbarSize
;
2745 } else if (ABE_RIGHT
== edge
) {
2746 mNonClientOffset
.right
-= kHiddenTaskbarSize
;
2747 } else if (ABE_BOTTOM
== edge
|| ABE_TOP
== edge
) {
2748 mNonClientOffset
.bottom
-= kHiddenTaskbarSize
;
2751 // When we are drawing the non-client region, we need
2752 // to clear the portion of the NC region that is exposed by the
2753 // hidden taskbar. As above, we clear the bottom of the NC region
2754 // when the taskbar is at the top of the screen.
2755 UINT clearEdge
= (edge
== ABE_TOP
) ? ABE_BOTTOM
: edge
;
2756 mClearNCEdge
= Some(clearEdge
);
2759 mNonClientOffset
= NormalWindowNonClientOffset();
2762 if (aReflowWindow
) {
2763 // Force a reflow of content based on the new client
2771 nsresult
nsWindow::SetNonClientMargins(const LayoutDeviceIntMargin
& margins
) {
2772 if (!mIsTopWidgetWindow
|| mBorderStyle
== BorderStyle::None
)
2773 return NS_ERROR_INVALID_ARG
;
2776 mFutureMarginsOnceChromeShows
= margins
;
2777 mFutureMarginsToUse
= true;
2780 mFutureMarginsToUse
= false;
2782 // Request for a reset
2783 if (margins
.top
== -1 && margins
.left
== -1 && margins
.right
== -1 &&
2784 margins
.bottom
== -1) {
2785 mCustomNonClient
= false;
2786 mNonClientMargins
= margins
;
2787 // Force a reflow of content based on the new client
2792 reinterpret_cast<LONG_PTR
>(GetPropW(mWnd
, kManageWindowInfoProperty
));
2794 ::SendMessageW(mWnd
, WM_NCACTIVATE
, 1 != windowStatus
, 0);
2800 if (margins
.top
< -1 || margins
.bottom
< -1 || margins
.left
< -1 ||
2802 return NS_ERROR_INVALID_ARG
;
2804 mNonClientMargins
= margins
;
2805 mCustomNonClient
= true;
2806 if (!UpdateNonClientMargins()) {
2807 NS_WARNING("UpdateNonClientMargins failed!");
2814 void nsWindow::SetResizeMargin(mozilla::LayoutDeviceIntCoord aResizeMargin
) {
2815 mUseResizeMarginOverrides
= true;
2816 mHorResizeMargin
= aResizeMargin
;
2817 mVertResizeMargin
= aResizeMargin
;
2818 UpdateNonClientMargins();
2821 void nsWindow::InvalidateNonClientRegion() {
2822 // +-+-----------------------+-+
2823 // | | app non-client chrome | |
2824 // | +-----------------------+ |
2825 // | | app client chrome | | }
2826 // | +-----------------------+ | }
2827 // | | app content | | } area we don't want to invalidate
2828 // | +-----------------------+ | }
2829 // | | app client chrome | | }
2830 // | +-----------------------+ |
2831 // +---------------------------+ <
2832 // ^ ^ windows non-client chrome
2833 // client area = app *
2835 GetWindowRect(mWnd
, &rect
);
2836 MapWindowPoints(nullptr, mWnd
, (LPPOINT
)&rect
, 2);
2837 HRGN winRgn
= CreateRectRgnIndirect(&rect
);
2839 // Subtract app client chrome and app content leaving
2840 // windows non-client chrome and app non-client chrome
2842 GetWindowRect(mWnd
, &rect
);
2843 rect
.top
+= mCaptionHeight
;
2844 rect
.right
-= mHorResizeMargin
;
2845 rect
.bottom
-= mVertResizeMargin
;
2846 rect
.left
+= mHorResizeMargin
;
2847 MapWindowPoints(nullptr, mWnd
, (LPPOINT
)&rect
, 2);
2848 HRGN clientRgn
= CreateRectRgnIndirect(&rect
);
2849 CombineRgn(winRgn
, winRgn
, clientRgn
, RGN_DIFF
);
2850 DeleteObject(clientRgn
);
2852 // triggers ncpaint and paint events for the two areas
2853 RedrawWindow(mWnd
, nullptr, winRgn
, RDW_FRAME
| RDW_INVALIDATE
);
2854 DeleteObject(winRgn
);
2857 /**************************************************************
2859 * SECTION: nsIWidget::SetBackgroundColor
2861 * Sets the window background paint color.
2863 **************************************************************/
2865 void nsWindow::SetBackgroundColor(const nscolor
& aColor
) {
2866 if (mBrush
) ::DeleteObject(mBrush
);
2868 mBrush
= ::CreateSolidBrush(NSRGB_2_COLOREF(aColor
));
2869 if (mWnd
!= nullptr) {
2870 ::SetClassLongPtrW(mWnd
, GCLP_HBRBACKGROUND
, (LONG_PTR
)mBrush
);
2874 /**************************************************************
2876 * SECTION: nsIWidget::SetCursor
2878 * SetCursor and related utilities for manging cursor state.
2880 **************************************************************/
2882 // Set this component cursor
2883 static HCURSOR
CursorFor(nsCursor aCursor
) {
2885 case eCursor_select
:
2886 return ::LoadCursor(nullptr, IDC_IBEAM
);
2888 return ::LoadCursor(nullptr, IDC_WAIT
);
2889 case eCursor_hyperlink
:
2890 return ::LoadCursor(nullptr, IDC_HAND
);
2891 case eCursor_standard
:
2892 case eCursor_context_menu
: // XXX See bug 258960.
2893 return ::LoadCursor(nullptr, IDC_ARROW
);
2895 case eCursor_n_resize
:
2896 case eCursor_s_resize
:
2897 return ::LoadCursor(nullptr, IDC_SIZENS
);
2899 case eCursor_w_resize
:
2900 case eCursor_e_resize
:
2901 return ::LoadCursor(nullptr, IDC_SIZEWE
);
2903 case eCursor_nw_resize
:
2904 case eCursor_se_resize
:
2905 return ::LoadCursor(nullptr, IDC_SIZENWSE
);
2907 case eCursor_ne_resize
:
2908 case eCursor_sw_resize
:
2909 return ::LoadCursor(nullptr, IDC_SIZENESW
);
2911 case eCursor_crosshair
:
2912 return ::LoadCursor(nullptr, IDC_CROSS
);
2915 return ::LoadCursor(nullptr, IDC_SIZEALL
);
2918 return ::LoadCursor(nullptr, IDC_HELP
);
2920 case eCursor_copy
: // CSS3
2921 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_COPY
));
2924 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_ALIAS
));
2927 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_CELL
));
2929 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_GRAB
));
2931 case eCursor_grabbing
:
2932 return ::LoadCursor(nsToolkit::mDllInstance
,
2933 MAKEINTRESOURCE(IDC_GRABBING
));
2935 case eCursor_spinning
:
2936 return ::LoadCursor(nullptr, IDC_APPSTARTING
);
2938 case eCursor_zoom_in
:
2939 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_ZOOMIN
));
2941 case eCursor_zoom_out
:
2942 return ::LoadCursor(nsToolkit::mDllInstance
,
2943 MAKEINTRESOURCE(IDC_ZOOMOUT
));
2945 case eCursor_not_allowed
:
2946 case eCursor_no_drop
:
2947 return ::LoadCursor(nullptr, IDC_NO
);
2949 case eCursor_col_resize
:
2950 return ::LoadCursor(nsToolkit::mDllInstance
,
2951 MAKEINTRESOURCE(IDC_COLRESIZE
));
2953 case eCursor_row_resize
:
2954 return ::LoadCursor(nsToolkit::mDllInstance
,
2955 MAKEINTRESOURCE(IDC_ROWRESIZE
));
2957 case eCursor_vertical_text
:
2958 return ::LoadCursor(nsToolkit::mDllInstance
,
2959 MAKEINTRESOURCE(IDC_VERTICALTEXT
));
2961 case eCursor_all_scroll
:
2962 // XXX not 100% appropriate perhaps
2963 return ::LoadCursor(nullptr, IDC_SIZEALL
);
2965 case eCursor_nesw_resize
:
2966 return ::LoadCursor(nullptr, IDC_SIZENESW
);
2968 case eCursor_nwse_resize
:
2969 return ::LoadCursor(nullptr, IDC_SIZENWSE
);
2971 case eCursor_ns_resize
:
2972 return ::LoadCursor(nullptr, IDC_SIZENS
);
2974 case eCursor_ew_resize
:
2975 return ::LoadCursor(nullptr, IDC_SIZEWE
);
2978 return ::LoadCursor(nsToolkit::mDllInstance
, MAKEINTRESOURCE(IDC_NONE
));
2981 NS_ERROR("Invalid cursor type");
2986 static HCURSOR
CursorForImage(const nsIWidget::Cursor
& aCursor
,
2987 CSSToLayoutDeviceScale aScale
) {
2988 if (!aCursor
.IsCustom()) {
2992 nsIntSize size
= nsIWidget::CustomCursorSize(aCursor
);
2994 // Reject cursors greater than 128 pixels in either direction, to prevent
2996 // XXX ideally we should rescale. Also, we could modify the API to
2997 // allow trusted content to set larger cursors.
2998 if (size
.width
> 128 || size
.height
> 128) {
3002 LayoutDeviceIntSize layoutSize
=
3003 RoundedToInt(CSSIntSize(size
.width
, size
.height
) * aScale
);
3004 LayoutDeviceIntPoint hotspot
=
3005 RoundedToInt(CSSIntPoint(aCursor
.mHotspotX
, aCursor
.mHotspotY
) * aScale
);
3007 nsresult rv
= nsWindowGfx::CreateIcon(aCursor
.mContainer
, true, hotspot
,
3008 layoutSize
, &cursor
);
3009 if (NS_FAILED(rv
)) {
3016 void nsWindow::SetCursor(const Cursor
& aCursor
) {
3017 static HCURSOR sCurrentHCursor
= nullptr;
3018 static bool sCurrentHCursorIsCustom
= false;
3022 if (sCurrentCursor
== aCursor
&& sCurrentHCursor
&& !mUpdateCursor
) {
3023 // Cursors in windows are global, so even if our mUpdateCursor flag is
3024 // false we always need to make sure the Windows cursor is up-to-date,
3025 // since stuff like native drag and drop / resizers code can mutate it
3026 // outside of this method.
3027 ::SetCursor(sCurrentHCursor
);
3031 mUpdateCursor
= false;
3033 if (sCurrentHCursorIsCustom
) {
3034 ::DestroyIcon(sCurrentHCursor
);
3036 sCurrentHCursor
= nullptr;
3037 sCurrentHCursorIsCustom
= false;
3038 sCurrentCursor
= aCursor
;
3040 HCURSOR cursor
= nullptr;
3041 if (mCustomCursorAllowed
) {
3042 cursor
= CursorForImage(aCursor
, GetDefaultScale());
3044 bool custom
= false;
3048 cursor
= CursorFor(aCursor
.mDefaultCursor
);
3055 sCurrentHCursor
= cursor
;
3056 sCurrentHCursorIsCustom
= custom
;
3057 ::SetCursor(cursor
);
3060 /**************************************************************
3062 * SECTION: nsIWidget::Get/SetTransparencyMode
3064 * Manage the transparency mode of the window containing this
3065 * widget. Only works for popup and dialog windows when the
3066 * Desktop Window Manager compositor is not enabled.
3068 **************************************************************/
3070 TransparencyMode
nsWindow::GetTransparencyMode() {
3071 return GetTopLevelWindow(true)->GetWindowTranslucencyInner();
3074 void nsWindow::SetTransparencyMode(TransparencyMode aMode
) {
3075 nsWindow
* window
= GetTopLevelWindow(true);
3078 if (!window
|| window
->DestroyCalled()) {
3082 window
->SetWindowTranslucencyInner(aMode
);
3085 /**************************************************************
3087 * SECTION: nsIWidget::UpdateWindowDraggingRegion
3089 * For setting the draggable titlebar region from CSS
3090 * with -moz-window-dragging: drag.
3092 **************************************************************/
3094 void nsWindow::UpdateWindowDraggingRegion(
3095 const LayoutDeviceIntRegion
& aRegion
) {
3096 if (mDraggableRegion
!= aRegion
) {
3097 mDraggableRegion
= aRegion
;
3101 /**************************************************************
3103 * SECTION: nsIWidget::HideWindowChrome
3105 * Show or hide window chrome.
3107 **************************************************************/
3109 void nsWindow::HideWindowChrome(bool aShouldHide
) {
3110 HWND hwnd
= WinUtils::GetTopLevelHWND(mWnd
, true);
3111 if (!WinUtils::GetNSWindowPtr(hwnd
)) {
3112 NS_WARNING("Trying to hide window decorations in an embedded context");
3116 if (mHideChrome
== aShouldHide
) return;
3118 DWORD_PTR style
, exStyle
;
3119 mHideChrome
= aShouldHide
;
3121 DWORD_PTR tempStyle
= ::GetWindowLongPtrW(hwnd
, GWL_STYLE
);
3122 DWORD_PTR tempExStyle
= ::GetWindowLongPtrW(hwnd
, GWL_EXSTYLE
);
3124 style
= tempStyle
& ~(WS_CAPTION
| WS_THICKFRAME
);
3125 exStyle
= tempExStyle
& ~(WS_EX_DLGMODALFRAME
| WS_EX_WINDOWEDGE
|
3126 WS_EX_CLIENTEDGE
| WS_EX_STATICEDGE
);
3128 mOldStyle
= tempStyle
;
3129 mOldExStyle
= tempExStyle
;
3131 if (!mOldStyle
|| !mOldExStyle
) {
3132 mOldStyle
= ::GetWindowLongPtrW(hwnd
, GWL_STYLE
);
3133 mOldExStyle
= ::GetWindowLongPtrW(hwnd
, GWL_EXSTYLE
);
3137 exStyle
= mOldExStyle
;
3138 if (mFutureMarginsToUse
) {
3139 SetNonClientMargins(mFutureMarginsOnceChromeShows
);
3143 VERIFY_WINDOW_STYLE(style
);
3144 ::SetWindowLongPtrW(hwnd
, GWL_STYLE
, style
);
3145 ::SetWindowLongPtrW(hwnd
, GWL_EXSTYLE
, exStyle
);
3148 /**************************************************************
3150 * SECTION: nsWindow::Invalidate
3152 * Invalidate an area of the client for painting.
3154 **************************************************************/
3156 // Invalidate this component visible area
3157 void nsWindow::Invalidate(bool aEraseBackground
, bool aUpdateNCArea
,
3158 bool aIncludeChildren
) {
3163 #ifdef WIDGET_DEBUG_OUTPUT
3164 debug_DumpInvalidate(stdout
, this, nullptr, "noname", (int32_t)mWnd
);
3165 #endif // WIDGET_DEBUG_OUTPUT
3167 DWORD flags
= RDW_INVALIDATE
;
3168 if (aEraseBackground
) {
3171 if (aUpdateNCArea
) {
3174 if (aIncludeChildren
) {
3175 flags
|= RDW_ALLCHILDREN
;
3178 VERIFY(::RedrawWindow(mWnd
, nullptr, nullptr, flags
));
3181 // Invalidate this component visible area
3182 void nsWindow::Invalidate(const LayoutDeviceIntRect
& aRect
) {
3184 #ifdef WIDGET_DEBUG_OUTPUT
3185 debug_DumpInvalidate(stdout
, this, &aRect
, "noname", (int32_t)mWnd
);
3186 #endif // WIDGET_DEBUG_OUTPUT
3190 rect
.left
= aRect
.X();
3191 rect
.top
= aRect
.Y();
3192 rect
.right
= aRect
.XMost();
3193 rect
.bottom
= aRect
.YMost();
3195 VERIFY(::InvalidateRect(mWnd
, &rect
, FALSE
));
3199 static LRESULT CALLBACK
FullscreenTransitionWindowProc(HWND hWnd
, UINT uMsg
,
3203 case WM_FULLSCREEN_TRANSITION_BEFORE
:
3204 case WM_FULLSCREEN_TRANSITION_AFTER
: {
3205 DWORD duration
= (DWORD
)lParam
;
3206 DWORD flags
= AW_BLEND
;
3207 if (uMsg
== WM_FULLSCREEN_TRANSITION_AFTER
) {
3210 ::AnimateWindow(hWnd
, duration
, flags
);
3211 // The message sender should have added ref for us.
3212 NS_DispatchToMainThread(
3213 already_AddRefed
<nsIRunnable
>((nsIRunnable
*)wParam
));
3217 ::PostQuitMessage(0);
3220 return ::DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
3225 struct FullscreenTransitionInitData
{
3226 LayoutDeviceIntRect mBounds
;
3231 FullscreenTransitionInitData()
3232 : mSemaphore(nullptr), mThread(nullptr), mWnd(nullptr) {}
3234 ~FullscreenTransitionInitData() {
3236 ::CloseHandle(mSemaphore
);
3239 ::CloseHandle(mThread
);
3244 static DWORD WINAPI
FullscreenTransitionThreadProc(LPVOID lpParam
) {
3245 // Initialize window class
3246 static bool sInitialized
= false;
3247 if (!sInitialized
) {
3249 wc
.lpfnWndProc
= ::FullscreenTransitionWindowProc
;
3250 wc
.hInstance
= nsToolkit::mDllInstance
;
3251 wc
.hbrBackground
= ::CreateSolidBrush(RGB(0, 0, 0));
3252 wc
.lpszClassName
= kClassNameTransition
;
3253 ::RegisterClassW(&wc
);
3254 sInitialized
= true;
3257 auto data
= static_cast<FullscreenTransitionInitData
*>(lpParam
);
3258 HWND wnd
= ::CreateWindowW(kClassNameTransition
, L
"", 0, 0, 0, 0, 0, nullptr,
3259 nullptr, nsToolkit::mDllInstance
, nullptr);
3261 ::ReleaseSemaphore(data
->mSemaphore
, 1, nullptr);
3265 // Since AnimateWindow blocks the thread of the transition window,
3266 // we need to hide the cursor for that window, otherwise the system
3267 // would show the busy pointer to the user.
3268 ::ShowCursor(false);
3269 ::SetWindowLongW(wnd
, GWL_STYLE
, 0);
3272 WS_EX_LAYERED
| WS_EX_TRANSPARENT
| WS_EX_TOOLWINDOW
| WS_EX_NOACTIVATE
);
3273 ::SetWindowPos(wnd
, HWND_TOPMOST
, data
->mBounds
.X(), data
->mBounds
.Y(),
3274 data
->mBounds
.Width(), data
->mBounds
.Height(), 0);
3276 ::ReleaseSemaphore(data
->mSemaphore
, 1, nullptr);
3277 // The initialization data may no longer be valid
3278 // after we release the semaphore.
3282 while (::GetMessageW(&msg
, nullptr, 0, 0)) {
3283 ::TranslateMessage(&msg
);
3284 ::DispatchMessage(&msg
);
3287 ::DestroyWindow(wnd
);
3291 class FullscreenTransitionData final
: public nsISupports
{
3295 explicit FullscreenTransitionData(HWND aWnd
) : mWnd(aWnd
) {
3296 MOZ_ASSERT(NS_IsMainThread(),
3297 "FullscreenTransitionData "
3298 "should be constructed in the main thread");
3304 ~FullscreenTransitionData() {
3305 MOZ_ASSERT(NS_IsMainThread(),
3306 "FullscreenTransitionData "
3307 "should be deconstructed in the main thread");
3308 ::PostMessageW(mWnd
, WM_DESTROY
, 0, 0);
3312 NS_IMPL_ISUPPORTS0(FullscreenTransitionData
)
3315 bool nsWindow::PrepareForFullscreenTransition(nsISupports
** aData
) {
3316 FullscreenTransitionInitData initData
;
3317 nsCOMPtr
<nsIScreen
> screen
= GetWidgetScreen();
3318 const DesktopIntRect rect
= screen
->GetRectDisplayPix();
3319 MOZ_ASSERT(BoundsUseDesktopPixels(),
3320 "Should only be called on top-level window");
3322 LayoutDeviceIntRect::Round(rect
* GetDesktopToDeviceScale());
3324 // Create a semaphore for synchronizing the window handle which will
3325 // be created by the transition thread and used by the main thread for
3326 // posting the transition messages.
3327 initData
.mSemaphore
= ::CreateSemaphore(nullptr, 0, 1, nullptr);
3328 if (initData
.mSemaphore
) {
3329 initData
.mThread
= ::CreateThread(
3330 nullptr, 0, FullscreenTransitionThreadProc
, &initData
, 0, nullptr);
3331 if (initData
.mThread
) {
3332 ::WaitForSingleObject(initData
.mSemaphore
, INFINITE
);
3335 if (!initData
.mWnd
) {
3339 mTransitionWnd
= initData
.mWnd
;
3341 auto data
= new FullscreenTransitionData(initData
.mWnd
);
3348 void nsWindow::PerformFullscreenTransition(FullscreenTransitionStage aStage
,
3351 nsIRunnable
* aCallback
) {
3352 auto data
= static_cast<FullscreenTransitionData
*>(aData
);
3353 nsCOMPtr
<nsIRunnable
> callback
= aCallback
;
3354 UINT msg
= aStage
== eBeforeFullscreenToggle
? WM_FULLSCREEN_TRANSITION_BEFORE
3355 : WM_FULLSCREEN_TRANSITION_AFTER
;
3356 WPARAM wparam
= (WPARAM
)callback
.forget().take();
3357 ::PostMessage(data
->mWnd
, msg
, wparam
, (LPARAM
)aDuration
);
3361 void nsWindow::CleanupFullscreenTransition() {
3362 MOZ_ASSERT(NS_IsMainThread(),
3363 "CleanupFullscreenTransition "
3364 "should only run on the main thread");
3366 mTransitionWnd
= nullptr;
3369 void nsWindow::TryDwmResizeHack() {
3370 // The "DWM resize hack", aka the "fullscreen resize hack", is a workaround
3371 // for DWM's occasional and not-entirely-predictable failure to update its
3372 // internal state when the client area of a window changes without changing
3373 // the window size. The effect of this is that DWM will clip the content of
3374 // the window to its former client area.
3376 // It is not known under what circumstances the bug will trigger. Windows 11
3377 // is known to be required, but many Windows 11 machines do not exhibit the
3378 // issue. Even machines that _do_ exhibit it will sometimes not do so when
3379 // apparently-irrelevant changes are made to the configuration. (See bug
3382 // The bug is triggered by Firefox when a maximized window (which has window
3383 // decorations) becomes fullscreen (which doesn't). To work around this, if we
3384 // think it may occur, we "flicker-resize" the relevant window -- that is, we
3385 // reduce its height by 1px, then restore it. This causes DWM to acquire the
3386 // new client-area metrics.
3388 // Note that, in particular, this bug will not occur when using a separate
3389 // compositor window, as our compositor windows never have any nonclient area.
3391 // This is admittedly a sledgehammer where a screwdriver should suffice.
3393 // ---------------------------------------------------------------------------
3395 // Regardless of preferences or heuristics, only apply the hack if this is the
3396 // first time we've entered fullscreen across the entire Firefox session.
3397 // (Subsequent transitions to fullscreen, even with different windows, don't
3398 // appear to induce the bug.)
3400 // (main thread only; `atomic` not needed)
3401 static bool sIsFirstFullscreenEntry
= true;
3402 bool isFirstFullscreenEntry
= sIsFirstFullscreenEntry
;
3403 sIsFirstFullscreenEntry
= false;
3404 if (MOZ_LIKELY(!isFirstFullscreenEntry
)) {
3407 MOZ_LOG(gWindowsLog
, LogLevel::Verbose
,
3408 ("%s: first fullscreen entry", __PRETTY_FUNCTION__
));
3411 // Check whether to try to apply the DWM resize hack, based on the override
3412 // pref and/or some internal heuristics.
3414 const auto hackApplicationHeuristics
= [&]() -> bool {
3415 // The bug has only been seen under Windows 11. (At time of writing, this
3416 // is the latest version of Windows.)
3417 if (!IsWin11OrLater()) {
3421 KnowsCompositor
const* const kc
= mWindowRenderer
->AsKnowsCompositor();
3422 // This should never happen...
3424 // ... so if it does, we are in uncharted territory: don't apply the hack.
3429 // The bug doesn't occur when we're using a separate compositor window
3430 // (since the compositor window always comprises exactly its client area,
3431 // with no non-client border).
3432 if (kc
->GetUseCompositorWnd()) {
3436 // Otherwise, apply the hack.
3440 // Figure out whether or not we should perform the hack, and -- arguably
3441 // more importantly -- log that decision.
3442 bool const shouldApplyHack
= [&]() {
3443 enum Reason
: bool { Pref
, Heuristics
};
3444 auto const msg
= [&](bool decision
, Reason reason
) -> bool {
3445 MOZ_LOG(gWindowsLog
, LogLevel::Verbose
,
3446 ("%s %s per %s", decision
? "applying" : "skipping",
3447 "DWM resize hack", reason
== Pref
? "pref" : "heuristics"));
3450 switch (StaticPrefs::widget_windows_apply_dwm_resize_hack()) {
3452 return msg(false, Pref
);
3454 return msg(true, Pref
);
3455 default: // treat all other values as `auto`
3456 return msg(hackApplicationHeuristics(), Heuristics
);
3460 if (!shouldApplyHack
) {
3465 // The DWM bug is believed to involve a race condition: some users have
3466 // reported that setting a custom theme or adding unused command-line
3467 // parameters sometimes causes the bug to vanish.
3469 // Out of an abundance of caution, we therefore apply the hack in a later
3470 // event, rather than inline.
3471 NS_DispatchToMainThread(NS_NewRunnableFunction(
3472 "nsWindow::TryFullscreenResizeHack", [self
= RefPtr(this)]() {
3473 HWND
const hwnd
= self
->GetWindowHandle();
3475 if (self
->mFrameState
->GetSizeMode() != nsSizeMode_Fullscreen
) {
3476 MOZ_LOG(gWindowsLog
, mozilla::LogLevel::Info
,
3477 ("DWM resize hack: window no longer fullscreen; aborting"));
3482 if (!::GetWindowRect(hwnd
, &origRect
)) {
3483 MOZ_LOG(gWindowsLog
, mozilla::LogLevel::Error
,
3484 ("DWM resize hack: could not get window size?!"));
3487 LONG
const x
= origRect
.left
;
3488 LONG
const y
= origRect
.top
;
3489 LONG
const width
= origRect
.right
- origRect
.left
;
3490 LONG
const height
= origRect
.bottom
- origRect
.top
;
3492 MOZ_DIAGNOSTIC_ASSERT(!self
->mIsPerformingDwmFlushHack
);
3494 MakeScopeExit([&, oldVal
= self
->mIsPerformingDwmFlushHack
]() {
3495 self
->mIsPerformingDwmFlushHack
= oldVal
;
3497 self
->mIsPerformingDwmFlushHack
= true;
3499 MOZ_LOG(gWindowsLog
, LogLevel::Debug
,
3500 ("beginning DWM resize hack for HWND %08" PRIXPTR
,
3502 ::MoveWindow(hwnd
, x
, y
, width
, height
- 1, FALSE
);
3503 ::MoveWindow(hwnd
, x
, y
, width
, height
, TRUE
);
3504 MOZ_LOG(gWindowsLog
, LogLevel::Debug
,
3505 ("concluded DWM resize hack for HWND %08" PRIXPTR
,
3510 void nsWindow::OnFullscreenChanged(nsSizeMode aOldSizeMode
, bool aFullScreen
) {
3511 MOZ_ASSERT((aOldSizeMode
!= nsSizeMode_Fullscreen
) == aFullScreen
);
3513 // HACK: Potentially flicker-resize the window, to force DWM to get the right
3514 // client-area information.
3519 // Hide chrome and reposition window. Note this will also cache dimensions for
3520 // restoration, so it should only be called once per fullscreen request.
3522 // Don't do this when minimized, since our bounds make no sense then, nor when
3523 // coming back from that state.
3524 const bool toOrFromMinimized
=
3525 mFrameState
->GetSizeMode() == nsSizeMode_Minimized
||
3526 aOldSizeMode
== nsSizeMode_Minimized
;
3527 if (!toOrFromMinimized
) {
3528 InfallibleMakeFullScreen(aFullScreen
);
3531 // Possibly notify the taskbar that we have changed our fullscreen mode.
3532 TaskbarConcealer::OnFullscreenChanged(this, aFullScreen
);
3535 nsresult
nsWindow::MakeFullScreen(bool aFullScreen
) {
3536 mFrameState
->EnsureFullscreenMode(aFullScreen
);
3540 /**************************************************************
3542 * SECTION: Native data storage
3544 * nsIWidget::GetNativeData
3545 * nsIWidget::FreeNativeData
3547 * Set or clear native data based on a constant.
3549 **************************************************************/
3551 // Return some native data according to aDataType
3552 void* nsWindow::GetNativeData(uint32_t aDataType
) {
3553 switch (aDataType
) {
3554 case NS_NATIVE_WIDGET
:
3555 case NS_NATIVE_WINDOW
:
3556 case NS_NATIVE_WINDOW_WEBRTC_DEVICE_ID
:
3558 case NS_NATIVE_GRAPHIC
:
3559 MOZ_ASSERT_UNREACHABLE("Not supported on Windows:");
3561 case NS_RAW_NATIVE_IME_CONTEXT
: {
3562 void* pseudoIMEContext
= GetPseudoIMEContext();
3563 if (pseudoIMEContext
) {
3564 return pseudoIMEContext
;
3568 case NS_NATIVE_TSF_THREAD_MGR
:
3569 case NS_NATIVE_TSF_CATEGORY_MGR
:
3570 case NS_NATIVE_TSF_DISPLAY_ATTR_MGR
:
3571 return IMEHandler::GetNativeData(this, aDataType
);
3580 // Free some native data according to aDataType
3581 void nsWindow::FreeNativeData(void* data
, uint32_t aDataType
) {
3582 switch (aDataType
) {
3583 case NS_NATIVE_GRAPHIC
:
3584 case NS_NATIVE_WIDGET
:
3585 case NS_NATIVE_WINDOW
:
3592 /**************************************************************
3594 * SECTION: nsIWidget::SetTitle
3596 * Set the main windows title text.
3598 **************************************************************/
3600 nsresult
nsWindow::SetTitle(const nsAString
& aTitle
) {
3601 const nsString
& strTitle
= PromiseFlatString(aTitle
);
3602 AutoRestore
<bool> sendingText(mSendingSetText
);
3603 mSendingSetText
= true;
3604 ::SendMessageW(mWnd
, WM_SETTEXT
, (WPARAM
)0, (LPARAM
)(LPCWSTR
)strTitle
.get());
3608 /**************************************************************
3610 * SECTION: nsIWidget::SetIcon
3612 * Set the main windows icon.
3614 **************************************************************/
3616 void nsWindow::SetBigIcon(HICON aIcon
) {
3618 (HICON
)::SendMessageW(mWnd
, WM_SETICON
, (WPARAM
)ICON_BIG
, (LPARAM
)aIcon
);
3620 ::DestroyIcon(icon
);
3626 void nsWindow::SetSmallIcon(HICON aIcon
) {
3627 HICON icon
= (HICON
)::SendMessageW(mWnd
, WM_SETICON
, (WPARAM
)ICON_SMALL
,
3630 ::DestroyIcon(icon
);
3636 void nsWindow::SetIcon(const nsAString
& aIconSpec
) {
3637 // Assume the given string is a local identifier for an icon file.
3639 nsCOMPtr
<nsIFile
> iconFile
;
3640 ResolveIconName(aIconSpec
, u
".ico"_ns
, getter_AddRefs(iconFile
));
3641 if (!iconFile
) return;
3643 nsAutoString iconPath
;
3644 iconFile
->GetPath(iconPath
);
3646 // XXX this should use MZLU (see bug 239279)
3651 (HICON
)::LoadImageW(nullptr, (LPCWSTR
)iconPath
.get(), IMAGE_ICON
,
3652 ::GetSystemMetrics(SM_CXICON
),
3653 ::GetSystemMetrics(SM_CYICON
), LR_LOADFROMFILE
);
3655 (HICON
)::LoadImageW(nullptr, (LPCWSTR
)iconPath
.get(), IMAGE_ICON
,
3656 ::GetSystemMetrics(SM_CXSMICON
),
3657 ::GetSystemMetrics(SM_CYSMICON
), LR_LOADFROMFILE
);
3660 SetBigIcon(bigIcon
);
3662 #ifdef DEBUG_SetIcon
3664 NS_LossyConvertUTF16toASCII
cPath(iconPath
);
3665 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
3666 ("\nIcon load error; icon=%s, rc=0x%08X\n\n", cPath
.get(),
3671 SetSmallIcon(smallIcon
);
3673 #ifdef DEBUG_SetIcon
3675 NS_LossyConvertUTF16toASCII
cPath(iconPath
);
3676 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
3677 ("\nSmall icon load error; icon=%s, rc=0x%08X\n\n", cPath
.get(),
3683 void nsWindow::SetBigIconNoData() {
3685 ::LoadIconW(::GetModuleHandleW(nullptr), gStockApplicationIcon
);
3686 SetBigIcon(bigIcon
);
3689 void nsWindow::SetSmallIconNoData() {
3691 ::LoadIconW(::GetModuleHandleW(nullptr), gStockApplicationIcon
);
3692 SetSmallIcon(smallIcon
);
3695 /**************************************************************
3697 * SECTION: nsIWidget::WidgetToScreenOffset
3699 * Return this widget's origin in screen coordinates.
3701 **************************************************************/
3703 LayoutDeviceIntPoint
nsWindow::WidgetToScreenOffset() {
3707 ::ClientToScreen(mWnd
, &point
);
3708 return LayoutDeviceIntPoint(point
.x
, point
.y
);
3711 LayoutDeviceIntMargin
nsWindow::ClientToWindowMargin() {
3712 if (mWindowType
== WindowType::Popup
&& !IsPopupWithTitleBar()) {
3716 if (mCustomNonClient
) {
3717 return NonClientSizeMargin(NormalWindowNonClientOffset());
3720 // Just use a dummy 200x200 at (200, 200) client rect as the rect.
3722 clientRect
.left
= 200;
3723 clientRect
.top
= 200;
3724 clientRect
.right
= 400;
3725 clientRect
.bottom
= 400;
3727 auto ToRect
= [](const RECT
& aRect
) -> LayoutDeviceIntRect
{
3728 return {aRect
.left
, aRect
.top
, aRect
.right
- aRect
.left
,
3729 aRect
.bottom
- aRect
.top
};
3732 RECT windowRect
= clientRect
;
3733 ::AdjustWindowRectEx(&windowRect
, WindowStyle(), false, WindowExStyle());
3735 return ToRect(windowRect
) - ToRect(clientRect
);
3738 /**************************************************************
3740 * SECTION: nsIWidget::EnableDragDrop
3742 * Enables/Disables drag and drop of files on this widget.
3744 **************************************************************/
3746 void nsWindow::EnableDragDrop(bool aEnable
) {
3748 // Return early if the window already closed
3753 if (!mNativeDragTarget
) {
3754 mNativeDragTarget
= new nsNativeDragTarget(this);
3755 mNativeDragTarget
->AddRef();
3756 ::RegisterDragDrop(mWnd
, (LPDROPTARGET
)mNativeDragTarget
);
3759 if (mWnd
&& mNativeDragTarget
) {
3760 ::RevokeDragDrop(mWnd
);
3761 mNativeDragTarget
->DragCancel();
3762 NS_RELEASE(mNativeDragTarget
);
3767 /**************************************************************
3769 * SECTION: nsIWidget::CaptureMouse
3771 * Enables/Disables system mouse capture.
3773 **************************************************************/
3775 void nsWindow::CaptureMouse(bool aCapture
) {
3776 TRACKMOUSEEVENT mTrack
;
3777 mTrack
.cbSize
= sizeof(TRACKMOUSEEVENT
);
3778 mTrack
.dwHoverTime
= 0;
3779 mTrack
.hwndTrack
= mWnd
;
3781 mTrack
.dwFlags
= TME_CANCEL
| TME_LEAVE
;
3784 mTrack
.dwFlags
= TME_LEAVE
;
3787 sIsInMouseCapture
= aCapture
;
3788 TrackMouseEvent(&mTrack
);
3791 /**************************************************************
3793 * SECTION: nsIWidget::CaptureRollupEvents
3795 * Dealing with event rollup on destroy for popups. Enables &
3796 * Disables system capture of any and all events that would
3797 * cause a dropdown to be rolled up.
3799 **************************************************************/
3801 void nsWindow::CaptureRollupEvents(bool aDoCapture
) {
3803 if (!sMsgFilterHook
&& !sCallProcHook
&& !sCallMouseHook
) {
3804 RegisterSpecialDropdownHooks();
3806 sProcessHook
= true;
3808 sProcessHook
= false;
3809 UnregisterSpecialDropdownHooks();
3813 /**************************************************************
3815 * SECTION: nsIWidget::GetAttention
3817 * Bring this window to the user's attention.
3819 **************************************************************/
3821 // Draw user's attention to this window until it comes to foreground.
3822 nsresult
nsWindow::GetAttention(int32_t aCycleCount
) {
3824 if (!mWnd
) return NS_ERROR_NOT_INITIALIZED
;
3826 HWND flashWnd
= WinUtils::GetTopLevelHWND(mWnd
, false, false);
3827 HWND fgWnd
= ::GetForegroundWindow();
3828 // Don't flash if the flash count is 0 or if the foreground window is our
3829 // window handle or that of our owned-most window.
3830 if (aCycleCount
== 0 || flashWnd
== fgWnd
||
3831 flashWnd
== WinUtils::GetTopLevelHWND(fgWnd
, false, false)) {
3835 DWORD defaultCycleCount
= 0;
3836 ::SystemParametersInfo(SPI_GETFOREGROUNDFLASHCOUNT
, 0, &defaultCycleCount
, 0);
3838 FLASHWINFO flashInfo
= {sizeof(FLASHWINFO
), flashWnd
, FLASHW_ALL
,
3839 aCycleCount
> 0 ? aCycleCount
: defaultCycleCount
, 0};
3840 ::FlashWindowEx(&flashInfo
);
3845 void nsWindow::StopFlashing() {
3846 HWND flashWnd
= mWnd
;
3847 while (HWND ownerWnd
= ::GetWindow(flashWnd
, GW_OWNER
)) {
3848 flashWnd
= ownerWnd
;
3851 FLASHWINFO flashInfo
= {sizeof(FLASHWINFO
), flashWnd
, FLASHW_STOP
, 0, 0};
3852 ::FlashWindowEx(&flashInfo
);
3855 /**************************************************************
3857 * SECTION: nsIWidget::HasPendingInputEvent
3859 * Ask whether there user input events pending. All input events are
3860 * included, including those not targeted at this nsIwidget instance.
3862 **************************************************************/
3864 bool nsWindow::HasPendingInputEvent() {
3865 // If there is pending input or the user is currently
3866 // moving the window then return true.
3867 // Note: When the user is moving the window WIN32 spins
3868 // a separate event loop and input events are not
3869 // reported to the application.
3870 if (HIWORD(GetQueueStatus(QS_INPUT
))) return true;
3871 GUITHREADINFO guiInfo
;
3872 guiInfo
.cbSize
= sizeof(GUITHREADINFO
);
3873 if (!GetGUIThreadInfo(GetCurrentThreadId(), &guiInfo
)) return false;
3874 return GUI_INMOVESIZE
== (guiInfo
.flags
& GUI_INMOVESIZE
);
3877 /**************************************************************
3879 * SECTION: nsIWidget::GetWindowRenderer
3881 * Get the window renderer associated with this widget.
3883 **************************************************************/
3885 WindowRenderer
* nsWindow::GetWindowRenderer() {
3886 if (mWindowRenderer
) {
3887 return mWindowRenderer
;
3890 if (!mLocalesChangedObserver
) {
3891 mLocalesChangedObserver
= new LocalesChangedObserver(this);
3895 if (!mWindowRenderer
&& ShouldUseOffMainThreadCompositing()) {
3896 gfxWindowsPlatform::GetPlatform()->UpdateRenderMode();
3900 if (!mWindowRenderer
) {
3901 MOZ_ASSERT(!mCompositorSession
&& !mCompositorBridgeChild
);
3902 MOZ_ASSERT(!mCompositorWidgetDelegate
);
3904 // Ensure we have a widget proxy even if we're not using the compositor,
3905 // since all our transparent window handling lives there.
3906 WinCompositorWidgetInitData
initData(
3907 reinterpret_cast<uintptr_t>(mWnd
),
3908 reinterpret_cast<uintptr_t>(static_cast<nsIWidget
*>(this)),
3909 mTransparencyMode
, mFrameState
->GetSizeMode());
3910 // If we're not using the compositor, the options don't actually matter.
3911 CompositorOptions
options(false, false);
3912 mBasicLayersSurface
=
3913 new InProcessWinCompositorWidget(initData
, options
, this);
3914 mCompositorWidgetDelegate
= mBasicLayersSurface
;
3915 mWindowRenderer
= CreateFallbackRenderer();
3918 NS_ASSERTION(mWindowRenderer
, "Couldn't provide a valid window renderer.");
3920 if (mWindowRenderer
) {
3921 // Update the size constraints now that the layer manager has been
3923 KnowsCompositor
* knowsCompositor
= mWindowRenderer
->AsKnowsCompositor();
3924 if (knowsCompositor
) {
3925 SizeConstraints c
= mSizeConstraints
;
3926 mMaxTextureSize
= knowsCompositor
->GetMaxTextureSize();
3927 c
.mMaxSize
.width
= std::min(c
.mMaxSize
.width
, mMaxTextureSize
);
3928 c
.mMaxSize
.height
= std::min(c
.mMaxSize
.height
, mMaxTextureSize
);
3929 nsBaseWidget::SetSizeConstraints(c
);
3933 return mWindowRenderer
;
3936 /**************************************************************
3938 * SECTION: nsBaseWidget::SetCompositorWidgetDelegate
3940 * Called to connect the nsWindow to the delegate providing
3941 * platform compositing API access.
3943 **************************************************************/
3945 void nsWindow::SetCompositorWidgetDelegate(CompositorWidgetDelegate
* delegate
) {
3947 mCompositorWidgetDelegate
= delegate
->AsPlatformSpecificDelegate();
3948 MOZ_ASSERT(mCompositorWidgetDelegate
,
3949 "nsWindow::SetCompositorWidgetDelegate called with a "
3950 "non-PlatformCompositorWidgetDelegate");
3952 mCompositorWidgetDelegate
= nullptr;
3956 /**************************************************************
3958 * SECTION: nsIWidget::OnDefaultButtonLoaded
3960 * Called after the dialog is loaded and it has a default button.
3962 **************************************************************/
3964 nsresult
nsWindow::OnDefaultButtonLoaded(
3965 const LayoutDeviceIntRect
& aButtonRect
) {
3966 if (aButtonRect
.IsEmpty()) return NS_OK
;
3968 // Don't snap when we are not active.
3969 HWND activeWnd
= ::GetActiveWindow();
3970 if (activeWnd
!= ::GetForegroundWindow() ||
3971 WinUtils::GetTopLevelHWND(mWnd
, true) !=
3972 WinUtils::GetTopLevelHWND(activeWnd
, true)) {
3976 bool isAlwaysSnapCursor
=
3977 Preferences::GetBool("ui.cursor_snapping.always_enabled", false);
3979 if (!isAlwaysSnapCursor
) {
3980 BOOL snapDefaultButton
;
3981 if (!::SystemParametersInfo(SPI_GETSNAPTODEFBUTTON
, 0, &snapDefaultButton
,
3987 LayoutDeviceIntRect widgetRect
= GetScreenBounds();
3988 LayoutDeviceIntRect
buttonRect(aButtonRect
+ widgetRect
.TopLeft());
3990 LayoutDeviceIntPoint
centerOfButton(buttonRect
.X() + buttonRect
.Width() / 2,
3991 buttonRect
.Y() + buttonRect
.Height() / 2);
3992 // The center of the button can be outside of the widget.
3993 // E.g., it could be hidden by scrolling.
3994 if (!widgetRect
.Contains(centerOfButton
)) {
3998 if (!::SetCursorPos(centerOfButton
.x
, centerOfButton
.y
)) {
3999 NS_ERROR("SetCursorPos failed");
4000 return NS_ERROR_FAILURE
;
4005 uint32_t nsWindow::GetMaxTouchPoints() const {
4006 return WinUtils::GetMaxTouchPoints();
4009 void nsWindow::SetWindowClass(const nsAString
& xulWinType
,
4010 const nsAString
& xulWinClass
,
4011 const nsAString
& xulWinName
) {
4012 mIsEarlyBlankWindow
= xulWinType
.EqualsLiteral("navigator:blank");
4015 /**************************************************************
4016 **************************************************************
4018 ** BLOCK: Moz Events
4020 ** Moz GUI event management.
4022 **************************************************************
4023 **************************************************************/
4025 /**************************************************************
4027 * SECTION: Mozilla event initialization
4029 * Helpers for initializing moz events.
4031 **************************************************************/
4033 // Event initialization
4034 void nsWindow::InitEvent(WidgetGUIEvent
& event
, LayoutDeviceIntPoint
* aPoint
) {
4035 if (nullptr == aPoint
) { // use the point from the event
4036 // get the message position in client coordinates
4037 if (mWnd
!= nullptr) {
4038 DWORD pos
= ::GetMessagePos();
4041 cpos
.x
= GET_X_LPARAM(pos
);
4042 cpos
.y
= GET_Y_LPARAM(pos
);
4044 ::ScreenToClient(mWnd
, &cpos
);
4045 event
.mRefPoint
= LayoutDeviceIntPoint(cpos
.x
, cpos
.y
);
4047 event
.mRefPoint
= LayoutDeviceIntPoint(0, 0);
4050 // use the point override if provided
4051 event
.mRefPoint
= *aPoint
;
4054 event
.AssignEventTime(CurrentMessageWidgetEventTime());
4057 WidgetEventTime
nsWindow::CurrentMessageWidgetEventTime() const {
4058 LONG messageTime
= ::GetMessageTime();
4059 return WidgetEventTime(GetMessageTimeStamp(messageTime
));
4062 /**************************************************************
4064 * SECTION: Moz event dispatch helpers
4066 * Helpers for dispatching different types of moz events.
4068 **************************************************************/
4070 // Main event dispatch. Invokes callback and ProcessEvent method on
4071 // Event Listener object. Part of nsIWidget.
4072 nsresult
nsWindow::DispatchEvent(WidgetGUIEvent
* event
,
4073 nsEventStatus
& aStatus
) {
4074 #ifdef WIDGET_DEBUG_OUTPUT
4075 debug_DumpEvent(stdout
, event
->mWidget
, event
, "something", (int32_t)mWnd
);
4076 #endif // WIDGET_DEBUG_OUTPUT
4078 aStatus
= nsEventStatus_eIgnore
;
4080 // Top level windows can have a view attached which requires events be sent
4081 // to the underlying base window and the view. Added when we combined the
4082 // base chrome window with the main content child for nc client area (title
4084 if (mAttachedWidgetListener
) {
4085 aStatus
= mAttachedWidgetListener
->HandleEvent(event
, mUseAttachedEvents
);
4086 } else if (mWidgetListener
) {
4087 aStatus
= mWidgetListener
->HandleEvent(event
, mUseAttachedEvents
);
4090 // the window can be destroyed during processing of seemingly innocuous events
4091 // like, say, mousedowns due to the magic of scripting. mousedowns will return
4092 // nsEventStatus_eIgnore, which causes problems with the deleted window.
4094 if (mOnDestroyCalled
) aStatus
= nsEventStatus_eConsumeNoDefault
;
4098 bool nsWindow::DispatchStandardEvent(EventMessage aMsg
) {
4099 WidgetGUIEvent
event(true, aMsg
, this);
4102 bool result
= DispatchWindowEvent(event
);
4106 bool nsWindow::DispatchKeyboardEvent(WidgetKeyboardEvent
* event
) {
4107 nsEventStatus status
= DispatchInputEvent(event
).mContentStatus
;
4108 return ConvertStatus(status
);
4111 bool nsWindow::DispatchContentCommandEvent(WidgetContentCommandEvent
* aEvent
) {
4112 nsEventStatus status
;
4113 DispatchEvent(aEvent
, status
);
4114 return ConvertStatus(status
);
4117 bool nsWindow::DispatchWheelEvent(WidgetWheelEvent
* aEvent
) {
4118 nsEventStatus status
=
4119 DispatchInputEvent(aEvent
->AsInputEvent()).mContentStatus
;
4120 return ConvertStatus(status
);
4123 // Recursively dispatch synchronous paints for nsIWidget
4124 // descendants with invalidated rectangles.
4125 BOOL CALLBACK
nsWindow::DispatchStarvedPaints(HWND aWnd
, LPARAM aMsg
) {
4126 LONG_PTR proc
= ::GetWindowLongPtrW(aWnd
, GWLP_WNDPROC
);
4127 if (proc
== (LONG_PTR
)&nsWindow::WindowProc
) {
4128 // its one of our windows so check to see if it has a
4129 // invalidated rect. If it does. Dispatch a synchronous
4131 if (GetUpdateRect(aWnd
, nullptr, FALSE
)) VERIFY(::UpdateWindow(aWnd
));
4136 // Check for pending paints and dispatch any pending paint
4137 // messages for any nsIWidget which is a descendant of the
4138 // top-level window that *this* window is embedded within.
4140 // Note: We do not dispatch pending paint messages for non
4141 // nsIWidget managed windows.
4142 void nsWindow::DispatchPendingEvents() {
4143 // We need to ensure that reflow events do not get starved.
4144 // At the same time, we don't want to recurse through here
4145 // as that would prevent us from dispatching starved paints.
4146 static int recursionBlocker
= 0;
4147 if (recursionBlocker
++ == 0) {
4148 NS_ProcessPendingEvents(nullptr, PR_MillisecondsToInterval(100));
4152 // Quickly check to see if there are any paint events pending,
4153 // but only dispatch them if it has been long enough since the
4154 // last paint completed.
4155 if (::GetQueueStatus(QS_PAINT
) &&
4156 ((TimeStamp::Now() - mLastPaintEndTime
).ToMilliseconds() >= 50)) {
4157 // Find the top level window.
4158 HWND topWnd
= WinUtils::GetTopLevelHWND(mWnd
);
4160 // Dispatch pending paints for topWnd and all its descendant windows.
4161 // Note: EnumChildWindows enumerates all descendant windows not just
4162 // the children (but not the window itself).
4163 nsWindow::DispatchStarvedPaints(topWnd
, 0);
4164 ::EnumChildWindows(topWnd
, nsWindow::DispatchStarvedPaints
, 0);
4168 void nsWindow::DispatchCustomEvent(const nsString
& eventName
) {
4169 if (Document
* doc
= GetDocument()) {
4170 if (nsPIDOMWindowOuter
* win
= doc
->GetWindow()) {
4171 win
->DispatchCustomEvent(eventName
, ChromeOnlyDispatch::eYes
);
4176 bool nsWindow::TouchEventShouldStartDrag(EventMessage aEventMessage
,
4177 LayoutDeviceIntPoint aEventPoint
) {
4178 // Allow users to start dragging by double-tapping.
4179 if (aEventMessage
== eMouseDoubleClick
) {
4183 // In chrome UI, allow touchdownstartsdrag attributes
4184 // to cause any touchdown event to trigger a drag.
4185 if (aEventMessage
== eMouseDown
) {
4186 WidgetMouseEvent
hittest(true, eMouseHitTest
, this,
4187 WidgetMouseEvent::eReal
);
4188 hittest
.mRefPoint
= aEventPoint
;
4189 hittest
.mIgnoreRootScrollFrame
= true;
4190 hittest
.mInputSource
= MouseEvent_Binding::MOZ_SOURCE_TOUCH
;
4191 DispatchInputEvent(&hittest
);
4193 if (EventTarget
* target
= hittest
.GetDOMEventTarget()) {
4194 if (nsIContent
* content
= nsIContent::FromEventTarget(target
)) {
4195 // Check if the element or any parent element has the
4196 // attribute we're looking for.
4197 for (Element
* element
= content
->GetAsElementOrParentElement(); element
;
4198 element
= element
->GetParentElement()) {
4199 nsAutoString startDrag
;
4200 element
->GetAttribute(u
"touchdownstartsdrag"_ns
, startDrag
);
4201 if (!startDrag
.IsEmpty()) {
4212 // Deal with all sort of mouse event
4213 bool nsWindow::DispatchMouseEvent(EventMessage aEventMessage
, WPARAM wParam
,
4214 LPARAM lParam
, bool aIsContextMenuKey
,
4215 int16_t aButton
, uint16_t aInputSource
,
4216 WinPointerInfo
* aPointerInfo
,
4218 ContextMenuPreventer
contextMenuPreventer(this);
4219 bool result
= false;
4223 if (!mWidgetListener
) {
4227 LayoutDeviceIntPoint
eventPoint(GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
4228 LayoutDeviceIntPoint mpScreen
= eventPoint
+ WidgetToScreenOffset();
4230 // Suppress mouse moves caused by widget creation. Make sure to do this early
4231 // so that we update sLastMouseMovePoint even for touch-induced mousemove
4233 if (aEventMessage
== eMouseMove
) {
4234 if ((sLastMouseMovePoint
.x
== mpScreen
.x
.value
) &&
4235 (sLastMouseMovePoint
.y
== mpScreen
.y
.value
)) {
4238 sLastMouseMovePoint
.x
= mpScreen
.x
;
4239 sLastMouseMovePoint
.y
= mpScreen
.y
;
4242 if (!aIgnoreAPZ
&& WinUtils::GetIsMouseFromTouch(aEventMessage
)) {
4244 // If mTouchWindow is true, then we must have APZ enabled and be
4245 // feeding it raw touch events. In that case we only want to
4246 // send touch-generated mouse events to content if they should
4247 // start a touch-based drag-and-drop gesture, such as on
4248 // double-tapping or when tapping elements marked with the
4249 // touchdownstartsdrag attribute in chrome UI.
4251 if (TouchEventShouldStartDrag(aEventMessage
, eventPoint
)) {
4252 aEventMessage
= eMouseTouchDrag
;
4259 uint32_t pointerId
=
4260 aPointerInfo
? aPointerInfo
->pointerId
: MOUSE_POINTERID();
4262 switch (aEventMessage
) {
4267 // eMouseMove and eMouseExitFromWidget are here because we need to make
4268 // sure capture flag isn't left on after a drag where we wouldn't see a
4269 // button up message (see bug 324131).
4272 case eMouseExitFromWidget
:
4273 if (!(wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
)) &&
4275 CaptureMouse(false);
4283 WidgetMouseEvent
event(true, aEventMessage
, this, WidgetMouseEvent::eReal
,
4284 aIsContextMenuKey
? WidgetMouseEvent::eContextMenuKey
4285 : WidgetMouseEvent::eNormal
);
4286 if (aEventMessage
== eContextMenu
&& aIsContextMenuKey
) {
4287 LayoutDeviceIntPoint
zero(0, 0);
4288 InitEvent(event
, &zero
);
4290 InitEvent(event
, &eventPoint
);
4293 ModifierKeyState modifierKeyState
;
4294 modifierKeyState
.InitInputEvent(event
);
4296 // eContextMenu with Shift state is special. It won't fire "contextmenu"
4297 // event in the web content for blocking web content to prevent its default.
4298 // However, Shift+F10 is a standard shortcut key on Windows. Therefore,
4299 // this should not block web page to prevent its default. I.e., it should
4300 // behave same as ContextMenu key without Shift key.
4301 // XXX Should we allow to block web page to prevent its default with
4302 // Ctrl+Shift+F10 or Alt+Shift+F10 instead?
4303 if (aEventMessage
== eContextMenu
&& aIsContextMenuKey
&& event
.IsShift() &&
4304 NativeKey::LastKeyOrCharMSG().message
== WM_SYSKEYDOWN
&&
4305 NativeKey::LastKeyOrCharMSG().wParam
== VK_F10
) {
4306 event
.mModifiers
&= ~MODIFIER_SHIFT
;
4309 event
.mButton
= aButton
;
4310 event
.mInputSource
= aInputSource
;
4312 // Mouse events from Windows WM_POINTER*. Fill more information in
4313 // WidgetMouseEvent.
4314 event
.AssignPointerHelperData(*aPointerInfo
);
4315 event
.mPressure
= aPointerInfo
->mPressure
;
4316 event
.mButtons
= aPointerInfo
->mButtons
;
4318 // If we get here the mouse events must be from non-touch sources, so
4319 // convert it to pointer events as well
4320 event
.convertToPointer
= true;
4321 event
.pointerId
= pointerId
;
4324 // Static variables used to distinguish simple-, double- and triple-clicks.
4325 static POINT sLastMousePoint
= {0};
4326 static LONG sLastMouseDownTime
= 0L;
4327 static LONG sLastClickCount
= 0L;
4328 static BYTE sLastMouseButton
= 0;
4330 bool insideMovementThreshold
=
4331 (DeprecatedAbs(sLastMousePoint
.x
- eventPoint
.x
.value
) <
4332 (short)::GetSystemMetrics(SM_CXDOUBLECLK
)) &&
4333 (DeprecatedAbs(sLastMousePoint
.y
- eventPoint
.y
.value
) <
4334 (short)::GetSystemMetrics(SM_CYDOUBLECLK
));
4338 case MouseButton::ePrimary
:
4339 eventButton
= VK_LBUTTON
;
4341 case MouseButton::eMiddle
:
4342 eventButton
= VK_MBUTTON
;
4344 case MouseButton::eSecondary
:
4345 eventButton
= VK_RBUTTON
;
4352 // Doubleclicks are used to set the click count, then changed to mousedowns
4353 // We're going to time double-clicks from mouse *up* to next mouse *down*
4354 LONG curMsgTime
= ::GetMessageTime();
4356 switch (aEventMessage
) {
4357 case eMouseDoubleClick
:
4358 event
.mMessage
= eMouseDown
;
4359 event
.mButton
= aButton
;
4360 sLastClickCount
= 2;
4361 sLastMouseDownTime
= curMsgTime
;
4364 // remember when this happened for the next mouse down
4365 sLastMousePoint
.x
= eventPoint
.x
;
4366 sLastMousePoint
.y
= eventPoint
.y
;
4367 sLastMouseButton
= eventButton
;
4370 // now look to see if we want to convert this to a double- or triple-click
4371 if (((curMsgTime
- sLastMouseDownTime
) < (LONG
)::GetDoubleClickTime()) &&
4372 insideMovementThreshold
&& eventButton
== sLastMouseButton
) {
4375 // reset the click count, to count *this* click
4376 sLastClickCount
= 1;
4378 // Set last Click time on MouseDown only
4379 sLastMouseDownTime
= curMsgTime
;
4382 if (!insideMovementThreshold
) {
4383 sLastClickCount
= 0;
4386 case eMouseExitFromWidget
:
4388 Some(IsTopLevelMouseExit(mWnd
) ? WidgetMouseEvent::ePlatformTopLevel
4389 : WidgetMouseEvent::ePlatformChild
);
4394 event
.mClickCount
= sLastClickCount
;
4397 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
4398 ("Msg Time: %d Click Count: %d\n", curMsgTime
, event
.mClickCount
));
4401 // call the event callback
4402 if (mWidgetListener
) {
4403 if (aEventMessage
== eMouseMove
) {
4404 LayoutDeviceIntRect rect
= GetBounds();
4407 if (rect
.Contains(event
.mRefPoint
)) {
4408 if (sCurrentWindow
== nullptr || sCurrentWindow
!= this) {
4409 if ((nullptr != sCurrentWindow
) && (!sCurrentWindow
->mInDtor
)) {
4410 LPARAM pos
= sCurrentWindow
->lParamToClient(lParamToScreen(lParam
));
4411 sCurrentWindow
->DispatchMouseEvent(
4412 eMouseExitFromWidget
, wParam
, pos
, false, MouseButton::ePrimary
,
4413 aInputSource
, aPointerInfo
);
4415 sCurrentWindow
= this;
4417 LPARAM pos
= sCurrentWindow
->lParamToClient(lParamToScreen(lParam
));
4418 sCurrentWindow
->DispatchMouseEvent(
4419 eMouseEnterIntoWidget
, wParam
, pos
, false,
4420 MouseButton::ePrimary
, aInputSource
, aPointerInfo
);
4424 } else if (aEventMessage
== eMouseExitFromWidget
) {
4425 if (sCurrentWindow
== this) {
4426 sCurrentWindow
= nullptr;
4430 nsIWidget::ContentAndAPZEventStatus eventStatus
=
4431 DispatchInputEvent(&event
);
4432 contextMenuPreventer
.Update(event
, eventStatus
);
4433 return ConvertStatus(eventStatus
.mContentStatus
);
4439 HWND
nsWindow::GetTopLevelForFocus(HWND aCurWnd
) {
4440 // retrieve the toplevel window or dialogue
4441 HWND toplevelWnd
= nullptr;
4443 toplevelWnd
= aCurWnd
;
4444 nsWindow
* win
= WinUtils::GetNSWindowPtr(aCurWnd
);
4446 if (win
->mWindowType
== WindowType::TopLevel
||
4447 win
->mWindowType
== WindowType::Dialog
) {
4452 aCurWnd
= ::GetParent(aCurWnd
); // Parent or owner (if has no parent)
4457 void nsWindow::DispatchFocusToTopLevelWindow(bool aIsActivate
) {
4459 sJustGotActivate
= false;
4461 sJustGotDeactivate
= false;
4462 mLastKillFocusWindow
= nullptr;
4464 HWND toplevelWnd
= GetTopLevelForFocus(mWnd
);
4467 nsWindow
* win
= WinUtils::GetNSWindowPtr(toplevelWnd
);
4468 if (win
&& win
->mWidgetListener
) {
4470 win
->mWidgetListener
->WindowActivated();
4472 win
->mWidgetListener
->WindowDeactivated();
4478 HWND
nsWindow::WindowAtMouse() {
4479 DWORD pos
= ::GetMessagePos();
4481 mp
.x
= GET_X_LPARAM(pos
);
4482 mp
.y
= GET_Y_LPARAM(pos
);
4483 return ::WindowFromPoint(mp
);
4486 bool nsWindow::IsTopLevelMouseExit(HWND aWnd
) {
4487 HWND mouseWnd
= WindowAtMouse();
4489 // WinUtils::GetTopLevelHWND() will return a HWND for the window frame
4490 // (which includes the non-client area). If the mouse has moved into
4491 // the non-client area, we should treat it as a top-level exit.
4492 HWND mouseTopLevel
= WinUtils::GetTopLevelHWND(mouseWnd
);
4493 if (mouseWnd
== mouseTopLevel
) return true;
4495 return WinUtils::GetTopLevelHWND(aWnd
) != mouseTopLevel
;
4498 /**************************************************************
4502 * IPC related helpers.
4504 **************************************************************/
4507 bool nsWindow::IsAsyncResponseEvent(UINT aMsg
, LRESULT
& aResult
) {
4512 case WM_WINDOWPOSCHANGING
:
4513 case WM_WINDOWPOSCHANGED
:
4514 case WM_PARENTNOTIFY
:
4515 case WM_ACTIVATEAPP
:
4518 case WM_CHILDACTIVATE
:
4519 case WM_IME_SETCONTEXT
:
4523 case WM_MOUSEACTIVATE
:
4524 case WM_CONTEXTMENU
:
4528 case WM_SETTINGCHANGE
:
4536 "An unhandled ISMEX_SEND message was received during spin loop! (%X)",
4544 void nsWindow::IPCWindowProcHandler(UINT
& msg
, WPARAM
& wParam
, LPARAM
& lParam
) {
4546 msg
!= WM_GETOBJECT
,
4547 !mozilla::ipc::MessageChannel::IsPumpingMessages() ||
4548 mozilla::ipc::SuppressedNeuteringRegion::IsNeuteringSuppressed());
4550 // Modal UI being displayed in windowless plugins.
4551 if (mozilla::ipc::MessageChannel::IsSpinLoopActive() &&
4552 (InSendMessageEx(nullptr) & (ISMEX_REPLIED
| ISMEX_SEND
)) == ISMEX_SEND
) {
4554 if (IsAsyncResponseEvent(msg
, res
)) {
4560 // Handle certain sync plugin events sent to the parent which
4561 // trigger ipc calls that result in deadlocks.
4564 bool handled
= false;
4567 // Windowless flash sending WM_ACTIVATE events to the main window
4568 // via calls to ShowWindow.
4570 if (lParam
!= 0 && LOWORD(wParam
) == WA_ACTIVE
&&
4571 IsWindow((HWND
)lParam
)) {
4572 // Check for Adobe Reader X sync activate message from their
4573 // helper window and ignore. Fixes an annoying focus problem.
4574 if ((InSendMessageEx(nullptr) & (ISMEX_REPLIED
| ISMEX_SEND
)) ==
4576 wchar_t szClass
[10];
4577 HWND focusWnd
= (HWND
)lParam
;
4578 if (IsWindowVisible(focusWnd
) &&
4579 GetClassNameW(focusWnd
, szClass
,
4580 sizeof(szClass
) / sizeof(char16_t
)) &&
4581 !wcscmp(szClass
, L
"Edit") &&
4582 !WinUtils::IsOurProcessWindow(focusWnd
)) {
4589 // Plugins taking or losing focus triggering focus app messages.
4592 // Windowed plugins that pass sys key events to defwndproc generate
4593 // WM_SYSCOMMAND events to the main window.
4595 // Windowed plugins that fire context menu selection events to parent
4597 case WM_CONTEXTMENU
:
4598 // IME events fired as a result of synchronous focus changes
4599 case WM_IME_SETCONTEXT
:
4605 (InSendMessageEx(nullptr) & (ISMEX_REPLIED
| ISMEX_SEND
)) == ISMEX_SEND
) {
4606 ReplyMessage(dwResult
);
4610 /**************************************************************
4611 **************************************************************
4613 ** BLOCK: Native events
4615 ** Main Windows message handlers and OnXXX handlers for
4616 ** Windows event handling.
4618 **************************************************************
4619 **************************************************************/
4621 /**************************************************************
4623 * SECTION: Wind proc.
4625 * The main Windows event procedures and associated
4626 * message processing methods.
4628 **************************************************************/
4630 static bool DisplaySystemMenu(HWND hWnd
, nsSizeMode sizeMode
, bool isRtl
,
4631 int32_t x
, int32_t y
) {
4632 HMENU hMenu
= GetSystemMenu(hWnd
, FALSE
);
4635 mii
.cbSize
= sizeof(MENUITEMINFO
);
4636 mii
.fMask
= MIIM_STATE
;
4639 // update the options
4640 mii
.fState
= MF_ENABLED
;
4641 SetMenuItemInfo(hMenu
, SC_RESTORE
, FALSE
, &mii
);
4642 SetMenuItemInfo(hMenu
, SC_SIZE
, FALSE
, &mii
);
4643 SetMenuItemInfo(hMenu
, SC_MOVE
, FALSE
, &mii
);
4644 SetMenuItemInfo(hMenu
, SC_MAXIMIZE
, FALSE
, &mii
);
4645 SetMenuItemInfo(hMenu
, SC_MINIMIZE
, FALSE
, &mii
);
4647 mii
.fState
= MF_GRAYED
;
4649 case nsSizeMode_Fullscreen
:
4650 // intentional fall through
4651 case nsSizeMode_Maximized
:
4652 SetMenuItemInfo(hMenu
, SC_SIZE
, FALSE
, &mii
);
4653 SetMenuItemInfo(hMenu
, SC_MOVE
, FALSE
, &mii
);
4654 SetMenuItemInfo(hMenu
, SC_MAXIMIZE
, FALSE
, &mii
);
4656 case nsSizeMode_Minimized
:
4657 SetMenuItemInfo(hMenu
, SC_MINIMIZE
, FALSE
, &mii
);
4659 case nsSizeMode_Normal
:
4660 SetMenuItemInfo(hMenu
, SC_RESTORE
, FALSE
, &mii
);
4662 case nsSizeMode_Invalid
:
4663 NS_ASSERTION(false, "Did the argument come from invalid IPC?");
4666 MOZ_ASSERT_UNREACHABLE("Unhnalded nsSizeMode value detected");
4669 LPARAM cmd
= TrackPopupMenu(
4671 (TPM_LEFTBUTTON
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
| TPM_TOPALIGN
|
4672 (isRtl
? TPM_RIGHTALIGN
: TPM_LEFTALIGN
)),
4673 x
, y
, 0, hWnd
, nullptr);
4675 PostMessage(hWnd
, WM_SYSCOMMAND
, cmd
, 0);
4682 // The WndProc procedure for all nsWindows in this toolkit. This merely catches
4683 // SEH exceptions and passes the real work to WindowProcInternal. See bug 587406
4684 // and http://msdn.microsoft.com/en-us/library/ms633573%28VS.85%29.aspx
4685 LRESULT CALLBACK
nsWindow::WindowProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
4687 mozilla::ipc::CancelCPOWs();
4689 BackgroundHangMonitor().NotifyActivity();
4691 return mozilla::CallWindowProcCrashProtected(WindowProcInternal
, hWnd
, msg
,
4695 LRESULT CALLBACK
nsWindow::WindowProcInternal(HWND hWnd
, UINT msg
,
4696 WPARAM wParam
, LPARAM lParam
) {
4697 if (::GetWindowLongPtrW(hWnd
, GWLP_ID
) == eFakeTrackPointScrollableID
) {
4698 // This message was sent to the FAKETRACKPOINTSCROLLABLE.
4699 if (msg
== WM_HSCROLL
) {
4700 // Route WM_HSCROLL messages to the main window.
4701 hWnd
= ::GetParent(::GetParent(hWnd
));
4703 // Handle all other messages with its original window procedure.
4704 WNDPROC prevWindowProc
= (WNDPROC
)::GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
4705 return ::CallWindowProcW(prevWindowProc
, hWnd
, msg
, wParam
, lParam
);
4709 if (msg
== MOZ_WM_TRACE
) {
4710 // This is a tracer event for measuring event loop latency.
4711 // See WidgetTraceEvent.cpp for more details.
4712 mozilla::SignalTracerThread();
4716 // Get the window which caused the event and ask it to process the message
4717 nsWindow
* targetWindow
= WinUtils::GetNSWindowPtr(hWnd
);
4718 NS_ASSERTION(targetWindow
, "nsWindow* is null!");
4719 if (!targetWindow
) return ::DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
4721 // Hold the window for the life of this method, in case it gets
4722 // destroyed during processing, unless we're in the dtor already.
4723 nsCOMPtr
<nsIWidget
> kungFuDeathGrip
;
4724 if (!targetWindow
->mInDtor
) kungFuDeathGrip
= targetWindow
;
4726 targetWindow
->IPCWindowProcHandler(msg
, wParam
, lParam
);
4728 // Create this here so that we store the last rolled up popup until after
4729 // the event has been processed.
4730 nsAutoRollup autoRollup
;
4732 LRESULT popupHandlingResult
;
4733 if (DealWithPopups(hWnd
, msg
, wParam
, lParam
, &popupHandlingResult
))
4734 return popupHandlingResult
;
4736 // Call ProcessMessage
4738 if (targetWindow
->ProcessMessage(msg
, wParam
, lParam
, &retValue
)) {
4742 LRESULT res
= ::CallWindowProcW(targetWindow
->GetPrevWindowProc(), hWnd
, msg
,
4748 const char16_t
* GetQuitType() {
4749 if (Preferences::GetBool(PREF_WIN_REGISTER_APPLICATION_RESTART
, false)) {
4750 DWORD cchCmdLine
= 0;
4751 HRESULT rc
= ::GetApplicationRestartSettings(::GetCurrentProcess(), nullptr,
4752 &cchCmdLine
, nullptr);
4754 return u
"os-restart";
4760 bool nsWindow::ExternalHandlerProcessMessage(UINT aMessage
, WPARAM
& aWParam
,
4762 MSGResult
& aResult
) {
4763 if (mWindowHook
.Notify(mWnd
, aMessage
, aWParam
, aLParam
, aResult
)) {
4767 if (IMEHandler::ProcessMessage(this, aMessage
, aWParam
, aLParam
, aResult
)) {
4771 if (MouseScrollHandler::ProcessMessage(this, aMessage
, aWParam
, aLParam
,
4779 // The main windows message processing method. Wraps ProcessMessageInternal so
4780 // we can log aRetValue.
4781 bool nsWindow::ProcessMessage(UINT msg
, WPARAM
& wParam
, LPARAM
& lParam
,
4782 LRESULT
* aRetValue
) {
4783 // For some events we might change the parameter values, so log
4784 // before and after we process them.
4785 NativeEventLogger
eventLogger("nsWindow", mWnd
, msg
, wParam
, lParam
);
4786 bool result
= ProcessMessageInternal(msg
, wParam
, lParam
, aRetValue
);
4787 eventLogger
.SetResult(*aRetValue
, result
);
4792 // The main windows message processing method. Called by ProcessMessage.
4793 bool nsWindow::ProcessMessageInternal(UINT msg
, WPARAM
& wParam
, LPARAM
& lParam
,
4794 LRESULT
* aRetValue
) {
4795 MSGResult
msgResult(aRetValue
);
4796 if (ExternalHandlerProcessMessage(msg
, wParam
, lParam
, msgResult
)) {
4797 return (msgResult
.mConsumed
|| !mWnd
);
4800 bool result
= false; // call the default nsWindow proc
4803 // The DWM resize hack (see bug 1763981) causes us to process a number of
4804 // messages, notably including some WM_WINDOWPOSCHANG{ING,ED} messages which
4805 // would ordinarily result in a whole lot of internal state being updated.
4807 // Since we're supposed to end in the same state we started in (and since the
4808 // content shouldn't know about any of this nonsense), just discard any
4809 // messages synchronously dispatched from within the hack.
4810 if (MOZ_UNLIKELY(mIsPerformingDwmFlushHack
)) {
4814 // Glass hit testing w/custom transparent margins.
4816 // FIXME(emilio): is this needed? We deal with titlebar buttons non-natively
4818 LRESULT dwmHitResult
;
4819 if (mCustomNonClient
&&
4820 DwmDefWindowProc(mWnd
, msg
, wParam
, lParam
, &dwmHitResult
)) {
4821 *aRetValue
= dwmHitResult
;
4825 // The preference whether to use a different keyboard layout for each
4826 // window is cached, and updating it will not take effect until the
4827 // next restart. We read the preference here and not upon WM_ACTIVATE to make
4828 // sure that this behavior is consistent. Otherwise, if the user changed the
4829 // preference before having ever lowered the window, the preference would take
4830 // effect immediately.
4831 static const bool sSwitchKeyboardLayout
=
4832 Preferences::GetBool("intl.keyboard.per_window_layout", false);
4833 AppShutdownReason shutdownReason
= AppShutdownReason::Unknown
;
4835 // (Large blocks of code should be broken out into OnEvent handlers.)
4837 // WM_QUERYENDSESSION must be handled by all windows.
4838 // Otherwise Windows thinks the window can just be killed at will.
4839 case WM_QUERYENDSESSION
: {
4840 // Ask around if it's ok to quit.
4841 nsCOMPtr
<nsIObserverService
> obsServ
=
4842 mozilla::services::GetObserverService();
4843 nsCOMPtr
<nsISupportsPRBool
> cancelQuitWrapper
=
4844 do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID
);
4845 cancelQuitWrapper
->SetData(false);
4847 const char16_t
* quitType
= GetQuitType();
4848 obsServ
->NotifyObservers(cancelQuitWrapper
, "quit-application-requested",
4851 bool shouldCancelQuit
;
4852 cancelQuitWrapper
->GetData(&shouldCancelQuit
);
4853 *aRetValue
= !shouldCancelQuit
;
4857 case MOZ_WM_STARTA11Y
:
4858 #if defined(ACCESSIBILITY)
4859 Unused
<< GetAccessible();
4866 case WM_ENDSESSION
: {
4867 // For WM_ENDSESSION, wParam indicates whether we need to shutdown
4868 // (TRUE) or not (FALSE).
4873 // According to WM_ENDSESSION lParam documentation:
4874 // 0 -> OS shutdown or restart (no way to distinguish)
4875 // ENDSESSION_LOGOFF -> User is logging off
4876 // ENDSESSION_CLOSEAPP -> Application must shutdown
4877 // ENDSESSION_CRITICAL -> Application is forced to shutdown
4878 // The difference of the last two is not very clear.
4880 shutdownReason
= AppShutdownReason::OSShutdown
;
4881 } else if (lParam
& ENDSESSION_LOGOFF
) {
4882 shutdownReason
= AppShutdownReason::OSSessionEnd
;
4883 } else if (lParam
& (ENDSESSION_CLOSEAPP
| ENDSESSION_CRITICAL
)) {
4884 shutdownReason
= AppShutdownReason::OSForceClose
;
4886 MOZ_DIAGNOSTIC_ASSERT(false,
4887 "Received WM_ENDSESSION with unknown flags.");
4888 shutdownReason
= AppShutdownReason::OSForceClose
;
4892 case MOZ_WM_APP_QUIT
: {
4893 if (shutdownReason
== AppShutdownReason::Unknown
) {
4894 // TODO: We do not expect that these days anybody sends us
4895 // MOZ_WM_APP_QUIT, see bug 1827807.
4896 shutdownReason
= AppShutdownReason::WinUnexpectedMozQuit
;
4898 // Let's fake a shutdown sequence without actually closing windows etc.
4899 // to avoid Windows killing us in the middle. A proper shutdown would
4900 // require having a chance to pump some messages. Unfortunately
4901 // Windows won't let us do that. Bug 212316.
4902 nsCOMPtr
<nsIObserverService
> obsServ
=
4903 mozilla::services::GetObserverService();
4904 const char16_t
* syncShutdown
= u
"syncShutdown";
4905 const char16_t
* quitType
= GetQuitType();
4907 AppShutdown::Init(AppShutdownMode::Normal
, 0, shutdownReason
);
4909 obsServ
->NotifyObservers(nullptr, "quit-application-granted",
4911 obsServ
->NotifyObservers(nullptr, "quit-application-forced", nullptr);
4913 AppShutdown::OnShutdownConfirmed();
4915 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownConfirmed
,
4917 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownNetTeardown
,
4919 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTeardown
,
4921 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdown
, nullptr);
4922 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownQM
, nullptr);
4923 AppShutdown::AdvanceShutdownPhase(ShutdownPhase::AppShutdownTelemetry
,
4926 AppShutdown::DoImmediateExit();
4927 MOZ_ASSERT_UNREACHABLE("Our process was supposed to exit.");
4930 case WM_SYSCOLORCHANGE
:
4931 // No need to invalidate layout for system color changes, but we need to
4932 // invalidate style.
4933 NotifyThemeChanged(widget::ThemeChangeKind::Style
);
4936 case WM_THEMECHANGED
: {
4937 // Update non-client margin offsets
4938 UpdateNonClientMargins();
4939 nsUXThemeData::UpdateNativeThemeInfo();
4941 // We assume pretty much everything could've changed here.
4942 NotifyThemeChanged(widget::ThemeChangeKind::StyleAndLayout
);
4944 UpdateDarkModeToolbar();
4946 // Invalidate the window so that the repaint will
4947 // pick up the new theme.
4948 Invalidate(true, true, true);
4951 case WM_WTSSESSION_CHANGE
: {
4953 case WTS_CONSOLE_CONNECT
:
4954 case WTS_REMOTE_CONNECT
:
4955 case WTS_SESSION_UNLOCK
:
4956 // When a session becomes visible, we should invalidate.
4957 Invalidate(true, true, true);
4964 case WM_FONTCHANGE
: {
4965 // We only handle this message for the hidden window,
4966 // as we only need to update the (global) font list once
4967 // for any given change, not once per window!
4968 if (mWindowType
!= WindowType::Invisible
) {
4972 // update the global font list
4973 gfxPlatform::GetPlatform()->UpdateFontList();
4976 case WM_SETTINGCHANGE
: {
4977 if (wParam
== SPI_SETCLIENTAREAANIMATION
||
4978 wParam
== SPI_SETKEYBOARDDELAY
|| wParam
== SPI_SETMOUSEVANISH
) {
4979 // These need to update LookAndFeel cached values.
4980 // They affect reduced motion settings / caret blink count / show
4981 // pointer while typing, so no need to invalidate style / layout.
4982 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly
);
4985 if (wParam
== SPI_SETFONTSMOOTHING
||
4986 wParam
== SPI_SETFONTSMOOTHINGTYPE
) {
4987 gfxDWriteFont::UpdateSystemTextVars();
4990 if (wParam
== SPI_SETWORKAREA
) {
4991 // NB: We also refresh screens on WM_DISPLAYCHANGE but the rcWork
4992 // values are sometimes wrong at that point. This message then
4993 // arrives soon afterward, when we can get the right rcWork values.
4994 ScreenHelperWin::RefreshScreens();
4997 if (auto lParamString
= reinterpret_cast<const wchar_t*>(lParam
)) {
4998 if (!wcscmp(lParamString
, L
"ImmersiveColorSet")) {
4999 // This affects system colors (-moz-win-accentcolor), so gotta pass
5001 NotifyThemeChanged(widget::ThemeChangeKind::Style
);
5005 // UserInteractionMode, ConvertibleSlateMode, SystemDockMode may cause
5006 // @media(pointer) queries to change, which layout needs to know about
5008 // (WM_SETTINGCHANGE will be sent to all top-level windows, so we
5009 // only respond to the hidden top-level window to avoid hammering
5010 // layout with a bunch of NotifyThemeChanged() calls)
5012 if (mWindowType
== WindowType::Invisible
) {
5013 if (!wcscmp(lParamString
, L
"UserInteractionMode") ||
5014 !wcscmp(lParamString
, L
"ConvertibleSlateMode") ||
5015 !wcscmp(lParamString
, L
"SystemDockMode")) {
5016 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly
);
5017 WindowsUIUtils::UpdateInTabletMode();
5023 case WM_DEVICECHANGE
: {
5024 if (wParam
== DBT_DEVICEARRIVAL
|| wParam
== DBT_DEVICEREMOVECOMPLETE
) {
5025 DEV_BROADCAST_HDR
* hdr
= reinterpret_cast<DEV_BROADCAST_HDR
*>(lParam
);
5026 // Check dbch_devicetype explicitly since we will get other device types
5027 // (e.g. DBT_DEVTYP_VOLUME) for some reasons even if we specify
5028 // DBT_DEVTYP_DEVICEINTERFACE in the filter for
5029 // RegisterDeviceNotification.
5030 if (hdr
->dbch_devicetype
== DBT_DEVTYP_DEVICEINTERFACE
) {
5031 // This can only change media queries (any-hover/any-pointer).
5032 NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly
);
5037 case WM_NCCALCSIZE
: {
5038 // NOTE: the following block is mirrored in PreXULSkeletonUI.cpp, and
5039 // will need to be kept in sync.
5040 if (mCustomNonClient
) {
5041 // If `wParam` is `FALSE`, `lParam` points to a `RECT` that contains
5042 // the proposed window rectangle for our window. During our
5043 // processing of the `WM_NCCALCSIZE` message, we are expected to
5044 // modify the `RECT` that `lParam` points to, so that its value upon
5045 // our return is the new client area. We must return 0 if `wParam`
5048 // If `wParam` is `TRUE`, `lParam` points to a `NCCALCSIZE_PARAMS`
5049 // struct. This struct contains an array of 3 `RECT`s, the first of
5050 // which has the exact same meaning as the `RECT` that is pointed to
5051 // by `lParam` when `wParam` is `FALSE`. The remaining `RECT`s, in
5052 // conjunction with our return value, can
5053 // be used to specify portions of the source and destination window
5054 // rectangles that are valid and should be preserved. We opt not to
5055 // implement an elaborate client-area preservation technique, and
5056 // simply return 0, which means "preserve the entire old client area
5057 // and align it with the upper-left corner of our new client area".
5059 wParam
? &(reinterpret_cast<NCCALCSIZE_PARAMS
*>(lParam
))->rgrc
[0]
5060 : (reinterpret_cast<RECT
*>(lParam
));
5061 auto margin
= NonClientSizeMargin();
5062 clientRect
->top
+= margin
.top
;
5063 clientRect
->left
+= margin
.left
;
5064 clientRect
->right
-= margin
.right
;
5065 clientRect
->bottom
-= margin
.bottom
;
5066 // Make client rect's width and height more than 0 to
5067 // avoid problems of webrender and angle.
5068 clientRect
->right
= std::max(clientRect
->right
, clientRect
->left
+ 1);
5069 clientRect
->bottom
= std::max(clientRect
->bottom
, clientRect
->top
+ 1);
5077 case WM_NCHITTEST
: {
5078 if (mInputRegion
.mFullyTransparent
) {
5079 // Treat this window as transparent.
5080 *aRetValue
= HTTRANSPARENT
;
5085 if (mInputRegion
.mMargin
) {
5086 const LayoutDeviceIntPoint
screenPoint(GET_X_LPARAM(lParam
),
5087 GET_Y_LPARAM(lParam
));
5088 LayoutDeviceIntRect screenRect
= GetScreenBounds();
5089 screenRect
.Deflate(mInputRegion
.mMargin
);
5090 if (!screenRect
.Contains(screenPoint
)) {
5091 *aRetValue
= HTTRANSPARENT
;
5098 * If an nc client area margin has been moved, we are responsible
5099 * for calculating where the resize margins are and returning the
5100 * appropriate set of hit test constants. DwmDefWindowProc (above)
5101 * will handle hit testing on it's command buttons if we are on a
5102 * composited desktop.
5105 if (!mCustomNonClient
) {
5110 ClientMarginHitTestPoint(GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
5117 * WM_SETTEXT paints the titlebar area. Avoid this if we have a
5118 * custom titlebar we paint ourselves, or if we're the ones
5119 * sending the message with an updated title
5122 if (mSendingSetText
|| !mCustomNonClient
|| mNonClientMargins
.top
== -1)
5126 // From msdn, the way around this is to disable the visible state
5127 // temporarily. We need the text to be set but we don't want the
5128 // redraw to occur. However, we need to make sure that we don't
5129 // do this at the same time that a Present is happening.
5131 // To do this we take mPresentLock in nsWindow::PreRender and
5132 // if that lock is taken we wait before doing WM_SETTEXT
5133 if (mCompositorWidgetDelegate
) {
5134 mCompositorWidgetDelegate
->EnterPresentLock();
5136 DWORD style
= GetWindowLong(mWnd
, GWL_STYLE
);
5137 SetWindowLong(mWnd
, GWL_STYLE
, style
& ~WS_VISIBLE
);
5139 CallWindowProcW(GetPrevWindowProc(), mWnd
, msg
, wParam
, lParam
);
5140 SetWindowLong(mWnd
, GWL_STYLE
, style
);
5141 if (mCompositorWidgetDelegate
) {
5142 mCompositorWidgetDelegate
->LeavePresentLock();
5148 case WM_NCACTIVATE
: {
5150 * WM_NCACTIVATE paints nc areas. Avoid this and re-route painting
5151 * through WM_NCPAINT via InvalidateNonClientRegion.
5153 UpdateGetWindowInfoCaptionStatus(FALSE
!= wParam
);
5155 if (!mCustomNonClient
) {
5159 // There is a case that rendered result is not kept. Bug 1237617
5160 if (wParam
== TRUE
&& !gfxEnv::MOZ_DISABLE_FORCE_PRESENT()) {
5161 NS_DispatchToMainThread(NewRunnableMethod(
5162 "nsWindow::ForcePresent", this, &nsWindow::ForcePresent
));
5165 // let the dwm handle nc painting on glass
5166 // Never allow native painting if we are on fullscreen
5167 if (mFrameState
->GetSizeMode() != nsSizeMode_Fullscreen
) break;
5169 if (wParam
== TRUE
) {
5171 *aRetValue
= FALSE
; // ignored
5173 // invalidate to trigger a paint
5174 InvalidateNonClientRegion();
5178 *aRetValue
= TRUE
; // go ahead and deactive
5180 // invalidate to trigger a paint
5181 InvalidateNonClientRegion();
5188 * ClearType changes often don't send a WM_SETTINGCHANGE message. But they
5189 * do seem to always send a WM_NCPAINT message, so let's update on that.
5191 gfxDWriteFont::UpdateSystemTextVars();
5194 case WM_POWERBROADCAST
:
5196 case PBT_APMSUSPEND
:
5197 PostSleepWakeNotification(true);
5199 case PBT_APMRESUMEAUTOMATIC
:
5200 case PBT_APMRESUMECRITICAL
:
5201 case PBT_APMRESUMESUSPEND
:
5202 PostSleepWakeNotification(false);
5207 case WM_CLOSE
: // close request
5208 if (mWidgetListener
) mWidgetListener
->RequestWindowClose(this);
5209 result
= true; // abort window closure
5214 DestroyLayerManager();
5220 *aRetValue
= (int)OnPaint(0);
5225 result
= OnHotKey(wParam
, lParam
);
5230 MSG nativeMsg
= WinUtils::InitMSG(msg
, wParam
, lParam
, mWnd
);
5231 result
= ProcessCharMessage(nativeMsg
, nullptr);
5232 DispatchPendingEvents();
5237 MSG nativeMsg
= WinUtils::InitMSG(msg
, wParam
, lParam
, mWnd
);
5238 nativeMsg
.time
= ::GetMessageTime();
5239 result
= ProcessKeyUpMessage(nativeMsg
, nullptr);
5240 DispatchPendingEvents();
5245 MSG nativeMsg
= WinUtils::InitMSG(msg
, wParam
, lParam
, mWnd
);
5246 result
= ProcessKeyDownMessage(nativeMsg
, nullptr);
5247 DispatchPendingEvents();
5250 // Say we've dealt with erasing the background. (This is actually handled in
5251 // WM_PAINT, where necessary.)
5252 case WM_ERASEBKGND
: {
5257 case WM_MOUSEMOVE
: {
5258 LPARAM lParamScreen
= lParamToScreen(lParam
);
5259 mSimulatedClientArea
= IsSimulatedClientArea(GET_X_LPARAM(lParamScreen
),
5260 GET_Y_LPARAM(lParamScreen
));
5262 if (!mMousePresent
&& !sIsInMouseCapture
) {
5263 // First MOUSEMOVE over the client area. Ask for MOUSELEAVE
5264 TRACKMOUSEEVENT mTrack
;
5265 mTrack
.cbSize
= sizeof(TRACKMOUSEEVENT
);
5266 mTrack
.dwFlags
= TME_LEAVE
;
5267 mTrack
.dwHoverTime
= 0;
5268 mTrack
.hwndTrack
= mWnd
;
5269 TrackMouseEvent(&mTrack
);
5271 mMousePresent
= true;
5273 // Suppress dispatch of pending events
5274 // when mouse moves are generated by widget
5275 // creation instead of user input.
5277 mp
.x
= GET_X_LPARAM(lParamScreen
);
5278 mp
.y
= GET_Y_LPARAM(lParamScreen
);
5279 bool userMovedMouse
= false;
5280 if ((sLastMouseMovePoint
.x
!= mp
.x
) || (sLastMouseMovePoint
.y
!= mp
.y
)) {
5281 userMovedMouse
= true;
5284 if (userMovedMouse
) {
5285 result
= DispatchMouseEvent(
5286 eMouseMove
, wParam
, lParam
, false, MouseButton::ePrimary
,
5287 MOUSE_INPUT_SOURCE(),
5288 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5289 DispatchPendingEvents();
5293 case WM_NCMOUSEMOVE
: {
5294 LPARAM lParamClient
= lParamToClient(lParam
);
5295 if (IsSimulatedClientArea(GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
))) {
5296 if (!sIsInMouseCapture
) {
5297 TRACKMOUSEEVENT mTrack
;
5298 mTrack
.cbSize
= sizeof(TRACKMOUSEEVENT
);
5299 mTrack
.dwFlags
= TME_LEAVE
| TME_NONCLIENT
;
5300 mTrack
.dwHoverTime
= 0;
5301 mTrack
.hwndTrack
= mWnd
;
5302 TrackMouseEvent(&mTrack
);
5304 // If we noticed the mouse moving in our draggable region, forward the
5305 // message as a normal WM_MOUSEMOVE.
5306 SendMessage(mWnd
, WM_MOUSEMOVE
, 0, lParamClient
);
5308 // We've transitioned from a draggable area to somewhere else within
5309 // the non-client area - perhaps one of the edges of the window for
5311 mSimulatedClientArea
= false;
5314 if (mMousePresent
&& !sIsInMouseCapture
&& !mSimulatedClientArea
) {
5315 SendMessage(mWnd
, WM_MOUSELEAVE
, 0, 0);
5319 case WM_LBUTTONDOWN
: {
5321 DispatchMouseEvent(eMouseDown
, wParam
, lParam
, false,
5322 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE(),
5323 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5324 DispatchPendingEvents();
5327 case WM_LBUTTONUP
: {
5329 DispatchMouseEvent(eMouseUp
, wParam
, lParam
, false,
5330 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE(),
5331 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5332 DispatchPendingEvents();
5335 case WM_NCMOUSELEAVE
: {
5336 mSimulatedClientArea
= false;
5338 if (EventIsInsideWindow(this)) {
5339 // If we're handling WM_NCMOUSELEAVE and the mouse is still over the
5340 // window, then by process of elimination, the mouse has moved from the
5341 // non-client to client area, so no need to fall-through to the
5342 // WM_MOUSELEAVE handler. We also need to re-register for the
5343 // WM_MOUSELEAVE message, since according to the documentation at [1],
5344 // all tracking requested via TrackMouseEvent is cleared once
5345 // WM_NCMOUSELEAVE or WM_MOUSELEAVE fires.
5347 // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-trackmouseevent
5348 TRACKMOUSEEVENT mTrack
;
5349 mTrack
.cbSize
= sizeof(TRACKMOUSEEVENT
);
5350 mTrack
.dwFlags
= TME_LEAVE
;
5351 mTrack
.dwHoverTime
= 0;
5352 mTrack
.hwndTrack
= mWnd
;
5353 TrackMouseEvent(&mTrack
);
5356 // We've transitioned from non-client to outside of the window, so
5357 // fall-through to the WM_MOUSELEAVE handler.
5360 case WM_MOUSELEAVE
: {
5361 if (!mMousePresent
) break;
5362 if (mSimulatedClientArea
) break;
5363 mMousePresent
= false;
5365 // Check if the mouse is over the fullscreen transition window, if so
5366 // clear sLastMouseMovePoint. This way the WM_MOUSEMOVE we get after the
5367 // transition window disappears will not be ignored, even if the mouse
5369 if (mTransitionWnd
&& WindowAtMouse() == mTransitionWnd
) {
5370 sLastMouseMovePoint
= {0};
5373 // We need to check mouse button states and put them in for
5375 WPARAM mouseState
= (GetKeyState(VK_LBUTTON
) ? MK_LBUTTON
: 0) |
5376 (GetKeyState(VK_MBUTTON
) ? MK_MBUTTON
: 0) |
5377 (GetKeyState(VK_RBUTTON
) ? MK_RBUTTON
: 0);
5378 // Synthesize an event position because we don't get one from
5380 LPARAM pos
= lParamToClient(::GetMessagePos());
5381 DispatchMouseEvent(eMouseExitFromWidget
, mouseState
, pos
, false,
5382 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE());
5385 case WM_CONTEXTMENU
: {
5386 // If the context menu is brought up by a touch long-press, then
5387 // the APZ code is responsible for dealing with this, so we don't
5388 // need to do anything.
5390 MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_TOUCH
) {
5391 MOZ_ASSERT(mAPZC
); // since mTouchWindow is true, APZ must be enabled
5396 // If this WM_CONTEXTMENU is triggered by a mouse's secondary button up
5397 // event in overscroll gutter, we shouldn't open context menu.
5398 if (MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_MOUSE
&&
5399 mNeedsToPreventContextMenu
) {
5404 // if the context menu is brought up from the keyboard, |lParam|
5407 bool contextMenukey
= false;
5409 contextMenukey
= true;
5410 pos
= lParamToClient(GetMessagePos());
5412 pos
= lParamToClient(lParam
);
5415 result
= DispatchMouseEvent(
5416 eContextMenu
, wParam
, pos
, contextMenukey
,
5417 contextMenukey
? MouseButton::ePrimary
: MouseButton::eSecondary
,
5418 MOUSE_INPUT_SOURCE());
5419 if (lParam
!= -1 && !result
&& mCustomNonClient
&&
5420 mDraggableRegion
.Contains(GET_X_LPARAM(pos
), GET_Y_LPARAM(pos
))) {
5421 // Blank area hit, throw up the system menu.
5422 DisplaySystemMenu(mWnd
, mFrameState
->GetSizeMode(), mIsRTL
,
5423 GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
));
5428 case WM_POINTERLEAVE
:
5429 case WM_POINTERDOWN
:
5431 case WM_POINTERUPDATE
:
5432 result
= OnPointerEvents(msg
, wParam
, lParam
);
5434 DispatchPendingEvents();
5438 case DM_POINTERHITTEST
:
5440 UINT contactId
= GET_POINTERID_WPARAM(wParam
);
5441 POINTER_INPUT_TYPE pointerType
;
5442 if (mPointerEvents
.GetPointerType(contactId
, &pointerType
) &&
5443 pointerType
== PT_TOUCHPAD
) {
5444 mDmOwner
->SetContact(contactId
);
5449 case WM_LBUTTONDBLCLK
:
5450 result
= DispatchMouseEvent(eMouseDoubleClick
, wParam
, lParam
, false,
5451 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE());
5452 DispatchPendingEvents();
5455 case WM_MBUTTONDOWN
:
5456 result
= DispatchMouseEvent(eMouseDown
, wParam
, lParam
, false,
5457 MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5458 DispatchPendingEvents();
5462 result
= DispatchMouseEvent(eMouseUp
, wParam
, lParam
, false,
5463 MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5464 DispatchPendingEvents();
5467 case WM_MBUTTONDBLCLK
:
5468 result
= DispatchMouseEvent(eMouseDoubleClick
, wParam
, lParam
, false,
5469 MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5470 DispatchPendingEvents();
5473 case WM_NCMBUTTONDOWN
:
5474 result
= DispatchMouseEvent(eMouseDown
, 0, lParamToClient(lParam
), false,
5475 MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5476 DispatchPendingEvents();
5479 case WM_NCMBUTTONUP
:
5480 result
= DispatchMouseEvent(eMouseUp
, 0, lParamToClient(lParam
), false,
5481 MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5482 DispatchPendingEvents();
5485 case WM_NCMBUTTONDBLCLK
:
5487 DispatchMouseEvent(eMouseDoubleClick
, 0, lParamToClient(lParam
),
5488 false, MouseButton::eMiddle
, MOUSE_INPUT_SOURCE());
5489 DispatchPendingEvents();
5492 case WM_RBUTTONDOWN
:
5494 DispatchMouseEvent(eMouseDown
, wParam
, lParam
, false,
5495 MouseButton::eSecondary
, MOUSE_INPUT_SOURCE(),
5496 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5497 DispatchPendingEvents();
5502 DispatchMouseEvent(eMouseUp
, wParam
, lParam
, false,
5503 MouseButton::eSecondary
, MOUSE_INPUT_SOURCE(),
5504 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5505 DispatchPendingEvents();
5508 case WM_RBUTTONDBLCLK
:
5510 DispatchMouseEvent(eMouseDoubleClick
, wParam
, lParam
, false,
5511 MouseButton::eSecondary
, MOUSE_INPUT_SOURCE());
5512 DispatchPendingEvents();
5515 case WM_NCRBUTTONDOWN
:
5517 DispatchMouseEvent(eMouseDown
, 0, lParamToClient(lParam
), false,
5518 MouseButton::eSecondary
, MOUSE_INPUT_SOURCE());
5519 DispatchPendingEvents();
5522 case WM_NCRBUTTONUP
:
5524 DispatchMouseEvent(eMouseUp
, 0, lParamToClient(lParam
), false,
5525 MouseButton::eSecondary
, MOUSE_INPUT_SOURCE());
5526 DispatchPendingEvents();
5529 case WM_NCRBUTTONDBLCLK
:
5530 result
= DispatchMouseEvent(eMouseDoubleClick
, 0, lParamToClient(lParam
),
5531 false, MouseButton::eSecondary
,
5532 MOUSE_INPUT_SOURCE());
5533 DispatchPendingEvents();
5536 // Windows doesn't provide to customize the behavior of 4th nor 5th button
5537 // of mouse. If 5-button mouse works with standard mouse deriver of
5538 // Windows, users cannot disable 4th button (browser back) nor 5th button
5539 // (browser forward). We should allow to do it with our prefs since we can
5540 // prevent Windows to generate WM_APPCOMMAND message if WM_XBUTTONUP
5541 // messages are not sent to DefWindowProc.
5542 case WM_XBUTTONDOWN
:
5544 case WM_NCXBUTTONDOWN
:
5545 case WM_NCXBUTTONUP
:
5547 switch (GET_XBUTTON_WPARAM(wParam
)) {
5549 result
= !Preferences::GetBool("mousebutton.4th.enabled", true);
5552 result
= !Preferences::GetBool("mousebutton.5th.enabled", true);
5560 if (mAspectRatio
> 0) {
5561 LPRECT rect
= (LPRECT
)lParam
;
5562 int32_t newWidth
, newHeight
;
5564 // The following conditions and switch statement borrow heavily from the
5565 // Chromium source code from
5566 // https://chromium.googlesource.com/chromium/src/+/456d6e533cfb4531995e0ef52c279d4b5aa8a352/ui/views/window/window_resize_utils.cc#45
5567 if (wParam
== WMSZ_LEFT
|| wParam
== WMSZ_RIGHT
||
5568 wParam
== WMSZ_TOPLEFT
|| wParam
== WMSZ_BOTTOMLEFT
) {
5569 newWidth
= rect
->right
- rect
->left
;
5570 newHeight
= newWidth
/ mAspectRatio
;
5571 if (newHeight
< mSizeConstraints
.mMinSize
.height
) {
5572 newHeight
= mSizeConstraints
.mMinSize
.height
;
5573 newWidth
= newHeight
* mAspectRatio
;
5574 } else if (newHeight
> mSizeConstraints
.mMaxSize
.height
) {
5575 newHeight
= mSizeConstraints
.mMaxSize
.height
;
5576 newWidth
= newHeight
* mAspectRatio
;
5579 newHeight
= rect
->bottom
- rect
->top
;
5580 newWidth
= newHeight
* mAspectRatio
;
5581 if (newWidth
< mSizeConstraints
.mMinSize
.width
) {
5582 newWidth
= mSizeConstraints
.mMinSize
.width
;
5583 newHeight
= newWidth
/ mAspectRatio
;
5584 } else if (newWidth
> mSizeConstraints
.mMaxSize
.width
) {
5585 newWidth
= mSizeConstraints
.mMaxSize
.width
;
5586 newHeight
= newWidth
/ mAspectRatio
;
5593 rect
->right
= newWidth
+ rect
->left
;
5594 rect
->bottom
= rect
->top
+ newHeight
;
5597 rect
->right
= newWidth
+ rect
->left
;
5598 rect
->top
= rect
->bottom
- newHeight
;
5602 rect
->left
= rect
->right
- newWidth
;
5603 rect
->top
= rect
->bottom
- newHeight
;
5606 rect
->right
= rect
->left
+ newWidth
;
5607 rect
->top
= rect
->bottom
- newHeight
;
5609 case WMSZ_BOTTOMLEFT
:
5610 rect
->left
= rect
->right
- newWidth
;
5611 rect
->bottom
= rect
->top
+ newHeight
;
5613 case WMSZ_BOTTOMRIGHT
:
5614 rect
->right
= rect
->left
+ newWidth
;
5615 rect
->bottom
= rect
->top
+ newHeight
;
5620 // When we get WM_ENTERSIZEMOVE we don't know yet if we're in a live
5621 // resize or move event. Instead we wait for first VM_SIZING message
5622 // within a ENTERSIZEMOVE to consider this a live resize event.
5623 if (mResizeState
== IN_SIZEMOVE
) {
5624 mResizeState
= RESIZING
;
5625 NotifyLiveResizeStarted();
5631 FinishLiveResizing(MOVING
);
5632 if (WinUtils::IsPerMonitorDPIAware()) {
5633 // Sometimes, we appear to miss a WM_DPICHANGED message while moving
5634 // a window around. Therefore, call ChangedDPI and ResetLayout here
5635 // if it appears that the window's scaling is not what we expect.
5636 // This causes the prescontext and appshell window management code to
5637 // check the appUnitsPerDevPixel value and current widget size, and
5638 // refresh them if necessary. If nothing has changed, these calls will
5639 // return without actually triggering any extra reflow or painting.
5640 if (WinUtils::LogToPhysFactor(mWnd
) != mDefaultScale
) {
5643 if (mWidgetListener
) {
5644 mWidgetListener
->UIResolutionChanged();
5650 case WM_ENTERSIZEMOVE
: {
5651 if (mResizeState
== NOT_RESIZING
) {
5652 mResizeState
= IN_SIZEMOVE
;
5657 case WM_EXITSIZEMOVE
: {
5658 FinishLiveResizing(NOT_RESIZING
);
5660 if (!sIsInMouseCapture
) {
5661 NotifySizeMoveDone();
5664 // Windows spins a separate hidden event loop when moving a window so we
5665 // don't hear mouse events during this time and WM_EXITSIZEMOVE is fired
5666 // when the hidden event loop exits. We set mDraggingWindowWithMouse to
5667 // true in WM_NCLBUTTONDOWN when we started moving the window with the
5668 // mouse so we know that if mDraggingWindowWithMouse is true, we can send
5669 // a mouse up event.
5670 if (mDraggingWindowWithMouse
) {
5671 mDraggingWindowWithMouse
= false;
5672 result
= DispatchMouseEvent(
5673 eMouseUp
, wParam
, lParam
, false, MouseButton::ePrimary
,
5674 MOUSE_INPUT_SOURCE(),
5675 mPointerEvents
.GetCachedPointerInfo(msg
, wParam
));
5681 case WM_DISPLAYCHANGE
: {
5682 ScreenHelperWin::RefreshScreens();
5683 if (mWidgetListener
) {
5684 mWidgetListener
->UIResolutionChanged();
5689 case WM_NCLBUTTONDBLCLK
:
5690 DispatchMouseEvent(eMouseDoubleClick
, 0, lParamToClient(lParam
), false,
5691 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE());
5692 result
= DispatchMouseEvent(eMouseUp
, 0, lParamToClient(lParam
), false,
5693 MouseButton::ePrimary
, MOUSE_INPUT_SOURCE());
5694 DispatchPendingEvents();
5697 case WM_NCLBUTTONDOWN
: {
5698 // Dispatch a custom event when this happens in the draggable region, so
5699 // that non-popup-based panels can react to it. This doesn't send an
5700 // actual mousedown event because that would break dragging or interfere
5701 // with other mousedown handling in the caption area.
5702 if (ClientMarginHitTestPoint(GET_X_LPARAM(lParam
),
5703 GET_Y_LPARAM(lParam
)) == HTCAPTION
) {
5704 DispatchCustomEvent(u
"draggableregionleftmousedown"_ns
);
5705 mDraggingWindowWithMouse
= true;
5708 if (IsWindowButton(wParam
) && mCustomNonClient
) {
5709 DispatchMouseEvent(eMouseDown
, wParamFromGlobalMouseState(),
5710 lParamToClient(lParam
), false, MouseButton::ePrimary
,
5711 MOUSE_INPUT_SOURCE(), nullptr, true);
5712 DispatchPendingEvents();
5718 case WM_APPCOMMAND
: {
5719 MSG nativeMsg
= WinUtils::InitMSG(msg
, wParam
, lParam
, mWnd
);
5720 result
= HandleAppCommandMsg(nativeMsg
, aRetValue
);
5724 // The WM_ACTIVATE event is fired when a window is raised or lowered,
5725 // and the loword of wParam specifies which. But we don't want to tell
5726 // the focus system about this until the WM_SETFOCUS or WM_KILLFOCUS
5727 // events are fired. Instead, set either the sJustGotActivate or
5728 // gJustGotDeactivate flags and activate/deactivate once the focus
5731 int32_t fActive
= LOWORD(wParam
);
5732 if (mWidgetListener
) {
5733 if (WA_INACTIVE
== fActive
) {
5734 // when minimizing a window, the deactivation and focus events will
5735 // be fired in the reverse order. Instead, just deactivate right away.
5736 // This can also happen when a modal system dialog is opened, so check
5737 // if the last window to receive the WM_KILLFOCUS message was this one
5738 // or a child of this one.
5739 if (HIWORD(wParam
) ||
5740 (mLastKillFocusWindow
&&
5741 (GetTopLevelForFocus(mLastKillFocusWindow
) == mWnd
))) {
5742 DispatchFocusToTopLevelWindow(false);
5744 sJustGotDeactivate
= true;
5746 if (mIsTopWidgetWindow
) {
5747 mLastKeyboardLayout
= KeyboardLayout::GetLayout();
5752 sJustGotActivate
= true;
5753 WidgetMouseEvent
event(true, eMouseActivate
, this,
5754 WidgetMouseEvent::eReal
);
5756 ModifierKeyState modifierKeyState
;
5757 modifierKeyState
.InitInputEvent(event
);
5758 DispatchInputEvent(&event
);
5759 if (sSwitchKeyboardLayout
&& mLastKeyboardLayout
)
5760 ActivateKeyboardLayout(mLastKeyboardLayout
, 0);
5762 #ifdef ACCESSIBILITY
5763 a11y::LazyInstantiator::ResetUiaDetectionCache();
5769 case WM_ACTIVATEAPP
: {
5770 // Bug 1851991: Sometimes this can be called before gfxPlatform::Init
5771 // when a window is created very early. In that case we just forego
5772 // setting this and accept the GPU process might briefly run at a lower
5774 if (GPUProcessManager::Get()) {
5775 GPUProcessManager::Get()->SetAppInForeground(wParam
);
5779 case WM_MOUSEACTIVATE
:
5780 // A popup with a parent owner should not be activated when clicked but
5781 // should still allow the mouse event to be fired, so the return value
5782 // is set to MA_NOACTIVATE. But if the owner isn't the frontmost window,
5783 // just use default processing so that the window is activated.
5784 if (IsPopup() && IsOwnerForegroundWindow()) {
5785 *aRetValue
= MA_NOACTIVATE
;
5790 case WM_WINDOWPOSCHANGING
: {
5791 LPWINDOWPOS info
= (LPWINDOWPOS
)lParam
;
5792 OnWindowPosChanging(info
);
5796 // Workaround for race condition in explorer.exe.
5797 case MOZ_WM_FULLSCREEN_STATE_UPDATE
: {
5798 TaskbarConcealer::OnAsyncStateUpdateRequest(mWnd
);
5802 case WM_GETMINMAXINFO
: {
5803 MINMAXINFO
* mmi
= (MINMAXINFO
*)lParam
;
5804 // Set the constraints. The minimum size should also be constrained to the
5805 // default window maximum size so that it fits on screen.
5806 mmi
->ptMinTrackSize
.x
=
5807 std::min((int32_t)mmi
->ptMaxTrackSize
.x
,
5808 std::max((int32_t)mmi
->ptMinTrackSize
.x
,
5809 mSizeConstraints
.mMinSize
.width
));
5810 mmi
->ptMinTrackSize
.y
=
5811 std::min((int32_t)mmi
->ptMaxTrackSize
.y
,
5812 std::max((int32_t)mmi
->ptMinTrackSize
.y
,
5813 mSizeConstraints
.mMinSize
.height
));
5814 mmi
->ptMaxTrackSize
.x
= std::min((int32_t)mmi
->ptMaxTrackSize
.x
,
5815 mSizeConstraints
.mMaxSize
.width
);
5816 mmi
->ptMaxTrackSize
.y
= std::min((int32_t)mmi
->ptMaxTrackSize
.y
,
5817 mSizeConstraints
.mMaxSize
.height
);
5821 WndProcUrgentInvocation::Marker _marker
;
5823 // If previous focused window isn't ours, it must have received the
5824 // redirected message. So, we should forget it.
5825 if (!WinUtils::IsOurProcessWindow(HWND(wParam
))) {
5826 RedirectedKeyDownMessageManager::Forget();
5828 if (sJustGotActivate
) {
5829 DispatchFocusToTopLevelWindow(true);
5831 TaskbarConcealer::OnFocusAcquired(this);
5835 if (sJustGotDeactivate
) {
5836 DispatchFocusToTopLevelWindow(false);
5838 mLastKillFocusWindow
= mWnd
;
5842 case WM_WINDOWPOSCHANGED
: {
5843 WINDOWPOS
* wp
= (LPWINDOWPOS
)lParam
;
5844 OnWindowPosChanged(wp
);
5845 TaskbarConcealer::OnWindowPosChanged(this);
5849 case WM_INPUTLANGCHANGEREQUEST
:
5854 case WM_INPUTLANGCHANGE
:
5855 KeyboardLayout::GetInstance()->OnLayoutChange(
5856 reinterpret_cast<HKL
>(lParam
));
5857 nsBidiKeyboard::OnLayoutChange();
5858 result
= false; // always pass to child window
5861 case WM_DESTROYCLIPBOARD
: {
5862 nsIClipboard
* clipboard
;
5863 nsresult rv
= CallGetService(kCClipboardCID
, &clipboard
);
5864 if (NS_SUCCEEDED(rv
)) {
5865 clipboard
->EmptyClipboard(nsIClipboard::kGlobalClipboard
);
5866 NS_RELEASE(clipboard
);
5870 #ifdef ACCESSIBILITY
5871 case WM_GETOBJECT
: {
5873 // Do explicit casting to make it working on 64bit systems (see bug 649236
5875 int32_t objId
= static_cast<DWORD
>(lParam
);
5876 if (objId
== OBJID_CLIENT
) { // oleacc.dll will be loaded dynamically
5877 RefPtr
<IAccessible
> root(
5878 a11y::LazyInstantiator::GetRootAccessible(mWnd
));
5880 *aRetValue
= LresultFromObject(IID_IAccessible
, wParam
, root
);
5881 a11y::LazyInstantiator::EnableBlindAggregation(mWnd
);
5888 case WM_SYSCOMMAND
: {
5889 WPARAM
const filteredWParam
= (wParam
& 0xFFF0);
5891 // SC_CLOSE may trigger a synchronous confirmation prompt. If we're in the
5892 // middle of something important, put off responding to it.
5893 if (filteredWParam
== SC_CLOSE
&& WndProcUrgentInvocation::IsActive()) {
5894 ::PostMessageW(mWnd
, msg
, wParam
, lParam
);
5899 if (mFrameState
->GetSizeMode() == nsSizeMode_Fullscreen
&&
5900 filteredWParam
== SC_RESTORE
&&
5901 GetCurrentShowCmd(mWnd
) != SW_SHOWMINIMIZED
) {
5902 mFrameState
->EnsureFullscreenMode(false);
5906 // Handle the system menu manually when we're in full screen mode
5907 // so we can set the appropriate options.
5908 if (filteredWParam
== SC_KEYMENU
&& lParam
== VK_SPACE
&&
5909 mFrameState
->GetSizeMode() == nsSizeMode_Fullscreen
) {
5910 DisplaySystemMenu(mWnd
, mFrameState
->GetSizeMode(), mIsRTL
,
5911 MOZ_SYSCONTEXT_X_POS
, MOZ_SYSCONTEXT_Y_POS
);
5916 case WM_DPICHANGED
: {
5917 LPRECT rect
= (LPRECT
)lParam
;
5918 OnDPIChanged(rect
->left
, rect
->top
, rect
->right
- rect
->left
,
5919 rect
->bottom
- rect
->top
);
5923 /* Gesture support events */
5924 case WM_TABLET_QUERYSYSTEMGESTURESTATUS
:
5925 // According to MS samples, this must be handled to enable
5926 // rotational support in multi-touch drivers.
5928 *aRetValue
= TABLET_ROTATE_GESTURE_ENABLE
;
5932 result
= OnTouch(wParam
, lParam
);
5939 result
= OnGesture(wParam
, lParam
);
5942 case WM_GESTURENOTIFY
: {
5943 if (mWindowType
!= WindowType::Invisible
) {
5944 // A GestureNotify event is dispatched to decide which single-finger
5945 // panning direction should be active (including none) and if pan
5946 // feedback should be displayed. Java and plugin windows can make their
5949 GESTURENOTIFYSTRUCT
* gestureinfo
= (GESTURENOTIFYSTRUCT
*)lParam
;
5950 nsPointWin touchPoint
;
5951 touchPoint
= gestureinfo
->ptsLocation
;
5952 touchPoint
.ScreenToClient(mWnd
);
5953 WidgetGestureNotifyEvent
gestureNotifyEvent(true, eGestureNotify
, this);
5954 gestureNotifyEvent
.mRefPoint
=
5955 LayoutDeviceIntPoint::FromUnknownPoint(touchPoint
);
5956 nsEventStatus status
;
5957 DispatchEvent(&gestureNotifyEvent
, status
);
5958 mDisplayPanFeedback
= gestureNotifyEvent
.mDisplayPanFeedback
;
5960 mGesture
.SetWinGestureSupport(mWnd
, gestureNotifyEvent
.mPanDirection
);
5962 result
= false; // should always bubble to DefWindowProc
5966 WidgetContentCommandEvent
command(true, eContentCommandDelete
, this);
5967 DispatchWindowEvent(command
);
5972 WidgetContentCommandEvent
command(true, eContentCommandCut
, this);
5973 DispatchWindowEvent(command
);
5978 WidgetContentCommandEvent
command(true, eContentCommandCopy
, this);
5979 DispatchWindowEvent(command
);
5984 WidgetContentCommandEvent
command(true, eContentCommandPaste
, this);
5985 DispatchWindowEvent(command
);
5990 WidgetContentCommandEvent
command(true, eContentCommandUndo
, this);
5991 DispatchWindowEvent(command
);
5992 *aRetValue
= (LRESULT
)(command
.mSucceeded
&& command
.mIsEnabled
);
5997 WidgetContentCommandEvent
command(true, eContentCommandRedo
, this);
5998 DispatchWindowEvent(command
);
5999 *aRetValue
= (LRESULT
)(command
.mSucceeded
&& command
.mIsEnabled
);
6004 // Support EM_CANPASTE message only when wParam isn't specified or
6005 // is plain text format.
6006 if (wParam
== 0 || wParam
== CF_TEXT
|| wParam
== CF_UNICODETEXT
) {
6007 WidgetContentCommandEvent
command(true, eContentCommandPaste
, this,
6009 DispatchWindowEvent(command
);
6010 *aRetValue
= (LRESULT
)(command
.mSucceeded
&& command
.mIsEnabled
);
6016 WidgetContentCommandEvent
command(true, eContentCommandUndo
, this, true);
6017 DispatchWindowEvent(command
);
6018 *aRetValue
= (LRESULT
)(command
.mSucceeded
&& command
.mIsEnabled
);
6023 WidgetContentCommandEvent
command(true, eContentCommandRedo
, this, true);
6024 DispatchWindowEvent(command
);
6025 *aRetValue
= (LRESULT
)(command
.mSucceeded
&& command
.mIsEnabled
);
6029 case MOZ_WM_SKEWFIX
: {
6030 TimeStamp skewStamp
;
6031 if (CurrentWindowsTimeGetter::GetAndClearBackwardsSkewStamp(wParam
,
6033 TimeConverter().CompensateForBackwardsSkew(::GetMessageTime(),
6039 if (msg
== nsAppShell::GetTaskbarButtonCreatedMessage()) {
6040 SetHasTaskbarIconBeenCreated();
6045 //*aRetValue = result;
6049 // Events which caused mWnd destruction and aren't consumed
6050 // will crash during the Windows default processing.
6055 void nsWindow::FinishLiveResizing(ResizeState aNewState
) {
6056 if (mResizeState
== RESIZING
) {
6057 NotifyLiveResizeStopped();
6059 mResizeState
= aNewState
;
6063 /**************************************************************
6065 * SECTION: Event processing helpers
6067 * Special processing for certain event types and
6068 * synthesized events.
6070 **************************************************************/
6072 LayoutDeviceIntMargin
nsWindow::NonClientSizeMargin(
6073 const LayoutDeviceIntMargin
& aNonClientOffset
) const {
6074 return LayoutDeviceIntMargin(mCaptionHeight
- aNonClientOffset
.top
,
6075 mHorResizeMargin
- aNonClientOffset
.right
,
6076 mVertResizeMargin
- aNonClientOffset
.bottom
,
6077 mHorResizeMargin
- aNonClientOffset
.left
);
6080 int32_t nsWindow::ClientMarginHitTestPoint(int32_t aX
, int32_t aY
) {
6081 const nsSizeMode sizeMode
= mFrameState
->GetSizeMode();
6082 if (sizeMode
== nsSizeMode_Minimized
|| sizeMode
== nsSizeMode_Fullscreen
) {
6086 // Calculations are done in screen coords
6087 const LayoutDeviceIntRect winRect
= GetScreenBounds();
6088 const LayoutDeviceIntPoint
point(aX
, aY
);
6090 // hit return constants:
6091 // HTBORDER - non-resizable border
6092 // HTBOTTOM, HTLEFT, HTRIGHT, HTTOP - resizable border
6093 // HTBOTTOMLEFT, HTBOTTOMRIGHT - resizable corner
6094 // HTTOPLEFT, HTTOPRIGHT - resizable corner
6095 // HTCAPTION - general title bar area
6096 // HTCLIENT - area considered the client
6097 // HTCLOSE - hovering over the close button
6098 // HTMAXBUTTON - maximize button
6099 // HTMINBUTTON - minimize button
6101 int32_t testResult
= HTCLIENT
;
6102 const bool isResizable
=
6103 sizeMode
!= nsSizeMode_Maximized
&&
6105 (BorderStyle::All
| BorderStyle::ResizeH
| BorderStyle::Default
));
6107 LayoutDeviceIntMargin nonClientSizeMargin
= NonClientSizeMargin();
6109 // Ensure being accessible to borders of window. Even if contents are in
6110 // this area, the area must behave as border.
6111 nonClientSizeMargin
.EnsureAtLeast(
6112 LayoutDeviceIntMargin(kResizableBorderMinSize
, kResizableBorderMinSize
,
6113 kResizableBorderMinSize
, kResizableBorderMinSize
));
6115 LayoutDeviceIntRect clientRect
= winRect
;
6116 clientRect
.Deflate(nonClientSizeMargin
);
6118 const bool allowContentOverride
=
6119 sizeMode
== nsSizeMode_Maximized
|| clientRect
.Contains(point
);
6121 // The border size. If there is no content under mouse cursor, the border
6122 // size should be larger than the values in system settings. Otherwise,
6123 // contents under the mouse cursor should be able to override the behavior.
6124 // E.g., user must expect that Firefox button always opens the popup menu
6125 // even when the user clicks on the above edge of it.
6126 LayoutDeviceIntMargin borderSize
= nonClientSizeMargin
;
6127 borderSize
.EnsureAtLeast(
6128 LayoutDeviceIntMargin(mVertResizeMargin
, mHorResizeMargin
,
6129 mVertResizeMargin
, mHorResizeMargin
));
6132 bool bottom
= false;
6136 if (point
.y
>= winRect
.y
&& point
.y
< winRect
.y
+ borderSize
.top
) {
6138 } else if (point
.y
<= winRect
.YMost() &&
6139 point
.y
> winRect
.YMost() - borderSize
.bottom
) {
6143 // (the 2x case here doubles the resize area for corners)
6144 int multiplier
= (top
|| bottom
) ? 2 : 1;
6145 if (point
.x
>= winRect
.x
&&
6146 point
.x
< winRect
.x
+ (multiplier
* borderSize
.left
)) {
6148 } else if (point
.x
<= winRect
.XMost() &&
6149 point
.x
> winRect
.XMost() - (multiplier
* borderSize
.right
)) {
6153 bool inResizeRegion
= false;
6158 testResult
= HTTOPLEFT
;
6160 testResult
= HTTOPRIGHT
;
6162 } else if (bottom
) {
6163 testResult
= HTBOTTOM
;
6165 testResult
= HTBOTTOMLEFT
;
6167 testResult
= HTBOTTOMRIGHT
;
6171 testResult
= HTLEFT
;
6174 testResult
= HTRIGHT
;
6177 inResizeRegion
= (testResult
!= HTCLIENT
);
6180 testResult
= HTCAPTION
;
6181 } else if (bottom
|| left
|| right
) {
6182 testResult
= HTBORDER
;
6186 if (!sIsInMouseCapture
&& allowContentOverride
) {
6188 POINT pt
= {aX
, aY
};
6189 ::ScreenToClient(mWnd
, &pt
);
6191 if (pt
.x
== mCachedHitTestPoint
.x
.value
&&
6192 pt
.y
== mCachedHitTestPoint
.y
.value
&&
6193 TimeStamp::Now() - mCachedHitTestTime
<
6194 TimeDuration::FromMilliseconds(HITTEST_CACHE_LIFETIME_MS
)) {
6195 return mCachedHitTestResult
;
6198 mCachedHitTestPoint
= {pt
.x
, pt
.y
};
6199 mCachedHitTestTime
= TimeStamp::Now();
6202 auto pt
= mCachedHitTestPoint
;
6204 if (mWindowBtnRect
[WindowButtonType::Minimize
].Contains(pt
)) {
6205 testResult
= HTMINBUTTON
;
6206 } else if (mWindowBtnRect
[WindowButtonType::Maximize
].Contains(pt
)) {
6207 testResult
= HTMAXBUTTON
;
6208 } else if (mWindowBtnRect
[WindowButtonType::Close
].Contains(pt
)) {
6209 testResult
= HTCLOSE
;
6210 } else if (!inResizeRegion
) {
6211 // If we're in the resize region, avoid overriding that with either a
6212 // drag or a client result; resize takes priority over either (but not
6213 // over the window controls, which is why we check this after those).
6214 if (mDraggableRegion
.Contains(pt
)) {
6215 testResult
= HTCAPTION
;
6217 testResult
= HTCLIENT
;
6221 mCachedHitTestResult
= testResult
;
6227 bool nsWindow::IsSimulatedClientArea(int32_t screenX
, int32_t screenY
) {
6228 int32_t testResult
= ClientMarginHitTestPoint(screenX
, screenY
);
6229 return testResult
== HTCAPTION
|| IsWindowButton(testResult
);
6232 bool nsWindow::IsWindowButton(int32_t hitTestResult
) {
6233 return hitTestResult
== HTMINBUTTON
|| hitTestResult
== HTMAXBUTTON
||
6234 hitTestResult
== HTCLOSE
;
6237 TimeStamp
nsWindow::GetMessageTimeStamp(LONG aEventTime
) const {
6238 CurrentWindowsTimeGetter
getCurrentTime(mWnd
);
6239 return TimeConverter().GetTimeStampFromSystemTime(aEventTime
, getCurrentTime
);
6242 void nsWindow::PostSleepWakeNotification(const bool aIsSleepMode
) {
6243 // Retain the previous mode that was notified to observers
6244 static bool sWasSleepMode
= false;
6246 // Only notify observers if mode changed
6247 if (aIsSleepMode
== sWasSleepMode
) return;
6249 sWasSleepMode
= aIsSleepMode
;
6251 nsCOMPtr
<nsIObserverService
> observerService
=
6252 mozilla::services::GetObserverService();
6253 if (observerService
)
6254 observerService
->NotifyObservers(nullptr,
6256 ? NS_WIDGET_SLEEP_OBSERVER_TOPIC
6257 : NS_WIDGET_WAKE_OBSERVER_TOPIC
,
6261 LRESULT
nsWindow::ProcessCharMessage(const MSG
& aMsg
, bool* aEventDispatched
) {
6262 if (IMEHandler::IsComposingOn(this)) {
6263 IMEHandler::NotifyIME(this, REQUEST_TO_COMMIT_COMPOSITION
);
6265 // These must be checked here too as a lone WM_CHAR could be received
6266 // if a child window didn't handle it (for example Alt+Space in a content
6268 ModifierKeyState modKeyState
;
6269 NativeKey
nativeKey(this, aMsg
, modKeyState
);
6270 return static_cast<LRESULT
>(nativeKey
.HandleCharMessage(aEventDispatched
));
6273 LRESULT
nsWindow::ProcessKeyUpMessage(const MSG
& aMsg
, bool* aEventDispatched
) {
6274 ModifierKeyState modKeyState
;
6275 NativeKey
nativeKey(this, aMsg
, modKeyState
);
6276 bool result
= nativeKey
.HandleKeyUpMessage(aEventDispatched
);
6277 if (aMsg
.wParam
== VK_F10
) {
6278 // Bug 1382199: Windows default behavior will trigger the System menu bar
6279 // when F10 is released. Among other things, this causes the System menu bar
6280 // to appear when a web page overrides the contextmenu event. We *never*
6281 // want this default behavior, so eat this key (never pass it to Windows).
6287 LRESULT
nsWindow::ProcessKeyDownMessage(const MSG
& aMsg
,
6288 bool* aEventDispatched
) {
6289 // If this method doesn't call NativeKey::HandleKeyDownMessage(), this method
6290 // must clean up the redirected message information itself. For more
6291 // information, see above comment of
6292 // RedirectedKeyDownMessageManager::AutoFlusher class definition in
6293 // KeyboardLayout.h.
6294 RedirectedKeyDownMessageManager::AutoFlusher
redirectedMsgFlusher(this, aMsg
);
6296 ModifierKeyState modKeyState
;
6298 NativeKey
nativeKey(this, aMsg
, modKeyState
);
6300 static_cast<LRESULT
>(nativeKey
.HandleKeyDownMessage(aEventDispatched
));
6301 // HandleKeyDownMessage cleaned up the redirected message information
6302 // itself, so, we should do nothing.
6303 redirectedMsgFlusher
.Cancel();
6305 if (aMsg
.wParam
== VK_MENU
||
6306 (aMsg
.wParam
== VK_F10
&& !modKeyState
.IsShift())) {
6307 // We need to let Windows handle this keypress,
6308 // by returning false, if there's a native menu
6309 // bar somewhere in our containing window hierarchy.
6310 // Otherwise we handle the keypress and don't pass
6311 // it on to Windows, by returning true.
6312 bool hasNativeMenu
= false;
6315 if (::GetMenu(hWnd
)) {
6316 hasNativeMenu
= true;
6319 hWnd
= ::GetParent(hWnd
);
6321 result
= !hasNativeMenu
;
6327 nsresult
nsWindow::SynthesizeNativeKeyEvent(
6328 int32_t aNativeKeyboardLayout
, int32_t aNativeKeyCode
,
6329 uint32_t aModifierFlags
, const nsAString
& aCharacters
,
6330 const nsAString
& aUnmodifiedCharacters
, nsIObserver
* aObserver
) {
6331 AutoObserverNotifier
notifier(aObserver
, "keyevent");
6333 KeyboardLayout
* keyboardLayout
= KeyboardLayout::GetInstance();
6334 return keyboardLayout
->SynthesizeNativeKeyEvent(
6335 this, aNativeKeyboardLayout
, aNativeKeyCode
, aModifierFlags
, aCharacters
,
6336 aUnmodifiedCharacters
);
6339 nsresult
nsWindow::SynthesizeNativeMouseEvent(
6340 LayoutDeviceIntPoint aPoint
, NativeMouseMessage aNativeMessage
,
6341 MouseButton aButton
, nsIWidget::Modifiers aModifierFlags
,
6342 nsIObserver
* aObserver
) {
6343 AutoObserverNotifier
notifier(aObserver
, "mouseevent");
6346 memset(&input
, 0, sizeof(input
));
6348 // TODO (bug 1693240):
6349 // Now, we synthesize native mouse events asynchronously since we want to
6350 // synthesize the event on the front window at the point. However, Windows
6351 // does not provide a way to set modifier only while a mouse message is
6352 // being handled, and MOUSEEVENTF_MOVE may be coalesced by Windows. So, we
6353 // need a trick for handling it.
6355 switch (aNativeMessage
) {
6356 case NativeMouseMessage::Move
:
6357 input
.mi
.dwFlags
= MOUSEEVENTF_MOVE
;
6358 // Reset sLastMouseMovePoint so that even if we're moving the mouse
6359 // to the position it's already at, we still dispatch a mousemove
6360 // event, because the callers of this function expect that.
6361 sLastMouseMovePoint
= {0};
6363 case NativeMouseMessage::ButtonDown
:
6364 case NativeMouseMessage::ButtonUp
: {
6365 const bool isDown
= aNativeMessage
== NativeMouseMessage::ButtonDown
;
6367 case MouseButton::ePrimary
:
6368 input
.mi
.dwFlags
= isDown
? MOUSEEVENTF_LEFTDOWN
: MOUSEEVENTF_LEFTUP
;
6370 case MouseButton::eMiddle
:
6372 isDown
? MOUSEEVENTF_MIDDLEDOWN
: MOUSEEVENTF_MIDDLEUP
;
6374 case MouseButton::eSecondary
:
6376 isDown
? MOUSEEVENTF_RIGHTDOWN
: MOUSEEVENTF_RIGHTUP
;
6378 case MouseButton::eX1
:
6379 input
.mi
.dwFlags
= isDown
? MOUSEEVENTF_XDOWN
: MOUSEEVENTF_XUP
;
6380 input
.mi
.mouseData
= XBUTTON1
;
6382 case MouseButton::eX2
:
6383 input
.mi
.dwFlags
= isDown
? MOUSEEVENTF_XDOWN
: MOUSEEVENTF_XUP
;
6384 input
.mi
.mouseData
= XBUTTON2
;
6387 return NS_ERROR_INVALID_ARG
;
6391 case NativeMouseMessage::EnterWindow
:
6392 case NativeMouseMessage::LeaveWindow
:
6393 MOZ_ASSERT_UNREACHABLE("Non supported mouse event on Windows");
6394 return NS_ERROR_INVALID_ARG
;
6397 input
.type
= INPUT_MOUSE
;
6398 ::SetCursorPos(aPoint
.x
, aPoint
.y
);
6399 ::SendInput(1, &input
, sizeof(INPUT
));
6404 nsresult
nsWindow::SynthesizeNativeMouseScrollEvent(
6405 LayoutDeviceIntPoint aPoint
, uint32_t aNativeMessage
, double aDeltaX
,
6406 double aDeltaY
, double aDeltaZ
, uint32_t aModifierFlags
,
6407 uint32_t aAdditionalFlags
, nsIObserver
* aObserver
) {
6408 AutoObserverNotifier
notifier(aObserver
, "mousescrollevent");
6409 return MouseScrollHandler::SynthesizeNativeMouseScrollEvent(
6410 this, aPoint
, aNativeMessage
,
6411 (aNativeMessage
== WM_MOUSEWHEEL
|| aNativeMessage
== WM_VSCROLL
)
6412 ? static_cast<int32_t>(aDeltaY
)
6413 : static_cast<int32_t>(aDeltaX
),
6414 aModifierFlags
, aAdditionalFlags
);
6417 nsresult
nsWindow::SynthesizeNativeTouchpadPan(TouchpadGesturePhase aEventPhase
,
6418 LayoutDeviceIntPoint aPoint
,
6419 double aDeltaX
, double aDeltaY
,
6420 int32_t aModifierFlags
,
6421 nsIObserver
* aObserver
) {
6422 AutoObserverNotifier
notifier(aObserver
, "touchpadpanevent");
6423 DirectManipulationOwner::SynthesizeNativeTouchpadPan(
6424 this, aEventPhase
, aPoint
, aDeltaX
, aDeltaY
, aModifierFlags
);
6428 static void MaybeLogPosChanged(HWND aWnd
, WINDOWPOS
* wp
) {
6429 #ifdef WINSTATE_DEBUG_OUTPUT
6430 if (aWnd
== WinUtils::GetTopLevelHWND(aWnd
)) {
6431 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("*** OnWindowPosChanged: [ top] "));
6433 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("*** OnWindowPosChanged: [child] "));
6435 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("WINDOWPOS flags:"));
6436 if (wp
->flags
& SWP_FRAMECHANGED
) {
6437 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_FRAMECHANGED "));
6439 if (wp
->flags
& SWP_SHOWWINDOW
) {
6440 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_SHOWWINDOW "));
6442 if (wp
->flags
& SWP_NOSIZE
) {
6443 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_NOSIZE "));
6445 if (wp
->flags
& SWP_HIDEWINDOW
) {
6446 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_HIDEWINDOW "));
6448 if (wp
->flags
& SWP_NOZORDER
) {
6449 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_NOZORDER "));
6451 if (wp
->flags
& SWP_NOACTIVATE
) {
6452 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("SWP_NOACTIVATE "));
6454 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("\n"));
6458 /**************************************************************
6460 * SECTION: OnXXX message handlers
6462 * For message handlers that need to be broken out or
6463 * implemented in specific platform code.
6465 **************************************************************/
6467 void nsWindow::OnWindowPosChanged(WINDOWPOS
* wp
) {
6472 MaybeLogPosChanged(mWnd
, wp
);
6474 // Handle window size mode changes
6475 if (wp
->flags
& SWP_FRAMECHANGED
) {
6476 // Bug 566135 - Windows theme code calls show window on SW_SHOWMINIMIZED
6477 // windows when fullscreen games disable desktop composition. If we're
6478 // minimized and not being activated, ignore the event and let windows
6480 if (mFrameState
->GetSizeMode() == nsSizeMode_Minimized
&&
6481 (wp
->flags
& SWP_NOACTIVATE
)) {
6485 mFrameState
->OnFrameChanged();
6487 if (mFrameState
->GetSizeMode() == nsSizeMode_Minimized
) {
6488 // Skip window size change events below on minimization.
6493 // Notify visibility change when window is activated.
6494 if (!(wp
->flags
& SWP_NOACTIVATE
) && NeedsToTrackWindowOcclusionState()) {
6495 WinWindowOcclusionTracker::Get()->OnWindowVisibilityChanged(
6496 this, mFrameState
->GetSizeMode() != nsSizeMode_Minimized
);
6499 // Handle window position changes
6500 if (!(wp
->flags
& SWP_NOMOVE
)) {
6501 mBounds
.MoveTo(wp
->x
, wp
->y
);
6502 NotifyWindowMoved(wp
->x
, wp
->y
);
6505 // Handle window size changes
6506 if (!(wp
->flags
& SWP_NOSIZE
)) {
6508 int32_t newWidth
, newHeight
;
6510 ::GetWindowRect(mWnd
, &r
);
6512 newWidth
= r
.right
- r
.left
;
6513 newHeight
= r
.bottom
- r
.top
;
6515 if (newWidth
> mLastSize
.width
) {
6519 drect
.left
= wp
->x
+ mLastSize
.width
;
6521 drect
.right
= drect
.left
+ (newWidth
- mLastSize
.width
);
6522 drect
.bottom
= drect
.top
+ newHeight
;
6524 ::RedrawWindow(mWnd
, &drect
, nullptr,
6525 RDW_INVALIDATE
| RDW_NOERASE
| RDW_NOINTERNALPAINT
|
6526 RDW_ERASENOW
| RDW_ALLCHILDREN
);
6528 if (newHeight
> mLastSize
.height
) {
6533 drect
.top
= wp
->y
+ mLastSize
.height
;
6534 drect
.right
= drect
.left
+ newWidth
;
6535 drect
.bottom
= drect
.top
+ (newHeight
- mLastSize
.height
);
6537 ::RedrawWindow(mWnd
, &drect
, nullptr,
6538 RDW_INVALIDATE
| RDW_NOERASE
| RDW_NOINTERNALPAINT
|
6539 RDW_ERASENOW
| RDW_ALLCHILDREN
);
6542 mBounds
.SizeTo(newWidth
, newHeight
);
6543 mLastSize
.width
= newWidth
;
6544 mLastSize
.height
= newHeight
;
6546 #ifdef WINSTATE_DEBUG_OUTPUT
6547 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
6548 ("*** Resize window: %d x %d x %d x %d\n", wp
->x
, wp
->y
, newWidth
,
6552 if (mAspectRatio
> 0) {
6553 // It's possible (via Windows Aero Snap) that the size of the window
6554 // has changed such that it violates the aspect ratio constraint. If so,
6555 // queue up an event to enforce the aspect ratio constraint and repaint.
6556 // When resized with Windows Aero Snap, we are in the NOT_RESIZING state.
6557 float newAspectRatio
= (float)newWidth
/ newHeight
;
6558 if (mResizeState
== NOT_RESIZING
&& mAspectRatio
!= newAspectRatio
) {
6559 // Hold a reference to self alive and pass it into the lambda to make
6560 // sure this nsIWidget stays alive long enough to run this function.
6561 nsCOMPtr
<nsIWidget
> self(this);
6562 NS_DispatchToMainThread(NS_NewRunnableFunction(
6563 "EnforceAspectRatio", [self
, this, newWidth
]() -> void {
6565 Resize(newWidth
, newWidth
/ mAspectRatio
, true);
6571 // If a maximized window is resized, recalculate the non-client margins.
6572 if (mFrameState
->GetSizeMode() == nsSizeMode_Maximized
) {
6573 if (UpdateNonClientMargins(true)) {
6574 // gecko resize event already sent by UpdateNonClientMargins.
6580 // Notify the widget listener for size change of client area for gecko
6581 // events. This needs to be done when either window size is changed,
6582 // or window frame is changed. They may not happen together.
6583 // However, we don't invoke that for popup when window frame changes,
6584 // because popups may trigger frame change before size change via
6585 // {Set,Clear}ThemeRegion they invoke in Resize. That would make the
6586 // code below call OnResize with a wrong client size first, which can
6587 // lead to flickerling for some popups.
6588 if (!(wp
->flags
& SWP_NOSIZE
) ||
6589 ((wp
->flags
& SWP_FRAMECHANGED
) && !IsPopup())) {
6591 LayoutDeviceIntSize clientSize
;
6592 if (::GetClientRect(mWnd
, &r
)) {
6593 clientSize
= WinUtils::ToIntRect(r
).Size();
6595 clientSize
= mBounds
.Size();
6597 // Send a gecko resize event
6598 OnResize(clientSize
);
6602 void nsWindow::OnWindowPosChanging(WINDOWPOS
* info
) {
6603 // Update non-client margins if the frame size is changing, and let the
6604 // browser know we are changing size modes, so alternative css can kick in.
6605 // If we're going into fullscreen mode, ignore this, since it'll reset
6606 // margins to normal mode.
6607 if (info
->flags
& SWP_FRAMECHANGED
&& !(info
->flags
& SWP_NOSIZE
)) {
6608 mFrameState
->OnFrameChanging();
6611 // Force fullscreen. This works around a bug in Windows 10 1809 where
6612 // using fullscreen when a window is "snapped" causes a spurious resize
6613 // smaller than the full screen, see bug 1482920.
6614 if (mFrameState
->GetSizeMode() == nsSizeMode_Fullscreen
&&
6615 !(info
->flags
& SWP_NOMOVE
) && !(info
->flags
& SWP_NOSIZE
)) {
6616 nsCOMPtr
<nsIScreenManager
> screenmgr
=
6617 do_GetService(sScreenManagerContractID
);
6619 LayoutDeviceIntRect
bounds(info
->x
, info
->y
, info
->cx
, info
->cy
);
6620 DesktopIntRect deskBounds
=
6621 RoundedToInt(bounds
/ GetDesktopToDeviceScale());
6622 nsCOMPtr
<nsIScreen
> screen
;
6623 screenmgr
->ScreenForRect(deskBounds
.X(), deskBounds
.Y(),
6624 deskBounds
.Width(), deskBounds
.Height(),
6625 getter_AddRefs(screen
));
6628 auto rect
= screen
->GetRect();
6631 info
->cx
= rect
.width
;
6632 info
->cy
= rect
.height
;
6637 // enforce local z-order rules
6638 if (!(info
->flags
& SWP_NOZORDER
)) {
6639 HWND hwndAfter
= info
->hwndInsertAfter
;
6641 nsWindow
* aboveWindow
= 0;
6642 nsWindowZ placement
;
6644 if (hwndAfter
== HWND_BOTTOM
)
6645 placement
= nsWindowZBottom
;
6646 else if (hwndAfter
== HWND_TOP
|| hwndAfter
== HWND_TOPMOST
||
6647 hwndAfter
== HWND_NOTOPMOST
)
6648 placement
= nsWindowZTop
;
6650 placement
= nsWindowZRelative
;
6651 aboveWindow
= WinUtils::GetNSWindowPtr(hwndAfter
);
6654 if (mWidgetListener
) {
6655 nsCOMPtr
<nsIWidget
> actualBelow
= nullptr;
6656 if (mWidgetListener
->ZLevelChanged(false, &placement
, aboveWindow
,
6657 getter_AddRefs(actualBelow
))) {
6658 if (placement
== nsWindowZBottom
)
6659 info
->hwndInsertAfter
= HWND_BOTTOM
;
6660 else if (placement
== nsWindowZTop
)
6661 info
->hwndInsertAfter
= HWND_TOP
;
6663 info
->hwndInsertAfter
=
6664 (HWND
)actualBelow
->GetNativeData(NS_NATIVE_WINDOW
);
6669 // prevent rude external programs from making hidden window visible
6670 if (mWindowType
== WindowType::Invisible
) info
->flags
&= ~SWP_SHOWWINDOW
;
6672 // When waking from sleep or switching out of tablet mode, Windows 10
6673 // Version 1809 will reopen popup windows that should be hidden. Detect
6674 // this case and refuse to show the window.
6675 static bool sDWMUnhidesPopups
= IsWin10Sep2018UpdateOrLater();
6676 if (sDWMUnhidesPopups
&& (info
->flags
& SWP_SHOWWINDOW
) &&
6677 mWindowType
== WindowType::Popup
&& mWidgetListener
&&
6678 mWidgetListener
->ShouldNotBeVisible()) {
6679 info
->flags
&= ~SWP_SHOWWINDOW
;
6683 void nsWindow::UserActivity() {
6684 // Check if we have the idle service, if not we try to get it.
6685 if (!mIdleService
) {
6686 mIdleService
= do_GetService("@mozilla.org/widget/useridleservice;1");
6689 // Check that we now have the idle service.
6691 mIdleService
->ResetIdleTimeOut(0);
6695 // Helper function for TouchDeviceNeedsPanGestureConversion(PTOUCHINPUT,
6697 static bool TouchDeviceNeedsPanGestureConversion(HANDLE aSource
) {
6698 std::string deviceName
;
6700 // The first call just queries how long the name string will be.
6701 GetRawInputDeviceInfoA(aSource
, RIDI_DEVICENAME
, nullptr, &dataSize
);
6702 if (!dataSize
|| dataSize
> 0x10000) {
6705 deviceName
.resize(dataSize
);
6706 // The second call actually populates the string.
6707 UINT result
= GetRawInputDeviceInfoA(aSource
, RIDI_DEVICENAME
, &deviceName
[0],
6709 if (result
== UINT_MAX
) {
6712 // The affected device name is "\\?\VIRTUAL_DIGITIZER", but each backslash
6713 // needs to be escaped with another one.
6714 std::string expectedDeviceName
= "\\\\?\\VIRTUAL_DIGITIZER";
6715 // For some reason, the dataSize returned by the first call is double the
6716 // actual length of the device name (as if it were returning the size of a
6717 // wide-character string in bytes) even though we are using the narrow
6718 // version of the API. For the comparison against the expected device name
6719 // to pass, we truncate the buffer to be no longer tha the expected device
6721 if (deviceName
.substr(0, expectedDeviceName
.length()) != expectedDeviceName
) {
6725 RID_DEVICE_INFO deviceInfo
;
6726 deviceInfo
.cbSize
= sizeof(deviceInfo
);
6727 dataSize
= sizeof(deviceInfo
);
6729 GetRawInputDeviceInfoA(aSource
, RIDI_DEVICEINFO
, &deviceInfo
, &dataSize
);
6730 if (result
== UINT_MAX
) {
6733 // The device identifiers that we check for here come from bug 1355162
6734 // comment 1 (see also bug 1511901 comment 35).
6735 return deviceInfo
.dwType
== RIM_TYPEHID
&& deviceInfo
.hid
.dwVendorId
== 0 &&
6736 deviceInfo
.hid
.dwProductId
== 0 &&
6737 deviceInfo
.hid
.dwVersionNumber
== 1 &&
6738 deviceInfo
.hid
.usUsagePage
== 13 && deviceInfo
.hid
.usUsage
== 4;
6741 // Determine if the touch device that originated |aOSEvent| needs to have
6742 // touch events representing a two-finger gesture converted to pan
6744 // We only do this for touch devices with a specific name and identifiers.
6745 static bool TouchDeviceNeedsPanGestureConversion(PTOUCHINPUT aOSEvent
,
6746 uint32_t aTouchCount
) {
6747 if (!StaticPrefs::apz_windows_check_for_pan_gesture_conversion()) {
6750 if (aTouchCount
== 0) {
6753 HANDLE source
= aOSEvent
[0].hSource
;
6755 // Cache the result of this computation for each touch device.
6756 // Touch devices are identified by the HANDLE stored in the hSource
6757 // field of TOUCHINPUT.
6758 static std::map
<HANDLE
, bool> sResultCache
;
6759 auto [iter
, inserted
] = sResultCache
.emplace(source
, false);
6761 iter
->second
= TouchDeviceNeedsPanGestureConversion(source
);
6763 return iter
->second
;
6766 Maybe
<PanGestureInput
> nsWindow::ConvertTouchToPanGesture(
6767 const MultiTouchInput
& aTouchInput
, PTOUCHINPUT aOSEvent
) {
6768 // Checks if the touch device that originated the touch event is one
6769 // for which we want to convert the touch events to pang gesture events.
6770 bool shouldConvert
= TouchDeviceNeedsPanGestureConversion(
6771 aOSEvent
, aTouchInput
.mTouches
.Length());
6772 if (!shouldConvert
) {
6776 // Only two-finger gestures need conversion.
6777 if (aTouchInput
.mTouches
.Length() != 2) {
6781 PanGestureInput::PanGestureType eventType
= PanGestureInput::PANGESTURE_PAN
;
6782 if (aTouchInput
.mType
== MultiTouchInput::MULTITOUCH_START
) {
6783 eventType
= PanGestureInput::PANGESTURE_START
;
6784 } else if (aTouchInput
.mType
== MultiTouchInput::MULTITOUCH_END
) {
6785 eventType
= PanGestureInput::PANGESTURE_END
;
6786 } else if (aTouchInput
.mType
== MultiTouchInput::MULTITOUCH_CANCEL
) {
6787 eventType
= PanGestureInput::PANGESTURE_CANCELLED
;
6790 // Use the midpoint of the two touches as the start point of the pan gesture.
6791 ScreenPoint focusPoint
= (aTouchInput
.mTouches
[0].mScreenPoint
+
6792 aTouchInput
.mTouches
[1].mScreenPoint
) /
6794 // To compute the displacement of the pan gesture, we keep track of the
6795 // location of the previous event.
6796 ScreenPoint displacement
= (eventType
== PanGestureInput::PANGESTURE_START
)
6798 : (focusPoint
- mLastPanGestureFocus
);
6799 mLastPanGestureFocus
= focusPoint
;
6801 // We need to negate the displacement because for a touch event, moving the
6802 // fingers down results in scrolling up, but for a touchpad gesture, we want
6803 // moving the fingers down to result in scrolling down.
6804 PanGestureInput
result(eventType
, aTouchInput
.mTimeStamp
, focusPoint
,
6805 -displacement
, aTouchInput
.modifiers
);
6806 result
.mSimulateMomentum
= true;
6808 return Some(result
);
6811 // Dispatch an event that originated as an OS touch event.
6812 // Usually, we want to dispatch it as a touch event, but some touchpads
6813 // produce touch events for two-finger scrolling, which need to be converted
6814 // to pan gesture events for correct behaviour.
6815 void nsWindow::DispatchTouchOrPanGestureInput(MultiTouchInput
& aTouchInput
,
6816 PTOUCHINPUT aOSEvent
) {
6817 if (Maybe
<PanGestureInput
> panInput
=
6818 ConvertTouchToPanGesture(aTouchInput
, aOSEvent
)) {
6819 DispatchPanGestureInput(*panInput
);
6823 DispatchTouchInput(aTouchInput
);
6826 bool nsWindow::OnTouch(WPARAM wParam
, LPARAM lParam
) {
6827 uint32_t cInputs
= LOWORD(wParam
);
6828 PTOUCHINPUT pInputs
= new TOUCHINPUT
[cInputs
];
6830 if (GetTouchInputInfo((HTOUCHINPUT
)lParam
, cInputs
, pInputs
,
6831 sizeof(TOUCHINPUT
))) {
6832 MultiTouchInput touchInput
, touchEndInput
;
6834 // Walk across the touch point array processing each contact point.
6835 for (uint32_t i
= 0; i
< cInputs
; i
++) {
6836 bool addToEvent
= false, addToEndEvent
= false;
6838 // N.B.: According with MS documentation
6839 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd317334(v=vs.85).aspx
6840 // TOUCHEVENTF_DOWN cannot be combined with TOUCHEVENTF_MOVE or
6841 // TOUCHEVENTF_UP. Possibly, it means that TOUCHEVENTF_MOVE and
6842 // TOUCHEVENTF_UP can be combined together.
6844 if (pInputs
[i
].dwFlags
& (TOUCHEVENTF_DOWN
| TOUCHEVENTF_MOVE
)) {
6845 if (touchInput
.mTimeStamp
.IsNull()) {
6846 // Initialize a touch event to send.
6847 touchInput
.mType
= MultiTouchInput::MULTITOUCH_MOVE
;
6848 touchInput
.mTimeStamp
= GetMessageTimeStamp(::GetMessageTime());
6849 ModifierKeyState modifierKeyState
;
6850 touchInput
.modifiers
= modifierKeyState
.GetModifiers();
6852 // Pres shell expects this event to be a eTouchStart
6853 // if any new contact points have been added since the last event sent.
6854 if (pInputs
[i
].dwFlags
& TOUCHEVENTF_DOWN
) {
6855 touchInput
.mType
= MultiTouchInput::MULTITOUCH_START
;
6859 if (pInputs
[i
].dwFlags
& TOUCHEVENTF_UP
) {
6860 // Pres shell expects removed contacts points to be delivered in a
6861 // separate eTouchEnd event containing only the contact points that were
6863 if (touchEndInput
.mTimeStamp
.IsNull()) {
6864 // Initialize a touch event to send.
6865 touchEndInput
.mType
= MultiTouchInput::MULTITOUCH_END
;
6866 touchEndInput
.mTimeStamp
= GetMessageTimeStamp(::GetMessageTime());
6867 ModifierKeyState modifierKeyState
;
6868 touchEndInput
.modifiers
= modifierKeyState
.GetModifiers();
6870 addToEndEvent
= true;
6872 if (!addToEvent
&& !addToEndEvent
) {
6873 // Filter out spurious Windows events we don't understand, like palm
6878 // Setup the touch point we'll append to the touch event array.
6879 nsPointWin touchPoint
;
6880 touchPoint
.x
= TOUCH_COORD_TO_PIXEL(pInputs
[i
].x
);
6881 touchPoint
.y
= TOUCH_COORD_TO_PIXEL(pInputs
[i
].y
);
6882 touchPoint
.ScreenToClient(mWnd
);
6884 // Initialize the touch data.
6885 SingleTouchData
touchData(
6886 pInputs
[i
].dwID
, // aIdentifier
6887 ScreenIntPoint::FromUnknownPoint(touchPoint
), // aScreenPoint
6888 // The contact area info cannot be trusted even when
6889 // TOUCHINPUTMASKF_CONTACTAREA is set when the input source is pen,
6890 // which somehow violates the API docs. (bug 1710509) Ultimately the
6891 // dwFlags check will become redundant since we want to migrate to
6892 // WM_POINTER for pens. (bug 1707075)
6893 (pInputs
[i
].dwMask
& TOUCHINPUTMASKF_CONTACTAREA
) &&
6894 !(pInputs
[i
].dwFlags
& TOUCHEVENTF_PEN
)
6895 ? ScreenSize(TOUCH_COORD_TO_PIXEL(pInputs
[i
].cxContact
) / 2,
6896 TOUCH_COORD_TO_PIXEL(pInputs
[i
].cyContact
) / 2)
6897 : ScreenSize(1, 1), // aRadius
6898 0.0f
, // aRotationAngle
6901 // Append touch data to the appropriate event.
6903 touchInput
.mTouches
.AppendElement(touchData
);
6905 if (addToEndEvent
) {
6906 touchEndInput
.mTouches
.AppendElement(touchData
);
6910 // Dispatch touch start and touch move event if we have one.
6911 if (!touchInput
.mTimeStamp
.IsNull()) {
6912 DispatchTouchOrPanGestureInput(touchInput
, pInputs
);
6914 // Dispatch touch end event if we have one.
6915 if (!touchEndInput
.mTimeStamp
.IsNull()) {
6916 DispatchTouchOrPanGestureInput(touchEndInput
, pInputs
);
6921 CloseTouchInputHandle((HTOUCHINPUT
)lParam
);
6925 // Gesture event processing. Handles WM_GESTURE events.
6926 bool nsWindow::OnGesture(WPARAM wParam
, LPARAM lParam
) {
6927 // Treatment for pan events which translate into scroll events:
6928 if (mGesture
.IsPanEvent(lParam
)) {
6929 if (!mGesture
.ProcessPanMessage(mWnd
, wParam
, lParam
))
6930 return false; // ignore
6932 nsEventStatus status
;
6934 WidgetWheelEvent
wheelEvent(true, eWheel
, this);
6936 ModifierKeyState modifierKeyState
;
6937 modifierKeyState
.InitInputEvent(wheelEvent
);
6939 wheelEvent
.mButton
= 0;
6940 wheelEvent
.mTimeStamp
= GetMessageTimeStamp(::GetMessageTime());
6941 wheelEvent
.mInputSource
= MouseEvent_Binding::MOZ_SOURCE_TOUCH
;
6943 bool endFeedback
= true;
6945 if (mGesture
.PanDeltaToPixelScroll(wheelEvent
)) {
6946 DispatchEvent(&wheelEvent
, status
);
6949 if (mDisplayPanFeedback
) {
6950 mGesture
.UpdatePanFeedbackX(
6951 mWnd
, DeprecatedAbs(RoundDown(wheelEvent
.mOverflowDeltaX
)),
6953 mGesture
.UpdatePanFeedbackY(
6954 mWnd
, DeprecatedAbs(RoundDown(wheelEvent
.mOverflowDeltaY
)),
6956 mGesture
.PanFeedbackFinalize(mWnd
, endFeedback
);
6959 CloseGestureInfoHandle((HGESTUREINFO
)lParam
);
6964 // Other gestures translate into simple gesture events:
6965 WidgetSimpleGestureEvent
event(true, eVoidEvent
, this);
6966 if (!mGesture
.ProcessGestureMessage(mWnd
, wParam
, lParam
, event
)) {
6967 return false; // fall through to DefWndProc
6970 // Polish up and send off the new event
6971 ModifierKeyState modifierKeyState
;
6972 modifierKeyState
.InitInputEvent(event
);
6974 event
.mTimeStamp
= GetMessageTimeStamp(::GetMessageTime());
6975 event
.mInputSource
= MouseEvent_Binding::MOZ_SOURCE_TOUCH
;
6977 nsEventStatus status
;
6978 DispatchEvent(&event
, status
);
6979 if (status
== nsEventStatus_eIgnore
) {
6980 return false; // Ignored, fall through
6983 // Only close this if we process and return true.
6984 CloseGestureInfoHandle((HGESTUREINFO
)lParam
);
6986 return true; // Handled
6989 // WM_DESTROY event handler
6990 void nsWindow::OnDestroy() {
6991 mOnDestroyCalled
= true;
6993 // If this is a toplevel window, notify the taskbar concealer to clean up any
6996 TaskbarConcealer::OnWindowDestroyed(mWnd
);
6999 // Make sure we don't get destroyed in the process of tearing down.
7000 nsCOMPtr
<nsIWidget
> kungFuDeathGrip(this);
7002 // Dispatch the destroy notification.
7003 if (!mInDtor
) NotifyWindowDestroyed();
7005 // Prevent the widget from sending additional events.
7006 mWidgetListener
= nullptr;
7007 mAttachedWidgetListener
= nullptr;
7009 DestroyDirectManipulation();
7011 if (mWnd
== mLastKillFocusWindow
) {
7012 mLastKillFocusWindow
= nullptr;
7014 // Unregister notifications from terminal services
7015 ::WTSUnRegisterSessionNotification(mWnd
);
7017 // We will stop receiving native events after dissociating from our native
7018 // window. We will also disappear from the output of WinUtils::GetNSWindowPtr
7020 DissociateFromNativeWindow();
7022 // Once mWidgetListener is cleared and the subclass is reset, sCurrentWindow
7023 // can be cleared. (It's used in tracking windows for mouse events.)
7024 if (sCurrentWindow
== this) sCurrentWindow
= nullptr;
7026 // Disconnects us from our parent, will call our GetParent().
7027 nsBaseWidget::Destroy();
7029 // Release references to children, device context, toolkit, and app shell.
7030 nsBaseWidget::OnDestroy();
7032 // Clear our native parent handle.
7033 // XXX Windows will take care of this in the proper order, and
7034 // SetParent(nullptr)'s remove child on the parent already took place in
7035 // nsBaseWidget's Destroy call above.
7036 // SetParent(nullptr);
7039 // We have to destroy the native drag target before we null out our window
7041 EnableDragDrop(false);
7043 // If we're going away and for some reason we're still the rollup widget,
7044 // rollup and turn off capture.
7045 nsIRollupListener
* rollupListener
= nsBaseWidget::GetActiveRollupListener();
7046 nsCOMPtr
<nsIWidget
> rollupWidget
;
7047 if (rollupListener
) {
7048 rollupWidget
= rollupListener
->GetRollupWidget();
7050 if (this == rollupWidget
) {
7051 rollupListener
->Rollup({});
7052 CaptureRollupEvents(false);
7055 IMEHandler::OnDestroyWindow(this);
7057 // Free GDI window class objects
7059 VERIFY(::DeleteObject(mBrush
));
7063 // Destroy any custom cursor resources.
7064 if (mCursor
.IsCustom()) {
7065 SetCursor(Cursor
{eCursor_standard
});
7068 if (mCompositorWidgetDelegate
) {
7069 mCompositorWidgetDelegate
->OnDestroyWindow();
7071 mBasicLayersSurface
= nullptr;
7073 // Finalize panning feedback to possibly restore window displacement
7074 mGesture
.PanFeedbackFinalize(mWnd
, true);
7076 // Clear the main HWND.
7080 // Send a resize message to the listener
7081 bool nsWindow::OnResize(const LayoutDeviceIntSize
& aSize
) {
7082 if (mCompositorWidgetDelegate
&&
7083 !mCompositorWidgetDelegate
->OnWindowResize(aSize
)) {
7087 bool result
= false;
7088 if (mWidgetListener
) {
7089 result
= mWidgetListener
->WindowResized(this, aSize
.width
, aSize
.height
);
7092 // If there is an attached view, inform it as well as the normal widget
7094 if (mAttachedWidgetListener
) {
7095 return mAttachedWidgetListener
->WindowResized(this, aSize
.width
,
7102 void nsWindow::OnSizeModeChange() {
7103 const nsSizeMode mode
= mFrameState
->GetSizeMode();
7105 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
7106 ("nsWindow::OnSizeModeChange() sizeMode %d", mode
));
7108 if (NeedsToTrackWindowOcclusionState()) {
7109 WinWindowOcclusionTracker::Get()->OnWindowVisibilityChanged(
7110 this, mode
!= nsSizeMode_Minimized
);
7112 wr::DebugFlags flags
{0};
7113 flags
._0
= gfx::gfxVars::WebRenderDebugFlags();
7114 bool debugEnabled
= bool(flags
& wr::DebugFlags::WINDOW_VISIBILITY_DBG
);
7115 if (debugEnabled
&& mCompositorWidgetDelegate
) {
7116 mCompositorWidgetDelegate
->NotifyVisibilityUpdated(mode
,
7121 if (mCompositorWidgetDelegate
) {
7122 mCompositorWidgetDelegate
->OnWindowModeChange(mode
);
7125 if (mWidgetListener
) {
7126 mWidgetListener
->SizeModeChanged(mode
);
7130 bool nsWindow::OnHotKey(WPARAM wParam
, LPARAM lParam
) { return true; }
7132 bool nsWindow::IsPopup() { return mWindowType
== WindowType::Popup
; }
7134 bool nsWindow::ShouldUseOffMainThreadCompositing() {
7135 if (mWindowType
== WindowType::Popup
&& mPopupType
== PopupType::Tooltip
) {
7139 // Content rendering of popup is always done by child window.
7140 // See nsDocumentViewer::ShouldAttachToTopLevel().
7141 if (mWindowType
== WindowType::Popup
&& !mIsChildWindow
) {
7142 MOZ_ASSERT(!mParent
);
7146 return nsBaseWidget::ShouldUseOffMainThreadCompositing();
7149 void nsWindow::WindowUsesOMTC() {
7150 ULONG_PTR style
= ::GetClassLongPtr(mWnd
, GCL_STYLE
);
7152 NS_WARNING("Could not get window class style");
7155 style
|= CS_HREDRAW
| CS_VREDRAW
;
7156 DebugOnly
<ULONG_PTR
> result
= ::SetClassLongPtr(mWnd
, GCL_STYLE
, style
);
7157 NS_WARNING_ASSERTION(result
, "Could not reset window class style");
7160 void nsWindow::OnDPIChanged(int32_t x
, int32_t y
, int32_t width
,
7162 // Don't try to handle WM_DPICHANGED for popup windows (see bug 1239353);
7163 // they remain tied to their original parent's resolution.
7164 if (mWindowType
== WindowType::Popup
) {
7167 if (StaticPrefs::layout_css_devPixelsPerPx() > 0.0) {
7170 mDefaultScale
= -1.0; // force recomputation of scale factor
7172 if (mResizeState
!= RESIZING
&&
7173 mFrameState
->GetSizeMode() == nsSizeMode_Normal
) {
7174 // Limit the position (if not in the middle of a drag-move) & size,
7175 // if it would overflow the destination screen
7176 nsCOMPtr
<nsIScreenManager
> sm
= do_GetService(sScreenManagerContractID
);
7178 nsCOMPtr
<nsIScreen
> screen
;
7179 sm
->ScreenForRect(x
, y
, width
, height
, getter_AddRefs(screen
));
7181 int32_t availLeft
, availTop
, availWidth
, availHeight
;
7182 screen
->GetAvailRect(&availLeft
, &availTop
, &availWidth
, &availHeight
);
7183 if (mResizeState
!= MOVING
) {
7184 x
= std::max(x
, availLeft
);
7185 y
= std::max(y
, availTop
);
7187 width
= std::min(width
, availWidth
);
7188 height
= std::min(height
, availHeight
);
7192 Resize(x
, y
, width
, height
, true);
7194 UpdateNonClientMargins();
7199 // Callback to generate OnCloakChanged pseudo-events.
7201 void nsWindow::OnCloakEvent(HWND aWnd
, bool aCloaked
) {
7202 MOZ_ASSERT(NS_IsMainThread());
7204 const char* const kEventName
= aCloaked
? "CLOAKED" : "UNCLOAKED";
7205 nsWindow
* pWin
= WinUtils::GetNSWindowPtr(aWnd
);
7208 sCloakingLog
, LogLevel::Debug
,
7209 ("Received %s event for HWND %p (not an nsWindow)", kEventName
, aWnd
));
7213 const char* const kWasCloakedStr
= pWin
->mIsCloaked
? "cloaked" : "uncloaked";
7214 if (mozilla::IsCloaked(aWnd
) == pWin
->mIsCloaked
) {
7215 MOZ_LOG(sCloakingLog
, LogLevel::Debug
,
7216 ("Received redundant %s event for %s HWND %p; discarding",
7217 kEventName
, kWasCloakedStr
, aWnd
));
7222 sCloakingLog
, LogLevel::Info
,
7223 ("Received %s event for %s HWND %p", kEventName
, kWasCloakedStr
, aWnd
));
7225 // Cloaking events like the one we've just received are sent asynchronously.
7226 // Rather than process them one-by-one, we jump the gun a bit and perform
7227 // updates on all newly cloaked/uncloaked nsWindows at once. This also lets us
7228 // batch operations that consider more than one window's state.
7233 nsTArray
<Item
> changedWindows
;
7235 mozilla::EnumerateThreadWindows([&](HWND hwnd
) {
7236 nsWindow
* pWin
= WinUtils::GetNSWindowPtr(hwnd
);
7241 const bool isCloaked
= mozilla::IsCloaked(hwnd
);
7242 if (isCloaked
!= pWin
->mIsCloaked
) {
7243 changedWindows
.AppendElement(Item
{pWin
, isCloaked
});
7247 if (changedWindows
.IsEmpty()) {
7251 for (const Item
& item
: changedWindows
) {
7252 item
.win
->OnCloakChanged(item
.nowCloaked
);
7255 nsWindow::TaskbarConcealer::OnCloakChanged();
7258 void nsWindow::OnCloakChanged(bool aCloaked
) {
7259 MOZ_LOG(sCloakingLog
, LogLevel::Info
,
7260 ("Calling OnCloakChanged(): HWND %p, aCloaked %s", mWnd
,
7261 aCloaked
? "true" : "false"));
7262 mIsCloaked
= aCloaked
;
7265 /**************************************************************
7266 **************************************************************
7268 ** BLOCK: IME management and accessibility
7270 ** Handles managing IME input and accessibility.
7272 **************************************************************
7273 **************************************************************/
7275 void nsWindow::SetInputContext(const InputContext
& aContext
,
7276 const InputContextAction
& aAction
) {
7277 InputContext newInputContext
= aContext
;
7278 IMEHandler::SetInputContext(this, newInputContext
, aAction
);
7279 mInputContext
= newInputContext
;
7282 InputContext
nsWindow::GetInputContext() {
7283 mInputContext
.mIMEState
.mOpen
= IMEState::CLOSED
;
7284 if (WinUtils::IsIMEEnabled(mInputContext
) && IMEHandler::GetOpenState(this)) {
7285 mInputContext
.mIMEState
.mOpen
= IMEState::OPEN
;
7287 mInputContext
.mIMEState
.mOpen
= IMEState::CLOSED
;
7289 return mInputContext
;
7292 TextEventDispatcherListener
* nsWindow::GetNativeTextEventDispatcherListener() {
7293 return IMEHandler::GetNativeTextEventDispatcherListener();
7296 #ifdef ACCESSIBILITY
7298 # define NS_LOG_WMGETOBJECT(aWnd, aHwnd, aAcc) \
7299 if (a11y::logging::IsEnabled(a11y::logging::ePlatforms)) { \
7301 "Get the window:\n {\n HWND: %p, parent HWND: %p, wndobj: " \
7303 aHwnd, ::GetParent(aHwnd), aWnd); \
7304 printf(" acc: %p", aAcc); \
7306 nsAutoString name; \
7308 printf(", accname: %s", NS_ConvertUTF16toUTF8(name).get()); \
7314 # define NS_LOG_WMGETOBJECT(aWnd, aHwnd, aAcc)
7317 a11y::LocalAccessible
* nsWindow::GetAccessible() {
7318 // If the pref was ePlatformIsDisabled, return null here, disabling a11y.
7319 if (a11y::PlatformDisabledState() == a11y::ePlatformIsDisabled
)
7322 if (mInDtor
|| mOnDestroyCalled
|| mWindowType
== WindowType::Invisible
) {
7326 // In case of popup window return a popup accessible.
7327 nsView
* view
= nsView::GetViewFor(this);
7329 nsIFrame
* frame
= view
->GetFrame();
7330 if (frame
&& nsLayoutUtils::IsPopup(frame
)) {
7331 nsAccessibilityService
* accService
= GetOrCreateAccService();
7333 a11y::DocAccessible
* docAcc
=
7334 GetAccService()->GetDocAccessible(frame
->PresShell());
7338 docAcc
->GetAccessibleOrDescendant(frame
->GetContent()));
7339 return docAcc
->GetAccessibleOrDescendant(frame
->GetContent());
7345 // otherwise root document accessible.
7346 NS_LOG_WMGETOBJECT(this, mWnd
, GetRootAccessible());
7347 return GetRootAccessible();
7351 /**************************************************************
7352 **************************************************************
7354 ** BLOCK: Transparency
7356 ** Window transparency helpers.
7358 **************************************************************
7359 **************************************************************/
7361 void nsWindow::SetWindowTranslucencyInner(TransparencyMode aMode
) {
7362 if (aMode
== mTransparencyMode
) return;
7364 // stop on dialogs and popups!
7365 HWND hWnd
= WinUtils::GetTopLevelHWND(mWnd
, true);
7366 nsWindow
* parent
= WinUtils::GetNSWindowPtr(hWnd
);
7369 NS_WARNING("Trying to use transparent chrome in an embedded context");
7373 if (parent
!= this) {
7375 "Setting SetWindowTranslucencyInner on a parent this is not us!");
7378 if (aMode
== TransparencyMode::Transparent
) {
7379 // If we're switching to the use of a transparent window, hide the chrome
7381 HideWindowChrome(true);
7382 } else if (mHideChrome
&&
7383 mTransparencyMode
== TransparencyMode::Transparent
) {
7384 // if we're switching out of transparent, re-enable our parent's chrome.
7385 HideWindowChrome(false);
7388 LONG_PTR style
= ::GetWindowLongPtrW(hWnd
, GWL_STYLE
),
7389 exStyle
= ::GetWindowLongPtr(hWnd
, GWL_EXSTYLE
);
7391 if (parent
->mIsVisible
) {
7392 style
|= WS_VISIBLE
;
7393 if (parent
->mFrameState
->GetSizeMode() == nsSizeMode_Maximized
) {
7394 style
|= WS_MAXIMIZE
;
7395 } else if (parent
->mFrameState
->GetSizeMode() == nsSizeMode_Minimized
) {
7396 style
|= WS_MINIMIZE
;
7400 if (aMode
== TransparencyMode::Transparent
)
7401 exStyle
|= WS_EX_LAYERED
;
7403 exStyle
&= ~WS_EX_LAYERED
;
7405 VERIFY_WINDOW_STYLE(style
);
7406 ::SetWindowLongPtrW(hWnd
, GWL_STYLE
, style
);
7407 ::SetWindowLongPtrW(hWnd
, GWL_EXSTYLE
, exStyle
);
7409 mTransparencyMode
= aMode
;
7411 if (mCompositorWidgetDelegate
) {
7412 mCompositorWidgetDelegate
->UpdateTransparency(aMode
);
7416 /**************************************************************
7417 **************************************************************
7419 ** BLOCK: Popup rollup hooks
7421 ** Deals with CaptureRollup on popup windows.
7423 **************************************************************
7424 **************************************************************/
7426 // Schedules a timer for a window, so we can rollup after processing the hook
7428 void nsWindow::ScheduleHookTimer(HWND aWnd
, UINT aMsgId
) {
7429 // In some cases multiple hooks may be scheduled
7430 // so ignore any other requests once one timer is scheduled
7431 if (sHookTimerId
== 0) {
7432 // Remember the window handle and the message ID to be used later
7433 sRollupMsgId
= aMsgId
;
7434 sRollupMsgWnd
= aWnd
;
7435 // Schedule native timer for doing the rollup after
7436 // this event is done being processed
7437 sHookTimerId
= ::SetTimer(nullptr, 0, 0, (TIMERPROC
)HookTimerForPopups
);
7438 NS_ASSERTION(sHookTimerId
, "Timer couldn't be created.");
7442 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7443 int gLastMsgCode
= 0;
7444 extern MSGFEventMsgInfo gMSGFEvents
[];
7447 // Process Menu messages, rollup when popup is clicked.
7448 LRESULT CALLBACK
nsWindow::MozSpecialMsgFilter(int code
, WPARAM wParam
,
7450 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7452 MSG
* pMsg
= (MSG
*)lParam
;
7455 while (gMSGFEvents
[inx
].mId
!= code
&& gMSGFEvents
[inx
].mStr
!= nullptr) {
7458 if (code
!= gLastMsgCode
) {
7459 if (gMSGFEvents
[inx
].mId
== code
) {
7461 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
7462 ("MozSpecialMessageProc - code: 0x%X - %s hw: %p\n", code
,
7463 gMSGFEvents
[inx
].mStr
, pMsg
->hwnd
));
7467 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
7468 ("MozSpecialMessageProc - code: 0x%X - %d hw: %p\n", code
,
7469 gMSGFEvents
[inx
].mId
, pMsg
->hwnd
));
7472 gLastMsgCode
= code
;
7474 PrintEvent(pMsg
->message
, FALSE
, FALSE
);
7476 #endif // #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7478 if (sProcessHook
&& code
== MSGF_MENU
) {
7479 MSG
* pMsg
= (MSG
*)lParam
;
7480 ScheduleHookTimer(pMsg
->hwnd
, pMsg
->message
);
7483 return ::CallNextHookEx(sMsgFilterHook
, code
, wParam
, lParam
);
7486 // Process all mouse messages. Roll up when a click is in a native window
7487 // that doesn't have an nsIWidget.
7488 LRESULT CALLBACK
nsWindow::MozSpecialMouseProc(int code
, WPARAM wParam
,
7491 switch (WinUtils::GetNativeMessage(wParam
)) {
7492 case WM_LBUTTONDOWN
:
7493 case WM_RBUTTONDOWN
:
7494 case WM_MBUTTONDOWN
:
7496 case WM_MOUSEHWHEEL
: {
7497 MOUSEHOOKSTRUCT
* ms
= (MOUSEHOOKSTRUCT
*)lParam
;
7498 nsIWidget
* mozWin
= WinUtils::GetNSWindowPtr(ms
->hwnd
);
7500 ScheduleHookTimer(ms
->hwnd
, (UINT
)wParam
);
7506 return ::CallNextHookEx(sCallMouseHook
, code
, wParam
, lParam
);
7509 // Process all messages. Roll up when the window is moving, or
7510 // is resizing or when maximized or mininized.
7511 LRESULT CALLBACK
nsWindow::MozSpecialWndProc(int code
, WPARAM wParam
,
7513 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7515 CWPSTRUCT
* cwpt
= (CWPSTRUCT
*)lParam
;
7516 PrintEvent(cwpt
->message
, FALSE
, FALSE
);
7521 CWPSTRUCT
* cwpt
= (CWPSTRUCT
*)lParam
;
7522 if (cwpt
->message
== WM_MOVING
|| cwpt
->message
== WM_SIZING
||
7523 cwpt
->message
== WM_GETMINMAXINFO
) {
7524 ScheduleHookTimer(cwpt
->hwnd
, (UINT
)cwpt
->message
);
7528 return ::CallNextHookEx(sCallProcHook
, code
, wParam
, lParam
);
7531 // Register the special "hooks" for dropdown processing.
7532 void nsWindow::RegisterSpecialDropdownHooks() {
7533 NS_ASSERTION(!sMsgFilterHook
, "sMsgFilterHook must be NULL!");
7534 NS_ASSERTION(!sCallProcHook
, "sCallProcHook must be NULL!");
7536 DISPLAY_NMM_PRT("***************** Installing Msg Hooks ***************\n");
7538 // Install msg hook for moving the window and resizing
7539 if (!sMsgFilterHook
) {
7540 DISPLAY_NMM_PRT("***** Hooking sMsgFilterHook!\n");
7541 sMsgFilterHook
= SetWindowsHookEx(WH_MSGFILTER
, MozSpecialMsgFilter
,
7542 nullptr, GetCurrentThreadId());
7543 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7544 if (!sMsgFilterHook
) {
7545 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
7546 ("***** SetWindowsHookEx is NOT installed for WH_MSGFILTER!\n"));
7551 // Install msg hook for menus
7552 if (!sCallProcHook
) {
7553 DISPLAY_NMM_PRT("***** Hooking sCallProcHook!\n");
7554 sCallProcHook
= SetWindowsHookEx(WH_CALLWNDPROC
, MozSpecialWndProc
, nullptr,
7555 GetCurrentThreadId());
7556 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7557 if (!sCallProcHook
) {
7559 gWindowsLog
, LogLevel::Info
,
7560 ("***** SetWindowsHookEx is NOT installed for WH_CALLWNDPROC!\n"));
7565 // Install msg hook for the mouse
7566 if (!sCallMouseHook
) {
7567 DISPLAY_NMM_PRT("***** Hooking sCallMouseHook!\n");
7568 sCallMouseHook
= SetWindowsHookEx(WH_MOUSE
, MozSpecialMouseProc
, nullptr,
7569 GetCurrentThreadId());
7570 #ifdef POPUP_ROLLUP_DEBUG_OUTPUT
7571 if (!sCallMouseHook
) {
7572 MOZ_LOG(gWindowsLog
, LogLevel::Info
,
7573 ("***** SetWindowsHookEx is NOT installed for WH_MOUSE!\n"));
7579 // Unhook special message hooks for dropdowns.
7580 void nsWindow::UnregisterSpecialDropdownHooks() {
7582 "***************** De-installing Msg Hooks ***************\n");
7584 if (sCallProcHook
) {
7585 DISPLAY_NMM_PRT("***** Unhooking sCallProcHook!\n");
7586 if (!::UnhookWindowsHookEx(sCallProcHook
)) {
7587 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sCallProcHook!\n");
7589 sCallProcHook
= nullptr;
7592 if (sMsgFilterHook
) {
7593 DISPLAY_NMM_PRT("***** Unhooking sMsgFilterHook!\n");
7594 if (!::UnhookWindowsHookEx(sMsgFilterHook
)) {
7595 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sMsgFilterHook!\n");
7597 sMsgFilterHook
= nullptr;
7600 if (sCallMouseHook
) {
7601 DISPLAY_NMM_PRT("***** Unhooking sCallMouseHook!\n");
7602 if (!::UnhookWindowsHookEx(sCallMouseHook
)) {
7603 DISPLAY_NMM_PRT("***** UnhookWindowsHookEx failed for sCallMouseHook!\n");
7605 sCallMouseHook
= nullptr;
7609 // This timer is designed to only fire one time at most each time a "hook"
7610 // function is used to rollup the dropdown. In some cases, the timer may be
7611 // scheduled from the hook, but that hook event or a subsequent event may roll
7612 // up the dropdown before this timer function is executed.
7614 // For example, if an MFC control takes focus, the combobox will lose focus and
7615 // rollup before this function fires.
7616 VOID CALLBACK
nsWindow::HookTimerForPopups(HWND hwnd
, UINT uMsg
, UINT idEvent
,
7618 if (sHookTimerId
!= 0) {
7619 // if the window is nullptr then we need to use the ID to kill the timer
7620 DebugOnly
<BOOL
> status
= ::KillTimer(nullptr, sHookTimerId
);
7621 NS_ASSERTION(status
, "Hook Timer was not killed.");
7625 if (sRollupMsgId
!= 0) {
7626 // Note: DealWithPopups does the check to make sure that the rollup widget
7628 LRESULT popupHandlingResult
;
7629 nsAutoRollup autoRollup
;
7630 DealWithPopups(sRollupMsgWnd
, sRollupMsgId
, 0, 0, &popupHandlingResult
);
7632 sRollupMsgWnd
= nullptr;
7636 static bool IsDifferentThreadWindow(HWND aWnd
) {
7637 return ::GetCurrentThreadId() != ::GetWindowThreadProcessId(aWnd
, nullptr);
7641 bool nsWindow::EventIsInsideWindow(nsWindow
* aWindow
,
7642 Maybe
<POINT
> aEventPoint
) {
7644 ::GetWindowRect(aWindow
->mWnd
, &r
);
7649 DWORD pos
= ::GetMessagePos();
7650 mp
.x
= GET_X_LPARAM(pos
);
7651 mp
.y
= GET_Y_LPARAM(pos
);
7654 auto margin
= aWindow
->mInputRegion
.mMargin
;
7662 // was the event inside this window?
7663 return static_cast<bool>(::PtInRect(&r
, mp
));
7667 bool nsWindow::GetPopupsToRollup(nsIRollupListener
* aRollupListener
,
7668 uint32_t* aPopupsToRollup
,
7669 Maybe
<POINT
> aEventPoint
) {
7670 // If we're dealing with menus, we probably have submenus and we don't want
7671 // to rollup some of them if the click is in a parent menu of the current
7673 *aPopupsToRollup
= UINT32_MAX
;
7674 AutoTArray
<nsIWidget
*, 5> widgetChain
;
7675 uint32_t sameTypeCount
= aRollupListener
->GetSubmenuWidgetChain(&widgetChain
);
7676 for (uint32_t i
= 0; i
< widgetChain
.Length(); ++i
) {
7677 nsIWidget
* widget
= widgetChain
[i
];
7678 if (EventIsInsideWindow(static_cast<nsWindow
*>(widget
), aEventPoint
)) {
7679 // Don't roll up if the mouse event occurred within a menu of the
7680 // same type. If the mouse event occurred in a menu higher than that,
7681 // roll up, but pass the number of popups to Rollup so that only those
7682 // of the same type close up.
7683 if (i
< sameTypeCount
) {
7687 *aPopupsToRollup
= sameTypeCount
;
7695 bool nsWindow::NeedsToHandleNCActivateDelayed(HWND aWnd
) {
7696 // While popup is open, popup window might be activated by other application.
7697 // At this time, we need to take back focus to the previous window but it
7698 // causes flickering its nonclient area because WM_NCACTIVATE comes before
7699 // WM_ACTIVATE and we cannot know which window will take focus at receiving
7700 // WM_NCACTIVATE. Therefore, we need a hack for preventing the flickerling.
7702 // If non-popup window receives WM_NCACTIVATE at deactivating, default
7703 // wndproc shouldn't handle it as deactivating. Instead, at receiving
7704 // WM_ACTIVIATE after that, WM_NCACTIVATE should be sent again manually.
7705 // This returns true if the window needs to handle WM_NCACTIVATE later.
7707 nsWindow
* window
= WinUtils::GetNSWindowPtr(aWnd
);
7708 return window
&& !window
->IsPopup();
7711 static bool IsTouchSupportEnabled(HWND aWnd
) {
7712 nsWindow
* topWindow
=
7713 WinUtils::GetNSWindowPtr(WinUtils::GetTopLevelHWND(aWnd
, true));
7714 return topWindow
? topWindow
->IsTouchWindow() : false;
7717 static Maybe
<POINT
> GetSingleTouch(WPARAM wParam
, LPARAM lParam
) {
7719 uint32_t cInputs
= LOWORD(wParam
);
7724 if (GetTouchInputInfo((HTOUCHINPUT
)lParam
, cInputs
, &input
,
7725 sizeof(TOUCHINPUT
))) {
7727 ret
->x
= TOUCH_COORD_TO_PIXEL(input
.x
);
7728 ret
->y
= TOUCH_COORD_TO_PIXEL(input
.y
);
7730 // Note that we don't call CloseTouchInputHandle here because we need
7731 // to read the touch input info again in OnTouch later.
7736 bool nsWindow::DealWithPopups(HWND aWnd
, UINT aMessage
, WPARAM aWParam
,
7737 LPARAM aLParam
, LRESULT
* aResult
) {
7738 NS_ASSERTION(aResult
, "Bad outResult");
7740 // XXX Why do we use the return value of WM_MOUSEACTIVATE for all messages?
7741 *aResult
= MA_NOACTIVATE
;
7743 if (!::IsWindowVisible(aWnd
)) {
7747 if (MOZ_UNLIKELY(aMessage
== WM_KILLFOCUS
)) {
7748 // NOTE: We deal with this here rather than on the switch below because we
7749 // want to do this even if there are no menus to rollup (tooltips don't set
7750 // the rollup listener etc).
7751 if (RefPtr pm
= nsXULPopupManager::GetInstance()) {
7752 pm
->RollupTooltips();
7756 nsIRollupListener
* rollupListener
= nsBaseWidget::GetActiveRollupListener();
7757 NS_ENSURE_TRUE(rollupListener
, false);
7759 nsCOMPtr
<nsIWidget
> popup
= rollupListener
->GetRollupWidget();
7764 static bool sSendingNCACTIVATE
= false;
7765 static bool sPendingNCACTIVATE
= false;
7766 uint32_t popupsToRollup
= UINT32_MAX
;
7768 bool consumeRollupEvent
= false;
7769 Maybe
<POINT
> touchPoint
; // In screen coords.
7771 // If we rollup with animations but get occluded right away, we might not
7772 // advance the refresh driver enough for the animation to finish.
7773 auto allowAnimations
= nsIRollupListener::AllowAnimations::Yes
;
7774 nsWindow
* popupWindow
= static_cast<nsWindow
*>(popup
.get());
7775 UINT nativeMessage
= WinUtils::GetNativeMessage(aMessage
);
7776 switch (nativeMessage
) {
7778 if (!IsTouchSupportEnabled(aWnd
)) {
7779 // If APZ is disabled, don't allow touch inputs to dismiss popups. The
7780 // compatibility mouse events will do it instead.
7783 touchPoint
= GetSingleTouch(aWParam
, aLParam
);
7788 case WM_LBUTTONDOWN
:
7789 case WM_RBUTTONDOWN
:
7790 case WM_MBUTTONDOWN
:
7791 case WM_NCLBUTTONDOWN
:
7792 case WM_NCRBUTTONDOWN
:
7793 case WM_NCMBUTTONDOWN
:
7794 if (nativeMessage
!= WM_TOUCH
&& IsTouchSupportEnabled(aWnd
) &&
7795 MOUSE_INPUT_SOURCE() == MouseEvent_Binding::MOZ_SOURCE_TOUCH
) {
7796 // If any of these mouse events are really compatibility events that
7797 // Windows is sending for touch inputs, then don't allow them to dismiss
7798 // popups when APZ is enabled (instead we do the dismissing as part of
7799 // WM_TOUCH handling which is more correct).
7800 // If we don't do this, then when the user lifts their finger after a
7801 // long-press, the WM_RBUTTONDOWN compatibility event that Windows sends
7802 // us will dismiss the contextmenu popup that we displayed as part of
7803 // handling the long-tap-up.
7806 if (!EventIsInsideWindow(popupWindow
, touchPoint
) &&
7807 GetPopupsToRollup(rollupListener
, &popupsToRollup
, touchPoint
)) {
7811 case WM_POINTERDOWN
: {
7812 WinPointerEvents pointerEvents
;
7813 if (!pointerEvents
.ShouldRollupOnPointerEvent(nativeMessage
, aWParam
)) {
7817 pt
.x
= GET_X_LPARAM(aLParam
);
7818 pt
.y
= GET_Y_LPARAM(aLParam
);
7819 if (!GetPopupsToRollup(rollupListener
, &popupsToRollup
, Some(pt
))) {
7822 if (EventIsInsideWindow(popupWindow
, Some(pt
))) {
7823 // Don't roll up if the event is inside the popup window.
7827 case MOZ_WM_DMANIP
: {
7829 ::GetCursorPos(&pt
);
7830 if (!GetPopupsToRollup(rollupListener
, &popupsToRollup
, Some(pt
))) {
7833 if (EventIsInsideWindow(popupWindow
, Some(pt
))) {
7834 // Don't roll up if the event is inside the popup window
7839 case WM_MOUSEHWHEEL
:
7840 // We need to check if the popup thinks that it should cause closing
7841 // itself when mouse wheel events are fired outside the rollup widget.
7842 if (!EventIsInsideWindow(popupWindow
)) {
7843 // Check if we should consume this event even if we don't roll-up:
7844 consumeRollupEvent
= rollupListener
->ShouldConsumeOnMouseWheelEvent();
7845 *aResult
= MA_ACTIVATE
;
7846 if (rollupListener
->ShouldRollupOnMouseWheelEvent() &&
7847 GetPopupsToRollup(rollupListener
, &popupsToRollup
)) {
7851 return consumeRollupEvent
;
7853 case WM_ACTIVATEAPP
:
7854 allowAnimations
= nsIRollupListener::AllowAnimations::No
;
7858 WndProcUrgentInvocation::Marker _marker
;
7860 // NOTE: Don't handle WA_INACTIVE for preventing popup taking focus
7861 // because we cannot distinguish it's caused by mouse or not.
7862 if (LOWORD(aWParam
) == WA_ACTIVE
&& aLParam
) {
7863 nsWindow
* window
= WinUtils::GetNSWindowPtr(aWnd
);
7864 if (window
&& (window
->IsPopup() || window
->mIsAlert
)) {
7865 // Cancel notifying widget listeners of deactivating the previous
7866 // active window (see WM_KILLFOCUS case in ProcessMessage()).
7867 sJustGotDeactivate
= false;
7868 // Reactivate the window later.
7869 ::PostMessageW(aWnd
, MOZ_WM_REACTIVATE
, aWParam
, aLParam
);
7872 // Don't rollup the popup when focus moves back to the parent window
7873 // from a popup because such case is caused by strange mouse drivers.
7874 nsWindow
* prevWindow
=
7875 WinUtils::GetNSWindowPtr(reinterpret_cast<HWND
>(aLParam
));
7876 if (prevWindow
&& prevWindow
->IsPopup()) {
7877 // Consume this message here since previous window must not have
7878 // been inactivated since we've already stopped accepting the
7879 // inactivation below.
7882 } else if (LOWORD(aWParam
) == WA_INACTIVE
) {
7883 nsWindow
* activeWindow
=
7884 WinUtils::GetNSWindowPtr(reinterpret_cast<HWND
>(aLParam
));
7885 if (sPendingNCACTIVATE
&& NeedsToHandleNCActivateDelayed(aWnd
)) {
7886 // If focus moves to non-popup widget or focusable popup, the window
7887 // needs to update its nonclient area.
7888 if (!activeWindow
|| !activeWindow
->IsPopup()) {
7889 sSendingNCACTIVATE
= true;
7890 ::SendMessageW(aWnd
, WM_NCACTIVATE
, false, 0);
7891 sSendingNCACTIVATE
= false;
7893 sPendingNCACTIVATE
= false;
7895 // If focus moves from/to popup, we don't need to rollup the popup
7896 // because such case is caused by strange mouse drivers. And in
7897 // such case, we should consume the message here since we need to
7898 // hide this odd focus move from our content. (If we didn't consume
7899 // the message here, ProcessMessage() will notify widget listener of
7900 // inactivation and that causes unnecessary reflow for supporting
7901 // -moz-window-inactive pseudo class.
7903 if (activeWindow
->IsPopup()) {
7906 nsWindow
* deactiveWindow
= WinUtils::GetNSWindowPtr(aWnd
);
7907 if (deactiveWindow
&& deactiveWindow
->IsPopup()) {
7911 } else if (LOWORD(aWParam
) == WA_CLICKACTIVE
) {
7912 // If the WM_ACTIVATE message is caused by a click in a popup,
7913 // we should not rollup any popups.
7914 nsWindow
* window
= WinUtils::GetNSWindowPtr(aWnd
);
7915 if ((window
&& window
->IsPopup()) ||
7916 !GetPopupsToRollup(rollupListener
, &popupsToRollup
)) {
7920 allowAnimations
= nsIRollupListener::AllowAnimations::No
;
7923 case MOZ_WM_REACTIVATE
:
7924 // The previous active window should take back focus.
7925 if (::IsWindow(reinterpret_cast<HWND
>(aLParam
))) {
7926 // FYI: Even without this API call, you see expected result (e.g., the
7927 // owner window of the popup keeps active without flickering
7928 // the non-client area). And also this causes initializing
7929 // TSF and it causes using CPU time a lot. However, even if we
7930 // consume WM_ACTIVE messages, native focus change has already
7931 // been occurred. I.e., a popup window is active now. Therefore,
7932 // you'll see some odd behavior if we don't reactivate the owner
7933 // window here. For example, if you do:
7934 // 1. Turn wheel on a bookmark panel.
7935 // 2. Turn wheel on another window.
7936 // then, you'll see that the another window becomes active but the
7937 // owner window of the bookmark panel looks still active and the
7938 // bookmark panel keeps open. The reason is that the first wheel
7939 // operation gives focus to the bookmark panel. Therefore, when
7940 // the next operation gives focus to the another window, previous
7941 // focus window is the bookmark panel (i.e., a popup window).
7942 // So, in this case, our hack around here prevents to inactivate
7943 // the owner window and roll up the bookmark panel.
7944 ::SetForegroundWindow(reinterpret_cast<HWND
>(aLParam
));
7949 if (!aWParam
&& !sSendingNCACTIVATE
&&
7950 NeedsToHandleNCActivateDelayed(aWnd
)) {
7951 // Don't just consume WM_NCACTIVATE. It doesn't handle only the
7952 // nonclient area state change.
7953 ::DefWindowProcW(aWnd
, aMessage
, TRUE
, aLParam
);
7954 // Accept the deactivating because it's necessary to receive following
7957 sPendingNCACTIVATE
= true;
7962 case WM_MOUSEACTIVATE
:
7963 if (!EventIsInsideWindow(popupWindow
) &&
7964 GetPopupsToRollup(rollupListener
, &popupsToRollup
)) {
7965 // WM_MOUSEACTIVATE may be caused by moving the mouse (e.g., X-mouse
7966 // of TweakUI is enabled. Then, check if the popup should be rolled up
7967 // with rollup listener. If not, just consume the message.
7968 if (HIWORD(aLParam
) == WM_MOUSEMOVE
&&
7969 !rollupListener
->ShouldRollupOnMouseActivate()) {
7972 // Otherwise, it should be handled by wndproc.
7976 // Prevent the click inside the popup from causing a change in window
7977 // activation. Since the popup is shown non-activated, we need to eat any
7978 // requests to activate the window while it is displayed. Windows will
7979 // automatically activate the popup on the mousedown otherwise.
7983 // If the window is being minimized, close popups.
7984 if (aLParam
== SW_PARENTCLOSING
) {
7985 allowAnimations
= nsIRollupListener::AllowAnimations::No
;
7991 // If focus moves to other window created in different process/thread,
7992 // e.g., a plugin window, popups should be rolled up.
7993 if (IsDifferentThreadWindow(reinterpret_cast<HWND
>(aWParam
))) {
7994 allowAnimations
= nsIRollupListener::AllowAnimations::No
;
8007 // Only need to deal with the last rollup for left mouse down events.
8008 NS_ASSERTION(!nsAutoRollup::GetLastRollup(), "last rollup is null");
8010 nsIRollupListener::RollupOptions rollupOptions
{
8012 nsIRollupListener::FlushViews::Yes
,
8013 /* mPoint = */ nullptr,
8017 if (nativeMessage
== WM_TOUCH
|| nativeMessage
== WM_LBUTTONDOWN
||
8018 nativeMessage
== WM_POINTERDOWN
) {
8019 LayoutDeviceIntPoint pos
;
8020 if (nativeMessage
== WM_TOUCH
) {
8021 pos
.x
= touchPoint
->x
;
8022 pos
.y
= touchPoint
->y
;
8025 pt
.x
= GET_X_LPARAM(aLParam
);
8026 pt
.y
= GET_Y_LPARAM(aLParam
);
8027 // POINTERDOWN is already in screen coords.
8028 if (nativeMessage
== WM_LBUTTONDOWN
) {
8029 ::ClientToScreen(aWnd
, &pt
);
8031 pos
= LayoutDeviceIntPoint(pt
.x
, pt
.y
);
8034 rollupOptions
.mPoint
= &pos
;
8035 nsIContent
* lastRollup
= nullptr;
8036 consumeRollupEvent
= rollupListener
->Rollup(rollupOptions
, &lastRollup
);
8037 nsAutoRollup::SetLastRollup(lastRollup
);
8039 consumeRollupEvent
= rollupListener
->Rollup(rollupOptions
);
8042 // Tell hook to stop processing messages
8043 sProcessHook
= false;
8045 sRollupMsgWnd
= nullptr;
8047 // If we are NOT supposed to be consuming events, let it go through
8048 if (consumeRollupEvent
&& nativeMessage
!= WM_RBUTTONDOWN
) {
8049 *aResult
= MA_ACTIVATE
;
8056 /**************************************************************
8057 **************************************************************
8059 ** BLOCK: Misc. utility methods and functions.
8063 **************************************************************
8064 **************************************************************/
8066 // Note that the result of GetTopLevelWindow method can be different from the
8067 // result of WinUtils::GetTopLevelHWND(). The result can be non-floating
8068 // window. Because our top level window may be contained in another window
8069 // which is not managed by us.
8070 nsWindow
* nsWindow::GetTopLevelWindow(bool aStopOnDialogOrPopup
) {
8071 nsWindow
* curWindow
= this;
8074 if (aStopOnDialogOrPopup
) {
8075 switch (curWindow
->mWindowType
) {
8076 case WindowType::Dialog
:
8077 case WindowType::Popup
:
8084 // Retrieve the top level parent or owner window
8085 nsWindow
* parentWindow
= curWindow
->GetParentWindow(true);
8087 if (!parentWindow
) return curWindow
;
8089 curWindow
= parentWindow
;
8093 // Set a flag if hwnd is a (non-popup) visible window from this process,
8094 // and bail out of the enumeration. Otherwise leave the flag unmodified
8095 // and continue the enumeration.
8096 // lParam must be a bool* pointing at the flag to be set.
8097 static BOOL CALLBACK
EnumVisibleWindowsProc(HWND hwnd
, LPARAM lParam
) {
8099 ::GetWindowThreadProcessId(hwnd
, &pid
);
8100 if (pid
== ::GetCurrentProcessId() && ::IsWindowVisible(hwnd
)) {
8101 // Don't count popups as visible windows, since they don't take focus,
8102 // in case we only have a popup visible (see bug 1554490 where the gfx
8103 // test window is an offscreen popup).
8104 nsWindow
* window
= WinUtils::GetNSWindowPtr(hwnd
);
8105 if (!window
|| !window
->IsPopup()) {
8106 bool* windowsVisible
= reinterpret_cast<bool*>(lParam
);
8107 *windowsVisible
= true;
8114 // Determine if it would be ok to activate a window, taking focus.
8115 // We want to avoid stealing focus from another app (bug 225305).
8116 bool nsWindow::CanTakeFocus() {
8117 HWND fgWnd
= ::GetForegroundWindow();
8119 // There is no foreground window, so don't worry about stealing focus.
8122 // We can take focus if the current foreground window is already from
8125 ::GetWindowThreadProcessId(fgWnd
, &pid
);
8126 if (pid
== ::GetCurrentProcessId()) {
8130 bool windowsVisible
= false;
8131 ::EnumWindows(EnumVisibleWindowsProc
,
8132 reinterpret_cast<LPARAM
>(&windowsVisible
));
8134 if (!windowsVisible
) {
8135 // We're probably creating our first visible window, allow that to
8142 /* static */ const wchar_t* nsWindow::GetMainWindowClass() {
8143 static const wchar_t* sMainWindowClass
= nullptr;
8144 if (!sMainWindowClass
) {
8145 nsAutoString className
;
8146 Preferences::GetString("ui.window_class_override", className
);
8147 if (!className
.IsEmpty()) {
8148 sMainWindowClass
= wcsdup(className
.get());
8150 sMainWindowClass
= kClassNameGeneral
;
8153 return sMainWindowClass
;
8156 LPARAM
nsWindow::lParamToScreen(LPARAM lParam
) {
8158 pt
.x
= GET_X_LPARAM(lParam
);
8159 pt
.y
= GET_Y_LPARAM(lParam
);
8160 ::ClientToScreen(mWnd
, &pt
);
8161 return MAKELPARAM(pt
.x
, pt
.y
);
8164 LPARAM
nsWindow::lParamToClient(LPARAM lParam
) {
8166 pt
.x
= GET_X_LPARAM(lParam
);
8167 pt
.y
= GET_Y_LPARAM(lParam
);
8168 ::ScreenToClient(mWnd
, &pt
);
8169 return MAKELPARAM(pt
.x
, pt
.y
);
8172 WPARAM
nsWindow::wParamFromGlobalMouseState() {
8175 if (!!::GetKeyState(VK_CONTROL
)) {
8176 result
|= MK_CONTROL
;
8179 if (!!::GetKeyState(VK_SHIFT
)) {
8183 if (!!::GetKeyState(VK_LBUTTON
)) {
8184 result
|= MK_LBUTTON
;
8187 if (!!::GetKeyState(VK_MBUTTON
)) {
8188 result
|= MK_MBUTTON
;
8191 if (!!::GetKeyState(VK_RBUTTON
)) {
8192 result
|= MK_RBUTTON
;
8195 if (!!::GetKeyState(VK_XBUTTON1
)) {
8196 result
|= MK_XBUTTON1
;
8199 if (!!::GetKeyState(VK_XBUTTON2
)) {
8200 result
|= MK_XBUTTON2
;
8206 void nsWindow::PickerOpen() {
8207 AssertIsOnMainThread();
8208 mPickerDisplayCount
++;
8211 void nsWindow::PickerClosed() {
8212 AssertIsOnMainThread();
8213 NS_ASSERTION(mPickerDisplayCount
> 0, "mPickerDisplayCount out of sync!");
8214 if (!mPickerDisplayCount
) return;
8215 mPickerDisplayCount
--;
8217 // WORKAROUND FOR UNDOCUMENTED BEHAVIOR: `IFileDialog::Show` disables the
8218 // top-level ancestor of its provided owner-window. If the modal window's
8219 // container process crashes, it will never get a chance to undo that, so we
8220 // do it manually here.
8222 // Note that this may cause problems in the embedded case if you reparent a
8223 // subtree of the native window hierarchy containing a Gecko window while that
8224 // Gecko window has a file-dialog open.
8225 if (!mPickerDisplayCount
) {
8226 ::EnableWindow(::GetAncestor(GetWindowHandle(), GA_ROOT
), TRUE
);
8229 if (!mPickerDisplayCount
&& mDestroyCalled
) {
8234 bool nsWindow::WidgetTypeSupportsAcceleration() {
8235 // We don't currently support using an accelerated layer manager with
8236 // transparent windows so don't even try. I'm also not sure if we even
8237 // want to support this case. See bug 593471.
8239 // Windows' support for transparent accelerated surfaces isn't great.
8240 // Some possible approaches:
8241 // - Readback the data and update it using
8242 // UpdateLayeredWindow/UpdateLayeredWindowIndirect
8243 // This is what WPF does. See
8244 // CD3DDeviceLevel1::PresentWithGDI/CD3DSwapChainWithSwDC in WpfGfx. The
8245 // rationale for not using IDirect3DSurface9::GetDC is explained here:
8246 // https://web.archive.org/web/20160521191104/https://blogs.msdn.microsoft.com/dwayneneed/2008/09/08/transparent-windows-in-wpf/
8247 // - Use D3D11_RESOURCE_MISC_GDI_COMPATIBLE, IDXGISurface1::GetDC(),
8248 // and UpdateLayeredWindowIndirect.
8249 // This is suggested here:
8250 // https://docs.microsoft.com/en-us/archive/msdn-magazine/2009/december/windows-with-c-layered-windows-with-direct2d
8251 // but might have the same problem that IDirect3DSurface9::GetDC has.
8252 // - Creating the window with the WS_EX_NOREDIRECTIONBITMAP flag and use
8253 // DirectComposition.
8254 // Not supported on Win7.
8255 // - Using DwmExtendFrameIntoClientArea with negative margins and something
8256 // to turn off the glass effect.
8257 // This doesn't work when the DWM is not running (Win7)
8259 // Also see bug 1150376, D3D11 composition can cause issues on some devices
8260 // on Windows 7 where presentation fails randomly for windows with drop
8262 return mTransparencyMode
!= TransparencyMode::Transparent
&&
8263 !(IsPopup() && DeviceManagerDx::Get()->IsWARP());
8266 bool nsWindow::DispatchTouchEventFromWMPointer(
8267 UINT msg
, LPARAM aLParam
, const WinPointerInfo
& aPointerInfo
,
8268 mozilla::MouseButton aButton
) {
8269 MultiTouchInput::MultiTouchType touchType
;
8271 case WM_POINTERDOWN
:
8272 touchType
= MultiTouchInput::MULTITOUCH_START
;
8274 case WM_POINTERUPDATE
:
8275 if (aPointerInfo
.mPressure
== 0) {
8276 return false; // hover
8278 touchType
= MultiTouchInput::MULTITOUCH_MOVE
;
8281 touchType
= MultiTouchInput::MULTITOUCH_END
;
8287 nsPointWin touchPoint
;
8288 touchPoint
.x
= GET_X_LPARAM(aLParam
);
8289 touchPoint
.y
= GET_Y_LPARAM(aLParam
);
8290 touchPoint
.ScreenToClient(mWnd
);
8292 SingleTouchData
touchData(static_cast<int32_t>(aPointerInfo
.pointerId
),
8293 ScreenIntPoint::FromUnknownPoint(touchPoint
),
8294 ScreenSize(1, 1), // pixel size radius for pen
8295 0.0f
, // no radius rotation
8296 aPointerInfo
.mPressure
);
8297 touchData
.mTiltX
= aPointerInfo
.tiltX
;
8298 touchData
.mTiltY
= aPointerInfo
.tiltY
;
8299 touchData
.mTwist
= aPointerInfo
.twist
;
8301 MultiTouchInput touchInput
;
8302 touchInput
.mType
= touchType
;
8303 touchInput
.mTimeStamp
= GetMessageTimeStamp(::GetMessageTime());
8304 touchInput
.mTouches
.AppendElement(touchData
);
8305 touchInput
.mButton
= aButton
;
8306 touchInput
.mButtons
= aPointerInfo
.mButtons
;
8308 // POINTER_INFO.dwKeyStates can't be used as it only supports Shift and Ctrl
8309 ModifierKeyState modifierKeyState
;
8310 touchInput
.modifiers
= modifierKeyState
.GetModifiers();
8312 DispatchTouchInput(touchInput
, MouseEvent_Binding::MOZ_SOURCE_PEN
);
8316 static MouseButton
PenFlagsToMouseButton(PEN_FLAGS aPenFlags
) {
8317 // Theoretically flags can be set together but they do not
8318 if (aPenFlags
& PEN_FLAG_BARREL
) {
8319 return MouseButton::eSecondary
;
8321 if (aPenFlags
& PEN_FLAG_ERASER
) {
8322 return MouseButton::eEraser
;
8324 return MouseButton::ePrimary
;
8327 bool nsWindow::OnPointerEvents(UINT msg
, WPARAM aWParam
, LPARAM aLParam
) {
8329 // APZ is not available on context menu. Follow the behavior of touch input
8330 // which fallbacks to WM_LBUTTON* and WM_GESTURE, to keep consistency.
8333 if (!mPointerEvents
.ShouldHandleWinPointerMessages(msg
, aWParam
)) {
8336 if (!mPointerEvents
.ShouldFirePointerEventByWinPointerMessages()) {
8337 // We have to handle WM_POINTER* to fetch and cache pen related information
8338 // and fire WidgetMouseEvent with the cached information the WM_*BUTTONDOWN
8339 // handler. This is because Windows doesn't support ::DoDragDrop in the
8340 // touch or pen message handlers.
8341 mPointerEvents
.ConvertAndCachePointerInfo(msg
, aWParam
);
8342 // Don't consume the Windows WM_POINTER* messages
8346 uint32_t pointerId
= mPointerEvents
.GetPointerId(aWParam
);
8347 POINTER_PEN_INFO penInfo
{};
8348 if (!mPointerEvents
.GetPointerPenInfo(pointerId
, &penInfo
)) {
8352 // When dispatching mouse events with pen, there may be some
8353 // WM_POINTERUPDATE messages between WM_POINTERDOWN and WM_POINTERUP with
8354 // small movements. Those events will reset sLastMousePoint and reset
8355 // sLastClickCount. To prevent that, we keep the last pen down position
8356 // and compare it with the subsequent WM_POINTERUPDATE. If the movement is
8357 // smaller than GetSystemMetrics(SM_CXDRAG), then we suppress firing
8358 // eMouseMove for WM_POINTERUPDATE.
8359 static POINT sLastPointerDownPoint
= {0};
8361 // We don't support chorded buttons for pen. Keep the button at
8363 static mozilla::MouseButton sLastPenDownButton
= MouseButton::ePrimary
;
8364 static bool sPointerDown
= false;
8366 EventMessage message
;
8367 mozilla::MouseButton button
= MouseButton::ePrimary
;
8369 case WM_POINTERDOWN
: {
8370 LayoutDeviceIntPoint
eventPoint(GET_X_LPARAM(aLParam
),
8371 GET_Y_LPARAM(aLParam
));
8372 sLastPointerDownPoint
.x
= eventPoint
.x
;
8373 sLastPointerDownPoint
.y
= eventPoint
.y
;
8374 message
= eMouseDown
;
8375 button
= PenFlagsToMouseButton(penInfo
.penFlags
);
8376 sLastPenDownButton
= button
;
8377 sPointerDown
= true;
8381 MOZ_ASSERT(sPointerDown
, "receive WM_POINTERUP w/o WM_POINTERDOWN");
8382 button
= sPointerDown
? sLastPenDownButton
: MouseButton::ePrimary
;
8383 sPointerDown
= false;
8385 case WM_POINTERUPDATE
:
8386 message
= eMouseMove
;
8388 LayoutDeviceIntPoint
eventPoint(GET_X_LPARAM(aLParam
),
8389 GET_Y_LPARAM(aLParam
));
8390 int32_t movementX
= sLastPointerDownPoint
.x
> eventPoint
.x
8391 ? sLastPointerDownPoint
.x
- eventPoint
.x
.value
8392 : eventPoint
.x
.value
- sLastPointerDownPoint
.x
;
8393 int32_t movementY
= sLastPointerDownPoint
.y
> eventPoint
.y
8394 ? sLastPointerDownPoint
.y
- eventPoint
.y
.value
8395 : eventPoint
.y
.value
- sLastPointerDownPoint
.y
;
8396 bool insideMovementThreshold
=
8397 movementX
< (int32_t)::GetSystemMetrics(SM_CXDRAG
) &&
8398 movementY
< (int32_t)::GetSystemMetrics(SM_CYDRAG
);
8400 if (insideMovementThreshold
) {
8401 // Suppress firing eMouseMove for WM_POINTERUPDATE if the movement
8402 // from last WM_POINTERDOWN is smaller than SM_CXDRAG / SM_CYDRAG
8405 button
= sLastPenDownButton
;
8408 case WM_POINTERLEAVE
:
8409 message
= eMouseExitFromWidget
;
8415 // Windows defines the pen pressure is normalized to a range between 0 and
8416 // 1024. Convert it to float.
8417 float pressure
= penInfo
.pressure
? (float)penInfo
.pressure
/ 1024 : 0;
8418 int16_t buttons
= sPointerDown
8419 ? nsContentUtils::GetButtonsFlagForButton(button
)
8420 : MouseButtonsFlag::eNoButtons
;
8421 WinPointerInfo
pointerInfo(pointerId
, penInfo
.tiltX
, penInfo
.tiltY
, pressure
,
8424 // https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-pointer_pen_info,
8425 // the rotation is normalized in a range of 0 to 359.
8426 MOZ_ASSERT(penInfo
.rotation
<= 359);
8427 pointerInfo
.twist
= (int32_t)penInfo
.rotation
;
8429 // Fire touch events but not when the barrel button is pressed.
8430 if (button
!= MouseButton::eSecondary
&&
8431 StaticPrefs::dom_w3c_pointer_events_scroll_by_pen_enabled() &&
8432 DispatchTouchEventFromWMPointer(msg
, aLParam
, pointerInfo
, button
)) {
8436 // The aLParam of WM_POINTER* is the screen location. Convert it to client
8438 LPARAM newLParam
= lParamToClient(aLParam
);
8439 DispatchMouseEvent(message
, aWParam
, newLParam
, false, button
,
8440 MouseEvent_Binding::MOZ_SOURCE_PEN
, &pointerInfo
);
8442 if (button
== MouseButton::eSecondary
&& message
== eMouseUp
) {
8443 // Fire eContextMenu manually since consuming WM_POINTER* blocks
8445 DispatchMouseEvent(eContextMenu
, aWParam
, newLParam
, false, button
,
8446 MouseEvent_Binding::MOZ_SOURCE_PEN
, &pointerInfo
);
8448 // Consume WM_POINTER* to stop Windows fires WM_*BUTTONDOWN / WM_*BUTTONUP
8453 void nsWindow::GetCompositorWidgetInitData(
8454 mozilla::widget::CompositorWidgetInitData
* aInitData
) {
8455 *aInitData
= WinCompositorWidgetInitData(
8456 reinterpret_cast<uintptr_t>(mWnd
),
8457 reinterpret_cast<uintptr_t>(static_cast<nsIWidget
*>(this)),
8458 mTransparencyMode
, mFrameState
->GetSizeMode());
8461 bool nsWindow::SynchronouslyRepaintOnResize() { return false; }
8463 void nsWindow::MaybeDispatchInitialFocusEvent() {
8464 if (mIsShowingPreXULSkeletonUI
&& ::GetActiveWindow() == mWnd
) {
8465 DispatchFocusToTopLevelWindow(true);
8469 already_AddRefed
<nsIWidget
> nsIWidget::CreateTopLevelWindow() {
8470 nsCOMPtr
<nsIWidget
> window
= new nsWindow();
8471 return window
.forget();
8474 already_AddRefed
<nsIWidget
> nsIWidget::CreateChildWindow() {
8475 nsCOMPtr
<nsIWidget
> window
= new nsWindow(true);
8476 return window
.forget();
8480 bool nsWindow::InitTouchInjection() {
8481 if (!sTouchInjectInitialized
) {
8482 // Initialize touch injection on the first call
8483 HMODULE hMod
= LoadLibraryW(kUser32LibName
);
8488 InitializeTouchInjectionPtr func
=
8489 (InitializeTouchInjectionPtr
)GetProcAddress(hMod
,
8490 "InitializeTouchInjection");
8492 WinUtils::Log("InitializeTouchInjection not available.");
8496 if (!func(TOUCH_INJECT_MAX_POINTS
, TOUCH_FEEDBACK_DEFAULT
)) {
8497 WinUtils::Log("InitializeTouchInjection failure. GetLastError=%d",
8502 sInjectTouchFuncPtr
=
8503 (InjectTouchInputPtr
)GetProcAddress(hMod
, "InjectTouchInput");
8504 if (!sInjectTouchFuncPtr
) {
8505 WinUtils::Log("InjectTouchInput not available.");
8508 sTouchInjectInitialized
= true;
8513 bool nsWindow::InjectTouchPoint(uint32_t aId
, LayoutDeviceIntPoint
& aPoint
,
8514 POINTER_FLAGS aFlags
, uint32_t aPressure
,
8515 uint32_t aOrientation
) {
8516 if (aId
> TOUCH_INJECT_MAX_POINTS
) {
8517 WinUtils::Log("Pointer ID exceeds maximum. See TOUCH_INJECT_MAX_POINTS.");
8521 POINTER_TOUCH_INFO info
{};
8523 info
.touchFlags
= TOUCH_FLAG_NONE
;
8525 TOUCH_MASK_CONTACTAREA
| TOUCH_MASK_ORIENTATION
| TOUCH_MASK_PRESSURE
;
8526 info
.pressure
= aPressure
;
8527 info
.orientation
= aOrientation
;
8529 info
.pointerInfo
.pointerFlags
= aFlags
;
8530 info
.pointerInfo
.pointerType
= PT_TOUCH
;
8531 info
.pointerInfo
.pointerId
= aId
;
8532 info
.pointerInfo
.ptPixelLocation
.x
= aPoint
.x
;
8533 info
.pointerInfo
.ptPixelLocation
.y
= aPoint
.y
;
8535 info
.rcContact
.top
= info
.pointerInfo
.ptPixelLocation
.y
- 2;
8536 info
.rcContact
.bottom
= info
.pointerInfo
.ptPixelLocation
.y
+ 2;
8537 info
.rcContact
.left
= info
.pointerInfo
.ptPixelLocation
.x
- 2;
8538 info
.rcContact
.right
= info
.pointerInfo
.ptPixelLocation
.x
+ 2;
8540 for (int i
= 0; i
< 3; i
++) {
8541 if (sInjectTouchFuncPtr(1, &info
)) {
8544 DWORD error
= GetLastError();
8545 if (error
== ERROR_NOT_READY
&& i
< 2) {
8546 // We sent it too quickly after the previous injection (see bug 1535140
8547 // comment 10). On the first loop iteration we just yield (via Sleep(0))
8548 // and try again. If it happens again on the second loop iteration we
8549 // explicitly Sleep(1) and try again. If that doesn't work either we just
8554 WinUtils::Log("InjectTouchInput failure. GetLastError=%d", error
);
8560 void nsWindow::ChangedDPI() {
8561 if (mWidgetListener
) {
8562 if (PresShell
* presShell
= mWidgetListener
->GetPresShell()) {
8563 presShell
->BackingScaleFactorChanged();
8568 static Result
<POINTER_FLAGS
, nsresult
> PointerStateToFlag(
8569 nsWindow::TouchPointerState aPointerState
, bool isUpdate
) {
8570 bool hover
= aPointerState
& nsWindow::TOUCH_HOVER
;
8571 bool contact
= aPointerState
& nsWindow::TOUCH_CONTACT
;
8572 bool remove
= aPointerState
& nsWindow::TOUCH_REMOVE
;
8573 bool cancel
= aPointerState
& nsWindow::TOUCH_CANCEL
;
8575 POINTER_FLAGS flags
;
8577 // We know about this pointer, send an update
8578 flags
= POINTER_FLAG_UPDATE
;
8580 flags
|= POINTER_FLAG_INRANGE
;
8581 } else if (contact
) {
8582 flags
|= POINTER_FLAG_INCONTACT
| POINTER_FLAG_INRANGE
;
8583 } else if (remove
) {
8584 flags
= POINTER_FLAG_UP
;
8588 flags
|= POINTER_FLAG_CANCELED
;
8591 // Missing init state, error out
8592 if (remove
|| cancel
) {
8593 return Err(NS_ERROR_INVALID_ARG
);
8596 // Create a new pointer
8597 flags
= POINTER_FLAG_INRANGE
;
8599 flags
|= POINTER_FLAG_INCONTACT
| POINTER_FLAG_DOWN
;
8605 nsresult
nsWindow::SynthesizeNativeTouchPoint(
8606 uint32_t aPointerId
, nsIWidget::TouchPointerState aPointerState
,
8607 LayoutDeviceIntPoint aPoint
, double aPointerPressure
,
8608 uint32_t aPointerOrientation
, nsIObserver
* aObserver
) {
8609 AutoObserverNotifier
notifier(aObserver
, "touchpoint");
8611 if (StaticPrefs::apz_test_fails_with_native_injection() ||
8612 !InitTouchInjection()) {
8613 // If we don't have touch injection from the OS, or if we are running a test
8614 // that cannot properly inject events to satisfy the OS requirements (see
8615 // bug 1313170) we can just fake it and synthesize the events from here.
8616 MOZ_ASSERT(NS_IsMainThread());
8617 if (aPointerState
== TOUCH_HOVER
) {
8618 return NS_ERROR_UNEXPECTED
;
8621 if (!mSynthesizedTouchInput
) {
8622 mSynthesizedTouchInput
= MakeUnique
<MultiTouchInput
>();
8625 WidgetEventTime time
= CurrentMessageWidgetEventTime();
8626 LayoutDeviceIntPoint pointInWindow
= aPoint
- WidgetToScreenOffset();
8627 MultiTouchInput inputToDispatch
= UpdateSynthesizedTouchState(
8628 mSynthesizedTouchInput
.get(), time
.mTimeStamp
, aPointerId
,
8629 aPointerState
, pointInWindow
, aPointerPressure
, aPointerOrientation
);
8630 DispatchTouchInput(inputToDispatch
);
8634 // win api expects a value from 0 to 1024. aPointerPressure is a value
8636 uint32_t pressure
= (uint32_t)ceil(aPointerPressure
* 1024);
8638 // If we already know about this pointer id get it's record
8639 return mActivePointers
.WithEntryHandle(aPointerId
, [&](auto&& entry
) {
8640 POINTER_FLAGS flags
;
8641 // Can't use MOZ_TRY_VAR because it confuses WithEntryHandle
8642 auto result
= PointerStateToFlag(aPointerState
, !!entry
);
8643 if (result
.isOk()) {
8644 flags
= result
.unwrap();
8646 return result
.unwrapErr();
8650 entry
.Insert(MakeUnique
<PointerInfo
>(aPointerId
, aPoint
,
8651 PointerInfo::PointerType::TOUCH
));
8653 if (entry
.Data()->mType
!= PointerInfo::PointerType::TOUCH
) {
8654 return NS_ERROR_UNEXPECTED
;
8656 if (aPointerState
& TOUCH_REMOVE
) {
8657 // Remove the pointer from our tracking list. This is UniquePtr wrapped,
8658 // so shouldn't leak.
8663 return !InjectTouchPoint(aPointerId
, aPoint
, flags
, pressure
,
8664 aPointerOrientation
)
8665 ? NS_ERROR_UNEXPECTED
8670 nsresult
nsWindow::ClearNativeTouchSequence(nsIObserver
* aObserver
) {
8671 AutoObserverNotifier
notifier(aObserver
, "cleartouch");
8672 if (!sTouchInjectInitialized
) {
8676 // cancel all input points
8677 for (auto iter
= mActivePointers
.Iter(); !iter
.Done(); iter
.Next()) {
8678 auto* info
= iter
.UserData();
8679 if (info
->mType
!= PointerInfo::PointerType::TOUCH
) {
8682 InjectTouchPoint(info
->mPointerId
, info
->mPosition
, POINTER_FLAG_CANCELED
);
8686 nsBaseWidget::ClearNativeTouchSequence(nullptr);
8691 #if !defined(NTDDI_WIN10_RS5) || (NTDDI_VERSION < NTDDI_WIN10_RS5)
8692 static CreateSyntheticPointerDevicePtr CreateSyntheticPointerDevice
;
8693 static DestroySyntheticPointerDevicePtr DestroySyntheticPointerDevice
;
8694 static InjectSyntheticPointerInputPtr InjectSyntheticPointerInput
;
8696 static HSYNTHETICPOINTERDEVICE sSyntheticPenDevice
;
8698 static bool InitPenInjection() {
8699 if (sSyntheticPenDevice
) {
8702 #if !defined(NTDDI_WIN10_RS5) || (NTDDI_VERSION < NTDDI_WIN10_RS5)
8703 HMODULE hMod
= LoadLibraryW(kUser32LibName
);
8707 CreateSyntheticPointerDevice
=
8708 (CreateSyntheticPointerDevicePtr
)GetProcAddress(
8709 hMod
, "CreateSyntheticPointerDevice");
8710 if (!CreateSyntheticPointerDevice
) {
8711 WinUtils::Log("CreateSyntheticPointerDevice not available.");
8714 DestroySyntheticPointerDevice
=
8715 (DestroySyntheticPointerDevicePtr
)GetProcAddress(
8716 hMod
, "DestroySyntheticPointerDevice");
8717 if (!DestroySyntheticPointerDevice
) {
8718 WinUtils::Log("DestroySyntheticPointerDevice not available.");
8721 InjectSyntheticPointerInput
= (InjectSyntheticPointerInputPtr
)GetProcAddress(
8722 hMod
, "InjectSyntheticPointerInput");
8723 if (!InjectSyntheticPointerInput
) {
8724 WinUtils::Log("InjectSyntheticPointerInput not available.");
8728 sSyntheticPenDevice
=
8729 CreateSyntheticPointerDevice(PT_PEN
, 1, POINTER_FEEDBACK_DEFAULT
);
8730 return !!sSyntheticPenDevice
;
8733 nsresult
nsWindow::SynthesizeNativePenInput(
8734 uint32_t aPointerId
, nsIWidget::TouchPointerState aPointerState
,
8735 LayoutDeviceIntPoint aPoint
, double aPressure
, uint32_t aRotation
,
8736 int32_t aTiltX
, int32_t aTiltY
, int32_t aButton
, nsIObserver
* aObserver
) {
8737 AutoObserverNotifier
notifier(aObserver
, "peninput");
8738 if (!InitPenInjection()) {
8739 return NS_ERROR_UNEXPECTED
;
8742 // win api expects a value from 0 to 1024. aPointerPressure is a value
8744 uint32_t pressure
= (uint32_t)ceil(aPressure
* 1024);
8746 // If we already know about this pointer id get it's record
8747 return mActivePointers
.WithEntryHandle(aPointerId
, [&](auto&& entry
) {
8748 POINTER_FLAGS flags
;
8749 // Can't use MOZ_TRY_VAR because it confuses WithEntryHandle
8750 auto result
= PointerStateToFlag(aPointerState
, !!entry
);
8751 if (result
.isOk()) {
8752 flags
= result
.unwrap();
8754 return result
.unwrapErr();
8758 entry
.Insert(MakeUnique
<PointerInfo
>(aPointerId
, aPoint
,
8759 PointerInfo::PointerType::PEN
));
8761 if (entry
.Data()->mType
!= PointerInfo::PointerType::PEN
) {
8762 return NS_ERROR_UNEXPECTED
;
8764 if (aPointerState
& TOUCH_REMOVE
) {
8765 // Remove the pointer from our tracking list. This is UniquePtr wrapped,
8766 // so shouldn't leak.
8771 POINTER_TYPE_INFO info
{};
8774 info
.penInfo
.pointerInfo
.pointerType
= PT_PEN
;
8775 info
.penInfo
.pointerInfo
.pointerFlags
= flags
;
8776 info
.penInfo
.pointerInfo
.pointerId
= aPointerId
;
8777 info
.penInfo
.pointerInfo
.ptPixelLocation
.x
= aPoint
.x
;
8778 info
.penInfo
.pointerInfo
.ptPixelLocation
.y
= aPoint
.y
;
8780 info
.penInfo
.penFlags
= PEN_FLAG_NONE
;
8781 // PEN_FLAG_ERASER is not supported this way, unfortunately.
8783 info
.penInfo
.penFlags
|= PEN_FLAG_BARREL
;
8785 info
.penInfo
.penMask
= PEN_MASK_PRESSURE
| PEN_MASK_ROTATION
|
8786 PEN_MASK_TILT_X
| PEN_MASK_TILT_Y
;
8787 info
.penInfo
.pressure
= pressure
;
8788 info
.penInfo
.rotation
= aRotation
;
8789 info
.penInfo
.tiltX
= aTiltX
;
8790 info
.penInfo
.tiltY
= aTiltY
;
8792 return InjectSyntheticPointerInput(sSyntheticPenDevice
, &info
, 1)
8794 : NS_ERROR_UNEXPECTED
;
8798 bool nsWindow::HandleAppCommandMsg(const MSG
& aAppCommandMsg
,
8799 LRESULT
* aRetValue
) {
8800 ModifierKeyState modKeyState
;
8801 NativeKey
nativeKey(this, aAppCommandMsg
, modKeyState
);
8802 bool consumed
= nativeKey
.HandleAppCommandMessage();
8803 *aRetValue
= consumed
? 1 : 0;
8808 nsresult
nsWindow::SetHiDPIMode(bool aHiDPI
) {
8809 return WinUtils::SetHiDPIMode(aHiDPI
);
8812 nsresult
nsWindow::RestoreHiDPIMode() { return WinUtils::RestoreHiDPIMode(); }
8815 mozilla::Maybe
<UINT
> nsWindow::GetHiddenTaskbarEdge() {
8816 HMONITOR windowMonitor
= ::MonitorFromWindow(mWnd
, MONITOR_DEFAULTTONEAREST
);
8818 // Check all four sides of our monitor for an appbar. Skip any that aren't
8819 // the system taskbar.
8821 mi
.cbSize
= sizeof(MONITORINFO
);
8822 ::GetMonitorInfo(windowMonitor
, &mi
);
8824 APPBARDATA appBarData
;
8825 appBarData
.cbSize
= sizeof(appBarData
);
8826 appBarData
.rc
= mi
.rcMonitor
;
8827 const auto kEdges
= {ABE_BOTTOM
, ABE_TOP
, ABE_LEFT
, ABE_RIGHT
};
8828 for (auto edge
: kEdges
) {
8829 appBarData
.uEdge
= edge
;
8830 HWND appBarHwnd
= (HWND
)SHAppBarMessage(ABM_GETAUTOHIDEBAREX
, &appBarData
);
8832 nsAutoString className
;
8833 if (WinUtils::GetClassName(appBarHwnd
, className
)) {
8834 if (className
.Equals(L
"Shell_TrayWnd") ||
8835 className
.Equals(L
"Shell_SecondaryTrayWnd")) {
8845 static nsSizeMode
GetSizeModeForWindowFrame(HWND aWnd
, bool aFullscreenMode
) {
8847 pl
.length
= sizeof(pl
);
8848 ::GetWindowPlacement(aWnd
, &pl
);
8850 if (pl
.showCmd
== SW_SHOWMINIMIZED
) {
8851 return nsSizeMode_Minimized
;
8852 } else if (aFullscreenMode
) {
8853 return nsSizeMode_Fullscreen
;
8854 } else if (pl
.showCmd
== SW_SHOWMAXIMIZED
) {
8855 return nsSizeMode_Maximized
;
8857 return nsSizeMode_Normal
;
8861 static void ShowWindowWithMode(HWND aWnd
, nsSizeMode aMode
) {
8862 // This will likely cause a callback to
8863 // nsWindow::FrameState::{OnFrameChanging() and OnFrameChanged()}
8865 case nsSizeMode_Fullscreen
:
8866 ::ShowWindow(aWnd
, SW_SHOW
);
8869 case nsSizeMode_Maximized
:
8870 ::ShowWindow(aWnd
, SW_MAXIMIZE
);
8873 case nsSizeMode_Minimized
:
8874 ::ShowWindow(aWnd
, SW_MINIMIZE
);
8878 // Don't call ::ShowWindow if we're trying to "restore" a window that is
8879 // already in a normal state. Prevents a bug where snapping to one side
8880 // of the screen and then minimizing would cause Windows to forget our
8881 // window's correct restored position/size.
8882 if (GetCurrentShowCmd(aWnd
) != SW_SHOWNORMAL
) {
8883 ::ShowWindow(aWnd
, SW_RESTORE
);
8888 nsWindow::FrameState::FrameState(nsWindow
* aWindow
) : mWindow(aWindow
) {}
8890 nsSizeMode
nsWindow::FrameState::GetSizeMode() const { return mSizeMode
; }
8892 void nsWindow::FrameState::CheckInvariant() const {
8893 MOZ_ASSERT(mSizeMode
>= 0 && mSizeMode
< nsSizeMode_Invalid
);
8894 MOZ_ASSERT(mLastSizeMode
>= 0 && mLastSizeMode
< nsSizeMode_Invalid
);
8895 MOZ_ASSERT(mPreFullscreenSizeMode
>= 0 &&
8896 mPreFullscreenSizeMode
< nsSizeMode_Invalid
);
8897 MOZ_ASSERT(mWindow
);
8899 // We should never observe fullscreen sizemode unless fullscreen is enabled
8900 MOZ_ASSERT_IF(mSizeMode
== nsSizeMode_Fullscreen
, mFullscreenMode
);
8901 MOZ_ASSERT_IF(!mFullscreenMode
, mSizeMode
!= nsSizeMode_Fullscreen
);
8903 // Something went wrong if we somehow saved fullscreen mode when we are
8904 // changing into fullscreen mode
8905 MOZ_ASSERT(mPreFullscreenSizeMode
!= nsSizeMode_Fullscreen
);
8908 void nsWindow::FrameState::ConsumePreXULSkeletonState(bool aWasMaximized
) {
8909 mSizeMode
= aWasMaximized
? nsSizeMode_Maximized
: nsSizeMode_Normal
;
8912 void nsWindow::FrameState::EnsureSizeMode(nsSizeMode aMode
,
8913 DoShowWindow aDoShowWindow
) {
8914 if (mSizeMode
== aMode
) {
8918 if (StaticPrefs::widget_windows_fullscreen_remind_taskbar()) {
8919 // If we're unminimizing a window, asynchronously notify the taskbar after
8920 // the message has been processed. This redundant notification works around
8921 // a race condition in explorer.exe. (See bug 1835851, or comments in
8922 // TaskbarConcealer.)
8924 // Note that we notify regardless of `aMode`: unminimizing a non-fullscreen
8925 // window can also affect the correct taskbar state, yet fail to affect the
8926 // current taskbar state.
8927 if (mSizeMode
== nsSizeMode_Minimized
) {
8928 ::PostMessage(mWindow
->mWnd
, MOZ_WM_FULLSCREEN_STATE_UPDATE
, 0, 0);
8932 if (aMode
== nsSizeMode_Fullscreen
) {
8933 EnsureFullscreenMode(true, aDoShowWindow
);
8934 MOZ_ASSERT(mSizeMode
== nsSizeMode_Fullscreen
);
8935 } else if (mSizeMode
== nsSizeMode_Fullscreen
&& aMode
== nsSizeMode_Normal
) {
8936 // If we are in fullscreen mode, minimize should work like normal and
8937 // return us to fullscreen mode when unminimized. Maximize isn't really
8938 // available and won't do anything. "Restore" should do the same thing as
8939 // requesting to end fullscreen.
8940 EnsureFullscreenMode(false, aDoShowWindow
);
8942 SetSizeModeInternal(aMode
, aDoShowWindow
);
8946 void nsWindow::FrameState::EnsureFullscreenMode(bool aFullScreen
,
8947 DoShowWindow aDoShowWindow
) {
8948 const bool changed
= aFullScreen
!= mFullscreenMode
;
8949 if (changed
&& aFullScreen
) {
8950 // Save the size mode from before fullscreen.
8951 mPreFullscreenSizeMode
= mSizeMode
;
8953 mFullscreenMode
= aFullScreen
;
8954 if (changed
|| aFullScreen
) {
8955 // NOTE(emilio): When minimizing a fullscreen window we remain with
8956 // mFullscreenMode = true, but mSizeMode = nsSizeMode_Minimized. We need to
8957 // make sure to call SetSizeModeInternal even if mFullscreenMode didn't
8958 // change, to ensure we actually end up with a fullscreen sizemode when
8959 // restoring a window from that state.
8960 SetSizeModeInternal(
8961 aFullScreen
? nsSizeMode_Fullscreen
: mPreFullscreenSizeMode
,
8966 void nsWindow::FrameState::OnFrameChanging() {
8967 const nsSizeMode newSizeMode
=
8968 GetSizeModeForWindowFrame(mWindow
->mWnd
, mFullscreenMode
);
8969 EnsureSizeMode(newSizeMode
);
8970 mWindow
->UpdateNonClientMargins(false);
8973 void nsWindow::FrameState::OnFrameChanged() {
8974 // We don't want to perform the ShowWindow ourselves if we're on the frame
8975 // changed message. Windows has done the frame change for us, and we take care
8976 // of activating as needed. We also don't want to potentially trigger
8977 // more focus / restore. Among other things, this addresses a bug on Win7
8978 // related to window docking. (bug 489258)
8979 const auto newSizeMode
=
8980 GetSizeModeForWindowFrame(mWindow
->mWnd
, mFullscreenMode
);
8981 EnsureSizeMode(newSizeMode
, DoShowWindow::No
);
8983 // If window was restored, activate the window now to get correct attributes.
8984 if (mWindow
->mIsVisible
&& mWindow
->IsForegroundWindow() &&
8985 mLastSizeMode
== nsSizeMode_Minimized
&&
8986 mSizeMode
!= nsSizeMode_Minimized
) {
8987 mWindow
->DispatchFocusToTopLevelWindow(true);
8989 mLastSizeMode
= mSizeMode
;
8992 static void MaybeLogSizeMode(nsSizeMode aMode
) {
8993 #ifdef WINSTATE_DEBUG_OUTPUT
8994 MOZ_LOG(gWindowsLog
, LogLevel::Info
, ("*** SizeMode: %d\n", int(aMode
)));
8998 void nsWindow::FrameState::SetSizeModeInternal(nsSizeMode aMode
,
8999 DoShowWindow aDoShowWindow
) {
9000 if (mSizeMode
== aMode
) {
9004 const auto oldSizeMode
= mSizeMode
;
9005 const bool fullscreenChange
=
9006 mSizeMode
== nsSizeMode_Fullscreen
|| aMode
== nsSizeMode_Fullscreen
;
9007 const bool fullscreen
= aMode
== nsSizeMode_Fullscreen
;
9009 mLastSizeMode
= mSizeMode
;
9012 MaybeLogSizeMode(mSizeMode
);
9014 if (bool(aDoShowWindow
) && mWindow
->mIsVisible
) {
9015 ShowWindowWithMode(mWindow
->mWnd
, aMode
);
9018 mWindow
->UpdateNonClientMargins(false);
9020 if (fullscreenChange
) {
9021 mWindow
->OnFullscreenChanged(oldSizeMode
, fullscreen
);
9024 mWindow
->OnSizeModeChange();
9027 void nsWindow::ContextMenuPreventer::Update(
9028 const WidgetMouseEvent
& aEvent
,
9029 const nsIWidget::ContentAndAPZEventStatus
& aEventStatus
) {
9030 mNeedsToPreventContextMenu
=
9031 aEvent
.mMessage
== eMouseUp
&&
9032 aEvent
.mButton
== MouseButton::eSecondary
&&
9033 aEvent
.mInputSource
== MouseEvent_Binding::MOZ_SOURCE_MOUSE
&&
9034 aEventStatus
.mApzStatus
== nsEventStatus_eConsumeNoDefault
;