2009-10-05 19:26:54 -07:00
|
|
|
/* vim: se cin sw=2 ts=2 et : */
|
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
#include "TaskbarTabPreview.h"
|
2011-07-13 08:58:33 -07:00
|
|
|
#include "nsWindowGfx.h"
|
2009-10-05 19:26:54 -07:00
|
|
|
#include "nsUXThemeData.h"
|
2012-01-04 02:21:44 -08:00
|
|
|
#include "WinUtils.h"
|
2009-10-05 19:26:54 -07:00
|
|
|
#include <nsITaskbarPreviewController.h>
|
|
|
|
|
|
|
|
#define TASKBARPREVIEW_HWNDID L"TaskbarTabPreviewHwnd"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(TaskbarTabPreview, nsITaskbarTabPreview)
|
2009-10-05 19:26:54 -07:00
|
|
|
|
2013-12-03 07:12:57 -08:00
|
|
|
const wchar_t *const kWindowClass = L"MozillaTaskbarPreviewClass";
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
TaskbarTabPreview::TaskbarTabPreview(ITaskbarList4 *aTaskbar, nsITaskbarPreviewController *aController, HWND aHWND, nsIDocShell *aShell)
|
|
|
|
: TaskbarPreview(aTaskbar, aController, aHWND, aShell),
|
2013-10-08 11:48:20 -07:00
|
|
|
mProxyWindow(nullptr),
|
|
|
|
mIcon(nullptr),
|
2011-10-01 19:16:19 -07:00
|
|
|
mRegistered(false)
|
2009-10-05 19:26:54 -07:00
|
|
|
{
|
2010-01-17 06:29:00 -08:00
|
|
|
WindowHook &hook = GetWindowHook();
|
|
|
|
hook.AddMonitor(WM_WINDOWPOSCHANGED, MainWindowHook, this);
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskbarTabPreview::~TaskbarTabPreview() {
|
|
|
|
if (mIcon) {
|
|
|
|
::DestroyIcon(mIcon);
|
2013-10-08 11:48:20 -07:00
|
|
|
mIcon = nullptr;
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
2010-05-12 11:31:24 -07:00
|
|
|
|
|
|
|
// We need to ensure that proxy window disappears or else Bad Things happen.
|
|
|
|
if (mProxyWindow)
|
|
|
|
Disable();
|
|
|
|
|
|
|
|
NS_ASSERTION(!mProxyWindow, "Taskbar proxy window was not destroyed!");
|
|
|
|
|
2011-09-28 07:02:36 -07:00
|
|
|
if (IsWindowAvailable()) {
|
2010-04-21 12:09:59 -07:00
|
|
|
DetachFromNSWindow();
|
2011-09-28 07:02:36 -07:00
|
|
|
} else {
|
2013-10-08 11:48:20 -07:00
|
|
|
mWnd = nullptr;
|
2011-09-28 07:02:36 -07:00
|
|
|
}
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
TaskbarTabPreview::ShowActive(bool active) {
|
2009-10-05 19:26:54 -07:00
|
|
|
NS_ASSERTION(mVisible && CanMakeTaskbarCalls(), "ShowActive called on invisible window or before taskbar calls can be made for this window");
|
2013-10-08 11:48:20 -07:00
|
|
|
return FAILED(mTaskbar->SetTabActive(active ? mProxyWindow : nullptr,
|
|
|
|
mWnd, 0)) ? NS_ERROR_FAILURE : NS_OK;
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
HWND &
|
|
|
|
TaskbarTabPreview::PreviewWindow() {
|
|
|
|
return mProxyWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
nativeWindow
|
|
|
|
TaskbarTabPreview::GetHWND() {
|
|
|
|
return mProxyWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TaskbarTabPreview::EnsureRegistration() {
|
|
|
|
NS_ASSERTION(mVisible && CanMakeTaskbarCalls(), "EnsureRegistration called when it is not safe to do so");
|
|
|
|
|
|
|
|
(void) UpdateTaskbarProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskbarTabPreview::GetTitle(nsAString &aTitle) {
|
|
|
|
aTitle = mTitle;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskbarTabPreview::SetTitle(const nsAString &aTitle) {
|
|
|
|
mTitle = aTitle;
|
|
|
|
return mVisible ? UpdateTitle() : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskbarTabPreview::SetIcon(imgIContainer *icon) {
|
2013-10-08 11:48:20 -07:00
|
|
|
HICON hIcon = nullptr;
|
2009-10-05 19:26:54 -07:00
|
|
|
if (icon) {
|
|
|
|
nsresult rv;
|
2011-10-01 19:16:19 -07:00
|
|
|
rv = nsWindowGfx::CreateIcon(icon, false, 0, 0,
|
2011-09-28 11:36:43 -07:00
|
|
|
nsWindowGfx::GetIconMetrics(nsWindowGfx::kSmallIcon),
|
|
|
|
&hIcon);
|
2009-10-05 19:26:54 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIcon)
|
|
|
|
::DestroyIcon(mIcon);
|
|
|
|
mIcon = hIcon;
|
|
|
|
mIconImage = icon;
|
|
|
|
return mVisible ? UpdateIcon() : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskbarTabPreview::GetIcon(imgIContainer **icon) {
|
2009-10-16 15:02:27 -07:00
|
|
|
NS_IF_ADDREF(*icon = mIconImage);
|
2009-10-05 19:26:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TaskbarTabPreview::Move(nsITaskbarTabPreview *aNext) {
|
|
|
|
if (aNext == this)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
mNext = aNext;
|
|
|
|
return CanMakeTaskbarCalls() ? UpdateNext() : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::UpdateTaskbarProperties() {
|
|
|
|
if (mRegistered)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (FAILED(mTaskbar->RegisterTab(mProxyWindow, mWnd)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv = UpdateNext();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = TaskbarPreview::UpdateTaskbarProperties();
|
2011-10-01 19:16:19 -07:00
|
|
|
mRegistered = true;
|
2009-10-05 19:26:54 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT
|
|
|
|
TaskbarTabPreview::WndProc(UINT nMsg, WPARAM wParam, LPARAM lParam) {
|
|
|
|
nsRefPtr<TaskbarTabPreview> kungFuDeathGrip(this);
|
|
|
|
switch (nMsg) {
|
|
|
|
case WM_CREATE:
|
2011-10-01 19:16:19 -07:00
|
|
|
TaskbarPreview::EnableCustomDrawing(mProxyWindow, true);
|
2009-10-05 19:26:54 -07:00
|
|
|
return 0;
|
|
|
|
case WM_CLOSE:
|
|
|
|
mController->OnClose();
|
|
|
|
return 0;
|
|
|
|
case WM_ACTIVATE:
|
2010-02-10 06:39:00 -08:00
|
|
|
if (LOWORD(wParam) == WA_ACTIVE) {
|
2010-04-28 11:56:55 -07:00
|
|
|
// Activate the tab the user selected then restore the main window,
|
|
|
|
// keeping normal/max window state intact.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool activateWindow;
|
2009-10-05 19:26:54 -07:00
|
|
|
nsresult rv = mController->OnActivate(&activateWindow);
|
|
|
|
if (NS_SUCCEEDED(rv) && activateWindow) {
|
2012-01-04 02:21:44 -08:00
|
|
|
nsWindow* win = WinUtils::GetNSWindowPtr(mWnd);
|
2010-04-28 11:56:55 -07:00
|
|
|
if (win) {
|
|
|
|
nsWindow * parent = win->GetTopLevelWindow(true);
|
|
|
|
if (parent) {
|
|
|
|
parent->Show(true);
|
|
|
|
}
|
|
|
|
}
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case WM_GETICON:
|
|
|
|
return (LRESULT)mIcon;
|
2010-02-10 06:39:00 -08:00
|
|
|
case WM_SYSCOMMAND:
|
2010-04-28 11:56:55 -07:00
|
|
|
// Send activation events to the top level window and select the proper
|
|
|
|
// tab through the controller.
|
|
|
|
if (wParam == SC_RESTORE || wParam == SC_MAXIMIZE) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool activateWindow;
|
2010-04-28 11:56:55 -07:00
|
|
|
nsresult rv = mController->OnActivate(&activateWindow);
|
|
|
|
if (NS_SUCCEEDED(rv) && activateWindow) {
|
|
|
|
// Note, restoring an iconic, maximized window here will only
|
|
|
|
// activate the maximized window. This is not a bug, it's default
|
|
|
|
// windows behavior.
|
|
|
|
::SendMessageW(mWnd, WM_SYSCOMMAND, wParam, lParam);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Forward everything else to the top level window. Do not forward
|
|
|
|
// close since that's intended for the tab. When the preview proxy
|
|
|
|
// closes, we'll close the tab above.
|
2010-02-10 06:39:00 -08:00
|
|
|
return wParam == SC_CLOSE
|
|
|
|
? ::DefWindowProcW(mProxyWindow, WM_SYSCOMMAND, wParam, lParam)
|
|
|
|
: ::SendMessageW(mWnd, WM_SYSCOMMAND, wParam, lParam);
|
|
|
|
return 0;
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
return TaskbarPreview::WndProc(nMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
LRESULT CALLBACK
|
|
|
|
TaskbarTabPreview::GlobalWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {
|
2012-07-30 07:20:58 -07:00
|
|
|
TaskbarTabPreview *preview(nullptr);
|
2009-10-05 19:26:54 -07:00
|
|
|
if (nMsg == WM_CREATE) {
|
|
|
|
CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT*>(lParam);
|
|
|
|
preview = reinterpret_cast<TaskbarTabPreview*>(cs->lpCreateParams);
|
|
|
|
if (!::SetPropW(hWnd, TASKBARPREVIEW_HWNDID, preview))
|
|
|
|
NS_ERROR("Could not associate native window with tab preview");
|
|
|
|
preview->mProxyWindow = hWnd;
|
|
|
|
} else {
|
|
|
|
preview = reinterpret_cast<TaskbarTabPreview*>(::GetPropW(hWnd, TASKBARPREVIEW_HWNDID));
|
2010-05-02 20:20:00 -07:00
|
|
|
if (nMsg == WM_DESTROY)
|
|
|
|
::RemovePropW(hWnd, TASKBARPREVIEW_HWNDID);
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preview)
|
|
|
|
return preview->WndProc(nMsg, wParam, lParam);
|
|
|
|
return ::DefWindowProcW(hWnd, nMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::Enable() {
|
|
|
|
WNDCLASSW wc;
|
2013-10-08 11:48:20 -07:00
|
|
|
HINSTANCE module = GetModuleHandle(nullptr);
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
if (!GetClassInfoW(module, kWindowClass, &wc)) {
|
|
|
|
wc.style = 0;
|
|
|
|
wc.lpfnWndProc = GlobalWndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = module;
|
2013-10-08 11:48:20 -07:00
|
|
|
wc.hIcon = nullptr;
|
|
|
|
wc.hCursor = nullptr;
|
|
|
|
wc.hbrBackground = (HBRUSH) nullptr;
|
|
|
|
wc.lpszMenuName = (LPCWSTR) nullptr;
|
2009-10-05 19:26:54 -07:00
|
|
|
wc.lpszClassName = kWindowClass;
|
|
|
|
RegisterClassW(&wc);
|
|
|
|
}
|
|
|
|
::CreateWindowW(kWindowClass, L"TaskbarPreviewWindow",
|
2013-10-08 11:48:20 -07:00
|
|
|
WS_CAPTION | WS_SYSMENU, 0, 0, 200, 60,
|
|
|
|
nullptr, nullptr, module, this);
|
2009-10-05 19:26:54 -07:00
|
|
|
// GlobalWndProc will set mProxyWindow so that WM_CREATE can have a valid HWND
|
|
|
|
if (!mProxyWindow)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2010-01-17 06:29:00 -08:00
|
|
|
UpdateProxyWindowStyle();
|
|
|
|
|
2009-10-05 19:26:54 -07:00
|
|
|
nsresult rv = TaskbarPreview::Enable();
|
|
|
|
nsresult rvUpdate;
|
|
|
|
rvUpdate = UpdateTitle();
|
|
|
|
if (NS_FAILED(rvUpdate))
|
|
|
|
rv = rvUpdate;
|
|
|
|
|
|
|
|
rvUpdate = UpdateIcon();
|
|
|
|
if (NS_FAILED(rvUpdate))
|
|
|
|
rv = rvUpdate;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::Disable() {
|
2010-05-12 11:31:24 -07:00
|
|
|
// TaskbarPreview::Disable assumes that mWnd is valid but this method can be
|
|
|
|
// called when it is null iff the nsWindow has already been destroyed and we
|
|
|
|
// are still visible for some reason during object destruction.
|
|
|
|
if (mWnd)
|
|
|
|
TaskbarPreview::Disable();
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
if (FAILED(mTaskbar->UnregisterTab(mProxyWindow)))
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-10-01 19:16:19 -07:00
|
|
|
mRegistered = false;
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
// TaskbarPreview::WndProc will set mProxyWindow to null
|
|
|
|
if (!DestroyWindow(mProxyWindow))
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-10-08 11:48:20 -07:00
|
|
|
mProxyWindow = nullptr;
|
2009-10-05 19:26:54 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-21 12:09:59 -07:00
|
|
|
TaskbarTabPreview::DetachFromNSWindow() {
|
2011-10-01 19:16:19 -07:00
|
|
|
(void) SetVisible(false);
|
2010-04-21 12:09:59 -07:00
|
|
|
WindowHook &hook = GetWindowHook();
|
|
|
|
hook.RemoveMonitor(WM_WINDOWPOSCHANGED, MainWindowHook, this);
|
2009-10-05 19:26:54 -07:00
|
|
|
|
2010-04-21 12:09:59 -07:00
|
|
|
TaskbarPreview::DetachFromNSWindow();
|
2009-10-05 19:26:54 -07:00
|
|
|
}
|
|
|
|
|
2010-01-17 06:29:00 -08:00
|
|
|
/* static */
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-01-17 06:29:00 -08:00
|
|
|
TaskbarTabPreview::MainWindowHook(void *aContext,
|
|
|
|
HWND hWnd, UINT nMsg,
|
|
|
|
WPARAM wParam, LPARAM lParam,
|
|
|
|
LRESULT *aResult) {
|
|
|
|
if (nMsg == WM_WINDOWPOSCHANGED) {
|
|
|
|
TaskbarTabPreview *preview = reinterpret_cast<TaskbarTabPreview*>(aContext);
|
|
|
|
WINDOWPOS *pos = reinterpret_cast<WINDOWPOS*>(lParam);
|
|
|
|
if (SWP_FRAMECHANGED == (pos->flags & SWP_FRAMECHANGED))
|
|
|
|
preview->UpdateProxyWindowStyle();
|
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("Style changed hook fired on non-style changed message");
|
|
|
|
}
|
2011-10-01 19:16:19 -07:00
|
|
|
return false;
|
2010-01-17 06:29:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TaskbarTabPreview::UpdateProxyWindowStyle() {
|
|
|
|
if (!mProxyWindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DWORD minMaxMask = WS_MINIMIZE | WS_MAXIMIZE;
|
|
|
|
DWORD windowStyle = GetWindowLongW(mWnd, GWL_STYLE);
|
|
|
|
|
|
|
|
DWORD proxyStyle = GetWindowLongW(mProxyWindow, GWL_STYLE);
|
|
|
|
proxyStyle &= ~minMaxMask;
|
|
|
|
proxyStyle |= windowStyle & minMaxMask;
|
|
|
|
SetWindowLongW(mProxyWindow, GWL_STYLE, proxyStyle);
|
|
|
|
|
|
|
|
DWORD exStyle = (WS_MAXIMIZE == (windowStyle & WS_MAXIMIZE)) ? WS_EX_TOOLWINDOW : 0;
|
|
|
|
SetWindowLongW(mProxyWindow, GWL_EXSTYLE, exStyle);
|
|
|
|
}
|
|
|
|
|
2009-10-05 19:26:54 -07:00
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::UpdateTitle() {
|
|
|
|
NS_ASSERTION(mVisible, "UpdateTitle called on invisible preview");
|
|
|
|
|
|
|
|
if (!::SetWindowTextW(mProxyWindow, mTitle.get()))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::UpdateIcon() {
|
|
|
|
NS_ASSERTION(mVisible, "UpdateIcon called on invisible preview");
|
|
|
|
|
|
|
|
::SendMessageW(mProxyWindow, WM_SETICON, ICON_SMALL, (LPARAM)mIcon);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TaskbarTabPreview::UpdateNext() {
|
|
|
|
NS_ASSERTION(CanMakeTaskbarCalls() && mVisible, "UpdateNext called on invisible tab preview");
|
2013-10-08 11:48:20 -07:00
|
|
|
HWND hNext = nullptr;
|
2009-10-05 19:26:54 -07:00
|
|
|
if (mNext) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool visible;
|
2009-10-05 19:26:54 -07:00
|
|
|
nsresult rv = mNext->GetVisible(&visible);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Can only move next to enabled previews
|
|
|
|
if (!visible)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
hNext = (HWND)mNext->GetHWND();
|
|
|
|
|
|
|
|
// hNext must be registered with the taskbar if the call is to succeed
|
|
|
|
mNext->EnsureRegistration();
|
|
|
|
}
|
|
|
|
if (FAILED(mTaskbar->SetTabOrder(mProxyWindow, hNext)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|
|
|
|
|