1 // Copyright (c) 2012 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/global_error_bubble_controller.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/search_engines/util.h"
13 #import "chrome/browser/ui/browser.h"
14 #import "chrome/browser/ui/browser_window.h"
15 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
16 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
17 #import "chrome/browser/ui/cocoa/l10n_util.h"
18 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
19 #import "chrome/browser/ui/cocoa/wrench_menu/wrench_menu_controller.h"
20 #include "chrome/browser/ui/global_error/global_error.h"
21 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
22 #include "chrome/browser/ui/global_error/global_error_service.h"
23 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
24 #include "grit/generated_resources.h"
25 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/gfx/image/image.h"
30 const CGFloat kParagraphSpacing = 6;
33 namespace GlobalErrorBubbleControllerInternal {
35 // This is the bridge to the C++ GlobalErrorBubbleViewBase object.
36 class Bridge : public GlobalErrorBubbleViewBase {
38 Bridge(GlobalErrorBubbleController* controller) : controller_(controller) {
42 virtual void CloseBubbleView() OVERRIDE {
46 GlobalErrorBubbleController* controller_; // Weak, owns this.
49 } // namespace GlobalErrorBubbleControllerInternal
51 @implementation GlobalErrorBubbleController
53 + (GlobalErrorBubbleViewBase*)showForBrowser:(Browser*)browser
54 error:(const base::WeakPtr<GlobalErrorWithStandardBubble>&)error {
55 NSWindow* parentWindow = browser->window()->GetNativeWindow();
56 BrowserWindowController* bwc = [BrowserWindowController
57 browserWindowControllerForWindow:parentWindow];
58 NSView* wrenchButton = [[bwc toolbarController] wrenchButton];
59 NSPoint offset = NSMakePoint(
60 NSMidX([wrenchButton bounds]),
61 wrench_menu_controller::kWrenchBubblePointOffsetY);
63 // The bubble will be automatically deleted when the window is closed.
64 GlobalErrorBubbleController* bubble = [[GlobalErrorBubbleController alloc]
65 initWithWindowNibPath:@"GlobalErrorBubble"
66 relativeToView:wrenchButton
68 bubble->error_ = error;
69 bubble->bridge_.reset(new GlobalErrorBubbleControllerInternal::Bridge(
71 bubble->browser_ = browser;
72 [bubble showWindow:nil];
74 return bubble->bridge_.get();
77 - (void)awakeFromNib {
82 gfx::Image image = error_->GetBubbleViewIcon();
83 DCHECK(!image.IsEmpty());
84 [iconView_ setImage:image.ToNSImage()];
86 [title_ setStringValue:SysUTF16ToNSString(error_->GetBubbleViewTitle())];
87 std::vector<base::string16> messages = error_->GetBubbleViewMessages();
88 base::string16 message = JoinString(messages, '\n');
90 base::scoped_nsobject<NSMutableAttributedString> messageValue(
91 [[NSMutableAttributedString alloc]
92 initWithString:SysUTF16ToNSString(message)]);
93 base::scoped_nsobject<NSMutableParagraphStyle> style(
94 [[NSMutableParagraphStyle alloc] init]);
95 [style setParagraphSpacing:kParagraphSpacing];
96 [messageValue addAttribute:NSParagraphStyleAttributeName
98 range:NSMakeRange(0, [messageValue length])];
100 [message_ setAttributedStringValue:messageValue];
102 [acceptButton_ setTitle:
103 SysUTF16ToNSString(error_->GetBubbleViewAcceptButtonLabel())];
104 base::string16 cancelLabel = error_->GetBubbleViewCancelButtonLabel();
105 if (cancelLabel.empty())
106 [cancelButton_ setHidden:YES];
108 [cancelButton_ setTitle:SysUTF16ToNSString(cancelLabel)];
110 // First make sure that the window is wide enough to accommodate the buttons.
111 NSRect frame = [[self window] frame];
112 [layoutTweaker_ tweakUI:buttonContainer_];
113 CGFloat delta = NSWidth([buttonContainer_ frame]) - NSWidth(frame);
115 frame.size.width += delta;
116 [[self window] setFrame:frame display:NO];
119 // Adapt window height to bottom buttons. Do this before all other layouting.
120 NSArray* views = [NSArray arrayWithObjects:
121 title_, message_, buttonContainer_, nil];
122 NSSize ds = NSMakeSize(0, cocoa_l10n_util::VerticallyReflowGroup(views));
123 ds = [[self bubble] convertSize:ds toView:nil];
125 frame.origin.y -= ds.height;
126 frame.size.height += ds.height;
127 [[self window] setFrame:frame display:YES];
130 - (void)showWindow:(id)sender {
131 BrowserWindowController* bwc = [BrowserWindowController
132 browserWindowControllerForWindow:[self parentWindow]];
133 [bwc lockBarVisibilityForOwner:self withAnimation:NO delay:NO];
134 [super showWindow:sender];
139 error_->BubbleViewDidClose(browser_);
141 BrowserWindowController* bwc = [BrowserWindowController
142 browserWindowControllerForWindow:[self parentWindow]];
143 [bwc releaseBarVisibilityForOwner:self withAnimation:YES delay:NO];
147 - (IBAction)onAccept:(id)sender {
149 error_->BubbleViewAcceptButtonPressed(browser_);
153 - (IBAction)onCancel:(id)sender {
155 error_->BubbleViewCancelButtonPressed(browser_);
161 GlobalErrorBubbleViewBase* GlobalErrorBubbleViewBase::ShowStandardBubbleView(
163 const base::WeakPtr<GlobalErrorWithStandardBubble>& error) {
164 return [GlobalErrorBubbleController showForBrowser:browser error:error];