Bug 710148 - Remove MOZ_REQUIRE_CURRENT_SDK from nsPrintDialogUtil.cpp; r=bsmedberg

This commit is contained in:
Ed Morley 2011-12-16 09:13:29 +00:00
parent 19995f3366
commit ef84a75477

View File

@ -35,13 +35,6 @@
*
* ***** END LICENSE BLOCK ***** */
#ifdef MOZ_REQUIRE_CURRENT_SDK
#undef WINVER
#define WINVER 0x0500
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x500
#endif
/* -------------------------------------------------------------------
To Build This:
@ -95,11 +88,6 @@ WIN_LIBS= \
// This is for extending the dialog
#include <dlgs.h>
// For PrintDlgEx
// needed because there are unicode/ansi versions of this routine
// and we need to make sure we get the correct one.
#define GetPrintDlgExQuoted "PrintDlgExA"
// Default labels for the radio buttons
static const char* kAsLaidOutOnScreenStr = "As &laid out on the screen";
static const char* kTheSelectedFrameStr = "The selected &frame";
@ -175,19 +163,6 @@ const NativePaperSizes kPaperSizes[] = {
};
const PRInt32 kNumPaperSizes = 41;
//----------------------------------------------------------------------------------
static bool
CheckForExtendedDialog()
{
#ifdef MOZ_REQUIRE_CURRENT_SDK
HMODULE lib = GetModuleHandle("comdlg32.dll");
if ( lib ) {
return GetProcAddress(lib, GetPrintDlgExQuoted);
}
#endif
return false;
}
//----------------------------------------------------------------------------------
// Map an incoming size to a Windows Native enum in the DevMode
static void
@ -1085,350 +1060,6 @@ ShowNativePrintDialog(HWND aHWnd,
return NS_OK;
}
#ifdef MOZ_REQUIRE_CURRENT_SDK
//------------------------------------------------------------------
// Callback for Property Sheet
static BOOL APIENTRY PropSheetCallBack(HWND hdlg, UINT uiMsg, UINT wParam, LONG lParam)
{
if (uiMsg == WM_COMMAND) {
UINT id = LOWORD(wParam);
if (id == rad4 || id == rad5 || id == rad6) {
gFrameSelectedRadioBtn = id;
SetRadioOfGroup(hdlg, id);
}
} else if (uiMsg == WM_INITDIALOG) {
// Create the groupbox and Radiobuttons on the "Options" Property Sheet
// We temporarily borrowed the global value for initialization
// now clear it before the dialog appears
PRInt16 howToEnableFrameUI = gFrameSelectedRadioBtn;
// don't add frame options if they would be disabled anyway
// because there are no frames
if (howToEnableFrameUI == nsIPrintSettings::kFrameEnableNone)
return TRUE;
gFrameSelectedRadioBtn = 0;
HINSTANCE hInst = (HINSTANCE)::GetWindowLongPtr(hdlg, GWLP_HINSTANCE);
if (hInst == NULL) return 0L;
// Get default font for the dialog & then its font metrics
// we need the text height to determine the height of the radio buttons
TEXTMETRIC metrics;
HFONT hFont = (HFONT)::SendMessage(hdlg, WM_GETFONT, (WPARAM)0, (LPARAM)0);
HDC localDC = ::GetDC(hdlg);
::SelectObject(localDC, (HGDIOBJ)hFont);
::GetTextMetrics(localDC, &metrics);
::ReleaseDC(hdlg, localDC);
// calculate various different "gaps" for layout purposes
RECT dlgr;
::GetWindowRect(hdlg, &dlgr);
int horzGap = 5; // generic horz gap
int vertGap = 5; // generic vert gap
int rbGap = metrics.tmHeight / 2; // gap between radiobtns
int top = vertGap*2; // start at the top
int radHgt = metrics.tmHeight; // top of new group box
int y = top; // starting pos of first radio
int x = horzGap*2;
int rbWidth = dlgr.right - dlgr.left - (5*horzGap);
int grpWidth = dlgr.right - dlgr.left - (2*horzGap);
nsRect rect;
// Create and position the radio buttons
//
// If any one control cannot be created then
// hide the others and bail out
//
x += horzGap*2;
y += vertGap + metrics.tmHeight;
rect.SetRect(x, y, rbWidth,radHgt);
HWND rad4Wnd = CreateRadioBtn(hInst, hdlg, rad4, kAsLaidOutOnScreenStr, rect);
if (rad4Wnd == NULL) return 0L;
y += radHgt + rbGap;
rect.SetRect(x, y, rbWidth, radHgt);
HWND rad5Wnd = CreateRadioBtn(hInst, hdlg, rad5, kTheSelectedFrameStr, rect);
if (rad5Wnd == NULL) {
Show(rad4Wnd, FALSE); // hide
return 0L;
}
y += radHgt + rbGap;
rect.SetRect(x, y, rbWidth, radHgt);
HWND rad6Wnd = CreateRadioBtn(hInst, hdlg, rad6, kEachFrameSeparately, rect);
if (rad6Wnd == NULL) {
Show(rad4Wnd, FALSE); // hide
Show(rad5Wnd, FALSE); // hide
return 0L;
}
y += radHgt + (vertGap*2);
x -= horzGap*2;
// Create and position the group box
rect.SetRect (x, top, grpWidth, y-top+1);
HWND grpBoxWnd = CreateGroupBox(hInst, hdlg, grp3, NS_LITERAL_STRING("Print Frame"), rect);
if (grpBoxWnd == NULL) {
Show(rad4Wnd, FALSE); // hide
Show(rad5Wnd, FALSE); // hide
Show(rad6Wnd, FALSE); // hide
return 0L;
}
// localize and initialize the groupbox and radiobuttons
InitializeExtendedDialog(hdlg, howToEnableFrameUI);
// Looks like we were able to extend the dialog
gDialogWasExtended = true;
return TRUE;
}
return 0L;
}
//------------------------------------------------------------------
// Creates the "Options" Property Sheet
static HPROPSHEETPAGE ExtendPrintDialog(HWND aHWnd, char* aTitle)
{
// The resource "OPTPROPSHEET" comes out of the widget/build/widget.rc file
HINSTANCE hInst = (HINSTANCE)::GetWindowLongPtr(aHWnd, GWLP_HINSTANCE);
PROPSHEETPAGE psp;
memset(&psp, 0, sizeof(PROPSHEETPAGE));
psp.dwSize = sizeof(PROPSHEETPAGE);
psp.dwFlags = PSP_USETITLE | PSP_PREMATURE;
psp.hInstance = hInst;
psp.pszTemplate = "OPTPROPSHEET";
psp.pfnDlgProc = PropSheetCallBack;
psp.pszTitle = aTitle?aTitle:"Options";
HPROPSHEETPAGE newPropSheet = ::CreatePropertySheetPage(&psp);
return newPropSheet;
}
//------------------------------------------------------------------
// Displays the native Print Dialog
static nsresult
ShowNativePrintDialogEx(HWND aHWnd,
nsIPrintSettings* aPrintSettings)
{
NS_ENSURE_ARG_POINTER(aHWnd);
NS_ENSURE_ARG_POINTER(aPrintSettings);
gDialogWasExtended = false;
// Create a Moveable Memory Object that holds a new DevMode
// from the Printer Name
// The PRINTDLG.hDevMode requires that it be a moveable memory object
// NOTE: We only need to free hGlobalDevMode when the dialog is cancelled
// When the user prints, it comes back in the printdlg struct and
// is used and cleaned up later
nsXPIDLString printerName;
aPrintSettings->GetPrinterName(getter_Copies(printerName));
HGLOBAL hGlobalDevMode = NULL;
if (!printerName.IsEmpty()) {
hGlobalDevMode = CreateGlobalDevModeAndInit(printerName, aPrintSettings);
}
// Prepare to Display the Print Dialog
PRINTDLGEX prntdlg;
memset(&prntdlg, 0, sizeof(PRINTDLGEX));
prntdlg.lStructSize = sizeof(prntdlg);
prntdlg.hwndOwner = aHWnd;
prntdlg.hDevMode = hGlobalDevMode;
prntdlg.Flags = PD_ALLPAGES | PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
PD_NOCURRENTPAGE | PD_COLLATE;
prntdlg.nStartPage = START_PAGE_GENERAL;
// if there is a current selection then enable the "Selection" radio button
PRInt16 howToEnableFrameUI = nsIPrintSettings::kFrameEnableNone;
if (aPrintSettings != nsnull) {
bool isOn;
aPrintSettings->GetPrintOptions(nsIPrintSettings::kEnableSelectionRB, &isOn);
if (!isOn) {
prntdlg.Flags |= PD_NOSELECTION;
}
aPrintSettings->GetHowToEnableFrameUI(&howToEnableFrameUI);
}
// At the moment we can only support one page range
// from all the documentation I can find, it appears that this
// will get cleanup automatically when the struct goes away
const int kNumPageRanges = 1;
LPPRINTPAGERANGE pPageRanges = (LPPRINTPAGERANGE) GlobalAlloc(GPTR, kNumPageRanges * sizeof(PRINTPAGERANGE));
if (!pPageRanges)
return E_OUTOFMEMORY;
prntdlg.nPageRanges = 0;
prntdlg.nMaxPageRanges = kNumPageRanges;
prntdlg.lpPageRanges = pPageRanges;
prntdlg.nMinPage = 1;
prntdlg.nMaxPage = 0xFFFF;
prntdlg.nCopies = 1;
if (ShouldExtendPrintDialog()) {
// lLcalize the Property Sheet (Tab) title
nsCAutoString title;
nsString optionsStr;
if (NS_SUCCEEDED(GetLocalizedString(strBundle, "optionsTitleWindows", optionsStr))) {
// Failure here just means a blank string
NS_CopyUnicodeToNative(optionsStr, title);
}
// Temporarily borrow this variable for setting up the radiobuttons
// if we don't use this, we will need to define a new global var
gFrameSelectedRadioBtn = howToEnableFrameUI;
HPROPSHEETPAGE psp[1];
psp[0] = ExtendPrintDialog(aHWnd, title.get());
prntdlg.nPropertyPages = 1;
prntdlg.lphPropertyPages = psp;
}
HRESULT result = ::PrintDlgEx(&prntdlg);
if (S_OK == result && (prntdlg.dwResultAction == PD_RESULT_PRINT)) {
// check to make sure we don't have any NULL pointers
NS_ENSURE_TRUE(aPrintSettings && prntdlg.hDevMode, NS_ERROR_FAILURE);
if (prntdlg.hDevNames == NULL) {
::GlobalFree(hGlobalDevMode);
return NS_ERROR_FAILURE;
}
// Lock the deviceNames and check for NULL
DEVNAMES *devnames = (DEVNAMES *)::GlobalLock(prntdlg.hDevNames);
if (devnames == NULL) {
::GlobalFree(hGlobalDevMode);
return NS_ERROR_FAILURE;
}
char* device = &(((char *)devnames)[devnames->wDeviceOffset]);
char* driver = &(((char *)devnames)[devnames->wDriverOffset]);
// Check to see if the "Print To File" control is checked
// then take the name from devNames and set it in the PrintSettings
//
// NOTE:
// As per Microsoft SDK documentation the returned value offset from
// devnames->wOutputOffset is either "FILE:" or NULL
// if the "Print To File" checkbox is checked it MUST be "FILE:"
// We assert as an extra safety check.
if (prntdlg.Flags & PD_PRINTTOFILE) {
char* fileName = &(((char *)devnames)[devnames->wOutputOffset]);
NS_ASSERTION(strcmp(fileName, "FILE:") == 0, "FileName must be `FILE:`");
aPrintSettings->SetToFileName(NS_ConvertASCIItoUTF16(fileName).get());
aPrintSettings->SetPrintToFile(true);
} else {
// clear "print to file" info
aPrintSettings->SetPrintToFile(false);
aPrintSettings->SetToFileName(nsnull);
}
nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(aPrintSettings));
if (!psWin) {
::GlobalFree(hGlobalDevMode);
return NS_ERROR_FAILURE;
}
// Setup local Data members
psWin->SetDeviceName(device);
psWin->SetDriverName(driver);
#if defined(DEBUG_rods) || defined(DEBUG_dcone)
printf("printer: driver %s, device %s flags: %d\n", driver, device, prntdlg.Flags);
#endif
::GlobalUnlock(prntdlg.hDevNames);
// fill the print options with the info from the dialog
if (aPrintSettings != nsnull) {
if (prntdlg.Flags & PD_SELECTION) {
aPrintSettings->SetPrintRange(nsIPrintSettings::kRangeSelection);
} else if (prntdlg.Flags & PD_PAGENUMS) {
aPrintSettings->SetPrintRange(nsIPrintSettings::kRangeSpecifiedPageRange);
aPrintSettings->SetStartPageRange(pPageRanges->nFromPage);
aPrintSettings->SetEndPageRange(pPageRanges->nToPage);
} else { // (prntdlg.Flags & PD_ALLPAGES)
aPrintSettings->SetPrintRange(nsIPrintSettings::kRangeAllPages);
}
if (howToEnableFrameUI != nsIPrintSettings::kFrameEnableNone) {
// make sure the dialog got extended
if (gDialogWasExtended) {
// check to see about the frame radio buttons
switch (gFrameSelectedRadioBtn) {
case rad4:
aPrintSettings->SetPrintFrameType(nsIPrintSettings::kFramesAsIs);
break;
case rad5:
aPrintSettings->SetPrintFrameType(nsIPrintSettings::kSelectedFrame);
break;
case rad6:
aPrintSettings->SetPrintFrameType(nsIPrintSettings::kEachFrameSep);
break;
} // switch
} else {
// if it didn't get extended then have it default to printing
// each frame separately
aPrintSettings->SetPrintFrameType(nsIPrintSettings::kEachFrameSep);
}
} else {
aPrintSettings->SetPrintFrameType(nsIPrintSettings::kNoFrames);
}
}
// Unlock DeviceNames
::GlobalUnlock(prntdlg.hDevNames);
// Transfer the settings from the native data to the PrintSettings
LPDEVMODEW devMode = (LPDEVMODEW)::GlobalLock(prntdlg.hDevMode);
if (devMode == NULL) {
::GlobalFree(hGlobalDevMode);
return NS_ERROR_FAILURE;
}
psWin->SetDevMode(devMode); // copies DevMode
SetPrintSettingsFromDevMode(aPrintSettings, devMode);
::GlobalUnlock(prntdlg.hDevMode);
#if defined(DEBUG_rods) || defined(DEBUG_dcone)
bool printSelection = prntdlg.Flags & PD_SELECTION;
bool printAllPages = prntdlg.Flags & PD_ALLPAGES;
bool printNumPages = prntdlg.Flags & PD_PAGENUMS;
PRInt32 fromPageNum = 0;
PRInt32 toPageNum = 0;
if (printNumPages) {
fromPageNum = pPageRanges->nFromPage;
toPageNum = pPageRanges->nToPage;
}
if (printSelection) {
printf("Printing the selection\n");
} else if (printAllPages) {
printf("Printing all the pages\n");
} else {
printf("Printing from page no. %d to %d\n", fromPageNum, toPageNum);
}
#endif
} else {
if (hGlobalDevMode) ::GlobalFree(hGlobalDevMode);
return NS_ERROR_ABORT;
}
::GlobalFree(pPageRanges);
return NS_OK;
}
#endif // MOZ_REQUIRE_CURRENT_SDK
//------------------------------------------------------------------
static void
PrepareForPrintDialog(nsIWebBrowserPrint* aWebBrowserPrint, nsIPrintSettings* aPS)
@ -1469,19 +1100,9 @@ nsresult NativeShowPrintDialog(HWND aHWnd,
nsIWebBrowserPrint* aWebBrowserPrint,
nsIPrintSettings* aPrintSettings)
{
nsresult rv = NS_ERROR_FAILURE;
PrepareForPrintDialog(aWebBrowserPrint, aPrintSettings);
#ifdef MOZ_REQUIRE_CURRENT_SDK
if (CheckForExtendedDialog()) {
rv = ShowNativePrintDialogEx(aHWnd, aPrintSettings);
} else {
rv = ShowNativePrintDialog(aHWnd, aPrintSettings);
}
#else
rv = ShowNativePrintDialog(aHWnd, aPrintSettings);
#endif
nsresult rv = ShowNativePrintDialog(aHWnd, aPrintSettings);
if (aHWnd) {
::DestroyWindow(aHWnd);
}