This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathcefclient_win.cpp
More file actions
1066 lines (898 loc) · 33.1 KB
/
cefclient_win.cpp
File metadata and controls
1066 lines (898 loc) · 33.1 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010 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 "cefclient.h"
#include <windows.h>
#include <commdlg.h>
#include <direct.h>
#include <MMSystem.h>
#include <sstream>
#include <string>
#include "include/cef_app.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "client_handler.h"
#include "config.h"
#include "resource.h"
#include "string_util.h"
#include "client_switches.h"
#include "native_menu_model.h"
#include "appshell_node_process.h"
#include <algorithm>
#include <ShellAPI.h>
#include <ShlObj.h>
#define MAX_LOADSTRING 100
#ifdef SHOW_TOOLBAR_UI
#define MAX_URL_LENGTH 255
#define BUTTON_WIDTH 72
#define URLBAR_HEIGHT 24
#endif // SHOW_TOOLBAR_UI
#define CLOSING_PROP L"CLOSING"
#define FIRST_INSTANCE_MUTEX_NAME L".Shell.Instance"
#define ID_WM_COPYDATA_SENDOPENFILECOMMAND (WM_USER+1001)
// Global Variables:
DWORD g_appStartupTime;
HINSTANCE hInst; // current instance
HACCEL hAccelTable;
HWND hWndMain;
std::wstring gFilesToOpen; // Filenames passed as arguments to app
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
char szWorkingDir[MAX_PATH]; // The current working directory
TCHAR szInitialUrl[MAX_PATH] = {0};
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance, const cef_string_t& locale);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
// The global ClientHandler reference.
extern CefRefPtr<ClientHandler> g_handler;
#if defined(OS_WIN)
// Add Common Controls to the application manifest because it's required to
// support the default tooltip implementation.
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#endif
// Registry access functions
void EnsureTrailingSeparator(LPWSTR pRet);
void GetKey(LPCWSTR pBase, LPCWSTR pGroup, LPCWSTR pApp, LPCWSTR pFolder, LPWSTR pRet);
bool GetRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int* pDefault, int& ret);
bool WriteRegistryInt (LPCWSTR pFolder, LPCWSTR pEntry, int val);
// Registry key strings
#define PREF_APPSHELL_BASE L"Software"
#define PREF_WINPOS_FOLDER L"Window Position"
#define PREF_LEFT L"Left"
#define PREF_TOP L"Top"
#define PREF_WIDTH L"Width"
#define PREF_HEIGHT L"Height"
#define PREF_RESTORE_LEFT L"Restore Left"
#define PREF_RESTORE_TOP L"Restore Top"
#define PREF_RESTORE_RIGHT L"Restore Right"
#define PREF_RESTORE_BOTTOM L"Restore Bottom"
#define PREF_SHOWSTATE L"Show State"
// Window state functions
void SaveWindowRect(HWND hWnd);
void RestoreWindowRect(int& left, int& top, int& width, int& height, int& showCmd);
void RestoreWindowPlacement(HWND hWnd, int showCmd);
// If 'str' ends with a colon followed by some digits, then remove the colon and digits. For example:
// "c:\bob\abc.txt:123:456" will be changed to "c:\bob\abc.txt:123"
// "c:\bob\abc.txt:123" will be changed to "c:\bob\abc.txt"
// "c:\bob\abc.txt" will not be changed
// (Note: we could do this with a regular expression, but there is no regex library currently
// built into brackets-shell, and I don't want to add one just for this simple case).
void StripColonNumber(std::wstring& str) {
bool gotDigits = false;
int index;
for (index = str.size() - 1; index >= 0; index--) {
if (!isdigit(str[index]))
break;
gotDigits = true;
}
if (gotDigits && index >= 0 && str[index] == ':') {
str.resize(index);
}
}
// Determine if 'str' is a valid filename.
bool IsFilename(const std::wstring& str) {
// Strip off trailing line and column number, if any.
std::wstring temp(str);
StripColonNumber(temp);
StripColonNumber(temp);
// Return true if the OS thinks the filename is OK.
return (GetFileAttributes(temp.c_str()) != INVALID_FILE_ATTRIBUTES);
}
std::wstring GetFilenamesFromCommandLine() {
std::wstring result = L"[]";
if (AppGetCommandLine()->HasArguments()) {
bool firstEntry = true;
std::vector<CefString> args;
AppGetCommandLine()->GetArguments(args);
std::vector<CefString>::iterator iterator;
result = L"[";
for (iterator = args.begin(); iterator != args.end(); iterator++) {
std::wstring argument = (*iterator).ToWString();
if (IsFilename(argument)) {
if (!firstEntry) {
result += L",";
}
firstEntry = false;
result += L"\"" + argument + L"\"";
}
}
result += L"]";
}
return result;
}
// EnumWindowsProc callback function
// - searches for an already running Brackets application window
BOOL CALLBACK FindSuitableBracketsInstance(HWND hwnd, LPARAM lParam)
{
ASSERT(lParam != NULL); // must be passed an HWND pointer to return, if found
// check for the Brackets application window by class name and title
WCHAR cName[MAX_PATH+1] = {0}, cTitle[MAX_PATH+1] = {0};
::GetClassName(hwnd, cName, MAX_PATH);
::GetWindowText(hwnd, cTitle, MAX_PATH);
if ((wcscmp(cName, szWindowClass) == 0) && (wcsstr(cTitle, APP_NAME) != 0)) {
// found an already running instance of Brackets. Now, check that that window
// isn't currently disabled (eg. modal dialog). If it is keep searching.
if ((::GetWindowLong(hwnd, GWL_STYLE) & WS_DISABLED) == 0) {
//return the window handle and stop searching
*(HWND*)lParam = hwnd;
return FALSE;
}
}
return TRUE; // otherwise, continue searching
}
// forward declaration; implemented in appshell_extensions_win.cpp
void ConvertToUnixPath(ExtensionString& filename);
// Program entry point function.
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
g_appStartupTime = timeGetTime();
CefMainArgs main_args(hInstance);
CefRefPtr<ClientApp> app(new ClientApp);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, app.get());
if (exit_code >= 0)
return exit_code;
// Retrieve the current working directory.
if (_getcwd(szWorkingDir, MAX_PATH) == NULL)
szWorkingDir[0] = 0;
// Parse command line arguments. The passed in values are ignored on Windows.
AppInitCommandLine(0, NULL);
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_CEFCLIENT, szWindowClass, MAX_LOADSTRING);
// Determine if we should use an already running instance of Brackets.
HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, APP_NAME FIRST_INSTANCE_MUTEX_NAME);
if ((hMutex != NULL) && AppGetCommandLine()->HasArguments() && (lpCmdLine != NULL)) {
// for subsequent instances, re-use an already running instance if we're being called to
// open an existing file on the command-line (eg. Open With.. from Windows Explorer)
HWND hFirstInstanceWnd = NULL;
::EnumWindows(FindSuitableBracketsInstance, (LPARAM)&hFirstInstanceWnd);
if (hFirstInstanceWnd != NULL) {
::SetForegroundWindow(hFirstInstanceWnd);
if (::IsIconic(hFirstInstanceWnd))
::ShowWindow(hFirstInstanceWnd, SW_RESTORE);
// message the other Brackets instance to actually open the given filename
std::wstring wstrFilename = lpCmdLine;
ConvertToUnixPath(wstrFilename);
// note: WM_COPYDATA will manage passing the string across process space
COPYDATASTRUCT data;
data.dwData = ID_WM_COPYDATA_SENDOPENFILECOMMAND;
data.cbData = (wstrFilename.length() + 1) * sizeof(WCHAR);
data.lpData = (LPVOID)wstrFilename.c_str();
::SendMessage(hFirstInstanceWnd, WM_COPYDATA, (WPARAM)(HWND)hFirstInstanceWnd, (LPARAM)(LPVOID)&data);
// exit this instance
return 0;
}
// otherwise, fall thru and launch a new instance
}
if (hMutex == NULL) {
// first instance of this app, so create the mutex and continue execution of this instance.
hMutex = ::CreateMutex(NULL, FALSE, APP_NAME FIRST_INSTANCE_MUTEX_NAME);
}
CefSettings settings;
// Populate the settings based on command line arguments.
AppGetSettings(settings, app);
// Check command
if (CefString(&settings.cache_path).length() == 0) {
CefString(&settings.cache_path) = AppGetCachePath();
}
// Initialize CEF.
CefInitialize(main_args, settings, app.get());
// Register window class
MyRegisterClass(hInstance, *(app->GetCurrentLanguage().GetStruct()));
CefRefPtr<CefCommandLine> cmdLine = AppGetCommandLine();
if (cmdLine->HasSwitch(cefclient::kStartupPath)) {
wcscpy(szInitialUrl, cmdLine->GetSwitchValue(cefclient::kStartupPath).c_str());
}
else {
// If the shift key is not pressed, look for the index.html file
if (GetAsyncKeyState(VK_SHIFT) == 0) {
// Get the full pathname for the app. We look for the index.html
// file relative to this location.
wchar_t appPath[MAX_PATH];
wchar_t *pathRoot;
GetModuleFileName(NULL, appPath, MAX_PATH);
// Strip the .exe filename (and preceding "\") from the appPath
// and store in pathRoot
pathRoot = wcsrchr(appPath, '\\');
// Look for .\dev\src\index.html first
wcscpy(pathRoot, L"\\dev\\src\\index.html");
// If the file exists, use it
if (GetFileAttributes(appPath) != INVALID_FILE_ATTRIBUTES) {
wcscpy(szInitialUrl, appPath);
}
if (!wcslen(szInitialUrl)) {
// Look for .\www\index.html next
wcscpy(pathRoot, L"\\www\\index.html");
if (GetFileAttributes(appPath) != INVALID_FILE_ATTRIBUTES) {
wcscpy(szInitialUrl, appPath);
}
}
}
}
if (!wcslen(szInitialUrl)) {
// If we got here, either the startup file couldn't be found, or the user pressed the
// shift key while launching. Prompt to select the index.html file.
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szInitialUrl;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Web Files\0*.htm;*.html\0\0";
ofn.lpstrTitle = L"Please select the " APP_NAME L" index.html file.";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_EXPLORER;
if (!GetOpenFileName(&ofn)) {
// User cancelled, exit the app
CefShutdown();
return 0;
}
}
// Perform application initialization
if (!InitInstance (hInstance, nCmdShow))
return FALSE;
// Start the node server process
startNodeProcess();
gFilesToOpen = GetFilenamesFromCommandLine();
int result = 0;
if (!settings.multi_threaded_message_loop) {
// Run the CEF message loop. This function will block until the application
// recieves a WM_QUIT message.
CefRunMessageLoop();
} else {
MSG msg;
// Run the application message loop.
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
result = static_cast<int>(msg.wParam);
}
OnBeforeShutdown();
// Shut down CEF.
CefShutdown();
// release the first instance mutex
if (hMutex != NULL)
ReleaseMutex(hMutex);
return result;
}
// Add trailing separator, if necessary
void EnsureTrailingSeparator(LPWSTR pRet)
{
if (!pRet)
return;
int len = wcslen(pRet);
if (len > 0 && wcscmp(&(pRet[len-1]), L"\\") != 0)
{
wcscat(pRet, L"\\");
}
}
// Helper method to build Registry Key string
void GetKey(LPCWSTR pBase, LPCWSTR pGroup, LPCWSTR pApp, LPCWSTR pFolder, LPWSTR pRet)
{
// Check for required params
ASSERT(pBase && pApp && pRet);
if (!pBase || !pApp || !pRet)
return;
// Base
wcscpy(pRet, pBase);
// Group (optional)
if (pGroup && (pGroup[0] != '\0'))
{
EnsureTrailingSeparator(pRet);
wcscat(pRet, pGroup);
}
// App name
EnsureTrailingSeparator(pRet);
wcscat(pRet, pApp);
// Folder (optional)
if (pFolder && (pFolder[0] != '\0'))
{
EnsureTrailingSeparator(pRet);
wcscat(pRet, pFolder);
}
}
// get integer value from registry key
// caller can either use return value to determine success/fail, or pass a default to be used on fail
bool GetRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int* pDefault, int& ret)
{
HKEY hKey;
bool result = false;
wchar_t key[MAX_PATH];
key[0] = '\0';
GetKey(PREF_APPSHELL_BASE, GROUP_NAME, APP_NAME, pFolder, (LPWSTR)&key);
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, (LPCWSTR)key, 0, KEY_READ, &hKey))
{
DWORD dwValue = 0;
DWORD dwType = 0;
DWORD dwCount = sizeof(DWORD);
if (ERROR_SUCCESS == RegQueryValueEx(hKey, pEntry, NULL, &dwType, (LPBYTE)&dwValue, &dwCount))
{
result = true;
ASSERT(dwType == REG_DWORD);
ASSERT(dwCount == sizeof(dwValue));
ret = (int)dwValue;
}
RegCloseKey(hKey);
}
if (!result)
{
// couldn't read value, so use default, if specified
if (pDefault)
ret = *pDefault;
}
return result;
}
// write integer value to registry key
bool WriteRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int val)
{
HKEY hKey;
bool result = false;
wchar_t key[MAX_PATH];
key[0] = '\0';
GetKey(PREF_APPSHELL_BASE, GROUP_NAME, APP_NAME, pFolder, (LPWSTR)&key);
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, (LPCWSTR)key, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL))
{
DWORD dwCount = sizeof(int);
if (ERROR_SUCCESS == RegSetValueEx(hKey, pEntry, 0, REG_DWORD, (LPBYTE)&val, dwCount))
result = true;
RegCloseKey(hKey);
}
return result;
}
void SaveWindowRect(HWND hWnd)
{
// Save position of active window
if (!hWnd)
return;
WINDOWPLACEMENT wp;
memset(&wp, 0, sizeof(WINDOWPLACEMENT));
wp.length = sizeof(WINDOWPLACEMENT);
if (GetWindowPlacement(hWnd, &wp))
{
// Only save window positions for "restored" and "maximized" states.
// If window is closed while "minimized", we don't want it to open minimized
// for next session, so don't update registry so it opens in previous state.
if (wp.showCmd == SW_SHOWNORMAL || wp.showCmd == SW_SHOW || wp.showCmd == SW_MAXIMIZE)
{
RECT rect;
if (GetWindowRect(hWnd, &rect))
{
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_LEFT, rect.left);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_TOP, rect.top);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_WIDTH, rect.right - rect.left);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_HEIGHT, rect.bottom - rect.top);
}
if (wp.showCmd == SW_MAXIMIZE)
{
// When window is maximized, we also store the "restore" size
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_LEFT, wp.rcNormalPosition.left);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_TOP, wp.rcNormalPosition.top);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_RIGHT, wp.rcNormalPosition.right);
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_BOTTOM, wp.rcNormalPosition.bottom);
}
// Maximize is the only special case we handle
WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_SHOWSTATE,
(wp.showCmd == SW_MAXIMIZE) ? SW_MAXIMIZE : SW_SHOW);
}
}
}
void RestoreWindowRect(int& left, int& top, int& width, int& height, int& showCmd)
{
// Start with Registry data
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_LEFT, NULL, left);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_TOP, NULL, top);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_WIDTH, NULL, width);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_HEIGHT, NULL, height);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_SHOWSTATE, NULL, showCmd);
// If window size has changed, we may need to alter window size
HMONITOR hMonitor;
MONITORINFO mi;
RECT rcOrig, rcMax;
// Get the nearest monitor to the passed rect
rcOrig.left = left;
rcOrig.top = top;
rcOrig.right = left + width;
rcOrig.bottom = top + height;
hMonitor = MonitorFromRect(&rcOrig, MONITOR_DEFAULTTONEAREST);
// Get the monitor rect
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
rcMax = mi.rcMonitor;
if (showCmd == SW_MAXIMIZE)
{
// For maximized case, just use monitor dimensions
left = rcMax.left;
top = rcMax.top;
width = rcMax.right - rcMax.left;
height = rcMax.bottom - rcMax.top;
}
else
{
// For non-maximized case, adjust window to fit on screen
// make sure width fits
int rcMaxWidth = static_cast<int>(rcMax.right - rcMax.left);
if (width > rcMaxWidth)
width = rcMaxWidth;
// make sure left is on screen
if (left < rcMax.left)
left = static_cast<int>(rcMax.left);
// make sure right is on screen
if ((left + width) > rcMax.right)
left = static_cast<int>(rcMax.right) - width;
// make sure height fits
int rcMaxHeight = static_cast<int>(rcMax.bottom - rcMax.top);
if (height > rcMaxHeight)
height = rcMaxHeight;
// make sure top is on screen
if (top < rcMax.top)
top = static_cast<int>(rcMax.top);
// make sure bottom is on screen
if ((top + height) > rcMax.bottom)
top = static_cast<int>(rcMax.bottom) - height;
}
}
void RestoreWindowPlacement(HWND hWnd, int showCmd)
{
if (!hWnd)
return;
// If window is maximized, set the "restore" window position
if (showCmd == SW_MAXIMIZE)
{
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
wp.flags = 0;
wp.showCmd = SW_MAXIMIZE;
wp.ptMinPosition.x = -1;
wp.ptMinPosition.y = -1;
wp.ptMaxPosition.x = -1;
wp.ptMaxPosition.y = -1;
wp.rcNormalPosition.left = CW_USEDEFAULT;
wp.rcNormalPosition.top = CW_USEDEFAULT;
wp.rcNormalPosition.right = CW_USEDEFAULT;
wp.rcNormalPosition.bottom = CW_USEDEFAULT;
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_LEFT, NULL, (int&)wp.rcNormalPosition.left);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_TOP, NULL, (int&)wp.rcNormalPosition.top);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_RIGHT, NULL, (int&)wp.rcNormalPosition.right);
GetRegistryInt(PREF_WINPOS_FOLDER, PREF_RESTORE_BOTTOM, NULL, (int&)wp.rcNormalPosition.bottom);
// This returns an error code, but not sure what we could do on an error
SetWindowPlacement(hWnd, &wp);
}
ShowWindow(hWnd, showCmd);
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this
// function so that the application will get 'well formed' small icons
// associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, const cef_string_t& locale) {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CEFCLIENT));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
hInst = hInstance; // Store instance handle in our global variable
// TODO: test this cases:
// - window in secondary monitor when shutdown, disconnect secondary monitor, restart
int left = CW_USEDEFAULT;
int top = CW_USEDEFAULT;
int width = CW_USEDEFAULT;
int height = CW_USEDEFAULT;
int showCmd = SW_SHOW;
RestoreWindowRect(left, top, width, height, showCmd);
DWORD styles = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
if (showCmd == SW_MAXIMIZE)
styles |= WS_MAXIMIZE;
hWndMain = CreateWindow(szWindowClass, szTitle, styles,
left, top, width, height, NULL, NULL, hInstance, NULL);
if (!hWndMain)
return FALSE;
RestoreWindowPlacement(hWndMain, showCmd);
UpdateWindow(hWndMain);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
static HWND backWnd = NULL, forwardWnd = NULL, reloadWnd = NULL,
stopWnd = NULL, editWnd = NULL;
static WNDPROC editWndOldProc = NULL;
// Static members used for the find dialog.
static FINDREPLACE fr;
static WCHAR szFindWhat[80] = {0};
static WCHAR szLastFindWhat[80] = {0};
static bool findNext = false;
static bool lastMatchCase = false;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
#ifdef SHOW_TOOLBAR_UI
if (hWnd == editWnd) {
// Callback for the edit window
switch (message) {
case WM_CHAR:
if (wParam == VK_RETURN && g_handler.get()) {
// When the user hits the enter key load the URL
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
wchar_t strPtr[MAX_URL_LENGTH+1] = {0};
*((LPWORD)strPtr) = MAX_URL_LENGTH;
LRESULT strLen = SendMessage(hWnd, EM_GETLINE, 0, (LPARAM)strPtr);
if (strLen > 0) {
strPtr[strLen] = 0;
browser->GetMainFrame()->LoadURL(strPtr);
}
return 0;
}
}
return (LRESULT)CallWindowProc(editWndOldProc, hWnd, message, wParam,
lParam);
} else
#endif // SHOW_TOOLBAR_UI
{
// Callback for the main window
switch (message) {
case WM_CREATE: {
// Create the single static handler class instance
g_handler = new ClientHandler();
g_handler->SetMainHwnd(hWnd);
// Create the child windows used for navigation
RECT rect;
int x = 0;
GetClientRect(hWnd, &rect);
#ifdef SHOW_TOOLBAR_UI
backWnd = CreateWindow(L"BUTTON", L"Back",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
| WS_DISABLED, x, 0, BUTTON_WIDTH, URLBAR_HEIGHT,
hWnd, (HMENU) IDC_NAV_BACK, hInst, 0);
x += BUTTON_WIDTH;
forwardWnd = CreateWindow(L"BUTTON", L"Forward",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
| WS_DISABLED, x, 0, BUTTON_WIDTH,
URLBAR_HEIGHT, hWnd, (HMENU) IDC_NAV_FORWARD,
hInst, 0);
x += BUTTON_WIDTH;
reloadWnd = CreateWindow(L"BUTTON", L"Reload",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
| WS_DISABLED, x, 0, BUTTON_WIDTH,
URLBAR_HEIGHT, hWnd, (HMENU) IDC_NAV_RELOAD,
hInst, 0);
x += BUTTON_WIDTH;
stopWnd = CreateWindow(L"BUTTON", L"Stop",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON
| WS_DISABLED, x, 0, BUTTON_WIDTH, URLBAR_HEIGHT,
hWnd, (HMENU) IDC_NAV_STOP, hInst, 0);
x += BUTTON_WIDTH;
editWnd = CreateWindow(L"EDIT", 0,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT |
ES_AUTOVSCROLL | ES_AUTOHSCROLL| WS_DISABLED,
x, 0, rect.right - BUTTON_WIDTH * 4,
URLBAR_HEIGHT, hWnd, 0, hInst, 0);
// Assign the edit window's WNDPROC to this function so that we can
// capture the enter key
editWndOldProc =
reinterpret_cast<WNDPROC>(GetWindowLongPtr(editWnd, GWLP_WNDPROC));
SetWindowLongPtr(editWnd, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(WndProc));
g_handler->SetEditHwnd(editWnd);
g_handler->SetButtonHwnds(backWnd, forwardWnd, reloadWnd, stopWnd);
rect.top += URLBAR_HEIGHT;
#endif // SHOW_TOOLBAR_UI
CefWindowInfo info;
CefBrowserSettings settings;
settings.web_security = STATE_DISABLED;
// Initialize window info to the defaults for a child window
info.SetAsChild(hWnd, rect);
// Creat the new child browser window
CefBrowserHost::CreateBrowser(info,
static_cast<CefRefPtr<CefClient> >(g_handler),
szInitialUrl, settings);
return 0;
}
case WM_COMMAND: {
CefRefPtr<CefBrowser> browser;
if (g_handler.get())
browser = g_handler->GetBrowser();
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId) {
case IDM_EXIT:
if (g_handler.get()) {
g_handler->QuittingApp(true);
g_handler->DispatchCloseToNextBrowser();
} else {
DestroyWindow(hWnd);
}
return 0;
case ID_WARN_CONSOLEMESSAGE:
/*
if (g_handler.get()) {
std::wstringstream ss;
ss << L"Console messages will be written to "
<< std::wstring(CefString(g_handler->GetLogFile()));
MessageBox(hWnd, ss.str().c_str(), L"Console Messages",
MB_OK | MB_ICONINFORMATION);
}
*/
return 0;
case ID_WARN_DOWNLOADCOMPLETE:
case ID_WARN_DOWNLOADERROR:
if (g_handler.get()) {
std::wstringstream ss;
ss << L"File \"" <<
std::wstring(CefString(g_handler->GetLastDownloadFile())) <<
L"\" ";
if (wmId == ID_WARN_DOWNLOADCOMPLETE)
ss << L"downloaded successfully.";
else
ss << L"failed to download.";
MessageBox(hWnd, ss.str().c_str(), L"File Download",
MB_OK | MB_ICONINFORMATION);
}
return 0;
#ifdef SHOW_TOOLBAR_UI
case IDC_NAV_BACK: // Back button
if (browser.get())
browser->GoBack();
return 0;
case IDC_NAV_FORWARD: // Forward button
if (browser.get())
browser->GoForward();
return 0;
case IDC_NAV_RELOAD: // Reload button
if (browser.get())
browser->Reload();
return 0;
case IDC_NAV_STOP: // Stop button
if (browser.get())
browser->StopLoad();
return 0;
#endif // SHOW_TOOLBAR_UI
default:
ExtensionString commandId = NativeMenuModel::getInstance(getMenuParent(g_handler->GetBrowser())).getCommandId(wmId);
if (commandId.size() > 0) {
CefRefPtr<CommandCallback> callback = new EditCommandCallback(g_handler->GetBrowser(), commandId);
g_handler->SendJSCommand(g_handler->GetBrowser(), commandId, callback);
}
}
break;
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0;
case WM_SETFOCUS:
if (g_handler.get() && g_handler->GetBrowser()) {
// Pass focus to the browser window
CefWindowHandle hwnd =
g_handler->GetBrowser()->GetHost()->GetWindowHandle();
if (hwnd)
PostMessage(hwnd, WM_SETFOCUS, wParam, NULL);
}
return 0;
case WM_SIZE:
// Minimizing resizes the window to 0x0 which causes our layout to go all
// screwy, so we just ignore it.
if (wParam != SIZE_MINIMIZED && g_handler.get() &&
g_handler->GetBrowser()) {
CefWindowHandle hwnd =
g_handler->GetBrowser()->GetHost()->GetWindowHandle();
if (hwnd) {
// Resize the browser window and address bar to match the new frame
// window size
RECT rect;
GetClientRect(hWnd, &rect);
#ifdef SHOW_TOOLBAR_UI
rect.top += URLBAR_HEIGHT;
int urloffset = rect.left + BUTTON_WIDTH * 4;
#endif // SHOW_TOOLBAR_UI
HDWP hdwp = BeginDeferWindowPos(1);
#ifdef SHOW_TOOLBAR_UI
hdwp = DeferWindowPos(hdwp, editWnd, NULL, urloffset,
0, rect.right - urloffset, URLBAR_HEIGHT, SWP_NOZORDER);
#endif // SHOW_TOOLBAR_UI
hdwp = DeferWindowPos(hdwp, hwnd, NULL,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER);
EndDeferWindowPos(hdwp);
}
}
break;
case WM_ERASEBKGND:
if (g_handler.get() && g_handler->GetBrowser()) {
CefWindowHandle hwnd =
g_handler->GetBrowser()->GetHost()->GetWindowHandle();
if (hwnd) {
// Dont erase the background if the browser window has been loaded
// (this avoids flashing)
return 0;
}
}
break;
case WM_CLOSE:
if (g_handler.get()) {
HWND hWnd = GetActiveWindow();
SaveWindowRect(hWnd);
// If we already initiated the browser closing, then let default window proc handle it.
HWND browserHwnd = g_handler->GetBrowser()->GetHost()->GetWindowHandle();
HANDLE closing = GetProp(browserHwnd, CLOSING_PROP);
if (closing) {
RemoveProp(browserHwnd, CLOSING_PROP);
break;
}
g_handler->QuittingApp(true);
g_handler->DispatchCloseToNextBrowser();
return 0;
}
break;
case WM_DESTROY:
// The frame window has exited
PostQuitMessage(0);
return 0;
case WM_COPYDATA:
// handle the interprocess communication request from another Brackets running instance
if (lParam != NULL) {
PCOPYDATASTRUCT data = (PCOPYDATASTRUCT)lParam;
if ((data->dwData == ID_WM_COPYDATA_SENDOPENFILECOMMAND) && (data->cbData > 0)) {
// another Brackets instance requests that we open the given filename
std::wstring wstrFilename = (LPCWSTR)data->lpData;
// Windows Explorer might enclose the filename in double-quotes. We need to strip these off.
if ((wstrFilename.front() == '\"') && wstrFilename.back() == '\"')
wstrFilename = wstrFilename.substr(1, wstrFilename.length() - 2);
ASSERT(g_handler != NULL);
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
// call into Javascript code to handle the open file command
ASSERT(browser != NULL);
g_handler->SendOpenFileCommand(browser, CefString(wstrFilename.c_str()));
}
}
break;
case WM_INITMENUPOPUP:
// Notify before popping up
g_handler->SendJSCommand(g_handler->GetBrowser(), APP_BEFORE_MENUPOPUP);
HMENU menu = (HMENU)wParam;
int count = GetMenuItemCount(menu);
void* menuParent = getMenuParent(g_handler->GetBrowser());
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;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
UNREFERENCED_PARAMETER(lParam);
switch (message) {
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
// Global functions