OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2009 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/gtk/options/general_page_gtk.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "chrome/browser/gtk/options/options_layout_gtk.h" |
| 9 #include "chrome/browser/net/url_fixer_upper.h" |
| 10 #include "chrome/browser/session_startup_pref.h" |
| 11 #include "chrome/browser/shell_integration.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/pref_service.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 #include "grit/chromium_strings.h" |
| 16 #include "grit/generated_resources.h" |
| 17 |
| 18 // TODO(mattm): spacing constants should be moved into a system-wide location |
| 19 namespace { |
| 20 |
| 21 // Spacing between options of the same group |
| 22 const int kOptionSpacing = 6; |
| 23 |
| 24 // Horizontal spacing between a label and it's control |
| 25 const int kLabelSpacing = 12; |
| 26 |
| 27 // Markup for the text showing the current state of the default browser |
| 28 const char kDefaultBrowserLabelMarkup[] = "<span color='#%s'>%s</span>"; |
| 29 |
| 30 // Color of the default browser text when Chromium is the default browser |
| 31 const char kDefaultBrowserLabelColor[] = "008700"; |
| 32 |
| 33 // Color of the default browser text when Chromium is not the default browser |
| 34 const char kNotDefaultBrowserLabelColor[] = "870000"; |
| 35 |
| 36 } |
| 37 |
| 38 /////////////////////////////////////////////////////////////////////////////// |
| 39 // GeneralPageGtk, public: |
| 40 |
| 41 GeneralPageGtk::GeneralPageGtk(Profile* profile) |
| 42 : OptionsPageBase(profile) { |
| 43 OptionsLayoutBuilderGtk options_builder(4); |
| 44 options_builder.AddOptionGroup( |
| 45 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_GROUP_NAME), |
| 46 InitStartupGroup()); |
| 47 options_builder.AddOptionGroup( |
| 48 l10n_util::GetStringUTF8(IDS_OPTIONS_HOMEPAGE_GROUP_NAME), |
| 49 InitHomepageGroup()); |
| 50 options_builder.AddOptionGroup( |
| 51 l10n_util::GetStringUTF8(IDS_OPTIONS_DEFAULTSEARCH_GROUP_NAME), |
| 52 InitDefaultSearchGroup()); |
| 53 options_builder.AddOptionGroup( |
| 54 l10n_util::GetStringUTF8(IDS_OPTIONS_DEFAULTBROWSER_GROUP_NAME), |
| 55 InitDefaultBrowserGroup()); |
| 56 page_ = options_builder.get_page_widget(); |
| 57 |
| 58 profile->GetPrefs()->AddPrefObserver(prefs::kRestoreOnStartup, this); |
| 59 profile->GetPrefs()->AddPrefObserver(prefs::kURLsToRestoreOnStartup, this); |
| 60 |
| 61 new_tab_page_is_home_page_.Init(prefs::kHomePageIsNewTabPage, |
| 62 profile->GetPrefs(), this); |
| 63 homepage_.Init(prefs::kHomePage, profile->GetPrefs(), this); |
| 64 show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this); |
| 65 |
| 66 // Load initial values |
| 67 NotifyPrefChanged(NULL); |
| 68 } |
| 69 |
| 70 GeneralPageGtk::~GeneralPageGtk() { |
| 71 profile()->GetPrefs()->RemovePrefObserver(prefs::kRestoreOnStartup, this); |
| 72 profile()->GetPrefs()->RemovePrefObserver( |
| 73 prefs::kURLsToRestoreOnStartup, this); |
| 74 } |
| 75 |
| 76 /////////////////////////////////////////////////////////////////////////////// |
| 77 // GeneralPageGtk, OptionsPageBase overrides: |
| 78 |
| 79 void GeneralPageGtk::NotifyPrefChanged(const std::wstring* pref_name) { |
| 80 if (!pref_name || *pref_name == prefs::kRestoreOnStartup) { |
| 81 PrefService* prefs = profile()->GetPrefs(); |
| 82 const SessionStartupPref startup_pref = |
| 83 SessionStartupPref::GetStartupPref(prefs); |
| 84 switch (startup_pref.type) { |
| 85 case SessionStartupPref::DEFAULT: |
| 86 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(startup_homepage_radio_), |
| 87 TRUE); |
| 88 EnableCustomHomepagesControls(false); |
| 89 break; |
| 90 |
| 91 case SessionStartupPref::LAST: |
| 92 gtk_toggle_button_set_active( |
| 93 GTK_TOGGLE_BUTTON(startup_last_session_radio_), TRUE); |
| 94 EnableCustomHomepagesControls(false); |
| 95 break; |
| 96 |
| 97 case SessionStartupPref::URLS: |
| 98 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(startup_custom_radio_), |
| 99 TRUE); |
| 100 EnableCustomHomepagesControls(true); |
| 101 break; |
| 102 } |
| 103 } |
| 104 |
| 105 // TODO(mattm): kURLsToRestoreOnStartup |
| 106 |
| 107 if (!pref_name || *pref_name == prefs::kHomePageIsNewTabPage) { |
| 108 if (new_tab_page_is_home_page_.GetValue()) { |
| 109 gtk_toggle_button_set_active( |
| 110 GTK_TOGGLE_BUTTON(homepage_use_newtab_radio_), TRUE); |
| 111 gtk_widget_set_sensitive(homepage_use_url_entry_, FALSE); |
| 112 } else { |
| 113 gtk_toggle_button_set_active( |
| 114 GTK_TOGGLE_BUTTON(homepage_use_url_radio_), TRUE); |
| 115 gtk_widget_set_sensitive(homepage_use_url_entry_, TRUE); |
| 116 } |
| 117 } |
| 118 |
| 119 if (!pref_name || *pref_name == prefs::kHomePage) { |
| 120 bool enabled = homepage_.GetValue() != |
| 121 UTF8ToWide(chrome::kChromeUINewTabURL); |
| 122 if (enabled) |
| 123 gtk_entry_set_text(GTK_ENTRY(homepage_use_url_entry_), |
| 124 WideToUTF8(homepage_.GetValue()).c_str()); |
| 125 } |
| 126 |
| 127 if (!pref_name || *pref_name == prefs::kShowHomeButton) { |
| 128 gtk_toggle_button_set_active( |
| 129 GTK_TOGGLE_BUTTON(homepage_show_home_button_checkbox_), |
| 130 show_home_button_.GetValue()); |
| 131 } |
| 132 } |
| 133 |
| 134 void GeneralPageGtk::HighlightGroup(OptionsGroup highlight_group) { |
| 135 // TODO(mattm): implement group highlighting |
| 136 } |
| 137 |
| 138 /////////////////////////////////////////////////////////////////////////////// |
| 139 // GeneralPageGtk, private: |
| 140 |
| 141 GtkWidget* GeneralPageGtk::InitStartupGroup() { |
| 142 GtkWidget* vbox = gtk_vbox_new(FALSE, kOptionSpacing); |
| 143 |
| 144 startup_homepage_radio_ = gtk_radio_button_new_with_label(NULL, |
| 145 l10n_util::GetStringUTF8( |
| 146 IDS_OPTIONS_STARTUP_SHOW_DEFAULT_AND_NEWTAB).c_str()); |
| 147 g_signal_connect(G_OBJECT(startup_homepage_radio_), "toggled", |
| 148 G_CALLBACK(OnStartupRadioToggled), this); |
| 149 gtk_container_add(GTK_CONTAINER(vbox), startup_homepage_radio_); |
| 150 |
| 151 startup_last_session_radio_ = gtk_radio_button_new_with_label_from_widget( |
| 152 GTK_RADIO_BUTTON(startup_homepage_radio_), |
| 153 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_SHOW_LAST_SESSION).c_str()); |
| 154 g_signal_connect(G_OBJECT(startup_last_session_radio_), "toggled", |
| 155 G_CALLBACK(OnStartupRadioToggled), this); |
| 156 gtk_container_add(GTK_CONTAINER(vbox), startup_last_session_radio_); |
| 157 |
| 158 startup_custom_radio_ = gtk_radio_button_new_with_label_from_widget( |
| 159 GTK_RADIO_BUTTON(startup_homepage_radio_), |
| 160 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_SHOW_PAGES).c_str()); |
| 161 g_signal_connect(G_OBJECT(startup_custom_radio_), "toggled", |
| 162 G_CALLBACK(OnStartupRadioToggled), this); |
| 163 gtk_container_add(GTK_CONTAINER(vbox), startup_custom_radio_); |
| 164 |
| 165 GtkWidget* url_list_container = gtk_hbox_new(FALSE, kOptionSpacing); |
| 166 gtk_container_add(GTK_CONTAINER(vbox), url_list_container); |
| 167 |
| 168 GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL); |
| 169 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window), |
| 170 GTK_POLICY_NEVER, |
| 171 GTK_POLICY_AUTOMATIC); |
| 172 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window), |
| 173 GTK_SHADOW_ETCHED_IN); |
| 174 gtk_container_add(GTK_CONTAINER(url_list_container), |
| 175 scroll_window); |
| 176 startup_custom_pages_tree_ = gtk_tree_view_new(); |
| 177 gtk_container_add(GTK_CONTAINER(scroll_window), startup_custom_pages_tree_); |
| 178 |
| 179 GtkWidget* url_list_buttons = gtk_vbox_new(FALSE, kOptionSpacing); |
| 180 gtk_box_pack_end(GTK_BOX(url_list_container), url_list_buttons, |
| 181 FALSE, FALSE, 0); |
| 182 |
| 183 // TODO(mattm): fix mnemonics (see |
| 184 // MenuGtk::ConvertAcceleratorsFromWindowsStyle) |
| 185 startup_add_custom_page_button_ = gtk_button_new_with_label( |
| 186 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_ADD_BUTTON).c_str()); |
| 187 gtk_container_add(GTK_CONTAINER(url_list_buttons), |
| 188 startup_add_custom_page_button_); |
| 189 startup_remove_custom_page_button_ = gtk_button_new_with_label( |
| 190 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_REMOVE_BUTTON).c_str()); |
| 191 gtk_container_add(GTK_CONTAINER(url_list_buttons), |
| 192 startup_remove_custom_page_button_); |
| 193 startup_use_current_page_button_ = gtk_button_new_with_label( |
| 194 l10n_util::GetStringUTF8(IDS_OPTIONS_STARTUP_USE_CURRENT).c_str()); |
| 195 gtk_container_add(GTK_CONTAINER(url_list_buttons), |
| 196 startup_use_current_page_button_); |
| 197 |
| 198 // TODO(mattm): hook up custom url list stuff |
| 199 |
| 200 return vbox; |
| 201 } |
| 202 |
| 203 GtkWidget* GeneralPageGtk::InitHomepageGroup() { |
| 204 GtkWidget* vbox = gtk_vbox_new(FALSE, kOptionSpacing); |
| 205 |
| 206 homepage_use_newtab_radio_ = gtk_radio_button_new_with_label(NULL, |
| 207 l10n_util::GetStringUTF8(IDS_OPTIONS_HOMEPAGE_USE_NEWTAB).c_str()); |
| 208 g_signal_connect(G_OBJECT(homepage_use_newtab_radio_), "toggled", |
| 209 G_CALLBACK(OnNewTabIsHomePageToggled), this); |
| 210 gtk_container_add(GTK_CONTAINER(vbox), homepage_use_newtab_radio_); |
| 211 |
| 212 GtkWidget* homepage_hbox = gtk_hbox_new(FALSE, kLabelSpacing); |
| 213 gtk_container_add(GTK_CONTAINER(vbox), homepage_hbox); |
| 214 |
| 215 homepage_use_url_radio_ = gtk_radio_button_new_with_label_from_widget( |
| 216 GTK_RADIO_BUTTON(homepage_use_newtab_radio_), |
| 217 l10n_util::GetStringUTF8(IDS_OPTIONS_HOMEPAGE_USE_URL).c_str()); |
| 218 g_signal_connect(G_OBJECT(homepage_use_url_radio_), "toggled", |
| 219 G_CALLBACK(OnNewTabIsHomePageToggled), this); |
| 220 gtk_box_pack_start(GTK_BOX(homepage_hbox), homepage_use_url_radio_, |
| 221 FALSE, FALSE, 0); |
| 222 homepage_use_url_entry_ = gtk_entry_new(); |
| 223 g_signal_connect(G_OBJECT(homepage_use_url_entry_), "changed", |
| 224 G_CALLBACK(OnHomepageUseUrlEntryChanged), this); |
| 225 gtk_box_pack_start(GTK_BOX(homepage_hbox), homepage_use_url_entry_, |
| 226 TRUE, TRUE, 0); |
| 227 |
| 228 homepage_show_home_button_checkbox_ = gtk_check_button_new_with_label( |
| 229 l10n_util::GetStringUTF8(IDS_OPTIONS_HOMEPAGE_SHOW_BUTTON).c_str()); |
| 230 g_signal_connect(G_OBJECT(homepage_show_home_button_checkbox_), "toggled", |
| 231 G_CALLBACK(OnShowHomeButtonToggled), this); |
| 232 gtk_container_add(GTK_CONTAINER(vbox), homepage_show_home_button_checkbox_); |
| 233 |
| 234 return vbox; |
| 235 } |
| 236 |
| 237 GtkWidget* GeneralPageGtk::InitDefaultSearchGroup() { |
| 238 GtkWidget* hbox = gtk_hbox_new(FALSE, kOptionSpacing); |
| 239 |
| 240 // TODO(mattm): hook these up |
| 241 default_search_engine_combobox_ = gtk_combo_box_new(); |
| 242 gtk_container_add(GTK_CONTAINER(hbox), default_search_engine_combobox_); |
| 243 |
| 244 default_search_manage_engines_button_ = gtk_button_new_with_label( |
| 245 l10n_util::GetStringUTF8( |
| 246 IDS_OPTIONS_DEFAULTSEARCH_MANAGE_ENGINES_LINK).c_str()); |
| 247 gtk_box_pack_end(GTK_BOX(hbox), default_search_manage_engines_button_, |
| 248 FALSE, FALSE, 0); |
| 249 |
| 250 return hbox; |
| 251 } |
| 252 |
| 253 GtkWidget* GeneralPageGtk::InitDefaultBrowserGroup() { |
| 254 GtkWidget* vbox = gtk_vbox_new(FALSE, kOptionSpacing); |
| 255 |
| 256 default_browser_status_label_ = gtk_label_new(NULL); |
| 257 gtk_box_pack_start(GTK_BOX(vbox), default_browser_status_label_, |
| 258 FALSE, FALSE, 0); |
| 259 |
| 260 default_browser_use_as_default_button_ = gtk_button_new_with_label( |
| 261 l10n_util::GetStringFUTF8(IDS_OPTIONS_DEFAULTBROWSER_USEASDEFAULT, |
| 262 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)).c_str()); |
| 263 g_signal_connect(G_OBJECT(default_browser_use_as_default_button_), "clicked", |
| 264 G_CALLBACK(OnBrowserUseAsDefaultClicked), this); |
| 265 gtk_box_pack_start(GTK_BOX(vbox), default_browser_use_as_default_button_, |
| 266 FALSE, FALSE, 0); |
| 267 |
| 268 GtkWidget* vbox_alignment = gtk_alignment_new(0.0, 0.5, 0.0, 0.0); |
| 269 gtk_container_add(GTK_CONTAINER(vbox_alignment), vbox); |
| 270 |
| 271 SetDefaultBrowserUIState(ShellIntegration::IsDefaultBrowser()); |
| 272 |
| 273 return vbox_alignment; |
| 274 } |
| 275 |
| 276 // static |
| 277 void GeneralPageGtk::OnStartupRadioToggled(GtkToggleButton* toggle_button, |
| 278 GeneralPageGtk* general_page) { |
| 279 if (!gtk_toggle_button_get_active(toggle_button)) { |
| 280 // When selecting a radio button, we get two signals (one for the old radio |
| 281 // being toggled off, one for the new one being toggled on.) Ignore the |
| 282 // signal for toggling off the old button. |
| 283 return; |
| 284 } |
| 285 general_page->SaveStartupPref(); |
| 286 GtkWidget* sender = GTK_WIDGET(toggle_button); |
| 287 if (sender == general_page->startup_homepage_radio_) { |
| 288 general_page->UserMetricsRecordAction(L"Options_Startup_Homepage", |
| 289 general_page->profile()->GetPrefs()); |
| 290 } else if (sender == general_page->startup_last_session_radio_) { |
| 291 general_page->UserMetricsRecordAction(L"Options_Startup_LastSession", |
| 292 general_page->profile()->GetPrefs()); |
| 293 } else if (sender == general_page->startup_custom_radio_) { |
| 294 general_page->UserMetricsRecordAction(L"Options_Startup_Custom", |
| 295 general_page->profile()->GetPrefs()); |
| 296 } |
| 297 } |
| 298 |
| 299 // static |
| 300 void GeneralPageGtk::OnNewTabIsHomePageToggled(GtkToggleButton* toggle_button, |
| 301 GeneralPageGtk* general_page) { |
| 302 if (!gtk_toggle_button_get_active(toggle_button)) { |
| 303 // Ignore the signal for toggling off the old button. |
| 304 return; |
| 305 } |
| 306 GtkWidget* sender = GTK_WIDGET(toggle_button); |
| 307 if (sender == general_page->homepage_use_newtab_radio_) { |
| 308 general_page->SetHomepage(GURL()); |
| 309 general_page->UserMetricsRecordAction(L"Options_Homepage_UseNewTab", |
| 310 general_page->profile()->GetPrefs()); |
| 311 gtk_widget_set_sensitive(general_page->homepage_use_url_entry_, FALSE); |
| 312 } else if (sender == general_page->homepage_use_url_radio_) { |
| 313 general_page->SetHomepageFromEntry(); |
| 314 general_page->UserMetricsRecordAction(L"Options_Homepage_UseURL", |
| 315 general_page->profile()->GetPrefs()); |
| 316 gtk_widget_set_sensitive(general_page->homepage_use_url_entry_, TRUE); |
| 317 } |
| 318 } |
| 319 |
| 320 // static |
| 321 void GeneralPageGtk::OnHomepageUseUrlEntryChanged( |
| 322 GtkEditable* editable, |
| 323 GeneralPageGtk* general_page) { |
| 324 general_page->SetHomepageFromEntry(); |
| 325 } |
| 326 |
| 327 // static |
| 328 void GeneralPageGtk::OnShowHomeButtonToggled(GtkToggleButton* toggle_button, |
| 329 GeneralPageGtk* general_page) { |
| 330 bool enabled = gtk_toggle_button_get_active(toggle_button); |
| 331 general_page->show_home_button_.SetValue(enabled); |
| 332 if (enabled) { |
| 333 general_page->UserMetricsRecordAction(L"Options_Homepage_ShowHomeButton", |
| 334 general_page->profile()->GetPrefs()); |
| 335 } else { |
| 336 general_page->UserMetricsRecordAction(L"Options_Homepage_HideHomeButton", |
| 337 general_page->profile()->GetPrefs()); |
| 338 } |
| 339 } |
| 340 |
| 341 // static |
| 342 void GeneralPageGtk::OnBrowserUseAsDefaultClicked( |
| 343 GtkButton* button, |
| 344 GeneralPageGtk* general_page) { |
| 345 general_page->SetDefaultBrowserUIState( |
| 346 ShellIntegration::SetAsDefaultBrowser()); |
| 347 // If the user made Chrome the default browser, then he/she arguably wants |
| 348 // to be notified when that changes. |
| 349 general_page->profile()->GetPrefs()->SetBoolean(prefs::kCheckDefaultBrowser, |
| 350 true); |
| 351 general_page->UserMetricsRecordAction(L"Options_SetAsDefaultBrowser", |
| 352 general_page->profile()->GetPrefs()); |
| 353 } |
| 354 |
| 355 void GeneralPageGtk::SaveStartupPref() { |
| 356 SessionStartupPref pref; |
| 357 |
| 358 if (gtk_toggle_button_get_active( |
| 359 GTK_TOGGLE_BUTTON(startup_last_session_radio_))) { |
| 360 pref.type = SessionStartupPref::LAST; |
| 361 } else if (gtk_toggle_button_get_active( |
| 362 GTK_TOGGLE_BUTTON(startup_custom_radio_))) { |
| 363 pref.type = SessionStartupPref::URLS; |
| 364 } |
| 365 |
| 366 // TODO(mattm): save custom URL list |
| 367 // pref.urls = startup_custom_pages_table_model_->GetURLs(); |
| 368 |
| 369 SessionStartupPref::SetStartupPref(profile()->GetPrefs(), pref); |
| 370 } |
| 371 |
| 372 void GeneralPageGtk::SetHomepage(const GURL& homepage) { |
| 373 if (!homepage.is_valid() || homepage.spec() == chrome::kChromeUINewTabURL) { |
| 374 new_tab_page_is_home_page_.SetValue(true); |
| 375 } else { |
| 376 new_tab_page_is_home_page_.SetValue(false); |
| 377 homepage_.SetValue(UTF8ToWide(homepage.spec())); |
| 378 } |
| 379 } |
| 380 |
| 381 void GeneralPageGtk::SetHomepageFromEntry() { |
| 382 GURL url(URLFixerUpper::FixupURL( |
| 383 gtk_entry_get_text(GTK_ENTRY(homepage_use_url_entry_)), "")); |
| 384 SetHomepage(url); |
| 385 } |
| 386 |
| 387 void GeneralPageGtk::EnableCustomHomepagesControls(bool enable) { |
| 388 gtk_widget_set_sensitive(startup_add_custom_page_button_, enable); |
| 389 GtkTreeSelection* selection = gtk_tree_view_get_selection( |
| 390 GTK_TREE_VIEW(startup_custom_pages_tree_)); |
| 391 gtk_widget_set_sensitive(startup_remove_custom_page_button_, |
| 392 enable && gtk_tree_selection_count_selected_rows(selection)); |
| 393 gtk_widget_set_sensitive(startup_use_current_page_button_, enable); |
| 394 gtk_widget_set_sensitive(startup_custom_pages_tree_, enable); |
| 395 } |
| 396 |
| 397 void GeneralPageGtk::SetDefaultBrowserUIState(bool is_default) { |
| 398 const char* color; |
| 399 std::string text; |
| 400 if (is_default) { |
| 401 color = kDefaultBrowserLabelColor; |
| 402 text = l10n_util::GetStringFUTF8(IDS_OPTIONS_DEFAULTBROWSER_DEFAULT, |
| 403 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| 404 } else { |
| 405 color = kNotDefaultBrowserLabelColor; |
| 406 text = l10n_util::GetStringFUTF8(IDS_OPTIONS_DEFAULTBROWSER_NOTDEFAULT, |
| 407 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
| 408 } |
| 409 char* markup = g_markup_printf_escaped(kDefaultBrowserLabelMarkup, |
| 410 color, text.c_str()); |
| 411 gtk_label_set_markup(GTK_LABEL(default_browser_status_label_), markup); |
| 412 g_free(markup); |
| 413 |
| 414 gtk_widget_set_sensitive(default_browser_use_as_default_button_, !is_default); |
| 415 } |
OLD | NEW |