Home | History | Annotate | Download | only in cocoa
      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/repost_form_warning_mac.h"
      6 
      7 #include "base/memory/scoped_nsobject.h"
      8 #include "chrome/browser/repost_form_warning_controller.h"
      9 #include "grit/generated_resources.h"
     10 #include "ui/base/l10n/l10n_util_mac.h"
     11 
     12 // The delegate of the NSAlert used to display the dialog. Forwards the alert's
     13 // completion event to the C++ class |RepostFormWarningController|.
     14 @interface RepostDelegate : NSObject {
     15   RepostFormWarningController* warning_;  // weak
     16 }
     17 - (id)initWithWarning:(RepostFormWarningController*)warning;
     18 - (void)alertDidEnd:(NSAlert*)alert
     19          returnCode:(int)returnCode
     20         contextInfo:(void*)contextInfo;
     21 @end
     22 
     23 @implementation RepostDelegate
     24 - (id)initWithWarning:(RepostFormWarningController*)warning {
     25   if ((self = [super init])) {
     26     warning_ = warning;
     27   }
     28   return self;
     29 }
     30 
     31 - (void)alertDidEnd:(NSAlert*)alert
     32          returnCode:(int)returnCode
     33         contextInfo:(void*)contextInfo {
     34   if (returnCode == NSAlertFirstButtonReturn) {
     35     warning_->Continue();
     36   } else {
     37     warning_->Cancel();
     38   }
     39 }
     40 @end
     41 
     42 RepostFormWarningMac* RepostFormWarningMac::Create(NSWindow* parent,
     43                                                   TabContents* tab_contents) {
     44   return new RepostFormWarningMac(
     45       parent,
     46       new RepostFormWarningController(tab_contents));
     47 }
     48 
     49 RepostFormWarningMac::RepostFormWarningMac(
     50     NSWindow* parent,
     51     RepostFormWarningController* controller)
     52     : ConstrainedWindowMacDelegateSystemSheet(
     53         [[[RepostDelegate alloc] initWithWarning:controller]
     54             autorelease],
     55         @selector(alertDidEnd:returnCode:contextInfo:)),
     56       controller_(controller) {
     57   scoped_nsobject<NSAlert> alert([[NSAlert alloc] init]);
     58   [alert setMessageText:
     59       l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_TITLE)];
     60   [alert setInformativeText:
     61       l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING)];
     62   [alert addButtonWithTitle:
     63       l10n_util::GetNSStringWithFixup(IDS_HTTP_POST_WARNING_RESEND)];
     64   [alert addButtonWithTitle:
     65       l10n_util::GetNSStringWithFixup(IDS_CANCEL)];
     66 
     67   set_sheet(alert);
     68 
     69   controller->Show(this);
     70 }
     71 
     72 RepostFormWarningMac::~RepostFormWarningMac() {
     73   NSWindow* window = [(NSAlert*)sheet() window];
     74   if (window && is_sheet_open()) {
     75     [NSApp endSheet:window
     76          returnCode:NSAlertSecondButtonReturn];
     77   }
     78 }
     79 
     80 void RepostFormWarningMac::DeleteDelegate() {
     81   delete this;
     82 }
     83