Event handling

Volker Bartheld dr_versaeg at freenet.de
Sun Mar 2 02:05:27 PST 2008


Hi Igor!

> Is it possible to handle an event at the specific time?
> 5. after the first iteration of all system events -
>    main window shows up properly resised - my event
>    handler invokes.
> The thing is: I am looking at the Doc/View MDI architecture.
> What I need is when the application starts, it automatiucally opens
> first document with the view attached.

You're probably looking for a some handler like wxEVT_IDLE_ONCE
(see below for an implementation). You just need to include the
.h-file, declare a wxIdleOnceEvtHandler* m_idleonce; member, add

m_idleonce=new wxIdleOnceEvtHandler(this);

in the c'tor,

delete m_idleonce;

in the d'tor of your app and finally declare and implement the
handler, i. e. through

BEGIN_EVENT_TABLE(MyApp, wxApp)
EVT_IDLE_ONCE(MyApp::OnIdleOnce)
END_EVENT_TABLE()

and

void MyApp::OnIdleOnce(wxIdleEvent&)
{
  //...
}

But, while thinking of it...: Do you really need the feature to
wait until you app is idle for the first time after starting? If
this behaviour is unnecessary, you could just post a custom event
to your own event queue which will, when handled, open the default/
new document in your MDI app.

Does that help?

Cheers,
Volker


<idleonce.h>
#ifndef WXIDLEONCEEVTHANDLER_INCLUDED
#define WXIDLEONCEEVTHANDLER_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <wx/window.h>
#include <wx/event.h>

#define EVT_IDLE_ONCE(func) wx__DECLARE_EVT0(wxEVT_IDLE_ONCE, wxIdleEventHandler(func))
extern wxEventType wxEVT_IDLE_ONCE;

/**
* define a new handler type that will deal with the wxEVT_IDLE event when it arrives in the queue for the *FIRST* time
*/
class wxIdleOnceEvtHandler : public wxEvtHandler
{
public:
  wxIdleOnceEvtHandler(wxEvtHandler* target);
  void Stop(void);
  ~wxIdleOnceEvtHandler();
  bool ProcessEvent(wxEvent& event);
private:
  wxEvtHandler* m_target;
};

#endif // #ifndef WXIDLEONCEEVTHANDLER_INCLUDED
</idleonce.h>

<idleonce.cpp>
#include <wx/window.h>
#include <wx/event.h>

#include "wxidleonceevthandler.h"

wxEventType wxEVT_IDLE_ONCE=wxNewEventType();

/**
* define a new handler type that will deal with the wxEVT_IDLE event when it arrives in the queue for the *FIRST* time
*/
wxIdleOnceEvtHandler::wxIdleOnceEvtHandler(wxEvtHandler* target) : m_target(target), wxEvtHandler()
{
  if(wxIS_KIND_OF(m_target, wxWindow) && GetEvtHandlerEnabled()) ((wxWindow*)m_target)->PushEventHandler(this);   // push event handler into parent's list
}
void wxIdleOnceEvtHandler::Stop(void)
{
  if(wxIS_KIND_OF(m_target, wxWindow) && GetEvtHandlerEnabled()) ((wxWindow*)m_target)->RemoveEventHandler(this); // remove handler from parent's list
  SetEvtHandlerEnabled(false);
} // void wxIdleOnceEvtHandler::Stop(void)
wxIdleOnceEvtHandler::~wxIdleOnceEvtHandler() { Stop(); }
bool wxIdleOnceEvtHandler::ProcessEvent(wxEvent& event)                                 // event handle "hook"
{
  if(wxEVT_IDLE==event.GetEventType() && GetEvtHandlerEnabled())                        // is it the wxEVT_IDLE and it arrived for the first time (event handler still enabled)?
  {
    Stop();                                                                             // stop the handler
    wxEvent* temp=event.Clone();                                                        // duplicate the event
    temp->SetEventType(wxEVT_IDLE_ONCE);                                                // set correct event type
    wxPostEvent(m_target, *temp);                                                       // and reinsert it into parent's queue
    delete temp;
  } // if(wxEVT_IDLE==event.GetEventType() && GetEvtHandlerEnabled())
  return wxEvtHandler::ProcessEvent(event);
} // bool wxIdleOnceEvtHandler::ProcessEvent(wxEvent& event)
</idleonce.cpp>


-- 
mailto:  V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list