panel_view.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 2012 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_PANELS_PANEL_VIEW_H_
6#define CHROME_BROWSER_UI_VIEWS_PANELS_PANEL_VIEW_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "chrome/browser/ui/panels/native_panel.h"
10#include "ui/base/animation/animation_delegate.h"
11#include "ui/views/focus/widget_focus_manager.h"
12#include "ui/views/widget/widget_delegate.h"
13#include "ui/views/widget/widget_observer.h"
14
15#if defined(OS_WIN)
16#include "ui/base/win/hwnd_subclass.h"
17#endif
18
19class Panel;
20class PanelBoundsAnimation;
21class PanelFrameView;
22class TaskbarWindowThumbnailerWin;
23
24namespace views {
25class WebView;
26}
27
28class PanelView : public NativePanel,
29                  public views::WidgetObserver,
30                  public views::WidgetDelegateView,
31                  public views::WidgetFocusChangeListener,
32#if defined(OS_WIN)
33                  public ui::HWNDMessageFilter,
34#endif
35                  public ui::AnimationDelegate {
36 public:
37  // The size of inside area used for mouse resizing.
38  static const int kResizeInsideBoundsSize = 5;
39
40  PanelView(Panel* panel, const gfx::Rect& bounds, bool always_on_top);
41  virtual ~PanelView();
42
43  // Overridden from NativePanel:
44  virtual void ShowPanel() OVERRIDE;
45  virtual void ShowPanelInactive() OVERRIDE;
46  virtual gfx::Rect GetPanelBounds() const OVERRIDE;
47  virtual void SetPanelBounds(const gfx::Rect& bounds) OVERRIDE;
48  virtual void SetPanelBoundsInstantly(const gfx::Rect& bounds) OVERRIDE;
49  virtual void ClosePanel() OVERRIDE;
50  virtual void ActivatePanel() OVERRIDE;
51  virtual void DeactivatePanel() OVERRIDE;
52  virtual bool IsPanelActive() const OVERRIDE;
53  virtual void PreventActivationByOS(bool prevent_activation) OVERRIDE;
54  virtual gfx::NativeWindow GetNativePanelWindow() OVERRIDE;
55  virtual void UpdatePanelTitleBar() OVERRIDE;
56  virtual void UpdatePanelLoadingAnimations(bool should_animate) OVERRIDE;
57  virtual void PanelWebContentsFocused(content::WebContents* contents) OVERRIDE;
58  virtual void PanelCut() OVERRIDE;
59  virtual void PanelCopy() OVERRIDE;
60  virtual void PanelPaste() OVERRIDE;
61  virtual void DrawAttention(bool draw_attention) OVERRIDE;
62  virtual bool IsDrawingAttention() const OVERRIDE;
63  virtual void HandlePanelKeyboardEvent(
64      const content::NativeWebKeyboardEvent& event) OVERRIDE;
65  virtual void FullScreenModeChanged(bool is_full_screen) OVERRIDE;
66  virtual bool IsPanelAlwaysOnTop() const OVERRIDE;
67  virtual void SetPanelAlwaysOnTop(bool on_top) OVERRIDE;
68  virtual void EnableResizeByMouse(bool enable) OVERRIDE;
69  virtual void UpdatePanelMinimizeRestoreButtonVisibility() OVERRIDE;
70  virtual void SetWindowCornerStyle(panel::CornerStyle corner_style) OVERRIDE;
71  virtual void PanelExpansionStateChanging(
72      Panel::ExpansionState old_state,
73      Panel::ExpansionState new_state) OVERRIDE;
74  virtual void AttachWebContents(content::WebContents* contents) OVERRIDE;
75  virtual void DetachWebContents(content::WebContents* contents) OVERRIDE;
76  virtual gfx::Size WindowSizeFromContentSize(
77      const gfx::Size& content_size) const OVERRIDE;
78  virtual gfx::Size ContentSizeFromWindowSize(
79      const gfx::Size& window_size) const OVERRIDE;
80  virtual int TitleOnlyHeight() const OVERRIDE;
81  virtual void MinimizePanelBySystem() OVERRIDE;
82  virtual bool IsPanelMinimizedBySystem() const OVERRIDE;
83  virtual void ShowShadow(bool show) OVERRIDE;
84  virtual NativePanelTesting* CreateNativePanelTesting() OVERRIDE;
85
86  // Overridden from views::View:
87  virtual gfx::Size GetMinimumSize() OVERRIDE;
88  virtual gfx::Size GetMaximumSize() OVERRIDE;
89
90  // Return true if the mouse event is handled.
91  // |mouse_location| is in screen coordinate system.
92  bool OnTitlebarMousePressed(const gfx::Point& mouse_location);
93  bool OnTitlebarMouseDragged(const gfx::Point& mouse_location);
94  bool OnTitlebarMouseReleased(panel::ClickModifier modifier);
95  bool OnTitlebarMouseCaptureLost();
96
97  PanelFrameView* GetFrameView() const;
98  bool IsAnimatingBounds() const;
99
100  // The panel does not show a resizing border. Instead, the inner content area
101  // can be used to trigger the mouse resizing. Return true if |mouse_location|
102  // falls within this area.
103  // |mouse_location| is in screen coordinate system.
104  bool IsWithinResizingArea(const gfx::Point& mouse_location) const;
105
106  Panel* panel() const { return panel_.get(); }
107  views::Widget* window() const { return window_; }
108  bool force_to_paint_as_inactive() const {
109    return force_to_paint_as_inactive_;
110  }
111
112  // PanelStackView might want to update the stored bounds directly since it
113  // has already taken care of updating the window bounds directly.
114  void set_cached_bounds_directly(const gfx::Rect& bounds) { bounds_ = bounds; }
115
116 private:
117  enum MouseDraggingState {
118    NO_DRAGGING,
119    DRAGGING_STARTED,
120    DRAGGING_ENDED
121  };
122
123  // Overridden from views::WidgetDelegate:
124  virtual void OnDisplayChanged() OVERRIDE;
125  virtual void OnWorkAreaChanged() OVERRIDE;
126  virtual bool WillProcessWorkAreaChange() const OVERRIDE;
127  virtual views::View* GetContentsView() OVERRIDE;
128  virtual views::NonClientFrameView* CreateNonClientFrameView(
129      views::Widget* widget) OVERRIDE;
130  virtual bool CanResize() const OVERRIDE;
131  virtual bool CanMaximize() const OVERRIDE;
132  virtual views::Widget* GetWidget() OVERRIDE;
133  virtual const views::Widget* GetWidget() const OVERRIDE;
134  virtual string16 GetWindowTitle() const OVERRIDE;
135  virtual gfx::ImageSkia GetWindowAppIcon() OVERRIDE;
136  virtual gfx::ImageSkia GetWindowIcon() OVERRIDE;
137  virtual void WindowClosing() OVERRIDE;
138  virtual void DeleteDelegate() OVERRIDE;
139  virtual void OnWindowBeginUserBoundsChange() OVERRIDE;
140  virtual void OnWindowEndUserBoundsChange() OVERRIDE;
141
142  // Overridden from views::View:
143  virtual void Layout() OVERRIDE;
144  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
145
146  // Overridden from views::WidgetObserver:
147  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
148  virtual void OnWidgetActivationChanged(views::Widget* widget,
149                                         bool active) OVERRIDE;
150  virtual void OnWidgetBoundsChanged(views::Widget* widget,
151                                     const gfx::Rect& new_bounds) OVERRIDE;
152
153  // Overridden from views::WidgetFocusChangeListener:
154  virtual void OnNativeFocusChange(gfx::NativeView focused_before,
155                                   gfx::NativeView focused_now) OVERRIDE;
156
157  // Overridden from ui::HWNDMessageFilter:
158#if defined(OS_WIN)
159  virtual bool FilterMessage(HWND hwnd,
160                             UINT message,
161                             WPARAM w_param,
162                             LPARAM l_param,
163                             LRESULT* l_result) OVERRIDE;
164#endif
165
166  // Overridden from AnimationDelegate:
167  virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE;
168  virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
169
170  void UpdateLoadingAnimations(bool should_animate);
171  void UpdateWindowTitle();
172  void UpdateWindowIcon();
173  void SetBoundsInternal(const gfx::Rect& bounds, bool animate);
174  bool EndDragging(bool cancelled);
175  void OnViewWasResized();
176
177  // Sets the bounds of the underlying window to |new_bounds|. Note that this
178  // might update the window style to work around the minimum overlapped
179  // window height limitation.
180  void SetWidgetBounds(const gfx::Rect& new_bounds);
181
182#if defined(OS_WIN)
183  // Sets |attribute_value_to_set| and/or clears |attribute_value_to_reset| for
184  // the attibute denoted by |attribute_index|. This is used to update the style
185  // or extended style for the native window.
186  void UpdateWindowAttribute(int attribute_index,
187                             int attribute_value_to_set,
188                             int attribute_value_to_reset,
189                             bool update_frame);
190#endif
191
192  scoped_ptr<Panel> panel_;
193  gfx::Rect bounds_;
194
195  // The window that holds all panel views. Lifetime managed by native widget.
196  // See widget.h.
197  views::Widget* window_;
198
199  // Close gets called more than once, so use this to do one-time clean up once.
200  bool window_closed_;
201
202  // The view hosting the web contents. Will be destroyed when child views
203  // of this class are destroyed.
204  views::WebView* web_view_;
205
206  // True if the panel should always stay on top of other windows.
207  bool always_on_top_;
208
209  // Is the panel receiving the focus?
210  bool focused_;
211
212  // True if the user is resizing the panel.
213  bool user_resizing_;
214
215#if defined(OS_WIN)
216  // True if the user is resizing the interior edge of a stack.
217  bool user_resizing_interior_stacked_panel_edge_;
218
219  // The original full size of the resizing panel before the resizing states.
220  gfx::Size original_full_size_of_resizing_panel_;
221
222  // The original full size of the panel below the resizing panel before the
223  // resizing starts.
224  gfx::Size original_full_size_of_panel_below_resizing_panel_;
225#endif
226
227
228  // Is the mouse button currently down?
229  bool mouse_pressed_;
230
231  // Location the mouse was pressed at or dragged to last time when we process
232  // the mouse event. Used in drag-and-drop.
233  // This point is represented in the screen coordinate system.
234  gfx::Point last_mouse_location_;
235
236  // Is the titlebar currently being dragged?  That is, has the cursor
237  // moved more than kDragThreshold away from its starting position?
238  MouseDraggingState mouse_dragging_state_;
239
240  // Used to animate the bounds change.
241  scoped_ptr<PanelBoundsAnimation> bounds_animator_;
242  gfx::Rect animation_start_bounds_;
243
244  // Is the panel in highlighted state to draw people's attention?
245  bool is_drawing_attention_;
246
247  // Should we force to paint the panel as inactive? This is needed when we need
248  // to capture the screenshot before an active panel goes minimized.
249  bool force_to_paint_as_inactive_;
250
251  // The last view that had focus in the panel. This is saved so that focus can
252  // be restored properly when a drag ends.
253  views::View* old_focused_view_;
254
255#if defined(OS_WIN)
256  // Used to provide custom taskbar thumbnail for Windows 7 and later.
257  scoped_ptr<TaskbarWindowThumbnailerWin> thumbnailer_;
258#endif
259
260  DISALLOW_COPY_AND_ASSIGN(PanelView);
261};
262
263#endif  // CHROME_BROWSER_UI_VIEWS_PANELS_PANEL_VIEW_H_
264