dialog_select_one_pcb_layer.cpp 7.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/**
 * @file select_pcb_layer.cpp
 * @brief Set up a dialog to choose a PCB Layer.
 */

#include <fctsys.h>
#include <gerbview_frame.h>
#include <select_layers_to_pcb.h>

// Exported function
const wxString GetPCBDefaultLayerName( int aLayerNumber );


enum layer_sel_id {
    ID_LAYER_SELECT_TOP = 1800,
    ID_LAYER_SELECT_BOTTOM,
    ID_LAYER_SELECT
};


class SELECT_LAYER_DIALOG : public wxDialog
{
private:
    GERBVIEW_FRAME* m_Parent;
    wxRadioBox*     m_LayerList;
    int m_LayerId[NB_LAYERS + 1]; // One extra element for "(Deselect)" radiobutton

public:
    // Constructor and destructor
    SELECT_LAYER_DIALOG( GERBVIEW_FRAME* parent, int aDefaultLayer,
                         int aCopperLayerCount, bool aShowDeselectOption );
    ~SELECT_LAYER_DIALOG() { };

private:
    void OnLayerSelected( wxCommandEvent& event );
    void OnCancelClick( wxCommandEvent& event );

    DECLARE_EVENT_TABLE()
};


BEGIN_EVENT_TABLE( SELECT_LAYER_DIALOG, wxDialog )
    EVT_BUTTON( wxID_OK, SELECT_LAYER_DIALOG::OnLayerSelected )
    EVT_BUTTON( wxID_CANCEL, SELECT_LAYER_DIALOG::OnCancelClick )
    EVT_RADIOBOX( ID_LAYER_SELECT, SELECT_LAYER_DIALOG::OnLayerSelected )
END_EVENT_TABLE()


/** Install the dialog box for layer selection
 * @param aDefaultLayer = Preselection (NB_LAYERS for "(Deselect)" layer)
 * @param aCopperLayerCount = number of copper layers
 * @param aShowDeselectOption = display a "(Deselect)" radiobutton (when set to true)
 * @return new layer value (NB_LAYERS when "(Deselect)" radiobutton selected),
 *                         or -1 if canceled
 *
 * Providing the option to also display a "(Deselect)" radiobutton makes the
 *  GerbView's "Export to Pcbnew" command) more "user friendly",
 * by permitting any layer to be "deselected" immediately after its
 * corresponding radiobutton has been clicked on. (It would otherwise be
 * necessary to first cancel the "Select Layer:" dialog box (invoked after a
 * different radiobutton is clicked on) prior to then clicking on the "Deselect"
 * button provided within the "Layer selection:" dialog box).
 */
int GERBVIEW_FRAME::SelectPCBLayer( int aDefaultLayer, int aCopperLayerCount, bool aShowDeselectOption )
{
    int layer;
    SELECT_LAYER_DIALOG* frame = new SELECT_LAYER_DIALOG( this, aDefaultLayer,
                                                          aCopperLayerCount,
                                                          aShowDeselectOption );

    layer = frame->ShowModal();
    frame->Destroy();
    return layer;
}


/*
 * The "OK" and "Cancel" buttons are positioned (in a horizontal line)
 * beneath the "Layer" radiobox, unless that contains only one column of
 * radiobuttons, in which case they are positioned (in a vertical line)
 * to the right of that radiobox.
 */
SELECT_LAYER_DIALOG::SELECT_LAYER_DIALOG( GERBVIEW_FRAME* parent,
                                          int aDefaultLayer, int aCopperLayerCount,
                                          bool aShowDeselectOption ) :
    wxDialog( parent, -1, _( "Select Layer:" ), wxPoint( -1, -1 ),
              wxSize( 470, 250 ),
              DIALOG_STYLE )
{
    wxButton* Button;
    int       ii;
    wxString  LayerList[NB_LAYERS + 1]; // One extra element for "(Deselect)"
                                        // radiobutton
    int       LayerCount, LayerSelect = -1;

    m_Parent = parent;

    // Build the layer list; first build copper layers list
    LayerCount = 0;
    for( ii = 0; ii < BOARD_COPPER_LAYERS_MAX_COUNT; ii++ )
    {
        m_LayerId[ii] = 0;

        if( ii == 0 || ii == BOARD_COPPER_LAYERS_MAX_COUNT-1 || ii < aCopperLayerCount-1 )
        {
            LayerList[LayerCount] = GetPCBDefaultLayerName( ii );

            if( ii == aDefaultLayer )
                LayerSelect = LayerCount;

            m_LayerId[LayerCount] = ii;
            LayerCount++;
        }
    }
    // Build the layer list; build copper layers list
    for( ; ii < NB_LAYERS; ii++ )
    {
        m_LayerId[ii] = 0;

        LayerList[LayerCount] = GetPCBDefaultLayerName( ii );

        if( ii == aDefaultLayer )
            LayerSelect = LayerCount;

        m_LayerId[LayerCount] = ii;
        LayerCount++;
    }

    // When appropriate, also provide a "(Deselect)" radiobutton
    if( aShowDeselectOption )
    {
        LayerList[LayerCount] = _( "(Deselect)" );

        if( NB_LAYERS == aDefaultLayer )
            LayerSelect = LayerCount;

        m_LayerId[LayerCount] = NB_LAYERS;
        LayerCount++;
    }

    m_LayerList = new wxRadioBox( this, ID_LAYER_SELECT, _( "Layer" ),
                                  wxPoint( -1, -1 ), wxSize( -1, -1 ),
                                  LayerCount, LayerList,
                                  (LayerCount < 8) ? LayerCount : 8,
                                  wxRA_SPECIFY_ROWS );

    if( LayerSelect >= 0 )
        m_LayerList->SetSelection( LayerSelect );

    wxBoxSizer* FrameBoxSizer = new wxBoxSizer( wxHORIZONTAL );
    SetSizer( FrameBoxSizer );
    FrameBoxSizer->Add( m_LayerList, 0, wxALIGN_TOP | wxALL, 5 );
    wxBoxSizer* ButtonBoxSizer = new wxBoxSizer( wxVERTICAL );
    FrameBoxSizer->Add( ButtonBoxSizer, 0, wxALIGN_BOTTOM | wxALL, 0 );

    Button = new wxButton( this, wxID_OK, _( "OK" ) );
    Button->SetDefault();
    ButtonBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );

    Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
    ButtonBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );

    SetFocus();

    GetSizer()->SetSizeHints( this );

    Center();
}


void SELECT_LAYER_DIALOG::OnLayerSelected( wxCommandEvent& event )
{
    int ii = m_LayerId[m_LayerList->GetSelection()];

    EndModal( ii );
}


void SELECT_LAYER_DIALOG::OnCancelClick( wxCommandEvent& event )
{
    EndModal( -1 );
}

const wxString GetPCBDefaultLayerName( int aLayerNumber )
{
    const wxChar* txt;

    // Use a switch to explicitly show the mapping more clearly
    switch( aLayerNumber )
    {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        case LAYER_N_FRONT:         txt = _( "Front" );         break;
        case LAYER_N_2:             txt = _( "Inner1" );        break;
        case LAYER_N_3:             txt = _( "Inner2" );        break;
        case LAYER_N_4:             txt = _( "Inner3" );        break;
        case LAYER_N_5:             txt = _( "Inner4" );        break;
        case LAYER_N_6:             txt = _( "Inner5" );        break;
        case LAYER_N_7:             txt = _( "Inner6" );        break;
        case LAYER_N_8:             txt = _( "Inner7" );        break;
        case LAYER_N_9:             txt = _( "Inner8" );        break;
        case LAYER_N_10:            txt = _( "Inner9" );        break;
        case LAYER_N_11:            txt = _( "Inner10" );       break;
        case LAYER_N_12:            txt = _( "Inner11" );       break;
        case LAYER_N_13:            txt = _( "Inner12" );       break;
        case LAYER_N_14:            txt = _( "Inner13" );       break;
        case LAYER_N_15:            txt = _( "Inner14" );       break;
        case LAYER_N_BACK:          txt = _( "Back" );          break;
        case ADHESIVE_N_BACK:       txt = _( "Adhes_Back" );    break;
        case ADHESIVE_N_FRONT:      txt = _( "Adhes_Front" );   break;
        case SOLDERPASTE_N_BACK:    txt = _( "SoldP_Back" );    break;
        case SOLDERPASTE_N_FRONT:   txt = _( "SoldP_Front" );   break;
        case SILKSCREEN_N_BACK:     txt = _( "SilkS_Back" );    break;
        case SILKSCREEN_N_FRONT:    txt = _( "SilkS_Front" );   break;
        case SOLDERMASK_N_BACK:     txt = _( "Mask_Back" );     break;
        case SOLDERMASK_N_FRONT:    txt = _( "Mask_Front" );    break;
        case DRAW_N:                txt = _( "Drawings" );      break;
        case COMMENT_N:             txt = _( "Comments" );      break;
        case ECO1_N:                txt = _( "Eco1" );          break;
        case ECO2_N:                txt = _( "Eco2" );          break;
        case EDGE_N:                txt = _( "PCB_Edges" );     break;
        default:                    txt = wxT( "BAD INDEX" );   break;
221 222 223 224 225 226
    }

    return wxString( txt );
}