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/l10n_util.h" 6 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" 10 11namespace cocoa_l10n_util { 12 13NSInteger CompareFrameY(id view1, id view2, void* context) { 14 CGFloat y1 = NSMinY([view1 frame]); 15 CGFloat y2 = NSMinY([view2 frame]); 16 if (y1 < y2) 17 return NSOrderedAscending; 18 else if (y1 > y2) 19 return NSOrderedDescending; 20 else 21 return NSOrderedSame; 22} 23 24NSSize WrapOrSizeToFit(NSView* view) { 25 if ([view isKindOfClass:[NSTextField class]]) { 26 NSTextField* textField = static_cast<NSTextField*>(view); 27 if ([textField isEditable]) 28 return NSZeroSize; 29 CGFloat heightChange = 30 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField]; 31 return NSMakeSize(0.0, heightChange); 32 } 33 if ([view isKindOfClass:[NSMatrix class]]) { 34 NSMatrix* radioGroup = static_cast<NSMatrix*>(view); 35 [GTMUILocalizerAndLayoutTweaker wrapRadioGroupForWidth:radioGroup]; 36 return [GTMUILocalizerAndLayoutTweaker sizeToFitView:view]; 37 } 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]; 46 } 47 } 48 return [GTMUILocalizerAndLayoutTweaker sizeToFitView:view]; 49} 50 51CGFloat VerticallyReflowGroup(NSArray* views) { 52 views = [views sortedArrayUsingFunction:CompareFrameY 53 context:NULL]; 54 CGFloat localVerticalShift = 0; 55 for (NSInteger index = [views count] - 1; index >= 0; --index) { 56 NSView* view = [views objectAtIndex:index]; 57 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]; 64 } 65 } 66 return localVerticalShift; 67} 68 69NSString* ReplaceNSStringPlaceholders(NSString* formatString, 70 const base::string16& a, 71 size_t* offset) { 72 return base::SysUTF16ToNSString( 73 ReplaceStringPlaceholders(base::SysNSStringToUTF16(formatString), 74 a, 75 offset)); 76} 77 78NSString* TooltipForURLAndTitle(NSString* url, NSString* title) { 79 if ([title length] == 0) 80 return url; 81 else if ([url length] == 0 || [url isEqualToString:title]) 82 return title; 83 else 84 return [NSString stringWithFormat:@"%@\n%@", title, url]; 85} 86 87} // namespace cocoa_l10n_util 88