Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/status/network_menu.h" | 5 #include "chrome/browser/chromeos/status/network_menu.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h" | 11 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h" |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" | 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 13 #include "chrome/browser/chromeos/sim_unlock_dialog_delegate.h" | 13 #include "chrome/browser/chromeos/sim_unlock_dialog_delegate.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 16 #include "chrome/browser/ui/views/window.h" | 16 #include "chrome/browser/ui/views/window.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 19 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 20 #include "net/base/escape.h" | 20 #include "net/base/escape.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/gfx/canvas_skia.h" | 23 #include "ui/gfx/canvas_skia.h" |
| 24 #include "ui/gfx/skbitmap_operations.h" | 24 #include "ui/gfx/skbitmap_operations.h" |
| 25 #include "views/controls/menu/menu_2.h" | 25 #include "views/controls/menu/menu_item_view.h" |
| 26 #include "views/controls/menu/submenu_view.h" | |
| 27 #include "views/widget/widget.h" | |
| 26 #include "views/window/window.h" | 28 #include "views/window/window.h" |
| 27 | 29 |
| 30 using views::MenuItemView; | |
| 31 | |
| 28 namespace chromeos { | 32 namespace chromeos { |
| 29 | 33 |
| 30 //////////////////////////////////////////////////////////////////////////////// | 34 //////////////////////////////////////////////////////////////////////////////// |
| 31 // NetworkMenu | 35 // NetworkMenu |
| 32 | 36 |
| 33 // static | 37 // static |
| 34 const int NetworkMenu::kNumBarsImages = 4; | 38 const int NetworkMenu::kNumBarsImages = 4; |
| 35 | 39 |
| 36 // NOTE: Use an array rather than just calculating a resource number to avoid | 40 // NOTE: Use an array rather than just calculating a resource number to avoid |
| 37 // creating implicit ordering dependencies on the resource values. | 41 // creating implicit ordering dependencies on the resource values. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 const int NetworkMenu::kNumAnimatingImages = 10; | 74 const int NetworkMenu::kNumAnimatingImages = 10; |
| 71 | 75 |
| 72 // static | 76 // static |
| 73 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 77 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
| 74 | 78 |
| 75 // static | 79 // static |
| 76 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 80 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
| 77 | 81 |
| 78 NetworkMenu::NetworkMenu() | 82 NetworkMenu::NetworkMenu() |
| 79 : min_width_(-1) { | 83 : min_width_(-1) { |
| 80 network_menu_.reset(new views::Menu2(this)); | 84 network_menu_.reset(new views::MenuItemView(this)); |
| 85 network_menu_->set_has_icons(true); | |
| 81 } | 86 } |
| 82 | 87 |
| 83 NetworkMenu::~NetworkMenu() { | 88 NetworkMenu::~NetworkMenu() { |
| 84 } | 89 } |
| 85 | 90 |
| 86 bool NetworkMenu::ConnectToNetworkAt(int index, | 91 bool NetworkMenu::ConnectToNetworkAt(int index, |
| 87 const std::string& passphrase, | 92 const std::string& passphrase, |
| 88 const std::string& ssid, | 93 const std::string& ssid, |
| 89 int auto_connect) const { | 94 int auto_connect) const { |
| 90 int flags = menu_items_[index].flags; | 95 int flags = menu_items_[index].flags; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 } | 145 } |
| 141 } else if (flags & FLAG_OTHER_WIFI_NETWORK) { | 146 } else if (flags & FLAG_OTHER_WIFI_NETWORK) { |
| 142 ShowOtherWifi(); | 147 ShowOtherWifi(); |
| 143 } else if (flags & FLAG_OTHER_CELLULAR_NETWORK) { | 148 } else if (flags & FLAG_OTHER_CELLULAR_NETWORK) { |
| 144 ShowOtherCellular(); | 149 ShowOtherCellular(); |
| 145 } | 150 } |
| 146 return true; | 151 return true; |
| 147 } | 152 } |
| 148 | 153 |
| 149 //////////////////////////////////////////////////////////////////////////////// | 154 //////////////////////////////////////////////////////////////////////////////// |
| 150 // NetworkMenu, ui::MenuModel implementation: | 155 // NetworkMenu, views::MenuItemView implementation: |
| 151 | 156 std::wstring NetworkMenu::GetLabel(int id) const { |
| 152 int NetworkMenu::GetItemCount() const { | 157 DCHECK_LE(0,id); |
|
oshima
2011/04/15 17:40:12
space after , . same for the rest
rhashimoto
2011/04/15 21:20:27
Done.
| |
| 153 return static_cast<int>(menu_items_.size()); | 158 DCHECK_GT(static_cast<int>(menu_items_.size()),id); |
| 159 return UTF16ToWide(menu_items_[id].label); | |
| 154 } | 160 } |
| 155 | 161 |
| 156 ui::MenuModel::ItemType NetworkMenu::GetTypeAt(int index) const { | 162 const gfx::Font* NetworkMenu::GetLabelFont(int id) const |
| 157 return menu_items_[index].type; | 163 { |
| 164 return (menu_items_[id].flags & FLAG_ASSOCIATED) ? | |
| 165 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | |
| 166 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | |
| 158 } | 167 } |
| 159 | 168 |
| 160 string16 NetworkMenu::GetLabelAt(int index) const { | 169 bool NetworkMenu::IsCommandEnabled(int id) const { |
| 161 return menu_items_[index].label; | 170 if (id < static_cast<int>(menu_items_.size())) |
| 171 return !(menu_items_[id].flags & FLAG_DISABLED); | |
| 172 else | |
| 173 return false; | |
| 162 } | 174 } |
| 163 | 175 |
| 164 const gfx::Font* NetworkMenu::GetLabelFontAt(int index) const { | 176 bool NetworkMenu::IsItemChecked(int id) const { |
| 165 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? | 177 DCHECK_LE(0,id); |
| 166 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | 178 DCHECK_GT(static_cast<int>(menu_items_.size()),id); |
| 167 NULL; | 179 return menu_items_[id].type == ui::MenuModel::TYPE_CHECK; |
| 168 } | 180 } |
| 169 | 181 |
| 170 bool NetworkMenu::IsItemCheckedAt(int index) const { | 182 void NetworkMenu::ExecuteCommand(int id) { |
| 171 // All ui::MenuModel::TYPE_CHECK menu items are checked. | 183 DCHECK_LE(0,id); |
| 172 return true; | 184 DCHECK_GT(static_cast<int>(menu_items_.size()),id); |
| 173 } | |
| 174 | 185 |
| 175 bool NetworkMenu::GetIconAt(int index, SkBitmap* icon) { | |
| 176 if (!menu_items_[index].icon.empty()) { | |
| 177 *icon = menu_items_[index].icon; | |
| 178 return true; | |
| 179 } | |
| 180 return false; | |
| 181 } | |
| 182 | |
| 183 bool NetworkMenu::IsEnabledAt(int index) const { | |
| 184 return !(menu_items_[index].flags & FLAG_DISABLED); | |
| 185 } | |
| 186 | |
| 187 void NetworkMenu::ActivatedAt(int index) { | |
| 188 // When we are refreshing the menu, ignore menu item activation. | 186 // When we are refreshing the menu, ignore menu item activation. |
| 189 if (refreshing_menu_) | 187 if (refreshing_menu_) |
| 190 return; | 188 return; |
| 191 | 189 |
| 192 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 190 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 193 int flags = menu_items_[index].flags; | 191 int flags = menu_items_[id].flags; |
| 194 if (flags & FLAG_OPTIONS) { | 192 if (flags & FLAG_OPTIONS) { |
| 195 OpenButtonOptions(); | 193 OpenButtonOptions(); |
| 196 } else if (flags & FLAG_TOGGLE_ETHERNET) { | 194 } else if (flags & FLAG_TOGGLE_ETHERNET) { |
| 197 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); | 195 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); |
| 198 } else if (flags & FLAG_TOGGLE_WIFI) { | 196 } else if (flags & FLAG_TOGGLE_WIFI) { |
| 199 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); | 197 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); |
| 200 } else if (flags & FLAG_TOGGLE_CELLULAR) { | 198 } else if (flags & FLAG_TOGGLE_CELLULAR) { |
| 201 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 199 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
| 202 // TODO(nkostylev): Integrate with SIM locked state. crosbug.com/12007. | 200 // TODO(nkostylev): Integrate with SIM locked state. crosbug.com/12007. |
| 203 // SimUnlockDialogDelegate::ShowDialog(GetNativeWindow()); | 201 // SimUnlockDialogDelegate::ShowDialog(GetNativeWindow()); |
| 204 } else if (flags & FLAG_TOGGLE_OFFLINE) { | 202 } else if (flags & FLAG_TOGGLE_OFFLINE) { |
| 205 cros->EnableOfflineMode(!cros->offline_mode()); | 203 cros->EnableOfflineMode(!cros->offline_mode()); |
| 206 } else if (flags & FLAG_ETHERNET) { | 204 } else if (flags & FLAG_ETHERNET) { |
| 207 if (cros->ethernet_connected()) { | 205 if (cros->ethernet_connected()) { |
| 208 ShowTabbedNetworkSettings(cros->ethernet_network()); | 206 ShowTabbedNetworkSettings(cros->ethernet_network()); |
| 209 } | 207 } |
| 210 } else if (flags & FLAG_WIFI) { | 208 } else if (flags & FLAG_WIFI) { |
| 211 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 209 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
| 212 } else if (flags & FLAG_OTHER_WIFI_NETWORK) { | 210 } else if (flags & FLAG_OTHER_WIFI_NETWORK) { |
| 213 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 211 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
| 214 } else if (flags & FLAG_CELLULAR) { | 212 } else if (flags & FLAG_CELLULAR) { |
| 215 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 213 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
| 216 } else if (flags & FLAG_OTHER_CELLULAR_NETWORK) { | 214 } else if (flags & FLAG_OTHER_CELLULAR_NETWORK) { |
| 217 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 215 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
| 218 } | 216 } |
| 219 } | 217 } |
| 220 | 218 |
| 221 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 219 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
| 222 min_width_ = width; | 220 min_width_ = width; |
| 223 // This actually has no effect since menu is rebuilt before showing. | 221 // This actually has no effect since menu is rebuilt before showing. |
| 224 network_menu_->SetMinimumWidth(width); | |
| 225 } | 222 } |
| 226 | 223 |
| 227 void NetworkMenu::CancelMenu() { | 224 void NetworkMenu::CancelMenu() { |
| 228 network_menu_->CancelMenu(); | 225 network_menu_->Cancel(); |
| 229 } | 226 } |
| 230 | 227 |
| 231 void NetworkMenu::UpdateMenu() { | 228 void NetworkMenu::UpdateMenu() { |
| 232 refreshing_menu_ = true; | 229 refreshing_menu_ = true; |
| 230 | |
| 231 // Rebuild list of available networks. | |
| 233 InitMenuItems(); | 232 InitMenuItems(); |
| 234 network_menu_->Rebuild(); | 233 |
| 234 // Clear and repopulate MenuItemView. | |
| 235 network_menu_->CreateSubmenu()->RemoveAllChildViews(true); | |
| 236 for (int i = 0, n = menu_items_.size(); i < n; ++i) { | |
| 237 if (menu_items_[i].type == ui::MenuModel::TYPE_SEPARATOR) | |
| 238 network_menu_->AppendSeparator(); | |
| 239 else | |
| 240 network_menu_->AppendMenuItemWithIcon( | |
| 241 i, | |
| 242 UTF16ToWide(menu_items_[i].label), | |
| 243 menu_items_[i].icon); | |
| 244 } | |
| 245 | |
| 246 network_menu_->ChildrenChanged(); | |
| 235 refreshing_menu_ = false; | 247 refreshing_menu_ = false; |
| 236 } | 248 } |
| 237 | 249 |
| 238 // static | 250 // static |
| 239 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, | 251 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, |
| 240 bool black) { | 252 bool black) { |
| 241 DCHECK(wifi); | 253 DCHECK(wifi); |
| 242 if (wifi->strength() == 0) { | 254 if (wifi->strength() == 0) { |
| 243 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 255 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 244 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : | 256 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 kBottomRightBadgeY); | 419 kBottomRightBadgeY); |
| 408 if (top_left_badge != NULL) | 420 if (top_left_badge != NULL) |
| 409 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); | 421 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); |
| 410 return canvas.ExtractBitmap(); | 422 return canvas.ExtractBitmap(); |
| 411 } | 423 } |
| 412 | 424 |
| 413 //////////////////////////////////////////////////////////////////////////////// | 425 //////////////////////////////////////////////////////////////////////////////// |
| 414 // NetworkMenu, views::ViewMenuDelegate implementation: | 426 // NetworkMenu, views::ViewMenuDelegate implementation: |
| 415 | 427 |
| 416 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { | 428 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { |
| 417 refreshing_menu_ = true; | 429 DCHECK_EQ(GetMenuButton(),source); |
| 430 | |
| 418 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 431 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 419 cros->RequestNetworkScan(); | 432 cros->RequestNetworkScan(); |
| 420 | 433 |
| 421 // Build initial menu items. They will be updated when UpdateMenu is | 434 UpdateMenu(); |
| 422 // called from NetworkChanged. | |
| 423 InitMenuItems(); | |
| 424 network_menu_->Rebuild(); | |
| 425 | 435 |
| 426 // Restore menu width, if it was set up. | 436 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
| 427 // NOTE: width isn't checked for correctness here since all width-related | 437 // top-level widget on the ChromeOS login screen that is a window. |
| 428 // logic implemented inside |network_menu_|. | 438 // The current BackgroundView class for the ChromeOS login screen |
| 429 if (min_width_ != -1) | 439 // creates a owning Widget that has a native GtkWindow but is not a |
| 430 network_menu_->SetMinimumWidth(min_width_); | 440 // Window. This makes it impossible to get the NativeWindow via |
| 431 refreshing_menu_ = false; | 441 // the views API. This workaround casts the top-level NativeWidget |
| 432 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 442 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
| 443 gfx::NativeWindow window; | |
| 444 if (source->GetWindow()) { | |
| 445 // This is the normal case with a browser. | |
| 446 window = source->GetWindow()->GetNativeWindow(); | |
| 447 } else { | |
| 448 #if defined(OS_WIN) | |
| 449 NOTREACHED(); | |
| 450 #elif defined(USE_X11) | |
| 451 window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
| 452 #endif | |
| 453 } | |
| 454 | |
| 455 gfx::Point screen_loc; | |
| 456 views::View::ConvertPointToScreen(source, &screen_loc); | |
| 457 gfx::Rect bounds(screen_loc, source->size()); | |
| 458 network_menu_->RunMenuAt( | |
| 459 window, | |
| 460 GetMenuButton(), | |
| 461 bounds, | |
| 462 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
| 463 true); | |
| 433 } | 464 } |
| 434 | 465 |
| 435 void NetworkMenu::InitMenuItems() { | 466 void NetworkMenu::InitMenuItems() { |
| 436 // This gets called on initialization, so any changes should be reflected | 467 // This gets called on initialization, so any changes should be reflected |
| 437 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 468 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 438 | 469 |
| 439 menu_items_.clear(); | 470 menu_items_.clear(); |
| 440 // Populate our MenuItems with the current list of wifi networks. | 471 // Populate our MenuItems with the current list of wifi networks. |
| 441 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 472 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 442 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 473 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 | 757 |
| 727 void NetworkMenu::ShowOtherWifi() const { | 758 void NetworkMenu::ShowOtherWifi() const { |
| 728 ShowNetworkConfigView(new NetworkConfigView()); | 759 ShowNetworkConfigView(new NetworkConfigView()); |
| 729 } | 760 } |
| 730 | 761 |
| 731 void NetworkMenu::ShowOtherCellular() const { | 762 void NetworkMenu::ShowOtherCellular() const { |
| 732 ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow()); | 763 ChooseMobileNetworkDialog::ShowDialog(GetNativeWindow()); |
| 733 } | 764 } |
| 734 | 765 |
| 735 } // namespace chromeos | 766 } // namespace chromeos |
| OLD | NEW |