Index: chrome/browser/ui/views/avatar_menu.cc |
diff --git a/chrome/browser/ui/views/avatar_menu.cc b/chrome/browser/ui/views/avatar_menu.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3eb962aba8b08625bab8c07f8a078b7cadee098b |
--- /dev/null |
+++ b/chrome/browser/ui/views/avatar_menu.cc |
@@ -0,0 +1,253 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/views/avatar_menu.h" |
+ |
+#include "base/string_number_conversions.h" |
+#include "base/utf_string_conversions.h" |
+#include "chrome/app/chrome_command_ids.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/profiles/profile_info_cache.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/profile_menu_model.h" |
+#include "grit/chromium_strings.h" |
+#include "grit/generated_resources.h" |
+#include "grit/theme_resources.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/resource/resource_bundle.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/gfx/image/image.h" |
+#include "views/controls/button/image_button.h" |
+#include "views/controls/button/menu_button.h" |
+#include "views/controls/menu/menu_item_view.h" |
+#include "views/controls/menu/submenu_view.h" |
+#include "views/widget/widget.h" |
+ |
+namespace { |
+ |
+const int CELL_WIDTH = 32; |
sky
2011/07/08 21:01:44
kCellWidth. Same for all these constants.
sail
2011/07/08 21:53:57
Done.
|
+const int CELL_HEIGHT = 32; |
+const int CELL_PADDING_X = 5; |
+const int CELL_PADDING_Y = 5; |
+const int GRID_MAX_COL = 3; |
+ |
+static inline int Round(double x) { |
+ return static_cast<int>(x + 0.5); |
+} |
+ |
+// This is an button that scales its image to fit its bounds. |
+class ScaledImageButton : public views::ImageButton { |
+ public: |
+ explicit ScaledImageButton(views::ButtonListener* listener); |
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(ScaledImageButton); |
+}; |
+ |
+// A view that displays avatar icons in a grid. |
+class AvatarIconGridView : public views::View, public views::ButtonListener { |
+ public: |
+ explicit AvatarIconGridView(Profile* profile); |
+ |
+ virtual void Layout() OVERRIDE; |
+ virtual gfx::Size GetPreferredSize() OVERRIDE; |
+ virtual void ButtonPressed(views::Button* sender, |
+ const views::Event& event) OVERRIDE; |
+ |
+ private: |
+ void SetAvatarIconToIndex(int icon_index); |
sky
2011/07/08 21:01:44
Description?
sail
2011/07/08 21:53:57
Done.
|
+ |
+ typedef std::map<int, views::View*> IconIndexToButtonMap; |
+ IconIndexToButtonMap icon_index_to_button_map_; |
+ Profile* profile_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AvatarIconGridView); |
+}; |
+ |
+ScaledImageButton::ScaledImageButton(views::ButtonListener* listener) |
+ : views::ImageButton(listener) { |
+} |
+ |
+void ScaledImageButton::OnPaint(gfx::Canvas* canvas) { |
+ if (state() == views::CustomButton::BS_HOT) |
+ canvas->FillRectInt(SkColorSetARGB(20, 0, 0, 0), 0, 0, width(), height()); |
+ else if (state() == views::CustomButton::BS_PUSHED) |
+ canvas->FillRectInt(SkColorSetARGB(40, 0, 0, 0), 0, 0, width(), height()); |
+ |
+ const SkBitmap& icon = GetImageToPaint(); |
+ if (icon.isNull()) |
+ return; |
+ |
+ int dst_width; |
+ int dst_height; |
+ if (icon.width() > icon.height()) { |
+ dst_width = std::min(width(), icon.width()); |
+ float scale = static_cast<float>(dst_width) / |
+ static_cast<float>(icon.width()); |
+ dst_height = Round(icon.height() * scale); |
+ } else { |
+ dst_height = std::min(height(), icon.height()); |
+ float scale = static_cast<float>(dst_height) / |
+ static_cast<float>(icon.height()); |
+ dst_width = Round(icon.width() * scale); |
+ } |
+ int dst_x = (width() - dst_width) / 2; |
+ int dst_y = (height() - dst_height) / 2; |
+ canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), |
+ dst_x, dst_y, dst_width, dst_height, false); |
+} |
+ |
+AvatarIconGridView::AvatarIconGridView(Profile* profile) : profile_(profile) { |
+ ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
+ for (size_t i = 0; i < ProfileInfoCache::GetDefaultAvatarIconCount(); ++i) { |
+ int resource_id = |
+ ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i); |
sky
2011/07/08 21:01:44
indent by 4.
sail
2011/07/08 21:53:57
Done.
|
+ views::ImageButton* button = new ScaledImageButton(this); |
+ button->SetImage(views::CustomButton::BS_NORMAL, |
+ rb.GetImageNamed(resource_id)); |
+ button->SetAccessibleName( |
+ l10n_util::GetStringFUTF16Int(IDS_NUMBERED_AVATAR_NAME, |
+ static_cast<int>(i + 1))); |
+ AddChildView(button); |
+ icon_index_to_button_map_[i] = button; |
+ } |
+} |
+ |
+void AvatarIconGridView::Layout() { |
+ for (IconIndexToButtonMap::const_iterator it = |
+ icon_index_to_button_map_.begin(); |
+ it != icon_index_to_button_map_.end(); ++it) { |
+ views::View* view = it->second; |
+ int icon_index = it->first; |
+ int col = icon_index % GRID_MAX_COL; |
+ int row = icon_index / GRID_MAX_COL; |
+ int x = col * CELL_WIDTH + col * CELL_PADDING_X; |
+ int y = row * CELL_HEIGHT + row * CELL_PADDING_Y; |
+ view->SetBounds(x, y, CELL_WIDTH, CELL_HEIGHT); |
+ } |
+} |
+ |
+gfx::Size AvatarIconGridView::GetPreferredSize() { |
+ int cols = GRID_MAX_COL; |
+ int rows = ProfileInfoCache::GetDefaultAvatarIconCount() / cols; |
+ if (ProfileInfoCache::GetDefaultAvatarIconCount() % cols != 0) |
+ rows++; |
+ return gfx::Size(cols * CELL_WIDTH + (cols - 1) * CELL_PADDING_X, |
+ rows * CELL_HEIGHT + (rows - 1) * CELL_PADDING_Y); |
+} |
+ |
+void AvatarIconGridView::ButtonPressed(views::Button* sender, |
+ const views::Event& event) { |
+ for (IconIndexToButtonMap::const_iterator it = |
+ icon_index_to_button_map_.begin(); |
+ it != icon_index_to_button_map_.end(); ++it) { |
+ if (it->second == sender) { |
+ SetAvatarIconToIndex(it->first); |
+ break; |
sky
2011/07/08 21:01:44
Do you want to close the menu when the user clicks
sail
2011/07/08 21:53:57
Ahh, that would be a good idea. Off the top of you
sky
2011/07/08 22:31:25
CAncel will close the menu.
|
+ } |
+ } |
+} |
sky
2011/07/08 21:01:44
NOTREACHED
sail
2011/07/08 21:53:57
Done.
|
+ |
+void AvatarIconGridView::SetAvatarIconToIndex(int icon_index) { |
+ ProfileInfoCache& cache = |
+ g_browser_process->profile_manager()->GetProfileInfoCache(); |
+ size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); |
+ cache.SetAvatarIconOfProfileAtIndex(profile_index, icon_index); |
+} |
+ |
+} // namespace |
+ |
+AvatarMenu::AvatarMenu(Browser* browser) : browser_(browser) { |
+} |
+ |
+void AvatarMenu::Init(ui::MenuModel* model) { |
+ DCHECK(!root_.get()); |
+ root_.reset(new views::MenuItemView(this)); |
+ int next_id = 1; |
+ PopulateMenu(root_.get(), model, &next_id); |
+} |
+ |
+void AvatarMenu::RunMenu(views::MenuButton* host) { |
+ // Up the ref count while the menu is displaying. This way if the window is |
+ // deleted while we're running we won't prematurely delete the menu. |
+ // TODO(sky): fix this, the menu should really take ownership of the menu |
+ // (57890). |
+ scoped_refptr<AvatarMenu> dont_delete_while_running(this); |
+ gfx::Point screen_loc; |
+ views::View::ConvertPointToScreen(host, &screen_loc); |
+ gfx::Rect bounds(screen_loc, host->size()); |
+ root_->RunMenuAt(host->GetWidget()->GetNativeWindow(), host, bounds, |
+ views::MenuItemView::TOPRIGHT, true); |
+} |
+ |
+void AvatarMenu::PopulateMenu(views::MenuItemView* parent, |
+ ui::MenuModel* model, |
+ int* next_id) { |
+ int index_offset = model->GetFirstItemIndex(NULL); |
+ for (int i = 0, max = model->GetItemCount(); i < max; ++i) { |
+ int index = i + index_offset; |
+ |
+ views::MenuItemView* item = |
+ AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id); |
+ |
+ if (model->GetTypeAt(index) == ui::MenuModel::TYPE_SUBMENU) |
+ PopulateMenu(item, model->GetSubmenuModelAt(index), next_id); |
+ |
+ if (model->GetCommandIdAt(index) == |
+ ProfileMenuModel::COMMAND_CHOOSE_AVATAR_ICON) |
+ item->AddChildView(new AvatarIconGridView(browser_->profile())); |
+ } |
+} |
+ |
+views::MenuItemView* AvatarMenu::AppendMenuItem( |
+ views::MenuItemView* parent, |
+ ui::MenuModel* model, |
+ int index, |
+ ui::MenuModel::ItemType menu_type, |
+ int* next_id) { |
+ int id = (*next_id)++; |
+ id_to_entry_[id].first = model; |
+ id_to_entry_[id].second = index; |
+ views::MenuItemView* menu_item = |
+ parent->AppendMenuItemFromModel(model, index, id); |
+ if (menu_item) |
+ menu_item->SetVisible(model->IsVisibleAt(index)); |
+ if (menu_type == ui::MenuModel::TYPE_COMMAND && model->HasIcons()) { |
+ SkBitmap icon; |
+ if (model->GetIconAt(index, &icon)) |
+ menu_item->SetIcon(icon); |
+ } |
+ return menu_item; |
+} |
+ |
+bool AvatarMenu::IsItemChecked(int id) const { |
+ const Entry& entry = id_to_entry_.find(id)->second; |
+ return entry.first->IsItemCheckedAt(entry.second); |
+} |
+ |
+bool AvatarMenu::IsCommandEnabled(int id) const { |
+ if (id == 0) |
+ return false; // The root item. |
+ const Entry& entry = id_to_entry_.find(id)->second; |
+ return entry.first->IsEnabledAt(entry.second); |
+} |
+ |
+void AvatarMenu::ExecuteCommand(int id, int mouse_event_flags) { |
+ const Entry& entry = id_to_entry_.find(id)->second; |
+ return entry.first->ActivatedAt(entry.second); |
+} |
+ |
+bool AvatarMenu::GetAccelerator(int id, views::Accelerator* accelerator) { |
+ const Entry& entry = id_to_entry_.find(id)->second; |
+ ui::Accelerator menu_accelerator; |
+ if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) |
+ return false; |
+ |
+ *accelerator = views::Accelerator(menu_accelerator.key_code(), |
+ menu_accelerator.modifiers()); |
+ return true; |
+} |