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
28 typedef struct _previewinfo
32 int saved_pages_shown
;
33 int *pageEnds
, pageCapacity
;
45 } previewinfo
, *ppreviewinfo
;
47 static HGLOBAL devMode
;
48 static HGLOBAL devNames
;
51 static previewinfo preview
;
53 extern const WCHAR wszPreviewWndClass
[];
55 static const WCHAR var_pagemargin
[] = {'P','a','g','e','M','a','r','g','i','n',0};
56 static const WCHAR var_previewpages
[] = {'P','r','e','v','i','e','w','P','a','g','e','s',0};
58 static LPWSTR
get_print_file_filter(HWND hMainWnd
)
60 static WCHAR wszPrintFilter
[MAX_STRING_LEN
*2+6+4+1];
61 const WCHAR files_prn
[] = {'*','.','P','R','N',0};
62 const WCHAR files_all
[] = {'*','.','*','\0'};
64 HINSTANCE hInstance
= GetModuleHandleW(0);
67 LoadStringW(hInstance
, STRING_PRINTER_FILES_PRN
, p
, MAX_STRING_LEN
);
69 lstrcpyW(p
, files_prn
);
71 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
73 lstrcpyW(p
, files_all
);
77 return wszPrintFilter
;
80 void registry_set_pagemargins(HKEY hKey
)
82 RegSetValueExW(hKey
, var_pagemargin
, 0, REG_BINARY
, (LPBYTE
)&margins
, sizeof(RECT
));
85 void registry_read_pagemargins(HKEY hKey
)
87 DWORD size
= sizeof(RECT
);
89 if(!hKey
|| RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
90 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
93 margins
.bottom
= 1417;
99 void registry_set_previewpages(HKEY hKey
)
101 RegSetValueExW(hKey
, var_previewpages
, 0, REG_DWORD
,
102 (LPBYTE
)&preview
.pages_shown
, sizeof(DWORD
));
105 void registry_read_previewpages(HKEY hKey
)
107 DWORD size
= sizeof(DWORD
);
109 RegQueryValueExW(hKey
, var_previewpages
, 0, NULL
,
110 (LPBYTE
)&preview
.pages_shown
, &size
) != ERROR_SUCCESS
||
111 size
!= sizeof(DWORD
))
113 preview
.pages_shown
= 1;
115 if (preview
.pages_shown
< 1) preview
.pages_shown
= 1;
116 else if (preview
.pages_shown
> 2) preview
.pages_shown
= 2;
121 static void AddTextButton(HWND hRebarWnd
, UINT string
, UINT command
, UINT id
)
124 HINSTANCE hInstance
= GetModuleHandleW(0);
125 WCHAR text
[MAX_STRING_LEN
];
128 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
129 hButton
= CreateWindowW(WC_BUTTONW
, text
,
130 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
131 hRebarWnd
, ULongToHandle(command
), hInstance
, NULL
);
133 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
134 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
135 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
136 rb
.hwndChild
= hButton
;
137 rb
.cyChild
= rb
.cyMinChild
= 22;
138 rb
.cx
= rb
.cxMinChild
= 90;
142 SendMessageW(hRebarWnd
, RB_INSERTBANDW
, -1, (LPARAM
)&rb
);
145 static HDC
make_dc(void)
147 if(devNames
&& devMode
)
149 LPDEVNAMES dn
= GlobalLock(devNames
);
150 LPDEVMODEW dm
= GlobalLock(devMode
);
153 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
154 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
166 static LONG
twips_to_centmm(int twips
)
168 return MulDiv(twips
, CENTMM_PER_INCH
, TWIPS_PER_INCH
);
171 static LONG
centmm_to_twips(int mm
)
173 return MulDiv(mm
, TWIPS_PER_INCH
, CENTMM_PER_INCH
);
176 static LONG
twips_to_pixels(int twips
, int dpi
)
178 return MulDiv(twips
, dpi
, TWIPS_PER_INCH
);
181 static LONG
devunits_to_twips(int units
, int dpi
)
183 return MulDiv(units
, TWIPS_PER_INCH
, dpi
);
187 static RECT
get_print_rect(HDC hdc
)
194 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
195 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
196 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
197 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
200 width
= centmm_to_twips(18500);
201 height
= centmm_to_twips(27000);
204 rc
.left
= margins
.left
;
205 rc
.right
= width
- margins
.right
;
206 rc
.top
= margins
.top
;
207 rc
.bottom
= height
- margins
.bottom
;
212 void target_device(HWND hMainWnd
, DWORD wordWrap
)
214 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
216 if(wordWrap
== ID_WORDWRAP_MARGIN
)
221 RECT rc
= get_print_rect(hdc
);
223 width
= rc
.right
- rc
.left
;
226 HDC hMaindc
= GetDC(hMainWnd
);
227 hdc
= CreateCompatibleDC(hMaindc
);
228 ReleaseDC(hMainWnd
, hMaindc
);
230 result
= SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
234 /* otherwise EM_SETTARGETDEVICE failed, so fall back on wrapping
235 * to window using the NULL DC. */
238 if (wordWrap
!= ID_WORDWRAP_NONE
) {
239 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 0);
241 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 1);
246 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
249 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
250 static const WCHAR defExt
[] = {'P','R','N',0};
251 static LPWSTR file_filter
;
254 file_filter
= get_print_file_filter(hMainWnd
);
256 ZeroMemory(&ofn
, sizeof(ofn
));
258 ofn
.lStructSize
= sizeof(ofn
);
259 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
260 ofn
.hwndOwner
= hMainWnd
;
261 ofn
.lpstrFilter
= file_filter
;
262 ofn
.lpstrFile
= file
;
263 ofn
.nMaxFile
= MAX_PATH
;
264 ofn
.lpstrDefExt
= defExt
;
266 if(GetSaveFileNameW(&ofn
))
272 static void char_from_pagenum(HWND hEditorWnd
, FORMATRANGE
*fr
, int page
)
278 for(i
= 1; i
< page
; i
++)
280 int bottom
= fr
->rc
.bottom
;
281 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, (LPARAM
)fr
);
282 fr
->rc
.bottom
= bottom
;
286 static HWND
get_ruler_wnd(HWND hMainWnd
)
288 return GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
291 void redraw_ruler(HWND hRulerWnd
)
295 GetClientRect(hRulerWnd
, &rc
);
296 InvalidateRect(hRulerWnd
, &rc
, TRUE
);
299 static void update_ruler(HWND hRulerWnd
)
301 SendMessageW(hRulerWnd
, WM_USER
, 0, 0);
302 redraw_ruler(hRulerWnd
);
305 static void add_ruler_units(HDC hdcRuler
, RECT
* drawRect
, BOOL NewMetrics
, LONG EditLeftmost
)
311 static HBITMAP hBitmap
;
312 int i
, x
, y
, RulerTextEnd
;
316 WCHAR FontName
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
321 DeleteObject(hBitmap
);
324 hdc
= CreateCompatibleDC(0);
326 CmPixels
= twips_to_pixels(centmm_to_twips(1000), GetDeviceCaps(hdc
, LOGPIXELSX
));
327 QuarterCmPixels
= (int)((float)CmPixels
/ 4.0);
329 hBitmap
= CreateCompatibleBitmap(hdc
, drawRect
->right
, drawRect
->bottom
);
330 SelectObject(hdc
, hBitmap
);
331 FillRect(hdc
, drawRect
, GetStockObject(WHITE_BRUSH
));
333 hFont
= CreateFontW(10, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName
);
335 SelectObject(hdc
, hFont
);
336 SetBkMode(hdc
, TRANSPARENT
);
337 SetTextAlign(hdc
, TA_CENTER
);
338 y
= (int)(((float)drawRect
->bottom
- (float)drawRect
->top
) / 2.0) + 1;
339 RulerTextEnd
= drawRect
->right
- EditLeftmost
+ 1;
340 for(i
= 1, x
= EditLeftmost
; x
< (drawRect
->right
- EditLeftmost
+ 1); i
++)
343 WCHAR format
[] = {'%','d',0};
346 x2
+= QuarterCmPixels
;
347 if(x2
> RulerTextEnd
)
350 MoveToEx(hdc
, x2
, y
, NULL
);
351 LineTo(hdc
, x2
, y
+2);
353 x2
+= QuarterCmPixels
;
354 if(x2
> RulerTextEnd
)
357 MoveToEx(hdc
, x2
, y
- 3, NULL
);
358 LineTo(hdc
, x2
, y
+ 3);
360 x2
+= QuarterCmPixels
;
361 if(x2
> RulerTextEnd
)
364 MoveToEx(hdc
, x2
, y
, NULL
);
365 LineTo(hdc
, x2
, y
+2);
371 wsprintfW(str
, format
, i
);
372 TextOutW(hdc
, x
, 5, str
, lstrlenW(str
));
377 BitBlt(hdcRuler
, 0, 0, drawRect
->right
, drawRect
->bottom
, hdc
, 0, 0, SRCAND
);
380 static void paint_ruler(HWND hWnd
, LONG EditLeftmost
, BOOL NewMetrics
)
383 HDC hdc
= BeginPaint(hWnd
, &ps
);
384 HDC hdcPrint
= make_dc();
385 RECT printRect
= get_print_rect(hdcPrint
);
387 HBRUSH hBrush
= CreateSolidBrush(GetSysColor(COLOR_MENU
));
389 GetClientRect(hWnd
, &drawRect
);
390 FillRect(hdc
, &drawRect
, hBrush
);
393 drawRect
.bottom
-= 3;
394 drawRect
.left
= EditLeftmost
;
395 drawRect
.right
= twips_to_pixels(printRect
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
396 FillRect(hdc
, &drawRect
, GetStockObject(WHITE_BRUSH
));
400 DrawEdge(hdc
, &drawRect
, EDGE_SUNKEN
, BF_RECT
);
402 drawRect
.left
= drawRect
.right
- 1;
403 drawRect
.right
= twips_to_pixels(printRect
.right
+ margins
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
404 DrawEdge(hdc
, &drawRect
, EDGE_ETCHED
, BF_RECT
);
408 add_ruler_units(hdc
, &drawRect
, NewMetrics
, EditLeftmost
);
410 SelectObject(hdc
, GetStockObject(BLACK_BRUSH
));
411 DeleteObject(hBrush
);
416 LRESULT CALLBACK
ruler_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
418 static WNDPROC pPrevRulerProc
;
419 static LONG EditLeftmost
;
420 static BOOL NewMetrics
;
427 EditLeftmost
= ((POINTL
*)wParam
)->x
;
428 pPrevRulerProc
= (WNDPROC
)lParam
;
434 paint_ruler(hWnd
, EditLeftmost
, NewMetrics
);
438 return CallWindowProcW(pPrevRulerProc
, hWnd
, msg
, wParam
, lParam
);
444 static void print(LPPRINTDLGW pd
, LPWSTR wszFileName
)
448 HWND hEditorWnd
= GetDlgItem(pd
->hwndOwner
, IDC_EDITOR
);
449 int printedPages
= 0;
452 fr
.hdcTarget
= pd
->hDC
;
454 fr
.rc
= get_print_rect(fr
.hdc
);
456 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
458 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
460 ZeroMemory(&di
, sizeof(di
));
461 di
.cbSize
= sizeof(di
);
462 di
.lpszDocName
= wszFileName
;
464 if(pd
->Flags
& PD_PRINTTOFILE
)
466 di
.lpszOutput
= dialog_print_to_file(pd
->hwndOwner
);
471 if(pd
->Flags
& PD_SELECTION
)
473 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
477 gt
.flags
= GTL_DEFAULT
;
480 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
482 if(pd
->Flags
& PD_PAGENUMS
)
483 char_from_pagenum(hEditorWnd
, &fr
, pd
->nToPage
);
486 StartDocW(fr
.hdc
, &di
);
489 if(StartPage(fr
.hdc
) <= 0)
492 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
494 if(EndPage(fr
.hdc
) <= 0)
498 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
501 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
504 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
507 void dialog_printsetup(HWND hMainWnd
)
511 ZeroMemory(&ps
, sizeof(ps
));
512 ps
.lStructSize
= sizeof(ps
);
513 ps
.hwndOwner
= hMainWnd
;
514 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
515 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
516 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
517 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
518 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
519 ps
.hDevMode
= devMode
;
520 ps
.hDevNames
= devNames
;
522 if(PageSetupDlgW(&ps
))
524 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
525 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
526 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
527 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
528 devMode
= ps
.hDevMode
;
529 devNames
= ps
.hDevNames
;
530 update_ruler(get_ruler_wnd(hMainWnd
));
534 void get_default_printer_opts(void)
537 ZeroMemory(&pd
, sizeof(pd
));
539 ZeroMemory(&pd
, sizeof(pd
));
540 pd
.lStructSize
= sizeof(pd
);
541 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
542 pd
.hDevMode
= devMode
;
546 devMode
= pd
.hDevMode
;
547 devNames
= pd
.hDevNames
;
550 void print_quick(HWND hMainWnd
, LPWSTR wszFileName
)
554 ZeroMemory(&pd
, sizeof(pd
));
555 pd
.hwndOwner
= hMainWnd
;
558 print(&pd
, wszFileName
);
562 void dialog_print(HWND hMainWnd
, LPWSTR wszFileName
)
565 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
569 ZeroMemory(&pd
, sizeof(pd
));
570 pd
.lStructSize
= sizeof(pd
);
571 pd
.hwndOwner
= hMainWnd
;
572 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
575 pd
.hDevMode
= devMode
;
576 pd
.hDevNames
= devNames
;
578 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
580 pd
.Flags
|= PD_NOSELECTION
;
584 devMode
= pd
.hDevMode
;
585 devNames
= pd
.hDevNames
;
586 print(&pd
, wszFileName
);
587 update_ruler(get_ruler_wnd(hMainWnd
));
591 static void preview_bar_show(HWND hMainWnd
, BOOL show
)
593 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
600 UINT num_pages_string
= preview
.pages_shown
> 1 ? STRING_PREVIEW_ONEPAGE
:
601 STRING_PREVIEW_TWOPAGES
;
603 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
604 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
605 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
606 AddTextButton(hReBar
, num_pages_string
, ID_PREVIEW_NUMPAGES
, BANDID_PREVIEW_BTN4
);
607 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMIN
, ID_PREVIEW_ZOOMIN
, BANDID_PREVIEW_BTN5
);
608 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMOUT
, ID_PREVIEW_ZOOMOUT
, BANDID_PREVIEW_BTN6
);
609 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN7
);
611 hStatic
= CreateWindowW(WC_STATICW
, NULL
,
612 WS_VISIBLE
| WS_CHILD
, 0, 0, 0, 0,
613 hReBar
, NULL
, NULL
, NULL
);
615 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
616 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
617 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
618 rb
.hwndChild
= hStatic
;
619 rb
.cyChild
= rb
.cyMinChild
= 22;
620 rb
.cx
= rb
.cxMinChild
= 90;
622 rb
.wID
= BANDID_PREVIEW_BUFFER
;
624 SendMessageW(hReBar
, RB_INSERTBANDW
, -1, (LPARAM
)&rb
);
627 for(i
= 0; i
<= PREVIEW_BUTTONS
; i
++)
628 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
632 static const int min_spacing
= 10;
634 static void update_preview_scrollbars(HWND hwndPreview
, RECT
*window
)
637 sbi
.cbSize
= sizeof(sbi
);
638 sbi
.fMask
= SIF_PAGE
|SIF_RANGE
;
640 if (preview
.zoomlevel
== 0)
642 /* Hide scrollbars when zoomed out. */
644 sbi
.nPage
= window
->right
;
645 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
646 sbi
.nPage
= window
->bottom
;
647 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
649 sbi
.nMax
= preview
.bmScaledSize
.cx
* preview
.pages_shown
+
650 min_spacing
* (preview
.pages_shown
+ 1);
651 sbi
.nPage
= window
->right
;
652 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
653 /* Change in the horizontal scrollbar visibility affects the
654 * client rect, so update the client rect. */
655 GetClientRect(hwndPreview
, window
);
656 sbi
.nMax
= preview
.bmScaledSize
.cy
+ min_spacing
* 2;
657 sbi
.nPage
= window
->bottom
;
658 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
662 static void update_preview_sizes(HWND hwndPreview
, BOOL zoomLevelUpdated
)
666 GetClientRect(hwndPreview
, &window
);
668 /* The zoom ratio isn't updated for partial zoom because of resizing the window. */
669 if (zoomLevelUpdated
|| preview
.zoomlevel
!= 1)
671 float ratio
, ratioHeight
, ratioWidth
;
672 if (preview
.zoomlevel
== 2)
676 ratioHeight
= (window
.bottom
- min_spacing
* 2) / (float)preview
.bmSize
.cy
;
678 ratioWidth
= (float)(window
.right
-
679 min_spacing
* (preview
.pages_shown
+ 1)) /
680 (preview
.pages_shown
* preview
.bmSize
.cx
);
682 if(ratioWidth
> ratioHeight
)
687 if (preview
.zoomlevel
== 1)
688 ratio
+= (1.0 - ratio
) / 2;
690 preview
.zoomratio
= ratio
;
693 preview
.bmScaledSize
.cx
= preview
.bmSize
.cx
* preview
.zoomratio
;
694 preview
.bmScaledSize
.cy
= preview
.bmSize
.cy
* preview
.zoomratio
;
696 preview
.spacing
.cy
= max(min_spacing
, (window
.bottom
- preview
.bmScaledSize
.cy
) / 2);
698 preview
.spacing
.cx
= (window
.right
-
699 preview
.bmScaledSize
.cx
* preview
.pages_shown
) /
700 (preview
.pages_shown
+ 1);
701 if (preview
.spacing
.cx
< min_spacing
)
702 preview
.spacing
.cx
= min_spacing
;
704 update_preview_scrollbars(hwndPreview
, &window
);
707 static void draw_margin_lines(HDC hdc
, int x
, int y
, float ratio
)
711 RECT page_margin
= preview
.rcPage
;
713 dpi
.cx
= GetDeviceCaps(hdc
, LOGPIXELSX
);
714 dpi
.cy
= GetDeviceCaps(hdc
, LOGPIXELSY
);
716 page_margin
.left
= preview
.rcPage
.left
+ margins
.left
;
717 page_margin
.top
= preview
.rcPage
.top
+ margins
.top
;
718 page_margin
.bottom
= preview
.rcPage
.bottom
- margins
.bottom
;
719 page_margin
.right
= preview
.rcPage
.right
- margins
.right
;
721 page_margin
.left
= (int)((float)twips_to_pixels(page_margin
.left
, dpi
.cx
) * ratio
);
722 page_margin
.top
= (int)((float)twips_to_pixels(page_margin
.top
, dpi
.cy
) * ratio
);
723 page_margin
.bottom
= (int)((float)twips_to_pixels(page_margin
.bottom
, dpi
.cy
) * ratio
);
724 page_margin
.right
= (int)((float)twips_to_pixels(page_margin
.right
, dpi
.cx
) * ratio
);
726 OffsetRect(&page_margin
, x
, y
);
728 hPen
= CreatePen(PS_DOT
, 1, RGB(0,0,0));
729 oldPen
= SelectObject(hdc
, hPen
);
731 MoveToEx(hdc
, x
, page_margin
.top
, NULL
);
732 LineTo(hdc
, x
+ preview
.bmScaledSize
.cx
, page_margin
.top
);
733 MoveToEx(hdc
, x
, page_margin
.bottom
, NULL
);
734 LineTo(hdc
, x
+ preview
.bmScaledSize
.cx
, page_margin
.bottom
);
736 MoveToEx(hdc
, page_margin
.left
, y
, NULL
);
737 LineTo(hdc
, page_margin
.left
, y
+ preview
.bmScaledSize
.cy
);
738 MoveToEx(hdc
, page_margin
.right
, y
, NULL
);
739 LineTo(hdc
, page_margin
.right
, y
+ preview
.bmScaledSize
.cy
);
741 SelectObject(hdc
, oldPen
);
745 static BOOL
is_last_preview_page(int page
)
747 return preview
.pageEnds
[page
- 1] >= preview
.textlength
;
750 void init_preview(HWND hMainWnd
, LPWSTR wszFileName
)
752 HINSTANCE hInstance
= GetModuleHandleW(0);
756 preview
.wszFileName
= wszFileName
;
757 preview
.zoomratio
= 0;
758 preview
.zoomlevel
= 0;
759 preview_bar_show(hMainWnd
, TRUE
);
761 CreateWindowExW(0, wszPreviewWndClass
, NULL
,
762 WS_VISIBLE
| WS_CHILD
| WS_VSCROLL
| WS_HSCROLL
,
763 0, 0, 200, 10, hMainWnd
, (HMENU
)IDC_PREVIEW
, hInstance
, NULL
);
766 void close_preview(HWND hMainWnd
)
768 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
769 preview
.window
.right
= 0;
770 preview
.window
.bottom
= 0;
772 HeapFree(GetProcessHeap(), 0, preview
.pageEnds
);
773 preview
.pageEnds
= NULL
;
774 preview
.pageCapacity
= 0;
775 if (preview
.zoomlevel
> 0)
776 preview
.pages_shown
= preview
.saved_pages_shown
;
778 HBITMAP oldbm
= GetCurrentObject(preview
.hdc
, OBJ_BITMAP
);
779 DeleteDC(preview
.hdc
);
784 HBITMAP oldbm
= GetCurrentObject(preview
.hdc2
, OBJ_BITMAP
);
785 DeleteDC(preview
.hdc2
);
790 preview_bar_show(hMainWnd
, FALSE
);
791 DestroyWindow(hwndPreview
);
794 BOOL
preview_isactive(void)
796 return preview
.page
!= 0;
799 static void draw_preview(HWND hEditorWnd
, FORMATRANGE
* lpFr
, RECT
* paper
, int page
)
803 if (!preview
.pageEnds
)
805 preview
.pageCapacity
= 32;
806 preview
.pageEnds
= HeapAlloc(GetProcessHeap(), 0,
807 sizeof(int) * preview
.pageCapacity
);
808 if (!preview
.pageEnds
) return;
809 } else if (page
>= preview
.pageCapacity
) {
811 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, preview
.pageEnds
,
812 sizeof(int) * preview
.pageCapacity
* 2);
813 if (!new_buffer
) return;
814 preview
.pageCapacity
*= 2;
815 preview
.pageEnds
= new_buffer
;
818 FillRect(lpFr
->hdc
, paper
, GetStockObject(WHITE_BRUSH
));
819 if (page
> 1 && is_last_preview_page(page
- 1)) return;
820 lpFr
->chrg
.cpMin
= page
<= 1 ? 0 : preview
.pageEnds
[page
-2];
821 bottom
= lpFr
->rc
.bottom
;
822 preview
.pageEnds
[page
-1] = SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)lpFr
);
824 /* EM_FORMATRANGE sets fr.rc.bottom to indicate the area printed in,
825 * but we want to keep the original for drawing margins */
826 lpFr
->rc
.bottom
= bottom
;
827 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
830 static void update_preview_buttons(HWND hMainWnd
)
832 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
833 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
834 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
),
835 !is_last_preview_page(preview
.page
) &&
836 !is_last_preview_page(preview
.page
+ preview
.pages_shown
- 1));
837 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
),
838 preview
.pages_shown
> 1 ||
839 (!is_last_preview_page(1) && preview
.zoomlevel
== 0));
840 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMIN
), preview
.zoomlevel
< 2);
841 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMOUT
), preview
.zoomlevel
> 0);
844 static LRESULT
print_preview(HWND hwndPreview
)
848 HRGN back_rgn
, excl_rgn
;
849 RECT window
, background
;
853 hdc
= BeginPaint(hwndPreview
, &ps
);
854 GetClientRect(hwndPreview
, &window
);
855 back_rgn
= CreateRectRgnIndirect(&window
);
857 x
= preview
.spacing
.cx
- GetScrollPos(hwndPreview
, SB_HORZ
);
858 y
= preview
.spacing
.cy
- GetScrollPos(hwndPreview
, SB_VERT
);
860 /* draw page outlines */
861 hPen
= CreatePen(PS_SOLID
|PS_INSIDEFRAME
, 2, RGB(0,0,0));
862 oldPen
= SelectObject(hdc
, hPen
);
863 background
.left
= x
- 2;
864 background
.right
= x
+ preview
.bmScaledSize
.cx
+ 2;
865 background
.top
= y
- 2;
866 background
.bottom
= y
+ preview
.bmScaledSize
.cy
+ 2;
867 Rectangle(hdc
, background
.left
, background
.top
,
868 background
.right
, background
.bottom
);
869 excl_rgn
= CreateRectRgnIndirect(&background
);
870 CombineRgn(back_rgn
, back_rgn
, excl_rgn
, RGN_DIFF
);
871 if(preview
.pages_shown
> 1)
873 background
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
874 background
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
875 Rectangle(hdc
, background
.left
, background
.top
,
876 background
.right
, background
.bottom
);
877 SetRectRgn(excl_rgn
, background
.left
, background
.top
,
878 background
.right
, background
.bottom
);
879 CombineRgn(back_rgn
, back_rgn
, excl_rgn
, RGN_DIFF
);
881 SelectObject(hdc
, oldPen
);
883 FillRgn(hdc
, back_rgn
, GetStockObject(GRAY_BRUSH
));
884 DeleteObject(excl_rgn
);
885 DeleteObject(back_rgn
);
887 StretchBlt(hdc
, x
, y
, preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
888 preview
.hdc
, 0, 0, preview
.bmSize
.cx
, preview
.bmSize
.cy
, SRCCOPY
);
890 draw_margin_lines(hdc
, x
, y
, preview
.zoomratio
);
892 if(preview
.pages_shown
> 1)
894 if (!is_last_preview_page(preview
.page
)) {
895 x
+= preview
.spacing
.cx
+ preview
.bmScaledSize
.cx
;
896 StretchBlt(hdc
, x
, y
,
897 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
899 preview
.bmSize
.cx
, preview
.bmSize
.cy
, SRCCOPY
);
901 draw_margin_lines(hdc
, x
, y
, preview
.zoomratio
);
903 background
.left
+= 2;
904 background
.right
-= 2;
906 background
.bottom
-= 2;
907 FillRect(hdc
, &background
, GetStockObject(WHITE_BRUSH
));
911 preview
.window
= window
;
913 EndPaint(hwndPreview
, &ps
);
918 static void update_preview_statusbar(HWND hMainWnd
)
920 HWND hStatusbar
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
921 HINSTANCE hInst
= GetModuleHandleW(0);
923 WCHAR wstr
[MAX_STRING_LEN
];
926 if (preview
.pages_shown
< 2 || is_last_preview_page(preview
.page
))
928 static const WCHAR fmt
[] = {' ','%','d','\0'};
929 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGE
, wstr
, MAX_STRING_LEN
);
930 wsprintfW(p
, fmt
, preview
.page
);
932 static const WCHAR fmt
[] = {' ','%','d','-','%','d','\0'};
933 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGES
, wstr
, MAX_STRING_LEN
);
934 wsprintfW(p
, fmt
, preview
.page
, preview
.page
+ 1);
936 SetWindowTextW(hStatusbar
, wstr
);
939 /* Update for page changes. */
940 static void update_preview(HWND hMainWnd
)
943 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
944 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
945 HBITMAP hBitmapCapture
;
947 HDC hdc
= GetDC(hwndPreview
);
949 fr
.hdcTarget
= make_dc();
950 fr
.rc
= fr
.rcPage
= preview
.rcPage
;
951 fr
.rc
.left
+= margins
.left
;
952 fr
.rc
.top
+= margins
.top
;
953 fr
.rc
.bottom
-= margins
.bottom
;
954 fr
.rc
.right
-= margins
.right
;
957 fr
.chrg
.cpMax
= preview
.textlength
;
960 paper
.right
= preview
.bmSize
.cx
;
962 paper
.bottom
= preview
.bmSize
.cy
;
965 preview
.hdc
= CreateCompatibleDC(hdc
);
966 hBitmapCapture
= CreateCompatibleBitmap(hdc
, preview
.bmSize
.cx
, preview
.bmSize
.cy
);
967 SelectObject(preview
.hdc
, hBitmapCapture
);
970 fr
.hdc
= preview
.hdc
;
971 draw_preview(hEditorWnd
, &fr
, &paper
, preview
.page
);
973 if(preview
.pages_shown
> 1)
977 preview
.hdc2
= CreateCompatibleDC(hdc
);
978 hBitmapCapture
= CreateCompatibleBitmap(hdc
,
981 SelectObject(preview
.hdc2
, hBitmapCapture
);
984 fr
.hdc
= preview
.hdc2
;
985 draw_preview(hEditorWnd
, &fr
, &fr
.rcPage
, preview
.page
+ 1);
987 DeleteDC(fr
.hdcTarget
);
988 ReleaseDC(hwndPreview
, hdc
);
990 InvalidateRect(hwndPreview
, NULL
, FALSE
);
991 update_preview_buttons(hMainWnd
);
992 update_preview_statusbar(hMainWnd
);
995 static void toggle_num_pages(HWND hMainWnd
)
997 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
998 WCHAR name
[MAX_STRING_LEN
];
999 HINSTANCE hInst
= GetModuleHandleW(0);
1002 preview
.pages_shown
= preview
.pages_shown
> 1 ? 1 : 2;
1004 nPreviewPages
= preview
.zoomlevel
> 0 ? preview
.saved_pages_shown
:
1005 preview
.pages_shown
;
1007 LoadStringW(hInst
, nPreviewPages
> 1 ? STRING_PREVIEW_ONEPAGE
:
1008 STRING_PREVIEW_TWOPAGES
,
1009 name
, MAX_STRING_LEN
);
1011 SetWindowTextW(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), name
);
1012 update_preview_sizes(GetDlgItem(hMainWnd
, IDC_PREVIEW
), TRUE
);
1013 update_preview(hMainWnd
);
1016 /* Returns the page shown that the point is in (1 or 2) or 0 if the point
1017 * isn't inside either page */
1018 static int preview_page_hittest(POINT pt
)
1021 rc
.left
= preview
.spacing
.cx
;
1022 rc
.right
= rc
.left
+ preview
.bmScaledSize
.cx
;
1023 rc
.top
= preview
.spacing
.cy
;
1024 rc
.bottom
= rc
.top
+ preview
.bmScaledSize
.cy
;
1025 if (PtInRect(&rc
, pt
))
1028 if (preview
.pages_shown
<= 1)
1031 rc
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1032 rc
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1033 if (PtInRect(&rc
, pt
))
1034 return is_last_preview_page(preview
.page
) ? 1 : 2;
1039 LRESULT CALLBACK
preview_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1045 HWND hMainWnd
= GetParent(hWnd
);
1046 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
1048 GETTEXTLENGTHEX gt
= {GTL_DEFAULT
, 1200};
1049 HDC hdc
= GetDC(hWnd
);
1050 HDC hdcTarget
= make_dc();
1052 fr
.rc
= preview
.rcPage
= get_print_rect(hdcTarget
);
1053 preview
.rcPage
.bottom
+= margins
.bottom
;
1054 preview
.rcPage
.right
+= margins
.right
;
1055 preview
.rcPage
.top
= preview
.rcPage
.left
= 0;
1056 fr
.rcPage
= preview
.rcPage
;
1058 preview
.bmSize
.cx
= twips_to_pixels(preview
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
1059 preview
.bmSize
.cy
= twips_to_pixels(preview
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
1061 preview
.textlength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1063 fr
.hdc
= CreateCompatibleDC(hdc
);
1064 fr
.hdcTarget
= hdcTarget
;
1066 fr
.chrg
.cpMax
= preview
.textlength
;
1068 DeleteDC(hdcTarget
);
1069 ReleaseDC(hWnd
, hdc
);
1071 update_preview_sizes(hWnd
, TRUE
);
1072 update_preview(hMainWnd
);
1077 return print_preview(hWnd
);
1081 update_preview_sizes(hWnd
, FALSE
);
1082 InvalidateRect(hWnd
, NULL
, FALSE
);
1091 int nBar
= (msg
== WM_VSCROLL
) ? SB_VERT
: SB_HORZ
;
1094 GetClientRect(hWnd
, &rc
);
1095 si
.cbSize
= sizeof(si
);
1097 GetScrollInfo(hWnd
, nBar
, &si
);
1099 switch(LOWORD(wParam
))
1101 case SB_TOP
: /* == SB_LEFT */
1104 case SB_BOTTOM
: /* == SB_RIGHT */
1107 case SB_LINEUP
: /* == SB_LINELEFT */
1108 si
.nPos
-= si
.nPage
/ 10;
1110 case SB_LINEDOWN
: /* == SB_LINERIGHT */
1111 si
.nPos
+= si
.nPage
/ 10;
1113 case SB_PAGEUP
: /* == SB_PAGELEFT */
1114 si
.nPos
-= si
.nPage
;
1116 case SB_PAGEDOWN
: /* SB_PAGERIGHT */
1117 si
.nPos
+= si
.nPage
;
1120 si
.nPos
= si
.nTrackPos
;
1124 SetScrollInfo(hWnd
, nBar
, &si
, TRUE
);
1125 GetScrollInfo(hWnd
, nBar
, &si
);
1126 if (si
.nPos
!= origPos
)
1128 int amount
= origPos
- si
.nPos
;
1129 if (msg
== WM_VSCROLL
)
1130 ScrollWindow(hWnd
, 0, amount
, NULL
, NULL
);
1132 ScrollWindow(hWnd
, amount
, 0, NULL
, NULL
);
1142 DWORD messagePos
= GetMessagePos();
1143 pt
.x
= (short)LOWORD(messagePos
);
1144 pt
.y
= (short)HIWORD(messagePos
);
1145 ScreenToClient(hWnd
, &pt
);
1147 GetClientRect(hWnd
, &rc
);
1148 if (PtInRect(&rc
, pt
))
1150 pt
.x
+= GetScrollPos(hWnd
, SB_HORZ
);
1151 pt
.y
+= GetScrollPos(hWnd
, SB_VERT
);
1152 bHittest
= preview_page_hittest(pt
);
1156 SetCursor(LoadCursorW(GetModuleHandleW(0),
1157 MAKEINTRESOURCEW(IDC_ZOOM
)));
1159 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
1164 case WM_LBUTTONDOWN
:
1168 pt
.x
= (short)LOWORD(lParam
) + GetScrollPos(hWnd
, SB_HORZ
);
1169 pt
.y
= (short)HIWORD(lParam
) + GetScrollPos(hWnd
, SB_VERT
);
1170 if ((page
= preview_page_hittest(pt
)) > 0)
1172 HWND hMainWnd
= GetParent(hWnd
);
1174 /* Convert point from client coordinate to unzoomed page
1176 pt
.x
-= preview
.spacing
.cx
;
1178 pt
.x
-= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1179 pt
.y
-= preview
.spacing
.cy
;
1180 pt
.x
/= preview
.zoomratio
;
1181 pt
.y
/= preview
.zoomratio
;
1183 if (preview
.zoomlevel
== 0)
1184 preview
.saved_pages_shown
= preview
.pages_shown
;
1185 preview
.zoomlevel
= (preview
.zoomlevel
+ 1) % 3;
1186 preview
.zoomratio
= 0;
1187 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1)
1189 toggle_num_pages(hMainWnd
);
1190 } else if (preview
.pages_shown
> 1) {
1191 if (page
>= 2) preview
.page
++;
1192 toggle_num_pages(hMainWnd
);
1194 update_preview_sizes(hWnd
, TRUE
);
1195 InvalidateRect(hWnd
, NULL
, FALSE
);
1196 update_preview_buttons(hMainWnd
);
1199 if (preview
.zoomlevel
> 0) {
1201 /* Convert the coordinate back to client coordinate. */
1202 pt
.x
*= preview
.zoomratio
;
1203 pt
.y
*= preview
.zoomratio
;
1204 pt
.x
+= preview
.spacing
.cx
;
1205 pt
.y
+= preview
.spacing
.cy
;
1206 /* Scroll to center view at that point on the page */
1207 si
.cbSize
= sizeof(si
);
1208 si
.fMask
= SIF_PAGE
;
1209 GetScrollInfo(hWnd
, SB_HORZ
, &si
);
1210 pt
.x
-= si
.nPage
/ 2;
1211 SetScrollPos(hWnd
, SB_HORZ
, pt
.x
, TRUE
);
1212 GetScrollInfo(hWnd
, SB_VERT
, &si
);
1213 pt
.y
-= si
.nPage
/ 2;
1214 SetScrollPos(hWnd
, SB_VERT
, pt
.y
, TRUE
);
1220 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1226 LRESULT
preview_command(HWND hWnd
, WPARAM wParam
)
1228 switch(LOWORD(wParam
))
1231 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
1234 case ID_PREVIEW_NEXTPAGE
:
1235 case ID_PREVIEW_PREVPAGE
:
1237 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1242 update_preview(hWnd
);
1246 case ID_PREVIEW_NUMPAGES
:
1247 toggle_num_pages(hWnd
);
1250 case ID_PREVIEW_ZOOMIN
:
1251 if (preview
.zoomlevel
< 2)
1253 if (preview
.zoomlevel
== 0)
1254 preview
.saved_pages_shown
= preview
.pages_shown
;
1255 preview
.zoomlevel
++;
1256 preview
.zoomratio
= 0;
1257 if (preview
.pages_shown
> 1)
1259 /* Forced switch to one page when zooming in. */
1260 toggle_num_pages(hWnd
);
1262 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1263 update_preview_sizes(hwndPreview
, TRUE
);
1264 InvalidateRect(hwndPreview
, NULL
, FALSE
);
1265 update_preview_buttons(hWnd
);
1270 case ID_PREVIEW_ZOOMOUT
:
1271 if (preview
.zoomlevel
> 0)
1273 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1274 preview
.zoomlevel
--;
1275 preview
.zoomratio
= 0;
1276 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1) {
1277 toggle_num_pages(hWnd
);
1279 update_preview_sizes(hwndPreview
, TRUE
);
1280 InvalidateRect(hwndPreview
, NULL
, FALSE
);
1281 update_preview_buttons(hWnd
);
1287 dialog_print(hWnd
, preview
.wszFileName
);
1288 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);