Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Plugins development => Topic started by: LETARTARE on May 02, 2023, 11:39:12 am

Title: Project execution event
Post by: LETARTARE on May 02, 2023, 11:39:12 am
I am a third party plugin (AddonForQt :https://forums.codeblocks.org/index.php/topic,20000.msg172194.html#msg172194 (https://forums.codeblocks.org/index.php/topic,20000.msg172194.html#msg172194)
Is there a CB event that would be generated at the beginning of a project's execution that I could intercept ?
Title: Re: Project execution event
Post by: Miguel Gimenez on May 03, 2023, 12:57:05 pm
I do not think so. The complete list of events is here (https://wiki.codeblocks.org/index.php/Code::Blocks_SDK_events#List_of_Available_Code::Blocks_Events)
Title: Re: Project execution event
Post by: LETARTARE on May 03, 2023, 02:11:08 pm
Thank you.
We could create in 'sdk_event' a 'cbEVT_COMPILER_PROJECT_LAUNCHED' event, but where should we fire it ?

Code
1- 'int CompilerGCC::Run(ProjectBuildTarget* target)'
just before
Code
'return 0'

2- or
Code
'int CompilerGCC::DoRunQueue()' 
after
   
Code
'process.PID = process.pProcess->Launch(cmd->command, flags);'
but here you have to wait for the command corresponding to the launch of the current project.

Is this possible ?

Title: Re: Project execution event
Post by: Miguel Gimenez on May 03, 2023, 02:54:02 pm
Adding events used to require a solid reason, I do not know if this will be accepted.
Title: Re: Project execution event
Post by: Pecan on May 03, 2023, 09:57:12 pm
Adding events used to require a solid reason, I do not know if this will be accepted.

I've done this before, but I used the main menu events to do it by intercepting the event id for "Run" and "Build and run".
The event id(s) were obtained and bindings were set at the "OnAppStartupDone()" event. If you set your own  bindings (connects) during this event, you'll get called before the sdk does.
Be sure to "event.Skip()", else the the sdk will stall.
Title: Re: Project execution event
Post by: LETARTARE on May 05, 2023, 09:24:05 am
Thanks to both of you.
@Michel Gimenez
So far I don't have a solid reason!
@Pecan
I'll try your lead, even though I didn't get it all.
Title: Re: Project execution event
Post by: Pecan on May 05, 2023, 05:55:30 pm
@LETARTARE

Pseudo Code to listen for specific menu item selection/clicks:
Code
In OnAttach()
    Manager* pMgr = Manager::Get();
    pMgr->RegisterEventSink(cbEVT_APP_STARTUP_DONE, new cbEventFunctor<className>, CodeBlocksEvent>(this, &<className>::OnAppStartupDone));
        //Do not attempt to capture the menu items ID here. Other plugins might change the menu structure.
        // when being attached.

In OnAppStartUpDone()
    // int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString)
    // in <wx/utils.h>
    int m_RunMenuItemID = wxFindMenuItemId(pAppFrame, _("Build"), _("Run"));
    int m_BuildAndRunMenuItemID = wxFindMenuItemId(pAppFrame, _("Build"), _("Build and run"));
    if (m_RunMenuItemID !=  wxNOT_FOUND)
        Bind(wxEVT_COMMAND_MENU_SELECTED, &<classname>::SetRunEventOccured, this, runMenuItemID);
    // do like binding for buildAndRunMenuItemId or any other id's you're listening for.

In SetRunEventOccured(wxCommandEvent& event)
    event.Skip(); <--- don't forget this !!!
    // Do anything else you'd like here but remember that a
    // call back or an AddPendingEvent() is better than blocking the event processing.
    if (event.GetId() == m_RunMenuItemID)
        // do something (like set a OnCompilerFinished() event etc)
    else if (event.GtId() == m_BuildAnd RunMenuItemId)
        // do something
    else
        return;


I found that these events did not necessarily have all the info I needed (like the return code from the build). But there is a way of scraping the logs to find out.
Let me know if you need that kind of info.
Title: Re: Project execution event
Post by: LETARTARE on May 13, 2023, 09:35:23 am
Good day, I am absent.

Many thanks for this efficient and elegant solution.
I didn't know this feature existed :
       
Quote
int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString)

Here are the results in 'Addons Log' :
Code
Plateforme : 'Linux-64'-'fr_FR', sdk => '2.24.0',   'extrasforQt' version : '3.6.1', construit le '12/05/2023::19:07:47' 
    ==> Begin AddOnForQt::OnAppStartupDone(...)
        m_RunMenuItemID = -2484
        m_BuildAndRunMenuItemID = -2483
        m_AbortMenuID = -2485
    <= End AddOnForQt::OnAppStartupDone(...)

I will provide the code in another post
Title: Re: Project execution event
Post by: LETARTARE on May 13, 2023, 09:47:17 am
Here is the code of a method :
Code
/// called by
///     1. 'cbEVT_APP_STARTUP_DONE'
void AddOnForQt::OnAppStartupDone(CodeBlocksEvent& _event)
{
_print("    ==> Begin AddOnForQt::OnAppStartupDone(...)");
    // int wxFindMenuItemId(wxFrame *frame, const wxString& menuString, const wxString& itemString)
    // in <wx/utils.h>
    if (!m_pAppFrame)
    {
        _event.Skip();
        return;
    }
//
    m_RunMenuItemID = wxFindMenuItemId(m_pAppFrame, _("&Build"), _("&Run"));
_printWarn("        m_RunMenuItemID = " + iToStr(m_RunMenuItemID) );
    m_BuildAndRunMenuItemID = wxFindMenuItemId(m_pAppFrame, _("&Build"), _("Build and run"));
_printWarn("        m_BuildAndRunMenuItemID = " + iToStr(m_BuildAndRunMenuItemID) );
    m_AbortMenuID = wxFindMenuItemId(m_pAppFrame, _("&Build"), _("&Abort"));
_printWarn("        m_AbortMenuID = " + iToStr(m_AbortMenuID) );
    if (m_RunMenuItemID !=  wxNOT_FOUND)
        Bind(wxEVT_COMMAND_MENU_SELECTED, &AddOnForQt::onRunEventOccured, this, m_RunMenuItemID);
    if (m_BuildAndRunMenuItemID !=  wxNOT_FOUND)
        Bind(wxEVT_COMMAND_MENU_SELECTED, &AddOnForQt::onRunEventOccured, this, m_BuildAndRunMenuItemID);
    if (m_AbortMenuID != wxNOT_FOUND)
        Bind(wxEVT_COMMAND_MENU_SELECTED, &AddOnForQt::onRunEventOccured, this, m_AbortMenuID);
    // do like binding for buildAndRunMenuItemId or any other id's you're listening for.
/// The event processing system continues searching
_event.Skip();

_print("    <= End AddOnForQt::OnAppStartupDone(...)");
}
Title: Re: Project execution event
Post by: LETARTARE on May 13, 2023, 09:50:05 am
and the one of the 2nd :
Code
// called by :
//    AddOnForQt::OnAppStartupDone(CodeBlocksEvent& _event):1, 
void AddOnForQt::onRunEventOccured(wxCommandEvent& _event)
{
_print("    ==> Begin AddOnForQt::onRunEventOccured(" + iToStr(_event.GetId()) + ")");
/// The event processing system continues searching
     _event.Skip();

    const int id = _event.GetId() ;
    if (id == m_RunMenuItemID)
// do something (like set a OnCompilerFinished() event etc)
        _printError("       m_RunMenuItemID => " + iToStr(m_RunMenuItemID) );
    else
    if (id == m_BuildAndRunMenuItemID)
        _printError("       m_BuildAndRunMenuItemID => " + iToStr(m_BuildAndRunMenuItemID) );
    else
    if (id == m_AbortMenuID)
        _printError("       m_AbortMenuID = " + iToStr(m_AbortMenuID) );

_print("    <= End AddOnForQt::onRunEventOccured(...)");
}

Macros '_print(wxString)' and 'iToStr(int)' allow to write in 'AddonQt Log
Title: Re: Project execution event
Post by: Pecan on May 14, 2023, 06:46:13 am
@Letartare
Glad it helped you.
Good work.