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.
5 #include "remoting/host/setup/win/host_configurer_window.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string16.h"
14 #include "remoting/host/setup/win/load_string_from_resource.h"
15 #include "remoting/host/setup/win/start_host_window.h"
19 HostConfigurerWindow::HostConfigurerWindow(
20 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
21 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
22 base::Closure on_destroy)
23 : url_request_context_getter_(url_request_context_getter),
24 ui_task_runner_(ui_task_runner),
25 on_destroy_(on_destroy) {
28 void HostConfigurerWindow::OnCancel(UINT code, int id, HWND control) {
29 DCHECK(ui_task_runner_->BelongsToCurrentThread());
34 void HostConfigurerWindow::OnClose() {
35 DCHECK(ui_task_runner_->BelongsToCurrentThread());
40 LRESULT HostConfigurerWindow::OnInitDialog(HWND wparam, LPARAM lparam) {
41 DCHECK(ui_task_runner_->BelongsToCurrentThread());
43 // Failure of any of these calls is acceptable.
44 SetWindowText(LoadStringFromResource(IDS_TITLE));
45 GetDlgItem(IDC_CHANGE_PIN).EnableWindow(FALSE);
46 GetDlgItem(IDC_STOP_HOST).EnableWindow(FALSE);
51 void HostConfigurerWindow::OnOk(UINT code, int id, HWND control) {
52 DCHECK(ui_task_runner_->BelongsToCurrentThread());
57 void HostConfigurerWindow::OnStartHost(UINT code, int id, HWND control) {
58 DCHECK(ui_task_runner_->BelongsToCurrentThread());
60 StartHostWindow start_host_window(url_request_context_getter_);
61 start_host_window.DoModal();
64 void HostConfigurerWindow::PositionWindow() {
65 DCHECK(ui_task_runner_->BelongsToCurrentThread());
67 // Get the window dimensions.
69 if (!GetWindowRect(&rect)) {
73 // Position against the owner window unless it is minimized or invisible.
74 HWND owner = ::GetWindow(m_hWnd, GW_OWNER);
76 DWORD style = ::GetWindowLong(owner, GWL_STYLE);
77 if ((style & WS_MINIMIZE) != 0 || (style & WS_VISIBLE) == 0) {
82 // Make sure that the window will not end up split by a monitor's boundary.
84 if (!::SystemParametersInfo(SPI_GETWORKAREA, NULL, &area_rect, NULL)) {
88 // On a multi-monitor system use the monitor where the owner window is shown.
89 RECT owner_rect = area_rect;
90 if (owner != NULL && ::GetWindowRect(owner, &owner_rect)) {
91 HMONITOR monitor = ::MonitorFromRect(&owner_rect, MONITOR_DEFAULTTONEAREST);
92 if (monitor != NULL) {
93 MONITORINFO monitor_info = {0};
94 monitor_info.cbSize = sizeof(monitor_info);
95 if (::GetMonitorInfo(monitor, &monitor_info)) {
96 area_rect = monitor_info.rcWork;
101 LONG width = rect.right - rect.left;
102 LONG height = rect.bottom - rect.top;
103 // Avoid the center of the owner rectangle, because the host controller will
104 // put its confirmation window there.
105 LONG x = (3 * owner_rect.left + owner_rect.right) / 4 - (width / 2);
106 LONG y = (3 * owner_rect.top + owner_rect.bottom) / 4 - (height / 2);
108 x = std::max(x, area_rect.left);
109 x = std::min(x, area_rect.right - width);
110 y = std::max(y, area_rect.top);
111 y = std::min(y, area_rect.bottom - width);
113 SetWindowPos(NULL, x, y, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
116 void HostConfigurerWindow::Finish() {
117 DCHECK(ui_task_runner_->BelongsToCurrentThread());
123 } // namespace remoting