10
#include "../../../../C/Alloc.h"
13
#include "Common/Defs.h"
14
#include "Common/StringConvert.h"
15
// #include "Common/CommandLineParser.h"
17
// FIXME #include "Windows/Control/Toolbar.h"
18
#include "Windows/Error.h"
19
#include "Windows/COM.h"
20
#include "Windows/DLL.h"
21
// FIXME #include "Windows/Security.h"
22
// FIXME #include "Windows/MemoryLock.h"
24
#include "ViewSettings.h"
25
#include "../GUI/ExtractRes.h"
28
#include "StringUtils.h"
30
#include "MyLoadMenu.h"
31
#include "LangUtils.h"
32
#include "FormatUtils.h"
33
#include "RegistryUtils.h"
35
using namespace NWindows;
36
using namespace NFile;
37
using namespace NFind;
38
// using namespace NCommandLineParser;
40
#define MAX_LOADSTRING 100
42
#define MENU_HEIGHT 26
47
HINSTANCE g_hInstance;
49
bool g_OpenArchive = false;
50
static UString g_MainPath;
52
const int kNumDefaultPanels = 1;
54
const int kSplitterWidth = 4;
55
int kSplitterRateMax = 1 << 16;
57
// bool OnMenuCommand(HWND hWnd, int id);
59
static void local_WM_CREATE(HWND hWnd);
62
static UString GetProgramPath()
65
NDLL::MyGetModuleFileName(g_hInstance, s);
69
UString GetProgramFolderPrefix()
71
UString path = GetProgramPath();
72
int pos = path.ReverseFind(WCHAR_PATH_SEPARATOR);
73
return path.Left(pos + 1);
79
int _ratio; // 10000 is max
82
void SetRatioFromPos(HWND hWnd)
83
{ _ratio = (_pos + kSplitterWidth / 2) * kSplitterRateMax /
84
MyMax(GetWidth(hWnd), 1); }
88
int GetWidth(HWND hWnd) const
91
::GetClientRect(hWnd, &rect);
94
void SetRatio(HWND hWnd, int aRatio)
97
SetPosFromRatio(hWnd);
99
void SetPosPure(HWND hWnd, int pos)
101
int posMax = GetWidth(hWnd) - kSplitterWidth;
108
void SetPos(HWND hWnd, int pos)
110
_fullWidth = GetWidth(hWnd);
111
SetPosPure(hWnd, pos);
112
SetRatioFromPos(hWnd);
114
void SetPosFromRatio(HWND hWnd)
116
int fullWidth = GetWidth(hWnd);
117
if (_fullWidth != fullWidth)
119
_fullWidth = fullWidth;
120
SetPosPure(hWnd, GetWidth(hWnd) * _ratio / kSplitterRateMax - kSplitterWidth / 2);
126
bool g_CanChangeSplitter = false;
127
UINT32 g_SplitterPos = 0;
128
// FIXME CSplitterPos g_Splitter;
129
bool g_PanelsInfoDefined = false;
131
int g_StartCaptureMousePos;
132
int g_StartCaptureSplitterPos;
136
void MoveSubWindows(HWND hWnd);
137
void OnSize(HWND hWnd);
139
// FIXME LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
141
const wchar_t *kWindowClass = L"FM";
144
static bool IsItWindowsNT()
146
OSVERSIONINFO versionInfo;
147
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
148
if (!::GetVersionEx(&versionInfo))
150
return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
154
// FUNCTION: InitInstance(HANDLE, int)
155
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
159
g_hInstance = hInstance;
163
// LoadString(hInstance, IDS_CLASS, windowClass, MAX_LOADSTRING);
165
// LoadString(hInstance, IDS_APP_TITLE, title, MAX_LOADSTRING);
166
UString title = LangString(IDS_APP_TITLE, 0x03000000);
169
//If it is already running, then focus on the window
170
hWnd = FindWindow(windowClass, title);
173
SetForegroundWindow ((HWND) (((DWORD)hWnd) | 0x01));
181
// wc.style = CS_HREDRAW | CS_VREDRAW;
183
wc.lpfnWndProc = (WNDPROC) WndProc;
186
wc.hInstance = hInstance;
187
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FAM));
189
// wc.hCursor = LoadCursor (NULL, IDC_ARROW);
190
wc.hCursor = ::LoadCursor(0, IDC_SIZEWE);
191
// wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
192
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
194
wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MENU);
195
wc.lpszClassName = kWindowClass;
197
MyRegisterClass(&wc);
200
// GetClientRect(hWnd, &rect);
202
DWORD style = WS_OVERLAPPEDWINDOW;
206
bool maximized = false;
207
int x , y, xSize, ySize;
208
x = y = xSize = ySize = CW_USEDEFAULT;
209
bool windowPosIsRead = ReadWindowSize(rect, maximized);
215
xSize = rect.right - rect.left;
216
ySize = rect.bottom - rect.top;
220
UINT32 numPanels, currentPanel;
221
g_PanelsInfoDefined = ReadPanelsInfo(numPanels, currentPanel, g_SplitterPos);
222
if (g_PanelsInfoDefined)
224
if (numPanels < 1 || numPanels > 2)
225
numPanels = kNumDefaultPanels;
226
if (currentPanel >= 2)
231
numPanels = kNumDefaultPanels;
234
g_App.NumPanels = numPanels;
235
g_App.LastFocusedPanel = currentPanel;
237
#ifdef _WIN32 // FIXME
238
if (!wnd.Create(kWindowClass, title, style,
239
x, y, xSize, ySize, NULL, NULL, hInstance, NULL))
244
extern HWND myCreateAndShowMainWindow(LPCTSTR title,void (*fct)(HWND));
245
g_HWND = myCreateAndShowMainWindow(title,local_WM_CREATE);
250
WINDOWPLACEMENT placement;
251
placement.length = sizeof(placement);
252
if (wnd.GetPlacement(&placement))
254
if (nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOW ||
255
nCmdShow == SW_SHOWDEFAULT)
258
placement.showCmd = SW_SHOWMAXIMIZED;
260
placement.showCmd = SW_SHOWNORMAL;
263
placement.showCmd = nCmdShow;
265
placement.rcNormalPosition = rect;
266
wnd.SetPlacement(&placement);
267
// window.Show(nCmdShow);
276
static void GetCommands(const UString &aCommandLine, UString &aCommands)
278
UString aProgramName;
280
bool aQuoteMode = false;
281
for (int i = 0; i < aCommandLine.Length(); i++)
283
wchar_t aChar = aCommandLine[i];
285
aQuoteMode = !aQuoteMode;
286
else if (aChar == L' ' && !aQuoteMode)
295
aProgramName += aChar;
297
aCommands = aCommandLine.Mid(i);
302
DWORD GetDllVersion(LPCTSTR lpszDllName)
306
hinstDll = LoadLibrary(lpszDllName);
309
DLLGETVERSIONPROC pDllGetVersion;
310
pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hinstDll, "DllGetVersion");
312
/*Because some DLLs might not implement this function, you
313
must test for it explicitly. Depending on the particular
314
DLL, the lack of a DllGetVersion function can be a useful
315
indicator of the version.
322
ZeroMemory(&dvi, sizeof(dvi));
323
dvi.cbSize = sizeof(dvi);
325
hr = (*pDllGetVersion)(&dvi);
329
dwVersion = MAKELONG(dvi.dwMinorVersion, dvi.dwMajorVersion);
332
FreeLibrary(hinstDll);
337
DWORD g_ComCtl32Version;
342
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
344
static bool IsWow64()
346
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
347
GetModuleHandle("kernel32"), "IsWow64Process");
348
if (fnIsWow64Process == NULL)
351
if (!fnIsWow64Process(GetCurrentProcess(),&isWow))
353
return isWow != FALSE;
359
bool IsLargePageSupported()
364
OSVERSIONINFO versionInfo;
365
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
366
if (!::GetVersionEx(&versionInfo))
368
if (versionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT || versionInfo.dwMajorVersion < 5)
370
if (versionInfo.dwMajorVersion > 5)
372
if (versionInfo.dwMinorVersion < 1)
374
if (versionInfo.dwMinorVersion > 1)
381
static void SetMemoryLock()
383
if (!IsLargePageSupported())
385
// if (ReadLockMemoryAdd())
386
NSecurity::AddLockMemoryPrivilege();
388
if (ReadLockMemoryEnable())
389
NSecurity::EnableLockMemoryPrivilege();
394
static const int kNumSwitches = 1;
404
static const CSwitchForm kSwitchForms[kNumSwitches] =
406
{ L"SOA", NSwitchType::kSimple, false },
410
// int APIENTRY WinMain2(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */);
413
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int nCmdShow)
416
g_IsNT = IsItWindowsNT();
423
InitCommonControls();
425
g_ComCtl32Version = ::GetDllVersion(TEXT("comctl32.dll"));
427
// OleInitialize is required for drag and drop.
429
// Maybe needs CoInitializeEx also ?
430
// NCOM::CComInitializer comInitializer;
432
UString programString, commandsString;
433
// MessageBoxW(0, GetCommandLineW(), L"", 0);
434
SplitStringToTwoStrings(GetCommandLineW(), programString, commandsString);
436
commandsString.Trim();
437
UString paramString, tailString;
438
SplitStringToTwoStrings(commandsString, paramString, tailString);
441
if (!paramString.IsEmpty())
443
g_MainPath = paramString;
444
// return WinMain2(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
446
// MessageBoxW(0, paramString, L"", 0);
449
UStringVector commandStrings;
450
NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
451
NCommandLineParser::CParser parser(kNumSwitches);
454
parser.ParseStrings(kSwitchForms, commandStrings);
455
const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;
456
if(nonSwitchStrings.Size() > 1)
458
g_MainPath = nonSwitchStrings[1];
459
// g_OpenArchive = parser[NKey::kOpenArachive].ThereIs;
461
if (FindFile(g_MainPath, fileInfo))
463
if (!fileInfo.IsDir())
464
g_OpenArchive = true;
475
if (!InitInstance (hInstance, nCmdShow))
483
HACCEL hAccels = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(IDR_ACCELERATOR1));
484
while (GetMessageW(&msg, NULL, 0, 0))
486
if (TranslateAcceleratorW(g_HWND, hAccels, &msg) == 0)
488
TranslateMessage(&msg);
489
DispatchMessageW(&msg);
496
HACCEL hAccels = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));
497
while (GetMessage(&msg, NULL, 0, 0))
499
if (TranslateAccelerator(g_HWND, hAccels, &msg) == 0)
501
// if (g_Hwnd != NULL || !IsDialogMessage(g_Hwnd, &msg))
502
// if (!IsDialogMessage(g_Hwnd, &msg))
503
TranslateMessage(&msg);
504
DispatchMessage(&msg);
511
return (int)msg.wParam;
514
static void SaveWindowInfo(HWND aWnd)
518
if (!::GetWindowRect(aWnd, &rect))
521
WINDOWPLACEMENT placement;
522
placement.length = sizeof(placement);
523
if (!::GetWindowPlacement(aWnd, &placement))
525
SaveWindowSize(placement.rcNormalPosition,
526
BOOLToBool(::IsZoomed(aWnd)));
527
SavePanelsInfo(g_App.NumPanels, g_App.LastFocusedPanel,
528
g_Splitter.GetPos());
531
int Main1(int argc,TCHAR **argv)
535
g_MainPath = argv[1];
538
if (!InitInstance (0, 0))
543
// FIXME : install Accelerators ?
550
void ExecuteCommand(UINT commandID)
552
printf("FM - ExecuteCommand(%d)\n",commandID);
556
g_App.AddToArchive();
558
case kExtractCommand:
559
g_App.ExtractArchives();
562
g_App.TestArchives();
568
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
574
wmId = LOWORD(wParam);
575
wmEvent = HIWORD(wParam);
576
if ((HWND) lParam != NULL && wmEvent != 0)
578
if (wmId >= kToolbarStartID)
580
ExecuteCommand(wmId);
583
if (OnMenuCommand(hWnd, wmId))
586
case WM_INITMENUPOPUP:
587
OnMenuActivating(hWnd, HMENU(wParam), LOWORD(lParam));
592
case WM_EXITMENULOOP:
594
OnMenuUnActivating(hWnd);
597
case WM_UNINITMENUPOPUP:
598
OnMenuUnActivating(hWnd, HMENU(wParam), lParam);
606
INITCOMMONCONTROLSEX icex;
607
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
608
icex.dwICC = ICC_BAR_CLASSES;
609
InitCommonControlsEx(&icex);
611
// Toolbar buttons used to create the first 4 buttons.
614
// {0, 0, TBSTATE_ENABLED, BTNS_SEP, 0L, 0},
615
// {VIEW_PARENTFOLDER, kParentFolderID, TBSTATE_ENABLED, BTNS_BUTTON, 0L, 0},
616
// {0, 0, TBSTATE_ENABLED, BTNS_SEP, 0L, 0},
617
{VIEW_NEWFOLDER, ID_FILE_CREATEFOLDER, TBSTATE_ENABLED, BTNS_BUTTON, 0L, 0},
621
NWindows::NControl::CToolBar aToolBar;
622
aToolBar.Attach(::CreateToolbarEx (hWnd,
623
WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS, // | TBSTYLE_FLAT
625
(HINSTANCE)HINST_COMMCTRL, IDB_VIEW_SMALL_COLOR,
626
(LPCTBBUTTON)&tbb, sizeof(tbb) / sizeof(tbb[0]),
627
0, 0, 100, 30, sizeof (TBBUTTON)));
629
// HCURSOR cursor = ::LoadCursor(0, IDC_SIZEWE);
630
// ::SetCursor(cursor);
632
if (g_PanelsInfoDefined)
633
g_Splitter.SetPos(hWnd, g_SplitterPos);
636
g_Splitter.SetRatio(hWnd, kSplitterRateMax / 2);
637
g_SplitterPos = g_Splitter.GetPos();
641
::GetClientRect(hWnd, &rect);
642
int xSize = rect.right;
644
xSizes[0] = g_Splitter.GetPos();
645
xSizes[1] = xSize - kSplitterWidth - xSizes[0];
649
g_App.CreateDragTarget();
650
bool archiveIsOpened;
652
bool needOpenFile = false;
653
if (!g_MainPath.IsEmpty() /* && g_OpenArchive */)
655
NFile::NFind::CFileInfoW fileInfo;
656
if (NFile::NFind::FindFile(g_MainPath, fileInfo))
657
if (!fileInfo.IsDir())
660
g_App.Create(hWnd, g_MainPath, xSizes, archiveIsOpened, encrypted);
662
if (needOpenFile && !archiveIsOpened)
666
message = MyFormatNew(IDS_CANT_OPEN_ENCRYPTED_ARCHIVE, 0x0200060A, g_MainPath);
668
message = MyFormatNew(IDS_CANT_OPEN_ARCHIVE, 0x02000609, g_MainPath);
669
MessageBoxW(0, message, L"7-zip", MB_ICONERROR);
672
// g_SplitterPos = 0;
674
// ::DragAcceptFiles(hWnd, TRUE);
675
RegisterDragDrop(hWnd, g_App._dropTarget);
681
// ::DragAcceptFiles(hWnd, FALSE);
682
RevokeDragDrop(hWnd);
683
g_App._dropTarget.Release();
687
SaveWindowInfo(hWnd);
698
g_StartCaptureMousePos = LOWORD(lParam);
699
g_StartCaptureSplitterPos = g_Splitter.GetPos();
709
if ((wParam & MK_LBUTTON) != 0 && ::GetCapture() == hWnd)
711
g_Splitter.SetPos(hWnd, g_StartCaptureSplitterPos +
712
(short)LOWORD(lParam) - g_StartCaptureMousePos);
713
MoveSubWindows(hWnd);
720
if (g_CanChangeSplitter)
721
g_Splitter.SetPosFromRatio(hWnd);
724
g_Splitter.SetPos(hWnd, g_SplitterPos );
725
g_CanChangeSplitter = true;
730
int xSize = LOWORD(lParam);
731
int ySize = HIWORD(lParam);
732
// int xSplitter = 2;
733
int xWidth = g_SplitPos;
734
// int xSplitPos = xWidth;
735
g_Panel[0]._listView.MoveWindow(0, 0, xWidth, ySize);
736
g_Panel[1]._listView.MoveWindow(xSize - xWidth, 0, xWidth, ySize);
742
// g_App.SetFocus(g_App.LastFocusedPanel);
743
g_App.SetFocusToLastItem();
748
int fActive = LOWORD(wParam);
753
// g_FocusIndex = g_App.LastFocusedPanel;
754
// g_App.LastFocusedPanel = g_App.GetFocusedPanelIndex();
762
case kLangWasChangedMessage:
768
case WM_SETTINGCHANGE:
773
g_App.OnNotify((int)wParam, (LPNMHDR)lParam);
779
g_App.GetFocusedPanel().CompressDropFiles((HDROP)wParam);
786
return DefWindowProcW(hWnd, message, wParam, lParam);
789
return DefWindowProc(hWnd, message, wParam, lParam);
793
void OnSize(HWND hWnd)
799
::GetClientRect(hWnd, &rect);
800
int xSize = rect.right;
801
int ySize = rect.bottom;
803
// g_App._rebar.SizeToRect(&rect);
804
// g_App._rebar.Move(0, 0, xSize, ySize);
807
MoveSubWindows(hWnd);
810
int Window_GetRealHeight(NWindows::CWindow &w)
813
WINDOWPLACEMENT placement;
814
w.GetWindowRect(&rect);
815
int res = rect.bottom - rect.top;
816
if (w.GetPlacement(&placement))
817
res += placement.rcNormalPosition.top;
821
static void local_WM_CREATE(HWND hWnd)
823
printf("**local_WM_CREATE**\n");
825
if (g_PanelsInfoDefined)
826
g_Splitter.SetPos(hWnd, g_SplitterPos);
829
g_Splitter.SetRatio(hWnd, kSplitterRateMax / 2);
830
g_SplitterPos = g_Splitter.GetPos();
834
::GetClientRect(hWnd, &rect);
835
int xSize = rect.right;
837
xSizes[0] = g_Splitter.GetPos();
838
xSizes[1] = xSize - kSplitterWidth - xSizes[0];
842
g_App.CreateDragTarget();
844
int xSizes[2] = { 0,0 };
846
bool archiveIsOpened;
848
bool needOpenFile = false;
849
if (!g_MainPath.IsEmpty() /* && g_OpenArchive */)
851
NFile::NFind::CFileInfoW fileInfo;
852
if (NFile::NFind::FindFile(g_MainPath, fileInfo))
853
if (!fileInfo.IsDir())
856
g_App.Create(hWnd, g_MainPath, xSizes, archiveIsOpened, encrypted);
858
if (needOpenFile && !archiveIsOpened)
862
message = MyFormatNew(IDS_CANT_OPEN_ENCRYPTED_ARCHIVE, 0x0200060A, g_MainPath);
864
message = MyFormatNew(IDS_CANT_OPEN_ARCHIVE, 0x02000609, g_MainPath);
865
MessageBoxW(0, message, L"7-zip", MB_ICONERROR);
868
// g_SplitterPos = 0;
870
// FIXME RegisterDragDrop(hWnd, g_App._dropTarget);
874
void main_WM_DESTROY()
876
// RevokeDragDrop(hWnd);
877
// g_App._dropTarget.Release();
878
printf("main_WM_DESTROY\n");
881
// SaveWindowInfo(hWnd);
882
// PostQuitMessage(0);
886
void MoveSubWindows(HWND hWnd)
890
::GetClientRect(hWnd, &rect);
891
int xSize = rect.right;
894
headerSize = Window_GetRealHeight(g_App._rebar);
895
int ySize = MyMax((int)(rect.bottom - headerSize), 0);
897
if (g_App.NumPanels > 1)
899
g_App.Panels[0].Move(0, headerSize, g_Splitter.GetPos(), ySize);
900
int xWidth1 = g_Splitter.GetPos() + kSplitterWidth;
901
g_App.Panels[1].Move(xWidth1, headerSize, xSize - xWidth1, ySize);
906
int otherPanel = 1 - g_App.LastFocusedPanel;
907
if (g_App.PanelsCreated[otherPanel])
908
g_App.Panels[otherPanel].Move(0, headerSize, 0, ySize);
910
g_App.Panels[g_App.LastFocusedPanel].Move(0, headerSize, xSize, ySize);