KiCad PCB EDA Suite
GRID_TRICKS Class Reference

Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance. More...

#include <grid_tricks.h>

Inheritance diagram for GRID_TRICKS:
FP_GRID_TRICKS SYMBOL_GRID_TRICKS

Public Member Functions

 GRID_TRICKS (wxGrid *aGrid)
 

Protected Member Functions

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 onGridCellRightClick (wxGridEvent &event)
 
void onRightDown (wxMouseEvent &event)
 
virtual void showPopupMenu ()
 
void onPopupSelection (wxCommandEvent &event)
 
void onKeyDown (wxKeyEvent &ev)
 
virtual void paste_clipboard ()
 
virtual void paste_text (const wxString &cb_text)
 
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

Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance.

Definition at line 34 of file grid_tricks.h.

Constructor & Destructor Documentation

GRID_TRICKS::GRID_TRICKS ( wxGrid *  aGrid)

Definition at line 52 of file grid_tricks.cpp.

References m_sel_col_count, m_sel_col_start, m_sel_row_count, m_sel_row_start, MYID_FIRST, MYID_LAST, onGridCellRightClick(), onKeyDown(), onPopupSelection(), and onRightDown().

52  :
53  m_grid( aGrid )
54 {
55  m_sel_row_start = 0;
56  m_sel_col_start = 0;
57  m_sel_row_count = 0;
58  m_sel_col_count = 0;
59 
60  aGrid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEventHandler( GRID_TRICKS::onGridCellRightClick ), NULL, this );
61  aGrid->Connect( MYID_FIRST, MYID_LAST, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GRID_TRICKS::onPopupSelection ), NULL, this );
62  aGrid->Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( GRID_TRICKS::onKeyDown ), NULL, this );
63  aGrid->Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( GRID_TRICKS::onRightDown ), NULL, this );
64 }
wxGrid * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:42
int m_sel_row_count
Definition: grid_tricks.h:48
void onRightDown(wxMouseEvent &event)
Definition: grid_tricks.h:78
void onGridCellRightClick(wxGridEvent &event)
Definition: grid_tricks.h:73
int m_sel_col_start
Definition: grid_tricks.h:47
void onPopupSelection(wxCommandEvent &event)
int m_sel_col_count
Definition: grid_tricks.h:49
int m_sel_row_start
Definition: grid_tricks.h:46
void onKeyDown(wxKeyEvent &ev)

Member Function Documentation

void GRID_TRICKS::cutcopy ( bool  doCut)
protectedvirtual

Definition at line 258 of file grid_tricks.cpp.

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

Referenced by onKeyDown(), and onPopupSelection().

259 {
260  if( wxTheClipboard->Open() )
261  {
262  wxGridTableBase* tbl = m_grid->GetTable();
263  wxString txt;
264 
265  // fill txt with a format that is compatible with most spreadsheets
266  for( int row = m_sel_row_start; row < m_sel_row_start + m_sel_row_count; ++row )
267  {
268  for( int col = m_sel_col_start; col < m_sel_col_start + m_sel_col_count; ++col )
269  {
270  txt += tbl->GetValue( row, col );
271 
272  if( col < m_sel_col_start + m_sel_col_count - 1 ) // that was not last column
273  txt += COL_SEP;
274 
275  if( doCut )
276  tbl->SetValue( row, col, wxEmptyString );
277  }
278  txt += ROW_SEP;
279  }
280 
281  wxTheClipboard->SetData( new wxTextDataObject( txt ) );
282  wxTheClipboard->Close();
283 
284  if( doCut )
285  {
286  m_grid->AutoSizeColumns( false );
287  m_grid->ForceRefresh();
288  }
289  }
290 }
wxGrid * m_grid
I don'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
inlineprotected

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 paste_text().

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

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.

Referenced by SYMBOL_GRID_TRICKS::paste_text(), paste_text(), and FP_GRID_TRICKS::paste_text().

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

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

Definition at line 67 of file grid_tricks.cpp.

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

Referenced by onKeyDown(), and showPopupMenu().

68 {
69  wxGridCellCoordsArray topLeft = m_grid->GetSelectionBlockTopLeft();
70  wxGridCellCoordsArray botRight = m_grid->GetSelectionBlockBottomRight();
71 
72  wxArrayInt cols = m_grid->GetSelectedCols();
73  wxArrayInt rows = m_grid->GetSelectedRows();
74 
75  DBG(printf("topLeft.Count():%d botRight:Count():%d\n", int( topLeft.Count() ), int( botRight.Count() ) );)
76 
77  if( topLeft.Count() && botRight.Count() )
78  {
79  m_sel_row_start = topLeft[0].GetRow();
80  m_sel_col_start = topLeft[0].GetCol();
81 
82  m_sel_row_count = botRight[0].GetRow() - m_sel_row_start + 1;
83  m_sel_col_count = botRight[0].GetCol() - m_sel_col_start + 1;
84  }
85  else if( cols.Count() )
86  {
87  m_sel_col_start = cols[0];
88  m_sel_col_count = cols.Count();
89  m_sel_row_start = 0;
90  m_sel_row_count = m_grid->GetNumberRows();
91  }
92  else if( rows.Count() )
93  {
94  m_sel_col_start = 0;
95  m_sel_col_count = m_grid->GetNumberCols();
96  m_sel_row_start = rows[0];
97  m_sel_row_count = rows.Count();
98  }
99  else
100  {
101  m_sel_row_start = -1;
102  m_sel_col_start = -1;
103  m_sel_row_count = 0;
104  m_sel_col_count = 0;
105  }
106 
107  //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 );)
108 }
wxGrid * m_grid
I don'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 
)
inlinestaticprotected

Definition at line 68 of file grid_tricks.h.

Referenced by onKeyDown().

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

Definition at line 73 of file grid_tricks.h.

References showPopupMenu().

Referenced by GRID_TRICKS().

74  {
75  showPopupMenu();
76  }
virtual void showPopupMenu()
void GRID_TRICKS::onKeyDown ( wxKeyEvent &  ev)
protected

Definition at line 176 of file grid_tricks.cpp.

References cutcopy(), getSelectedArea(), isCtl(), m_grid, and paste_clipboard().

Referenced by GRID_TRICKS().

177 {
178  if( isCtl( 'A', ev ) )
179  {
180  m_grid->SelectAll();
181  }
182  else if( isCtl( 'C', ev ) )
183  {
184  getSelectedArea();
185  cutcopy( false );
186  }
187  else if( isCtl( 'V', ev ) )
188  {
189  getSelectedArea();
190  paste_clipboard();
191  }
192  else if( isCtl( 'X', ev ) )
193  {
194  getSelectedArea();
195  cutcopy( true );
196  }
197  else
198  {
199  ev.Skip( true );
200  }
201 }
wxGrid * m_grid
I don'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...
Definition: grid_tricks.cpp:67
static bool isCtl(int aChar, const wxKeyEvent &e)
Definition: grid_tricks.h:68
virtual void paste_clipboard()
virtual void cutcopy(bool doCut)
void GRID_TRICKS::onPopupSelection ( wxCommandEvent &  event)
protected

Definition at line 148 of file grid_tricks.cpp.

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

Referenced by GRID_TRICKS().

149 {
150  int menu_id = event.GetId();
151 
152  // assume getSelectedArea() was called by rightClickPopupMenu() and there's
153  // no way to have gotten here without that having been called.
154 
155  switch( menu_id )
156  {
157  case MYID_CUT:
158  case MYID_COPY:
159  cutcopy( menu_id == MYID_CUT );
160  break;
161 
162  case MYID_PASTE:
163  paste_clipboard();
164  break;
165 
166  case MYID_SELECT:
167  m_grid->SelectAll();
168  break;
169 
170  default:
171  ;
172  }
173 }
wxGrid * m_grid
I don'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)
inlineprotected

Definition at line 78 of file grid_tricks.h.

References showPopupMenu().

Referenced by GRID_TRICKS().

79  {
80  showPopupMenu();
81  }
virtual void showPopupMenu()
void GRID_TRICKS::paste_clipboard ( )
protectedvirtual

Definition at line 204 of file grid_tricks.cpp.

References m_grid, and paste_text().

Referenced by onKeyDown(), and onPopupSelection().

205 {
206  if( wxTheClipboard->Open() )
207  {
208  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
209  {
210  wxTextDataObject data;
211 
212  wxTheClipboard->GetData( data );
213 
214  wxString cb_text = data.GetText();
215 
216  paste_text( cb_text );
217  }
218 
219  wxTheClipboard->Close();
220  m_grid->ForceRefresh();
221  }
222 }
wxGrid * m_grid
I don't own the grid, but he owns me.
Definition: grid_tricks.h:42
virtual void paste_text(const wxString &cb_text)
void GRID_TRICKS::paste_text ( const wxString &  cb_text)
protectedvirtual

Reimplemented in FP_GRID_TRICKS, and SYMBOL_GRID_TRICKS.

Definition at line 225 of file grid_tricks.cpp.

References COL_SEP, getCursorCol(), getCursorRow(), m_grid, max, and ROW_SEP.

Referenced by paste_clipboard(), SYMBOL_GRID_TRICKS::paste_text(), and FP_GRID_TRICKS::paste_text().

226 {
227  wxGridTableBase* tbl = m_grid->GetTable();
228 
229  const int cur_row = std::max( getCursorRow(), 0 ); // no -1
230  const int cur_col = std::max( getCursorCol(), 0 );
231 
232  wxStringTokenizer rows( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
233 
234  // if clipboard rows would extend past end of current table size...
235  if( int( rows.CountTokens() ) > tbl->GetNumberRows() - cur_row )
236  {
237  int newRowsNeeded = rows.CountTokens() - ( tbl->GetNumberRows() - cur_row );
238 
239  tbl->AppendRows( newRowsNeeded );
240  }
241 
242  for( int row = cur_row; rows.HasMoreTokens(); ++row )
243  {
244  wxString rowTxt = rows.GetNextToken();
245 
246  wxStringTokenizer cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
247 
248  for( int col = cur_col; cols.HasMoreTokens(); ++col )
249  {
250  wxString cellTxt = cols.GetNextToken();
251  tbl->SetValue( row, col, cellTxt );
252  }
253  }
254  m_grid->AutoSizeColumns( false );
255 }
wxGrid * m_grid
I don'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
#define ROW_SEP
Definition: grid_tricks.cpp:38
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
#define max(a, b)
Definition: auxiliary.h:86
#define COL_SEP
Definition: grid_tricks.cpp:37
void GRID_TRICKS::showPopupMenu ( )
protectedvirtual

Definition at line 111 of file grid_tricks.cpp.

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

Referenced by onGridCellRightClick(), and onRightDown().

112 {
113  wxMenu menu;
114 
115  menu.Append( MYID_CUT, _( "Cut\tCTRL+X" ), _( "Clear selected cells pasting original contents to clipboard" ) );
116  menu.Append( MYID_COPY, _( "Copy\tCTRL+C" ), _( "Copy selected cells to clipboard" ) );
117  menu.Append( MYID_PASTE, _( "Paste\tCTRL+V" ), _( "Paste clipboard cells to matrix at current cell" ) );
118  menu.Append( MYID_SELECT, _( "Select All\tCTRL+A" ), _( "Select all cells" ) );
119 
120  getSelectedArea();
121 
122  // if nothing is selected, disable cut and copy.
124  {
125  menu.Enable( MYID_CUT, false );
126  menu.Enable( MYID_COPY, false );
127  }
128 
129  bool have_cb_text = false;
130  if( wxTheClipboard->Open() )
131  {
132  if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
133  have_cb_text = true;
134 
135  wxTheClipboard->Close();
136  }
137 
138  if( !have_cb_text )
139  {
140  // if nothing on clipboard, disable paste.
141  menu.Enable( MYID_PASTE, false );
142  }
143 
144  m_grid->PopupMenu( &menu );
145 }
wxGrid * m_grid
I don'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...
Definition: grid_tricks.cpp:67
int m_sel_row_count
Definition: grid_tricks.h:48
int m_sel_col_count
Definition: grid_tricks.h:49

Member Data Documentation

wxGrid* GRID_TRICKS::m_grid
protected
int GRID_TRICKS::m_sel_col_count
protected

Definition at line 49 of file grid_tricks.h.

Referenced by cutcopy(), getSelectedArea(), GRID_TRICKS(), and showPopupMenu().

int GRID_TRICKS::m_sel_col_start
protected

Definition at line 47 of file grid_tricks.h.

Referenced by cutcopy(), getSelectedArea(), and GRID_TRICKS().

int GRID_TRICKS::m_sel_row_count
protected

Definition at line 48 of file grid_tricks.h.

Referenced by cutcopy(), getSelectedArea(), GRID_TRICKS(), and showPopupMenu().

int GRID_TRICKS::m_sel_row_start
protected

Definition at line 46 of file grid_tricks.h.

Referenced by cutcopy(), getSelectedArea(), and GRID_TRICKS().


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