Back to index

lightning-sunbird  0.9+nobinonly
PageSetupDlg.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Adam Lock <adamlock@eircom.net>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef PAGESETUPDLG_H
00040 #define PAGESETUPDLG_H
00041 
00042 #include "resource.h"
00043 
00044 #include "nsIPrintSettings.h"
00045 
00046 /* VC98 doesn't define _tstof() */
00047 #if defined(_MSC_VER) && _MSC_VER < 1300
00048 #ifndef UNICODE
00049 #define _tstof atof
00050 #else
00051 inline double _wtof(const wchar_t *string)
00052 {
00053    USES_CONVERSION;
00054    return atof(W2CA(string));
00055 }
00056 #define _tstof _wtof
00057 #endif
00058 #endif
00059 
00060 class CCustomFieldDlg : public CDialogImpl<CCustomFieldDlg>
00061 {
00062 public:
00063     enum { IDD = IDD_CUSTOM_FIELD };
00064 
00065     BEGIN_MSG_MAP(CCustomFieldDlg)
00066         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
00067         COMMAND_ID_HANDLER(IDOK, OnOK)
00068         COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
00069     END_MSG_MAP()
00070 
00071     nsString m_field;
00072 
00073     CCustomFieldDlg(const nsAString &str) :
00074         m_field(str)
00075     {
00076     }
00077 
00078     LRESULT OnInitDialog(UINT uMsg, WPARAM wParam,  LPARAM lParam, BOOL& bHandled)
00079     {
00080         USES_CONVERSION;
00081         SetDlgItemText(IDC_VALUE, W2CT(m_field.get()));
00082         return 0;
00083     }
00084 
00085     LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00086     {
00087         TCHAR szBuf[128];
00088         GetDlgItemText(IDC_VALUE, szBuf, sizeof(szBuf) / sizeof(szBuf[0]));
00089         USES_CONVERSION;
00090         m_field = T2CW(szBuf);
00091         EndDialog(IDOK);
00092         return 0;
00093     }
00094 
00095     LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00096     {
00097         EndDialog(IDCANCEL);
00098         return 0;
00099     }
00100 };
00101 
00102 class CPageSetupFormatDlg : public CDialogImpl<CPageSetupFormatDlg>
00103 {
00104 public:
00105     enum { IDD = IDD_PPAGE_FORMAT };
00106 
00107     BEGIN_MSG_MAP(CPageSetupFormatDlg)
00108         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
00109         COMMAND_ID_HANDLER(IDC_SHRINKTOFIT, OnClickShrinkToFit)
00110     END_MSG_MAP()
00111 
00112     CPageSetupFormatDlg()
00113     {
00114     }
00115 
00116     void Init(nsIPrintSettings *aPrintSettings)
00117     {
00118         // Set landscape / portrait mode
00119         PRInt32 orientation = nsIPrintSettings::kPortraitOrientation;
00120         aPrintSettings->GetOrientation(&orientation);
00121         SendDlgItemMessage(
00122             (orientation == nsIPrintSettings::kPortraitOrientation)
00123                 ? IDC_PORTRAIT : IDC_LANDSCAPE,
00124             BM_SETCHECK, 
00125             BST_CHECKED, 0);
00126 
00127         // Set scaling
00128         TCHAR szBuf[10];
00129         double scaling = 1.0;
00130         aPrintSettings->GetScaling(&scaling);
00131         _stprintf(szBuf, _T("%.1f"), scaling * 100.0);
00132         SetDlgItemText(IDC_SCALE, szBuf);
00133 
00134         // Set shrink to fit (& disable scale field)
00135         PRBool shrinkToFit = PR_FALSE;
00136         aPrintSettings->GetShrinkToFit(&shrinkToFit);
00137         CheckDlgButton(IDC_SHRINKTOFIT, shrinkToFit ? BST_CHECKED : BST_UNCHECKED);
00138         ::EnableWindow(GetDlgItem(IDC_SCALE), shrinkToFit ? FALSE : TRUE);
00139 
00140         // Print background - we use PrintBGColors to control both images & colours
00141         PRBool printBGColors = PR_TRUE;
00142         aPrintSettings->GetPrintBGColors(&printBGColors);
00143         CheckDlgButton(IDC_PRINTBACKGROUND, printBGColors ? BST_CHECKED : BST_UNCHECKED);
00144     }
00145 
00146     void Apply(nsIPrintSettings *aPrintSettings)
00147     {
00148 
00149         // Background options are tied to a single checkbox
00150         PRBool boolVal = 
00151             (SendDlgItemMessage(IDC_PRINTBACKGROUND, BM_GETCHECK) == BST_CHECKED) ?
00152                 PR_TRUE : PR_FALSE;
00153         aPrintSettings->SetPrintBGColors(boolVal);
00154         aPrintSettings->SetPrintBGImages(boolVal);
00155 
00156         // Print scale
00157         TCHAR szBuf[128];
00158         GetDlgItemText(IDC_SCALE, szBuf, sizeof(szBuf) / sizeof(szBuf[0]));
00159         double scale = _tstof(szBuf) / 100.0;
00160         aPrintSettings->SetScaling(scale);
00161 
00162         // Shrink to fit
00163         PRBool shrinkToFit =
00164             (IsDlgButtonChecked(IDC_SHRINKTOFIT) == BST_CHECKED) ? PR_TRUE : PR_FALSE;
00165         aPrintSettings->SetShrinkToFit(shrinkToFit);
00166 
00167         // Landscape or portrait
00168         PRInt32 orientation = nsIPrintSettings::kLandscapeOrientation;
00169         if (SendDlgItemMessage(IDC_PORTRAIT, BM_GETCHECK) == BST_CHECKED)
00170         {
00171             orientation = nsIPrintSettings::kPortraitOrientation;
00172         }
00173         aPrintSettings->SetOrientation(orientation);
00174     }
00175 
00176     LRESULT OnInitDialog(UINT uMsg, WPARAM wParam,  LPARAM lParam, BOOL& bHandled)
00177     {
00178         return 0;
00179     }
00180 
00181     LRESULT OnClickShrinkToFit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00182     {
00183         BOOL bEnableScale = TRUE;
00184         if (IsDlgButtonChecked(IDC_SHRINKTOFIT) == BST_CHECKED)
00185             bEnableScale = FALSE;
00186         ::EnableWindow(GetDlgItem(IDC_SCALE), bEnableScale);
00187         return 0;
00188     }
00189 };
00190 
00191 class CPageSetupMarginsDlg : public CDialogImpl<CPageSetupMarginsDlg>
00192 {
00193 public:
00194     nsString mHdrLeft;
00195     nsString mHdrCenter;
00196     nsString mHdrRight;
00197     nsString mFtrLeft;
00198     nsString mFtrCenter;
00199     nsString mFtrRight;
00200 
00201     enum { IDD = IDD_PPAGE_MARGINS };
00202 
00203     BEGIN_MSG_MAP(CPageSetupMarginsDlg)
00204         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
00205         COMMAND_RANGE_HANDLER(IDC_HDR_LEFT, IDC_FTR_RIGHT, OnHeaderFooterChange)
00206     END_MSG_MAP()
00207 
00208     CPageSetupMarginsDlg()
00209     {
00210     }
00211 
00212     const wchar_t * const * GetHeaderFooterValues(size_t &aNumValues) const
00213     {
00214         static const wchar_t *szValues[] = {
00215             L"",
00216             L"&T",
00217             L"&U",
00218             L"&D",
00219             L"&P",
00220             L"&PT"
00221         };
00222         aNumValues = sizeof(szValues) / sizeof(szValues[0]);
00223         return szValues;
00224     }
00225 
00226     const TCHAR * const * GetHeaderFooterOptions(size_t &aNumOptions) const
00227     {
00228         static const TCHAR *szOptions[] =
00229         {
00230             _T("-- Blank --"),
00231             _T("Title"),
00232             _T("URL"),
00233             _T("Date/Time"),
00234             _T("Page #"),
00235             _T("Page # of #"),
00236             _T("Custom...")
00237         };
00238         aNumOptions = sizeof(szOptions) / sizeof(szOptions[0]);
00239         return szOptions;
00240     }
00241 
00242     void Init(nsIPrintSettings *aPrintSettings)
00243     {
00244         double top = 0.0;
00245         double left = 0.0;
00246         double right = 0.0;
00247         double bottom = 0.0;
00248         aPrintSettings->GetMarginTop(&top);
00249         aPrintSettings->GetMarginLeft(&left);
00250         aPrintSettings->GetMarginRight(&right);
00251         aPrintSettings->GetMarginBottom(&bottom);
00252 
00253         // Get the margins
00254         TCHAR szBuf[16];
00255         _stprintf(szBuf, _T("%5.2f"), top);
00256         SetDlgItemText(IDC_MARGIN_TOP, szBuf);
00257         _stprintf(szBuf, _T("%5.2f"), left);
00258         SetDlgItemText(IDC_MARGIN_LEFT, szBuf);
00259         _stprintf(szBuf, _T("%5.2f"), right);
00260         SetDlgItemText(IDC_MARGIN_RIGHT, szBuf);
00261         _stprintf(szBuf, _T("%5.2f"), bottom);
00262         SetDlgItemText(IDC_MARGIN_BOTTOM, szBuf);
00263 
00264         // Get the header & footer settings
00265         PRUnichar* uStr = nsnull;
00266         aPrintSettings->GetHeaderStrLeft(&uStr);
00267         mHdrLeft = uStr;
00268         SetComboIndex(IDC_HDR_LEFT, uStr);
00269         if (uStr != nsnull) nsMemory::Free(uStr);
00270 
00271         aPrintSettings->GetHeaderStrCenter(&uStr);
00272         mHdrCenter = uStr;
00273         SetComboIndex(IDC_HDR_MIDDLE, uStr);
00274         if (uStr != nsnull) nsMemory::Free(uStr);
00275 
00276         aPrintSettings->GetHeaderStrRight(&uStr);
00277         mHdrRight = uStr;
00278         SetComboIndex(IDC_HDR_RIGHT, uStr);
00279         if (uStr != nsnull) nsMemory::Free(uStr);
00280 
00281         aPrintSettings->GetFooterStrLeft(&uStr);
00282         mFtrLeft = uStr;
00283         SetComboIndex(IDC_FTR_LEFT, uStr);
00284         if (uStr != nsnull) nsMemory::Free(uStr);
00285 
00286         aPrintSettings->GetFooterStrCenter(&uStr);
00287         mFtrCenter = uStr;
00288         SetComboIndex(IDC_FTR_MIDDLE, uStr);
00289         if (uStr != nsnull) nsMemory::Free(uStr);
00290 
00291         aPrintSettings->GetFooterStrRight(&uStr);
00292         mFtrRight = uStr;
00293         SetComboIndex(IDC_FTR_RIGHT, uStr);
00294         if (uStr != nsnull) nsMemory::Free(uStr);
00295     }
00296 
00297     void SetComboIndex(int aID, const wchar_t *szValue)
00298     {
00299         size_t nValues;
00300         const wchar_t * const * szValues = GetHeaderFooterValues(nValues);
00301 
00302         int nCurSel = 0;
00303         if (szValue[0] != L'\0')
00304         {
00305             while (nCurSel < nValues)
00306             {
00307                 if (wcscmp(szValue, szValues[nCurSel]) == 0)
00308                 {
00309                     break;
00310                 }
00311                 ++nCurSel;
00312             }
00313             // nCurSel might contain nValues but that just means the
00314             // Custom... field gets selected.
00315         }
00316         SendDlgItemMessage(aID, CB_SETCURSEL, nCurSel);
00317     }
00318 
00319     void Apply(nsIPrintSettings *aPrintSettings)
00320     {
00321         TCHAR szBuf[128];
00322         const size_t kBufSize = sizeof(szBuf) / sizeof(szBuf[0]);
00323 
00324         GetDlgItemText(IDC_MARGIN_TOP, szBuf, kBufSize);
00325         aPrintSettings->SetMarginTop(_tstof(szBuf));
00326         GetDlgItemText(IDC_MARGIN_LEFT, szBuf, kBufSize);
00327         aPrintSettings->SetMarginLeft(_tstof(szBuf));
00328         GetDlgItemText(IDC_MARGIN_BOTTOM, szBuf, kBufSize);
00329         aPrintSettings->SetMarginBottom(_tstof(szBuf));
00330         GetDlgItemText(IDC_MARGIN_RIGHT, szBuf, kBufSize);
00331         aPrintSettings->SetMarginRight(_tstof(szBuf));
00332 
00333         aPrintSettings->SetHeaderStrLeft(mHdrLeft.get());
00334         aPrintSettings->SetHeaderStrCenter(mHdrCenter.get());
00335         aPrintSettings->SetHeaderStrRight(mHdrRight.get());
00336         aPrintSettings->SetFooterStrLeft(mFtrLeft.get());
00337         aPrintSettings->SetFooterStrCenter(mFtrCenter.get());
00338         aPrintSettings->SetFooterStrRight(mFtrRight.get());
00339     }
00340 
00341     LRESULT OnInitDialog(UINT uMsg, WPARAM wParam,  LPARAM lParam, BOOL& bHandled)
00342     {
00343         size_t nOptions;
00344         const TCHAR * const *szOptions = GetHeaderFooterOptions(nOptions);
00345 
00346         // Fill out the drop down choices
00347         for (size_t i = 0; i < nOptions; ++i)
00348         {
00349             const TCHAR *szOption = szOptions[i];
00350             SendDlgItemMessage(IDC_HDR_LEFT, CB_ADDSTRING, 0, LPARAM(szOption));
00351             SendDlgItemMessage(IDC_HDR_MIDDLE, CB_ADDSTRING, 0, LPARAM(szOption));
00352             SendDlgItemMessage(IDC_HDR_RIGHT, CB_ADDSTRING, 0, LPARAM(szOption));
00353             SendDlgItemMessage(IDC_FTR_LEFT, CB_ADDSTRING, 0, LPARAM(szOption));
00354             SendDlgItemMessage(IDC_FTR_MIDDLE, CB_ADDSTRING, 0, LPARAM(szOption));
00355             SendDlgItemMessage(IDC_FTR_RIGHT, CB_ADDSTRING, 0, LPARAM(szOption));
00356         }
00357         return 0;
00358     }
00359 
00360     LRESULT OnHeaderFooterChange(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00361     {
00362         if (wNotifyCode != CBN_SELCHANGE)
00363         {
00364             bHandled = FALSE;
00365             return 0;
00366         }
00367 
00368         // One of the header / footer combos has changed, so set the string
00369         // to the updated value.
00370 
00371         nsString *pStr = nsnull;
00372         switch (wID)
00373         {
00374         case IDC_HDR_LEFT:   pStr = &mHdrLeft;   break;
00375         case IDC_HDR_MIDDLE: pStr = &mHdrCenter; break;
00376         case IDC_HDR_RIGHT:  pStr = &mHdrRight;  break;
00377         case IDC_FTR_LEFT:   pStr = &mFtrLeft;   break;
00378         case IDC_FTR_MIDDLE: pStr = &mFtrCenter; break;
00379         case IDC_FTR_RIGHT:  pStr = &mFtrRight;  break;
00380         }
00381         if (!pStr)
00382         {
00383             return 0;
00384         }
00385 
00386         size_t nValues;
00387         const wchar_t * const * szValues = GetHeaderFooterValues(nValues);
00388 
00389         int nCurSel = SendDlgItemMessage(wID, CB_GETCURSEL);
00390         if (nCurSel == nValues) // Custom...
00391         {
00392             CCustomFieldDlg dlg(*pStr);
00393             if (dlg.DoModal() == IDOK)
00394             {
00395                 *pStr = dlg.m_field;
00396             }
00397             // Update combo in case their custom value is not custom at all
00398             // For example, if someone opens the custom dlg and types "&P"
00399             // (i.e. "Page #"), we should select that since it is already a
00400             // choice in the combo.
00401             SetComboIndex(wID, pStr->get());
00402         }
00403         else
00404         {
00405             *pStr = szValues[nCurSel];
00406         }
00407 
00408         return 0;
00409     }
00410 
00411 };
00412 class CPageSetupDlg : public CDialogImpl<CPageSetupDlg>
00413 {
00414 public:
00415     enum { IDD = IDD_PAGESETUP };
00416 
00417     CPPageDlg *mPPage;
00418 
00419     BEGIN_MSG_MAP(CPageSetupDlg)
00420         MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
00421         NOTIFY_HANDLER(IDC_TAB, TCN_SELCHANGE, OnTabSelChange)
00422         COMMAND_ID_HANDLER(IDOK, OnOK)
00423         COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
00424     END_MSG_MAP()
00425 
00426     nsCOMPtr<nsIPrintSettings> mPrintSettings;
00427     CPageSetupFormatDlg mFormatDlg;
00428     CPageSetupMarginsDlg mMarginsDlg;
00429 
00430     CPageSetupDlg(nsIPrintSettings *aPrintSettings) :
00431         mPrintSettings(aPrintSettings)
00432     {
00433     }
00434 
00435     LRESULT OnInitDialog(UINT uMsg, WPARAM wParam,  LPARAM lParam, BOOL& bHandled)
00436     {
00437         // The marker tells us where to stick the pages
00438         RECT rcMarker;
00439         ::GetWindowRect(GetDlgItem(IDC_PAGE_MARKER), &rcMarker);
00440         ScreenToClient(&rcMarker);
00441 
00442          // Create the two pages, the first is shown, the second is not
00443         mFormatDlg.Create(m_hWnd);
00444         mFormatDlg.Init(mPrintSettings);
00445         mFormatDlg.SetWindowPos(HWND_TOP, &rcMarker, SWP_SHOWWINDOW);
00446         
00447         mMarginsDlg.Create(m_hWnd);
00448         mMarginsDlg.Init(mPrintSettings);
00449         mMarginsDlg.SetWindowPos(HWND_TOP, &rcMarker, SWP_HIDEWINDOW);
00450 
00451         // Get the tab control
00452         HWND hwndTab = GetDlgItem(IDC_TAB);
00453         
00454         TCITEM tcItem;
00455         
00456         memset(&tcItem, 0, sizeof(tcItem));
00457         tcItem.mask = TCIF_TEXT;
00458 
00459         tcItem.pszText = _T("Format && Options");
00460         TabCtrl_InsertItem(hwndTab, 0, &tcItem);
00461 
00462         tcItem.pszText = _T("Margins && Header / Footer");
00463         TabCtrl_InsertItem(hwndTab, 1, &tcItem);
00464 
00465         TabCtrl_SetCurSel(hwndTab, 0);
00466 
00467         return 0;
00468     }
00469 
00470     LRESULT OnTabSelChange(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
00471     {
00472         HWND hwndTab = GetDlgItem(IDC_TAB);
00473         if (TabCtrl_GetCurSel(hwndTab) == 0)
00474         {
00475             mFormatDlg.SetWindowPos(HWND_TOP, 0, 0, 0, 0,
00476                 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
00477             mMarginsDlg.ShowWindow(SW_HIDE); 
00478         }
00479         else
00480         {
00481             mMarginsDlg.SetWindowPos(HWND_TOP, 0, 0, 0, 0,
00482                 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
00483             mFormatDlg.ShowWindow(SW_HIDE); 
00484         }
00485         return 0;
00486     }
00487 
00488     LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00489     {
00490         mFormatDlg.Apply(mPrintSettings);
00491         mMarginsDlg.Apply(mPrintSettings);
00492         EndDialog(IDOK);
00493         return 0;
00494     }
00495 
00496     LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
00497     {
00498         EndDialog(IDCANCEL);
00499         return 0;
00500     }
00501 };
00502 
00503 #endif