Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: content/shell/shell_js_dialog_mac.mm

Issue 9836127: Content shell: Javascript dialogs, first pass, just Mac for now. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "content/shell/shell_js_dialog.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #import "base/mac/cocoa_protocols.h"
10 #import "base/memory/scoped_nsobject.h"
11 #include "base/sys_string_conversions.h"
12 #include "content/shell/shell_js_dialog_creator.h"
13
14 // Helper object that receives the notification that the dialog/sheet is
15 // going away. Is responsible for cleaning itself up.
16 @interface ShellJavaScriptDialogHelper : NSObject<NSAlertDelegate> {
17 @private
18 scoped_nsobject<NSAlert> alert_;
19 NSTextField* textField_; // WEAK; owned by alert_
20
21 // Copies of the fields in ShellJavaScriptDialog because they're private.
22 content::ShellJavaScriptDialogCreator* creator_;
23 content::JavaScriptDialogCreator::DialogClosedCallback callback_;
24 }
25
26 - (id)initHelperWithCreator:(content::ShellJavaScriptDialogCreator*)creator
27 andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback;
28 - (NSAlert*)alert;
29 - (NSTextField*)textField;
30 - (void)alertDidEnd:(NSAlert*)alert
31 returnCode:(int)returnCode
32 contextInfo:(void*)contextInfo;
33 - (void)cancel;
34
35 @end
36
37 @implementation ShellJavaScriptDialogHelper
38
39 - (id)initHelperWithCreator:(content::ShellJavaScriptDialogCreator*)creator
40 andCallback:(content::JavaScriptDialogCreator::DialogClosedCallback)callback {
41 if (self = [super init]) {
42 creator_ = creator;
43 callback_ = callback;
44 }
45
46 return self;
47 }
48
49 - (NSAlert*)alert {
50 alert_.reset([[NSAlert alloc] init]);
51 return alert_;
52 }
53
54 - (NSTextField*)textField {
55 textField_ = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 22)];
56 [[textField_ cell] setLineBreakMode:NSLineBreakByTruncatingTail];
57 [alert_ setAccessoryView:textField_];
58 [textField_ release];
59
60 return textField_;
61 }
62
63 - (void)alertDidEnd:(NSAlert*)alert
64 returnCode:(int)returnCode
65 contextInfo:(void*)contextInfo {
66 if (returnCode == NSRunStoppedResponse)
67 return;
68
69 bool success = returnCode == NSAlertFirstButtonReturn;
70 string16 input;
71 if (textField_)
72 input = base::SysNSStringToUTF16([textField_ stringValue]);
73
74 content::ShellJavaScriptDialog* native_dialog =
75 reinterpret_cast<content::ShellJavaScriptDialog*>(contextInfo);
76 callback_.Run(success, input);
77 creator_->DialogClosed(native_dialog);
78 }
79
80 - (void)cancel {
81 [NSApp endSheet:[alert_ window]];
82 alert_.reset();
83 }
84
85 @end
86
87 namespace content {
88
89 ShellJavaScriptDialog::ShellJavaScriptDialog(
90 ShellJavaScriptDialogCreator* creator,
91 ui::JavascriptMessageType javascript_message_type,
92 const string16& message_text,
93 const string16& default_prompt_text,
94 const JavaScriptDialogCreator::DialogClosedCallback& callback)
95 : creator_(creator),
96 callback_(callback) {
97 bool text_field =
98 javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT;
99 bool one_button =
100 javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_ALERT;
101
102 helper_ =
103 [[ShellJavaScriptDialogHelper alloc] initHelperWithCreator:creator
104 andCallback:callback];
105
106 // Show the modal dialog.
107 alert_ = [helper_ alert];
108 NSTextField* field = nil;
109 if (text_field) {
110 field = [helper_ textField];
111 [field setStringValue:base::SysUTF16ToNSString(default_prompt_text)];
112 }
113 [alert_ setDelegate:helper_];
114 [alert_ setInformativeText:base::SysUTF16ToNSString(message_text)];
115 [alert_ setMessageText:@"Javascript alert"];
116 [alert_ addButtonWithTitle:@"OK"];
117 if (!one_button) {
118 NSButton* other = [alert_ addButtonWithTitle:@"Cancel"];
119 [other setKeyEquivalent:@"\e"];
120 }
121
122 [alert_
123 beginSheetModalForWindow:nil // nil here makes it app-modal
124 modalDelegate:helper_
125 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
126 contextInfo:this];
127
128 if ([alert_ accessoryView])
129 [[alert_ window] makeFirstResponder:[alert_ accessoryView]];
130 }
131
132 ShellJavaScriptDialog::~ShellJavaScriptDialog() {
133 [helper_ release];
134 }
135
136 void ShellJavaScriptDialog::Cancel() {
137 [helper_ cancel];
138 }
139
140 } // namespace content
OLDNEW
« content/shell/shell_js_dialog.h ('K') | « content/shell/shell_js_dialog_creator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698