home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / trayicon / trayicon.cpp < prev    next >
C/C++ Source or Header  |  2001-09-15  |  5KB  |  224 lines

  1. #include "trayicon.h"
  2. #include "qpopupmenu.h"
  3.  
  4. /*!
  5.   \class TrayIcon qtrayicon.h
  6.   \brief The TrayIcon class implements an entry in the system tray.
  7. */
  8.  
  9. /*!
  10.   Creates a TrayIcon object. \a parent and \a name are propagated
  11.   to the QObject constructor. The icon is initially invisible.
  12.  
  13.   \sa show
  14. */
  15. TrayIcon::TrayIcon( QObject *parent, const char *name )
  16. : QObject(parent, name), pop(0), d(0)
  17. {
  18. }
  19.  
  20. /*!
  21.   Creates a TrayIcon object displaying \a icon and \a tooltip, and opening
  22.   \a popup when clicked with the right mousebutton. \a parent and \a name are
  23.   propagated to the QObject constructor. The icon is initially invisible.
  24.  
  25.   \sa show
  26. */
  27. TrayIcon::TrayIcon( const QPixmap &icon, const QString &tooltip, QPopupMenu *popup, QObject *parent, const char *name )
  28. : QObject(parent, name), pop(popup), pm(icon), tip(tooltip), d(0)
  29. {
  30. }
  31.  
  32. /*!
  33.   Removes the icon from the system tray and frees all allocated resources.
  34. */
  35. TrayIcon::~TrayIcon()
  36. {
  37.     sysRemove();
  38. }
  39.  
  40. /*!
  41.   Sets the context menu to \a popup. The context menu will pop up when the
  42.   user clicks the system tray entry with the right mouse button.
  43. */
  44. void TrayIcon::setPopup( QPopupMenu* popup )
  45. {
  46.     pop = popup;
  47. }
  48.  
  49. /*!
  50.   Returns the current popup menu.
  51.  
  52.   \sa setPopup
  53. */
  54. QPopupMenu* TrayIcon::popup() const
  55. {
  56.     return pop;
  57. }
  58.  
  59. /*!
  60.   \property TrayIcon::icon
  61.   \brief the system tray icon.
  62. */
  63. void TrayIcon::setIcon( const QPixmap &icon )
  64. {
  65.     pm = icon;
  66.     sysUpdateIcon();
  67. }
  68.  
  69. QPixmap TrayIcon::icon() const
  70. {
  71.     return pm;
  72. }
  73.  
  74. /*!
  75.   \property TrayIcon::toolTip
  76.   \brief the tooltip for the system tray entry
  77.   
  78.   On some systems, the tooltip's length is limited and will be truncated as necessary.
  79. */
  80. void TrayIcon::setToolTip( const QString &tooltip )
  81. {
  82.     tip = tooltip;
  83.     sysUpdateToolTip();
  84. }
  85.  
  86. QString TrayIcon::toolTip() const
  87. {
  88.     return tip;
  89. }
  90.  
  91. /*!
  92.   Shows the icon in the system tray.
  93.  
  94.   \sa hide
  95. */
  96. void TrayIcon::show()
  97. {
  98.     sysInstall();
  99. }
  100.  
  101. /*!
  102.   Hides the system tray entry.
  103. */
  104. void TrayIcon::hide()
  105. {
  106.     sysRemove();
  107. }
  108.  
  109. /*!
  110.   \reimp
  111. */
  112. bool TrayIcon::event( QEvent *e )
  113. {
  114.     switch ( e->type() ) {
  115.     case QEvent::MouseMove:
  116.     mouseMoveEvent( (QMouseEvent*)e );
  117.     break;
  118.  
  119.     case QEvent::MouseButtonPress:
  120.     mousePressEvent( (QMouseEvent*)e );
  121.     break;
  122.  
  123.     case QEvent::MouseButtonRelease:
  124.     mouseReleaseEvent( (QMouseEvent*)e );
  125.     break;
  126.  
  127.     case QEvent::MouseButtonDblClick:
  128.     mouseDoubleClickEvent( (QMouseEvent*)e );
  129.     break;
  130.     default:
  131.     return QObject::event( e );
  132.     }
  133.  
  134.     return TRUE;
  135. }
  136.  
  137. /*!
  138.   This event handler can be reimplemented in a subclass to receive
  139.   mouse move events for the system tray entry.
  140.  
  141.   \sa mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(),  QMouseEvent
  142. */
  143. void TrayIcon::mouseMoveEvent( QMouseEvent *e )
  144. {
  145.     e->ignore();
  146. }
  147.  
  148. /*!
  149.   This event handler can be reimplemented in a subclass to receive
  150.   mouse press events for the system tray entry.
  151.  
  152.   \sa mouseReleaseEvent(), mouseDoubleClickEvent(),
  153.   mouseMoveEvent(), QMouseEvent
  154. */
  155. void TrayIcon::mousePressEvent( QMouseEvent *e )
  156. {
  157.     e->ignore();
  158. }
  159.  
  160. /*!
  161.   This event handler can be reimplemented in a subclass to receive
  162.   mouse release events for the system tray entry.
  163.  
  164.   The default implementations opens the context menu when the entry
  165.   has been clicked with the right mouse button.
  166.  
  167.   \sa setPopup(), mousePressEvent(), mouseDoubleClickEvent(),
  168.   mouseMoveEvent(), QMouseEvent
  169. */
  170. void TrayIcon::mouseReleaseEvent( QMouseEvent *e )
  171. {
  172.     switch ( e->button() ) {
  173.     case RightButton:
  174.     if ( pop ) {
  175.         // Necessary to make keyboard focus
  176.         // and menu closing work on Windows.
  177.         pop->setActiveWindow();
  178.         pop->popup( e->globalPos() );
  179.         pop->setActiveWindow();
  180.         e->accept();
  181.     }
  182.     break;
  183.     case LeftButton:
  184.     emit clicked( e->globalPos() );
  185.     break;
  186.     default:
  187.     break;
  188.     }
  189.     e->ignore();
  190. }
  191.  
  192. /*!
  193.   This event handler can be reimplemented in a subclass to receive
  194.   mouse double click events for the system tray entry.
  195.  
  196.   Note that the system tray entry gets a mousePressEvent() and a
  197.   mouseReleaseEvent() before the mouseDoubleClickEvent().
  198.  
  199.   \sa mousePressEvent(), mouseReleaseEvent(),
  200.   mouseMoveEvent(), QMouseEvent
  201. */
  202. void TrayIcon::mouseDoubleClickEvent( QMouseEvent *e )
  203. {
  204.     if ( e->button() == LeftButton )
  205.     emit doubleClicked( e->globalPos() );
  206.     e->ignore();
  207. }
  208.  
  209. /*!
  210.   \fn void TrayIcon::clicked( const QPoint &p )
  211.  
  212.   This signal is emitted when the user clicks the system tray icon
  213.   with the left mouse button, with \a p being the global mouse position
  214.   at that moment.
  215. */
  216.  
  217. /*!
  218.   \fn void TrayIcon::doubleClicked( const QPoint &p )
  219.  
  220.   This signal is emitted when the user double clicks the system tray
  221.   icon with the left mouse button, with \a p being the global mouse position
  222.   at that moment.
  223. */
  224.