-
Notifications
You must be signed in to change notification settings - Fork 20
/
dxprint.cpp
713 lines (601 loc) · 22 KB
/
dxprint.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//-----------------------------------------------------------------------------
// Name: dxprint.cpp
//
// Desc: DirectX Capabilities Viewer Printing Support
//
// Copyright(c) Microsoft Corporation.
// Licensed under the MIT License.
//
// https://go.microsoft.com/fwlink/?linkid=2136896
//-----------------------------------------------------------------------------
#include "dxview.h"
#include <Windowsx.h>
#include <commdlg.h>
#include <shlobj.h>
BOOL g_PrintToFile = FALSE; // Don't print to printer print to dxview.log
TCHAR g_PrintToFilePath[MAX_PATH]; // "Print" to this file instead of dxview.log
namespace
{
BOOL g_fAbortPrint = FALSE; // Did User Abort Print operation ?!?
HWND g_hAbortPrintDlg = nullptr; // Print Abort Dialog handle
HANDLE g_FileHandle = nullptr; // Handle to log file
DWORD iLastXPos = 0;
//-----------------------------------------------------------------------------
// Local Prototypes
//-----------------------------------------------------------------------------
#define MAX_TITLE 64
#define MAX_MESSAGE 256
VOID DoMessage(DWORD dwTitle, DWORD dwMsg);
BOOL CALLBACK PrintTreeStats(HINSTANCE hInstance, HWND hWnd, HWND hTreeWnd,
HTREEITEM hRoot);
//-----------------------------------------------------------------------------
// Name: PrintDialogProc()
// Desc: Dialog procedure for printing
//-----------------------------------------------------------------------------
BOOL CALLBACK PrintDialogProc(HWND hDlg, UINT msg, WPARAM /*wParam*/, LPARAM /*lParam*/)
{
switch (msg)
{
case WM_INITDIALOG:
// Disable system menu on dialog
EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
return TRUE;
case WM_COMMAND:
// User is aborting print operation
g_fAbortPrint = TRUE;
EnableWindow(GetParent(hDlg), TRUE);
DestroyWindow(hDlg);
g_hAbortPrintDlg = nullptr;
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name: AbortProc()
// Desc: Abort procedure for printing
//-----------------------------------------------------------------------------
BOOL CALLBACK AbortProc(HDC /*hPrinterDC*/, int /*iCode*/)
{
MSG msg;
while (!g_fAbortPrint && PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
if ((!g_hAbortPrintDlg) || !IsDialogMessage(g_hAbortPrintDlg, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return !g_fAbortPrint;
}
//-----------------------------------------------------------------------------
// Name: PrintStartPage
// Desc: Check if we need to start a new page
//-----------------------------------------------------------------------------
HRESULT PrintStartPage(_In_ PRINTCBINFO* pci)
{
if (g_PrintToFile)
return S_OK;
if (!pci)
return E_FAIL;
// Check if we need to start a new page
if (pci->fStartPage)
{
// Check for user abort
if (g_fAbortPrint)
return E_FAIL;
// Start new page
if (StartPage(pci->hdcPrint) < 0)
return E_FAIL;
// Reset line count
pci->fStartPage = FALSE;
pci->dwCurrLine = 0;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: PrintEndPage()
// Desc: Check if we need to start a new page
//-----------------------------------------------------------------------------
HRESULT PrintEndPage(_In_ PRINTCBINFO* pci)
{
if (g_PrintToFile)
return S_OK;
if (!pci)
return E_FAIL;
// Check if we need to end this page
if (!pci->fStartPage)
{
// End page
if (EndPage(pci->hdcPrint) < 0)
return E_FAIL;
pci->fStartPage = TRUE;
// Check for user abort
if (g_fAbortPrint)
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DoMessage()
// Desc: Display warning message to user
//-----------------------------------------------------------------------------
VOID DoMessage(DWORD dwTitle, DWORD dwMsg)
{
TCHAR strTitle[MAX_TITLE];
TCHAR strMsg[MAX_MESSAGE];
LoadString(GetModuleHandle(nullptr), dwTitle, strTitle, MAX_TITLE);
LoadString(GetModuleHandle(nullptr), dwMsg, strMsg, MAX_MESSAGE);
MessageBox(nullptr, strMsg, strTitle, MB_OK);
}
//-----------------------------------------------------------------------------
// Name: PrintStats()
// Desc: Print user defined stuff
//-----------------------------------------------------------------------------
BOOL CALLBACK PrintTreeStats(HINSTANCE hInstance, HWND hWnd, HWND hTreeWnd,
HTREEITEM hRoot)
{
static DOCINFO di;
static PRINTDLG pd = {};
// Check Parameters
if (!hInstance || !hWnd || !hTreeWnd)
return FALSE;
// Get Starting point for tree
HTREEITEM hStartTree = (hRoot) ? hRoot : TreeView_GetRoot(hTreeWnd);
if (!hStartTree)
return FALSE;
BOOL fDone = FALSE;
BOOL fFindNext = FALSE;
BOOL fResult = FALSE;
BOOL fStartDoc = FALSE;
BOOL fDisableWindow = FALSE;
LPTSTR pstrTitle = nullptr;
LPTSTR pstrBuff = nullptr;
DWORD dwCurrCopy;
HANDLE hHeap = nullptr;
DWORD buffSize;
DWORD cchLen;
TV_ITEM tvi;
// Initialize Print Dialog structure
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.Flags = PD_ALLPAGES | PD_RETURNDC;
pd.nCopies = 1;
PRINTCBINFO pci = {};
TEXTMETRIC tm = {};
if (g_PrintToFile)
{
pci.hdcPrint = nullptr;
}
else
{
// Call Common Print Dialog to get printer DC
if (!PrintDlg(&pd) || !pd.hDC)
{
// Print Dialog failed or user canceled
return TRUE;
}
pci.hdcPrint = pd.hDC;
// Get Text metrics for printing
if (!GetTextMetrics(pci.hdcPrint, &tm))
{
// Error, TextMetrics failed
goto lblCLEANUP;
}
}
if (g_PrintToFile)
{
pci.dwLineHeight = 1;
pci.dwCharWidth = 1;
pci.dwCharsPerLine = 80;
pci.dwLinesPerPage = 66;
}
else
{
pci.dwLineHeight = tm.tmHeight + tm.tmExternalLeading;
pci.dwCharWidth = tm.tmAveCharWidth;
pci.dwCharsPerLine = GetDeviceCaps(pci.hdcPrint, HORZRES) / pci.dwCharWidth;
pci.dwLinesPerPage = GetDeviceCaps(pci.hdcPrint, VERTRES) / pci.dwLineHeight;
}
// Get Heap
hHeap = GetProcessHeap();
if (!hHeap)
{
// Error, no heap associated with this process
goto lblCLEANUP;
}
// Create line buffer
buffSize = (pci.dwCharsPerLine + 1) * sizeof(TCHAR);
pstrBuff = (LPTSTR)HeapAlloc(hHeap, HEAP_NO_SERIALIZE, buffSize);
if (!pstrBuff)
{
// Error, not enough memory
goto lblCLEANUP;
}
*pstrBuff = 0;
// Disable Parent window
EnableWindow(hWnd, FALSE);
fDisableWindow = TRUE;
// Start Printer Abort Dialog
g_fAbortPrint = FALSE;
g_hAbortPrintDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_ABORTPRINTDLG),
hWnd, (DLGPROC)PrintDialogProc);
if (!g_hAbortPrintDlg)
{
// Error, unable to create abort dialog
goto lblCLEANUP;
}
//
// Set Document title to Root string
//
tvi.mask = TVIF_CHILDREN | TVIF_TEXT;
tvi.hItem = hStartTree;
tvi.pszText = pstrBuff;
tvi.cchTextMax = pci.dwCharsPerLine;
if (TreeView_GetItem(hTreeWnd, &tvi))
{
SetWindowText(g_hAbortPrintDlg, pstrBuff);
SetAbortProc(pd.hDC, AbortProc);
cchLen = static_cast<DWORD>(_tcsclen(pstrBuff));
DWORD cbSize = (cchLen + 1) * sizeof(TCHAR);
pstrTitle = (LPTSTR)HeapAlloc(hHeap, HEAP_NO_SERIALIZE, cbSize);
if (!pstrTitle)
{
// Error, not enough memory
goto lblCLEANUP;
}
strcpy_s(pstrTitle, cbSize, pstrBuff);
pstrTitle[cchLen] = 0;
}
else
{
SetWindowText(g_hAbortPrintDlg, TEXT("Unknown"));
SetAbortProc(pd.hDC, AbortProc);
cchLen = static_cast<DWORD>(_tcsclen(TEXT("Unknown")));
DWORD cbSize = (cchLen + 1) * sizeof(TCHAR);
pstrTitle = (LPTSTR)HeapAlloc(hHeap, HEAP_NO_SERIALIZE, cbSize);
if (!pstrTitle)
{
// Error, not enough memory
goto lblCLEANUP;
}
strcpy_s(pstrTitle, cbSize, TEXT("Unknown"));
pstrTitle[cchLen] = 0;
}
// Initialize Document Structure
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = pstrTitle;
di.lpszOutput = nullptr;
di.lpszDatatype = nullptr;
di.fwType = 0;
// Start document
if (g_PrintToFile)
{
const TCHAR* pstrFile;
TCHAR buff[MAX_PATH];
if (strlen(g_PrintToFilePath) > 0)
pstrFile = g_PrintToFilePath;
else
{
HRESULT hr = SHGetFolderPath(nullptr, CSIDL_DESKTOP, nullptr, SHGFP_TYPE_CURRENT, buff);
if (SUCCEEDED(hr))
{
strcat_s(buff, MAX_PATH, TEXT("\\dxview.log"));
pstrFile = buff;
}
else
pstrFile = TEXT("dxview.log");
}
g_FileHandle = CreateFile(pstrFile, GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
else
if (StartDoc(pd.hDC, &di) < 0)
{
// Error, StartDoc failed
goto lblCLEANUP;
}
fStartDoc = TRUE;
// Print requested number of copies
fResult = FALSE;
for (dwCurrCopy = 0; dwCurrCopy < (DWORD)pd.nCopies; dwCurrCopy++)
{
pci.hCurrTree = hStartTree;
pci.fStartPage = TRUE;
pci.dwCurrIndent = 0;
// Note: We are basically doing an pre-order traversal
// of the Tree. Printing the current node
// before moving on to it's children or siblings
fDone = FALSE;
while (!fDone)
{
// Check if we need to start a new page
if (FAILED(PrintStartPage(&pci)))
{
goto lblCLEANUP;
}
//
// Get Current Item in Tree
// and print it's text info and associated Node caps
//
tvi.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_PARAM;
tvi.hItem = pci.hCurrTree;
tvi.pszText = pstrBuff;
tvi.lParam = 0;
tvi.cchTextMax = pci.dwCharsPerLine;
if (TreeView_GetItem(hTreeWnd, &tvi))
{
cchLen = static_cast<DWORD>(_tcslen(pstrBuff));
cchLen = __min(cchLen, buffSize);
if (cchLen > 0)
{
int xOffset = (int)(pci.dwCurrIndent * DEF_TAB_SIZE * pci.dwCharWidth);
int yOffset = (int)(pci.dwLineHeight * pci.dwCurrLine);
// Print this line
if (FAILED(PrintLine(xOffset, yOffset, pstrBuff, cchLen, &pci)))
{
goto lblCLEANUP;
}
// Advance to next line in page
if (FAILED(PrintNextLine(&pci)))
{
goto lblCLEANUP;
}
// Check if there is any additional node info
// that needs to be printed
if (tvi.lParam != 0)
{
NODEINFO* pni = (NODEINFO*)tvi.lParam;
if (pni && pni->fnDisplayCallback)
{
// Force indent to offset node info from tree info
pci.dwCurrIndent += 2;
if (pni->bUseLParam3)
{
if (FAILED(((DISPLAYCALLBACKEX)(pni->fnDisplayCallback))(pni->lParam1, pni->lParam2, pni->lParam3, &pci)))
{
// Error, callback failed
goto lblCLEANUP;
}
}
else
{
if (FAILED(pni->fnDisplayCallback(pni->lParam1, pni->lParam2, &pci)))
{
// Error, callback failed
goto lblCLEANUP;
}
}
// Recover indent
pci.dwCurrIndent -= 2;
}
}
}
} // End if TreeView_GetItem()
//
// Get Next Item in tree
//
// Get first child, if any
if (tvi.cChildren)
{
// Get First child
HTREEITEM hTempTree = TreeView_GetChild(hTreeWnd, pci.hCurrTree);
if (hTempTree)
{
// Increase Indentation
pci.dwCurrIndent++;
pci.hCurrTree = hTempTree;
continue;
}
}
// Exit, if we are the root
if (pci.hCurrTree == hRoot)
{
// We have reached the root, so stop
PrintEndPage(&pci);
fDone = TRUE;
continue;
}
// Get next sibling in the chain
HTREEITEM hTempTree = TreeView_GetNextSibling(hTreeWnd, pci.hCurrTree);
if (hTempTree)
{
pci.hCurrTree = hTempTree;
continue;
}
// Get next Ancestor yet to be processed
// (uncle, granduncle, etc)
fFindNext = FALSE;
while (!fFindNext)
{
hTempTree = TreeView_GetParent(hTreeWnd, pci.hCurrTree);
if ((!hTempTree) || (hTempTree == hRoot))
{
// We have reached the root, so stop
PrintEndPage(&pci);
fDone = TRUE;
fFindNext = TRUE;
}
else
{
// Move up to the parent
pci.hCurrTree = hTempTree;
// Decrease Indentation
pci.dwCurrIndent--;
// Since we have already processed the parent
// we want to get the uncle/aunt node
hTempTree = TreeView_GetNextSibling(hTreeWnd, pci.hCurrTree);
if (hTempTree)
{
// Found a non-processed node
pci.hCurrTree = hTempTree;
fFindNext = TRUE;
}
}
}
} // End While (! fDone)
} // End for num copies
// Success
fResult = TRUE;
lblCLEANUP:
// End Document
if (fStartDoc)
{
if (g_PrintToFile)
{
CloseHandle(g_FileHandle);
}
else
EndDoc(pd.hDC);
fStartDoc = FALSE;
}
// Re-enable main window
// Note: Do this before destroying abort dialog
// otherwise the main window loses focus
if (fDisableWindow)
{
EnableWindow(hWnd, TRUE);
fDisableWindow = FALSE;
}
// Destroy Abort Dialog
if (g_hAbortPrintDlg)
{
DestroyWindow(g_hAbortPrintDlg);
g_hAbortPrintDlg = nullptr;
}
// Free title memory
if (pstrTitle)
{
HeapFree(hHeap, 0, (VOID*)pstrTitle);
pstrTitle = nullptr;
di.lpszDocName = nullptr;
}
// Free buffer memory
if (pstrBuff)
{
HeapFree(hHeap, 0, (VOID*)pstrBuff);
pstrBuff = nullptr;
}
// Cleanup printer DC
if (pd.hDC)
{
DeleteDC(pd.hDC);
pd.hDC = nullptr;
}
return fResult;
}
}
//-----------------------------------------------------------------------------
// Name: DXView_OnPrint()
// Desc: Print user defined stuff
//-----------------------------------------------------------------------------
BOOL DXView_OnPrint(HWND hWnd, HWND hTreeWnd, BOOL bPrintAll)
{
HINSTANCE hInstance;
HTREEITEM hRoot;
// Check Parameters
if (!hWnd || !hTreeWnd)
return FALSE;
// Get hInstance
hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
if (!hInstance)
return FALSE;
if (bPrintAll)
{
hRoot = nullptr;
}
else
{
hRoot = TreeView_GetSelection(hTreeWnd);
if (!hRoot)
DoMessage(IDS_PRINT_WARNING, IDS_PRINT_NEEDSELECT);
}
g_PrintToFile = FALSE;
// Do actual printing
return PrintTreeStats(hInstance, hWnd, hTreeWnd, hRoot);
}
//-----------------------------------------------------------------------------
// Name: DXView_OnFile()
//-----------------------------------------------------------------------------
BOOL DXView_OnFile(HWND hWnd, HWND hTreeWnd, BOOL bPrintAll)
{
HINSTANCE hInstance;
HTREEITEM hRoot;
// Check Parameters
if (!hWnd || !hTreeWnd)
return FALSE;
// Get hInstance
hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
if (!hInstance)
return FALSE;
if (bPrintAll)
{
hRoot = nullptr;
}
else
{
hRoot = TreeView_GetSelection(hTreeWnd);
if (!hRoot)
DoMessage(IDS_PRINT_WARNING, IDS_PRINT_NEEDSELECT);
}
g_PrintToFile = TRUE;
// Do actual printing
return PrintTreeStats(hInstance, hWnd, hTreeWnd, hRoot);
}
//-----------------------------------------------------------------------------
// Name: PrintLine()
// Desc: Prints text to page at specified location
//-----------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT PrintLine(int xOffset, int yOffset, LPCTSTR pszBuff, size_t cchBuff,
PRINTCBINFO* pci)
{
if (!pci)
return E_FAIL;
// Check if we need to start a new page
if (FAILED(PrintStartPage(pci)))
return E_FAIL;
// Return if there's nothing to print
if (!pszBuff || !cchBuff)
return S_OK;
// Print text out to buffer current line
if (g_PrintToFile)
{
DWORD dwDummy;
TCHAR Temp[80];
int offset = (xOffset - iLastXPos) / pci->dwCharWidth;
if (offset < 0 || offset >= 80)
return S_OK;
memset(Temp, ' ', sizeof(TCHAR) * 79);
Temp[offset] = 0;
WriteFile(g_FileHandle, Temp, (xOffset - iLastXPos) / pci->dwCharWidth, &dwDummy, nullptr);
iLastXPos = (xOffset - iLastXPos) + (pci->dwCharWidth * static_cast<DWORD>(cchBuff));
WriteFile(g_FileHandle, pszBuff, static_cast<DWORD>(cchBuff), &dwDummy, nullptr);
}
else
{
TextOut(pci->hdcPrint, xOffset, yOffset, pszBuff, static_cast<int>(cchBuff));
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: PrintNextLine()
// Desc: Advance to next line on page
//-----------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT PrintNextLine(PRINTCBINFO* pci)
{
if (g_PrintToFile)
{
DWORD dwDummy;
WriteFile(g_FileHandle, "\r\n", 2, &dwDummy, nullptr);
iLastXPos = 0;
return S_OK;
}
if (!pci)
return E_FAIL;
pci->dwCurrLine++;
// Check if we need to end the page
if (pci->dwCurrLine >= pci->dwLinesPerPage)
return PrintEndPage(pci);
return S_OK;
}