diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 00:00:56 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 00:00:56 +0000 |
commit | c5c0e096269948fa0d323ce2016d6dfdbf04543d (patch) | |
tree | 5844d879a46b2fadb1a1e1dbfa40c3aafd9f46be | |
parent | 3269829189afca38b26b968f693bcc7e611f37c6 (diff) | |
download | chromium_src-c5c0e096269948fa0d323ce2016d6dfdbf04543d.zip chromium_src-c5c0e096269948fa0d323ce2016d6dfdbf04543d.tar.gz chromium_src-c5c0e096269948fa0d323ce2016d6dfdbf04543d.tar.bz2 |
GTK: button clicking cleanup.
I found a much better way to implement middle-click-to-navigate: use gtk_get_current_event() to get the button release event that triggered the "clicked" signal. This simplifies greatly a lot of places that I had previously added complication.
Also this adds middle click to navigate on the go button.
Also this makes middle click on a bookmark bar button depress the button.
Review URL: http://codereview.chromium.org/165261
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22976 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/gtk/back_forward_button_gtk.cc | 18 | ||||
-rw-r--r-- | chrome/browser/gtk/back_forward_button_gtk.h | 7 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_bar_gtk.cc | 57 | ||||
-rw-r--r-- | chrome/browser/gtk/bookmark_bar_gtk.h | 14 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_gtk.cc | 15 | ||||
-rw-r--r-- | chrome/browser/gtk/browser_toolbar_gtk.h | 7 | ||||
-rw-r--r-- | chrome/browser/gtk/download_shelf_gtk.cc | 1 | ||||
-rw-r--r-- | chrome/browser/gtk/go_button_gtk.cc | 10 | ||||
-rw-r--r-- | chrome/browser/gtk/gtk_chrome_link_button.cc | 47 | ||||
-rw-r--r-- | chrome/browser/gtk/gtk_chrome_link_button.h | 7 | ||||
-rw-r--r-- | chrome/browser/gtk/infobar_gtk.cc | 4 |
11 files changed, 34 insertions, 153 deletions
diff --git a/chrome/browser/gtk/back_forward_button_gtk.cc b/chrome/browser/gtk/back_forward_button_gtk.cc index 009be74..7f4bd69 100644 --- a/chrome/browser/gtk/back_forward_button_gtk.cc +++ b/chrome/browser/gtk/back_forward_button_gtk.cc @@ -24,7 +24,6 @@ static const int kMenuTimerDelay = 500; BackForwardButtonGtk::BackForwardButtonGtk(Browser* browser, bool is_forward) : browser_(browser), is_forward_(is_forward), - last_release_event_flags_(0), show_menu_factory_(this) { int normal, active, highlight, depressed, tooltip; const char* stock; @@ -59,8 +58,6 @@ BackForwardButtonGtk::BackForwardButtonGtk(Browser* browser, bool is_forward) G_CALLBACK(OnClick), this); g_signal_connect(widget(), "button-press-event", G_CALLBACK(OnButtonPress), this); - g_signal_connect(widget(), "button-release-event", - G_CALLBACK(OnButtonRelease), this); gtk_widget_add_events(widget(), GDK_POINTER_MOTION_MASK); g_signal_connect(widget(), "motion-notify-event", G_CALLBACK(OnMouseMove), this); @@ -96,19 +93,17 @@ void BackForwardButtonGtk::ShowBackForwardMenu() { void BackForwardButtonGtk::OnClick(GtkWidget* widget, BackForwardButtonGtk* button) { button->show_menu_factory_.RevokeAll(); + GdkEventButton* event = + reinterpret_cast<GdkEventButton*>(gtk_get_current_event()); - DCHECK(button->last_release_event_flags_ != 0); button->browser_->ExecuteCommandWithDisposition( button->is_forward_ ? IDC_FORWARD : IDC_BACK, - event_utils::DispositionFromEventFlags( - button->last_release_event_flags_)); + event_utils::DispositionFromEventFlags(event->state)); } // static gboolean BackForwardButtonGtk::OnButtonPress(GtkWidget* widget, GdkEventButton* event, BackForwardButtonGtk* button) { - button->last_release_event_flags_ = 0; - if (event->button == 3) button->ShowBackForwardMenu(); @@ -124,13 +119,6 @@ gboolean BackForwardButtonGtk::OnButtonPress(GtkWidget* widget, } // static -gboolean BackForwardButtonGtk::OnButtonRelease(GtkWidget* widget, - GdkEventButton* event, BackForwardButtonGtk* button) { - button->last_release_event_flags_ = event->state; - return FALSE; -} - -// static gboolean BackForwardButtonGtk::OnMouseMove(GtkWidget* widget, GdkEventMotion* event, BackForwardButtonGtk* button) { // If we aren't waiting to show the back forward menu, do nothing. diff --git a/chrome/browser/gtk/back_forward_button_gtk.h b/chrome/browser/gtk/back_forward_button_gtk.h index cb4c1ab..49ea05f 100644 --- a/chrome/browser/gtk/back_forward_button_gtk.h +++ b/chrome/browser/gtk/back_forward_button_gtk.h @@ -36,10 +36,6 @@ class BackForwardButtonGtk { GdkEventButton* event, BackForwardButtonGtk* toolbar); - static gboolean OnButtonRelease(GtkWidget* button, - GdkEventButton* event, - BackForwardButtonGtk* toolbar); - // If there is a timer to show the dropdown menu, and the mouse has moved // sufficiently down the screen, cancel the timer and immediately show the // menu. @@ -61,9 +57,6 @@ class BackForwardButtonGtk { // Whether this button is a forward button. bool is_forward_; - // The event state from the last button release. - int last_release_event_flags_; - // The dropdown menu delegate. scoped_ptr<BackForwardMenuModelGtk> menu_model_; diff --git a/chrome/browser/gtk/bookmark_bar_gtk.cc b/chrome/browser/gtk/bookmark_bar_gtk.cc index b3f4e96..359a2bc 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.cc +++ b/chrome/browser/gtk/bookmark_bar_gtk.cc @@ -82,7 +82,6 @@ BookmarkBarGtk::BookmarkBarGtk(Profile* profile, Browser* browser, window_(window), model_(NULL), instructions_(NULL), - ignore_button_release_(false), dragged_node_(NULL), toolbar_drop_item_(NULL), theme_provider_(GtkThemeProvider::GetFrom(profile)), @@ -437,8 +436,9 @@ GtkWidget* BookmarkBarGtk::CreateBookmarkButton(const BookmarkNode* node) { // button. g_signal_connect(G_OBJECT(button), "button-press-event", G_CALLBACK(OnButtonPressed), this); - g_signal_connect(G_OBJECT(button), "button-release-event", - G_CALLBACK(OnButtonReleased), this); + g_signal_connect(G_OBJECT(button), "clicked", + G_CALLBACK(OnClicked), this); + gtk_util::SetButtonTriggersNavigation(button); } else { // TODO(erg): This button can also be a drop target. ConnectFolderButtonEvents(button); @@ -470,8 +470,8 @@ void BookmarkBarGtk::ConnectFolderButtonEvents(GtkWidget* widget) { // button. g_signal_connect(G_OBJECT(widget), "button-press-event", G_CALLBACK(OnButtonPressed), this); - g_signal_connect(G_OBJECT(widget), "button-release-event", - G_CALLBACK(OnFolderButtonReleased), this); + g_signal_connect(G_OBJECT(widget), "clicked", + G_CALLBACK(OnFolderClicked), this); } const BookmarkNode* BookmarkBarGtk::GetNodeForToolButton(GtkWidget* widget) { @@ -534,10 +534,6 @@ void BookmarkBarGtk::PopupMenuForNode(GtkWidget* sender, gboolean BookmarkBarGtk::OnButtonPressed(GtkWidget* sender, GdkEventButton* event, BookmarkBarGtk* bar) { - if (sender != bar->other_bookmarks_button_ && - sender != bar->bookmark_toolbar_.get()) - bar->ignore_button_release_ = false; - if (event->button == 3) { const BookmarkNode* node = bar->GetNodeForToolButton(sender); DCHECK(node); @@ -549,25 +545,15 @@ gboolean BookmarkBarGtk::OnButtonPressed(GtkWidget* sender, } // static -gboolean BookmarkBarGtk::OnButtonReleased(GtkWidget* sender, - GdkEventButton* event, - BookmarkBarGtk* bar) { - if (bar->ignore_button_release_) { - // Don't handle this message; it was a drag. - bar->ignore_button_release_ = false; - return FALSE; - } - - // Don't take any action if the user releases outside the button. - if (event->x < 0 || event->y < 0 || event->x >= sender->allocation.width || - event->y >= sender->allocation.height) { - return FALSE; - } - +void BookmarkBarGtk::OnClicked(GtkWidget* sender, + BookmarkBarGtk* bar) { const BookmarkNode* node = bar->GetNodeForToolButton(sender); DCHECK(node); DCHECK(bar->page_navigator_); + GdkEventButton* event = + reinterpret_cast<GdkEventButton*>(gtk_get_current_event()); + if (node->is_url()) { bar->page_navigator_->OpenURL( node->GetURL(), GURL(), @@ -580,9 +566,6 @@ gboolean BookmarkBarGtk::OnButtonReleased(GtkWidget* sender, } UserMetrics::RecordAction(L"ClickedBookmarkBarURLButton", bar->profile_); - - // Allow other handlers to run so the button state is updated correctly. - return FALSE; } // static @@ -593,10 +576,6 @@ void BookmarkBarGtk::OnButtonDragBegin(GtkWidget* button, // won't get destroyed. g_object_ref(button->parent); - // Signal to any future OnButtonReleased calls that we're dragging instead of - // pressing. - bar->ignore_button_release_ = true; - const BookmarkNode* node = bar->GetNodeForToolButton(button); DCHECK(!bar->dragged_node_); bar->dragged_node_ = node; @@ -640,15 +619,8 @@ void BookmarkBarGtk::OnButtonDragGet(GtkWidget* widget, GdkDragContext* context, } // static -gboolean BookmarkBarGtk::OnFolderButtonReleased(GtkWidget* sender, - GdkEventButton* event, - BookmarkBarGtk* bar) { - if (bar->ignore_button_release_) { - // Don't handle this message; it was a drag. - bar->ignore_button_release_ = false; - return FALSE; - } - +void BookmarkBarGtk::OnFolderClicked(GtkWidget* sender, + BookmarkBarGtk* bar) { const BookmarkNode* node = bar->GetNodeForToolButton(sender); DCHECK(node); DCHECK(bar->page_navigator_); @@ -660,10 +632,9 @@ gboolean BookmarkBarGtk::OnFolderButtonReleased(GtkWidget* sender, node, 0, false)); + GdkEventButton* event = + reinterpret_cast<GdkEventButton*>(gtk_get_current_event()); bar->current_menu_->Popup(sender, event->button, event->time); - - // Allow other handlers to run so the button state is updated correctly. - return FALSE; } // static diff --git a/chrome/browser/gtk/bookmark_bar_gtk.h b/chrome/browser/gtk/bookmark_bar_gtk.h index 6600046..3136527 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.h +++ b/chrome/browser/gtk/bookmark_bar_gtk.h @@ -142,9 +142,8 @@ class BookmarkBarGtk : public AnimationDelegate, static gboolean OnButtonPressed(GtkWidget* sender, GdkEventButton* event, BookmarkBarGtk* bar); - static gboolean OnButtonReleased(GtkWidget* sender, - GdkEventButton* event, - BookmarkBarGtk* bar); + static void OnClicked(GtkWidget* sender, + BookmarkBarGtk* bar); static void OnButtonDragBegin(GtkWidget* widget, GdkDragContext* drag_context, BookmarkBarGtk* bar); @@ -157,9 +156,8 @@ class BookmarkBarGtk : public AnimationDelegate, BookmarkBarGtk* bar); // GtkButton callbacks for folder buttons. - static gboolean OnFolderButtonReleased(GtkWidget* sender, - GdkEventButton* event, - BookmarkBarGtk* bar); + static void OnFolderClicked(GtkWidget* sender, + BookmarkBarGtk* bar); // GtkToolbar callbacks. static gboolean OnToolbarExpose(GtkWidget* widget, GdkEventExpose* event, @@ -216,10 +214,6 @@ class BookmarkBarGtk : public AnimationDelegate, // The other bookmarks button. GtkWidget* other_bookmarks_button_; - // Whether we should ignore the next button release event (because we were - // dragging). - bool ignore_button_release_; - // The BookmarkNode from the model being dragged. NULL when we aren't // dragging. const BookmarkNode* dragged_node_; diff --git a/chrome/browser/gtk/browser_toolbar_gtk.cc b/chrome/browser/gtk/browser_toolbar_gtk.cc index 1a3f83d..81ec816 100644 --- a/chrome/browser/gtk/browser_toolbar_gtk.cc +++ b/chrome/browser/gtk/browser_toolbar_gtk.cc @@ -78,8 +78,7 @@ BrowserToolbarGtk::BrowserToolbarGtk(Browser* browser, BrowserWindowGtk* window) model_(browser->toolbar_model()), browser_(browser), window_(window), - profile_(NULL), - last_release_event_flags_(0) { + profile_(NULL) { browser_->command_updater()->AddCommandObserver(IDC_BACK, this); browser_->command_updater()->AddCommandObserver(IDC_FORWARD, this); browser_->command_updater()->AddCommandObserver(IDC_RELOAD, this); @@ -443,8 +442,6 @@ CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( localized_tooltip.c_str()); g_signal_connect(button->widget(), "clicked", G_CALLBACK(OnButtonClick), this); - g_signal_connect(button->widget(), "button-release-event", - G_CALLBACK(OnButtonRelease), this); gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0); return button; @@ -626,15 +623,7 @@ void BrowserToolbarGtk::OnButtonClick(GtkWidget* button, DCHECK_NE(tag, -1) << "Unexpected button click callback"; toolbar->browser_->ExecuteCommandWithDisposition(tag, event_utils::DispositionFromEventFlags( - toolbar->last_release_event_flags_)); -} - -// static -gboolean BrowserToolbarGtk::OnButtonRelease(GtkWidget* button, - GdkEventButton* event, - BrowserToolbarGtk* toolbar) { - toolbar->last_release_event_flags_ = event->state; - return FALSE; + reinterpret_cast<GdkEventButton*>(gtk_get_current_event())->state)); } // static diff --git a/chrome/browser/gtk/browser_toolbar_gtk.h b/chrome/browser/gtk/browser_toolbar_gtk.h index b3c3eef..f4fb16d 100644 --- a/chrome/browser/gtk/browser_toolbar_gtk.h +++ b/chrome/browser/gtk/browser_toolbar_gtk.h @@ -120,10 +120,6 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver, // Gtk callback for the "clicked" signal. static void OnButtonClick(GtkWidget* button, BrowserToolbarGtk* toolbar); - // Gtk callback for the "button-release-event" signal. - static gboolean OnButtonRelease(GtkWidget* button, GdkEventButton* event, - BrowserToolbarGtk* toolbar); - // Gtk callback to intercept mouse clicks to the menu buttons. static gboolean OnMenuButtonPressEvent(GtkWidget* button, GdkEventButton* event, @@ -199,9 +195,6 @@ class BrowserToolbarGtk : public CommandUpdater::CommandObserver, // Controls whether or not a home button should be shown on the toolbar. BooleanPrefMember show_home_button_; - // The event state the last time we observed a button release event. - int last_release_event_flags_; - NotificationRegistrar registrar_; // A GtkEntry that isn't part of the hierarchy. We keep this for native diff --git a/chrome/browser/gtk/download_shelf_gtk.cc b/chrome/browser/gtk/download_shelf_gtk.cc index 4606577..9601fd06 100644 --- a/chrome/browser/gtk/download_shelf_gtk.cc +++ b/chrome/browser/gtk/download_shelf_gtk.cc @@ -101,6 +101,7 @@ DownloadShelfGtk::DownloadShelfGtk(Browser* browser, GtkWidget* parent) GTK_CHROME_LINK_BUTTON(link_button), FALSE); g_signal_connect(link_button, "clicked", G_CALLBACK(OnButtonClick), this); + gtk_util::SetButtonTriggersNavigation(link_button); // Until we switch to vector graphics, force the font size. // 13.4px == 10pt @ 96dpi gtk_util::ForceFontSizePixels(GTK_CHROME_LINK_BUTTON(link_button)->label, diff --git a/chrome/browser/gtk/go_button_gtk.cc b/chrome/browser/gtk/go_button_gtk.cc index ec9f594..a976a5c 100644 --- a/chrome/browser/gtk/go_button_gtk.cc +++ b/chrome/browser/gtk/go_button_gtk.cc @@ -13,6 +13,7 @@ #include "chrome/browser/gtk/gtk_theme_provider.h" #include "chrome/browser/gtk/location_bar_view_gtk.h" #include "chrome/browser/profile.h" +#include "chrome/common/gtk_util.h" #include "chrome/common/notification_service.h" #include "grit/generated_resources.h" #include "grit/theme_resources.h" @@ -49,6 +50,7 @@ GoButtonGtk::GoButtonGtk(LocationBarViewGtk* location_bar, Browser* browser) GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS); SetTooltip(); + gtk_util::SetButtonTriggersNavigation(widget()); if (theme_provider_) { theme_provider_->InitThemesFor(this); @@ -146,8 +148,12 @@ gboolean GoButtonGtk::OnClicked(GtkButton* widget, GoButtonGtk* button) { button->ChangeMode(MODE_GO, true); } else if (button->visible_mode_ == MODE_GO && button->stop_timer_.empty()) { // If the go button is visible and not within the double click timer, go. - if (button->browser_) - button->browser_->ExecuteCommand(IDC_GO); + GdkEventButton* event = + reinterpret_cast<GdkEventButton*>(gtk_get_current_event()); + if (button->browser_) { + button->browser_->ExecuteCommandWithDisposition(IDC_GO, + event_utils::DispositionFromEventFlags(event->state)); + } // Figure out the system double-click time. if (button->button_delay_ == 0) { diff --git a/chrome/browser/gtk/gtk_chrome_link_button.cc b/chrome/browser/gtk/gtk_chrome_link_button.cc index 735dec6..d4c7fb2 100644 --- a/chrome/browser/gtk/gtk_chrome_link_button.cc +++ b/chrome/browser/gtk/gtk_chrome_link_button.cc @@ -120,39 +120,6 @@ static gboolean gtk_chrome_link_button_expose(GtkWidget* widget, return TRUE; } -static gboolean gtk_chrome_link_button_button_press(GtkWidget* widget, - GdkEventButton* event) { - GtkButton* button; - - if (event->type == GDK_BUTTON_PRESS) { - button = GTK_BUTTON(widget); - - if (button->focus_on_click && !GTK_WIDGET_HAS_FOCUS (widget)) - gtk_widget_grab_focus(widget); - - if (event->button == 1 || event->button == 2) - gtk_button_pressed(button); - } - - return TRUE; -} - -static gboolean gtk_chrome_link_button_button_release(GtkWidget* widget, - GdkEventButton* event) { - GtkButton* button = GTK_BUTTON(widget); - GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(widget); - - free(link_button->click_button_event); - link_button->click_button_event = static_cast<GdkEventButton*>( - malloc(sizeof(GdkEventButton))); - *link_button->click_button_event = *event; - - if (event->button == 1 || event->button == 2) - gtk_button_released(button); - - return TRUE; -} - static void gtk_chrome_link_button_enter(GtkButton* button) { GtkWidget* widget = GTK_WIDGET(button); GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); @@ -161,10 +128,7 @@ static void gtk_chrome_link_button_enter(GtkButton* button) { static void gtk_chrome_link_button_leave(GtkButton* button) { GtkWidget* widget = GTK_WIDGET(button); - GtkChromeLinkButton* link_button = GTK_CHROME_LINK_BUTTON(button); gdk_window_set_cursor(widget->window, NULL); - free(link_button->click_button_event); - link_button->click_button_event = NULL; } static void gtk_chrome_link_button_destroy(GtkObject* object) { @@ -185,9 +149,6 @@ static void gtk_chrome_link_button_destroy(GtkObject* object) { button->hand_cursor = NULL; } - free(button->click_button_event); - button->click_button_event = NULL; - free(button->text); button->text = NULL; @@ -203,8 +164,6 @@ static void gtk_chrome_link_button_class_init( GtkObjectClass* object_class = reinterpret_cast<GtkObjectClass*>(link_button_class); widget_class->expose_event = >k_chrome_link_button_expose; - widget_class->button_press_event = >k_chrome_link_button_button_press; - widget_class->button_release_event = >k_chrome_link_button_button_release; button_class->enter = >k_chrome_link_button_enter; button_class->leave = >k_chrome_link_button_leave; object_class->destroy = >k_chrome_link_button_destroy; @@ -223,7 +182,6 @@ static void gtk_chrome_link_button_init(GtkChromeLinkButton* button) { button->native_markup = NULL; button->using_native_theme = TRUE; button->hand_cursor = gdk_cursor_new(GDK_HAND2); - button->click_button_event = NULL; button->text = NULL; gtk_container_add(GTK_CONTAINER(button), button->label); @@ -254,9 +212,4 @@ void gtk_chrome_link_button_set_use_gtk_theme(GtkChromeLinkButton* button, } } -const GdkEventButton* gtk_chrome_link_button_get_event_for_click( - GtkChromeLinkButton* button) { - return button->click_button_event; -} - G_END_DECLS diff --git a/chrome/browser/gtk/gtk_chrome_link_button.h b/chrome/browser/gtk/gtk_chrome_link_button.h index e8cf891..adc6771 100644 --- a/chrome/browser/gtk/gtk_chrome_link_button.h +++ b/chrome/browser/gtk/gtk_chrome_link_button.h @@ -43,7 +43,6 @@ struct _GtkChromeLinkButton { gchar* native_markup; gboolean using_native_theme; GdkCursor* hand_cursor; - GdkEventButton* click_button_event; gchar* text; gboolean uses_markup; }; @@ -63,12 +62,6 @@ GtkWidget* gtk_chrome_link_button_new_with_markup(const char* markup); void gtk_chrome_link_button_set_use_gtk_theme(GtkChromeLinkButton* button, gboolean use_gtk); -// Call this from within a "clicked" handler to get the release event that -// triggered the click. It will return NULL if the click was triggered by a -// keyboard event. -const GdkEventButton* gtk_chrome_link_button_get_event_for_click( - GtkChromeLinkButton* button); - GType gtk_chrome_link_button_get_type(); G_END_DECLS diff --git a/chrome/browser/gtk/infobar_gtk.cc b/chrome/browser/gtk/infobar_gtk.cc index 47aa247..5ca91ea 100644 --- a/chrome/browser/gtk/infobar_gtk.cc +++ b/chrome/browser/gtk/infobar_gtk.cc @@ -188,6 +188,7 @@ class LinkInfoBar : public InfoBar { GTK_CHROME_LINK_BUTTON(link_button), FALSE); g_signal_connect(link_button, "clicked", G_CALLBACK(OnLinkClick), this); + gtk_util::SetButtonTriggersNavigation(link_button); // If link_offset is npos, we right-align the link instead of embedding it // in the text. @@ -220,8 +221,7 @@ class LinkInfoBar : public InfoBar { private: static void OnLinkClick(GtkWidget* button, LinkInfoBar* link_info_bar) { const GdkEventButton* button_click_event = - gtk_chrome_link_button_get_event_for_click( - GTK_CHROME_LINK_BUTTON(button)); + reinterpret_cast<GdkEventButton*>(gtk_get_current_event()); WindowOpenDisposition disposition = CURRENT_TAB; if (button_click_event) { disposition = event_utils::DispositionFromEventFlags( |