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.
5 #import "chrome/browser/ui/cocoa/l10n_util.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
11 namespace cocoa_l10n_util {
13 NSInteger CompareFrameY(id view1, id view2, void* context) {
14 CGFloat y1 = NSMinY([view1 frame]);
15 CGFloat y2 = NSMinY([view2 frame]);
17 return NSOrderedAscending;
19 return NSOrderedDescending;
24 NSSize WrapOrSizeToFit(NSView* view) {
25 if ([view isKindOfClass:[NSTextField class]]) {
26 NSTextField* textField = static_cast<NSTextField*>(view);
27 if ([textField isEditable])
29 CGFloat heightChange =
30 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField];
31 return NSMakeSize(0.0, heightChange);
33 if ([view isKindOfClass:[NSMatrix class]]) {
34 NSMatrix* radioGroup = static_cast<NSMatrix*>(view);
35 [GTMUILocalizerAndLayoutTweaker wrapRadioGroupForWidth:radioGroup];
36 return [GTMUILocalizerAndLayoutTweaker sizeToFitView:view];
38 if ([view isKindOfClass:[NSButton class]]) {
39 NSButton* button = static_cast<NSButton*>(view);
40 NSButtonCell* buttonCell = [button cell];
41 // Decide it's a checkbox via showsStateBy and highlightsBy.
42 if (([buttonCell showsStateBy] == NSCellState) &&
43 ([buttonCell highlightsBy] == NSCellState)) {
44 [GTMUILocalizerAndLayoutTweaker wrapButtonTitleForWidth:button];
45 return [GTMUILocalizerAndLayoutTweaker sizeToFitView:view];
48 return [GTMUILocalizerAndLayoutTweaker sizeToFitView:view];
51 CGFloat VerticallyReflowGroup(NSArray* views) {
52 views = [views sortedArrayUsingFunction:CompareFrameY
54 CGFloat localVerticalShift = 0;
55 for (NSInteger index = [views count] - 1; index >= 0; --index) {
56 NSView* view = [views objectAtIndex:index];
58 NSSize delta = WrapOrSizeToFit(view);
59 localVerticalShift += delta.height;
60 if (localVerticalShift) {
61 NSPoint origin = [view frame].origin;
62 origin.y -= localVerticalShift;
63 [view setFrameOrigin:origin];
66 return localVerticalShift;
69 NSString* ReplaceNSStringPlaceholders(NSString* formatString,
70 const base::string16& a,
72 return base::SysUTF16ToNSString(base::ReplaceStringPlaceholders(
73 base::SysNSStringToUTF16(formatString), a, offset));
76 NSString* TooltipForURLAndTitle(NSString* url, NSString* title) {
77 if ([title length] == 0)
79 else if ([url length] == 0 || [url isEqualToString:title])
82 return [NSString stringWithFormat:@"%@\n%@", title, url];
85 } // namespace cocoa_l10n_util