diff options
author | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-12 02:49:34 +0000 |
---|---|---|
committer | mattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-12 02:49:34 +0000 |
commit | 15f1a32e4d437f10fed7bc056d8877fb7c9d7d21 (patch) | |
tree | e1aa223f7b19b151c7e0755c1468f225a0f96577 /chrome/common/gtk_tree.cc | |
parent | 14239acc00731e94736ac62e80fc6b17c31ea131 (diff) | |
download | chromium_src-15f1a32e4d437f10fed7bc056d8877fb7c9d7d21.zip chromium_src-15f1a32e4d437f10fed7bc056d8877fb7c9d7d21.tar.gz chromium_src-15f1a32e4d437f10fed7bc056d8877fb7c9d7d21.tar.bz2 |
Move common Gtk TreeModel handling code into gtk_tree::ModelAdapter.
Also add gtk_tree::SelectAndFocusRowNum.
BUG=none
TEST=cookie manage, bookmark editor, url picker, search engine manager should all still work.
Review URL: http://codereview.chromium.org/165359
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23145 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/gtk_tree.cc')
-rw-r--r-- | chrome/common/gtk_tree.cc | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/chrome/common/gtk_tree.cc b/chrome/common/gtk_tree.cc index fafee0f..6bca98b 100644 --- a/chrome/common/gtk_tree.cc +++ b/chrome/common/gtk_tree.cc @@ -4,6 +4,7 @@ #include "chrome/common/gtk_tree.h" +#include "app/table_model.h" #include "base/logging.h" namespace gtk_tree { @@ -33,4 +34,93 @@ gint GetTreeSortChildRowNumForPath(GtkTreeModel* sort_model, return row; } +void SelectAndFocusRowNum(int row, GtkTreeView* tree_view) { + GtkTreeModel* model = gtk_tree_view_get_model(tree_view); + if (!model) { + NOTREACHED(); + return; + } + GtkTreeIter iter; + if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row)) { + NOTREACHED(); + return; + } + GtkTreePath* path = gtk_tree_model_get_path(model, &iter); + gtk_tree_view_set_cursor(tree_view, path, NULL, FALSE); + gtk_tree_path_free(path); +} + +//////////////////////////////////////////////////////////////////////////////// +// ModelAdapter + +ModelAdapter::ModelAdapter(Delegate* delegate, GtkListStore* list_store, + TableModel* table_model) + : delegate_(delegate), list_store_(list_store), table_model_(table_model) { + if (table_model) + table_model->SetObserver(this); +} + +void ModelAdapter::SetModel(TableModel* table_model) { + table_model_ = table_model; + table_model_->SetObserver(this); +} + +void ModelAdapter::AddNodeToList(int row) { + GtkTreeIter iter; + if (row == 0) { + gtk_list_store_prepend(list_store_, &iter); + } else { + GtkTreeIter sibling; + gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &sibling, NULL, + row - 1); + gtk_list_store_insert_after(list_store_, &iter, &sibling); + } + + delegate_->SetColumnValues(row, &iter); +} + +void ModelAdapter::OnModelChanged() { + gtk_list_store_clear(list_store_); + delegate_->OnModelChanged(); + for (int i = 0; i < table_model_->RowCount(); ++i) + AddNodeToList(i); + delegate_->OnAnyModelUpdate(); +} + +void ModelAdapter::OnItemsChanged(int start, int length) { + GtkTreeIter iter; + bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &iter, + NULL, start); + for (int i = 0; i < length; ++i) { + if (!rv) { + NOTREACHED(); + return; + } + delegate_->SetColumnValues(start + i, &iter); + rv = gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store_), &iter); + } + delegate_->OnAnyModelUpdate(); +} + +void ModelAdapter::OnItemsAdded(int start, int length) { + for (int i = 0; i < length; ++i) { + AddNodeToList(start + i); + } + delegate_->OnAnyModelUpdate(); +} + +void ModelAdapter::OnItemsRemoved(int start, int length) { + GtkTreeIter iter; + bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &iter, + NULL, start); + for (int i = 0; i < length; ++i) { + if (!rv) { + NOTREACHED(); + return; + } + rv = gtk_list_store_remove(list_store_, &iter); + } + delegate_->OnAnyModelUpdate(); +} + } // namespace gtk_tree |