| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "base/utf_string_conversions.h" | |
| 6 #include "chrome/browser/browser_process.h" | |
| 7 #include "chrome/browser/profiles/profile.h" | |
| 8 #include "chrome/browser/ui/browser_list.h" | |
| 9 #include "chrome/browser/ui/browser_window.h" | |
| 10 #include "chrome/browser/ui/options/options_window.h" | |
| 11 #include "chrome/browser/ui/views/options/advanced_page_view.h" | |
| 12 #include "chrome/browser/ui/views/options/content_page_view.h" | |
| 13 #include "chrome/browser/ui/views/options/general_page_view.h" | |
| 14 #include "chrome/browser/ui/window_sizer.h" | |
| 15 #include "chrome/common/chrome_constants.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "grit/chromium_strings.h" | |
| 18 #include "grit/generated_resources.h" | |
| 19 #include "grit/locale_settings.h" | |
| 20 #include "ui/base/l10n/l10n_util.h" | |
| 21 #include "views/controls/tabbed_pane/tabbed_pane.h" | |
| 22 #include "views/widget/root_view.h" | |
| 23 #include "views/widget/widget.h" | |
| 24 #include "views/widget/native_widget.h" | |
| 25 #include "views/window/dialog_delegate.h" | |
| 26 #include "views/window/window.h" | |
| 27 | |
| 28 /////////////////////////////////////////////////////////////////////////////// | |
| 29 // OptionsWindowView | |
| 30 // | |
| 31 // The contents of the Options dialog window. | |
| 32 // | |
| 33 class OptionsWindowView : public views::View, | |
| 34 public views::DialogDelegate, | |
| 35 public views::TabbedPane::Listener { | |
| 36 public: | |
| 37 explicit OptionsWindowView(Profile* profile); | |
| 38 virtual ~OptionsWindowView(); | |
| 39 | |
| 40 // Shows the Tab corresponding to the specified OptionsPage. | |
| 41 void ShowOptionsPage(OptionsPage page, OptionsGroup highlight_group); | |
| 42 | |
| 43 // views::DialogDelegate implementation: | |
| 44 virtual int GetDialogButtons() const { | |
| 45 return MessageBoxFlags::DIALOGBUTTON_CANCEL; | |
| 46 } | |
| 47 virtual std::wstring GetWindowTitle() const; | |
| 48 virtual std::wstring GetWindowName() const; | |
| 49 virtual void WindowClosing(); | |
| 50 virtual bool Cancel(); | |
| 51 virtual views::View* GetContentsView(); | |
| 52 virtual bool ShouldRestoreWindowSize() const; | |
| 53 | |
| 54 // views::TabbedPane::Listener implementation: | |
| 55 virtual void TabSelectedAt(int index); | |
| 56 | |
| 57 // views::View overrides: | |
| 58 virtual AccessibilityTypes::Role GetAccessibleRole(); | |
| 59 virtual void Layout(); | |
| 60 virtual gfx::Size GetPreferredSize(); | |
| 61 | |
| 62 protected: | |
| 63 // views::View overrides: | |
| 64 virtual void ViewHierarchyChanged(bool is_add, | |
| 65 views::View* parent, | |
| 66 views::View* child); | |
| 67 private: | |
| 68 // Init the assorted Tabbed pages | |
| 69 void Init(); | |
| 70 | |
| 71 // Returns the currently selected OptionsPageView. | |
| 72 OptionsPageView* GetCurrentOptionsPageView() const; | |
| 73 | |
| 74 // The Tab view that contains all of the options pages. | |
| 75 views::TabbedPane* tabs_; | |
| 76 | |
| 77 // The Profile associated with these options. | |
| 78 Profile* profile_; | |
| 79 | |
| 80 // The last page the user was on when they opened the Options window. | |
| 81 IntegerPrefMember last_selected_page_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(OptionsWindowView); | |
| 84 }; | |
| 85 | |
| 86 // static | |
| 87 static OptionsWindowView* instance_ = NULL; | |
| 88 static const int kDialogPadding = 7; | |
| 89 | |
| 90 /////////////////////////////////////////////////////////////////////////////// | |
| 91 // OptionsWindowView, public: | |
| 92 | |
| 93 OptionsWindowView::OptionsWindowView(Profile* profile) | |
| 94 // Always show preferences for the original profile. Most state when off | |
| 95 // the record comes from the original profile, but we explicitly use | |
| 96 // the original profile to avoid potential problems. | |
| 97 : profile_(profile->GetOriginalProfile()) { | |
| 98 // We don't need to observe changes in this value. | |
| 99 last_selected_page_.Init(prefs::kOptionsWindowLastTabIndex, | |
| 100 g_browser_process->local_state(), NULL); | |
| 101 } | |
| 102 | |
| 103 OptionsWindowView::~OptionsWindowView() { | |
| 104 } | |
| 105 | |
| 106 void OptionsWindowView::ShowOptionsPage(OptionsPage page, | |
| 107 OptionsGroup highlight_group) { | |
| 108 // Positioning is handled by window_delegate. we just need to show the window. | |
| 109 // This will show invisible windows and bring visible windows to the front. | |
| 110 window()->Show(); | |
| 111 | |
| 112 if (page == OPTIONS_PAGE_DEFAULT) { | |
| 113 // Remember the last visited page from local state. | |
| 114 page = static_cast<OptionsPage>(last_selected_page_.GetValue()); | |
| 115 if (page == OPTIONS_PAGE_DEFAULT) | |
| 116 page = OPTIONS_PAGE_GENERAL; | |
| 117 } | |
| 118 // If the page number is out of bounds, reset to the first tab. | |
| 119 if (page < 0 || page >= tabs_->GetTabCount()) | |
| 120 page = OPTIONS_PAGE_GENERAL; | |
| 121 | |
| 122 tabs_->SelectTabAt(static_cast<int>(page)); | |
| 123 | |
| 124 GetCurrentOptionsPageView()->HighlightGroup(highlight_group); | |
| 125 } | |
| 126 | |
| 127 /////////////////////////////////////////////////////////////////////////////// | |
| 128 // OptionsWindowView, views::DialogDelegate implementation: | |
| 129 | |
| 130 std::wstring OptionsWindowView::GetWindowTitle() const { | |
| 131 return UTF16ToWide( | |
| 132 l10n_util::GetStringFUTF16(IDS_OPTIONS_DIALOG_TITLE, | |
| 133 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
| 134 } | |
| 135 | |
| 136 std::wstring OptionsWindowView::GetWindowName() const { | |
| 137 return UTF8ToWide(prefs::kPreferencesWindowPlacement); | |
| 138 } | |
| 139 | |
| 140 void OptionsWindowView::WindowClosing() { | |
| 141 // Clear the static instance so that the next time ShowOptionsWindow() is | |
| 142 // called a new window is opened. | |
| 143 instance_ = NULL; | |
| 144 } | |
| 145 | |
| 146 bool OptionsWindowView::Cancel() { | |
| 147 return GetCurrentOptionsPageView()->CanClose(); | |
| 148 } | |
| 149 | |
| 150 views::View* OptionsWindowView::GetContentsView() { | |
| 151 return this; | |
| 152 } | |
| 153 | |
| 154 bool OptionsWindowView::ShouldRestoreWindowSize() const { | |
| 155 // By returning false the options window is always sized to its preferred | |
| 156 // size. | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 /////////////////////////////////////////////////////////////////////////////// | |
| 161 // OptionsWindowView, views::TabbedPane::Listener implementation: | |
| 162 | |
| 163 void OptionsWindowView::TabSelectedAt(int index) { | |
| 164 DCHECK(index > OPTIONS_PAGE_DEFAULT && index < OPTIONS_PAGE_COUNT); | |
| 165 last_selected_page_.SetValue(index); | |
| 166 } | |
| 167 | |
| 168 /////////////////////////////////////////////////////////////////////////////// | |
| 169 // OptionsWindowView, views::View overrides: | |
| 170 | |
| 171 AccessibilityTypes::Role OptionsWindowView::GetAccessibleRole() { | |
| 172 return AccessibilityTypes::ROLE_CLIENT; | |
| 173 } | |
| 174 | |
| 175 void OptionsWindowView::Layout() { | |
| 176 tabs_->SetBounds(kDialogPadding, kDialogPadding, | |
| 177 width() - (2 * kDialogPadding), | |
| 178 height() - (2 * kDialogPadding)); | |
| 179 } | |
| 180 | |
| 181 gfx::Size OptionsWindowView::GetPreferredSize() { | |
| 182 gfx::Size size(tabs_->GetPreferredSize()); | |
| 183 size.Enlarge(2 * kDialogPadding, 2 * kDialogPadding); | |
| 184 return size; | |
| 185 } | |
| 186 | |
| 187 void OptionsWindowView::ViewHierarchyChanged(bool is_add, | |
| 188 views::View* parent, | |
| 189 views::View* child) { | |
| 190 // Can't init before we're inserted into a Container, because we require a | |
| 191 // HWND to parent native child controls to. | |
| 192 if (is_add && child == this) | |
| 193 Init(); | |
| 194 } | |
| 195 | |
| 196 /////////////////////////////////////////////////////////////////////////////// | |
| 197 // OptionsWindowView, private: | |
| 198 | |
| 199 void OptionsWindowView::Init() { | |
| 200 tabs_ = new views::TabbedPane; | |
| 201 tabs_->SetAccessibleName(l10n_util::GetStringFUTF16( | |
| 202 IDS_OPTIONS_DIALOG_TITLE, | |
| 203 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
| 204 tabs_->SetListener(this); | |
| 205 AddChildView(tabs_); | |
| 206 | |
| 207 int tab_index = 0; | |
| 208 GeneralPageView* general_page = new GeneralPageView(profile_); | |
| 209 tabs_->AddTabAtIndex( | |
| 210 tab_index++, | |
| 211 UTF16ToWide(l10n_util::GetStringUTF16(IDS_OPTIONS_GENERAL_TAB_LABEL)), | |
| 212 general_page, false); | |
| 213 | |
| 214 ContentPageView* content_page = new ContentPageView(profile_); | |
| 215 tabs_->AddTabAtIndex( | |
| 216 tab_index++, | |
| 217 UTF16ToWide(l10n_util::GetStringUTF16(IDS_OPTIONS_CONTENT_TAB_LABEL)), | |
| 218 content_page, false); | |
| 219 | |
| 220 AdvancedPageView* advanced_page = new AdvancedPageView(profile_); | |
| 221 tabs_->AddTabAtIndex( | |
| 222 tab_index++, | |
| 223 UTF16ToWide(l10n_util::GetStringUTF16(IDS_OPTIONS_ADVANCED_TAB_LABEL)), | |
| 224 advanced_page, false); | |
| 225 | |
| 226 // Bind the profile to the window so that the ChromeViewsDelegate can find | |
| 227 // the user preferences to store and retrieve window placement settings. | |
| 228 window()->AsWidget()->native_widget()->SetNativeWindowProperty( | |
| 229 Profile::kProfileKey, profile_); | |
| 230 | |
| 231 DCHECK(tabs_->GetTabCount() == OPTIONS_PAGE_COUNT); | |
| 232 } | |
| 233 | |
| 234 OptionsPageView* OptionsWindowView::GetCurrentOptionsPageView() const { | |
| 235 return static_cast<OptionsPageView*>(tabs_->GetSelectedTab()); | |
| 236 } | |
| 237 | |
| 238 /////////////////////////////////////////////////////////////////////////////// | |
| 239 // Factory/finder method: | |
| 240 | |
| 241 void ShowOptionsWindow(OptionsPage page, | |
| 242 OptionsGroup highlight_group, | |
| 243 Profile* profile) { | |
| 244 DCHECK(profile); | |
| 245 // If there's already an existing options window, activate it and switch to | |
| 246 // the specified page. | |
| 247 // TODO(beng): note this is not multi-simultaneous-profile-safe. When we care | |
| 248 // about this case this will have to be fixed. | |
| 249 if (!instance_) { | |
| 250 instance_ = new OptionsWindowView(profile); | |
| 251 views::Window::CreateChromeWindow(NULL, gfx::Rect(), instance_); | |
| 252 } | |
| 253 instance_->ShowOptionsPage(page, highlight_group); | |
| 254 } | |
| OLD | NEW |