OLD | NEW |
(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_javascript_dialog.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "content/shell/resource.h" |
| 9 #include "content/shell/shell.h" |
| 10 #include "content/shell/shell_javascript_dialog_creator.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 class ShellJavaScriptDialog; |
| 15 |
| 16 INT_PTR CALLBACK ShellJavaScriptDialog::DialogProc(HWND dialog, UINT message, |
| 17 WPARAM wparam, |
| 18 LPARAM lparam) { |
| 19 switch (message) { |
| 20 case WM_INITDIALOG: { |
| 21 SetWindowLongPtr(dialog, DWL_USER, static_cast<LONG_PTR>(lparam)); |
| 22 ShellJavaScriptDialog* owner = |
| 23 reinterpret_cast<ShellJavaScriptDialog*>(lparam); |
| 24 owner->dialog_win_ = dialog; |
| 25 SetDlgItemText(dialog, IDC_DIALOGTEXT, owner->message_text_.c_str()); |
| 26 if (owner->message_type_ == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT) |
| 27 SetDlgItemText(dialog, IDC_PROMPTEDIT, |
| 28 owner->default_prompt_text_.c_str()); |
| 29 break; |
| 30 } |
| 31 case WM_DESTROY: { |
| 32 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
| 33 GetWindowLongPtr(dialog, DWL_USER)); |
| 34 if (owner->dialog_win_) { |
| 35 owner->dialog_win_ = 0; |
| 36 owner->callback_.Run(false, string16()); |
| 37 owner->creator_->DialogClosed(owner); |
| 38 } |
| 39 break; |
| 40 } |
| 41 case WM_COMMAND: { |
| 42 ShellJavaScriptDialog* owner = reinterpret_cast<ShellJavaScriptDialog*>( |
| 43 GetWindowLongPtr(dialog, DWL_USER)); |
| 44 string16 user_input; |
| 45 bool finish = false; |
| 46 bool result; |
| 47 switch (LOWORD(wparam)) { |
| 48 case IDOK: |
| 49 finish = true; |
| 50 result = true; |
| 51 if (owner->message_type_ == ui::JAVASCRIPT_MESSAGE_TYPE_PROMPT) { |
| 52 size_t length = |
| 53 GetWindowTextLength(GetDlgItem(dialog, IDC_PROMPTEDIT)) + 1; |
| 54 GetDlgItemText(dialog, IDC_PROMPTEDIT, |
| 55 WriteInto(&user_input, length), length); |
| 56 } |
| 57 break; |
| 58 case IDCANCEL: |
| 59 finish = true; |
| 60 result = false; |
| 61 break; |
| 62 } |
| 63 if (finish) { |
| 64 owner->dialog_win_ = 0; |
| 65 owner->callback_.Run(result, user_input); |
| 66 DestroyWindow(dialog); |
| 67 owner->creator_->DialogClosed(owner); |
| 68 } |
| 69 break; |
| 70 } |
| 71 default: |
| 72 return DefWindowProc(dialog, message, wparam, lparam); |
| 73 } |
| 74 return 0; |
| 75 } |
| 76 |
| 77 ShellJavaScriptDialog::ShellJavaScriptDialog( |
| 78 ShellJavaScriptDialogCreator* creator, |
| 79 ui::JavascriptMessageType javascript_message_type, |
| 80 const string16& message_text, |
| 81 const string16& default_prompt_text, |
| 82 const JavaScriptDialogCreator::DialogClosedCallback& callback) |
| 83 : creator_(creator), |
| 84 callback_(callback), |
| 85 message_text_(message_text), |
| 86 default_prompt_text_(default_prompt_text), |
| 87 message_type_(javascript_message_type) { |
| 88 int dialog_type; |
| 89 if (javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_ALERT) |
| 90 dialog_type = IDD_ALERT; |
| 91 else if (javascript_message_type == ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM) |
| 92 dialog_type = IDD_CONFIRM; |
| 93 else // JAVASCRIPT_MESSAGE_TYPE_PROMPT |
| 94 dialog_type = IDD_PROMPT; |
| 95 |
| 96 dialog_win_ = CreateDialogParam(GetModuleHandle(0), |
| 97 MAKEINTRESOURCE(dialog_type), 0, DialogProc, |
| 98 reinterpret_cast<LPARAM>(this)); |
| 99 ShowWindow(dialog_win_, SW_SHOWNORMAL); |
| 100 } |
| 101 |
| 102 ShellJavaScriptDialog::~ShellJavaScriptDialog() { |
| 103 Cancel(); |
| 104 } |
| 105 |
| 106 void ShellJavaScriptDialog::Cancel() { |
| 107 if (dialog_win_) |
| 108 DestroyWindow(dialog_win_); |
| 109 } |
| 110 |
| 111 } // namespace content |
OLD | NEW |