| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Rich Text Format plugin |
| 5 |
|
| 6 |
Copyright (C) 2022 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 richview; |
| 23 |
|
| 24 |
{$mode objfpc}{$H+} |
| 25 |
|
| 26 |
uses |
| 27 |
RichEdit, Windows, Messages, WlxPlugin; |
| 28 |
|
| 29 |
type |
| 30 |
{$push}{$packrecords 4} |
| 31 |
TEditStream = record |
| 32 |
dwCookie : DWORD_PTR; |
| 33 |
dwError : DWORD; |
| 34 |
pfnCallback : EDITSTREAMCALLBACK; |
| 35 |
end; |
| 36 |
{$pop} |
| 37 |
|
| 38 |
var |
| 39 |
RichEditClass: PWideChar; |
| 40 |
|
| 41 |
function LoadLibraryX(const AName: PWideChar): HMODULE; |
| 42 |
begin |
| 43 |
Result:= LoadLibraryExW(AName, 0, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 44 |
end; |
| 45 |
|
| 46 |
function RichEditWndProc(hWnd: HWND; uiMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; |
| 47 |
var |
| 48 |
AWndProc: Windows.WNDPROC; |
| 49 |
AHandle: LONG_PTR absolute AWndProc; |
| 50 |
begin |
| 51 |
if (uiMsg = WM_KEYDOWN) then |
| 52 |
begin |
| 53 |
if (wParam = VK_ESCAPE) then |
| 54 |
begin |
| 55 |
PostMessage(GetParent(hWnd), WM_SYSCOMMAND, SC_CLOSE, 0); |
| 56 |
end; |
| 57 |
end; |
| 58 |
AHandle := GetWindowLongPtr(hWnd, GWLP_USERDATA); |
| 59 |
if Assigned(AWndProc) then |
| 60 |
Result := CallWindowProc(AWndProc, hWnd, uiMsg, wParam, lParam) |
| 61 |
else begin |
| 62 |
Result := DefWindowProc(hWnd, uiMsg, wParam, lParam); |
| 63 |
end; |
| 64 |
end; |
| 65 |
|
| 66 |
function EditStreamCallback(dwCookie: DWORD_PTR; lpBuff: LPBYTE; cb: LONG; pcb: PLONG): DWORD; stdcall; |
| 67 |
var |
| 68 |
AFile: THandle absolute dwCookie; |
| 69 |
begin |
| 70 |
if (ReadFile(AFile, lpBuff^, cb, PDWORD(pcb)^, nil)) then |
| 71 |
Exit(0); |
| 72 |
|
| 73 |
Result:= DWORD(-1); |
| 74 |
end; |
| 75 |
|
| 76 |
function ListLoadW(ParentWin: HWND; FileToLoad: PWideChar; ShowFlags: Integer): HWND; stdcall; |
| 77 |
var |
| 78 |
ARect: TRect; |
| 79 |
AFile: THandle; |
| 80 |
AResult: Boolean; |
| 81 |
hInstance: HMODULE; |
| 82 |
AWndProc: LONG_PTR; |
| 83 |
AStream: TEditStream; |
| 84 |
begin |
| 85 |
if (RichEditClass = nil) then Exit(wlxInvalidHandle); |
| 86 |
|
| 87 |
AFile:= CreateFileW(FileToLoad, GENERIC_READ, FILE_SHARE_READ, nil, |
| 88 |
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0); |
| 89 |
|
| 90 |
if (AFile = INVALID_HANDLE_VALUE) then Exit(wlxInvalidHandle); |
| 91 |
|
| 92 |
hInstance := GetModuleHandleW(nil); |
| 93 |
|
| 94 |
if not GetClientRect(ParentWin, @ARect) then ARect:= TRect.Create(0, 0, 640, 480); |
| 95 |
|
| 96 |
Result:= CreateWindowW(RichEditClass, nil, WS_CHILD or WS_HSCROLL or WS_VSCROLL or ES_READONLY or |
| 97 |
ES_MULTILINE or ES_AUTOHSCROLL or ES_AUTOVSCROLL or WS_TABSTOP, |
| 98 |
ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, ParentWin, hInstance, 0, nil); |
| 99 |
|
| 100 |
if (Result <> wlxInvalidHandle) then |
| 101 |
begin |
| 102 |
AStream.dwError := 0; |
| 103 |
AStream.dwCookie := DWORD_PTR(AFile); |
| 104 |
AStream.pfnCallback := @EditStreamCallback; |
| 105 |
|
| 106 |
AResult := (SendMessage(Result, EM_STREAMIN, SF_RTF, LPARAM(@AStream)) <> 0) and (AStream.dwError = 0); |
| 107 |
|
| 108 |
if AResult then |
| 109 |
begin |
| 110 |
AWndProc:= SetWindowLongPtr(Result, GWLP_WNDPROC, LONG_PTR(@RichEditWndProc)); |
| 111 |
SetWindowLongPtr(Result, GWLP_USERDATA, AWndProc); |
| 112 |
ShowWindow(Result, SW_SHOW); |
| 113 |
end |
| 114 |
else begin |
| 115 |
DestroyWindow(Result); |
| 116 |
Result:= wlxInvalidHandle; |
| 117 |
end; |
| 118 |
end; |
| 119 |
CloseHandle(AFile); |
| 120 |
end; |
| 121 |
|
| 122 |
function ListSearchTextW(ListWin: HWND; SearchString: PWideChar; SearchParameter: Integer): Integer; stdcall; |
| 123 |
var |
| 124 |
AMode: TFindTextExW; |
| 125 |
wParam: Windows.WPARAM; |
| 126 |
ARange: RichEdit.TCharRange; |
| 127 |
begin |
| 128 |
AMode:= Default(TFindTextExW); |
| 129 |
AMode.lpstrText:= SearchString; |
| 130 |
SendMessageW(ListWin, EM_EXGETSEL, 0, LPARAM(@ARange)); |
| 131 |
if (SearchParameter and lcs_findfirst <> 0) then |
| 132 |
begin |
| 133 |
if SearchParameter and lcs_backwards <> 0 then |
| 134 |
ARange.cpMin:= High(LONG) |
| 135 |
else |
| 136 |
ARange.cpMax:= 0; |
| 137 |
end; |
| 138 |
if SearchParameter and lcs_backwards <> 0 then |
| 139 |
begin |
| 140 |
wParam:= 0; |
| 141 |
AMode.chrg.cpMax:= 0; |
| 142 |
AMode.chrg.cpMin:= ARange.cpMin; |
| 143 |
end |
| 144 |
else begin |
| 145 |
wParam:= FR_DOWN; |
| 146 |
AMode.chrg.cpMax:= -1; |
| 147 |
AMode.chrg.cpMin:= ARange.cpMax; |
| 148 |
end; |
| 149 |
if (SearchParameter and lcs_matchcase <> 0) then wParam:= wParam or FR_MATCHCASE; |
| 150 |
if (SearchParameter and lcs_wholewords <> 0) then wParam:= wParam or FR_WHOLEWORD; |
| 151 |
if SendMessageW(ListWin, EM_FINDTEXTEXW, wParam, LPARAM(@AMode)) >= 0 then |
| 152 |
begin |
| 153 |
Result:= LISTPLUGIN_OK; |
| 154 |
SendMessageW(ListWin, EM_EXSETSEL, 0, LPARAM(@AMode.chrgText)); |
| 155 |
end |
| 156 |
else begin |
| 157 |
Result:= LISTPLUGIN_ERROR; |
| 158 |
end; |
| 159 |
end; |
| 160 |
|
| 161 |
function ListSendCommand(ListWin: HWND; Command, Parameter: Integer): Integer; stdcall; |
| 162 |
var |
| 163 |
ARange: TCharRange; |
| 164 |
begin |
| 165 |
case Command of |
| 166 |
lc_selectall: |
| 167 |
begin |
| 168 |
ARange.cpMin:= 0; |
| 169 |
ARange.cpMax:= -1; |
| 170 |
if SendMessageW(ListWin, EM_EXSETSEL, 0, LPARAM(@ARange)) >= 0 then |
| 171 |
Result:= LISTPLUGIN_OK |
| 172 |
else begin |
| 173 |
Result:= LISTPLUGIN_ERROR; |
| 174 |
end; |
| 175 |
end; |
| 176 |
lc_copy: |
| 177 |
begin |
| 178 |
if SendMessageW(ListWin, WM_COPY, 0, 0) <> 0 then |
| 179 |
Result:= LISTPLUGIN_OK |
| 180 |
else begin |
| 181 |
Result:= LISTPLUGIN_ERROR; |
| 182 |
end; |
| 183 |
end; |
| 184 |
else begin |
| 185 |
Result:= LISTPLUGIN_ERROR; |
| 186 |
end; |
| 187 |
end; |
| 188 |
end; |
| 189 |
|
| 190 |
procedure ListGetDetectString(DetectString: PAnsiChar; MaxLen: Integer); stdcall; |
| 191 |
begin |
| 192 |
lstrcpynA(DetectString, 'EXT="RTF"', MaxLen); |
| 193 |
end; |
| 194 |
|
| 195 |
procedure ListSetDefaultParams(dps: PListDefaultParamStruct); stdcall; |
| 196 |
begin |
| 197 |
// Rich Edit Version 4.1 |
| 198 |
if (LoadLibraryX('Msftedit.dll') <> NilHandle) then |
| 199 |
RichEditClass := 'RichEdit50W' |
| 200 |
// Rich Edit Version 2.0/3.0 |
| 201 |
else if (LoadLibraryX('riched20.dll') <> NilHandle) then |
| 202 |
RichEditClass := 'RichEdit20W' |
| 203 |
else begin |
| 204 |
RichEditClass := nil; |
| 205 |
end; |
| 206 |
end; |
| 207 |
|
| 208 |
exports |
| 209 |
ListLoadW, |
| 210 |
ListSearchTextW, |
| 211 |
ListSendCommand, |
| 212 |
ListGetDetectString, |
| 213 |
ListSetDefaultParams; |
| 214 |
|
| 215 |
end. |
| 216 |
|