libyui  3.4.2
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 
34 #define VERBOSE_EVENTS 0
35 #define VERBOSE_BLOCK 0
36 
37 
39 {
40  _pendingEvent = 0;
41  _eventsBlocked = false;
42 }
43 
44 
46 {
47  clear();
48 }
49 
50 
52 {
53  if ( _pendingEvent )
54  {
55 #if VERBOSE_EVENTS
56  yuiDebug() << "Clearing pending event: " << _pendingEvent << std::endl;
57 #endif
58  deleteEvent( _pendingEvent );
59  }
60 }
61 
62 
64 {
65  YEvent * event = _pendingEvent;
66  _pendingEvent = 0;
67 
68 #if VERBOSE_EVENTS
69  yuiDebug() << "Consuming " << event << std::endl;
70 #endif
71 
72  return event;
73 }
74 
75 
77 {
78  if ( ! event )
79  {
80  yuiError() << "Ignoring NULL event" << std::endl;
81  return;
82  }
83 
84  if ( eventsBlocked() )
85  {
86 #if VERBOSE_BLOCK
87  yuiDebug() << "Blocking " << event << std::endl;
88 #endif
89  // Avoid memory leak: The event handler assumes ownership of the newly
90  // created event, so we have to clean it up here.
91  deleteEvent( event );
92 
93  return;
94  }
95 
96  if ( _pendingEvent )
97  {
98  /**
99  * This simple event handler keeps track of only the latest user event.
100  * If there is more than one, older events are automatically discarded.
101  * Since Events are created on the heap with the "new" operator,
102  * discarded events need to be deleted.
103  *
104  * Events that are not discarded are deleted later (after they are
105  * processed) by the generic UI.
106  **/
107 
108  deleteEvent( _pendingEvent );
109  }
110 
111 #if VERBOSE_EVENTS
112  yuiDebug() << "New pending event: " << event << std::endl;
113 #endif
114 
115  _pendingEvent = event;
116 }
117 
118 
119 bool
121 {
122  YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
123 
124  if ( ! event )
125  return false;
126 
127  return event->widget() == widget;
128 }
129 
130 
132 {
133  if ( ! _pendingEvent )
134  return;
135 
136  YWidgetEvent * event = dynamic_cast<YWidgetEvent *> (_pendingEvent);
137 
138  if ( event && event->widget() == widget && event->isValid() )
139  {
140  yuiDebug() << "Deleting " << _pendingEvent << std::endl;
141  deleteEvent( _pendingEvent );
142  }
143 }
144 
145 
147 {
148 #if VERBOSE_BLOCK
149  if ( block ) yuiDebug() << "Blocking events" << std::endl;
150  else yuiDebug() << "Unblocking events" << std::endl;
151 #endif
152 
153  _eventsBlocked = block;
154 }
155 
156 
158 {
159  if ( event == _pendingEvent )
160  _pendingEvent = 0;
161 
162  if ( event )
163  {
164  if ( event->isValid() )
165  {
166 #if VERBOSE_EVENTS
167  yuiDebug() << "Deleting " << event << std::endl;
168 #endif
169  delete event;
170  }
171  else
172  {
173  yuiError() << "Attempt to delete invalid event " << event << std::endl;
174  }
175  }
176 }
virtual ~YSimpleEventHandler()
Destructor.
YEvent * consumePendingEvent()
Consumes the pending event.
bool isValid() const
Check if this event is valid.
Definition: YEvent.cc:53
bool eventPendingFor(YWidget *widget) const
Returns &#39;true&#39; if there is any event pending for the specified widget.
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:43
bool isValid() const
Checks whether or not this object is valid.
Definition: YWidget.cc:242
void sendEvent(YEvent *event_disown)
Widget event handlers call this when an event occured that should be the answer to a UserInput() / Po...
bool eventsBlocked() const
Returns &#39;true&#39; if events are currently blocked.
void clear()
Clears any pending event (deletes the corresponding object).
void deleteEvent(YEvent *event)
Delete an event.
void blockEvents(bool block=true)
Block (or unblock) events.
YSimpleEventHandler()
Constructor.
void deletePendingEventsFor(YWidget *widget)
Delete any pending events for the specified widget.
Abstract base class of all UI widgets.
Definition: YWidget.h:54
virtual YWidget * widget() const
Returns the widget that caused this event.
Definition: YEvent.h:180