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
43 } previewinfo
, *ppreviewinfo
;
45 static HGLOBAL devMode
;
46 static HGLOBAL devNames
;
49 static previewinfo preview
;
51 extern const WCHAR wszPreviewWndClass
[];
53 static const WCHAR var_pagemargin
[] = {'P','a','g','e','M','a','r','g','i','n',0};
55 static LPWSTR
get_print_file_filter(HWND hMainWnd
)
57 static WCHAR wszPrintFilter
[MAX_STRING_LEN
*2+6+4+1];
58 const WCHAR files_prn
[] = {'*','.','P','R','N',0};
59 const WCHAR files_all
[] = {'*','.','*','\0'};
61 HINSTANCE hInstance
= GetModuleHandleW(0);
64 LoadStringW(hInstance
, STRING_PRINTER_FILES_PRN
, p
, MAX_STRING_LEN
);
66 lstrcpyW(p
, files_prn
);
68 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
70 lstrcpyW(p
, files_all
);
74 return wszPrintFilter
;
77 void registry_set_pagemargins(HKEY hKey
)
79 RegSetValueExW(hKey
, var_pagemargin
, 0, REG_BINARY
, (LPBYTE
)&margins
, sizeof(RECT
));
82 void registry_read_pagemargins(HKEY hKey
)
84 DWORD size
= sizeof(RECT
);
86 if(!hKey
|| RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
87 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
90 margins
.bottom
= 1417;
96 static void AddTextButton(HWND hRebarWnd
, UINT string
, UINT command
, UINT id
)
99 HINSTANCE hInstance
= GetModuleHandleW(0);
100 WCHAR text
[MAX_STRING_LEN
];
103 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
104 hButton
= CreateWindowW(WC_BUTTONW
, text
,
105 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
106 hRebarWnd
, ULongToHandle(command
), hInstance
, NULL
);
108 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
109 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
110 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
111 rb
.hwndChild
= hButton
;
112 rb
.cyChild
= rb
.cyMinChild
= 22;
113 rb
.cx
= rb
.cxMinChild
= 90;
117 SendMessageW(hRebarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
120 static HDC
make_dc(void)
122 if(devNames
&& devMode
)
124 LPDEVNAMES dn
= GlobalLock(devNames
);
125 LPDEVMODEW dm
= GlobalLock(devMode
);
128 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
129 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
141 static LONG
twips_to_centmm(int twips
)
143 return MulDiv(twips
, CENTMM_PER_INCH
, TWIPS_PER_INCH
);
146 static LONG
centmm_to_twips(int mm
)
148 return MulDiv(mm
, TWIPS_PER_INCH
, CENTMM_PER_INCH
);
151 static LONG
twips_to_pixels(int twips
, int dpi
)
153 return MulDiv(twips
, dpi
, TWIPS_PER_INCH
);
156 static LONG
devunits_to_twips(int units
, int dpi
)
158 return MulDiv(units
, TWIPS_PER_INCH
, dpi
);
162 static RECT
get_print_rect(HDC hdc
)
169 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
170 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
171 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
172 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
175 width
= centmm_to_twips(18500);
176 height
= centmm_to_twips(27000);
179 rc
.left
= margins
.left
;
180 rc
.right
= width
- margins
.right
;
181 rc
.top
= margins
.top
;
182 rc
.bottom
= height
- margins
.bottom
;
187 void target_device(HWND hMainWnd
, DWORD wordWrap
)
189 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
191 if(wordWrap
== ID_WORDWRAP_MARGIN
)
196 RECT rc
= get_print_rect(hdc
);
198 width
= rc
.right
- rc
.left
;
201 HDC hMaindc
= GetDC(hMainWnd
);
202 hdc
= CreateCompatibleDC(hMaindc
);
203 ReleaseDC(hMainWnd
, hMaindc
);
205 result
= SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
209 /* otherwise EM_SETTARGETDEVICE failed, so fall back on wrapping
210 * to window using the NULL DC. */
213 if (wordWrap
!= ID_WORDWRAP_NONE
) {
214 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 0);
216 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 1);
221 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
224 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
225 static const WCHAR defExt
[] = {'P','R','N',0};
226 static LPWSTR file_filter
;
229 file_filter
= get_print_file_filter(hMainWnd
);
231 ZeroMemory(&ofn
, sizeof(ofn
));
233 ofn
.lStructSize
= sizeof(ofn
);
234 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
235 ofn
.hwndOwner
= hMainWnd
;
236 ofn
.lpstrFilter
= file_filter
;
237 ofn
.lpstrFile
= file
;
238 ofn
.nMaxFile
= MAX_PATH
;
239 ofn
.lpstrDefExt
= defExt
;
241 if(GetSaveFileNameW(&ofn
))
247 static int get_num_pages(HWND hEditorWnd
, FORMATRANGE fr
)
254 int bottom
= fr
.rc
.bottom
;
256 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
,
258 fr
.rc
.bottom
= bottom
;
260 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
265 static void char_from_pagenum(HWND hEditorWnd
, FORMATRANGE
*fr
, int page
)
271 for(i
= 1; i
< page
; i
++)
273 int bottom
= fr
->rc
.bottom
;
274 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, (LPARAM
)fr
);
275 fr
->rc
.bottom
= bottom
;
279 static HWND
get_ruler_wnd(HWND hMainWnd
)
281 return GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
284 void redraw_ruler(HWND hRulerWnd
)
288 GetClientRect(hRulerWnd
, &rc
);
289 InvalidateRect(hRulerWnd
, &rc
, TRUE
);
292 static void update_ruler(HWND hRulerWnd
)
294 SendMessageW(hRulerWnd
, WM_USER
, 0, 0);
295 redraw_ruler(hRulerWnd
);
298 static void print(LPPRINTDLGW pd
, LPWSTR wszFileName
)
302 HWND hEditorWnd
= GetDlgItem(pd
->hwndOwner
, IDC_EDITOR
);
303 int printedPages
= 0;
306 fr
.hdcTarget
= pd
->hDC
;
308 fr
.rc
= get_print_rect(fr
.hdc
);
310 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
312 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
314 ZeroMemory(&di
, sizeof(di
));
315 di
.cbSize
= sizeof(di
);
316 di
.lpszDocName
= wszFileName
;
318 if(pd
->Flags
& PD_PRINTTOFILE
)
320 di
.lpszOutput
= dialog_print_to_file(pd
->hwndOwner
);
325 if(pd
->Flags
& PD_SELECTION
)
327 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
331 gt
.flags
= GTL_DEFAULT
;
334 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
336 if(pd
->Flags
& PD_PAGENUMS
)
337 char_from_pagenum(hEditorWnd
, &fr
, pd
->nToPage
);
340 StartDocW(fr
.hdc
, &di
);
343 int bottom
= fr
.rc
.bottom
;
344 if(StartPage(fr
.hdc
) <= 0)
347 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
349 if(EndPage(fr
.hdc
) <= 0)
351 bottom
= fr
.rc
.bottom
;
354 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
357 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
360 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
363 void dialog_printsetup(HWND hMainWnd
)
367 ZeroMemory(&ps
, sizeof(ps
));
368 ps
.lStructSize
= sizeof(ps
);
369 ps
.hwndOwner
= hMainWnd
;
370 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
371 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
372 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
373 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
374 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
375 ps
.hDevMode
= devMode
;
376 ps
.hDevNames
= devNames
;
378 if(PageSetupDlgW(&ps
))
380 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
381 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
382 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
383 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
384 devMode
= ps
.hDevMode
;
385 devNames
= ps
.hDevNames
;
386 update_ruler(get_ruler_wnd(hMainWnd
));
390 void get_default_printer_opts(void)
393 ZeroMemory(&pd
, sizeof(pd
));
395 ZeroMemory(&pd
, sizeof(pd
));
396 pd
.lStructSize
= sizeof(pd
);
397 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
398 pd
.hDevMode
= devMode
;
402 devMode
= pd
.hDevMode
;
403 devNames
= pd
.hDevNames
;
406 void print_quick(LPWSTR wszFileName
)
410 ZeroMemory(&pd
, sizeof(pd
));
413 print(&pd
, wszFileName
);
416 void dialog_print(HWND hMainWnd
, LPWSTR wszFileName
)
419 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
423 ZeroMemory(&pd
, sizeof(pd
));
424 pd
.lStructSize
= sizeof(pd
);
425 pd
.hwndOwner
= hMainWnd
;
426 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
429 pd
.hDevMode
= devMode
;
430 pd
.hDevNames
= devNames
;
432 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
434 pd
.Flags
|= PD_NOSELECTION
;
438 devMode
= pd
.hDevMode
;
439 devNames
= pd
.hDevNames
;
440 print(&pd
, wszFileName
);
441 update_ruler(get_ruler_wnd(hMainWnd
));
445 static void preview_bar_show(HWND hMainWnd
, BOOL show
)
447 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
455 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
456 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
457 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
458 AddTextButton(hReBar
, STRING_PREVIEW_TWOPAGES
, ID_PREVIEW_NUMPAGES
, BANDID_PREVIEW_BTN4
);
459 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMIN
, ID_PREVIEW_ZOOMIN
, BANDID_PREVIEW_BTN5
);
460 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMOUT
, ID_PREVIEW_ZOOMOUT
, BANDID_PREVIEW_BTN6
);
461 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN7
);
463 hStatic
= CreateWindowW(WC_STATICW
, NULL
,
464 WS_VISIBLE
| WS_CHILD
, 0, 0, 0, 0,
465 hReBar
, NULL
, NULL
, NULL
);
467 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
468 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
469 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
470 rb
.hwndChild
= hStatic
;
471 rb
.cyChild
= rb
.cyMinChild
= 22;
472 rb
.cx
= rb
.cxMinChild
= 90;
474 rb
.wID
= BANDID_PREVIEW_BUFFER
;
476 SendMessageW(hReBar
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
479 for(i
= 0; i
<= PREVIEW_BUTTONS
; i
++)
480 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
484 static const int min_spacing
= 10;
486 static void update_preview_scrollbars(HWND hwndPreview
, RECT
*window
)
489 sbi
.cbSize
= sizeof(sbi
);
490 sbi
.fMask
= SIF_PAGE
|SIF_RANGE
;
492 if (preview
.zoomlevel
== 0)
494 /* Hide scrollbars when zoomed out. */
496 sbi
.nPage
= window
->right
;
497 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
498 sbi
.nPage
= window
->bottom
;
499 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
502 sbi
.nMax
= preview
.bmScaledSize
.cx
+ min_spacing
* 2;
504 sbi
.nMax
= preview
.bmScaledSize
.cx
* 2 + min_spacing
* 3;
505 sbi
.nPage
= window
->right
;
506 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
507 /* Change in the horizontal scrollbar visibility affects the
508 * client rect, so update the client rect. */
509 GetClientRect(hwndPreview
, window
);
510 sbi
.nMax
= preview
.bmScaledSize
.cy
+ min_spacing
* 2;
511 sbi
.nPage
= window
->bottom
;
512 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
516 static void update_preview_sizes(HWND hwndPreview
, BOOL zoomLevelUpdated
)
520 GetClientRect(hwndPreview
, &window
);
522 /* The zoom ratio isn't updated for partial zoom because of resizing the window. */
523 if (zoomLevelUpdated
|| preview
.zoomlevel
!= 1)
525 float ratio
, ratioHeight
, ratioWidth
;
526 if (preview
.zoomlevel
== 2)
530 ratioHeight
= (window
.bottom
- min_spacing
* 2) / (float)preview
.bmSize
.cy
;
533 ratioWidth
= ((window
.right
- min_spacing
* 3) / 2.0) / (float)preview
.bmSize
.cx
;
535 ratioWidth
= (window
.right
- min_spacing
* 2) / (float)preview
.bmSize
.cx
;
537 if(ratioWidth
> ratioHeight
)
542 if (preview
.zoomlevel
== 1)
543 ratio
+= (1.0 - ratio
) / 2;
545 preview
.zoomratio
= ratio
;
548 preview
.bmScaledSize
.cx
= preview
.bmSize
.cx
* preview
.zoomratio
;
549 preview
.bmScaledSize
.cy
= preview
.bmSize
.cy
* preview
.zoomratio
;
551 preview
.spacing
.cy
= max(min_spacing
, (window
.bottom
- preview
.bmScaledSize
.cy
) / 2);
554 preview
.spacing
.cx
= (window
.right
- preview
.bmScaledSize
.cx
) / 2;
556 preview
.spacing
.cx
= (window
.right
- preview
.bmScaledSize
.cx
* 2) / 3;
557 if (preview
.spacing
.cx
< min_spacing
)
558 preview
.spacing
.cx
= min_spacing
;
560 update_preview_scrollbars(hwndPreview
, &window
);
563 /* Update for zoom ratio changes with same page. */
564 static void update_scaled_preview(HWND hMainWnd
)
566 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
567 preview
.window
.right
= 0;
568 InvalidateRect(hwndPreview
, NULL
, TRUE
);
571 LRESULT CALLBACK
preview_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
577 HWND hEditorWnd
= GetDlgItem(GetParent(hWnd
), IDC_EDITOR
);
579 GETTEXTLENGTHEX gt
= {GTL_DEFAULT
, 1200};
580 HDC hdc
= GetDC(hWnd
);
581 HDC hdcTarget
= make_dc();
583 fr
.rc
= preview
.rcPage
= get_print_rect(hdcTarget
);
584 preview
.rcPage
.bottom
+= margins
.bottom
;
585 preview
.rcPage
.right
+= margins
.right
;
586 preview
.rcPage
.top
= preview
.rcPage
.left
= 0;
587 fr
.rcPage
= preview
.rcPage
;
589 preview
.bmSize
.cx
= twips_to_pixels(preview
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
590 preview
.bmSize
.cy
= twips_to_pixels(preview
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
592 fr
.hdc
= CreateCompatibleDC(hdc
);
593 fr
.hdcTarget
= hdcTarget
;
595 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
596 preview
.pages
= get_num_pages(hEditorWnd
, fr
);
599 update_preview_sizes(hWnd
, TRUE
);
604 return print_preview(hWnd
);
608 update_preview_sizes(hWnd
, FALSE
);
609 update_scaled_preview(hWnd
);
618 int nBar
= (msg
== WM_VSCROLL
) ? SB_VERT
: SB_HORZ
;
621 GetClientRect(hWnd
, &rc
);
622 si
.cbSize
= sizeof(si
);
624 GetScrollInfo(hWnd
, nBar
, &si
);
626 switch(LOWORD(wParam
))
628 case SB_TOP
: /* == SB_LEFT */
631 case SB_BOTTOM
: /* == SB_RIGHT */
634 case SB_LINEUP
: /* == SB_LINELEFT */
635 si
.nPos
-= si
.nPage
/ 10;
637 case SB_LINEDOWN
: /* == SB_LINERIGHT */
638 si
.nPos
+= si
.nPage
/ 10;
640 case SB_PAGEUP
: /* == SB_PAGELEFT */
643 case SB_PAGEDOWN
: /* SB_PAGERIGHT */
647 si
.nPos
= si
.nTrackPos
;
651 SetScrollInfo(hWnd
, nBar
, &si
, TRUE
);
652 GetScrollInfo(hWnd
, nBar
, &si
);
653 if (si
.nPos
!= origPos
)
655 int amount
= origPos
- si
.nPos
;
656 if (msg
== WM_VSCROLL
)
657 ScrollWindow(hWnd
, 0, amount
, NULL
, NULL
);
659 ScrollWindow(hWnd
, amount
, 0, NULL
, NULL
);
665 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
671 void init_preview(HWND hMainWnd
, LPWSTR wszFileName
)
674 HINSTANCE hInstance
= GetModuleHandleW(0);
678 preview
.wszFileName
= wszFileName
;
679 preview
.zoomratio
= 0;
680 preview
.zoomlevel
= 0;
681 preview_bar_show(hMainWnd
, TRUE
);
683 hwndPreview
= CreateWindowExW(0, wszPreviewWndClass
, NULL
,
684 WS_VISIBLE
| WS_CHILD
| WS_VSCROLL
| WS_HSCROLL
,
685 0, 0, 200, 10, hMainWnd
, (HMENU
)IDC_PREVIEW
, hInstance
, NULL
);
688 void close_preview(HWND hMainWnd
)
690 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
691 preview
.window
.right
= 0;
692 preview
.window
.bottom
= 0;
696 preview_bar_show(hMainWnd
, FALSE
);
697 DestroyWindow(hwndPreview
);
700 BOOL
preview_isactive(void)
702 return preview
.page
!= 0;
705 static void add_ruler_units(HDC hdcRuler
, RECT
* drawRect
, BOOL NewMetrics
, LONG EditLeftmost
)
711 static HBITMAP hBitmap
;
712 int i
, x
, y
, RulerTextEnd
;
716 WCHAR FontName
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
721 DeleteObject(hBitmap
);
724 hdc
= CreateCompatibleDC(0);
726 CmPixels
= twips_to_pixels(centmm_to_twips(1000), GetDeviceCaps(hdc
, LOGPIXELSX
));
727 QuarterCmPixels
= (int)((float)CmPixels
/ 4.0);
729 hBitmap
= CreateCompatibleBitmap(hdc
, drawRect
->right
, drawRect
->bottom
);
730 SelectObject(hdc
, hBitmap
);
731 FillRect(hdc
, drawRect
, GetStockObject(WHITE_BRUSH
));
733 hFont
= CreateFontW(10, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName
);
735 SelectObject(hdc
, hFont
);
736 SetBkMode(hdc
, TRANSPARENT
);
737 SetTextAlign(hdc
, TA_CENTER
);
738 y
= (int)(((float)drawRect
->bottom
- (float)drawRect
->top
) / 2.0) + 1;
739 RulerTextEnd
= drawRect
->right
- EditLeftmost
+ 1;
740 for(i
= 1, x
= EditLeftmost
; x
< (drawRect
->right
- EditLeftmost
+ 1); i
++)
743 WCHAR format
[] = {'%','d',0};
746 x2
+= QuarterCmPixels
;
747 if(x2
> RulerTextEnd
)
750 MoveToEx(hdc
, x2
, y
, NULL
);
751 LineTo(hdc
, x2
, y
+2);
753 x2
+= QuarterCmPixels
;
754 if(x2
> RulerTextEnd
)
757 MoveToEx(hdc
, x2
, y
- 3, NULL
);
758 LineTo(hdc
, x2
, y
+ 3);
760 x2
+= QuarterCmPixels
;
761 if(x2
> RulerTextEnd
)
764 MoveToEx(hdc
, x2
, y
, NULL
);
765 LineTo(hdc
, x2
, y
+2);
771 wsprintfW(str
, format
, i
);
772 TextOutW(hdc
, x
, 5, str
, lstrlenW(str
));
777 BitBlt(hdcRuler
, 0, 0, drawRect
->right
, drawRect
->bottom
, hdc
, 0, 0, SRCAND
);
780 static void paint_ruler(HWND hWnd
, LONG EditLeftmost
, BOOL NewMetrics
)
783 HDC hdc
= BeginPaint(hWnd
, &ps
);
784 HDC hdcPrint
= make_dc();
785 RECT printRect
= get_print_rect(hdcPrint
);
787 HBRUSH hBrush
= CreateSolidBrush(GetSysColor(COLOR_MENU
));
789 GetClientRect(hWnd
, &drawRect
);
790 FillRect(hdc
, &drawRect
, hBrush
);
793 drawRect
.bottom
-= 3;
794 drawRect
.left
= EditLeftmost
;
795 drawRect
.right
= twips_to_pixels(printRect
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
796 FillRect(hdc
, &drawRect
, GetStockObject(WHITE_BRUSH
));
800 DrawEdge(hdc
, &drawRect
, EDGE_SUNKEN
, BF_RECT
);
802 drawRect
.left
= drawRect
.right
- 1;
803 drawRect
.right
= twips_to_pixels(printRect
.right
+ margins
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
804 DrawEdge(hdc
, &drawRect
, EDGE_ETCHED
, BF_RECT
);
808 add_ruler_units(hdc
, &drawRect
, NewMetrics
, EditLeftmost
);
810 SelectObject(hdc
, GetStockObject(BLACK_BRUSH
));
811 DeleteObject(hBrush
);
816 LRESULT CALLBACK
ruler_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
818 static WNDPROC pPrevRulerProc
;
819 static LONG EditLeftmost
;
820 static BOOL NewMetrics
;
827 EditLeftmost
= ((POINTL
*)wParam
)->x
;
828 pPrevRulerProc
= (WNDPROC
)lParam
;
834 paint_ruler(hWnd
, EditLeftmost
, NewMetrics
);
838 return CallWindowProcW(pPrevRulerProc
, hWnd
, msg
, wParam
, lParam
);
844 static void draw_preview_page(HDC hdc
, HDC
* hdcSized
, FORMATRANGE
* lpFr
, float ratio
, int bmNewWidth
, int bmNewHeight
, int bmWidth
, int bmHeight
)
846 HBITMAP hBitmapScaled
= CreateCompatibleBitmap(hdc
, bmNewWidth
, bmNewHeight
);
848 int TopMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.top
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
849 int BottomMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
850 int LeftMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.left
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
851 int RightMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.right
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
855 *hdcSized
= CreateCompatibleDC(hdc
);
856 SelectObject(*hdcSized
, hBitmapScaled
);
858 StretchBlt(*hdcSized
, 0, 0, bmNewWidth
, bmNewHeight
, hdc
, 0, 0, bmWidth
, bmHeight
, SRCCOPY
);
860 /* Draw margin lines */
861 hPen
= CreatePen(PS_DOT
, 1, RGB(0,0,0));
862 SelectObject(*hdcSized
, hPen
);
864 MoveToEx(*hdcSized
, 0, TopMargin
, NULL
);
865 LineTo(*hdcSized
, bmNewWidth
, TopMargin
);
866 MoveToEx(*hdcSized
, 0, BottomMargin
, NULL
);
867 LineTo(*hdcSized
, bmNewWidth
, BottomMargin
);
869 MoveToEx(*hdcSized
, LeftMargin
, 0, NULL
);
870 LineTo(*hdcSized
, LeftMargin
, bmNewHeight
);
871 MoveToEx(*hdcSized
, RightMargin
, 0, NULL
);
872 LineTo(*hdcSized
, RightMargin
, bmNewHeight
);
876 static void draw_preview(HWND hEditorWnd
, FORMATRANGE
* lpFr
, int bmWidth
, int bmHeight
, RECT
* paper
, int page
)
878 HBITMAP hBitmapCapture
= CreateCompatibleBitmap(lpFr
->hdc
, bmWidth
, bmHeight
);
881 char_from_pagenum(hEditorWnd
, lpFr
, page
);
882 SelectObject(lpFr
->hdc
, hBitmapCapture
);
883 FillRect(lpFr
->hdc
, paper
, GetStockObject(WHITE_BRUSH
));
884 bottom
= lpFr
->rc
.bottom
;
885 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)lpFr
);
886 /* EM_FORMATRANGE sets fr.rc.bottom to indicate the area printed in,
887 * but we want to keep the original for drawing margins */
888 lpFr
->rc
.bottom
= bottom
;
889 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
892 static void update_preview_buttons(HWND hMainWnd
)
894 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
895 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
896 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
), preview
.hdc2
?
897 (preview
.page
+ 1) < preview
.pages
:
898 preview
.page
< preview
.pages
);
899 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), preview
.pages
> 1 && preview
.zoomlevel
== 0);
900 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMIN
), preview
.zoomlevel
< 2);
901 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMOUT
), preview
.zoomlevel
> 0);
904 LRESULT
print_preview(HWND hwndPreview
)
908 RECT window
, background
;
910 HWND hMainWnd
= GetParent(hwndPreview
);
913 hdc
= BeginPaint(hwndPreview
, &ps
);
914 GetClientRect(hwndPreview
, &window
);
916 fr
.hdcTarget
= make_dc();
917 fr
.rc
= fr
.rcPage
= preview
.rcPage
;
918 fr
.rc
.left
+= margins
.left
;
919 fr
.rc
.top
+= margins
.top
;
920 fr
.rc
.bottom
-= margins
.bottom
;
921 fr
.rc
.right
-= margins
.right
;
927 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
929 preview
.hdc
= CreateCompatibleDC(hdc
);
933 if(preview
.hdc2
!= (HDC
)-1)
934 DeleteDC(preview
.hdc2
);
935 preview
.hdc2
= CreateCompatibleDC(hdc
);
938 gt
.flags
= GTL_DEFAULT
;
941 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
944 paper
.right
= preview
.bmSize
.cx
;
946 paper
.bottom
= preview
.bmSize
.cy
;
948 fr
.hdc
= preview
.hdc
;
949 draw_preview(hEditorWnd
, &fr
, preview
.bmSize
.cx
, preview
.bmSize
.cy
, &paper
, preview
.page
);
953 fr
.hdc
= preview
.hdc2
;
954 draw_preview(hEditorWnd
, &fr
, preview
.bmSize
.cx
, preview
.bmSize
.cy
, &fr
.rcPage
, preview
.page
+ 1);
957 update_preview_buttons(hMainWnd
);
960 FillRect(hdc
, &window
, GetStockObject(GRAY_BRUSH
));
962 scrollpos
.x
= GetScrollPos(hwndPreview
, SB_HORZ
);
963 scrollpos
.y
= GetScrollPos(hwndPreview
, SB_VERT
);
965 background
.left
= preview
.spacing
.cx
- 2 - scrollpos
.x
;
966 background
.right
= background
.left
+ preview
.bmScaledSize
.cx
+ 4;
967 background
.top
= preview
.spacing
.cy
- 2 - scrollpos
.y
;
968 background
.bottom
= background
.top
+ preview
.bmScaledSize
.cy
+ 4;
970 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
974 background
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
975 background
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
977 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
980 if(window
.right
!= preview
.window
.right
|| window
.bottom
!= preview
.window
.bottom
)
982 draw_preview_page(preview
.hdc
, &preview
.hdcSized
, &fr
, preview
.zoomratio
,
983 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
984 preview
.bmSize
.cx
, preview
.bmSize
.cy
);
988 draw_preview_page(preview
.hdc2
, &preview
.hdcSized2
, &fr
, preview
.zoomratio
,
989 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
990 preview
.bmSize
.cx
, preview
.bmSize
.cy
);
994 BitBlt(hdc
, preview
.spacing
.cx
- scrollpos
.x
, preview
.spacing
.cy
- scrollpos
.y
,
995 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
996 preview
.hdcSized
, 0, 0, SRCCOPY
);
1000 BitBlt(hdc
, preview
.spacing
.cx
* 2 + preview
.bmScaledSize
.cx
- scrollpos
.x
,
1001 preview
.spacing
.cy
- scrollpos
.y
, preview
.bmScaledSize
.cx
,
1002 preview
.bmScaledSize
.cy
, preview
.hdcSized2
, 0, 0, SRCCOPY
);
1005 DeleteDC(fr
.hdcTarget
);
1006 preview
.window
= window
;
1008 EndPaint(hwndPreview
, &ps
);
1013 /* Update for page changes. */
1014 static void update_preview(HWND hMainWnd
)
1016 DeleteDC(preview
.hdc
);
1019 update_scaled_preview(hMainWnd
);
1022 static void toggle_num_pages(HWND hMainWnd
)
1024 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1025 WCHAR name
[MAX_STRING_LEN
];
1026 HINSTANCE hInst
= GetModuleHandleW(0);
1030 DeleteDC(preview
.hdc2
);
1034 if(preview
.page
== preview
.pages
)
1036 preview
.hdc2
= (HDC
)-1;
1039 LoadStringW(hInst
, preview
.hdc2
? STRING_PREVIEW_ONEPAGE
: STRING_PREVIEW_TWOPAGES
,
1040 name
, MAX_STRING_LEN
);
1042 SetWindowTextW(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), name
);
1043 update_preview_sizes(GetDlgItem(hMainWnd
, IDC_PREVIEW
), TRUE
);
1044 update_preview(hMainWnd
);
1047 LRESULT
preview_command(HWND hWnd
, WPARAM wParam
)
1049 switch(LOWORD(wParam
))
1052 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
1055 case ID_PREVIEW_NEXTPAGE
:
1056 case ID_PREVIEW_PREVPAGE
:
1058 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1063 update_preview(hWnd
);
1067 case ID_PREVIEW_NUMPAGES
:
1068 toggle_num_pages(hWnd
);
1071 case ID_PREVIEW_ZOOMIN
:
1072 if (preview
.zoomlevel
< 2)
1074 preview
.zoomlevel
++;
1075 preview
.zoomratio
= 0;
1078 /* Forced switch to one page when zooming in. */
1079 toggle_num_pages(hWnd
);
1081 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1082 update_preview_sizes(hwndPreview
, TRUE
);
1083 update_scaled_preview(hWnd
);
1084 update_preview_buttons(hWnd
);
1089 case ID_PREVIEW_ZOOMOUT
:
1090 if (preview
.zoomlevel
> 0)
1092 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1093 preview
.zoomlevel
--;
1094 preview
.zoomratio
= 0;
1095 update_preview_sizes(hwndPreview
, TRUE
);
1096 update_scaled_preview(hWnd
);
1097 update_preview_buttons(hWnd
);
1102 dialog_print(hWnd
, preview
.wszFileName
);
1103 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);