KiCad PCB EDA Suite
FP_GRID_TRICKS Class Reference
Inheritance diagram for FP_GRID_TRICKS:
GRID_TRICKS

Public Member Functions

 FP_GRID_TRICKS (wxGrid *aGrid)
 

Protected Member Functions

virtual void paste_text (const wxString &cb_text) override
 handle specialized clipboard text, with leading "(fp_lib_table", OR spreadsheet formatted text. More...
 
int getCursorCol () const
 If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based column index. More...
 
int getCursorRow () const
 If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based row index. More...
 
void getSelectedArea ()
 Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above. More...
 
void onGridCellLeftClick (wxGridEvent &event)
 
void onGridCellRightClick (wxGridEvent &event)
 
void onRightDown (wxMouseEvent &event)
 
virtual void showPopupMenu ()
 
void onPopupSelection (wxCommandEvent &event)
 
void onKeyDown (wxKeyEvent &ev)
 
bool toggleCell (int aRow, int aCol)
 
virtual void paste_clipboard ()
 
virtual void cutcopy (bool doCut)
 

Static Protected Member Functions

static bool isCtl (int aChar, const wxKeyEvent &e)
 

Protected Attributes

wxGrid * m_grid
 I don't own the grid, but he owns me. More...
 
int m_sel_row_start
 
int m_sel_col_start
 
int m_sel_row_count
 
int m_sel_col_count
 

Detailed Description

Definition at line 91 of file dialog_fp_lib_table.cpp.

Constructor & Destructor Documentation

FP_GRID_TRICKS::FP_GRID_TRICKS ( wxGrid *  aGrid)
inline

Definition at line 94 of file dialog_fp_lib_table.cpp.

94  :
95  GRID_TRICKS( aGrid )
96  {
97  }
GRID_TRICKS(wxGrid *aGrid)
Definition: grid_tricks.cpp:52

Member Function Documentation

void GRID_TRICKS::cutcopy ( bool  doCut)
protectedvirtualinherited

Definition at line 329 of file grid_tricks.cpp.

References COL_SEP, GRID_TRICKS::m_grid, GRID_TRICKS::m_sel_col_count, GRID_TRICKS::m_sel_col_start, GRID_TRICKS::m_sel_row_count, GRID_TRICKS::m_sel_row_start, and ROW_SEP.

Referenced by GRID_TRICKS::onKeyDown(), GRID_TRICKS::onPopupSelection(), and GRID_TRICKS::onRightDown().

330 {
331  if( wxTheClipboard->Open() )
332  {
333  wxGridTableBase* tbl = m_grid->GetTable();
334  wxString txt;
335 
336  // fill txt with a format that is compatible with most spreadsheets
337  for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
338  {
339  for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
340  {
341  txt += tbl->GetValue( row, col );
342 
343  if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
344  txt += COL_SEP;
345 
346  if( doCut )
347  tbl->SetValue( row, col, wxEmptyString );
348  }
349  txt += ROW_SEP;
350  }
351 
352  wxTheClipboard->SetData( new wxTextDataObject( txt ) );
353  wxTheClipboard->Close();
354 
355  if( doCut )
356  {
357  m_grid->AutoSizeColumns( false );
358  m_grid->ForceRefresh();
359  }
360  }
361 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
int m_sel_row_count
Definition: grid_tricks.h:48
int m_sel_col_start
Definition: grid_tricks.h:47
int m_sel_col_count
Definition: grid_tricks.h:49
int m_sel_row_start
Definition: grid_tricks.h:46
#define ROW_SEP
Definition: grid_tricks.cpp:38
#define COL_SEP
Definition: grid_tricks.cpp:37
int GRID_TRICKS::getCursorCol ( ) const
inlineprotectedinherited

If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based column index.

Definition at line 53 of file grid_tricks.h.

Referenced by GRID_TRICKS::onKeyDown(), and GRID_TRICKS::paste_text().

54  {
55  return m_grid->GetGridCursorCol();
56  }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
int GRID_TRICKS::getCursorRow ( ) const
inlineprotectedinherited

If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based row index.

Definition at line 60 of file grid_tricks.h.

References GRID_TRICKS::getSelectedArea().

Referenced by GRID_TRICKS::onKeyDown(), and GRID_TRICKS::paste_text().

61  {
62  return m_grid->GetGridCursorRow();
63  }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
void GRID_TRICKS::getSelectedArea ( )
protectedinherited

Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above.

Definition at line 128 of file grid_tricks.cpp.

References DBG, GRID_TRICKS::m_grid, GRID_TRICKS::m_sel_col_count, GRID_TRICKS::m_sel_col_start, GRID_TRICKS::m_sel_row_count, and GRID_TRICKS::m_sel_row_start.

Referenced by GRID_TRICKS::getCursorRow(), GRID_TRICKS::onKeyDown(), and GRID_TRICKS::showPopupMenu().

129 {
130  wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
131  wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
132 
133  wxArrayInt cols = m_grid->GetSelectedCols();
134  wxArrayInt rows = m_grid->GetSelectedRows();
135 
136  DBG(printf("topLeft.Count():%d botRight:Count():%d\n", int( topLeft.Count() ), int( botRight.Count() ) );)
137 
138  if( topLeft.Count() && botRight.Count() )
139  {
140  m_sel_row_start = topLeft[0].GetRow();
141  m_sel_col_start = topLeft[0].GetCol();
142 
143  m_sel_row_count = botRight[0].GetRow() - m_sel_row_start + 1;
144  m_sel_col_count = botRight[0].GetCol() - m_sel_col_start + 1;
145  }
146  else if( cols.Count() )
147  {
148  m_sel_col_start = cols[0];
149  m_sel_col_count = cols.Count();
150  m_sel_row_start = 0;
151  m_sel_row_count = m_grid->GetNumberRows();
152  }
153  else if( rows.Count() )
154  {
155  m_sel_col_start = 0;
156  m_sel_col_count = m_grid->GetNumberCols();
157  m_sel_row_start = rows[0];
158  m_sel_row_count = rows.Count();
159  }
160  else
161  {
162  m_sel_row_start = -1;
163  m_sel_col_start = -1;
164  m_sel_row_count = 0;
165  m_sel_col_count = 0;
166  }
167 
168  //DBG(printf("m_sel_row_start:%d m_sel_col_start:%d m_sel_row_count:%d m_sel_col_count:%d\n", m_sel_row_start, m_sel_col_start, m_sel_row_count, m_sel_col_count );)
169 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
int m_sel_row_count
Definition: grid_tricks.h:48
int m_sel_col_start
Definition: grid_tricks.h:47
int m_sel_col_count
Definition: grid_tricks.h:49
int m_sel_row_start
Definition: grid_tricks.h:46
#define DBG(x)
Definition: fctsys.h:33
static bool GRID_TRICKS::isCtl ( int  aChar,
const wxKeyEvent &  e 
)
inlinestaticprotectedinherited

Definition at line 68 of file grid_tricks.h.

References GRID_TRICKS::onGridCellLeftClick().

Referenced by GRID_TRICKS::onKeyDown().

69  {
70  return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() && !e.ShiftDown() && !e.MetaDown();
71  }
void GRID_TRICKS::onGridCellLeftClick ( wxGridEvent &  event)
protectedinherited

Definition at line 107 of file grid_tricks.cpp.

References GRID_TRICKS::m_grid, and GRID_TRICKS::toggleCell().

Referenced by GRID_TRICKS::GRID_TRICKS(), and GRID_TRICKS::isCtl().

108 {
109  int row = aEvent.GetRow();
110  int col = aEvent.GetCol();
111 
112  // Don't make users click twice to toggle a checkbox
113 
114  if( !aEvent.GetModifiers() && toggleCell( row, col ) )
115  {
116  m_grid->ClearSelection();
117  m_grid->SetGridCursor( row, col );
118 
119  // eat event
120  }
121  else
122  {
123  aEvent.Skip();
124  }
125 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
bool toggleCell(int aRow, int aCol)
Definition: grid_tricks.cpp:69
void GRID_TRICKS::onGridCellRightClick ( wxGridEvent &  event)
inlineprotectedinherited

Definition at line 75 of file grid_tricks.h.

References GRID_TRICKS::showPopupMenu().

Referenced by GRID_TRICKS::GRID_TRICKS().

76  {
77  showPopupMenu();
78  }
virtual void showPopupMenu()
void GRID_TRICKS::onKeyDown ( wxKeyEvent &  ev)
protectedinherited

Definition at line 237 of file grid_tricks.cpp.

References GRID_TRICKS::cutcopy(), GRID_TRICKS::getCursorCol(), GRID_TRICKS::getCursorRow(), GRID_TRICKS::getSelectedArea(), GRID_TRICKS::isCtl(), GRID_TRICKS::m_grid, GRID_TRICKS::paste_clipboard(), and GRID_TRICKS::toggleCell().

Referenced by GRID_TRICKS::GRID_TRICKS(), and GRID_TRICKS::onRightDown().

238 {
239  if( isCtl( 'A', ev ) )
240  {
241  m_grid->SelectAll();
242  return;
243  }
244  else if( isCtl( 'C', ev ) )
245  {
246  getSelectedArea();
247  cutcopy( false );
248  return;
249  }
250  else if( isCtl( 'V', ev ) )
251  {
252  getSelectedArea();
253  paste_clipboard();
254  return;
255  }
256  else if( isCtl( 'X', ev ) )
257  {
258  getSelectedArea();
259  cutcopy( true );
260  return;
261  }
262  else if( ev.GetKeyCode() == ' ' )
263  {
264  int row = getCursorRow();
265  int col = getCursorCol();
266 
267  if( m_grid->IsVisible( row, col ) && toggleCell( row, col ) )
268  return;
269  }
270 
271  ev.Skip( true );
272 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
int getCursorCol() const
If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based column index.
Definition: grid_tricks.h:53
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above...
static bool isCtl(int aChar, const wxKeyEvent &e)
Definition: grid_tricks.h:68
bool toggleCell(int aRow, int aCol)
Definition: grid_tricks.cpp:69
virtual void paste_clipboard()
int getCursorRow() const
If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based row index.
Definition: grid_tricks.h:60
virtual void cutcopy(bool doCut)
void GRID_TRICKS::onPopupSelection ( wxCommandEvent &  event)
protectedinherited

Definition at line 209 of file grid_tricks.cpp.

References GRID_TRICKS::cutcopy(), GRID_TRICKS::m_grid, MYID_COPY, MYID_CUT, MYID_PASTE, MYID_SELECT, and GRID_TRICKS::paste_clipboard().

Referenced by GRID_TRICKS::GRID_TRICKS(), and GRID_TRICKS::onRightDown().

210 {
211  int menu_id = event.GetId();
212 
213  // assume getSelectedArea() was called by rightClickPopupMenu() and there's
214  // no way to have gotten here without that having been called.
215 
216  switch( menu_id )
217  {
218  case MYID_CUT:
219  case MYID_COPY:
220  cutcopy( menu_id == MYID_CUT );
221  break;
222 
223  case MYID_PASTE:
224  paste_clipboard();
225  break;
226 
227  case MYID_SELECT:
228  m_grid->SelectAll();
229  break;
230 
231  default:
232  ;
233  }
234 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
virtual void paste_clipboard()
virtual void cutcopy(bool doCut)
void GRID_TRICKS::onRightDown ( wxMouseEvent &  event)
inlineprotectedinherited
void GRID_TRICKS::paste_clipboard ( )
protectedvirtualinherited

Definition at line 275 of file grid_tricks.cpp.

References GRID_TRICKS::m_grid, and GRID_TRICKS::paste_text().

Referenced by GRID_TRICKS::onKeyDown(), GRID_TRICKS::onPopupSelection(), and GRID_TRICKS::onRightDown().

276 {
277  if( wxTheClipboard->Open() )
278  {
279  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
280  {
281  wxTextDataObject data;
282 
283  wxTheClipboard->GetData( data );
284 
285  wxString cb_text = data.GetText();
286 
287  paste_text( cb_text );
288  }
289 
290  wxTheClipboard->Close();
291  m_grid->ForceRefresh();
292  }
293 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
virtual void paste_text(const wxString &cb_text)
virtual void FP_GRID_TRICKS::paste_text ( const wxString &  cb_text)
inlineoverrideprotectedvirtual

handle specialized clipboard text, with leading "(fp_lib_table", OR spreadsheet formatted text.

Reimplemented from GRID_TRICKS.

Definition at line 103 of file dialog_fp_lib_table.cpp.

References LIB_TABLE_GRID::AppendRows(), LIB_TABLE::At(), DisplayError(), LIB_TABLE::GetCount(), LIB_TABLE_GRID::GetNumberRows(), i, max, FP_LIB_TABLE::Parse(), GRID_TRICKS::paste_text(), LIB_TABLE::rows, TO_UTF8, and IO_ERROR::What().

104  {
105  FP_LIB_TABLE_GRID* tbl = (FP_LIB_TABLE_GRID*) m_grid->GetTable();
106 
107  size_t ndx = cb_text.find( "(fp_lib_table" );
108 
109  if( ndx != std::string::npos )
110  {
111  // paste the FP_LIB_TABLE_ROWs of s-expression (fp_lib_table), starting
112  // at column 0 regardless of current cursor column.
113 
114  STRING_LINE_READER slr( TO_UTF8( cb_text ), "Clipboard" );
115  LIB_TABLE_LEXER lexer( &slr );
116  FP_LIB_TABLE tmp_tbl;
117  bool parsed = true;
118 
119  try
120  {
121  tmp_tbl.Parse( &lexer );
122  }
123  catch( PARSE_ERROR& pe )
124  {
125  DisplayError( NULL, pe.What() );
126  parsed = false;
127  }
128 
129  if( parsed )
130  {
131  const int cur_row = std::max( getCursorRow(), 0 );
132 
133  // if clipboard rows would extend past end of current table size...
134  if( tmp_tbl.GetCount() > tbl->GetNumberRows() - cur_row )
135  {
136  int newRowsNeeded = tmp_tbl.GetCount() - ( tbl->GetNumberRows() - cur_row );
137  tbl->AppendRows( newRowsNeeded );
138  }
139 
140  for( int i = 0; i < tmp_tbl.GetCount(); ++i )
141  {
142  tbl->rows.replace( cur_row+i, tmp_tbl.At( i ) );
143  }
144  }
145 
146  m_grid->AutoSizeColumns( false );
147  }
148  else
149  {
150  // paste spreadsheet formatted text.
151  GRID_TRICKS::paste_text( cb_text );
152  }
153  }
int GetCount()
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
LIB_TABLE_ROW * At(int aIndex)
LIB_TABLE_ROWS rows
int GetNumberRows() override
virtual void paste_text(const wxString &cb_text)
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
virtual void Parse(LIB_TABLE_LEXER *aLexer) override
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
bool AppendRows(size_t aNumRows=1) override
int getCursorRow() const
If the cursor is not on a valid cell, because there are no rows at all, return -1, else return a 0 based row index.
Definition: grid_tricks.h:60
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller&#39;s report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
#define max(a, b)
Definition: auxiliary.h:86
size_t i
Definition: json11.cpp:597
This class builds a wxGridTableBase by wrapping an FP_LIB_TABLE object.
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
void GRID_TRICKS::showPopupMenu ( )
protectedvirtualinherited

Definition at line 172 of file grid_tricks.cpp.

References GRID_TRICKS::getSelectedArea(), GRID_TRICKS::m_grid, GRID_TRICKS::m_sel_col_count, GRID_TRICKS::m_sel_row_count, MYID_COPY, MYID_CUT, MYID_PASTE, and MYID_SELECT.

Referenced by GRID_TRICKS::onGridCellRightClick(), and GRID_TRICKS::onRightDown().

173 {
174  wxMenu menu;
175 
176  menu.Append( MYID_CUT, _( "Cut\tCTRL+X" ), _( "Clear selected cells pasting original contents to clipboard" ) );
177  menu.Append( MYID_COPY, _( "Copy\tCTRL+C" ), _( "Copy selected cells to clipboard" ) );
178  menu.Append( MYID_PASTE, _( "Paste\tCTRL+V" ), _( "Paste clipboard cells to matrix at current cell" ) );
179  menu.Append( MYID_SELECT, _( "Select All\tCTRL+A" ), _( "Select all cells" ) );
180 
181  getSelectedArea();
182 
183  // if nothing is selected, disable cut and copy.
185  {
186  menu.Enable( MYID_CUT, false );
187  menu.Enable( MYID_COPY, false );
188  }
189 
190  bool have_cb_text = false;
191  if( wxTheClipboard->Open() )
192  {
193  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
194  have_cb_text = true;
195 
196  wxTheClipboard->Close();
197  }
198 
199  if( !have_cb_text )
200  {
201  // if nothing on clipboard, disable paste.
202  menu.Enable( MYID_PASTE, false );
203  }
204 
205  m_grid->PopupMenu( &menu );
206 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42
void getSelectedArea()
Puts the selected area into a sensible rectangle of m_sel_{row,col}_{start,count} above...
int m_sel_row_count
Definition: grid_tricks.h:48
int m_sel_col_count
Definition: grid_tricks.h:49
bool GRID_TRICKS::toggleCell ( int  aRow,
int  aCol 
)
protectedinherited

Definition at line 69 of file grid_tricks.cpp.

References GRID_TRICKS::m_grid.

Referenced by GRID_TRICKS::onGridCellLeftClick(), GRID_TRICKS::onKeyDown(), and GRID_TRICKS::onRightDown().

70 {
71  auto renderer = m_grid->GetCellRenderer( aRow, aCol );
72  bool isCheckbox = ( dynamic_cast<wxGridCellBoolRenderer*>( renderer ) != nullptr );
73  renderer->DecRef();
74 
75  if( isCheckbox )
76  {
77  wxGridTableBase* model = m_grid->GetTable();
78 
79  if( model->CanGetValueAs( aRow, aCol, wxGRID_VALUE_BOOL )
80  && model->CanSetValueAs( aRow, aCol, wxGRID_VALUE_BOOL ))
81  {
82  model->SetValueAsBool( aRow, aCol, !model->GetValueAsBool( aRow, aCol ));
83  }
84  else // fall back to string processing
85  {
86  if( model->GetValue( aRow, aCol ) == wxT( "1" ) )
87  model->SetValue( aRow, aCol, wxT( "0" ) );
88  else
89  model->SetValue( aRow, aCol, wxT( "1" ) );
90  }
91 
92  // Mac needs this for the keyboard events; Linux appears to always need it.
93  m_grid->ForceRefresh();
94 
95  // Let any clients know
96  wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, aRow, aCol );
97  event.SetString( model->GetValue( aRow, aCol ) );
98  m_grid->GetEventHandler()->ProcessEvent( event );
99 
100  return true;
101  }
102 
103  return false;
104 }
wxGrid * m_grid
I don&#39;t own the grid, but he owns me.
Definition: grid_tricks.h:42

Member Data Documentation

int GRID_TRICKS::m_sel_col_count
protectedinherited
int GRID_TRICKS::m_sel_col_start
protectedinherited
int GRID_TRICKS::m_sel_row_count
protectedinherited
int GRID_TRICKS::m_sel_row_start
protectedinherited

The documentation for this class was generated from the following file: