API  1.0.0
CPTrackingArea.j
Go to the documentation of this file.
1 /*
2  * CPTrackingArea.j
3  * AppKit
4  *
5  * Created by Didier Korthoudt.
6  * Copyright 2015, Cappuccino Project.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 /* @group CPTrackingAreaOptions */
26 @typedef CPTrackingAreaOptions
37 
38 var CPTrackingAreaViewRectKey = @"CPTrackinkAreaViewRectKey",
39  CPTrackingAreaOptionsKey = @"CPTrackingAreaOptionsKey",
40  CPTrackingAreaOwnerKey = @"CPTrackingAreaOwnerKey",
41  CPTrackingAreaUserInfoKey = @"CPTrackingAreaUserInfoKey",
42  CPTrackingAreaReferencingViewKey = @"CPTrackingAreaReferencingViewKey",
43  CPTrackingAreaWindowRect = @"CPTrackingAreaWindowRect";
44 
49 
56 @implementation CPTrackingArea : CPObject
57 {
58  CGRect _viewRect;
59  CPTrackingAreaOptions _options;
60  id _owner;
61  CPDictionary _userInfo;
62 
63  CPView _referencingView;
64  CGRect _windowRect;
65 
66  unsigned _implementedOwnerMethods;
67 }
68 
69 
70 #pragma mark -
71 #pragma mark Initialization
72 
77 - (CPTrackingArea)initWithRect:(CGRect)aRect options:(CPTrackingAreaOptions)options owner:(id)owner userInfo:(CPDictionary)userInfo
78 {
79  if (owner === nil)
80  [CPException raise:CPInternalInconsistencyException reason:"No owner specified"];
81 
82  if (options === 0)
83  [CPException raise:CPInternalInconsistencyException reason:"Invalid CPTrackingArea options"];
84 
85  // Check options:
86  // - at least one of CPTrackingMouseEnteredAndExited, CPTrackingMouseMoved, CPTrackingCursorUpdate
87  // - exactly one of CPTrackingActiveWhenFirstResponder, CPTrackingActiveInKeyWindow, CPTrackingActiveInActiveApp, CPTrackingActiveAlways
88  // - no check on CPTrackingAssumeInside, CPTrackingInVisibleRect, CPTrackingEnableDuringMouseDrag
89 
90  if (!((options & CPTrackingMouseEnteredAndExited) || (options & CPTrackingMouseMoved) || (options & CPTrackingCursorUpdate)))
91  [CPException raise:CPInternalInconsistencyException reason:"Invalid CPTrackingAreaOptions: must use at least one of [CPTrackingMouseEnteredAndExited | CPTrackingMouseMoved | CPTrackingCursorUpdate]"];
92 
93  if ((((options & CPTrackingActiveWhenFirstResponder) > 0) + ((options & CPTrackingActiveInKeyWindow) > 0) + ((options & CPTrackingActiveInActiveApp) > 0) + ((options & CPTrackingActiveAlways) > 0)) !== 1)
94  [CPException raise:CPInternalInconsistencyException reason:"Tracking area options may only specify one of [CPTrackingActiveWhenFirstResponder | CPTrackingActiveInKeyWindow | CPTrackingActiveInActiveApp | CPTrackingActiveAlways]."];
95 
96  if (self = [super init])
97  {
98  _viewRect = aRect;
99  _options = options;
100  _owner = owner;
101  _userInfo = userInfo;
102 
103  // Cache owner implemented methods
104 
105  if ([_owner respondsToSelector:@selector(mouseEntered:)])
106  _implementedOwnerMethods |= CPTrackingOwnerImplementsMouseEntered;
107 
108  if ([_owner respondsToSelector:@selector(mouseExited:)])
109  _implementedOwnerMethods |= CPTrackingOwnerImplementsMouseExited;
110 
111  if ([_owner respondsToSelector:@selector(mouseMoved:)])
112  _implementedOwnerMethods |= CPTrackingOwnerImplementsMouseMoved;
113 
114  if ([_owner respondsToSelector:@selector(cursorUpdate:)])
115  _implementedOwnerMethods |= CPTrackingOwnerImplementsCursorUpdate;
116  }
117 
118  return self;
119 }
120 
121 
122 #pragma mark -
123 #pragma mark Implementation
124 
125 - (void)_updateWindowRect
126 {
127  _windowRect = [_referencingView convertRect:((_options & CPTrackingInVisibleRect) ? [_referencingView visibleRect] : _viewRect) toView:[[_referencingView window] _windowView]];
128 }
129 
130 @end
131 
132 #pragma mark -
133 #pragma mark CPCoding
134 
136 
137 - (id)initWithCoder:(CPCoder)aCoder
138 {
139  if (self = [super init])
140  {
141  _viewRect = [aCoder decodeObjectForKey:CPTrackingAreaViewRectKey];
142  _options = [aCoder decodeObjectForKey:CPTrackingAreaOptionsKey];
143  _owner = [aCoder decodeObjectForKey:CPTrackingAreaOwnerKey];
144  _userInfo = [aCoder decodeObjectForKey:CPTrackingAreaUserInfoKey];
145  _referencingView = [aCoder decodeObjectForKey:CPTrackingAreaReferencingViewKey];
146  _windowRect = [aCoder decodeObjectForKey:CPTrackingAreaWindowRect];
147  }
148 
149  return self;
150 }
151 
152 - (void)encodeWithCoder:(CPCoder)aCoder
153 {
154  [aCoder encodeObject:_viewRect forKey:CPTrackingAreaViewRectKey];
155  [aCoder encodeObject:_options forKey:CPTrackingAreaOptionsKey];
156  [aCoder encodeObject:_owner forKey:CPTrackingAreaOwnerKey];
157  [aCoder encodeObject:_userInfo forKey:CPTrackingAreaUserInfoKey];
158  [aCoder encodeObject:_referencingView forKey:CPTrackingAreaReferencingViewKey];
159  [aCoder encodeObject:_windowRect forKey:CPTrackingAreaWindowRect];
160 }
161 
162 @end
163 
165 
169 - (CGRect)rect
170 {
171  return _viewRect;
172 }
173 
177 - (CPTrackingAreaOptions)options
178 {
179  return _options;
180 }
181 
185 - (id)owner
186 {
187  return _owner;
188 }
189 
193 - (CPDictionary)userInfo
194 {
195  return _userInfo;
196 }
197 
201 - (CPView)view
202 {
203  return _referencingView;
204 }
205 
209 - (void)setView:(CPView)aValue
210 {
211  _referencingView = aValue;
212 }
213 
217 - (CGRect)windowRect
218 {
219  return _windowRect;
220 }
221 
225 - (unsigned)implementedOwnerMethods
226 {
227  return _implementedOwnerMethods;
228 }
229 
230 @end
var CPTrackingAreaWindowRect
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPTrackingAssumeInside
var CPTrackingAreaReferencingViewKey
id init()
Definition: CALayer.j:126
CPTrackingOwnerImplementsCursorUpdate
CPTrackingMouseMoved
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
CPTrackingActiveInKeyWindow
CPTrackingActiveInActiveApp
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPTrackingActiveWhenFirstResponder
var CPTrackingAreaViewRectKey
var CPTrackingAreaUserInfoKey
CPTrackingOwnerImplementsMouseMoved
CPTrackingOwnerImplementsMouseExited
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
CPTrackingAreaOptions CPTrackingMouseEnteredAndExited
var CPTrackingAreaOptionsKey
CPTrackingEnabledDuringMouseDrag
CPTrackingOwnerImplementsMouseEntered
CPTrackingInVisibleRect
var CPTrackingAreaOwnerKey
Definition: CPView.j:137
CPTrackingCursorUpdate
CPTrackingActiveAlways