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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/chromeos/options/network_config_view.h" 6 7 #include <algorithm> 8 9 #include "base/string_util.h" 10 #include "base/utf_string_conversions.h" 11 #include "chrome/browser/chromeos/options/vpn_config_view.h" 12 #include "chrome/browser/chromeos/options/wifi_config_view.h" 13 #include "grit/chromium_strings.h" 14 #include "grit/generated_resources.h" 15 #include "grit/locale_settings.h" 16 #include "ui/base/accessibility/accessible_view_state.h" 17 #include "ui/base/l10n/l10n_util.h" 18 #include "views/layout/grid_layout.h" 19 #include "views/layout/layout_constants.h" 20 #include "views/widget/widget_gtk.h" 21 #include "views/window/window.h" 22 23 using views::WidgetGtk; 24 25 namespace chromeos { 26 27 // static 28 const int ChildNetworkConfigView::kPassphraseWidth = 150; 29 30 NetworkConfigView::NetworkConfigView(Network* network) 31 : browser_mode_(true), 32 delegate_(NULL) { 33 if (network->type() == TYPE_WIFI) { 34 child_config_view_ = 35 new WifiConfigView(this, static_cast<WifiNetwork*>(network)); 36 } else if (network->type() == TYPE_VPN) { 37 child_config_view_ = 38 new VPNConfigView(this, static_cast<VirtualNetwork*>(network)); 39 } else { 40 NOTREACHED(); 41 child_config_view_ = NULL; 42 } 43 } 44 45 NetworkConfigView::NetworkConfigView(ConnectionType type) 46 : browser_mode_(true), 47 delegate_(NULL) { 48 if (type == TYPE_WIFI) { 49 child_config_view_ = new WifiConfigView(this); 50 } else if (type == TYPE_VPN) { 51 child_config_view_ = new VPNConfigView(this); 52 } else { 53 NOTREACHED(); 54 child_config_view_ = NULL; 55 } 56 } 57 58 gfx::NativeWindow NetworkConfigView::GetNativeWindow() const { 59 return 60 GTK_WINDOW(static_cast<const WidgetGtk*>(GetWidget())->GetNativeView()); 61 } 62 63 std::wstring NetworkConfigView::GetDialogButtonLabel( 64 MessageBoxFlags::DialogButton button) const { 65 if (button == MessageBoxFlags::DIALOGBUTTON_OK) 66 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONNECT)); 67 return std::wstring(); 68 } 69 70 bool NetworkConfigView::IsDialogButtonEnabled( 71 MessageBoxFlags::DialogButton button) const { 72 // Disable connect button if cannot login. 73 if (button == MessageBoxFlags::DIALOGBUTTON_OK) 74 return child_config_view_->CanLogin(); 75 return true; 76 } 77 78 bool NetworkConfigView::Cancel() { 79 if (delegate_) 80 delegate_->OnDialogCancelled(); 81 child_config_view_->Cancel(); 82 return true; 83 } 84 85 bool NetworkConfigView::Accept() { 86 bool result = child_config_view_->Login(); 87 if (result && delegate_) 88 delegate_->OnDialogAccepted(); 89 return result; 90 } 91 92 std::wstring NetworkConfigView::GetWindowTitle() const { 93 return UTF16ToWide(child_config_view_->GetTitle()); 94 } 95 96 void NetworkConfigView::GetAccessibleState(ui::AccessibleViewState* state) { 97 state->name = 98 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS); 99 state->role = ui::AccessibilityTypes::ROLE_DIALOG; 100 } 101 102 void NetworkConfigView::Layout() { 103 child_config_view_->SetBounds(0, 0, width(), height()); 104 } 105 106 gfx::Size NetworkConfigView::GetPreferredSize() { 107 gfx::Size result(views::Window::GetLocalizedContentsSize( 108 IDS_JOIN_WIFI_NETWORK_DIALOG_WIDTH_CHARS, 109 IDS_JOIN_WIFI_NETWORK_DIALOG_MINIMUM_HEIGHT_LINES)); 110 gfx::Size size = child_config_view_->GetPreferredSize(); 111 result.set_height(size.height()); 112 if (size.width() > result.width()) 113 result.set_width(size.width()); 114 return result; 115 } 116 117 void NetworkConfigView::ViewHierarchyChanged( 118 bool is_add, views::View* parent, views::View* child) { 119 // Can't init before we're inserted into a Container, because we require 120 // a HWND to parent native child controls to. 121 if (is_add && child == this) { 122 AddChildView(child_config_view_); 123 child_config_view_->InitFocus(); 124 } 125 } 126 127 } // namespace chromeos 128