1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6 
7 #ifndef BASE_CHROME_APPLICATION_MAC_H_
8 #define BASE_CHROME_APPLICATION_MAC_H_
9 
10 #import <AppKit/AppKit.h>
11 
12 #include "base/basictypes.h"
13 #include "base/scoped_nsobject.h"
14 
15 // Event hooks must implement this protocol.
16 @protocol CrApplicationEventHookProtocol
17 - (void)hookForEvent:(NSEvent*)theEvent;
18 @end
19 
20 @interface CrApplication : NSApplication {
21  @private
22   BOOL handlingSendEvent_;
23   // Array of objects implementing the CrApplicationEventHookProtocol
24   scoped_nsobject<NSMutableArray> eventHooks_;
25 }
26 @property(readonly, getter=isHandlingSendEvent, nonatomic) BOOL handlingSendEvent;
27 
28 // Add or remove an event hook to be called for every sendEvent:
29 // that the application receives.  These handlers are called before
30 // the normal [NSApplication sendEvent:] call is made.
31 
32 // This is not a good alternative to a nested event loop.  It should
33 // be used only when normal event logic and notification breaks down
34 // (e.g. when clicking outside a canBecomeKey:NO window to "switch
35 // context" out of it).
36 - (void)addEventHook:(id<CrApplicationEventHookProtocol>)hook;
37 - (void)removeEventHook:(id<CrApplicationEventHookProtocol>)hook;
38 
39 + (NSApplication*)sharedApplication;
40 @end
41 
42 namespace chrome_application_mac {
43 
44 // Controls the state of |handlingSendEvent_| in the event loop so that it is
45 // reset properly.
46 class ScopedSendingEvent {
47  public:
48   ScopedSendingEvent();
49   ~ScopedSendingEvent();
50 
51  private:
52   CrApplication* app_;
53   BOOL handling_;
54   DISALLOW_COPY_AND_ASSIGN(ScopedSendingEvent);
55 };
56 
57 }  // chrome_application_mac
58 
59 #endif  // BASE_CHROME_APPLICATION_MAC_H_
60