1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, Haiku 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: MessageEvent.cpp 23 // Author: Ingo Weinhold (bonefish@users.sf.net) 24 // YellowBites (http://www.yellowbites.com) 25 // Description: A special event that sends a message when executed. 26 //------------------------------------------------------------------------------ 27 28 #include <Handler.h> 29 #include <Looper.h> 30 31 #include "MessageEvent.h" 32 33 /*! \class MessageEvent 34 \brief A special event that sends a message when executed. 35 36 The constructors set the "auto delete" flag to \c true by default. It can 37 be changed by SetAutoDelete(), but note, that the object's creator is 38 responsible for its destruction then. 39 */ 40 41 /*! \var BMessage MessageEvent::fMessage 42 \brief Message to be sent. 43 */ 44 45 /*! \var BMessenger MessageEvent::fMessenger 46 \brief Message target. 47 48 Valid only, if \a fHandler is \c NULL. 49 */ 50 51 /*! \var BHandler *MessageEvent::fHandler 52 \brief Message target. 53 54 May be \c NULL, then \a fMessenger specifies the message target. 55 */ 56 57 58 // constructor 59 /*! \brief Creates a new MessageEvent with a target BHandler and a message 60 command. 61 62 \note The supplied BHandler must be valid the whole life time of the 63 MessageEvent. 64 65 \param time The time at which the message shall be sent. 66 \param handler The BHandler to which the message shall be delivered. 67 \param command The "what" field of the message to be sent. 68 */ 69 MessageEvent::MessageEvent(bigtime_t time, BHandler* handler, uint32 command) 70 : Event(time, true), 71 fMessage(command), 72 fMessenger(), 73 fHandler(handler) 74 { 75 } 76 77 // constructor 78 /*! \brief Creates a new MessageEvent with a target BHandler and a message. 79 80 The caller retains ownership of the supplied message. It can savely be 81 deleted after the constructor returns. 82 83 \note The supplied BHandler must be valid the whole life time of the 84 MessageEvent. 85 86 \param time The time at which the message shall be sent. 87 \param handler The BHandler to which the message shall be delivered. 88 \param message The message to be sent. 89 */ 90 MessageEvent::MessageEvent(bigtime_t time, BHandler* handler, 91 const BMessage *message) 92 : Event(time, true), 93 fMessage(*message), 94 fMessenger(), 95 fHandler(handler) 96 { 97 } 98 99 // constructor 100 /*! \brief Creates a new MessageEvent with a target BMessenger and a message 101 command. 102 \param time The time at which the message shall be sent. 103 \param messenger The BMessenger specifying the target to which the message 104 shall be delivered. 105 \param command The "what" field of the message to be sent. 106 */ 107 MessageEvent::MessageEvent(bigtime_t time, const BMessenger& messenger, 108 uint32 command) 109 : Event(time, true), 110 fMessage(command), 111 fMessenger(messenger), 112 fHandler(NULL) 113 { 114 } 115 116 // constructor 117 /*! \brief Creates a new MessageEvent with a target BMessenger and a message. 118 119 The caller retains ownership of the supplied message. It can savely be 120 deleted after the constructor returns. 121 122 \param time The time at which the message shall be sent. 123 \param messenger The BMessenger specifying the target to which the message 124 shall be delivered. 125 \param message The message to be sent. 126 */ 127 MessageEvent::MessageEvent(bigtime_t time, const BMessenger& messenger, 128 const BMessage *message) 129 : Event(time, true), 130 fMessage(*message), 131 fMessenger(messenger), 132 fHandler(NULL) 133 { 134 } 135 136 // destructor 137 /*! \brief Frees all resources associated with the object. 138 */ 139 MessageEvent::~MessageEvent() 140 { 141 } 142 143 // Do 144 /*! \brief Hook method invoked when the event is executed. 145 146 Implements Event. Delivers the message to the target. 147 148 \param queue The event queue executing the event. 149 \return \c true, if the object shall be deleted, \c false otherwise. 150 */ 151 bool 152 MessageEvent::Do(EventQueue *queue) 153 { 154 if (fHandler) { 155 if (BLooper* looper = fHandler->Looper()) 156 looper->PostMessage(&fMessage, fHandler); 157 } else 158 fMessenger.SendMessage(&fMessage); 159 return false; 160 } 161 162