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
))
91 SetRect(&margins
, 1757, 1417, 1757, 1417);
94 void registry_set_previewpages(HKEY hKey
)
96 RegSetValueExW(hKey
, var_previewpages
, 0, REG_DWORD
,
97 (LPBYTE
)&preview
.pages_shown
, sizeof(DWORD
));
100 void registry_read_previewpages(HKEY hKey
)
102 DWORD size
= sizeof(DWORD
);
104 RegQueryValueExW(hKey
, var_previewpages
, 0, NULL
,
105 (LPBYTE
)&preview
.pages_shown
, &size
) != ERROR_SUCCESS
||
106 size
!= sizeof(DWORD
))
108 preview
.pages_shown
= 1;
110 if (preview
.pages_shown
< 1) preview
.pages_shown
= 1;
111 else if (preview
.pages_shown
> 2) preview
.pages_shown
= 2;
116 static void AddTextButton(HWND hRebarWnd
, UINT string
, UINT command
, UINT id
)
119 HINSTANCE hInstance
= GetModuleHandleW(0);
120 WCHAR text
[MAX_STRING_LEN
];
123 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
124 hButton
= CreateWindowW(WC_BUTTONW
, text
,
125 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
126 hRebarWnd
, ULongToHandle(command
), hInstance
, NULL
);
128 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
129 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
130 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
131 rb
.hwndChild
= hButton
;
132 rb
.cyChild
= rb
.cyMinChild
= 22;
133 rb
.cx
= rb
.cxMinChild
= 90;
137 SendMessageW(hRebarWnd
, RB_INSERTBANDW
, -1, (LPARAM
)&rb
);
140 static HDC
make_dc(void)
142 if(devNames
&& devMode
)
144 LPDEVNAMES dn
= GlobalLock(devNames
);
145 LPDEVMODEW dm
= GlobalLock(devMode
);
148 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
149 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
161 static LONG
twips_to_centmm(int twips
)
163 return MulDiv(twips
, CENTMM_PER_INCH
, TWIPS_PER_INCH
);
166 static LONG
centmm_to_twips(int mm
)
168 return MulDiv(mm
, TWIPS_PER_INCH
, CENTMM_PER_INCH
);
171 static LONG
twips_to_pixels(int twips
, int dpi
)
173 return MulDiv(twips
, dpi
, TWIPS_PER_INCH
);
176 static LONG
devunits_to_twips(int units
, int dpi
)
178 return MulDiv(units
, TWIPS_PER_INCH
, dpi
);
182 static RECT
get_print_rect(HDC hdc
)
189 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
190 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
191 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
192 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
195 width
= centmm_to_twips(18500);
196 height
= centmm_to_twips(27000);
199 SetRect(&rc
, margins
.left
, margins
.top
, width
- margins
.right
, height
- margins
.bottom
);
204 void target_device(HWND hMainWnd
, DWORD wordWrap
)
206 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
208 if(wordWrap
== ID_WORDWRAP_MARGIN
)
213 RECT rc
= get_print_rect(hdc
);
215 width
= rc
.right
- rc
.left
;
218 HDC hMaindc
= GetDC(hMainWnd
);
219 hdc
= CreateCompatibleDC(hMaindc
);
220 ReleaseDC(hMainWnd
, hMaindc
);
222 result
= SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
226 /* otherwise EM_SETTARGETDEVICE failed, so fall back on wrapping
227 * to window using the NULL DC. */
230 if (wordWrap
!= ID_WORDWRAP_NONE
) {
231 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 0);
233 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 1);
238 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
241 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
242 static const WCHAR defExt
[] = {'P','R','N',0};
243 static LPWSTR file_filter
;
246 file_filter
= get_print_file_filter(hMainWnd
);
248 ZeroMemory(&ofn
, sizeof(ofn
));
250 ofn
.lStructSize
= sizeof(ofn
);
251 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
252 ofn
.hwndOwner
= hMainWnd
;
253 ofn
.lpstrFilter
= file_filter
;
254 ofn
.lpstrFile
= file
;
255 ofn
.nMaxFile
= MAX_PATH
;
256 ofn
.lpstrDefExt
= defExt
;
258 if(GetSaveFileNameW(&ofn
))
264 static void char_from_pagenum(HWND hEditorWnd
, FORMATRANGE
*fr
, int page
)
270 for(i
= 1; i
< page
; i
++)
272 int bottom
= fr
->rc
.bottom
;
273 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, (LPARAM
)fr
);
274 fr
->rc
.bottom
= bottom
;
278 static HWND
get_ruler_wnd(HWND hMainWnd
)
280 return GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
283 void redraw_ruler(HWND hRulerWnd
)
287 GetClientRect(hRulerWnd
, &rc
);
288 InvalidateRect(hRulerWnd
, &rc
, TRUE
);
291 static void update_ruler(HWND hRulerWnd
)
293 SendMessageW(hRulerWnd
, WM_USER
, 0, 0);
294 redraw_ruler(hRulerWnd
);
297 static void add_ruler_units(HDC hdcRuler
, RECT
* drawRect
, BOOL NewMetrics
, LONG EditLeftmost
)
303 static HBITMAP hBitmap
;
304 int i
, x
, y
, RulerTextEnd
;
308 WCHAR FontName
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
313 DeleteObject(hBitmap
);
316 hdc
= CreateCompatibleDC(0);
318 CmPixels
= twips_to_pixels(centmm_to_twips(1000), GetDeviceCaps(hdc
, LOGPIXELSX
));
319 QuarterCmPixels
= (int)((float)CmPixels
/ 4.0);
321 hBitmap
= CreateCompatibleBitmap(hdc
, drawRect
->right
, drawRect
->bottom
);
322 SelectObject(hdc
, hBitmap
);
323 FillRect(hdc
, drawRect
, GetStockObject(WHITE_BRUSH
));
325 hFont
= CreateFontW(10, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName
);
327 SelectObject(hdc
, hFont
);
328 SetBkMode(hdc
, TRANSPARENT
);
329 SetTextAlign(hdc
, TA_CENTER
);
330 y
= (int)(((float)drawRect
->bottom
- (float)drawRect
->top
) / 2.0) + 1;
331 RulerTextEnd
= drawRect
->right
- EditLeftmost
+ 1;
332 for(i
= 1, x
= EditLeftmost
; x
< (drawRect
->right
- EditLeftmost
+ 1); i
++)
335 WCHAR format
[] = {'%','d',0};
338 x2
+= QuarterCmPixels
;
339 if(x2
> RulerTextEnd
)
342 MoveToEx(hdc
, x2
, y
, NULL
);
343 LineTo(hdc
, x2
, y
+2);
345 x2
+= QuarterCmPixels
;
346 if(x2
> RulerTextEnd
)
349 MoveToEx(hdc
, x2
, y
- 3, NULL
);
350 LineTo(hdc
, x2
, y
+ 3);
352 x2
+= QuarterCmPixels
;
353 if(x2
> RulerTextEnd
)
356 MoveToEx(hdc
, x2
, y
, NULL
);
357 LineTo(hdc
, x2
, y
+2);
363 wsprintfW(str
, format
, i
);
364 TextOutW(hdc
, x
, 5, str
, lstrlenW(str
));
369 BitBlt(hdcRuler
, 0, 0, drawRect
->right
, drawRect
->bottom
, hdc
, 0, 0, SRCAND
);
372 static void paint_ruler(HWND hWnd
, LONG EditLeftmost
, BOOL NewMetrics
)
375 HDC hdc
= BeginPaint(hWnd
, &ps
);
376 HDC hdcPrint
= make_dc();
377 RECT printRect
= get_print_rect(hdcPrint
);
380 GetClientRect(hWnd
, &drawRect
);
381 FillRect(hdc
, &drawRect
, GetSysColorBrush(COLOR_MENU
));
383 InflateRect(&drawRect
, 0, -3);
384 drawRect
.left
= EditLeftmost
;
385 drawRect
.right
= twips_to_pixels(printRect
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
386 FillRect(hdc
, &drawRect
, GetStockObject(WHITE_BRUSH
));
390 DrawEdge(hdc
, &drawRect
, EDGE_SUNKEN
, BF_RECT
);
392 drawRect
.left
= drawRect
.right
- 1;
393 drawRect
.right
= twips_to_pixels(printRect
.right
+ margins
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
394 DrawEdge(hdc
, &drawRect
, EDGE_ETCHED
, BF_RECT
);
398 add_ruler_units(hdc
, &drawRect
, NewMetrics
, EditLeftmost
);
400 SelectObject(hdc
, GetStockObject(BLACK_BRUSH
));
405 LRESULT CALLBACK
ruler_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
407 static WNDPROC pPrevRulerProc
;
408 static LONG EditLeftmost
;
409 static BOOL NewMetrics
;
416 EditLeftmost
= ((POINTL
*)wParam
)->x
;
417 pPrevRulerProc
= (WNDPROC
)lParam
;
423 paint_ruler(hWnd
, EditLeftmost
, NewMetrics
);
427 return CallWindowProcW(pPrevRulerProc
, hWnd
, msg
, wParam
, lParam
);
433 static void print(LPPRINTDLGW pd
, LPWSTR wszFileName
)
437 HWND hEditorWnd
= GetDlgItem(pd
->hwndOwner
, IDC_EDITOR
);
438 int printedPages
= 0;
441 fr
.hdcTarget
= pd
->hDC
;
443 fr
.rc
= get_print_rect(fr
.hdc
);
444 SetRect(&fr
.rcPage
, 0, 0, fr
.rc
.right
+ margins
.right
, fr
.rc
.bottom
+ margins
.bottom
);
446 ZeroMemory(&di
, sizeof(di
));
447 di
.cbSize
= sizeof(di
);
448 di
.lpszDocName
= wszFileName
;
450 if(pd
->Flags
& PD_PRINTTOFILE
)
452 di
.lpszOutput
= dialog_print_to_file(pd
->hwndOwner
);
457 if(pd
->Flags
& PD_SELECTION
)
459 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
463 gt
.flags
= GTL_DEFAULT
;
466 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
468 if(pd
->Flags
& PD_PAGENUMS
)
469 char_from_pagenum(hEditorWnd
, &fr
, pd
->nToPage
);
472 StartDocW(fr
.hdc
, &di
);
475 if(StartPage(fr
.hdc
) <= 0)
478 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
480 if(EndPage(fr
.hdc
) <= 0)
484 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
487 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
490 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
493 void dialog_printsetup(HWND hMainWnd
)
497 ZeroMemory(&ps
, sizeof(ps
));
498 ps
.lStructSize
= sizeof(ps
);
499 ps
.hwndOwner
= hMainWnd
;
500 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
501 SetRect(&ps
.rtMargin
, twips_to_centmm(margins
.left
), twips_to_centmm(margins
.top
),
502 twips_to_centmm(margins
.right
), twips_to_centmm(margins
.bottom
));
503 ps
.hDevMode
= devMode
;
504 ps
.hDevNames
= devNames
;
506 if(PageSetupDlgW(&ps
))
508 SetRect(&margins
, centmm_to_twips(ps
.rtMargin
.left
), centmm_to_twips(ps
.rtMargin
.top
),
509 centmm_to_twips(ps
.rtMargin
.right
), centmm_to_twips(ps
.rtMargin
.bottom
));
510 devMode
= ps
.hDevMode
;
511 devNames
= ps
.hDevNames
;
512 update_ruler(get_ruler_wnd(hMainWnd
));
516 void get_default_printer_opts(void)
519 ZeroMemory(&pd
, sizeof(pd
));
521 ZeroMemory(&pd
, sizeof(pd
));
522 pd
.lStructSize
= sizeof(pd
);
523 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
524 pd
.hDevMode
= devMode
;
528 devMode
= pd
.hDevMode
;
529 devNames
= pd
.hDevNames
;
532 void print_quick(HWND hMainWnd
, LPWSTR wszFileName
)
536 ZeroMemory(&pd
, sizeof(pd
));
537 pd
.hwndOwner
= hMainWnd
;
540 print(&pd
, wszFileName
);
544 void dialog_print(HWND hMainWnd
, LPWSTR wszFileName
)
547 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
551 ZeroMemory(&pd
, sizeof(pd
));
552 pd
.lStructSize
= sizeof(pd
);
553 pd
.hwndOwner
= hMainWnd
;
554 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
557 pd
.hDevMode
= devMode
;
558 pd
.hDevNames
= devNames
;
560 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
562 pd
.Flags
|= PD_NOSELECTION
;
566 devMode
= pd
.hDevMode
;
567 devNames
= pd
.hDevNames
;
568 print(&pd
, wszFileName
);
569 update_ruler(get_ruler_wnd(hMainWnd
));
573 static void preview_bar_show(HWND hMainWnd
, BOOL show
)
575 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
582 UINT num_pages_string
= preview
.pages_shown
> 1 ? STRING_PREVIEW_ONEPAGE
:
583 STRING_PREVIEW_TWOPAGES
;
585 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
586 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
587 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
588 AddTextButton(hReBar
, num_pages_string
, ID_PREVIEW_NUMPAGES
, BANDID_PREVIEW_BTN4
);
589 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMIN
, ID_PREVIEW_ZOOMIN
, BANDID_PREVIEW_BTN5
);
590 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMOUT
, ID_PREVIEW_ZOOMOUT
, BANDID_PREVIEW_BTN6
);
591 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN7
);
593 hStatic
= CreateWindowW(WC_STATICW
, NULL
,
594 WS_VISIBLE
| WS_CHILD
, 0, 0, 0, 0,
595 hReBar
, NULL
, NULL
, NULL
);
597 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
598 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
599 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
600 rb
.hwndChild
= hStatic
;
601 rb
.cyChild
= rb
.cyMinChild
= 22;
602 rb
.cx
= rb
.cxMinChild
= 90;
604 rb
.wID
= BANDID_PREVIEW_BUFFER
;
606 SendMessageW(hReBar
, RB_INSERTBANDW
, -1, (LPARAM
)&rb
);
609 for(i
= 0; i
<= PREVIEW_BUTTONS
; i
++)
610 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
614 static const int min_spacing
= 10;
616 static void update_preview_scrollbars(HWND hwndPreview
, RECT
*window
)
619 sbi
.cbSize
= sizeof(sbi
);
620 sbi
.fMask
= SIF_PAGE
|SIF_RANGE
;
622 if (preview
.zoomlevel
== 0)
624 /* Hide scrollbars when zoomed out. */
626 sbi
.nPage
= window
->right
;
627 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
628 sbi
.nPage
= window
->bottom
;
629 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
631 sbi
.nMax
= preview
.bmScaledSize
.cx
* preview
.pages_shown
+
632 min_spacing
* (preview
.pages_shown
+ 1);
633 sbi
.nPage
= window
->right
;
634 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
635 /* Change in the horizontal scrollbar visibility affects the
636 * client rect, so update the client rect. */
637 GetClientRect(hwndPreview
, window
);
638 sbi
.nMax
= preview
.bmScaledSize
.cy
+ min_spacing
* 2;
639 sbi
.nPage
= window
->bottom
;
640 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
644 static void update_preview_sizes(HWND hwndPreview
, BOOL zoomLevelUpdated
)
648 GetClientRect(hwndPreview
, &window
);
650 /* The zoom ratio isn't updated for partial zoom because of resizing the window. */
651 if (zoomLevelUpdated
|| preview
.zoomlevel
!= 1)
653 float ratio
, ratioHeight
, ratioWidth
;
654 if (preview
.zoomlevel
== 2)
658 ratioHeight
= (window
.bottom
- min_spacing
* 2) / (float)preview
.bmSize
.cy
;
660 ratioWidth
= (float)(window
.right
-
661 min_spacing
* (preview
.pages_shown
+ 1)) /
662 (preview
.pages_shown
* preview
.bmSize
.cx
);
664 if(ratioWidth
> ratioHeight
)
669 if (preview
.zoomlevel
== 1)
670 ratio
+= (1.0 - ratio
) / 2;
672 preview
.zoomratio
= ratio
;
675 preview
.bmScaledSize
.cx
= preview
.bmSize
.cx
* preview
.zoomratio
;
676 preview
.bmScaledSize
.cy
= preview
.bmSize
.cy
* preview
.zoomratio
;
678 preview
.spacing
.cy
= max(min_spacing
, (window
.bottom
- preview
.bmScaledSize
.cy
) / 2);
680 preview
.spacing
.cx
= (window
.right
-
681 preview
.bmScaledSize
.cx
* preview
.pages_shown
) /
682 (preview
.pages_shown
+ 1);
683 if (preview
.spacing
.cx
< min_spacing
)
684 preview
.spacing
.cx
= min_spacing
;
686 update_preview_scrollbars(hwndPreview
, &window
);
689 static void draw_margin_lines(HDC hdc
, int x
, int y
, float ratio
)
693 RECT page_margin
= preview
.rcPage
;
695 dpi
.cx
= GetDeviceCaps(hdc
, LOGPIXELSX
);
696 dpi
.cy
= GetDeviceCaps(hdc
, LOGPIXELSY
);
698 SetRect(&page_margin
, preview
.rcPage
.left
+ margins
.left
, preview
.rcPage
.top
+ margins
.top
,
699 preview
.rcPage
.right
- margins
.right
, preview
.rcPage
.bottom
- margins
.bottom
);
701 page_margin
.left
= (int)((float)twips_to_pixels(page_margin
.left
, dpi
.cx
) * ratio
);
702 page_margin
.top
= (int)((float)twips_to_pixels(page_margin
.top
, dpi
.cy
) * ratio
);
703 page_margin
.bottom
= (int)((float)twips_to_pixels(page_margin
.bottom
, dpi
.cy
) * ratio
);
704 page_margin
.right
= (int)((float)twips_to_pixels(page_margin
.right
, dpi
.cx
) * ratio
);
706 OffsetRect(&page_margin
, x
, y
);
708 hPen
= CreatePen(PS_DOT
, 1, RGB(0,0,0));
709 oldPen
= SelectObject(hdc
, hPen
);
711 MoveToEx(hdc
, x
, page_margin
.top
, NULL
);
712 LineTo(hdc
, x
+ preview
.bmScaledSize
.cx
, page_margin
.top
);
713 MoveToEx(hdc
, x
, page_margin
.bottom
, NULL
);
714 LineTo(hdc
, x
+ preview
.bmScaledSize
.cx
, page_margin
.bottom
);
716 MoveToEx(hdc
, page_margin
.left
, y
, NULL
);
717 LineTo(hdc
, page_margin
.left
, y
+ preview
.bmScaledSize
.cy
);
718 MoveToEx(hdc
, page_margin
.right
, y
, NULL
);
719 LineTo(hdc
, page_margin
.right
, y
+ preview
.bmScaledSize
.cy
);
721 SelectObject(hdc
, oldPen
);
725 static BOOL
is_last_preview_page(int page
)
727 return preview
.pageEnds
[page
- 1] >= preview
.textlength
;
730 void init_preview(HWND hMainWnd
, LPWSTR wszFileName
)
732 HINSTANCE hInstance
= GetModuleHandleW(0);
736 preview
.wszFileName
= wszFileName
;
737 preview
.zoomratio
= 0;
738 preview
.zoomlevel
= 0;
739 preview_bar_show(hMainWnd
, TRUE
);
741 CreateWindowExW(0, wszPreviewWndClass
, NULL
,
742 WS_VISIBLE
| WS_CHILD
| WS_VSCROLL
| WS_HSCROLL
,
743 0, 0, 200, 10, hMainWnd
, (HMENU
)IDC_PREVIEW
, hInstance
, NULL
);
746 void close_preview(HWND hMainWnd
)
748 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
749 preview
.window
.right
= 0;
750 preview
.window
.bottom
= 0;
752 HeapFree(GetProcessHeap(), 0, preview
.pageEnds
);
753 preview
.pageEnds
= NULL
;
754 preview
.pageCapacity
= 0;
755 if (preview
.zoomlevel
> 0)
756 preview
.pages_shown
= preview
.saved_pages_shown
;
758 HBITMAP oldbm
= GetCurrentObject(preview
.hdc
, OBJ_BITMAP
);
759 DeleteDC(preview
.hdc
);
764 HBITMAP oldbm
= GetCurrentObject(preview
.hdc2
, OBJ_BITMAP
);
765 DeleteDC(preview
.hdc2
);
770 preview_bar_show(hMainWnd
, FALSE
);
771 DestroyWindow(hwndPreview
);
774 BOOL
preview_isactive(void)
776 return preview
.page
!= 0;
779 static void draw_preview(HWND hEditorWnd
, FORMATRANGE
* lpFr
, RECT
* paper
, int page
)
783 if (!preview
.pageEnds
)
785 preview
.pageCapacity
= 32;
786 preview
.pageEnds
= HeapAlloc(GetProcessHeap(), 0,
787 sizeof(int) * preview
.pageCapacity
);
788 if (!preview
.pageEnds
) return;
789 } else if (page
>= preview
.pageCapacity
) {
791 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, preview
.pageEnds
,
792 sizeof(int) * preview
.pageCapacity
* 2);
793 if (!new_buffer
) return;
794 preview
.pageCapacity
*= 2;
795 preview
.pageEnds
= new_buffer
;
798 FillRect(lpFr
->hdc
, paper
, GetStockObject(WHITE_BRUSH
));
799 if (page
> 1 && is_last_preview_page(page
- 1)) return;
800 lpFr
->chrg
.cpMin
= page
<= 1 ? 0 : preview
.pageEnds
[page
-2];
801 bottom
= lpFr
->rc
.bottom
;
802 preview
.pageEnds
[page
-1] = SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)lpFr
);
804 /* EM_FORMATRANGE sets fr.rc.bottom to indicate the area printed in,
805 * but we want to keep the original for drawing margins */
806 lpFr
->rc
.bottom
= bottom
;
807 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
810 static void update_preview_buttons(HWND hMainWnd
)
812 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
813 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
814 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
),
815 !is_last_preview_page(preview
.page
) &&
816 !is_last_preview_page(preview
.page
+ preview
.pages_shown
- 1));
817 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
),
818 preview
.pages_shown
> 1 ||
819 (!is_last_preview_page(1) && preview
.zoomlevel
== 0));
820 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMIN
), preview
.zoomlevel
< 2);
821 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMOUT
), preview
.zoomlevel
> 0);
824 static LRESULT
print_preview(HWND hwndPreview
)
828 HRGN back_rgn
, excl_rgn
;
829 RECT window
, background
;
833 hdc
= BeginPaint(hwndPreview
, &ps
);
834 GetClientRect(hwndPreview
, &window
);
835 back_rgn
= CreateRectRgnIndirect(&window
);
837 x
= preview
.spacing
.cx
- GetScrollPos(hwndPreview
, SB_HORZ
);
838 y
= preview
.spacing
.cy
- GetScrollPos(hwndPreview
, SB_VERT
);
840 /* draw page outlines */
841 hPen
= CreatePen(PS_SOLID
|PS_INSIDEFRAME
, 2, RGB(0,0,0));
842 oldPen
= SelectObject(hdc
, hPen
);
843 SetRect(&background
, x
- 2, y
- 2, x
+ preview
.bmScaledSize
.cx
+ 2,
844 y
+ preview
.bmScaledSize
.cy
+ 2);
845 Rectangle(hdc
, background
.left
, background
.top
,
846 background
.right
, background
.bottom
);
847 excl_rgn
= CreateRectRgnIndirect(&background
);
848 CombineRgn(back_rgn
, back_rgn
, excl_rgn
, RGN_DIFF
);
849 if(preview
.pages_shown
> 1)
851 background
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
852 background
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
853 Rectangle(hdc
, background
.left
, background
.top
,
854 background
.right
, background
.bottom
);
855 SetRectRgn(excl_rgn
, background
.left
, background
.top
,
856 background
.right
, background
.bottom
);
857 CombineRgn(back_rgn
, back_rgn
, excl_rgn
, RGN_DIFF
);
859 SelectObject(hdc
, oldPen
);
861 FillRgn(hdc
, back_rgn
, GetStockObject(GRAY_BRUSH
));
862 DeleteObject(excl_rgn
);
863 DeleteObject(back_rgn
);
865 StretchBlt(hdc
, x
, y
, preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
866 preview
.hdc
, 0, 0, preview
.bmSize
.cx
, preview
.bmSize
.cy
, SRCCOPY
);
868 draw_margin_lines(hdc
, x
, y
, preview
.zoomratio
);
870 if(preview
.pages_shown
> 1)
872 if (!is_last_preview_page(preview
.page
)) {
873 x
+= preview
.spacing
.cx
+ preview
.bmScaledSize
.cx
;
874 StretchBlt(hdc
, x
, y
,
875 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
877 preview
.bmSize
.cx
, preview
.bmSize
.cy
, SRCCOPY
);
879 draw_margin_lines(hdc
, x
, y
, preview
.zoomratio
);
881 InflateRect(&background
, -2, -2);
882 FillRect(hdc
, &background
, GetStockObject(WHITE_BRUSH
));
886 preview
.window
= window
;
888 EndPaint(hwndPreview
, &ps
);
893 static void update_preview_statusbar(HWND hMainWnd
)
895 HWND hStatusbar
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
896 HINSTANCE hInst
= GetModuleHandleW(0);
898 WCHAR wstr
[MAX_STRING_LEN
];
901 if (preview
.pages_shown
< 2 || is_last_preview_page(preview
.page
))
903 static const WCHAR fmt
[] = {' ','%','d','\0'};
904 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGE
, wstr
, MAX_STRING_LEN
);
905 wsprintfW(p
, fmt
, preview
.page
);
907 static const WCHAR fmt
[] = {' ','%','d','-','%','d','\0'};
908 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGES
, wstr
, MAX_STRING_LEN
);
909 wsprintfW(p
, fmt
, preview
.page
, preview
.page
+ 1);
911 SetWindowTextW(hStatusbar
, wstr
);
914 /* Update for page changes. */
915 static void update_preview(HWND hMainWnd
)
918 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
919 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
920 HBITMAP hBitmapCapture
;
922 HDC hdc
= GetDC(hwndPreview
);
924 fr
.hdcTarget
= make_dc();
925 fr
.rc
= fr
.rcPage
= preview
.rcPage
;
926 fr
.rc
.left
+= margins
.left
;
927 fr
.rc
.top
+= margins
.top
;
928 fr
.rc
.bottom
-= margins
.bottom
;
929 fr
.rc
.right
-= margins
.right
;
932 fr
.chrg
.cpMax
= preview
.textlength
;
934 SetRect(&paper
, 0, 0, preview
.bmSize
.cx
, preview
.bmSize
.cy
);
937 preview
.hdc
= CreateCompatibleDC(hdc
);
938 hBitmapCapture
= CreateCompatibleBitmap(hdc
, preview
.bmSize
.cx
, preview
.bmSize
.cy
);
939 SelectObject(preview
.hdc
, hBitmapCapture
);
942 fr
.hdc
= preview
.hdc
;
943 draw_preview(hEditorWnd
, &fr
, &paper
, preview
.page
);
945 if(preview
.pages_shown
> 1)
949 preview
.hdc2
= CreateCompatibleDC(hdc
);
950 hBitmapCapture
= CreateCompatibleBitmap(hdc
,
953 SelectObject(preview
.hdc2
, hBitmapCapture
);
956 fr
.hdc
= preview
.hdc2
;
957 draw_preview(hEditorWnd
, &fr
, &fr
.rcPage
, preview
.page
+ 1);
959 DeleteDC(fr
.hdcTarget
);
960 ReleaseDC(hwndPreview
, hdc
);
962 InvalidateRect(hwndPreview
, NULL
, FALSE
);
963 update_preview_buttons(hMainWnd
);
964 update_preview_statusbar(hMainWnd
);
967 static void toggle_num_pages(HWND hMainWnd
)
969 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
970 WCHAR name
[MAX_STRING_LEN
];
971 HINSTANCE hInst
= GetModuleHandleW(0);
974 preview
.pages_shown
= preview
.pages_shown
> 1 ? 1 : 2;
976 nPreviewPages
= preview
.zoomlevel
> 0 ? preview
.saved_pages_shown
:
979 LoadStringW(hInst
, nPreviewPages
> 1 ? STRING_PREVIEW_ONEPAGE
:
980 STRING_PREVIEW_TWOPAGES
,
981 name
, MAX_STRING_LEN
);
983 SetWindowTextW(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), name
);
984 update_preview_sizes(GetDlgItem(hMainWnd
, IDC_PREVIEW
), TRUE
);
985 update_preview(hMainWnd
);
988 /* Returns the page shown that the point is in (1 or 2) or 0 if the point
989 * isn't inside either page */
990 static int preview_page_hittest(POINT pt
)
993 rc
.left
= preview
.spacing
.cx
;
994 rc
.right
= rc
.left
+ preview
.bmScaledSize
.cx
;
995 rc
.top
= preview
.spacing
.cy
;
996 rc
.bottom
= rc
.top
+ preview
.bmScaledSize
.cy
;
997 if (PtInRect(&rc
, pt
))
1000 if (preview
.pages_shown
<= 1)
1003 rc
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1004 rc
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1005 if (PtInRect(&rc
, pt
))
1006 return is_last_preview_page(preview
.page
) ? 1 : 2;
1011 LRESULT CALLBACK
preview_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1017 HWND hMainWnd
= GetParent(hWnd
);
1018 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
1020 GETTEXTLENGTHEX gt
= {GTL_DEFAULT
, 1200};
1021 HDC hdc
= GetDC(hWnd
);
1022 HDC hdcTarget
= make_dc();
1024 fr
.rc
= preview
.rcPage
= get_print_rect(hdcTarget
);
1025 preview
.rcPage
.bottom
+= margins
.bottom
;
1026 preview
.rcPage
.right
+= margins
.right
;
1027 preview
.rcPage
.top
= preview
.rcPage
.left
= 0;
1028 fr
.rcPage
= preview
.rcPage
;
1030 preview
.bmSize
.cx
= twips_to_pixels(preview
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
1031 preview
.bmSize
.cy
= twips_to_pixels(preview
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
1033 preview
.textlength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1035 fr
.hdc
= CreateCompatibleDC(hdc
);
1036 fr
.hdcTarget
= hdcTarget
;
1038 fr
.chrg
.cpMax
= preview
.textlength
;
1040 DeleteDC(hdcTarget
);
1041 ReleaseDC(hWnd
, hdc
);
1043 update_preview_sizes(hWnd
, TRUE
);
1044 update_preview(hMainWnd
);
1049 return print_preview(hWnd
);
1053 update_preview_sizes(hWnd
, FALSE
);
1054 InvalidateRect(hWnd
, NULL
, FALSE
);
1063 int nBar
= (msg
== WM_VSCROLL
) ? SB_VERT
: SB_HORZ
;
1066 GetClientRect(hWnd
, &rc
);
1067 si
.cbSize
= sizeof(si
);
1069 GetScrollInfo(hWnd
, nBar
, &si
);
1071 switch(LOWORD(wParam
))
1073 case SB_TOP
: /* == SB_LEFT */
1076 case SB_BOTTOM
: /* == SB_RIGHT */
1079 case SB_LINEUP
: /* == SB_LINELEFT */
1080 si
.nPos
-= si
.nPage
/ 10;
1082 case SB_LINEDOWN
: /* == SB_LINERIGHT */
1083 si
.nPos
+= si
.nPage
/ 10;
1085 case SB_PAGEUP
: /* == SB_PAGELEFT */
1086 si
.nPos
-= si
.nPage
;
1088 case SB_PAGEDOWN
: /* SB_PAGERIGHT */
1089 si
.nPos
+= si
.nPage
;
1092 si
.nPos
= si
.nTrackPos
;
1096 SetScrollInfo(hWnd
, nBar
, &si
, TRUE
);
1097 GetScrollInfo(hWnd
, nBar
, &si
);
1098 if (si
.nPos
!= origPos
)
1100 int amount
= origPos
- si
.nPos
;
1101 if (msg
== WM_VSCROLL
)
1102 ScrollWindow(hWnd
, 0, amount
, NULL
, NULL
);
1104 ScrollWindow(hWnd
, amount
, 0, NULL
, NULL
);
1114 DWORD messagePos
= GetMessagePos();
1115 pt
.x
= (short)LOWORD(messagePos
);
1116 pt
.y
= (short)HIWORD(messagePos
);
1117 ScreenToClient(hWnd
, &pt
);
1119 GetClientRect(hWnd
, &rc
);
1120 if (PtInRect(&rc
, pt
))
1122 pt
.x
+= GetScrollPos(hWnd
, SB_HORZ
);
1123 pt
.y
+= GetScrollPos(hWnd
, SB_VERT
);
1124 bHittest
= preview_page_hittest(pt
);
1128 SetCursor(LoadCursorW(GetModuleHandleW(0),
1129 MAKEINTRESOURCEW(IDC_ZOOM
)));
1131 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
1136 case WM_LBUTTONDOWN
:
1140 pt
.x
= (short)LOWORD(lParam
) + GetScrollPos(hWnd
, SB_HORZ
);
1141 pt
.y
= (short)HIWORD(lParam
) + GetScrollPos(hWnd
, SB_VERT
);
1142 if ((page
= preview_page_hittest(pt
)) > 0)
1144 HWND hMainWnd
= GetParent(hWnd
);
1146 /* Convert point from client coordinate to unzoomed page
1148 pt
.x
-= preview
.spacing
.cx
;
1150 pt
.x
-= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1151 pt
.y
-= preview
.spacing
.cy
;
1152 pt
.x
/= preview
.zoomratio
;
1153 pt
.y
/= preview
.zoomratio
;
1155 if (preview
.zoomlevel
== 0)
1156 preview
.saved_pages_shown
= preview
.pages_shown
;
1157 preview
.zoomlevel
= (preview
.zoomlevel
+ 1) % 3;
1158 preview
.zoomratio
= 0;
1159 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1)
1161 toggle_num_pages(hMainWnd
);
1162 } else if (preview
.pages_shown
> 1) {
1163 if (page
>= 2) preview
.page
++;
1164 toggle_num_pages(hMainWnd
);
1166 update_preview_sizes(hWnd
, TRUE
);
1167 InvalidateRect(hWnd
, NULL
, FALSE
);
1168 update_preview_buttons(hMainWnd
);
1171 if (preview
.zoomlevel
> 0) {
1173 /* Convert the coordinate back to client coordinate. */
1174 pt
.x
*= preview
.zoomratio
;
1175 pt
.y
*= preview
.zoomratio
;
1176 pt
.x
+= preview
.spacing
.cx
;
1177 pt
.y
+= preview
.spacing
.cy
;
1178 /* Scroll to center view at that point on the page */
1179 si
.cbSize
= sizeof(si
);
1180 si
.fMask
= SIF_PAGE
;
1181 GetScrollInfo(hWnd
, SB_HORZ
, &si
);
1182 pt
.x
-= si
.nPage
/ 2;
1183 SetScrollPos(hWnd
, SB_HORZ
, pt
.x
, TRUE
);
1184 GetScrollInfo(hWnd
, SB_VERT
, &si
);
1185 pt
.y
-= si
.nPage
/ 2;
1186 SetScrollPos(hWnd
, SB_VERT
, pt
.y
, TRUE
);
1192 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1198 LRESULT
preview_command(HWND hWnd
, WPARAM wParam
)
1200 switch(LOWORD(wParam
))
1203 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
1206 case ID_PREVIEW_NEXTPAGE
:
1207 case ID_PREVIEW_PREVPAGE
:
1209 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1214 update_preview(hWnd
);
1218 case ID_PREVIEW_NUMPAGES
:
1219 toggle_num_pages(hWnd
);
1222 case ID_PREVIEW_ZOOMIN
:
1223 if (preview
.zoomlevel
< 2)
1225 if (preview
.zoomlevel
== 0)
1226 preview
.saved_pages_shown
= preview
.pages_shown
;
1227 preview
.zoomlevel
++;
1228 preview
.zoomratio
= 0;
1229 if (preview
.pages_shown
> 1)
1231 /* Forced switch to one page when zooming in. */
1232 toggle_num_pages(hWnd
);
1234 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1235 update_preview_sizes(hwndPreview
, TRUE
);
1236 InvalidateRect(hwndPreview
, NULL
, FALSE
);
1237 update_preview_buttons(hWnd
);
1242 case ID_PREVIEW_ZOOMOUT
:
1243 if (preview
.zoomlevel
> 0)
1245 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1246 preview
.zoomlevel
--;
1247 preview
.zoomratio
= 0;
1248 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1) {
1249 toggle_num_pages(hWnd
);
1251 update_preview_sizes(hwndPreview
, TRUE
);
1252 InvalidateRect(hwndPreview
, NULL
, FALSE
);
1253 update_preview_buttons(hWnd
);
1259 dialog_print(hWnd
, preview
.wszFileName
);
1260 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);