OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/mac/mac_util.h" | 5 #include "base/mac/mac_util.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
8 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | 8 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
9 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 9 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
10 #import "chrome/browser/ui/cocoa/validation_message_bubble_controller.h" | 10 #import "chrome/browser/ui/cocoa/validation_message_bubble_controller.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 const CGFloat kIconTextMargin = 4; | 25 const CGFloat kIconTextMargin = 4; |
26 const CGFloat kTextVerticalMargin = 4; | 26 const CGFloat kTextVerticalMargin = 4; |
27 | 27 |
28 @implementation ValidationMessageBubbleController | 28 @implementation ValidationMessageBubbleController |
29 | 29 |
30 - (id)init:(NSWindow*)parentWindow | 30 - (id)init:(NSWindow*)parentWindow |
31 anchoredAt:(NSPoint)anchorPoint | 31 anchoredAt:(NSPoint)anchorPoint |
32 mainText:(const string16&)mainText | 32 mainText:(const string16&)mainText |
33 subText:(const string16&)subText { | 33 subText:(const string16&)subText { |
34 | 34 |
35 scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc] | 35 base::scoped_nsobject<InfoBubbleWindow> window( |
36 initWithContentRect: | 36 [[InfoBubbleWindow alloc] initWithContentRect: |
37 NSMakeRect(0, 0, kWindowInitialWidth, kWindowInitialHeight) | 37 NSMakeRect(0, 0, kWindowInitialWidth, kWindowInitialHeight) |
38 styleMask:NSBorderlessWindowMask | 38 styleMask:NSBorderlessWindowMask |
39 backing:NSBackingStoreBuffered | 39 backing:NSBackingStoreBuffered |
40 defer:NO]); | 40 defer:NO]); |
41 if ((self = [super initWithWindow:window.get() | 41 if ((self = [super initWithWindow:window.get() |
42 parentWindow:parentWindow | 42 parentWindow:parentWindow |
43 anchoredAt:anchorPoint])) { | 43 anchoredAt:anchorPoint])) { |
44 [[self bubble] setArrowLocation:info_bubble::kTopLeft]; | 44 [[self bubble] setArrowLocation:info_bubble::kTopLeft]; |
45 self.shouldOpenAsKeyWindow = NO; | 45 self.shouldOpenAsKeyWindow = NO; |
46 | 46 |
47 NSView* contentView = [ValidationMessageBubbleController | 47 NSView* contentView = [ValidationMessageBubbleController |
48 constructContentView:mainText subText:subText]; | 48 constructContentView:mainText subText:subText]; |
49 [[window contentView] addSubview:contentView]; | 49 [[window contentView] addSubview:contentView]; |
50 NSRect contentFrame = [contentView frame]; | 50 NSRect contentFrame = [contentView frame]; |
51 NSRect windowFrame = [window frame]; | 51 NSRect windowFrame = [window frame]; |
52 windowFrame.size.width = NSWidth(contentFrame) + kWindowPadding * 2; | 52 windowFrame.size.width = NSWidth(contentFrame) + kWindowPadding * 2; |
53 windowFrame.size.height = NSHeight(contentFrame) + kWindowPadding * 2 | 53 windowFrame.size.height = NSHeight(contentFrame) + kWindowPadding * 2 |
54 + info_bubble::kBubbleArrowHeight; | 54 + info_bubble::kBubbleArrowHeight; |
55 [window setFrame:windowFrame display:nil]; | 55 [window setFrame:windowFrame display:nil]; |
56 | 56 |
57 [self showWindow:nil]; | 57 [self showWindow:nil]; |
58 } | 58 } |
59 return self; | 59 return self; |
60 } | 60 } |
61 | 61 |
62 + (NSView*)constructContentView:(const string16&)mainText | 62 + (NSView*)constructContentView:(const string16&)mainText |
63 subText:(const string16&)subText { | 63 subText:(const string16&)subText { |
64 NSRect contentFrame = NSMakeRect(kWindowPadding, kWindowPadding, 0, 0); | 64 NSRect contentFrame = NSMakeRect(kWindowPadding, kWindowPadding, 0, 0); |
65 FlippedView* contentView = [[FlippedView alloc] initWithFrame:contentFrame]; | 65 FlippedView* contentView = [[FlippedView alloc] initWithFrame:contentFrame]; |
66 | 66 |
67 NSImage* image = ResourceBundle::GetSharedInstance() | 67 NSImage* image = ResourceBundle::GetSharedInstance() |
68 .GetNativeImageNamed(IDR_INPUT_ALERT).ToNSImage(); | 68 .GetNativeImageNamed(IDR_INPUT_ALERT).ToNSImage(); |
69 scoped_nsobject<NSImageView> imageView([[NSImageView alloc] | 69 base::scoped_nsobject<NSImageView> imageView([[NSImageView alloc] |
70 initWithFrame:NSMakeRect(0, 0, image.size.width, image.size.height)]); | 70 initWithFrame:NSMakeRect(0, 0, image.size.width, image.size.height)]); |
71 [imageView setImageFrameStyle:NSImageFrameNone]; | 71 [imageView setImageFrameStyle:NSImageFrameNone]; |
72 [imageView setImage:image]; | 72 [imageView setImage:image]; |
73 [contentView addSubview:imageView]; | 73 [contentView addSubview:imageView]; |
74 contentFrame.size.height = image.size.height; | 74 contentFrame.size.height = image.size.height; |
75 | 75 |
76 const CGFloat textX = NSWidth([imageView frame]) + kIconTextMargin; | 76 const CGFloat textX = NSWidth([imageView frame]) + kIconTextMargin; |
77 NSRect textFrame = NSMakeRect(textX, 0, NSWidth(contentFrame) - textX, 0); | 77 NSRect textFrame = NSMakeRect(textX, 0, NSWidth(contentFrame) - textX, 0); |
78 scoped_nsobject<NSTextField> text( | 78 base::scoped_nsobject<NSTextField> text( |
79 [[NSTextField alloc] initWithFrame:textFrame]); | 79 [[NSTextField alloc] initWithFrame:textFrame]); |
80 [text setStringValue:base::SysUTF16ToNSString(mainText)]; | 80 [text setStringValue:base::SysUTF16ToNSString(mainText)]; |
81 [text setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; | 81 [text setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; |
82 [text setEditable:NO]; | 82 [text setEditable:NO]; |
83 [text setBezeled:NO]; | 83 [text setBezeled:NO]; |
84 const CGFloat minTextWidth = kWindowMinWidth - kWindowPadding * 2 - textX; | 84 const CGFloat minTextWidth = kWindowMinWidth - kWindowPadding * 2 - textX; |
85 const CGFloat maxTextWidth = kWindowMaxWidth - kWindowPadding * 2 - textX; | 85 const CGFloat maxTextWidth = kWindowMaxWidth - kWindowPadding * 2 - textX; |
86 [text sizeToFit]; | 86 [text sizeToFit]; |
87 [contentView addSubview:text]; | 87 [contentView addSubview:text]; |
88 textFrame = [text frame]; | 88 textFrame = [text frame]; |
89 if (NSWidth(textFrame) < minTextWidth) { | 89 if (NSWidth(textFrame) < minTextWidth) { |
90 textFrame.size.width = minTextWidth; | 90 textFrame.size.width = minTextWidth; |
91 [text setFrame:textFrame]; | 91 [text setFrame:textFrame]; |
92 } else if (NSWidth(textFrame) > maxTextWidth) { | 92 } else if (NSWidth(textFrame) > maxTextWidth) { |
93 textFrame.size.width = maxTextWidth; | 93 textFrame.size.width = maxTextWidth; |
94 textFrame.size.height = 0; | 94 textFrame.size.height = 0; |
95 [text setFrame:textFrame]; | 95 [text setFrame:textFrame]; |
96 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:text]; | 96 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:text]; |
97 textFrame = [text frame]; | 97 textFrame = [text frame]; |
98 } | 98 } |
99 contentFrame.size.width = NSMaxX(textFrame); | 99 contentFrame.size.width = NSMaxX(textFrame); |
100 contentFrame.size.height = | 100 contentFrame.size.height = |
101 std::max(NSHeight(contentFrame), NSHeight(textFrame)); | 101 std::max(NSHeight(contentFrame), NSHeight(textFrame)); |
102 | 102 |
103 if (!subText.empty()) { | 103 if (!subText.empty()) { |
104 NSRect subTextFrame = NSMakeRect( | 104 NSRect subTextFrame = NSMakeRect( |
105 textX, NSMaxY(textFrame) + kTextVerticalMargin, | 105 textX, NSMaxY(textFrame) + kTextVerticalMargin, |
106 NSWidth(textFrame), 0); | 106 NSWidth(textFrame), 0); |
107 scoped_nsobject<NSTextField> text2( | 107 base::scoped_nsobject<NSTextField> text2( |
108 [[NSTextField alloc] initWithFrame:subTextFrame]); | 108 [[NSTextField alloc] initWithFrame:subTextFrame]); |
109 [text2 setStringValue:base::SysUTF16ToNSString(subText)]; | 109 [text2 setStringValue:base::SysUTF16ToNSString(subText)]; |
110 [text2 setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | 110 [text2 setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
111 [text2 setEditable:NO]; | 111 [text2 setEditable:NO]; |
112 [text2 setBezeled:NO]; | 112 [text2 setBezeled:NO]; |
113 [text2 sizeToFit]; | 113 [text2 sizeToFit]; |
114 subTextFrame = [text2 frame]; | 114 subTextFrame = [text2 frame]; |
115 if (NSWidth(subTextFrame) > maxTextWidth) { | 115 if (NSWidth(subTextFrame) > maxTextWidth) { |
116 subTextFrame.size.width = maxTextWidth; | 116 subTextFrame.size.width = maxTextWidth; |
117 [text2 setFrame:subTextFrame]; | 117 [text2 setFrame:subTextFrame]; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 } | 167 } |
168 | 168 |
169 virtual void SetPositionRelativeToAnchor( | 169 virtual void SetPositionRelativeToAnchor( |
170 content::RenderWidgetHost* widget_host, | 170 content::RenderWidgetHost* widget_host, |
171 const gfx::Rect& anchor_in_root_view) OVERRIDE { | 171 const gfx::Rect& anchor_in_root_view) OVERRIDE { |
172 [controller_.get() | 172 [controller_.get() |
173 setAnchorPoint:GetAnchorPoint(widget_host, anchor_in_root_view)]; | 173 setAnchorPoint:GetAnchorPoint(widget_host, anchor_in_root_view)]; |
174 } | 174 } |
175 | 175 |
176 private: | 176 private: |
177 scoped_nsobject<ValidationMessageBubbleController> controller_; | 177 base::scoped_nsobject<ValidationMessageBubbleController> controller_; |
178 }; | 178 }; |
179 | 179 |
180 } | 180 } |
181 | 181 |
182 // ---------------------------------------------------------------- | 182 // ---------------------------------------------------------------- |
183 | 183 |
184 namespace chrome { | 184 namespace chrome { |
185 | 185 |
186 scoped_ptr<ValidationMessageBubble> ValidationMessageBubble::CreateAndShow( | 186 scoped_ptr<ValidationMessageBubble> ValidationMessageBubble::CreateAndShow( |
187 content::RenderWidgetHost* widget_host, | 187 content::RenderWidgetHost* widget_host, |
188 const gfx::Rect& anchor_in_root_view, | 188 const gfx::Rect& anchor_in_root_view, |
189 const string16& main_text, | 189 const string16& main_text, |
190 const string16& sub_text) { | 190 const string16& sub_text) { |
191 return scoped_ptr<ValidationMessageBubble>(new ValidationMessageBubbleCocoa( | 191 return scoped_ptr<ValidationMessageBubble>(new ValidationMessageBubbleCocoa( |
192 widget_host, anchor_in_root_view, main_text, sub_text)).Pass(); | 192 widget_host, anchor_in_root_view, main_text, sub_text)).Pass(); |
193 } | 193 } |
194 | 194 |
195 } | 195 } |
OLD | NEW |