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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_TABS_BASE_TAB_H_
6#define CHROME_BROWSER_UI_VIEWS_TABS_BASE_TAB_H_
7#pragma once
8
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "chrome/browser/ui/views/tabs/tab_renderer_data.h"
12#include "ui/base/animation/animation_delegate.h"
13#include "views/controls/button/button.h"
14#include "views/view.h"
15
16class BaseTab;
17class TabController;
18
19namespace gfx {
20class Font;
21}
22
23namespace ui {
24class AnimationContainer;
25class SlideAnimation;
26class ThrobAnimation;
27}
28
29namespace views {
30class ImageButton;
31}
32
33// Base class for tab renderers.
34class BaseTab : public ui::AnimationDelegate,
35                public views::ButtonListener,
36                public views::ContextMenuController,
37                public views::View {
38 public:
39  explicit BaseTab(TabController* controller);
40  virtual ~BaseTab();
41
42  // Sets the data this tabs displays. Invokes DataChanged for subclasses to
43  // update themselves appropriately.
44  void SetData(const TabRendererData& data);
45  const TabRendererData& data() const { return data_; }
46
47  // Sets the network state. If the network state changes NetworkStateChanged is
48  // invoked.
49  virtual void UpdateLoadingAnimation(TabRendererData::NetworkState state);
50
51  // Starts/Stops a pulse animation.
52  void StartPulse();
53  void StopPulse();
54
55  // Used to set/check whether this Tab is being animated closed.
56  void set_closing(bool closing) { closing_ = closing; }
57  bool closing() const { return closing_; }
58
59  // See description above field.
60  void set_dragging(bool dragging) { dragging_ = dragging; }
61  bool dragging() const { return dragging_; }
62
63  // Sets the container all animations run from.
64  void set_animation_container(ui::AnimationContainer* container);
65  ui::AnimationContainer* animation_container() const {
66    return animation_container_.get();
67  }
68
69  // Set the theme provider - because we get detached, we are frequently
70  // outside of a hierarchy with a theme provider at the top. This should be
71  // called whenever we're detached or attached to a hierarchy.
72  void set_theme_provider(ui::ThemeProvider* provider) {
73    theme_provider_ = provider;
74  }
75
76  // Returns true if the tab is closeable.
77  bool IsCloseable() const;
78
79  // Returns true if this tab is the active tab.
80  bool IsActive() const;
81
82  // Returns true if the tab is selected.
83  virtual bool IsSelected() const;
84
85  // Overridden from views::View:
86  virtual ThemeProvider* GetThemeProvider() const OVERRIDE;
87  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
88  virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
89  virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
90  virtual void OnMouseCaptureLost() OVERRIDE;
91  virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE;
92  virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE;
93  virtual bool GetTooltipText(const gfx::Point& p,
94                              std::wstring* tooltip) OVERRIDE;
95  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
96
97 protected:
98  // Invoked from SetData after |data_| has been updated to the new data.
99  virtual void DataChanged(const TabRendererData& old) {}
100
101  // Invoked if data_.network_state changes, or the network_state is not none.
102  virtual void AdvanceLoadingAnimation(TabRendererData::NetworkState old_state,
103                                       TabRendererData::NetworkState state);
104
105  TabController* controller() const { return controller_; }
106
107  // Returns the pulse animation. The pulse animation is non-null if StartPulse
108  // has been invoked.
109  ui::ThrobAnimation* pulse_animation() const { return pulse_animation_.get(); }
110
111  // Returns the hover animation. This may return null.
112  const ui::SlideAnimation* hover_animation() const {
113    return hover_animation_.get();
114  }
115
116  views::ImageButton* close_button() const { return close_button_; }
117
118  // Paints the icon at the specified coordinates, mirrored for RTL if needed.
119  void PaintIcon(gfx::Canvas* canvas);
120  void PaintTitle(gfx::Canvas* canvas, SkColor title_color);
121
122  // Overridden from AnimationDelegate:
123  virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
124  virtual void AnimationCanceled(const ui::Animation* animation) OVERRIDE;
125  virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE;
126
127  // Overridden from views::ButtonListener:
128  virtual void ButtonPressed(views::Button* sender,
129                             const views::Event& event) OVERRIDE;
130
131  // Overridden from views::ContextMenuController:
132  virtual void ShowContextMenuForView(views::View* source,
133                                      const gfx::Point& p,
134                                      bool is_mouse_gesture) OVERRIDE;
135
136  // Returns the bounds of the title and icon.
137  virtual const gfx::Rect& GetTitleBounds() const = 0;
138  virtual const gfx::Rect& GetIconBounds() const = 0;
139
140  virtual int loading_animation_frame() const;
141  virtual bool should_display_crashed_favicon() const;
142  virtual int favicon_hiding_offset() const;
143
144  static gfx::Font* font() { return font_; }
145  static int font_height() { return font_height_; }
146
147 private:
148  // The animation object used to swap the favicon with the sad tab icon.
149  class FaviconCrashAnimation;
150
151  // Set the temporary offset for the favicon. This is used during the crash
152  // animation.
153  void SetFaviconHidingOffset(int offset);
154
155  void DisplayCrashedFavicon();
156  void ResetCrashedFavicon();
157
158  // Starts/Stops the crash animation.
159  void StartCrashAnimation();
160  void StopCrashAnimation();
161
162  // Returns true if the crash animation is currently running.
163  bool IsPerformingCrashAnimation() const;
164
165  // Schedules repaint task for icon.
166  void ScheduleIconPaint();
167
168  static void InitResources();
169
170  // The controller.
171  // WARNING: this is null during detached tab dragging.
172  TabController* controller_;
173
174  TabRendererData data_;
175
176  // True if the tab is being animated closed.
177  bool closing_;
178
179  // True if the tab is being dragged.
180  bool dragging_;
181
182  // The offset used to animate the favicon location. This is used when the tab
183  // crashes.
184  int favicon_hiding_offset_;
185
186  // The current index of the loading animation.
187  int loading_animation_frame_;
188
189  bool should_display_crashed_favicon_;
190
191  // Pulse animation.
192  scoped_ptr<ui::ThrobAnimation> pulse_animation_;
193
194  // Hover animation.
195  scoped_ptr<ui::SlideAnimation> hover_animation_;
196
197  // Crash animation.
198  scoped_ptr<FaviconCrashAnimation> crash_animation_;
199
200  scoped_refptr<ui::AnimationContainer> animation_container_;
201
202  views::ImageButton* close_button_;
203
204  // Whether to disable throbber animations. Only true if this is an app tab
205  // renderer and a command line flag has been passed in to disable the
206  // animations.
207  bool throbber_disabled_;
208
209  ui::ThemeProvider* theme_provider_;
210
211  static gfx::Font* font_;
212  static int font_height_;
213
214  DISALLOW_COPY_AND_ASSIGN(BaseTab);
215};
216
217#endif  // CHROME_BROWSER_UI_VIEWS_TABS_BASE_TAB_H_
218