event handling seems to work again, new sample (event) added and documented
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9242 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
13
samples/event/Makefile.in
Normal file
13
samples/event/Makefile.in
Normal file
@@ -0,0 +1,13 @@
|
||||
# Purpose: makefile for event example (UNIX).
|
||||
# Created: 2001-01-31
|
||||
|
||||
top_srcdir = @top_srcdir@/..
|
||||
top_builddir = ../..
|
||||
program_dir = samples/event
|
||||
|
||||
PROGRAM=event
|
||||
|
||||
OBJECTS=$(PROGRAM).o
|
||||
|
||||
include ../../src/makeprog.env
|
||||
|
321
samples/event/event.cpp
Normal file
321
samples/event/event.cpp
Normal file
@@ -0,0 +1,321 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: event.cpp
|
||||
// Purpose: wxWindows sample demonstrating different event usage
|
||||
// Author: Vadim Zeitlin
|
||||
// Modified by:
|
||||
// Created: 31.01.01
|
||||
// RCS-ID: $Id$
|
||||
// Copyright: (c) 2001 Vadim Zeitlin
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// ============================================================================
|
||||
// declarations
|
||||
// ============================================================================
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// headers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#ifdef __GNUG__
|
||||
// DO NOT use event.cpp here, it breaks statics initialization in
|
||||
// src/common/event.cpp and nothing works at all then!
|
||||
#pragma implementation "eventsample.cpp"
|
||||
#pragma interface "eventsample.cpp"
|
||||
#endif
|
||||
|
||||
// For compilers that support precompilation, includes "wx/wx.h".
|
||||
#include "wx/wxprec.h"
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
// for all others, include the necessary headers (this file is usually all you
|
||||
// need because it includes almost all "standard" wxWindows headers)
|
||||
#ifndef WX_PRECOMP
|
||||
#include "wx/wx.h"
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// private classes
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Define a new application type, each program should derive a class from wxApp
|
||||
class MyApp : public wxApp
|
||||
{
|
||||
public:
|
||||
// override base class virtuals
|
||||
// ----------------------------
|
||||
|
||||
// this one is called on application startup and is a good place for the app
|
||||
// initialization (doing it here and not in the ctor allows to have an error
|
||||
// return: if OnInit() returns false, the application terminates)
|
||||
virtual bool OnInit();
|
||||
};
|
||||
|
||||
// Define a new frame type: this is going to be our main frame
|
||||
class MyFrame : public wxFrame
|
||||
{
|
||||
public:
|
||||
// ctor(s)
|
||||
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
|
||||
virtual ~MyFrame();
|
||||
|
||||
void OnQuit(wxCommandEvent& event);
|
||||
void OnAbout(wxCommandEvent& event);
|
||||
void OnConnect(wxCommandEvent& event);
|
||||
void OnDynamic(wxCommandEvent& event);
|
||||
void OnPushEventHandler(wxCommandEvent& event);
|
||||
void OnPopEventHandler(wxCommandEvent& event);
|
||||
void OnTest(wxCommandEvent& event);
|
||||
|
||||
void OnUpdateUIPop(wxUpdateUIEvent& event);
|
||||
|
||||
protected:
|
||||
// number of pushed event handlers
|
||||
size_t m_nPush;
|
||||
|
||||
private:
|
||||
// any class wishing to process wxWindows events must use this macro
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
// Define a custom event handler
|
||||
class MyEvtHandler : public wxEvtHandler
|
||||
{
|
||||
public:
|
||||
MyEvtHandler(size_t level) { m_level = level; }
|
||||
|
||||
void OnTest(wxCommandEvent& event)
|
||||
{
|
||||
wxLogMessage(_T("This is the pushed test event handler #%u"), m_level);
|
||||
|
||||
// if we don't skip the event, the other event handlers won't get it:
|
||||
// try commenting out this line and see what changes
|
||||
event.Skip();
|
||||
}
|
||||
|
||||
private:
|
||||
size_t m_level;
|
||||
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// constants
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// IDs for the controls and the menu commands
|
||||
enum
|
||||
{
|
||||
// menu items
|
||||
Event_Quit = 1,
|
||||
Event_About,
|
||||
Event_Connect,
|
||||
Event_Dynamic,
|
||||
Event_Push,
|
||||
Event_Pop,
|
||||
Event_Test
|
||||
};
|
||||
|
||||
// status bar fields
|
||||
enum
|
||||
{
|
||||
Status_Main = 0,
|
||||
Status_Dynamic,
|
||||
Status_Push
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// event tables and other macros for wxWindows
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// the event tables connect the wxWindows events with the functions (event
|
||||
// handlers) which process them. It can be also done at run-time, but for the
|
||||
// simple menu events like this the static method is much simpler.
|
||||
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||
EVT_MENU(Event_Quit, MyFrame::OnQuit)
|
||||
EVT_MENU(Event_About, MyFrame::OnAbout)
|
||||
|
||||
EVT_MENU(Event_Connect, MyFrame::OnConnect)
|
||||
|
||||
EVT_MENU(Event_Test, MyFrame::OnTest)
|
||||
EVT_MENU(Event_Push, MyFrame::OnPushEventHandler)
|
||||
EVT_MENU(Event_Pop, MyFrame::OnPopEventHandler)
|
||||
|
||||
EVT_UPDATE_UI(Event_Pop, MyFrame::OnUpdateUIPop)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
BEGIN_EVENT_TABLE(MyEvtHandler, wxEvtHandler)
|
||||
EVT_MENU(Event_Test, MyEvtHandler::OnTest)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
// Create a new application object: this macro will allow wxWindows to create
|
||||
// the application object during program execution (it's better than using a
|
||||
// static object for many reasons) and also declares the accessor function
|
||||
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
|
||||
// not wxApp)
|
||||
IMPLEMENT_APP(MyApp)
|
||||
|
||||
// ============================================================================
|
||||
// implementation
|
||||
// ============================================================================
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// the application class
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// 'Main program' equivalent: the program execution "starts" here
|
||||
bool MyApp::OnInit()
|
||||
{
|
||||
// create the main application window
|
||||
MyFrame *frame = new MyFrame("Event wxWindows Sample",
|
||||
wxPoint(50, 50), wxSize(450, 340));
|
||||
|
||||
// and show it (the frames, unlike simple controls, are not shown when
|
||||
// created initially)
|
||||
frame->Show(TRUE);
|
||||
|
||||
// success: wxApp::OnRun() will be called which will enter the main message
|
||||
// loop and the application will run. If we returned FALSE here, the
|
||||
// application would exit immediately.
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// main frame
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// frame constructor
|
||||
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
|
||||
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
|
||||
{
|
||||
// create a menu bar
|
||||
wxMenu *menuFile = new wxMenu;
|
||||
|
||||
menuFile->Append(Event_About, "&About...\tCtrl-A", "Show about dialog");
|
||||
menuFile->AppendSeparator();
|
||||
menuFile->Append(Event_Quit, "E&xit\tAlt-X", "Quit this program");
|
||||
|
||||
wxMenu *menuEvent = new wxMenu;
|
||||
menuEvent->Append(Event_Connect, "&Connect\tCtrl-C",
|
||||
"Connect or disconnect the dynamic event handler",
|
||||
TRUE /* checkable */);
|
||||
menuEvent->Append(Event_Dynamic, "&Dynamic event\tCtrl-D",
|
||||
"Dynamic event sample - only works after Connect");
|
||||
menuEvent->AppendSeparator();
|
||||
menuEvent->Append(Event_Push, "&Push event handler\tCtrl-P",
|
||||
"Push event handler for test event");
|
||||
menuEvent->Append(Event_Pop, "P&op event handler\tCtrl-O",
|
||||
"Pop event handler for test event");
|
||||
menuEvent->Append(Event_Test, "Test event\tCtrl-T",
|
||||
"Test event processed by pushed event handler");
|
||||
|
||||
// now append the freshly created menu to the menu bar...
|
||||
wxMenuBar *menuBar = new wxMenuBar();
|
||||
menuBar->Append(menuFile, "&File");
|
||||
menuBar->Append(menuEvent, "&Event");
|
||||
|
||||
// ... and attach this menu bar to the frame
|
||||
SetMenuBar(menuBar);
|
||||
|
||||
#if wxUSE_STATUSBAR
|
||||
CreateStatusBar(3);
|
||||
SetStatusText("Welcome to wxWindows event sample");
|
||||
SetStatusText(_T("Dynamic: off"), Status_Dynamic);
|
||||
SetStatusText(_T("Push count: 0"), Status_Push);
|
||||
#endif // wxUSE_STATUSBAR
|
||||
}
|
||||
|
||||
MyFrame::~MyFrame()
|
||||
{
|
||||
// we must pop any remaining event handlers to avoid memory leaks and
|
||||
// crashes!
|
||||
while ( m_nPush-- != 0 )
|
||||
{
|
||||
PopEventHandler(TRUE /* delete handler */);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// standard event handlers
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
// TRUE is to force the frame to close
|
||||
Close(TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxMessageBox(_T("Event sample shows different ways of using events\n"
|
||||
"<EFBFBD> 2001 Vadim Zeitlin"),
|
||||
_T("About Event Sample"), wxOK | wxICON_INFORMATION, this);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// dynamic event handling stuff
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void MyFrame::OnDynamic(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxMessageBox(_T("This is a dynamic event handler which can be connected "
|
||||
"and disconnected during run-time."),
|
||||
_T("Dynamic Event Handler"), wxOK | wxICON_INFORMATION, this);
|
||||
}
|
||||
|
||||
void MyFrame::OnConnect(wxCommandEvent& event)
|
||||
{
|
||||
if ( event.IsChecked() )
|
||||
{
|
||||
// disconnect
|
||||
Connect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED,
|
||||
(wxObjectEventFunction)
|
||||
(wxEventFunction)
|
||||
(wxCommandEventFunction)&MyFrame::OnDynamic);
|
||||
|
||||
SetStatusText(_T("You can now use \"Dynamic\" item in the menu"));
|
||||
SetStatusText(_T("Dynamic: on"), Status_Dynamic);
|
||||
}
|
||||
else // connect
|
||||
{
|
||||
Disconnect(Event_Dynamic, -1, wxEVT_COMMAND_MENU_SELECTED);
|
||||
|
||||
SetStatusText(_T("You can no more use \"Dynamic\" item in the menu"));
|
||||
SetStatusText(_T("Dynamic: off"), Status_Dynamic);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// push/pop event handlers support
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void MyFrame::OnPushEventHandler(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
PushEventHandler(new MyEvtHandler(++m_nPush));
|
||||
|
||||
SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
|
||||
}
|
||||
|
||||
void MyFrame::OnPopEventHandler(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxCHECK_RET( m_nPush, _T("this command should be disabled!") );
|
||||
|
||||
PopEventHandler(TRUE /* delete handler */);
|
||||
m_nPush--;
|
||||
|
||||
SetStatusText(wxString::Format(_T("Push count: %u"), m_nPush), Status_Push);
|
||||
}
|
||||
|
||||
void MyFrame::OnTest(wxCommandEvent& event)
|
||||
{
|
||||
wxLogMessage(_T("This is the test event handler in the main frame"));
|
||||
}
|
||||
|
||||
void MyFrame::OnUpdateUIPop(wxUpdateUIEvent& event)
|
||||
{
|
||||
event.Enable( m_nPush > 0 );
|
||||
}
|
||||
|
10
samples/event/makefile.b32
Normal file
10
samples/event/makefile.b32
Normal file
@@ -0,0 +1,10 @@
|
||||
# Purpose: makefile for event example (BC++ 32bit)
|
||||
# Created: 2001-01-31
|
||||
|
||||
WXDIR = $(WXWIN)
|
||||
|
||||
TARGET=event
|
||||
OBJECTS = $(TARGET).obj
|
||||
|
||||
!include $(WXDIR)\src\makeprog.b32
|
||||
|
14
samples/event/makefile.bcc
Normal file
14
samples/event/makefile.bcc
Normal file
@@ -0,0 +1,14 @@
|
||||
# Purpose: makefile for event example (BC++ 16bit)
|
||||
# Created: 2001-01-31
|
||||
|
||||
!if "$(WXWIN)" == ""
|
||||
!error You must define the WXWIN variable in autoexec.bat, e.g. WXWIN=c:\wx
|
||||
!endif
|
||||
|
||||
WXDIR = $(WXWIN)
|
||||
|
||||
TARGET=event
|
||||
OBJECTS=$(TARGET).obj
|
||||
|
||||
!include $(WXDIR)\src\makeprog.bcc
|
||||
|
10
samples/event/makefile.dos
Normal file
10
samples/event/makefile.dos
Normal file
@@ -0,0 +1,10 @@
|
||||
# Purpose: makefile for event example (VC++ 1.5x)
|
||||
# Created: 2001-01-31
|
||||
|
||||
WXDIR = $(WXWIN)
|
||||
|
||||
TARGET=event
|
||||
OBJECTS=$(TARGET).obj
|
||||
|
||||
!include $(WXDIR)\src\makeprog.msc
|
||||
|
10
samples/event/makefile.g95
Normal file
10
samples/event/makefile.g95
Normal file
@@ -0,0 +1,10 @@
|
||||
# Purpose: makefile for event example (Cygwin/Mingw32)
|
||||
# Created: #03.01.00
|
||||
|
||||
WXDIR = ../..
|
||||
|
||||
TARGET=event
|
||||
OBJECTS = $(TARGET).o
|
||||
|
||||
include $(WXDIR)/src/makeprog.g95
|
||||
|
37
samples/event/makefile.sc
Normal file
37
samples/event/makefile.sc
Normal file
@@ -0,0 +1,37 @@
|
||||
# Purpose: makefile for event example (Symantec C++)
|
||||
# Created: 2001-01-31
|
||||
|
||||
WXDIR = $(WXWIN)
|
||||
WXLIB = $(WXDIR)\lib\wx.lib
|
||||
INCDIR = $(WXDIR)\include
|
||||
INCLUDE=$(INCDIR)
|
||||
TARGET=event
|
||||
|
||||
include $(WXDIR)\src\makesc.env
|
||||
|
||||
event.exe: event.obj $(DEFFILE) event.res
|
||||
*$(CC) $(LDFLAGS) -o$@ $** $(LIBS)
|
||||
*$(RC) -k event.res
|
||||
|
||||
sc32.def:
|
||||
echo EXETYPE NT > sc32.def
|
||||
echo SUBSYSTEM WINDOWS >> sc32.def
|
||||
|
||||
sc16.def:
|
||||
echo NAME $(TARGET) > sc16.def
|
||||
echo EXETYPE WINDOWS >> sc16.def
|
||||
echo STUB 'WINSTUB.EXE' >> sc16.def
|
||||
echo CODE PRELOAD MOVEABLE DISCARDABLE >> sc16.def
|
||||
echo DATA PRELOAD MOVEABLE MULTIPLE >> sc16.def
|
||||
echo HEAPSIZE 1024 >> sc16.def
|
||||
echo STACKSIZE 8192 >> sc16.def
|
||||
|
||||
clean:
|
||||
-del *.obj
|
||||
-del *.exe
|
||||
-del *.res
|
||||
-del *.map
|
||||
-del *.rws
|
||||
-del sc32.def
|
||||
-del sc16.def
|
||||
|
14
samples/event/makefile.sl
Normal file
14
samples/event/makefile.sl
Normal file
@@ -0,0 +1,14 @@
|
||||
# Purpose: makefile for event example (Salford C++)
|
||||
# Created: 2001-01-31
|
||||
|
||||
PROGRAM = event
|
||||
OBJECTS = $(PROGRAM).obj
|
||||
|
||||
include ..\..\src\makeprog.sl
|
||||
|
||||
all: wx $(TARGET)
|
||||
|
||||
wx:
|
||||
cd $(WXDIR)\src\msw ^ mk32 -f makefile.sl all
|
||||
cd $(WXDIR)\samples\event
|
||||
|
35
samples/event/makefile.twn
Normal file
35
samples/event/makefile.twn
Normal file
@@ -0,0 +1,35 @@
|
||||
# Purpose: makefile for event example (TWIN)
|
||||
# Created: 2001-01-31
|
||||
|
||||
WXDIR = ../..
|
||||
|
||||
# All common UNIX compiler flags and options are now in
|
||||
# this central makefile.
|
||||
include $(WXDIR)/src/maketwin.env
|
||||
|
||||
OBJECTS = $(OBJDIR)/event.$(OBJSUFF) $(OBJDIR)/event.$(OBJSUFF)
|
||||
|
||||
all: $(OBJDIR) event$(GUISUFFIX)$(EXESUFF)
|
||||
|
||||
wx:
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
event$(GUISUFFIX)$(EXESUFF): $(OBJECTS) $(WXLIB)
|
||||
$(CC) $(LDFLAGS) -o event$(GUISUFFIX)$(EXESUFF) $(OBJECTS) $(LDLIBS)
|
||||
|
||||
$(OBJDIR)/event.$(OBJSUFF): event.$(SRCSUFF)
|
||||
$(CC) -c $(CPPFLAGS) -o $@ event.$(SRCSUFF)
|
||||
|
||||
event.c: event.rc
|
||||
$(RESCOMP) $(RCINPUTSWITCH) event.rc $(RCOUTPUTSWITCH) event.c $(RESFLAGS)
|
||||
|
||||
$(OBJDIR)/event.$(OBJSUFF): event.c
|
||||
$(CC) -c $(CPPFLAGS) -o $@ event.c
|
||||
|
||||
#$(OBJDIR)/event.o: event.rc
|
||||
# $(RESCOMP) $(RCINPUTSWITCH) event.rc $(RCOUTPUTSWITCH) $(OBJDIR)/event.o $(RESFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJECTS) event$(GUISUFFIX).exe core *.rsc *.res
|
23
samples/event/makefile.unx
Normal file
23
samples/event/makefile.unx
Normal file
@@ -0,0 +1,23 @@
|
||||
# Purpose: makefile for event example (Unix)
|
||||
# Created: 2001-01-31
|
||||
|
||||
CC = gcc
|
||||
|
||||
PROGRAM = event
|
||||
|
||||
OBJECTS = $(PROGRAM).o
|
||||
|
||||
# implementation
|
||||
|
||||
.SUFFIXES: .o .cpp
|
||||
|
||||
.cpp.o :
|
||||
$(CC) -c `wx-config --cflags` -o $@ $<
|
||||
|
||||
all: $(PROGRAM)
|
||||
|
||||
$(PROGRAM): $(OBJECTS)
|
||||
$(CC) -o $(PROGRAM) $(OBJECTS) `wx-config --libs`
|
||||
|
||||
clean:
|
||||
rm -f *.o $(PROGRAM)
|
11
samples/event/makefile.vc
Normal file
11
samples/event/makefile.vc
Normal file
@@ -0,0 +1,11 @@
|
||||
# Purpose: makefile for event example (VC++ 32bit)
|
||||
# Created: 2001-01-31
|
||||
|
||||
# Set WXDIR for your system
|
||||
WXDIR = $(WXWIN)
|
||||
|
||||
PROGRAM=event
|
||||
OBJECTS = $(PROGRAM).obj
|
||||
|
||||
!include $(WXDIR)\src\makeprog.vc
|
||||
|
11
samples/event/makefile.wat
Normal file
11
samples/event/makefile.wat
Normal file
@@ -0,0 +1,11 @@
|
||||
# Purpose: makefile for event example (Watcom)
|
||||
# Created: 2001-01-31
|
||||
|
||||
WXDIR = $(%WXWIN)
|
||||
|
||||
PROGRAM = event
|
||||
OBJECTS = $(PROGRAM).obj
|
||||
|
||||
!include $(WXDIR)\src\makeprog.wat
|
||||
|
||||
|
Reference in New Issue
Block a user