Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 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 CHROME_BROWSER_UI_COCOA_TABLE_ROW_NSIMAGE_CACHE_H_
      6 #define CHROME_BROWSER_UI_COCOA_TABLE_ROW_NSIMAGE_CACHE_H_
      7 #pragma once
      8 
      9 #import <Cocoa/Cocoa.h>
     10 
     11 #include "base/memory/scoped_nsobject.h"
     12 
     13 class SkBitmap;
     14 
     15 // There are several dialogs that display tabular data with one SkBitmap
     16 // per row. This class converts these SkBitmaps to NSImages on demand, and
     17 // caches the results.
     18 class TableRowNSImageCache {
     19  public:
     20   // Interface this cache expects for its table model.
     21   class Table {
     22    public:
     23     // Returns the number of rows in the table.
     24     virtual int RowCount() const = 0;
     25 
     26     // Returns the icon of the |row|th row.
     27     virtual SkBitmap GetIcon(int row) const = 0;
     28 
     29    protected:
     30     virtual ~Table() {}
     31   };
     32 
     33   // |model| must outlive the cache.
     34   explicit TableRowNSImageCache(Table* model);
     35   ~TableRowNSImageCache();
     36 
     37   // Lazily converts the image at the given row and caches it in |icon_images_|.
     38   NSImage* GetImageForRow(int row);
     39 
     40   // Call these functions every time the table changes, to update the cache.
     41   void OnModelChanged();
     42   void OnItemsChanged(int start, int length);
     43   void OnItemsAdded(int start, int length);
     44   void OnItemsRemoved(int start, int length);
     45 
     46  private:
     47   // The table model we query for row count and icons.
     48   Table* model_;  // weak
     49 
     50   // Stores strong NSImage refs for icons. If an entry is NULL, it will be
     51   // created in GetImageForRow().
     52   scoped_nsobject<NSPointerArray> icon_images_;
     53 };
     54 
     55 #endif  // CHROME_BROWSER_UI_COCOA_TABLE_ROW_NSIMAGE_CACHE_H_
     56 
     57