forked from adobe/brackets-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_handler_win.cpp
More file actions
275 lines (236 loc) · 8.82 KB
/
Copy pathclient_handler_win.cpp
File metadata and controls
275 lines (236 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "config.h"
#include "client_handler.h"
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "resource.h"
#include "native_menu_model.h"
#include <ShellAPI.h>
#define CLOSING_PROP L"CLOSING"
extern CefRefPtr<ClientHandler> g_handler;
// WM_DROPFILES handler, defined in cefclient_win.cpp
extern LRESULT HandleDropFiles(HDROP hDrop, CefRefPtr<ClientHandler> handler, CefRefPtr<CefBrowser> browser);
// Additional globals
extern HACCEL hAccelTable;
bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> parentBrowser,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
const CefString& url,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings) {
return false;
}
void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& url) {
REQUIRE_UI_THREAD();
#ifdef SHOW_TOOLBAR_UI
if (m_BrowserId == browser->GetIdentifier() && frame->IsMain()) {
// Set the edit window text
SetWindowText(m_EditHwnd, std::wstring(url).c_str());
}
#endif // SHOW_TOOLBAR_UI
}
void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
REQUIRE_UI_THREAD();
// Set the frame window title bar
CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle();
if (m_BrowserId == browser->GetIdentifier()) {
// The frame window will be the parent of the browser window
hwnd = GetParent(hwnd);
}
SetWindowText(hwnd, std::wstring(title).c_str());
}
void ClientHandler::SendNotification(NotificationType type) {
UINT id;
switch (type) {
case NOTIFY_CONSOLE_MESSAGE:
id = ID_WARN_CONSOLEMESSAGE;
break;
case NOTIFY_DOWNLOAD_COMPLETE:
id = ID_WARN_DOWNLOADCOMPLETE;
break;
case NOTIFY_DOWNLOAD_ERROR:
id = ID_WARN_DOWNLOADERROR;
break;
default:
return;
}
PostMessage(m_MainHwnd, WM_COMMAND, id, 0);
}
void ClientHandler::SetLoading(bool isLoading) {
#ifdef SHOW_TOOLBAR_UI
ASSERT(m_EditHwnd != NULL && m_ReloadHwnd != NULL && m_StopHwnd != NULL);
EnableWindow(m_EditHwnd, TRUE);
EnableWindow(m_ReloadHwnd, !isLoading);
EnableWindow(m_StopHwnd, isLoading);
#endif // SHOW_TOOLBAR_UI
}
void ClientHandler::SetNavState(bool canGoBack, bool canGoForward) {
#ifdef SHOW_TOOLBAR_UI
ASSERT(m_BackHwnd != NULL && m_ForwardHwnd != NULL);
EnableWindow(m_BackHwnd, canGoBack);
EnableWindow(m_ForwardHwnd, canGoForward);
#endif // SHOW_TOOLBAR_UI
}
void ClientHandler::CloseMainWindow() {
::PostMessage(m_MainHwnd, WM_CLOSE, 0, 0);
}
static WNDPROC g_popupWndOldProc = NULL;
//BRACKETS: added so our popup windows can have a menu bar too
//
// FUNCTION: PopupWndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Handle commands from the menus.
LRESULT CALLBACK PopupWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//For now, we are only interest in WM_COMMAND's that we know are in our menus
//and WM_CLOSE so we can delegate that back to the browser
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(hWnd);
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
int wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_CLOSE:
if (g_handler.get() && browser.get()) {
HWND browserHwnd = browser->GetHost()->GetWindowHandle();
HANDLE closing = GetProp(browserHwnd, CLOSING_PROP);
if (closing) {
RemoveProp(browserHwnd, CLOSING_PROP);
break;
}
CefRefPtr<CommandCallback> callback = new CloseWindowCommandCallback(browser);
g_handler->SendJSCommand(browser, FILE_CLOSE_WINDOW, callback);
}
return 0;
default:
ExtensionString commandId = NativeMenuModel::getInstance(getMenuParent(browser)).getCommandId(wmId);
if (commandId.size() > 0) {
CefRefPtr<CommandCallback> callback = new EditCommandCallback(browser, commandId);
g_handler->SendJSCommand(browser, commandId, callback);
}
}
}
break;
case WM_CLOSE:
if (g_handler.get() && browser.get()) {
HWND browserHwnd = browser->GetHost()->GetWindowHandle();
HANDLE closing = GetProp(browserHwnd, CLOSING_PROP);
if (closing) {
RemoveProp(browserHwnd, CLOSING_PROP);
break;
}
CefRefPtr<CommandCallback> callback = new CloseWindowCommandCallback(browser);
g_handler->SendJSCommand(browser, FILE_CLOSE_WINDOW, callback);
return 0;
}
break;
case WM_DROPFILES:
if (g_handler.get() && browser.get()) {
return HandleDropFiles((HDROP)wParam, g_handler, browser);
}
break;
case WM_INITMENUPOPUP:
HMENU menu = (HMENU)wParam;
int count = GetMenuItemCount(menu);
void* menuParent = getMenuParent(browser);
for (int i = 0; i < count; i++) {
UINT id = GetMenuItemID(menu, i);
bool enabled = NativeMenuModel::getInstance(menuParent).isMenuItemEnabled(id);
UINT flagEnabled = enabled ? MF_ENABLED | MF_BYCOMMAND : MF_DISABLED | MF_BYCOMMAND;
EnableMenuItem(menu, id, flagEnabled);
bool checked = NativeMenuModel::getInstance(menuParent).isMenuItemChecked(id);
UINT flagChecked = checked ? MF_CHECKED | MF_BYCOMMAND : MF_UNCHECKED | MF_BYCOMMAND;
CheckMenuItem(menu, id, flagChecked);
}
break;
}
if (g_popupWndOldProc)
return (LRESULT)::CallWindowProc(g_popupWndOldProc, hWnd, message, wParam, lParam);
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void AttachWindProcToPopup(HWND wnd)
{
if (!wnd) {
return;
}
WNDPROC curProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(wnd, GWLP_WNDPROC));
//if this is the first time, assume the above checks are all we need, otherwise
//it had better be the same proc we pulled before
if (!g_popupWndOldProc) {
g_popupWndOldProc = curProc;
} else if (g_popupWndOldProc != curProc) {
return;
}
SetWindowLongPtr(wnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(PopupWndProc));
}
void LoadWindowsIcons(HWND wnd)
{
if (!wnd) {
return;
}
//We need to load the icons after the pop up is created so they have the
//brackets icon instead of the generic window icon
HINSTANCE inst = ::GetModuleHandle(NULL);
HICON bigIcon = ::LoadIcon(inst, MAKEINTRESOURCE(IDI_CEFCLIENT));
HICON smIcon = ::LoadIcon(inst, MAKEINTRESOURCE(IDI_SMALL));
if(bigIcon) {
::SendMessage(wnd, WM_SETICON, ICON_BIG, (LPARAM)bigIcon);
}
if(smIcon) {
::SendMessage(wnd, WM_SETICON, ICON_SMALL, (LPARAM)smIcon);
}
}
void ClientHandler::PopupCreated(CefRefPtr<CefBrowser> browser)
{
HWND hWnd = browser->GetHost()->GetWindowHandle();
AttachWindProcToPopup(hWnd);
LoadWindowsIcons(hWnd);
DragAcceptFiles(hWnd, true);
browser->GetHost()->SetFocus(true);
}
CefRefPtr<CefBrowser> ClientHandler::GetBrowserForNativeWindow(void* window) {
CefRefPtr<CefBrowser> browser = NULL;
if (window) {
//go through all the browsers looking for a browser within this window
BrowserWindowMap::const_iterator i = browser_window_map_.find((HWND)window);
if (i != browser_window_map_.end()) {
browser = i->second;
}
}
return browser;
}
bool ClientHandler::CanCloseBrowser(CefRefPtr<CefBrowser> browser) {
// On windows, the main browser is the first in the map. It needs to be
// destroyed last. So, don't allow main browser to be closed until there's
// only 1 browser remaining in the map.
if (browser_window_map_.size() > 1) {
CefWindowHandle hWndBrowser = browser->GetHost()->GetWindowHandle();
CefWindowHandle hWndMain = browser_window_map_.begin()->first;
return (hWndBrowser != hWndMain);
}
return true;
}
bool ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event,
bool* is_keyboard_shortcut) {
if (::TranslateAccelerator((HWND)getMenuParent(browser), hAccelTable, os_event)) {
return true;
}
return false;
}
bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event) {
return false;
}