basepcbframe.cpp 29.4 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file basepcbframe.cpp
 */
30

31
#include <fctsys.h>
32
#include <kiface_i.h>
33 34 35
#include <wxstruct.h>
#include <pcbcommon.h>
#include <confirm.h>
36 37
//#include <pgm_base.h>
#include <kiface_i.h>
38 39 40
#include <dialog_helpers.h>
#include <kicad_device_context.h>
#include <wxBasePcbFrame.h>
41
#include <base_units.h>
42
#include <msgpanel.h>
43

44
#include <pcbnew.h>
45
#include <fp_lib_table.h>
46 47
#include <pcbnew_id.h>
#include <class_board.h>
48 49 50
#include <class_track.h>
#include <class_module.h>
#include <class_drawsegment.h>
51

52 53
#include <collectors.h>
#include <class_drawpanel.h>
54
#include <class_draw_panel_gal.h>
55
#include <view/view.h>
56
#include <math/vector2d.h>
57
#include <trigo.h>
58
#include <pcb_painter.h>
59
#include <worksheet_viewitem.h>
Maciej Suminski's avatar
Maciej Suminski committed
60 61
#include <ratsnest_data.h>
#include <ratsnest_viewitem.h>
62

63 64
#include <tool/tool_manager.h>
#include <tool/tool_dispatcher.h>
65

66
// Configuration entry names.
67 68 69
static const wxString UserGridSizeXEntry( wxT( "PcbUserGrid_X" ) );
static const wxString UserGridSizeYEntry( wxT( "PcbUserGrid_Y" ) );
static const wxString UserGridUnitsEntry( wxT( "PcbUserGrid_Unit" ) );
70
static const wxString DisplayPadFillEntry( wxT( "DiPadFi" ) );
dickelbeck's avatar
dickelbeck committed
71
static const wxString DisplayViaFillEntry( wxT( "DiViaFi" ) );
72 73 74
static const wxString DisplayPadNumberEntry( wxT( "DiPadNu" ) );
static const wxString DisplayModuleEdgeEntry( wxT( "DiModEd" ) );
static const wxString DisplayModuleTextEntry( wxT( "DiModTx" ) );
Roman Bashkov's avatar
Roman Bashkov committed
75 76
static const wxString FastGrid1Entry( wxT( "FastGrid1" ) );
static const wxString FastGrid2Entry( wxT( "FastGrid2" ) );
77

78 79
const LAYER_NUM PCB_BASE_FRAME::GAL_LAYER_ORDER[] =
{
80
    ITEM_GAL_LAYER( GP_OVERLAY ),
81
    ITEM_GAL_LAYER( DRC_VISIBLE ),
82
    NETNAMES_GAL_LAYER( PADS_NETNAMES_VISIBLE ),
83 84 85 86 87
    DRAW_N, COMMENT_N, ECO1_N, ECO2_N, EDGE_N,
    UNUSED_LAYER_29, UNUSED_LAYER_30, UNUSED_LAYER_31,
    ITEM_GAL_LAYER( MOD_TEXT_FR_VISIBLE ),
    ITEM_GAL_LAYER( MOD_REFERENCES_VISIBLE), ITEM_GAL_LAYER( MOD_VALUES_VISIBLE ),

Maciej Suminski's avatar
Maciej Suminski committed
88
    ITEM_GAL_LAYER( RATSNEST_VISIBLE ),
89 90 91
    ITEM_GAL_LAYER( VIAS_HOLES_VISIBLE ), ITEM_GAL_LAYER( PADS_HOLES_VISIBLE ),
    ITEM_GAL_LAYER( VIAS_VISIBLE ), ITEM_GAL_LAYER( PADS_VISIBLE ),

92 93
    NETNAMES_GAL_LAYER( PAD_FR_NETNAMES_VISIBLE ), ITEM_GAL_LAYER( PAD_FR_VISIBLE ), SOLDERMASK_N_FRONT,
    NETNAMES_GAL_LAYER( LAYER_16_NETNAMES_VISIBLE ), LAYER_N_FRONT,
94
    SILKSCREEN_N_FRONT, SOLDERPASTE_N_FRONT, ADHESIVE_N_FRONT,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    NETNAMES_GAL_LAYER( LAYER_15_NETNAMES_VISIBLE ), LAYER_N_15,
    NETNAMES_GAL_LAYER( LAYER_14_NETNAMES_VISIBLE ), LAYER_N_14,
    NETNAMES_GAL_LAYER( LAYER_13_NETNAMES_VISIBLE ), LAYER_N_13,
    NETNAMES_GAL_LAYER( LAYER_12_NETNAMES_VISIBLE ), LAYER_N_12,
    NETNAMES_GAL_LAYER( LAYER_11_NETNAMES_VISIBLE ), LAYER_N_11,
    NETNAMES_GAL_LAYER( LAYER_10_NETNAMES_VISIBLE ), LAYER_N_10,
    NETNAMES_GAL_LAYER( LAYER_9_NETNAMES_VISIBLE ), LAYER_N_9,
    NETNAMES_GAL_LAYER( LAYER_8_NETNAMES_VISIBLE ), LAYER_N_8,
    NETNAMES_GAL_LAYER( LAYER_7_NETNAMES_VISIBLE ), LAYER_N_7,
    NETNAMES_GAL_LAYER( LAYER_6_NETNAMES_VISIBLE ), LAYER_N_6,
    NETNAMES_GAL_LAYER( LAYER_5_NETNAMES_VISIBLE ), LAYER_N_5,
    NETNAMES_GAL_LAYER( LAYER_4_NETNAMES_VISIBLE ), LAYER_N_4,
    NETNAMES_GAL_LAYER( LAYER_3_NETNAMES_VISIBLE ), LAYER_N_3,
    NETNAMES_GAL_LAYER( LAYER_2_NETNAMES_VISIBLE ), LAYER_N_2,
    NETNAMES_GAL_LAYER( PAD_BK_NETNAMES_VISIBLE ), ITEM_GAL_LAYER( PAD_BK_VISIBLE ), SOLDERMASK_N_BACK,
    NETNAMES_GAL_LAYER( LAYER_1_NETNAMES_VISIBLE ), LAYER_N_BACK,
111 112 113 114 115

    ADHESIVE_N_BACK, SOLDERPASTE_N_BACK, SILKSCREEN_N_BACK,
    ITEM_GAL_LAYER( MOD_TEXT_BK_VISIBLE ),
    ITEM_GAL_LAYER( WORKSHEET )
};
116

117
BEGIN_EVENT_TABLE( PCB_BASE_FRAME, EDA_DRAW_FRAME )
118
    EVT_MENU_RANGE( ID_POPUP_PCB_ITEM_SELECTION_START, ID_POPUP_PCB_ITEM_SELECTION_END,
119 120 121 122 123 124 125 126 127
                    PCB_BASE_FRAME::ProcessItemSelection )

    EVT_TOOL( ID_TB_OPTIONS_SHOW_POLAR_COORD, PCB_BASE_FRAME::OnTogglePolarCoords )
    EVT_TOOL( ID_TB_OPTIONS_SHOW_PADS_SKETCH, PCB_BASE_FRAME::OnTogglePadDrawMode )

    EVT_UPDATE_UI( ID_TB_OPTIONS_SHOW_POLAR_COORD, PCB_BASE_FRAME::OnUpdateCoordType )
    EVT_UPDATE_UI( ID_TB_OPTIONS_SHOW_PADS_SKETCH, PCB_BASE_FRAME::OnUpdatePadDrawMode )
    EVT_UPDATE_UI( ID_ON_GRID_SELECT, PCB_BASE_FRAME::OnUpdateSelectGrid )
    EVT_UPDATE_UI( ID_ON_ZOOM_SELECT, PCB_BASE_FRAME::OnUpdateSelectZoom )
128

129
    EVT_UPDATE_UI_RANGE( ID_ZOOM_IN, ID_ZOOM_PAGE, PCB_BASE_FRAME::OnUpdateSelectZoom )
130
END_EVENT_TABLE()
131 132


133 134 135
PCB_BASE_FRAME::PCB_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, ID_DRAWFRAME_TYPE aFrameType,
        const wxString& aTitle, const wxPoint& aPos, const wxSize& aSize,
        long aStyle, const wxString & aFrameName ) :
Maciej Suminski's avatar
Maciej Suminski committed
136
    EDA_DRAW_FRAME( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
137
    m_toolManager( TOOL_MANAGER::Instance() )
138
{
139
    m_Pcb                 = NULL;
140
    m_toolDispatcher      = NULL;
141

142
    m_DisplayPadFill      = true;   // How to draw pads
dickelbeck's avatar
dickelbeck committed
143
    m_DisplayViaFill      = true;   // How to draw vias
144
    m_DisplayPadNum       = true;   // show pads number
145

146 147
    m_DisplayModEdge      = FILLED; // How to display module drawings (line/ filled / sketch)
    m_DisplayModText      = FILLED; // How to display module texts (line/ filled / sketch)
148
    m_DisplayPcbTrackFill = true;   // false = sketch , true = filled
149
    m_Draw3DFrame         = NULL;   // Display Window in 3D mode (OpenGL)
150

151
    m_UserGridSize        = wxRealPoint( 100.0, 100.0 );
152
    m_UserGridUnit        = INCHES;
153
    m_Collector           = new GENERAL_COLLECTOR();
Roman Bashkov's avatar
Roman Bashkov committed
154 155 156

    m_FastGrid1           = 0;
    m_FastGrid2           = 0;
157

158 159 160 161
    SetGalCanvas( new EDA_DRAW_PANEL_GAL(
            this, -1, wxPoint( 0, 0 ), m_FrameSize,
            EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL ) );

162
    // Hide by default, it has to be explicitly shown
163
    GetGalCanvas()->Hide();
164

165
    m_auxiliaryToolBar    = NULL;
166 167
}

168

169
PCB_BASE_FRAME::~PCB_BASE_FRAME()
170
{
171
    delete m_Collector;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
172

173
    delete m_Pcb;       // is already NULL for FOOTPRINT_EDIT_FRAME
174
    delete GetGalCanvas();
175 176 177
}


178 179 180 181 182 183 184
FP_LIB_TABLE* PCB_BASE_FRAME::FootprintLibs() const
{
    PROJECT&        prj = Prj();
    FP_LIB_TABLE*   tbl = dynamic_cast<FP_LIB_TABLE*>( prj.Elem( PROJECT::FPTBL ) );

    if( !tbl )
    {
185 186 187
        // Stack the project specific FP_LIB_TABLE overlay on top of the global table.
        // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
        // stack this way, all using the same global fallback table.
188
        tbl = new FP_LIB_TABLE( &GFootprintTable );
189

190 191 192 193 194 195 196
        prj.Elem( PROJECT::FPTBL, tbl );
    }

    return tbl;
}


197
void PCB_BASE_FRAME::SetBoard( BOARD* aBoard )
dickelbeck's avatar
dickelbeck committed
198
{
199
    delete m_Pcb;
dickelbeck's avatar
dickelbeck committed
200 201 202
    m_Pcb = aBoard;
}

203

204 205 206 207
void PCB_BASE_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetPageSettings( aPageSettings );
208 209 210

    if( GetScreen() )
        GetScreen()->InitDataPoints( aPageSettings.GetSizeIU() );
211 212 213 214 215 216 217 218 219 220 221 222 223 224
}


const PAGE_INFO& PCB_BASE_FRAME::GetPageSettings() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetPageSettings();
}


const wxSize PCB_BASE_FRAME::GetPageSizeIU() const
{
    wxASSERT( m_Pcb );

225 226 227 228 229 230 231
    // this function is only needed because EDA_DRAW_FRAME is not compiled
    // with either -DPCBNEW or -DEESCHEMA, so the virtual is used to route
    // into an application specific source file.
    return m_Pcb->GetPageSettings().GetSizeIU();
}


232
const wxPoint& PCB_BASE_FRAME::GetAuxOrigin() const
233 234
{
    wxASSERT( m_Pcb );
235
    return m_Pcb->GetAuxOrigin();
236 237 238
}


239
void PCB_BASE_FRAME::SetAuxOrigin( const wxPoint& aPoint )
240 241
{
    wxASSERT( m_Pcb );
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    m_Pcb->SetAuxOrigin( aPoint );
}


const wxPoint& PCB_BASE_FRAME::GetGridOrigin() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetGridOrigin();
}


void PCB_BASE_FRAME::SetGridOrigin( const wxPoint& aPoint )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetGridOrigin( aPoint );
257 258
}

dickelbeck's avatar
dickelbeck committed
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273
const TITLE_BLOCK& PCB_BASE_FRAME::GetTitleBlock() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetTitleBlock();
}


void PCB_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetTitleBlock( aTitleBlock );
}


274 275 276 277 278 279 280 281 282 283 284 285 286 287
BOARD_DESIGN_SETTINGS& PCB_BASE_FRAME::GetDesignSettings() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetDesignSettings();
}


void PCB_BASE_FRAME::SetDesignSettings( const BOARD_DESIGN_SETTINGS& aSettings )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetDesignSettings( aSettings );
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301
const ZONE_SETTINGS& PCB_BASE_FRAME::GetZoneSettings() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetZoneSettings();
}


void PCB_BASE_FRAME::SetZoneSettings( const ZONE_SETTINGS& aSettings )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetZoneSettings( aSettings );
}


302 303 304 305 306 307 308 309 310 311 312 313 314 315
const PCB_PLOT_PARAMS& PCB_BASE_FRAME::GetPlotSettings() const
{
    wxASSERT( m_Pcb );
    return m_Pcb->GetPlotOptions();
}


void PCB_BASE_FRAME::SetPlotSettings( const PCB_PLOT_PARAMS& aSettings )
{
    wxASSERT( m_Pcb );
    m_Pcb->SetPlotOptions( aSettings );
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
316 317 318 319 320 321 322 323
EDA_RECT PCB_BASE_FRAME::GetBoardBoundingBox( bool aBoardEdgesOnly ) const
{
    wxASSERT( m_Pcb );

    EDA_RECT area = m_Pcb->ComputeBoundingBox( aBoardEdgesOnly );

    if( area.GetWidth() == 0 && area.GetHeight() == 0 )
    {
324 325
        wxSize pageSize = GetPageSizeIU();

326
        if( m_showBorderAndTitleBlock )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
327 328
        {
            area.SetOrigin( 0, 0 );
329
            area.SetEnd( pageSize.x, pageSize.y );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
330 331 332
        }
        else
        {
333 334
            area.SetOrigin( -pageSize.x / 2, -pageSize.y / 2 );
            area.SetEnd( pageSize.x / 2, pageSize.y / 2 );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
335 336 337 338 339 340 341
        }
    }

    return area;
}


342
double PCB_BASE_FRAME::BestZoom()
343
{
344
    if( m_Pcb == NULL )
345
        return 1.0;
346

347 348 349
    EDA_RECT    ibbbox  = GetBoardBoundingBox();
    DSIZE       clientz = m_canvas->GetClientSize();
    DSIZE       boardz( ibbbox.GetWidth(), ibbbox.GetHeight() );
350

351 352
    double iu_per_du_X = clientz.x ? boardz.x / clientz.x : 1.0;
    double iu_per_du_Y = clientz.y ? boardz.y / clientz.y : 1.0;
353

354
    double bestzoom = std::max( iu_per_du_X, iu_per_du_Y );
355

356
    SetScrollCenterPosition( ibbbox.Centre() );
357

358
    return bestzoom;
359 360 361
}


362
void PCB_BASE_FRAME::CursorGoto( const wxPoint& aPos, bool aWarp )
dickelbeck's avatar
dickelbeck committed
363 364 365
{
    // factored out of pcbnew/find.cpp

366
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
367

368
    // There may be need to reframe the drawing.
369
    if( !m_canvas->IsPointOnDisplay( aPos ) )
dickelbeck's avatar
dickelbeck committed
370
    {
371
        SetCrossHairPosition( aPos );
372
        RedrawScreen( aPos, aWarp );
dickelbeck's avatar
dickelbeck committed
373 374 375
    }
    else
    {
376
        // Put cursor on item position
377
        m_canvas->CrossHairOff( &dc );
378
        SetCrossHairPosition( aPos );
379 380 381

        if( aWarp )
            m_canvas->MoveCursorToCrossHair();
dickelbeck's avatar
dickelbeck committed
382
    }
383 384
    m_canvas->CrossHairOn( &dc );
    m_canvas->CrossHairOn( &dc );
dickelbeck's avatar
dickelbeck committed
385 386 387
}


388
// Virtual function
389
void PCB_BASE_FRAME::ReCreateMenuBar( void )
390 391 392 393
{
}


394
// Virtual functions: Do nothing for PCB_BASE_FRAME window
395
void PCB_BASE_FRAME::Show3D_Frame( wxCommandEvent& event )
396
{
plyatov's avatar
plyatov committed
397
}
398 399


400
// Note: virtual, overridden in PCB_EDIT_FRAME;
401
void PCB_BASE_FRAME::SwitchLayer( wxDC* DC, LAYER_NUM layer )
402
{
403
    LAYER_NUM preslayer = ((PCB_SCREEN*)GetScreen())->m_Active_Layer;
404

405 406
    // Check if the specified layer matches the present layer
    if( layer == preslayer )
407
        return;
408

409 410
    // Copper layers cannot be selected unconditionally; how many
    // of those layers are currently enabled needs to be checked.
411
    if( IsCopperLayer( layer ) )
412 413 414 415
    {
        // If only one copper layer is enabled, the only such layer
        // that can be selected to is the "Copper" layer (so the
        // selection of any other copper layer is disregarded).
416
        if( m_Pcb->GetCopperLayerCount() < 2 )
417
        {
dickelbeck's avatar
dickelbeck committed
418
            if( layer != LAYER_N_BACK )
419 420 421 422 423 424 425 426 427 428 429
            {
                return;
            }
        }

        // If more than one copper layer is enabled, the "Copper"
        // and "Component" layers can be selected, but the total
        // number of copper layers determines which internal
        // layers are also capable of being selected.
        else
        {
dickelbeck's avatar
dickelbeck committed
430
            if( ( layer != LAYER_N_BACK ) && ( layer != LAYER_N_FRONT )
431
                && ( layer >= m_Pcb->GetCopperLayerCount() - 1 ) )
432 433 434 435 436 437 438 439 440 441
            {
                return;
            }
        }
    }

    // Is yet more checking required? E.g. when the layer to be selected
    // is a non-copper layer, or when switching between a copper layer
    // and a non-copper layer, or vice-versa?
    // ...
442

charras's avatar
charras committed
443
    GetScreen()->m_Active_Layer = layer;
444 445

    if( DisplayOpt.ContrastModeDisplay )
446
        m_canvas->Refresh();
447 448
}

449

450
void PCB_BASE_FRAME::OnTogglePolarCoords( wxCommandEvent& aEvent )
451 452 453 454 455 456 457
{
    SetStatusText( wxEmptyString );
    DisplayOpt.DisplayPolarCood = !DisplayOpt.DisplayPolarCood;
    UpdateStatusBar();
}


458
void PCB_BASE_FRAME::OnTogglePadDrawMode( wxCommandEvent& aEvent )
459 460
{
    m_DisplayPadFill = DisplayOpt.DisplayPadFill = !m_DisplayPadFill;
461 462

    // Apply new display options to the GAL canvas
463
    KIGFX::PCB_PAINTER* painter =
464
            static_cast<KIGFX::PCB_PAINTER*> ( GetGalCanvas()->GetView()->GetPainter() );
465 466
    KIGFX::PCB_RENDER_SETTINGS* settings =
            static_cast<KIGFX::PCB_RENDER_SETTINGS*> ( painter->GetSettings() );
467
    settings->LoadDisplayOptions( DisplayOpt );
468 469 470 471 472 473 474 475

    // Update pads
    BOARD* board = GetBoard();
    for( MODULE* module = board->m_Modules; module; module = module->Next() )
    {
        for( D_PAD* pad = module->Pads(); pad; pad = pad->Next() )
            pad->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
    }
476

477
    m_canvas->Refresh();
478 479 480
}


481
void PCB_BASE_FRAME::OnUpdateCoordType( wxUpdateUIEvent& aEvent )
482 483
{
    aEvent.Check( DisplayOpt.DisplayPolarCood );
484
    m_optionsToolBar->SetToolShortHelp( ID_TB_OPTIONS_SHOW_POLAR_COORD,
485 486 487 488 489 490
                                        DisplayOpt.DisplayPolarCood ?
                                        _( "Display rectangular coordinates" ) :
                                        _( "Display polar coordinates" ) );
}


491
void PCB_BASE_FRAME::OnUpdatePadDrawMode( wxUpdateUIEvent& aEvent )
492 493
{
    aEvent.Check( !m_DisplayPadFill );
494
    m_optionsToolBar->SetToolShortHelp( ID_TB_OPTIONS_SHOW_PADS_SKETCH,
495 496 497 498 499 500
                                        m_DisplayPadFill ?
                                        _( "Show pads in outline mode" ) :
                                        _( "Show pads in fill mode" ) );
}


501
void PCB_BASE_FRAME::OnUpdateSelectGrid( wxUpdateUIEvent& aEvent )
502 503 504
{
    // No need to update the grid select box if it doesn't exist or the grid setting change
    // was made using the select box.
505
    if( m_gridSelectBox == NULL || m_auxiliaryToolBar == NULL )
506 507 508 509
        return;

    int select = wxNOT_FOUND;

510
    for( size_t i = 0; i < GetScreen()->GetGridCount(); i++ )
511
    {
512
        if( GetScreen()->GetGridId() == GetScreen()->GetGrid( i ).m_Id )
513
        {
514 515
            select = (int) i;
            break;
516 517 518
        }
    }

519 520
    if( select != m_gridSelectBox->GetSelection() )
        m_gridSelectBox->SetSelection( select );
521 522 523
}


524
void PCB_BASE_FRAME::OnUpdateSelectZoom( wxUpdateUIEvent& aEvent )
525
{
526
    if( m_zoomSelectBox == NULL || m_auxiliaryToolBar == NULL )
527 528 529 530
        return;

    int current = 0;

531
    for( unsigned i = 0; i < GetScreen()->m_ZoomList.size(); i++ )
532 533 534 535 536 537 538 539
    {
        if( GetScreen()->GetZoom() == GetScreen()->m_ZoomList[i] )
        {
            current = i + 1;
            break;
        }
    }

540 541
    if( current != m_zoomSelectBox->GetSelection() )
        m_zoomSelectBox->SetSelection( current );
542 543 544
}


545
void PCB_BASE_FRAME::ProcessItemSelection( wxCommandEvent& aEvent )
546
{
547
    int id = aEvent.GetId();
548

549
    // index into the collector list:
550 551
    int itemNdx = id - ID_POPUP_PCB_ITEM_SELECTION_START;

552
    if( id >= ID_POPUP_PCB_ITEM_SELECTION_START && id <= ID_POPUP_PCB_ITEM_SELECTION_END )
553 554
    {
        BOARD_ITEM* item = (*m_Collector)[itemNdx];
555
        m_canvas->SetAbortRequest( false );
556

dickelbeck's avatar
dickelbeck committed
557
#if 0 && defined (DEBUG)
558
        item->Show( 0, std::cout );
559
#endif
560 561 562

        SetCurItem( item );
    }
563 564 565
}


566
void PCB_BASE_FRAME::SetCurItem( BOARD_ITEM* aItem, bool aDisplayInfo )
567
{
568
    GetScreen()->SetCurItem( aItem );
569

570
    if( aItem )
571
    {
572
        if( aDisplayInfo )
573 574 575 576 577
        {
            MSG_PANEL_ITEMS items;
            aItem->GetMsgPanelInfo( items );
            SetMsgPanel( items );
        }
578

579
#if 0 && defined(DEBUG)
580 581
    aItem->Show( 0, std::cout );
#endif
582

583
    }
584 585
    else
    {
586
        // we can use either of these two:
587

588 589 590
        MSG_PANEL_ITEMS items;
        m_Pcb->GetMsgPanelInfo( items );       // show the BOARD stuff
        SetMsgPanel( items );
591

dickelbeck's avatar
dickelbeck committed
592
#if 0 && defined(DEBUG)
593 594 595
        std::cout << "SetCurItem(NULL)\n";
#endif

596 597 598 599
    }
}


600
BOARD_ITEM* PCB_BASE_FRAME::GetCurItem()
601
{
602
    return GetScreen()->GetCurItem();
603 604 605
}


606
GENERAL_COLLECTORS_GUIDE PCB_BASE_FRAME::GetCollectorsGuide()
607
{
608
    GENERAL_COLLECTORS_GUIDE guide( m_Pcb->GetVisibleLayers(),
stambaughw's avatar
stambaughw committed
609
                                    ( (PCB_SCREEN*)GetScreen())->m_Active_Layer );
610 611

    // account for the globals
612 613 614 615 616
    guide.SetIgnoreMTextsMarkedNoShow( ! m_Pcb->IsElementVisible( MOD_TEXT_INVISIBLE ));
    guide.SetIgnoreMTextsOnCopper( ! m_Pcb->IsElementVisible( MOD_TEXT_BK_VISIBLE ));
    guide.SetIgnoreMTextsOnCmp( ! m_Pcb->IsElementVisible( MOD_TEXT_FR_VISIBLE ));
    guide.SetIgnoreModulesOnCu( ! m_Pcb->IsElementVisible( MOD_BK_VISIBLE ) );
    guide.SetIgnoreModulesOnCmp( ! m_Pcb->IsElementVisible( MOD_FR_VISIBLE ) );
617 618
    guide.SetIgnorePadsOnBack( ! m_Pcb->IsElementVisible( PAD_BK_VISIBLE ) );
    guide.SetIgnorePadsOnFront( ! m_Pcb->IsElementVisible( PAD_FR_VISIBLE ) );
Guillaume Simard's avatar
Guillaume Simard committed
619 620
    guide.SetIgnoreModulesVals( ! m_Pcb->IsElementVisible( MOD_VALUES_VISIBLE ) );
    guide.SetIgnoreModulesRefs( ! m_Pcb->IsElementVisible( MOD_REFERENCES_VISIBLE ) );
621 622 623

    return guide;
}
624

625
void PCB_BASE_FRAME::SetToolID( int aId, int aCursor, const wxString& aToolMsg )
626 627 628
{
    bool redraw = false;

629
    EDA_DRAW_FRAME::SetToolID( aId, aCursor, aToolMsg );
630

631
    if( aId < 0 )
632 633 634
        return;

    // handle color changes for transitions in and out of ID_TRACK_BUTT
635
    if( ( GetToolId() == ID_TRACK_BUTT && aId != ID_TRACK_BUTT )
636
        || ( GetToolId() != ID_TRACK_BUTT && aId == ID_TRACK_BUTT ) )
637 638 639 640 641 642 643
    {
        if( DisplayOpt.ContrastModeDisplay )
            redraw = true;
    }

    // must do this after the tool has been set, otherwise pad::Draw() does
    // not show proper color when DisplayOpt.ContrastModeDisplay is true.
644
    if( redraw && m_canvas )
645
        m_canvas->Refresh();
646 647
}

stambaughw's avatar
stambaughw committed
648

649 650 651
/*
 * Update the status bar information.
 */
652
void PCB_BASE_FRAME::UpdateStatusBar()
653
{
654
    EDA_DRAW_FRAME::UpdateStatusBar();
655

656 657 658 659
    PCB_SCREEN* screen = GetScreen();

    if( !screen )
        return;
660

661 662 663 664 665 666
    int dx;
    int dy;
    double dXpos;
    double dYpos;
    wxString line;
    wxString locformatter;
667

668 669
    if( DisplayOpt.DisplayPolarCood )  // display polar coordinates
    {
670
        double       theta, ro;
671

672 673
        dx = GetCrossHairPosition().x - screen->m_O_Curseur.x;
        dy = GetCrossHairPosition().y - screen->m_O_Curseur.y;
674

675
        theta = ArcTangente( -dy, dx ) / 10;
676

677
        ro = hypot( dx, dy );
678 679 680
        wxString formatter;
        switch( g_UserUnit )
        {
681 682 683 684 685 686 687 688 689
#if defined( USE_PCBNEW_NANOMETRE )
        case INCHES:
            formatter = wxT( "Ro %.6f Th %.1f" );
            break;

        case MILLIMETRES:
            formatter = wxT( "Ro %.6f Th %.1f" );
            break;
#else
690 691 692 693 694 695 696
        case INCHES:
            formatter = wxT( "Ro %.4f Th %.1f" );
            break;

        case MILLIMETRES:
            formatter = wxT( "Ro %.3f Th %.1f" );
            break;
697
#endif
698 699 700 701 702 703

        case UNSCALED_UNITS:
            formatter = wxT( "Ro %f Th %f" );
            break;
        }

704
        line.Printf( formatter, To_User_Unit( g_UserUnit, ro ), theta );
705

706
        SetStatusText( line, 3 );
707
    }
708

709
    // Display absolute coordinates:
710 711
    dXpos = To_User_Unit( g_UserUnit, GetCrossHairPosition().x );
    dYpos = To_User_Unit( g_UserUnit, GetCrossHairPosition().y );
712

713 714
    // The following sadly is an if Eeschema/if Pcbnew
    wxString absformatter;
715

716 717
    switch( g_UserUnit )
    {
718 719 720 721 722 723 724 725 726
    case INCHES:
        absformatter = wxT( "X %.6f  Y %.6f" );
        locformatter = wxT( "dx %.6f  dy %.6f  d %.6f" );
        break;

    case MILLIMETRES:
        absformatter = wxT( "X %.6f  Y %.6f" );
        locformatter = wxT( "dx %.6f  dy %.6f  d %.6f" );
        break;
727 728 729 730 731

    case UNSCALED_UNITS:
        absformatter = wxT( "X %f  Y %f" );
        locformatter = wxT( "dx %f  dy %f  d %f" );
        break;
732 733
    }

734 735
    line.Printf( absformatter, dXpos, dYpos );
    SetStatusText( line, 2 );
736

737
    if( !DisplayOpt.DisplayPolarCood )  // display relative cartesian coordinates
738
    {
739
        // Display relative coordinates:
740 741
        dx = GetCrossHairPosition().x - screen->m_O_Curseur.x;
        dy = GetCrossHairPosition().y - screen->m_O_Curseur.y;
742 743
        dXpos = To_User_Unit( g_UserUnit, dx );
        dYpos = To_User_Unit( g_UserUnit, dy );
744

745
        // We already decided the formatter above
746
        line.Printf( locformatter, dXpos, dYpos, hypot( dXpos, dYpos ) );
747 748
        SetStatusText( line, 3 );
    }
749
}
750 751


752 753 754 755 756 757 758 759
void PCB_BASE_FRAME::unitsChangeRefresh()
{
    EDA_DRAW_FRAME::unitsChangeRefresh();    // Update the status bar.

    updateGridSelectBox();
}


760
void PCB_BASE_FRAME::LoadSettings( wxConfigBase* aCfg )
761
{
762
    EDA_DRAW_FRAME::LoadSettings( aCfg );
763

stambaughw's avatar
stambaughw committed
764
    // Ensure grid id is an existent grid id:
765 766 767 768
    if( (m_LastGridSizeId <= 0) ||
        (m_LastGridSizeId > (ID_POPUP_GRID_USER - ID_POPUP_GRID_LEVEL_1000)) )
        m_LastGridSizeId = ID_POPUP_GRID_LEVEL_500 - ID_POPUP_GRID_LEVEL_1000;

769 770
    aCfg->Read( m_FrameName + UserGridSizeXEntry, &m_UserGridSize.x, 0.01 );
    aCfg->Read( m_FrameName + UserGridSizeYEntry, &m_UserGridSize.y, 0.01 );
771 772

    long itmp;
773
    aCfg->Read( m_FrameName + UserGridUnitsEntry, &itmp, ( long )INCHES );
774
    m_UserGridUnit = (EDA_UNITS_T) itmp;
775 776 777 778
    aCfg->Read( m_FrameName + DisplayPadFillEntry, &m_DisplayPadFill, true );
    aCfg->Read( m_FrameName + DisplayViaFillEntry, &m_DisplayViaFill, true );
    aCfg->Read( m_FrameName + DisplayPadNumberEntry, &m_DisplayPadNum, true );
    aCfg->Read( m_FrameName + DisplayModuleEdgeEntry, &m_DisplayModEdge, ( long )FILLED );
779

780
    aCfg->Read( m_FrameName + FastGrid1Entry, &itmp, ( long )0);
Roman Bashkov's avatar
Roman Bashkov committed
781
    m_FastGrid1 = itmp;
782
    aCfg->Read( m_FrameName + FastGrid2Entry, &itmp, ( long )0);
Roman Bashkov's avatar
Roman Bashkov committed
783 784
    m_FastGrid2 = itmp;

785
    if( m_DisplayModEdge < LINE || m_DisplayModEdge > SKETCH )
786
        m_DisplayModEdge = FILLED;
787

788
    aCfg->Read( m_FrameName + DisplayModuleTextEntry, &m_DisplayModText, ( long )FILLED );
789

790
    if( m_DisplayModText < LINE || m_DisplayModText > SKETCH )
791
        m_DisplayModText = FILLED;
792

793
    // Apply display settings for GAL
794
    KIGFX::VIEW* view = GetGalCanvas()->GetView();
795 796

    // Set rendering order and properties of layers
797
    for( LAYER_NUM i = 0; (unsigned) i < sizeof(GAL_LAYER_ORDER) / sizeof(LAYER_NUM); ++i )
798
    {
799
        LAYER_NUM layer = GAL_LAYER_ORDER[i];
800
        wxASSERT( layer < KIGFX::VIEW::VIEW_MAX_LAYERS );
801

802
        view->SetLayerOrder( layer, i );
803

804
        if( IsCopperLayer( layer ) )
805 806
        {
            // Copper layers are required for netname layers
807
            view->SetRequired( GetNetnameLayer( layer ), layer );
808
            view->SetLayerTarget( layer, KIGFX::TARGET_CACHED );
809
        }
810
        else if( IsNetnameLayer( layer ) )
811 812 813
        {
            // Netnames are drawn only when scale is sufficient (level of details)
            // so there is no point in caching them
814
            view->SetLayerTarget( layer, KIGFX::TARGET_NONCACHED );
815
        }
816 817
    }

818 819 820
    // Some more required layers settings
    view->SetRequired( ITEM_GAL_LAYER( VIAS_HOLES_VISIBLE ), ITEM_GAL_LAYER( VIAS_VISIBLE ) );
    view->SetRequired( ITEM_GAL_LAYER( PADS_HOLES_VISIBLE ), ITEM_GAL_LAYER( PADS_VISIBLE ) );
821
    view->SetRequired( NETNAMES_GAL_LAYER( PADS_NETNAMES_VISIBLE ), ITEM_GAL_LAYER( PADS_VISIBLE ) );
822

823
    view->SetRequired( NETNAMES_GAL_LAYER( PAD_FR_NETNAMES_VISIBLE ), ITEM_GAL_LAYER( PAD_FR_VISIBLE ) );
824 825 826 827
    view->SetRequired( ADHESIVE_N_FRONT, ITEM_GAL_LAYER( PAD_FR_VISIBLE ) );
    view->SetRequired( SOLDERPASTE_N_FRONT, ITEM_GAL_LAYER( PAD_FR_VISIBLE ) );
    view->SetRequired( SOLDERMASK_N_FRONT, ITEM_GAL_LAYER( PAD_FR_VISIBLE ) );

828
    view->SetRequired( NETNAMES_GAL_LAYER( PAD_BK_NETNAMES_VISIBLE ), ITEM_GAL_LAYER( PAD_BK_VISIBLE ) );
829 830 831 832
    view->SetRequired( ADHESIVE_N_BACK, ITEM_GAL_LAYER( PAD_BK_VISIBLE ) );
    view->SetRequired( SOLDERPASTE_N_BACK, ITEM_GAL_LAYER( PAD_BK_VISIBLE ) );
    view->SetRequired( SOLDERMASK_N_BACK, ITEM_GAL_LAYER( PAD_BK_VISIBLE ) );

833 834 835
    view->SetRequired( ITEM_GAL_LAYER( PAD_FR_VISIBLE ), ITEM_GAL_LAYER( MOD_FR_VISIBLE ) );
    view->SetRequired( ITEM_GAL_LAYER( PAD_BK_VISIBLE ), ITEM_GAL_LAYER( MOD_BK_VISIBLE ) );

836
    view->SetLayerTarget( ITEM_GAL_LAYER( GP_OVERLAY ), KIGFX::TARGET_OVERLAY );
Maciej Suminski's avatar
Maciej Suminski committed
837
    view->SetLayerTarget( ITEM_GAL_LAYER( RATSNEST_VISIBLE ), KIGFX::TARGET_OVERLAY );
838

839 840 841
    // Apply layer coloring scheme & display options
    if( view->GetPainter() )
    {
842
        KIGFX::PCB_RENDER_SETTINGS* settings = new KIGFX::PCB_RENDER_SETTINGS();
843 844 845 846 847 848 849 850 851

        // Load layers' colors from PCB data
        settings->ImportLegacyColors( m_Pcb->GetColorsSettings() );
        view->GetPainter()->ApplySettings( settings );

        // Load display options (such as filled/outline display of items)
        settings->LoadDisplayOptions( DisplayOpt );
    }

852 853 854 855
    // WxWidgets 2.9.1 seems call setlocale( LC_NUMERIC, "" )
    // when reading doubles in config,
    // but forget to back to current locale. So we call SetLocaleTo_Default
    SetLocaleTo_Default( );
856 857 858
}


859
void PCB_BASE_FRAME::SaveSettings( wxConfigBase* aCfg )
860
{
861
    EDA_DRAW_FRAME::SaveSettings( aCfg );
862

863 864 865 866 867 868 869 870 871 872
    aCfg->Write( m_FrameName + UserGridSizeXEntry, m_UserGridSize.x );
    aCfg->Write( m_FrameName + UserGridSizeYEntry, m_UserGridSize.y );
    aCfg->Write( m_FrameName + UserGridUnitsEntry, ( long )m_UserGridUnit );
    aCfg->Write( m_FrameName + DisplayPadFillEntry, m_DisplayPadFill );
    aCfg->Write( m_FrameName + DisplayViaFillEntry, m_DisplayViaFill );
    aCfg->Write( m_FrameName + DisplayPadNumberEntry, m_DisplayPadNum );
    aCfg->Write( m_FrameName + DisplayModuleEdgeEntry, ( long )m_DisplayModEdge );
    aCfg->Write( m_FrameName + DisplayModuleTextEntry, ( long )m_DisplayModText );
    aCfg->Write( m_FrameName + FastGrid1Entry, ( long )m_FastGrid1 );
    aCfg->Write( m_FrameName + FastGrid2Entry, ( long )m_FastGrid2 );
873
}
874 875


876
void PCB_BASE_FRAME::OnModify()
877
{
878 879
    GetScreen()->SetModify();
    GetScreen()->SetSave();
880
}
881 882


883
void PCB_BASE_FRAME::updateGridSelectBox()
884 885 886 887
{
    UpdateStatusBar();
    DisplayUnitsMsg();

888
    if( m_gridSelectBox == NULL )
889 890 891
        return;

    // Update grid values with the current units setting.
892
    m_gridSelectBox->Clear();
893 894

    wxString msg;
895
    wxString format = _( "Grid:");
896 897 898

    switch( g_UserUnit )
    {
899
    case INCHES:    // the grid size is displayed in mils
900
    case MILLIMETRES:
901
        format += wxT( " %.6f" );
902 903 904 905 906 907 908 909 910 911
        break;

    case UNSCALED_UNITS:
        format += wxT( " %f" );
        break;
    }

    for( size_t i = 0; i < GetScreen()->GetGridCount(); i++ )
    {
        GRID_TYPE& grid = GetScreen()->GetGrid( i );
912
        double value = To_User_Unit( g_UserUnit, grid.m_Size.x );
913 914
        if( g_UserUnit == INCHES )
            value *= 1000;
915 916 917

        if( grid.m_Id != ID_POPUP_GRID_USER )
        {
918 919
            msg.Printf( format.GetData(), value );
            StripTrailingZeros( msg );
920 921 922 923
        }
        else
            msg = _( "User Grid" );

924
        m_gridSelectBox->Append( msg, (void*) &grid.m_Id );
925 926

        if( ( m_LastGridSizeId + ID_POPUP_GRID_LEVEL_1000 ) == GetScreen()->GetGrid( i ).m_Id )
927
            m_gridSelectBox->SetSelection( i );
928 929 930
    }
}

931
void PCB_BASE_FRAME::updateZoomSelectBox()
932
{
933
    if( m_zoomSelectBox == NULL )
934 935 936 937
        return;

    wxString msg;

938 939 940
    m_zoomSelectBox->Clear();
    m_zoomSelectBox->Append( _( "Auto" ) );
    m_zoomSelectBox->SetSelection( 0 );
941

942
    for( unsigned i = 0;  i < GetScreen()->m_ZoomList.size();  ++i )
943 944 945
    {
        msg = _( "Zoom " );

946 947 948 949 950 951
        wxString value = wxString::Format( wxT( "%g" ),

                                // @todo could do scaling here and show a "percentage"
                                GetScreen()->m_ZoomList[i]
                                );

952
        msg += value;
953

954
        m_zoomSelectBox->Append( msg );
955 956

        if( GetScreen()->GetZoom() == GetScreen()->m_ZoomList[i] )
957
            m_zoomSelectBox->SetSelection( i + 1 );
958 959
    }
}
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985


void PCB_BASE_FRAME::SetFastGrid1()
{
    if( m_gridSelectBox )
    {
        m_gridSelectBox->SetSelection( m_FastGrid1 );

        wxCommandEvent cmd( wxEVT_COMMAND_COMBOBOX_SELECTED );
        cmd.SetEventObject( this );
        OnSelectGrid( cmd );
    }
}


void PCB_BASE_FRAME::SetFastGrid2()
{
    if( m_gridSelectBox )
    {
        m_gridSelectBox->SetSelection( m_FastGrid2 );

        wxCommandEvent cmd( wxEVT_COMMAND_COMBOBOX_SELECTED );
        cmd.SetEventObject( this );
        OnSelectGrid( cmd );
    }
}