OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/ui/views/avatar_menu.h" | |
6 | |
7 #include "base/string_number_conversions.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/app/chrome_command_ids.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/profiles/profile_info_cache.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/profile_menu_model.h" | |
16 #include "grit/chromium_strings.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "grit/theme_resources.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 #include "ui/base/resource/resource_bundle.h" | |
21 #include "ui/gfx/canvas.h" | |
22 #include "ui/gfx/image/image.h" | |
23 #include "views/controls/button/image_button.h" | |
24 #include "views/controls/button/menu_button.h" | |
25 #include "views/controls/menu/menu_item_view.h" | |
26 #include "views/controls/menu/submenu_view.h" | |
27 #include "views/widget/widget.h" | |
28 | |
29 namespace { | |
30 | |
31 const int kCellWidth = 38; | |
32 const int kCellHeight = 31; | |
33 const int kCellPaddingX = 5; | |
34 const int kCellPaddingY = 5; | |
35 const int kGridMaxCol = 3; | |
36 | |
37 static inline int Round(double x) { | |
38 return static_cast<int>(x + 0.5); | |
39 } | |
40 | |
41 // This is an button that scales its image to fit its bounds. | |
42 class ScaledImageButton : public views::ImageButton { | |
43 public: | |
44 explicit ScaledImageButton(views::ButtonListener* listener); | |
45 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
46 | |
47 private: | |
48 DISALLOW_COPY_AND_ASSIGN(ScaledImageButton); | |
49 }; | |
50 | |
51 // A view that displays avatar icons in a grid. | |
52 class AvatarIconGridView : public views::View, public views::ButtonListener { | |
53 public: | |
54 AvatarIconGridView(Profile* profile, views::MenuItemView* menu); | |
55 virtual ~AvatarIconGridView(); | |
56 | |
57 virtual void Layout() OVERRIDE; | |
58 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
59 virtual void ButtonPressed(views::Button* sender, | |
60 const views::Event& event) OVERRIDE; | |
61 | |
62 private: | |
63 // Sets the profile's avatar icon to the icon at the given index. | |
64 void SetAvatarIconToIndex(int icon_index); | |
65 | |
66 typedef std::map<int, views::View*> IconIndexToButtonMap; | |
67 IconIndexToButtonMap icon_index_to_button_map_; | |
68 Profile* profile_; | |
69 views::MenuItemView* menu_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(AvatarIconGridView); | |
72 }; | |
73 | |
74 ScaledImageButton::ScaledImageButton(views::ButtonListener* listener) | |
75 : views::ImageButton(listener) { | |
76 } | |
77 | |
78 void ScaledImageButton::OnPaint(gfx::Canvas* canvas) { | |
79 if (state() == views::CustomButton::BS_HOT) | |
80 canvas->FillRectInt(SkColorSetARGB(20, 0, 0, 0), 0, 0, width(), height()); | |
81 else if (state() == views::CustomButton::BS_PUSHED) | |
82 canvas->FillRectInt(SkColorSetARGB(40, 0, 0, 0), 0, 0, width(), height()); | |
83 | |
84 const SkBitmap& icon = GetImageToPaint(); | |
85 if (icon.isNull()) | |
86 return; | |
87 | |
88 int dst_width; | |
89 int dst_height; | |
90 if (icon.width() > icon.height()) { | |
91 dst_width = std::min(width(), icon.width()); | |
92 float scale = static_cast<float>(dst_width) / | |
93 static_cast<float>(icon.width()); | |
94 dst_height = Round(icon.height() * scale); | |
95 } else { | |
96 dst_height = std::min(height(), icon.height()); | |
97 float scale = static_cast<float>(dst_height) / | |
98 static_cast<float>(icon.height()); | |
99 dst_width = Round(icon.width() * scale); | |
100 } | |
101 int dst_x = (width() - dst_width) / 2; | |
102 int dst_y = (height() - dst_height) / 2; | |
103 canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), | |
104 dst_x, dst_y, dst_width, dst_height, false); | |
105 } | |
106 | |
107 AvatarIconGridView::AvatarIconGridView(Profile* profile, | |
108 views::MenuItemView* menu) | |
109 : profile_(profile), | |
110 menu_(menu) { | |
111 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
112 for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) { | |
113 int resource_id = | |
114 ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i); | |
115 views::ImageButton* button = new ScaledImageButton(this); | |
116 button->SetImage(views::CustomButton::BS_NORMAL, | |
117 rb.GetImageNamed(resource_id)); | |
118 button->SetAccessibleName( | |
119 l10n_util::GetStringFUTF16Int(IDS_NUMBERED_AVATAR_NAME, | |
120 static_cast<int>(i + 1))); | |
121 AddChildView(button); | |
122 icon_index_to_button_map_[i] = button; | |
123 } | |
124 } | |
125 | |
126 AvatarIconGridView::~AvatarIconGridView() { | |
127 } | |
128 | |
129 void AvatarIconGridView::Layout() { | |
130 for (IconIndexToButtonMap::const_iterator it = | |
131 icon_index_to_button_map_.begin(); | |
132 it != icon_index_to_button_map_.end(); ++it) { | |
133 views::View* view = it->second; | |
134 int icon_index = it->first; | |
135 int col = icon_index % kGridMaxCol; | |
136 int row = icon_index / kGridMaxCol; | |
137 int x = col * kCellWidth + col * kCellPaddingX; | |
138 int y = row * kCellHeight + row * kCellPaddingY; | |
139 view->SetBounds(x, y, kCellWidth, kCellHeight); | |
140 } | |
141 } | |
142 | |
143 gfx::Size AvatarIconGridView::GetPreferredSize() { | |
144 int cols = kGridMaxCol; | |
145 int rows = ProfileInfoCache::GetDefaultAvatarIconCount() / cols; | |
146 if (ProfileInfoCache::GetDefaultAvatarIconCount() % cols != 0) | |
147 rows++; | |
148 return gfx::Size(cols * kCellWidth + (cols - 1) * kCellPaddingX, | |
149 rows * kCellHeight + (rows - 1) * kCellPaddingY); | |
150 } | |
151 | |
152 void AvatarIconGridView::ButtonPressed(views::Button* sender, | |
153 const views::Event& event) { | |
154 for (IconIndexToButtonMap::const_iterator it = | |
155 icon_index_to_button_map_.begin(); | |
156 it != icon_index_to_button_map_.end(); ++it) { | |
157 if (it->second == sender) { | |
158 SetAvatarIconToIndex(it->first); | |
159 menu_->Cancel(); | |
160 return; | |
161 } | |
162 } | |
163 NOTREACHED(); | |
164 } | |
165 | |
166 void AvatarIconGridView::SetAvatarIconToIndex(int icon_index) { | |
167 ProfileInfoCache& cache = | |
168 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
169 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
170 cache.SetAvatarIconOfProfileAtIndex(profile_index, icon_index); | |
171 } | |
172 | |
173 } // namespace | |
174 | |
175 AvatarMenu::AvatarMenu(ui::MenuModel* model, Profile* profile) | |
176 : MenuModelAdapter(model), | |
177 profile_(profile) { | |
178 root_.reset(new views::MenuItemView(this)); | |
179 BuildMenu(root_.get()); | |
180 views::MenuItemView* item = | |
181 root_->GetMenuItemByID(ProfileMenuModel::COMMAND_CHOOSE_AVATAR_ICON); | |
182 item->AddChildView(new AvatarIconGridView(profile_, root_.get())); | |
183 } | |
184 | |
185 AvatarMenu::~AvatarMenu() { | |
186 } | |
187 | |
188 void AvatarMenu::RunMenu(views::MenuButton* host) { | |
189 // Up the ref count while the menu is displaying. This way if the window is | |
190 // deleted while we're running we won't prematurely delete the menu. | |
191 // TODO(sky): fix this, the menu should really take ownership of the menu | |
192 // (57890). | |
193 scoped_refptr<AvatarMenu> dont_delete_while_running(this); | |
194 gfx::Point screen_loc; | |
195 views::View::ConvertPointToScreen(host, &screen_loc); | |
196 gfx::Rect bounds(screen_loc, host->size()); | |
197 root_->RunMenuAt(host->GetWidget(), host, bounds, | |
198 views::MenuItemView::TOPLEFT, true); | |
199 } | |
OLD | NEW |