Home | History | Annotate | Download | only in controls
      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 /////////////////////////////////////////////////////////////////////////////
     18 //
     19 // ImageView class.
     20 //
     21 // An ImageView can display an image from an ImageSkia. If a size is provided,
     22 // the ImageView will resize the provided image to fit if it is too big or will
     23 // center the image if smaller. Otherwise, the preferred size matches the
     24 // provided image size.
     25 //
     26 /////////////////////////////////////////////////////////////////////////////
     27 class VIEWS_EXPORT ImageView : public View {
     28  public:
     29   enum Alignment {
     30     LEADING = 0,
     31     CENTER,
     32     TRAILING
     33   };
     34 
     35   ImageView();
     36   virtual ~ImageView();
     37 
     38   // Set the image that should be displayed.
     39   void SetImage(const gfx::ImageSkia& img);
     40 
     41   // Set the image that should be displayed from a pointer. Reset the image
     42   // if the pointer is NULL. The pointer contents is copied in the receiver's
     43   // image.
     44   void SetImage(const gfx::ImageSkia* image_skia);
     45 
     46   // Returns the image currently displayed or NULL of none is currently set.
     47   // The returned image is still owned by the ImageView.
     48   const gfx::ImageSkia& GetImage();
     49 
     50   // Set the desired image size for the receiving ImageView.
     51   void SetImageSize(const gfx::Size& image_size);
     52 
     53   // Return the preferred size for the receiving view. Returns false if the
     54   // preferred size is not defined, which means that the view uses the image
     55   // size.
     56   bool GetImageSize(gfx::Size* image_size);
     57 
     58   // Returns the actual bounds of the visible image inside the view.
     59   gfx::Rect GetImageBounds() const;
     60 
     61   // Reset the image size to the current image dimensions.
     62   void ResetImageSize();
     63 
     64   // Set / Get the horizontal alignment.
     65   void SetHorizontalAlignment(Alignment ha);
     66   Alignment GetHorizontalAlignment() const;
     67 
     68   // Set / Get the vertical alignment.
     69   void SetVerticalAlignment(Alignment va);
     70   Alignment GetVerticalAlignment() const;
     71 
     72   // Set / Get the tooltip text.
     73   void SetTooltipText(const string16& tooltip);
     74   string16 GetTooltipText() const;
     75 
     76   void set_interactive(bool interactive) { interactive_ = interactive; }
     77 
     78   // Overriden from View:
     79   virtual gfx::Size GetPreferredSize() OVERRIDE;
     80   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
     81   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
     82   virtual bool GetTooltipText(const gfx::Point& p,
     83                               string16* tooltip) const OVERRIDE;
     84   virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE;
     85 
     86  private:
     87   // Compute the image origin given the desired size and the receiver alignment
     88   // properties.
     89   gfx::Point ComputeImageOrigin(const gfx::Size& image_size) const;
     90 
     91   // Whether the image size is set.
     92   bool image_size_set_;
     93 
     94   // The actual image size.
     95   gfx::Size image_size_;
     96 
     97   // The underlying image.
     98   gfx::ImageSkia image_;
     99 
    100   // Horizontal alignment.
    101   Alignment horiz_alignment_;
    102 
    103   // Vertical alignment.
    104   Alignment vert_alignment_;
    105 
    106   // The current tooltip text.
    107   string16 tooltip_text_;
    108 
    109   // A flag controlling hit test handling for interactivity.
    110   bool interactive_;
    111 
    112   DISALLOW_COPY_AND_ASSIGN(ImageView);
    113 };
    114 
    115 }  // namespace views
    116 
    117 #endif  // UI_VIEWS_CONTROLS_IMAGE_VIEW_H_
    118