blob: e274b6614b015ac13b6182e3864f578e36d1eb73 [file] [log] [blame]
[email protected]5cba3bf2012-01-14 02:40:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9fbd3f862011-09-20 23:31:342// 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]e99ca5112011-09-26 17:22:5412#include "base/utf_string_conversions.h"
[email protected]9fbd3f862011-09-20 23:31:3413#include "base/win/resource_util.h"
[email protected]e99ca5112011-09-26 17:22:5414#include "content/browser/tab_contents/tab_contents.h"
[email protected]5cba3bf2012-01-14 02:40:3515#include "content/browser/tab_contents/tab_contents_view_win.h"
[email protected]9fbd3f862011-09-20 23:31:3416#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]e99ca5112011-09-26 17:22:5420#include "ipc/ipc_message.h"
[email protected]9fbd3f862011-09-20 23:31:3421#include "net/base/net_module.h"
22#include "ui/base/win/hwnd_util.h"
23
24namespace {
25
[email protected]3fdc4622a2011-10-06 22:25:4926const wchar_t kWindowTitle[] = L"Content Shell";
27const wchar_t kWindowClass[] = L"CONTENT_SHELL";
[email protected]9fbd3f862011-09-20 23:31:3428
[email protected]3fdc4622a2011-10-06 22:25:4929const int kButtonWidth = 72;
30const int kURLBarHeight = 24;
[email protected]9fbd3f862011-09-20 23:31:3431
[email protected]3fdc4622a2011-10-06 22:25:4932const int kMaxURLLength = 1024;
[email protected]9fbd3f862011-09-20 23:31:3433
34static 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
45namespace content {
46
47HINSTANCE Shell::instance_handle_;
48
49void 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
59base::StringPiece Shell::PlatformResourceProvider(int key) {
60 return GetRawDataResource(::GetModuleHandle(NULL), key);
61}
62
[email protected]e99ca5112011-09-26 17:22:5463void Shell::PlatformExit() {
64}
65
[email protected]9fbd3f862011-09-20 23:31:3466void Shell::PlatformCleanUp() {
67 // When the window is destroyed, tell the Edit field to forget about us,
68 // otherwise we will crash.
[email protected]46d04fa2011-10-06 18:32:4369 ui::SetWindowProc(url_edit_view_, default_edit_wnd_proc_);
70 ui::SetWindowUserData(url_edit_view_, NULL);
[email protected]9fbd3f862011-09-20 23:31:3471}
72
73void 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]46d04fa2011-10-06 18:32:4389 EnableWindow(GetDlgItem(window_, id), is_enabled);
[email protected]9fbd3f862011-09-20 23:31:3490}
91
[email protected]e99ca5112011-09-26 17:22:5492void Shell::PlatformSetAddressBarURL(const GURL& url) {
93 std::wstring url_string = UTF8ToWide(url.spec());
[email protected]46d04fa2011-10-06 18:32:4394 SendMessage(url_edit_view_, WM_SETTEXT, 0,
[email protected]e99ca5112011-09-26 17:22:5495 reinterpret_cast<LPARAM>(url_string.c_str()));
96}
97
[email protected]3fdc4622a2011-10-06 22:25:4998void Shell::PlatformCreateWindow(int width, int height) {
[email protected]46d04fa2011-10-06 18:32:4399 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]9fbd3f862011-09-20 23:31:34104
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]46d04fa2011-10-06 18:32:43111 window_, (HMENU) IDC_NAV_BACK, instance_handle_, 0);
[email protected]9fbd3f862011-09-20 23:31:34112 x += kButtonWidth;
113
114 hwnd = CreateWindow(L"BUTTON", L"Forward",
115 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
116 x, 0, kButtonWidth, kURLBarHeight,
[email protected]46d04fa2011-10-06 18:32:43117 window_, (HMENU) IDC_NAV_FORWARD, instance_handle_, 0);
[email protected]9fbd3f862011-09-20 23:31:34118 x += kButtonWidth;
119
120 hwnd = CreateWindow(L"BUTTON", L"Reload",
121 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
122 x, 0, kButtonWidth, kURLBarHeight,
[email protected]46d04fa2011-10-06 18:32:43123 window_, (HMENU) IDC_NAV_RELOAD, instance_handle_, 0);
[email protected]9fbd3f862011-09-20 23:31:34124 x += kButtonWidth;
125
126 hwnd = CreateWindow(L"BUTTON", L"Stop",
127 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
128 x, 0, kButtonWidth, kURLBarHeight,
[email protected]46d04fa2011-10-06 18:32:43129 window_, (HMENU) IDC_NAV_STOP, instance_handle_, 0);
[email protected]9fbd3f862011-09-20 23:31:34130 x += kButtonWidth;
131
132 // This control is positioned by PlatformResizeSubViews.
[email protected]46d04fa2011-10-06 18:32:43133 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]9fbd3f862011-09-20 23:31:34137
[email protected]46d04fa2011-10-06 18:32:43138 default_edit_wnd_proc_ = ui::SetWindowProc(url_edit_view_,
139 Shell::EditWndProc);
140 ui::SetWindowUserData(url_edit_view_, this);
[email protected]9fbd3f862011-09-20 23:31:34141
[email protected]46d04fa2011-10-06 18:32:43142 ShowWindow(window_, SW_SHOW);
[email protected]3fdc4622a2011-10-06 22:25:49143
144 PlatformSizeTo(width, height);
[email protected]9fbd3f862011-09-20 23:31:34145}
146
[email protected]5cba3bf2012-01-14 02:40:35147void Shell::PlatformSetContents() {
148 TabContentsViewWin* view =
149 static_cast<TabContentsViewWin*>(tab_contents_->GetView());
150 view->SetParent(window_);
151}
152
[email protected]9fbd3f862011-09-20 23:31:34153void Shell::PlatformSizeTo(int width, int height) {
154 RECT rc, rw;
[email protected]46d04fa2011-10-06 18:32:43155 GetClientRect(window_, &rc);
156 GetWindowRect(window_, &rw);
[email protected]9fbd3f862011-09-20 23:31:34157
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]46d04fa2011-10-06 18:32:43169 SetWindowPos(window_, NULL, 0, 0, window_width, window_height,
[email protected]9fbd3f862011-09-20 23:31:34170 SWP_NOMOVE | SWP_NOZORDER);
171}
172
173void Shell::PlatformResizeSubViews() {
174 RECT rc;
[email protected]46d04fa2011-10-06 18:32:43175 GetClientRect(window_, &rc);
[email protected]9fbd3f862011-09-20 23:31:34176
177 int x = kButtonWidth * 4;
[email protected]46d04fa2011-10-06 18:32:43178 MoveWindow(url_edit_view_, x, 0, rc.right - x, kURLBarHeight, TRUE);
[email protected]9fbd3f862011-09-20 23:31:34179
180 MoveWindow(GetContentView(), 0, kURLBarHeight, rc.right,
181 rc.bottom - kURLBarHeight, TRUE);
182}
183
184ATOM 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
202LRESULT 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]e99ca5112011-09-26 17:22:54210 case IDM_NEW_WINDOW:
211 CreateNewWindow(
[email protected]627e0512011-12-21 22:55:30212 shell->tab_contents()->GetBrowserContext(),
[email protected]e99ca5112011-09-26 17:22:54213 GURL(), NULL, MSG_ROUTING_NONE, NULL);
214 break;
215 case IDM_CLOSE_WINDOW:
[email protected]9fbd3f862011-09-20 23:31:34216 DestroyWindow(hwnd);
217 break;
[email protected]e99ca5112011-09-26 17:22:54218 case IDM_EXIT:
219 PlatformExit();
220 break;
[email protected]9fbd3f862011-09-20 23:31:34221 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]3fdc4622a2011-10-06 22:25:49228 shell->Reload();
[email protected]9fbd3f862011-09-20 23:31:34229 break;
[email protected]3fdc4622a2011-10-06 22:25:49230 case IDC_NAV_STOP:
231 shell->Stop();
232 break;
[email protected]9fbd3f862011-09-20 23:31:34233 }
234 break;
235 }
236 case WM_DESTROY: {
237 delete shell;
[email protected]e99ca5112011-09-26 17:22:54238 if (windows_.empty())
[email protected]9fbd3f862011-09-20 23:31:34239 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
253LRESULT 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]e99ca5112011-09-26 17:22:54265 GURL url(str);
266 if (!url.has_scheme())
267 url = GURL(std::wstring(L"http://") + std::wstring(str));
268 shell->LoadURL(url);
[email protected]9fbd3f862011-09-20 23:31:34269 }
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