libyui  3.10.0
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 using std::string;
35 
36 
37 unsigned long YEvent::_nextSerial = 0;
38 
39 
40 YEvent::YEvent( EventType eventType )
41  : _eventType( eventType )
42 {
43  _dialog = YDialog::currentDialog( false ); // don't throw
44  _serial = _nextSerial++;
45 }
46 
47 
49 {
50  invalidate();
51 }
52 
53 
54 bool
56 {
57  return _eventType != InvalidEvent;
58 }
59 
60 
61 void
63 {
64  _eventType = InvalidEvent;
65 }
66 
67 
68 const char *
69 YEvent::toString( EventType eventType )
70 {
71  switch ( eventType )
72  {
73  case NoEvent: return "NoEvent";
74  case UnknownEvent: return "UnknownEvent";
75  case WidgetEvent: return "WidgetEvent";
76  case MenuEvent: return "MenuEvent";
77  case KeyEvent: return "KeyEvent";
78  case CancelEvent: return "CancelEvent";
79  case TimeoutEvent: return "TimeoutEvent";
80  case DebugEvent: return "DebugEvent";
81  case InvalidEvent: return "InvalidEvent";
82 
83  // Intentionally omitting "default" branch so the compiler can
84  // detect unhandled enums
85  }
86 
87  return "<Unknown event type - internal error>";
88 }
89 
90 
91 const char *
92 YEvent::toString( EventReason reason )
93 {
94  switch ( reason )
95  {
96  case UnknownReason: return "Unknown";
97  case Activated: return "Activated";
98  case SelectionChanged: return "SelectionChanged";
99  case ValueChanged: return "ValueChanged";
100  case ContextMenuActivated: return "ContextMenuActivated";
101 
102  // Intentionally omitting "default" branch so the compiler can
103  // detect unhandled enums
104  }
105 
106  return "<Unknown event reason - internal error>";
107 }
108 
109 
110 
111 
113  EventReason reason,
114  EventType eventType )
115  : YEvent( eventType )
116  , _widget( widget )
117  , _reason( reason )
118 {
119  if ( widget )
121 }
122 
123 
124 
125 YKeyEvent::YKeyEvent( const string & keySymbol,
126  YWidget * focusWidget )
127  : YEvent( KeyEvent )
128  , _keySymbol( keySymbol )
129  , _focusWidget( focusWidget )
130 {
131 }
132 
133 
134 
135 std::ostream &
136 operator<<( std::ostream & stream, const YEvent * event )
137 {
138  if ( event )
139  {
140  stream << YEvent::toString( event->eventType() )
141  << " at " << std::hex << (void *) event << std::dec;
142  }
143  else
144  {
145  stream << "<NULL event>";
146  }
147 
148  return stream;
149 }
static YDialog * currentDialog(bool doThrow=true)
Return the current (topmost) dialog.
Definition: YDialog.cc:539
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:44
static const char * toString(EventType eventType)
Returns the character representation of an event type.
Definition: YEvent.cc:69
virtual ~YEvent()
Protected destructor - events can only be deleted via YDialog::deleteEvent().
Definition: YEvent.cc:48
bool isValid() const
Check if this event is valid.
Definition: YEvent.cc:55
YEvent(EventType eventType=UnknownEvent)
Constructor.
Definition: YEvent.cc:40
void invalidate()
Mark this event as invalid.
Definition: YEvent.cc:62
void setDialog(YDialog *dia)
Set the dialog this event belongs to.
Definition: YEvent.h:129
EventType eventType() const
Returns the event type.
Definition: YEvent.h:79
YKeyEvent(const std::string &keySymbol, YWidget *focusWidget=0)
Constructor.
Definition: YEvent.cc:125
YWidgetEvent(YWidget *widget=0, EventReason reason=Activated, EventType eventType=WidgetEvent)
Constructor.
Definition: YEvent.cc:112
virtual YWidget * widget() const
Returns the widget that caused this event.
Definition: YEvent.h:180
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YDialog * findDialog()
Traverse up the widget hierarchy and find the dialog this widget belongs to.
Definition: YWidget.cc:376