Back to index

lightning-sunbird  0.9+nobinonly
CFormatOptionTab.cpp
Go to the documentation of this file.
00001 // FormatOptionTab.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "mfcembed.h"
00006 #include "CFormatOptionTab.h"
00007 
00008 #ifdef _DEBUG
00009 #define new DEBUG_NEW
00010 #undef THIS_FILE
00011 static char THIS_FILE[] = __FILE__;
00012 #endif
00013 
00014 const int kMinScaleRange = 30;
00015 
00017 typedef struct {
00018   TCHAR*   mDesc;
00019   short   mUnit;
00020   double  mWidth;
00021   double  mHeight;
00022   BOOL    mIsUserDefined;
00023 } PaperSizes;
00024 
00025 static const PaperSizes gPaperSize[] = {
00026   {_T("Letter (8.5 x 11.0)"), nsIPrintSettings::kPaperSizeInches, 8.5, 11.0, FALSE},
00027   {_T("Legal (8.5 x 14.0)"),  nsIPrintSettings::kPaperSizeInches, 8.5, 14.0, FALSE},
00028   {_T("A4 (210 x 297mm)"),    nsIPrintSettings::kPaperSizeMillimeters, 210.0, 297.0, FALSE},
00029   {_T("User Defined"),        nsIPrintSettings::kPaperSizeInches, 8.5, 11.0, TRUE}
00030 };
00031 static const int gNumPaperSizes = 4;
00032 
00033 
00035 // CFormatOptionTab property page
00036 
00037 IMPLEMENT_DYNCREATE(CFormatOptionTab, CPropertyPage)
00038 
00039 CFormatOptionTab::CFormatOptionTab() : CPropertyPage(CFormatOptionTab::IDD)
00040 {
00041          //{{AFX_DATA_INIT(CFormatOptionTab)
00042          m_PaperSize = -1;
00043          m_BGColors = FALSE;
00044          m_Scaling = 0;
00045          m_ScalingText = _T("");
00046          m_PaperHeight = 0.0;
00047          m_PaperWidth = 0.0;
00048          m_DoInches = -1;
00049          m_BGImages = FALSE;
00050          m_IsLandScape = FALSE;
00051          //}}AFX_DATA_INIT
00052 }
00053 
00054 CFormatOptionTab::~CFormatOptionTab()
00055 {
00056 }
00057 
00058 void CFormatOptionTab::DoDataExchange(CDataExchange* pDX)
00059 {
00060          CPropertyPage::DoDataExchange(pDX);
00061          //{{AFX_DATA_MAP(CFormatOptionTab)
00062          DDX_CBIndex(pDX, IDC_PAPER_SIZE_CBX, m_PaperSize);
00063          DDX_Check(pDX, IDC_PRT_BGCOLORS, m_BGColors);
00064          DDX_Slider(pDX, IDC_SCALE, m_Scaling);
00065          DDX_Text(pDX, IDC_SCALE_TXT, m_ScalingText);
00066          DDX_Text(pDX, IDC_UD_PAPER_HGT, m_PaperHeight);
00067          DDX_Text(pDX, IDC_UD_PAPER_WDTH, m_PaperWidth);
00068          DDX_Radio(pDX, IDC_INCHES_RD, m_DoInches);
00069          DDX_Check(pDX, IDC_PRT_BGIMAGES, m_BGImages);
00070          DDX_Radio(pDX, IDC_PRT_PORTRAIT_RD, m_IsLandScape);
00071          //}}AFX_DATA_MAP
00072 }
00073 
00074 
00075 BEGIN_MESSAGE_MAP(CFormatOptionTab, CPropertyPage)
00076        //{{AFX_MSG_MAP(CFormatOptionTab)
00077        ON_NOTIFY(NM_CUSTOMDRAW, IDC_SCALE, OnCustomdrawScale)
00078        ON_EN_KILLFOCUS(IDC_SCALE_TXT, OnKillfocusScaleTxt)
00079        ON_CBN_SELCHANGE(IDC_PAPER_SIZE_CBX, OnSelchangePaperSizeCbx)
00080        //}}AFX_MSG_MAP
00081 END_MESSAGE_MAP()
00082 
00084 // CFormatOptionTab message handlers
00085 
00086 BOOL CFormatOptionTab::OnInitDialog() 
00087 {
00088          CDialog::OnInitDialog();
00089          
00090     CSliderCtrl* scale = (CSliderCtrl*)GetDlgItem(IDC_SCALE);
00091     CWnd* scaleTxt     = GetDlgItem(IDC_SCALE_TXT);
00092     if (scale != NULL) 
00093     {
00094         scale->SetRange(kMinScaleRange, 100);
00095         scale->SetBuddy(scaleTxt, FALSE);
00096         scale->SetTicFreq(10);
00097     }
00098 
00099          CComboBox* cbx = (CComboBox*)GetDlgItem(IDC_PAPER_SIZE_CBX);
00100     if (cbx != NULL) 
00101     {
00102         // First Initialize the Combobox
00103         for (int i=0;i<gNumPaperSizes;i++) 
00104         {
00105             cbx->AddString(gPaperSize[i].mDesc);
00106         }
00107         short  unit;
00108         double paperWidth  = 0.0;
00109         double paperHeight = 0.0;
00110         m_PrintSettings->GetPaperSizeType(&unit);
00111         m_PrintSettings->GetPaperWidth(&paperWidth);
00112         m_PrintSettings->GetPaperHeight(&paperHeight);
00113 
00114         m_PaperSizeInx = GetPaperSizeIndexFromData(unit, paperWidth, paperHeight);
00115         if (m_PaperSizeInx == -1) 
00116         { // couldn't find a match
00117             m_PaperSizeInx = 0;
00118             unit        = gPaperSize[m_PaperSizeInx].mUnit;
00119             paperWidth  = gPaperSize[m_PaperSizeInx].mWidth;
00120             paperHeight = gPaperSize[m_PaperSizeInx].mHeight;
00121         }
00122 
00123         cbx->SetCurSel(m_PaperSizeInx);
00124 
00125         EnableUserDefineControls(gPaperSize[m_PaperSizeInx].mIsUserDefined);
00126 
00127         if (gPaperSize[m_PaperSizeInx].mIsUserDefined) 
00128         {
00129             CString wStr;
00130             CString hStr;
00131             if (unit == nsIPrintSettings::kPaperSizeInches) 
00132             {
00133                 wStr.Format(_T("%6.2f"), paperWidth);
00134                 hStr.Format(_T("%6.2f"), paperHeight);
00135                 CheckRadioButton(IDC_INCHES_RD, IDC_MILLI_RD, IDC_INCHES_RD);
00136             } 
00137             else 
00138             {
00139                 wStr.Format(_T("%d"), int(paperWidth));
00140                 hStr.Format(_T("%d"), int(paperHeight));
00141                 CheckRadioButton(IDC_INCHES_RD, IDC_MILLI_RD, IDC_MILLI_RD);
00142             }
00143                  CWnd* widthTxt  = GetDlgItem(IDC_UD_PAPER_WDTH);
00144                  CWnd* heightTxt = GetDlgItem(IDC_UD_PAPER_HGT);
00145             widthTxt->SetWindowText(wStr);
00146             heightTxt->SetWindowText(hStr);
00147         } 
00148         else 
00149         {
00150             CheckRadioButton(IDC_INCHES_RD, IDC_MILLI_RD, IDC_INCHES_RD);
00151         }
00152     }
00153 
00154          return TRUE;  // return TRUE unless you set the focus to a control
00155                        // EXCEPTION: OCX Property Pages should return FALSE
00156 }
00157 
00158 void CFormatOptionTab::OnCustomdrawScale(NMHDR* pNMHDR, LRESULT* pResult) 
00159 {
00160     CSliderCtrl* scale = (CSliderCtrl*)GetDlgItem(IDC_SCALE);
00161     CWnd* scaleTxt     = GetDlgItem(IDC_SCALE_TXT);
00162     if (scale != NULL && scaleTxt != NULL) 
00163     {
00164         CString str;
00165         str.Format(_T("%d"), scale->GetPos());
00166         scaleTxt->SetWindowText(str);
00167     }
00168          
00169          *pResult = 0;
00170 }
00171 
00172 static int GetIntFromStr(const TCHAR * aStr, int aMinVal = kMinScaleRange, int aMaxVal = 100)
00173 {
00174     int val = aMinVal;
00175     _stscanf(aStr, _T("%d"), &val);
00176     if (val < aMinVal) 
00177     {
00178         return aMinVal;
00179     } 
00180     else if (val > aMaxVal) 
00181     {
00182         return aMaxVal;
00183     }
00184     return val;
00185 }
00186 
00187 void CFormatOptionTab::OnKillfocusScaleTxt() 
00188 {
00189     CSliderCtrl* scale = (CSliderCtrl*)GetDlgItem(IDC_SCALE);
00190     CWnd* scaleTxt     = GetDlgItem(IDC_SCALE_TXT);
00191     if (scale != NULL && scaleTxt != NULL) 
00192     {
00193         CString str;
00194         scaleTxt->GetWindowText(str);
00195         scale->SetPos(GetIntFromStr(str));
00196     }  
00197 }
00198 
00199 
00200 void CFormatOptionTab::EnableUserDefineControls(BOOL aEnable) 
00201 {
00202          CWnd* cntrl = GetDlgItem(IDC_UD_WIDTH_LBL);
00203     cntrl->EnableWindow(aEnable);
00204          cntrl = GetDlgItem(IDC_UD_HEIGHT_LBL);
00205     cntrl->EnableWindow(aEnable);
00206          cntrl = GetDlgItem(IDC_UD_PAPER_WDTH);
00207     cntrl->EnableWindow(aEnable);
00208          cntrl = GetDlgItem(IDC_UD_PAPER_HGT);
00209     cntrl->EnableWindow(aEnable);
00210          cntrl = GetDlgItem(IDC_INCHES_RD);
00211     cntrl->EnableWindow(aEnable);
00212          cntrl = GetDlgItem(IDC_MILLI_RD);
00213     cntrl->EnableWindow(aEnable);
00214 }
00215 
00216 void CFormatOptionTab::OnSelchangePaperSizeCbx() 
00217 {
00218          CComboBox* cbx = (CComboBox*)GetDlgItem(IDC_PAPER_SIZE_CBX);
00219     if (cbx) 
00220     {
00221         CString text;
00222         cbx->GetWindowText(text);
00223         m_PaperSizeInx = GetPaperSizeIndex(text);
00224         EnableUserDefineControls(gPaperSize[m_PaperSizeInx].mIsUserDefined);
00225     }
00226 }
00227 
00228 void CFormatOptionTab::GetPaperSizeInfo(short& aUnit, double& aWidth, double& aHeight)
00229 {
00230     if (gPaperSize[m_PaperSizeInx].mIsUserDefined) 
00231     {
00232         aUnit   = m_DoInches == 0?nsIPrintSettings::kPaperSizeInches:nsIPrintSettings::kPaperSizeMillimeters;
00233         aWidth  = m_PaperWidth;
00234         aHeight = m_PaperHeight;
00235     } 
00236     else 
00237     {
00238         aUnit   = gPaperSize[m_PaperSizeInx].mUnit;
00239         aWidth  = gPaperSize[m_PaperSizeInx].mWidth;
00240         aHeight = gPaperSize[m_PaperSizeInx].mHeight;
00241     }
00242 }
00243 
00244 // Search for Sizes in Pape Size Data
00245 int CFormatOptionTab::GetPaperSizeIndexFromData(short aUnit, double aW, double aH) 
00246 {
00247     int i;
00248     for (i=0;i<gNumPaperSizes;i++) 
00249     {
00250         if (gPaperSize[i].mUnit == aUnit && 
00251             gPaperSize[i].mWidth == aW &&
00252             gPaperSize[i].mHeight == aH) 
00253         {
00254             return i;
00255         }
00256     }
00257 
00258     // find the first user defined
00259     for (i=0;i<gNumPaperSizes;i++) 
00260     {
00261         if (gPaperSize[i].mIsUserDefined) 
00262         {
00263             return i;
00264         }
00265     }
00266 
00267     return -1;
00268 }
00269 
00270 int CFormatOptionTab::GetPaperSizeIndex(const CString& aStr) 
00271 {
00272     for (int i=0;i<gNumPaperSizes;i++) 
00273     {
00274         if (!aStr.Compare(gPaperSize[i].mDesc)) 
00275         {
00276             return i;
00277         }
00278     }
00279     return -1;
00280 }
00281