examples/SysLibs/Clipboard/Basics/Basics.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Example demonstrates a simple use of the clipboard.
00015 //
00016                 
00017 #include <baclipb.h>    
00018 
00019 #include "CommonToResourceFilesEx.h"
00020 #include "Basics.h"
00021 
00022 
00025 
00026 // Do the example(s)
00027 
00028 static void doExampleL()
00029     {
00030         doCopyL();
00031         doPasteL();
00032         }
00033 
00036 
00037 
00038 static void doCopyL()
00039         {
00040         _LIT(KSomeText,"Some text");
00041         _LIT(KContentOfTheCClassAObject,"Content of the CClassA object ...");
00042 
00043         // Construct an object of type CClassA
00044         CClassA*  item = new (ELeave) CClassA;
00045         CleanupStack::PushL(item);
00046 
00047         // Put some data into it
00048         item->iBuffer = KSomeText;
00049         item->iXA     = -1;
00050         item->iYA     = 2;
00051 
00052         // Show contents of the CClassA object
00053         doShow(KContentOfTheCClassAObject,item);
00054 
00055         // Construct the clipboard object and prepare the 
00056         // clipboard for writing
00057         CClipboard* cb = CClipboard::NewForWritingLC(fsSession);
00058 
00059         // Now put the object onto the clipboard and identify the data with
00060         // the uid KExampleClipUid. In a real app, this would typically be 
00061         // done in response to a request from the user interface to cut or copy.
00062         //
00063         // Note that the value of KExampleClipUid is arbitrary and is used
00064         // solely for the purpose oif demonstration
00065         RStoreWriteStream  stream;
00066         TStreamId stid = stream.CreateLC(cb->Store());
00067         stream << *item;
00068         stream.CommitL();
00069         (cb->StreamDictionary()).AssignL(KExampleClipUid,stid);
00070         CleanupStack::PopAndDestroy(); //the stream
00071         
00072         // commit the clipboard - this writes the stream dictionary to the
00073         // store as the root stream and commits all changes to the store 
00074         cb->CommitL();
00075 
00076         // Delete the clipboard object - this closes the clipboard file store
00077         // and delete the CClassA object
00078         CleanupStack::PopAndDestroy(2);
00079         }
00080 
00083 
00084 static void doPasteL()
00085         {
00086         _LIT(KNothingToPaste,"Nothing to paste");
00087         _LIT(KTheCClassAObjectAfterPastingIn,"The CClassA object after pasting in ...");
00088 
00089         CClipboard* cb = NULL;
00090 
00091         // Construct the clipboard object and prepare the 
00092         // clipboard for reading.
00093         TRAPD(ret,cb=CClipboard::NewForReadingL(fsSession));
00094         CleanupStack::PushL(cb);
00095     if (ret!=KErrNone)
00096                 {
00097                 doShow(KNothingToPaste,NULL);
00098                 User::Leave(ret);
00099                 }
00100                 
00101         // Construct an object of type CClassA
00102         CClassA*  item = new (ELeave) CClassA;
00103         CleanupStack::PushL(item);
00104 
00105         // Check whether there is a CClassA object on the clipboard
00106         TStreamId stid = (cb->StreamDictionary()).At(KExampleClipUid);
00107         if (stid == KNullStreamId)
00108                 {
00109                 doShow(KNothingToPaste,NULL);
00110                 User::Leave(0);
00111                 }
00112         
00113         // Fetch the CClassA object from the clipboard
00114         RStoreReadStream stream;
00115         stream.OpenLC(cb->Store(),stid);
00116         stream >> *item;
00117         CleanupStack::PopAndDestroy();  // the stream
00118         
00119         // Show contents of the CClassA object as pasted in
00120         // from the  clipboard.
00121         doShow(KTheCClassAObjectAfterPastingIn,item);
00122 
00123         // delete:
00124         // 1. the CClassA object
00125         // 2. the clipboard object
00126         CleanupStack::PopAndDestroy(2);
00127         }
00128 
00131 
00132 static void doShow(const TDesC& aHeading,const CClassA* anItem)
00133         {
00134         _LIT(KNewline,"\n");
00135         _LIT(KFormatS,"\n%S");
00136         _LIT(KFormatD,"\n%d");
00137         _LIT(KFormatU,"\n%u");
00138 
00139         console->Printf(KNewline);
00140         console->Printf(aHeading);
00141         if (anItem)
00142                 {
00143                 console->Printf(KFormatS,&anItem->iBuffer);
00144                 console->Printf(KFormatD,anItem->iXA);
00145                 console->Printf(KFormatU,anItem->iYA);
00146                 console->Printf(KNewline);
00147                 }
00148         }
00149 
00152 
00153 CClassA::CClassA()
00154         {
00155         _LIT(KDefault,"DEFAULT");
00156         iBuffer = KDefault;
00157         }
00158 
00159 void CClassA::ExternalizeL(RWriteStream& aStream) const
00160         {
00161         aStream << iBuffer;
00162         aStream.WriteInt32L(iXA);
00163         aStream.WriteUint32L(iYA);
00164         }  
00165  
00166 void CClassA::InternalizeL(RReadStream& aStream)
00167         {
00168         aStream >> iBuffer;
00169         iXA  = aStream.ReadInt32L();
00170         iYA  = aStream.ReadUint32L();
00171         }  
00172         
00173                 
00174 
00175         
00176          
00177 

Generated by  doxygen 1.6.2