examples/Graphics/PDRStore/PDRExample.cpp

00001 /*
00002 Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include "PDRExample.h"
00032 
00033 
00034 CPdrExample::CPdrExample()
00035         {}
00036 
00037 CPdrExample::~CPdrExample()
00038         {
00039         _LIT(KExit,"\n\nPress any key to exit the application ");
00040         iConsole->Printf(KExit);
00041         iConsole->Getch();
00042         
00043         // Free user defined objects
00044         delete iDriver;
00045         delete iStopper;
00046         RFbsSession::Disconnect();
00047         delete iConsole;
00048         }
00049 
00050 CPdrExample* CPdrExample::NewL()
00051         {
00052         CPdrExample* self= new (ELeave) CPdrExample();
00053         CleanupStack::PushL(self);
00054         self->ConstructL();
00055         CleanupStack::Pop();
00056         return self;
00057         }
00058 
00059 void CPdrExample::ConstructL()
00060         {
00061         iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00062 
00063         _LIT(KTextPressAKey, "\n\nPress any key to step through the example");
00064         iConsole->Printf ( KTextPressAKey );
00065         iConsole->Getch ();
00066         
00067         iConsole->Printf ( _L("\nLoading printer driver...\n") );
00068         
00069         // Load printer driver
00070         User::LoadPhysicalDevice(PDD_NAME);
00071         // Load printer device
00072         User::LoadLogicalDevice(LDD_NAME);
00073         FbsStartup();
00074         
00075         // Open a session with the font and bitmap server
00076         RFbsSession::Connect();         
00077         }
00078         
00079 // Create a printer driver
00080 void CPdrExample::CreatePrinterDriverL()
00081         {
00082                 
00083         iStopper=new(ELeave) CPdrPrint();
00084         CActiveScheduler::Add(iStopper);
00085 
00086         // Create a printer driver object
00087         iDriver=CPrinterDriver::NewL();         
00088         
00089         TFileName filename( KDriveName );
00090         filename.Append( KDefaultPrinterDriverPath );
00091         filename.Append( KEpsonFileName );
00092         
00093         iConsole->Printf ( _L("\nOpening printer driver information file...\n") );
00094         
00095         // Open a printer driver information(.pdr) file
00096         TInt ret;
00097         TRAP(ret,iDriver->OpenPdrL( filename ));
00098         if ( ret == KErrNone )
00099                 {
00100                 iConsole->Printf(_L("\nNumber of models: %d\n"),iDriver->NumModels());          
00101                 }
00102 
00103         // Get models
00104         TPrinterModelEntry entry;
00105         for (TInt i=0; i<iDriver->NumModels(); i++)
00106                 {
00107                 entry=iDriver->Model(i);
00108                 iConsole->Printf(_L("Model: %S\n"),&entry.iModelName);
00109                 }
00110         
00111         iConsole->Getch();
00112         iConsole->Printf(_L("\nSelect model from 1 to %d\n"),iDriver->NumModels());
00113         iConsole->Printf(_L("\nSelecting... %d\n"),iDriver->NumModels());
00114         iConsole->Printf(_L("\nPrinter driver properties:\n"));
00115 
00116         // Create a printer device              
00117         iDevice=NULL;
00118         entry=iDriver->Model(iDriver->NumModels()-1);
00119         TRAP(ret,iDevice=iDriver->CreatePrinterDeviceL(entry.iUid));
00120         if ( ret == KErrNone )
00121                 {
00122                 iConsole->Printf(_L("PDR device: %d\n"),iDevice);
00123                 iConsole->Printf(_L("No. of typefaces: %d\n"),iDevice->NumTypefaces());
00124                 TPrinterModelName iModelName = iDevice->Model().iModelName;
00125                 iConsole->Printf(_L("Model name: %S\n"),&iModelName );
00126                 iConsole->Getch();
00127                 
00128                 TPageSpec pagespec(TPageSpec::EPortrait,KA4PaperSizeInTwips);
00129                 iDevice->SelectPageSpecInTwips(pagespec);
00130         
00131                 }       
00132         }
00133 
00134 // Display supported typefaces 
00135 void CPdrExample::DisplayTypefaces()
00136         {
00137         // Get typefaces 
00138         TTypefaceSupport support;
00139         iDevice->TypefaceSupport(support,0);
00140         TBuf<KMaxTypefaceNameLength> name;
00141                 
00142         iConsole->Printf(_L("\nSupported typefaces:\n"));
00143         TInt i;
00144         for (i=0; i<iDevice->NumTypefaces(); i++)
00145                 {
00146                 iDevice->TypefaceSupport(support,i);
00147                 name.Copy(support.iTypeface.iName);
00148                 iConsole->Printf(_L(" %S\n"),&name);
00149                 }
00150         iConsole->Getch();
00151         }
00152 
00153 // Create a printer control     
00154 void CPdrExample::CreatePrinterControl()
00155         {       
00156         CPrinterPort* printerport=NULL;
00157 
00158         if (iDevice->Model().iRequiresPrinterPort)
00159                 {
00160                 TInt ret;
00161                 TRAP(ret,printerport=CFilePrinterPort::NewL(_L("\\temp.prn")));
00162                 if (ret==KErrNone)
00163                         {
00164                         iConsole->Printf(_L("\nCreate a printer control to process and terminate the print job\n"));
00165                         TRAP(ret,iDevice->CreateControlL(printerport));
00166                         }               
00167                 }
00168         }
00169 
00170         
00171 // Create a graphic context, process and terminate the print job
00172 void CPdrExample::PrintPages()
00173         {
00174         
00175         TBandAttributes attributes;
00176         CPrinterControl::TMoreOnPage moreonpage=CPrinterControl::EMoreOnPage;
00177 
00178         TInt h=iDevice->HorizontalPixelsToTwips(1000);
00179         TInt v=iDevice->VerticalPixelsToTwips(1000);
00180         TInt k=4800;
00181 
00182         TInt nuBands = iDevice->iControl->BandsPerPage();
00183         iConsole->Printf(_L("Number of bands per page: %d\n"), nuBands);
00184         iConsole->Printf(_L("Printing bands on the page...\n"));
00185         iConsole->Getch();
00186 
00187         TInt i=0;       
00188         do
00189                 {
00190                 iConsole->Printf(_L("Band %d\n"),i+1);
00191                 moreonpage=iDevice->iControl->QueueGetBand(iStopper->Queue(),attributes);
00192                 CActiveScheduler::Start();
00193                 
00194                 // Create a graphic context for breaking the drawing into bands
00195                 CGraphicsContext* gc=NULL;
00196                 iDevice->CreateContext((CGraphicsContext *&) gc);
00197                 gc->SetPenColor(KRgbBlack);                                                      
00198                 gc->SetPenStyle(CGraphicsContext::ESolidPen);
00199                 gc->SetPenSize(TSize(4*k/h,4*k/v));
00200                 gc->DrawEllipse(TRect(391,352,1152,601));       
00201                 delete gc;
00202                 i++;
00203                 }
00204         while (moreonpage==CPrinterControl::EMoreOnPage);
00205         
00206         iConsole->Printf(_L("\nPrinting completed\n"));
00207         iDevice->iControl->QueueEndPrint(iStopper->Queue());
00208         CActiveScheduler::Start();
00209         }
00210 
00211 LOCAL_C void MainL()
00212         {
00213         CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00214         CleanupStack::PushL(scheduler);
00215         CActiveScheduler::Install( scheduler );
00216 
00217         CPdrExample* app = CPdrExample::NewL();
00218         CleanupStack::PushL(app);
00219         
00220         // Load the printer driver
00221         app->CreatePrinterDriverL();
00222         // Display supported typefaces 
00223         app->DisplayTypefaces();
00224         // Create a printer control for handling the print jobs
00225         app->CreatePrinterControl();
00226         // Process and terminate the print job
00227         app->PrintPages();
00228         
00229         CleanupStack::PopAndDestroy(2,scheduler);
00230         }
00231 
00232 GLDEF_C TInt E32Main()
00233         {
00234     __UHEAP_MARK;
00235     CTrapCleanup* cleanup = CTrapCleanup::New();
00236     if(cleanup == NULL)
00237         {
00238         return KErrNoMemory;
00239         }
00240     TRAPD(err, MainL());
00241         if(err != KErrNone)
00242                 {
00243                 User::Panic(_L("Failed to complete"),err);
00244                 }
00245 
00246     delete cleanup;
00247     __UHEAP_MARKEND;
00248     return KErrNone;
00249         }
00250 
00251 
00252 

Generated by  doxygen 1.6.2