forked from microsoft/terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.cpp
More file actions
450 lines (379 loc) · 12.9 KB
/
Copy pathpreview.cpp
File metadata and controls
450 lines (379 loc) · 12.9 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
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
Module Name:
preview.c
Abstract:
This module contains the code for console preview window
Author:
Therese Stowell (thereses) Feb-3-1992 (swiped from Win3.1)
Revision History:
--*/
#include "precomp.h"
#pragma hdrstop
/* ----- Equates ----- */
#define PREVIEW_HSCROLL 0x01
#define PREVIEW_VSCROLL 0x02
/* ----- Prototypes ----- */
void AspectPoint(
RECT* rectPreview,
POINT* pt);
LONG AspectScale(
LONG n1,
LONG n2,
LONG m);
/* ----- Globals ----- */
POINT NonClientSize;
RECT WindowRect;
DWORD PreviewFlags;
VOID
UpdatePreviewRect(VOID)
/*++
Update the global window size and dimensions
--*/
{
POINT MinSize;
POINT MaxSize;
POINT WindowSize;
PFONT_INFO lpFont;
HMONITOR hMonitor;
MONITORINFO mi = { 0 };
/*
* Get the font pointer
*/
lpFont = &FontInfo[g_currentFontIndex];
/*
* Get the window size
*/
MinSize.x = (GetSystemMetrics(SM_CXMIN) - NonClientSize.x) / lpFont->Size.X;
MinSize.y = (GetSystemMetrics(SM_CYMIN) - NonClientSize.y) / lpFont->Size.Y;
MaxSize.x = GetSystemMetrics(SM_CXFULLSCREEN) / lpFont->Size.X;
MaxSize.y = GetSystemMetrics(SM_CYFULLSCREEN) / lpFont->Size.Y;
WindowSize.x = max(MinSize.x, min(MaxSize.x, gpStateInfo->WindowSize.X));
WindowSize.y = max(MinSize.y, min(MaxSize.y, gpStateInfo->WindowSize.Y));
/*
* Get the window rectangle, making sure it's at least twice the
* size of the non-client area.
*/
WindowRect.left = gpStateInfo->WindowPosX;
WindowRect.top = gpStateInfo->WindowPosY;
WindowRect.right = WindowSize.x * lpFont->Size.X + NonClientSize.x;
if (WindowRect.right < NonClientSize.x * 2)
{
WindowRect.right = NonClientSize.x * 2;
}
WindowRect.right += WindowRect.left;
WindowRect.bottom = WindowSize.y * lpFont->Size.Y + NonClientSize.y;
if (WindowRect.bottom < NonClientSize.y * 2)
{
WindowRect.bottom = NonClientSize.y * 2;
}
WindowRect.bottom += WindowRect.top;
/*
* Get information about the monitor we're on
*/
hMonitor = MonitorFromRect(&WindowRect, MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(mi);
GetMonitorInfo(hMonitor, &mi);
gcxScreen = mi.rcWork.right - mi.rcWork.left;
gcyScreen = mi.rcWork.bottom - mi.rcWork.top;
/*
* Convert window rectangle to monitor relative coordinates
*/
WindowRect.right -= WindowRect.left;
WindowRect.left -= mi.rcWork.left;
WindowRect.bottom -= WindowRect.top;
WindowRect.top -= mi.rcWork.top;
/*
* Update the display flags
*/
if (WindowSize.x < gpStateInfo->ScreenBufferSize.X)
{
PreviewFlags |= PREVIEW_HSCROLL;
}
else
{
PreviewFlags &= ~PREVIEW_HSCROLL;
}
if (WindowSize.y < gpStateInfo->ScreenBufferSize.Y)
{
PreviewFlags |= PREVIEW_VSCROLL;
}
else
{
PreviewFlags &= ~PREVIEW_VSCROLL;
}
}
VOID InvalidatePreviewRect(HWND hWnd)
/*++
Invalidate the area covered by the preview "window"
--*/
{
RECT rectWin;
RECT rectPreview;
/*
* Get the size of the preview "screen"
*/
GetClientRect(hWnd, &rectPreview);
/*
* Get the dimensions of the preview "window" and scale it to the
* preview "screen"
*/
rectWin.left = WindowRect.left;
rectWin.top = WindowRect.top;
rectWin.right = WindowRect.left + WindowRect.right;
rectWin.bottom = WindowRect.top + WindowRect.bottom;
AspectPoint(&rectPreview, (POINT*)&rectWin.left);
AspectPoint(&rectPreview, (POINT*)&rectWin.right);
/*
* Invalidate the area covered by the preview "window"
*/
InvalidateRect(hWnd, &rectWin, FALSE);
}
VOID PreviewPaint(
PAINTSTRUCT* pPS,
HWND hWnd)
/*++
Paints the font preview. This is called inside the paint message
handler for the preview window
--*/
{
RECT rectWin;
RECT rectPreview;
HBRUSH hbrFrame;
HBRUSH hbrTitle;
HBRUSH hbrOld;
HBRUSH hbrClient;
HBRUSH hbrBorder;
HBRUSH hbrButton;
HBRUSH hbrScroll;
HBRUSH hbrDesktop;
POINT ptButton;
POINT ptScroll;
HDC hDC;
HBITMAP hBitmap;
HBITMAP hBitmapOld;
COLORREF rgbClient;
/*
* Get the size of the preview "screen"
*/
GetClientRect(hWnd, &rectPreview);
/*
* Get the dimensions of the preview "window" and scale it to the
* preview "screen"
*/
rectWin = WindowRect;
AspectPoint(&rectPreview, (POINT*)&rectWin.left);
AspectPoint(&rectPreview, (POINT*)&rectWin.right);
/*
* Compute the dimensions of some other window components
*/
ptButton.x = GetSystemMetrics(SM_CXSIZE);
ptButton.y = GetSystemMetrics(SM_CYSIZE);
AspectPoint(&rectPreview, &ptButton);
ptButton.y *= 2; /* Double the computed size for "looks" */
ptScroll.x = GetSystemMetrics(SM_CXVSCROLL);
ptScroll.y = GetSystemMetrics(SM_CYHSCROLL);
AspectPoint(&rectPreview, &ptScroll);
/*
* Create the memory device context
*/
hDC = CreateCompatibleDC(pPS->hdc);
hBitmap = CreateCompatibleBitmap(pPS->hdc,
rectPreview.right,
rectPreview.bottom);
hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap);
/*
* Create the brushes
*/
hbrBorder = CreateSolidBrush(GetSysColor(COLOR_ACTIVEBORDER));
hbrTitle = CreateSolidBrush(GetSysColor(COLOR_ACTIVECAPTION));
hbrFrame = CreateSolidBrush(GetSysColor(COLOR_WINDOWFRAME));
hbrButton = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
hbrScroll = CreateSolidBrush(GetSysColor(COLOR_SCROLLBAR));
hbrDesktop = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
rgbClient = GetNearestColor(hDC, ScreenBkColor(gpStateInfo));
hbrClient = CreateSolidBrush(rgbClient);
/*
* Erase the clipping area
*/
FillRect(hDC, &(pPS->rcPaint), hbrDesktop);
/*
* Fill in the whole window with the client brush
*/
hbrOld = (HBRUSH)SelectObject(hDC, hbrClient);
PatBlt(hDC, rectWin.left, rectWin.top, rectWin.right - 1, rectWin.bottom - 1, PATCOPY);
/*
* Fill in the caption bar
*/
SelectObject(hDC, hbrTitle);
PatBlt(hDC, rectWin.left + 3, rectWin.top + 3, rectWin.right - 7, ptButton.y - 2, PATCOPY);
/*
* Draw the "buttons"
*/
SelectObject(hDC, hbrButton);
PatBlt(hDC, rectWin.left + 3, rectWin.top + 3, ptButton.x, ptButton.y - 2, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4 - ptButton.x, rectWin.top + 3, ptButton.x, ptButton.y - 2, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4 - 2 * ptButton.x - 1, rectWin.top + 3, ptButton.x, ptButton.y - 2, PATCOPY);
SelectObject(hDC, hbrFrame);
PatBlt(hDC, rectWin.left + 3 + ptButton.x, rectWin.top + 3, 1, ptButton.y - 2, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4 - ptButton.x - 1, rectWin.top + 3, 1, ptButton.y - 2, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4 - 2 * ptButton.x - 2, rectWin.top + 3, 1, ptButton.y - 2, PATCOPY);
/*
* Draw the scrollbars
*/
SelectObject(hDC, hbrScroll);
if (PreviewFlags & PREVIEW_HSCROLL)
{
PatBlt(hDC, rectWin.left + 3, rectWin.top + rectWin.bottom - 4 - ptScroll.y, rectWin.right - 7, ptScroll.y, PATCOPY);
}
if (PreviewFlags & PREVIEW_VSCROLL)
{
PatBlt(hDC, rectWin.left + rectWin.right - 4 - ptScroll.x, rectWin.top + 1 + ptButton.y + 1, ptScroll.x, rectWin.bottom - 6 - ptButton.y, PATCOPY);
if (PreviewFlags & PREVIEW_HSCROLL)
{
SelectObject(hDC, hbrFrame);
PatBlt(hDC, rectWin.left + rectWin.right - 5 - ptScroll.x, rectWin.top + rectWin.bottom - 4 - ptScroll.y, 1, ptScroll.y, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4 - ptScroll.x, rectWin.top + rectWin.bottom - 5 - ptScroll.y, ptScroll.x, 1, PATCOPY);
}
}
/*
* Draw the interior window frame and caption frame
*/
SelectObject(hDC, hbrFrame);
PatBlt(hDC, rectWin.left + 2, rectWin.top + 2, 1, rectWin.bottom - 5, PATCOPY);
PatBlt(hDC, rectWin.left + 2, rectWin.top + 2, rectWin.right - 5, 1, PATCOPY);
PatBlt(hDC, rectWin.left + 2, rectWin.top + rectWin.bottom - 4, rectWin.right - 5, 1, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 4, rectWin.top + 2, 1, rectWin.bottom - 5, PATCOPY);
PatBlt(hDC, rectWin.left + 2, rectWin.top + 1 + ptButton.y, rectWin.right - 5, 1, PATCOPY);
/*
* Draw the border
*/
SelectObject(hDC, hbrBorder);
PatBlt(hDC, rectWin.left + 1, rectWin.top + 1, 1, rectWin.bottom - 3, PATCOPY);
PatBlt(hDC, rectWin.left + 1, rectWin.top + 1, rectWin.right - 3, 1, PATCOPY);
PatBlt(hDC, rectWin.left + 1, rectWin.top + rectWin.bottom - 3, rectWin.right - 3, 1, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 3, rectWin.top + 1, 1, rectWin.bottom - 3, PATCOPY);
/*
* Draw the exterior window frame
*/
SelectObject(hDC, hbrFrame);
PatBlt(hDC, rectWin.left, rectWin.top, 1, rectWin.bottom - 1, PATCOPY);
PatBlt(hDC, rectWin.left, rectWin.top, rectWin.right - 1, 1, PATCOPY);
PatBlt(hDC, rectWin.left, rectWin.top + rectWin.bottom - 2, rectWin.right - 1, 1, PATCOPY);
PatBlt(hDC, rectWin.left + rectWin.right - 2, rectWin.top, 1, rectWin.bottom - 1, PATCOPY);
/*
* Copy the memory device context to the screen device context
*/
BitBlt(pPS->hdc, 0, 0, rectPreview.right, rectPreview.bottom, hDC, 0, 0, SRCCOPY);
/*
* Clean up everything
*/
SelectObject(hDC, hbrOld);
SelectObject(hDC, hBitmapOld);
DeleteObject(hbrBorder);
DeleteObject(hbrFrame);
DeleteObject(hbrTitle);
DeleteObject(hbrClient);
DeleteObject(hbrButton);
DeleteObject(hbrScroll);
DeleteObject(hbrDesktop);
DeleteObject(hBitmap);
DeleteDC(hDC);
}
[[nodiscard]] LRESULT
CALLBACK
PreviewWndProc(
HWND hWnd,
UINT wMessage,
WPARAM wParam,
LPARAM lParam)
/*
* PreviewWndProc
* Handles the preview window
*/
{
PAINTSTRUCT ps;
LPCREATESTRUCT lpcs;
RECT rcWindow;
int cx;
int cy;
switch (wMessage)
{
case WM_CREATE:
/*
* Figure out space used by non-client area
*/
SetRect(&rcWindow, 0, 0, 50, 50);
AdjustWindowRect(&rcWindow, WS_OVERLAPPEDWINDOW, FALSE);
NonClientSize.x = rcWindow.right - rcWindow.left - 50;
NonClientSize.y = rcWindow.bottom - rcWindow.top - 50;
/*
* Compute the size of the preview "window"
*/
UpdatePreviewRect();
/*
* Scale the window so it has the same aspect ratio as the screen
*/
lpcs = (LPCREATESTRUCT)lParam;
cx = lpcs->cx;
cy = AspectScale(gcyScreen, gcxScreen, cx);
if (cy > lpcs->cy)
{
cy = lpcs->cy;
cx = AspectScale(gcxScreen, gcyScreen, cy);
}
MoveWindow(hWnd, lpcs->x, lpcs->y, cx, cy, TRUE);
break;
case WM_PAINT:
BeginPaint(hWnd, &ps);
PreviewPaint(&ps, hWnd);
EndPaint(hWnd, &ps);
break;
case CM_PREVIEW_UPDATE:
InvalidatePreviewRect(hWnd);
UpdatePreviewRect();
/*
* Make sure the preview "screen" has the correct aspect ratio
*/
GetWindowRect(hWnd, &rcWindow);
cx = rcWindow.right - rcWindow.left;
cy = AspectScale(gcyScreen, gcxScreen, cx);
if (cy != rcWindow.bottom - rcWindow.top)
{
SetWindowPos(hWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
}
InvalidatePreviewRect(hWnd);
break;
default:
return DefWindowProc(hWnd, wMessage, wParam, lParam);
}
return 0L;
}
/* AspectScale
* Performs the following calculation in LONG arithmetic to avoid
* overflow:
* return = n1 * m / n2
* This can be used to make an aspect ration calculation where n1/n2
* is the aspect ratio and m is a known value. The return value will
* be the value that corresponds to m with the correct apsect ratio.
*/
LONG AspectScale(
LONG n1,
LONG n2,
LONG m)
{
LONG Temp;
Temp = n1 * m + (n2 >> 1);
return Temp / n2;
}
/* AspectPoint
* Scales a point to be preview-sized instead of screen-sized.
*/
void AspectPoint(
RECT* rectPreview,
POINT* pt)
{
pt->x = AspectScale(rectPreview->right, gcxScreen, pt->x);
pt->y = AspectScale(rectPreview->bottom, gcyScreen, pt->y);
}