msxml3: Fold in reset_output_buffer.
[wine/zf.git] / programs / wordpad / print.c
blobacc583eefc522626dc7a9b880223689b0601aa48
1 /*
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
21 #include <windows.h>
22 #include <richedit.h>
23 #include <commctrl.h>
24 #include <commdlg.h>
26 #include "wordpad.h"
28 typedef struct _previewinfo
30 int page;
31 int pages_shown;
32 int saved_pages_shown;
33 int *pageEnds, pageCapacity;
34 int textlength;
35 HDC hdc;
36 HDC hdc2;
37 RECT window;
38 RECT rcPage;
39 SIZE bmSize;
40 SIZE bmScaledSize;
41 SIZE spacing;
42 float zoomratio;
43 int zoomlevel;
44 LPWSTR wszFileName;
45 } previewinfo, *ppreviewinfo;
47 static HGLOBAL devMode;
48 static HGLOBAL devNames;
50 static RECT margins;
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'};
63 LPWSTR p;
64 HINSTANCE hInstance = GetModuleHandleW(0);
66 p = wszPrintFilter;
67 LoadStringW(hInstance, STRING_PRINTER_FILES_PRN, p, MAX_STRING_LEN);
68 p += lstrlenW(p) + 1;
69 lstrcpyW(p, files_prn);
70 p += lstrlenW(p) + 1;
71 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
72 p += lstrlenW(p) + 1;
73 lstrcpyW(p, files_all);
74 p += lstrlenW(p) + 1;
75 *p = 0;
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);
103 if(!hKey ||
104 RegQueryValueExW(hKey, var_previewpages, 0, NULL,
105 (LPBYTE)&preview.pages_shown, &size) != ERROR_SUCCESS ||
106 size != sizeof(DWORD))
108 preview.pages_shown = 1;
109 } else {
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)
118 REBARBANDINFOW rb;
119 HINSTANCE hInstance = GetModuleHandleW(0);
120 WCHAR text[MAX_STRING_LEN];
121 HWND hButton;
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;
134 rb.cxIdeal = 100;
135 rb.wID = id;
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);
146 HDC ret;
148 ret = CreateDCW((LPWSTR)dn + dn->wDriverOffset,
149 (LPWSTR)dn + dn->wDeviceOffset, 0, dm);
151 GlobalUnlock(dn);
152 GlobalUnlock(dm);
154 return ret;
155 } else
157 return 0;
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)
184 RECT rc;
185 int width, height;
187 if(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);
193 } else
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);
201 return rc;
204 void target_device(HWND hMainWnd, DWORD wordWrap)
206 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
208 if(wordWrap == ID_WORDWRAP_MARGIN)
210 int width = 0;
211 LRESULT result;
212 HDC hdc = make_dc();
213 RECT rc = get_print_rect(hdc);
215 width = rc.right - rc.left;
216 if(!hdc)
218 HDC hMaindc = GetDC(hMainWnd);
219 hdc = CreateCompatibleDC(hMaindc);
220 ReleaseDC(hMainWnd, hMaindc);
222 result = SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, (WPARAM)hdc, width);
223 DeleteDC(hdc);
224 if (result)
225 return;
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);
232 } else {
233 SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, 0, 1);
238 static LPWSTR dialog_print_to_file(HWND hMainWnd)
240 OPENFILENAMEW ofn;
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;
245 if(!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))
259 return file;
260 else
261 return FALSE;
264 static void char_from_pagenum(HWND hEditorWnd, FORMATRANGE *fr, int page)
266 int i;
268 fr->chrg.cpMin = 0;
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)
285 RECT rc;
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)
299 static HDC hdc;
301 if(NewMetrics)
303 static HBITMAP hBitmap;
304 int i, x, y, RulerTextEnd;
305 int CmPixels;
306 int QuarterCmPixels;
307 HFONT hFont;
308 WCHAR FontName[] = {'M','S',' ','S','a','n','s',' ','S','e','r','i','f',0};
310 if(hdc)
312 DeleteDC(hdc);
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 ++)
334 WCHAR str[3];
335 WCHAR format[] = {'%','d',0};
336 int x2 = x;
338 x2 += QuarterCmPixels;
339 if(x2 > RulerTextEnd)
340 break;
342 MoveToEx(hdc, x2, y, NULL);
343 LineTo(hdc, x2, y+2);
345 x2 += QuarterCmPixels;
346 if(x2 > RulerTextEnd)
347 break;
349 MoveToEx(hdc, x2, y - 3, NULL);
350 LineTo(hdc, x2, y + 3);
352 x2 += QuarterCmPixels;
353 if(x2 > RulerTextEnd)
354 break;
356 MoveToEx(hdc, x2, y, NULL);
357 LineTo(hdc, x2, y+2);
359 x += CmPixels;
360 if(x > RulerTextEnd)
361 break;
363 wsprintfW(str, format, i);
364 TextOutW(hdc, x, 5, str, lstrlenW(str));
366 DeleteObject(hFont);
369 BitBlt(hdcRuler, 0, 0, drawRect->right, drawRect->bottom, hdc, 0, 0, SRCAND);
372 static void paint_ruler(HWND hWnd, LONG EditLeftmost, BOOL NewMetrics)
374 PAINTSTRUCT ps;
375 HDC hdc = BeginPaint(hWnd, &ps);
376 HDC hdcPrint = make_dc();
377 RECT printRect = get_print_rect(hdcPrint);
378 RECT drawRect;
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));
388 drawRect.top--;
389 drawRect.bottom++;
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);
396 drawRect.left = 0;
397 drawRect.top = 0;
398 add_ruler_units(hdc, &drawRect, NewMetrics, EditLeftmost);
400 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
401 DeleteDC(hdcPrint);
402 EndPaint(hWnd, &ps);
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;
411 switch(msg)
413 case WM_USER:
414 if(wParam)
416 EditLeftmost = ((POINTL*)wParam)->x;
417 pPrevRulerProc = (WNDPROC)lParam;
419 NewMetrics = TRUE;
420 break;
422 case WM_PAINT:
423 paint_ruler(hWnd, EditLeftmost, NewMetrics);
424 break;
426 default:
427 return CallWindowProcW(pPrevRulerProc, hWnd, msg, wParam, lParam);
430 return 0;
433 static void print(LPPRINTDLGW pd, LPWSTR wszFileName)
435 FORMATRANGE fr;
436 DOCINFOW di;
437 HWND hEditorWnd = GetDlgItem(pd->hwndOwner, IDC_EDITOR);
438 int printedPages = 0;
440 fr.hdc = pd->hDC;
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);
453 if(!di.lpszOutput)
454 return;
457 if(pd->Flags & PD_SELECTION)
459 SendMessageW(hEditorWnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
460 } else
462 GETTEXTLENGTHEX gt;
463 gt.flags = GTL_DEFAULT;
464 gt.codepage = 1200;
465 fr.chrg.cpMin = 0;
466 fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 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)
476 break;
478 fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
480 if(EndPage(fr.hdc) <= 0)
481 break;
483 printedPages++;
484 if((pd->Flags & PD_PAGENUMS) && (printedPages > (pd->nToPage - pd->nFromPage)))
485 break;
487 while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
489 EndDoc(fr.hdc);
490 SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
493 void dialog_printsetup(HWND hMainWnd)
495 PAGESETUPDLGW ps;
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)
518 PRINTDLGW pd;
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;
526 PrintDlgW(&pd);
528 devMode = pd.hDevMode;
529 devNames = pd.hDevNames;
532 void print_quick(HWND hMainWnd, LPWSTR wszFileName)
534 PRINTDLGW pd;
536 ZeroMemory(&pd, sizeof(pd));
537 pd.hwndOwner = hMainWnd;
538 pd.hDC = make_dc();
540 print(&pd, wszFileName);
541 DeleteDC(pd.hDC);
544 void dialog_print(HWND hMainWnd, LPWSTR wszFileName)
546 PRINTDLGW pd;
547 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
548 int from = 0;
549 int to = 0;
551 ZeroMemory(&pd, sizeof(pd));
552 pd.lStructSize = sizeof(pd);
553 pd.hwndOwner = hMainWnd;
554 pd.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
555 pd.nMinPage = 1;
556 pd.nMaxPage = -1;
557 pd.hDevMode = devMode;
558 pd.hDevNames = devNames;
560 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
561 if(from == to)
562 pd.Flags |= PD_NOSELECTION;
564 if(PrintDlgW(&pd))
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);
576 int i;
578 if(show)
580 REBARBANDINFOW rb;
581 HWND hStatic;
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;
603 rb.cxIdeal = 100;
604 rb.wID = BANDID_PREVIEW_BUFFER;
606 SendMessageW(hReBar, RB_INSERTBANDW, -1, (LPARAM)&rb);
607 } else
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)
618 SCROLLINFO sbi;
619 sbi.cbSize = sizeof(sbi);
620 sbi.fMask = SIF_PAGE|SIF_RANGE;
621 sbi.nMin = 0;
622 if (preview.zoomlevel == 0)
624 /* Hide scrollbars when zoomed out. */
625 sbi.nMax = 0;
626 sbi.nPage = window->right;
627 SetScrollInfo(hwndPreview, SB_HORZ, &sbi, TRUE);
628 sbi.nPage = window->bottom;
629 SetScrollInfo(hwndPreview, SB_VERT, &sbi, TRUE);
630 } else {
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)
646 RECT window;
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)
656 ratio = 1.0;
657 } else {
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)
665 ratio = ratioHeight;
666 else
667 ratio = ratioWidth;
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)
691 HPEN hPen, oldPen;
692 SIZE dpi;
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);
722 DeleteObject(hPen);
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);
733 preview.page = 1;
734 preview.hdc = 0;
735 preview.hdc2 = 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;
751 preview.page = 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;
757 if(preview.hdc) {
758 HBITMAP oldbm = GetCurrentObject(preview.hdc, OBJ_BITMAP);
759 DeleteDC(preview.hdc);
760 DeleteObject(oldbm);
761 preview.hdc = NULL;
763 if(preview.hdc2) {
764 HBITMAP oldbm = GetCurrentObject(preview.hdc2, OBJ_BITMAP);
765 DeleteDC(preview.hdc2);
766 DeleteObject(oldbm);
767 preview.hdc2 = NULL;
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)
781 int bottom;
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) {
790 int *new_buffer;
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)
826 HPEN hPen, oldPen;
827 HDC hdc;
828 HRGN back_rgn, excl_rgn;
829 RECT window, background;
830 PAINTSTRUCT ps;
831 int x, y;
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);
860 DeleteObject(hPen);
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,
876 preview.hdc2, 0, 0,
877 preview.bmSize.cx, preview.bmSize.cy, SRCCOPY);
879 draw_margin_lines(hdc, x, y, preview.zoomratio);
880 } else {
881 InflateRect(&background, -2, -2);
882 FillRect(hdc, &background, GetStockObject(WHITE_BRUSH));
886 preview.window = window;
888 EndPaint(hwndPreview, &ps);
890 return 0;
893 static void update_preview_statusbar(HWND hMainWnd)
895 HWND hStatusbar = GetDlgItem(hMainWnd, IDC_STATUSBAR);
896 HINSTANCE hInst = GetModuleHandleW(0);
897 WCHAR *p;
898 WCHAR wstr[MAX_STRING_LEN];
900 p = wstr;
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);
906 } else {
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)
917 RECT paper;
918 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
919 HWND hwndPreview = GetDlgItem(hMainWnd, IDC_PREVIEW);
920 HBITMAP hBitmapCapture;
921 FORMATRANGE fr;
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;
931 fr.chrg.cpMin = 0;
932 fr.chrg.cpMax = preview.textlength;
934 SetRect(&paper, 0, 0, preview.bmSize.cx, preview.bmSize.cy);
936 if (!preview.hdc) {
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)
947 if (!preview.hdc2)
949 preview.hdc2 = CreateCompatibleDC(hdc);
950 hBitmapCapture = CreateCompatibleBitmap(hdc,
951 preview.bmSize.cx,
952 preview.bmSize.cy);
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);
972 int nPreviewPages;
974 preview.pages_shown = preview.pages_shown > 1 ? 1 : 2;
976 nPreviewPages = preview.zoomlevel > 0 ? preview.saved_pages_shown :
977 preview.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)
992 RECT rc;
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))
998 return 1;
1000 if (preview.pages_shown <= 1)
1001 return 0;
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;
1008 return 0;
1011 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1013 switch(msg)
1015 case WM_CREATE:
1017 HWND hMainWnd = GetParent(hWnd);
1018 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
1019 FORMATRANGE fr;
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)&gt, 0);
1035 fr.hdc = CreateCompatibleDC(hdc);
1036 fr.hdcTarget = hdcTarget;
1037 fr.chrg.cpMin = 0;
1038 fr.chrg.cpMax = preview.textlength;
1039 DeleteDC(fr.hdc);
1040 DeleteDC(hdcTarget);
1041 ReleaseDC(hWnd, hdc);
1043 update_preview_sizes(hWnd, TRUE);
1044 update_preview(hMainWnd);
1045 break;
1048 case WM_PAINT:
1049 return print_preview(hWnd);
1051 case WM_SIZE:
1053 update_preview_sizes(hWnd, FALSE);
1054 InvalidateRect(hWnd, NULL, FALSE);
1055 break;
1058 case WM_VSCROLL:
1059 case WM_HSCROLL:
1061 SCROLLINFO si;
1062 RECT rc;
1063 int nBar = (msg == WM_VSCROLL) ? SB_VERT : SB_HORZ;
1064 int origPos;
1066 GetClientRect(hWnd, &rc);
1067 si.cbSize = sizeof(si);
1068 si.fMask = SIF_ALL;
1069 GetScrollInfo(hWnd, nBar, &si);
1070 origPos = si.nPos;
1071 switch(LOWORD(wParam))
1073 case SB_TOP: /* == SB_LEFT */
1074 si.nPos = si.nMin;
1075 break;
1076 case SB_BOTTOM: /* == SB_RIGHT */
1077 si.nPos = si.nMax;
1078 break;
1079 case SB_LINEUP: /* == SB_LINELEFT */
1080 si.nPos -= si.nPage / 10;
1081 break;
1082 case SB_LINEDOWN: /* == SB_LINERIGHT */
1083 si.nPos += si.nPage / 10;
1084 break;
1085 case SB_PAGEUP: /* == SB_PAGELEFT */
1086 si.nPos -= si.nPage;
1087 break;
1088 case SB_PAGEDOWN: /* SB_PAGERIGHT */
1089 si.nPos += si.nPage;
1090 break;
1091 case SB_THUMBTRACK:
1092 si.nPos = si.nTrackPos;
1093 break;
1095 si.fMask = SIF_POS;
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);
1103 else
1104 ScrollWindow(hWnd, amount, 0, NULL, NULL);
1106 return 0;
1109 case WM_SETCURSOR:
1111 POINT pt;
1112 RECT rc;
1113 int bHittest = 0;
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);
1127 if (bHittest)
1128 SetCursor(LoadCursorW(GetModuleHandleW(0),
1129 MAKEINTRESOURCEW(IDC_ZOOM)));
1130 else
1131 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
1133 return TRUE;
1136 case WM_LBUTTONDOWN:
1138 int page;
1139 POINT pt;
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
1147 * coordinate. */
1148 pt.x -= preview.spacing.cx;
1149 if (page > 1)
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);
1165 } else {
1166 update_preview_sizes(hWnd, TRUE);
1167 InvalidateRect(hWnd, NULL, FALSE);
1168 update_preview_buttons(hMainWnd);
1171 if (preview.zoomlevel > 0) {
1172 SCROLLINFO si;
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);
1191 default:
1192 return DefWindowProcW(hWnd, msg, wParam, lParam);
1195 return 0;
1198 LRESULT preview_command(HWND hWnd, WPARAM wParam)
1200 switch(LOWORD(wParam))
1202 case ID_FILE_EXIT:
1203 PostMessageW(hWnd, WM_CLOSE, 0, 0);
1204 break;
1206 case ID_PREVIEW_NEXTPAGE:
1207 case ID_PREVIEW_PREVPAGE:
1209 if(LOWORD(wParam) == ID_PREVIEW_NEXTPAGE)
1210 preview.page++;
1211 else
1212 preview.page--;
1214 update_preview(hWnd);
1216 break;
1218 case ID_PREVIEW_NUMPAGES:
1219 toggle_num_pages(hWnd);
1220 break;
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);
1233 } else {
1234 HWND hwndPreview = GetDlgItem(hWnd, IDC_PREVIEW);
1235 update_preview_sizes(hwndPreview, TRUE);
1236 InvalidateRect(hwndPreview, NULL, FALSE);
1237 update_preview_buttons(hWnd);
1240 break;
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);
1250 } else {
1251 update_preview_sizes(hwndPreview, TRUE);
1252 InvalidateRect(hwndPreview, NULL, FALSE);
1253 update_preview_buttons(hWnd);
1256 break;
1258 case ID_PRINT:
1259 dialog_print(hWnd, preview.wszFileName);
1260 SendMessageW(hWnd, WM_CLOSE, 0, 0);
1261 break;
1264 return 0;