Debian Codegraph @

plugins/wlx/preview/src/preview.lpr

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 Explorer preview plugin
5
6 Copyright (C) 2021 Alexander Koblov ([email protected])
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 }
21
22 library Preview;
23
24 {$mode objfpc}{$H+}
25
26 uses
27 SysUtils, JwaWinUser, Windows, Messages, WlxPlugin, uPreviewHandler;
28
29 const
30 WH_KEYBOARD_LL = 13;
31 PREVIEW_HANDLER = UnicodeString('IPreviewHandler');
32
33 type
34 TPreviewData = class
35 Handler: IPreviewHandler;
36 end;
37
38 var
39 hhk: HHOOK;
40 Count: Integer = 0;
41 ProcessIdWide: PWideChar;
42 ProcessIdAtom: ATOM absolute ProcessIdWide;
43
44 procedure DLL_Entry_Hook(lpReserved : PtrInt);
45 begin
46 GlobalDeleteAtom(ProcessIdAtom);
47 end;
48
49 function LowLevelKeyboardProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
50 var
51 ParentWin: HWND;
52 ProcessId: HANDLE;
53 Msg: PKbDllHookStruct absolute lParam;
54
55 function IsKeyUp(Key: Integer): Boolean; inline;
56 begin
57 Result := (GetKeyState(Key) and $8000) = 0;
58 end;
59
60 begin
61 if (nCode = HC_ACTION) then
62 begin
63 if (wParam = WM_KEYDOWN) and (Msg^.vkCode = VK_ESCAPE) then
64 begin
65 ParentWin:= Windows.GetAncestor(GetForegroundWindow, GA_ROOT);
66 ProcessId:= Windows.GetPropW(ParentWin, ProcessIdWide);
67 if (ProcessId <> 0) and (ProcessId = GetProcessID) then
68 begin
69 if IsKeyUp(VK_MENU) and IsKeyUp(VK_CONTROL) and IsKeyUp(VK_MENU) then
70 begin
71 PostMessage(ParentWin, WM_SYSCOMMAND, SC_CLOSE, 0);
72 end;
73 end;
74 end;
75 end;
76 Result:= CallNextHookEx(hhk, nCode, wParam, lParam);
77 end;
78
79 function WindowProc(hWnd: HWND; uiMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
80 var
81 ARect: TRect;
82 AData: TPreviewData;
83 AHandle: THandle absolute AData;
84 begin
85 if (uiMsg = WM_SETFOCUS) then
86 begin
87 AHandle:= GetWindowLongPtr(hWnd, GWLP_USERDATA);
88 if (AHandle <> 0) then AData.Handler.SetFocus();
89 end
90 else if (uiMsg = WM_SIZE) then
91 begin
92 AHandle:= GetWindowLongPtr(hWnd, GWLP_USERDATA);
93 if (AHandle <> 0) then
94 begin
95 ARect.Top:= 0;
96 ARect.Left:= 0;
97 ARect.Width:= LOWORD(lParam);
98 ARect.Height:= HIWORD(lParam);
99 AData.Handler.SetRect(@ARect);
100 end;
101 end;
102 Result := DefWindowProc(hWnd, uiMsg, wParam, lParam);
103 end;
104
105 function ListLoadW(ParentWin: HWND; FileToLoad: PWideChar; ShowFlags: Integer): HWND; stdcall;
106 var
107 ARect: TRect;
108 AData: TPreviewData;
109 WindowClassW: TWndClassW;
110 AHandler: IPreviewHandler;
111 AHandle: THandle absolute AData;
112 begin
113 AHandler:= GetPreviewHandler(FileToLoad);
114 if (AHandler = nil) then Exit(wlxInvalidHandle);
115
116 ZeroMemory(@WindowClassW, SizeOf(WndClassW));
117 with WindowClassW do
118 begin
119 Style := CS_DBLCLKS;
120 lpfnWndProc := @WindowProc;
121 cbWndExtra := SizeOf(Pointer);
122 hCursor := LoadCursor(0, IDC_ARROW);
123 lpszClassName := PREVIEW_HANDLER;
124 hInstance := GetModuleHandleW(nil);
125 end;
126 Windows.RegisterClassW(WindowClassW);
127
128 if not GetClientRect(ParentWin, ARect) then
129 ARect:= TRect.Create(0, 0, 640, 480);
130
131 Result:= CreateWindowW(PREVIEW_HANDLER, 'PreviewHandler', WS_CHILD or WS_VISIBLE, ARect.Left, ARect.Top,
132 ARect.Right, ARect.Bottom, ParentWin, 0, WindowClassW.hInstance, nil);
133
134 if (Result <> wlxInvalidHandle) then
135 begin
136 AData:= TPreviewData.Create;
137 AData.Handler:= AHandler;
138 SetPropW(ParentWin, ProcessIdWide, GetProcessID);
139 SetWindowLongPtr(Result, GWLP_USERDATA, AHandle);
140 AHandler.SetWindow(Result, ARect);
141 AData.Handler.DoPreview();
142 Inc(Count);
143 if (Count = 1) then begin
144 hhk:= SetWindowsHookExW(WH_KEYBOARD_LL, @LowLevelKeyboardProc, WindowClassW.hInstance, 0);
145 end;
146 end;
147 end;
148
149 procedure ListCloseWindow(ListWin: HWND); stdcall;
150 var
151 AData: TPreviewData;
152 AHandle: THandle absolute AData;
153 begin
154 AHandle:= GetWindowLongPtr(ListWin, GWLP_USERDATA);
155 DestroyWindow(ListWin);
156 if Assigned(AData) then
157 begin
158 AData.Handler.Unload();
159 AData.Free;
160 Dec(Count);
161 if Count = 0 then begin
162 UnhookWindowsHookEx(hhk);
163 end;
164 end;
165 end;
166
167 procedure ListGetDetectString(DetectString: PAnsiChar; MaxLen: Integer); stdcall;
168 begin
169 StrLCopy(DetectString, '(EXT="HTM")|(EXT="HTML")|(EXT="MHT")|(EXT="MHTML")', MaxLen);
170 end;
171
172 procedure ListSetDefaultParams(dps: PListDefaultParamStruct); stdcall;
173 begin
174 Dll_Process_Detach_Hook:= @DLL_Entry_Hook;
175 ProcessIdAtom := GlobalAddAtomW(PREVIEW_HANDLER);
176 end;
177
178 exports
179 ListLoadW,
180 ListCloseWindow,
181 ListGetDetectString,
182 ListSetDefaultParams;
183
184 end.
185