summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk
diff options
context:
space:
mode:
authorerg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-10 17:58:48 +0000
committererg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-10 17:58:48 +0000
commita60c7ac21e326e6a9587ae6d114f3bb2eecaf2b2 (patch)
tree054bb01b993f803af1cd8ba7d7a99e54f4be162e /chrome/browser/gtk
parentbb86b871359ba34fb15b15e7693315fd92c26486 (diff)
downloadchromium_src-a60c7ac21e326e6a9587ae6d114f3bb2eecaf2b2.zip
chromium_src-a60c7ac21e326e6a9587ae6d114f3bb2eecaf2b2.tar.gz
chromium_src-a60c7ac21e326e6a9587ae6d114f3bb2eecaf2b2.tar.bz2
Fill in NOTIMPLEMENTEDs in BookmarkBarGtk. Can now reorder bookmarks.
Review URL: http://codereview.chromium.org/66022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13518 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/gtk')
-rw-r--r--chrome/browser/gtk/bookmark_bar_gtk.cc182
-rw-r--r--chrome/browser/gtk/bookmark_bar_gtk.h67
2 files changed, 198 insertions, 51 deletions
diff --git a/chrome/browser/gtk/bookmark_bar_gtk.cc b/chrome/browser/gtk/bookmark_bar_gtk.cc
index 59537cb..bef0bdd 100644
--- a/chrome/browser/gtk/bookmark_bar_gtk.cc
+++ b/chrome/browser/gtk/bookmark_bar_gtk.cc
@@ -8,7 +8,6 @@
#include "chrome/browser/browser.h"
#include "chrome/browser/gtk/custom_button.h"
#include "chrome/browser/gtk/gtk_chrome_button.h"
-#include "chrome/browser/gtk/nine_box.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/profile.h"
#include "chrome/common/gfx/text_elider.h"
@@ -69,7 +68,7 @@ void BookmarkBarGtk::SetProfile(Profile* profile) {
if (model_)
model_->RemoveObserver(this);
- // TODO(erg): Add the other bookmarked button, disabled.
+ gtk_widget_set_sensitive(other_bookmarks_button_, false);
// TODO(erg): Handle extensions
@@ -110,6 +109,8 @@ void BookmarkBarGtk::Init(Profile* profile) {
G_CALLBACK(&OnToolbarDragMotion), this);
g_signal_connect(bookmark_toolbar_.get(), "drag-leave",
G_CALLBACK(&OnToolbarDragLeave), this);
+ g_signal_connect(bookmark_toolbar_.get(), "drag-drop",
+ G_CALLBACK(&OnToolbarDragDrop), this);
gtk_box_pack_start(GTK_BOX(bookmark_hbox_), gtk_vseparator_new(),
FALSE, FALSE, 0);
@@ -151,7 +152,90 @@ void BookmarkBarGtk::Loaded(BookmarkModel* model) {
BookmarkNode* node = model_->GetBookmarkBarNode();
DCHECK(node && model_->other_node());
+ CreateAllBookmarkButtons(node);
+ gtk_widget_set_sensitive(other_bookmarks_button_, true);
+}
+
+void BookmarkBarGtk::BookmarkModelBeingDeleted(BookmarkModel* model) {
+ // The bookmark model should never be deleted before us. This code exists
+ // to check for regressions in shutdown code and not crash.
+ NOTREACHED();
+
+ // Do minimal cleanup, presumably we'll be deleted shortly.
+ model_->RemoveObserver(this);
+ model_ = NULL;
+}
+
+void BookmarkBarGtk::BookmarkNodeMoved(BookmarkModel* model,
+ BookmarkNode* old_parent,
+ int old_index,
+ BookmarkNode* new_parent,
+ int new_index) {
+ BookmarkNodeRemoved(model, old_parent, old_index);
+ BookmarkNodeAdded(model, new_parent, new_index);
+}
+
+void BookmarkBarGtk::BookmarkNodeAdded(BookmarkModel* model,
+ BookmarkNode* parent,
+ int index) {
+ if (parent != model_->GetBookmarkBarNode()) {
+ // We only care about nodes on the bookmark bar.
+ return;
+ }
+ DCHECK(index >= 0 && index <= GetBookmarkButtonCount());
+
+ gtk_toolbar_insert(GTK_TOOLBAR(bookmark_toolbar_.get()),
+ CreateBookmarkToolItem(parent->GetChild(index)),
+ index);
+}
+
+void BookmarkBarGtk::BookmarkNodeRemoved(BookmarkModel* model,
+ BookmarkNode* parent,
+ int index) {
+ if (parent != model_->GetBookmarkBarNode()) {
+ // We only care about nodes on the bookmark bar.
+ return;
+ }
+ DCHECK(index >= 0 && index < GetBookmarkButtonCount());
+
+ GtkWidget* to_remove = GTK_WIDGET(gtk_toolbar_get_nth_item(
+ GTK_TOOLBAR(bookmark_toolbar_.get()), index));
+ gtk_container_remove(GTK_CONTAINER(bookmark_toolbar_.get()),
+ to_remove);
+}
+
+void BookmarkBarGtk::BookmarkNodeChanged(BookmarkModel* model,
+ BookmarkNode* node) {
+ if (node->GetParent() != model_->GetBookmarkBarNode()) {
+ // We only care about nodes on the bookmark bar.
+ return;
+ }
+ int index = model_->GetBookmarkBarNode()->IndexOfChild(node);
+ DCHECK(index != -1);
+
+ GtkToolItem* item = gtk_toolbar_get_nth_item(
+ GTK_TOOLBAR(bookmark_toolbar_.get()), index);
+ GtkWidget* button = gtk_bin_get_child(GTK_BIN(item));
+ ConfigureButtonForNode(node, button);
+}
+
+void BookmarkBarGtk::BookmarkNodeFavIconLoaded(BookmarkModel* model,
+ BookmarkNode* node) {
+ BookmarkNodeChanged(model, node);
+}
+
+void BookmarkBarGtk::BookmarkNodeChildrenReordered(BookmarkModel* model,
+ BookmarkNode* node) {
+ if (node != model_->GetBookmarkBarNode())
+ return; // We only care about reordering of the bookmark bar node.
+
+ // Purge and rebuild the bar.
+ RemoveAllBookmarkButtons();
+ CreateAllBookmarkButtons(node);
+}
+
+void BookmarkBarGtk::CreateAllBookmarkButtons(BookmarkNode* node) {
// Create a button for each of the children on the bookmark bar.
for (int i = 0; i < node->GetChildCount(); ++i) {
GtkToolItem* item = CreateBookmarkToolItem(node->GetChild(i));
@@ -164,26 +248,40 @@ void BookmarkBarGtk::Loaded(BookmarkModel* model) {
} else {
gtk_widget_hide(instructions_);
}
-
- // TODO(erg): Reenable the other bookmarks button here once it exists.
}
void BookmarkBarGtk::RemoveAllBookmarkButtons() {
gfx::RemoveAllChildren(bookmark_toolbar_.get());
}
+int BookmarkBarGtk::GetBookmarkButtonCount() {
+ GList* children = gtk_container_get_children(
+ GTK_CONTAINER(bookmark_toolbar_.get()));
+ int count = g_list_length(children);
+ g_list_free(children);
+ return count;
+}
+
bool BookmarkBarGtk::IsAlwaysShown() {
return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
}
+void BookmarkBarGtk::ConfigureButtonForNode(BookmarkNode* node,
+ GtkWidget* button) {
+ gtk_widget_set_tooltip_text(button, BuildTooltip(node).c_str());
+ gtk_button_set_label(GTK_BUTTON(button),
+ WideToUTF8(node->GetTitle()).c_str());
+ // TODO(erg): Munge the icon from a SkBitmap into something GtkButton can
+ // use. See BookmarkBarView::ConfigureButton() for the code I need to adapt
+ // to here...
+}
+
GtkWidget* BookmarkBarGtk::CreateBookmarkButton(
BookmarkNode* node) {
GtkWidget* button = gtk_chrome_button_new();
+ ConfigureButtonForNode(node, button);
if (node->is_url()) {
- gtk_widget_set_tooltip_text(button, BuildTooltip(node).c_str());
- gtk_button_set_label(GTK_BUTTON(button),
- WideToUTF8(node->GetTitle()).c_str());
// TODO(erg): Consider a soft maximum instead of this hard 15.
gtk_label_set_max_width_chars(
GTK_LABEL(gtk_bin_get_child(GTK_BIN(button))),
@@ -206,8 +304,6 @@ GtkWidget* BookmarkBarGtk::CreateBookmarkButton(
g_signal_connect(G_OBJECT(button), "button-release-event",
G_CALLBACK(OnButtonReleased), this);
GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
-
- g_object_set_data(G_OBJECT(button), "bookmark-node", node);
} else {
NOTIMPLEMENTED();
}
@@ -231,6 +327,36 @@ std::string BookmarkBarGtk::BuildTooltip(BookmarkNode* node) {
return node->GetURL().possibly_invalid_spec();
}
+BookmarkNode* BookmarkBarGtk::GetNodeForToolButton(GtkWidget* button) {
+ GtkWidget* item_to_find = gtk_widget_get_parent(button);
+ int index_to_use = -1;
+ int index = 0;
+ GList* children = gtk_container_get_children(
+ GTK_CONTAINER(bookmark_toolbar_.get()));
+ for (GList* item = children; item; item = item->next, index++) {
+ if (item->data == item_to_find) {
+ index_to_use = index;
+ break;
+ }
+ }
+ g_list_free(children);
+
+ if (index_to_use != -1)
+ return model_->GetBookmarkBarNode()->GetChild(index_to_use);
+
+ return NULL;
+}
+
+void BookmarkBarGtk::PopupMenuForNode(BookmarkNode* node,
+ GdkEventButton* event) {
+ GtkWidget* menu = gtk_menu_new();
+ gtk_menu_shell_append(GTK_MENU_SHELL(menu),
+ gtk_menu_item_new_with_label("TODO(erg): Write menus"));
+ gtk_widget_show_all(menu);
+ gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button,
+ event->time);
+}
+
gboolean BookmarkBarGtk::OnButtonPressed(GtkWidget* sender,
GdkEventButton* event,
BookmarkBarGtk* bar) {
@@ -247,11 +373,15 @@ gboolean BookmarkBarGtk::OnButtonReleased(GtkWidget* sender,
return FALSE;
}
- gpointer user_data = g_object_get_data(G_OBJECT(sender), "bookmark-node");
- BookmarkNode* node = static_cast<BookmarkNode*>(user_data);
+ BookmarkNode* node = bar->GetNodeForToolButton(sender);
DCHECK(node);
DCHECK(bar->page_navigator_);
+ if (event->button == 3) {
+ bar->PopupMenuForNode(node, event);
+ return FALSE;
+ }
+
if (node->is_url()) {
bar->page_navigator_->OpenURL(
node->GetURL(), GURL(),
@@ -276,8 +406,7 @@ void BookmarkBarGtk::OnButtonDragBegin(GtkWidget* button,
// pressing.
bar->ignore_button_release_ = true;
- gpointer user_data = g_object_get_data(G_OBJECT(button), "bookmark-node");
- BookmarkNode* node = static_cast<BookmarkNode*>(user_data);
+ BookmarkNode* node = bar->GetNodeForToolButton(button);
DCHECK(node);
bar->dragged_node_ = node;
@@ -326,12 +455,10 @@ gboolean BookmarkBarGtk::OnToolbarExpose(GtkWidget* widget,
// A GtkToolbar's expose handler first draws a box. We don't want that so we
// need to propagate the expose event to all the container's children.
GList* children = gtk_container_get_children(GTK_CONTAINER(widget));
- while (children) {
- GList *next = children->next;
+ for (GList* item = children; item; item = item->next) {
gtk_container_propagate_expose(GTK_CONTAINER(widget),
- GTK_WIDGET(children->data),
+ GTK_WIDGET(item->data),
event);
- children = next;
}
g_list_free(children);
@@ -370,3 +497,24 @@ void BookmarkBarGtk::OnToolbarDragLeave(GtkToolbar* toolbar,
gtk_toolbar_set_drop_highlight_item(toolbar, NULL, 0);
}
+
+// static
+gboolean BookmarkBarGtk::OnToolbarDragDrop(GtkWidget* toolbar,
+ GdkDragContext* drag_context,
+ gint x,
+ gint y,
+ guint time,
+ BookmarkBarGtk* bar) {
+ // TODO(erg): This implementation only works within the same profile, which
+ // is OK for now because we're restricted to drags from within the same
+ // window.
+ if (bar->dragged_node_) {
+ gint index = gtk_toolbar_get_drop_index(GTK_TOOLBAR(toolbar), x, y);
+ // Drag from same profile, do a move.
+ bar->model_->Move(bar->dragged_node_, bar->model_->GetBookmarkBarNode(),
+ index);
+ return TRUE;
+ }
+
+ return FALSE;
+}
diff --git a/chrome/browser/gtk/bookmark_bar_gtk.h b/chrome/browser/gtk/bookmark_bar_gtk.h
index 9b1a30f..d45fcdbb 100644
--- a/chrome/browser/gtk/bookmark_bar_gtk.h
+++ b/chrome/browser/gtk/bookmark_bar_gtk.h
@@ -15,7 +15,6 @@
class Browser;
class CustomContainerButton;
-class NineBox;
class PageNavigator;
class Profile;
@@ -57,10 +56,22 @@ class BookmarkBarGtk : public BookmarkModelObserver {
bool IsAlwaysShown();
private:
- // Helper function which destroys all the bookmark buttons in
- // |current_bookmark_buttons_|.
+ // Helper function that sets visual properties of GtkButton |button| to the
+ // contents of |node|.
+ void ConfigureButtonForNode(BookmarkNode* node,
+ GtkWidget* button);
+
+ // Helper function which generates GtkToolItems for |bookmark_toolbar_|.
+ void CreateAllBookmarkButtons(BookmarkNode* node);
+
+ // Helper function which destroys all the bookmark buttons in the GtkToolbar.
void RemoveAllBookmarkButtons();
+ // Returns the number of buttons corresponding to starred urls/groups. This
+ // is equivalent to the number of children the bookmark bar node from the
+ // bookmark bar model has.
+ int GetBookmarkButtonCount();
+
// Overridden from BookmarkModelObserver:
// Invoked when the bookmark bar model has finished loading. Creates a button
@@ -68,46 +79,27 @@ class BookmarkBarGtk : public BookmarkModelObserver {
virtual void Loaded(BookmarkModel* model);
// Invoked when the model is being deleted.
- virtual void BookmarkModelBeingDeleted(BookmarkModel* model) {
- NOTIMPLEMENTED();
- }
+ virtual void BookmarkModelBeingDeleted(BookmarkModel* model);
// Invoked when a node has moved.
virtual void BookmarkNodeMoved(BookmarkModel* model,
BookmarkNode* old_parent,
int old_index,
BookmarkNode* new_parent,
- int new_index) {
- NOTIMPLEMENTED();
- }
-
+ int new_index);
virtual void BookmarkNodeAdded(BookmarkModel* model,
BookmarkNode* parent,
- int index) {
- NOTIMPLEMENTED();
- }
-
+ int index);
virtual void BookmarkNodeRemoved(BookmarkModel* model,
BookmarkNode* parent,
- int index) {
- NOTIMPLEMENTED();
- }
-
+ int index);
virtual void BookmarkNodeChanged(BookmarkModel* model,
- BookmarkNode* node) {
- NOTIMPLEMENTED();
- }
-
+ BookmarkNode* node);
// Invoked when a favicon has finished loading.
virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model,
- BookmarkNode* node) {
- NOTIMPLEMENTED();
- }
-
+ BookmarkNode* node);
virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
- BookmarkNode* node) {
- NOTIMPLEMENTED();
- }
+ BookmarkNode* node);
private:
GtkWidget* CreateBookmarkButton(BookmarkNode* node);
@@ -115,6 +107,12 @@ class BookmarkBarGtk : public BookmarkModelObserver {
std::string BuildTooltip(BookmarkNode* node);
+ // Finds the BookmarkNode from the model associated with |button|.
+ BookmarkNode* GetNodeForToolButton(GtkWidget* button);
+
+ // Creates and displays a popup menu for BookmarkNode |node|.
+ void PopupMenuForNode(BookmarkNode* node, GdkEventButton* event);
+
// GtkButton callbacks
static gboolean OnButtonPressed(GtkWidget* sender,
GdkEventButton* event,
@@ -143,6 +141,12 @@ class BookmarkBarGtk : public BookmarkModelObserver {
GdkDragContext* context,
guint time,
BookmarkBarGtk* bar);
+ static gboolean OnToolbarDragDrop(GtkWidget* toolbar,
+ GdkDragContext* drag_context,
+ gint x,
+ gint y,
+ guint time,
+ BookmarkBarGtk* bar);
Profile* profile_;
@@ -184,11 +188,6 @@ class BookmarkBarGtk : public BookmarkModelObserver {
// Whether we should show the instructional text in the bookmark bar.
bool show_instructions_;
-
- // The theme graphics for when the mouse is over the button.
- scoped_ptr<NineBox> nine_box_prelight_;
- // The theme graphics for when the button is clicked.
- scoped_ptr<NineBox> nine_box_active_;
};
#endif // CHROME_BROWSER_GTK_BOOKMARK_BAR_GTK_H_