{{{
[adiumx.git] / Source / AdiumFormatting.m
blob0f3a856ffa9994dc4f54fd800946f6cf5368b0ef
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
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.
8  * 
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.
12  * 
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.
15  */
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
31 /*!
32  * @brief Init
33  */
34 - (id)init
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;
41         }
42         
43         return self;
46 /*!
47  * @brief Finish Initing
48  *
49  * Requires:
50  * 1) Preference controller is ready
51  */
52 - (void)controllerDidLoad
54         //Observe formatting preference changes
55         [[adium preferenceController] registerPreferenceObserver:self forGroup:PREF_GROUP_FORMATTING];
56         
57         //Reset formatting menu item
58         NSMenuItem      *menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:AILocalizedString(@"Restore Default Formatting",nil)
59                                                                                                                                                                  target:self
60                                                                                                                                                                  action:@selector(restoreDefaultFormat:)
61                                                                                                                                                   keyEquivalent:@""];
62         [[adium menuController] addMenuItem:menuItem toLocation:LOC_Format_Additions];
63         [menuItem release];
66 - (void)dealloc
68         [[adium notificationCenter] removeObserver:self];
69         [_defaultAttributes release]; _defaultAttributes = nil;
70         
71         [super dealloc];
74 /*!
75  * @brief Returns the default formatting attributes
76  *
77  * These attributes should be used for new text entry views, messages, etc.
78  * @return NSDictionary of NSAttributedString attributes
79  */
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];
89                                 
90                 //Build formatting dict
91                 _defaultAttributes = [[NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName] retain];
92                 if (textColor && ![textColor equalToRGBColor:[NSColor textColor]]) {
93                         [_defaultAttributes setObject:textColor forKey:NSForegroundColorAttributeName]; 
94                 }       
95                 if (backgroundColor && ![backgroundColor equalToRGBColor:[NSColor textBackgroundColor]]) {
96                         [_defaultAttributes setObject:backgroundColor forKey:AIBodyColorAttributeName]; 
97                         [_defaultAttributes setObject:backgroundColor forKey:NSBackgroundColorAttributeName];   
98                 }
99         }
100         
101         return _defaultAttributes;
105  * @brief Formatting preferences changed, reset our formatting cache
106  */
107 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key object:(AIListObject *)object
108                                         preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
110         [_defaultAttributes release];
111         _defaultAttributes = nil;
114 - (void)restoreDefaultFormat:(id)sender
116         NSResponder *responder = [[NSApp mainWindow] firstResponder];
117         if ([responder isKindOfClass:[NSTextView class]]) {
118                 [(NSTextView *)responder setTypingAttributes:[self defaultFormattingAttributes]];
119         }
123  * @brief Enable/disable our restore default formatting menu item
125  * The item should only be enabled if the current responder has typing attributes and those typing attributes are not the default attributes
126  */
127 - (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
129         NSResponder *responder = [[NSApp mainWindow] firstResponder];
131         return (([responder isKindOfClass:[NSTextView class]]) &&
132                         (![[(NSTextView *)responder typingAttributes] isEqualToDictionary:[self defaultFormattingAttributes]]));
135 @end