Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


Clipboard example code

[Top]


Basics: store and restore object using clipboard


Example Code

This code can also be found in: examples\SysLibs\ClipBoard\Basics

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

Basics.h

// Basics.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.



//  This header file contains the class definitions and local
//  
//  function prototypes used by the example
    
    
const TUid KExampleClipUid={2306};
 
class CClassA : public CBase
    {
public :
    CClassA();
    void ExternalizeL(RWriteStream& aStream) const;
    void InternalizeL(RReadStream& aStream);
public :
    TBuf<32> iBuffer;
    TInt     iXA;
    TUint    iYA;
    };

LOCAL_C void doCopyL();

LOCAL_C void doPasteL();

LOCAL_C void doShow(const TDesC& aHeading,const CClassA* anItem);

Basics.cpp

// Basics.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

//  Example demonstrates a simple use of the clipboard.
        
#include <baclipb.h> 

#include "CommonToResourceFilesEx.h"
#include "Basics.h"


////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// Do the example(s)

LOCAL_C void doExampleL()
    {
    doCopyL();
    doPasteL();
    }

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


LOCAL_C void doCopyL()
    {
    _LIT(KSomeText,"Some text");
    _LIT(KContentOfTheCClassAObject,"Content of the CClassA object ...");

    // Construct an object of type CClassA
    CClassA*  item = new (ELeave) CClassA;
    CleanupStack::PushL(item);

    // Put some data into it
    item->iBuffer = KSomeText;
    item->iXA     = -1;
    item->iYA     = 2;

    // Show contents of the CClassA object
    doShow(KContentOfTheCClassAObject,item);

    // Construct the clipboard object and prepare the 
    // clipboard for writing
    CClipboard* cb = CClipboard::NewForWritingLC(fsSession);

    // Now put the object onto the clipboard and identify the data with
    // the uid KExampleClipUid. In a real app, this would typically be 
    // done in response to a request from the user interface to cut or copy.
    //
    // Note that the value of KExampleClipUid is arbitrary and is used
    // solely for the purpose oif demonstration
    RStoreWriteStream  stream;
    TStreamId stid = stream.CreateLC(cb->Store());
    stream << *item;
    stream.CommitL();
    (cb->StreamDictionary()).AssignL(KExampleClipUid,stid);
    CleanupStack::PopAndDestroy(); //the stream
    
    // commit the clipboard - this writes the stream dictionary to the
    // store as the root stream and commits all changes to the store 
    cb->CommitL();

    // Delete the clipboard object - this closes the clipboard file store
    // and delete the CClassA object
    CleanupStack::PopAndDestroy(2);
    }

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

LOCAL_C void doPasteL()
    {
    _LIT(KNothingToPaste,"Nothing to paste");
    _LIT(KTheCClassAObjectAfterPastingIn,"The CClassA object after pasting in ...");

    CClipboard* cb = NULL;

    // Construct the clipboard object and prepare the 
    // clipboard for reading.
    TRAPD(ret,cb=CClipboard::NewForReadingL(fsSession));
    CleanupStack::PushL(cb);
    if (ret!=KErrNone)
        {
        doShow(KNothingToPaste,NULL);
        User::Leave(ret);
        }
        
    // Construct an object of type CClassA
    CClassA*  item = new (ELeave) CClassA;
    CleanupStack::PushL(item);

    // Check whether there is a CClassA object on the clipboard
    TStreamId stid = (cb->StreamDictionary()).At(KExampleClipUid);
    if (stid == KNullStreamId)
        {
        doShow(KNothingToPaste,NULL);
        User::Leave(0);
        }
    
    // Fetch the CClassA object from the clipboard
    RStoreReadStream stream;
    stream.OpenLC(cb->Store(),stid);
    stream >> *item;
    CleanupStack::PopAndDestroy();  // the stream
    
    // Show contents of the CClassA object as pasted in
    // from the  clipboard.
    doShow(KTheCClassAObjectAfterPastingIn,item);

    // delete:
    // 1. the CClassA object
    // 2. the clipboard object
    CleanupStack::PopAndDestroy(2);
    }

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

LOCAL_C void doShow(const TDesC& aHeading,const CClassA* anItem)
    {
    _LIT(KNewline,"\n");
    _LIT(KFormatS,"\n%S");
    _LIT(KFormatD,"\n%d");
    _LIT(KFormatU,"\n%u");

    console->Printf(KNewline);
    console->Printf(aHeading);
    if (anItem)
        {
        console->Printf(KFormatS,&anItem->iBuffer);
        console->Printf(KFormatD,anItem->iXA);
        console->Printf(KFormatU,anItem->iYA);
        console->Printf(KNewline);
        }
    }

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

CClassA::CClassA()
    {
    _LIT(KDefault,"DEFAULT");
    iBuffer = KDefault;
    }

void CClassA::ExternalizeL(RWriteStream& aStream) const
    {
    aStream << iBuffer;
    aStream.WriteInt32L(iXA);
    aStream.WriteUint32L(iYA);
    }  
 
void CClassA::InternalizeL(RReadStream& aStream)
    {
    aStream >> iBuffer;
      iXA  = aStream.ReadInt32L();
    iYA  = aStream.ReadUint32L();
    }  
    
        

    
     
// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

Basics.mmp
// Basics.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET        ClipboardBasics.exe
TARGETTYPE    exe
UID           0
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        Basics.cpp

USERINCLUDE   .
USERINCLUDE   ..\..\Commonframework
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib efsrv.lib  bafl.lib estor.lib

Description

This is an example of basic clipboard use.


Classes used