1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/options/network_config_view.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/login/base_login_display_host.h"
12 #include "chrome/browser/chromeos/options/vpn_config_view.h"
13 #include "chrome/browser/chromeos/options/wifi_config_view.h"
14 #include "chrome/browser/chromeos/options/wimax_config_view.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/host_desktop.h"
20 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h"
22 #include "grit/locale_settings.h"
23 #include "grit/theme_resources.h"
24 #include "ui/base/accessibility/accessible_view_state.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/rect.h"
29 #include "ui/views/controls/button/text_button.h"
30 #include "ui/views/controls/image_view.h"
31 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/layout/layout_constants.h"
33 #include "ui/views/widget/widget.h"
37 gfx::NativeWindow
GetDialogParent() {
38 if (chromeos::BaseLoginDisplayHost::default_host()) {
39 return chromeos::BaseLoginDisplayHost::default_host()->GetNativeWindow();
41 Browser
* browser
= browser::FindTabbedBrowser(
42 ProfileManager::GetDefaultProfileOrOffTheRecord(),
44 chrome::HOST_DESKTOP_TYPE_ASH
);
46 return browser
->window()->GetNativeWindow();
51 // Avoid global static initializer.
52 chromeos::NetworkConfigView
** GetActiveDialogPointer() {
53 static chromeos::NetworkConfigView
* active_dialog
= NULL
;
54 return &active_dialog
;
57 chromeos::NetworkConfigView
* GetActiveDialog() {
58 return *(GetActiveDialogPointer());
61 void SetActiveDialog(chromeos::NetworkConfigView
* dialog
) {
62 *(GetActiveDialogPointer()) = dialog
;
70 const int ChildNetworkConfigView::kInputFieldMinWidth
= 270;
72 NetworkConfigView::NetworkConfigView(Network
* network
)
74 advanced_button_(NULL
),
75 advanced_button_container_(NULL
) {
76 DCHECK(GetActiveDialog() == NULL
);
77 SetActiveDialog(this);
78 if (network
->type() == TYPE_WIFI
) {
80 new WifiConfigView(this, static_cast<WifiNetwork
*>(network
));
81 } else if (network
->type() == TYPE_WIMAX
) {
83 new WimaxConfigView(this, static_cast<WimaxNetwork
*>(network
));
84 } else if (network
->type() == TYPE_VPN
) {
86 new VPNConfigView(this, static_cast<VirtualNetwork
*>(network
));
89 child_config_view_
= NULL
;
93 NetworkConfigView::NetworkConfigView(ConnectionType type
)
95 advanced_button_(NULL
),
96 advanced_button_container_(NULL
) {
97 DCHECK(GetActiveDialog() == NULL
);
98 SetActiveDialog(this);
99 if (type
== TYPE_WIFI
) {
100 child_config_view_
= new WifiConfigView(this, false /* show_8021x */);
101 CreateAdvancedButton();
102 } else if (type
== TYPE_VPN
) {
103 child_config_view_
= new VPNConfigView(this);
106 child_config_view_
= NULL
;
110 NetworkConfigView::~NetworkConfigView() {
111 DCHECK(GetActiveDialog() == this);
112 SetActiveDialog(NULL
);
116 bool NetworkConfigView::Show(Network
* network
, gfx::NativeWindow parent
) {
117 if (GetActiveDialog() != NULL
)
119 NetworkConfigView
* view
= new NetworkConfigView(network
);
121 parent
= GetDialogParent();
122 views::Widget
* window
= views::Widget::CreateWindowWithParent(view
, parent
);
123 window
->SetAlwaysOnTop(true);
129 bool NetworkConfigView::ShowForType(ConnectionType type
,
130 gfx::NativeWindow parent
) {
131 if (GetActiveDialog() != NULL
)
133 NetworkConfigView
* view
= new NetworkConfigView(type
);
135 parent
= GetDialogParent();
136 views::Widget
* window
= views::Widget::CreateWindowWithParent(view
, parent
);
137 window
->SetAlwaysOnTop(true);
142 gfx::NativeWindow
NetworkConfigView::GetNativeWindow() const {
143 return GetWidget()->GetNativeWindow();
146 string16
NetworkConfigView::GetDialogButtonLabel(
147 ui::DialogButton button
) const {
148 if (button
== ui::DIALOG_BUTTON_OK
)
149 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONNECT
);
153 bool NetworkConfigView::IsDialogButtonEnabled(ui::DialogButton button
) const {
154 // Disable connect button if cannot login.
155 if (button
== ui::DIALOG_BUTTON_OK
)
156 return child_config_view_
->CanLogin();
160 bool NetworkConfigView::Cancel() {
162 delegate_
->OnDialogCancelled();
163 child_config_view_
->Cancel();
167 bool NetworkConfigView::Accept() {
168 // Do not attempt login if it is guaranteed to fail, keep the dialog open.
169 if (!child_config_view_
->CanLogin())
171 bool result
= child_config_view_
->Login();
172 if (result
&& delegate_
)
173 delegate_
->OnDialogAccepted();
177 views::View
* NetworkConfigView::GetExtraView() {
178 return advanced_button_container_
;
181 views::View
* NetworkConfigView::GetInitiallyFocusedView() {
182 return child_config_view_
->GetInitiallyFocusedView();
185 ui::ModalType
NetworkConfigView::GetModalType() const {
186 return ui::MODAL_TYPE_SYSTEM
;
189 views::View
* NetworkConfigView::GetContentsView() {
193 void NetworkConfigView::GetAccessibleState(ui::AccessibleViewState
* state
) {
195 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS
);
196 state
->role
= ui::AccessibilityTypes::ROLE_DIALOG
;
199 void NetworkConfigView::ButtonPressed(views::Button
* sender
,
200 const ui::Event
& event
) {
201 if (advanced_button_
&& sender
== advanced_button_
) {
202 advanced_button_
->SetVisible(false);
207 void NetworkConfigView::ShowAdvancedView() {
208 // Clear out the old widgets and build new ones.
209 RemoveChildView(child_config_view_
);
210 delete child_config_view_
;
211 // For now, there is only an advanced view for Wi-Fi 802.1X.
212 child_config_view_
= new WifiConfigView(this, true /* show_8021x */);
213 AddChildView(child_config_view_
);
214 // Resize the window to be able to hold the new widgets.
215 gfx::Size size
= views::Widget::GetLocalizedContentsSize(
216 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_WIDTH_CHARS
,
217 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_MINIMUM_HEIGHT_LINES
);
218 // Get the new bounds with desired size at the same center point.
219 gfx::Rect bounds
= GetWidget()->GetWindowBoundsInScreen();
220 int horiz_padding
= bounds
.width() - size
.width();
221 int vert_padding
= bounds
.height() - size
.height();
222 bounds
.Inset(horiz_padding
/ 2, vert_padding
/ 2,
223 horiz_padding
/ 2, vert_padding
/ 2);
224 GetWidget()->SetBoundsConstrained(bounds
);
226 child_config_view_
->InitFocus();
229 void NetworkConfigView::Layout() {
230 child_config_view_
->SetBounds(0, 0, width(), height());
233 gfx::Size
NetworkConfigView::GetPreferredSize() {
234 gfx::Size
result(views::Widget::GetLocalizedContentsSize(
235 IDS_JOIN_WIFI_NETWORK_DIALOG_WIDTH_CHARS
,
236 IDS_JOIN_WIFI_NETWORK_DIALOG_MINIMUM_HEIGHT_LINES
));
237 gfx::Size size
= child_config_view_
->GetPreferredSize();
238 result
.set_height(size
.height());
239 if (size
.width() > result
.width())
240 result
.set_width(size
.width());
244 void NetworkConfigView::ViewHierarchyChanged(
245 bool is_add
, views::View
* parent
, views::View
* child
) {
246 // Can't init before we're inserted into a Container, because we require
247 // a HWND to parent native child controls to.
248 if (is_add
&& child
== this) {
249 AddChildView(child_config_view_
);
253 void NetworkConfigView::CreateAdvancedButton() {
254 advanced_button_
= new views::NativeTextButton(this,
255 l10n_util::GetStringUTF16(
256 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_ADVANCED_BUTTON
));
258 // Wrap the advanced button in a grid layout in order to left-align it.
259 advanced_button_container_
= new views::View();
260 views::GridLayout
* layout
= new views::GridLayout(advanced_button_container_
);
261 advanced_button_container_
->SetLayoutManager(layout
);
263 int column_set_id
= 0;
264 views::ColumnSet
* column_set
= layout
->AddColumnSet(column_set_id
);
265 column_set
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::LEADING
,
266 0, views::GridLayout::USE_PREF
, 0, 0);
267 layout
->StartRow(0, column_set_id
);
268 layout
->AddView(advanced_button_
);
271 ControlledSettingIndicatorView::ControlledSettingIndicatorView()
277 ControlledSettingIndicatorView::ControlledSettingIndicatorView(
278 const NetworkPropertyUIData
& ui_data
)
285 ControlledSettingIndicatorView::~ControlledSettingIndicatorView() {}
287 void ControlledSettingIndicatorView::Update(
288 const NetworkPropertyUIData
& ui_data
) {
289 if (managed_
== ui_data
.managed())
292 managed_
= ui_data
.managed();
293 PreferredSizeChanged();
296 gfx::Size
ControlledSettingIndicatorView::GetPreferredSize() {
297 return (managed_
&& visible()) ? image_view_
->GetPreferredSize()
301 void ControlledSettingIndicatorView::Layout() {
302 image_view_
->SetBounds(0, 0, width(), height());
305 void ControlledSettingIndicatorView::OnMouseEntered(
306 const ui::MouseEvent
& event
) {
307 image_view_
->SetImage(color_image_
);
310 void ControlledSettingIndicatorView::OnMouseExited(
311 const ui::MouseEvent
& event
) {
312 image_view_
->SetImage(gray_image_
);
315 void ControlledSettingIndicatorView::Init() {
316 color_image_
= ResourceBundle::GetSharedInstance().GetImageNamed(
317 IDR_CONTROLLED_SETTING_MANDATORY
).ToImageSkia();
318 gray_image_
= ResourceBundle::GetSharedInstance().GetImageNamed(
319 IDR_CONTROLLED_SETTING_MANDATORY_GRAY
).ToImageSkia();
320 image_view_
= new views::ImageView();
321 // Disable |image_view_| so mouse events propagate to the parent.
322 image_view_
->SetEnabled(false);
323 image_view_
->SetImage(gray_image_
);
324 image_view_
->SetTooltipText(
325 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_POLICY
));
326 AddChildView(image_view_
);
329 } // namespace chromeos