Chromium Code Reviews| Index: chrome/browser/ui/webui/chromeos/login/network_dropdown.cc |
| diff --git a/chrome/browser/ui/webui/chromeos/login/network_dropdown.cc b/chrome/browser/ui/webui/chromeos/login/network_dropdown.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..356085d13b4e94da421ec95f7cfcebaa51ff2629 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/chromeos/login/network_dropdown.cc |
| @@ -0,0 +1,261 @@ |
| +// 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/webui/chromeos/login/network_dropdown.h" |
| + |
| +#include <string> |
| + |
| +#include "base/base64.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "content/browser/webui/web_ui.h" |
| +#include "chrome/browser/chromeos/login/proxy_settings_dialog.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "chrome/browser/ui/webui/web_ui_util.h" |
| +#include "views/controls/menu/menu_delegate.h" |
| +#include "views/controls/menu/menu_item_view.h" |
| +#include "ui/gfx/font.h" |
| + |
| +namespace chromeos { |
| + |
| +// WebUI specific implementation of the views::MenuItemInterface class. |
| +class WebUIMenuItem : public views::MenuItemInterface { |
| + public: |
| + WebUIMenuItem(views::MenuDelegate* delegate, WebUI* web_ui); |
| + |
| + virtual ~WebUIMenuItem(); |
| + |
| + base::ListValue* CreateValue(); |
| + |
| + // views::MenuItemInterface implementation: |
| + virtual MenuItemInterface* AppendSubMenu(int item_id, |
| + const std::wstring& label); |
| + virtual MenuItemInterface* AppendSubMenuWithIcon(int item_id, |
| + const std::wstring& label, |
| + const SkBitmap& icon); |
| + virtual MenuItemInterface* AppendMenuItemWithLabel(int item_id, |
| + const std::wstring& label); |
| + virtual MenuItemInterface* AppendMenuItemWithIcon(int item_id, |
| + const std::wstring& label, |
| + const SkBitmap& icon); |
| + virtual void AppendSeparator(); |
| + virtual void ClearSubmenu(); |
| + virtual void ChildrenChanged(); |
| + virtual void Cancel(); |
| + virtual void set_margins(int top_margin, int bottom_margin); |
| + |
| + private: |
| + WebUIMenuItem(const std::wstring& label, |
| + int id, |
| + const std::string& icon, |
| + WebUIMenuItem* previous, |
| + views::MenuDelegate *delegate); |
| + |
| + views::MenuDelegate* delegate_; |
| + |
| + // Item content. Icon is base64 encoded data URL. |
| + int id_; |
| + std::string icon_; |
| + std::string label_; |
| + |
| + // Points to the previous item in the same menu level or NULL. |
| + WebUIMenuItem* previous_; |
| + // Points to the last item in the sub-menu or NULL. |
| + WebUIMenuItem* last_child_; |
| + |
| + WebUI* web_ui_; |
| +}; |
| + |
| +// WebUI specific implementation of the NetworkMenu class. |
| +class NetworkMenuWebUI : public NetworkMenu { |
| + public: |
| + NetworkMenuWebUI(NetworkMenu::Delegate* delegate, WebUI* web_ui) |
| + : NetworkMenu(delegate, false, NULL) { |
| + menu_item_.reset(new WebUIMenuItem(GetMenuDelegate(), web_ui)); |
| + } |
| + |
| + void OnItemChosen(int id) { |
| + GetMenuDelegate()->ExecuteCommand(id); |
| + } |
| +}; |
| + |
| +// WebUIMenuItem --------------------------------------------------------------- |
| + |
| +WebUIMenuItem::WebUIMenuItem(views::MenuDelegate* delegate, WebUI* web_ui) |
| + : delegate_(delegate), |
| + id_(-1), |
| + previous_(NULL), |
| + last_child_(NULL), |
| + web_ui_(web_ui) { |
| +} |
| + |
| +WebUIMenuItem::~WebUIMenuItem() { |
| + if (last_child_) |
| + delete last_child_; |
|
xiyuan
2011/07/29 21:39:33
why not use scoped_ptr?
Nikita (slow)
2011/08/05 23:40:26
Done.
|
| + if (previous_) |
| + delete previous_; |
|
xiyuan
2011/07/29 21:39:33
ditto
Nikita (slow)
2011/08/05 23:40:26
Done.
|
| +} |
| + |
| +base::ListValue* WebUIMenuItem::CreateValue() { |
| + base::DictionaryValue* item = new base::DictionaryValue(); |
| + item->SetInteger("id", id_); |
| + item->SetString("label", label_); |
| + item->SetString("icon", icon_); |
| + if (id_ >= 0) { |
| + item->SetBoolean("enabled", delegate_->IsCommandEnabled(id_)); |
| + item->SetBoolean( |
| + "bold", delegate_->GetLabelFont(id_).GetStyle() == gfx::Font::BOLD); |
| + } |
| + if (last_child_) { |
| + item->Set("sub", last_child_->CreateValue()); |
| + } |
| + |
| + base::ListValue* list; |
| + if (previous_) { |
| + list = previous_->CreateValue(); |
| + } else { |
| + list = new base::ListValue(); |
| + } |
| + |
| + list->Append(item); |
| + |
| + return list; |
| +} |
| + |
| +views::MenuItemInterface* WebUIMenuItem::AppendSubMenu( |
| + int item_id, const std::wstring& label) { |
| + return last_child_ = |
| + new WebUIMenuItem(label, item_id, std::string(), last_child_, delegate_); |
| +} |
| + |
| +views::MenuItemInterface* WebUIMenuItem::AppendSubMenuWithIcon( |
| + int item_id, const std::wstring& label, const SkBitmap& icon) { |
| + return last_child_ = new WebUIMenuItem(label, |
| + item_id, |
| + web_ui_util::GetImageDataUrl(icon), |
| + last_child_, |
| + delegate_); |
| +} |
| + |
| +views::MenuItemInterface* WebUIMenuItem::AppendMenuItemWithLabel( |
| + int item_id, const std::wstring& label) { |
| + return last_child_ = |
| + new WebUIMenuItem(label, item_id, std::string(), last_child_, delegate_); |
| +} |
| + |
| +views::MenuItemInterface* WebUIMenuItem::AppendMenuItemWithIcon( |
| + int item_id, const std::wstring& label, const SkBitmap& icon) { |
| + return last_child_ = new WebUIMenuItem(label, |
| + item_id, |
| + web_ui_util::GetImageDataUrl(icon), |
| + last_child_, |
| + delegate_); |
| +} |
| + |
| +void WebUIMenuItem::AppendSeparator() { |
| + last_child_ = new WebUIMenuItem(UTF8ToWide("-----"), -2, |
| + std::string(), last_child_, |
| + delegate_); |
| +} |
| + |
| +void WebUIMenuItem::ClearSubmenu() { |
| + if (last_child_) |
| + delete last_child_; |
| + last_child_ = NULL; |
| +} |
| + |
| +void WebUIMenuItem::ChildrenChanged() { |
| + if (web_ui_) { |
| + scoped_ptr<base::ListValue> data(CreateValue()); |
| + web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworks", *data); |
| + } |
| +} |
| + |
| +void WebUIMenuItem::Cancel() { |
| +} |
| + |
| +void WebUIMenuItem::set_margins(int top_margin, int bottom_margin) { |
| +} |
| + |
| +WebUIMenuItem::WebUIMenuItem(const std::wstring& label, |
| + int id, |
| + const std::string& icon, |
| + WebUIMenuItem* previous, |
| + views::MenuDelegate *delegate) |
| + : delegate_(delegate), |
| + id_(id), |
| + icon_(icon), |
| + label_(WideToUTF8(label)), |
| + previous_(previous), |
| + last_child_(NULL), |
| + web_ui_(NULL) { |
| +} |
| + |
| +// NetworkDropdown ------------------------------------------------------------- |
| + |
| +NetworkDropdown::NetworkDropdown(WebUI *web_ui, gfx::NativeWindow parent_window) |
| + : parent_window_(parent_window), |
| + web_ui_(web_ui) { |
| + network_menu_.reset(new NetworkMenuWebUI(this, web_ui)); |
| + network_icon_.reset( |
| + new NetworkMenuIcon(this, NetworkMenuIcon::DROPDOWN_MODE)); |
| + CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this); |
| + |
| + SetNetworkIconAndText(); |
| + network_menu_->UpdateMenu(); |
| +} |
| + |
| +NetworkDropdown::~NetworkDropdown() { |
| + CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this); |
| +} |
| + |
| +void NetworkDropdown::OnItemChosen(int id) { |
| + network_menu_->OnItemChosen(id); |
| +} |
| + |
| +views::MenuButton *NetworkDropdown::GetMenuButton() { |
| + NOTREACHED(); |
| + return NULL; |
| +} |
| + |
| +gfx::NativeWindow NetworkDropdown::GetNativeWindow() const { |
| + return parent_window_; |
| +} |
| + |
| +void NetworkDropdown::OpenButtonOptions() { |
| + if (proxy_settings_dialog_.get() == NULL) { |
| + proxy_settings_dialog_.reset( |
| + new ProxySettingsDialog(this, GetNativeWindow())); |
| + } |
| + proxy_settings_dialog_->Show(); |
| +} |
| + |
| +bool NetworkDropdown::ShouldOpenButtonOptions() const { |
| + return true; |
| +} |
| + |
| +void NetworkDropdown::OnNetworkManagerChanged(NetworkLibrary* cros) { |
| + SetNetworkIconAndText(); |
| + network_menu_->UpdateMenu(); |
| +} |
| + |
| +void NetworkDropdown::OnDialogClosed() { |
| +} |
| + |
| +void NetworkDropdown::NetworkMenuIconChanged() { |
| + // TODO(altimofeev): update icon. |
| +} |
| + |
| +void NetworkDropdown::SetNetworkIconAndText() { |
| + string16 text; |
| + const SkBitmap* icon_bitmap = network_icon_->GetIconAndText(&text); |
| + std::string icon_str = |
| + icon_bitmap ? web_ui_util::GetImageDataUrl(*icon_bitmap) : std::string(); |
| + base::StringValue title(text); |
| + base::StringValue icon(icon_str); |
| + web_ui_->CallJavascriptFunction("oobe.NetworkScreen.updateNetworkTitle", |
| + title, icon); |
| + // TODO(altimofeev): update icon. |
| +} |
| + |
| +} // namespace chromeos |