libyui  3.10.0
YSimpleEventHandler.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YEvent.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui-events"
27 #include "YUILog.h"
28 
29 #include "YEvent.h"
30 #include "YSimpleEventHandler.h"
31 
32 
33 #define VERBOSE_EVENTS 0
34 #define VERBOSE_BLOCK 0
35 
36 
38 {
39  _pendingEvent = 0;
40  _eventsBlocked = false;
41 }
42 
43 
45 {
46  clear();
47 }
48 
49 
51 {
52  if ( _pendingEvent )
53  {
54 #if VERBOSE_EVENTS
55  yuiDebug() << "Clearing pending event: " << _pendingEvent << endl;
56 #endif
57  deleteEvent( _pendingEvent );
58  }
59 }
60 
61 
63 {
64  YEvent * event = _pendingEvent;
65  _pendingEvent = 0;
66 
67 #if VERBOSE_EVENTS
68  yuiDebug() << "Consuming " << event << endl;
69 #endif
70 
71  return event;
72 }
73 
74 
76 {
77  if ( ! event )
78  {
79  yuiError() << "Ignoring NULL event" << endl;
80  return;
81  }
82 
83  if ( eventsBlocked() )
84  {
85 #if VERBOSE_BLOCK
86  yuiDebug() << "Blocking " << event << endl;
87 #endif
88  // Avoid memory leak: The event handler assumes ownership of the newly
89  // created event, so we have to clean it up here.
90  deleteEvent( event );
91 
92  return;
93  }
94 
95  if ( _pendingEvent )
96  {
97  /**
98  * This simple event handler keeps track of only the latest user event.
99  * If there is more than one, older events are automatically discarded.
100  * Since Events are created on the heap with the "new" operator,
101  * discarded events need to be deleted.
102  *
103  * Events that are not discarded are deleted later (after they are
104  * processed) by the generic UI.
105  **/
106 
107  deleteEvent( _pendingEvent );
108  }
109 
110 #if VERBOSE_EVENTS
111  yuiDebug() << "New pending event: " << event << endl;
112 #endif
113 
114  _pendingEvent = event;
115 }
116 
117 
118 bool
120 {
121  YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
122 
123  if ( ! event )
124  return false;
125 
126  return event->widget() == widget;
127 }
128 
129 
131 {
132  if ( ! _pendingEvent )
133  return;
134 
135  YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
136 
137  if ( event && event->widget() == widget && event->isValid() )
138  {
139  yuiDebug() << "Deleting " << _pendingEvent << endl;
140  deleteEvent( _pendingEvent );
141  }
142 }
143 
144 
146 {
147 #if VERBOSE_BLOCK
148  if ( block ) yuiDebug() << "Blocking events" << endl;
149  else yuiDebug() << "Unblocking events" << endl;
150 #endif
151 
152  _eventsBlocked = block;
153 }
154 
155 
157 {
158  if ( event == _pendingEvent )
159  _pendingEvent = 0;
160 
161  if ( event )
162  {
163  if ( event->isValid() )
164  {
165 #if VERBOSE_EVENTS
166  yuiDebug() << "Deleting " << event << endl;
167 #endif
168  delete event;
169  }
170  else
171  {
172  yuiError() << "Attempt to delete invalid event " << event << endl;
173  }
174  }
175 }
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YSimpleEventHandler::eventPendingFor
bool eventPendingFor(YWidget *widget) const
Returns 'true' if there is any event pending for the specified widget.
Definition: YSimpleEventHandler.cc:119
YWidgetEvent::widget
virtual YWidget * widget() const
Returns the widget that caused this event.
Definition: YEvent.h:180
YSimpleEventHandler::deletePendingEventsFor
void deletePendingEventsFor(YWidget *widget)
Delete any pending events for the specified widget.
Definition: YSimpleEventHandler.cc:130
YSimpleEventHandler::sendEvent
void sendEvent(YEvent *event_disown)
Widget event handlers call this when an event occured that should be the answer to a UserInput() / Po...
Definition: YSimpleEventHandler.cc:75
YSimpleEventHandler::clear
void clear()
Clears any pending event (deletes the corresponding object).
Definition: YSimpleEventHandler.cc:50
YSimpleEventHandler::~YSimpleEventHandler
virtual ~YSimpleEventHandler()
Destructor.
Definition: YSimpleEventHandler.cc:44
YSimpleEventHandler::deleteEvent
void deleteEvent(YEvent *event)
Delete an event.
Definition: YSimpleEventHandler.cc:156
YEvent
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:44
YEvent::isValid
bool isValid() const
Check if this event is valid.
Definition: YEvent.cc:55
YWidget::isValid
bool isValid() const
Checks whether or not this object is valid.
Definition: YWidget.cc:244
YWidgetEvent
Definition: YEvent.h:166
YSimpleEventHandler::eventsBlocked
bool eventsBlocked() const
Returns 'true' if events are currently blocked.
Definition: YSimpleEventHandler.h:121
YSimpleEventHandler::consumePendingEvent
YEvent * consumePendingEvent()
Consumes the pending event.
Definition: YSimpleEventHandler.cc:62
YSimpleEventHandler::blockEvents
void blockEvents(bool block=true)
Block (or unblock) events.
Definition: YSimpleEventHandler.cc:145
YSimpleEventHandler::YSimpleEventHandler
YSimpleEventHandler()
Constructor.
Definition: YSimpleEventHandler.cc:37