| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/gtk/back_forward_button_gtk.h" | 5 #include "chrome/browser/gtk/back_forward_button_gtk.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "chrome/app/chrome_dll_resource.h" | 11 #include "chrome/app/chrome_dll_resource.h" |
| 12 #include "chrome/browser/browser.h" | 12 #include "chrome/browser/browser.h" |
| 13 #include "chrome/browser/gtk/back_forward_menu_model_gtk.h" | 13 #include "chrome/browser/gtk/back_forward_menu_model_gtk.h" |
| 14 #include "chrome/browser/gtk/menu_gtk.h" | 14 #include "chrome/browser/gtk/menu_gtk.h" |
| 15 #include "chrome/common/gtk_util.h" |
| 15 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 16 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 17 | 18 |
| 18 // The time in milliseconds between when the user clicks and the menu appears. | 19 // The time in milliseconds between when the user clicks and the menu appears. |
| 19 static const int kMenuTimerDelay = 500; | 20 static const int kMenuTimerDelay = 500; |
| 20 | 21 |
| 21 BackForwardButtonGtk::BackForwardButtonGtk(Browser* browser, bool is_forward) | 22 BackForwardButtonGtk::BackForwardButtonGtk(Browser* browser, bool is_forward) |
| 22 : browser_(browser), | 23 : browser_(browser), |
| 23 is_forward_(is_forward), | 24 is_forward_(is_forward), |
| 25 last_release_event_flags_(0), |
| 24 show_menu_factory_(this) { | 26 show_menu_factory_(this) { |
| 25 int normal, active, highlight, depressed, tooltip; | 27 int normal, active, highlight, depressed, tooltip; |
| 26 if (is_forward) { | 28 if (is_forward) { |
| 27 normal = IDR_FORWARD; | 29 normal = IDR_FORWARD; |
| 28 active = IDR_FORWARD_P; | 30 active = IDR_FORWARD_P; |
| 29 highlight = IDR_FORWARD_H; | 31 highlight = IDR_FORWARD_H; |
| 30 depressed = IDR_FORWARD_D; | 32 depressed = IDR_FORWARD_D; |
| 31 tooltip = IDS_TOOLTIP_FORWARD; | 33 tooltip = IDS_TOOLTIP_FORWARD; |
| 32 } else { | 34 } else { |
| 33 normal = IDR_BACK; | 35 normal = IDR_BACK; |
| 34 active = IDR_BACK_P; | 36 active = IDR_BACK_P; |
| 35 highlight = IDR_BACK_H; | 37 highlight = IDR_BACK_H; |
| 36 depressed = IDR_BACK_D; | 38 depressed = IDR_BACK_D; |
| 37 tooltip = IDS_TOOLTIP_BACK; | 39 tooltip = IDS_TOOLTIP_BACK; |
| 38 } | 40 } |
| 39 button_.reset(new CustomDrawButton(normal, active, highlight, depressed)); | 41 button_.reset(new CustomDrawButton(normal, active, highlight, depressed)); |
| 40 gtk_widget_set_tooltip_text(widget(), | 42 gtk_widget_set_tooltip_text(widget(), |
| 41 l10n_util::GetStringUTF8(tooltip).c_str()); | 43 l10n_util::GetStringUTF8(tooltip).c_str()); |
| 42 menu_model_.reset(new BackForwardMenuModelGtk(browser, | 44 menu_model_.reset(new BackForwardMenuModelGtk(browser, |
| 43 is_forward ? | 45 is_forward ? |
| 44 BackForwardMenuModel::FORWARD_MENU : | 46 BackForwardMenuModel::FORWARD_MENU : |
| 45 BackForwardMenuModel::BACKWARD_MENU, | 47 BackForwardMenuModel::BACKWARD_MENU, |
| 46 this)); | 48 this)); |
| 47 | 49 |
| 48 g_signal_connect(widget(), "clicked", | 50 g_signal_connect(widget(), "clicked", |
| 49 G_CALLBACK(OnClick), this); | 51 G_CALLBACK(OnClick), this); |
| 50 g_signal_connect(widget(), "button-press-event", | 52 g_signal_connect(widget(), "button-press-event", |
| 51 G_CALLBACK(OnButtonPress), this); | 53 G_CALLBACK(OnButtonPress), this); |
| 54 g_signal_connect(widget(), "button-release-event", |
| 55 G_CALLBACK(OnButtonRelease), this); |
| 52 gtk_widget_add_events(widget(), GDK_POINTER_MOTION_MASK); | 56 gtk_widget_add_events(widget(), GDK_POINTER_MOTION_MASK); |
| 53 g_signal_connect(widget(), "motion-notify-event", | 57 g_signal_connect(widget(), "motion-notify-event", |
| 54 G_CALLBACK(OnMouseMove), this); | 58 G_CALLBACK(OnMouseMove), this); |
| 55 | 59 |
| 56 // Popup the menu as left-aligned relative to this widget rather than the | 60 // Popup the menu as left-aligned relative to this widget rather than the |
| 57 // default of right aligned. | 61 // default of right aligned. |
| 58 g_object_set_data(G_OBJECT(widget()), "left-align-popup", | 62 g_object_set_data(G_OBJECT(widget()), "left-align-popup", |
| 59 reinterpret_cast<void*>(true)); | 63 reinterpret_cast<void*>(true)); |
| 64 |
| 65 gtk_util::SetButtonTriggersNavigation(widget()); |
| 60 } | 66 } |
| 61 | 67 |
| 62 BackForwardButtonGtk::~BackForwardButtonGtk() { | 68 BackForwardButtonGtk::~BackForwardButtonGtk() { |
| 63 } | 69 } |
| 64 | 70 |
| 65 void BackForwardButtonGtk::StoppedShowingMenu() { | 71 void BackForwardButtonGtk::StoppedShowingMenu() { |
| 66 button_->UnsetPaintOverride(); | 72 button_->UnsetPaintOverride(); |
| 67 } | 73 } |
| 68 | 74 |
| 69 void BackForwardButtonGtk::ShowBackForwardMenu() { | 75 void BackForwardButtonGtk::ShowBackForwardMenu() { |
| 70 menu_.reset(new MenuGtk(menu_model_.get(), true)); | 76 menu_.reset(new MenuGtk(menu_model_.get(), true)); |
| 71 button_->SetPaintOverride(GTK_STATE_ACTIVE); | 77 button_->SetPaintOverride(GTK_STATE_ACTIVE); |
| 72 | 78 |
| 73 // gtk_menu_popup will ignore the first mouse button release if it matches | 79 // gtk_menu_popup will ignore the first mouse button release if it matches |
| 74 // the button type and is within a short span of the time we pass here. | 80 // the button type and is within a short span of the time we pass here. |
| 75 // Since this menu is not popped up by a button press (instead, it is popped | 81 // Since this menu is not popped up by a button press (instead, it is popped |
| 76 // up either on a timer or on a drag) this doesn't apply to us and we can | 82 // up either on a timer or on a drag) this doesn't apply to us and we can |
| 77 // pass arbitrary values. | 83 // pass arbitrary values. |
| 78 menu_->Popup(widget(), 1, gtk_get_current_event_time()); | 84 menu_->Popup(widget(), 1, gtk_get_current_event_time()); |
| 79 } | 85 } |
| 80 | 86 |
| 81 // static | 87 // static |
| 82 void BackForwardButtonGtk::OnClick(GtkWidget* widget, | 88 void BackForwardButtonGtk::OnClick(GtkWidget* widget, |
| 83 BackForwardButtonGtk* button) { | 89 BackForwardButtonGtk* button) { |
| 84 button->show_menu_factory_.RevokeAll(); | 90 button->show_menu_factory_.RevokeAll(); |
| 85 | 91 |
| 86 button->browser_->ExecuteCommand(button->is_forward_ ? | 92 DCHECK(button->last_release_event_flags_ != 0); |
| 87 IDC_FORWARD : IDC_BACK); | 93 button->browser_->ExecuteCommandWithDisposition( |
| 94 button->is_forward_ ? IDC_FORWARD : IDC_BACK, |
| 95 event_utils::DispositionFromEventFlags( |
| 96 button->last_release_event_flags_)); |
| 88 } | 97 } |
| 89 | 98 |
| 90 // static | 99 // static |
| 91 gboolean BackForwardButtonGtk::OnButtonPress(GtkWidget* widget, | 100 gboolean BackForwardButtonGtk::OnButtonPress(GtkWidget* widget, |
| 92 GdkEventButton* event, BackForwardButtonGtk* button) { | 101 GdkEventButton* event, BackForwardButtonGtk* button) { |
| 102 button->last_release_event_flags_ = 0; |
| 103 |
| 93 if (event->button != 1) | 104 if (event->button != 1) |
| 94 return FALSE; | 105 return FALSE; |
| 95 | 106 |
| 96 button->y_position_of_last_press_ = event->y; | 107 button->y_position_of_last_press_ = event->y; |
| 97 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 108 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 98 button->show_menu_factory_.NewRunnableMethod( | 109 button->show_menu_factory_.NewRunnableMethod( |
| 99 &BackForwardButtonGtk::ShowBackForwardMenu), | 110 &BackForwardButtonGtk::ShowBackForwardMenu), |
| 100 kMenuTimerDelay); | 111 kMenuTimerDelay); |
| 101 return FALSE; | 112 return FALSE; |
| 102 } | 113 } |
| 103 | 114 |
| 104 // static | 115 // static |
| 116 gboolean BackForwardButtonGtk::OnButtonRelease(GtkWidget* widget, |
| 117 GdkEventButton* event, BackForwardButtonGtk* button) { |
| 118 button->last_release_event_flags_ = event->state; |
| 119 return FALSE; |
| 120 } |
| 121 |
| 122 // static |
| 105 gboolean BackForwardButtonGtk::OnMouseMove(GtkWidget* widget, | 123 gboolean BackForwardButtonGtk::OnMouseMove(GtkWidget* widget, |
| 106 GdkEventMotion* event, BackForwardButtonGtk* button) { | 124 GdkEventMotion* event, BackForwardButtonGtk* button) { |
| 107 // If we aren't waiting to show the back forward menu, do nothing. | 125 // If we aren't waiting to show the back forward menu, do nothing. |
| 108 if (button->show_menu_factory_.empty()) | 126 if (button->show_menu_factory_.empty()) |
| 109 return FALSE; | 127 return FALSE; |
| 110 | 128 |
| 111 // We only count moves about a certain threshold. | 129 // We only count moves about a certain threshold. |
| 112 GtkSettings* settings = gtk_widget_get_settings(widget); | 130 GtkSettings* settings = gtk_widget_get_settings(widget); |
| 113 int drag_min_distance; | 131 int drag_min_distance; |
| 114 g_object_get(settings, "gtk-dnd-drag-threshold", &drag_min_distance, NULL); | 132 g_object_get(settings, "gtk-dnd-drag-threshold", &drag_min_distance, NULL); |
| 115 if (event->y - button->y_position_of_last_press_ < drag_min_distance) | 133 if (event->y - button->y_position_of_last_press_ < drag_min_distance) |
| 116 return FALSE; | 134 return FALSE; |
| 117 | 135 |
| 118 // We will show the menu now. Cancel the delayed event. | 136 // We will show the menu now. Cancel the delayed event. |
| 119 button->show_menu_factory_.RevokeAll(); | 137 button->show_menu_factory_.RevokeAll(); |
| 120 button->ShowBackForwardMenu(); | 138 button->ShowBackForwardMenu(); |
| 121 return FALSE; | 139 return FALSE; |
| 122 } | 140 } |
| OLD | NEW |