commands.h
上传用户:hzhsqp
上传日期:2007-01-06
资源大小:1600k
文件大小:14k
- /*
- * commands.h
- *
- * User command processing classes.
- *
- * Portable Windows Library
- *
- * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
- *
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- *
- * The Original Code is Portable Windows Library.
- *
- * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
- *
- * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
- * All Rights Reserved.
- *
- * Contributor(s): ______________________________________.
- *
- * $Log: commands.h,v $
- * Revision 1.11 1999/03/10 03:49:51 robertj
- * More documentation adjustments.
- *
- * Revision 1.10 1999/03/09 08:01:48 robertj
- * Changed comments for doc++ support (more to come).
- *
- * Revision 1.9 1999/02/16 08:08:45 robertj
- * MSVC 6.0 compatibility changes.
- *
- * Revision 1.8 1998/10/15 05:41:32 robertj
- * New memory leak check code.
- *
- * Revision 1.7 1998/09/23 06:23:11 robertj
- * Added open source copyright license.
- *
- * Revision 1.6 1997/07/08 13:02:07 robertj
- * DLL support.
- *
- * Revision 1.5 1996/08/17 10:00:30 robertj
- * Changes for Windows DLL support.
- *
- * Revision 1.4 1995/10/14 14:54:37 robertj
- * Fixed bug in command value callback.
- *
- * Revision 1.3 1995/06/17 11:12:25 robertj
- * Documentation update.
- *
- * Revision 1.2 1995/03/14 12:41:11 robertj
- * Updated documentation to use HTML codes.
- *
- * Revision 1.1 1995/02/21 11:24:57 robertj
- * Initial revision
- *
- */
- #ifndef _PCOMMANDS
- #define _PCOMMANDS
- #ifdef __GNUC__
- #pragma interface
- #endif
- ///////////////////////////////////////////////////////////////////////////////
- // PCommandSink
- /**This class represents a command sink. This works in conjunction with the
- Ref{PCommandSource} and Ref{PCommandManager} classes to implement
- dynamically bound commands on Ref{PTopLevelWindow} or Ref{PMDIDocWindow}
- classes.
- The user does not directly create descendents of this abstract class. These
- are created by a series of macros that are used in the declaration of the
- users Ref{PTopLevelWindow} or Ref{PMDIDocWindow} class descendent.
- To create a command sink, use one of the following macros:
- Ref{PDECLARE_COMMAND_X}
- Ref{PDECLARE_COMMAND_XE}
- Ref{PDECLARE_COMMAND_XS}
- Ref{PDECLARE_COMMAND_XES}
- Ref{PDECLARE_COMMAND}
- Ref{PDECLARE_COMMAND_ENABLE}
- Ref{PDECLARE_COMMAND_VALUE}
- Ref{PDECLARE_COMMAND_FULL}
- */
- class PCommandSink : public PObject
- {
- PCLASSINFO(PCommandSink, PObject);
- public:
- /**This constructor is used for instances created by the declaration macros
- within the window class. There is actually no instance data it is merely
- a place holder for the virtual function call.
- */
- PCommandSink() { }
- /**Execute the command. The overridden version of this function will
- execute the member function on the #wind# parameter. The
- override and code to do this is generated by the command declaration
- macros.
- */
- virtual void Execute(
- PTitledWindow * wind /// Window instance to receive the command.
- ) const = 0;
- /**Determine the commands enable state. The overridden version of this
- function will execute the member function on the #wind#
- parameter. The override and code to do this is generated by the command
- declaration macros.
- */
- virtual BOOL Enabled(
- PTitledWindow * wind /// Window instance to receive the command.
- ) const;
- /**Determine the commands value state. The overridden version of this
- function will execute the member function on the #wind#
- parameter. The override and code to do this is generated by the command
- declaration macros.
- */
- virtual PINDEX Value(
- PTitledWindow * wind /// Window instance to receive the command.
- ) const;
- protected:
- /**Register the command name which is attached to the specific window
- class. This allows the same command, eg "save", to have a different
- implementation depending on the class of the window, eg when several
- different types of MDI child window are used.
- */
- PCommandSink(
- const char * className,
- /**Name of the window class that the command sink descendent was
- declared within.
- */
- const char * commandName /// Arbitrary name of the command.
- );
- };
- #define PDECLARE_COMMAND_START(name, cls, func)
- class func##_PCommand : public PCommandSink {
- public:
- func##_PCommand()
- { PNEW func##_PCommand(cls::Class(), name); }
- func##_PCommand(const char * className, const char * commandName)
- : PCommandSink(className, commandName) { }
- virtual void Execute(PTitledWindow * wind) const
- { ((cls*)wind)->func(); }
- #define PDECLARE_COMMAND_END(func)
- } func##_PCommandInstance;
- friend class func##_PCommand
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version only allows the command to be executed. It is assumed that the
- command is always enabled and has a value state of zero.
-
- This version also assumes the member function #func# is declared
- elsewhere.
- */
- #define PDECLARE_COMMAND_X(name, cls, func)
- PDECLARE_COMMAND_START(name, cls, func)
- PDECLARE_COMMAND_END(func)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed and use a function to
- detemine if it is currently enabled. It is assumed that the command always
- has a value state of zero.
-
- This version also assumes the member functions #func# and
- #enab# are declared elsewhere.
- */
- #define PDECLARE_COMMAND_XE(name, cls, func, enab)
- PDECLARE_COMMAND_START(name, cls, func)
- virtual BOOL Enabled(PTitledWindow * wind) const
- { return ((cls*)wind)->enab(); }
- PDECLARE_COMMAND_END(func)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed and use a function to
- detemine its current value state. It is assumed that the command is always
- enabled.
-
- This version also assumes the member functions #func# and
- #val# are declared elsewhere.
- */
- #define PDECLARE_COMMAND_XS(name, cls, func, val)
- PDECLARE_COMMAND_START(name, cls, func)
- virtual PINDEX Value(PTitledWindow * wind) const
- { return ((cls*)wind)->val(); }
- PDECLARE_COMMAND_END(func)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed, use a function to detemine if
- it is enabled and use a function to detemine its current value state.
-
- This version also assumes the member functions #func#,
- #enab# and #val# are declared elsewhere.
- */
- #define PDECLARE_COMMAND_XES(name, cls, func, enab, val)
- PDECLARE_COMMAND_START(name, cls, func)
- virtual BOOL Enabled(PTitledWindow * wind) const
- { return ((cls*)wind)->enab(); }
- virtual PINDEX Value(PTitledWindow * wind) const
- { return ((cls*)wind)->val(); }
- PDECLARE_COMMAND_END(func)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version only allows the command to be executed. It is assumed that the
- command is always enabled and has a value state of zero.
-
- This version also declares the member function #func#.
- */
- #define PDECLARE_COMMAND(name, cls, func)
- virtual void func();
- PDECLARE_COMMAND_X(name, cls, func)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed, and use a function to
- detemine if it is currently enabled. It is assumed that the command always
- has a value state of zero.
- This version also declares the member functions #func# and
- #enab#.
- */
- #define PDECLARE_COMMAND_ENABLE(name, cls, func, enab)
- virtual void func();
- virtual BOOL enab();
- PDECLARE_COMMAND_XE(name, cls, func, enab)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed and use a function to
- detemine its current value state. It is assumed that the command is always
- enabled.
-
- This version also declares the member functions #func# and
- #val#.
- */
- #define PDECLARE_COMMAND_VALUE(name, cls, func, val)
- virtual void func();
- virtual PINDEX val();
- PDECLARE_COMMAND_XS(name, cls, func, val)
- /**Declare a command.
- This macro declares a descendent class of Ref{PCommandSink} which is
- attached to a Ref{PTopLevelWindow} or Ref{PMDIDocWindow} instance.
- This version allows the command to be executed, use a function to detemine if
- it is enabled and use a function to detemine its current value state.
-
- This version also declares the member functions #func#,
- #enab# and #val#.
- */
- #define PDECLARE_COMMAND_FULL(name, cls, func, enab, val)
- virtual void func();
- virtual BOOL enab();
- virtual PINDEX val();
- PDECLARE_COMMAND_XES(name, cls, func, enab, val)
- ///////////////////////////////////////////////////////////////////////////////
- // PCommandSource
- /**This class represents a command source. This works in conjunction with the
- Ref{PCommandSink} and Ref{PCommandManager} classes to implement
- dynamically bound commands on Ref{PTopLevelWindow} or Ref{PMDIDocWindow}
- classes.
- The user does not directly create descendents of this class. Instances of
- this class are created by the Ref{PCREATE_COMMAND} macro. These are
- instances of the Ref{PNotifier} class and may be attached to
- Ref{PMenuItem} or Ref{PControl} instances via the
- Ref{PControl::SetNotifier()} function in the same way as other callback
- functions.
- */
- class PCommandSource : public PNotifierFunction
- {
- PCLASSINFO(PCommandSource, PNotifierFunction);
- public:
- /**Create a command source notifier that will connect a Ref{PCommandSink}
- using the same command name string.
- */
- PCommandSource(
- const char * commandName /// Name of the command to call.
- );
- /**Notifier class callback function. This will connect to the
- Ref{PCommandSink} of the same command name by using the information in
- the Ref{PCommandManager} instance contained in the Ref{PApplication}
- descendent instance.
- The appropriate callback function, execute, enable or value is called
- according to the value of the #extra# field.
- */
- virtual void Call(
- PObject & notifier, /// Object executing the notification.
- INT extra /// Extra information about the notification.
- ) const;
- protected:
- /** Name of the command to execute. */
- PString name;
- };
- /**Create command instance.
- This macro creates an instance of the Ref{PCommandSource} smart pointer.
- This may be passed to any command source, eg menu item, which accepts a
- Ref{PNotifier} instance.
- */
- #define PCREATE_COMMAND(name) PNotifier(new PCommandSource(name))
- ///////////////////////////////////////////////////////////////////////////////
- // PCommandManager
- /**This class represents a manager for command sources and sinks. This works in
- conjunction with the Ref{PCommandSink} and Ref{PCommandSource} classes
- to implement dynamically bound commands on Ref{PTopLevelWindow} or
- Ref{PMDIDocWindow} classes.
- The Ref{PApplication} class contains a single instance of this class. This
- will contain all of the registered command sinks. When a command source
- executes a command, this database is consulted to locate the command sink
- that will actually execute the command functions.
- */
- class PCommandManager : public PObject
- {
- PCLASSINFO(PCommandManager, PObject);
- public:
- /** Register the command sink. */
- void Register(
- const char * className,
- /**Name of the Ref{PTitledWindow} class the Ref{PCommandSink} is
- declared in.
- */
- const char * commandName, /// Name of the command.
- PCommandSink * sink
- );
- /**Locate the command sink that was registered with the specified
- attributes.
- */
- PCommandSink * Find(
- const PString & className,
- /**Name of the Ref{PTitledWindow} class the Ref{PCommandSink} is
- declared in.
- */
- const PString & commandName /// Name of the command.
- );
- private:
- PDICTIONARY(CommandsByName, PString, PCommandSink);
- PDICTIONARY(CommandsByClass, PString, CommandsByName);
- CommandsByClass commands;
- };
- #endif /// _PCOMMANDS
- // End Of File ///////////////////////////////////////////////////////////////