1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/gtk/hover_controller_gtk.h"
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/ui/gtk/gtk_chrome_button.h"
9 #include "ui/gfx/gtk_compat.h"
11 static const gchar
* kHoverControllerGtkKey
= "__HOVER_CONTROLLER_GTK__";
13 HoverControllerGtk::HoverControllerGtk(GtkWidget
* button
)
14 : throb_animation_(this),
15 hover_animation_(this),
17 g_object_ref(button_
);
18 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
), 0);
20 signals_
.Connect(button_
, "enter-notify-event",
21 G_CALLBACK(OnEnterThunk
), this);
22 signals_
.Connect(button_
, "leave-notify-event",
23 G_CALLBACK(OnLeaveThunk
), this);
24 signals_
.Connect(button_
, "destroy",
25 G_CALLBACK(OnDestroyThunk
), this);
26 signals_
.Connect(button_
, "hierarchy-changed",
27 G_CALLBACK(OnHierarchyChangedThunk
), this);
30 if (g_object_get_data(G_OBJECT(button_
), kHoverControllerGtkKey
))
34 g_object_set_data(G_OBJECT(button
), kHoverControllerGtkKey
, this);
37 HoverControllerGtk::~HoverControllerGtk() {
40 void HoverControllerGtk::StartThrobbing(int cycles
) {
41 throb_animation_
.StartThrobbing(cycles
);
45 GtkWidget
* HoverControllerGtk::CreateChromeButton() {
46 GtkWidget
* widget
= gtk_chrome_button_new();
47 new HoverControllerGtk(widget
);
52 HoverControllerGtk
* HoverControllerGtk::GetHoverControllerGtk(
54 return reinterpret_cast<HoverControllerGtk
*>(
55 g_object_get_data(G_OBJECT(button
), kHoverControllerGtkKey
));
58 void HoverControllerGtk::Destroy() {
59 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
), -1.0);
61 g_object_set_data(G_OBJECT(button_
), kHoverControllerGtkKey
, NULL
);
62 g_object_unref(button_
);
68 void HoverControllerGtk::AnimationProgressed(const gfx::Animation
* animation
) {
72 // Ignore the hover animation if we are throbbing.
73 if (animation
== &hover_animation_
&& throb_animation_
.is_animating())
76 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
),
77 animation
->GetCurrentValue());
80 void HoverControllerGtk::AnimationEnded(const gfx::Animation
* animation
) {
83 if (animation
!= &throb_animation_
)
86 if (throb_animation_
.cycles_remaining() <= 1)
87 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
), 0);
90 void HoverControllerGtk::AnimationCanceled(const gfx::Animation
* animation
) {
91 AnimationEnded(animation
);
94 gboolean
HoverControllerGtk::OnEnter(GtkWidget
* widget
,
95 GdkEventCrossing
* event
) {
96 hover_animation_
.Show();
101 gboolean
HoverControllerGtk::OnLeave(GtkWidget
* widget
,
102 GdkEventCrossing
* event
) {
103 // When the user is holding a mouse button, we don't want to animate.
104 if (event
->state
& (GDK_BUTTON1_MASK
| GDK_BUTTON2_MASK
| GDK_BUTTON3_MASK
)) {
105 hover_animation_
.Reset();
106 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
), 0);
108 hover_animation_
.Hide();
114 void HoverControllerGtk::OnHierarchyChanged(GtkWidget
* widget
,
115 GtkWidget
* previous_toplevel
) {
116 // GTK+ does not emit leave-notify-event signals when a widget
117 // becomes unanchored, so manually unset the hover states.
118 if (!gtk_widget_is_toplevel(gtk_widget_get_toplevel(widget
))) {
119 gtk_widget_set_state(button_
, GTK_STATE_NORMAL
);
120 hover_animation_
.Reset();
121 gtk_chrome_button_set_hover_state(GTK_CHROME_BUTTON(button_
), 0.0);
125 void HoverControllerGtk::OnDestroy(GtkWidget
* widget
) {