Allow localization of the Alert Text label in the Display an Alert action
[adiumx.git] / Source / AdiumFormatting.m
blob042c88184163d8beb94a6e077841b327e0fe0497
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                         AILog(@"TextColor is %@; -[NSColor textColor] gives %@",textColor,[NSColor textColor]);
94                         [_defaultAttributes setObject:textColor forKey:NSForegroundColorAttributeName]; 
95                 }       
96                 if (backgroundColor && ![backgroundColor equalToRGBColor:[NSColor textBackgroundColor]]) {
97                         [_defaultAttributes setObject:backgroundColor forKey:AIBodyColorAttributeName]; 
98                         [_defaultAttributes setObject:backgroundColor forKey:NSBackgroundColorAttributeName];   
99                 }
100         }
101         
102         return _defaultAttributes;
106  * @brief Formatting preferences changed, reset our formatting cache
107  */
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]];
120         }
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
127  */
128 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
130         NSResponder *responder = [[NSApp mainWindow] firstResponder];
132         return (([responder isKindOfClass:[NSTextView class]]) &&
133                         (![[(NSTextView *)responder typingAttributes] isEqualToDictionary:[self defaultFormattingAttributes]]));
136 @end