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 "base/macros.h" 9 #include "ui/gfx/image/image_skia.h" 10 #include "ui/views/view.h" 11 12 namespace gfx { 13 class Canvas; 14 } 15 16 namespace views { 17 18 ///////////////////////////////////////////////////////////////////////////// 19 // 20 // ImageView class. 21 // 22 // An ImageView can display an image from an ImageSkia. If a size is provided, 23 // the ImageView will resize the provided image to fit if it is too big or will 24 // center the image if smaller. Otherwise, the preferred size matches the 25 // provided image size. 26 // 27 ///////////////////////////////////////////////////////////////////////////// 28 class VIEWS_EXPORT ImageView : public View { 29 public: 30 METADATA_HEADER(ImageView); 31 32 enum class Alignment { kLeading, kCenter, kTrailing }; 33 34 ImageView(); 35 ~ImageView() override; 36 37 // Set the image that should be displayed. 38 void SetImage(const gfx::ImageSkia& img); 39 40 // Set the image that should be displayed from a pointer. Reset the image 41 // if the pointer is NULL. The pointer contents is copied in the receiver's 42 // image. 43 void SetImage(const gfx::ImageSkia* image_skia); 44 45 // Sets the desired size of the image to be displayed. 46 void SetImageSize(const gfx::Size& size); 47 48 // Reset the image size to the current image dimensions. 49 void ResetImageSize(); 50 51 // Returns the actual bounds of the visible image inside the view. 52 gfx::Rect GetImageBounds() const; 53 54 // Returns the image currently displayed, which can be empty if not set. 55 // The returned image is still owned by the ImageView. 56 const gfx::ImageSkia& GetImage() const; 57 58 // Set / Get the horizontal alignment. 59 void SetHorizontalAlignment(Alignment ha); 60 Alignment GetHorizontalAlignment() const; 61 62 // Set / Get the vertical alignment. 63 void SetVerticalAlignment(Alignment va); 64 Alignment GetVerticalAlignment() const; 65 66 // Set / Get the accessible name text. 67 void SetAccessibleName(const base::string16& name); 68 const base::string16& GetAccessibleName() const; 69 70 // Set the tooltip text. set_tooltip_text(const base::string16 & tooltip)71 void set_tooltip_text(const base::string16& tooltip) { 72 tooltip_text_ = tooltip; 73 } 74 75 // Overridden from View: 76 void OnPaint(gfx::Canvas* canvas) override; 77 void GetAccessibleNodeData(ui::AXNodeData* node_data) override; 78 base::string16 GetTooltipText(const gfx::Point& p) const override; 79 gfx::Size CalculatePreferredSize() const override; 80 views::PaintInfo::ScaleType GetPaintScaleType() const override; 81 void OnBoundsChanged(const gfx::Rect& previous_bounds) override; 82 void PreferredSizeChanged() override; 83 84 private: 85 friend class ImageViewTest; 86 87 void OnPaintImage(gfx::Canvas* canvas); 88 89 // Gets an ImageSkia to paint that has proper rep for |scale|. 90 gfx::ImageSkia GetPaintImage(float scale); 91 92 // Returns true if |img| is the same as the last image we painted. This is 93 // intended to be a quick check, not exhaustive. In other words it's possible 94 // for this to return false even though the images are in fact equal. 95 bool IsImageEqual(const gfx::ImageSkia& img) const; 96 97 // Recomputes and updates the |image_origin_|. 98 void UpdateImageOrigin(); 99 100 gfx::Size GetImageSize() const; 101 102 // The origin of the image. 103 gfx::Point image_origin_; 104 105 // The current tooltip text. 106 base::string16 tooltip_text_; 107 108 // The current accessible name text. 109 base::string16 accessible_name_; 110 111 // Horizontal alignment. 112 Alignment horizontal_alignment_ = Alignment::kCenter; 113 114 // Vertical alignment. 115 Alignment vertical_alignment_ = Alignment::kCenter; 116 117 // The underlying image. 118 gfx::ImageSkia image_; 119 120 // Caches the scaled image reps. 121 gfx::ImageSkia scaled_image_; 122 123 // Scale last painted at. 124 float last_paint_scale_ = 0.f; 125 126 // Address of bytes we last painted. This is used only for comparison, so its 127 // safe to cache. 128 void* last_painted_bitmap_pixels_ = nullptr; 129 130 // The requested image size. 131 base::Optional<gfx::Size> image_size_; 132 133 DISALLOW_COPY_AND_ASSIGN(ImageView); 134 }; 135 136 } // namespace views 137 138 #endif // UI_VIEWS_CONTROLS_IMAGE_VIEW_H_ 139