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 UI_BASE_COCOA_TRACKING_AREA_H_
      6 #define UI_BASE_COCOA_TRACKING_AREA_H_
      7 
      8 #import <AppKit/AppKit.h>
      9 
     10 #include "base/mac/scoped_nsobject.h"
     11 #include "ui/base/ui_base_export.h"
     12 
     13 @class CrTrackingAreaOwnerProxy;
     14 
     15 // The CrTrackingArea can be used in place of an NSTrackingArea to shut off
     16 // messaging to the |owner| at a specific point in time.
     17 UI_BASE_EXPORT
     18 @interface CrTrackingArea : NSTrackingArea {
     19  @private
     20   base::scoped_nsobject<CrTrackingAreaOwnerProxy> ownerProxy_;
     21 }
     22 
     23 // Designated initializer. Forwards all arguments to the superclass, but wraps
     24 // |owner| in a proxy object.
     25 - (id)initWithRect:(NSRect)rect
     26            options:(NSTrackingAreaOptions)options
     27              owner:(id)owner
     28           userInfo:(NSDictionary*)userInfo;
     29 
     30 // Prevents any future messages from being delivered to the |owner|.
     31 - (void)clearOwner;
     32 
     33 // Watches |window| for its NSWindowWillCloseNotification and calls
     34 // |-clearOwner| when the notification is observed.
     35 - (void)clearOwnerWhenWindowWillClose:(NSWindow*)window;
     36 
     37 @end
     38 
     39 // Scoper //////////////////////////////////////////////////////////////////////
     40 
     41 namespace ui {
     42 
     43 // Use an instance of this class to call |-clearOwner| on the |tracking_area_|
     44 // when this goes out of scope.
     45 class UI_BASE_EXPORT ScopedCrTrackingArea {
     46  public:
     47   // Takes ownership of |tracking_area| without retaining it.
     48   explicit ScopedCrTrackingArea(CrTrackingArea* tracking_area = nil);
     49   ~ScopedCrTrackingArea();
     50 
     51   // This will call |scoped_nsobject<>::reset()| to take ownership of the new
     52   // tracking area.  Note that -clearOwner is NOT called on the existing
     53   // tracking area.
     54   void reset(CrTrackingArea* tracking_area = nil);
     55 
     56   CrTrackingArea* get() const;
     57 
     58  private:
     59   base::scoped_nsobject<CrTrackingArea> tracking_area_;
     60   DISALLOW_COPY_AND_ASSIGN(ScopedCrTrackingArea);
     61 };
     62 
     63 }  // namespace ui
     64 
     65 #endif  // UI_BASE_COCOA_TRACKING_AREA_H_
     66