libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YSimpleEventHandler.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YEvent.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "ui-events"
00027 #include "YUILog.h"
00028 
00029 #include "YEvent.h"
00030 #include "YSimpleEventHandler.h"
00031 
00032 
00033 
00034 #define VERBOSE_EVENTS  0
00035 #define VERBOSE_BLOCK   0
00036 
00037 
00038 YSimpleEventHandler::YSimpleEventHandler()
00039 {
00040     _pendingEvent       = 0;
00041     _eventsBlocked      = false;
00042 }
00043 
00044 
00045 YSimpleEventHandler::~YSimpleEventHandler()
00046 {
00047     clear();
00048 }
00049 
00050 
00051 void YSimpleEventHandler::clear()
00052 {
00053     if ( _pendingEvent )
00054     {
00055 #if VERBOSE_EVENTS
00056         yuiDebug() << "Clearing pending event: " << _pendingEvent << std::endl;
00057 #endif
00058         deleteEvent( _pendingEvent );
00059     }
00060 }
00061 
00062 
00063 YEvent * YSimpleEventHandler::consumePendingEvent()
00064 {
00065     YEvent * event = _pendingEvent;
00066     _pendingEvent = 0;
00067 
00068 #if VERBOSE_EVENTS
00069     yuiDebug() << "Consuming " << event << std::endl;
00070 #endif
00071 
00072     return event;
00073 }
00074 
00075 
00076 void YSimpleEventHandler::sendEvent( YEvent * event )
00077 {
00078     if ( ! event )
00079     {
00080         yuiError() << "Ignoring NULL event" << std::endl;
00081         return;
00082     }
00083 
00084     if ( eventsBlocked() )
00085     {
00086 #if VERBOSE_BLOCK
00087         yuiDebug() << "Blocking " << event << std::endl;
00088 #endif
00089         // Avoid memory leak: The event handler assumes ownership of the newly
00090         // created event, so we have to clean it up here.
00091         deleteEvent( event );
00092 
00093         return;
00094     }
00095 
00096     if ( _pendingEvent )
00097     {
00098         /**
00099          * This simple event handler keeps track of only the latest user event.
00100          * If there is more than one, older events are automatically discarded.
00101          * Since Events are created on the heap with the "new" operator,
00102          * discarded events need to be deleted.
00103          *
00104          * Events that are not discarded are deleted later (after they are
00105          * processed) by the generic UI.
00106          **/
00107 
00108         deleteEvent( _pendingEvent );
00109     }
00110 
00111 #if VERBOSE_EVENTS
00112     yuiDebug() << "New pending event: " << event << std::endl;
00113 #endif
00114 
00115     _pendingEvent = event;
00116 }
00117 
00118 
00119 bool
00120 YSimpleEventHandler::eventPendingFor( YWidget * widget ) const
00121 {
00122     YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
00123 
00124     if ( ! event )
00125         return false;
00126 
00127     return event->widget() == widget;
00128 }
00129 
00130 
00131 void YSimpleEventHandler::deletePendingEventsFor( YWidget * widget )
00132 {
00133     if ( ! _pendingEvent )
00134         return;
00135 
00136     YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
00137 
00138     if ( event && event->widget() == widget && event->isValid() )
00139     {
00140         yuiDebug() << "Deleting " << _pendingEvent << std::endl;
00141         deleteEvent( _pendingEvent );
00142     }
00143 }
00144 
00145 
00146 void YSimpleEventHandler::blockEvents( bool block )
00147 {
00148 #if VERBOSE_BLOCK
00149     if ( block )        yuiDebug() << "Blocking events"   << std::endl;
00150     else                yuiDebug() << "Unblocking events" << std::endl;
00151 #endif
00152 
00153     _eventsBlocked = block;
00154 }
00155 
00156 
00157 void YSimpleEventHandler::deleteEvent( YEvent * event )
00158 {
00159     if ( event == _pendingEvent )
00160         _pendingEvent = 0;
00161 
00162     if ( event )
00163     {
00164         if ( event->isValid() )
00165         {
00166 #if VERBOSE_EVENTS
00167             yuiDebug() << "Deleting " << event << std::endl;
00168 #endif
00169             delete event;
00170         }
00171         else
00172         {
00173             yuiError() << "Attempt to delete invalid event " << event << std::endl;
00174         }
00175     }
00176 }
 All Classes Functions Variables Enumerations Friends