-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinutils.c
357 lines (299 loc) · 10.2 KB
/
winutils.c
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
/*
* PROJECT: ReactOS Clipboard Viewer
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* PURPOSE: Display helper functions.
* COPYRIGHT: Copyright 2015-2018 Ricardo Hanke
* Copyright 2015-2018 Hermes Belusca-Maito
*/
#include "precomp.h"
/*
void ShowLastWin32Error(HWND hwndParent)
{
DWORD dwError;
LPWSTR lpMsgBuf = NULL;
dwError = GetLastError();
if (dwError == ERROR_SUCCESS)
return;
if (!FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
LANG_USER_DEFAULT,
(LPWSTR)&lpMsgBuf,
0, NULL))
{
return;
}
MessageBoxW(hwndParent, lpMsgBuf, NULL, MB_OK | MB_ICONERROR);
LocalFree(lpMsgBuf);
}
*/
void BringWindowToFront(HWND hWnd)
{
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
//SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
}
else
{
//SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
}
}
int MessageBoxRes(HWND hWnd, HINSTANCE hInstance, UINT uText, UINT uCaption, UINT uType)
{
char szMessage[MAX_STRING_LEN];
char szResource[MAX_STRING_LEN];
LoadString(hInstance, uText, szMessage, sizeof(szMessage));
LoadString(hInstance, uCaption, szResource, sizeof(szResource));
return MessageBox(hWnd, szMessage, szResource, uType);
}
void DrawTextFromResource(HINSTANCE hInstance, UINT uID, HDC hDC, LPRECT lpRect, UINT uFormat)
{
LPSTR lpBuffer;
int nCount;
nCount = LoadString(hInstance, uID, (LPSTR)&lpBuffer, 0);
if (nCount)
DrawText(hDC, lpBuffer, nCount, lpRect, uFormat);
}
void DrawTextFromClipboard(UINT uFormat, PAINTSTRUCT ps, SCROLLSTATE state)
{
POINT ptOrg;
HGLOBAL hGlobal;
LPSTR lpText, ptr;
size_t lineSize;
int FirstLine, LastLine;
hGlobal = GetClipboardData(uFormat);
if (!hGlobal)
return;
lpText = GlobalLock(hGlobal);
if (!lpText)
return;
/* Find the first and last line indices to display (Note that CurrentX/Y are in pixels!) */
FirstLine = max(0, (state.CurrentY + ps.rcPaint.top) / Globals.CharHeight);
// LastLine = min(LINES - 1, (state.CurrentY + ps.rcPaint.bottom) / Globals.CharHeight);
// NOTE: Can be less or greater than the actual number of lines in the text.
LastLine = (state.CurrentY + ps.rcPaint.bottom) / Globals.CharHeight;
/* Find the first text line to display */
while (FirstLine > 0)
{
/*if (uFormat == CF_UNICODETEXT)
{
if (*(LPCWSTR)lpText == UNICODE_NULL)
break;
GetLineExtent(lpText, (LPCWSTR*)&ptr);
}
else
{*/
if (*(LPCSTR)lpText == (char)0)
break;
GetLineExtent(lpText, (LPCSTR*)&ptr);
//}
--FirstLine;
--LastLine;
lpText = ptr;
}
ptOrg.x = ps.rcPaint.left;
ptOrg.y = /* FirstLine */ max(0, (state.CurrentY + ps.rcPaint.top) / Globals.CharHeight)
* Globals.CharHeight - state.CurrentY;
/* Display each line from the current one up to the last one */
++LastLine;
while (LastLine >= 0)
{
#if 0
if (uFormat == CF_UNICODETEXT)
{
if (*(LPCWSTR)lpText == UNICODE_NULL)
break;
lineSize = GetLineExtentW(lpText, (LPCWSTR*)&ptr);
TabbedTextOutW(ps.hdc, /*ptOrg.x*/0 - state.CurrentX, ptOrg.y,
lpText, lineSize, 0, NULL,
/*ptOrg.x*/0 - state.CurrentX);
}
else
{
#endif
if (*(LPCSTR)lpText == (char)0)
break;
lineSize = GetLineExtent(lpText, (LPCSTR FAR *)&ptr);
TabbedTextOut(ps.hdc, /*ptOrg.x*/0 - state.CurrentX, ptOrg.y,
lpText, lineSize, 0, NULL,
/*ptOrg.x*/0 - state.CurrentX);
//}
--LastLine;
ptOrg.y += Globals.CharHeight;
lpText = ptr;
}
GlobalUnlock(hGlobal);
}
void BitBltFromClipboard(PAINTSTRUCT ps, SCROLLSTATE state, DWORD dwRop)
{
HDC hdcMem;
HBITMAP hBitmap;
BITMAP bmp;
LONG bmWidth, bmHeight;
hdcMem = CreateCompatibleDC(ps.hdc);
if (!hdcMem)
return;
hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
GetObject(hBitmap, sizeof(bmp), &bmp);
SelectObject(hdcMem, hBitmap);
bmWidth = min(ps.rcPaint.right - ps.rcPaint.left, bmp.bmWidth - ps.rcPaint.left - state.CurrentX);
bmHeight = min(ps.rcPaint.bottom - ps.rcPaint.top , bmp.bmHeight - ps.rcPaint.top - state.CurrentY);
BitBlt(ps.hdc,
ps.rcPaint.left,
ps.rcPaint.top,
bmWidth,
bmHeight,
hdcMem,
ps.rcPaint.left + state.CurrentX,
ps.rcPaint.top + state.CurrentY,
dwRop);
DeleteDC(hdcMem);
}
void SetDIBitsToDeviceFromClipboard(UINT uFormat, PAINTSTRUCT ps, SCROLLSTATE state, UINT fuColorUse)
{
HGLOBAL hGlobal;
LPBITMAPINFOHEADER lpInfoHeader;
LPBYTE lpBits;
LONG bmWidth, bmHeight;
DWORD dwPalSize = 0;
hGlobal = GetClipboardData(uFormat);
if (!hGlobal)
return;
lpInfoHeader = (LPBITMAPINFOHEADER)GlobalLock(hGlobal);
if (!lpInfoHeader)
return;
if (lpInfoHeader->biSize == sizeof(BITMAPCOREHEADER))
{
LPBITMAPCOREHEADER lpCoreHeader = (LPBITMAPCOREHEADER)lpInfoHeader;
dwPalSize = 0;
if (lpCoreHeader->bcBitCount <= 8)
{
dwPalSize = (1 << lpCoreHeader->bcBitCount);
if (fuColorUse == DIB_RGB_COLORS)
dwPalSize *= sizeof(RGBTRIPLE);
else
dwPalSize *= sizeof(WORD);
}
bmWidth = lpCoreHeader->bcWidth;
bmHeight = lpCoreHeader->bcHeight;
}
else if ((lpInfoHeader->biSize == sizeof(BITMAPINFOHEADER)) /*||
(lpInfoHeader->biSize == sizeof(BITMAPV4HEADER)) ||
(lpInfoHeader->biSize == sizeof(BITMAPV5HEADER))*/)
{
dwPalSize = lpInfoHeader->biClrUsed;
if ((dwPalSize == 0) && (lpInfoHeader->biBitCount <= 8))
dwPalSize = (1 << lpInfoHeader->biBitCount);
if (fuColorUse == DIB_RGB_COLORS)
dwPalSize *= sizeof(RGBQUAD);
else
dwPalSize *= sizeof(WORD);
//if (/*(lpInfoHeader->biSize == sizeof(BITMAPINFOHEADER)) &&*/
// (lpInfoHeader->biCompression == BI_BITFIELDS))
//{
//dwPalSize += 3 * sizeof(DWORD);
//}
/*
* This is a (disabled) hack for Windows, when uFormat == CF_DIB
* it needs yet another extra 3 DWORDs, in addition to the
* ones already taken into account in via the compression.
* This problem doesn't happen when uFormat == CF_DIBV5
* (in that case, when compression is taken into account,
* everything is nice).
*
* NOTE 1: This fix is only for us, because when one pastes DIBs
* directly in apps, the bitmap offset problem is still present.
*
* NOTE 2: The problem can be seen with Windows' clipbrd.exe if
* one copies a CF_DIB image in the clipboard. By default Windows'
* clipbrd.exe works with CF_DIBV5 and CF_BITMAP, so the problem
* is unseen, and the clipboard doesn't have to convert to CF_DIB.
*
* FIXME: investigate!!
* ANSWER: this is a Windows bug; part of the answer is there:
* https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/ac7ab3b5-8609-4478-b86a-976dab44c271/bug-clipboard-format-conversions-cfdib-cfdibv5-cfdib
* May be related:
* https://blog.talosintelligence.com/2015/10/dangerous-clipboard.html
*/
#if 0
if ((lpInfoHeader->biSize == sizeof(BITMAPINFOHEADER)) &&
(lpInfoHeader->biCompression == BI_BITFIELDS))
{
dwPalSize += 3 * sizeof(DWORD);
}
#endif
bmWidth = lpInfoHeader->biWidth;
/* NOTE: biHeight < 0 for bottom-up DIBs, or > 0 for top-down DIBs */
bmHeight = lpInfoHeader->biHeight;
}
else
{
/* Invalid format */
GlobalUnlock(hGlobal);
return;
}
lpBits = (LPBYTE)lpInfoHeader + lpInfoHeader->biSize + dwPalSize;
/*
* The seventh parameter (YSrc) of SetDIBitsToDevice always designates
* the Y-coordinate of the "lower-left corner" of the image, be the DIB
* in bottom-up or top-down mode.
*/
SetDIBitsToDevice(ps.hdc,
-state.CurrentX, // ps.rcPaint.left,
-state.CurrentY, // ps.rcPaint.top,
bmWidth,
bmHeight,
0, // ps.rcPaint.left + state.CurrentX,
0, // -(ps.rcPaint.top + state.CurrentY),
0, // uStartScan,
bmHeight,
lpBits,
(LPBITMAPINFO)lpInfoHeader,
fuColorUse);
GlobalUnlock(hGlobal);
}
void PlayMetaFileFromClipboard(HDC hdc, const RECT *lpRect)
{
LPMETAFILEPICT mp;
HGLOBAL hGlobal;
hGlobal = GetClipboardData(CF_METAFILEPICT);
if (!hGlobal)
return;
mp = (LPMETAFILEPICT)GlobalLock(hGlobal);
if (!mp)
return;
SetMapMode(hdc, mp->mm);
SetViewportExtEx(hdc, lpRect->right, lpRect->bottom, NULL);
SetViewportOrgEx(hdc, lpRect->left, lpRect->top, NULL);
PlayMetaFile(hdc, mp->hMF);
GlobalUnlock(hGlobal);
}
#if 0
void PlayEnhMetaFileFromClipboard(HDC hdc, const RECT *lpRect)
{
HENHMETAFILE hEmf;
hEmf = GetClipboardData(CF_ENHMETAFILE);
PlayEnhMetaFile(hdc, hEmf, lpRect);
}
#endif
BOOL RealizeClipboardPalette(HDC hdc)
{
BOOL Success;
HPALETTE hPalette, hOldPalette;
if (!IsClipboardFormatAvailable(CF_PALETTE))
return FALSE;
hPalette = GetClipboardData(CF_PALETTE);
if (!hPalette)
return FALSE;
hOldPalette = SelectPalette(hdc, hPalette, FALSE);
if (!hOldPalette)
return FALSE;
Success = (RealizePalette(hdc) != -1/*GDI_ERROR*/);
SelectPalette(hdc, hOldPalette, FALSE);
return Success;
}