Back to index

lightning-sunbird  0.9+nobinonly
nsPrintSetupDialog.cpp
Go to the documentation of this file.
00001 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCOMPtr.h"
00039 #include "nsPrintSetupDialog.h"
00040 #include "nsIDOMEvent.h"
00041 #include "nsIXPBaseWindow.h"
00042 #include "nsBrowserWindow.h"
00043 #include "nsIDOMEventTarget.h"
00044 
00045 #include "nsIDOMHTMLInputElement.h"
00046 #include "nsIDOMHTMLDocument.h"
00047 
00048 PrintSetupInfo::PrintSetupInfo(const PrintSetupInfo & aPSI) :
00049   mPortrait(aPSI.mPortrait),
00050   mBevelLines(aPSI.mBevelLines),
00051   mBlackText(aPSI.mBlackText),
00052   mBlackLines(aPSI.mBlackLines),
00053   mLastPageFirst(aPSI.mLastPageFirst),
00054   mPrintBackgrounds(aPSI.mPrintBackgrounds),
00055   mTopMargin(aPSI.mTopMargin),
00056   mBottomMargin(aPSI.mBottomMargin),
00057   mLeftMargin(aPSI.mLeftMargin),
00058   mRightMargin(aPSI.mRightMargin),
00059   mDocTitle(aPSI.mDocTitle),
00060   mDocLocation(aPSI.mDocLocation),
00061   mHeaderText(aPSI.mHeaderText),
00062   mPageNum(aPSI.mPageNum),
00063   mPageTotal(aPSI.mPageTotal),
00064   mDatePrinted(aPSI.mDatePrinted),
00065   mFooterText(aPSI.mFooterText)
00066 {
00067 }
00068 
00069 
00070 //-------------------------------------------------------------------------
00071 //
00072 // nsPrintSetupDialog constructor
00073 //
00074 //-------------------------------------------------------------------------
00075 //-----------------------------------------------------------------
00076 nsPrintSetupDialog::nsPrintSetupDialog(nsBrowserWindow * aBrowserWindow) :
00077   nsBaseDialog(aBrowserWindow),
00078   mOKBtn(nsnull),
00079   mPrinterSetupInfo(nsnull)
00080 {
00081 }
00082 
00083 //-----------------------------------------------------------------
00084 nsPrintSetupDialog::~nsPrintSetupDialog()
00085 {
00086 }
00087 
00088 //---------------------------------------------------------------
00089 void nsPrintSetupDialog::Initialize(nsIXPBaseWindow * aWindow) 
00090 {
00091   nsBaseDialog::Initialize(aWindow);
00092 
00093   nsIDOMHTMLDocument *doc = nsnull;
00094   mWindow->GetDocument(doc);
00095   if (nsnull != doc) {
00096     doc->GetElementById(NS_LITERAL_STRING("ok"),       &mOKBtn);
00097 
00098     // XXX: Register event listening on each dom element. We should change this so
00099     // all DOM events are automatically passed through.
00100     mWindow->AddEventListener(mOKBtn);
00101     //SetChecked(mMatchCaseCB, PR_FALSE);
00102     
00103     NS_RELEASE(doc);
00104 
00105     if (nsnull != mPrinterSetupInfo) {
00106       SetSetupInfoInternal(*mPrinterSetupInfo);
00107       delete mPrinterSetupInfo;
00108       mPrinterSetupInfo = nsnull;
00109     }
00110   }
00111 }
00112 
00113 //-----------------------------------------------------------------
00114 void nsPrintSetupDialog::GetSetupInfo(PrintSetupInfo & aPSI)
00115 {
00116   aPSI.mPortrait         = IsChecked(NS_LITERAL_STRING("portrait"));
00117   aPSI.mBevelLines       = IsChecked(NS_LITERAL_STRING("bevellines"));
00118   aPSI.mBlackText        = IsChecked(NS_LITERAL_STRING("blacktext"));
00119   aPSI.mBlackLines       = IsChecked(NS_LITERAL_STRING("blacklines"));
00120   aPSI.mLastPageFirst    = IsChecked(NS_LITERAL_STRING("lastpagefirst"));
00121   aPSI.mPrintBackgrounds = IsChecked(NS_LITERAL_STRING("printbg"));
00122 
00123   nsString str;
00124   GetText(NS_LITERAL_STRING("toptext"), str);
00125   aPSI.mTopMargin = GetFloat(str);
00126 
00127   GetText(NS_LITERAL_STRING("lefttext"), str);
00128   aPSI.mLeftMargin = GetFloat(str);
00129 
00130   GetText(NS_LITERAL_STRING("bottomtext"), str);
00131   aPSI.mBottomMargin = GetFloat(str);
00132 
00133   GetText(NS_LITERAL_STRING("righttext"), str);
00134   aPSI.mRightMargin = GetFloat(str);
00135 
00136   GetText(NS_LITERAL_STRING("lefttext"),   str);
00137   GetText(NS_LITERAL_STRING("bottomtext"), str);
00138   GetText(NS_LITERAL_STRING("righttext"),  str);
00139 
00140   nsString header;
00141   nsString footer;
00142   GetText(NS_LITERAL_STRING("headertext"),  header);
00143   GetText(NS_LITERAL_STRING("footertext"),  footer);
00144   aPSI.mHeaderText = header;
00145   aPSI.mFooterText = footer;
00146 
00147   //GetText("headertext",  aPSI.mHeaderText);
00148   //GetText("footertext",  aPSI.mFooterText);
00149 
00150   aPSI.mDocTitle    = IsChecked(NS_LITERAL_STRING("doctitle"));
00151   aPSI.mDocLocation = IsChecked(NS_LITERAL_STRING("docloc"));
00152   aPSI.mPageNum     = IsChecked(NS_LITERAL_STRING("pagenum"));
00153   aPSI.mPageTotal   = IsChecked(NS_LITERAL_STRING("pagetotal"));
00154   aPSI.mDatePrinted = IsChecked(NS_LITERAL_STRING("dateprinted"));
00155 
00156 }
00157 
00158 //-----------------------------------------------------------------
00159 void nsPrintSetupDialog::SetSetupInfo(const PrintSetupInfo & aPSI)
00160 {
00161   if (nsnull == mWindow) {
00162     mPrinterSetupInfo = new PrintSetupInfo(aPSI);
00163   } else {
00164     SetSetupInfoInternal(aPSI);
00165   }
00166 }
00167 
00168 //-----------------------------------------------------------------
00169 void nsPrintSetupDialog::SetSetupInfoInternal(const PrintSetupInfo & aPSI)
00170 {
00171   SetChecked(NS_LITERAL_STRING("portrait"),      aPSI.mPortrait);
00172   SetChecked(NS_LITERAL_STRING("bevellines"),    aPSI.mBevelLines);
00173   SetChecked(NS_LITERAL_STRING("blacktext"),     aPSI.mBlackText);
00174   SetChecked(NS_LITERAL_STRING("blacklines"),    aPSI.mBlackLines);
00175   SetChecked(NS_LITERAL_STRING("lastpagefirst"), aPSI.mLastPageFirst);
00176   SetChecked(NS_LITERAL_STRING("printbg"),       aPSI.mPrintBackgrounds);
00177 
00178   char str[64];
00179   sprintf(str, "%5.2f", aPSI.mTopMargin);
00180   SetText(NS_LITERAL_STRING("toptext"), NS_ConvertASCIItoUCS2(str));
00181   sprintf(str, "%5.2f", aPSI.mLeftMargin);
00182   SetText(NS_LITERAL_STRING("lefttext"), NS_ConvertASCIItoUCS2(str));
00183   sprintf(str, "%5.2f", aPSI.mBottomMargin);
00184   SetText(NS_LITERAL_STRING("bottomtext"), NS_ConvertASCIItoUCS2(str));
00185   sprintf(str, "%5.2f", aPSI.mRightMargin);
00186   SetText(NS_LITERAL_STRING("righttext"),  NS_ConvertASCIItoUCS2(str));
00187 
00188   SetText(NS_LITERAL_STRING("headertext"),  aPSI.mHeaderText);
00189   SetText(NS_LITERAL_STRING("footertext"),  aPSI.mFooterText);
00190 
00191   SetChecked(NS_LITERAL_STRING("doctitle"),    aPSI.mDocTitle);
00192   SetChecked(NS_LITERAL_STRING("docloc"),      aPSI.mDocLocation);
00193   SetChecked(NS_LITERAL_STRING("pagenum"),     aPSI.mPageNum);
00194   SetChecked(NS_LITERAL_STRING("pagetotal"),   aPSI.mPageTotal);
00195   SetChecked(NS_LITERAL_STRING("dateprinted"), aPSI.mDatePrinted);
00196 
00197 }
00198 
00199 //-----------------------------------------------------------------
00200 void nsPrintSetupDialog::MouseClick(nsIDOMEvent* aMouseEvent, nsIXPBaseWindow * aWindow, PRBool &aStatus)
00201 {
00202    // Event Dispatch. This method should not contain
00203    // anything but calls to methods. This idea is that this dispatch
00204    // mechanism may be replaced by JavaScript EventHandlers which call the idl'ed
00205    // interfaces to perform the same operation that is currently being handled by
00206    // this C++ code.
00207 
00208   nsBaseDialog::MouseClick(aMouseEvent, aWindow, aStatus);
00209   if (!aStatus) { // is not consumed
00210     nsCOMPtr<nsIDOMEventTarget> target;
00211     aMouseEvent->GetTarget(getter_AddRefs(target));
00212     if (target) {
00213       nsCOMPtr<nsIDOMElement> node(do_QueryInterface(target));
00214       if (node.get() == mOKBtn) {
00215         GetSetupInfo(mBrowserWindow->mPrintSetupInfo);
00216         DoClose();
00217       } 
00218     }
00219   }
00220 }
00221 
00222 //-----------------------------------------------------------------
00223 void nsPrintSetupDialog::Destroy(nsIXPBaseWindow * aWindow)
00224 {
00225   // Unregister event listeners that were registered in the
00226   // Initialize here. 
00227   // XXX: Should change code in XPBaseWindow to automatically unregister
00228   // all event listening, That way this code will not be necessary.
00229   aWindow->RemoveEventListener(mOKBtn);
00230 }
00231 
00232 
00233 void
00234 nsPrintSetupDialog::DoClose()
00235 {
00236   mWindow->SetVisible(PR_FALSE);
00237 }
00238 
00239