Commit 45d0448b authored by Wayne Stambaugh's avatar Wayne Stambaugh
Browse files

Add path (environment variable) configuration dialog.

* Create new dialog to edit environment variables.
* Add helper class ENV_VAR_ITEM to manage environment variable values and
  if they were defined externally.
* A path configuration dialog access to KiCad, CvPcb, Pcbnew, and Footprint Editor
  window menus.
* Add validator for environment variables.
parent 34dfbc33
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -122,6 +122,8 @@ set( COMMON_ABOUT_DLG_SRCS
    dialogs/dialog_hotkeys_editor_base.cpp
    dialogs/dialog_list_selector_base.cpp
    dialogs/dialog_page_settings_base.cpp
    dialogs/dialog_env_var_config_base.cpp
    dialogs/dialog_env_var_config.cpp
    )

set( COMMON_PAGE_LAYOUT_SRCS
+233 −0
Original line number Diff line number Diff line
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2015 Wayne Stambaugh <stambaughw@gmail.com>
 * Copyright (C) 2015 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 dialg_env_var_config.cpp
 */

#include <dialog_env_var_config.h>

#include <validators.h>


DIALOG_ENV_VAR_CONFIG::DIALOG_ENV_VAR_CONFIG( wxWindow* aParent, const ENV_VAR_MAP& aEnvVarMap ) :
    DIALOG_ENV_VAR_CONFIG_BASE( aParent )
{
    m_extDefsChanged = false;
    m_envVarMap = aEnvVarMap;

    m_grid->AppendRows( (int) m_envVarMap.size() );

    for( size_t row = 0;  row < m_envVarMap.size();  row++ )
    {
        wxGridCellTextEditor* editor = new wxGridCellTextEditor;
        ENVIRONMENT_VARIABLE_CHAR_VALIDATOR envVarValidator;
        editor->SetValidator( envVarValidator );
        m_grid->SetCellEditor( (int) row, 0, editor );

        editor = new wxGridCellTextEditor;
        FILE_NAME_WITH_PATH_CHAR_VALIDATOR pathValidator;
        editor->SetValidator( pathValidator );
        m_grid->SetCellEditor( (int) row, 1, editor );
    }

    wxButton* okButton = (wxButton*) FindWindowById( wxID_OK );

    if( okButton )
        SetDefaultItem( okButton );

    wxLogDebug( wxT( "In DIALOG_ENV_VAR_CONFIG ctor." ) );
}


bool DIALOG_ENV_VAR_CONFIG::TransferDataToWindow()
{
    wxLogDebug( wxT( "In DIALOG_ENV_VAR_CONFIG::TransferDataToWindow()." ) );

    if( !wxDialog::TransferDataToWindow() )
        return false;

    long row = 0L;

    for( ENV_VAR_MAP_ITER it = m_envVarMap.begin(); it != m_envVarMap.end(); ++it )
    {
        m_grid->SetCellValue( row, 0, it->first );
        m_grid->SetCellValue( row, 1, it->second.GetValue() );

        // Highlight environment variables that are externally defined.
        if( it->second.GetDefinedExternally() )
        {
            wxGridCellAttr* attr = m_grid->GetOrCreateCellAttr( row, 0 );
            attr->SetBackgroundColour( *wxLIGHT_GREY );
            m_grid->SetRowAttr( row, attr );
        }

        row++;
    }

    m_grid->AutoSizeColumns();
    m_grid->AutoSizeRows();
    GetSizer()->Layout();
    GetSizer()->Fit( this );
    GetSizer()->SetSizeHints( this );

    return true;
}


bool DIALOG_ENV_VAR_CONFIG::TransferDataFromWindow()
{
    wxString nums( wxT( "0123456789" ) );

    if( !wxDialog::TransferDataFromWindow() )
        return false;

    int row;
    wxArrayString envVarNames;

    for( row = 0; row < m_grid->GetNumberRows(); row++ )
    {
        wxString caption = _( "Invalid Input" );
        wxString name = m_grid->GetCellValue( row, 0 );
        wxString value = m_grid->GetCellValue( row, 1 );

        // Ignore completely empty rows.
        if( name.IsEmpty() && value.IsEmpty() )
            continue;

        // Check for empty cells.
        if( name.IsEmpty() )
        {
            wxMessageBox( _( "Path configuration name cannot be empty." ), caption,
                          wxOK | wxICON_ERROR, this );
            return false;
        }
        if( value.IsEmpty() )
        {
            wxMessageBox( _( "Path configuration value cannot be empty." ), caption,
                          wxOK | wxICON_ERROR, this );
            return false;
        }

        // First character of environment variable name cannot be a number.
        if( nums.Find( name[0] ) != wxNOT_FOUND )
        {
            wxMessageBox( _( "Path configuration names cannot have a number as the first "
                             "character." ), caption, wxOK | wxICON_ERROR, this );
            return false;
        }

        // Check for duplicate environment variable names.
        if( envVarNames.Index( name ) != wxNOT_FOUND )
        {
            wxMessageBox( _( "Cannot have duplicate configuration names." ), caption,
                          wxOK | wxICON_ERROR, this );
            return false;
        }

        envVarNames.Add( name );
    }

    // Add new entries and update any modified entries..
    for( row = 0; row < m_grid->GetNumberRows(); row++ )
    {
        wxString name = m_grid->GetCellValue( row, 0 );
        wxString value = m_grid->GetCellValue( row, 1 );
        ENV_VAR_MAP_ITER it = m_envVarMap.find( name );

        if( it == m_envVarMap.end() )
        {
            ENV_VAR_ITEM item( value, wxGetEnv( name, NULL ) );

            // Add new envrionment variable.
            m_envVarMap[ name ] = item;
        }
        else if( it->second.GetValue() != value )
        {
            // Environment variable already defined but it's value changed.
            it->second.SetValue( value );

            // Externally defined variable has been changed.
            if( it->second.GetDefinedExternally() )
                m_extDefsChanged = true;
        }
    }

    // Remove deleted entries from the map.
    for( ENV_VAR_MAP_ITER it = m_envVarMap.begin(); it != m_envVarMap.end(); ++it )
    {
        bool found = false;

        for( row = 0; row < m_grid->GetNumberRows(); row++ )
        {
            if( m_grid->GetCellValue( row, 0 ) == it->first )
            {
                found = true;
                break;
            }
        }

        if( !found )
        {
            m_envVarMap.erase( it );
            it--;
        }
    }

    wxLogDebug( wxT( "In DIALOG_ENV_VAR_CONFIG::TransferDataFromWindow()." ) );
    return true;
}


void DIALOG_ENV_VAR_CONFIG::OnAddRow( wxCommandEvent& aEvent )
{
    m_grid->AppendRows();

    int row = m_grid->GetNumberRows() - 1;
    wxGridCellTextEditor* editor = new wxGridCellTextEditor;
    ENVIRONMENT_VARIABLE_CHAR_VALIDATOR envVarValidator;
    editor->SetValidator( envVarValidator );
    m_grid->SetCellEditor( row, 0, editor );
    editor = new wxGridCellTextEditor;
    FILE_NAME_WITH_PATH_CHAR_VALIDATOR pathValidator;
    editor->SetValidator( pathValidator );
    m_grid->SetCellEditor( row, 1, editor );
}


void DIALOG_ENV_VAR_CONFIG::OnDeleteSelectedRows( wxCommandEvent& aEvent )
{
    if( !m_grid->IsSelection() )
        return;

    wxGridUpdateLocker locker( m_grid );

    for( int n = 0; n < m_grid->GetNumberRows(); )
    {
        if( m_grid->IsInSelection( n , 0 ) )
            m_grid->DeleteRows( n, 1 );
        else
            n++;
    }
}
+96 −0
Original line number Diff line number Diff line
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Jun  5 2014)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#include "dialog_env_var_config_base.h"

///////////////////////////////////////////////////////////////////////////

DIALOG_ENV_VAR_CONFIG_BASE::DIALOG_ENV_VAR_CONFIG_BASE( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : DIALOG_SHIM( parent, id, title, pos, size, style )
{
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
	
	wxBoxSizer* mainSizer;
	mainSizer = new wxBoxSizer( wxHORIZONTAL );
	
	wxBoxSizer* bSizer1;
	bSizer1 = new wxBoxSizer( wxVERTICAL );
	
	m_grid = new wxGrid( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
	
	// Grid
	m_grid->CreateGrid( 0, 2 );
	m_grid->EnableEditing( true );
	m_grid->EnableGridLines( true );
	m_grid->EnableDragGridSize( true );
	m_grid->SetMargins( 0, 0 );
	
	// Columns
	m_grid->EnableDragColMove( false );
	m_grid->EnableDragColSize( true );
	m_grid->SetColLabelSize( 30 );
	m_grid->SetColLabelValue( 0, _("Name") );
	m_grid->SetColLabelValue( 1, _("Path") );
	m_grid->SetColLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
	
	// Rows
	m_grid->EnableDragRowSize( true );
	m_grid->SetRowLabelSize( 40 );
	m_grid->SetRowLabelAlignment( wxALIGN_CENTRE, wxALIGN_CENTRE );
	
	// Label Appearance
	
	// Cell Defaults
	m_grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_TOP );
	m_grid->SetToolTip( _("Enter the names and paths for each path.\n\nGrey enteries are names that have been defined externally as system or user level environment variables.") );
	
	bSizer1->Add( m_grid, 1, wxALL|wxEXPAND, 5 );
	
	
	mainSizer->Add( bSizer1, 1, wxEXPAND, 5 );
	
	wxBoxSizer* bSizer2;
	bSizer2 = new wxBoxSizer( wxVERTICAL );
	
	m_buttonOk = new wxButton( this, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0 );
	m_buttonOk->SetDefault(); 
	bSizer2->Add( m_buttonOk, 0, wxALL, 5 );
	
	m_buttonCancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizer2->Add( m_buttonCancel, 0, wxBOTTOM|wxLEFT|wxRIGHT, 5 );
	
	m_buttonAdd = new wxButton( this, wxID_ANY, _("Add"), wxDefaultPosition, wxDefaultSize, 0 );
	m_buttonAdd->SetToolTip( _("Add a new entry to the table.") );
	
	bSizer2->Add( m_buttonAdd, 0, wxBOTTOM|wxLEFT|wxRIGHT, 5 );
	
	m_buttonDelete = new wxButton( this, wxID_ANY, _("Delete"), wxDefaultPosition, wxDefaultSize, 0 );
	m_buttonDelete->SetToolTip( _("Remove the selectect entry from the table.") );
	
	bSizer2->Add( m_buttonDelete, 0, wxBOTTOM|wxLEFT|wxRIGHT, 5 );
	
	
	mainSizer->Add( bSizer2, 0, wxEXPAND, 5 );
	
	
	this->SetSizer( mainSizer );
	this->Layout();
	mainSizer->Fit( this );
	
	this->Centre( wxBOTH );
	
	// Connect Events
	m_buttonAdd->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_ENV_VAR_CONFIG_BASE::OnAddRow ), NULL, this );
	m_buttonDelete->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_ENV_VAR_CONFIG_BASE::OnDeleteSelectedRows ), NULL, this );
}

DIALOG_ENV_VAR_CONFIG_BASE::~DIALOG_ENV_VAR_CONFIG_BASE()
{
	// Disconnect Events
	m_buttonAdd->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_ENV_VAR_CONFIG_BASE::OnAddRow ), NULL, this );
	m_buttonDelete->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_ENV_VAR_CONFIG_BASE::OnDeleteSelectedRows ), NULL, this );
	
}
+616 −0

File added.

Preview size limit exceeded, changes collapsed.

+56 −0
Original line number Diff line number Diff line
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Jun  5 2014)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#ifndef __DIALOG_ENV_VAR_CONFIG_BASE_H__
#define __DIALOG_ENV_VAR_CONFIG_BASE_H__

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/intl.h>
class DIALOG_SHIM;

#include "dialog_shim.h"
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/font.h>
#include <wx/grid.h>
#include <wx/gdicmn.h>
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/dialog.h>

///////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
/// Class DIALOG_ENV_VAR_CONFIG_BASE
///////////////////////////////////////////////////////////////////////////////
class DIALOG_ENV_VAR_CONFIG_BASE : public DIALOG_SHIM
{
	private:
	
	protected:
		wxGrid* m_grid;
		wxButton* m_buttonOk;
		wxButton* m_buttonCancel;
		wxButton* m_buttonAdd;
		wxButton* m_buttonDelete;
		
		// Virtual event handlers, overide them in your derived class
		virtual void OnAddRow( wxCommandEvent& event ) { event.Skip(); }
		virtual void OnDeleteSelectedRows( wxCommandEvent& event ) { event.Skip(); }
		
	
	public:
		
		DIALOG_ENV_VAR_CONFIG_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Path Configuration"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER ); 
		~DIALOG_ENV_VAR_CONFIG_BASE();
	
};

#endif //__DIALOG_ENV_VAR_CONFIG_BASE_H__
Loading