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/customization_document.h" | 15 #include "chrome/browser/chromeos/customization_document.h" |
16 #include "chrome/browser/chromeos/sim_dialog_delegate.h" | 16 #include "chrome/browser/chromeos/sim_dialog_delegate.h" |
17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
19 #include "chrome/browser/ui/views/window.h" | 19 #include "chrome/browser/ui/views/window.h" |
20 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
23 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
24 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
26 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
27 #include "ui/gfx/canvas_skia.h" | 27 #include "ui/gfx/canvas_skia.h" |
28 #include "ui/gfx/skbitmap_operations.h" | 28 #include "ui/gfx/skbitmap_operations.h" |
29 #include "views/controls/menu/menu_2.h" | 29 #include "views/controls/menu/menu_item_view.h" |
30 #include "views/controls/menu/submenu_view.h" | |
30 #include "views/window/window.h" | 31 #include "views/window/window.h" |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 // Replace '&' in a string with "&&" to allow it to be a menu item label. | 35 // Replace '&' in a string with "&&" to allow it to be a menu item label. |
35 std::string EscapeAmpersands(const std::string& input) { | 36 std::string EscapeAmpersands(const std::string& input) { |
36 std::string str = input; | 37 std::string str = input; |
37 size_t found = str.find('&'); | 38 size_t found = str.find('&'); |
38 while (found != std::string::npos) { | 39 while (found != std::string::npos) { |
39 str.replace(found, 1, "&&"); | 40 str.replace(found, 1, "&&"); |
40 found = str.find('&', found + 2); | 41 found = str.find('&', found + 2); |
41 } | 42 } |
42 return str; | 43 return str; |
43 } | 44 } |
44 | 45 |
46 // Offsets for views menu ids (main menu and submenu ids use the same | |
47 // namespace). | |
48 const int kMainIndexOffset = 1000; | |
49 const int kVPNIndexOffset = 2000; | |
50 const int kMoreIndexOffset = 3000; | |
51 | |
52 // Default minimum width in pixels of the menu to prevent unnecessary | |
53 // resizing as networks are updated. | |
54 const int kDefaultMinimumWidth = 280; | |
55 | |
45 } // namespace | 56 } // namespace |
46 | 57 |
47 namespace chromeos { | 58 namespace chromeos { |
48 | 59 |
60 class NetworkMenuModel : public views::MenuDelegate { | |
61 public: | |
62 struct NetworkInfo { | |
63 NetworkInfo(); | |
64 ~NetworkInfo(); | |
65 | |
66 // "ethernet" | "wifi" | "cellular" | "other". | |
67 std::string network_type; | |
68 // "connected" | "connecting" | "disconnected" | "error". | |
69 std::string status; | |
70 // status message or error message, empty if unknown status. | |
71 std::string message; | |
72 // IP address (if network is active, empty otherwise) | |
73 std::string ip_address; | |
74 // Remembered passphrase. | |
75 std::string passphrase; | |
76 // true if the network requires a passphrase. | |
77 bool need_passphrase; | |
78 // true if the network is currently remembered. | |
79 bool remembered; | |
80 // true if the network is auto connect (meaningful for Wifi only). | |
81 bool auto_connect; | |
82 }; | |
83 | |
84 explicit NetworkMenuModel(NetworkMenu* owner); | |
85 virtual ~NetworkMenuModel(); | |
86 | |
87 // Connect or reconnect to the network at |index|. | |
88 // If remember >= 0, set the favorite state of the network. | |
89 // Returns true if a connect occurred (e.g. menu should be closed). | |
90 bool ConnectToNetworkAt(int index, | |
stevenjb
2011/05/18 17:36:15
I don't believe the return value for this is used
rhashimoto
2011/05/18 18:15:45
Done.
| |
91 const std::string& passphrase, | |
92 const std::string& ssid, | |
93 int remember) const; | |
94 | |
95 // Called by NetworkMenu::RunMenu to initialize list of menu items. | |
96 virtual void InitMenuItems(bool is_browser_mode, | |
97 bool should_open_button_options) = 0; | |
98 | |
99 // PopulateMenu() clears and reinstalls the menu items defined in this | |
100 // instance by calling PopulateMenuItem() on each one. Subclasses override | |
101 // PopulateMenuItem(), transform command_id into the correct range for | |
102 // the menu, and call the base class PopulateMenuItem(). | |
103 virtual void PopulateMenu(views::MenuItemView* menu); | |
104 virtual void PopulateMenuItem( | |
105 views::MenuItemView* menu, | |
106 int index, | |
107 int command_id); | |
108 | |
109 // Menu item field accessors. | |
110 int GetItemCount() const; | |
111 ui::MenuModel::ItemType GetTypeAt(int index) const; | |
112 string16 GetLabelAt(int index) const; | |
113 const gfx::Font* GetLabelFontAt(int index) const; | |
114 bool IsItemCheckedAt(int index) const; | |
115 bool GetIconAt(int index, SkBitmap* icon); | |
116 bool IsEnabledAt(int index) const; | |
117 NetworkMenuModel* GetSubmenuModelAt(int index) const; | |
118 void ActivatedAt(int index); | |
119 | |
120 protected: | |
121 enum MenuItemFlags { | |
122 FLAG_NONE = 0, | |
123 FLAG_DISABLED = 1 << 0, | |
124 FLAG_TOGGLE_ETHERNET = 1 << 1, | |
125 FLAG_TOGGLE_WIFI = 1 << 2, | |
126 FLAG_TOGGLE_CELLULAR = 1 << 3, | |
127 FLAG_TOGGLE_OFFLINE = 1 << 4, | |
128 FLAG_ASSOCIATED = 1 << 5, | |
129 FLAG_ETHERNET = 1 << 6, | |
130 FLAG_WIFI = 1 << 7, | |
131 FLAG_CELLULAR = 1 << 8, | |
132 FLAG_OPTIONS = 1 << 9, | |
133 FLAG_ADD_WIFI = 1 << 10, | |
134 FLAG_ADD_CELLULAR = 1 << 11, | |
135 FLAG_VPN = 1 << 12, | |
136 FLAG_ADD_VPN = 1 << 13, | |
137 FLAG_DISCONNECT_VPN = 1 << 14, | |
138 FLAG_VIEW_ACCOUNT = 1 << 15, | |
139 }; | |
140 | |
141 struct MenuItem { | |
142 MenuItem() | |
143 : type(ui::MenuModel::TYPE_SEPARATOR), | |
144 sub_menu_model(NULL), | |
145 flags(0) {} | |
146 MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon, | |
147 const std::string& service_path, int flags) | |
148 : type(type), | |
149 label(label), | |
150 icon(icon), | |
151 service_path(service_path), | |
152 sub_menu_model(NULL), | |
153 flags(flags) {} | |
154 MenuItem(ui::MenuModel::ItemType type, string16 label, SkBitmap icon, | |
155 NetworkMenuModel* sub_menu_model, int flags) | |
156 : type(type), | |
157 label(label), | |
158 icon(icon), | |
159 sub_menu_model(sub_menu_model), | |
160 flags(flags) {} | |
161 | |
162 ui::MenuModel::ItemType type; | |
163 string16 label; | |
164 SkBitmap icon; | |
165 std::string service_path; | |
166 NetworkMenuModel* sub_menu_model; // Weak. | |
167 int flags; | |
168 }; | |
169 typedef std::vector<MenuItem> MenuItemVector; | |
170 | |
171 // Our menu items. | |
172 MenuItemVector menu_items_; | |
173 | |
174 NetworkMenu* owner_; // Weak pointer to NetworkMenu that owns this MenuModel. | |
175 | |
176 // Top up URL of the current carrier on empty string if there's none. | |
177 std::string top_up_url_; | |
178 | |
179 // Carrier ID which top up URL is initialized for. | |
180 // Used to update top up URL only when cellular carrier has changed. | |
181 std::string carrier_id_; | |
182 | |
183 private: | |
184 // Show a NetworkConfigView modal dialog instance. | |
185 void ShowNetworkConfigView(NetworkConfigView* view) const; | |
186 | |
187 void ActivateCellular(const CellularNetwork* cellular) const; | |
188 void ShowOther(ConnectionType type) const; | |
189 void ShowOtherCellular() const; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel); | |
192 }; | |
193 | |
194 | |
49 class MoreMenuModel : public NetworkMenuModel { | 195 class MoreMenuModel : public NetworkMenuModel { |
50 public: | 196 public: |
51 explicit MoreMenuModel(NetworkMenu* owner); | 197 explicit MoreMenuModel(NetworkMenu* owner); |
52 virtual ~MoreMenuModel() {} | 198 virtual ~MoreMenuModel() {} |
53 | 199 |
54 // NetworkMenuModel implementation. | 200 // NetworkMenuModel implementation. |
55 virtual void InitMenuItems(bool is_browser_mode, | 201 virtual void InitMenuItems(bool is_browser_mode, |
56 bool should_open_button_options); | 202 bool should_open_button_options) OVERRIDE; |
203 virtual void PopulateMenuItem( | |
204 views::MenuItemView* menu, | |
205 int index, | |
206 int command_id) OVERRIDE; | |
stevenjb
2011/05/18 17:36:15
nit: use same indentation style for above function
rhashimoto
2011/05/18 18:15:45
Done.
| |
57 | 207 |
58 private: | 208 private: |
59 friend class MainMenuModel; | 209 friend class MainMenuModel; |
60 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); | 210 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); |
61 }; | 211 }; |
62 | 212 |
63 class VPNMenuModel : public NetworkMenuModel { | 213 class VPNMenuModel : public NetworkMenuModel { |
64 public: | 214 public: |
65 explicit VPNMenuModel(NetworkMenu* owner); | 215 explicit VPNMenuModel(NetworkMenu* owner); |
66 virtual ~VPNMenuModel() {} | 216 virtual ~VPNMenuModel() {} |
67 | 217 |
68 // NetworkMenuModel implementation. | 218 // NetworkMenuModel implementation. |
69 virtual void InitMenuItems(bool is_browser_mode, | 219 virtual void InitMenuItems(bool is_browser_mode, |
70 bool should_open_button_options); | 220 bool should_open_button_options) OVERRIDE; |
221 virtual void PopulateMenuItem( | |
222 views::MenuItemView* menu, | |
223 int index, | |
224 int command_id) OVERRIDE; | |
stevenjb
2011/05/18 17:36:15
nit: use same indentation style for above function
rhashimoto
2011/05/18 18:15:45
Done.
| |
71 | 225 |
72 static SkBitmap IconForDisplay(const Network* network); | 226 static SkBitmap IconForDisplay(const Network* network); |
73 | 227 |
74 private: | 228 private: |
75 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); | 229 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); |
76 }; | 230 }; |
77 | 231 |
78 class MainMenuModel : public NetworkMenuModel { | 232 class MainMenuModel : public NetworkMenuModel { |
79 public: | 233 public: |
80 explicit MainMenuModel(NetworkMenu* owner); | 234 explicit MainMenuModel(NetworkMenu* owner); |
81 virtual ~MainMenuModel() {} | 235 virtual ~MainMenuModel() {} |
82 | 236 |
83 // NetworkMenuModel implementation. | 237 // NetworkMenuModel implementation. |
84 virtual void InitMenuItems(bool is_browser_mode, | 238 virtual void InitMenuItems(bool is_browser_mode, |
85 bool should_open_button_options); | 239 bool should_open_button_options) OVERRIDE; |
240 virtual void PopulateMenuItem( | |
241 views::MenuItemView* menu, | |
242 int index, | |
243 int command_id) OVERRIDE; | |
stevenjb
2011/05/18 17:36:15
nit: use same indentation style for above function
rhashimoto
2011/05/18 18:15:45
Done.
| |
244 | |
245 // views::MenuDelegate implementation. | |
246 virtual const gfx::Font& GetLabelFont(int id) const OVERRIDE; | |
247 virtual bool IsItemChecked(int id) const OVERRIDE; | |
248 virtual bool IsCommandEnabled(int id) const OVERRIDE; | |
249 virtual void ExecuteCommand(int id) OVERRIDE; | |
86 | 250 |
87 private: | 251 private: |
88 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | 252 scoped_ptr<NetworkMenuModel> vpn_menu_model_; |
89 scoped_ptr<MoreMenuModel> more_menu_model_; | 253 scoped_ptr<MoreMenuModel> more_menu_model_; |
90 | 254 |
91 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | 255 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); |
92 }; | 256 }; |
93 | 257 |
94 //////////////////////////////////////////////////////////////////////////////// | 258 //////////////////////////////////////////////////////////////////////////////// |
95 // NetworkMenuModel::NetworkInfo | 259 // NetworkMenuModel::NetworkInfo |
96 | 260 |
97 NetworkMenuModel::NetworkInfo::NetworkInfo() | 261 NetworkMenuModel::NetworkInfo::NetworkInfo() |
98 : need_passphrase(false), remembered(true), auto_connect(true) { | 262 : need_passphrase(false), remembered(true), auto_connect(true) { |
99 } | 263 } |
100 | 264 |
101 NetworkMenuModel::NetworkInfo::~NetworkInfo() {} | 265 NetworkMenuModel::NetworkInfo::~NetworkInfo() {} |
102 | 266 |
103 //////////////////////////////////////////////////////////////////////////////// | 267 //////////////////////////////////////////////////////////////////////////////// |
104 // NetworkMenuModel, public methods: | 268 // NetworkMenuModel, public methods: |
105 | 269 |
106 NetworkMenuModel::NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {} | 270 NetworkMenuModel::NetworkMenuModel(NetworkMenu* owner) : owner_(owner) {} |
107 | 271 |
108 NetworkMenuModel::~NetworkMenuModel() {} | 272 NetworkMenuModel::~NetworkMenuModel() {} |
109 | 273 |
274 void NetworkMenuModel::PopulateMenu(views::MenuItemView* menu) { | |
275 const int old_count = menu->CreateSubmenu()->child_count(); | |
stevenjb
2011/05/18 17:36:15
CreateSubmenu() here is confusing since we are jus
rhashimoto
2011/05/18 18:15:45
Done.
| |
276 for (int i = 0; i < old_count; ++i) | |
277 menu->RemoveMenuItemAt(0); | |
278 | |
279 const int menu_items_count = GetItemCount(); | |
280 for (int i = 0; i < menu_items_count; ++i) | |
281 PopulateMenuItem(menu, i, i); | |
282 } | |
283 | |
284 void NetworkMenuModel::PopulateMenuItem( | |
285 views::MenuItemView* menu, | |
286 int index, | |
287 int command_id) { | |
288 DCHECK_GT(GetItemCount(), index); | |
289 switch (GetTypeAt(index)) { | |
290 case ui::MenuModel::TYPE_SEPARATOR: | |
291 menu->AppendSeparator(); | |
292 break; | |
293 case ui::MenuModel::TYPE_COMMAND: { | |
294 SkBitmap icon; | |
295 if (GetIconAt(index, &icon)) { | |
296 menu->AppendMenuItemWithIcon( | |
297 command_id, | |
298 UTF16ToWide(GetLabelAt(index)), | |
299 icon); | |
300 } else { | |
301 menu->AppendMenuItemWithLabel(command_id, | |
302 UTF16ToWide(GetLabelAt(index))); | |
303 } | |
stevenjb
2011/05/18 17:36:15
nit: use same indentation style for each if clause
rhashimoto
2011/05/18 18:15:45
Done.
| |
304 break; | |
305 } | |
306 case ui::MenuModel::TYPE_SUBMENU: { | |
307 views::MenuItemView* submenu = NULL; | |
308 SkBitmap icon; | |
309 if (GetIconAt(index, &icon)) { | |
310 submenu = menu->AppendSubMenuWithIcon(command_id, | |
311 UTF16ToWide(GetLabelAt(index)), | |
312 icon); | |
313 } else { | |
314 submenu = menu->AppendSubMenu(command_id, | |
315 UTF16ToWide(GetLabelAt(index))); | |
316 } | |
317 GetSubmenuModelAt(index)->PopulateMenu(submenu); | |
318 break; | |
319 } | |
320 default: | |
321 NOTREACHED(); | |
322 } | |
323 } | |
324 | |
110 bool NetworkMenuModel::ConnectToNetworkAt(int index, | 325 bool NetworkMenuModel::ConnectToNetworkAt(int index, |
111 const std::string& passphrase, | 326 const std::string& passphrase, |
112 const std::string& ssid, | 327 const std::string& ssid, |
113 int auto_connect) const { | 328 int auto_connect) const { |
114 int flags = menu_items_[index].flags; | 329 int flags = menu_items_[index].flags; |
115 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 330 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
116 const std::string& service_path = menu_items_[index].service_path; | 331 const std::string& service_path = menu_items_[index].service_path; |
117 if (flags & FLAG_WIFI) { | 332 if (flags & FLAG_WIFI) { |
118 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); | 333 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); |
119 if (wifi) { | 334 if (wifi) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; | 412 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; |
198 // TODO(stevenjb): Show notification. | 413 // TODO(stevenjb): Show notification. |
199 } | 414 } |
200 } | 415 } |
201 return true; | 416 return true; |
202 } | 417 } |
203 | 418 |
204 //////////////////////////////////////////////////////////////////////////////// | 419 //////////////////////////////////////////////////////////////////////////////// |
205 // NetworkMenuModel, ui::MenuModel implementation: | 420 // NetworkMenuModel, ui::MenuModel implementation: |
206 | 421 |
207 bool NetworkMenuModel::HasIcons() const { | |
208 return true; | |
209 } | |
210 | |
211 int NetworkMenuModel::GetItemCount() const { | 422 int NetworkMenuModel::GetItemCount() const { |
212 return static_cast<int>(menu_items_.size()); | 423 return static_cast<int>(menu_items_.size()); |
213 } | 424 } |
214 | 425 |
215 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { | 426 ui::MenuModel::ItemType NetworkMenuModel::GetTypeAt(int index) const { |
216 return menu_items_[index].type; | 427 return menu_items_[index].type; |
217 } | 428 } |
218 | 429 |
219 int NetworkMenuModel::GetCommandIdAt(int index) const { | |
220 return index; | |
221 } | |
222 | |
223 string16 NetworkMenuModel::GetLabelAt(int index) const { | 430 string16 NetworkMenuModel::GetLabelAt(int index) const { |
224 return menu_items_[index].label; | 431 return menu_items_[index].label; |
225 } | 432 } |
226 | 433 |
227 bool NetworkMenuModel::IsItemDynamicAt(int index) const { | |
228 return true; | |
229 } | |
230 | |
231 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { | 434 const gfx::Font* NetworkMenuModel::GetLabelFontAt(int index) const { |
232 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? | 435 return (menu_items_[index].flags & FLAG_ASSOCIATED) ? |
233 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : | 436 &ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BoldFont) : |
234 NULL; | 437 NULL; |
235 } | 438 } |
236 | 439 |
237 bool NetworkMenuModel::GetAcceleratorAt( | |
238 int index, ui::Accelerator* accelerator) const { | |
239 return false; | |
240 } | |
241 | |
242 bool NetworkMenuModel::IsItemCheckedAt(int index) const { | 440 bool NetworkMenuModel::IsItemCheckedAt(int index) const { |
243 // All ui::MenuModel::TYPE_CHECK menu items are checked. | 441 // All ui::MenuModel::TYPE_CHECK menu items are checked. |
244 return true; | 442 return true; |
245 } | 443 } |
246 | 444 |
247 int NetworkMenuModel::GetGroupIdAt(int index) const { | |
248 return 0; | |
249 } | |
250 | |
251 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { | 445 bool NetworkMenuModel::GetIconAt(int index, SkBitmap* icon) { |
252 if (!menu_items_[index].icon.empty()) { | 446 if (!menu_items_[index].icon.empty()) { |
253 *icon = menu_items_[index].icon; | 447 *icon = menu_items_[index].icon; |
254 return true; | 448 return true; |
255 } | 449 } |
256 return false; | 450 return false; |
257 } | 451 } |
258 | 452 |
259 ui::ButtonMenuItemModel* NetworkMenuModel::GetButtonMenuItemAt( | |
260 int index) const { | |
261 return NULL; | |
262 } | |
263 | |
264 bool NetworkMenuModel::IsEnabledAt(int index) const { | 453 bool NetworkMenuModel::IsEnabledAt(int index) const { |
265 return !(menu_items_[index].flags & FLAG_DISABLED); | 454 return !(menu_items_[index].flags & FLAG_DISABLED); |
266 } | 455 } |
267 | 456 |
268 ui::MenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { | 457 NetworkMenuModel* NetworkMenuModel::GetSubmenuModelAt(int index) const { |
269 return menu_items_[index].sub_menu_model; | 458 return menu_items_[index].sub_menu_model; |
270 } | 459 } |
271 | 460 |
272 void NetworkMenuModel::HighlightChangedTo(int index) {} | |
273 | |
274 void NetworkMenuModel::ActivatedAt(int index) { | 461 void NetworkMenuModel::ActivatedAt(int index) { |
275 // When we are refreshing the menu, ignore menu item activation. | 462 // When we are refreshing the menu, ignore menu item activation. |
276 if (owner_->refreshing_menu_) | 463 if (owner_->refreshing_menu_) |
277 return; | 464 return; |
278 | 465 |
279 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 466 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
280 int flags = menu_items_[index].flags; | 467 int flags = menu_items_[index].flags; |
281 if (flags & FLAG_OPTIONS) { | 468 if (flags & FLAG_OPTIONS) { |
282 owner_->OpenButtonOptions(); | 469 owner_->OpenButtonOptions(); |
283 } else if (flags & FLAG_TOGGLE_ETHERNET) { | 470 } else if (flags & FLAG_TOGGLE_ETHERNET) { |
(...skipping 28 matching lines...) Expand all Loading... | |
312 const VirtualNetwork* active_vpn = cros->virtual_network(); | 499 const VirtualNetwork* active_vpn = cros->virtual_network(); |
313 if (active_vpn) | 500 if (active_vpn) |
314 cros->DisconnectFromNetwork(active_vpn); | 501 cros->DisconnectFromNetwork(active_vpn); |
315 } else if (flags & FLAG_VIEW_ACCOUNT) { | 502 } else if (flags & FLAG_VIEW_ACCOUNT) { |
316 Browser* browser = BrowserList::GetLastActive(); | 503 Browser* browser = BrowserList::GetLastActive(); |
317 if (browser) | 504 if (browser) |
318 browser->ShowSingletonTab(GURL(top_up_url_)); | 505 browser->ShowSingletonTab(GURL(top_up_url_)); |
319 } | 506 } |
320 } | 507 } |
321 | 508 |
322 void NetworkMenuModel::MenuWillShow() {} | |
323 | |
324 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {} | |
325 | |
326 //////////////////////////////////////////////////////////////////////////////// | 509 //////////////////////////////////////////////////////////////////////////////// |
327 // NetworkMenuModel, private methods: | 510 // NetworkMenuModel, private methods: |
328 | 511 |
329 // TODO(stevenjb): deprecate this once we've committed to tabbed settings | 512 // TODO(stevenjb): deprecate this once we've committed to tabbed settings |
330 // and the embedded menu UI (and fully deprecated NetworkConfigView). | 513 // and the embedded menu UI (and fully deprecated NetworkConfigView). |
331 // Meanwhile, if MenuUI::IsEnabled() is true, always show the settings UI, | 514 // Meanwhile, if MenuUI::IsEnabled() is true, always show the settings UI, |
332 // otherwise show NetworkConfigView only to get passwords when not connected. | 515 // otherwise show NetworkConfigView only to get passwords when not connected. |
333 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { | 516 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { |
334 views::Window* window = browser::CreateViewsWindow( | 517 views::Window* window = browser::CreateViewsWindow( |
335 owner_->GetNativeWindow(), gfx::Rect(), view); | 518 owner_->GetNativeWindow(), gfx::Rect(), view); |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
683 } else { | 866 } else { |
684 if (!more_menu_model_->menu_items_.empty()) { | 867 if (!more_menu_model_->menu_items_.empty()) { |
685 menu_items_.push_back(MenuItem( | 868 menu_items_.push_back(MenuItem( |
686 ui::MenuModel::TYPE_SUBMENU, | 869 ui::MenuModel::TYPE_SUBMENU, |
687 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), | 870 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), |
688 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); | 871 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); |
689 } | 872 } |
690 } | 873 } |
691 } | 874 } |
692 | 875 |
876 void MainMenuModel::PopulateMenuItem( | |
877 views::MenuItemView* menu, | |
878 int index, | |
879 int command_id) { | |
880 NetworkMenuModel::PopulateMenuItem(menu, index, | |
881 command_id + kMainIndexOffset); | |
882 } | |
883 | |
884 // views::MenuDelegate implementation. | |
885 | |
886 const gfx::Font& MainMenuModel::GetLabelFont(int id) const { | |
887 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset); | |
888 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset); | |
889 const gfx::Font* font = NULL; | |
890 if (id >= kMoreIndexOffset) | |
891 font = more_menu_model_->GetLabelFontAt(id - kMoreIndexOffset); | |
892 else if (id >= kVPNIndexOffset) | |
893 font = vpn_menu_model_->GetLabelFontAt(id - kVPNIndexOffset); | |
894 else if (id >= kMainIndexOffset) | |
895 font = GetLabelFontAt(id - kMainIndexOffset); | |
896 | |
897 return font ? *font: views::MenuDelegate::GetLabelFont(id); | |
stevenjb
2011/05/18 17:36:15
nit: ' ' after * and before :
rhashimoto
2011/05/18 18:15:45
Done.
| |
898 } | |
899 | |
900 | |
901 bool MainMenuModel::IsItemChecked(int id) const { | |
902 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset); | |
903 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset); | |
904 if (id >= kMoreIndexOffset) | |
905 return more_menu_model_->IsItemCheckedAt(id - kMoreIndexOffset); | |
906 else if (id >= kVPNIndexOffset) | |
907 return vpn_menu_model_->IsItemCheckedAt(id - kVPNIndexOffset); | |
908 else if (id >= kMainIndexOffset) | |
909 return IsItemCheckedAt(id - kMainIndexOffset); | |
910 | |
911 return views::MenuDelegate::IsItemChecked(id); | |
912 } | |
913 | |
914 bool MainMenuModel::IsCommandEnabled(int id) const { | |
915 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset); | |
916 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset); | |
917 if (id >= kMoreIndexOffset) | |
918 return more_menu_model_->IsEnabledAt(id - kMoreIndexOffset); | |
919 else if (id >= kVPNIndexOffset) | |
920 return vpn_menu_model_->IsEnabledAt(id - kVPNIndexOffset); | |
921 else if (id >= kMainIndexOffset) | |
922 return IsEnabledAt(id - kMainIndexOffset); | |
923 | |
924 return views::MenuDelegate::IsCommandEnabled(id); | |
925 } | |
926 | |
927 void MainMenuModel::ExecuteCommand(int id) { | |
928 DCHECK_GT(kMoreIndexOffset, kVPNIndexOffset); | |
929 DCHECK_GT(kVPNIndexOffset, kMainIndexOffset); | |
930 if (id >= kMoreIndexOffset) | |
931 more_menu_model_->ActivatedAt(id - kMoreIndexOffset); | |
932 else if (id >= kVPNIndexOffset) | |
933 vpn_menu_model_->ActivatedAt(id - kVPNIndexOffset); | |
934 else if (id >= kMainIndexOffset) | |
935 ActivatedAt(id - kMainIndexOffset); | |
936 } | |
937 | |
693 //////////////////////////////////////////////////////////////////////////////// | 938 //////////////////////////////////////////////////////////////////////////////// |
694 // VPNMenuModel | 939 // VPNMenuModel |
695 | 940 |
696 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) | 941 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) |
697 : NetworkMenuModel(owner) { | 942 : NetworkMenuModel(owner) { |
698 } | 943 } |
699 | 944 |
700 void VPNMenuModel::InitMenuItems(bool is_browser_mode, | 945 void VPNMenuModel::InitMenuItems(bool is_browser_mode, |
701 bool should_open_button_options) { | 946 bool should_open_button_options) { |
702 // This gets called on initialization, so any changes should be reflected | 947 // This gets called on initialization, so any changes should be reflected |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
755 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), | 1000 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_ADD_VPN), |
756 SkBitmap(), std::string(), FLAG_ADD_VPN)); | 1001 SkBitmap(), std::string(), FLAG_ADD_VPN)); |
757 if (active_vpn) { | 1002 if (active_vpn) { |
758 menu_items_.push_back(MenuItem( | 1003 menu_items_.push_back(MenuItem( |
759 ui::MenuModel::TYPE_COMMAND, | 1004 ui::MenuModel::TYPE_COMMAND, |
760 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), | 1005 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_DISCONNECT_VPN), |
761 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); | 1006 SkBitmap(), std::string(), FLAG_DISCONNECT_VPN)); |
762 } | 1007 } |
763 } | 1008 } |
764 | 1009 |
1010 void VPNMenuModel::PopulateMenuItem( | |
1011 views::MenuItemView* menu, | |
1012 int index, | |
1013 int command_id) { | |
1014 NetworkMenuModel::PopulateMenuItem(menu, index, | |
1015 command_id + kVPNIndexOffset); | |
1016 } | |
1017 | |
765 // static | 1018 // static |
766 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) { | 1019 SkBitmap VPNMenuModel::IconForDisplay(const Network* network) { |
767 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 1020 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
768 const SkBitmap* icon = NULL; | 1021 const SkBitmap* icon = NULL; |
769 const SkBitmap* bottom_right_badge = NULL; | 1022 const SkBitmap* bottom_right_badge = NULL; |
770 const SkBitmap* top_left_badge = NULL; | 1023 const SkBitmap* top_left_badge = NULL; |
771 // We know for sure |network| is the active network, so no more checking | 1024 // We know for sure |network| is the active network, so no more checking |
772 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL. | 1025 // is needed by BadgeForPrivateNetworkStatus, hence pass in NULL. |
773 const SkBitmap* bottom_left_badge = | 1026 const SkBitmap* bottom_left_badge = |
774 NetworkMenu::BadgeForPrivateNetworkStatus(NULL); | 1027 NetworkMenu::BadgeForPrivateNetworkStatus(NULL); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
864 } | 1117 } |
865 } | 1118 } |
866 | 1119 |
867 menu_items_ = link_items; | 1120 menu_items_ = link_items; |
868 if (!menu_items_.empty() && address_items.size() > 1) | 1121 if (!menu_items_.empty() && address_items.size() > 1) |
869 menu_items_.push_back(MenuItem()); // Separator | 1122 menu_items_.push_back(MenuItem()); // Separator |
870 menu_items_.insert(menu_items_.end(), | 1123 menu_items_.insert(menu_items_.end(), |
871 address_items.begin(), address_items.end()); | 1124 address_items.begin(), address_items.end()); |
872 } | 1125 } |
873 | 1126 |
1127 void MoreMenuModel::PopulateMenuItem( | |
1128 views::MenuItemView* menu, | |
1129 int index, | |
1130 int command_id) { | |
1131 NetworkMenuModel::PopulateMenuItem(menu, index, | |
1132 command_id + kMoreIndexOffset); | |
1133 } | |
1134 | |
874 //////////////////////////////////////////////////////////////////////////////// | 1135 //////////////////////////////////////////////////////////////////////////////// |
875 // NetworkMenu | 1136 // NetworkMenu |
876 | 1137 |
877 // static | 1138 // static |
878 const int NetworkMenu::kNumBarsImages = 4; | 1139 const int NetworkMenu::kNumBarsImages = 4; |
879 | 1140 |
880 // NOTE: Use an array rather than just calculating a resource number to avoid | 1141 // NOTE: Use an array rather than just calculating a resource number to avoid |
881 // creating implicit ordering dependencies on the resource values. | 1142 // creating implicit ordering dependencies on the resource values. |
882 // static | 1143 // static |
883 const int NetworkMenu::kBarsImages[kNumBarsImages] = { | 1144 const int NetworkMenu::kBarsImages[kNumBarsImages] = { |
(...skipping 29 matching lines...) Expand all Loading... | |
913 | 1174 |
914 // static | 1175 // static |
915 const int NetworkMenu::kNumAnimatingImages = 10; | 1176 const int NetworkMenu::kNumAnimatingImages = 10; |
916 | 1177 |
917 // static | 1178 // static |
918 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 1179 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
919 | 1180 |
920 // static | 1181 // static |
921 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 1182 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
922 | 1183 |
923 NetworkMenu::NetworkMenu() : min_width_(-1) { | 1184 NetworkMenu::NetworkMenu() : min_width_(kDefaultMinimumWidth) { |
924 main_menu_model_.reset(new MainMenuModel(this)); | 1185 main_menu_model_.reset(new MainMenuModel(this)); |
925 network_menu_.reset(new views::Menu2(main_menu_model_.get())); | 1186 network_menu_.reset(new views::MenuItemView(main_menu_model_.get())); |
1187 network_menu_->set_has_icons(true); | |
926 } | 1188 } |
927 | 1189 |
928 NetworkMenu::~NetworkMenu() { | 1190 NetworkMenu::~NetworkMenu() { |
929 } | 1191 } |
930 | 1192 |
931 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 1193 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
932 min_width_ = width; | 1194 min_width_ = width; |
933 // This actually has no effect since menu is rebuilt before showing. | |
934 network_menu_->SetMinimumWidth(width); | |
935 } | 1195 } |
936 | 1196 |
937 void NetworkMenu::CancelMenu() { | 1197 void NetworkMenu::CancelMenu() { |
938 network_menu_->CancelMenu(); | 1198 network_menu_->Cancel(); |
939 } | 1199 } |
940 | 1200 |
941 void NetworkMenu::UpdateMenu() { | 1201 void NetworkMenu::UpdateMenu() { |
942 refreshing_menu_ = true; | 1202 refreshing_menu_ = true; |
943 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); | 1203 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); |
944 network_menu_->Rebuild(); | 1204 main_menu_model_->PopulateMenu(network_menu_.get()); |
1205 network_menu_->ChildrenChanged(); | |
945 refreshing_menu_ = false; | 1206 refreshing_menu_ = false; |
946 } | 1207 } |
947 | 1208 |
948 // static | 1209 // static |
949 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, | 1210 const SkBitmap* NetworkMenu::IconForNetworkStrength(const WifiNetwork* wifi, |
950 bool black) { | 1211 bool black) { |
951 DCHECK(wifi); | 1212 DCHECK(wifi); |
952 if (wifi->strength() == 0) { | 1213 if (wifi->strength() == 0) { |
953 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 1214 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
954 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : | 1215 black ? IDR_STATUSBAR_NETWORK_BARS0_BLACK : |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1149 chrome::kInternetOptionsSubPage, | 1410 chrome::kInternetOptionsSubPage, |
1150 EscapeUrlEncodedData(network->service_path()).c_str(), | 1411 EscapeUrlEncodedData(network->service_path()).c_str(), |
1151 network->type()); | 1412 network->type()); |
1152 browser->ShowOptionsTab(page); | 1413 browser->ShowOptionsTab(page); |
1153 } | 1414 } |
1154 | 1415 |
1155 //////////////////////////////////////////////////////////////////////////////// | 1416 //////////////////////////////////////////////////////////////////////////////// |
1156 // NetworkMenu, views::ViewMenuDelegate implementation: | 1417 // NetworkMenu, views::ViewMenuDelegate implementation: |
1157 | 1418 |
1158 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { | 1419 void NetworkMenu::RunMenu(views::View* source, const gfx::Point& pt) { |
1159 refreshing_menu_ = true; | |
1160 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 1420 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
1161 cros->RequestNetworkScan(); | 1421 cros->RequestNetworkScan(); |
1162 | 1422 |
1163 // Build initial menu items. They will be updated when UpdateMenu is | 1423 UpdateMenu(); |
1164 // called from NetworkChanged. | |
1165 main_menu_model_->InitMenuItems(IsBrowserMode(), ShouldOpenButtonOptions()); | |
1166 network_menu_->Rebuild(); | |
1167 | 1424 |
1168 // Restore menu width, if it was set up. | 1425 // TODO(rhashimoto): Remove this workaround when WebUI provides a |
1169 // NOTE: width isn't checked for correctness here since all width-related | 1426 // top-level widget on the ChromeOS login screen that is a window. |
1170 // logic implemented inside |network_menu_|. | 1427 // The current BackgroundView class for the ChromeOS login screen |
1171 if (min_width_ != -1) | 1428 // creates a owning Widget that has a native GtkWindow but is not a |
1172 network_menu_->SetMinimumWidth(min_width_); | 1429 // Window. This makes it impossible to get the NativeWindow via |
1173 refreshing_menu_ = false; | 1430 // the views API. This workaround casts the top-level NativeWidget |
1174 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 1431 // to a NativeWindow that we can pass to MenuItemView::RunMenuAt(). |
1432 gfx::NativeWindow window = GTK_WINDOW(source->GetWidget()->GetNativeView()); | |
1433 | |
1434 gfx::Point screen_loc; | |
1435 views::View::ConvertPointToScreen(source, &screen_loc); | |
1436 gfx::Rect bounds(screen_loc, source->size()); | |
1437 network_menu_->GetSubmenu()->set_minimum_preferred_width(min_width_); | |
1438 network_menu_->RunMenuAt(window, GetMenuButton(), bounds, | |
1439 views::MenuItemView::TOPRIGHT, true); | |
1175 } | 1440 } |
1176 | 1441 |
1177 } // namespace chromeos | 1442 } // namespace chromeos |
1178 | |
1179 | |
OLD | NEW |