-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAXOrigin.m
121 lines (110 loc) · 3.83 KB
/
AXOrigin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// AXOrigin.m
// FMN
//
// Created by Nathaniel Gray on 11/24/07.
// Copyright 2007 Nathaniel Gray. All rights reserved.
//
#import "AXOrigin.h"
#import "AXApplication.h"
#import "FMNRestorable.h"
#include <Carbon/Carbon.h> // For GetCurrentProcess
@implementation AXOrigin
// A timer callback to do some work after the window has been created.
- (void) setupOrigin:(NSTimer *) timer
{
NSLog(@"Initializing origin");
[self getOrigin];
[self resetOrigin];
}
// Make a little invisible window that sits at (0,0) to provide us with a
// reliable origin.
- (void) createOriginWindow
{
NSPoint origin;
NSSize small;
NSRect r;
origin.x = 0.0; origin.y = 0.0;
small.width = 10.0; small.height = 10.0;
r.origin = origin; r.size = small;
originWindow = [[NSWindow alloc] initWithContentRect:r
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[originWindow setCanHide:NO];
[originWindow setHasShadow:NO];
[originWindow setOpaque:NO];
[originWindow setAlphaValue:0.0];
//[originWindow setIgnoresMouseEvents:YES];
// I'm making this very visible for now so we know it's working
//[originWindow setBackgroundColor:[NSColor redColor]];
// XXX: This is only in Leopard -- do we want to try to work around that?
//[originWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
if ([originWindow respondsToSelector:@selector(setCollectionBehavior:)])
[originWindow setCollectionBehavior:1];
[originWindow orderBack:self];
// Quartz uses bottom-left (mathematical) coordinates, so this is how we
// put the window at the top-left corner
size_t height = CGDisplayPixelsHigh(CGMainDisplayID());
[originWindow setFrameTopLeftPoint:NSMakePoint(0.0, height)];
// We can't get the AXWindow yet. Do it later.
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(setupOrigin:)
userInfo:nil
repeats:NO];
originAXWindow = nil;
}
- (id) init
{
self = [super init];
if (!self)
return nil;
[self createOriginWindow];
return self;
}
- (void) dealloc
{
[originWindow release];
if (originAXWindow)
[originAXWindow release];
[super dealloc];
}
- (NSPoint) getOrigin
{
// XXX: Error checking here!!!
//NSLog(@"Getting origin...");
if (originAXWindow == nil) {
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
AXApplication* app = [AXApplication configWithPSN:psn
appName:@"Forget-Me-Not"
origin:NSMakePoint(0.0,0.0)];
NSArray *appWindows = [app getWindows];
//NSLog(@"FMN has %d window", [appWindows count]);
originAXWindow = [[appWindows objectAtIndex:0] retain];
}
NSPoint origin = [originAXWindow getWindowPosition];
NSLog(@"AX: Got origin at (%f, %f)", origin.x, origin.y);
return origin;
}
- (void) resetOrigin
{
if (originAXWindow == nil) {
@throw
[NSException
exceptionWithName : @"NoOriginError"
reason : @"originAXWindow is nil!"
userInfo : nil
];
}
NSLog(@"Resetting Origin");
// This doesn't work properly!
[originAXWindow setWindowPosition:NSMakePoint(0.0,0.0)];
// Try the quartz way again.
//size_t height = CGDisplayPixelsHigh(CGMainDisplayID());
//[originWindow setFrameTopLeftPoint:NSMakePoint(0.0, height)];
NSPoint origin = [self getOrigin];
NSLog(@"Origin is now reset to (%f,%f)", origin.x, origin.y);
}
@end