Back to index

lightning-sunbird  0.9+nobinonly
CPageSetupPropSheet.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: Mozilla-sample-code 1.0
00004  *
00005  * Copyright (c) 2002 Netscape Communications Corporation and
00006  * other contributors
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a
00009  * copy of this Mozilla sample software and associated documentation files
00010  * (the "Software"), to deal in the Software without restriction, including
00011  * without limitation the rights to use, copy, modify, merge, publish,
00012  * distribute, sublicense, and/or sell copies of the Software, and to permit
00013  * persons to whom the Software is furnished to do so, subject to the
00014  * following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  *
00027  * Contributor(s):
00028  *   Rod Spears <rods@netscape.com>
00029  *
00030  * ***** END LICENSE BLOCK ***** */
00031 
00032 // CPageSetupPropSheet.cpp : implementation file
00033 //
00034 
00035 #include "stdafx.h"
00036 #include "resource.h"
00037 #include "CPageSetupPropSheet.h"
00038 #include "nsMemory.h"
00039 
00040 #ifdef _DEBUG
00041 #define new DEBUG_NEW
00042 #undef THIS_FILE
00043 static char THIS_FILE[] = __FILE__;
00044 #endif
00045 
00047 // CPageSetupPropSheet
00048 
00049 IMPLEMENT_DYNAMIC(CPageSetupPropSheet, CPropertySheet)
00050 
00051 CPageSetupPropSheet::CPageSetupPropSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
00052        :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
00053 {
00054          AddControlPages();
00055 }
00056 
00057 CPageSetupPropSheet::CPageSetupPropSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
00058        :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
00059 {
00060          AddControlPages();
00061 }
00062 
00063 CPageSetupPropSheet::~CPageSetupPropSheet()
00064 {
00065 }
00066 
00067 
00068 BEGIN_MESSAGE_MAP(CPageSetupPropSheet, CPropertySheet)
00069        //{{AFX_MSG_MAP(CPageSetupPropSheet)
00070               // NOTE - the ClassWizard will add and remove mapping macros here.
00071        //}}AFX_MSG_MAP
00072 END_MESSAGE_MAP()
00073 
00075 // CPageSetupPropSheet message handlers
00076 
00078 void CPageSetupPropSheet::AddControlPages()
00079 {
00080          m_psh.dwFlags &= ~PSH_HASHELP;  // Lose the Help button
00081          AddPage(&m_FormatOptionTab);
00082          AddPage(&m_MarginHeaderFooterTab);
00083 }
00084 
00086 static float GetFloatFromStr(const TCHAR * aStr, float aMaxVal = 1.0)
00087 {
00088     float val;
00089     _stscanf(aStr, _T("%f"), &val);
00090     if (val <= aMaxVal) 
00091     {
00092         return val;
00093     } 
00094     else 
00095     {
00096         return 0.5;
00097     }
00098 }
00099 
00101 static PRUnichar* GetUnicodeFromCString(const CString& aStr)
00102 {
00103 #ifdef _UNICODE
00104     nsEmbedString str(aStr);
00105 #else
00106     nsEmbedString str;
00107     NS_CStringToUTF16(nsEmbedCString(aStr), NS_CSTRING_ENCODING_ASCII, str);
00108 #endif
00109     return NS_StringCloneData(str);
00110 }
00111 
00113 void CPageSetupPropSheet::SetPrintSettingsValues(nsIPrintSettings* aPrintSettings) 
00114 { 
00115   m_FormatOptionTab.m_PrintSettings = aPrintSettings;
00116 
00117   if (aPrintSettings != NULL) 
00118   {
00119     double top, left, right, bottom;
00120     aPrintSettings->GetMarginTop(&top);
00121     aPrintSettings->GetMarginLeft(&left);
00122     aPrintSettings->GetMarginRight(&right);
00123     aPrintSettings->GetMarginBottom(&bottom);
00124 
00125     char buf[16];
00126     sprintf(buf, "%5.2f", top);
00127     m_MarginHeaderFooterTab.m_TopMarginText = buf;
00128     sprintf(buf, "%5.2f", left);
00129     m_MarginHeaderFooterTab.m_LeftMarginText = buf;
00130     sprintf(buf, "%5.2f", right);
00131     m_MarginHeaderFooterTab.m_RightMarginText = buf;
00132     sprintf(buf, "%5.2f", bottom);
00133     m_MarginHeaderFooterTab.m_BottomMarginText = buf;
00134 
00135     PRInt32 orientation;
00136     aPrintSettings->GetOrientation(&orientation);
00137     m_FormatOptionTab.m_IsLandScape = orientation != nsIPrintSettings::kPortraitOrientation;
00138 
00139     double scaling;
00140     aPrintSettings->GetScaling(&scaling);
00141          m_FormatOptionTab.m_Scaling = int(scaling * 100.0);
00142 
00143     PRBool boolVal;
00144     aPrintSettings->GetPrintBGColors(&boolVal);
00145     m_FormatOptionTab.m_BGColors = boolVal == PR_TRUE;
00146     aPrintSettings->GetPrintBGImages(&boolVal);
00147     m_FormatOptionTab.m_BGImages = boolVal == PR_TRUE;
00148 
00149     PRUnichar* uStr;
00150     aPrintSettings->GetHeaderStrLeft(&uStr);
00151               m_MarginHeaderFooterTab.m_HeaderLeftText = uStr;
00152     if (uStr != nsnull) nsMemory::Free(uStr);
00153 
00154     aPrintSettings->GetHeaderStrCenter(&uStr);
00155               m_MarginHeaderFooterTab.m_HeaderCenterText = uStr;
00156     if (uStr != nsnull) nsMemory::Free(uStr);
00157 
00158     aPrintSettings->GetHeaderStrRight(&uStr);
00159               m_MarginHeaderFooterTab.m_HeaderRightText = uStr;
00160     if (uStr != nsnull) nsMemory::Free(uStr);
00161 
00162     aPrintSettings->GetFooterStrLeft(&uStr);
00163               m_MarginHeaderFooterTab.m_FooterLeftText = uStr;
00164     if (uStr != nsnull) nsMemory::Free(uStr);
00165 
00166     aPrintSettings->GetFooterStrCenter(&uStr);
00167               m_MarginHeaderFooterTab.m_FooterCenterText = uStr;
00168     if (uStr != nsnull) nsMemory::Free(uStr);
00169 
00170     aPrintSettings->GetFooterStrRight(&uStr);
00171               m_MarginHeaderFooterTab.m_FooterRightText = uStr;
00172     if (uStr != nsnull) nsMemory::Free(uStr);
00173   }
00174 }
00175 
00177 void CPageSetupPropSheet::GetPrintSettingsValues(nsIPrintSettings* aPrintSettings) 
00178 { 
00179 
00180     if (!aPrintSettings) return;
00181 
00182     aPrintSettings->SetScaling(double(m_FormatOptionTab.m_Scaling) / 100.0);
00183     aPrintSettings->SetPrintBGColors(m_FormatOptionTab.m_BGColors);
00184     aPrintSettings->SetPrintBGImages(m_FormatOptionTab.m_BGImages);
00185 
00186     PRInt32 orientation  = m_FormatOptionTab.m_IsLandScape?
00187                            nsIPrintSettings::kLandscapeOrientation:nsIPrintSettings::kPortraitOrientation;
00188     aPrintSettings->SetOrientation(orientation);
00189 
00190     short  type;
00191     double width;
00192     double height;
00193     m_FormatOptionTab.GetPaperSizeInfo(type, width, height);
00194     aPrintSettings->SetPaperSizeType(nsIPrintSettings::kPaperSizeDefined);
00195     aPrintSettings->SetPaperSizeUnit(type);
00196     aPrintSettings->SetPaperWidth(width);
00197     aPrintSettings->SetPaperHeight(height);
00198 
00199     aPrintSettings->SetMarginTop(GetFloatFromStr(m_MarginHeaderFooterTab.m_TopMarginText));
00200     aPrintSettings->SetMarginLeft(GetFloatFromStr(m_MarginHeaderFooterTab.m_LeftMarginText));
00201     aPrintSettings->SetMarginRight(GetFloatFromStr(m_MarginHeaderFooterTab.m_RightMarginText));
00202     aPrintSettings->SetMarginBottom(GetFloatFromStr(m_MarginHeaderFooterTab.m_BottomMarginText));
00203 
00204     PRUnichar* uStr;
00205     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_HeaderLeftText);
00206     aPrintSettings->SetHeaderStrLeft(uStr);
00207     if (uStr != nsnull) nsMemory::Free(uStr);
00208 
00209     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_HeaderCenterText);
00210     aPrintSettings->SetHeaderStrCenter(uStr);
00211     if (uStr != nsnull) nsMemory::Free(uStr);
00212 
00213     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_HeaderRightText);
00214     aPrintSettings->SetHeaderStrRight(uStr);
00215     if (uStr != nsnull) nsMemory::Free(uStr);
00216 
00217     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_FooterLeftText);
00218     aPrintSettings->SetFooterStrLeft(uStr);
00219     if (uStr != nsnull) nsMemory::Free(uStr);
00220 
00221     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_FooterCenterText);
00222     aPrintSettings->SetFooterStrCenter(uStr);
00223     if (uStr != nsnull) nsMemory::Free(uStr);
00224 
00225     uStr = GetUnicodeFromCString(m_MarginHeaderFooterTab.m_FooterRightText);
00226     aPrintSettings->SetFooterStrRight(uStr);
00227     if (uStr != nsnull) nsMemory::Free(uStr);
00228 }
00229