Version: 3.1.0
wxCloseEvent Class Reference

#include <wx/event.h>

+ Inheritance diagram for wxCloseEvent:

Detailed Description

This event class contains information about window and session close events.

The handler function for EVT_CLOSE is called when the user has tried to close a a frame or dialog box using the window manager (X) or system menu (Windows). It can also be invoked by the application itself programmatically, for example by calling the wxWindow::Close function.

You should check whether the application is forcing the deletion of the window using wxCloseEvent::CanVeto. If this is false, you must destroy the window using wxWindow::Destroy.

If the return value is true, it is up to you whether you respond by destroying the window.

If you don't destroy the window, you should call wxCloseEvent::Veto to let the calling code know that you did not destroy the window. This allows the wxWindow::Close function to return true or false depending on whether the close instruction was honoured or not.

Example of a wxCloseEvent handler:

void MyFrame::OnClose(wxCloseEvent& event)
{
if ( event.CanVeto() && m_bFileNotSaved )
{
if ( wxMessageBox("The file has not been saved... continue closing?",
"Please confirm",
{
event.Veto();
return;
}
}
Destroy(); // you may also do: event.Skip();
// since the default event handler does call Destroy(), too
}

The EVT_END_SESSION event is slightly different as it is sent by the system when the user session is ending (e.g. because of log out or shutdown) and so all windows are being forcefully closed. At least under MSW, after the handler for this event is executed the program is simply killed by the system. Because of this, the default handler for this event provided by wxWidgets calls all the usual cleanup code (including wxApp::OnExit()) so that it could still be executed and exit()s the process itself, without waiting for being killed. If this behaviour is for some reason undesirable, make sure that you define a handler for this event in your wxApp-derived class and do not call event.Skip() in it (but be aware that the system will still kill your application).

Events using this class

The following event handler macros redirect the events to member function handlers 'func' with prototypes like:

void handlerFuncName(wxCloseEvent& event)

Event macros:

  • EVT_CLOSE(func):
    Process a wxEVT_CLOSE_WINDOW command event, supplying the member function. This event applies to wxFrame and wxDialog classes.
  • EVT_QUERY_END_SESSION(func):
    Process a wxEVT_QUERY_END_SESSION session event, supplying the member function. This event can be handled in wxApp-derived class only.
  • EVT_END_SESSION(func):
    Process a wxEVT_END_SESSION session event, supplying the member function. This event can be handled in wxApp-derived class only.

Library:  wxCore
Category:  Events
See Also
wxWindow::Close, Window Deletion

Public Member Functions

 wxCloseEvent (wxEventType commandEventType=wxEVT_NULL, int id=0)
 Constructor.
 
bool CanVeto () const
 Returns true if you can veto a system shutdown or a window close event.
 
bool GetLoggingOff () const
 Returns true if the user is just logging off or false if the system is shutting down.
 
void SetCanVeto (bool canVeto)
 Sets the 'can veto' flag.
 
void SetLoggingOff (bool loggingOff)
 Sets the 'logging off' flag.
 
void Veto (bool veto=true)
 Call this from your event handler to veto a system shutdown or to signal to the calling application that a window close did not happen.
 
bool GetVeto () const
 Returns whether the Veto flag was set.
 
- Public Member Functions inherited from wxEvent
 wxEvent (int id=0, wxEventType eventType=wxEVT_NULL)
 Constructor.
 
virtual wxEventClone () const =0
 Returns a copy of the event.
 
wxObjectGetEventObject () const
 Returns the object (usually a window) associated with the event, if any.
 
wxEventType GetEventType () const
 Returns the identifier of the given event type, such as wxEVT_BUTTON.
 
virtual wxEventCategory GetEventCategory () const
 Returns a generic category for this event.
 
int GetId () const
 Returns the identifier associated with this event, such as a button command id.
 
wxObjectGetEventUserData () const
 Return the user data associated with a dynamically connected event handler.
 
bool GetSkipped () const
 Returns true if the event handler should be skipped, false otherwise.
 
long GetTimestamp () const
 Gets the timestamp for the event.
 
bool IsCommandEvent () const
 Returns true if the event is or is derived from wxCommandEvent else it returns false.
 
void ResumePropagation (int propagationLevel)
 Sets the propagation level to the given value (for example returned from an earlier call to wxEvent::StopPropagation).
 
void SetEventObject (wxObject *object)
 Sets the originating object.
 
void SetEventType (wxEventType type)
 Sets the event type.
 
void SetId (int id)
 Sets the identifier associated with this event, such as a button command id.
 
void SetTimestamp (long timeStamp=0)
 Sets the timestamp for the event.
 
bool ShouldPropagate () const
 Test if this event should be propagated or not, i.e. if the propagation level is currently greater than 0.
 
void Skip (bool skip=true)
 This method can be used inside an event handler to control whether further event handlers bound to this event will be called after the current one returns.
 
int StopPropagation ()
 Stop the event from propagating to its parent window.
 
- Public Member Functions inherited from wxObject
 wxObject ()
 Default ctor; initializes to NULL the internal reference data.
 
 wxObject (const wxObject &other)
 Copy ctor.
 
virtual ~wxObject ()
 Destructor.
 
virtual wxClassInfoGetClassInfo () const
 This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar).
 
wxObjectRefDataGetRefData () const
 Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
 
bool IsKindOf (const wxClassInfo *info) const
 Determines whether this class is a subclass of (or the same class as) the given class.
 
bool IsSameAs (const wxObject &obj) const
 Returns true if this object has the same data pointer as obj.
 
void Ref (const wxObject &clone)
 Makes this object refer to the data in clone.
 
void SetRefData (wxObjectRefData *data)
 Sets the wxObject::m_refData pointer.
 
void UnRef ()
 Decrements the reference count in the associated data, and if it is zero, deletes the data.
 
void UnShare ()
 This is the same of AllocExclusive() but this method is public.
 
void operator delete (void *buf)
 The delete operator is defined for debugging versions of the library only, when the identifier WXDEBUG is defined.
 
void * operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
 The new operator is defined for debugging versions of the library only, when the identifier WXDEBUG is defined.
 

Additional Inherited Members

- Protected Member Functions inherited from wxObject
void AllocExclusive ()
 Ensure that this object's data is not shared with any other object.
 
virtual wxObjectRefDataCreateRefData () const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and returns it.
 
virtual wxObjectRefDataCloneRefData (const wxObjectRefData *data) const
 Creates a new instance of the wxObjectRefData-derived class specific to this object and initializes it copying data.
 
- Protected Attributes inherited from wxEvent
int m_propagationLevel
 Indicates how many levels the event can propagate.
 

Constructor & Destructor Documentation

wxCloseEvent::wxCloseEvent ( wxEventType  commandEventType = wxEVT_NULL,
int  id = 0 
)

Constructor.

Member Function Documentation

bool wxCloseEvent::CanVeto ( ) const

Returns true if you can veto a system shutdown or a window close event.

Vetoing a window close event is not possible if the calling code wishes to force the application to exit, and so this function must be called to check this.

bool wxCloseEvent::GetLoggingOff ( ) const

Returns true if the user is just logging off or false if the system is shutting down.

This method can only be called for end session and query end session events, it doesn't make sense for close window event.

bool wxCloseEvent::GetVeto ( ) const

Returns whether the Veto flag was set.

void wxCloseEvent::SetCanVeto ( bool  canVeto)

Sets the 'can veto' flag.

void wxCloseEvent::SetLoggingOff ( bool  loggingOff)

Sets the 'logging off' flag.

void wxCloseEvent::Veto ( bool  veto = true)

Call this from your event handler to veto a system shutdown or to signal to the calling application that a window close did not happen.

You can only veto a shutdown if CanVeto() returns true.