File: NSScreen.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (448 lines) | stat: -rw-r--r-- 10,766 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/** <title>NSScreen</title>

   Copyright (C) 1996, 2000 Free Software Foundation, Inc.

   Author: Scott Christley <scottc@net-community.com>
   Date: 1996
   Major modifications and updates
   Author: Gregory John Casamento <borgheron@yahoo.com>
   Date: 2000
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/ 

#import <Foundation/NSArray.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSException.h>
#import <Foundation/NSGeometry.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSUserDefaults.h>
#import "AppKit/AppKitExceptions.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSInterfaceStyle.h"
#import "AppKit/NSMenu.h"
#import "AppKit/NSMenuView.h"
#import "AppKit/NSScreen.h"
#import "AppKit/NSWindow.h"
#import "GNUstepGUI/GSDisplayServer.h"

@interface NSScreen (Private)
- (id) _initWithScreenNumber: (int)screen;
@end

@implementation NSScreen

/*
 * Class methods
 */

+ (void) initialize
{
  if (self == [NSScreen class])
    {
      [self setVersion: 1];
      [[NSNotificationCenter defaultCenter]
          addObserver: self
          selector: @selector(_resetScreens:)
          name: NSApplicationDidChangeScreenParametersNotification
          object: nil];
     }
}

static NSMutableArray *screenArray = nil;

/**
 * Resets the cached list of screens.
 */
+ (void) _resetScreens: (NSNotification*)notification
{
  [self resetScreens];
}

+ (void) resetScreens
{
  DESTROY(screenArray);
}

/**
 * Returns an NSArray containing NSScreen instances representing all of the
 * screen devices attached to the computer.
 */
+ (NSArray*) screens
{
  int count = 0, index = 0;
  NSArray *screens;
  GSDisplayServer *srv;

  if (screenArray != nil)
    return screenArray;

  srv = GSCurrentServer();
  screens = [srv screenList];
  count = [screens count];
  if (count == 0)
    {
      // something is wrong. This shouldn't happen.
      [NSException raise: NSWindowServerCommunicationException
                   format: @"Unable to retrieve list of screens from window server."];
      return nil;
    }

  screenArray = [NSMutableArray new];

  // Iterate over the list
  for (index = 0; index < count; index++)
    {
      NSScreen *screen = nil;
      
      screen = [[NSScreen alloc] _initWithScreenNumber: 
                                     [[screens objectAtIndex: index] intValue]];
      [screenArray addObject: screen];
      RELEASE(screen);
    }

  return [NSArray arrayWithArray: screenArray];
}

// Creating NSScreen Instances
/**
 * Gets information about the main screen.
 */
+ (NSScreen*) mainScreen
{
  NSWindow *keyWindow;

  keyWindow = [NSApp keyWindow];
  if (keyWindow != nil)
    {
      return [keyWindow screen];
    }
  else
    {
      NSArray *screenArray = [self screens];

      if (screenArray != nil)
        {
          return [screenArray objectAtIndex: 0];
        }
      else
        {
          return nil;
        }
    }
}

/**
 * Gets information about the screen with the highest depth (i.e. bits per pixel).
 */
+ (NSScreen*) deepestScreen
{
  NSArray *screenArray = [self screens];
  NSEnumerator *screenEnumerator = nil;
  NSScreen *deepestScreen = nil, *screen = nil;  
  int maxBits = 0;

  // Iterate over the list of screens and find the
  // one with the most depth.
  screenEnumerator = [screenArray objectEnumerator];
  while ((screen = [screenEnumerator nextObject]) != nil)
    {
      int bits = 0;
      
      bits = [screen depth];
      
      if (bits > maxBits)
        {
          maxBits = bits;
          deepestScreen = screen;
        }
    }

  return deepestScreen;
}


/*
 * Instance methods
 */

/**
 * NSScreen does not respond to the init method.
 */
- (id) init
{
  [self doesNotRecognizeSelector: _cmd];
  return nil;
}

/**
 * Get all of the infomation for a given screen.
 */
- (id) _initWithScreenNumber: (int)screen
{
  GSDisplayServer *srv;

  if (!(self = [super init]))
    {
      return nil;
    }

  // Check for problems
  if (screen < 0)
    {
      NSLog(@"Internal error: Invalid screen number %d\n", screen);
      RELEASE(self);
      return nil;
    }

  srv = GSCurrentServer();
  if (srv == nil)
    {
      NSLog(@"Internal error: No current context\n");
      RELEASE(self);
      return nil;
    }

  // Fill in all of the i-vars with appropriate values.
  _screenNumber = screen;
  _frame = [srv boundsForScreen: _screenNumber];
  _depth = [srv windowDepthForScreen: _screenNumber];
  _supportedWindowDepths = NULL;

  return self;
}

- (BOOL) isEqual: (id)anObject
{
  if (anObject == self)
    return YES;
  if ([anObject isKindOfClass: [self class]] == NO)
    return NO;
  if (_screenNumber == ((NSScreen *)anObject)->_screenNumber)
    return YES;
  return NO;
}

/**
 * Returns the depth of the screen in bits.
 */
- (NSWindowDepth) depth
{
  return _depth;
}

/**
 * The full frame of the screen.
 */
- (NSRect) frame
{
  return _frame;
}

- (NSString*) description
{
  return [NSString stringWithFormat: @"%@ number: %ld frame: %@", 
                   [super description], (long)_screenNumber,
                   NSStringFromRect(_frame)];
}


/**
 * <p>
 * This method generates a dictionary containing information
 * about the screen device.  The resulting dictionary will have the following
 * entires: NSScreenNumber, NSDeviceSize, NSDeviceResolution, NSDeviceBitsPerSample,
 * NSDeviceColorSpaceName.
 * </p>
 */
- (NSDictionary*) deviceDescription
{
  if (_reserved == 0)
    {
      NSMutableDictionary	*devDesc;
      int			bps = 0;
      NSSize			screenResolution;
      NSString			*colorSpaceName = nil;
      CGFloat                   scaleFactor;

      /*
       * This method generates a dictionary from the
       * information we have gathered from the screen.
       */

      // Set the screen number in the current object.
      devDesc = [[NSMutableDictionary alloc] initWithCapacity: 8];
      [devDesc setObject: [NSNumber numberWithInt: _screenNumber]
		  forKey: @"NSScreenNumber"];

      // This is assumed since we are in NSScreen.
      [devDesc setObject: @"YES"  forKey: NSDeviceIsScreen];

      // Add the NSDeviceSize dictionary item
      [devDesc setObject: [NSValue valueWithSize: _frame.size]
		  forKey: NSDeviceSize];

      // Add the NSDeviceResolution dictionary item
      scaleFactor = [self userSpaceScaleFactor];
      screenResolution = NSMakeSize(72.0 * scaleFactor, 72.0 * scaleFactor);
      [devDesc setObject: [NSValue valueWithSize: screenResolution]
		  forKey: NSDeviceResolution];

      // Add the bits per sample entry
      bps = NSBitsPerSampleFromDepth(_depth);
      [devDesc setObject: [NSNumber numberWithInt: bps]
		  forKey: NSDeviceBitsPerSample];

      // Add the color space entry.
      colorSpaceName = NSColorSpaceFromDepth(_depth);
      [devDesc setObject: colorSpaceName
		  forKey: NSDeviceColorSpaceName];
		    
      _reserved = (void*)[devDesc copy];
      RELEASE(devDesc);
    }
  return (NSDictionary*)_reserved;
}

// Mac OS X methods
/**
 * Returns the window depths this screen will support.
 */
- (const NSWindowDepth*) supportedWindowDepths
{
  // If the variable isn't initialized, get the info and
  // store it for the future.
  if (_supportedWindowDepths == NULL)
    {
      _supportedWindowDepths = 
        (NSWindowDepth*)[GSCurrentServer()
			       availableDepthsForScreen: _screenNumber];

      // Check the results
      if (_supportedWindowDepths == NULL)
        {
          NSLog(@"Internal error: no depth list returned from window server.");
          return NULL;
        }
    }

  return _supportedWindowDepths;
}

/**
 * Returns the NSRect representing the visible area of the screen.
 */
- (NSRect) visibleFrame
{
  NSRect visFrame = _frame;
  float menuHeight;

  switch (NSInterfaceStyleForKey(@"NSMenuInterfaceStyle", nil))
    {
      case NSMacintoshInterfaceStyle:
        if ([NSApp mainMenu] == nil)
          {
            // No menu yet ... assume a standard height
            menuHeight = [NSMenuView menuBarHeight];
          }
        else
          {
            menuHeight = [[[NSApp mainMenu] window] frame].size.height;
          }
        
        visFrame.size.height -= menuHeight;
        break;

      case GSWindowMakerInterfaceStyle:
      case NSNextStepInterfaceStyle:
        /* FIXME: Menu width will vary from app to app and  there is no
         * fixed position for the menu ... should we be making room for
         * a menu top left, or something else?
         */
#if 0
        if ([NSApp mainMenu] != nil)
          {
            float menuWidth = [[[NSApp mainMenu] window] frame].size.width;
      
            visFrame.size.width -= menuWidth;
            visFrame.origin.x += menuWidth;
          }
#endif
        break;
      
      default:
        break;
    }
  return visFrame;
}

/** Returns the screen number */
- (int) screenNumber
{
  return _screenNumber;
}

// Release the memory for the depths array.
- (void) dealloc
{
  // _supportedWindowDepths can be NULL since it may or may not
  // be necessary to get this info.  The most common use of NSScreen
  // is to get the depth and frame attributes.
  if (_supportedWindowDepths != NULL)
    {
      NSZoneFree(NSDefaultMallocZone(), _supportedWindowDepths);
    }
  if (_reserved != 0)
    {
      [(id)_reserved release];
    }
  [super dealloc];
}

- (float) userSpaceScaleFactor
{
  NSNumber *factor = [[NSUserDefaults standardUserDefaults]
		       objectForKey: @"GSScaleFactor"];

  if (factor != nil)
    {
      return [factor floatValue];
    }
  else
    {    
      GSDisplayServer *srv = GSCurrentServer();
      if (srv != nil)
      {
        NSSize dpi = [GSCurrentServer() resolutionForScreen: _screenNumber];
        // average the width and height
        return (dpi.width + dpi.height) / 144.0;
      }
      else
      {
        return 1.0;
      }
    }
}

@end