2 * Wordpad implementation - Printing and print preview functions
4 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 typedef struct _previewinfo
37 } previewinfo
, *ppreviewinfo
;
39 static HGLOBAL devMode
;
40 static HGLOBAL devNames
;
43 static previewinfo preview
;
45 static const WCHAR var_pagemargin
[] = {'P','a','g','e','M','a','r','g','i','n',0};
47 static LPWSTR
get_print_file_filter(HWND hMainWnd
)
49 static WCHAR wszPrintFilter
[MAX_STRING_LEN
*2+6+4+1];
50 const WCHAR files_prn
[] = {'*','.','P','R','N',0};
51 const WCHAR files_all
[] = {'*','.','*','\0'};
53 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
56 LoadStringW(hInstance
, STRING_PRINTER_FILES_PRN
, p
, MAX_STRING_LEN
);
58 lstrcpyW(p
, files_prn
);
60 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
62 lstrcpyW(p
, files_all
);
66 return wszPrintFilter
;
69 void registry_set_pagemargins(HKEY hKey
)
71 RegSetValueExW(hKey
, var_pagemargin
, 0, REG_BINARY
, (LPBYTE
)&margins
, sizeof(RECT
));
74 void registry_read_pagemargins(HKEY hKey
)
76 DWORD size
= sizeof(RECT
);
78 if(!hKey
|| RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
79 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
82 margins
.bottom
= 1417;
88 static void AddTextButton(HWND hRebarWnd
, UINT string
, UINT command
, UINT id
)
91 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hRebarWnd
, GWLP_HINSTANCE
);
92 WCHAR text
[MAX_STRING_LEN
];
95 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
96 hButton
= CreateWindowW(WC_BUTTONW
, text
,
97 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
98 hRebarWnd
, (HMENU
)ULongToHandle(command
), hInstance
, NULL
);
100 rb
.cbSize
= sizeof(rb
);
101 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
102 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
103 rb
.hwndChild
= hButton
;
104 rb
.cyChild
= rb
.cyMinChild
= 22;
105 rb
.cx
= rb
.cxMinChild
= 90;
109 SendMessageW(hRebarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
112 static HDC
make_dc(void)
114 if(devNames
&& devMode
)
116 LPDEVNAMES dn
= GlobalLock(devNames
);
117 LPDEVMODEW dm
= GlobalLock(devMode
);
120 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
121 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
133 static LONG
twips_to_centmm(int twips
)
135 return MulDiv(twips
, 1000, TWIPS_PER_CM
);
138 static LONG
centmm_to_twips(int mm
)
140 return MulDiv(mm
, TWIPS_PER_CM
, 1000);
143 static LONG
twips_to_pixels(int twips
, int dpi
)
145 float ret
= ((float)twips
/ ((float)TWIPS_PER_CM
* 2.54)) * (float)dpi
;
149 static LONG
devunits_to_twips(int units
, int dpi
)
151 float ret
= ((float)units
/ (float)dpi
) * (float)TWIPS_PER_CM
* 2.54;
156 static RECT
get_print_rect(HDC hdc
)
163 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
164 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
165 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
166 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
169 width
= centmm_to_twips(18500);
170 height
= centmm_to_twips(27000);
173 rc
.left
= margins
.left
;
174 rc
.right
= width
- margins
.right
;
175 rc
.top
= margins
.top
;
176 rc
.bottom
= height
- margins
.bottom
;
181 void target_device(HWND hMainWnd
, DWORD wordWrap
)
183 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
187 if(wordWrap
== ID_WORDWRAP_MARGIN
)
189 RECT rc
= get_print_rect(hdc
);
190 width
= rc
.right
- rc
.left
;
195 HDC hMaindc
= GetDC(hMainWnd
);
196 hdc
= CreateCompatibleDC(hMaindc
);
197 ReleaseDC(hMainWnd
, hMaindc
);
200 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
205 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
208 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
209 static const WCHAR defExt
[] = {'P','R','N',0};
210 static LPWSTR file_filter
;
213 file_filter
= get_print_file_filter(hMainWnd
);
215 ZeroMemory(&ofn
, sizeof(ofn
));
217 ofn
.lStructSize
= sizeof(ofn
);
218 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
219 ofn
.hwndOwner
= hMainWnd
;
220 ofn
.lpstrFilter
= file_filter
;
221 ofn
.lpstrFile
= (LPWSTR
)file
;
222 ofn
.nMaxFile
= MAX_PATH
;
223 ofn
.lpstrDefExt
= (LPWSTR
)defExt
;
225 if(GetSaveFileNameW(&ofn
))
231 static int get_num_pages(HWND hEditorWnd
, FORMATRANGE fr
)
239 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
,
242 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
247 static void char_from_pagenum(HWND hEditorWnd
, FORMATRANGE
*fr
, int page
)
253 for(i
= 1; i
< page
; i
++)
255 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)fr
);
259 static HWND
get_ruler_wnd(HWND hMainWnd
)
261 return GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
264 void redraw_ruler(HWND hRulerWnd
)
268 GetClientRect(hRulerWnd
, &rc
);
269 InvalidateRect(hRulerWnd
, &rc
, TRUE
);
272 static void update_ruler(HWND hRulerWnd
)
274 SendMessageW(hRulerWnd
, WM_USER
, 0, 0);
275 redraw_ruler(hRulerWnd
);
278 static void print(LPPRINTDLGW pd
, LPWSTR wszFileName
)
282 HWND hEditorWnd
= GetDlgItem(pd
->hwndOwner
, IDC_EDITOR
);
283 int printedPages
= 0;
286 fr
.hdcTarget
= pd
->hDC
;
288 fr
.rc
= get_print_rect(fr
.hdc
);
290 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
292 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
294 ZeroMemory(&di
, sizeof(di
));
295 di
.cbSize
= sizeof(di
);
296 di
.lpszDocName
= wszFileName
;
298 if(pd
->Flags
& PD_PRINTTOFILE
)
300 di
.lpszOutput
= dialog_print_to_file(pd
->hwndOwner
);
305 if(pd
->Flags
& PD_SELECTION
)
307 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
311 gt
.flags
= GTL_DEFAULT
;
314 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
316 if(pd
->Flags
& PD_PAGENUMS
)
317 char_from_pagenum(hEditorWnd
, &fr
, pd
->nToPage
);
320 StartDocW(fr
.hdc
, &di
);
323 if(StartPage(fr
.hdc
) <= 0)
326 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
328 if(EndPage(fr
.hdc
) <= 0)
332 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
335 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
338 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
341 void dialog_printsetup(HWND hMainWnd
)
345 ZeroMemory(&ps
, sizeof(ps
));
346 ps
.lStructSize
= sizeof(ps
);
347 ps
.hwndOwner
= hMainWnd
;
348 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
349 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
350 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
351 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
352 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
353 ps
.hDevMode
= devMode
;
354 ps
.hDevNames
= devNames
;
356 if(PageSetupDlgW(&ps
))
358 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
359 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
360 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
361 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
362 devMode
= ps
.hDevMode
;
363 devNames
= ps
.hDevNames
;
364 update_ruler(get_ruler_wnd(hMainWnd
));
368 void get_default_printer_opts(void)
371 ZeroMemory(&pd
, sizeof(pd
));
373 ZeroMemory(&pd
, sizeof(pd
));
374 pd
.lStructSize
= sizeof(pd
);
375 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
376 pd
.hDevMode
= devMode
;
380 devMode
= pd
.hDevMode
;
381 devNames
= pd
.hDevNames
;
384 void print_quick(LPWSTR wszFileName
)
388 ZeroMemory(&pd
, sizeof(pd
));
391 print(&pd
, wszFileName
);
394 void dialog_print(HWND hMainWnd
, LPWSTR wszFileName
)
397 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
401 ZeroMemory(&pd
, sizeof(pd
));
402 pd
.lStructSize
= sizeof(pd
);
403 pd
.hwndOwner
= hMainWnd
;
404 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
407 pd
.hDevMode
= devMode
;
408 pd
.hDevNames
= devNames
;
410 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
412 pd
.Flags
|= PD_NOSELECTION
;
416 devMode
= pd
.hDevMode
;
417 devNames
= pd
.hDevNames
;
418 print(&pd
, wszFileName
);
419 update_ruler(get_ruler_wnd(hMainWnd
));
423 static void preview_bar_show(HWND hMainWnd
, BOOL show
)
425 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
433 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
434 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
435 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
436 AddTextButton(hReBar
, STRING_PREVIEW_TWOPAGES
, ID_PREVIEW_NUMPAGES
, BANDID_PREVIEW_BTN4
);
437 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN5
);
439 hStatic
= CreateWindowW(WC_STATICW
, NULL
,
440 WS_VISIBLE
| WS_CHILD
, 0, 0, 0, 0,
441 hReBar
, NULL
, NULL
, NULL
);
443 rb
.cbSize
= sizeof(rb
);
444 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
445 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
446 rb
.hwndChild
= hStatic
;
447 rb
.cyChild
= rb
.cyMinChild
= 22;
448 rb
.cx
= rb
.cxMinChild
= 90;
450 rb
.wID
= BANDID_PREVIEW_BUFFER
;
452 SendMessageW(hReBar
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
455 for(i
= 0; i
<= PREVIEW_BUTTONS
; i
++)
456 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
460 void init_preview(HWND hMainWnd
, LPWSTR wszFileName
)
465 preview
.wszFileName
= wszFileName
;
466 preview_bar_show(hMainWnd
, TRUE
);
469 void close_preview(HWND hMainWnd
)
471 preview
.window
.right
= 0;
472 preview
.window
.bottom
= 0;
476 preview_bar_show(hMainWnd
, FALSE
);
479 BOOL
preview_isactive(void)
481 return preview
.page
!= 0;
484 static void add_ruler_units(HDC hdcRuler
, RECT
* drawRect
, BOOL NewMetrics
, long EditLeftmost
)
490 static HBITMAP hBitmap
;
491 int i
, x
, y
, RulerTextEnd
;
495 WCHAR FontName
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
500 DeleteObject(hBitmap
);
503 hdc
= CreateCompatibleDC(0);
505 CmPixels
= twips_to_pixels(TWIPS_PER_CM
, GetDeviceCaps(hdc
, LOGPIXELSX
));
506 QuarterCmPixels
= (int)((float)CmPixels
/ 4.0);
508 hBitmap
= CreateCompatibleBitmap(hdc
, drawRect
->right
, drawRect
->bottom
);
509 SelectObject(hdc
, hBitmap
);
510 FillRect(hdc
, drawRect
, GetStockObject(WHITE_BRUSH
));
512 hFont
= CreateFontW(10, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName
);
514 SelectObject(hdc
, hFont
);
515 SetBkMode(hdc
, TRANSPARENT
);
516 SetTextAlign(hdc
, TA_CENTER
);
517 y
= (int)(((float)drawRect
->bottom
- (float)drawRect
->top
) / 2.0) + 1;
518 RulerTextEnd
= drawRect
->right
- EditLeftmost
+ 1;
519 for(i
= 1, x
= EditLeftmost
; x
< (drawRect
->right
- EditLeftmost
+ 1); i
++)
522 WCHAR format
[] = {'%','d',0};
525 x2
+= QuarterCmPixels
;
526 if(x2
> RulerTextEnd
)
529 MoveToEx(hdc
, x2
, y
, NULL
);
530 LineTo(hdc
, x2
, y
+2);
532 x2
+= QuarterCmPixels
;
533 if(x2
> RulerTextEnd
)
536 MoveToEx(hdc
, x2
, y
- 3, NULL
);
537 LineTo(hdc
, x2
, y
+ 3);
539 x2
+= QuarterCmPixels
;
540 if(x2
> RulerTextEnd
)
543 MoveToEx(hdc
, x2
, y
, NULL
);
544 LineTo(hdc
, x2
, y
+2);
550 wsprintfW(str
, format
, i
);
551 TextOutW(hdc
, x
, 5, str
, lstrlenW(str
));
556 BitBlt(hdcRuler
, 0, 0, drawRect
->right
, drawRect
->bottom
, hdc
, 0, 0, SRCAND
);
559 static void paint_ruler(HWND hWnd
, long EditLeftmost
, BOOL NewMetrics
)
562 HDC hdc
= BeginPaint(hWnd
, &ps
);
563 HDC hdcPrint
= make_dc();
564 RECT printRect
= get_print_rect(hdcPrint
);
566 HBRUSH hBrush
= CreateSolidBrush(GetSysColor(COLOR_MENU
));
568 GetClientRect(hWnd
, &drawRect
);
569 FillRect(hdc
, &drawRect
, hBrush
);
572 drawRect
.bottom
-= 3;
573 drawRect
.left
= EditLeftmost
;
574 drawRect
.right
= twips_to_pixels(printRect
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
575 FillRect(hdc
, &drawRect
, GetStockObject(WHITE_BRUSH
));
579 DrawEdge(hdc
, &drawRect
, EDGE_SUNKEN
, BF_RECT
);
581 drawRect
.left
= drawRect
.right
- 1;
582 drawRect
.right
= twips_to_pixels(printRect
.right
+ margins
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
583 DrawEdge(hdc
, &drawRect
, EDGE_ETCHED
, BF_RECT
);
587 add_ruler_units(hdc
, &drawRect
, NewMetrics
, EditLeftmost
);
589 SelectObject(hdc
, GetStockObject(BLACK_BRUSH
));
590 DeleteObject(hBrush
);
595 LRESULT CALLBACK
ruler_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
597 static WNDPROC pPrevRulerProc
;
598 static long EditLeftmost
;
599 static BOOL NewMetrics
;
606 EditLeftmost
= ((POINTL
*)wParam
)->x
;
607 pPrevRulerProc
= (WNDPROC
)lParam
;
613 paint_ruler(hWnd
, EditLeftmost
, NewMetrics
);
617 return CallWindowProcW(pPrevRulerProc
, hWnd
, msg
, wParam
, lParam
);
623 static void draw_preview_page(HDC hdc
, HDC
* hdcSized
, FORMATRANGE
* lpFr
, float ratio
, int bmNewWidth
, int bmNewHeight
, int bmWidth
, int bmHeight
)
625 HBITMAP hBitmapScaled
= CreateCompatibleBitmap(hdc
, bmNewWidth
, bmNewHeight
);
627 int TopMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.top
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
628 int BottomMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
629 int LeftMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.left
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
630 int RightMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.right
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
634 *hdcSized
= CreateCompatibleDC(hdc
);
635 SelectObject(*hdcSized
, hBitmapScaled
);
637 StretchBlt(*hdcSized
, 0, 0, bmNewWidth
, bmNewHeight
, hdc
, 0, 0, bmWidth
, bmHeight
, SRCCOPY
);
639 /* Draw margin lines */
640 hPen
= CreatePen(PS_DOT
, 1, RGB(0,0,0));
641 SelectObject(*hdcSized
, hPen
);
643 MoveToEx(*hdcSized
, 0, TopMargin
, NULL
);
644 LineTo(*hdcSized
, bmNewWidth
, TopMargin
);
645 MoveToEx(*hdcSized
, 0, BottomMargin
, NULL
);
646 LineTo(*hdcSized
, bmNewWidth
, BottomMargin
);
648 MoveToEx(*hdcSized
, LeftMargin
, 0, NULL
);
649 LineTo(*hdcSized
, LeftMargin
, bmNewHeight
);
650 MoveToEx(*hdcSized
, RightMargin
, 0, NULL
);
651 LineTo(*hdcSized
, RightMargin
, bmNewHeight
);
655 static void draw_preview(HWND hEditorWnd
, FORMATRANGE
* lpFr
, int bmWidth
, int bmHeight
, RECT
* paper
, int page
)
657 HBITMAP hBitmapCapture
= CreateCompatibleBitmap(lpFr
->hdc
, bmWidth
, bmHeight
);
659 char_from_pagenum(hEditorWnd
, lpFr
, page
);
660 SelectObject(lpFr
->hdc
, hBitmapCapture
);
661 FillRect(lpFr
->hdc
, paper
, GetStockObject(WHITE_BRUSH
));
662 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)lpFr
);
663 /* EM_FORMATRANGE sets fr.rc to indicate the area printed in, but we want to
664 keep the original for drawing margins */
665 lpFr
->rc
= get_print_rect(lpFr
->hdcTarget
);
666 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
669 LRESULT
print_preview(HWND hMainWnd
)
674 RECT window
, background
;
675 int bmWidth
, bmHeight
, bmNewWidth
, bmNewHeight
;
676 float ratioWidth
, ratioHeight
, ratio
;
677 int xOffset
, yOffset
;
679 float spacing
= 20.0;
680 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
683 hdc
= BeginPaint(hMainWnd
, &ps
);
684 GetClientRect(hMainWnd
, &window
);
686 fr
.hdcTarget
= make_dc();
687 fr
.rc
= get_print_rect(fr
.hdcTarget
);
690 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
691 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
693 bmWidth
= twips_to_pixels(fr
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
694 bmHeight
= twips_to_pixels(fr
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
699 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
701 preview
.hdc
= CreateCompatibleDC(hdc
);
705 if(preview
.hdc2
!= (HDC
)-1)
706 DeleteDC(preview
.hdc2
);
707 preview
.hdc2
= CreateCompatibleDC(hdc
);
710 fr
.hdc
= preview
.hdc
;
711 gt
.flags
= GTL_DEFAULT
;
714 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
717 paper
.right
= bmWidth
;
719 paper
.bottom
= bmHeight
;
722 preview
.pages
= get_num_pages(hEditorWnd
, fr
);
724 fr
.hdc
= preview
.hdc
;
725 draw_preview(hEditorWnd
, &fr
, bmWidth
, bmHeight
, &paper
, preview
.page
);
729 fr
.hdc
= preview
.hdc2
;
730 draw_preview(hEditorWnd
, &fr
, bmWidth
, bmHeight
, &paper
, preview
.page
+ 1);
733 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
734 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
), preview
.hdc2
?
735 (preview
.page
+ 1) < preview
.pages
:
736 preview
.page
< preview
.pages
);
737 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), preview
.pages
> 1);
740 barheight
= SendMessageW(hReBar
, RB_GETBARHEIGHT
, 0, 0);
741 ratioHeight
= ((float)window
.bottom
- spacing
- (float)barheight
) / (float)bmHeight
;
744 ratioWidth
= ((float)window
.right
/ 2.0 - spacing
* 2.0) / (float)bmWidth
;
746 ratioWidth
= ((float)window
.right
- spacing
* 3.0) / (float)bmWidth
;
748 if(ratioWidth
> ratioHeight
)
753 bmNewWidth
= (int)((float)bmWidth
* ratio
);
754 bmNewHeight
= (int)((float)bmHeight
* ratio
);
756 yOffset
= ((window
.bottom
- bmNewHeight
+ barheight
) / 2);
759 xOffset
= (window
.right
- bmNewWidth
) / 2;
761 xOffset
= (window
.right
- bmNewWidth
* 2) / 2;
763 window
.top
= barheight
;
764 FillRect(hdc
, &window
, GetStockObject(GRAY_BRUSH
));
766 background
.left
= xOffset
- 2;
767 background
.right
= xOffset
+ bmNewWidth
+ 2;
768 background
.top
= yOffset
- 2;
769 background
.bottom
= yOffset
+ bmNewHeight
+ 2;
771 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
773 if(window
.right
!= preview
.window
.right
|| window
.bottom
!= preview
.window
.bottom
)
775 draw_preview_page(preview
.hdc
, &preview
.hdcSized
, &fr
, ratio
, bmNewWidth
, bmNewHeight
, bmWidth
, bmHeight
);
779 background
.left
+= bmNewWidth
+ spacing
;
780 background
.right
+= bmNewWidth
+ spacing
;
782 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
784 draw_preview_page(preview
.hdc2
, &preview
.hdcSized2
, &fr
, ratio
, bmNewWidth
, bmNewHeight
, bmWidth
, bmHeight
);
788 BitBlt(hdc
, xOffset
, yOffset
, bmNewWidth
, bmNewHeight
, preview
.hdcSized
, 0, 0, SRCCOPY
);
792 BitBlt(hdc
, xOffset
+ bmNewWidth
+ spacing
, yOffset
, bmNewWidth
, bmNewHeight
, preview
.hdcSized2
, 0, 0, SRCCOPY
);
795 DeleteDC(fr
.hdcTarget
);
796 preview
.window
= window
;
798 EndPaint(hMainWnd
, &ps
);
803 static void update_preview(HWND hWnd
)
807 DeleteDC(preview
.hdc
);
810 preview
.window
.right
= 0;
812 GetClientRect(hWnd
, &rc
);
813 rc
.top
+= SendMessageW(GetDlgItem(hWnd
, IDC_REBAR
), RB_GETBARHEIGHT
, 0, 0);
814 InvalidateRect(hWnd
, &rc
, TRUE
);
817 LRESULT
preview_command(HWND hWnd
, WPARAM wParam
)
819 switch(LOWORD(wParam
))
822 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
825 case ID_PREVIEW_NEXTPAGE
:
826 case ID_PREVIEW_PREVPAGE
:
828 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
833 update_preview(hWnd
);
837 case ID_PREVIEW_NUMPAGES
:
839 HWND hReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
840 WCHAR name
[MAX_STRING_LEN
];
841 HINSTANCE hInst
= (HINSTANCE
)GetWindowLongPtrW(hWnd
, GWLP_HINSTANCE
);
845 DeleteDC(preview
.hdc2
);
849 if(preview
.page
== preview
.pages
)
851 preview
.hdc2
= (HDC
)-1;
854 LoadStringW(hInst
, preview
.hdc2
? STRING_PREVIEW_ONEPAGE
: STRING_PREVIEW_TWOPAGES
,
855 name
, MAX_STRING_LEN
);
857 SetWindowTextW(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), name
);
858 update_preview(hWnd
);
863 dialog_print(hWnd
, preview
.wszFileName
);
864 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);