summaryrefslogtreecommitdiffstats
path: root/widget/windows/IconLoaderHelperWin.cpp
blob: a6be500fe4ac466078f036156bf77979e878a31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

/*
 * Retrieves and displays icons in native menu items on Windows.
 */

#include "gfxPlatform.h"
#include "imgIContainer.h"
#include "imgLoader.h"
#include "imgRequestProxy.h"
#include "mozilla/dom/Document.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsNameSpaceManager.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"
#include "nsToolkit.h"
#include "nsWindowGfx.h"
#include "IconLoaderHelperWin.h"

using namespace mozilla;

using mozilla::gfx::SourceSurface;
using mozilla::widget::IconLoader;
using mozilla::widget::IconLoaderListenerWin;

namespace mozilla::widget {

NS_IMPL_CYCLE_COLLECTION(IconLoaderHelperWin, mLoadListener)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IconLoaderHelperWin)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(IconLoaderHelperWin)
NS_IMPL_CYCLE_COLLECTING_RELEASE(IconLoaderHelperWin)

IconLoaderHelperWin::IconLoaderHelperWin(IconLoaderListenerWin* aListener)
    : mLoadListener(aListener) {
  MOZ_ASSERT(aListener);
  MOZ_ASSERT(NS_IsMainThread());
}

IconLoaderHelperWin::~IconLoaderHelperWin() { Destroy(); }

nsresult IconLoaderHelperWin::OnComplete(imgIContainer* aImage,
                                         const nsIntRect& aRect) {
  NS_ENSURE_ARG_POINTER(aImage);

  nsresult rv = nsWindowGfx::CreateIcon(
      aImage, false, LayoutDeviceIntPoint(),
      nsWindowGfx::GetIconMetrics(nsWindowGfx::kRegularIcon),
      &mNativeIconImage);
  NS_ENSURE_SUCCESS(rv, rv);

  mLoadListener->OnComplete();
  return NS_OK;
}

HICON IconLoaderHelperWin::GetNativeIconImage() {
  if (mNativeIconImage) {
    return mNativeIconImage;
  }
  return ::LoadIcon(::GetModuleHandle(NULL), IDI_APPLICATION);
}

void IconLoaderHelperWin::Destroy() {
  if (mNativeIconImage) {
    ::DestroyIcon(mNativeIconImage);
    mNativeIconImage = nullptr;
  }
  if (mLoadListener) {
    mLoadListener = nullptr;
  }
}

}  // namespace mozilla::widget