Commit a1378190 authored by Alexander Nicolaysen Sørnes's avatar Alexander Nicolaysen Sørnes Committed by Alexandre Julliard

wordpad: Move printing functions to a separate file.

parent 62d90a60
......@@ -10,6 +10,7 @@ EXTRADEFS = -DNO_LIBWINE_PORT
MODCFLAGS = @BUILTINFLAG@
C_SRCS = \
print.c \
wordpad.c
RC_SRCS = rsrc.rc
......
/*
* Wordpad implementation - Printing and print preview functions
*
* Copyright 2007 by Alexander N. Sørnes <alex@thehandofagony.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <windows.h>
#include <richedit.h>
#include <commctrl.h>
#include "wordpad.h"
typedef struct _previewinfo
{
int page;
int pages;
HDC hdc;
HDC hdcSized;
RECT window;
LPWSTR wszFileName;
} previewinfo, *ppreviewinfo;
static HGLOBAL devMode;
static HGLOBAL devNames;
static RECT margins;
static previewinfo preview;
static const WCHAR var_pagemargin[] = {'P','a','g','e','M','a','r','g','i','n',0};
static LPWSTR get_print_file_filter(HWND hMainWnd)
{
static WCHAR wszPrintFilter[MAX_STRING_LEN*2+6+4+1];
const WCHAR files_prn[] = {'*','.','P','R','N',0};
const WCHAR files_all[] = {'*','.','*','\0'};
LPWSTR p;
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
p = wszPrintFilter;
LoadStringW(hInstance, STRING_PRINTER_FILES_PRN, p, MAX_STRING_LEN);
p += lstrlenW(p) + 1;
lstrcpyW(p, files_prn);
p += lstrlenW(p) + 1;
LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
p += lstrlenW(p) + 1;
lstrcpyW(p, files_all);
p += lstrlenW(p) + 1;
*p = 0;
return wszPrintFilter;
}
void registry_set_pagemargins(HKEY hKey)
{
RegSetValueExW(hKey, var_pagemargin, 0, REG_BINARY, (LPBYTE)&margins, sizeof(RECT));
}
void registry_read_pagemargins(HKEY hKey)
{
DWORD size = sizeof(RECT);
if(!hKey || RegQueryValueExW(hKey, var_pagemargin, 0, NULL, (LPBYTE)&margins,
&size) != ERROR_SUCCESS || size != sizeof(RECT))
{
margins.top = 1417;
margins.bottom = 1417;
margins.left = 1757;
margins.right = 1757;
}
}
static void AddTextButton(HWND hRebarWnd, int string, int command, int id)
{
REBARBANDINFOW rb;
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hRebarWnd, GWLP_HINSTANCE);
WCHAR text[MAX_STRING_LEN];
HWND hButton;
LoadStringW(hInstance, string, text, MAX_STRING_LEN);
hButton = CreateWindowW(WC_BUTTONW, text,
WS_VISIBLE | WS_CHILD, 5, 5, 100, 15,
hRebarWnd, (HMENU)command, hInstance, NULL);
rb.cbSize = sizeof(rb);
rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
rb.hwndChild = hButton;
rb.cyChild = rb.cyMinChild = 22;
rb.cx = rb.cxMinChild = 90;
rb.cxIdeal = 100;
rb.wID = id;
SendMessageW(hRebarWnd, RB_INSERTBAND, -1, (LPARAM)&rb);
}
static HDC make_dc(void)
{
if(devNames && devMode)
{
LPDEVNAMES dn = GlobalLock(devNames);
LPDEVMODEW dm = GlobalLock(devMode);
HDC ret;
ret = CreateDCW((LPWSTR)dn + dn->wDriverOffset,
(LPWSTR)dn + dn->wDeviceOffset, 0, dm);
GlobalUnlock(dn);
GlobalUnlock(dm);
return ret;
} else
{
return 0;
}
}
static LONG twips_to_centmm(int twips)
{
return MulDiv(twips, 1000, 567);
}
LONG centmm_to_twips(int mm)
{
return MulDiv(mm, 567, 1000);
}
static LONG twips_to_pixels(int twips, int dpi)
{
float ret = ((float)twips / ((float)567 * 2.54)) * (float)dpi;
return (LONG)ret;
}
static LONG devunits_to_twips(int units, int dpi)
{
float ret = ((float)units / (float)dpi) * (float)567 * 2.54;
return (LONG)ret;
}
static RECT get_print_rect(HDC hdc)
{
RECT rc;
int width, height;
if(hdc)
{
int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
width = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALWIDTH), dpiX);
height = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALHEIGHT), dpiY);
} else
{
width = centmm_to_twips(18500);
height = centmm_to_twips(27000);
}
rc.left = margins.left;
rc.right = width - margins.right;
rc.top = margins.top;
rc.bottom = height - margins.bottom;
return rc;
}
void target_device(HWND hMainWnd, DWORD wordWrap)
{
HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
HDC hdc = make_dc();
int width = 0;
if(wordWrap == ID_WORDWRAP_MARGIN)
{
RECT rc = get_print_rect(hdc);
width = rc.right;
}
if(!hdc)
{
HDC hMaindc = GetDC(hMainWnd);
hdc = CreateCompatibleDC(hMaindc);
ReleaseDC(hMainWnd, hMaindc);
}
SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, (WPARAM)hdc, width);
DeleteDC(hdc);
}
static LPWSTR dialog_print_to_file(HWND hMainWnd)
{
OPENFILENAMEW ofn;
static WCHAR file[MAX_PATH] = {'O','U','T','P','U','T','.','P','R','N',0};
static const WCHAR defExt[] = {'P','R','N',0};
static LPWSTR file_filter;
if(!file_filter)
file_filter = get_print_file_filter(hMainWnd);
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.hwndOwner = hMainWnd;
ofn.lpstrFilter = file_filter;
ofn.lpstrFile = (LPWSTR)file;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = (LPWSTR)defExt;
if(GetSaveFileNameW(&ofn))
return (LPWSTR)file;
else
return FALSE;
}
static int get_num_pages(HWND hEditorWnd, FORMATRANGE fr)
{
int page = 0;
do
{
page++;
fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE,
(LPARAM)&fr);
}
while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
return page;
}
static void char_from_pagenum(HWND hEditorWnd, FORMATRANGE *fr, int page)
{
int i;
for(i = 1; i <= page; i++)
{
if(i == page)
break;
fr->chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)fr);
}
}
static void print(LPPRINTDLGW pd, LPWSTR wszFileName)
{
FORMATRANGE fr;
DOCINFOW di;
HWND hEditorWnd = GetDlgItem(pd->hwndOwner, IDC_EDITOR);
int printedPages = 0;
fr.hdc = pd->hDC;
fr.hdcTarget = pd->hDC;
fr.rc = get_print_rect(fr.hdc);
fr.rcPage.left = 0;
fr.rcPage.right = fr.rc.right + margins.right;
fr.rcPage.top = 0;
fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(di);
di.lpszDocName = wszFileName;
if(pd->Flags & PD_PRINTTOFILE)
{
di.lpszOutput = dialog_print_to_file(pd->hwndOwner);
if(!di.lpszOutput)
return;
}
if(pd->Flags & PD_SELECTION)
{
SendMessageW(hEditorWnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
} else
{
GETTEXTLENGTHEX gt;
gt.flags = GTL_DEFAULT;
gt.codepage = 1200;
fr.chrg.cpMin = 0;
fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
if(pd->Flags & PD_PAGENUMS)
char_from_pagenum(hEditorWnd, &fr, pd->nToPage);
}
StartDocW(fr.hdc, &di);
do
{
if(StartPage(fr.hdc) <= 0)
break;
fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
if(EndPage(fr.hdc) <= 0)
break;
printedPages++;
if((pd->Flags & PD_PAGENUMS) && (printedPages > (pd->nToPage - pd->nFromPage)))
break;
}
while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
EndDoc(fr.hdc);
SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
}
void dialog_printsetup(HWND hMainWnd)
{
PAGESETUPDLGW ps;
ZeroMemory(&ps, sizeof(ps));
ps.lStructSize = sizeof(ps);
ps.hwndOwner = hMainWnd;
ps.Flags = PSD_INHUNDREDTHSOFMILLIMETERS | PSD_MARGINS;
ps.rtMargin.left = twips_to_centmm(margins.left);
ps.rtMargin.right = twips_to_centmm(margins.right);
ps.rtMargin.top = twips_to_centmm(margins.top);
ps.rtMargin.bottom = twips_to_centmm(margins.bottom);
ps.hDevMode = devMode;
ps.hDevNames = devNames;
if(PageSetupDlgW(&ps))
{
margins.left = centmm_to_twips(ps.rtMargin.left);
margins.right = centmm_to_twips(ps.rtMargin.right);
margins.top = centmm_to_twips(ps.rtMargin.top);
margins.bottom = centmm_to_twips(ps.rtMargin.bottom);
devMode = ps.hDevMode;
devNames = ps.hDevNames;
}
}
void get_default_printer_opts(void)
{
PRINTDLGW pd;
ZeroMemory(&pd, sizeof(pd));
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
pd.hDevMode = devMode;
PrintDlgW(&pd);
devMode = pd.hDevMode;
devNames = pd.hDevNames;
}
void print_quick(LPWSTR wszFileName)
{
PRINTDLGW pd;
ZeroMemory(&pd, sizeof(pd));
pd.hDC = make_dc();
print(&pd, wszFileName);
}
void dialog_print(HWND hMainWnd, LPWSTR wszFileName)
{
PRINTDLGW pd;
HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
int from = 0;
int to = 0;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hMainWnd;
pd.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
pd.nMinPage = 1;
pd.nMaxPage = -1;
pd.hDevMode = devMode;
pd.hDevNames = devNames;
SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
if(from == to)
pd.Flags |= PD_NOSELECTION;
if(PrintDlgW(&pd))
{
devMode = pd.hDevMode;
devNames = pd.hDevNames;
print(&pd, wszFileName);
}
}
static void preview_bar_show(HWND hMainWnd, BOOL show)
{
HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
int i;
if(show)
{
REBARBANDINFOW rb;
HWND hStatic;
AddTextButton(hReBar, STRING_PREVIEW_PRINT, ID_PRINT, BANDID_PREVIEW_BTN1);
AddTextButton(hReBar, STRING_PREVIEW_NEXTPAGE, ID_PREVIEW_NEXTPAGE, BANDID_PREVIEW_BTN2);
AddTextButton(hReBar, STRING_PREVIEW_PREVPAGE, ID_PREVIEW_PREVPAGE, BANDID_PREVIEW_BTN3);
AddTextButton(hReBar, STRING_PREVIEW_CLOSE, ID_FILE_EXIT, BANDID_PREVIEW_BTN4);
hStatic = CreateWindowW(WC_STATICW, NULL,
WS_VISIBLE | WS_CHILD, 0, 0, 0, 0,
hReBar, NULL, NULL, NULL);
rb.cbSize = sizeof(rb);
rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
rb.hwndChild = hStatic;
rb.cyChild = rb.cyMinChild = 22;
rb.cx = rb.cxMinChild = 90;
rb.cxIdeal = 100;
rb.wID = BANDID_PREVIEW_BUFFER;
SendMessageW(hReBar, RB_INSERTBAND, -1, (LPARAM)&rb);
} else
{
for(i = 0; i <= PREVIEW_BUTTONS; i++)
SendMessageW(hReBar, RB_DELETEBAND, SendMessageW(hReBar, RB_IDTOINDEX, BANDID_PREVIEW_BTN1+i, 0), 0);
}
}
void init_preview(HWND hMainWnd, LPWSTR wszFileName)
{
preview.page = 1;
preview.hdc = 0;
preview.wszFileName = wszFileName;
preview_bar_show(hMainWnd, TRUE);
}
void close_preview(HWND hMainWnd)
{
preview.window.right = 0;
preview.window.bottom = 0;
preview.page = 0;
preview.pages = 0;
preview_bar_show(hMainWnd, FALSE);
}
BOOL preview_isactive(void)
{
return preview.page != 0;
}
LRESULT print_preview(HWND hMainWnd)
{
FORMATRANGE fr;
GETTEXTLENGTHEX gt;
HDC hdc;
RECT window, background;
HBITMAP hBitmapCapture, hBitmapScaled;
int bmWidth, bmHeight, bmNewWidth, bmNewHeight;
float ratioWidth, ratioHeight, ratio;
int xOffset, yOffset;
int barheight;
HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
PAINTSTRUCT ps;
hdc = BeginPaint(hMainWnd, &ps);
GetClientRect(hMainWnd, &window);
fr.hdcTarget = make_dc();
fr.rc = get_print_rect(fr.hdcTarget);
fr.rcPage.left = 0;
fr.rcPage.top = 0;
fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
fr.rcPage.right = fr.rc.right + margins.right;
bmWidth = twips_to_pixels(fr.rcPage.right, GetDeviceCaps(hdc, LOGPIXELSX));
bmHeight = twips_to_pixels(fr.rcPage.bottom, GetDeviceCaps(hdc, LOGPIXELSY));
hBitmapCapture = CreateCompatibleBitmap(hdc, bmWidth, bmHeight);
if(!preview.hdc)
{
RECT paper;
HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
preview.hdc = CreateCompatibleDC(hdc);
fr.hdc = preview.hdc;
gt.flags = GTL_DEFAULT;
gt.codepage = 1200;
fr.chrg.cpMin = 0;
fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
paper.left = 0;
paper.right = bmWidth;
paper.top = 0;
paper.bottom = bmHeight;
if(!preview.pages)
preview.pages = get_num_pages(hEditorWnd, fr);
SelectObject(preview.hdc, hBitmapCapture);
char_from_pagenum(hEditorWnd, &fr, preview.page);
FillRect(preview.hdc, &paper, GetStockObject(WHITE_BRUSH));
SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_PREVPAGE), preview.page > 1);
EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_NEXTPAGE), preview.page < preview.pages);
}
barheight = SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
ratioWidth = ((float)window.right - 20.0) / (float)bmHeight;
ratioHeight = ((float)window.bottom - 20.0 - (float)barheight) / (float)bmHeight;
if(ratioWidth > ratioHeight)
ratio = ratioHeight;
else
ratio = ratioWidth;
bmNewWidth = (int)((float)bmWidth * ratio);
bmNewHeight = (int)((float)bmHeight * ratio);
hBitmapScaled = CreateCompatibleBitmap(hdc, bmNewWidth, bmNewHeight);
xOffset = ((window.right - bmNewWidth) / 2);
yOffset = ((window.bottom - bmNewHeight + barheight) / 2);
if(window.right != preview.window.right || window.bottom != preview.window.bottom)
{
DeleteDC(preview.hdcSized),
preview.hdcSized = CreateCompatibleDC(hdc);
SelectObject(preview.hdcSized, hBitmapScaled);
StretchBlt(preview.hdcSized, 0, 0, bmNewWidth, bmNewHeight, preview.hdc, 0, 0, bmWidth, bmHeight, SRCCOPY);
}
window.top = barheight;
FillRect(hdc, &window, GetStockObject(GRAY_BRUSH));
SelectObject(hdc, hBitmapScaled);
background.left = xOffset - 2;
background.right = xOffset + bmNewWidth + 2;
background.top = yOffset - 2;
background.bottom = yOffset + bmNewHeight + 2;
FillRect(hdc, &background, GetStockObject(BLACK_BRUSH));
BitBlt(hdc, xOffset, yOffset, bmNewWidth, bmNewHeight, preview.hdcSized, 0, 0, SRCCOPY);
DeleteDC(fr.hdcTarget);
preview.window = window;
EndPaint(hMainWnd, &ps);
return 0;
}
LRESULT preview_command(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessageW(hWnd, WM_CLOSE, 0, 0);
break;
case ID_PREVIEW_NEXTPAGE:
case ID_PREVIEW_PREVPAGE:
{
HWND hReBar = GetDlgItem(hWnd, IDC_REBAR);
RECT rc;
if(LOWORD(wParam) == ID_PREVIEW_NEXTPAGE)
preview.page++;
else
preview.page--;
preview.hdc = 0;
preview.window.right = 0;
GetClientRect(hWnd, &rc);
rc.top += SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
InvalidateRect(hWnd, &rc, TRUE);
}
break;
case ID_PRINT:
dialog_print(hWnd, preview.wszFileName);
SendMessageW(hWnd, WM_CLOSE, 0, 0);
break;
}
return 0;
}
......@@ -20,7 +20,7 @@
#include <windef.h>
#include <winuser.h>
#include "resource.h"
#include "wordpad.h"
MAINACCELTABLE ACCELERATORS
BEGIN
......
......@@ -22,8 +22,6 @@
#define WIN32_LEAN_AND_MEAN
#define _WIN32_IE 0x0400
#define MAX_STRING_LEN 255
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
......@@ -38,7 +36,7 @@
#include <math.h>
#include <errno.h>
#include "resource.h"
#include "wordpad.h"
#ifdef NONAMELESSUNION
# define U(x) (x).u
......@@ -66,7 +64,6 @@ static const WCHAR key_text[] = {'T','e','x','t',0};
static const WCHAR var_file[] = {'F','i','l','e','%','d',0};
static const WCHAR var_framerect[] = {'F','r','a','m','e','R','e','c','t',0};
static const WCHAR var_barstate0[] = {'B','a','r','S','t','a','t','e','0',0};
static const WCHAR var_pagemargin[] = {'P','a','g','e','M','a','r','g','i','n',0};
static const WCHAR stringFormat[] = {'%','2','d','\0'};
......@@ -77,10 +74,13 @@ static HMENU hPopupMenu;
static UINT ID_FINDMSGSTRING;
static DWORD wordWrap[2];
static WPARAM fileFormat = SF_RTF;
static WCHAR wszFileName[MAX_PATH];
static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
static WCHAR wszDefaultFileName[MAX_STRING_LEN];
static WCHAR wszSaveChanges[MAX_STRING_LEN];
static WCHAR wszPrintFilter[MAX_STRING_LEN*2+6+4+1];
static WCHAR units_cmW[MAX_STRING_LEN];
static char units_cmA[MAX_STRING_LEN];
......@@ -94,7 +94,7 @@ static void DoLoadStrings(void)
static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
static const WCHAR files_all[] = {'*','.','*','\0'};
static const WCHAR files_prn[] = {'*','.','P','R','N',0};
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
......@@ -115,17 +115,6 @@ static void DoLoadStrings(void)
p += lstrlenW(p) + 1;
*p = '\0';
p = wszPrintFilter;
LoadStringW(hInstance, STRING_PRINTER_FILES_PRN, p, MAX_STRING_LEN);
p += lstrlenW(p) + 1;
lstrcpyW(p, files_prn);
p += lstrlenW(p) + 1;
LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
p += lstrlenW(p) + 1;
lstrcpyW(p, files_all);
p += lstrlenW(p) + 1;
*p = 0;
p = wszDefaultFileName;
LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
......@@ -150,30 +139,6 @@ static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
}
static void AddTextButton(HWND hRebarWnd, int string, int command, int id)
{
REBARBANDINFOW rb;
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hRebarWnd, GWLP_HINSTANCE);
WCHAR text[MAX_STRING_LEN];
HWND hButton;
LoadStringW(hInstance, string, text, MAX_STRING_LEN);
hButton = CreateWindowW(WC_BUTTONW, text,
WS_VISIBLE | WS_CHILD, 5, 5, 100, 15,
hRebarWnd, (HMENU)command, hInstance, NULL);
rb.cbSize = sizeof(rb);
rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
rb.hwndChild = hButton;
rb.cyChild = rb.cyMinChild = 22;
rb.cx = rb.cxMinChild = 90;
rb.cxIdeal = 100;
rb.wID = id;
SendMessageW(hRebarWnd, RB_INSERTBAND, -1, (LPARAM)&rb);
}
static void AddSeparator(HWND hwndToolBar)
{
TBBUTTON button;
......@@ -234,9 +199,6 @@ static LPWSTR file_basename(LPWSTR path)
return pos;
}
static WCHAR wszFileName[MAX_PATH];
static WPARAM fileFormat = SF_RTF;
static void set_caption(LPCWSTR wszNewFileName)
{
static const WCHAR wszSeparator[] = {' ','-',' '};
......@@ -306,8 +268,6 @@ static LRESULT registry_get_handle(HKEY *hKey, LPDWORD action, LPCWSTR subKey)
return ret;
}
static RECT margins;
static void registry_set_options(void)
{
HKEY hKey;
......@@ -321,7 +281,7 @@ static void registry_set_options(void)
RegSetValueExW(hKey, var_framerect, 0, REG_BINARY, (LPBYTE)&rc, sizeof(RECT));
RegSetValueExW(hKey, var_pagemargin, 0, REG_BINARY, (LPBYTE)&margins, sizeof(RECT));
registry_set_pagemargins(hKey);
}
}
......@@ -885,7 +845,6 @@ static void update_window(void)
}
static DWORD barState[2];
static DWORD wordWrap[2];
static BOOL is_bar_visible(int bandId)
{
......@@ -949,99 +908,21 @@ static void set_bar_states(void)
update_window();
}
static HGLOBAL devMode;
static HGLOBAL devNames;
static HDC make_dc(void)
{
if(devNames && devMode)
{
LPDEVNAMES dn = GlobalLock(devNames);
LPDEVMODEW dm = GlobalLock(devMode);
HDC ret;
ret = CreateDCW((LPWSTR)dn + dn->wDriverOffset,
(LPWSTR)dn + dn->wDeviceOffset, 0, dm);
GlobalUnlock(dn);
GlobalUnlock(dm);
return ret;
} else
{
return 0;
}
}
static LONG twips_to_pixels(int twips, int dpi)
static void preview_exit(HWND hMainWnd)
{
float ret = ((float)twips / ((float)567 * 2.54)) * (float)dpi;
return (LONG)ret;
}
static LONG devunits_to_twips(int units, int dpi)
{
float ret = ((float)units / (float)dpi) * (float)567 * 2.54;
return (LONG)ret;
}
static LONG centmm_to_twips(int mm)
{
return MulDiv(mm, 567, 1000);
}
static LONG twips_to_centmm(int twips)
{
return MulDiv(twips, 1000, 567);
}
static RECT get_print_rect(HDC hdc)
{
RECT rc;
int width, height;
if(hdc)
{
int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
width = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALWIDTH), dpiX);
height = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALHEIGHT), dpiY);
} else
{
width = centmm_to_twips(18500);
height = centmm_to_twips(27000);
}
rc.left = margins.left;
rc.right = width - margins.right;
rc.top = margins.top;
rc.bottom = height - margins.bottom;
return rc;
}
static void target_device(void)
{
HDC hdc = make_dc();
int width = 0;
int index = reg_formatindex(fileFormat);
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAINMENU));
HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
if(wordWrap[index] == ID_WORDWRAP_MARGIN)
{
RECT rc = get_print_rect(hdc);
width = rc.right;
}
set_bar_states();
ShowWindow(hEditorWnd, TRUE);
if(!hdc)
{
HDC hMaindc = GetDC(hMainWnd);
hdc = CreateCompatibleDC(hMaindc);
ReleaseDC(hMainWnd, hMaindc);
}
close_preview(hMainWnd);
SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, (WPARAM)hdc, width);
SetMenu(hMainWnd, hMenu);
registry_read_filelist(hMainWnd);
DeleteDC(hdc);
update_window();
}
static void set_fileformat(WPARAM format)
......@@ -1059,7 +940,7 @@ static void set_fileformat(WPARAM format)
set_bar_states();
set_default_font();
target_device();
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
}
static void DoOpenFile(LPCWSTR szOpenFileName)
......@@ -1283,412 +1164,6 @@ static void DialogOpenFile(void)
}
}
static LPWSTR dialog_print_to_file(void)
{
OPENFILENAMEW ofn;
static WCHAR file[MAX_PATH] = {'O','U','T','P','U','T','.','P','R','N',0};
static const WCHAR defExt[] = {'P','R','N',0};
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.hwndOwner = hMainWnd;
ofn.lpstrFilter = (LPWSTR)wszPrintFilter;
ofn.lpstrFile = (LPWSTR)file;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = (LPWSTR)defExt;
if(GetSaveFileNameW(&ofn))
return (LPWSTR)file;
else
return FALSE;
}
static int get_num_pages(FORMATRANGE fr)
{
int page = 0;
do
{
page++;
fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE,
(LPARAM)&fr);
}
while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
return page;
}
static void char_from_pagenum(FORMATRANGE *fr, int page)
{
int i;
for(i = 1; i <= page; i++)
{
if(i == page)
break;
fr->chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)fr);
}
}
static void print(LPPRINTDLGW pd)
{
FORMATRANGE fr;
DOCINFOW di;
int printedPages = 0;
fr.hdc = pd->hDC;
fr.hdcTarget = pd->hDC;
fr.rc = get_print_rect(fr.hdc);
fr.rcPage.left = 0;
fr.rcPage.right = fr.rc.right + margins.right;
fr.rcPage.top = 0;
fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(di);
di.lpszDocName = (LPWSTR)wszFileName;
if(pd->Flags & PD_PRINTTOFILE)
{
di.lpszOutput = dialog_print_to_file();
if(!di.lpszOutput)
return;
}
if(pd->Flags & PD_SELECTION)
{
SendMessageW(hEditorWnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
} else
{
GETTEXTLENGTHEX gt;
gt.flags = GTL_DEFAULT;
gt.codepage = 1200;
fr.chrg.cpMin = 0;
fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
if(pd->Flags & PD_PAGENUMS)
char_from_pagenum(&fr, pd->nToPage);
}
StartDocW(fr.hdc, &di);
do
{
if(StartPage(fr.hdc) <= 0)
break;
fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
if(EndPage(fr.hdc) <= 0)
break;
printedPages++;
if((pd->Flags & PD_PAGENUMS) && (printedPages > (pd->nToPage - pd->nFromPage)))
break;
}
while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
EndDoc(fr.hdc);
SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
target_device();
}
static void dialog_printsetup(void)
{
PAGESETUPDLGW ps;
ZeroMemory(&ps, sizeof(ps));
ps.lStructSize = sizeof(ps);
ps.hwndOwner = hMainWnd;
ps.Flags = PSD_INHUNDREDTHSOFMILLIMETERS | PSD_MARGINS;
ps.rtMargin.left = twips_to_centmm(margins.left);
ps.rtMargin.right = twips_to_centmm(margins.right);
ps.rtMargin.top = twips_to_centmm(margins.top);
ps.rtMargin.bottom = twips_to_centmm(margins.bottom);
ps.hDevMode = devMode;
ps.hDevNames = devNames;
if(PageSetupDlgW(&ps))
{
margins.left = centmm_to_twips(ps.rtMargin.left);
margins.right = centmm_to_twips(ps.rtMargin.right);
margins.top = centmm_to_twips(ps.rtMargin.top);
margins.bottom = centmm_to_twips(ps.rtMargin.bottom);
devMode = ps.hDevMode;
devNames = ps.hDevNames;
target_device();
}
}
static void get_default_printer_opts(void)
{
PRINTDLGW pd;
ZeroMemory(&pd, sizeof(pd));
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
pd.hwndOwner = hMainWnd;
pd.hDevMode = devMode;
PrintDlgW(&pd);
devMode = pd.hDevMode;
devNames = pd.hDevNames;
}
static void print_quick(void)
{
PRINTDLGW pd;
ZeroMemory(&pd, sizeof(pd));
pd.hDC = make_dc();
print(&pd);
}
static void dialog_print(void)
{
PRINTDLGW pd;
int from = 0;
int to = 0;
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hMainWnd;
pd.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
pd.nMinPage = 1;
pd.nMaxPage = -1;
pd.hDevMode = devMode;
pd.hDevNames = devNames;
SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
if(from == to)
pd.Flags |= PD_NOSELECTION;
if(PrintDlgW(&pd))
{
devMode = pd.hDevMode;
devNames = pd.hDevNames;
print(&pd);
}
}
typedef struct _previewinfo
{
int page;
int pages;
HDC hdc;
HDC hdcSized;
RECT window;
} previewinfo, *ppreviewinfo;
static previewinfo preview;
static void preview_bar_show(BOOL show)
{
HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
int i;
if(show)
{
REBARBANDINFOW rb;
HWND hStatic;
AddTextButton(hReBar, STRING_PREVIEW_PRINT, ID_PRINT, BANDID_PREVIEW_BTN1);
AddTextButton(hReBar, STRING_PREVIEW_NEXTPAGE, ID_PREVIEW_NEXTPAGE, BANDID_PREVIEW_BTN2);
AddTextButton(hReBar, STRING_PREVIEW_PREVPAGE, ID_PREVIEW_PREVPAGE, BANDID_PREVIEW_BTN3);
AddTextButton(hReBar, STRING_PREVIEW_CLOSE, ID_FILE_EXIT, BANDID_PREVIEW_BTN4);
hStatic = CreateWindowW(WC_STATICW, NULL,
WS_VISIBLE | WS_CHILD, 0, 0, 0, 0,
hReBar, NULL, NULL, NULL);
rb.cbSize = sizeof(rb);
rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
rb.hwndChild = hStatic;
rb.cyChild = rb.cyMinChild = 22;
rb.cx = rb.cxMinChild = 90;
rb.cxIdeal = 100;
rb.wID = BANDID_PREVIEW_BUFFER;
SendMessageW(hReBar, RB_INSERTBAND, -1, (LPARAM)&rb);
} else
{
for(i = 0; i <= PREVIEW_BUTTONS; i++)
SendMessageW(hReBar, RB_DELETEBAND, SendMessageW(hReBar, RB_IDTOINDEX, BANDID_PREVIEW_BTN1+i, 0), 0);
}
}
static void preview_exit(void)
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAINMENU));
set_bar_states();
preview.window.right = 0;
preview.window.bottom = 0;
preview.page = 0;
preview.pages = 0;
ShowWindow(hEditorWnd, TRUE);
preview_bar_show(FALSE);
SetMenu(hMainWnd, hMenu);
registry_read_filelist(hMainWnd);
update_window();
}
static LRESULT print_preview(void)
{
FORMATRANGE fr;
GETTEXTLENGTHEX gt;
HDC hdc;
RECT window, background;
HBITMAP hBitmapCapture, hBitmapScaled;
int bmWidth, bmHeight, bmNewWidth, bmNewHeight;
float ratioWidth, ratioHeight, ratio;
int xOffset, yOffset;
int barheight;
HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
PAINTSTRUCT ps;
hdc = BeginPaint(hMainWnd, &ps);
GetClientRect(hMainWnd, &window);
fr.hdcTarget = make_dc();
fr.rc = get_print_rect(fr.hdcTarget);
fr.rcPage.left = 0;
fr.rcPage.top = 0;
fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
fr.rcPage.right = fr.rc.right + margins.right;
bmWidth = twips_to_pixels(fr.rcPage.right, GetDeviceCaps(hdc, LOGPIXELSX));
bmHeight = twips_to_pixels(fr.rcPage.bottom, GetDeviceCaps(hdc, LOGPIXELSY));
hBitmapCapture = CreateCompatibleBitmap(hdc, bmWidth, bmHeight);
if(!preview.hdc)
{
RECT paper;
preview.hdc = CreateCompatibleDC(hdc);
fr.hdc = preview.hdc;
gt.flags = GTL_DEFAULT;
gt.codepage = 1200;
fr.chrg.cpMin = 0;
fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
paper.left = 0;
paper.right = bmWidth;
paper.top = 0;
paper.bottom = bmHeight;
if(!preview.pages)
preview.pages = get_num_pages(fr);
SelectObject(preview.hdc, hBitmapCapture);
char_from_pagenum(&fr, preview.page);
FillRect(preview.hdc, &paper, GetStockObject(WHITE_BRUSH));
SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_PREVPAGE), preview.page > 1);
EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_NEXTPAGE), preview.page < preview.pages);
}
barheight = SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
ratioWidth = ((float)window.right - 20.0) / (float)bmHeight;
ratioHeight = ((float)window.bottom - 20.0 - (float)barheight) / (float)bmHeight;
if(ratioWidth > ratioHeight)
ratio = ratioHeight;
else
ratio = ratioWidth;
bmNewWidth = (int)((float)bmWidth * ratio);
bmNewHeight = (int)((float)bmHeight * ratio);
hBitmapScaled = CreateCompatibleBitmap(hdc, bmNewWidth, bmNewHeight);
xOffset = ((window.right - bmNewWidth) / 2);
yOffset = ((window.bottom - bmNewHeight + barheight) / 2);
if(window.right != preview.window.right || window.bottom != preview.window.bottom)
{
DeleteDC(preview.hdcSized),
preview.hdcSized = CreateCompatibleDC(hdc);
SelectObject(preview.hdcSized, hBitmapScaled);
StretchBlt(preview.hdcSized, 0, 0, bmNewWidth, bmNewHeight, preview.hdc, 0, 0, bmWidth, bmHeight, SRCCOPY);
}
window.top = barheight;
FillRect(hdc, &window, GetStockObject(GRAY_BRUSH));
SelectObject(hdc, hBitmapScaled);
background.left = xOffset - 2;
background.right = xOffset + bmNewWidth + 2;
background.top = yOffset - 2;
background.bottom = yOffset + bmNewHeight + 2;
FillRect(hdc, &background, GetStockObject(BLACK_BRUSH));
BitBlt(hdc, xOffset, yOffset, bmNewWidth, bmNewHeight, preview.hdcSized, 0, 0, SRCCOPY);
DeleteDC(fr.hdcTarget);
preview.window = window;
EndPaint(hMainWnd, &ps);
return 0;
}
static LRESULT preview_command(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessageW(hMainWnd, WM_CLOSE, 0, 0);
break;
case ID_PREVIEW_NEXTPAGE:
case ID_PREVIEW_PREVPAGE:
{
HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
RECT rc;
if(LOWORD(wParam) == ID_PREVIEW_NEXTPAGE)
preview.page++;
else
preview.page--;
preview.hdc = 0;
preview.window.right = 0;
GetClientRect(hMainWnd, &rc);
rc.top += SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
InvalidateRect(hMainWnd, &rc, TRUE);
}
break;
case ID_PRINT:
dialog_print();
preview_exit();
break;
}
return 0;
}
static void dialog_about(void)
{
HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
......@@ -1824,7 +1299,7 @@ static void dialog_viewproperties(void)
U2(psh).nStartPage = 0;
PropertySheetW(&psh);
set_bar_states();
target_device();
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
}
static void HandleCommandLine(LPWSTR cmdline)
......@@ -1993,17 +1468,11 @@ static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
static void registry_read_options(void)
{
HKEY hKey;
DWORD size = sizeof(RECT);
if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS ||
RegQueryValueExW(hKey, var_pagemargin, 0, NULL, (LPBYTE)&margins,
&size) != ERROR_SUCCESS || size != sizeof(RECT))
{
margins.top = 1417;
margins.bottom = 1417;
margins.left = 1757;
margins.right = 1757;
}
if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS)
registry_read_pagemargins(NULL);
else
registry_read_pagemargins(hKey);
RegCloseKey(hKey);
}
......@@ -2783,11 +2252,13 @@ static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
break;
case ID_PRINT:
dialog_print();
dialog_print(hWnd, wszFileName);
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
break;
case ID_PRINT_QUICK:
print_quick();
print_quick(wszFileName);
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
break;
case ID_PREVIEW:
......@@ -2798,17 +2269,17 @@ static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
set_bar_states();
barState[index] = tmp;
ShowWindow(hEditorWnd, FALSE);
preview_bar_show(TRUE);
preview.page = 1;
preview.hdc = 0;
init_preview(hWnd, wszFileName);
SetMenu(hWnd, NULL);
InvalidateRect(0, 0, TRUE);
}
break;
case ID_PRINTSETUP:
dialog_printsetup();
dialog_printsetup(hWnd);
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
break;
case ID_FORMAT_BOLD:
......@@ -3177,9 +2648,11 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
return OnNotify( hWnd, wParam, lParam );
case WM_COMMAND:
if(preview.page)
if(preview_isactive())
{
return preview_command( hWnd, wParam, lParam );
else
}
return OnCommand( hWnd, wParam, lParam );
case WM_DESTROY:
......@@ -3187,9 +2660,9 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
break;
case WM_CLOSE:
if(preview.page)
if(preview_isactive())
{
preview_exit();
preview_exit(hWnd);
} else if(prompt_save_changes())
{
registry_set_options();
......@@ -3226,8 +2699,8 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
}
break;
case WM_PAINT:
if(preview.page)
return print_preview();
if(preview_isactive())
return print_preview(hWnd);
else
return DefWindowProcW(hWnd, msg, wParam, lParam);
......@@ -3274,7 +2747,7 @@ int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdPar
set_fileformat(SF_RTF);
hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
get_default_printer_opts();
target_device();
target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
HandleCommandLine(GetCommandLineW());
......
......@@ -17,6 +17,8 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define MAX_STRING_LEN 255
#define ID_FILE_EXIT 1000
#define ID_FILE_OPEN 1001
#define ID_FILE_SAVE 1002
......@@ -182,3 +184,16 @@
#define STRING_SAVE_LOSEFORMATTING 1704
#define STRING_INVALID_NUMBER 1705
#define STRING_OLE_STORAGE_NOT_SUPPORTED 1706
void dialog_printsetup(HWND);
void dialog_print(HWND, LPWSTR);
void target_device(HWND, DWORD);
void print_quick(LPWSTR);
LRESULT preview_command(HWND, WPARAM, LPARAM);
void init_preview(HWND, LPWSTR);
void close_preview(HWND);
BOOL preview_isactive(void);
LRESULT print_preview(HWND);
void get_default_printer_opts(void);
void registry_set_pagemargins(HKEY);
void registry_read_pagemargins(HKEY);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment