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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h" | 13 #include "chrome/browser/chromeos/choose_mobile_network_dialog.h" |
| 14 #include "chrome/browser/chromeos/cros/cros_library.h" | 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 15 #include "chrome/browser/chromeos/sim_dialog_delegate.h" | 15 #include "chrome/browser/chromeos/sim_dialog_delegate.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/browser/ui/views/window.h" | 18 #include "chrome/browser/ui/views/window.h" |
| 19 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
| 23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/gfx/canvas_skia.h" | 26 #include "ui/gfx/canvas_skia.h" |
| 27 #include "ui/gfx/skbitmap_operations.h" | 27 #include "ui/gfx/skbitmap_operations.h" |
| 28 #include "views/controls/menu/menu_2.h" | 28 #include "views/controls/button/menu_button.h" |
| 29 #include "views/controls/menu/menu_item_view.h" | |
| 30 #include "views/controls/menu/submenu_view.h" | |
| 31 #include "views/widget/widget.h" | |
| 29 #include "views/window/window.h" | 32 #include "views/window/window.h" |
| 30 | 33 |
| 34 using views::MenuItemView; | |
| 35 | |
| 36 namespace { | |
| 37 | |
| 38 // Offset for VPN menu items. | |
| 39 const int kVPNCommandIndexOffset = 10000; | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 31 namespace chromeos { | 43 namespace chromeos { |
| 32 | 44 |
| 33 class MainMenuModel : public NetworkMenuModel { | 45 class MainMenuModel : public NetworkMenuModel { |
| 34 public: | 46 public: |
| 35 explicit MainMenuModel(NetworkMenu* owner); | 47 explicit MainMenuModel(NetworkMenu* owner); |
| 36 virtual ~MainMenuModel() {} | 48 virtual ~MainMenuModel() {} |
| 37 | 49 |
| 38 // NetworkMenuModel implementation. | 50 // NetworkMenuModel implementation ------------------------------------------- |
| 39 virtual void InitMenuItems(bool is_browser_mode, | 51 virtual void InitMenuItems(bool is_browser_mode, |
| 40 bool should_open_button_options); | 52 bool should_open_button_options) OVERRIDE; |
| 53 virtual void PopulateMenuItem(views::MenuItemView* menu, | |
| 54 int index, | |
| 55 int command_id) OVERRIDE; | |
| 56 | |
| 57 // MenuDelegate implementaion ------------------------------------------------ | |
| 58 virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE; | |
| 59 virtual bool IsItemChecked(int id) const OVERRIDE; | |
| 60 virtual bool IsCommandEnabled(int id) const OVERRIDE; | |
| 61 virtual void ExecuteCommand(int id) OVERRIDE; | |
| 41 | 62 |
| 42 private: | 63 private: |
| 43 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | 64 scoped_ptr<NetworkMenuModel> vpn_menu_model_; |
| 44 | 65 |
| 45 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | 66 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); |
| 46 }; | 67 }; |
| 47 | 68 |
| 48 class VPNMenuModel : public NetworkMenuModel { | 69 class VPNMenuModel : public NetworkMenuModel { |
| 49 public: | 70 public: |
| 50 explicit VPNMenuModel(NetworkMenu* owner); | 71 explicit VPNMenuModel(NetworkMenu* owner); |
| 51 virtual ~VPNMenuModel() {} | 72 virtual ~VPNMenuModel() {} |
| 52 | 73 |
| 53 // NetworkMenuModel implementation. | 74 // NetworkMenuModel implementation. |
| 54 virtual void InitMenuItems(bool is_browser_mode, | 75 virtual void InitMenuItems(bool is_browser_mode, |
| 55 bool should_open_button_options); | 76 bool should_open_button_options); |
| 77 virtual void PopulateMenuItem( | |
| 78 views::MenuItemView* menu, | |
| 79 int index, | |
| 80 int command_id) OVERRIDE; | |
| 56 | 81 |
| 57 static SkBitmap IconForDisplay(const Network* network); | 82 static SkBitmap IconForDisplay(const Network* network); |
| 58 | 83 |
| 59 private: | 84 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); | 85 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); |
| 61 }; | 86 }; |
| 62 | 87 |
| 63 //////////////////////////////////////////////////////////////////////////////// | 88 // NetworkMenuModel, public methods: ------------------------------------------- |
| 64 // NetworkMenuModel, public methods: | |
| 65 | 89 |
| 66 bool NetworkMenuModel::ConnectToNetworkAt(int index, | 90 bool NetworkMenuModel::ConnectToNetworkAt(int index, |
| 67 const std::string& passphrase, | 91 const std::string& passphrase, |
| 68 const std::string& ssid, | 92 const std::string& ssid, |
| 69 int auto_connect) const { | 93 int auto_connect) const { |
| 70 int flags = menu_items_[index].flags; | 94 int flags = menu_items_[index].flags; |
| 71 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 95 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 72 const std::string& service_path = menu_items_[index].service_path; | 96 const std::string& service_path = menu_items_[index].service_path; |
| 73 if (flags & FLAG_WIFI) { | 97 if (flags & FLAG_WIFI) { |
| 74 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); | 98 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 } else { | 174 } else { |
| 151 // If we are attempting to connect to a network that no longer exists, | 175 // If we are attempting to connect to a network that no longer exists, |
| 152 // display a notification. | 176 // display a notification. |
| 153 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; | 177 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; |
| 154 // TODO(stevenjb): Show notification. | 178 // TODO(stevenjb): Show notification. |
| 155 } | 179 } |
| 156 } | 180 } |
| 157 return true; | 181 return true; |
| 158 } | 182 } |
| 159 | 183 |
| 160 //////////////////////////////////////////////////////////////////////////////// | 184 // NetworkMenuModel, views::MenuDelegate implementation ------------------------ |
| 161 // NetworkMenuModel, ui::MenuModel implementation: | |
| 162 | 185 |
| 163 int NetworkMenuModel::GetItemCount() const { | 186 const gfx::Font& NetworkMenuModel::GetLabelFont(int id) const { |
| 164 return static_cast<int>(menu_items_.size()); | 187 DCHECK_LE(0, id); |
| 188 DCHECK_GT(static_cast<int>(menu_items_.size()), id); | |
| 189 return (menu_items_[id].flags & FLAG_ASSOCIATED) ? | |
| 190 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | |
| 191 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | |
| 165 } | 192 } |
| 166 | 193 |
| 167 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { | 194 bool NetworkMenuModel::IsItemChecked(int id) const { |
| 168 return menu_items_[index].type; | |
| 169 } | |
| 170 | |
| 171 string16 NetworkMenuModel::GetLabelAt(int index) const { | |
| 172 return menu_items_[index].label; | |
| 173 } | |
| 174 | |
| 175 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { | |
| 176 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? | |
| 177 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | |
| 178 NULL; | |
| 179 } | |
| 180 | |
| 181 bool NetworkMenuModel::IsItemCheckedAt(int index) const { | |
| 182 // All ui::MenuModel::TYPE_CHECK menu items are checked. | |
| 183 return true; | 195 return true; |
| 184 } | 196 } |
| 185 | 197 |
| 186 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { | 198 bool NetworkMenuModel::IsCommandEnabled(int id) const { |
| 187 if (!menu_items_[index].icon.empty()) { | 199 if (id < static_cast<int>(menu_items_.size())) |
| 188 *icon = menu_items_[index].icon; | 200 return !(menu_items_[id].flags & FLAG_DISABLED); |
| 201 else | |
| 189 return true; | 202 return true; |
| 190 } | |
| 191 return false; | |
| 192 } | 203 } |
| 193 | 204 |
| 194 bool NetworkMenuModel::IsEnabledAt(int index) const { | 205 void NetworkMenuModel::ExecuteCommand(int id) { |
| 195 return !(menu_items_[index].flags & FLAG_DISABLED); | 206 DCHECK_LE(0, id); |
| 196 } | 207 DCHECK_GT(static_cast<int>(menu_items_.size()), id); |
| 197 | |
| 198 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { | |
| 199 return menu_items_[index].sub_menu_model; | |
| 200 } | |
| 201 | |
| 202 void NetworkMenuModel::ActivatedAt(int index) { | |
| 203 // When we are refreshing the menu, ignore menu item activation. | 208 // When we are refreshing the menu, ignore menu item activation. |
| 204 if (owner_->refreshing_menu_) | 209 if (owner_->refreshing_menu_) |
| 205 return; | 210 return; |
| 206 | 211 |
| 207 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 212 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 208 int flags = menu_items_[index].flags; | 213 int flags = menu_items_[id].flags; |
| 209 if (flags & FLAG_OPTIONS) { | 214 if (flags & FLAG_OPTIONS) { |
| 210 owner_->OpenButtonOptions(); | 215 owner_->OpenButtonOptions(); |
| 211 } else if (flags & FLAG_TOGGLE_ETHERNET) { | 216 } else if (flags & FLAG_TOGGLE_ETHERNET) { |
| 212 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); | 217 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); |
| 213 } else if (flags & FLAG_TOGGLE_WIFI) { | 218 } else if (flags & FLAG_TOGGLE_WIFI) { |
| 214 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); | 219 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); |
| 215 } else if (flags & FLAG_TOGGLE_CELLULAR) { | 220 } else if (flags & FLAG_TOGGLE_CELLULAR) { |
| 216 const NetworkDevice* cellular = cros->FindCellularDevice(); | 221 const NetworkDevice* cellular = cros->FindCellularDevice(); |
| 217 if (!cellular) { | 222 if (!cellular) { |
| 218 LOG(ERROR) << "Not found cellular device, it should be available."; | 223 LOG(ERROR) << "Not found cellular device, it should be available."; |
| 219 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 224 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
| 220 } else { | 225 } else { |
| 221 if (cellular->sim_lock_state() == SIM_UNLOCKED || | 226 if (cellular->sim_lock_state() == SIM_UNLOCKED || |
| 222 cellular->sim_lock_state() == SIM_UNKNOWN) { | 227 cellular->sim_lock_state() == SIM_UNKNOWN) { |
| 223 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 228 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
| 224 } else { | 229 } else { |
| 225 SimDialogDelegate::ShowDialog(owner_->GetNativeWindow(), | 230 SimDialogDelegate::ShowDialog(owner_->GetNativeWindow(), |
| 226 SimDialogDelegate::SIM_DIALOG_UNLOCK); | 231 SimDialogDelegate::SIM_DIALOG_UNLOCK); |
| 227 } | 232 } |
| 228 } | 233 } |
| 229 } else if (flags & FLAG_TOGGLE_OFFLINE) { | 234 } else if (flags & FLAG_TOGGLE_OFFLINE) { |
| 230 cros->EnableOfflineMode(!cros->offline_mode()); | 235 cros->EnableOfflineMode(!cros->offline_mode()); |
| 231 } else if (flags & FLAG_ETHERNET) { | 236 } else if (flags & FLAG_ETHERNET) { |
| 232 if (cros->ethernet_connected()) { | 237 if (cros->ethernet_connected()) { |
| 233 ShowTabbedNetworkSettings(cros->ethernet_network()); | 238 ShowTabbedNetworkSettings(cros->ethernet_network()); |
| 234 } | 239 } |
| 235 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | | 240 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | |
| 236 FLAG_CELLULAR | FLAG_ADD_CELLULAR | | 241 FLAG_CELLULAR | FLAG_ADD_CELLULAR | |
| 237 FLAG_VPN | FLAG_ADD_VPN)) { | 242 FLAG_VPN | FLAG_ADD_VPN)) { |
| 238 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 243 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
| 239 } else if (flags & FLAG_DISCONNECT_VPN) { | 244 } else if (flags & FLAG_DISCONNECT_VPN) { |
| 240 const VirtualNetwork* active_vpn = cros->virtual_network(); | 245 const VirtualNetwork* active_vpn = cros->virtual_network(); |
| 241 if (active_vpn) | 246 if (active_vpn) |
| 242 cros->DisconnectFromNetwork(active_vpn); | 247 cros->DisconnectFromNetwork(active_vpn); |
| 243 } | 248 } |
| 244 } | 249 } |
| 245 | 250 |
| 246 //////////////////////////////////////////////////////////////////////////////// | 251 void NetworkMenuModel::PopulateMenu(views::MenuItemView* menu) { |
| 247 // NetworkMenuModel, private methods: | 252 menu->CreateSubmenu()->RemoveAllChildViews(true); |
| 253 const int menu_items_count = static_cast<int>(menu_items_.size()); | |
| 254 for (int i = 0; i < menu_items_count; ++i) | |
| 255 PopulateMenuItem(menu, i, i); | |
| 256 | |
| 257 menu->ChildrenChanged(); | |
| 258 } | |
| 259 | |
| 260 void NetworkMenuModel::PopulateMenuItem( | |
| 261 views::MenuItemView* menu, | |
| 262 int index, | |
| 263 int command_id) { | |
| 264 DCHECK_GT(static_cast<int>(menu_items_.size()), index); | |
| 265 const MenuItem& item = menu_items_[index]; | |
| 266 switch (item.type) { | |
| 267 case ui::MenuModel::TYPE_SEPARATOR: | |
| 268 menu->AppendSeparator(); | |
| 269 break; | |
| 270 case ui::MenuModel::TYPE_COMMAND: | |
| 271 if (item.icon.empty()) | |
| 272 menu->AppendMenuItemWithLabel(command_id, UTF16ToWide(item.label)); | |
| 273 else | |
| 274 menu->AppendMenuItemWithIcon( | |
| 275 command_id, | |
| 276 UTF16ToWide(item.label), | |
| 277 item.icon); | |
|
stevenjb
2011/04/21 22:25:31
nit: slightly more readable with { }
rhashimoto
2011/04/21 22:49:58
Done.
| |
| 278 break; | |
| 279 default: | |
| 280 // Submenus should be handled by subclasses. | |
| 281 NOTREACHED(); | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 // NetworkMenuModel, private methods: ------------------------------------------ | |
| 248 | 286 |
| 249 void NetworkMenuModel::ShowTabbedNetworkSettings(const Network* network) const { | 287 void NetworkMenuModel::ShowTabbedNetworkSettings(const Network* network) const { |
| 250 DCHECK(network); | 288 DCHECK(network); |
| 251 Browser* browser = BrowserList::GetLastActive(); | 289 Browser* browser = BrowserList::GetLastActive(); |
| 252 if (!browser) | 290 if (!browser) |
| 253 return; | 291 return; |
| 254 std::string page = StringPrintf("%s?servicePath=%s&networkType=%d", | 292 std::string page = StringPrintf("%s?servicePath=%s&networkType=%d", |
| 255 chrome::kInternetOptionsSubPage, | 293 chrome::kInternetOptionsSubPage, |
| 256 EscapeUrlEncodedData(network->service_path()).c_str(), | 294 EscapeUrlEncodedData(network->service_path()).c_str(), |
| 257 network->type()); | 295 network->type()); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 279 } | 317 } |
| 280 | 318 |
| 281 void NetworkMenuModel::ShowOther(ConnectionType type) const { | 319 void NetworkMenuModel::ShowOther(ConnectionType type) const { |
| 282 ShowNetworkConfigView(new NetworkConfigView(type)); | 320 ShowNetworkConfigView(new NetworkConfigView(type)); |
| 283 } | 321 } |
| 284 | 322 |
| 285 void NetworkMenuModel::ShowOtherCellular() const { | 323 void NetworkMenuModel::ShowOtherCellular() const { |
| 286 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); | 324 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); |
| 287 } | 325 } |
| 288 | 326 |
| 289 //////////////////////////////////////////////////////////////////////////////// | 327 // MainMenuModel --------------------------------------------------------------- |
| 290 // MainMenuModel | |
| 291 | 328 |
| 292 MainMenuModel::MainMenuModel(NetworkMenu* owner) | 329 MainMenuModel::MainMenuModel(NetworkMenu* owner) |
| 293 : NetworkMenuModel(owner) { | 330 : NetworkMenuModel(owner) { |
| 294 vpn_menu_model_.reset(new VPNMenuModel(owner)); | 331 vpn_menu_model_.reset(new VPNMenuModel(owner)); |
| 295 } | 332 } |
| 296 | 333 |
| 334 // MainMenuModel, NetworkMenuModel implementation: ----------------------------- | |
| 335 | |
| 297 void MainMenuModel::InitMenuItems(bool is_browser_mode, | 336 void MainMenuModel::InitMenuItems(bool is_browser_mode, |
| 298 bool should_open_button_options) { | 337 bool should_open_button_options) { |
| 299 // This gets called on initialization, so any changes should be reflected | 338 // This gets called on initialization, so any changes should be reflected |
| 300 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 339 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 301 | 340 |
| 302 menu_items_.clear(); | 341 menu_items_.clear(); |
| 303 | 342 |
| 304 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 343 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 305 if (cros->IsLocked()) { | 344 if (cros->IsLocked()) { |
| 306 menu_items_.push_back( | 345 menu_items_.push_back( |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 // Recursively call each submenu to populate its own menu items. | 626 // Recursively call each submenu to populate its own menu items. |
| 588 for (size_t i = 0; i < menu_items_.size(); ++i) { | 627 for (size_t i = 0; i < menu_items_.size(); ++i) { |
| 589 if (menu_items_[i].type == ui::MenuModel::TYPE_SUBMENU && | 628 if (menu_items_[i].type == ui::MenuModel::TYPE_SUBMENU && |
| 590 menu_items_[i].sub_menu_model) { | 629 menu_items_[i].sub_menu_model) { |
| 591 menu_items_[i].sub_menu_model->InitMenuItems(is_browser_mode, | 630 menu_items_[i].sub_menu_model->InitMenuItems(is_browser_mode, |
| 592 should_open_button_options); | 631 should_open_button_options); |
| 593 } | 632 } |
| 594 } | 633 } |
| 595 } | 634 } |
| 596 | 635 |
| 597 //////////////////////////////////////////////////////////////////////////////// | 636 // MainMenuModel, views::MenuDelegate implementation --------------------------- |
| 598 // VPNMenuModel | 637 |
| 638 const gfx::Font& MainMenuModel::GetLabelFont(int id) const { | |
| 639 if (id >= kVPNCommandIndexOffset) | |
| 640 return vpn_menu_model_->GetLabelFont(id - kVPNCommandIndexOffset); | |
| 641 else | |
| 642 return NetworkMenuModel::GetLabelFont(id); | |
| 643 } | |
| 644 | |
| 645 bool MainMenuModel::IsItemChecked(int id) const { | |
| 646 if (id >= kVPNCommandIndexOffset) | |
| 647 return vpn_menu_model_->IsItemChecked(id - kVPNCommandIndexOffset); | |
| 648 else | |
| 649 return NetworkMenuModel::IsItemChecked(id); | |
| 650 } | |
| 651 | |
| 652 bool MainMenuModel::IsCommandEnabled(int id) const { | |
| 653 if (id >= kVPNCommandIndexOffset) | |
| 654 return vpn_menu_model_->IsCommandEnabled(id - kVPNCommandIndexOffset); | |
| 655 else | |
| 656 return NetworkMenuModel::IsCommandEnabled(id); | |
| 657 } | |
| 658 | |
| 659 void MainMenuModel::ExecuteCommand(int id) { | |
| 660 if (id >= kVPNCommandIndexOffset) | |
| 661 vpn_menu_model_->ExecuteCommand(id - kVPNCommandIndexOffset); | |
| 662 else | |
| 663 NetworkMenuModel::ExecuteCommand(id); | |
| 664 } | |
| 665 | |
| 666 // MainMenuModel, NetworkMenuModel implementation: ----------------------------- | |
| 667 | |
| 668 void MainMenuModel::PopulateMenuItem( | |
| 669 views::MenuItemView* menu, | |
| 670 int index, | |
| 671 int command_id) { | |
| 672 DCHECK_GT(static_cast<int>(menu_items_.size()), index); | |
| 673 const MenuItem& item = menu_items_[index]; | |
| 674 if (item.type == ui::MenuModel::TYPE_SUBMENU) { | |
| 675 views::MenuItemView* vpn_menu = NULL; | |
| 676 if (item.icon.empty()) | |
| 677 vpn_menu = menu->AppendSubMenu(command_id, UTF16ToWide(item.label)); | |
| 678 else | |
| 679 vpn_menu = menu->AppendSubMenuWithIcon( | |
| 680 command_id, | |
| 681 UTF16ToWide(item.label), | |
| 682 item.icon); | |
|
stevenjb
2011/04/21 22:25:31
nit: {}
rhashimoto
2011/04/21 22:49:58
Done.
| |
| 683 vpn_menu_model_->PopulateMenu(vpn_menu); | |
| 684 } else { | |
| 685 NetworkMenuModel::PopulateMenuItem(menu, index, command_id); | |
| 686 } | |
| 687 } | |
| 688 | |
| 689 // VPNMenuModel ---------------------------------------------------------------- | |
| 599 | 690 |
| 600 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) | 691 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) |
| 601 : NetworkMenuModel(owner) { | 692 : NetworkMenuModel(owner) { |
| 602 } | 693 } |
| 603 | 694 |
| 695 // VPNMenuModel, NetworkMenuModel implementation: ------------------------------ | |
| 696 | |
| 604 void VPNMenuModel::InitMenuItems(bool is_browser_mode, | 697 void VPNMenuModel::InitMenuItems(bool is_browser_mode, |
| 605 bool should_open_button_options) { | 698 bool should_open_button_options) { |
| 606 // This gets called on initialization, so any changes should be reflected | 699 // This gets called on initialization, so any changes should be reflected |
| 607 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 700 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 608 | 701 |
| 609 menu_items_.clear(); | 702 menu_items_.clear(); |
| 610 | 703 |
| 611 // VPN only applies if there's a connected underlying network. | 704 // VPN only applies if there's a connected underlying network. |
| 612 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 705 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 613 const Network* connected_network = cros->connected_network(); | 706 const Network* connected_network = cros->connected_network(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 659 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), | 752 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), |
| 660 SkBitmap(), std::string(), FLAG_ADD_VPN)); | 753 SkBitmap(), std::string(), FLAG_ADD_VPN)); |
| 661 if (active_vpn) { | 754 if (active_vpn) { |
| 662 menu_items_.push_back(MenuItem( | 755 menu_items_.push_back(MenuItem( |
| 663 ui::MenuModel::TYPE_COMMAND, | 756 ui::MenuModel::TYPE_COMMAND, |
| 664 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), | 757 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), |
| 665 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); | 758 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); |
| 666 } | 759 } |
| 667 } | 760 } |
| 668 | 761 |
| 762 void VPNMenuModel::PopulateMenuItem( | |
| 763 views::MenuItemView* menu, | |
| 764 int index, | |
| 765 int command_id) { | |
| 766 NetworkMenuModel::PopulateMenuItem( | |
| 767 menu, | |
| 768 index, | |
| 769 command_id + kVPNCommandIndexOffset); | |
| 770 } | |
| 771 | |
| 669 // static | 772 // static |
| 670 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) { | 773 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) { |
| 671 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 774 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 672 const SkBitmap* icon = NULL; | 775 const SkBitmap* icon = NULL; |
| 673 const SkBitmap* bottom_right_badge = NULL; | 776 const SkBitmap* bottom_right_badge = NULL; |
| 674 const SkBitmap* top_left_badge = NULL; | 777 const SkBitmap* top_left_badge = NULL; |
| 675 // We know for sure |network| is the active network, so no more checking | 778 // We know for sure |network| is the active network, so no more checking |
| 676 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL. | 779 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL. |
| 677 const SkBitmap* bottom_left_badge = | 780 const SkBitmap* bottom_left_badge = |
| 678 NetworkMenu::BadgeForPrivateNetworkStatus(NULL); | 781 NetworkMenu::BadgeForPrivateNetworkStatus(NULL); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 746 | 849 |
| 747 // static | 850 // static |
| 748 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 851 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
| 749 | 852 |
| 750 // static | 853 // static |
| 751 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 854 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
| 752 | 855 |
| 753 NetworkMenu::NetworkMenu() | 856 NetworkMenu::NetworkMenu() |
| 754 : min_width_(-1) { | 857 : min_width_(-1) { |
| 755 main_menu_model_.reset(new MainMenuModel(this)); | 858 main_menu_model_.reset(new MainMenuModel(this)); |
| 756 network_menu_.reset(new views::Menu2(main_menu_model_.get())); | 859 network_menu_.reset(new views::MenuItemView(main_menu_model_.get())); |
| 860 network_menu_->set_has_icons(true); | |
| 757 } | 861 } |
| 758 | 862 |
| 759 NetworkMenu::~NetworkMenu() { | 863 NetworkMenu::~NetworkMenu() { |
| 760 } | 864 } |
| 761 | 865 |
| 762 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 866 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
| 763 min_width_ = width; | 867 min_width_ = width; |
| 764 // This actually has no effect since menu is rebuilt before showing. | |
| 765 network_menu_->SetMinimumWidth(width); | |
| 766 } | 868 } |
| 767 | 869 |
| 768 void NetworkMenu::CancelMenu() { | 870 void NetworkMenu::CancelMenu() { |
| 769 network_menu_->CancelMenu(); | 871 network_menu_->Cancel(); |
| 770 } | 872 } |
| 771 | 873 |
| 772 void NetworkMenu::UpdateMenu() { | 874 void NetworkMenu::UpdateMenu() { |
| 773 refreshing_menu_ = true; | 875 refreshing_menu_ = true; |
| 774 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); | 876 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); |
| 775 network_menu_->Rebuild(); | 877 main_menu_model_->PopulateMenu(network_menu_.get()); |
| 776 refreshing_menu_ = false; | 878 refreshing_menu_ = false; |
| 777 } | 879 } |
| 778 | 880 |
| 779 // static | 881 // static |
| 780 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, | 882 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, |
| 781 bool black) { | 883 bool black) { |
| 782 DCHECK(wifi); | 884 DCHECK(wifi); |
| 783 if (wifi->strength() == 0) { | 885 if (wifi->strength() == 0) { |
| 784 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 886 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 785 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : | 887 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 964 kBottomRightBadgeX, | 1066 kBottomRightBadgeX, |
| 965 kBottomRightBadgeY); | 1067 kBottomRightBadgeY); |
| 966 if (top_left_badge != NULL) | 1068 if (top_left_badge != NULL) |
| 967 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); | 1069 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); |
| 968 if (bottom_left_badge != NULL) | 1070 if (bottom_left_badge != NULL) |
| 969 canvas.DrawBitmapInt(*bottom_left_badge, kBottomLeftBadgeX, | 1071 canvas.DrawBitmapInt(*bottom_left_badge, kBottomLeftBadgeX, |
| 970 kBottomLeftBadgeY); | 1072 kBottomLeftBadgeY); |
| 971 return canvas.ExtractBitmap(); | 1073 return canvas.ExtractBitmap(); |
| 972 } | 1074 } |
| 973 | 1075 |
| 974 //////////////////////////////////////////////////////////////////////////////// | 1076 // NetworkMenu, views::ViewMenuDelegate implementation: ------------------------ |
| 975 // NetworkMenu, views::ViewMenuDelegate implementation: | |
| 976 | 1077 |
| 977 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { | 1078 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { |
| 978 refreshing_menu_ = true; | 1079 DCHECK_EQ(GetMenuButton(), source); |
| 979 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 1080 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 980 cros->RequestNetworkScan(); | 1081 cros->RequestNetworkScan(); |
| 981 | 1082 |
| 982 // Build initial menu items. They will be updated when UpdateMenu is | 1083 UpdateMenu(); |
| 983 // called from NetworkChanged. | |
| 984 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); | |
| 985 network_menu_->Rebuild(); | |
| 986 | 1084 |
| 987 // Restore menu width, if it was set up. | 1085 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
| 988 // NOTE: width isn't checked for correctness here since all width-related | 1086 // top-level widget on the ChromeOS login screen that is a window. |
| 989 // logic implemented inside |network_menu_|. | 1087 // The current BackgroundView class for the ChromeOS login screen |
| 990 if (min_width_ != -1) | 1088 // creates a owning Widget that has a native GtkWindow but is not a |
| 991 network_menu_->SetMinimumWidth(min_width_); | 1089 // Window. This makes it impossible to get the NativeWindow via |
| 992 refreshing_menu_ = false; | 1090 // the views API. This workaround casts the top-level NativeWidget |
| 993 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 1091 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
| 1092 gfx::NativeWindow window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
| 1093 | |
| 1094 gfx::Point screen_loc; | |
| 1095 views::View::ConvertPointToScreen(source, &screen_loc); | |
| 1096 gfx::Rect bounds(screen_loc, source->size()); | |
| 1097 network_menu_->RunMenuAt( | |
| 1098 window, | |
| 1099 GetMenuButton(), | |
| 1100 bounds, | |
| 1101 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
| 1102 true); | |
| 994 } | 1103 } |
| 995 | 1104 |
| 996 } // namespace chromeos | 1105 } // namespace chromeos |
| OLD | NEW |