← Back to team overview

kicad-developers team mailing list archive

[patch 1/1] kicad-py-context-menu-merge.patch

 

Allow better context menu handling for python
- allow to set a menu per file
- allow to add menu items just before displaying it 
(kicad::TreeContextMenu event)

Bugfix:
- fixed directory moving to itself
- fixed files moving in the same directory

---
kicad/kicad.cpp | 4 ++++
kicad/kicad.h | 6 ++++++
kicad/treeprj.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 54 insertions(+), 2 deletions(-)

 --------------020001090606080509070605 Content-Type: text/x-patch;
name="kicad-py-context-menu-merge.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="kicad-py-context-menu-merge.patch"

Allow better context menu handling for python
- allow to set a menu per file
- allow to add menu items just before displaying it (kicad::TreeContextMenu event)

Bugfix:
- fixed directory moving to itself
- fixed files moving in the same directory

---
kicad/kicad.cpp | 4 ++++
kicad/kicad.h | 6 ++++++
kicad/treeprj.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 54 insertions(+), 2 deletions(-)


Index: kicad-dev/kicad/kicad.cpp
===================================================================
--- kicad-dev.orig/kicad/kicad.cpp	2007-05-05 23:47:52.000000000 +0200
+++ kicad-dev/kicad/kicad.cpp	2007-05-06 01:23:31.000000000 +0200
@@ -78,6 +78,7 @@
object WinEDA_PrjFrame::GetFtExPy( enum TreeFileType type ) const { return PyHandler::Convert( GetFileExt( type ) ); }
object WinEDA_PrjFrame::GetMenuPy( enum TreeFileType type ) { return object( handle<>( borrowed( wxPyMake_wxObject( GetContextMenu( (int) type ), false ) ) ) ); }
object WinEDA_PrjFrame::GetTreeCtrl() { return object( handle<>( borrowed( wxPyMake_wxObject( m_TreeProject, false ) ) ) ); }
+object WinEDA_PrjFrame::GetCurrentMenu()	{ return object( handle<>( borrowed( wxPyMake_wxObject( m_PopupMenu, false ) ) ) ); }

void WinEDA_PrjFrame::NewFilePy( const str & name, enum TreeFileType type, object & id )
{
@@ -111,6 +112,7 @@
bool TreePrjItemData::RenamePy( const str & newname, bool check ) { return Rename( PyHandler::MakeStr(newname), check ); }
object TreePrjItemData::GetDirPy() const { return PyHandler::Convert( GetDir() ); }
object TreePrjItemData::GetFileNamePy() const	{ return PyHandler::Convert( GetFileName() ); }
+object TreePrjItemData::GetMenuPy()	{ return object( handle<>( borrowed( wxPyMake_wxObject( &m_fileMenu, false ) ) ) ); }

// kicad module init function
// ( this function is called from PyHandler to init the kicad module )
@@ -126,6 +128,7 @@
.def( "GetDir",	&TreePrjItemData::GetDirPy )
.def( "GetType",	&TreePrjItemData::GetType )
.def( "GetId",	&TreePrjItemData::GetIdPy )
+	.def( "GetMenu", &TreePrjItemData::GetMenuPy )
// Item control
.def( "Rename", &TreePrjItemData::RenamePy )
.def( "Move",	&TreePrjItemData::Move )
@@ -157,6 +160,7 @@
.def( "ClearFilters", &WinEDA_PrjFrame::ClearFilters )
.def( "RemoveFilter", &WinEDA_PrjFrame::RemoveFilterPy )
.def( "GetFilters", &WinEDA_PrjFrame::GetFilters, return_value_policy < copy_const_reference >() )
+	.def( "GetCurrentMenu",	&WinEDA_PrjFrame::GetCurrentMenu )
// Project tree control
.def( "GetTreeCtrl",	&WinEDA_PrjFrame::GetTreeCtrl )
.def( "GetItemData", &WinEDA_PrjFrame::GetItemData, return_value_policy < reference_existing_object >() )
Index: kicad-dev/kicad/kicad.h
===================================================================
--- kicad-dev.orig/kicad/kicad.h	2007-05-05 23:44:24.000000000 +0200
+++ kicad-dev/kicad/kicad.h	2007-05-06 01:22:07.000000000 +0200
@@ -117,6 +117,7 @@
enum TreeFileType m_Type;
wxString m_FileName;
wxTreeCtrl * m_Parent;
+	wxMenu m_fileMenu;

public:

@@ -141,11 +142,14 @@
void Move( TreePrjItemData * dest );
void Activate();

+	const wxMenu * GetMenu() { return &m_fileMenu; }
+
#ifdef KICAD_PYTHON
boost::python::object GetFileNamePy() const;
bool RenamePy( const boost::python::str & newname, bool check = true );
boost::python::object GetDirPy() const;
boost::python::object GetIdPy() const;
+	boost::python::object GetMenuPy();
#endif
};

@@ -155,6 +159,7 @@
private:

std::vector< wxMenu* > m_ContextMenus;
+	wxMenu * m_PopupMenu;
std::vector< wxString > m_Filters;

wxCursor m_DragCursor;
@@ -222,6 +227,7 @@
void NewFilePy( const boost::python::str & name, enum TreeFileType type, boost::python::object & root );

TreePrjItemData * FindItemData( const boost::python::str & name );
+	boost::python::object GetCurrentMenu();

#endif

Index: kicad-dev/kicad/treeprj.cpp
===================================================================
--- kicad-dev.orig/kicad/treeprj.cpp	2007-05-05 22:41:03.000000000 +0200
+++ kicad-dev/kicad/treeprj.cpp	2007-05-06 01:26:12.000000000 +0200
@@ -84,6 +84,10 @@
const wxString sep = wxFileName().GetPathSeparator();
if (!dest) return;
if ( m_Parent != dest->m_Parent ) return;// Can not cross move!
+	if ( dest == this ) return; // Can not move to ourself...
+
+	wxTreeItemId parent = m_Parent->GetItemParent( GetId() );
+	if ( dest == dynamic_cast<TreePrjItemData*>( m_Parent->GetItemData( parent ) ) ) return; // same parent ?

// We need to create a new item from us, and move
// data to there ...
@@ -94,6 +98,8 @@
if ( !dest->GetDir().IsEmpty() ) destName = dest->GetDir() + sep;
destName += fname.GetFullName();

+	if ( destName == GetFileName() ) return; // Same place ??
+
// Move the file on the disk:
if ( !wxRenameFile( GetFileName(), destName, false ) )
{
@@ -405,6 +411,7 @@
m_Parent = parent;
m_TreeProject = NULL;
wxMenuItem *item;
+	m_PopupMenu = NULL;

#ifdef KICAD_PYTHON
PyHandler::GetInstance()->DeclareEvent( wxT( "kicad::RunScript" ) );
@@ -731,23 +738,58 @@
}

void WinEDA_PrjFrame::OnRight(wxTreeEvent & Event)
+// Opens (popup) the context menu
{
int tree_id;
TreePrjItemData * tree_data;
wxString FullFileName;

+	// Delete and recreate the context menu
+	delete( m_PopupMenu );
+	m_PopupMenu = new wxMenu();
+
+	// Get the current filename:
tree_data = GetSelectedData();
if (!tree_data) return;

tree_id = tree_data->GetType();
FullFileName = tree_data->GetFileName();

+	// copy menu contents in order of the next array:
+	wxMenu * menus[] =
+	{
+	GetContextMenu( tree_id )
+	,	const_cast<wxMenu*>( tree_data->GetMenu() )
+	};
+
+	for ( unsigned int j = 0; j < sizeof(menus)/sizeof(wxMenu*); j++ )
+	{
+	wxMenu * menu = menus[j];
+	if ( ! menu ) continue;
+	wxMenuItemList list = menu->GetMenuItems();
+
+	for ( unsigned int i = 0; i < list.GetCount() ; i ++ )
+	{
+	// Grrrr! wxMenu does not have any copy constructor !! (do it by hand)
+	wxMenuItem * src = list[i];
+	wxString label = src->GetText();
+	// for obscure reasons, the & is translated into _ ... so replace it
+	label.Replace( wxT("_"), wxT("&"), true );
+	wxMenuItem * item = new wxMenuItem( m_PopupMenu, src->GetId()
+	, label, src->GetHelp()
+	, src->GetKind() );
+	item->SetBitmap( src->GetBitmap() );
+	m_PopupMenu->Append( item );
+	}
+	}
+
+	// At last, call python to let python add menu items "on the fly"
+
#ifdef KICAD_PYTHON
PyHandler::GetInstance()->TriggerEvent( wxT("kicad::TreeContextMenu"), PyHandler::Convert( FullFileName ) );
#endif

-	wxMenu * menu = GetContextMenu( tree_id );
-	if ( menu ) PopupMenu( menu );
+	if ( m_PopupMenu ) PopupMenu( m_PopupMenu );
}

void WinEDA_MainFrame::OnRefresh(wxCommandEvent & event )
 --------------020001090606080509070605--