blob: ba48e4620133a46d24bb418484f10a9b3ced7d01 [file] [log] [blame]
[email protected]08f75ed2009-04-21 17:12:531// 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]220705c2009-05-05 04:52:117#include "app/animation.h"
8#include "app/slide_animation.h"
[email protected]08f75ed2009-04-21 17:12:539#include "base/logging.h"
[email protected]08f75ed2009-04-21 17:12:5310
[email protected]623630c2009-12-21 05:59:0811#include "chrome/browser/gtk/gtk_expanded_container.h"
12
[email protected]08f75ed2009-04-21 17:12:5313namespace {
14
[email protected]623630c2009-12-21 05:59:0815void 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]08f75ed2009-04-21 17:12:5325}
26
27} // namespace
28
29SlideAnimatorGtk::SlideAnimatorGtk(GtkWidget* child,
30 Direction direction,
[email protected]1aab6b892009-04-22 18:54:1131 int duration,
32 bool linear,
[email protected]1ba48dd2009-08-21 19:57:0033 bool control_child_size,
[email protected]08f75ed2009-04-21 17:12:5334 Delegate* delegate)
35 : child_(child),
36 direction_(direction),
[email protected]1c5fe5c32009-10-03 03:51:3237 delegate_(delegate) {
[email protected]623630c2009-12-21 05:59:0838 widget_.Own(gtk_expanded_container_new());
39 gtk_container_add(GTK_CONTAINER(widget_.get()), child);
[email protected]08f75ed2009-04-21 17:12:5340 gtk_widget_set_size_request(widget_.get(), -1, 0);
[email protected]623630c2009-12-21 05:59:0841
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]91278f542009-04-29 21:33:5248
[email protected]d2192e32009-10-07 01:57:3749 // 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]91278f542009-04-29 21:33:5253 g_signal_connect(child, "size-allocate",
54 G_CALLBACK(OnChildSizeAllocate), this);
[email protected]08f75ed2009-04-21 17:12:5355
[email protected]36c165e2009-05-01 22:44:5656 child_needs_move_ = (direction == DOWN);
57
[email protected]08f75ed2009-04-21 17:12:5358 animation_.reset(new SlideAnimation(this));
[email protected]1aab6b892009-04-22 18:54:1159 // Default tween type is EASE_OUT.
60 if (linear)
61 animation_->SetTweenType(SlideAnimation::NONE);
62 if (duration != 0)
63 animation_->SetSlideDuration(duration);
[email protected]08f75ed2009-04-21 17:12:5364}
65
66SlideAnimatorGtk::~SlideAnimatorGtk() {
67 widget_.Destroy();
68}
69
70void SlideAnimatorGtk::Open() {
[email protected]a0a9577b2009-05-27 23:52:3271 gtk_widget_show(widget_.get());
[email protected]08f75ed2009-04-21 17:12:5372 animation_->Show();
73}
74
75void SlideAnimatorGtk::OpenWithoutAnimation() {
76 animation_->Reset(1.0);
77 Open();
[email protected]1c5fe5c32009-10-03 03:51:3278 AnimationProgressed(animation_.get());
[email protected]08f75ed2009-04-21 17:12:5379}
80
81void SlideAnimatorGtk::Close() {
82 animation_->Hide();
83}
84
[email protected]268bdbc2009-07-31 19:46:5385void SlideAnimatorGtk::End() {
86 animation_->End();
87}
88
[email protected]9495e202009-05-04 17:59:5589void SlideAnimatorGtk::CloseWithoutAnimation() {
90 animation_->Reset(0.0);
91 animation_->Hide();
92 AnimationProgressed(animation_.get());
[email protected]1d9489e2009-05-12 22:53:3493 gtk_widget_hide(widget_.get());
[email protected]9495e202009-05-04 17:59:5594}
95
[email protected]08f75ed2009-04-21 17:12:5396bool SlideAnimatorGtk::IsShowing() {
97 return animation_->IsShowing();
98}
99
[email protected]0a2aeb82009-05-15 21:52:48100bool SlideAnimatorGtk::IsClosing() {
[email protected]3a1c05a2009-06-01 18:46:37101 return animation_->IsClosing();
[email protected]0a2aeb82009-05-15 21:52:48102}
103
[email protected]62391372009-08-31 18:31:05104bool SlideAnimatorGtk::IsAnimating() {
105 return animation_->IsAnimating();
106}
107
[email protected]08f75ed2009-04-21 17:12:53108void SlideAnimatorGtk::AnimationProgressed(const Animation* animation) {
[email protected]1c5fe5c32009-10-03 03:51:32109 GtkRequisition req;
110 gtk_widget_size_request(child_, &req);
111
112 int showing_height = static_cast<int>(req.height *
[email protected]655750cb2009-09-14 17:46:15113 animation_->GetCurrentValue());
[email protected]08f75ed2009-04-21 17:12:53114 if (direction_ == DOWN) {
[email protected]623630c2009-12-21 05:59:08115 gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(widget_.get()),
116 child_, 0, showing_height - req.height);
[email protected]d2192e32009-10-07 01:57:37117 child_needs_move_ = false;
[email protected]08f75ed2009-04-21 17:12:53118 }
119 gtk_widget_set_size_request(widget_.get(), -1, showing_height);
120}
121
122void SlideAnimatorGtk::AnimationEnded(const Animation* animation) {
[email protected]1d9489e2009-05-12 22:53:34123 if (!animation_->IsShowing()) {
124 gtk_widget_hide(widget_.get());
125 if (delegate_)
126 delegate_->Closed();
127 }
[email protected]08f75ed2009-04-21 17:12:53128}
[email protected]91278f542009-04-29 21:33:52129
130// static
131void SlideAnimatorGtk::OnChildSizeAllocate(GtkWidget* child,
132 GtkAllocation* allocation,
133 SlideAnimatorGtk* slider) {
[email protected]36c165e2009-05-01 22:44:56134 if (slider->child_needs_move_) {
[email protected]623630c2009-12-21 05:59:08135 gtk_expanded_container_move(GTK_EXPANDED_CONTAINER(slider->widget()),
136 child, 0, -allocation->height);
[email protected]36c165e2009-05-01 22:44:56137 slider->child_needs_move_ = false;
138 }
[email protected]91278f542009-04-29 21:33:52139}