libyui  3.4.2
YEvent.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 
27 #define YUILogComponent "ui-events"
28 #include "YUILog.h"
29 
30 #include "YWidget.h"
31 #include "YEvent.h"
32 #include "YDialog.h"
33 
34 
35 unsigned long YEvent::_nextSerial = 0;
36 
37 
38 YEvent::YEvent( EventType eventType )
39  : _eventType( eventType )
40 {
41  _dialog = YDialog::currentDialog( false ); // don't throw
42  _serial = _nextSerial++;
43 }
44 
45 
47 {
48  invalidate();
49 }
50 
51 
52 bool
54 {
55  return _eventType != InvalidEvent;
56 }
57 
58 
59 void
61 {
62  _eventType = InvalidEvent;
63 }
64 
65 
66 const char *
68 {
69  switch ( eventType )
70  {
71  case NoEvent: return "NoEvent";
72  case UnknownEvent: return "UnknownEvent";
73  case WidgetEvent: return "WidgetEvent";
74  case MenuEvent: return "MenuEvent";
75  case KeyEvent: return "KeyEvent";
76  case CancelEvent: return "CancelEvent";
77  case TimeoutEvent: return "TimeoutEvent";
78  case DebugEvent: return "DebugEvent";
79  case InvalidEvent: return "InvalidEvent";
80 
81  // Intentionally omitting "default" branch so the compiler can
82  // detect unhandled enums
83  }
84 
85  return "<Unknown event type - internal error>";
86 }
87 
88 
89 const char *
90 YEvent::toString( EventReason reason )
91 {
92  switch ( reason )
93  {
94  case UnknownReason: return "Unknown";
95  case Activated: return "Activated";
96  case SelectionChanged: return "SelectionChanged";
97  case ValueChanged: return "ValueChanged";
98  case ContextMenuActivated: return "ContextMenuActivated";
99 
100  // Intentionally omitting "default" branch so the compiler can
101  // detect unhandled enums
102  }
103 
104  return "<Unknown event reason - internal error>";
105 }
106 
107 
108 
109 
111  EventReason reason,
112  EventType eventType )
113  : YEvent( eventType )
114  , _widget( widget )
115  , _reason( reason )
116 {
117  if ( widget )
118  setDialog( widget->findDialog() );
119 }
120 
121 
122 
123 YKeyEvent::YKeyEvent( const std::string & keySymbol,
124  YWidget * focusWidget )
125  : YEvent( KeyEvent )
126  , _keySymbol( keySymbol )
127  , _focusWidget( focusWidget )
128 {
129 }
130 
131 
132 
133 std::ostream &
134 operator<<( std::ostream & stream, const YEvent * event )
135 {
136  if ( event )
137  {
138  stream << YEvent::toString( event->eventType() )
139  << " at " << std::hex << (void *) event << std::dec;
140  }
141  else
142  {
143  stream << "<NULL event>";
144  }
145 
146  return stream;
147 }
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
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
YWidgetEvent(YWidget *widget=0, EventReason reason=Activated, EventType eventType=WidgetEvent)
Constructor.
Definition: YEvent.cc:110
EventType eventType() const
Returns the event type.
Definition: YEvent.h:79
YDialog * findDialog()
Traverse up the widget hierarchy and find the dialog this widget belongs to.
Definition: YWidget.cc:374
YKeyEvent(const std::string &keySymbol, YWidget *focusWidget=0)
Constructor.
Definition: YEvent.cc:123
void invalidate()
Mark this event as invalid.
Definition: YEvent.cc:60
static YDialog * currentDialog(bool doThrow=true)
Return the current (topmost) dialog.
Definition: YDialog.cc:531
YEvent(EventType eventType=UnknownEvent)
Constructor.
Definition: YEvent.cc:38
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