Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ui / base / resource / resource_bundle_win.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/base/resource/resource_bundle_win.h"
6
7 #include "base/logging.h"
8 #include "base/path_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "skia/ext/image_operations.h"
11 #include "ui/base/layout.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/base/resource/resource_data_dll_win.h"
14 #include "ui/gfx/geometry/size_conversions.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/image/image_skia_source.h"
17 #include "ui/gfx/win/dpi.h"
18
19 namespace ui {
20
21 namespace {
22
23 HINSTANCE resources_data_dll;
24
25 HINSTANCE GetCurrentResourceDLL() {
26   if (resources_data_dll)
27     return resources_data_dll;
28   return GetModuleHandle(NULL);
29 }
30
31 base::FilePath GetResourcesPakFilePath(const std::string& pak_name) {
32   base::FilePath path;
33   if (PathService::Get(base::DIR_MODULE, &path))
34     return path.AppendASCII(pak_name.c_str());
35
36   // Return just the name of the pack file.
37   return base::FilePath(base::ASCIIToUTF16(pak_name));
38 }
39
40 }  // namespace
41
42 void ResourceBundle::LoadCommonResources() {
43   // As a convenience, add the current resource module as a data packs.
44   data_packs_.push_back(new ResourceDataDLL(GetCurrentResourceDLL()));
45
46   if (IsScaleFactorSupported(SCALE_FACTOR_100P)) {
47     AddDataPackFromPath(
48         GetResourcesPakFilePath("chrome_100_percent.pak"),
49         SCALE_FACTOR_100P);
50   }
51   if (IsScaleFactorSupported(SCALE_FACTOR_200P)) {
52     DCHECK(gfx::IsHighDPIEnabled());
53     AddDataPackFromPath(
54         GetResourcesPakFilePath("chrome_200_percent.pak"),
55         SCALE_FACTOR_200P);
56   }
57 }
58
59 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
60   // Flipped image is not used on Windows.
61   DCHECK_EQ(rtl, RTL_DISABLED);
62
63   // Windows only uses SkBitmap for gfx::Image, so this is the same as
64   // GetImageNamed.
65   return GetImageNamed(resource_id);
66 }
67
68 // static
69 SkBitmap ResourceBundle::PlatformScaleImage(const SkBitmap& image,
70                                             float loaded_image_scale,
71                                             float desired_bitmap_scale) {
72   if (!gfx::IsHighDPIEnabled())
73     return image;
74
75   // On Windows we can have multiple device scales like 1/1.25/1.5/2, etc.
76   // We only have 1x and 2x data packs. We need to scale the bitmaps
77   // accordingly.
78   if (loaded_image_scale == desired_bitmap_scale)
79     return image;
80
81   SkBitmap scaled_image;
82   gfx::Size unscaled_size(image.width(), image.height());
83   gfx::Size scaled_size = ToCeiledSize(
84       gfx::ScaleSize(unscaled_size,
85                       desired_bitmap_scale / loaded_image_scale));
86   scaled_image = skia::ImageOperations::Resize(
87       image,
88       skia::ImageOperations::RESIZE_LANCZOS3,
89       scaled_size.width(),
90       scaled_size.height());
91   DCHECK_EQ(scaled_image.width(), scaled_size.width());
92   DCHECK_EQ(scaled_image.height(), scaled_size.height());
93   return scaled_image;
94 }
95
96 void SetResourcesDataDLL(HINSTANCE handle) {
97   resources_data_dll = handle;
98 }
99
100 HICON LoadThemeIconFromResourcesDataDLL(int icon_id) {
101   return ::LoadIcon(GetCurrentResourceDLL(), MAKEINTRESOURCE(icon_id));
102 }
103
104 }  // namespace ui;