[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 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.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | #include <commctrl.h> |
| 9 | |
| 10 | #include "base/message_loop.h" |
| 11 | #include "base/string_piece.h" |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 13 | #include "base/win/resource_util.h" |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 14 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame^] | 15 | #include "content/browser/tab_contents/tab_contents_view_win.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 16 | #include "content/shell/resource.h" |
| 17 | #include "googleurl/src/gurl.h" |
| 18 | #include "grit/webkit_resources.h" |
| 19 | #include "grit/webkit_chromium_resources.h" |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 20 | #include "ipc/ipc_message.h" |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 21 | #include "net/base/net_module.h" |
| 22 | #include "ui/base/win/hwnd_util.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 26 | const wchar_t kWindowTitle[] = L"Content Shell"; |
| 27 | const wchar_t kWindowClass[] = L"CONTENT_SHELL"; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 28 | |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 29 | const int kButtonWidth = 72; |
| 30 | const int kURLBarHeight = 24; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 31 | |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 32 | const int kMaxURLLength = 1024; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 33 | |
| 34 | static base::StringPiece GetRawDataResource(HMODULE module, int resource_id) { |
| 35 | void* data_ptr; |
| 36 | size_t data_size; |
| 37 | return base::win::GetDataResourceFromModule(module, resource_id, &data_ptr, |
| 38 | &data_size) |
| 39 | ? base::StringPiece(static_cast<char*>(data_ptr), data_size) |
| 40 | : base::StringPiece(); |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | namespace content { |
| 46 | |
| 47 | HINSTANCE Shell::instance_handle_; |
| 48 | |
| 49 | void Shell::PlatformInitialize() { |
| 50 | instance_handle_ = ::GetModuleHandle(NULL); |
| 51 | |
| 52 | INITCOMMONCONTROLSEX InitCtrlEx; |
| 53 | InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX); |
| 54 | InitCtrlEx.dwICC = ICC_STANDARD_CLASSES; |
| 55 | InitCommonControlsEx(&InitCtrlEx); |
| 56 | RegisterWindowClass(); |
| 57 | } |
| 58 | |
| 59 | base::StringPiece Shell::PlatformResourceProvider(int key) { |
| 60 | return GetRawDataResource(::GetModuleHandle(NULL), key); |
| 61 | } |
| 62 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 63 | void Shell::PlatformExit() { |
| 64 | } |
| 65 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 66 | void Shell::PlatformCleanUp() { |
| 67 | // When the window is destroyed, tell the Edit field to forget about us, |
| 68 | // otherwise we will crash. |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 69 | ui::SetWindowProc(url_edit_view_, default_edit_wnd_proc_); |
| 70 | ui::SetWindowUserData(url_edit_view_, NULL); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) { |
| 74 | int id; |
| 75 | switch (control) { |
| 76 | case BACK_BUTTON: |
| 77 | id = IDC_NAV_BACK; |
| 78 | break; |
| 79 | case FORWARD_BUTTON: |
| 80 | id = IDC_NAV_FORWARD; |
| 81 | break; |
| 82 | case STOP_BUTTON: |
| 83 | id = IDC_NAV_STOP; |
| 84 | break; |
| 85 | default: |
| 86 | NOTREACHED() << "Unknown UI control"; |
| 87 | return; |
| 88 | } |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 89 | EnableWindow(GetDlgItem(window_, id), is_enabled); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 92 | void Shell::PlatformSetAddressBarURL(const GURL& url) { |
| 93 | std::wstring url_string = UTF8ToWide(url.spec()); |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 94 | SendMessage(url_edit_view_, WM_SETTEXT, 0, |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 95 | reinterpret_cast<LPARAM>(url_string.c_str())); |
| 96 | } |
| 97 | |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 98 | void Shell::PlatformCreateWindow(int width, int height) { |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 99 | window_ = CreateWindow(kWindowClass, kWindowTitle, |
| 100 | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, |
| 101 | CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, |
| 102 | NULL, NULL, instance_handle_, NULL); |
| 103 | ui::SetWindowUserData(window_, this); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 104 | |
| 105 | HWND hwnd; |
| 106 | int x = 0; |
| 107 | |
| 108 | hwnd = CreateWindow(L"BUTTON", L"Back", |
| 109 | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , |
| 110 | x, 0, kButtonWidth, kURLBarHeight, |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 111 | window_, (HMENU) IDC_NAV_BACK, instance_handle_, 0); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 112 | x += kButtonWidth; |
| 113 | |
| 114 | hwnd = CreateWindow(L"BUTTON", L"Forward", |
| 115 | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , |
| 116 | x, 0, kButtonWidth, kURLBarHeight, |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 117 | window_, (HMENU) IDC_NAV_FORWARD, instance_handle_, 0); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 118 | x += kButtonWidth; |
| 119 | |
| 120 | hwnd = CreateWindow(L"BUTTON", L"Reload", |
| 121 | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , |
| 122 | x, 0, kButtonWidth, kURLBarHeight, |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 123 | window_, (HMENU) IDC_NAV_RELOAD, instance_handle_, 0); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 124 | x += kButtonWidth; |
| 125 | |
| 126 | hwnd = CreateWindow(L"BUTTON", L"Stop", |
| 127 | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , |
| 128 | x, 0, kButtonWidth, kURLBarHeight, |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 129 | window_, (HMENU) IDC_NAV_STOP, instance_handle_, 0); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 130 | x += kButtonWidth; |
| 131 | |
| 132 | // This control is positioned by PlatformResizeSubViews. |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 133 | url_edit_view_ = CreateWindow(L"EDIT", 0, |
| 134 | WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | |
| 135 | ES_AUTOVSCROLL | ES_AUTOHSCROLL, |
| 136 | x, 0, 0, 0, window_, 0, instance_handle_, 0); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 137 | |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 138 | default_edit_wnd_proc_ = ui::SetWindowProc(url_edit_view_, |
| 139 | Shell::EditWndProc); |
| 140 | ui::SetWindowUserData(url_edit_view_, this); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 141 | |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 142 | ShowWindow(window_, SW_SHOW); |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 143 | |
| 144 | PlatformSizeTo(width, height); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 5cba3bf | 2012-01-14 02:40:35 | [diff] [blame^] | 147 | void Shell::PlatformSetContents() { |
| 148 | TabContentsViewWin* view = |
| 149 | static_cast<TabContentsViewWin*>(tab_contents_->GetView()); |
| 150 | view->SetParent(window_); |
| 151 | } |
| 152 | |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 153 | void Shell::PlatformSizeTo(int width, int height) { |
| 154 | RECT rc, rw; |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 155 | GetClientRect(window_, &rc); |
| 156 | GetWindowRect(window_, &rw); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 157 | |
| 158 | int client_width = rc.right - rc.left; |
| 159 | int window_width = rw.right - rw.left; |
| 160 | window_width = (window_width - client_width) + width; |
| 161 | |
| 162 | int client_height = rc.bottom - rc.top; |
| 163 | int window_height = rw.bottom - rw.top; |
| 164 | window_height = (window_height - client_height) + height; |
| 165 | |
| 166 | // Add space for the url bar. |
| 167 | window_height += kURLBarHeight; |
| 168 | |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 169 | SetWindowPos(window_, NULL, 0, 0, window_width, window_height, |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 170 | SWP_NOMOVE | SWP_NOZORDER); |
| 171 | } |
| 172 | |
| 173 | void Shell::PlatformResizeSubViews() { |
| 174 | RECT rc; |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 175 | GetClientRect(window_, &rc); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 176 | |
| 177 | int x = kButtonWidth * 4; |
[email protected] | 46d04fa | 2011-10-06 18:32:43 | [diff] [blame] | 178 | MoveWindow(url_edit_view_, x, 0, rc.right - x, kURLBarHeight, TRUE); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 179 | |
| 180 | MoveWindow(GetContentView(), 0, kURLBarHeight, rc.right, |
| 181 | rc.bottom - kURLBarHeight, TRUE); |
| 182 | } |
| 183 | |
| 184 | ATOM Shell::RegisterWindowClass() { |
| 185 | WNDCLASSEX wcex = { |
| 186 | sizeof(WNDCLASSEX), |
| 187 | CS_HREDRAW | CS_VREDRAW, |
| 188 | Shell::WndProc, |
| 189 | 0, |
| 190 | 0, |
| 191 | instance_handle_, |
| 192 | NULL, |
| 193 | LoadCursor(NULL, IDC_ARROW), |
| 194 | 0, |
| 195 | MAKEINTRESOURCE(IDC_CONTENTSHELL), |
| 196 | kWindowClass, |
| 197 | NULL, |
| 198 | }; |
| 199 | return RegisterClassEx(&wcex); |
| 200 | } |
| 201 | |
| 202 | LRESULT CALLBACK Shell::WndProc(HWND hwnd, UINT message, WPARAM wParam, |
| 203 | LPARAM lParam) { |
| 204 | Shell* shell = static_cast<Shell*>(ui::GetWindowUserData(hwnd)); |
| 205 | |
| 206 | switch (message) { |
| 207 | case WM_COMMAND: { |
| 208 | int id = LOWORD(wParam); |
| 209 | switch (id) { |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 210 | case IDM_NEW_WINDOW: |
| 211 | CreateNewWindow( |
[email protected] | 627e051 | 2011-12-21 22:55:30 | [diff] [blame] | 212 | shell->tab_contents()->GetBrowserContext(), |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 213 | GURL(), NULL, MSG_ROUTING_NONE, NULL); |
| 214 | break; |
| 215 | case IDM_CLOSE_WINDOW: |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 216 | DestroyWindow(hwnd); |
| 217 | break; |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 218 | case IDM_EXIT: |
| 219 | PlatformExit(); |
| 220 | break; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 221 | case IDC_NAV_BACK: |
| 222 | shell->GoBackOrForward(-1); |
| 223 | break; |
| 224 | case IDC_NAV_FORWARD: |
| 225 | shell->GoBackOrForward(1); |
| 226 | break; |
| 227 | case IDC_NAV_RELOAD: |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 228 | shell->Reload(); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 229 | break; |
[email protected] | 3fdc4622a | 2011-10-06 22:25:49 | [diff] [blame] | 230 | case IDC_NAV_STOP: |
| 231 | shell->Stop(); |
| 232 | break; |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 233 | } |
| 234 | break; |
| 235 | } |
| 236 | case WM_DESTROY: { |
| 237 | delete shell; |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 238 | if (windows_.empty()) |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 239 | MessageLoop::current()->Quit(); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | case WM_SIZE: { |
| 244 | if (shell->GetContentView()) |
| 245 | shell->PlatformResizeSubViews(); |
| 246 | return 0; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return DefWindowProc(hwnd, message, wParam, lParam); |
| 251 | } |
| 252 | |
| 253 | LRESULT CALLBACK Shell::EditWndProc(HWND hwnd, UINT message, |
| 254 | WPARAM wParam, LPARAM lParam) { |
| 255 | Shell* shell = static_cast<Shell*>(ui::GetWindowUserData(hwnd)); |
| 256 | |
| 257 | switch (message) { |
| 258 | case WM_CHAR: |
| 259 | if (wParam == VK_RETURN) { |
| 260 | wchar_t str[kMaxURLLength + 1]; // Leave room for adding a NULL; |
| 261 | *(str) = kMaxURLLength; |
| 262 | LRESULT str_len = SendMessage(hwnd, EM_GETLINE, 0, (LPARAM)str); |
| 263 | if (str_len > 0) { |
| 264 | str[str_len] = 0; // EM_GETLINE doesn't NULL terminate. |
[email protected] | e99ca511 | 2011-09-26 17:22:54 | [diff] [blame] | 265 | GURL url(str); |
| 266 | if (!url.has_scheme()) |
| 267 | url = GURL(std::wstring(L"http://") + std::wstring(str)); |
| 268 | shell->LoadURL(url); |
[email protected] | 9fbd3f86 | 2011-09-20 23:31:34 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return CallWindowProc(shell->default_edit_wnd_proc_, hwnd, message, wParam, |
| 276 | lParam); |
| 277 | } |
| 278 | |
| 279 | } // namespace content |