2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import <Adium/AIContentControllerProtocol.h>
18 #import <Adium/AIPreferenceControllerProtocol.h>
19 #import <Adium/AIMenuControllerProtocol.h>
20 #import "AdiumFormatting.h"
21 #import <AIUtilities/AIColorAdditions.h>
22 #import <AIUtilities/AIDictionaryAdditions.h>
23 #import <AIUtilities/AIFontAdditions.h>
24 #import <AIUtilities/AIMenuAdditions.h>
25 #import <AIUtilities/AITextAttributes.h>
27 #define DEFAULT_FORMATTING_DEFAULT_PREFS @"FormattingDefaults"
29 @implementation AdiumFormatting
36 if ((self = [super init])) {
37 [[adium preferenceController] registerDefaults:[NSDictionary dictionaryNamed:DEFAULT_FORMATTING_DEFAULT_PREFS
38 forClass:[self class]]
39 forGroup:PREF_GROUP_FORMATTING];
40 _defaultAttributes = nil;
47 * @brief Finish Initing
50 * 1) Preference controller is ready
52 - (void)controllerDidLoad
54 //Observe formatting preference changes
55 [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_FORMATTING];
57 //Reset formatting menu item
58 NSMenuItem *menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:AILocalizedString(@"Restore Default Formatting",nil)
60 action:@selector(restoreDefaultFormat:)
62 [[adium menuController] addMenuItem:menuItem toLocation:LOC_Format_Additions];
68 [[adium notificationCenter] removeObserver:self];
69 [_defaultAttributes release]; _defaultAttributes = nil;
75 * @brief Returns the default formatting attributes
77 * These attributes should be used for new text entry views, messages, etc.
78 * @return NSDictionary of NSAttributedString attributes
80 - (NSDictionary *)defaultFormattingAttributes
82 if(!_defaultAttributes){
83 NSFont *font = [[[adium preferenceController] preferenceForKey:KEY_FORMATTING_FONT
84 group:PREF_GROUP_FORMATTING] representedFont];
85 NSColor *textColor = [[[adium preferenceController] preferenceForKey:KEY_FORMATTING_TEXT_COLOR
86 group:PREF_GROUP_FORMATTING] representedColor];
87 NSColor *backgroundColor = [[[adium preferenceController] preferenceForKey:KEY_FORMATTING_BACKGROUND_COLOR
88 group:PREF_GROUP_FORMATTING] representedColor];
90 //Build formatting dict
91 _defaultAttributes = [[NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName] retain];
92 if (textColor && ![textColor equalToRGBColor:[NSColor textColor]]) {
93 AILog(@"TextColor is %@; -[NSColor textColor] gives %@",textColor,[NSColor textColor]);
94 [_defaultAttributes setObject:textColor forKey:NSForegroundColorAttributeName];
96 if (backgroundColor && ![backgroundColor equalToRGBColor:[NSColor textBackgroundColor]]) {
97 [_defaultAttributes setObject:backgroundColor forKey:AIBodyColorAttributeName];
98 [_defaultAttributes setObject:backgroundColor forKey:NSBackgroundColorAttributeName];
102 return _defaultAttributes;
106 * @brief Formatting preferences changed, reset our formatting cache
108 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key object:(AIListObject *)object
109 preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
111 [_defaultAttributes release];
112 _defaultAttributes = nil;
115 - (void)restoreDefaultFormat:(id)sender
117 NSResponder *responder = [[NSApp mainWindow] firstResponder];
118 if ([responder isKindOfClass:[NSTextView class]]) {
119 [(NSTextView *)responder setTypingAttributes:[self defaultFormattingAttributes]];
124 * @brief Enable/disable our restore default formatting menu item
126 * The item should only be enabled if the current responder has typing attributes and those typing attributes are not the default attributes
128 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
130 NSResponder *responder = [[NSApp mainWindow] firstResponder];
132 return (([responder isKindOfClass:[NSTextView class]]) &&
133 (![[(NSTextView *)responder typingAttributes] isEqualToDictionary:[self defaultFormattingAttributes]]));