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/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "chrome/browser/chromeos/sim_unlock_dialog_delegate.h" | 12 #include "chrome/browser/chromeos/sim_unlock_dialog_delegate.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
15 #include "chrome/browser/ui/views/window.h" | 15 #include "chrome/browser/ui/views/window.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
18 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
19 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/gfx/canvas_skia.h" | 22 #include "ui/gfx/canvas_skia.h" |
23 #include "ui/gfx/skbitmap_operations.h" | 23 #include "ui/gfx/skbitmap_operations.h" |
24 #include "views/controls/menu/menu_2.h" | 24 #include "views/controls/menu/menu_item_view.h" |
25 #include "views/controls/menu/submenu_view.h" | |
26 #include "views/widget/widget.h" | |
25 #include "views/window/window.h" | 27 #include "views/window/window.h" |
26 | 28 |
29 using views::MenuItemView; | |
30 | |
27 namespace { | 31 namespace { |
28 // Constants passed to Javascript: | 32 // Constants passed to Javascript: |
29 static const char* kNetworkTypeEthernet = "ethernet"; | 33 static const char* kNetworkTypeEthernet = "ethernet"; |
30 static const char* kNetworkTypeWifi = "wifi"; | 34 static const char* kNetworkTypeWifi = "wifi"; |
31 static const char* kNetworkTypeCellular = "cellular"; | 35 static const char* kNetworkTypeCellular = "cellular"; |
32 static const char* kNetworkTypeOther = "other"; | 36 static const char* kNetworkTypeOther = "other"; |
33 | 37 |
34 static const char* kNetworkStatusConnected = "connected"; | 38 static const char* kNetworkStatusConnected = "connected"; |
35 static const char* kNetworkStatusConnecting = "connecting"; | 39 static const char* kNetworkStatusConnecting = "connecting"; |
36 static const char* kNetworkStatusDisconnected = "disconnected"; | 40 static const char* kNetworkStatusDisconnected = "disconnected"; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 const int NetworkMenu::kNumAnimatingImages = 10; | 86 const int NetworkMenu::kNumAnimatingImages = 10; |
83 | 87 |
84 // static | 88 // static |
85 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 89 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
86 | 90 |
87 // static | 91 // static |
88 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 92 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
89 | 93 |
90 NetworkMenu::NetworkMenu() | 94 NetworkMenu::NetworkMenu() |
91 : min_width_(-1) { | 95 : min_width_(-1) { |
92 network_menu_.reset(new views::Menu2(this)); | 96 network_menu_.reset(new views::MenuItemView(this)); |
97 network_menu_->set_has_icons(true); | |
93 } | 98 } |
94 | 99 |
95 NetworkMenu::~NetworkMenu() { | 100 NetworkMenu::~NetworkMenu() { |
96 } | 101 } |
97 | 102 |
98 bool NetworkMenu::GetNetworkAt(int index, NetworkInfo* info) const { | 103 bool NetworkMenu::GetNetworkAt(int index, NetworkInfo* info) const { |
99 DCHECK(info); | 104 DCHECK(info); |
100 bool res = true; // True unless a network doesn't exist. | 105 bool res = true; // True unless a network doesn't exist. |
101 int flags = menu_items_[index].flags; | 106 int flags = menu_items_[index].flags; |
102 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 107 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 // display a notification. | 253 // display a notification. |
249 // TODO(stevenjb): Show notification. | 254 // TODO(stevenjb): Show notification. |
250 } | 255 } |
251 } else if (flags & FLAG_OTHER_NETWORK) { | 256 } else if (flags & FLAG_OTHER_NETWORK) { |
252 ShowOther(); | 257 ShowOther(); |
253 } | 258 } |
254 return true; | 259 return true; |
255 } | 260 } |
256 | 261 |
257 //////////////////////////////////////////////////////////////////////////////// | 262 //////////////////////////////////////////////////////////////////////////////// |
258 // NetworkMenu, ui::MenuModel implementation: | 263 // NetworkMenu, views::MenuItemView implementation: |
259 | 264 std::wstring NetworkMenu::GetLabel(int id) const { |
260 int NetworkMenu::GetItemCount() const { | 265 DCHECK(id >= 0 && id < static_cast<int>(menu_items_.size())); |
oshima
2011/04/14 17:27:31
use two DCHECK_LE (or _GT)
rhashimoto
2011/04/14 18:27:04
Done.
| |
261 return static_cast<int>(menu_items_.size()); | 266 return UTF16ToWide(menu_items_[id].label); |
262 } | 267 } |
263 | 268 |
264 ui::MenuModel::ItemType NetworkMenu::GetTypeAt(int index) const { | 269 const gfx::Font* NetworkMenu::GetLabelFont(int id) const |
265 return menu_items_[index].type; | 270 { |
271 return (menu_items_[id].flags & FLAG_ASSOCIATED) ? | |
272 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | |
273 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | |
266 } | 274 } |
267 | 275 |
268 string16 NetworkMenu::GetLabelAt(int index) const { | 276 bool NetworkMenu::IsCommandEnabled(int id) const { |
269 return menu_items_[index].label; | 277 if (id < static_cast<int>(menu_items_.size())) |
278 return !(menu_items_[id].flags & FLAG_DISABLED); | |
279 else | |
280 return false; | |
oshima
2011/04/14 17:27:31
can this happen? or should we use DCHECK?
rhashimoto
2011/04/14 18:27:04
This can happen. This method is called with 0 bef
oshima
2011/04/15 22:26:51
Hmm, that sounds odd. Can you check why this is ha
rhashimoto
2011/04/15 23:44:32
Commands are ids, not indices, and there is one na
oshima
2011/04/15 23:52:19
Yes, but i still don't understand why we're doing
| |
270 } | 281 } |
271 | 282 |
272 const gfx::Font* NetworkMenu::GetLabelFontAt(int index) const { | 283 bool NetworkMenu::IsItemChecked(int id) const { |
273 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? | 284 DCHECK(id >= 0 && id < static_cast<int>(menu_items_.size())); |
oshima
2011/04/14 17:27:31
same here
rhashimoto
2011/04/14 18:27:04
Done.
| |
274 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | 285 return menu_items_[id].type == ui::MenuModel::TYPE_CHECK; |
275 NULL; | |
276 } | 286 } |
277 | 287 |
278 bool NetworkMenu::IsItemCheckedAt(int index) const { | 288 void NetworkMenu::ExecuteCommand(int id) { |
279 // All ui::MenuModel::TYPE_CHECK menu items are checked. | 289 DCHECK(id >= 0 && id < static_cast<int>(menu_items_.size())); |
oshima
2011/04/14 17:27:31
same here
rhashimoto
2011/04/14 18:27:04
Done.
| |
280 return true; | |
281 } | |
282 | 290 |
283 bool NetworkMenu::GetIconAt(int index, SkBitmap* icon) { | |
284 if (!menu_items_[index].icon.empty()) { | |
285 *icon = menu_items_[index].icon; | |
286 return true; | |
287 } | |
288 return false; | |
289 } | |
290 | |
291 bool NetworkMenu::IsEnabledAt(int index) const { | |
292 return !(menu_items_[index].flags & FLAG_DISABLED); | |
293 } | |
294 | |
295 void NetworkMenu::ActivatedAt(int index) { | |
296 // When we are refreshing the menu, ignore menu item activation. | 291 // When we are refreshing the menu, ignore menu item activation. |
297 if (refreshing_menu_) | 292 if (refreshing_menu_) |
298 return; | 293 return; |
299 | 294 |
300 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 295 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
301 int flags = menu_items_[index].flags; | 296 int flags = menu_items_[id].flags; |
302 if (flags & FLAG_OPTIONS) { | 297 if (flags & FLAG_OPTIONS) { |
303 OpenButtonOptions(); | 298 OpenButtonOptions(); |
304 } else if (flags & FLAG_TOGGLE_ETHERNET) { | 299 } else if (flags & FLAG_TOGGLE_ETHERNET) { |
305 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); | 300 cros->EnableEthernetNetworkDevice(!cros->ethernet_enabled()); |
306 } else if (flags & FLAG_TOGGLE_WIFI) { | 301 } else if (flags & FLAG_TOGGLE_WIFI) { |
307 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); | 302 cros->EnableWifiNetworkDevice(!cros->wifi_enabled()); |
308 } else if (flags & FLAG_TOGGLE_CELLULAR) { | 303 } else if (flags & FLAG_TOGGLE_CELLULAR) { |
309 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 304 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
310 // TODO(nkostylev): Integrate with SIM locked state. crosbug.com/12007. | 305 // TODO(nkostylev): Integrate with SIM locked state. crosbug.com/12007. |
311 // SimUnlockDialogDelegate::ShowDialog(GetNativeWindow()); | 306 // SimUnlockDialogDelegate::ShowDialog(GetNativeWindow()); |
312 } else if (flags & FLAG_TOGGLE_OFFLINE) { | 307 } else if (flags & FLAG_TOGGLE_OFFLINE) { |
313 cros->EnableOfflineMode(!cros->offline_mode()); | 308 cros->EnableOfflineMode(!cros->offline_mode()); |
314 } else if (flags & FLAG_ETHERNET) { | 309 } else if (flags & FLAG_ETHERNET) { |
315 if (cros->ethernet_connected()) { | 310 if (cros->ethernet_connected()) { |
316 ShowTabbedNetworkSettings(cros->ethernet_network()); | 311 ShowTabbedNetworkSettings(cros->ethernet_network()); |
317 } | 312 } |
318 } else if (flags & FLAG_WIFI) { | 313 } else if (flags & FLAG_WIFI) { |
319 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 314 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
320 } else if (flags & FLAG_OTHER_NETWORK) { | 315 } else if (flags & FLAG_OTHER_NETWORK) { |
321 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 316 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
322 } else if (flags & FLAG_CELLULAR) { | 317 } else if (flags & FLAG_CELLULAR) { |
323 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 318 ConnectToNetworkAt(id, std::string(), std::string(), -1); |
324 } | 319 } |
325 } | 320 } |
326 | 321 |
327 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 322 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
328 min_width_ = width; | 323 min_width_ = width; |
329 // This actually has no effect since menu is rebuilt before showing. | 324 // This actually has no effect since menu is rebuilt before showing. |
330 network_menu_->SetMinimumWidth(width); | |
331 } | 325 } |
332 | 326 |
333 void NetworkMenu::CancelMenu() { | 327 void NetworkMenu::CancelMenu() { |
334 network_menu_->CancelMenu(); | 328 network_menu_->Cancel(); |
335 } | 329 } |
336 | 330 |
337 void NetworkMenu::UpdateMenu() { | 331 void NetworkMenu::UpdateMenu() { |
338 refreshing_menu_ = true; | 332 refreshing_menu_ = true; |
333 | |
334 // Rebuild list of available networks. | |
339 InitMenuItems(); | 335 InitMenuItems(); |
340 network_menu_->Rebuild(); | 336 |
337 // Clear and repopulate MenuItemView. | |
338 network_menu_->CreateSubmenu()->RemoveAllChildViews(true); | |
339 for (int i = 0, n = menu_items_.size(); i < n; ++i) | |
340 { | |
oshima
2011/04/14 17:27:31
move { to previous ine
rhashimoto
2011/04/14 18:27:04
Done.
| |
341 if (menu_items_[i].type == ui::MenuModel::TYPE_SEPARATOR) | |
342 network_menu_->AppendSeparator(); | |
343 else | |
344 network_menu_->AppendMenuItemWithIcon( | |
345 i, | |
346 UTF16ToWide(menu_items_[i].label), | |
347 menu_items_[i].icon); | |
348 } | |
349 | |
350 network_menu_->ChildrenChanged(); | |
341 refreshing_menu_ = false; | 351 refreshing_menu_ = false; |
342 } | 352 } |
343 | 353 |
344 // static | 354 // static |
345 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, | 355 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, |
346 bool black) { | 356 bool black) { |
347 DCHECK(wifi); | 357 DCHECK(wifi); |
348 if (wifi->strength() == 0) { | 358 if (wifi->strength() == 0) { |
349 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 359 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
350 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : | 360 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 kBottomRightBadgeY); | 523 kBottomRightBadgeY); |
514 if (top_left_badge != NULL) | 524 if (top_left_badge != NULL) |
515 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); | 525 canvas.DrawBitmapInt(*top_left_badge, kTopLeftBadgeX, kTopLeftBadgeY); |
516 return canvas.ExtractBitmap(); | 526 return canvas.ExtractBitmap(); |
517 } | 527 } |
518 | 528 |
519 //////////////////////////////////////////////////////////////////////////////// | 529 //////////////////////////////////////////////////////////////////////////////// |
520 // NetworkMenu, views::ViewMenuDelegate implementation: | 530 // NetworkMenu, views::ViewMenuDelegate implementation: |
521 | 531 |
522 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { | 532 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { |
523 refreshing_menu_ = true; | 533 DCHECK(source == GetMenuButton()); |
oshima
2011/04/14 17:27:31
DCHECK_EQ
rhashimoto
2011/04/14 18:27:04
Done.
| |
534 | |
524 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 535 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
525 cros->RequestNetworkScan(); | 536 cros->RequestNetworkScan(); |
526 | 537 |
527 // Build initial menu items. They will be updated when UpdateMenu is | 538 UpdateMenu(); |
528 // called from NetworkChanged. | |
529 InitMenuItems(); | |
530 network_menu_->Rebuild(); | |
531 | 539 |
532 // Restore menu width, if it was set up. | 540 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
533 // NOTE: width isn't checked for correctness here since all width-related | 541 // top-level widget on the ChromeOS login screen that is a window. |
534 // logic implemented inside |network_menu_|. | 542 // The current BackgroundView class for the ChromeOS login screen |
535 if (min_width_ != -1) | 543 // creates a owning Widget that has a native GtkWindow but is not a |
536 network_menu_->SetMinimumWidth(min_width_); | 544 // Window. This makes it impossible to get the NativeWindow via |
537 refreshing_menu_ = false; | 545 // the views API. This workaround casts the top-level NativeWidget |
538 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 546 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
547 gfx::NativeWindow window; | |
548 if (source->GetWindow()) { | |
549 // This is the normal case with a browser. | |
550 window = source->GetWindow()->GetNativeWindow(); | |
551 } else { | |
552 #if defined(OS_WIN) | |
553 NOTREACHED(); | |
554 #elif defined(USE_X11) | |
555 window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
556 #endif | |
557 } | |
558 | |
559 gfx::Point screen_loc; | |
560 views::View::ConvertPointToScreen(source, &screen_loc); | |
561 gfx::Rect bounds(screen_loc, source->size()); | |
562 network_menu_->RunMenuAt( | |
563 window, | |
564 GetMenuButton(), | |
565 bounds, | |
566 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, | |
567 true); | |
539 } | 568 } |
540 | 569 |
541 void NetworkMenu::InitMenuItems() { | 570 void NetworkMenu::InitMenuItems() { |
542 // This gets called on initialization, so any changes should be reflected | 571 // This gets called on initialization, so any changes should be reflected |
543 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 572 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
544 | 573 |
545 menu_items_.clear(); | 574 menu_items_.clear(); |
546 // Populate our MenuItems with the current list of wifi networks. | 575 // Populate our MenuItems with the current list of wifi networks. |
547 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 576 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
548 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 577 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
814 if (!browser) | 843 if (!browser) |
815 return; | 844 return; |
816 browser->OpenMobilePlanTabAndActivate(); | 845 browser->OpenMobilePlanTabAndActivate(); |
817 } | 846 } |
818 | 847 |
819 void NetworkMenu::ShowOther() const { | 848 void NetworkMenu::ShowOther() const { |
820 ShowNetworkConfigView(new NetworkConfigView()); | 849 ShowNetworkConfigView(new NetworkConfigView()); |
821 } | 850 } |
822 | 851 |
823 } // namespace chromeos | 852 } // namespace chromeos |
OLD | NEW |