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.

300 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. struct SCALED_BITMAP_ID {
  39. BITMAP_DEF bitmap;
  40. int scale;
  41. bool operator==( SCALED_BITMAP_ID const& other ) const noexcept
  42. {
  43. return bitmap == other.bitmap && scale == other.scale;
  44. }
  45. };
  46. namespace std {
  47. template<> struct hash<SCALED_BITMAP_ID>
  48. {
  49. typedef SCALED_BITMAP_ID argument_type;
  50. typedef std::size_t result_type;
  51. result_type operator()( argument_type const& id ) const noexcept
  52. {
  53. static const bool sz64 = sizeof( uintptr_t ) == 8;
  54. static const size_t mask = sz64 ? 0xF000000000000000uLL : 0xF0000000uL;
  55. static const size_t offset = sz64 ? 60 : 28;
  56. // The hash only needs to be fast and simple, not necessarily accurate - a collision
  57. // only makes things slower, not broken. BITMAP_DEF is a pointer, so the most
  58. // significant several bits are generally going to be the same for all. Just convert
  59. // it to an integer and stuff the scale factor into those bits.
  60. return
  61. ( (uintptr_t)( id.bitmap ) & ~mask ) |
  62. ( ( (uintptr_t)( id.scale ) & 0xF ) << offset );
  63. }
  64. };
  65. }
  66. wxBitmap KiBitmap( BITMAP_DEF aBitmap )
  67. {
  68. wxMemoryInputStream is( aBitmap->png, aBitmap->byteCount );
  69. wxImage image( is, wxBITMAP_TYPE_PNG );
  70. wxBitmap bitmap( image );
  71. return bitmap;
  72. }
  73. int KiIconScale( wxWindow* aWindow )
  74. {
  75. const int vert_size = aWindow->ConvertDialogToPixels( wxSize( 0, 8 ) ).y;
  76. // Autoscale won't exceed unity until the system has quite high resolution,
  77. // because we don't want the icons to look obviously scaled on a system
  78. // where it's easy to see it.
  79. if( vert_size > 34 ) return 8;
  80. else if( vert_size > 29 ) return 7;
  81. else if( vert_size > 24 ) return 6;
  82. else return 4;
  83. }
  84. static int get_scale_factor( EDA_BASE_FRAME* aWindow )
  85. {
  86. int requested_scale;
  87. Pgm().CommonSettings()->Read( ICON_SCALE_KEY, &requested_scale, 0 );
  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->GetGalCanvas()->GetClientSize();
  161. wxClientDC dc( aFrame->GetGalCanvas() );
  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;
  177. Pgm().CommonSettings()->Read( USE_ICONS_IN_MENUS_KEY, &useImagesInMenus );
  178. wxItemKind menu_type = aMenu->GetKind();
  179. if( useImagesInMenus )
  180. {
  181. if( menu_type == wxITEM_CHECK || menu_type == wxITEM_RADIO )
  182. {
  183. #if defined( __WINDOWS__ )
  184. aMenu->SetBitmaps( KiBitmap( checked_ok_xpm ), aImage );
  185. // A workaround to a strange bug on Windows, wx Widgets 3.0:
  186. // size of bitmaps is not taken in account for wxITEM_{CHECK,RADIO} menu
  187. // unless we call SetFont
  188. aMenu->SetFont( *wxNORMAL_FONT );
  189. #endif
  190. }
  191. else if( menu_type != wxITEM_RADIO )
  192. {
  193. aMenu->SetBitmap( aImage );
  194. }
  195. }
  196. }
  197. wxMenuItem* AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
  198. const wxBitmap& aImage, wxItemKind aType = wxITEM_NORMAL )
  199. {
  200. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, wxEmptyString, aType );
  201. AddBitmapToMenuItem( item, aImage );
  202. aMenu->Append( item );
  203. return item;
  204. }
  205. wxMenuItem* AddMenuItem( wxMenu* aMenu, int aId, const wxString& aText,
  206. const wxString& aHelpText, const wxBitmap& aImage,
  207. wxItemKind aType = wxITEM_NORMAL )
  208. {
  209. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText, aType );
  210. AddBitmapToMenuItem( item, aImage );
  211. aMenu->Append( item );
  212. return item;
  213. }
  214. wxMenuItem* AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId,
  215. const wxString& aText, const wxBitmap& aImage )
  216. {
  217. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText );
  218. item->SetSubMenu( aSubMenu );
  219. AddBitmapToMenuItem( item, aImage );
  220. aMenu->Append( item );
  221. return item;
  222. }
  223. wxMenuItem* AddMenuItem( wxMenu* aMenu, wxMenu* aSubMenu, int aId,
  224. const wxString& aText, const wxString& aHelpText,
  225. const wxBitmap& aImage )
  226. {
  227. wxMenuItem* item = new wxMenuItem( aMenu, aId, aText, aHelpText );
  228. item->SetSubMenu( aSubMenu );
  229. AddBitmapToMenuItem( item, aImage );
  230. aMenu->Append( item );
  231. return item;
  232. }