OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "chrome/browser/web_app_icon_manager.h" | |
6 | |
7 #include "chrome/app/chrome_dll_resource.h" | |
8 #include "chrome/app/theme/theme_resources.h" | |
9 #include "chrome/browser/tab_contents/web_contents.h" | |
10 #include "chrome/common/gfx/icon_util.h" | |
11 #include "skia/include/SkBitmap.h" | |
12 | |
13 // Returns the default icon for the window. | |
14 static HICON GetDefaultIcon() { | |
15 HMODULE chrome_dll = GetModuleHandle(L"chrome.dll"); | |
16 return ::LoadIcon(chrome_dll, MAKEINTRESOURCE(IDR_MAINFRAME)); | |
17 } | |
18 | |
19 // Updates the icon of the window from the specified SkBitmap. If the image | |
20 // is empty, the default icon is used. If icon is non-null it is updated to | |
21 // reflect the new icon (unless the default is used). | |
22 static void UpdateIcon(HWND hwnd, | |
23 const SkBitmap& image, | |
24 int icon_type, | |
25 HICON* icon) { | |
26 if (icon) { | |
27 if (*icon) | |
28 DestroyIcon(*icon); | |
29 *icon = NULL; | |
30 } | |
31 | |
32 HICON icon_to_set; | |
33 if (image.width() > 0) { | |
34 icon_to_set = IconUtil::CreateHICONFromSkBitmap(image); | |
35 if (icon) | |
36 *icon = icon_to_set; | |
37 } else { | |
38 icon_to_set = GetDefaultIcon(); | |
39 } | |
40 ::SendMessage(hwnd, WM_SETICON, icon_type, | |
41 reinterpret_cast<LPARAM>(icon_to_set)); | |
42 } | |
43 | |
44 WebAppIconManager::WebAppIconManager(HWND parent) | |
45 : hwnd_(parent), small_icon_(NULL), big_icon_(NULL), enabled_(true) { | |
46 } | |
47 | |
48 WebAppIconManager::~WebAppIconManager() { | |
49 if (small_icon_) | |
50 DestroyIcon(small_icon_); | |
51 | |
52 if (big_icon_) | |
53 DestroyIcon(big_icon_); | |
54 | |
55 if (app_.get()) | |
56 app_->RemoveObserver(this); | |
57 } | |
58 | |
59 void WebAppIconManager::SetContents(TabContents* contents) { | |
60 WebApp* new_app = NULL; | |
61 if (contents && contents->AsWebContents()) | |
62 new_app = contents->AsWebContents()->web_app(); | |
63 | |
64 if (new_app == app_.get()) | |
65 return; | |
66 | |
67 if (app_.get()) | |
68 app_->RemoveObserver(this); | |
69 app_ = new_app; | |
70 if (app_.get()) { | |
71 app_->AddObserver(this); | |
72 UpdateIconsFromApp(); | |
73 } else if (enabled_) { | |
74 UpdateIcon(hwnd_, SkBitmap(), ICON_SMALL, NULL); | |
75 UpdateIcon(hwnd_, SkBitmap(), ICON_BIG, NULL); | |
76 } | |
77 } | |
78 | |
79 void WebAppIconManager::SetUpdatesEnabled(bool enabled) { | |
80 if (enabled == enabled_) | |
81 return; | |
82 | |
83 enabled_ = enabled; | |
84 if (enabled_) | |
85 UpdateIconsFromApp(); | |
86 } | |
87 | |
88 void WebAppIconManager::WebAppImagesChanged(WebApp* web_app) { | |
89 UpdateIconsFromApp(); | |
90 } | |
91 | |
92 void WebAppIconManager::UpdateIconsFromApp() { | |
93 if (!enabled_) | |
94 return; | |
95 | |
96 SkBitmap small_image; | |
97 SkBitmap big_image; | |
98 if (app_.get() && !app_->GetImages().empty()) { | |
99 const WebApp::Images& images = app_->GetImages(); | |
100 WebApp::Images::const_iterator smallest = images.begin(); | |
101 WebApp::Images::const_iterator biggest = images.begin(); | |
102 for (WebApp::Images::const_iterator i = images.begin() + 1; | |
103 i != images.end(); ++i) { | |
104 if (i->width() > biggest->width()) | |
105 biggest = i; | |
106 else if (i->width() > 0 && i->width() < smallest->width()) | |
107 smallest = i; | |
108 } | |
109 small_image = *smallest; | |
110 big_image = *biggest; | |
111 } | |
112 UpdateIcon(hwnd_, small_image, ICON_SMALL, &small_icon_); | |
113 UpdateIcon(hwnd_, big_image, ICON_BIG, &big_icon_); | |
114 } | |
115 | |
OLD | NEW |