[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/gtk/slide_animator_gtk.h" |
| 6 | |
[email protected] | 220705c | 2009-05-05 04:52:11 | [diff] [blame] | 7 | #include "app/animation.h" |
| 8 | #include "app/slide_animation.h" |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 10 | |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 11 | #include "chrome/browser/gtk/gtk_expanded_container.h" |
| 12 | |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 13 | namespace { |
| 14 | |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 15 | void OnChildSizeRequest(GtkWidget* expanded, |
| 16 | GtkWidget* child, |
| 17 | GtkRequisition* requisition, |
| 18 | gpointer control_child_size) { |
| 19 | // If |control_child_size| is true, then we want |child_| to match the width |
| 20 | // of the |widget_|, but the height of |child_| should not change. |
| 21 | if (!GPOINTER_TO_INT(control_child_size)) { |
| 22 | requisition->width = -1; |
| 23 | } |
| 24 | requisition->height = -1; |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child, |
| 30 | Direction direction, |
[email protected] | 1aab6b89 | 2009-04-22 18:54:11 | [diff] [blame] | 31 | int duration, |
| 32 | bool linear, |
[email protected] | 1ba48dd | 2009-08-21 19:57:00 | [diff] [blame] | 33 | bool control_child_size, |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 34 | Delegate* delegate) |
| 35 | : child_(child), |
| 36 | direction_(direction), |
[email protected] | 1c5fe5c3 | 2009-10-03 03:51:32 | [diff] [blame] | 37 | delegate_(delegate) { |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 38 | widget_.Own(gtk_expanded_container_new()); |
| 39 | gtk_container_add(GTK_CONTAINER(widget_.get()), child); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 40 | gtk_widget_set_size_request(widget_.get(), -1, 0); |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 41 | |
| 42 | // If the child requests it, we will manually set the size request for |
| 43 | // |child_| every time the |widget_| changes sizes. This is mainly useful |
| 44 | // for bars, where we want the child to expand to fill all available space. |
| 45 | g_signal_connect(widget_.get(), "child-size-request", |
| 46 | G_CALLBACK(OnChildSizeRequest), |
| 47 | GINT_TO_POINTER(control_child_size)); |
[email protected] | 91278f54 | 2009-04-29 21:33:52 | [diff] [blame] | 48 | |
[email protected] | d2192e3 | 2009-10-07 01:57:37 | [diff] [blame] | 49 | // We connect to this signal to set an initial position for our child widget. |
| 50 | // The reason we connect to this signal rather than setting the initial |
| 51 | // position here is that the widget is currently unallocated and may not |
| 52 | // even have a size request. |
[email protected] | 91278f54 | 2009-04-29 21:33:52 | [diff] [blame] | 53 | g_signal_connect(child, "size-allocate", |
| 54 | G_CALLBACK(OnChildSizeAllocate), this); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 55 | |
[email protected] | 36c165e | 2009-05-01 22:44:56 | [diff] [blame] | 56 | child_needs_move_ = (direction == DOWN); |
| 57 | |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 58 | animation_.reset(new SlideAnimation(this)); |
[email protected] | 1aab6b89 | 2009-04-22 18:54:11 | [diff] [blame] | 59 | // Default tween type is EASE_OUT. |
| 60 | if (linear) |
| 61 | animation_->SetTweenType(SlideAnimation::NONE); |
| 62 | if (duration != 0) |
| 63 | animation_->SetSlideDuration(duration); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | SlideAnimatorGtk::~SlideAnimatorGtk() { |
| 67 | widget_.Destroy(); |
| 68 | } |
| 69 | |
| 70 | void SlideAnimatorGtk::Open() { |
[email protected] | a0a9577b | 2009-05-27 23:52:32 | [diff] [blame] | 71 | gtk_widget_show(widget_.get()); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 72 | animation_->Show(); |
| 73 | } |
| 74 | |
| 75 | void SlideAnimatorGtk::OpenWithoutAnimation() { |
| 76 | animation_->Reset(1.0); |
| 77 | Open(); |
[email protected] | 1c5fe5c3 | 2009-10-03 03:51:32 | [diff] [blame] | 78 | AnimationProgressed(animation_.get()); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void SlideAnimatorGtk::Close() { |
| 82 | animation_->Hide(); |
| 83 | } |
| 84 | |
[email protected] | 268bdbc | 2009-07-31 19:46:53 | [diff] [blame] | 85 | void SlideAnimatorGtk::End() { |
| 86 | animation_->End(); |
| 87 | } |
| 88 | |
[email protected] | 9495e20 | 2009-05-04 17:59:55 | [diff] [blame] | 89 | void SlideAnimatorGtk::CloseWithoutAnimation() { |
| 90 | animation_->Reset(0.0); |
| 91 | animation_->Hide(); |
| 92 | AnimationProgressed(animation_.get()); |
[email protected] | 1d9489e | 2009-05-12 22:53:34 | [diff] [blame] | 93 | gtk_widget_hide(widget_.get()); |
[email protected] | 9495e20 | 2009-05-04 17:59:55 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 96 | bool SlideAnimatorGtk::IsShowing() { |
| 97 | return animation_->IsShowing(); |
| 98 | } |
| 99 | |
[email protected] | 0a2aeb8 | 2009-05-15 21:52:48 | [diff] [blame] | 100 | bool SlideAnimatorGtk::IsClosing() { |
[email protected] | 3a1c05a | 2009-06-01 18:46:37 | [diff] [blame] | 101 | return animation_->IsClosing(); |
[email protected] | 0a2aeb8 | 2009-05-15 21:52:48 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 6239137 | 2009-08-31 18:31:05 | [diff] [blame] | 104 | bool SlideAnimatorGtk::IsAnimating() { |
| 105 | return animation_->IsAnimating(); |
| 106 | } |
| 107 | |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 108 | void SlideAnimatorGtk::AnimationProgressed(const Animation* animation) { |
[email protected] | 1c5fe5c3 | 2009-10-03 03:51:32 | [diff] [blame] | 109 | GtkRequisition req; |
| 110 | gtk_widget_size_request(child_, &req); |
| 111 | |
| 112 | int showing_height = static_cast<int>(req.height * |
[email protected] | 655750cb | 2009-09-14 17:46:15 | [diff] [blame] | 113 | animation_->GetCurrentValue()); |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 114 | if (direction_ == DOWN) { |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 115 | gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(widget_.get()), |
| 116 | child_, 0, showing_height - req.height); |
[email protected] | d2192e3 | 2009-10-07 01:57:37 | [diff] [blame] | 117 | child_needs_move_ = false; |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 118 | } |
| 119 | gtk_widget_set_size_request(widget_.get(), -1, showing_height); |
| 120 | } |
| 121 | |
| 122 | void SlideAnimatorGtk::AnimationEnded(const Animation* animation) { |
[email protected] | 1d9489e | 2009-05-12 22:53:34 | [diff] [blame] | 123 | if (!animation_->IsShowing()) { |
| 124 | gtk_widget_hide(widget_.get()); |
| 125 | if (delegate_) |
| 126 | delegate_->Closed(); |
| 127 | } |
[email protected] | 08f75ed | 2009-04-21 17:12:53 | [diff] [blame] | 128 | } |
[email protected] | 91278f54 | 2009-04-29 21:33:52 | [diff] [blame] | 129 | |
| 130 | // static |
| 131 | void SlideAnimatorGtk::OnChildSizeAllocate(GtkWidget* child, |
| 132 | GtkAllocation* allocation, |
| 133 | SlideAnimatorGtk* slider) { |
[email protected] | 36c165e | 2009-05-01 22:44:56 | [diff] [blame] | 134 | if (slider->child_needs_move_) { |
[email protected] | 623630c | 2009-12-21 05:59:08 | [diff] [blame] | 135 | gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(slider->widget()), |
| 136 | child, 0, -allocation->height); |
[email protected] | 36c165e | 2009-05-01 22:44:56 | [diff] [blame] | 137 | slider->child_needs_move_ = false; |
| 138 | } |
[email protected] | 91278f54 | 2009-04-29 21:33:52 | [diff] [blame] | 139 | } |