| 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 "chrome/browser/gtk/options/simple_content_exceptions_window.h" | |
| 6 | |
| 7 #include "app/l10n_util.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/gtk/gtk_util.h" | |
| 11 #include "gfx/gtk_util.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "grit/locale_settings.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Singleton for exception window. | |
| 18 SimpleContentExceptionsWindow* instance = NULL; | |
| 19 | |
| 20 enum { | |
| 21 COL_ACTION = gtk_tree::TableAdapter::COL_LAST_ID, | |
| 22 COL_COUNT | |
| 23 }; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 // static | |
| 28 void SimpleContentExceptionsWindow::ShowExceptionsWindow( | |
| 29 GtkWindow* parent, RemoveRowsTableModel* model, int title_message_id) { | |
| 30 DCHECK(model); | |
| 31 scoped_ptr<RemoveRowsTableModel> owned_model(model); | |
| 32 | |
| 33 if (!instance) { | |
| 34 instance = new SimpleContentExceptionsWindow( | |
| 35 parent, owned_model.release(), title_message_id); | |
| 36 } else { | |
| 37 gtk_util::PresentWindow(instance->dialog_, 0); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 SimpleContentExceptionsWindow::SimpleContentExceptionsWindow( | |
| 42 GtkWindow* parent, | |
| 43 RemoveRowsTableModel* model, | |
| 44 int title_message_id) | |
| 45 : ignore_selection_changes_(false) { | |
| 46 // Build the model adapters that translate views and TableModels into | |
| 47 // something GTK can use. | |
| 48 list_store_ = gtk_list_store_new(COL_COUNT, | |
| 49 G_TYPE_STRING, | |
| 50 G_TYPE_BOOLEAN, | |
| 51 G_TYPE_BOOLEAN, | |
| 52 G_TYPE_INT, | |
| 53 G_TYPE_INT, | |
| 54 G_TYPE_BOOLEAN, | |
| 55 G_TYPE_STRING); | |
| 56 treeview_ = gtk_tree_view_new_with_model(GTK_TREE_MODEL(list_store_)); | |
| 57 g_object_unref(list_store_); | |
| 58 | |
| 59 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview_), TRUE); | |
| 60 gtk_tree_view_set_row_separator_func( | |
| 61 GTK_TREE_VIEW(treeview_), | |
| 62 gtk_tree::TableAdapter::OnCheckRowIsSeparator, | |
| 63 NULL, | |
| 64 NULL); | |
| 65 | |
| 66 // Set up the properties of the treeview | |
| 67 GtkTreeViewColumn* hostname_column = gtk_tree_view_column_new_with_attributes( | |
| 68 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_HOSTNAME_HEADER).c_str(), | |
| 69 gtk_cell_renderer_text_new(), | |
| 70 "text", gtk_tree::TableAdapter::COL_TITLE, | |
| 71 "weight", gtk_tree::TableAdapter::COL_WEIGHT, | |
| 72 "weight-set", gtk_tree::TableAdapter::COL_WEIGHT_SET, | |
| 73 NULL); | |
| 74 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_), hostname_column); | |
| 75 | |
| 76 GtkTreeViewColumn* action_column = gtk_tree_view_column_new_with_attributes( | |
| 77 l10n_util::GetStringUTF8(IDS_EXCEPTIONS_ACTION_HEADER).c_str(), | |
| 78 gtk_cell_renderer_text_new(), | |
| 79 "text", COL_ACTION, | |
| 80 NULL); | |
| 81 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview_), action_column); | |
| 82 | |
| 83 treeview_selection_ = gtk_tree_view_get_selection( | |
| 84 GTK_TREE_VIEW(treeview_)); | |
| 85 gtk_tree_selection_set_mode(treeview_selection_, GTK_SELECTION_MULTIPLE); | |
| 86 gtk_tree_selection_set_select_function( | |
| 87 treeview_selection_, | |
| 88 gtk_tree::TableAdapter::OnSelectionFilter, | |
| 89 NULL, | |
| 90 NULL); | |
| 91 g_signal_connect(treeview_selection_, "changed", | |
| 92 G_CALLBACK(OnTreeSelectionChangedThunk), this); | |
| 93 | |
| 94 // Bind |list_store_| to our C++ model. | |
| 95 model_.reset(model); | |
| 96 model_adapter_.reset(new gtk_tree::TableAdapter(this, list_store_, | |
| 97 model_.get())); | |
| 98 // Force a reload of everything to copy data into |list_store_|. | |
| 99 model_adapter_->OnModelChanged(); | |
| 100 | |
| 101 dialog_ = gtk_dialog_new_with_buttons( | |
| 102 l10n_util::GetStringUTF8(title_message_id).c_str(), | |
| 103 parent, | |
| 104 static_cast<GtkDialogFlags>(GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR), | |
| 105 GTK_STOCK_CLOSE, | |
| 106 GTK_RESPONSE_CLOSE, | |
| 107 NULL); | |
| 108 gtk_window_set_default_size(GTK_WINDOW(dialog_), 500, 400); | |
| 109 // Allow browser windows to go in front of the options dialog in metacity. | |
| 110 gtk_window_set_type_hint(GTK_WINDOW(dialog_), GDK_WINDOW_TYPE_HINT_NORMAL); | |
| 111 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | |
| 112 gtk_util::kContentAreaSpacing); | |
| 113 | |
| 114 GtkWidget* hbox = gtk_hbox_new(FALSE, gtk_util::kControlSpacing); | |
| 115 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), hbox); | |
| 116 | |
| 117 // Create a scrolled window to wrap the treeview widget. | |
| 118 GtkWidget* scrolled = gtk_scrolled_window_new(NULL, NULL); | |
| 119 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), | |
| 120 GTK_SHADOW_ETCHED_IN); | |
| 121 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), | |
| 122 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | |
| 123 gtk_container_add(GTK_CONTAINER(scrolled), treeview_); | |
| 124 gtk_box_pack_start(GTK_BOX(hbox), scrolled, TRUE, TRUE, 0); | |
| 125 | |
| 126 GtkWidget* button_box = gtk_vbox_new(FALSE, gtk_util::kControlSpacing); | |
| 127 | |
| 128 remove_button_ = gtk_util::BuildDialogButton(dialog_, | |
| 129 IDS_EXCEPTIONS_REMOVE_BUTTON, | |
| 130 GTK_STOCK_REMOVE); | |
| 131 | |
| 132 g_signal_connect(remove_button_, "clicked", G_CALLBACK(RemoveThunk), this); | |
| 133 gtk_box_pack_start(GTK_BOX(button_box), remove_button_, FALSE, FALSE, 0); | |
| 134 | |
| 135 remove_all_button_ = gtk_util::BuildDialogButton( | |
| 136 dialog_, | |
| 137 IDS_EXCEPTIONS_REMOVEALL_BUTTON, | |
| 138 GTK_STOCK_CLEAR); | |
| 139 g_signal_connect(remove_all_button_, "clicked", G_CALLBACK(RemoveAllThunk), | |
| 140 this); | |
| 141 gtk_box_pack_start(GTK_BOX(button_box), remove_all_button_, FALSE, FALSE, 0); | |
| 142 | |
| 143 gtk_box_pack_start(GTK_BOX(hbox), button_box, FALSE, FALSE, 0); | |
| 144 | |
| 145 UpdateButtonState(); | |
| 146 | |
| 147 gtk_util::ShowDialogWithLocalizedSize(dialog_, | |
| 148 IDS_SIMPLE_CONTENT_EXCEPTION_DIALOG_WIDTH_CHARS, | |
| 149 IDS_SIMPLE_CONTENT_EXCEPTION_DIALOG_HEIGHT_LINES, | |
| 150 true); | |
| 151 | |
| 152 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); | |
| 153 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnWindowDestroyThunk), this); | |
| 154 } | |
| 155 | |
| 156 SimpleContentExceptionsWindow::~SimpleContentExceptionsWindow() {} | |
| 157 | |
| 158 void SimpleContentExceptionsWindow::SetColumnValues(int row, | |
| 159 GtkTreeIter* iter) { | |
| 160 string16 hostname = model_->GetText(row, IDS_EXCEPTIONS_HOSTNAME_HEADER); | |
| 161 gtk_list_store_set(list_store_, iter, gtk_tree::TableAdapter::COL_TITLE, | |
| 162 UTF16ToUTF8(hostname).c_str(), -1); | |
| 163 | |
| 164 string16 action = model_->GetText(row, IDS_EXCEPTIONS_ACTION_HEADER); | |
| 165 gtk_list_store_set(list_store_, iter, COL_ACTION, | |
| 166 UTF16ToUTF8(action).c_str(), -1); | |
| 167 } | |
| 168 | |
| 169 void SimpleContentExceptionsWindow::OnAnyModelUpdateStart() { | |
| 170 ignore_selection_changes_ = true; | |
| 171 } | |
| 172 | |
| 173 void SimpleContentExceptionsWindow::OnAnyModelUpdate() { | |
| 174 ignore_selection_changes_ = false; | |
| 175 } | |
| 176 | |
| 177 void SimpleContentExceptionsWindow::UpdateButtonState() { | |
| 178 int row_count = gtk_tree_model_iter_n_children( | |
| 179 GTK_TREE_MODEL(list_store_), NULL); | |
| 180 | |
| 181 RemoveRowsTableModel::Rows rows; | |
| 182 std::set<int> indices; | |
| 183 gtk_tree::GetSelectedIndices(treeview_selection_, &indices); | |
| 184 model_adapter_->MapListStoreIndicesToModelRows(indices, &rows); | |
| 185 gtk_widget_set_sensitive(remove_button_, model_->CanRemoveRows(rows)); | |
| 186 gtk_widget_set_sensitive(remove_all_button_, row_count > 0); | |
| 187 } | |
| 188 | |
| 189 void SimpleContentExceptionsWindow::Remove(GtkWidget* widget) { | |
| 190 RemoveRowsTableModel::Rows rows; | |
| 191 std::set<int> indices; | |
| 192 gtk_tree::GetSelectedIndices(treeview_selection_, &indices); | |
| 193 model_adapter_->MapListStoreIndicesToModelRows(indices, &rows); | |
| 194 model_->RemoveRows(rows); | |
| 195 UpdateButtonState(); | |
| 196 } | |
| 197 | |
| 198 void SimpleContentExceptionsWindow::RemoveAll(GtkWidget* widget) { | |
| 199 model_->RemoveAll(); | |
| 200 UpdateButtonState(); | |
| 201 } | |
| 202 | |
| 203 void SimpleContentExceptionsWindow::OnWindowDestroy(GtkWidget* widget) { | |
| 204 instance = NULL; | |
| 205 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 206 } | |
| 207 | |
| 208 void SimpleContentExceptionsWindow::OnTreeSelectionChanged( | |
| 209 GtkWidget* selection) { | |
| 210 if (!ignore_selection_changes_) | |
| 211 UpdateButtonState(); | |
| 212 } | |
| OLD | NEW |