| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
| 27 #include "grit/theme_resources.h" | 27 #include "grit/theme_resources.h" |
| 28 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 29 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/models/menu_model.h" | 31 #include "ui/base/models/menu_model.h" |
| 32 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/gfx/canvas_skia.h" | 33 #include "ui/gfx/canvas_skia.h" |
| 34 #include "ui/gfx/skbitmap_operations.h" | 34 #include "ui/gfx/skbitmap_operations.h" |
| 35 #include "views/controls/menu/menu_item_view.h" | 35 #include "views/controls/menu/menu_item_view.h" |
| 36 #include "views/controls/menu/menu_model_adapter.h" |
| 36 #include "views/controls/menu/submenu_view.h" | 37 #include "views/controls/menu/submenu_view.h" |
| 37 #include "views/widget/widget.h" | 38 #include "views/widget/widget.h" |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 // Offsets for views menu ids (main menu and submenu ids use the same | 42 // Offsets for views menu ids (main menu and submenu ids use the same |
| 42 // namespace). | 43 // namespace). |
| 43 const int kItemIndexMask = 0x0fff; | |
| 44 const int kMainIndexMask = 0x1000; | 44 const int kMainIndexMask = 0x1000; |
| 45 const int kVPNIndexMask = 0x2000; | 45 const int kVPNIndexMask = 0x2000; |
| 46 const int kMoreIndexMask = 0x4000; | 46 const int kMoreIndexMask = 0x4000; |
| 47 | 47 |
| 48 // Default minimum width in pixels of the menu to prevent unnecessary | 48 // Default minimum width in pixels of the menu to prevent unnecessary |
| 49 // resizing as networks are updated. | 49 // resizing as networks are updated. |
| 50 const int kDefaultMinimumWidth = 280; | 50 const int kDefaultMinimumWidth = 280; |
| 51 | 51 |
| 52 // Menu item margins. | 52 // Menu item margins. |
| 53 const int kTopMargin = 5; | 53 const int kTopMargin = 5; |
| 54 const int kBottomMargin = 7; | 54 const int kBottomMargin = 7; |
| 55 | 55 |
| 56 // Replace '&' in a string with "&&" to allow it to be a menu item label. | 56 // Replace '&' in a string with "&&" to allow it to be a menu item label. |
| 57 std::string EscapeAmpersands(const std::string& input) { | 57 std::string EscapeAmpersands(const std::string& input) { |
| 58 std::string str = input; | 58 std::string str = input; |
| 59 size_t found = str.find('&'); | 59 size_t found = str.find('&'); |
| 60 while (found != std::string::npos) { | 60 while (found != std::string::npos) { |
| 61 str.replace(found, 1, "&&"); | 61 str.replace(found, 1, "&&"); |
| 62 found = str.find('&', found + 2); | 62 found = str.find('&', found + 2); |
| 63 } | 63 } |
| 64 return str; | 64 return str; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Set vertical menu margins for entire menu hierarchy. |
| 68 void SetMenuMargins(views::MenuItemView* menu_item_view, int top, int bottom) { |
| 69 menu_item_view->set_margins(top, bottom); |
| 70 if (menu_item_view->HasSubmenu()) { |
| 71 views::SubmenuView* submenu = menu_item_view->GetSubmenu(); |
| 72 for (int i = 0; i < submenu->child_count(); ++i) { |
| 73 // Must skip separators. |
| 74 views::View* item = submenu->child_at(i); |
| 75 if (item->id() == views::MenuItemView::kMenuItemViewID) { |
| 76 views::MenuItemView* menu_item = |
| 77 static_cast<views::MenuItemView*>(item); |
| 78 SetMenuMargins(menu_item, top, bottom); |
| 79 } |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 67 } // namespace | 84 } // namespace |
| 68 | 85 |
| 69 namespace chromeos { | 86 namespace chromeos { |
| 70 | 87 |
| 71 class NetworkMenuModel : public views::MenuDelegate { | 88 class NetworkMenuModel : public ui::MenuModel { |
| 72 public: | 89 public: |
| 73 struct NetworkInfo { | 90 struct NetworkInfo { |
| 74 NetworkInfo() : need_passphrase(false), | 91 NetworkInfo() : need_passphrase(false), |
| 75 remembered(true), | 92 remembered(true), |
| 76 auto_connect(true) { | 93 auto_connect(true) { |
| 77 } | 94 } |
| 78 ~NetworkInfo() {} | 95 ~NetworkInfo() {} |
| 79 | 96 |
| 80 // "ethernet" | "wifi" | "cellular" | "other". | 97 // "ethernet" | "wifi" | "cellular" | "other". |
| 81 std::string network_type; | 98 std::string network_type; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 // If remember >= 0, set the favorite state of the network. | 152 // If remember >= 0, set the favorite state of the network. |
| 136 void ConnectToNetworkAt(int index, | 153 void ConnectToNetworkAt(int index, |
| 137 const std::string& passphrase, | 154 const std::string& passphrase, |
| 138 const std::string& ssid, | 155 const std::string& ssid, |
| 139 int remember) const; | 156 int remember) const; |
| 140 | 157 |
| 141 // Called by NetworkMenu::RunMenu to initialize list of menu items. | 158 // Called by NetworkMenu::RunMenu to initialize list of menu items. |
| 142 virtual void InitMenuItems(bool is_browser_mode, | 159 virtual void InitMenuItems(bool is_browser_mode, |
| 143 bool should_open_button_options) = 0; | 160 bool should_open_button_options) = 0; |
| 144 | 161 |
| 145 // PopulateMenu() clears and reinstalls the menu items defined in this | |
| 146 // instance by calling PopulateMenuItem() on each one. Subclasses override | |
| 147 // PopulateMenuItem(), transform command_id into the correct range for | |
| 148 // the menu, and call the base class PopulateMenuItem(). | |
| 149 virtual void PopulateMenu(views::MenuItemView* menu); | |
| 150 virtual void PopulateMenuItem(views::MenuItemView* menu, | |
| 151 int index, | |
| 152 int command_id); | |
| 153 | |
| 154 // Menu item field accessors. | 162 // Menu item field accessors. |
| 155 const MenuItemVector& menu_items() const { return menu_items_; } | 163 const MenuItemVector& menu_items() const { return menu_items_; } |
| 156 int GetItemCount() const; | 164 |
| 157 ui::MenuModel::ItemType GetTypeAt(int index) const; | 165 // ui::MenuModel implementation |
| 158 string16 GetLabelAt(int index) const; | 166 // GetCommandIdAt() must be implemented by subclasses. |
| 159 const gfx::Font* GetLabelFontAt(int index) const; | 167 virtual bool HasIcons() const OVERRIDE; |
| 160 bool IsItemCheckedAt(int index) const; | 168 virtual int GetItemCount() const OVERRIDE; |
| 161 bool GetIconAt(int index, SkBitmap* icon); | 169 virtual ui::MenuModel::ItemType GetTypeAt(int index) const OVERRIDE; |
| 162 bool IsEnabledAt(int index) const; | 170 virtual string16 GetLabelAt(int index) const OVERRIDE; |
| 163 NetworkMenuModel* GetSubmenuModelAt(int index) const; | 171 virtual bool IsItemDynamicAt(int index) const OVERRIDE; |
| 164 void ActivatedAt(int index); | 172 virtual const gfx::Font* GetLabelFontAt(int index) const OVERRIDE; |
| 173 virtual bool GetAcceleratorAt(int index, |
| 174 ui::Accelerator* accelerator) const OVERRIDE; |
| 175 virtual bool IsItemCheckedAt(int index) const OVERRIDE; |
| 176 virtual int GetGroupIdAt(int index) const OVERRIDE; |
| 177 virtual bool GetIconAt(int index, SkBitmap* icon) OVERRIDE; |
| 178 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt( |
| 179 int index) const OVERRIDE; |
| 180 virtual bool IsEnabledAt(int index) const OVERRIDE; |
| 181 virtual bool IsVisibleAt(int index) const OVERRIDE; |
| 182 virtual ui::MenuModel* GetSubmenuModelAt(int index) const OVERRIDE; |
| 183 virtual void HighlightChangedTo(int index) OVERRIDE; |
| 184 virtual void ActivatedAt(int index) OVERRIDE; |
| 185 virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) OVERRIDE; |
| 165 | 186 |
| 166 protected: | 187 protected: |
| 167 enum MenuItemFlags { | 188 enum MenuItemFlags { |
| 168 FLAG_NONE = 0, | 189 FLAG_NONE = 0, |
| 169 FLAG_DISABLED = 1 << 0, | 190 FLAG_DISABLED = 1 << 0, |
| 170 FLAG_TOGGLE_ETHERNET = 1 << 1, | 191 FLAG_TOGGLE_ETHERNET = 1 << 1, |
| 171 FLAG_TOGGLE_WIFI = 1 << 2, | 192 FLAG_TOGGLE_WIFI = 1 << 2, |
| 172 FLAG_TOGGLE_CELLULAR = 1 << 3, | 193 FLAG_TOGGLE_CELLULAR = 1 << 3, |
| 173 FLAG_TOGGLE_OFFLINE = 1 << 4, | 194 FLAG_TOGGLE_OFFLINE = 1 << 4, |
| 174 FLAG_ASSOCIATED = 1 << 5, | 195 FLAG_ASSOCIATED = 1 << 5, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 231 |
| 211 | 232 |
| 212 class MoreMenuModel : public NetworkMenuModel { | 233 class MoreMenuModel : public NetworkMenuModel { |
| 213 public: | 234 public: |
| 214 explicit MoreMenuModel(NetworkMenu* owner) : NetworkMenuModel(owner) {} | 235 explicit MoreMenuModel(NetworkMenu* owner) : NetworkMenuModel(owner) {} |
| 215 virtual ~MoreMenuModel() {} | 236 virtual ~MoreMenuModel() {} |
| 216 | 237 |
| 217 // NetworkMenuModel implementation. | 238 // NetworkMenuModel implementation. |
| 218 virtual void InitMenuItems(bool is_browser_mode, | 239 virtual void InitMenuItems(bool is_browser_mode, |
| 219 bool should_open_button_options) OVERRIDE; | 240 bool should_open_button_options) OVERRIDE; |
| 220 virtual void PopulateMenuItem(views::MenuItemView* menu, | 241 |
| 221 int index, | 242 // ui::MenuModel implementation |
| 222 int command_id) OVERRIDE; | 243 virtual int GetCommandIdAt(int index) const OVERRIDE; |
| 223 | 244 |
| 224 private: | 245 private: |
| 225 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); | 246 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); |
| 226 }; | 247 }; |
| 227 | 248 |
| 228 class VPNMenuModel : public NetworkMenuModel { | 249 class VPNMenuModel : public NetworkMenuModel { |
| 229 public: | 250 public: |
| 230 explicit VPNMenuModel(NetworkMenu* owner) : NetworkMenuModel(owner) {} | 251 explicit VPNMenuModel(NetworkMenu* owner) : NetworkMenuModel(owner) {} |
| 231 virtual ~VPNMenuModel() {} | 252 virtual ~VPNMenuModel() {} |
| 232 | 253 |
| 233 // NetworkMenuModel implementation. | 254 // NetworkMenuModel implementation. |
| 234 virtual void InitMenuItems(bool is_browser_mode, | 255 virtual void InitMenuItems(bool is_browser_mode, |
| 235 bool should_open_button_options) OVERRIDE; | 256 bool should_open_button_options) OVERRIDE; |
| 236 virtual void PopulateMenuItem(views::MenuItemView* menu, | 257 |
| 237 int index, | 258 // ui::MenuModel implementation |
| 238 int command_id) OVERRIDE; | 259 virtual int GetCommandIdAt(int index) const OVERRIDE; |
| 239 | 260 |
| 240 private: | 261 private: |
| 241 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); | 262 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); |
| 242 }; | 263 }; |
| 243 | 264 |
| 244 class MainMenuModel : public NetworkMenuModel { | 265 class MainMenuModel : public NetworkMenuModel { |
| 245 public: | 266 public: |
| 246 explicit MainMenuModel(NetworkMenu* owner) | 267 explicit MainMenuModel(NetworkMenu* owner) |
| 247 : NetworkMenuModel(owner), | 268 : NetworkMenuModel(owner), |
| 248 vpn_menu_model_(new VPNMenuModel(owner)), | 269 vpn_menu_model_(new VPNMenuModel(owner)), |
| 249 more_menu_model_(new MoreMenuModel(owner)) { | 270 more_menu_model_(new MoreMenuModel(owner)) { |
| 250 } | 271 } |
| 251 virtual ~MainMenuModel() {} | 272 virtual ~MainMenuModel() {} |
| 252 | 273 |
| 253 // NetworkMenuModel implementation. | 274 // NetworkMenuModel implementation. |
| 254 virtual void InitMenuItems(bool is_browser_mode, | 275 virtual void InitMenuItems(bool is_browser_mode, |
| 255 bool should_open_button_options) OVERRIDE; | 276 bool should_open_button_options) OVERRIDE; |
| 256 virtual void PopulateMenuItem(views::MenuItemView* menu, | |
| 257 int index, | |
| 258 int command_id) OVERRIDE; | |
| 259 | 277 |
| 260 // views::MenuDelegate implementation. | 278 // ui::MenuModel implementation |
| 261 virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE; | 279 virtual int GetCommandIdAt(int index) const OVERRIDE; |
| 262 virtual bool IsItemChecked(int id) const OVERRIDE; | |
| 263 virtual bool IsCommandEnabled(int id) const OVERRIDE; | |
| 264 virtual void ExecuteCommand(int id) OVERRIDE; | |
| 265 | 280 |
| 266 private: | 281 private: |
| 267 const NetworkMenuModel* GetMenuItemModel(int id) const; | |
| 268 | |
| 269 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | 282 scoped_ptr<NetworkMenuModel> vpn_menu_model_; |
| 270 scoped_ptr<MoreMenuModel> more_menu_model_; | 283 scoped_ptr<MoreMenuModel> more_menu_model_; |
| 271 | 284 |
| 272 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | 285 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); |
| 273 }; | 286 }; |
| 274 | 287 |
| 275 //////////////////////////////////////////////////////////////////////////////// | 288 //////////////////////////////////////////////////////////////////////////////// |
| 276 // NetworkMenuModel, public methods: | 289 // NetworkMenuModel, public methods: |
| 277 | 290 |
| 278 void NetworkMenuModel::PopulateMenu(views::MenuItemView* menu) { | |
| 279 if (menu->HasSubmenu()) { | |
| 280 const int old_count = menu->GetSubmenu()->child_count(); | |
| 281 for (int i = 0; i < old_count; ++i) | |
| 282 menu->RemoveMenuItemAt(0); | |
| 283 } | |
| 284 | |
| 285 const int menu_items_count = GetItemCount(); | |
| 286 for (int i = 0; i < menu_items_count; ++i) | |
| 287 PopulateMenuItem(menu, i, i); | |
| 288 } | |
| 289 | |
| 290 void NetworkMenuModel::PopulateMenuItem( | |
| 291 views::MenuItemView* menu, int index, int command_id) { | |
| 292 DCHECK_GT(GetItemCount(), index); | |
| 293 switch (GetTypeAt(index)) { | |
| 294 case ui::MenuModel::TYPE_SEPARATOR: | |
| 295 menu->AppendSeparator(); | |
| 296 break; | |
| 297 case ui::MenuModel::TYPE_COMMAND: { | |
| 298 views::MenuItemView* item = NULL; | |
| 299 SkBitmap icon; | |
| 300 if (GetIconAt(index, &icon)) { | |
| 301 item = menu->AppendMenuItemWithIcon(command_id, | |
| 302 UTF16ToWide(GetLabelAt(index)), | |
| 303 icon); | |
| 304 } else { | |
| 305 item = menu->AppendMenuItemWithLabel(command_id, | |
| 306 UTF16ToWide(GetLabelAt(index))); | |
| 307 } | |
| 308 item->set_margins(kTopMargin, kBottomMargin); | |
| 309 break; | |
| 310 } | |
| 311 case ui::MenuModel::TYPE_SUBMENU: { | |
| 312 views::MenuItemView* submenu = NULL; | |
| 313 SkBitmap icon; | |
| 314 if (GetIconAt(index, &icon)) { | |
| 315 submenu = menu->AppendSubMenuWithIcon(command_id, | |
| 316 UTF16ToWide(GetLabelAt(index)), | |
| 317 icon); | |
| 318 } else { | |
| 319 submenu = menu->AppendSubMenu(command_id, | |
| 320 UTF16ToWide(GetLabelAt(index))); | |
| 321 } | |
| 322 submenu->set_margins(kTopMargin, kBottomMargin); | |
| 323 GetSubmenuModelAt(index)->PopulateMenu(submenu); | |
| 324 break; | |
| 325 } | |
| 326 default: | |
| 327 NOTREACHED(); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 void NetworkMenuModel::ConnectToNetworkAt(int index, | 291 void NetworkMenuModel::ConnectToNetworkAt(int index, |
| 332 const std::string& passphrase, | 292 const std::string& passphrase, |
| 333 const std::string& ssid, | 293 const std::string& ssid, |
| 334 int auto_connect) const { | 294 int auto_connect) const { |
| 335 int flags = menu_items_[index].flags; | 295 int flags = menu_items_[index].flags; |
| 336 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 296 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 337 const std::string& service_path = menu_items_[index].service_path; | 297 const std::string& service_path = menu_items_[index].service_path; |
| 338 if (flags & FLAG_WIFI) { | 298 if (flags & FLAG_WIFI) { |
| 339 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); | 299 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); |
| 340 if (wifi) { | 300 if (wifi) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 // display a notification. | 369 // display a notification. |
| 410 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; | 370 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; |
| 411 // TODO(stevenjb): Show notification. | 371 // TODO(stevenjb): Show notification. |
| 412 } | 372 } |
| 413 } | 373 } |
| 414 } | 374 } |
| 415 | 375 |
| 416 //////////////////////////////////////////////////////////////////////////////// | 376 //////////////////////////////////////////////////////////////////////////////// |
| 417 // NetworkMenuModel, ui::MenuModel implementation: | 377 // NetworkMenuModel, ui::MenuModel implementation: |
| 418 | 378 |
| 379 bool NetworkMenuModel::HasIcons() const { |
| 380 return true; |
| 381 } |
| 382 |
| 419 int NetworkMenuModel::GetItemCount() const { | 383 int NetworkMenuModel::GetItemCount() const { |
| 420 return static_cast<int>(menu_items_.size()); | 384 return static_cast<int>(menu_items_.size()); |
| 421 } | 385 } |
| 422 | 386 |
| 423 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { | 387 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { |
| 424 return menu_items_[index].type; | 388 return menu_items_[index].type; |
| 425 } | 389 } |
| 426 | 390 |
| 427 string16 NetworkMenuModel::GetLabelAt(int index) const { | 391 string16 NetworkMenuModel::GetLabelAt(int index) const { |
| 428 return menu_items_[index].label; | 392 return menu_items_[index].label; |
| 429 } | 393 } |
| 430 | 394 |
| 395 bool NetworkMenuModel::IsItemDynamicAt(int index) const { |
| 396 return false; |
| 397 } |
| 398 |
| 431 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { | 399 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { |
| 432 const gfx::Font* font = NULL; | 400 const gfx::Font* font = NULL; |
| 433 if (menu_items_[index].flags & FLAG_ASSOCIATED) { | 401 if (menu_items_[index].flags & FLAG_ASSOCIATED) { |
| 434 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); | 402 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
| 435 font = &resource_bundle.GetFont( | 403 font = &resource_bundle.GetFont( |
| 436 browser_defaults::kAssociatedNetworkFontStyle); | 404 browser_defaults::kAssociatedNetworkFontStyle); |
| 437 } | 405 } |
| 438 | 406 |
| 439 return font; | 407 return font; |
| 440 } | 408 } |
| 441 | 409 |
| 410 bool NetworkMenuModel::GetAcceleratorAt(int index, |
| 411 ui::Accelerator* accelerator) const { |
| 412 return false; |
| 413 } |
| 414 |
| 442 bool NetworkMenuModel::IsItemCheckedAt(int index) const { | 415 bool NetworkMenuModel::IsItemCheckedAt(int index) const { |
| 443 // All ui::MenuModel::TYPE_CHECK menu items are checked. | 416 // All ui::MenuModel::TYPE_CHECK menu items are checked. |
| 444 return true; | 417 return true; |
| 445 } | 418 } |
| 446 | 419 |
| 420 int NetworkMenuModel::GetGroupIdAt(int index) const { |
| 421 return 0; |
| 422 } |
| 423 |
| 447 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { | 424 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { |
| 448 if (!menu_items_[index].icon.empty()) { | 425 if (!menu_items_[index].icon.empty()) { |
| 449 *icon = menu_items_[index].icon; | 426 *icon = menu_items_[index].icon; |
| 450 return true; | 427 return true; |
| 451 } | 428 } |
| 452 return false; | 429 return false; |
| 453 } | 430 } |
| 454 | 431 |
| 432 ui::ButtonMenuItemModel* NetworkMenuModel::GetButtonMenuItemAt( |
| 433 int index) const { |
| 434 return NULL; |
| 435 } |
| 436 |
| 455 bool NetworkMenuModel::IsEnabledAt(int index) const { | 437 bool NetworkMenuModel::IsEnabledAt(int index) const { |
| 456 return !(menu_items_[index].flags & FLAG_DISABLED); | 438 return !(menu_items_[index].flags & FLAG_DISABLED); |
| 457 } | 439 } |
| 458 | 440 |
| 459 NetworkMenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { | 441 bool NetworkMenuModel::IsVisibleAt(int index) const { |
| 442 return true; |
| 443 } |
| 444 |
| 445 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { |
| 460 return menu_items_[index].sub_menu_model; | 446 return menu_items_[index].sub_menu_model; |
| 461 } | 447 } |
| 462 | 448 |
| 449 void NetworkMenuModel::HighlightChangedTo(int index) { |
| 450 } |
| 451 |
| 463 void NetworkMenuModel::ActivatedAt(int index) { | 452 void NetworkMenuModel::ActivatedAt(int index) { |
| 464 // When we are refreshing the menu, ignore menu item activation. | 453 // When we are refreshing the menu, ignore menu item activation. |
| 465 if (owner_->refreshing_menu_) | 454 if (owner_->refreshing_menu_) |
| 466 return; | 455 return; |
| 467 | 456 |
| 468 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 457 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 469 int flags = menu_items_[index].flags; | 458 int flags = menu_items_[index].flags; |
| 470 if (flags & FLAG_OPTIONS) { | 459 if (flags & FLAG_OPTIONS) { |
| 471 owner_->delegate()->OpenButtonOptions(); | 460 owner_->delegate()->OpenButtonOptions(); |
| 472 } else if (flags & FLAG_TOGGLE_ETHERNET) { | 461 } else if (flags & FLAG_TOGGLE_ETHERNET) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 499 const VirtualNetwork* active_vpn = cros->virtual_network(); | 488 const VirtualNetwork* active_vpn = cros->virtual_network(); |
| 500 if (active_vpn) | 489 if (active_vpn) |
| 501 cros->DisconnectFromNetwork(active_vpn); | 490 cros->DisconnectFromNetwork(active_vpn); |
| 502 } else if (flags & FLAG_VIEW_ACCOUNT) { | 491 } else if (flags & FLAG_VIEW_ACCOUNT) { |
| 503 Browser* browser = BrowserList::GetLastActive(); | 492 Browser* browser = BrowserList::GetLastActive(); |
| 504 if (browser) | 493 if (browser) |
| 505 browser->ShowSingletonTab(GURL(top_up_url_)); | 494 browser->ShowSingletonTab(GURL(top_up_url_)); |
| 506 } | 495 } |
| 507 } | 496 } |
| 508 | 497 |
| 498 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { |
| 499 } |
| 500 |
| 509 //////////////////////////////////////////////////////////////////////////////// | 501 //////////////////////////////////////////////////////////////////////////////// |
| 510 // NetworkMenuModel, private methods: | 502 // NetworkMenuModel, private methods: |
| 511 | 503 |
| 512 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { | 504 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { |
| 513 views::Widget* window = browser::CreateViewsWindow( | 505 views::Widget* window = browser::CreateViewsWindow( |
| 514 owner_->delegate()->GetNativeWindow(), gfx::Rect(), view); | 506 owner_->delegate()->GetNativeWindow(), gfx::Rect(), view); |
| 515 window->SetAlwaysOnTop(true); | 507 window->SetAlwaysOnTop(true); |
| 516 window->Show(); | 508 window->Show(); |
| 517 } | 509 } |
| 518 | 510 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 529 ChooseMobileNetworkDialog::ShowDialog( | 521 ChooseMobileNetworkDialog::ShowDialog( |
| 530 owner_->delegate()->GetNativeWindow()); | 522 owner_->delegate()->GetNativeWindow()); |
| 531 } else { | 523 } else { |
| 532 ShowNetworkConfigView(new NetworkConfigView(type)); | 524 ShowNetworkConfigView(new NetworkConfigView(type)); |
| 533 } | 525 } |
| 534 } | 526 } |
| 535 | 527 |
| 536 //////////////////////////////////////////////////////////////////////////////// | 528 //////////////////////////////////////////////////////////////////////////////// |
| 537 // MainMenuModel | 529 // MainMenuModel |
| 538 | 530 |
| 539 const NetworkMenuModel* MainMenuModel::GetMenuItemModel(int id) const { | |
| 540 if (id & kMoreIndexMask) | |
| 541 return more_menu_model_.get(); | |
| 542 else if (id & kVPNIndexMask) | |
| 543 return vpn_menu_model_.get(); | |
| 544 else if (id & kMainIndexMask) | |
| 545 return this; | |
| 546 return NULL; | |
| 547 } | |
| 548 | |
| 549 void MainMenuModel::InitMenuItems(bool is_browser_mode, | 531 void MainMenuModel::InitMenuItems(bool is_browser_mode, |
| 550 bool should_open_button_options) { | 532 bool should_open_button_options) { |
| 551 // This gets called on initialization, so any changes should be reflected | 533 // This gets called on initialization, so any changes should be reflected |
| 552 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 534 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 553 | 535 |
| 554 menu_items_.clear(); | 536 menu_items_.clear(); |
| 555 | 537 |
| 556 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 538 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 557 if (cros->IsLocked()) { | 539 if (cros->IsLocked()) { |
| 558 menu_items_.push_back( | 540 menu_items_.push_back( |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 } else { | 839 } else { |
| 858 if (!more_menu_model_->menu_items().empty()) { | 840 if (!more_menu_model_->menu_items().empty()) { |
| 859 menu_items_.push_back(MenuItem( | 841 menu_items_.push_back(MenuItem( |
| 860 ui::MenuModel::TYPE_SUBMENU, | 842 ui::MenuModel::TYPE_SUBMENU, |
| 861 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), | 843 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), |
| 862 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); | 844 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); |
| 863 } | 845 } |
| 864 } | 846 } |
| 865 } | 847 } |
| 866 | 848 |
| 867 void MainMenuModel::PopulateMenuItem( | 849 int MainMenuModel::GetCommandIdAt(int index) const { |
| 868 views::MenuItemView* menu, int index, int command_id) { | 850 return index + kMainIndexMask; |
| 869 int main_command_id = command_id | kMainIndexMask; | |
| 870 NetworkMenuModel::PopulateMenuItem(menu, index, main_command_id); | |
| 871 } | |
| 872 | |
| 873 // views::MenuDelegate implementation. | |
| 874 | |
| 875 const gfx::Font& MainMenuModel::GetLabelFont(int id) const { | |
| 876 const NetworkMenuModel* model = GetMenuItemModel(id); | |
| 877 const gfx::Font* font = NULL; | |
| 878 if (model) | |
| 879 font = model->GetLabelFontAt(id & kItemIndexMask); | |
| 880 return font ? *font : views::MenuDelegate::GetLabelFont(id); | |
| 881 } | |
| 882 | |
| 883 bool MainMenuModel::IsItemChecked(int id) const { | |
| 884 const NetworkMenuModel* model = GetMenuItemModel(id); | |
| 885 if (model) | |
| 886 return model->IsItemCheckedAt(id & kItemIndexMask); | |
| 887 return views::MenuDelegate::IsItemChecked(id); | |
| 888 } | |
| 889 | |
| 890 bool MainMenuModel::IsCommandEnabled(int id) const { | |
| 891 const NetworkMenuModel* model = GetMenuItemModel(id); | |
| 892 if (model) | |
| 893 return model->IsEnabledAt(id & kItemIndexMask); | |
| 894 return views::MenuDelegate::IsCommandEnabled(id); | |
| 895 } | |
| 896 | |
| 897 // Not const, so can not use GetMenuItemModel(). | |
| 898 void MainMenuModel::ExecuteCommand(int id) { | |
| 899 int index = id & kItemIndexMask; | |
| 900 if (id & kMoreIndexMask) | |
| 901 return more_menu_model_->ActivatedAt(index); | |
| 902 else if (id & kVPNIndexMask) | |
| 903 return vpn_menu_model_->ActivatedAt(index); | |
| 904 else if (id & kMainIndexMask) | |
| 905 return ActivatedAt(index); | |
| 906 } | 851 } |
| 907 | 852 |
| 908 //////////////////////////////////////////////////////////////////////////////// | 853 //////////////////////////////////////////////////////////////////////////////// |
| 909 // VPNMenuModel | 854 // VPNMenuModel |
| 910 | 855 |
| 911 void VPNMenuModel::InitMenuItems(bool is_browser_mode, | 856 void VPNMenuModel::InitMenuItems(bool is_browser_mode, |
| 912 bool should_open_button_options) { | 857 bool should_open_button_options) { |
| 913 // This gets called on initialization, so any changes should be reflected | 858 // This gets called on initialization, so any changes should be reflected |
| 914 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 859 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 915 | 860 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 967 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), | 912 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), |
| 968 SkBitmap(), std::string(), FLAG_ADD_VPN)); | 913 SkBitmap(), std::string(), FLAG_ADD_VPN)); |
| 969 if (active_vpn) { | 914 if (active_vpn) { |
| 970 menu_items_.push_back(MenuItem( | 915 menu_items_.push_back(MenuItem( |
| 971 ui::MenuModel::TYPE_COMMAND, | 916 ui::MenuModel::TYPE_COMMAND, |
| 972 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), | 917 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), |
| 973 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); | 918 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); |
| 974 } | 919 } |
| 975 } | 920 } |
| 976 | 921 |
| 977 void VPNMenuModel::PopulateMenuItem( | 922 int VPNMenuModel::GetCommandIdAt(int index) const { |
| 978 views::MenuItemView* menu, int index, int command_id) { | 923 return index + kVPNIndexMask; |
| 979 int vpn_command_id = command_id | kVPNIndexMask; | |
| 980 NetworkMenuModel::PopulateMenuItem(menu, index, vpn_command_id); | |
| 981 } | 924 } |
| 982 | 925 |
| 983 //////////////////////////////////////////////////////////////////////////////// | 926 //////////////////////////////////////////////////////////////////////////////// |
| 984 // MoreMenuModel | 927 // MoreMenuModel |
| 985 | 928 |
| 986 void MoreMenuModel::InitMenuItems( | 929 void MoreMenuModel::InitMenuItems( |
| 987 bool is_browser_mode, bool should_open_button_options) { | 930 bool is_browser_mode, bool should_open_button_options) { |
| 988 // This gets called on initialization, so any changes should be reflected | 931 // This gets called on initialization, so any changes should be reflected |
| 989 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 932 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 990 | 933 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1052 } | 995 } |
| 1053 } | 996 } |
| 1054 | 997 |
| 1055 menu_items_ = link_items; | 998 menu_items_ = link_items; |
| 1056 if (!menu_items_.empty() && address_items.size() > 1) | 999 if (!menu_items_.empty() && address_items.size() > 1) |
| 1057 menu_items_.push_back(MenuItem()); // Separator | 1000 menu_items_.push_back(MenuItem()); // Separator |
| 1058 menu_items_.insert(menu_items_.end(), | 1001 menu_items_.insert(menu_items_.end(), |
| 1059 address_items.begin(), address_items.end()); | 1002 address_items.begin(), address_items.end()); |
| 1060 } | 1003 } |
| 1061 | 1004 |
| 1062 void MoreMenuModel::PopulateMenuItem( | 1005 int MoreMenuModel::GetCommandIdAt(int index) const { |
| 1063 views::MenuItemView* menu, int index, int command_id) { | 1006 return index + kMoreIndexMask; |
| 1064 int more_command_id = command_id | kMoreIndexMask; | |
| 1065 NetworkMenuModel::PopulateMenuItem(menu, index, more_command_id); | |
| 1066 } | 1007 } |
| 1067 | 1008 |
| 1068 //////////////////////////////////////////////////////////////////////////////// | 1009 //////////////////////////////////////////////////////////////////////////////// |
| 1069 // NetworkMenu | 1010 // NetworkMenu |
| 1070 | 1011 |
| 1071 NetworkMenu::NetworkMenu(Delegate* delegate, bool is_browser_mode) | 1012 NetworkMenu::NetworkMenu(Delegate* delegate, bool is_browser_mode) |
| 1072 : delegate_(delegate), | 1013 : delegate_(delegate), |
| 1073 is_browser_mode_(is_browser_mode), | 1014 is_browser_mode_(is_browser_mode), |
| 1074 refreshing_menu_(false), | 1015 refreshing_menu_(false), |
| 1075 min_width_(kDefaultMinimumWidth) { | 1016 min_width_(kDefaultMinimumWidth) { |
| 1076 main_menu_model_.reset(new MainMenuModel(this)); | 1017 main_menu_model_.reset(new MainMenuModel(this)); |
| 1077 menu_item_view_.reset(new views::MenuItemView(main_menu_model_.get())); | 1018 menu_model_adapter_.reset( |
| 1019 new views::MenuModelAdapter(main_menu_model_.get())); |
| 1020 menu_item_view_.reset(new views::MenuItemView(menu_model_adapter_.get())); |
| 1078 menu_item_view_->set_has_icons(true); | 1021 menu_item_view_->set_has_icons(true); |
| 1079 menu_item_view_->set_menu_position( | 1022 menu_item_view_->set_menu_position( |
| 1080 views::MenuItemView::POSITION_BELOW_BOUNDS); | 1023 views::MenuItemView::POSITION_BELOW_BOUNDS); |
| 1081 } | 1024 } |
| 1082 | 1025 |
| 1083 NetworkMenu::~NetworkMenu() { | 1026 NetworkMenu::~NetworkMenu() { |
| 1084 } | 1027 } |
| 1085 | 1028 |
| 1086 void NetworkMenu::CancelMenu() { | 1029 void NetworkMenu::CancelMenu() { |
| 1087 menu_item_view_->Cancel(); | 1030 menu_item_view_->Cancel(); |
| 1088 } | 1031 } |
| 1089 | 1032 |
| 1090 void NetworkMenu::UpdateMenu() { | 1033 void NetworkMenu::UpdateMenu() { |
| 1091 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1034 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1092 | 1035 |
| 1093 refreshing_menu_ = true; | 1036 refreshing_menu_ = true; |
| 1094 main_menu_model_->InitMenuItems( | 1037 main_menu_model_->InitMenuItems( |
| 1095 is_browser_mode(), delegate_->ShouldOpenButtonOptions()); | 1038 is_browser_mode(), delegate_->ShouldOpenButtonOptions()); |
| 1096 main_menu_model_->PopulateMenu(menu_item_view_.get()); | 1039 |
| 1040 menu_model_adapter_->BuildMenu(menu_item_view_.get()); |
| 1041 SetMenuMargins(menu_item_view_.get(), kTopMargin, kBottomMargin); |
| 1097 menu_item_view_->ChildrenChanged(); | 1042 menu_item_view_->ChildrenChanged(); |
| 1043 |
| 1098 refreshing_menu_ = false; | 1044 refreshing_menu_ = false; |
| 1099 } | 1045 } |
| 1100 | 1046 |
| 1101 void NetworkMenu::RunMenu(views::View* source) { | 1047 void NetworkMenu::RunMenu(views::View* source) { |
| 1102 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 1048 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 1103 cros->RequestNetworkScan(); | 1049 cros->RequestNetworkScan(); |
| 1104 | 1050 |
| 1105 UpdateMenu(); | 1051 UpdateMenu(); |
| 1106 | 1052 |
| 1107 gfx::Point screen_location; | 1053 gfx::Point screen_location; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1126 std::string page = StringPrintf( | 1072 std::string page = StringPrintf( |
| 1127 "%s?servicePath=%s&networkType=%d&networkName=%s", | 1073 "%s?servicePath=%s&networkType=%d&networkName=%s", |
| 1128 chrome::kInternetOptionsSubPage, | 1074 chrome::kInternetOptionsSubPage, |
| 1129 EscapeUrlEncodedData(network->service_path(), true).c_str(), | 1075 EscapeUrlEncodedData(network->service_path(), true).c_str(), |
| 1130 network->type(), | 1076 network->type(), |
| 1131 EscapeUrlEncodedData(network_name, false).c_str()); | 1077 EscapeUrlEncodedData(network_name, false).c_str()); |
| 1132 browser->ShowOptionsTab(page); | 1078 browser->ShowOptionsTab(page); |
| 1133 } | 1079 } |
| 1134 | 1080 |
| 1135 } // namespace chromeos | 1081 } // namespace chromeos |
| OLD | NEW |