1 // Copyright (c) 2009 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 #include "chrome/browser/ui/cocoa/tab_contents/sad_tab_view.h" 6 7 #include "base/logging.h" 8 #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" 9 #include "grit/theme_resources.h" 10 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" 11 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/gfx/image.h" 13 14 // Offset above vertical middle of page where contents of page start. 15 static const CGFloat kSadTabOffset = -64; 16 // Padding between icon and title. 17 static const CGFloat kIconTitleSpacing = 20; 18 // Padding between title and message. 19 static const CGFloat kTitleMessageSpacing = 15; 20 // Padding between message and link. 21 static const CGFloat kMessageLinkSpacing = 15; 22 // Paddings on left and right of page. 23 static const CGFloat kTabHorzMargin = 13; 24 25 @implementation SadTabView 26 27 - (void)awakeFromNib { 28 // Load resource for image and set it. 29 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 30 NSImage* image = rb.GetNativeImageNamed(IDR_SAD_TAB); 31 DCHECK(image); 32 [image_ setImage:image]; 33 34 // Set font for title. 35 NSFont* titleFont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]]; 36 [title_ setFont:titleFont]; 37 38 // Set font for message. 39 NSFont* messageFont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; 40 [message_ setFont:messageFont]; 41 42 // If necessary, set font and color for link. 43 if (linkButton_) { 44 [linkButton_ setFont:messageFont]; 45 [linkCell_ setTextColor:[NSColor whiteColor]]; 46 } 47 48 // Initialize background color. 49 NSColor* backgroundColor = [[NSColor colorWithCalibratedRed:(35.0f/255.0f) 50 green:(48.0f/255.0f) 51 blue:(64.0f/255.0f) 52 alpha:1.0] retain]; 53 backgroundColor_.reset(backgroundColor); 54 } 55 56 - (void)drawRect:(NSRect)dirtyRect { 57 // Paint background. 58 [backgroundColor_ set]; 59 NSRectFill(dirtyRect); 60 } 61 62 - (void)resizeSubviewsWithOldSize:(NSSize)oldSize { 63 NSRect newBounds = [self bounds]; 64 CGFloat maxWidth = NSWidth(newBounds) - (kTabHorzMargin * 2); 65 BOOL callSizeToFit = (messageSize_.width == 0); 66 67 // Set new frame origin for image. 68 NSRect iconFrame = [image_ frame]; 69 CGFloat iconX = (maxWidth - NSWidth(iconFrame)) / 2; 70 CGFloat iconY = 71 MIN(((NSHeight(newBounds) - NSHeight(iconFrame)) / 2) - kSadTabOffset, 72 NSHeight(newBounds) - NSHeight(iconFrame)); 73 iconX = floor(iconX); 74 iconY = floor(iconY); 75 [image_ setFrameOrigin:NSMakePoint(iconX, iconY)]; 76 77 // Set new frame origin for title. 78 if (callSizeToFit) 79 [title_ sizeToFit]; 80 NSRect titleFrame = [title_ frame]; 81 CGFloat titleX = (maxWidth - NSWidth(titleFrame)) / 2; 82 CGFloat titleY = iconY - kIconTitleSpacing - NSHeight(titleFrame); 83 [title_ setFrameOrigin:NSMakePoint(titleX, titleY)]; 84 85 // Set new frame for message, wrapping or unwrapping the text if necessary. 86 if (callSizeToFit) { 87 [message_ sizeToFit]; 88 messageSize_ = [message_ frame].size; 89 } 90 NSRect messageFrame = [message_ frame]; 91 if (messageSize_.width > maxWidth) { // Need to wrap message. 92 [message_ setFrameSize:NSMakeSize(maxWidth, messageSize_.height)]; 93 CGFloat heightChange = 94 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:message_]; 95 messageFrame.size.width = maxWidth; 96 messageFrame.size.height = messageSize_.height + heightChange; 97 messageFrame.origin.x = kTabHorzMargin; 98 } else { 99 if (!callSizeToFit) { 100 [message_ sizeToFit]; 101 messageFrame = [message_ frame]; 102 } 103 messageFrame.origin.x = (maxWidth - NSWidth(messageFrame)) / 2; 104 } 105 messageFrame.origin.y = 106 titleY - kTitleMessageSpacing - NSHeight(messageFrame); 107 [message_ setFrame:messageFrame]; 108 109 if (linkButton_) { 110 if (callSizeToFit) 111 [linkButton_ sizeToFit]; 112 // Set new frame origin for link. 113 NSRect linkFrame = [linkButton_ frame]; 114 CGFloat linkX = (maxWidth - NSWidth(linkFrame)) / 2; 115 CGFloat linkY = 116 NSMinY(messageFrame) - kMessageLinkSpacing - NSHeight(linkFrame); 117 [linkButton_ setFrameOrigin:NSMakePoint(linkX, linkY)]; 118 } 119 } 120 121 - (void)removeLinkButton { 122 if (linkButton_) { 123 [linkButton_ removeFromSuperview]; 124 linkButton_ = nil; 125 } 126 } 127 128 @end 129