2. LAS RELACIONES PROVEEDOR-CLIENTE
2.3. En el ámbito de la gestión en la cadena de suministros
2.3.6. La nueva competencia
We have discussed event handling mostly in terms of static event tables because this is the most common way to handle events. However, you can also specify the mapping between events and their handlers at runtime. You might use this method because you want to use different event handlers at different times in your program, or because you are using a different language (such as Python) that can’t use static event tables, or simply because you prefer it. Event handlers allow greater granularity—you can turn individual event han- dlers on and off at runtime, whereas PushEventHandler and PopEventHandler deal with a whole event table. Plus, they allow sharing of event handler func- tions between different objects.
There are two functions associated with dynamic event tables: wxEvtHandler::Connectand wxEvtHandler::Disconnect. Often, you won’t need to call wxEvtHandler::Disconnectbecause the disconnection will happen when the window object is destroyed.
Let’s use our simple frame class with two event handlers as an example. // Declare our main frame class
class MyFrame : public wxFrame {
public:
// Constructor
MyFrame(const wxString& title); // Event handlers
void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); private:
};
Notice that this time, we do not use the DECLARE_EVENT_TABLEmacro. To specify event handling dynamically, we add a couple of lines to our application class’s OnInitfunction: frame->Connect( wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) ); frame->Connect( wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) );
We pass to Connect the window identifier, the event identifier, and finally a pointer to the event handler function. Note that the event identifier (wxEVT_COMMAND_MENU_SELECTED) is different from the event table macro (EVT_MENU); the event table macro makes use of the event identifier internally. The wxCommandEventHandler macro around the function name is necessary to appease the compiler—casts are done by static event table macros automati- cally. In general, if you have a handler that takes an event called wxXYZEvent, then in your Connect call, you will wrap the function name in the macro wxXYZEventHandler.
If we want to remove the mapping between event and handler function, we can use the wxEvtHandler::Disconnectfunction, as follows:
frame->Disconnect( wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) ); frame->Disconnect( wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventFunction(MyFrame::OnAbout) );
W
INDOWI
DENTIFIERSWindow identifiers are integers, and are used to uniquely determine window identity in the event system. In fact, identifiers do not need to be unique across your entire application, just unique within a particular context, such as a frame and its children. You may use the wxID_OKidentifier, for example, on any number of dialogs as long as you don’t have several within the same dialog.
If you pass wxID_ANYto a window constructor, an identifier will be gener- ated for you automatically by wxWidgets. This is useful when you don’t care about the exact identifier, either because you’re not going to process the events from the control being created at all, or because you process the events from all controls in one place. In this case, you should specify wxID_ANYin the event table or wxEvtHandler::Connect call as well. The generated identifiers are
Table 3-1 Standard Window Identifiers Identifier Name Description
wxID_ANY This may be passed to a window constructor as an
identifier, and wxWidgets will generate an appro- priate identifier
wxID_LOWEST The lowest standard identifier value (4999)
wxID_HIGHEST The highest standard identifier value (5999)
wxID_OPEN File open
wxID_CLOSE Window close
wxID_NEW New window, file or document
wxID_SAVE File save
wxID_SAVEAS File save as (prompts for a save dialog)
wxID_REVERT Revert (revert to file on disk)
wxID_EXIT Exit application
wxID_UNDO Undo the last operation
wxID_REDO Redo the last undo
wxID_HELP General help (for example, for dialog Help
buttons)
wxID_PRINT Print
wxID_PRINT_SETUP Print setup dialog
wxID_PREVIEW Print preview
wxID_ABOUT Show a dialog describing the application
wxID_HELP_CONTENTS Show the help contents
wxID_HELP_COMMANDS Show the application commands
wxID_HELP_PROCEDURES Show the application procedures
always negative, so they will never conflict with the user-specified identifiers, which must always be positive.
wxWidgets supplies the standard identifiers listed in Table 3-1. Use the standard identifiers wherever possible: some systems can use the information to provide standard graphics (such as the OKand Cancelbuttons on GTK+) or default behavior (such as responding to the Escape key by emulating a wxID_CANCEL event). On Mac OS X,wxID_ABOUT,wxID_PREFERENCESand wxID_EXIT menu items are interpreted specially and transferred to the application menu. Some wxWidgets components, such as wxTextCtrl, know how to handle menu items or buttons with identifiers such as wxID_COPY,wxID_PASTE,wxID_UNDO.
You can use wxID_HIGHESTto determine the number above which it is safe to define your own identifiers, or you can use identifiers below wxID_LOWEST.
Identifier Name Description wxID_HELP_CONTEXT Unused wxID_CUT Cut wxID_COPY Copy wxID_PASTE Paste wxID_CLEAR Clear wxID_FIND Find wxID_DUPLICATE Duplicate wxID_SELECTALL Select all
wxID_DELETE Delete (cut without copying)
wxID_REPLACE Replace
wxID_REPLACE_ALL Replace all
wxID_PROPERTIES Show properties for the selection wxID_VIEW_DETAILS View details in a list control wxID_VIEW_LARGEICONS View as large icons in a list control wxID_VIEW_SMALLICONS View as small icons in a list control wxID_VIEW_LIST View as a list in a list control wxID_VIEW_SORTDATE Sort by date
wxID_VIEW_SORTNAME Sort by name wxID_VIEW_SORTSIZE Sort by size wxID_VIEW_SORTTYPE Sort by type wxID_FILE1to wxID_FILE9 View recent file
wxID_OK Confirms dialog selections wxID_CANCEL Vetoes dialog selections wxID_APPLY Applies selections to data wxID_YES Identifier for a Yesbutton wxID_NO Identifier for a Nobutton
wxID_STATIC Identifier for static text or bitmap control wxID_FORWARD Navigate forward
wxID_BACKWARD Navigate backward wxID_DEFAULT Restore default settings
wxID_MORE View more settings
wxID_SETUP View a setup dialog
wxID_RESET Reset settings
Window Identifiers 33
Table 3-1 Standard Window Identifiers(Continued)
Identifier Name Description
wxID_CONTEXT_HELP Show context-sensitive help wxID_YESTOALL Reply yes to all prompts wxID_NOTOALL Reply no to all prompts wxID_ABORT Abort the current operation wxID_RETRY Retry the operation
wxID_IGNORE Ignore an error condition
wxID_UP Navigate up
wxID_DOWN Navigate down
wxID_HOME Navigate home
wxID_REFRESH Refresh
wxID_STOP Stop the current operation
wxID_INDEX Show an index
wxID_BOLD Highlight in bold
wxID_ITALIC Highlight in italic wxID_JUSTIFY_CENTER Center
wxID_JUSTIFY_FILL Format wxID_JUSTIFY_RIGHT Right align wxID_JUSTIFY_LEFT Left align
wxID_UNDERLINE Underline
wxID_INDENT Indent
wxID_UNINDENT Unindent
wxID_ZOOM_100 Zoom to 100% wxID_ZOOM_FIT Fit to page
wxID_ZOOM_IN Zoom in
wxID_ZOOM_OUT Zoom out
wxID_UNDELETE Undelete
wxID_REVERT_TO_SAVED Revert to saved state
34 Event Handling Chapter 3