libyui  3.3.1
YEvent.h
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.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YEvent_h
26 #define YEvent_h
27 
28 
29 #include <string>
30 #include <iosfwd>
31 #include "YDialog.h"
32 #include "YSimpleEventHandler.h"
33 
34 class YWidget;
35 class YItem;
36 class YDialog;
37 
38 
39 /**
40  * Abstract base class for events to be returned upon UI::UserInput()
41  * and related functions.
42  **/
43 class YEvent
44 {
45 public:
46 
47  enum EventType
48  {
49  NoEvent = 0,
50  UnknownEvent,
51  WidgetEvent,
52  MenuEvent,
53  KeyEvent,
54  CancelEvent,
55  TimeoutEvent,
56  DebugEvent,
57  InvalidEvent = 0x4242
58  };
59 
60 
61  enum EventReason
62  {
63  UnknownReason = 0,
64  Activated,
65  SelectionChanged,
66  ValueChanged,
67  ContextMenuActivated
68  };
69 
70 
71  /**
72  * Constructor.
73  **/
74  YEvent( EventType eventType = UnknownEvent );
75 
76  /**
77  * Returns the event type.
78  **/
79  EventType eventType() const { return _eventType; }
80 
81  /**
82  * Returns the unique serial no. of this event.
83  * This is mainly useful for debugging.
84  **/
85  unsigned long serial() const { return _serial; }
86 
87  /**
88  * Returns the widget that caused this event or 0 if there is none.
89  *
90  * This default implementation always returns 0.
91  * Subclasses that actually return widgets should overwrite this method.
92  **/
93  virtual YWidget * widget() const { return 0; }
94 
95  /**
96  * Return the YItem that corresponds to this event or 0 if there is none.
97  *
98  * This default implementation always returns 0.
99  * Subclasses that actually return items should overwrite this method.
100  **/
101  virtual YItem * item() const { return 0; }
102 
103  /**
104  * Return the dialog this event belongs to or 0 if no dialog was set yet.
105  **/
106  YDialog * dialog() const { return _dialog; }
107 
108  /**
109  * Check if this event is valid. Events become invalid in the destructor.
110  **/
111  bool isValid() const;
112 
113  /**
114  * Returns the character representation of an event type.
115  **/
116  static const char * toString( EventType eventType );
117 
118  /**
119  * Returns the character representation of an event reason.
120  **/
121  static const char * toString( EventReason reason );
122 
123 
124 protected:
125 
126  /**
127  * Set the dialog this event belongs to.
128  **/
129  void setDialog( YDialog * dia ) { _dialog = dia; }
130 
131  /**
132  * Protected destructor - events can only be deleted via
133  * YDialog::deleteEvent(). The associated dialog will take care of this
134  * event and delete it when appropriate.
135  *
136  * This desctructor is virtual to force a polymorph object
137  * so dynamic_cast<> can be used.
138  **/
139  virtual ~YEvent();
140 
141  /**
142  * Mark this event as invalid. This cannot be undone.
143  **/
144  void invalidate();
145 
146 private:
147 
148  friend void YDialog::deleteEvent( YEvent * event );
149  friend void YSimpleEventHandler::deleteEvent( YEvent * event );
150 
151 
152  //
153  // Data members
154  //
155 
156  EventType _eventType;
157  unsigned long _serial;
158  YDialog * _dialog;
159 
160  static unsigned long _nextSerial;
161 };
162 
163 
164 
165 class YWidgetEvent: public YEvent
166 {
167 public:
168 
169  /**
170  * Constructor.
171  **/
172  YWidgetEvent( YWidget * widget = 0,
173  EventReason reason = Activated,
174  EventType eventType = WidgetEvent );
175 
176  /**
177  * Returns the widget that caused this event.
178  * Reimplemented from YEvent.
179  **/
180  virtual YWidget * widget() const { return _widget; }
181 
182  /**
183  * Returns the reason for this event. This very much like an event sub-type.
184  **/
185  EventReason reason() const { return _reason; }
186 
187 protected:
188 
189  /**
190  * Protected destructor - events can only be deleted via
191  * YDialog::deleteEvent(). The associated dialog will take care of this
192  * event and delete it when appropriate.
193  **/
194  virtual ~YWidgetEvent() {}
195 
196 
197  //
198  // Data members
199  //
200 
201  YWidget * _widget;
202  EventReason _reason;
203 };
204 
205 
206 class YKeyEvent: public YEvent
207 {
208 public:
209 
210  /**
211  * Constructor.
212  *
213  * Create a key event with a specified key symbol (a text describing the
214  * key, such as "CursorLeft", "F1", etc.) and optionally the widget that
215  * currently has the keyboard focus.
216  **/
217  YKeyEvent( const std::string & keySymbol,
218  YWidget * focusWidget = 0 );
219 
220  /**
221  * Returns the key symbol - a text describing the
222  * key, such as "CursorLeft", "F1", "a", "A", etc.
223  **/
224  std::string keySymbol() const { return _keySymbol; }
225 
226  /**
227  * Returns the widget that currently has the keyboard focus.
228  *
229  * This might be 0 if no widget has the focus or if the creator of
230  * this event could not obtain that information.
231  **/
232  YWidget * focusWidget() const { return _focusWidget; }
233 
234 protected:
235 
236  /**
237  * Protected destructor - events can only be deleted via
238  * YDialog::deleteEvent(). The associated dialog will take care of this
239  * event and delete it when appropriate.
240  **/
241  virtual ~YKeyEvent() {}
242 
243 
244  //
245  // Data members
246  //
247 
248  std::string _keySymbol;
249  YWidget * _focusWidget;
250 };
251 
252 
253 /**
254  * Event to be returned upon menu selection.
255  **/
256 class YMenuEvent: public YEvent
257 {
258 public:
259 
260  YMenuEvent( YItem * item )
261  : YEvent( MenuEvent )
262  , _item( item )
263  {}
264 
265  YMenuEvent( const char * id ) : YEvent( MenuEvent ), _item(0), _id( id ) {}
266  YMenuEvent( const std::string & id ) : YEvent( MenuEvent ), _item(0), _id( id ) {}
267 
268  /**
269  * Return the YItem that corresponds to this event or 0 if the event was
270  * constructed with a string ID.
271  *
272  * Reimplemented from YEvent.
273  **/
274  virtual YItem * item() const { return _item; }
275 
276  /**
277  * Return the string ID of this event. This will be an empty string if the
278  * event was constructed with a YItem.
279  **/
280  std::string id() const { return _id; }
281 
282 protected:
283 
284  /**
285  * Protected destructor - events can only be deleted via
286  * YDialog::deleteEvent(). The associated dialog will take care of this
287  * event and delete it when appropriate.
288  **/
289  virtual ~YMenuEvent() {}
290 
291 
292  //
293  // Data members
294  //
295 
296  YItem * _item;
297  std::string _id;
298 };
299 
300 
301 /**
302  * Event to be returned upon closing a dialog with the window manager close
303  * button (or Alt-F4)
304  **/
305 class YCancelEvent: public YEvent
306 {
307 public:
308 
309  YCancelEvent() : YEvent( CancelEvent ) {}
310 
311 
312 protected:
313  /**
314  * Protected destructor - events can only be deleted via
315  * YDialog::deleteEvent(). The associated dialog will take care of this
316  * event and delete it when appropriate.
317  **/
318  virtual ~YCancelEvent() {}
319 };
320 
321 
322 /**
323  * Event to be returned upon closing a dialog with the window manager close
324  * button (or Alt-F4)
325  **/
326 class YDebugEvent: public YEvent
327 {
328 public:
329 
330  YDebugEvent() : YEvent( DebugEvent ) {}
331 
332 protected:
333  /**
334  * Protected destructor - events can only be deleted via
335  * YDialog::deleteEvent(). The associated dialog will take care of this
336  * event and delete it when appropriate.
337  **/
338  virtual ~YDebugEvent() {}
339 };
340 
341 
342 /**
343  * Event to be returned upon timeout
344  * (i.e. no event available in the specified timeout)
345  **/
346 class YTimeoutEvent: public YEvent
347 {
348 public:
349 
350  YTimeoutEvent() : YEvent( TimeoutEvent ) {}
351 
352 protected:
353  /**
354  * Protected destructor - events can only be deleted via
355  * YDialog::deleteEvent(). The associated dialog will take care of this
356  * event and delete it when appropriate.
357  **/
358  virtual ~YTimeoutEvent() {}
359 };
360 
361 
362 std::ostream & operator<<( std::ostream & stream, const YEvent * event );
363 
364 
365 #endif // YEvent_h
virtual ~YKeyEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:241
void deleteEvent(YEvent *event)
Delete an event.
Definition: YDialog.cc:508
std::string keySymbol() const
Returns the key symbol - a text describing the key, such as "CursorLeft", "F1", "a", "A", etc.
Definition: YEvent.h:224
bool isValid() const
Check if this event is valid.
Definition: YEvent.cc:53
void setDialog(YDialog *dia)
Set the dialog this event belongs to.
Definition: YEvent.h:129
Event to be returned upon menu selection.
Definition: YEvent.h:256
virtual ~YEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.cc:46
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:43
virtual ~YTimeoutEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:358
virtual ~YWidgetEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:194
EventType eventType() const
Returns the event type.
Definition: YEvent.h:79
virtual YItem * item() const
Return the YItem that corresponds to this event or 0 if the event was constructed with a string ID...
Definition: YEvent.h:274
YWidget * focusWidget() const
Returns the widget that currently has the keyboard focus.
Definition: YEvent.h:232
virtual ~YMenuEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:289
void invalidate()
Mark this event as invalid.
Definition: YEvent.cc:60
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
virtual ~YDebugEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:338
void deleteEvent(YEvent *event)
Delete an event.
EventReason reason() const
Returns the reason for this event.
Definition: YEvent.h:185
unsigned long serial() const
Returns the unique serial no.
Definition: YEvent.h:85
virtual YItem * item() const
Return the YItem that corresponds to this event or 0 if there is none.
Definition: YEvent.h:101
YDialog * dialog() const
Return the dialog this event belongs to or 0 if no dialog was set yet.
Definition: YEvent.h:106
std::string id() const
Return the string ID of this event.
Definition: YEvent.h:280
YEvent(EventType eventType=UnknownEvent)
Constructor.
Definition: YEvent.cc:38
Event to be returned upon timeout (i.e.
Definition: YEvent.h:346
A window in the desktop environment.
Definition: YDialog.h:47
virtual YWidget * widget() const
Returns the widget that caused this event or 0 if there is none.
Definition: YEvent.h:93
Abstract base class of all UI widgets.
Definition: YWidget.h:54
static const char * toString(EventType eventType)
Returns the character representation of an event type.
Definition: YEvent.cc:67
virtual ~YCancelEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.h:318
virtual YWidget * widget() const
Returns the widget that caused this event.
Definition: YEvent.h:180
Event to be returned upon closing a dialog with the window manager close button (or Alt-F4) ...
Definition: YEvent.h:305
Event to be returned upon closing a dialog with the window manager close button (or Alt-F4) ...
Definition: YEvent.h:326