| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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_modal_dialog_mac.h" |
| 6 |
| 7 #include "base/memory/scoped_nsobject.h" |
| 8 #include "chrome/browser/ui/tab_modal_dialog_delegate.h" |
| 9 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 10 #include "ui/base/l10n/l10n_util_mac.h" |
| 11 #include "ui/gfx/image/image.h" |
| 12 |
| 13 // The delegate of the NSAlert used to display the dialog. Forwards the alert's |
| 14 // completion event to the C++ class |TabModalDialogDelegate|. |
| 15 @interface TabModalDialogMacBridge : NSObject { |
| 16 TabModalDialogDelegate* delegate_; // weak |
| 17 } |
| 18 - (id)initWithDelegate:(TabModalDialogDelegate*)delegate; |
| 19 - (void)alertDidEnd:(NSAlert*)alert |
| 20 returnCode:(int)returnCode |
| 21 contextInfo:(void*)contextInfo; |
| 22 @end |
| 23 |
| 24 @implementation TabModalDialogMacBridge |
| 25 - (id)initWithDelegate:(TabModalDialogDelegate*)delegate { |
| 26 if ((self = [super init])) { |
| 27 delegate_ = delegate; |
| 28 } |
| 29 return self; |
| 30 } |
| 31 |
| 32 - (void)alertDidEnd:(NSAlert*)alert |
| 33 returnCode:(int)returnCode |
| 34 contextInfo:(void*)contextInfo { |
| 35 if (returnCode == NSAlertFirstButtonReturn) { |
| 36 delegate_->Accept(); |
| 37 } else { |
| 38 delegate_->Cancel(); |
| 39 } |
| 40 } |
| 41 @end |
| 42 |
| 43 namespace browser { |
| 44 |
| 45 // Declared in browser_dialogs.h so others don't have to depend on our header. |
| 46 void ShowTabModalDialog(TabModalDialogDelegate* delegate, |
| 47 gfx::NativeWindow parent_window, |
| 48 TabContents* tab_contents) { |
| 49 new TabModalDialogMac(parent_window, delegate, tab_contents); |
| 50 } |
| 51 |
| 52 } |
| 53 |
| 54 TabModalDialogMac::TabModalDialogMac( |
| 55 NSWindow* parent, |
| 56 TabModalDialogDelegate* delegate, |
| 57 TabContents* tab_contents) |
| 58 : ConstrainedWindowMacDelegateSystemSheet( |
| 59 [[[TabModalDialogMacBridge alloc] initWithDelegate:delegate] |
| 60 autorelease], |
| 61 @selector(alertDidEnd:returnCode:contextInfo:)), |
| 62 delegate_(delegate) { |
| 63 scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]); |
| 64 [alert setMessageText: |
| 65 l10n_util::FixUpWindowsStyleLabel(delegate->GetTitle())]; |
| 66 [alert setInformativeText: |
| 67 l10n_util::FixUpWindowsStyleLabel(delegate->GetMessage())]; |
| 68 [alert addButtonWithTitle: |
| 69 l10n_util::FixUpWindowsStyleLabel(delegate->GetAcceptButtonTitle())]; |
| 70 [alert addButtonWithTitle: |
| 71 l10n_util::FixUpWindowsStyleLabel(delegate->GetCancelButtonTitle())]; |
| 72 gfx::Image* icon = delegate->GetIcon(); |
| 73 if (icon) |
| 74 [alert setIcon:icon->ToNSImage()]; |
| 75 |
| 76 set_sheet(alert); |
| 77 |
| 78 TabContentsWrapper* wrapper = |
| 79 TabContentsWrapper::GetCurrentWrapperForContents(tab_contents); |
| 80 delegate->set_window(new ConstrainedWindowMac(wrapper, this)); |
| 81 } |
| 82 |
| 83 TabModalDialogMac::~TabModalDialogMac() { |
| 84 NSWindow* window = [(NSAlert*)sheet() window]; |
| 85 if (window && is_sheet_open()) { |
| 86 [NSApp endSheet:window |
| 87 returnCode:NSAlertSecondButtonReturn]; |
| 88 } |
| 89 } |
| 90 |
| 91 void TabModalDialogMac::DeleteDelegate() { |
| 92 delete this; |
| 93 } |
| OLD | NEW |