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/slide_animator_gtk.h" | 5 #include "chrome/browser/gtk/slide_animator_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "chrome/common/animation.h" | 8 #include "chrome/common/animation.h" |
11 #include "chrome/common/slide_animation.h" | 9 #include "chrome/common/slide_animation.h" |
12 | 10 |
13 namespace { | 11 namespace { |
14 | 12 |
15 void OnSizeAllocate(GtkWidget* fixed, | 13 void OnFixedSizeAllocate(GtkWidget* fixed, |
16 GtkAllocation* allocation, | 14 GtkAllocation* allocation, |
17 GtkWidget* child) { | 15 GtkWidget* child) { |
18 gint height; | 16 gint height; |
19 gtk_widget_get_size_request(child, NULL, &height); | 17 gtk_widget_get_size_request(child, NULL, &height); |
20 // The size of the GtkFixed has changed. We want |child_| to match widths, | 18 // The size of the GtkFixed has changed. We want |child_| to match widths, |
21 // but the height should always be |widget_height_|. | 19 // but the height should always be |widget_height_|. |
22 gtk_widget_set_size_request(child, allocation->width, height); | 20 gtk_widget_set_size_request(child, allocation->width, height); |
23 } | 21 } |
24 | 22 |
25 } // namespace | 23 } // namespace |
26 | 24 |
27 SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child, | 25 SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child, |
28 Direction direction, | 26 Direction direction, |
29 int duration, | 27 int duration, |
30 bool linear, | 28 bool linear, |
31 Delegate* delegate) | 29 Delegate* delegate) |
32 : child_(child), | 30 : child_(child), |
33 direction_(direction), | 31 direction_(direction), |
34 delegate_(delegate) { | 32 delegate_(delegate), |
| 33 fixed_needs_resize_(false) { |
35 widget_.Own(gtk_fixed_new()); | 34 widget_.Own(gtk_fixed_new()); |
36 // We need to give the GtkFixed its own window so that painting will clip | 35 // We need to give the GtkFixed its own window so that painting will clip |
37 // correctly. | 36 // correctly. |
38 gtk_fixed_set_has_window(GTK_FIXED(widget_.get()), TRUE); | 37 gtk_fixed_set_has_window(GTK_FIXED(widget_.get()), TRUE); |
39 gtk_fixed_put(GTK_FIXED(widget_.get()), child, 0, 0); | 38 gtk_fixed_put(GTK_FIXED(widget_.get()), child, 0, 0); |
40 gtk_widget_set_size_request(widget_.get(), -1, 0); | 39 gtk_widget_set_size_request(widget_.get(), -1, 0); |
41 // We have to manually set the size request for |child_| every time the | 40 // We have to manually set the size request for |child_| every time the |
42 // GtkFixed changes sizes. | 41 // GtkFixed changes sizes. |
43 g_signal_connect(widget_.get(), "size-allocate", | 42 g_signal_connect(widget_.get(), "size-allocate", |
44 G_CALLBACK(OnSizeAllocate), child_); | 43 G_CALLBACK(OnFixedSizeAllocate), child_); |
| 44 |
| 45 // The size of the GtkFixed widget is set during animation. When we open |
| 46 // without showing the animation, we have to call AnimationProgressed |
| 47 // ourselves to properly set the size of the GtkFixed. We can't do this until |
| 48 // after the child has been allocated, hence we connect to "size-allocate" on |
| 49 // the child. |
| 50 g_signal_connect(child, "size-allocate", |
| 51 G_CALLBACK(OnChildSizeAllocate), this); |
45 | 52 |
46 animation_.reset(new SlideAnimation(this)); | 53 animation_.reset(new SlideAnimation(this)); |
47 // Default tween type is EASE_OUT. | 54 // Default tween type is EASE_OUT. |
48 if (linear) | 55 if (linear) |
49 animation_->SetTweenType(SlideAnimation::NONE); | 56 animation_->SetTweenType(SlideAnimation::NONE); |
50 if (duration != 0) | 57 if (duration != 0) |
51 animation_->SetSlideDuration(duration); | 58 animation_->SetSlideDuration(duration); |
52 } | 59 } |
53 | 60 |
54 SlideAnimatorGtk::~SlideAnimatorGtk() { | 61 SlideAnimatorGtk::~SlideAnimatorGtk() { |
55 widget_.Destroy(); | 62 widget_.Destroy(); |
56 } | 63 } |
57 | 64 |
58 void SlideAnimatorGtk::Open() { | 65 void SlideAnimatorGtk::Open() { |
59 gtk_widget_show_all(widget_.get()); | 66 gtk_widget_show_all(widget_.get()); |
60 animation_->Show(); | 67 animation_->Show(); |
61 } | 68 } |
62 | 69 |
63 void SlideAnimatorGtk::OpenWithoutAnimation() { | 70 void SlideAnimatorGtk::OpenWithoutAnimation() { |
64 animation_->Reset(1.0); | 71 animation_->Reset(1.0); |
65 Open(); | 72 Open(); |
| 73 fixed_needs_resize_ = true; |
66 } | 74 } |
67 | 75 |
68 void SlideAnimatorGtk::Close() { | 76 void SlideAnimatorGtk::Close() { |
69 animation_->Hide(); | 77 animation_->Hide(); |
70 } | 78 } |
71 | 79 |
72 bool SlideAnimatorGtk::IsShowing() { | 80 bool SlideAnimatorGtk::IsShowing() { |
73 return animation_->IsShowing(); | 81 return animation_->IsShowing(); |
74 } | 82 } |
75 | 83 |
76 void SlideAnimatorGtk::AnimationProgressed(const Animation* animation) { | 84 void SlideAnimatorGtk::AnimationProgressed(const Animation* animation) { |
77 int showing_height = child_->allocation.height * | 85 int showing_height = child_->allocation.height * |
78 animation_->GetCurrentValue(); | 86 animation_->GetCurrentValue(); |
79 if (direction_ == DOWN) { | 87 if (direction_ == DOWN) { |
80 gtk_fixed_move(GTK_FIXED(widget_.get()), child_, 0, | 88 gtk_fixed_move(GTK_FIXED(widget_.get()), child_, 0, |
81 showing_height - child_->allocation.height); | 89 showing_height - child_->allocation.height); |
82 } | 90 } |
83 gtk_widget_set_size_request(widget_.get(), -1, showing_height); | 91 gtk_widget_set_size_request(widget_.get(), -1, showing_height); |
84 } | 92 } |
85 | 93 |
86 void SlideAnimatorGtk::AnimationEnded(const Animation* animation) { | 94 void SlideAnimatorGtk::AnimationEnded(const Animation* animation) { |
87 if (!animation_->IsShowing() && delegate_) | 95 if (!animation_->IsShowing() && delegate_) |
88 delegate_->Closed(); | 96 delegate_->Closed(); |
89 } | 97 } |
| 98 |
| 99 // static |
| 100 void SlideAnimatorGtk::OnChildSizeAllocate(GtkWidget* child, |
| 101 GtkAllocation* allocation, |
| 102 SlideAnimatorGtk* slider) { |
| 103 if (!slider->fixed_needs_resize_) |
| 104 return; |
| 105 |
| 106 slider->fixed_needs_resize_ = false; |
| 107 slider->AnimationProgressed(slider->animation_.get()); |
| 108 } |
OLD | NEW |