OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/app_modal_dialog.h" | 5 #include "chrome/browser/app_modal_dialog.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "app/l10n_util_mac.h" | |
10 #include "app/message_box_flags.h" | |
11 #import "base/cocoa_protocols_mac.h" | |
12 #include "base/sys_string_conversions.h" | |
13 #include "grit/app_strings.h" | |
14 #include "grit/generated_resources.h" | |
15 | |
16 // Helper object that receives the notification that the dialog/sheet is | |
17 // going away. Is responsible for cleaning itself up. | |
18 @interface AppModalDialogHelper : NSObject<NSAlertDelegate> { | |
19 @private | |
20 NSAlert* alert_; | |
21 NSTextField* textField_; // WEAK; owned by alert_ | |
22 } | |
23 | |
24 - (NSAlert*)alert; | |
25 - (NSTextField*)textField; | |
26 - (void)alertDidEnd:(NSAlert *)alert | |
27 returnCode:(int)returnCode | |
28 contextInfo:(void*)contextInfo; | |
29 | |
30 @end | |
31 | |
32 @implementation AppModalDialogHelper | |
33 | |
34 - (NSAlert*)alert { | |
35 alert_ = [[NSAlert alloc] init]; | |
36 return alert_; | |
37 } | |
38 | |
39 - (NSTextField*)textField { | |
40 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)]; | |
41 [alert_ setAccessoryView:textField_]; | |
42 [textField_ release]; | |
43 | |
44 return textField_; | |
45 } | |
46 | |
47 - (void)dealloc { | |
48 [alert_ release]; | |
49 [super dealloc]; | |
50 } | |
51 | |
52 // |contextInfo| is the bridge back to the C++ AppModalDialog. When complete, | |
53 // autorelease to clean ourselves up. | |
54 - (void)alertDidEnd:(NSAlert*)alert | |
55 returnCode:(int)returnCode | |
56 contextInfo:(void*)contextInfo { | |
57 AppModalDialog* bridge = reinterpret_cast<AppModalDialog*>(contextInfo); | |
58 std::wstring input; | |
59 if (textField_) | |
60 input = base::SysNSStringToWide([textField_ stringValue]); | |
61 switch (returnCode) { | |
62 case NSAlertFirstButtonReturn: { // OK | |
63 bool shouldSuppress = false; | |
64 if ([alert showsSuppressionButton]) | |
65 shouldSuppress = [[alert suppressionButton] state] == NSOnState; | |
66 bridge->OnAccept(input, shouldSuppress); | |
67 break; | |
68 } | |
69 case NSAlertSecondButtonReturn: { // Cancel | |
70 bridge->OnCancel(); | |
71 break; | |
72 } | |
73 case NSRunStoppedResponse: { // Window was closed underneath us | |
74 // Need to call OnCancel() because there is some cleanup that needs | |
75 // to be done. It won't call back to the javascript since the | |
76 // AppModalDialog knows that the TabContents was destroyed. | |
77 bridge->OnCancel(); | |
78 break; | |
79 } | |
80 default: { | |
81 NOTREACHED(); | |
82 } | |
83 } | |
84 [self autorelease]; | |
85 delete bridge; // Done with the dialog, it needs be destroyed. | |
86 } | |
87 @end | |
88 | |
89 AppModalDialog::~AppModalDialog() { | 9 AppModalDialog::~AppModalDialog() { |
90 } | 10 } |
91 | 11 |
92 void AppModalDialog::CreateAndShowDialog() { | 12 void AppModalDialog::CreateAndShowDialog() { |
93 // Determine the names of the dialog buttons based on the flags. "Default" | 13 NOTIMPLEMENTED(); |
94 // is the OK button. "Other" is the cancel button. We don't use the | |
95 // "Alternate" button in NSRunAlertPanel. | |
96 NSString* default_button = l10n_util::GetNSStringWithFixup(IDS_APP_OK); | |
97 NSString* other_button = l10n_util::GetNSStringWithFixup(IDS_APP_CANCEL); | |
98 bool text_field = false; | |
99 bool one_button = false; | |
100 switch (dialog_flags_) { | |
101 case MessageBoxFlags::kIsJavascriptAlert: | |
102 one_button = true; | |
103 break; | |
104 case MessageBoxFlags::kIsJavascriptConfirm: | |
105 if (is_before_unload_dialog_) { | |
106 default_button = l10n_util::GetNSStringWithFixup( | |
107 IDS_BEFOREUNLOAD_MESSAGEBOX_OK_BUTTON_LABEL); | |
108 other_button = l10n_util::GetNSStringWithFixup( | |
109 IDS_BEFOREUNLOAD_MESSAGEBOX_CANCEL_BUTTON_LABEL); | |
110 } | |
111 break; | |
112 case MessageBoxFlags::kIsJavascriptPrompt: | |
113 text_field = true; | |
114 break; | |
115 | |
116 default: | |
117 NOTREACHED(); | |
118 } | |
119 | |
120 // Create a helper which will receive the sheet ended selector. It will | |
121 // delete itself when done. It doesn't need anything passed to its init | |
122 // as it will get a contextInfo parameter. | |
123 AppModalDialogHelper* helper = [[AppModalDialogHelper alloc] init]; | |
124 | |
125 // Show the modal dialog. | |
126 NSAlert* alert = [helper alert]; | |
127 dialog_ = alert; | |
128 NSTextField* field = nil; | |
129 if (text_field) { | |
130 field = [helper textField]; | |
131 [field setStringValue:base::SysWideToNSString(default_prompt_text_)]; | |
132 } | |
133 [alert setDelegate:helper]; | |
134 [alert setInformativeText:base::SysWideToNSString(message_text_)]; | |
135 [alert setMessageText:base::SysWideToNSString(title_)]; | |
136 [alert addButtonWithTitle:default_button]; | |
137 if (!one_button) | |
138 [alert addButtonWithTitle:other_button]; | |
139 if (display_suppress_checkbox_) { | |
140 [alert setShowsSuppressionButton:YES]; | |
141 NSString* suppression_title = l10n_util::GetNSStringWithFixup( | |
142 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | |
143 [[alert suppressionButton] setTitle:suppression_title]; | |
144 } | |
145 | |
146 [alert beginSheetModalForWindow:nil // nil here makes it app-modal | |
147 modalDelegate:helper | |
148 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) | |
149 contextInfo:this]; | |
150 | |
151 if (field) | |
152 [[alert window] makeFirstResponder:field]; | |
153 } | 14 } |
154 | 15 |
155 void AppModalDialog::ActivateModalDialog() { | 16 void AppModalDialog::ActivateModalDialog() { |
156 NOTIMPLEMENTED(); | 17 NOTIMPLEMENTED(); |
157 } | 18 } |
158 | 19 |
159 void AppModalDialog::CloseModalDialog() { | 20 void AppModalDialog::CloseModalDialog() { |
160 NSAlert* alert = dialog_; | 21 NSAlert* alert = dialog_; |
161 DCHECK([alert isKindOfClass:[NSAlert class]]); | 22 DCHECK([alert isKindOfClass:[NSAlert class]]); |
162 [NSApp endSheet:[alert window]]; | 23 [NSApp endSheet:[alert window]]; |
163 dialog_ = nil; | 24 dialog_ = nil; |
164 } | 25 } |
165 | |
166 int AppModalDialog::GetDialogButtons() { | |
167 NOTIMPLEMENTED(); | |
168 return 0; | |
169 } | |
170 | |
171 void AppModalDialog::AcceptWindow() { | |
172 NOTIMPLEMENTED(); | |
173 } | |
174 | |
175 void AppModalDialog::CancelWindow() { | |
176 NOTIMPLEMENTED(); | |
177 } | |
OLD | NEW |