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 "ui/gfx/image/image_skia.h"
9 #include "ui/views/view.h"
19 /////////////////////////////////////////////////////////////////////////////
23 // An ImageView can display an image from an ImageSkia. If a size is provided,
24 // the ImageView will resize the provided image to fit if it is too big or will
25 // center the image if smaller. Otherwise, the preferred size matches the
26 // provided image size.
28 /////////////////////////////////////////////////////////////////////////////
29 class VIEWS_EXPORT ImageView
: public View
{
31 // Internal class name.
32 static const char kViewClassName
[];
41 ~ImageView() override
;
43 // Set the image that should be displayed.
44 void SetImage(const gfx::ImageSkia
& img
);
46 // Set the image that should be displayed from a pointer. Reset the image
47 // if the pointer is NULL. The pointer contents is copied in the receiver's
49 void SetImage(const gfx::ImageSkia
* image_skia
);
51 // Returns the image currently displayed or NULL of none is currently set.
52 // The returned image is still owned by the ImageView.
53 const gfx::ImageSkia
& GetImage();
55 // Set the desired image size for the receiving ImageView.
56 void SetImageSize(const gfx::Size
& image_size
);
58 // Return the preferred size for the receiving view. Returns false if the
59 // preferred size is not defined, which means that the view uses the image
61 bool GetImageSize(gfx::Size
* image_size
) const;
63 // Returns the actual bounds of the visible image inside the view.
64 gfx::Rect
GetImageBounds() const;
66 // Reset the image size to the current image dimensions.
67 void ResetImageSize();
69 // Set / Get the horizontal alignment.
70 void SetHorizontalAlignment(Alignment ha
);
71 Alignment
GetHorizontalAlignment() const;
73 // Set / Get the vertical alignment.
74 void SetVerticalAlignment(Alignment va
);
75 Alignment
GetVerticalAlignment() const;
77 // Set / Get the tooltip text.
78 void SetTooltipText(const base::string16
& tooltip
);
79 base::string16
GetTooltipText() const;
81 void set_interactive(bool interactive
) { interactive_
= interactive
; }
83 void SetFocusPainter(scoped_ptr
<Painter
> focus_painter
);
85 // Overriden from View:
86 gfx::Size
GetPreferredSize() const override
;
87 void OnFocus() override
;
88 void OnBlur() override
;
89 void OnPaint(gfx::Canvas
* canvas
) override
;
90 void GetAccessibleState(ui::AXViewState
* state
) override
;
91 const char* GetClassName() const override
;
92 bool GetTooltipText(const gfx::Point
& p
,
93 base::string16
* tooltip
) const override
;
94 bool CanProcessEventsWithinSubtree() const override
;
97 void OnPaintImage(gfx::Canvas
* canvas
);
99 // Returns true if |img| is the same as the last image we painted. This is
100 // intended to be a quick check, not exhaustive. In other words it's possible
101 // for this to return false even though the images are in fact equal.
102 bool IsImageEqual(const gfx::ImageSkia
& img
) const;
104 // Compute the image origin given the desired size and the receiver alignment
106 gfx::Point
ComputeImageOrigin(const gfx::Size
& image_size
) const;
108 // Whether the image size is set.
109 bool image_size_set_
;
111 // The actual image size.
112 gfx::Size image_size_
;
114 // The underlying image.
115 gfx::ImageSkia image_
;
117 // Horizontal alignment.
118 Alignment horiz_alignment_
;
120 // Vertical alignment.
121 Alignment vert_alignment_
;
123 // The current tooltip text.
124 base::string16 tooltip_text_
;
126 // A flag controlling hit test handling for interactivity.
129 // Scale last painted at.
130 float last_paint_scale_
;
132 // Address of bytes we last painted. This is used only for comparison, so its
134 void* last_painted_bitmap_pixels_
;
136 scoped_ptr
<views::Painter
> focus_painter_
;
138 DISALLOW_COPY_AND_ASSIGN(ImageView
);
143 #endif // UI_VIEWS_CONTROLS_IMAGE_VIEW_H_