Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Plugins development => Topic started by: jensenm on July 10, 2012, 11:15:13 am

Title: Linking menu event table to member function
Post by: jensenm on July 10, 2012, 11:15:13 am
I'd like to thank any respondents in advance for the help.

I've been using Code::Blocks for a while now for C code programming and figure it's time I learn't how to extend it.

I've been following the tutorial and I'm having a little trouble getting a menu item to run a function in a Code::blocks plugin.  Following the tutorials I have performed the following tasks...

1. Create new plugin project

   Using generic project with Create menu entries checked on

2. Define menu entry creation code.  (This seems to work OK.)

     a)  Create a new id for the menu entry
     b)  Locate the menu position to add the item
     c)  Insert or append the item

3.  Define a member function to do something

4. Define the Event table entry linking the menu item ID to the member function

5. Compile and install the plugin using the plugin manager.

The menu appears, however when I click the entry no action seems to result.  My code is below.



Code
#include <sdk.h> // Code::Blocks SDK
#include <configurationpanel.h>
#include "MyTools.h"

namespace
{
    PluginRegistrant<MyTools> reg(_T("MyTools"));
}

const int idMyMenuItem     = wxNewId();

BEGIN_EVENT_TABLE(MyTools, cbPlugin)
    EVT_MENU(idMyMenuItem, MyTools::doSomething)
END_EVENT_TABLE()

MyTools::MyTools()
{
    if(!Manager::LoadResource(_T("MyTools.zip")))
    {
        NotifyMissingFile(_T("MyTools.zip"));
    }
}

MyTools::~MyTools()
{
}

void MyTools::OnAttach()
{
}

void MyTools::OnRelease(bool appShutDown)
{
    if(m_MyMenu)
    {
        delete m_MyMenu;
        m_MyMenu = NULL;
    }
}


void MyTools::BuildMenu(wxMenuBar* menuBar)
{
    int ToolsPos = menuBar->FindMenu(_("&Tools"));
    if(ToolsPos != wxNOT_FOUND)
    {
        m_MyMenu = new wxMenu;
        menuBar->Insert(ToolsPos,m_MyMenu,wxT("My Tools"));

        m_MyMenu->Append(idMyMenuItem, _("Do Something\tShift-F6"));
    }
    else
    {
        wxMessageBox(wxT("Menu not found"),wxT("ERROR"), wxOK, NULL);
    }
}

void MyTools::doSomething(wxCommandEvent& event)
{
    cbMessageBox(_("Do Something!"), _("Message"), wxOK, Manager::Get()->GetAppWindow());
}
Title: Re: Linking menu event table to member function
Post by: MortenMacFly on July 10, 2012, 11:23:33 am
The menu appears, however when I click the entry no action seems to result.  My code is below.
Do you declare the event table? W/O the full source code its hard to help. Its best if you provide a minimal, but complete example.
Title: Re: Linking menu event table to member function
Post by: jensenm on July 10, 2012, 10:27:48 pm
The event table is declared in the private section of the header file,

 The OS is windows 7 64 bit.
Codeblocks 10.05
wxWidgets 2.8.12

The full file project is attached.

Here is the article I am working with.    Once I know what I'm doing I'd be interested in helping bring the documentation up to date.

http://wiki.codeblocks.org/index.php?title=Creating_a_Plug-in_which_modifies_CB%27s_Menus (http://wiki.codeblocks.org/index.php?title=Creating_a_Plug-in_which_modifies_CB%27s_Menus)

Code
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <cbplugin.h> // for "class cbPlugin"

class MyTools : public cbPlugin
{
    public:
        MyTools();
        virtual ~MyTools();
        virtual void BuildMenu(wxMenuBar* menuBar);
        virtual void BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data = 0){}
        virtual bool BuildToolBar(wxToolBar* toolBar){ return false; }
        virtual int Execute();
    protected:
        virtual void OnAttach();
        virtual void OnRelease(bool appShutDown);

    private:
        wxMenu* m_MyMenu;
        void doSomething(wxCommandEvent& event);
        DECLARE_EVENT_TABLE();
};

#endif // MYTOOLS_H_INCLUDED
Title: Re: Linking menu event table to member function
Post by: MortenMacFly on July 11, 2012, 10:16:44 am
Works fine here.

- copy the plugin into the "contrib" plugin folder of you C::B sources
- use the project file attached, replace the ones of yours
- compile the plugin
- run the plugin.

As I said: It should work well, maybe an issue with you project setup...?!