summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authormattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-12 02:49:34 +0000
committermattm@chromium.org <mattm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-12 02:49:34 +0000
commit15f1a32e4d437f10fed7bc056d8877fb7c9d7d21 (patch)
treee1aa223f7b19b151c7e0755c1468f225a0f96577 /chrome/browser
parent14239acc00731e94736ac62e80fc6b17c31ea131 (diff)
downloadchromium_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/browser')
-rw-r--r--chrome/browser/gtk/bookmark_manager_gtk.cc93
-rw-r--r--chrome/browser/gtk/bookmark_manager_gtk.h20
-rw-r--r--chrome/browser/gtk/keyword_editor_view.cc12
-rw-r--r--chrome/browser/gtk/options/cookies_view.cc65
-rw-r--r--chrome/browser/gtk/options/cookies_view.h20
-rw-r--r--chrome/browser/gtk/options/url_picker_dialog_gtk.cc45
-rw-r--r--chrome/browser/gtk/options/url_picker_dialog_gtk.h21
7 files changed, 56 insertions, 220 deletions
diff --git a/chrome/browser/gtk/bookmark_manager_gtk.cc b/chrome/browser/gtk/bookmark_manager_gtk.cc
index a7a516d..6d5cc6b 100644
--- a/chrome/browser/gtk/bookmark_manager_gtk.cc
+++ b/chrome/browser/gtk/bookmark_manager_gtk.cc
@@ -274,39 +274,25 @@ void BookmarkManagerGtk::BookmarkNodeFavIconLoaded(BookmarkModel* model,
}
void BookmarkManagerGtk::OnModelChanged() {
- BuildRightStore();
-}
-
-void BookmarkManagerGtk::OnItemsChanged(int start, int length) {
- GtkTreeIter iter;
- bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(right_store_), &iter,
- NULL, start);
- for (int i = 0; i < length; ++i) {
- if (!rv) {
- NOTREACHED();
- return;
- }
- SetRightSideColumnValues(start + i, &iter);
- rv = gtk_tree_model_iter_next(GTK_TREE_MODEL(right_store_), &iter);
- }
+ ResetRightStoreModel();
}
-void BookmarkManagerGtk::OnItemsAdded(int start, int length) {
- for (int i = 0; i < length; ++i) {
- AddNodeToRightStore(start + i);
- }
-}
-
-void BookmarkManagerGtk::OnItemsRemoved(int start, int length) {
- for (int i = 0; i < length; ++i) {
- GtkTreeIter iter;
- if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(right_store_), &iter,
- NULL, start + i)) {
- NOTREACHED();
- return;
- }
- gtk_list_store_remove(right_store_, &iter);
- }
+void BookmarkManagerGtk::SetColumnValues(int row, GtkTreeIter* iter) {
+ // TODO(estade): building the path could be optimized out when we aren't
+ // showing the path column.
+ const BookmarkNode* node = right_tree_model_->GetNodeForRow(row);
+ GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model_, true);
+ std::wstring title =
+ right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_TITLE);
+ std::wstring url = right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_URL);
+ std::wstring path = right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_PATH);
+ gtk_list_store_set(right_store_, iter,
+ RIGHT_PANE_PIXBUF, pixbuf,
+ RIGHT_PANE_TITLE, WideToUTF8(title).c_str(),
+ RIGHT_PANE_URL, WideToUTF8(url).c_str(),
+ RIGHT_PANE_PATH, WideToUTF8(path).c_str(),
+ RIGHT_PANE_ID, node->id(), -1);
+ g_object_unref(pixbuf);
}
// BookmarkManagerGtk, private -------------------------------------------------
@@ -472,6 +458,8 @@ GtkWidget* BookmarkManagerGtk::MakeRightPane() {
right_store_ = gtk_list_store_new(RIGHT_PANE_NUM,
GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_INT64);
+ right_tree_adapter_.reset(new gtk_tree::ModelAdapter(this, right_store_,
+ NULL));
title_column_ = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(title_column_,
@@ -638,9 +626,11 @@ void BookmarkManagerGtk::BuildLeftStore() {
}
void BookmarkManagerGtk::BuildRightStore() {
- const BookmarkNode* node = GetFolder();
+ right_tree_adapter_->OnModelChanged();
+}
- gtk_list_store_clear(right_store_);
+void BookmarkManagerGtk::ResetRightStoreModel() {
+ const BookmarkNode* node = GetFolder();
if (node) {
SaveColumnConfiguration();
@@ -677,10 +667,7 @@ void BookmarkManagerGtk::BuildRightStore() {
gtk_drag_dest_unset(right_tree_view_);
}
- right_tree_model_->SetObserver(this);
-
- for (int i = 0; i < right_tree_model_->RowCount(); ++i)
- AddNodeToRightStore(i);
+ right_tree_adapter_->SetModel(right_tree_model_.get());
}
int64 BookmarkManagerGtk::GetRowIDAt(GtkTreeModel* model, GtkTreeIter* iter) {
@@ -735,38 +722,6 @@ std::vector<const BookmarkNode*> BookmarkManagerGtk::GetRightSelection() {
return nodes;
}
-void BookmarkManagerGtk::SetRightSideColumnValues(int row, GtkTreeIter* iter) {
- // TODO(estade): building the path could be optimized out when we aren't
- // showing the path column.
- const BookmarkNode* node = right_tree_model_->GetNodeForRow(row);
- GdkPixbuf* pixbuf = bookmark_utils::GetPixbufForNode(node, model_, true);
- std::wstring title =
- right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_TITLE);
- std::wstring url = right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_URL);
- std::wstring path = right_tree_model_->GetText(row, IDS_BOOKMARK_TABLE_PATH);
- gtk_list_store_set(right_store_, iter,
- RIGHT_PANE_PIXBUF, pixbuf,
- RIGHT_PANE_TITLE, WideToUTF8(title).c_str(),
- RIGHT_PANE_URL, WideToUTF8(url).c_str(),
- RIGHT_PANE_PATH, WideToUTF8(path).c_str(),
- RIGHT_PANE_ID, node->id(), -1);
- g_object_unref(pixbuf);
-}
-
-void BookmarkManagerGtk::AddNodeToRightStore(int row) {
- GtkTreeIter iter;
- if (row == 0) {
- gtk_list_store_prepend(right_store_, &iter);
- } else {
- GtkTreeIter sibling;
- gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(right_store_), &sibling,
- NULL, row - 1);
- gtk_list_store_insert_after(right_store_, &iter, &sibling);
- }
-
- SetRightSideColumnValues(row, &iter);
-}
-
void BookmarkManagerGtk::SizeColumn(GtkTreeViewColumn* column,
const wchar_t* prefname) {
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
diff --git a/chrome/browser/gtk/bookmark_manager_gtk.h b/chrome/browser/gtk/bookmark_manager_gtk.h
index 261a05c..4c98317 100644
--- a/chrome/browser/gtk/bookmark_manager_gtk.h
+++ b/chrome/browser/gtk/bookmark_manager_gtk.h
@@ -8,7 +8,6 @@
#include <gtk/gtk.h>
#include <vector>
-#include "app/table_model_observer.h"
#include "base/basictypes.h"
#include "base/gfx/rect.h"
#include "base/ref_counted.h"
@@ -16,13 +15,14 @@
#include "chrome/browser/bookmarks/bookmark_model_observer.h"
#include "chrome/browser/gtk/bookmark_context_menu.h"
#include "chrome/browser/shell_dialogs.h"
+#include "chrome/common/gtk_tree.h"
class BookmarkModel;
class BookmarkTableModel;
class Profile;
class BookmarkManagerGtk : public BookmarkModelObserver,
- public TableModelObserver,
+ public gtk_tree::ModelAdapter::Delegate,
public SelectFileDialog::Listener {
public:
virtual ~BookmarkManagerGtk();
@@ -59,11 +59,9 @@ class BookmarkManagerGtk : public BookmarkModelObserver,
virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model,
const BookmarkNode* node);
- // TableModelObserver implementation.
+ // gtk_tree::ModelAdapter::Delegate implementation.
+ virtual void SetColumnValues(int row, GtkTreeIter* iter);
virtual void OnModelChanged();
- virtual void OnItemsChanged(int start, int length);
- virtual void OnItemsAdded(int start, int length);
- virtual void OnItemsRemoved(int start, int length);
// SelectFileDialog::Listener implemenation.
virtual void FileSelected(const FilePath& path,
@@ -91,6 +89,9 @@ class BookmarkManagerGtk : public BookmarkModelObserver,
// whatever folder is selected on the left. It can be called multiple times.
void BuildRightStore();
+ // Reset the TableModel backing the right pane.
+ void ResetRightStoreModel();
+
// Get the ID of the item at |iter|.
int64 GetRowIDAt(GtkTreeModel* model, GtkTreeIter* iter);
@@ -107,12 +108,6 @@ class BookmarkManagerGtk : public BookmarkModelObserver,
// Get the nodes that are selected in the right tree view.
std::vector<const BookmarkNode*> GetRightSelection();
- // Set the fields for a row.
- void SetRightSideColumnValues(int row, GtkTreeIter* iter);
-
- // Stick update the right store to reflect |row| from |right_tree_model_|.
- void AddNodeToRightStore(int row);
-
// Set the size of a column based on the user prefs, and also sets the sizing
// properties of the column.
void SizeColumn(GtkTreeViewColumn* column,
@@ -297,6 +292,7 @@ class BookmarkManagerGtk : public BookmarkModelObserver,
GtkTreeViewColumn* url_column_;
GtkTreeViewColumn* path_column_;
scoped_ptr<BookmarkTableModel> right_tree_model_;
+ scoped_ptr<gtk_tree::ModelAdapter> right_tree_adapter_;
// |window_|'s current position and size.
gfx::Rect window_bounds_;
diff --git a/chrome/browser/gtk/keyword_editor_view.cc b/chrome/browser/gtk/keyword_editor_view.cc
index a2e098e..7257516 100644
--- a/chrome/browser/gtk/keyword_editor_view.cc
+++ b/chrome/browser/gtk/keyword_editor_view.cc
@@ -13,6 +13,7 @@
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/search_engines/template_url_table_model.h"
+#include "chrome/common/gtk_tree.h"
#include "chrome/common/gtk_util.h"
#include "grit/generated_resources.h"
@@ -270,16 +271,7 @@ int KeywordEditorView::GetSelectedModelRow() const {
void KeywordEditorView::SelectModelRow(int model_row) {
int row = GetListStoreRowForModelRow(model_row);
- GtkTreeIter iter;
- if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_),
- &iter, NULL, row)) {
- NOTREACHED();
- return;
- }
- GtkTreePath* path = gtk_tree_model_get_path(GTK_TREE_MODEL(list_store_),
- &iter);
- gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_), path, NULL, FALSE);
- gtk_tree_path_free(path);
+ gtk_tree::SelectAndFocusRowNum(row, GTK_TREE_VIEW(tree_));
}
void KeywordEditorView::AddNodeToList(int model_row) {
diff --git a/chrome/browser/gtk/options/cookies_view.cc b/chrome/browser/gtk/options/cookies_view.cc
index b61daee..4b4edf1 100644
--- a/chrome/browser/gtk/options/cookies_view.cc
+++ b/chrome/browser/gtk/options/cookies_view.cc
@@ -13,7 +13,6 @@
#include "base/string_util.h"
#include "base/time_format.h"
#include "chrome/browser/cookies_table_model.h"
-#include "chrome/common/gtk_tree.h"
#include "chrome/common/gtk_util.h"
#include "grit/generated_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
@@ -233,8 +232,10 @@ void CookiesView::Init() {
// Initialize model.
cookies_table_model_.reset(new CookiesTableModel(profile_));
- cookies_table_model_->SetObserver(this);
- OnModelChanged();
+ cookies_table_adapter_.reset(
+ new gtk_tree::ModelAdapter(this, list_store_,
+ cookies_table_model_.get()));
+ cookies_table_adapter_->OnModelChanged();
}
void CookiesView::InitStylesAndShow() {
@@ -364,6 +365,10 @@ void CookiesView::RemoveSelectedCookies() {
}
}
+void CookiesView::OnAnyModelUpdate() {
+ EnableControls();
+}
+
void CookiesView::SetColumnValues(int row, GtkTreeIter* iter) {
SkBitmap bitmap = cookies_table_model_->GetIcon(row);
GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&bitmap);
@@ -379,60 +384,6 @@ void CookiesView::SetColumnValues(int row, GtkTreeIter* iter) {
g_object_unref(pixbuf);
}
-void CookiesView::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);
- }
-
- SetColumnValues(row, &iter);
-}
-
-void CookiesView::OnModelChanged() {
- gtk_list_store_clear(list_store_);
- for (int i = 0; i < cookies_table_model_->RowCount(); ++i)
- AddNodeToList(i);
- EnableControls();
-}
-
-void CookiesView::OnItemsChanged(int start, int length) {
- GtkTreeIter iter;
- if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_),
- &iter, NULL, start)) {
- NOTREACHED();
- return;
- }
- for (int i = 0; i < length; ++i) {
- SetColumnValues(start + i, &iter);
- if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store_), &iter)) {
- NOTREACHED();
- return;
- }
- }
-}
-
-void CookiesView::OnItemsAdded(int start, int length) {
- NOTREACHED();
-}
-
-void CookiesView::OnItemsRemoved(int start, int length) {
- for (int i = 0; i < length; ++i) {
- GtkTreeIter iter;
- if (!gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(list_store_), &iter,
- NULL, start)) {
- NOTREACHED();
- return;
- }
- gtk_list_store_remove(list_store_, &iter);
- }
- EnableControls();
-}
-
// Compare the value of the given column at the given rows.
gint CookiesView::CompareRows(GtkTreeModel* model, GtkTreeIter* a,
GtkTreeIter* b, int column_id) {
diff --git a/chrome/browser/gtk/options/cookies_view.h b/chrome/browser/gtk/options/cookies_view.h
index 2cca9c6..a2ae010 100644
--- a/chrome/browser/gtk/options/cookies_view.h
+++ b/chrome/browser/gtk/options/cookies_view.h
@@ -9,10 +9,10 @@
#include <gtk/gtk.h>
-#include "app/table_model_observer.h"
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
#include "base/task.h"
+#include "chrome/common/gtk_tree.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
class CookiesTableModel;
@@ -25,18 +25,16 @@ class Profile;
// Once the CookiesView is shown, it is responsible for deleting itself when the
// user closes the dialog.
-class CookiesView : public TableModelObserver {
+class CookiesView : public gtk_tree::ModelAdapter::Delegate {
public:
virtual ~CookiesView();
// Create (if necessary) and show the cookie manager window.
static void Show(Profile* profile);
- // TableModelObserver implementation.
- virtual void OnModelChanged();
- virtual void OnItemsChanged(int start, int length);
- virtual void OnItemsAdded(int start, int length);
- virtual void OnItemsRemoved(int start, int length);
+ // gtk_tree::ModelAdapter::Delegate implementation.
+ virtual void OnAnyModelUpdate();
+ virtual void SetColumnValues(int row, GtkTreeIter* iter);
private:
// Column ids for |list_store_|.
@@ -73,13 +71,6 @@ class CookiesView : public TableModelObserver {
// Remove any cookies that are currently selected.
void RemoveSelectedCookies();
- // Set the column values for |row| of |cookies_table_model_| in the
- // |list_store_| at |iter|.
- void SetColumnValues(int row, GtkTreeIter* iter);
-
- // Add the values from |row| of |cookies_table_model_|.
- void AddNodeToList(int row);
-
// Compare the value of the given column at the given rows.
gint CompareRows(GtkTreeModel* model, GtkTreeIter* a, GtkTreeIter* b,
int column_id);
@@ -141,6 +132,7 @@ class CookiesView : public TableModelObserver {
// The Cookies Table model.
scoped_ptr<CookiesTableModel> cookies_table_model_;
+ scoped_ptr<gtk_tree::ModelAdapter> cookies_table_adapter_;
// A factory to construct Runnable Methods so that we can be called back to
// re-evaluate the model after the search query string changes.
diff --git a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc
index 631531f..99c09e42 100644
--- a/chrome/browser/gtk/options/url_picker_dialog_gtk.cc
+++ b/chrome/browser/gtk/options/url_picker_dialog_gtk.cc
@@ -150,7 +150,8 @@ UrlPickerDialogGtk::UrlPickerDialogGtk(UrlPickerCallback* callback,
// Loading data, showing dialog.
url_table_model_.reset(new PossibleURLModel());
- url_table_model_->SetObserver(this);
+ url_table_adapter_.reset(new gtk_tree::ModelAdapter(this, history_list_store_,
+ url_table_model_.get()));
url_table_model_->Reload(profile_);
EnableControls();
@@ -205,48 +206,6 @@ void UrlPickerDialogGtk::SetColumnValues(int row, GtkTreeIter* iter) {
g_object_unref(pixbuf);
}
-void UrlPickerDialogGtk::AddNodeToList(int row) {
- GtkTreeIter iter;
- if (row == 0) {
- gtk_list_store_prepend(history_list_store_, &iter);
- } else {
- GtkTreeIter sibling;
- gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(history_list_store_), &sibling,
- NULL, row - 1);
- gtk_list_store_insert_after(history_list_store_, &iter, &sibling);
- }
-
- SetColumnValues(row, &iter);
-}
-
-void UrlPickerDialogGtk::OnModelChanged() {
- gtk_list_store_clear(history_list_store_);
- for (int i = 0; i < url_table_model_->RowCount(); ++i)
- AddNodeToList(i);
-}
-
-void UrlPickerDialogGtk::OnItemsChanged(int start, int length) {
- GtkTreeIter iter;
- bool rv = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(history_list_store_),
- &iter, NULL, start);
- for (int i = 0; i < length; ++i) {
- if (!rv) {
- NOTREACHED();
- return;
- }
- SetColumnValues(start + i, &iter);
- rv = gtk_tree_model_iter_next(GTK_TREE_MODEL(history_list_store_), &iter);
- }
-}
-
-void UrlPickerDialogGtk::OnItemsAdded(int start, int length) {
- NOTREACHED();
-}
-
-void UrlPickerDialogGtk::OnItemsRemoved(int start, int length) {
- NOTREACHED();
-}
-
// static
gint UrlPickerDialogGtk::CompareTitle(GtkTreeModel* model,
GtkTreeIter* a,
diff --git a/chrome/browser/gtk/options/url_picker_dialog_gtk.h b/chrome/browser/gtk/options/url_picker_dialog_gtk.h
index 5c01f8f..eead60a 100644
--- a/chrome/browser/gtk/options/url_picker_dialog_gtk.h
+++ b/chrome/browser/gtk/options/url_picker_dialog_gtk.h
@@ -5,16 +5,16 @@
#ifndef CHROME_BROWSER_GTK_OPTIONS_URL_PICKER_DIALOG_GTK_H_
#define CHROME_BROWSER_GTK_OPTIONS_URL_PICKER_DIALOG_GTK_H_
-#include "app/table_model_observer.h"
#include "base/basictypes.h"
#include "base/task.h"
#include "chrome/browser/history/history.h"
+#include "chrome/common/gtk_tree.h"
class GURL;
class Profile;
class PossibleURLModel;
-class UrlPickerDialogGtk : public TableModelObserver {
+class UrlPickerDialogGtk : public gtk_tree::ModelAdapter::Delegate {
public:
typedef Callback1<const GURL&>::Type UrlPickerCallback;
@@ -24,6 +24,9 @@ class UrlPickerDialogGtk : public TableModelObserver {
~UrlPickerDialogGtk();
+ // gtk_tree::ModelAdapter::Delegate implementation.
+ virtual void SetColumnValues(int row, GtkTreeIter* iter);
+
private:
// Call the callback based on url entry.
void AddURL();
@@ -34,19 +37,6 @@ class UrlPickerDialogGtk : public TableModelObserver {
// Return the entry-formatted url for path in the sorted model.
std::string GetURLForPath(GtkTreePath* path) const;
- // Set the column values for |row| of |url_table_model_| in the
- // |history_list_store_| at |iter|.
- void SetColumnValues(int row, GtkTreeIter* iter);
-
- // Add the values from |row| of |url_table_model_|.
- void AddNodeToList(int row);
-
- // TableModelObserver implementation.
- virtual void OnModelChanged();
- virtual void OnItemsChanged(int start, int length);
- virtual void OnItemsAdded(int start, int length);
- virtual void OnItemsRemoved(int start, int length);
-
// GTK sorting callbacks.
static gint CompareTitle(GtkTreeModel* model, GtkTreeIter* a, GtkTreeIter* b,
gpointer window);
@@ -95,6 +85,7 @@ class UrlPickerDialogGtk : public TableModelObserver {
// The table model.
scoped_ptr<PossibleURLModel> url_table_model_;
+ scoped_ptr<gtk_tree::ModelAdapter> url_table_adapter_;
// Called if the user selects an url.
UrlPickerCallback* callback_;