Copying a metafile to the clipboard

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Copying a metafile to the clipboard

Post by leiradella » Mon Aug 17, 2009 2:14 am

Hi All,

I'd like to copy a metafile to the clipboard along with some other formats in a wxDataObjectComposite object.

I already copy metafiles using

Code: Select all

void
Panel::CopyAsMetafile()
{
#ifdef _WIN32
	wxMetafileDC mdc;
	m_Graph->Paint(mdc);
	wxMetafile *meta = mdc.Close();
	meta->SetClipboard();
	delete meta;
#endif
}
but how can I copy the metafile, a text content and a customized content all in one go?

Thanks,

Andre

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Mon Aug 17, 2009 3:47 am

Use the SetData() method of wxClipboard to set the data for each of the different formats you are interested in.

http://docs.wxwidgets.org/stable/wx_wxc ... xclipboard

-Max

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Mon Aug 17, 2009 3:52 am

mc2r wrote:Use the SetData() method of wxClipboard to set the data for each of the different formats you are interested in.

http://docs.wxwidgets.org/stable/wx_wxc ... xclipboard

-Max
Hum, the pages says:
Call this function to set the data object to the clipboard. This function will clear all previous contents in the clipboard, so calling it several times does not make any sense.
So I don't think it's the solution.

AFAIK, I have to create a wxDataObjectComposite and add the actual data objects to it. My problem is how I get a "wxMetafileDataObject", which I think doesn't exist.

Thanks,

Andre

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Mon Aug 17, 2009 3:55 am

Sorry.

Call wxClipboard::SetData() once, but call wxDataObject::SetData() of the data object you pass in the one call to wxClipboard::SetData() multiple times for each format

http://docs.wxwidgets.org/stable/wx_wxd ... ectsetdata

-Max

FYI: I haven't tried it but wxDataObject::SetData() reads like you could set multiple formats with it.

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Mon Aug 17, 2009 4:34 am

mc2r wrote:Sorry.

Call wxClipboard::SetData() once, but call wxDataObject::SetData() of the data object you pass in the one call to wxClipboard::SetData() multiple times for each format

http://docs.wxwidgets.org/stable/wx_wxd ... ectsetdata

-Max

FYI: I haven't tried it but wxDataObject::SetData() reads like you could set multiple formats with it.
Hum...
wxDataObjectSimple and wxDataObjectComposite. wxDataObjectSimple is the simplest wxDataObject possible and only holds data in a single format (such as HTML or text) and wxDataObjectComposite is the simplest way to implement a wxDataObject that does support multiple formats because it achieves this by simply holding several wxDataObjectSimple objects.
On the same page:
Each class derived directly from wxDataObject must override and implement all of its functions which are pure virtual in the base class.
So I can't instantiate a wxDataObject. I'm pretty sure I must use a wxDataObjectComposite, but I can't find a way to put my hands on a metafile object suitable for copying to the clipboard along with other data.

Thanks,

Andre

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Tue Aug 18, 2009 4:45 am

I debugged my app step-by-step and wxMetafile::SetClipboard() is defined as:

Code: Select all

bool wxEnhMetaFile::SetClipboard(int WXUNUSED(width), int WXUNUSED(height))
{
#if wxUSE_DRAG_AND_DROP && wxUSE_CLIPBOARD
    wxCHECK_MSG( m_hMF, false, _T("can't copy invalid metafile to clipboard") );

    return wxTheClipboard->AddData(new wxEnhMetaFileDataObject(*this));
#else // !wxUSE_DRAG_AND_DROP
    wxFAIL_MSG(_T("not implemented"));
    return false;
#endif // wxUSE_DRAG_AND_DROP/!wxUSE_DRAG_AND_DROP
}
So it looks like there's a data object which can handle metafiles: wxEnhMetaFileDataObject.

As it's not mentioned in the docs, would it be safe to use it in my code so I can put metafiles in the clipboard along with other data? I wouldn't like to use internal objects that keep changing between releases...

Cheers,

Andre

leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella » Tue Aug 18, 2009 4:55 am

Also, wxMetafile::SetClipboard() doesn't call wxTheClipboard->Open() to acquire the clipboard. Is it a bug or wxTheClipboard::AddData() does it behind the scenes?

Cheers,

Andre

Post Reply