- add sources.
[platform/framework/web/crosswalk.git] / src / remoting / host / setup / win / host_configurer_window.cc
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 "remoting/host/setup/win/host_configurer_window.h"
6
7 #include <atlbase.h>
8 #include <atlstr.h>
9 #include <atlwin.h>
10 #include <windows.h>
11
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"
16
17 namespace remoting {
18
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) {
26 }
27
28 void HostConfigurerWindow::OnCancel(UINT code, int id, HWND control) {
29   DCHECK(ui_task_runner_->BelongsToCurrentThread());
30
31   Finish();
32 }
33
34 void HostConfigurerWindow::OnClose() {
35   DCHECK(ui_task_runner_->BelongsToCurrentThread());
36
37   Finish();
38 }
39
40 LRESULT HostConfigurerWindow::OnInitDialog(HWND wparam, LPARAM lparam) {
41   DCHECK(ui_task_runner_->BelongsToCurrentThread());
42
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);
47   PositionWindow();
48   return TRUE;
49 }
50
51 void HostConfigurerWindow::OnOk(UINT code, int id, HWND control) {
52   DCHECK(ui_task_runner_->BelongsToCurrentThread());
53
54   Finish();
55 }
56
57 void HostConfigurerWindow::OnStartHost(UINT code, int id, HWND control) {
58   DCHECK(ui_task_runner_->BelongsToCurrentThread());
59
60   StartHostWindow start_host_window(url_request_context_getter_);
61   start_host_window.DoModal();
62 }
63
64 void HostConfigurerWindow::PositionWindow() {
65   DCHECK(ui_task_runner_->BelongsToCurrentThread());
66
67   // Get the window dimensions.
68   RECT rect;
69   if (!GetWindowRect(&rect)) {
70     return;
71   }
72
73   // Position against the owner window unless it is minimized or invisible.
74   HWND owner = ::GetWindow(m_hWnd, GW_OWNER);
75   if (owner != NULL) {
76     DWORD style = ::GetWindowLong(owner, GWL_STYLE);
77     if ((style & WS_MINIMIZE) != 0 || (style & WS_VISIBLE) == 0) {
78       owner = NULL;
79     }
80   }
81
82   // Make sure that the window will not end up split by a monitor's boundary.
83   RECT area_rect;
84   if (!::SystemParametersInfo(SPI_GETWORKAREA, NULL, &area_rect, NULL)) {
85     return;
86   }
87
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;
97       }
98     }
99   }
100
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);
107
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);
112
113   SetWindowPos(NULL, x, y, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
114 }
115
116 void HostConfigurerWindow::Finish() {
117   DCHECK(ui_task_runner_->BelongsToCurrentThread());
118
119   DestroyWindow();
120   on_destroy_.Run();
121 }
122
123 }  // namespace remoting