Commit 155ea57c authored by Miguel Angel Ajo's avatar Miguel Angel Ajo

LOCALE_IO toggle implementation for locale switches on scripting; code...

LOCALE_IO toggle implementation for locale switches on scripting; code cleanups to comply with kicad coding style policy
parent cefd3cd5
......@@ -36,12 +36,8 @@ void FOOTPRINT_WIZARD::register_wizard()
FOOTPRINT_WIZARDS::register_wizard( this );
}
/**
* FOOTPRINT_WIZARD system wide static list
*/
std::vector<FOOTPRINT_WIZARD*> FOOTPRINT_WIZARDS::m_FootprintWizards;
FOOTPRINT_WIZARD* FOOTPRINT_WIZARDS::GetWizard( int aIndex )
{
return m_FootprintWizards[aIndex];
......@@ -50,17 +46,17 @@ FOOTPRINT_WIZARD* FOOTPRINT_WIZARDS::GetWizard( int aIndex )
FOOTPRINT_WIZARD* FOOTPRINT_WIZARDS::GetWizard( wxString aName )
{
int max = GetSize();
for( int i=0; i<max; i++ )
{
FOOTPRINT_WIZARD *wizard = GetWizard( i );
wxString name = wizard->GetName();
if ( name.Cmp( aName ) )
return wizard;
return wizard;
}
return NULL;
}
......@@ -71,10 +67,10 @@ int FOOTPRINT_WIZARDS::GetSize()
void FOOTPRINT_WIZARDS::register_wizard(FOOTPRINT_WIZARD *aWizard)
{
wxString name = aWizard->GetName();
wxString name = aWizard->GetName();
m_FootprintWizards.push_back( aWizard );
}
......
......@@ -134,6 +134,9 @@ public:
class FOOTPRINT_WIZARDS
{
private:
/**
* FOOTPRINT_WIZARD system wide static list
*/
static std::vector<FOOTPRINT_WIZARD*> m_FootprintWizards;
public:
......
......@@ -20,25 +20,28 @@
#include <dialogs/dialog_footprint_wizard_list.h>
#include <base_units.h>
#define NEXT_PART 1
#define NEW_PART 0
#define PREVIOUS_PART -1
#define NEXT_PART 1
#define NEW_PART 0
#define PREVIOUS_PART -1
void FOOTPRINT_WIZARD_FRAME::Process_Special_Functions( wxCommandEvent& event )
{
wxString msg;
int page;
wxString msg;
int page;
switch( event.GetId() )
{
case ID_FOOTPRINT_WIZARD_NEXT:
m_PageList->SetSelection( m_PageList->GetSelection()+1, true );
m_PageList->SetSelection( m_PageList->GetSelection() + 1, true );
break;
case ID_FOOTPRINT_WIZARD_PREVIOUS:
page = m_PageList->GetSelection()-1;
if (page<0) page=0;
page = m_PageList->GetSelection() - 1;
if( page<0 )
page = 0;
m_PageList->SetSelection( page, true );
break;
......@@ -50,17 +53,19 @@ void FOOTPRINT_WIZARD_FRAME::Process_Special_Functions( wxCommandEvent& event )
}
}
/* Function OnLeftClick
* Captures a left click event in the dialog
*
*
*/
void FOOTPRINT_WIZARD_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
{
}
/* Function OnRightClick
* Captures a right click event in the dialog
*
* Captures a right click event in the dialog
*
*/
bool FOOTPRINT_WIZARD_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
{
......@@ -71,12 +76,12 @@ bool FOOTPRINT_WIZARD_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopM
/* Displays the name of the current opened library in the caption */
void FOOTPRINT_WIZARD_FRAME::DisplayWizardInfos()
{
wxString msg;
wxString msg;
msg = _( "Footprint Wizard" );
msg << wxT( " [" );
if( ! m_wizardName.IsEmpty() )
if( !m_wizardName.IsEmpty() )
msg << m_wizardName;
else
msg += _( "no wizard selected" );
......@@ -86,6 +91,7 @@ void FOOTPRINT_WIZARD_FRAME::DisplayWizardInfos()
SetTitle( msg );
}
void FOOTPRINT_WIZARD_FRAME::ReloadFootprint()
{
if( m_FootprintWizard == NULL )
......@@ -94,25 +100,28 @@ void FOOTPRINT_WIZARD_FRAME::ReloadFootprint()
SetCurItem( NULL );
// Delete the current footprint
GetBoard()->m_Modules.DeleteAll();
MODULE *m = m_FootprintWizard->GetModule();
if ( m )
MODULE* m = m_FootprintWizard->GetModule();
if( m )
{
/* Here we should make a copy of the object before adding to board*/
m->SetParent((EDA_ITEM*)GetBoard());
GetBoard()->m_Modules.Append(m);
wxPoint p( 0 , 0 );
m->SetParent( (EDA_ITEM*) GetBoard() );
GetBoard()->m_Modules.Append( m );
wxPoint p( 0, 0 );
m->SetPosition( p );
}
else
{
printf ("m_FootprintWizard->GetModule() returns NULL\n");
printf( "m_FootprintWizard->GetModule() returns NULL\n" );
}
m_canvas->Refresh();
}
MODULE* FOOTPRINT_WIZARD_FRAME::GetBuiltFootprint()
{
if ( m_FootprintWizard )
if( m_FootprintWizard )
{
return m_FootprintWizard->GetModule();
}
......@@ -122,16 +131,17 @@ MODULE* FOOTPRINT_WIZARD_FRAME::GetBuiltFootprint()
}
}
void FOOTPRINT_WIZARD_FRAME::SelectFootprintWizard()
{
DIALOG_FOOTPRINT_WIZARD_LIST *selectWizard =
new DIALOG_FOOTPRINT_WIZARD_LIST( this );
DIALOG_FOOTPRINT_WIZARD_LIST* selectWizard =
new DIALOG_FOOTPRINT_WIZARD_LIST( this );
selectWizard->ShowModal();
m_FootprintWizard = selectWizard->GetWizard();
if ( m_FootprintWizard )
if( m_FootprintWizard )
{
m_wizardName = m_FootprintWizard->GetName();
m_wizardDescription = m_FootprintWizard->GetDescription();
......@@ -142,64 +152,63 @@ void FOOTPRINT_WIZARD_FRAME::SelectFootprintWizard()
DisplayWizardInfos();
ReCreatePageList();
ReCreateParameterList();
}
void FOOTPRINT_WIZARD_FRAME::SelectCurrentWizard( wxCommandEvent& event )
{
SelectFootprintWizard();
}
/**
* Function SelectCurrentFootprint
* Selects the current footprint name and display it
*/
void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event )
{
int page = m_PageList->GetSelection();
if ( page<0 )
if( page<0 )
return;
int n=m_ParameterGrid->GetNumberRows();
wxArrayString arr;
wxArrayString ptList = m_FootprintWizard->GetParameterTypes(page);
for ( int i=0; i<n; i++ )
{
int n = m_ParameterGrid->GetNumberRows();
wxArrayString arr;
wxArrayString ptList = m_FootprintWizard->GetParameterTypes( page );
for( int i = 0; i<n; i++ )
{
wxString value = m_ParameterGrid->GetCellValue( i, 1 );
// if this parameter is expected to be an internal
// if this parameter is expected to be an internal
// unit convert it back from the user format
if (ptList[i]==wxT("IU"))
if( ptList[i]==wxT( "IU" ) )
{
LOCALE_IO toggle;
double dValue;
value.ToDouble( &dValue );
// convert from mils to inches where it's needed
if (g_UserUnit==INCHES) dValue = dValue / 1000.0;
dValue = From_User_Unit( g_UserUnit, dValue);
value.Printf( wxT("%lf"), dValue );
if( g_UserUnit==INCHES )
dValue = dValue / 1000.0;
dValue = From_User_Unit( g_UserUnit, dValue );
value.Printf( wxT( "%lf" ), dValue );
}
// If our locale is set to use , for decimal point, just change it
// to be scripting compatible
value.Replace( wxT( "," ), wxT( "." ) );
arr.Add( value );
}
wxString res = m_FootprintWizard->SetParameterValues( page, arr );
ReloadFootprint();
DisplayWizardInfos();
}
......@@ -207,8 +216,8 @@ void FOOTPRINT_WIZARD_FRAME::ParametersUpdated( wxGridEvent& event )
* Function RedrawActiveWindow
* Display the current selected component.
* If the component is an alias, the ROOT component is displayed
*
*/
*
*/
void FOOTPRINT_WIZARD_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
{
if( !GetBoard() )
......@@ -219,7 +228,7 @@ void FOOTPRINT_WIZARD_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
MODULE* module = GetBoard()->m_Modules;
if ( module )
if( module )
SetMsgPanel( module );
m_canvas->DrawCrossHair( DC );
......
This diff is collapsed.
......@@ -48,105 +48,104 @@ class FOOTPRINT_WIZARD_FRAME : public PCB_BASE_FRAME
{
private:
wxSashLayoutWindow* m_PageListWindow; //< List of libraries (for selection )
wxListBox* m_PageList; //< The list of pages
wxSize m_PageListSize; //< size of the window
wxSashLayoutWindow* m_PageListWindow; // < List of libraries (for selection )
wxListBox* m_PageList; // < The list of pages
wxSize m_PageListSize; // < size of the window
wxSashLayoutWindow* m_ParameterGridWindow; //< List of components in the selected library
wxGrid* m_ParameterGrid; //< The list of parameters
wxSize m_ParameterGridSize; //< size of the window
wxSashLayoutWindow* m_ParameterGridWindow; // < List of components in the selected library
wxGrid* m_ParameterGrid; // < The list of parameters
wxSize m_ParameterGridSize; // < size of the window
// Flags
wxSemaphore* m_Semaphore; //< != NULL if the frame must emulate a modal dialog
wxString m_configPath; //< subpath for configuration
FOOTPRINT_WIZARD* m_FootprintWizard;
wxSemaphore* m_Semaphore; // < != NULL if the frame must emulate a modal dialog
wxString m_configPath; // < subpath for configuration
FOOTPRINT_WIZARD* m_FootprintWizard;
protected:
wxString m_wizardName; //< name of the current wizard
wxString m_wizardDescription; //< description of the wizard
wxString m_wizardStatus; //< current wizard status
wxString m_wizardName; // < name of the current wizard
wxString m_wizardDescription; // < description of the wizard
wxString m_wizardStatus; // < current wizard status
public:
FOOTPRINT_WIZARD_FRAME( FOOTPRINT_EDIT_FRAME* parent,
wxSemaphore* semaphore = NULL,
long style = KICAD_DEFAULT_DRAWFRAME_STYLE );
FOOTPRINT_WIZARD_FRAME( FOOTPRINT_EDIT_FRAME* parent,
wxSemaphore* semaphore = NULL,
long style = KICAD_DEFAULT_DRAWFRAME_STYLE );
~FOOTPRINT_WIZARD_FRAME();
MODULE* GetBuiltFootprint( void );
MODULE* GetBuiltFootprint( void );
private:
void OnSize( wxSizeEvent& event );
void OnSize( wxSizeEvent& event );
/**
* Function ExportSelectedFootprint();
* will let the caller exit from the wait loop, and get the built footprint
*
*/
void ExportSelectedFootprint( wxCommandEvent& aEvent );
void ExportSelectedFootprint( wxCommandEvent& aEvent );
/**
* Function OnSashDrag
* resizes the child windows when dragging a sash window border.
*/
void OnSashDrag( wxSashEvent& event );
void OnSashDrag( wxSashEvent& event );
/**
* Function ReCreatePageList
* Creates or recreates the list of parameter pages for the current wizard.
* This list is sorted
*/
void ReCreatePageList();
void ReCreatePageList();
/**
* Function ReCreateParameterList
* Creates the list of parameters for the current page
*/
void ReCreateParameterList();
void ReCreateParameterList();
/**
* Function SelectFootprintWizard
* Shows the list of footprint wizards available into the system
*/
void SelectFootprintWizard();
void SelectFootprintWizard();
/**
* Function ReloadFootprint
* Reloads the current footprint
*/
void ReloadFootprint();
void ReloadFootprint();
void Process_Special_Functions( wxCommandEvent& event );
void Process_Special_Functions( wxCommandEvent& event );
/**
* Function DisplayWizardInfos
* Shows all the details about the current wizard
*/
void DisplayWizardInfos();
void DisplayWizardInfos();
void RedrawActiveWindow( wxDC* DC, bool EraseBg );
void OnCloseWindow( wxCloseEvent& Event );
void ReCreateHToolbar();
void ReCreateVToolbar();
void OnLeftClick( wxDC* DC, const wxPoint& MousePos );
void ClickOnPageList( wxCommandEvent& event );
void OnSetRelativeOffset( wxCommandEvent& event );
void RedrawActiveWindow( wxDC* DC, bool EraseBg );
void OnCloseWindow( wxCloseEvent& Event );
void ReCreateHToolbar();
void ReCreateVToolbar();
void OnLeftClick( wxDC* DC, const wxPoint& MousePos );
void ClickOnPageList( wxCommandEvent& event );
void OnSetRelativeOffset( wxCommandEvent& event );
void GeneralControl( wxDC* aDC, const wxPoint& aPosition, int aHotKey = 0 );
void GeneralControl( wxDC* aDC, const wxPoint& aPosition, int aHotKey = 0 );
/**
/**
* Function LoadSettings
* loads the library viewer frame specific configuration settings.
*
* Don't forget to call this base method from any derived classes or the
* settings will not get loaded.
*/
void LoadSettings();
void LoadSettings();
/**
* Function SaveSettings
......@@ -155,7 +154,7 @@ private:
* Don't forget to call this base method from any derived classes or the
* settings will not get saved.
*/
void SaveSettings();
void SaveSettings();
/**
......@@ -163,21 +162,20 @@ private:
* is called when the frame frame is activate to reload the libraries and component lists
* that can be changed by the schematic editor or the library editor.
*/
virtual void OnActivate( wxActivateEvent& event );
void SelectCurrentWizard( wxCommandEvent& event );
virtual void OnActivate( wxActivateEvent& event );
void ParametersUpdated( wxGridEvent& event );
void SelectCurrentWizard( wxCommandEvent& event );
void ParametersUpdated( wxGridEvent& event );
bool OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu );
bool OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu );
/**
* Function Show3D_Frame (virtual)
* displays 3D view of the footprint (module) being edited.
*/
void Show3D_Frame( wxCommandEvent& event );
void Show3D_Frame( wxCommandEvent& event );
/**
* Function Update3D_Frame
......@@ -186,18 +184,18 @@ private:
* @param aForceReloadFootprint = true to reload data (default)
* = false to update title only -(aftre creating the 3D viewer)
*/
void Update3D_Frame( bool aForceReloadFootprint = true );
void Update3D_Frame( bool aForceReloadFootprint = true );
/*
* Virtual functions, not used here, but needed by PCB_BASE_FRAME
* (virtual pure functions )
*/
void OnLeftDClick(wxDC*, const wxPoint&) {}
void SaveCopyInUndoList(BOARD_ITEM*, UNDO_REDO_T, const wxPoint&) {}
void SaveCopyInUndoList(PICKED_ITEMS_LIST&, UNDO_REDO_T, const wxPoint&) {}
void OnLeftDClick( wxDC*, const wxPoint& ) {}
void SaveCopyInUndoList( BOARD_ITEM*, UNDO_REDO_T, const wxPoint& ) {}
void SaveCopyInUndoList( PICKED_ITEMS_LIST&, UNDO_REDO_T, const wxPoint& ) {}
DECLARE_EVENT_TABLE()
};
#endif // FOOTPRINT_WIZARD_FRM_H_
#endif // FOOTPRINT_WIZARD_FRM_H_
......@@ -28,45 +28,41 @@
*/
#ifndef PCBNEW_FOOTPRINT_WIZARDS_H
#define PCBNEW_FOOTPRINT_WIZARDS_H
#define PCBNEW_FOOTPRINT_WIZARDS_H
#include <Python.h>
#include <vector>
#include <class_footprint_wizard.h>
class PYTHON_FOOTPRINT_WIZARD: public FOOTPRINT_WIZARD
class PYTHON_FOOTPRINT_WIZARD : public FOOTPRINT_WIZARD
{
PyObject *m_PyWizard;
PyObject *CallMethod( const char *aMethod, PyObject *aArglist=NULL );
wxString CallRetStrMethod( const char *aMethod, PyObject *aArglist=NULL );
wxArrayString CallRetArrayStrMethod( const char *aMethod,
PyObject *aArglist=NULL );
PyObject* m_PyWizard;
PyObject* CallMethod( const char* aMethod, PyObject* aArglist = NULL );
wxString CallRetStrMethod( const char* aMethod, PyObject* aArglist = NULL );
wxArrayString CallRetArrayStrMethod( const char* aMethod,
PyObject* aArglist = NULL );
public:
PYTHON_FOOTPRINT_WIZARD( PyObject *wizard );
PYTHON_FOOTPRINT_WIZARD( PyObject* wizard );
~PYTHON_FOOTPRINT_WIZARD();
wxString GetName();
wxString GetImage();
wxString GetDescription();
int GetNumParameterPages();
wxString GetParameterPageName( int aPage );
wxArrayString GetParameterNames( int aPage );
wxArrayString GetParameterTypes( int aPage );
wxArrayString GetParameterValues( int aPage );
wxArrayString GetParameterErrors( int aPage );
wxString SetParameterValues( int aPage, wxArrayString& aValues ); //< must return "OK" or error description
MODULE* GetModule();
wxString GetName();
wxString GetImage();
wxString GetDescription();
int GetNumParameterPages();
wxString GetParameterPageName( int aPage );
wxArrayString GetParameterNames( int aPage );
wxArrayString GetParameterTypes( int aPage );
wxArrayString GetParameterValues( int aPage );
wxArrayString GetParameterErrors( int aPage );
wxString SetParameterValues( int aPage, wxArrayString& aValues ); // < must return "OK" or error description
MODULE* GetModule();
};
class PYTHON_FOOTPRINT_WIZARDS
{
public:
static void register_wizard( PyObject *aPyWizard );
static void register_wizard( PyObject* aPyWizard );
};
#endif /* PCBNEW_FOOTPRINT_WIZARDS_H */
#endif /* PCBNEW_FOOTPRINT_WIZARDS_H */
......@@ -39,68 +39,68 @@
#include <macros.h>
#include <stdlib.h>
static PCB_EDIT_FRAME *PcbEditFrame=NULL;
static PCB_EDIT_FRAME* PcbEditFrame = NULL;
BOARD *GetBoard()
BOARD* GetBoard()
{
if (PcbEditFrame)
if( PcbEditFrame )
return PcbEditFrame->GetBoard();
else return NULL;
else
return NULL;
}
void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME *aPCBEdaFrame )
void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPCBEdaFrame )
{
PcbEditFrame = aPCBEdaFrame;
PcbEditFrame = aPCBEdaFrame;
}
BOARD* LoadBoard( wxString& aFileName )
{
if( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
return LoadBoard( aFileName, IO_MGR::KICAD );
if ( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
return LoadBoard(aFileName,IO_MGR::KICAD);
else if (aFileName.EndsWith(wxT(".brd")))
return LoadBoard(aFileName,IO_MGR::LEGACY);
else if( aFileName.EndsWith( wxT( ".brd" ) ) )
return LoadBoard( aFileName, IO_MGR::LEGACY );
// as fall back for any other kind use the legacy format
return LoadBoard(aFileName,IO_MGR::LEGACY);
return LoadBoard( aFileName, IO_MGR::LEGACY );
}
BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
{
return IO_MGR::Load( aFormat, aFileName );
return IO_MGR::Load( aFormat, aFileName );
}
bool SaveBoard( wxString& aFilename, BOARD* aBoard )
{
return SaveBoard( aFilename, aBoard,IO_MGR::KICAD );
return SaveBoard( aFilename, aBoard, IO_MGR::KICAD );
}
bool SaveBoard( wxString& aFileName, BOARD* aBoard,
IO_MGR::PCB_FILE_T aFormat )
{
aBoard->m_Status_Pcb &= ~CONNEXION_OK;
aBoard->SynchronizeNetsAndNetClasses();
aBoard->SetCurrentNetClass( aBoard->m_NetClasses.GetDefault()->GetName() );
wxString header;
PROPERTIES props;
if ( aFormat==IO_MGR::LEGACY )
{
header = wxString::Format(
wxT( "PCBNEW-BOARD Version %d date %s\n\n# Created by Pcbnew%s scripting\n\n" ),
LEGACY_BOARD_FILE_VERSION, DateAndTime().GetData(),
GetBuildVersion().GetData() );
props["header"] = header;
}
aBoard->m_Status_Pcb &= ~CONNEXION_OK;
aBoard->SynchronizeNetsAndNetClasses();
aBoard->SetCurrentNetClass( aBoard->m_NetClasses.GetDefault()->GetName() );
wxString header;
PROPERTIES props;
if( aFormat==IO_MGR::LEGACY )
{
header = wxString::Format(
wxT( "PCBNEW-BOARD Version %d date %s\n\n# Created by Pcbnew%s scripting\n\n" ),
LEGACY_BOARD_FILE_VERSION, DateAndTime().GetData(),
GetBuildVersion().GetData() );
props["header"] = header;
}
IO_MGR::Save( aFormat, aFileName, aBoard, &props );
return true;
IO_MGR::Save( aFormat, aFileName, aBoard, &props );
return true;
}
......@@ -31,16 +31,17 @@
* we want plain pcbnew.<method_name> access from python */
#ifndef SWIG
void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME *aPCBEdaFrame );
#endif
void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPCBEdaFrame );
BOARD *GetBoard();
#endif
BOARD* GetBoard();
BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat );
BOARD* LoadBoard( wxString& aFileName );
BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat );
BOARD* LoadBoard( wxString& aFileName );
bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat );
bool SaveBoard( wxString& aFileName, BOARD* aBoard );
bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat );
bool SaveBoard( wxString& aFileName, BOARD* aBoard );
#endif
......@@ -54,18 +54,18 @@ extern "C" void init_pcbnew( void );
* our own ones
*/
struct _inittab *SwigImportInittab;
static int SwigNumModules = 0;
struct _inittab* SwigImportInittab;
static int SwigNumModules = 0;
/* Add a name + initfuction to our SwigImportInittab */
static void swigAddModule( const char* name, void (* initfunc)() )
{
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigNumModules++;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].initfunc = 0;
}
......@@ -81,11 +81,12 @@ static void swigAddBuiltin()
i++;
/* allocate memory for the python module table */
SwigImportInittab = (struct _inittab*) malloc(
sizeof(struct _inittab)*(i+EXTRA_PYTHON_MODULES));
SwigImportInittab = (struct _inittab*) malloc(
sizeof(struct _inittab) * (i + EXTRA_PYTHON_MODULES) );
/* copy all pre-existing python modules into our newly created table */
i=0;
i = 0;
while( PyImport_Inittab[i].name )
{
swigAddModule( PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc );
......@@ -107,9 +108,10 @@ static void swigAddModules()
// finally it seems better to include all in just one module
// but in case we needed to include any other modules,
// it must be done like this:
// swigAddModule("_kicad",init_kicad);
// swigAddModule( "_kicad", init_kicad );
}
/* Function swigSwitchPythonBuiltin
* switches python module table to our built one .
*
......@@ -120,6 +122,7 @@ static void swigSwitchPythonBuiltin()
PyImport_Inittab = SwigImportInittab;
}
/* Function pcbnewInitPythonScripting
* Initializes all the python environment and publish our interface inside it
* initializes all the wxpython interface, and returns the python thread control structure
......@@ -142,9 +145,9 @@ bool pcbnewInitPythonScripting()
// Load the wxPython core API. Imports the wx._core_ module and sets a
// local pointer to a function table located there. The pointer is used
// internally by the rest of the API functions.
if( ! wxPyCoreAPI_IMPORT() )
if( !wxPyCoreAPI_IMPORT() )
{
wxLogError(wxT("***** Error importing the wxPython API! *****"));
wxLogError( wxT( "***** Error importing the wxPython API! *****" ) );
PyErr_Print();
Py_Finalize();
return false;
......@@ -160,7 +163,7 @@ bool pcbnewInitPythonScripting()
#endif
{
PyLOCK lock;
PyLOCK lock;
PyRun_SimpleString( "import sys\n"
"sys.path.append(\".\")\n"
......@@ -172,10 +175,11 @@ bool pcbnewInitPythonScripting()
return true;
}
void pcbnewFinishPythonScripting()
{
#ifdef KICAD_SCRIPTING_WXPYTHON
wxPyEndAllowThreads(g_PythonMainTState);
wxPyEndAllowThreads( g_PythonMainTState );
#endif
Py_Finalize();
}
......@@ -189,54 +193,55 @@ void RedirectStdio()
// redirects Python's stdout and stderr to a window that will popup
// only on demand when something is printed, like a traceback.
const char* python_redirect =
"import sys\n\
import wx\n\
output = wx.PyOnDemandOutputWindow()\n\
c sys.stderr = output\n";
"import sys\n"
"import wx\n"
"output = wx.PyOnDemandOutputWindow()\n"
"sys.stderr = output\n";
PyLOCK lock;
PyLOCK lock;
PyRun_SimpleString( python_redirect );
}
wxWindow* CreatePythonShellWindow(wxWindow* parent)
wxWindow* CreatePythonShellWindow( wxWindow* parent )
{
const char* pycrust_panel = "\
import wx\n\
from wx.py import shell, version\n\
\n\
class PyCrustPanel(wx.Panel):\n\
\tdef __init__(self, parent):\n\
\t\twx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)\n\
\t\t\n\
\t\t\n\
\t\tintro = \"Welcome To PyCrust %s - KiCAD Python Shell\" % version.VERSION\n\
\t\tpycrust = shell.Shell(self, -1, introText=intro)\n\
\t\t\n\
\t\tsizer = wx.BoxSizer(wx.VERTICAL)\n\n\
\t\tsizer.Add(pycrust, 1, wx.EXPAND|wx.BOTTOM|wx.LEFT|wx.RIGHT, 10)\n\n\
\t\tself.SetSizer(sizer)\n\n\
\n\
def makeWindow(parent):\n\
win = PyCrustPanel(parent)\n\
return win\n\
";
wxWindow* window = NULL;
PyObject* result;
const char* pycrust_panel =
"import wx\n"
"from wx.py import shell, version\n"
"\n"
"class PyCrustPanel(wx.Panel):\n"
"\tdef __init__(self, parent):\n"
"\t\twx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)\n"
"\t\t\n"
"\t\t\n"
"\t\tintro = \"Welcome To PyCrust %s - KiCAD Python Shell\" % version.VERSION\n"
"\t\tpycrust = shell.Shell(self, -1, introText=intro)\n"
"\t\t\n"
"\t\tsizer = wx.BoxSizer(wx.VERTICAL)\n\n"
"\t\tsizer.Add(pycrust, 1, wx.EXPAND|wx.BOTTOM|wx.LEFT|wx.RIGHT, 10)\n\n"
"\t\tself.SetSizer(sizer)\n\n"
"\n"
"def makeWindow(parent):\n"
" win = PyCrustPanel(parent)\n"
" return win\n"
"\n";
wxWindow* window = NULL;
PyObject* result;
// As always, first grab the GIL
PyLOCK lock;
PyLOCK lock;
// Now make a dictionary to serve as the global namespace when the code is
// executed. Put a reference to the builtins module in it.
PyObject* globals = PyDict_New();
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
PyObject* globals = PyDict_New();
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
PyDict_SetItemString( globals, "__builtins__", builtins );
Py_DECREF(builtins);
Py_DECREF( builtins );
// Execute the code to make the makeWindow function we defined above
result = PyRun_String( pycrust_panel, Py_file_input, globals, globals );
......@@ -247,7 +252,8 @@ def makeWindow(parent):\n\
PyErr_Print();
return NULL;
}
Py_DECREF(result);
Py_DECREF( result );
// Now there should be an object named 'makeWindow' in the dictionary that
// we can grab a pointer to:
......@@ -258,10 +264,10 @@ def makeWindow(parent):\n\
// use of another wxPython API to take a wxWindows object and build a
// wxPython object that wraps it.
PyObject* arg = wxPyMake_wxObject( parent, false );
PyObject* arg = wxPyMake_wxObject( parent, false );
wxASSERT( arg != NULL );
PyObject* tuple = PyTuple_New( 1 );
PyObject* tuple = PyTuple_New( 1 );
PyTuple_SET_ITEM( tuple, 0, arg );
result = PyEval_CallObject( func, tuple );
......@@ -273,11 +279,11 @@ def makeWindow(parent):\n\
{
// Otherwise, get the returned window out of Python-land and
// into C++-ville...
bool success = wxPyConvertSwigPtr(result, (void**)&window, _T("wxWindow") );
(void)success;
bool success = wxPyConvertSwigPtr( result, (void**) &window, _T( "wxWindow" ) );
(void) success;
wxASSERT_MSG(success, _T("Returned object was not a wxWindow!") );
Py_DECREF(result);
wxASSERT_MSG( success, _T( "Returned object was not a wxWindow!" ) );
Py_DECREF( result );
}
// Release the python objects we still have
......@@ -286,4 +292,6 @@ def makeWindow(parent):\n\
return window;
}
#endif
#ifndef __PYTHON_SCRIPTING_H
#define __PYTHON_SCRIPTING_H
// undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617
// undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
......@@ -21,19 +21,18 @@
* Initializes the Python engine inside pcbnew
*/
bool pcbnewInitPythonScripting();
void pcbnewFinishPythonScripting();
bool pcbnewInitPythonScripting();
void pcbnewFinishPythonScripting();
#ifdef KICAD_SCRIPTING_WXPYTHON
void RedirectStdio();
wxWindow* CreatePythonShellWindow( wxWindow* parent );
void RedirectStdio();
wxWindow* CreatePythonShellWindow( wxWindow* parent );
class PyLOCK
{
wxPyBlock_t b;
wxPyBlock_t b;
public:
// @todo, find out why these are wxPython specific. We need the GIL regardless.
......@@ -46,13 +45,12 @@ public:
#else
class PyLOCK
{
PyGILState_STATE gil_state;
PyGILState_STATE gil_state;
public:
PyLOCK() { gil_state = PyGILState_Ensure(); }
~PyLOCK() { PyGILState_Release( gil_state ); }
PyLOCK() { gil_state = PyGILState_Ensure(); }
~PyLOCK() { PyGILState_Release( gil_state ); }
};
#endif
#endif // __PYTHON_SCRIPTING_H
#endif // __PYTHON_SCRIPTING_H
......@@ -65,8 +65,8 @@ wxString* newWxStringFromPy( PyObject* src )
{
bool must_unref_str = false;
wxString* result = NULL;
PyObject* obj = src;
wxString* result = NULL;
PyObject* obj = src;
#if wxUSE_UNICODE
bool must_unref_obj = false;
......@@ -111,14 +111,14 @@ wxString* newWxStringFromPy( PyObject* src )
// normal string (or object) to normal python string
PyObject* str = src;
if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
if( PyUnicode_Check( src ) ) // if it's unicode convert to normal string
{
str = PyUnicode_AsEncodedString( src, wxPythonEncoding, "strict" );
if( PyErr_Occurred() )
return NULL;
}
else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
else if( !PyString_Check( src ) ) // if it's not a string, str(obj)
{
str = PyObject_Str( src );
must_unref_str = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment