~andrew-plumb/kicad/kicad

« back to all changes in this revision

Viewing changes to pcbnew/dialogs/dialog_fp_lib_table.cpp

  • Committer: Dick Hollenbeck
  • Date: 2013-10-03 22:12:56 UTC
  • Revision ID: dick@softplc.com-20131003221256-ny7xs3ucnjlj12j1
DIALOG_FP_LIB_TABLE does not use wxSplitterWindow, it would not obey.
Paste popup menu in this dialog now allows pasting a (fp_lib_table)
s-expression element holding (lib) elements.  This provides an 
opportunity for GITHUB library publishers to put the table description
into their README.md file as a (fp_lib_table) element where it can
be quickly copied to clipboard and pasted into the fp lib table dialog.
This is a paste operation, and overwrites from the initial cursor position.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include <fctsys.h>
42
42
#include <dialog_fp_lib_table_base.h>
43
43
#include <fp_lib_table.h>
 
44
#include <fp_lib_table_lexer.h>
44
45
#include <wx/grid.h>
45
46
#include <wx/clipbrd.h>
46
47
#include <wx/tokenzr.h>
287
288
            selColCount = 0;
288
289
        }
289
290
 
290
 
        // D(printf("selRowStart:%d selColStart:%d selRowCount:%d selColCount:%d\n", selRowStart, selColStart, selRowCount, selColCount );)
 
291
        D(printf("selRowStart:%d selColStart:%d selRowCount:%d selColCount:%d\n", selRowStart, selColStart, selRowCount, selColCount );)
291
292
    }
292
293
 
293
294
    void rightClickCellPopupMenu()
300
301
 
301
302
        getSelectedArea();
302
303
 
303
 
        // if nothing is selected, diable cut and copy.
 
304
        // if nothing is selected, disable cut and copy.
304
305
        if( !selRowCount && !selColCount )
305
306
        {
306
307
            menu.Enable( ID_CUT,  false );
307
308
            menu.Enable( ID_COPY, false );
308
309
        }
309
310
 
 
311
        bool have_cb_text = false;
 
312
        if( wxTheClipboard->Open() )
 
313
        {
 
314
            if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
 
315
                have_cb_text = true;
 
316
 
 
317
            wxTheClipboard->Close();
 
318
        }
 
319
 
 
320
        if( !have_cb_text )
 
321
        {
 
322
            // if nothing on clipboard, disable paste.
 
323
            menu.Enable( ID_PASTE, false );
 
324
        }
 
325
 
310
326
        // if there is no current cell cursor, disable paste.
311
 
        if( m_cur_row == -1 || m_cur_col == -1 )
 
327
        else if( m_cur_row == -1 || m_cur_col == -1 )
312
328
            menu.Enable( ID_PASTE, false );
313
329
 
314
330
        PopupMenu( &menu );
362
378
            {
363
379
                if( wxTheClipboard->IsSupported( wxDF_TEXT ) )
364
380
                {
365
 
                    wxGridTableBase*    tbl = m_cur_grid->GetTable();
366
381
                    wxTextDataObject    data;
 
382
                    FP_TBL_MODEL*       tbl = (FP_TBL_MODEL*) m_cur_grid->GetTable();
367
383
 
368
384
                    wxTheClipboard->GetData( data );
369
385
 
370
 
                    wxStringTokenizer   rows( data.GetText(), ROW_SEP, wxTOKEN_RET_EMPTY );
 
386
                    wxString    cb_text = data.GetText();
 
387
                    size_t      ndx = cb_text.find_first_of( wxT( "(fp_lib_table " ) );
371
388
 
372
 
                    // if clipboard rows would extend past end of current table size...
373
 
                    if( int( rows.CountTokens() ) > tbl->GetNumberRows() - m_cur_row )
 
389
                    if( ndx != std::string::npos )
374
390
                    {
375
 
                        int newRowsNeeded = rows.CountTokens() - ( tbl->GetNumberRows() - m_cur_row );
376
 
                        tbl->AppendRows( newRowsNeeded );
 
391
                        // paste the ROWs of s-expression (fp_lib_table), starting
 
392
                        // at column 0 regardless of current cursor column.
 
393
 
 
394
                        STRING_LINE_READER  slr( TO_UTF8( cb_text ), wxT( "Clipboard" ) );
 
395
                        FP_LIB_TABLE_LEXER  lexer( &slr );
 
396
                        FP_LIB_TABLE        tmp_tbl;
 
397
                        bool                parsed = true;
 
398
 
 
399
                        try
 
400
                        {
 
401
                            tmp_tbl.Parse( &lexer );
 
402
                        }
 
403
                        catch( PARSE_ERROR& pe )
 
404
                        {
 
405
                            // @todo tell what line and offset
 
406
                            parsed = false;
 
407
                        }
 
408
 
 
409
                        if( parsed )
 
410
                        {
 
411
                            // if clipboard rows would extend past end of current table size...
 
412
                            if( int( tmp_tbl.rows.size() ) > tbl->GetNumberRows() - m_cur_row )
 
413
                            {
 
414
                                int newRowsNeeded = tmp_tbl.rows.size() - ( tbl->GetNumberRows() - m_cur_row );
 
415
                                tbl->AppendRows( newRowsNeeded );
 
416
                            }
 
417
 
 
418
                            for( int i = 0;  i < (int) tmp_tbl.rows.size();  ++i )
 
419
                            {
 
420
                                tbl->rows[m_cur_row+i] = tmp_tbl.rows[i];
 
421
                            }
 
422
                        }
 
423
                        m_cur_grid->AutoSizeColumns();
377
424
                    }
378
 
 
379
 
                    for( int row = m_cur_row;  rows.HasMoreTokens();  ++row )
 
425
                    else
380
426
                    {
381
 
                        wxString rowTxt = rows.GetNextToken();
382
 
 
383
 
                        wxStringTokenizer   cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
384
 
 
385
 
                        for( int col = m_cur_col; cols.HasMoreTokens();  ++col )
386
 
                        {
387
 
                            wxString cellTxt = cols.GetNextToken();
388
 
                            tbl->SetValue( row, col, cellTxt );
 
427
                        wxStringTokenizer   rows( cb_text, ROW_SEP, wxTOKEN_RET_EMPTY );
 
428
 
 
429
                        // if clipboard rows would extend past end of current table size...
 
430
                        if( int( rows.CountTokens() ) > tbl->GetNumberRows() - m_cur_row )
 
431
                        {
 
432
                            int newRowsNeeded = rows.CountTokens() - ( tbl->GetNumberRows() - m_cur_row );
 
433
                            tbl->AppendRows( newRowsNeeded );
 
434
                        }
 
435
 
 
436
                        for( int row = m_cur_row;  rows.HasMoreTokens();  ++row )
 
437
                        {
 
438
                            wxString rowTxt = rows.GetNextToken();
 
439
 
 
440
                            wxStringTokenizer   cols( rowTxt, COL_SEP, wxTOKEN_RET_EMPTY );
 
441
 
 
442
                            for( int col = m_cur_col; cols.HasMoreTokens();  ++col )
 
443
                            {
 
444
                                wxString cellTxt = cols.GetNextToken();
 
445
                                tbl->SetValue( row, col, cellTxt );
 
446
                            }
389
447
                        }
390
448
                    }
391
449
                }
730
788
        m_project( aProject ),
731
789
        m_global_model( *aGlobal ),
732
790
        m_project_model( *aProject ),
733
 
        m_cur_row( -1 ),
734
 
        m_cur_col( -1 )
 
791
        m_cur_row( 0 ),
 
792
        m_cur_col( 0 )
735
793
    {
736
794
        m_global_grid->SetTable( (wxGridTableBase*) &m_global_model );
737
795
        m_project_grid->SetTable( (wxGridTableBase*) &m_project_model );
740
798
        m_project_grid->AutoSizeColumns( false );
741
799
 
742
800
        wxArrayString choices;
 
801
 
743
802
        choices.Add( IO_MGR::ShowType( IO_MGR::KICAD ) );
744
 
#if defined(BUILD_GITHUB_PLUGIN)
745
803
        choices.Add( IO_MGR::ShowType( IO_MGR::GITHUB ) );
746
 
#endif
747
804
        choices.Add( IO_MGR::ShowType( IO_MGR::LEGACY ) );
748
805
        choices.Add( IO_MGR::ShowType( IO_MGR::EAGLE ) );
749
806
        choices.Add( IO_MGR::ShowType( IO_MGR::GEDA_PCB ) );