Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: chrome/browser/ui/cocoa/font_language_settings_controller.mm

Issue 6339002: [Mac] Consolidate all files relating to preferences in a subdir of c/b/ui/coc... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/font_language_settings_controller.h"
6
7 #import <Cocoa/Cocoa.h>
8 #import "base/mac/mac_util.h"
9 #include "base/sys_string_conversions.h"
10 #include "chrome/browser/character_encoding.h"
11 #include "chrome/browser/fonts_languages_window.h"
12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15
16 NSString* const kCharacterInfoEncoding = @"encoding";
17 NSString* const kCharacterInfoName = @"name";
18 NSString* const kCharacterInfoID = @"id";
19
20 void ShowFontsLanguagesWindow(gfx::NativeWindow window,
21 FontsLanguagesPage page,
22 Profile* profile) {
23 NOTIMPLEMENTED();
24 }
25
26 @interface FontLanguageSettingsController (Private)
27 - (void)updateDisplayField:(NSTextField*)field
28 withFont:(NSFont*)font
29 withLabel:(NSTextField*)label;
30 @end
31
32 @implementation FontLanguageSettingsController
33
34 - (id)initWithProfile:(Profile*)profile {
35 DCHECK(profile);
36 NSString* nibpath = [base::mac::MainAppBundle()
37 pathForResource:@"FontLanguageSettings"
38 ofType:@"nib"];
39 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
40 profile_ = profile;
41
42 // Convert the name/size preference values to NSFont objects.
43 serifName_.Init(prefs::kWebKitSerifFontFamily, profile->GetPrefs(), NULL);
44 serifSize_.Init(prefs::kWebKitDefaultFontSize, profile->GetPrefs(), NULL);
45 NSString* serif = base::SysUTF8ToNSString(serifName_.GetValue());
46 serifFont_.reset(
47 [[NSFont fontWithName:serif size:serifSize_.GetValue()] retain]);
48
49 sansSerifName_.Init(prefs::kWebKitSansSerifFontFamily, profile->GetPrefs(),
50 NULL);
51 sansSerifSize_.Init(prefs::kWebKitDefaultFontSize, profile->GetPrefs(),
52 NULL);
53 NSString* sansSerif = base::SysUTF8ToNSString(sansSerifName_.GetValue());
54 sansSerifFont_.reset(
55 [[NSFont fontWithName:sansSerif
56 size:sansSerifSize_.GetValue()] retain]);
57
58 fixedWidthName_.Init(prefs::kWebKitFixedFontFamily, profile->GetPrefs(),
59 NULL);
60 fixedWidthSize_.Init(prefs::kWebKitDefaultFixedFontSize,
61 profile->GetPrefs(), NULL);
62 NSString* fixedWidth = base::SysUTF8ToNSString(fixedWidthName_.GetValue());
63 fixedWidthFont_.reset(
64 [[NSFont fontWithName:fixedWidth
65 size:fixedWidthSize_.GetValue()] retain]);
66
67 // Generate a list of encodings.
68 NSInteger count = CharacterEncoding::GetSupportCanonicalEncodingCount();
69 NSMutableArray* encodings = [NSMutableArray arrayWithCapacity:count];
70 for (NSInteger i = 0; i < count; ++i) {
71 int commandId = CharacterEncoding::GetEncodingCommandIdByIndex(i);
72 string16 name = CharacterEncoding::\
73 GetCanonicalEncodingDisplayNameByCommandId(commandId);
74 std::string encoding =
75 CharacterEncoding::GetCanonicalEncodingNameByCommandId(commandId);
76 NSDictionary* strings = [NSDictionary dictionaryWithObjectsAndKeys:
77 base::SysUTF16ToNSString(name), kCharacterInfoName,
78 base::SysUTF8ToNSString(encoding), kCharacterInfoEncoding,
79 [NSNumber numberWithInt:commandId], kCharacterInfoID,
80 nil
81 ];
82 [encodings addObject:strings];
83 }
84
85 // Sort the encodings.
86 scoped_nsobject<NSSortDescriptor> sorter(
87 [[NSSortDescriptor alloc] initWithKey:kCharacterInfoName
88 ascending:YES]);
89 NSArray* sorterArray = [NSArray arrayWithObject:sorter.get()];
90 encodings_.reset(
91 [[encodings sortedArrayUsingDescriptors:sorterArray] retain]);
92
93 // Find and set the default encoding.
94 defaultEncoding_.Init(prefs::kDefaultCharset, profile->GetPrefs(), NULL);
95 NSString* defaultEncoding =
96 base::SysUTF8ToNSString(defaultEncoding_.GetValue());
97 NSUInteger index = 0;
98 for (NSDictionary* entry in encodings_.get()) {
99 NSString* encoding = [entry objectForKey:kCharacterInfoEncoding];
100 if ([encoding isEqualToString:defaultEncoding]) {
101 defaultEncodingIndex_ = index;
102 break;
103 }
104 ++index;
105 }
106
107 // Register as a KVO observer so we can receive updates when the encoding
108 // changes.
109 [self addObserver:self
110 forKeyPath:@"defaultEncodingIndex_"
111 options:NSKeyValueObservingOptionNew
112 context:NULL];
113 }
114 return self;
115 }
116
117 - (void)dealloc {
118 [self removeObserver:self forKeyPath:@"defaultEncodingIndex_"];
119 [super dealloc];
120 }
121
122 - (void)awakeFromNib {
123 DCHECK([self window]);
124 [[self window] setDelegate:self];
125
126 // Set up the font display.
127 [self updateDisplayField:serifField_
128 withFont:serifFont_.get()
129 withLabel:serifLabel_];
130 [self updateDisplayField:sansSerifField_
131 withFont:sansSerifFont_.get()
132 withLabel:sansSerifLabel_];
133 [self updateDisplayField:fixedWidthField_
134 withFont:fixedWidthFont_.get()
135 withLabel:fixedWidthLabel_];
136 }
137
138 - (void)windowWillClose:(NSNotification*)notif {
139 [self autorelease];
140 }
141
142 - (IBAction)selectFont:(id)sender {
143 if (sender == serifButton_) {
144 currentFont_ = serifFont_.get();
145 currentType_ = FontSettingSerif;
146 } else if (sender == sansSerifButton_) {
147 currentFont_ = sansSerifFont_.get();
148 currentType_ = FontSettingSansSerif;
149 } else if (sender == fixedWidthButton_) {
150 currentFont_ = fixedWidthFont_.get();
151 currentType_ = FontSettingFixed;
152 } else {
153 NOTREACHED();
154 }
155
156 // Validate whatever editing is currently happening.
157 if ([[self window] makeFirstResponder:nil]) {
158 NSFontManager* manager = [NSFontManager sharedFontManager];
159 [manager setTarget:self];
160 [manager setSelectedFont:currentFont_ isMultiple:NO];
161 [manager orderFrontFontPanel:self];
162 }
163 }
164
165 // Called by the font manager when the user has selected a new font. We should
166 // then persist those changes into the preference system.
167 - (void)changeFont:(id)fontManager {
168 switch (currentType_) {
169 case FontSettingSerif:
170 serifFont_.reset([[fontManager convertFont:serifFont_] retain]);
171 [self updateDisplayField:serifField_
172 withFont:serifFont_.get()
173 withLabel:serifLabel_];
174 changedSerif_ = YES;
175 break;
176 case FontSettingSansSerif:
177 sansSerifFont_.reset([[fontManager convertFont:sansSerifFont_] retain]);
178 [self updateDisplayField:sansSerifField_
179 withFont:sansSerifFont_.get()
180 withLabel:sansSerifLabel_];
181 changedSansSerif_ = YES;
182 break;
183 case FontSettingFixed:
184 fixedWidthFont_.reset(
185 [[fontManager convertFont:fixedWidthFont_] retain]);
186 [self updateDisplayField:fixedWidthField_
187 withFont:fixedWidthFont_.get()
188 withLabel:fixedWidthLabel_];
189 changedFixedWidth_ = YES;
190 break;
191 default:
192 NOTREACHED();
193 }
194 }
195
196 - (IBAction)closeSheet:(id)sender {
197 NSFontPanel* panel = [[NSFontManager sharedFontManager] fontPanel:NO];
198 [panel close];
199 [NSApp endSheet:[self window]];
200 }
201
202 - (IBAction)save:(id)sender {
203 if (changedSerif_) {
204 serifName_.SetValue(base::SysNSStringToUTF8([serifFont_ fontName]));
205 serifSize_.SetValue([serifFont_ pointSize]);
206 }
207 if (changedSansSerif_) {
208 sansSerifName_.SetValue(
209 base::SysNSStringToUTF8([sansSerifFont_ fontName]));
210 sansSerifSize_.SetValue([sansSerifFont_ pointSize]);
211 }
212 if (changedFixedWidth_) {
213 fixedWidthName_.SetValue(
214 base::SysNSStringToUTF8([fixedWidthFont_ fontName]));
215 fixedWidthSize_.SetValue([fixedWidthFont_ pointSize]);
216 }
217 if (changedEncoding_) {
218 NSDictionary* object = [encodings_ objectAtIndex:defaultEncodingIndex_];
219 NSString* newEncoding = [object objectForKey:kCharacterInfoEncoding];
220 std::string encoding = base::SysNSStringToUTF8(newEncoding);
221 defaultEncoding_.SetValue(encoding);
222 }
223 [self closeSheet:sender];
224 }
225
226 - (NSArray*)encodings {
227 return encodings_.get();
228 }
229
230 // KVO notification.
231 - (void)observeValueForKeyPath:(NSString*)keyPath
232 ofObject:(id)object
233 change:(NSDictionary*)change
234 context:(void*)context {
235 // If this is the default encoding, then set the flag to persist the value.
236 if ([keyPath isEqual:@"defaultEncodingIndex_"]) {
237 changedEncoding_ = YES;
238 return;
239 }
240
241 [super observeValueForKeyPath:keyPath
242 ofObject:object
243 change:change
244 context:context];
245 }
246
247 #pragma mark Private
248
249 // Set the baseline for the font field to be aligned with the baseline
250 // of its corresponding label.
251 - (NSPoint)getFontFieldOrigin:(NSTextField*)field
252 forLabel:(NSTextField*)label {
253 [field sizeToFit];
254 NSRect labelFrame = [label frame];
255 NSPoint newOrigin =
256 [[label superview] convertPoint:labelFrame.origin
257 toView:[field superview]];
258 newOrigin.x = 0; // Left-align font field.
259 newOrigin.y += [[field font] descender] - [[label font] descender];
260 return newOrigin;
261 }
262
263 // This will set the font on |field| to be |font|, and will set the string
264 // value to something human-readable.
265 - (void)updateDisplayField:(NSTextField*)field
266 withFont:(NSFont*)font
267 withLabel:(NSTextField*)label {
268 if (!font) {
269 // Something has gone really wrong. Don't make things worse by showing the
270 // user "(null)".
271 return;
272 }
273 [field setFont:font];
274 NSString* value =
275 [NSString stringWithFormat:@"%@, %g", [font fontName], [font pointSize]];
276 [field setStringValue:value];
277 [field setFrameOrigin:[self getFontFieldOrigin:field forLabel:label]];
278 }
279
280 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698