Using the Clipboard

team bbl


To use the clipboard, you call member functions of the global pointer wxTheClipboard.

Before copying or pasting, you must take temporary ownership of the clipboard by calling wxClipboard::Open. If this operation returns TRue, you now own the clipboard. Call wxClipboard::SetData to put data on the clipboard or wxClipboard::GetData to retrieve data from the clipboard. Call wxClipboard::Close to close the clipboard and relinquish ownership. You should keep the clipboard open only as long as you are using it.

wxClipboardLocker is a helpful class that will open the clipboard (if possible) in its constructor and close it in its destructor, so you can write

 wxClipboardLocker locker; if (!locker) {    ... report an error and return ... } ... use the clipboard ... 

The following code shows how to write text to and read text from the clipboard:

 // Write some text to the clipboard if (wxTheClipboard->Open()) {     // Data objects are held by the clipboard,      // so do not delete them in the app.     wxTheClipboard->SetData(new wxTextDataObject(wxT("Some text")));     wxTheClipboard->Close(); } // Read some text if (wxTheClipboard->Open()) {     if (wxTheClipboard->IsSupported(wxDF_TEXT))     {         wxTextDataObject data;         wxTheClipboard->GetData(data);         wxMessageBox(data.GetText());     }     wxTheClipboard->Close(); } 

Here's the same thing, but with bitmaps:

 // Write a bitmap to the clipboard wxImage image(wxT("splash.png"), wxBITMAP_TYPE_PNG); wxBitmap bitmap(image.ConvertToBitmap()); if (wxTheClipboard->Open()) {     // Data objects are held by the clipboard,      // so do not delete them in the app.     wxTheClipboard->SetData(new wxBitmapDataObject(bitmap));     wxTheClipboard->Close(); } // Read a bitmap if (wxTheClipboard->Open()) {     if (wxTheClipboard->IsSupported(wxDF_BITMAP))     {         wxBitmapDataObject data;         wxTheClipboard->GetData( data );         bitmap = data.GetBitmap();     }     wxTheClipboard->Close(); } 

If you implement clipboard operations, you will have to update the user interface to enable or disable clipboard commands, whether they are menu items, toolbar buttons, or ordinary buttons. This is a job for the wxWidgets user interface update mechanism, which sends wxUpdateUIEvent events to your application in idle time; refer to Chapter 9, "Creating Custom Dialogs," for details. The idle time updating allows your interface to be updated even when data is copied to the clipboard without your application's knowledge.

Some controls, such as wxTextCtrl, already implement user interface update events. If you use the standard identifiers wxID_CUT, wxID_COPY, and wxID_PASTE for your menus and toolbars and arrange for command events from the focused control to be processed first, the interface will be updated as the user expects. Chapter 20, "Perfecting Your Application," shows how to direct command events to the focused control by overriding wxFrame::ProcessEvent.

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net