Clipboard and multiple formats. lazarus / 31-LCL ---------------------------------------------------------------------- taazz · 2013-07-13 02:58 Does the lcl clipboard class supports data with multiple formats? what I mean is I write something like this Code: [Select]clipboard.open; clipboard.clear; clipboard.assign(MyBmp); clipboard.Assign(MyMetafile); clipboard.Assign(MySVG); clipboard.close; If I press ctrl+V from coredraw the meafile contents will be pasted to the program if I paste from mspaint the bitmap contents will be used etc..... Is it possible or not? Regards Jo. ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-29 20:53 Works for me.  Not with your specific code, my code puts both a tab separated table (for pasting into notepad) and a HTML table (for pasting in Excel). We don't have any control over which version will get pasted into the third party app of course, we can only provide the data in a variety of formats. ---------------------------------------------------------------------- taazz · 2013-07-29 21:07 when you do get a chance I'd like to see the code that does the copy my experience is different only hte last format will be active on the windows clipboard. and I have tried almost everything I could think of. ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-30 00:56 Lots of calls to code I haven't included, but all the clipboard code is here. Code: [Select]  If oDataset.Active Then   Begin     Clipboard.Clear;     // Add a Tab Separated version to the clipboard for Notepad to use...     oTSV := TStringlist.Create;     Try       AppendDatasetAsTabSeparatedText(oTSV, oDataset, bOnlyActiveRow, bIncludeHeader);       ClipBoard.AsText:=oTSV.Text;     finally       oTSV.Free;     end;     // Now append the HTML for Excel etc...     oHTML := TStringList.Create;     Try       // Load the dataset into the string list       AppendDatasetAsHTML(oHTML, oDataset, bOnlyActiveRow, bIncludeHeader);       iStartHTML := 105;       // Big thanks to the SynEdit team for their solution to the variable header length problem       iStartFragment := iStartHTML + Length(HEADER) + 2;       iEndFragment := iStartFragment + Length(oHTML.Text) + Length(FOOTER1) + 2;       iEndHTML := iEndFragment + LENGTH(FOOTER2) + 2;       // insert the native header and opening html tags at the start of the string       oHTML.Insert(0, Format(NATIVEHEADER, [iStartHTML, iEndHTML, iStartFragment, iEndFragment]));       oHTML.Insert(1, HEADER);       // append the closing tags to the end of the string list       oHTML.Add(FOOTER1);       oHTML.Add(FOOTER2);       // Ensure the HTML mime type is registered       cfHTMLFormat := Clipboard.FindFormatID(HTML_MIME);       If cfHTMLFormat = 0 Then         cfHTMLFormat := RegisterClipboardFormat(HTML_MIME);       // Save the HTML to the clipboard       oHTMLStream := TMemoryStream.Create;       Try         oHTML.SaveToStream(oHTMLStream);         oHTMLStream.Position := 0;         Clipboard.AddFormat(cfHTMLFormat, oHTMLStream);       Finally         oHTMLStream.Free;       End;     Finally       oHTML.Free;     End;   End; End; Essentially: Code: [Select]Clipboard.Clear; Clipboard.AsText := ; Clipboard.AddFormat(cfHTMLFormat, oHTMLStream); Clear differences between my code and yours.  I'm not using assign, maybe that's your issue.  Try re-writing your code around AddFormat... Good luck :-) UPDATE: Yup, I've now stepped into Clipboard.Assign with a TGraphic.  It doesn't add to the clipboard, it calls SetFormat which will overwrite what's there.  AddFormat should be the way forward for you. ---------------------------------------------------------------------- taazz · 2013-07-30 07:04 here is the current code. Code: [Select]  if vObjectList.Count > 0 then   begin     Clipboard.Open;     try       Clipboard.Clear;       if cfBitmap in ClipboardFormats then       begin         vBitmap := GetAsBitmap(vObjectList);         try           vBitmap.HandleType := bmDDB;           Clipboard.Assign(vBitmap);         finally           vBitmap.Free;         end;       end;       if cfNative in ClipboardFormats then       begin         vStream := TMemoryStream.Create;         try           WriteObjects(vStream, vObjectList);           vStream.Position := 0;           Clipboard.AddFormat(CF_SIMPLEGRAPH, vStream);         finally           vStream.Free;         end;       end;     finally       Clipboard.Close;     end; As you can see the code is essentially clipboard.open; clipboard.clear; clipboard.assign(mybitmap); clipboard.addformat(mycustomFormat, Mystream); clipboard.close; and no joy. although both bitmap and custom format are always copied only the last format can be found in the clipboard. of course you are using plain text as one of your formats and that might be why you can have one more format. In any case I know the above worked with delphi at one point (no sethandle in lazarus). Oh well I'll give it a better look after I finish my current project. ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-30 18:02 This intrigues me.   Going to be a learning curve for us both :-) How are you confirming what's on the clipboard?  (Do you have a Clipboard Util for instance, or are you attempting to paste into different apps). I lack a Clipboard utility so produced my code by attempting to paste in different locations.  It took me a few days to realise I had an incorrect mime type for HTML.  If you do have a clipboard monitoring utility, then can you post a link? UPDATE:  Ignore everything below - you state only the last format is available on the clipboard, this means that AddFormat is working... Following is only a wild guess: You're checking to see if ClipboardFormats supports cfNative, but then use CF_SIMPLEGRAPH in your AddFormat.   Is CF_SIMPLEGRAPH a registered type?  I can't remember what will happen if it isn't.  Either an exception (which would mean this isn't the issue as you're not seeing that exception), or perhaps it would silently fail, which may explain your issue. Code: [Select]      cfHTMLFormat := Clipboard.FindFormatID(HTML_MIME);       If cfHTMLFormat = 0 Then         cfHTMLFormat := RegisterClipboardFormat(HTML_MIME);[/s] ---------------------------------------------------------------------- taazz · 2013-07-30 18:13 cfnative is part of a set with 3 values cfnative, cfbitmap and cfmetafile which are used to let the end user decide what to copy on the clipboard. CF_SIMPLEGRAPH on the other hand is a variable that holds the ID returned by the code Code: [Select]  CF_SIMPLEGRAPH := RegisterClipboardFormat('Simple Graph Format'); so I have to check for that in the clipboard the set items are of no use there. As for checking I use mspaint to paste the bitmap and the application I already have for the native format. If I define only cfBitmap then mspaint gets the correct image pasted if I define only native format the demo application gets the data correctly if I define both then only the last one get in the clipboard aka mspaint does not see the bitmap data at all. So each on its own works as expected both together one overrides the other. Just to make sure all this you can download from http://sourceforge.net/projects/evssimplegraph and test/look etc for your self. ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-30 19:05 :-)   Good news. Your SimpleGraph code worked perfectly for me.  I can paste into Paint AND the demo app in the one sitting.  There was a problem in the UI code.  actEditCopyExecute was only copying the native format, actCopyBmpExecute was only ever copying the bitmap.  Nothing was attempting both.  Here's what I needed to change to get it working. Code: [Select]procedure TEvsMain.actEditCopyExecute(Sender : TObject); var   vTmp : TEvsGraphClipboardFormats; begin //  Test.CopyToClipboard;  <-- original code   vTmp := Test.ClipboardFormats;   Test.ClipboardFormats := Test.ClipboardFormats + [cfBitmap];   Test.CopyToClipboard();   Test.ClipboardFormats := vTmp; end;  Probably worth mentioning I'm doing this on Win 7 64bit.  What OS are you on? ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-30 19:36 Incidently, while looking around earlier I noticed a misleading comment in clipbrd.pp.  In the documentation up at the top it states Clear:     Clear cache and supported format list. Looking into TClipboard.Clear it doesn't clear the supported format list at all, only the clipboard contents (which I guess is what the comment is referring to as the cache). I have no idea where to report this.  Seems too minor for a Bug Tracker item. Any one any ideas? ---------------------------------------------------------------------- taazz · 2013-07-30 19:46 I'm on win7 32bit I don't think there are any differences between windows and 32/64 bit at all. I placed that code after I failed to include both formats in one go and added the copy as bitmap as well just to make it clear what is available. If it works for you then I'll check again later tonight. thank you for your time, much appreciated. ---------------------------------------------------------------------- BigChimp · 2013-07-30 20:03 Quote from: Mike.Cornflake on July 30, 2013, 09:36:24 pmI have no idea where to report this.  Seems too minor for a Bug Tracker item. Any one any ideas? Nothing is too minor for the bug tracker Honestly, today we've seen language/grammar fixes come in via the bugtracker - which I'm very glad about. Every little bit helps. Please feel free to raise a bugtracker item, preferably with patch. ---------------------------------------------------------------------- Mike.Cornflake · 2013-07-30 20:11 Consider it done.  Patch will be 7 to 8 weeks away, I've left my copy of the repository at home, but I'll put a placeholder entry in place for now. And WHOO HOO!  Hundreth post!  No longer a green "Jr Member"  (Gonna be pissed if the boundary is 101 posts I can tell you  ) ---------------------------------------------------------------------- Mike.Cornflake · 2013-09-16 05:25 Quote from: BigChimp on July 30, 2013, 10:03:37 pmQuote from: Mike.Cornflake on July 30, 2013, 09:36:24 pmI have no idea where to report this.  Seems too minor for a Bug Tracker item. Any one any ideas? Nothing is too minor for the bug tracker Honestly, today we've seen language/grammar fixes come in via the bugtracker - which I'm very glad about. Every little bit helps. Please feel free to raise a bugtracker item, preferably with patch. Told you it would take a while :-)   While all the legendary work done is done on 64bit zip support, here's my meagre offering :-) http://mantis.freepascal.org/view.php?id=25033