tooltip_aura.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1// Copyright 2013 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 UI_VIEWS_COREWM_TOOLTIP_AURA_H_
6#define UI_VIEWS_COREWM_TOOLTIP_AURA_H_
7
8#include "ui/gfx/screen_type_delegate.h"
9#include "ui/views/controls/label.h"
10#include "ui/views/corewm/tooltip.h"
11#include "ui/views/widget/widget_observer.h"
12
13namespace views {
14
15class Widget;
16
17namespace corewm {
18
19// Implementation of Tooltip that shows the tooltip using a Widget and Label.
20class VIEWS_EXPORT TooltipAura : public Tooltip, public WidgetObserver {
21 public:
22  explicit TooltipAura(gfx::ScreenType screen_type);
23  virtual ~TooltipAura();
24
25  // Trims the tooltip to fit in the width |max_width|, setting |text| to the
26  // clipped result, |width| to the width (in pixels) of the clipped text
27  // and |line_count| to the number of lines of text in the tooltip. |x| and |y|
28  // give the location of the tooltip in screen coordinates. |max_width| comes
29  // from GetMaxWidth().
30  static void TrimTooltipToFit(int max_width,
31                               string16* text,
32                               int* width,
33                               int* line_count);
34
35 private:
36  // Returns the max width of the tooltip when shown at the specified location.
37  int GetMaxWidth(const gfx::Point& location) const;
38
39  // Returns the bounds to fit the tooltip in.
40  gfx::Rect GetBoundsForTooltip(const gfx::Point& origin) const;
41
42  // Adjusts the bounds given by the arguments to fit inside the desktop
43  // and applies the adjusted bounds to the label_.
44  void SetTooltipBounds(const gfx::Point& mouse_pos,
45                        int tooltip_width,
46                        int tooltip_height);
47
48  // Makes sure |widget_| is valid, creating as necessary.
49  void CreateWidget();
50
51  // Destroys |widget_|.
52  void DestroyWidget();
53
54  // Tooltip:
55  virtual void SetText(aura::Window* window,
56                       const string16& tooltip_text,
57                       const gfx::Point& location) OVERRIDE;
58  virtual void Show() OVERRIDE;
59  virtual void Hide() OVERRIDE;
60  virtual bool IsVisible() OVERRIDE;
61
62  // WidgetObserver:
63  virtual void OnWidgetDestroying(Widget* widget) OVERRIDE;
64
65  const gfx::ScreenType screen_type_;
66
67  // The label showing the tooltip.
68  Label label_;
69
70  // The widget containing the tooltip. May be NULL.
71  Widget* widget_;
72
73  // The window we're showing the tooltip for. Never NULL and valid while
74  // showing.
75  aura::Window* tooltip_window_;
76
77  DISALLOW_COPY_AND_ASSIGN(TooltipAura);
78};
79
80}  // namespace corewm
81}  // namespace views
82
83#endif  // UI_VIEWS_COREWM_TOOLTIP_AURA_H_
84