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.

299 lines
8.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <wx/image.h>
  25. #include <wx/bitmap.h>
  26. #include <wx/mstream.h>
  27. #include <wx/menu.h>
  28. #include <wx/menuitem.h>
  29. #include <wx/aui/auibar.h>
  30. #include <cstdint>
  31. #include <mutex>
  32. #include <unordered_map>
  33. #include <common.h>
  34. #include <bitmaps.h>
  35. #include <pgm_base.h>
  36. #include <eda_base_frame.h>
  37. #include <eda_draw_frame.h>
  38. #include <settings/common_settings.h>
  39. struct SCALED_BITMAP_ID {
  40. BITMAP_DEF bitmap;
  41. int scale;
  42. bool operator==( SCALED_BITMAP_ID const& other ) const noexcept
  43. {
  44. return bitmap == other.bitmap && scale == other.scale;
  45. }
  46. };
  47. namespace std {
  48. template<> struct hash<SCALED_BITMAP_ID>
  49. {
  50. typedef SCALED_BITMAP_ID argument_type;
  51. typedef std::size_t result_type;
  52. result_type operator()( argument_type const& id ) const noexcept
  53. {
  54. static const bool sz64 = sizeof( uintptr_t ) == 8;
  55. static const size_t mask = sz64 ? 0xF000000000000000uLL : 0xF0000000uL;
  56. static const size_t offset = sz64 ? 60 : 28;
  57. // The hash only needs to be fast and simple, not necessarily accurate - a collision
  58. // only makes things slower, not broken. BITMAP_DEF is a pointer, so the most
  59. // significant several bits are generally going to be the same for all. Just convert
  60. // it to an integer and stuff the scale factor into those bits.
  61. return
  62. ( (uintptr_t)( id.bitmap ) & ~mask ) |
  63. ( ( (uintptr_t)( id.scale ) & 0xF ) << offset );
  64. }
  65. };
  66. }
  67. wxBitmap KiBitmap( BITMAP_DEF aBitmap )
  68. {
  69. wxMemoryInputStream is( aBitmap->png, aBitmap->byteCount );
  70. wxImage image( is, wxBITMAP_TYPE_PNG );
  71. wxBitmap bitmap( image );
  72. return bitmap;
  73. }
  74. int KiIconScale( wxWindow* aWindow )
  75. {
  76. const int vert_size = aWindow->ConvertDialogToPixels( wxSize( 0, 8 ) ).y;
  77. // Autoscale won't exceed unity until the system has quite high resolution,
  78. // because we don't want the icons to look obviously scaled on a system
  79. // where it's easy to see it.
  80. if( vert_size > 34 ) return 8;
  81. else if( vert_size > 29 ) return 7;
  82. else if( vert_size > 24 ) return 6;
  83. else return 4;
  84. }
  85. static int get_scale_factor( EDA_BASE_FRAME* aWindow )
  86. {
  87. int requested_scale = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
  88. if( requested_scale > 0 )
  89. return requested_scale;
  90. else
  91. return KiIconScale( aWindow );
  92. }
  93. wxBitmap KiScaledBitmap( BITMAP_DEF aBitmap, EDA_BASE_FRAME* aWindow )
  94. {
  95. // Bitmap conversions are cached because they can be slow.
  96. static std::unordered_map<SCALED_BITMAP_ID, wxBitmap> bitmap_cache;
  97. static std::mutex bitmap_cache_mutex;
  98. const int scale = get_scale_factor( aWindow );
  99. SCALED_BITMAP_ID id = { aBitmap, scale };
  100. std::lock_guard<std::mutex> guard( bitmap_cache_mutex );
  101. auto it = bitmap_cache.find( id );
  102. if( it != bitmap_cache.end() )
  103. {
  104. return it->second;
  105. }
  106. else
  107. {
  108. wxMemoryInputStream is( aBitmap->png, aBitmap->byteCount );
  109. wxImage image( is, wxBITMAP_TYPE_PNG );
  110. // Bilinear seems to genuinely look better for these line-drawing icons
  111. // than bicubic, despite claims in the wx documentation that bicubic is
  112. // "highest quality". I don't recommend changing this. Bicubic looks
  113. // blurry and makes me want an eye exam.
  114. image.Rescale( scale * image.GetWidth() / 4, scale * image.GetHeight() / 4,
  115. wxIMAGE_QUALITY_BILINEAR );
  116. return bitmap_cache.emplace( id, wxBitmap( image ) ).first->second;
  117. }
  118. }
  119. wxBitmap KiScaledBitmap( const wxBitmap& aBitmap, EDA_BASE_FRAME* aWindow )
  120. {
  121. const int scale = get_scale_factor( aWindow );
  122. if( scale == 4)
  123. {
  124. return wxBitmap( aBitmap );
  125. }
  126. else
  127. {
  128. wxImage image = aBitmap.ConvertToImage();
  129. image.Rescale( scale * image.GetWidth() / 4, scale * image.GetHeight() / 4,
  130. wxIMAGE_QUALITY_BILINEAR );
  131. return wxBitmap( image );
  132. }
  133. }
  134. void KiScaledSeparator( wxAuiToolBar* aToolbar, EDA_BASE_FRAME* aWindow )
  135. {
  136. const int scale = get_scale_factor( aWindow );
  137. if( scale > 4 )
  138. {
  139. aToolbar->AddSpacer( 16 * ( scale - 4 ) / 4 );
  140. }
  141. aToolbar->AddSeparator();
  142. if( scale > 4 )
  143. {
  144. aToolbar->AddSpacer( 16 * ( scale - 4 ) / 4 );
  145. }
  146. }
  147. wxBitmap* KiBitmapNew( BITMAP_DEF aBitmap )
  148. {
  149. wxMemoryInputStream is( aBitmap->png, aBitmap->byteCount );
  150. wxImage image( is, wxBITMAP_TYPE_PNG );
  151. wxBitmap* bitmap = new wxBitmap( image );
  152. return bitmap;
  153. }
  154. bool SaveCanvasImageToFile( EDA_DRAW_FRAME* aFrame, const wxString& aFileName,
  155. wxBitmapType aBitmapType )
  156. {
  157. wxCHECK( aFrame != nullptr, false );
  158. bool retv = true;
  159. // Make a screen copy of the canvas:
  160. wxSize image_size = aFrame->GetCanvas()->GetClientSize();
  161. wxClientDC dc( aFrame->GetCanvas() );
  162. wxBitmap bitmap( image_size.x, image_size.y );
  163. wxMemoryDC memdc;
  164. memdc.SelectObject( bitmap );
  165. memdc.Blit( 0, 0, image_size.x, image_size.y, &dc, 0, 0 );
  166. memdc.SelectObject( wxNullBitmap );
  167. wxImage image = bitmap.ConvertToImage();
  168. if( !image.SaveFile( aFileName, aBitmapType ) )
  169. retv = false;
  170. image.Destroy();
  171. return retv;
  172. }
  173. void AddBitmapToMenuItem( wxMenuItem* aMenu, const wxBitmap& aImage )
  174. {
  175. // Retrieve the global applicaton show icon option:
  176. bool useImagesInMenus = Pgm().GetCommonSettings()->m_Appearance.use_icons_in_menus;
  177. wxItemKind menu_type = aMenu->GetKind();
  178. if( useImagesInMenus )
  179. {
  180. if( menu_type == wxITEM_CHECK || menu_type == wxITEM_RADIO )
  181. {
  182. #if defined( __WINDOWS__ )
  183. aMenu->SetBitmaps( KiBitmap( checked_ok_xpm ), aImage );
  184. // A workaround to a strange bug on Windows, wx Widgets 3.0:
  185. // size of bitmaps is not taken in account for wxITEM_{CHECK,RADIO} menu
  186. // unless we call SetFont
  187. aMenu->SetFont( *wxNORMAL_FONT );
  188. #endif
  189. }
  190. else if( menu_type != wxITEM_RADIO )
  191. {
  192. aMenu->SetBitmap( aImage );
  193. }
  194. }
  195. }
  196. wxMenuItem* AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
  197. const wxBitmap& aImage, wxItemKind aType = wxITEM_NORMAL )
  198. {
  199. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, wxEmptyString, aType );
  200. AddBitmapToMenuItem( item, aImage );
  201. aMenu->Append( item );
  202. return item;
  203. }
  204. wxMenuItem* AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
  205. const wxString& aHelpText, const wxBitmap& aImage,
  206. wxItemKind aType = wxITEM_NORMAL )
  207. {
  208. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText, aType );
  209. AddBitmapToMenuItem( item, aImage );
  210. aMenu->Append( item );
  211. return item;
  212. }
  213. wxMenuItem* AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId,
  214. const wxString& aText, const wxBitmap& aImage )
  215. {
  216. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText );
  217. item->SetSubMenu( aSubMenu );
  218. AddBitmapToMenuItem( item, aImage );
  219. aMenu->Append( item );
  220. return item;
  221. }
  222. wxMenuItem* AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId,
  223. const wxString& aText, const wxString& aHelpText,
  224. const wxBitmap& aImage )
  225. {
  226. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText );
  227. item->SetSubMenu( aSubMenu );
  228. AddBitmapToMenuItem( item, aImage );
  229. aMenu->Append( item );
  230. return item;
  231. }