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 UI_VIEWS_CONTROLS_IMAGE_VIEW_H_ 6 #define UI_VIEWS_CONTROLS_IMAGE_VIEW_H_ 7 8 #include "ui/gfx/image/image_skia.h" 9 #include "ui/views/view.h" 10 11 namespace gfx { 12 class Canvas; 13 } 14 15 namespace views { 16 17 class Painter; 18 19 ///////////////////////////////////////////////////////////////////////////// 20 // 21 // ImageView class. 22 // 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. 27 // 28 ///////////////////////////////////////////////////////////////////////////// 29 class VIEWS_EXPORT ImageView : public View { 30 public: 31 enum Alignment { 32 LEADING = 0, 33 CENTER, 34 TRAILING 35 }; 36 37 ImageView(); 38 virtual ~ImageView(); 39 40 // Set the image that should be displayed. 41 void SetImage(const gfx::ImageSkia& img); 42 43 // Set the image that should be displayed from a pointer. Reset the image 44 // if the pointer is NULL. The pointer contents is copied in the receiver's 45 // image. 46 void SetImage(const gfx::ImageSkia* image_skia); 47 48 // Returns the image currently displayed or NULL of none is currently set. 49 // The returned image is still owned by the ImageView. 50 const gfx::ImageSkia& GetImage(); 51 52 // Set the desired image size for the receiving ImageView. 53 void SetImageSize(const gfx::Size& image_size); 54 55 // Return the preferred size for the receiving view. Returns false if the 56 // preferred size is not defined, which means that the view uses the image 57 // size. 58 bool GetImageSize(gfx::Size* image_size) const; 59 60 // Returns the actual bounds of the visible image inside the view. 61 gfx::Rect GetImageBounds() const; 62 63 // Reset the image size to the current image dimensions. 64 void ResetImageSize(); 65 66 // Set / Get the horizontal alignment. 67 void SetHorizontalAlignment(Alignment ha); 68 Alignment GetHorizontalAlignment() const; 69 70 // Set / Get the vertical alignment. 71 void SetVerticalAlignment(Alignment va); 72 Alignment GetVerticalAlignment() const; 73 74 // Set / Get the tooltip text. 75 void SetTooltipText(const base::string16& tooltip); 76 base::string16 GetTooltipText() const; 77 set_interactive(bool interactive)78 void set_interactive(bool interactive) { interactive_ = interactive; } 79 80 void SetFocusPainter(scoped_ptr<Painter> focus_painter); 81 82 // Overriden from View: 83 virtual gfx::Size GetPreferredSize() const OVERRIDE; 84 virtual void OnFocus() OVERRIDE; 85 virtual void OnBlur() OVERRIDE; 86 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 87 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; 88 virtual bool GetTooltipText(const gfx::Point& p, 89 base::string16* tooltip) const OVERRIDE; 90 virtual bool CanProcessEventsWithinSubtree() const OVERRIDE; 91 92 private: 93 void OnPaintImage(gfx::Canvas* canvas); 94 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; 99 100 // Compute the image origin given the desired size and the receiver alignment 101 // properties. 102 gfx::Point ComputeImageOrigin(const gfx::Size& image_size) const; 103 104 // Whether the image size is set. 105 bool image_size_set_; 106 107 // The actual image size. 108 gfx::Size image_size_; 109 110 // The underlying image. 111 gfx::ImageSkia image_; 112 113 // Horizontal alignment. 114 Alignment horiz_alignment_; 115 116 // Vertical alignment. 117 Alignment vert_alignment_; 118 119 // The current tooltip text. 120 base::string16 tooltip_text_; 121 122 // A flag controlling hit test handling for interactivity. 123 bool interactive_; 124 125 // Scale last painted at. 126 float last_paint_scale_; 127 128 // Address of bytes we last painted. This is used only for comparison, so its 129 // safe to cache. 130 void* last_painted_bitmap_pixels_; 131 132 scoped_ptr<views::Painter> focus_painter_; 133 134 DISALLOW_COPY_AND_ASSIGN(ImageView); 135 }; 136 137 } // namespace views 138 139 #endif // UI_VIEWS_CONTROLS_IMAGE_VIEW_H_ 140