You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "clipboard.h"
  24. #include <wx/clipbrd.h>
  25. #include <wx/image.h>
  26. #include <wx/log.h>
  27. bool SaveClipboard( const std::string& aTextUTF8 )
  28. {
  29. wxLogNull doNotLog; // disable logging of failed clipboard actions
  30. if( wxTheClipboard->Open() )
  31. {
  32. // Store the UTF8 string as Unicode string in clipboard:
  33. wxTheClipboard->SetData(
  34. new wxTextDataObject( wxString( aTextUTF8.c_str(), wxConvUTF8 ) ) );
  35. wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
  36. wxTheClipboard->Close();
  37. return true;
  38. }
  39. return false;
  40. }
  41. std::string GetClipboardUTF8()
  42. {
  43. std::string result;
  44. wxLogNull doNotLog; // disable logging of failed clipboard actions
  45. if( wxTheClipboard->Open() )
  46. {
  47. if( wxTheClipboard->IsSupported( wxDF_TEXT )
  48. || wxTheClipboard->IsSupported( wxDF_UNICODETEXT ) )
  49. {
  50. wxTextDataObject data;
  51. wxTheClipboard->GetData( data );
  52. // The clipboard is expected containing a Unicode string, so return it
  53. // as UTF8 string
  54. result = data.GetText().utf8_str();
  55. }
  56. wxTheClipboard->Close();
  57. }
  58. return result;
  59. }
  60. std::unique_ptr<wxImage> GetImageFromClipboard()
  61. {
  62. std::unique_ptr<wxImage> image;
  63. wxLogNull doNotLog; // disable logging of failed clipboard actions
  64. // First try for an image
  65. if( wxTheClipboard->Open() )
  66. {
  67. if( wxTheClipboard->IsSupported( wxDF_BITMAP ) )
  68. {
  69. wxImageDataObject data;
  70. if( wxTheClipboard->GetData( data ) )
  71. {
  72. image = std::make_unique<wxImage>( data.GetImage() );
  73. }
  74. }
  75. else if( wxTheClipboard->IsSupported( wxDF_FILENAME ) )
  76. {
  77. wxFileDataObject data;
  78. if( wxTheClipboard->GetData( data ) && data.GetFilenames().size() == 1 )
  79. {
  80. image = std::make_unique<wxImage>( data.GetFilenames()[0] );
  81. if( !image->IsOk() )
  82. image.reset();
  83. }
  84. }
  85. wxTheClipboard->Close();
  86. }
  87. return image;
  88. }