1/**
2    Kuklomenos
3	NSScreen+AvailableResolutions.m copyright (C) 2008 Sijmen Mulder <sjmulder@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#import "NSScreen+AvailableResolutions.h"
20
21@implementation NSScreen(AvailableResolutions)
22
23- (NSArray *)availableResolutions
24{
25	CGDirectDisplayID displayID = (CGDirectDisplayID)[[[self deviceDescription] valueForKey:@"NSScreenNumber"] unsignedIntValue];
26
27	NSArray *availableModes = (NSArray *)CGDisplayAvailableModes(displayID);
28	int modeCount = [availableModes count];
29	NSMutableArray *availableResolutions = [[NSMutableArray alloc] initWithCapacity:modeCount];
30
31	NSSize lastResolution = NSMakeSize(0, 0);
32	for (int i = 0; i < modeCount; i++)
33	{
34		NSDictionary *mode = [availableModes objectAtIndex:i];
35
36		NSNumber *width  = [mode objectForKey:(NSString *)kCGDisplayWidth];
37		NSNumber *height = [mode objectForKey:(NSString *)kCGDisplayHeight];
38
39		NSSize resolution = NSMakeSize([width floatValue], [height floatValue]);
40		if (resolution.width != lastResolution.width && resolution.height != lastResolution.height)
41		{
42			[availableResolutions insertObject:[NSValue valueWithSize:resolution] atIndex:0];
43		}
44
45		lastResolution = resolution;
46	}
47
48	return [availableResolutions autorelease];
49}
50
51@end
52