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
31 int saved_pages_shown
;
32 int *pageEnds
, pageCapacity
;
46 } previewinfo
, *ppreviewinfo
;
48 static HGLOBAL devMode
;
49 static HGLOBAL devNames
;
52 static previewinfo preview
;
54 extern const WCHAR wszPreviewWndClass
[];
56 static const WCHAR var_pagemargin
[] = {'P','a','g','e','M','a','r','g','i','n',0};
57 static const WCHAR var_previewpages
[] = {'P','r','e','v','i','e','w','P','a','g','e','s',0};
59 static LPWSTR
get_print_file_filter(HWND hMainWnd
)
61 static WCHAR wszPrintFilter
[MAX_STRING_LEN
*2+6+4+1];
62 const WCHAR files_prn
[] = {'*','.','P','R','N',0};
63 const WCHAR files_all
[] = {'*','.','*','\0'};
65 HINSTANCE hInstance
= GetModuleHandleW(0);
68 LoadStringW(hInstance
, STRING_PRINTER_FILES_PRN
, p
, MAX_STRING_LEN
);
70 lstrcpyW(p
, files_prn
);
72 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
74 lstrcpyW(p
, files_all
);
78 return wszPrintFilter
;
81 void registry_set_pagemargins(HKEY hKey
)
83 RegSetValueExW(hKey
, var_pagemargin
, 0, REG_BINARY
, (LPBYTE
)&margins
, sizeof(RECT
));
86 void registry_read_pagemargins(HKEY hKey
)
88 DWORD size
= sizeof(RECT
);
90 if(!hKey
|| RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
91 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
94 margins
.bottom
= 1417;
100 void registry_set_previewpages(HKEY hKey
)
102 RegSetValueExW(hKey
, var_previewpages
, 0, REG_DWORD
,
103 (LPBYTE
)&preview
.pages_shown
, sizeof(DWORD
));
106 void registry_read_previewpages(HKEY hKey
)
108 DWORD size
= sizeof(DWORD
);
110 RegQueryValueExW(hKey
, var_previewpages
, 0, NULL
,
111 (LPBYTE
)&preview
.pages_shown
, &size
) != ERROR_SUCCESS
||
112 size
!= sizeof(DWORD
))
114 preview
.pages_shown
= 1;
116 if (preview
.pages_shown
< 1) preview
.pages_shown
= 1;
117 else if (preview
.pages_shown
> 2) preview
.pages_shown
= 2;
122 static void AddTextButton(HWND hRebarWnd
, UINT string
, UINT command
, UINT id
)
125 HINSTANCE hInstance
= GetModuleHandleW(0);
126 WCHAR text
[MAX_STRING_LEN
];
129 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
130 hButton
= CreateWindowW(WC_BUTTONW
, text
,
131 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
132 hRebarWnd
, ULongToHandle(command
), hInstance
, NULL
);
134 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
135 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
136 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
137 rb
.hwndChild
= hButton
;
138 rb
.cyChild
= rb
.cyMinChild
= 22;
139 rb
.cx
= rb
.cxMinChild
= 90;
143 SendMessageW(hRebarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
146 static HDC
make_dc(void)
148 if(devNames
&& devMode
)
150 LPDEVNAMES dn
= GlobalLock(devNames
);
151 LPDEVMODEW dm
= GlobalLock(devMode
);
154 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
155 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
167 static LONG
twips_to_centmm(int twips
)
169 return MulDiv(twips
, CENTMM_PER_INCH
, TWIPS_PER_INCH
);
172 static LONG
centmm_to_twips(int mm
)
174 return MulDiv(mm
, TWIPS_PER_INCH
, CENTMM_PER_INCH
);
177 static LONG
twips_to_pixels(int twips
, int dpi
)
179 return MulDiv(twips
, dpi
, TWIPS_PER_INCH
);
182 static LONG
devunits_to_twips(int units
, int dpi
)
184 return MulDiv(units
, TWIPS_PER_INCH
, dpi
);
188 static RECT
get_print_rect(HDC hdc
)
195 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
196 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
197 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
198 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
201 width
= centmm_to_twips(18500);
202 height
= centmm_to_twips(27000);
205 rc
.left
= margins
.left
;
206 rc
.right
= width
- margins
.right
;
207 rc
.top
= margins
.top
;
208 rc
.bottom
= height
- margins
.bottom
;
213 void target_device(HWND hMainWnd
, DWORD wordWrap
)
215 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
217 if(wordWrap
== ID_WORDWRAP_MARGIN
)
222 RECT rc
= get_print_rect(hdc
);
224 width
= rc
.right
- rc
.left
;
227 HDC hMaindc
= GetDC(hMainWnd
);
228 hdc
= CreateCompatibleDC(hMaindc
);
229 ReleaseDC(hMainWnd
, hMaindc
);
231 result
= SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
235 /* otherwise EM_SETTARGETDEVICE failed, so fall back on wrapping
236 * to window using the NULL DC. */
239 if (wordWrap
!= ID_WORDWRAP_NONE
) {
240 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 0);
242 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, 0, 1);
247 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
250 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
251 static const WCHAR defExt
[] = {'P','R','N',0};
252 static LPWSTR file_filter
;
255 file_filter
= get_print_file_filter(hMainWnd
);
257 ZeroMemory(&ofn
, sizeof(ofn
));
259 ofn
.lStructSize
= sizeof(ofn
);
260 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
261 ofn
.hwndOwner
= hMainWnd
;
262 ofn
.lpstrFilter
= file_filter
;
263 ofn
.lpstrFile
= file
;
264 ofn
.nMaxFile
= MAX_PATH
;
265 ofn
.lpstrDefExt
= defExt
;
267 if(GetSaveFileNameW(&ofn
))
273 static void char_from_pagenum(HWND hEditorWnd
, FORMATRANGE
*fr
, int page
)
279 for(i
= 1; i
< page
; i
++)
281 int bottom
= fr
->rc
.bottom
;
282 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, (LPARAM
)fr
);
283 fr
->rc
.bottom
= bottom
;
287 static HWND
get_ruler_wnd(HWND hMainWnd
)
289 return GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
292 void redraw_ruler(HWND hRulerWnd
)
296 GetClientRect(hRulerWnd
, &rc
);
297 InvalidateRect(hRulerWnd
, &rc
, TRUE
);
300 static void update_ruler(HWND hRulerWnd
)
302 SendMessageW(hRulerWnd
, WM_USER
, 0, 0);
303 redraw_ruler(hRulerWnd
);
306 static void add_ruler_units(HDC hdcRuler
, RECT
* drawRect
, BOOL NewMetrics
, LONG EditLeftmost
)
312 static HBITMAP hBitmap
;
313 int i
, x
, y
, RulerTextEnd
;
317 WCHAR FontName
[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
322 DeleteObject(hBitmap
);
325 hdc
= CreateCompatibleDC(0);
327 CmPixels
= twips_to_pixels(centmm_to_twips(1000), GetDeviceCaps(hdc
, LOGPIXELSX
));
328 QuarterCmPixels
= (int)((float)CmPixels
/ 4.0);
330 hBitmap
= CreateCompatibleBitmap(hdc
, drawRect
->right
, drawRect
->bottom
);
331 SelectObject(hdc
, hBitmap
);
332 FillRect(hdc
, drawRect
, GetStockObject(WHITE_BRUSH
));
334 hFont
= CreateFontW(10, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FontName
);
336 SelectObject(hdc
, hFont
);
337 SetBkMode(hdc
, TRANSPARENT
);
338 SetTextAlign(hdc
, TA_CENTER
);
339 y
= (int)(((float)drawRect
->bottom
- (float)drawRect
->top
) / 2.0) + 1;
340 RulerTextEnd
= drawRect
->right
- EditLeftmost
+ 1;
341 for(i
= 1, x
= EditLeftmost
; x
< (drawRect
->right
- EditLeftmost
+ 1); i
++)
344 WCHAR format
[] = {'%','d',0};
347 x2
+= QuarterCmPixels
;
348 if(x2
> RulerTextEnd
)
351 MoveToEx(hdc
, x2
, y
, NULL
);
352 LineTo(hdc
, x2
, y
+2);
354 x2
+= QuarterCmPixels
;
355 if(x2
> RulerTextEnd
)
358 MoveToEx(hdc
, x2
, y
- 3, NULL
);
359 LineTo(hdc
, x2
, y
+ 3);
361 x2
+= QuarterCmPixels
;
362 if(x2
> RulerTextEnd
)
365 MoveToEx(hdc
, x2
, y
, NULL
);
366 LineTo(hdc
, x2
, y
+2);
372 wsprintfW(str
, format
, i
);
373 TextOutW(hdc
, x
, 5, str
, lstrlenW(str
));
378 BitBlt(hdcRuler
, 0, 0, drawRect
->right
, drawRect
->bottom
, hdc
, 0, 0, SRCAND
);
381 static void paint_ruler(HWND hWnd
, LONG EditLeftmost
, BOOL NewMetrics
)
384 HDC hdc
= BeginPaint(hWnd
, &ps
);
385 HDC hdcPrint
= make_dc();
386 RECT printRect
= get_print_rect(hdcPrint
);
388 HBRUSH hBrush
= CreateSolidBrush(GetSysColor(COLOR_MENU
));
390 GetClientRect(hWnd
, &drawRect
);
391 FillRect(hdc
, &drawRect
, hBrush
);
394 drawRect
.bottom
-= 3;
395 drawRect
.left
= EditLeftmost
;
396 drawRect
.right
= twips_to_pixels(printRect
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
397 FillRect(hdc
, &drawRect
, GetStockObject(WHITE_BRUSH
));
401 DrawEdge(hdc
, &drawRect
, EDGE_SUNKEN
, BF_RECT
);
403 drawRect
.left
= drawRect
.right
- 1;
404 drawRect
.right
= twips_to_pixels(printRect
.right
+ margins
.right
- margins
.left
, GetDeviceCaps(hdc
, LOGPIXELSX
));
405 DrawEdge(hdc
, &drawRect
, EDGE_ETCHED
, BF_RECT
);
409 add_ruler_units(hdc
, &drawRect
, NewMetrics
, EditLeftmost
);
411 SelectObject(hdc
, GetStockObject(BLACK_BRUSH
));
412 DeleteObject(hBrush
);
417 LRESULT CALLBACK
ruler_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
419 static WNDPROC pPrevRulerProc
;
420 static LONG EditLeftmost
;
421 static BOOL NewMetrics
;
428 EditLeftmost
= ((POINTL
*)wParam
)->x
;
429 pPrevRulerProc
= (WNDPROC
)lParam
;
435 paint_ruler(hWnd
, EditLeftmost
, NewMetrics
);
439 return CallWindowProcW(pPrevRulerProc
, hWnd
, msg
, wParam
, lParam
);
445 static void print(LPPRINTDLGW pd
, LPWSTR wszFileName
)
449 HWND hEditorWnd
= GetDlgItem(pd
->hwndOwner
, IDC_EDITOR
);
450 int printedPages
= 0;
453 fr
.hdcTarget
= pd
->hDC
;
455 fr
.rc
= get_print_rect(fr
.hdc
);
457 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
459 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
461 ZeroMemory(&di
, sizeof(di
));
462 di
.cbSize
= sizeof(di
);
463 di
.lpszDocName
= wszFileName
;
465 if(pd
->Flags
& PD_PRINTTOFILE
)
467 di
.lpszOutput
= dialog_print_to_file(pd
->hwndOwner
);
472 if(pd
->Flags
& PD_SELECTION
)
474 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
478 gt
.flags
= GTL_DEFAULT
;
481 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
483 if(pd
->Flags
& PD_PAGENUMS
)
484 char_from_pagenum(hEditorWnd
, &fr
, pd
->nToPage
);
487 StartDocW(fr
.hdc
, &di
);
490 if(StartPage(fr
.hdc
) <= 0)
493 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
495 if(EndPage(fr
.hdc
) <= 0)
499 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
502 while(fr
.chrg
.cpMin
&& fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
505 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
508 void dialog_printsetup(HWND hMainWnd
)
512 ZeroMemory(&ps
, sizeof(ps
));
513 ps
.lStructSize
= sizeof(ps
);
514 ps
.hwndOwner
= hMainWnd
;
515 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
516 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
517 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
518 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
519 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
520 ps
.hDevMode
= devMode
;
521 ps
.hDevNames
= devNames
;
523 if(PageSetupDlgW(&ps
))
525 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
526 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
527 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
528 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
529 devMode
= ps
.hDevMode
;
530 devNames
= ps
.hDevNames
;
531 update_ruler(get_ruler_wnd(hMainWnd
));
535 void get_default_printer_opts(void)
538 ZeroMemory(&pd
, sizeof(pd
));
540 ZeroMemory(&pd
, sizeof(pd
));
541 pd
.lStructSize
= sizeof(pd
);
542 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
543 pd
.hDevMode
= devMode
;
547 devMode
= pd
.hDevMode
;
548 devNames
= pd
.hDevNames
;
551 void print_quick(HWND hMainWnd
, LPWSTR wszFileName
)
555 ZeroMemory(&pd
, sizeof(pd
));
556 pd
.hwndOwner
= hMainWnd
;
559 print(&pd
, wszFileName
);
563 void dialog_print(HWND hMainWnd
, LPWSTR wszFileName
)
566 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
570 ZeroMemory(&pd
, sizeof(pd
));
571 pd
.lStructSize
= sizeof(pd
);
572 pd
.hwndOwner
= hMainWnd
;
573 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
576 pd
.hDevMode
= devMode
;
577 pd
.hDevNames
= devNames
;
579 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
581 pd
.Flags
|= PD_NOSELECTION
;
585 devMode
= pd
.hDevMode
;
586 devNames
= pd
.hDevNames
;
587 print(&pd
, wszFileName
);
588 update_ruler(get_ruler_wnd(hMainWnd
));
592 static void preview_bar_show(HWND hMainWnd
, BOOL show
)
594 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
601 UINT num_pages_string
= preview
.pages_shown
> 1 ? STRING_PREVIEW_ONEPAGE
:
602 STRING_PREVIEW_TWOPAGES
;
604 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
605 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
606 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
607 AddTextButton(hReBar
, num_pages_string
, ID_PREVIEW_NUMPAGES
, BANDID_PREVIEW_BTN4
);
608 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMIN
, ID_PREVIEW_ZOOMIN
, BANDID_PREVIEW_BTN5
);
609 AddTextButton(hReBar
, STRING_PREVIEW_ZOOMOUT
, ID_PREVIEW_ZOOMOUT
, BANDID_PREVIEW_BTN6
);
610 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN7
);
612 hStatic
= CreateWindowW(WC_STATICW
, NULL
,
613 WS_VISIBLE
| WS_CHILD
, 0, 0, 0, 0,
614 hReBar
, NULL
, NULL
, NULL
);
616 rb
.cbSize
= REBARBANDINFOW_V6_SIZE
;
617 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
618 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
619 rb
.hwndChild
= hStatic
;
620 rb
.cyChild
= rb
.cyMinChild
= 22;
621 rb
.cx
= rb
.cxMinChild
= 90;
623 rb
.wID
= BANDID_PREVIEW_BUFFER
;
625 SendMessageW(hReBar
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
628 for(i
= 0; i
<= PREVIEW_BUTTONS
; i
++)
629 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
633 static const int min_spacing
= 10;
635 static void update_preview_scrollbars(HWND hwndPreview
, RECT
*window
)
638 sbi
.cbSize
= sizeof(sbi
);
639 sbi
.fMask
= SIF_PAGE
|SIF_RANGE
;
641 if (preview
.zoomlevel
== 0)
643 /* Hide scrollbars when zoomed out. */
645 sbi
.nPage
= window
->right
;
646 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
647 sbi
.nPage
= window
->bottom
;
648 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
650 sbi
.nMax
= preview
.bmScaledSize
.cx
* preview
.pages_shown
+
651 min_spacing
* (preview
.pages_shown
+ 1);
652 sbi
.nPage
= window
->right
;
653 SetScrollInfo(hwndPreview
, SB_HORZ
, &sbi
, TRUE
);
654 /* Change in the horizontal scrollbar visibility affects the
655 * client rect, so update the client rect. */
656 GetClientRect(hwndPreview
, window
);
657 sbi
.nMax
= preview
.bmScaledSize
.cy
+ min_spacing
* 2;
658 sbi
.nPage
= window
->bottom
;
659 SetScrollInfo(hwndPreview
, SB_VERT
, &sbi
, TRUE
);
663 static void update_preview_sizes(HWND hwndPreview
, BOOL zoomLevelUpdated
)
667 GetClientRect(hwndPreview
, &window
);
669 /* The zoom ratio isn't updated for partial zoom because of resizing the window. */
670 if (zoomLevelUpdated
|| preview
.zoomlevel
!= 1)
672 float ratio
, ratioHeight
, ratioWidth
;
673 if (preview
.zoomlevel
== 2)
677 ratioHeight
= (window
.bottom
- min_spacing
* 2) / (float)preview
.bmSize
.cy
;
679 ratioWidth
= (float)(window
.right
-
680 min_spacing
* (preview
.pages_shown
+ 1)) /
681 (preview
.pages_shown
* preview
.bmSize
.cx
);
683 if(ratioWidth
> ratioHeight
)
688 if (preview
.zoomlevel
== 1)
689 ratio
+= (1.0 - ratio
) / 2;
691 preview
.zoomratio
= ratio
;
694 preview
.bmScaledSize
.cx
= preview
.bmSize
.cx
* preview
.zoomratio
;
695 preview
.bmScaledSize
.cy
= preview
.bmSize
.cy
* preview
.zoomratio
;
697 preview
.spacing
.cy
= max(min_spacing
, (window
.bottom
- preview
.bmScaledSize
.cy
) / 2);
699 preview
.spacing
.cx
= (window
.right
-
700 preview
.bmScaledSize
.cx
* preview
.pages_shown
) /
701 (preview
.pages_shown
+ 1);
702 if (preview
.spacing
.cx
< min_spacing
)
703 preview
.spacing
.cx
= min_spacing
;
705 update_preview_scrollbars(hwndPreview
, &window
);
708 static void draw_preview_page(HDC hdc
, HDC
* hdcSized
, FORMATRANGE
* lpFr
, float ratio
, int bmNewWidth
, int bmNewHeight
, int bmWidth
, int bmHeight
, BOOL draw_margins
)
710 HBITMAP hBitmapScaled
= CreateCompatibleBitmap(hdc
, bmNewWidth
, bmNewHeight
);
713 int TopMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.top
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
714 int BottomMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSX
)) * ratio
);
715 int LeftMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.left
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
716 int RightMargin
= (int)((float)twips_to_pixels(lpFr
->rc
.right
, GetDeviceCaps(hdc
, LOGPIXELSY
)) * ratio
);
719 oldbm
= SelectObject(*hdcSized
, hBitmapScaled
);
722 *hdcSized
= CreateCompatibleDC(hdc
);
723 SelectObject(*hdcSized
, hBitmapScaled
);
726 StretchBlt(*hdcSized
, 0, 0, bmNewWidth
, bmNewHeight
, hdc
, 0, 0, bmWidth
, bmHeight
, SRCCOPY
);
728 if (!draw_margins
) return;
730 /* Draw margin lines */
731 hPen
= CreatePen(PS_DOT
, 1, RGB(0,0,0));
732 oldPen
= SelectObject(*hdcSized
, hPen
);
734 MoveToEx(*hdcSized
, 0, TopMargin
, NULL
);
735 LineTo(*hdcSized
, bmNewWidth
, TopMargin
);
736 MoveToEx(*hdcSized
, 0, BottomMargin
, NULL
);
737 LineTo(*hdcSized
, bmNewWidth
, BottomMargin
);
739 MoveToEx(*hdcSized
, LeftMargin
, 0, NULL
);
740 LineTo(*hdcSized
, LeftMargin
, bmNewHeight
);
741 MoveToEx(*hdcSized
, RightMargin
, 0, NULL
);
742 LineTo(*hdcSized
, RightMargin
, bmNewHeight
);
744 SelectObject(*hdcSized
, oldPen
);
748 static BOOL
is_last_preview_page(int page
)
750 return preview
.pageEnds
[page
- 1] >= preview
.textlength
;
753 /* Update for zoom ratio changes with same page. */
754 static void update_scaled_preview(HWND hMainWnd
)
759 /* This may occur on WM_CREATE before update_preview is called
760 * because a WM_SIZE message is generated from updating the
762 if (!preview
.hdc
) return;
764 hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
765 fr
.hdcTarget
= make_dc();
766 fr
.rc
= fr
.rcPage
= preview
.rcPage
;
767 fr
.rc
.left
+= margins
.left
;
768 fr
.rc
.top
+= margins
.top
;
769 fr
.rc
.bottom
-= margins
.bottom
;
770 fr
.rc
.right
-= margins
.right
;
772 draw_preview_page(preview
.hdc
, &preview
.hdcSized
, &fr
, preview
.zoomratio
,
773 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
774 preview
.bmSize
.cx
, preview
.bmSize
.cy
, TRUE
);
776 if(preview
.pages_shown
> 1)
778 draw_preview_page(preview
.hdc2
, &preview
.hdcSized2
, &fr
, preview
.zoomratio
,
779 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
780 preview
.bmSize
.cx
, preview
.bmSize
.cy
,
781 !is_last_preview_page(preview
.page
));
784 InvalidateRect(hwndPreview
, NULL
, TRUE
);
785 DeleteDC(fr
.hdcTarget
);
788 void init_preview(HWND hMainWnd
, LPWSTR wszFileName
)
790 HINSTANCE hInstance
= GetModuleHandleW(0);
794 preview
.wszFileName
= wszFileName
;
795 preview
.zoomratio
= 0;
796 preview
.zoomlevel
= 0;
797 preview_bar_show(hMainWnd
, TRUE
);
799 CreateWindowExW(0, wszPreviewWndClass
, NULL
,
800 WS_VISIBLE
| WS_CHILD
| WS_VSCROLL
| WS_HSCROLL
,
801 0, 0, 200, 10, hMainWnd
, (HMENU
)IDC_PREVIEW
, hInstance
, NULL
);
804 void close_preview(HWND hMainWnd
)
806 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
807 preview
.window
.right
= 0;
808 preview
.window
.bottom
= 0;
810 HeapFree(GetProcessHeap(), 0, preview
.pageEnds
);
811 preview
.pageEnds
= NULL
;
812 preview
.pageCapacity
= 0;
813 if (preview
.zoomlevel
> 0)
814 preview
.pages_shown
= preview
.saved_pages_shown
;
816 HBITMAP oldbm
= GetCurrentObject(preview
.hdc
, OBJ_BITMAP
);
817 DeleteDC(preview
.hdc
);
822 HBITMAP oldbm
= GetCurrentObject(preview
.hdc2
, OBJ_BITMAP
);
823 DeleteDC(preview
.hdc2
);
827 if(preview
.hdcSized
) {
828 HBITMAP oldbm
= GetCurrentObject(preview
.hdcSized
, OBJ_BITMAP
);
829 DeleteDC(preview
.hdcSized
);
831 preview
.hdcSized
= NULL
;
833 if(preview
.hdcSized2
) {
834 HBITMAP oldbm
= GetCurrentObject(preview
.hdcSized2
, OBJ_BITMAP
);
835 DeleteDC(preview
.hdcSized2
);
837 preview
.hdcSized2
= NULL
;
840 preview_bar_show(hMainWnd
, FALSE
);
841 DestroyWindow(hwndPreview
);
844 BOOL
preview_isactive(void)
846 return preview
.page
!= 0;
849 static void draw_preview(HWND hEditorWnd
, FORMATRANGE
* lpFr
, RECT
* paper
, int page
)
853 if (!preview
.pageEnds
)
855 preview
.pageCapacity
= 32;
856 preview
.pageEnds
= HeapAlloc(GetProcessHeap(), 0,
857 sizeof(int) * preview
.pageCapacity
);
858 if (!preview
.pageEnds
) return;
859 } else if (page
>= preview
.pageCapacity
) {
861 new_buffer
= HeapReAlloc(GetProcessHeap(), 0, preview
.pageEnds
,
862 sizeof(int) * preview
.pageCapacity
* 2);
863 if (!new_buffer
) return;
864 preview
.pageCapacity
*= 2;
865 preview
.pageEnds
= new_buffer
;
868 FillRect(lpFr
->hdc
, paper
, GetStockObject(WHITE_BRUSH
));
869 if (page
> 1 && is_last_preview_page(page
- 1)) return;
870 lpFr
->chrg
.cpMin
= page
<= 1 ? 0 : preview
.pageEnds
[page
-2];
871 bottom
= lpFr
->rc
.bottom
;
872 preview
.pageEnds
[page
-1] = SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)lpFr
);
874 /* EM_FORMATRANGE sets fr.rc.bottom to indicate the area printed in,
875 * but we want to keep the original for drawing margins */
876 lpFr
->rc
.bottom
= bottom
;
877 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
880 static void update_preview_buttons(HWND hMainWnd
)
882 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
883 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
884 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
),
885 !is_last_preview_page(preview
.page
) &&
886 !is_last_preview_page(preview
.page
+ preview
.pages_shown
- 1));
887 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
),
888 preview
.pages_shown
> 1 ||
889 (!is_last_preview_page(1) && preview
.zoomlevel
== 0));
890 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMIN
), preview
.zoomlevel
< 2);
891 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_ZOOMOUT
), preview
.zoomlevel
> 0);
894 static LRESULT
print_preview(HWND hwndPreview
)
897 RECT window
, background
;
901 hdc
= BeginPaint(hwndPreview
, &ps
);
902 GetClientRect(hwndPreview
, &window
);
904 FillRect(hdc
, &window
, GetStockObject(GRAY_BRUSH
));
906 scrollpos
.x
= GetScrollPos(hwndPreview
, SB_HORZ
);
907 scrollpos
.y
= GetScrollPos(hwndPreview
, SB_VERT
);
909 background
.left
= preview
.spacing
.cx
- 2 - scrollpos
.x
;
910 background
.right
= background
.left
+ preview
.bmScaledSize
.cx
+ 4;
911 background
.top
= preview
.spacing
.cy
- 2 - scrollpos
.y
;
912 background
.bottom
= background
.top
+ preview
.bmScaledSize
.cy
+ 4;
914 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
916 if(preview
.pages_shown
> 1)
918 background
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
919 background
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
921 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
924 BitBlt(hdc
, preview
.spacing
.cx
- scrollpos
.x
, preview
.spacing
.cy
- scrollpos
.y
,
925 preview
.bmScaledSize
.cx
, preview
.bmScaledSize
.cy
,
926 preview
.hdcSized
, 0, 0, SRCCOPY
);
928 if(preview
.pages_shown
> 1)
930 BitBlt(hdc
, preview
.spacing
.cx
* 2 + preview
.bmScaledSize
.cx
- scrollpos
.x
,
931 preview
.spacing
.cy
- scrollpos
.y
, preview
.bmScaledSize
.cx
,
932 preview
.bmScaledSize
.cy
, preview
.hdcSized2
, 0, 0, SRCCOPY
);
935 preview
.window
= window
;
937 EndPaint(hwndPreview
, &ps
);
942 static void update_preview_statusbar(HWND hMainWnd
)
944 HWND hStatusbar
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
945 HINSTANCE hInst
= GetModuleHandleW(0);
947 WCHAR wstr
[MAX_STRING_LEN
];
950 if (preview
.pages_shown
< 2 || is_last_preview_page(preview
.page
))
952 static const WCHAR fmt
[] = {' ','%','d','\0'};
953 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGE
, wstr
, MAX_STRING_LEN
);
954 wsprintfW(p
, fmt
, preview
.page
);
956 static const WCHAR fmt
[] = {' ','%','d','-','%','d','\0'};
957 p
+= LoadStringW(hInst
, STRING_PREVIEW_PAGES
, wstr
, MAX_STRING_LEN
);
958 wsprintfW(p
, fmt
, preview
.page
, preview
.page
+ 1);
960 SetWindowTextW(hStatusbar
, wstr
);
963 /* Update for page changes. */
964 static void update_preview(HWND hMainWnd
)
967 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
968 HWND hwndPreview
= GetDlgItem(hMainWnd
, IDC_PREVIEW
);
969 HBITMAP hBitmapCapture
;
971 HDC hdc
= GetDC(hwndPreview
);
973 fr
.hdcTarget
= make_dc();
974 fr
.rc
= fr
.rcPage
= preview
.rcPage
;
975 fr
.rc
.left
+= margins
.left
;
976 fr
.rc
.top
+= margins
.top
;
977 fr
.rc
.bottom
-= margins
.bottom
;
978 fr
.rc
.right
-= margins
.right
;
981 fr
.chrg
.cpMax
= preview
.textlength
;
984 paper
.right
= preview
.bmSize
.cx
;
986 paper
.bottom
= preview
.bmSize
.cy
;
989 preview
.hdc
= CreateCompatibleDC(hdc
);
990 hBitmapCapture
= CreateCompatibleBitmap(hdc
, preview
.bmSize
.cx
, preview
.bmSize
.cy
);
991 SelectObject(preview
.hdc
, hBitmapCapture
);
994 fr
.hdc
= preview
.hdc
;
995 draw_preview(hEditorWnd
, &fr
, &paper
, preview
.page
);
997 if(preview
.pages_shown
> 1)
1001 preview
.hdc2
= CreateCompatibleDC(hdc
);
1002 hBitmapCapture
= CreateCompatibleBitmap(hdc
,
1005 SelectObject(preview
.hdc2
, hBitmapCapture
);
1008 fr
.hdc
= preview
.hdc2
;
1009 draw_preview(hEditorWnd
, &fr
, &fr
.rcPage
, preview
.page
+ 1);
1011 DeleteDC(fr
.hdcTarget
);
1012 ReleaseDC(hwndPreview
, hdc
);
1014 update_scaled_preview(hMainWnd
);
1015 update_preview_buttons(hMainWnd
);
1016 update_preview_statusbar(hMainWnd
);
1019 static void toggle_num_pages(HWND hMainWnd
)
1021 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1022 WCHAR name
[MAX_STRING_LEN
];
1023 HINSTANCE hInst
= GetModuleHandleW(0);
1026 preview
.pages_shown
= preview
.pages_shown
> 1 ? 1 : 2;
1028 nPreviewPages
= preview
.zoomlevel
> 0 ? preview
.saved_pages_shown
:
1029 preview
.pages_shown
;
1031 LoadStringW(hInst
, nPreviewPages
> 1 ? STRING_PREVIEW_ONEPAGE
:
1032 STRING_PREVIEW_TWOPAGES
,
1033 name
, MAX_STRING_LEN
);
1035 SetWindowTextW(GetDlgItem(hReBar
, ID_PREVIEW_NUMPAGES
), name
);
1036 update_preview_sizes(GetDlgItem(hMainWnd
, IDC_PREVIEW
), TRUE
);
1037 update_preview(hMainWnd
);
1040 /* Returns the page shown that the point is in (1 or 2) or 0 if the point
1041 * isn't inside either page */
1042 static int preview_page_hittest(POINT pt
)
1045 rc
.left
= preview
.spacing
.cx
;
1046 rc
.right
= rc
.left
+ preview
.bmScaledSize
.cx
;
1047 rc
.top
= preview
.spacing
.cy
;
1048 rc
.bottom
= rc
.top
+ preview
.bmScaledSize
.cy
;
1049 if (PtInRect(&rc
, pt
))
1052 if (preview
.pages_shown
<= 1)
1055 rc
.left
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1056 rc
.right
+= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1057 if (PtInRect(&rc
, pt
))
1058 return is_last_preview_page(preview
.page
) ? 1 : 2;
1063 LRESULT CALLBACK
preview_proc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1069 HWND hMainWnd
= GetParent(hWnd
);
1070 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
1072 GETTEXTLENGTHEX gt
= {GTL_DEFAULT
, 1200};
1073 HDC hdc
= GetDC(hWnd
);
1074 HDC hdcTarget
= make_dc();
1076 fr
.rc
= preview
.rcPage
= get_print_rect(hdcTarget
);
1077 preview
.rcPage
.bottom
+= margins
.bottom
;
1078 preview
.rcPage
.right
+= margins
.right
;
1079 preview
.rcPage
.top
= preview
.rcPage
.left
= 0;
1080 fr
.rcPage
= preview
.rcPage
;
1082 preview
.bmSize
.cx
= twips_to_pixels(preview
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
1083 preview
.bmSize
.cy
= twips_to_pixels(preview
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
1085 preview
.textlength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1087 fr
.hdc
= CreateCompatibleDC(hdc
);
1088 fr
.hdcTarget
= hdcTarget
;
1090 fr
.chrg
.cpMax
= preview
.textlength
;
1092 DeleteDC(hdcTarget
);
1093 ReleaseDC(hWnd
, hdc
);
1095 update_preview_sizes(hWnd
, TRUE
);
1096 update_preview(hMainWnd
);
1101 return print_preview(hWnd
);
1105 update_preview_sizes(hWnd
, FALSE
);
1106 update_scaled_preview(hWnd
);
1115 int nBar
= (msg
== WM_VSCROLL
) ? SB_VERT
: SB_HORZ
;
1118 GetClientRect(hWnd
, &rc
);
1119 si
.cbSize
= sizeof(si
);
1121 GetScrollInfo(hWnd
, nBar
, &si
);
1123 switch(LOWORD(wParam
))
1125 case SB_TOP
: /* == SB_LEFT */
1128 case SB_BOTTOM
: /* == SB_RIGHT */
1131 case SB_LINEUP
: /* == SB_LINELEFT */
1132 si
.nPos
-= si
.nPage
/ 10;
1134 case SB_LINEDOWN
: /* == SB_LINERIGHT */
1135 si
.nPos
+= si
.nPage
/ 10;
1137 case SB_PAGEUP
: /* == SB_PAGELEFT */
1138 si
.nPos
-= si
.nPage
;
1140 case SB_PAGEDOWN
: /* SB_PAGERIGHT */
1141 si
.nPos
+= si
.nPage
;
1144 si
.nPos
= si
.nTrackPos
;
1148 SetScrollInfo(hWnd
, nBar
, &si
, TRUE
);
1149 GetScrollInfo(hWnd
, nBar
, &si
);
1150 if (si
.nPos
!= origPos
)
1152 int amount
= origPos
- si
.nPos
;
1153 if (msg
== WM_VSCROLL
)
1154 ScrollWindow(hWnd
, 0, amount
, NULL
, NULL
);
1156 ScrollWindow(hWnd
, amount
, 0, NULL
, NULL
);
1165 int bHittest
= FALSE
;
1166 DWORD messagePos
= GetMessagePos();
1167 pt
.x
= (short)LOWORD(messagePos
);
1168 pt
.y
= (short)HIWORD(messagePos
);
1169 ScreenToClient(hWnd
, &pt
);
1171 GetClientRect(hWnd
, &rc
);
1172 if (PtInRect(&rc
, pt
))
1174 pt
.x
+= GetScrollPos(hWnd
, SB_HORZ
);
1175 pt
.y
+= GetScrollPos(hWnd
, SB_VERT
);
1176 bHittest
= preview_page_hittest(pt
);
1180 SetCursor(LoadCursorW(GetModuleHandleW(0),
1181 MAKEINTRESOURCEW(IDC_ZOOM
)));
1183 SetCursor(LoadCursorW(NULL
, (WCHAR
*)IDC_ARROW
));
1188 case WM_LBUTTONDOWN
:
1192 pt
.x
= (short)LOWORD(lParam
) + GetScrollPos(hWnd
, SB_HORZ
);
1193 pt
.y
= (short)HIWORD(lParam
) + GetScrollPos(hWnd
, SB_VERT
);
1194 if ((page
= preview_page_hittest(pt
)) > 0)
1196 HWND hMainWnd
= GetParent(hWnd
);
1198 /* Convert point from client coordinate to unzoomed page
1200 pt
.x
-= preview
.spacing
.cx
;
1202 pt
.x
-= preview
.bmScaledSize
.cx
+ preview
.spacing
.cx
;
1203 pt
.y
-= preview
.spacing
.cy
;
1204 pt
.x
/= preview
.zoomratio
;
1205 pt
.y
/= preview
.zoomratio
;
1207 if (preview
.zoomlevel
== 0)
1208 preview
.saved_pages_shown
= preview
.pages_shown
;
1209 preview
.zoomlevel
= (preview
.zoomlevel
+ 1) % 3;
1210 preview
.zoomratio
= 0;
1211 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1)
1213 toggle_num_pages(hMainWnd
);
1214 } else if (preview
.pages_shown
> 1) {
1215 if (page
>= 2) preview
.page
++;
1216 toggle_num_pages(hMainWnd
);
1218 update_preview_sizes(hWnd
, TRUE
);
1219 update_scaled_preview(hMainWnd
);
1220 update_preview_buttons(hMainWnd
);
1223 if (preview
.zoomlevel
> 0) {
1225 /* Convert the coordinate back to client coordinate. */
1226 pt
.x
*= preview
.zoomratio
;
1227 pt
.y
*= preview
.zoomratio
;
1228 pt
.x
+= preview
.spacing
.cx
;
1229 pt
.y
+= preview
.spacing
.cy
;
1230 /* Scroll to center view at that point on the page */
1231 si
.cbSize
= sizeof(si
);
1232 si
.fMask
= SIF_PAGE
;
1233 GetScrollInfo(hWnd
, SB_HORZ
, &si
);
1234 pt
.x
-= si
.nPage
/ 2;
1235 SetScrollPos(hWnd
, SB_HORZ
, pt
.x
, TRUE
);
1236 GetScrollInfo(hWnd
, SB_VERT
, &si
);
1237 pt
.y
-= si
.nPage
/ 2;
1238 SetScrollPos(hWnd
, SB_VERT
, pt
.y
, TRUE
);
1244 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1250 LRESULT
preview_command(HWND hWnd
, WPARAM wParam
)
1252 switch(LOWORD(wParam
))
1255 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
1258 case ID_PREVIEW_NEXTPAGE
:
1259 case ID_PREVIEW_PREVPAGE
:
1261 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1266 update_preview(hWnd
);
1270 case ID_PREVIEW_NUMPAGES
:
1271 toggle_num_pages(hWnd
);
1274 case ID_PREVIEW_ZOOMIN
:
1275 if (preview
.zoomlevel
< 2)
1277 if (preview
.zoomlevel
== 0)
1278 preview
.saved_pages_shown
= preview
.pages_shown
;
1279 preview
.zoomlevel
++;
1280 preview
.zoomratio
= 0;
1281 if (preview
.pages_shown
> 1)
1283 /* Forced switch to one page when zooming in. */
1284 toggle_num_pages(hWnd
);
1286 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1287 update_preview_sizes(hwndPreview
, TRUE
);
1288 update_scaled_preview(hWnd
);
1289 update_preview_buttons(hWnd
);
1294 case ID_PREVIEW_ZOOMOUT
:
1295 if (preview
.zoomlevel
> 0)
1297 HWND hwndPreview
= GetDlgItem(hWnd
, IDC_PREVIEW
);
1298 preview
.zoomlevel
--;
1299 preview
.zoomratio
= 0;
1300 if (preview
.zoomlevel
== 0 && preview
.saved_pages_shown
> 1) {
1301 toggle_num_pages(hWnd
);
1303 update_preview_sizes(hwndPreview
, TRUE
);
1304 update_scaled_preview(hWnd
);
1305 update_preview_buttons(hWnd
);
1311 dialog_print(hWnd
, preview
.wszFileName
);
1312 SendMessageW(hWnd
, WM_CLOSE
, 0, 0);