Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / views / controls / image_view.h
blob2b67516beea0e6ab3835be2e158ed4cbd9aef859
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.
5 #ifndef UI_VIEWS_CONTROLS_IMAGE_VIEW_H_
6 #define UI_VIEWS_CONTROLS_IMAGE_VIEW_H_
8 #include "third_party/skia/include/core/SkColor.h"
9 #include "ui/gfx/image/image_skia.h"
10 #include "ui/views/view.h"
12 namespace gfx {
13 class Canvas;
16 namespace views {
18 class Painter;
20 /////////////////////////////////////////////////////////////////////////////
22 // ImageView class.
24 // An ImageView can display an image from an ImageSkia. If a size is provided,
25 // the ImageView will resize the provided image to fit if it is too big or will
26 // center the image if smaller. Otherwise, the preferred size matches the
27 // provided image size.
29 /////////////////////////////////////////////////////////////////////////////
30 class VIEWS_EXPORT ImageView : public View {
31 public:
32 // Internal class name.
33 static const char kViewClassName[];
35 enum Alignment {
36 LEADING = 0,
37 CENTER,
38 TRAILING
41 ImageView();
42 ~ImageView() override;
44 // Set the image that should be displayed.
45 void SetImage(const gfx::ImageSkia& img);
47 // Set the image that should be displayed from a pointer. Reset the image
48 // if the pointer is NULL. The pointer contents is copied in the receiver's
49 // image.
50 void SetImage(const gfx::ImageSkia* image_skia);
52 // Returns the image currently displayed or NULL of none is currently set.
53 // The returned image is still owned by the ImageView.
54 const gfx::ImageSkia& GetImage();
56 // Set the desired image size for the receiving ImageView.
57 void SetImageSize(const gfx::Size& image_size);
59 // Returns the actual bounds of the visible image inside the view.
60 gfx::Rect GetImageBounds() const;
62 // Reset the image size to the current image dimensions.
63 void ResetImageSize();
65 // Set / Get the horizontal alignment.
66 void SetHorizontalAlignment(Alignment ha);
67 Alignment GetHorizontalAlignment() const;
69 // Set / Get the vertical alignment.
70 void SetVerticalAlignment(Alignment va);
71 Alignment GetVerticalAlignment() const;
73 // Set / Get the tooltip text.
74 void SetTooltipText(const base::string16& tooltip);
75 base::string16 GetTooltipText() const;
77 void set_interactive(bool interactive) { interactive_ = interactive; }
79 void SetFocusPainter(scoped_ptr<Painter> focus_painter);
81 // Overriden from View:
82 gfx::Size GetPreferredSize() const override;
83 void OnFocus() override;
84 void OnBlur() override;
85 void OnPaint(gfx::Canvas* canvas) override;
86 void GetAccessibleState(ui::AXViewState* state) override;
87 const char* GetClassName() const override;
88 bool GetTooltipText(const gfx::Point& p,
89 base::string16* tooltip) const override;
90 bool CanProcessEventsWithinSubtree() const override;
92 private:
93 void OnPaintImage(gfx::Canvas* canvas);
95 // Returns true if |img| is the same as the last image we painted. This is
96 // intended to be a quick check, not exhaustive. In other words it's possible
97 // for this to return false even though the images are in fact equal.
98 bool IsImageEqual(const gfx::ImageSkia& img) const;
100 // Returns the size the image will be painted.
101 gfx::Size GetImageSize() const;
103 // Compute the image origin given the desired size and the receiver alignment
104 // properties.
105 gfx::Point ComputeImageOrigin(const gfx::Size& image_size) const;
107 // Whether the image size is set.
108 bool image_size_set_;
110 // The actual image size.
111 gfx::Size image_size_;
113 // The underlying image.
114 gfx::ImageSkia image_;
116 // Horizontal alignment.
117 Alignment horiz_alignment_;
119 // Vertical alignment.
120 Alignment vert_alignment_;
122 // The current tooltip text.
123 base::string16 tooltip_text_;
125 // A flag controlling hit test handling for interactivity.
126 bool interactive_;
128 // Scale last painted at.
129 float last_paint_scale_;
131 // Address of bytes we last painted. This is used only for comparison, so its
132 // safe to cache.
133 void* last_painted_bitmap_pixels_;
135 scoped_ptr<views::Painter> focus_painter_;
137 DISALLOW_COPY_AND_ASSIGN(ImageView);
140 } // namespace views
142 #endif // UI_VIEWS_CONTROLS_IMAGE_VIEW_H_