1 // Copyright (c) 2010 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/chromeos/wm_overview_favicon.h"
6
7 #include <vector>
8
9 #include "chrome/browser/chromeos/wm_ipc.h"
10 #include "chrome/browser/chromeos/wm_overview_snapshot.h"
11 #include "skia/ext/image_operations.h"
12 #include "third_party/cros/chromeos_wm_ipc_enums.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/base/x/x11_util.h"
15 #include "views/controls/image_view.h"
16 #include "views/controls/label.h"
17 #include "views/layout/grid_layout.h"
18
19 using std::vector;
20
21 #if !defined(OS_CHROMEOS)
22 #error This file is only meant to be compiled for ChromeOS
23 #endif
24
25 namespace chromeos {
26
27 const int WmOverviewFavicon::kIconSize = 32;
28
WmOverviewFavicon()29 WmOverviewFavicon::WmOverviewFavicon()
30 : WidgetGtk(TYPE_WINDOW),
31 favicon_view_(NULL) {
32 }
33
Init(WmOverviewSnapshot * snapshot)34 void WmOverviewFavicon::Init(WmOverviewSnapshot* snapshot) {
35 MakeTransparent();
36
37 favicon_view_ = new views::ImageView();
38
39 WidgetGtk::Init(NULL, gfx::Rect(0, 0, 0, 0));
40
41 SetContentsView(favicon_view_);
42
43 // Set the window type
44 vector<int> params;
45 params.push_back(ui::GetX11WindowFromGtkWidget(
46 GTK_WIDGET(snapshot->GetNativeView())));
47 WmIpc::instance()->SetWindowType(
48 GetNativeView(),
49 WM_IPC_WINDOW_CHROME_TAB_FAV_ICON,
50 ¶ms);
51 }
52
53
SetFavicon(const SkBitmap & image)54 void WmOverviewFavicon::SetFavicon(const SkBitmap& image) {
55 CHECK(favicon_view_) << "Init not called before setting favicon.";
56 SkBitmap icon;
57 if (image.width() && image.height()) {
58 float aspect_ratio = static_cast<float>(image.width()) / image.height();
59 int new_width = kIconSize;
60 int new_height = kIconSize;
61 if (aspect_ratio > 1.0f) {
62 new_height = kIconSize / aspect_ratio;
63 } else {
64 new_width = kIconSize * aspect_ratio;
65 }
66 if (new_width && new_height) {
67 icon = skia::ImageOperations::Resize(
68 image, skia::ImageOperations::RESIZE_BOX,
69 new_width, new_height);
70 }
71 }
72
73 favicon_view_->SetImage(icon);
74
75 // Reset the bounds to the size of the image.
76 SetBounds(gfx::Rect(icon.width(), icon.height()));
77 }
78
79 } // namespace chromeos
80