libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YEvent.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YEvent.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YEvent_h
00026 #define YEvent_h
00027 
00028 
00029 #include <string>
00030 #include <iosfwd>
00031 #include "YDialog.h"
00032 #include "YSimpleEventHandler.h"
00033 
00034 class YWidget;
00035 class YItem;
00036 class YDialog;
00037 
00038 
00039 /**
00040  * Abstract base class for events to be returned upon UI::UserInput()
00041  * and related functions.
00042  **/
00043 class YEvent
00044 {
00045 public:
00046 
00047     enum EventType
00048     {
00049         NoEvent = 0,
00050         UnknownEvent,
00051         WidgetEvent,
00052         MenuEvent,
00053         KeyEvent,
00054         CancelEvent,
00055         TimeoutEvent,
00056         DebugEvent,
00057         InvalidEvent = 0x4242
00058     };
00059 
00060 
00061     enum EventReason
00062     {
00063         UnknownReason = 0,
00064         Activated,
00065         SelectionChanged,
00066         ValueChanged,
00067         ContextMenuActivated
00068     };
00069 
00070 
00071     /**
00072      * Constructor.
00073      **/
00074     YEvent( EventType eventType = UnknownEvent );
00075 
00076     /**
00077      * Returns the event type.
00078      **/
00079     EventType eventType() const { return _eventType; }
00080 
00081     /**
00082      * Returns the unique serial no. of this event.
00083      * This is mainly useful for debugging.
00084      **/
00085     unsigned long serial() const { return _serial; }
00086 
00087     /**
00088      * Returns the widget that caused this event or 0 if there is none.
00089      *
00090      * This default implementation always returns 0.
00091      * Subclasses that actually return widgets should overwrite this method.
00092      **/
00093     virtual YWidget * widget() const { return 0; }
00094 
00095     /**
00096      * Return the YItem that corresponds to this event or 0 if there is none.
00097      *
00098      * This default implementation always returns 0.
00099      * Subclasses that actually return items should overwrite this method.
00100      **/
00101     virtual YItem * item() const { return 0; }
00102 
00103     /**
00104      * Return the dialog this event belongs to or 0 if no dialog was set yet.
00105      **/
00106     YDialog * dialog() const { return _dialog; }
00107 
00108     /**
00109      * Check if this event is valid. Events become invalid in the destructor.
00110      **/
00111     bool isValid() const;
00112 
00113     /**
00114      * Returns the character representation of an event type.
00115      **/
00116     static const char * toString( EventType eventType );
00117 
00118     /**
00119      * Returns the character representation of an event reason.
00120      **/
00121     static const char * toString( EventReason reason );
00122 
00123 
00124 protected:
00125 
00126     /**
00127      * Set the dialog this event belongs to.
00128      **/
00129     void setDialog( YDialog * dia ) { _dialog = dia; }
00130 
00131     /**
00132      * Protected destructor - events can only be deleted via
00133      * YDialog::deleteEvent(). The associated dialog will take care of this
00134      * event and delete it when appropriate.
00135      *
00136      * This desctructor is virtual to force a polymorph object
00137      * so dynamic_cast<> can be used.
00138      **/
00139     virtual ~YEvent();
00140 
00141     /**
00142      * Mark this event as invalid. This cannot be undone.
00143      **/
00144     void invalidate();
00145 
00146 private:
00147 
00148     friend void YDialog::deleteEvent( YEvent * event );
00149     friend void YSimpleEventHandler::deleteEvent( YEvent * event );
00150 
00151 
00152     //
00153     // Data members
00154     //
00155 
00156     EventType                   _eventType;
00157     unsigned long               _serial;
00158     YDialog *                   _dialog;
00159 
00160     static unsigned long        _nextSerial;
00161 };
00162 
00163 
00164 
00165 class YWidgetEvent: public YEvent
00166 {
00167 public:
00168 
00169     /**
00170      * Constructor.
00171      **/
00172     YWidgetEvent( YWidget *     widget          = 0,
00173                   EventReason   reason          = Activated,
00174                   EventType     eventType       = WidgetEvent );
00175 
00176     /**
00177      * Returns the widget that caused this event.
00178      * Reimplemented from YEvent.
00179      **/
00180     virtual YWidget * widget() const { return _widget; }
00181 
00182     /**
00183      * Returns the reason for this event. This very much like an event sub-type.
00184      **/
00185     EventReason reason() const { return _reason; }
00186 
00187 protected:
00188 
00189     /**
00190      * Protected destructor - events can only be deleted via
00191      * YDialog::deleteEvent(). The associated dialog will take care of this
00192      * event and delete it when appropriate.
00193      **/
00194     virtual ~YWidgetEvent() {}
00195 
00196 
00197     //
00198     // Data members
00199     //
00200 
00201     YWidget *   _widget;
00202     EventReason _reason;
00203 };
00204 
00205 
00206 class YKeyEvent: public YEvent
00207 {
00208 public:
00209 
00210     /**
00211      * Constructor.
00212      *
00213      * Create a key event with a specified key symbol (a text describing the
00214      * key, such as "CursorLeft", "F1", etc.) and optionally the widget that
00215      * currently has the keyboard focus.
00216      **/
00217     YKeyEvent( const std::string &      keySymbol,
00218                YWidget *        focusWidget = 0 );
00219 
00220     /**
00221      * Returns the key symbol - a text describing the
00222      * key, such as "CursorLeft", "F1", "a", "A", etc.
00223      **/
00224     std::string keySymbol() const { return _keySymbol; }
00225 
00226     /**
00227      * Returns the widget that currently has the keyboard focus.
00228      *
00229      * This might be 0 if no widget has the focus or if the creator of
00230      * this event could not obtain that information.
00231      **/
00232     YWidget * focusWidget() const { return _focusWidget; }
00233 
00234 protected:
00235 
00236     /**
00237      * Protected destructor - events can only be deleted via
00238      * YDialog::deleteEvent(). The associated dialog will take care of this
00239      * event and delete it when appropriate.
00240      **/
00241     virtual ~YKeyEvent() {}
00242 
00243 
00244     //
00245     // Data members
00246     //
00247 
00248     std::string _keySymbol;
00249     YWidget *   _focusWidget;
00250 };
00251 
00252 
00253 /**
00254  * Event to be returned upon menu selection.
00255  **/
00256 class YMenuEvent: public YEvent
00257 {
00258 public:
00259 
00260     YMenuEvent( YItem * item )
00261         : YEvent( MenuEvent )
00262         , _item( item )
00263         {}
00264 
00265     YMenuEvent( const char *            id )    : YEvent( MenuEvent ), _item(0), _id( id ) {}
00266     YMenuEvent( const std::string &     id )    : YEvent( MenuEvent ), _item(0), _id( id ) {}
00267 
00268     /**
00269      * Return the YItem that corresponds to this event or 0 if the event was
00270      * constructed with a string ID.
00271      *
00272      * Reimplemented from YEvent.
00273      **/
00274     virtual YItem * item() const { return _item; }
00275 
00276     /**
00277      * Return the string ID of this event. This will be an empty string if the
00278      * event was constructed with a YItem.
00279      **/
00280     std::string id() const { return _id; }
00281 
00282 protected:
00283 
00284     /**
00285      * Protected destructor - events can only be deleted via
00286      * YDialog::deleteEvent(). The associated dialog will take care of this
00287      * event and delete it when appropriate.
00288      **/
00289     virtual ~YMenuEvent() {}
00290 
00291 
00292     //
00293     // Data members
00294     //
00295 
00296     YItem *     _item;
00297     std::string _id;
00298 };
00299 
00300 
00301 /**
00302  * Event to be returned upon closing a dialog with the window manager close
00303  * button (or Alt-F4)
00304  **/
00305 class YCancelEvent: public YEvent
00306 {
00307 public:
00308 
00309     YCancelEvent() : YEvent( CancelEvent ) {}
00310 
00311 
00312 protected:
00313     /**
00314      * Protected destructor - events can only be deleted via
00315      * YDialog::deleteEvent(). The associated dialog will take care of this
00316      * event and delete it when appropriate.
00317      **/
00318     virtual ~YCancelEvent() {}
00319 };
00320 
00321 
00322 /**
00323  * Event to be returned upon closing a dialog with the window manager close
00324  * button (or Alt-F4)
00325  **/
00326 class YDebugEvent: public YEvent
00327 {
00328 public:
00329 
00330     YDebugEvent() : YEvent( DebugEvent ) {}
00331 
00332 protected:
00333     /**
00334      * Protected destructor - events can only be deleted via
00335      * YDialog::deleteEvent(). The associated dialog will take care of this
00336      * event and delete it when appropriate.
00337      **/
00338     virtual ~YDebugEvent() {}
00339 };
00340 
00341 
00342 /**
00343  * Event to be returned upon timeout
00344  * (i.e. no event available in the specified timeout)
00345  **/
00346 class YTimeoutEvent: public YEvent
00347 {
00348 public:
00349 
00350     YTimeoutEvent() : YEvent( TimeoutEvent ) {}
00351 
00352 protected:
00353     /**
00354      * Protected destructor - events can only be deleted via
00355      * YDialog::deleteEvent(). The associated dialog will take care of this
00356      * event and delete it when appropriate.
00357      **/
00358     virtual ~YTimeoutEvent() {}
00359 };
00360 
00361 
00362 std::ostream & operator<<( std::ostream & stream, const YEvent * event );
00363 
00364 
00365 #endif // YEvent_h
 All Classes Functions Variables Enumerations Friends