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.

297 lines
9.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2022 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 <widgets/grid_icon_text_helpers.h>
  24. #include <wx/artprov.h>
  25. #include <wx/defs.h>
  26. #include <wx/textctrl.h>
  27. #include <wx/dc.h>
  28. #include <bitmaps.h>
  29. GRID_CELL_ICON_TEXT_RENDERER::GRID_CELL_ICON_TEXT_RENDERER( const std::vector<BITMAPS>& icons,
  30. const wxArrayString& names ) :
  31. m_icons( icons ),
  32. m_names( names )
  33. {
  34. }
  35. void GRID_CELL_ICON_TEXT_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  36. const wxRect& aRect, int aRow, int aCol, bool isSelected )
  37. {
  38. wxString value = aGrid.GetCellValue( aRow, aCol );
  39. wxBitmap bitmap;
  40. wxRect rect = aRect;
  41. rect.Inflate( -1 );
  42. // erase background
  43. wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
  44. // draw the icon
  45. // note that the set of icons might be smaller than the set of labels if the last
  46. // label is <...>.
  47. int position = m_names.Index( value );
  48. if( position < (int) m_icons.size() && position != wxNOT_FOUND )
  49. {
  50. bitmap = KiBitmap( m_icons[ position ] );
  51. aDC.DrawBitmap( bitmap, rect.GetLeft() + 3, rect.GetTop() + 2, true );
  52. }
  53. else // still need a bitmap to fetch the width
  54. {
  55. bitmap = KiBitmap( m_icons[ 0 ] );
  56. }
  57. // draw the text
  58. rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 7 );
  59. SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
  60. aGrid.DrawTextRectangle( aDC, value, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
  61. }
  62. wxSize GRID_CELL_ICON_TEXT_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
  63. int row, int col )
  64. {
  65. wxBitmap bitmap = KiBitmap( m_icons[ row ] );
  66. wxString text = grid.GetCellValue( row, col );
  67. wxSize size = wxGridCellStringRenderer::DoGetBestSize( attr, dc, text );
  68. size.x += bitmap.GetWidth() + 6;
  69. return size;
  70. }
  71. GRID_CELL_ICON_RENDERER::GRID_CELL_ICON_RENDERER( const wxBitmap& icon ) :
  72. m_icon( icon )
  73. {
  74. }
  75. void GRID_CELL_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  76. const wxRect& aRect, int aRow, int aCol, bool isSelected )
  77. {
  78. wxRect rect = aRect;
  79. rect.Inflate( -1 );
  80. // erase background
  81. wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
  82. // Draw icon
  83. if( m_icon.IsOk() )
  84. {
  85. aDC.DrawBitmap( m_icon,
  86. rect.GetLeft() + ( rect.GetWidth() - m_icon.GetWidth() ) / 2,
  87. rect.GetTop() + ( rect.GetHeight() - m_icon.GetHeight() ) / 2,
  88. true );
  89. }
  90. }
  91. wxSize GRID_CELL_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
  92. int row, int col )
  93. {
  94. return wxSize( m_icon.GetWidth() + 6, m_icon.GetHeight() + 4 );
  95. }
  96. wxGridCellRenderer* GRID_CELL_ICON_RENDERER::Clone() const
  97. {
  98. return new GRID_CELL_ICON_RENDERER( m_icon );
  99. }
  100. //---- Grid helpers: custom wxGridCellRenderer that renders just an icon ----------------
  101. //
  102. // Note: this renderer is supposed to be used with read only cells
  103. GRID_CELL_STATUS_ICON_RENDERER::GRID_CELL_STATUS_ICON_RENDERER( int aStatus ) :
  104. m_status( aStatus )
  105. {
  106. if( m_status != 0 )
  107. {
  108. m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( m_status ),
  109. wxART_BUTTON );
  110. }
  111. else
  112. {
  113. // Dummy bitmap for size
  114. m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( wxICON_INFORMATION ),
  115. wxART_BUTTON );
  116. }
  117. }
  118. void GRID_CELL_STATUS_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  119. const wxRect& aRect, int aRow, int aCol,
  120. bool isSelected )
  121. {
  122. wxRect rect = aRect;
  123. rect.Inflate( -1 );
  124. // erase background
  125. wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
  126. // Draw icon
  127. if( ( m_status != 0 ) && m_bitmap.IsOk() )
  128. {
  129. aDC.DrawBitmap( m_bitmap,
  130. rect.GetLeft() + ( rect.GetWidth() - m_bitmap.GetWidth() ) / 2,
  131. rect.GetTop() + ( rect.GetHeight() - m_bitmap.GetHeight() ) / 2,
  132. true );
  133. }
  134. }
  135. wxSize GRID_CELL_STATUS_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
  136. int row, int col )
  137. {
  138. return wxSize( m_bitmap.GetWidth() + 6, m_bitmap.GetHeight() + 4 );
  139. }
  140. wxGridCellRenderer* GRID_CELL_STATUS_ICON_RENDERER::Clone() const
  141. {
  142. return new GRID_CELL_STATUS_ICON_RENDERER( m_status );
  143. }
  144. GRID_CELL_ICON_TEXT_POPUP::GRID_CELL_ICON_TEXT_POPUP( const std::vector<BITMAPS>& icons,
  145. const wxArrayString& names ) :
  146. m_icons( icons ),
  147. m_names( names )
  148. {
  149. }
  150. wxGridCellEditor* GRID_CELL_ICON_TEXT_POPUP::Clone() const
  151. {
  152. return new GRID_CELL_ICON_TEXT_POPUP( m_icons, m_names );
  153. }
  154. void GRID_CELL_ICON_TEXT_POPUP::Create( wxWindow* aParent, wxWindowID aId,
  155. wxEvtHandler* aEventHandler )
  156. {
  157. m_control = new wxBitmapComboBox( aParent, aId, wxEmptyString, wxDefaultPosition,
  158. wxDefaultSize, 0, nullptr,
  159. wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
  160. for( unsigned i = 0; i < m_names.size(); ++i )
  161. {
  162. // note that the set of icons might be smaller than the set of labels if
  163. // the last label is <...>.
  164. if( i < m_icons.size() && m_icons[ i ] != BITMAPS::INVALID_BITMAP )
  165. Combo()->Append( m_names[ i ], KiBitmap( m_icons[ i ] ) );
  166. else
  167. Combo()->Append( m_names[ i ] );
  168. }
  169. wxGridCellEditor::Create(aParent, aId, aEventHandler);
  170. }
  171. wxString GRID_CELL_ICON_TEXT_POPUP::GetValue() const
  172. {
  173. return Combo()->GetValue();
  174. }
  175. void GRID_CELL_ICON_TEXT_POPUP::SetSize( const wxRect& aRect )
  176. {
  177. wxRect rect( aRect );
  178. rect.Inflate( -1 );
  179. #if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
  180. // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
  181. Combo()->SetButtonPosition( 0, 0, wxRIGHT, 2 );
  182. #endif
  183. #if defined( __WXMAC__ )
  184. rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
  185. #endif
  186. Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
  187. }
  188. void GRID_CELL_ICON_TEXT_POPUP::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
  189. {
  190. auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  191. // Don't immediately end if we get a kill focus event within BeginEdit
  192. evtHandler->SetInSetFocus( true );
  193. m_value = aGrid->GetTable()->GetValue( aRow, aCol );
  194. Combo()->SetSelection( Combo()->FindString( m_value ) );
  195. Combo()->SetFocus();
  196. #ifdef __WXOSX_COCOA__
  197. // This is a work around for the combobox being simply dismissed when a
  198. // choice is made in it under OS X. The bug is almost certainly due to a
  199. // problem in focus events generation logic but it's not obvious to fix and
  200. // for now this at least allows one to use wxGrid.
  201. Combo()->Popup();
  202. #endif
  203. // When dropping down the menu, a kill focus event
  204. // happens after this point, so we can't reset the flag yet.
  205. #if !defined(__WXGTK__)
  206. evtHandler->SetInSetFocus( false );
  207. #endif
  208. }
  209. bool GRID_CELL_ICON_TEXT_POPUP::EndEdit( int , int , const wxGrid* , const wxString& ,
  210. wxString *aNewVal )
  211. {
  212. const wxString value = Combo()->GetValue();
  213. if( value == m_value )
  214. return false;
  215. m_value = value;
  216. if( aNewVal )
  217. *aNewVal = value;
  218. return true;
  219. }
  220. void GRID_CELL_ICON_TEXT_POPUP::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
  221. {
  222. aGrid->GetTable()->SetValue( aRow, aCol, m_value );
  223. }
  224. void GRID_CELL_ICON_TEXT_POPUP::Reset()
  225. {
  226. Combo()->SetSelection( Combo()->FindString( m_value ) );
  227. }