2001-12-17 16:51:51 +00:00
|
|
|
|
/** <title>NSInterfaceStyle</title>
|
1999-04-26 08:52:41 +00:00
|
|
|
|
|
|
|
|
|
Copyright (C) 1999 Free Software Foundation, Inc.
|
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
|
Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
1999-04-26 08:52:41 +00:00
|
|
|
|
Date: 1999
|
|
|
|
|
|
|
|
|
|
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 Library 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
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
|
If not, write to the Free Software Foundation,
|
2005-05-26 02:52:46 +00:00
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1999-04-26 08:52:41 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2003-07-31 23:52:10 +00:00
|
|
|
|
#include "config.h"
|
1999-04-26 08:52:41 +00:00
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
#include <Foundation/NSDictionary.h>
|
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
|
#include <Foundation/NSUserDefaults.h>
|
|
|
|
|
#include <Foundation/NSMapTable.h>
|
|
|
|
|
|
2003-06-13 15:01:12 +00:00
|
|
|
|
#include "AppKit/NSResponder.h"
|
|
|
|
|
#include "AppKit/NSInterfaceStyle.h"
|
1999-04-26 08:52:41 +00:00
|
|
|
|
|
|
|
|
|
NSString *NSInterfaceStyleDefault = @"NSInterfaceStyleDefault";
|
|
|
|
|
|
|
|
|
|
static NSMapTable *styleMap = 0;
|
|
|
|
|
static NSInterfaceStyle defStyle;
|
|
|
|
|
|
|
|
|
|
static NSInterfaceStyle
|
|
|
|
|
styleFromString(NSString* str)
|
|
|
|
|
{
|
|
|
|
|
if ([str isEqualToString: @"NSNextStepInterfaceStyle"])
|
|
|
|
|
return NSNextStepInterfaceStyle;
|
|
|
|
|
if ([str isEqualToString: @"NSMacintoshInterfaceStyle"])
|
|
|
|
|
return NSMacintoshInterfaceStyle;
|
|
|
|
|
if ([str isEqualToString: @"NSWindows95InterfaceStyle"])
|
|
|
|
|
return NSWindows95InterfaceStyle;
|
1999-09-07 08:59:35 +00:00
|
|
|
|
if ([str isEqualToString: @"GSWindowMakerInterfaceStyle"])
|
|
|
|
|
return GSWindowMakerInterfaceStyle;
|
1999-04-26 08:52:41 +00:00
|
|
|
|
return NSNoInterfaceStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@interface GSInterfaceStyle : NSObject
|
|
|
|
|
+ (void) defaultsDidChange: (NSNotification*)notification;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
@defs(NSResponder)
|
|
|
|
|
} *accessToResponder;
|
|
|
|
|
|
2002-01-23 16:32:20 +00:00
|
|
|
|
/**
|
|
|
|
|
<p>
|
|
|
|
|
Returns the interface style the responder should use, which affects
|
|
|
|
|
how a UI element (such as a button or menu) is displayed. If the
|
|
|
|
|
responder has an interface style set, the key is ignored and the
|
|
|
|
|
responder's interface style is returned. Otherwise the style
|
|
|
|
|
associated with the key is returned (if set), otherwise the default
|
|
|
|
|
style is returned. In no case will the style <code>NSNoInterfaceStyle</code>
|
|
|
|
|
be returned.
|
|
|
|
|
</p>
|
|
|
|
|
<p>
|
|
|
|
|
Styles can be set using the user defaults system. Currently available
|
2002-02-25 17:39:11 +00:00
|
|
|
|
styles are
|
2002-08-29 10:07:51 +00:00
|
|
|
|
</p>
|
2002-02-25 17:39:11 +00:00
|
|
|
|
<list>
|
2002-08-29 10:07:51 +00:00
|
|
|
|
<item>NSNextStepInterfaceStyle</item>
|
|
|
|
|
<item>NSMacintoshInterfaceStyle</item>
|
|
|
|
|
<item>NSWindows95InterfaceStyle</item>
|
|
|
|
|
<item>GSWindowMakerInterfaceStyle</item>
|
2002-02-25 17:39:11 +00:00
|
|
|
|
</list>
|
2002-08-29 10:07:51 +00:00
|
|
|
|
<p>
|
2002-02-25 17:39:11 +00:00
|
|
|
|
You can set a default style
|
2002-01-23 16:32:20 +00:00
|
|
|
|
for all UI elements using the <code>NSInterfaceStyleDefault</code> key:
|
2002-08-29 10:07:51 +00:00
|
|
|
|
</p>
|
2002-01-23 16:32:20 +00:00
|
|
|
|
<example>
|
|
|
|
|
defaults write NSGlobalDomain NSInterfaceStyleDefault GSWindowMakerInterfaceStyle
|
|
|
|
|
</example>
|
|
|
|
|
*/
|
1999-04-26 08:52:41 +00:00
|
|
|
|
extern NSInterfaceStyle
|
|
|
|
|
NSInterfaceStyleForKey(NSString *key, NSResponder *responder)
|
|
|
|
|
{
|
|
|
|
|
NSInterfaceStyle style;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the specified responder has a style set, return it.
|
|
|
|
|
*/
|
|
|
|
|
if (responder)
|
|
|
|
|
{
|
2002-08-29 10:07:51 +00:00
|
|
|
|
style
|
|
|
|
|
= (NSInterfaceStyle)((accessToResponder)responder)->_interface_style;
|
1999-04-26 08:52:41 +00:00
|
|
|
|
if (style != NSNoInterfaceStyle)
|
|
|
|
|
{
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there is no style map, the defaults/cache management class must be
|
|
|
|
|
* initialised.
|
|
|
|
|
*/
|
|
|
|
|
if (styleMap == 0)
|
|
|
|
|
[GSInterfaceStyle class];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there is a style for the given defaults key, return it - after
|
|
|
|
|
* caching it in a map table if necessary.
|
|
|
|
|
*/
|
|
|
|
|
if (key)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* First try the cache - then, if no style is found, use the
|
|
|
|
|
* defaults system and add the results into the cache.
|
|
|
|
|
*/
|
|
|
|
|
style = (NSInterfaceStyle)NSMapGet(styleMap, key);
|
|
|
|
|
if (style == NSNoInterfaceStyle)
|
|
|
|
|
{
|
|
|
|
|
NSUserDefaults *defs;
|
|
|
|
|
NSString *def;
|
|
|
|
|
|
|
|
|
|
defs = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
def = [defs stringForKey: key];
|
|
|
|
|
if (def == nil
|
|
|
|
|
|| (style = styleFromString(def)) == NSNoInterfaceStyle)
|
|
|
|
|
{
|
2002-01-23 16:32:20 +00:00
|
|
|
|
style = defStyle;
|
1999-04-26 08:52:41 +00:00
|
|
|
|
}
|
2002-01-23 16:32:20 +00:00
|
|
|
|
if (style != NSNoInterfaceStyle)
|
|
|
|
|
NSMapInsert(styleMap, (void*)key, (void*)style);
|
1999-04-26 08:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* No responder and no key - return the default style.
|
|
|
|
|
*/
|
|
|
|
|
return defStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The GSInterfaceStyle class is used solely to maintain our map of
|
|
|
|
|
* know interface styles by updating when user defaults change.
|
|
|
|
|
*/
|
|
|
|
|
@implementation GSInterfaceStyle
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [GSInterfaceStyle class])
|
|
|
|
|
{
|
|
|
|
|
styleMap = NSCreateMapTable(NSObjectMapKeyCallBacks,
|
|
|
|
|
NSIntMapValueCallBacks, 8);
|
|
|
|
|
|
|
|
|
|
[NSUserDefaults standardUserDefaults];
|
|
|
|
|
[self defaultsDidChange: nil];
|
1999-06-15 20:02:58 +00:00
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
|
addObserver: self
|
|
|
|
|
selector: @selector(defaultsDidChange:)
|
|
|
|
|
name: NSUserDefaultsDidChangeNotification
|
|
|
|
|
object: nil];
|
1999-04-26 08:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) defaultsDidChange: (NSNotification*)notification
|
|
|
|
|
{
|
|
|
|
|
NSUserDefaults *defs;
|
|
|
|
|
NSMapEnumerator enumerator;
|
|
|
|
|
NSString *key;
|
2005-08-22 06:38:04 +00:00
|
|
|
|
void *val;
|
1999-04-26 08:52:41 +00:00
|
|
|
|
|
|
|
|
|
defs = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Determine the default interface style for the application.
|
|
|
|
|
*/
|
|
|
|
|
key = [defs stringForKey: NSInterfaceStyleDefault];
|
|
|
|
|
if (key == nil || (defStyle = styleFromString(key)) == NSNoInterfaceStyle)
|
|
|
|
|
defStyle = NSNextStepInterfaceStyle;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Now check the interface styles for all the keys in use and adjust our
|
|
|
|
|
* map table for any changes.
|
|
|
|
|
*/
|
|
|
|
|
enumerator = NSEnumerateMapTable(styleMap);
|
|
|
|
|
while (NSNextMapEnumeratorPair(&enumerator, (void**)&key, (void**)&val))
|
|
|
|
|
{
|
|
|
|
|
NSInterfaceStyle newStyle;
|
|
|
|
|
NSString *def = [defs stringForKey: key];
|
|
|
|
|
|
|
|
|
|
if (def == nil)
|
|
|
|
|
{
|
|
|
|
|
newStyle = defStyle;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
newStyle = styleFromString(def);
|
|
|
|
|
if (newStyle == NSNoInterfaceStyle)
|
|
|
|
|
{
|
|
|
|
|
newStyle = defStyle;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-22 06:38:04 +00:00
|
|
|
|
if (newStyle != ((NSInterfaceStyle)val))
|
1999-04-26 08:52:41 +00:00
|
|
|
|
{
|
|
|
|
|
NSMapInsert(styleMap, (void*)key, (void*)newStyle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|