kdeui Library API Documentation

ksystemtray.cpp

00001 /* This file is part of the KDE libraries
00002 
00003     Copyright (C) 1999 Matthias Ettrich (ettrich@kde.org)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "kaction.h"
00022 #include "kshortcut.h"
00023 #include "ksystemtray.h"
00024 #include "kpopupmenu.h"
00025 #include "kapplication.h"
00026 #include "klocale.h"
00027 #include <kwin.h>
00028 #include <kwinmodule.h>
00029 #include <kiconloader.h>
00030 
00031 #include <qapplication.h>
00032 #ifndef Q_WS_QWS
00033 #include <X11/Xlib.h>
00034 #ifndef KDE_USE_FINAL
00035 const int XFocusOut = FocusOut;
00036 const int XFocusIn = FocusIn;
00037 #endif
00038 #undef FocusOut
00039 #undef FocusIn
00040 #undef KeyPress
00041 #undef KeyRelease
00042 
00043 extern Time qt_x_time;
00044 #endif
00045 
00046 class KSystemTrayPrivate
00047 {
00048 public:
00049     KSystemTrayPrivate()
00050     {
00051         actionCollection = 0;
00052     }
00053 
00054     ~KSystemTrayPrivate()
00055     {
00056         delete actionCollection;
00057     }
00058 
00059     KActionCollection* actionCollection;
00060 };
00061 
00062 KSystemTray::KSystemTray( QWidget* parent, const char* name )
00063     : QLabel( parent, name, WType_TopLevel )
00064 {
00065     d = new KSystemTrayPrivate;
00066     d->actionCollection = new KActionCollection(this);
00067 
00068 #ifndef Q_WS_QWS
00069     // FIXME(E): Talk with QWS
00070     KWin::setSystemTrayWindowFor( winId(), parent?parent->topLevelWidget()->winId(): qt_xrootwin() );
00071     setBackgroundMode(X11ParentRelative);
00072 #endif
00073     hasQuit = 0;
00074     menu = new KPopupMenu( this );
00075     menu->insertTitle( kapp->miniIcon(), kapp->caption() );
00076     move( -1000, -1000 );
00077     KAction* quitAction = KStdAction::quit(this, SIGNAL(quitSelected()), d->actionCollection);
00078 
00079     if (parentWidget())
00080     {
00081         connect(quitAction, SIGNAL(activated()), parentWidget(), SLOT(close()));
00082         new KAction(i18n("Minimize"), KShortcut(), 
00083                     this, SLOT( toggleMinimizeRestore() ), 
00084                     d->actionCollection, "minimizeRestore");
00085     }
00086     else
00087     {
00088         connect(quitAction, SIGNAL(activated()), qApp, SLOT(closeAllWindows()));
00089     }
00090 }
00091 
00092 KSystemTray::~KSystemTray()
00093 {
00094     delete d;
00095 }
00096 
00097 
00098 void KSystemTray::showEvent( QShowEvent * )
00099 {
00100     if ( !hasQuit ) {
00101     menu->insertSeparator();
00102         KAction* action = d->actionCollection->action("minimizeRestore");
00103 
00104         if (action)
00105         {
00106             action->plug(menu);
00107         }
00108 
00109         action = d->actionCollection->action(KStdAction::name(KStdAction::Quit));
00110         action->plug(menu);
00111     hasQuit = 1;
00112     }
00113 }
00114 
00115 void KSystemTray::enterEvent( QEvent* )
00116 {
00117 #ifndef Q_WS_QWS
00118     // FIXME(E): Implement for Qt Embedded
00119     if ( !qApp->focusWidget() ) {
00120     XEvent ev;
00121     memset(&ev, 0, sizeof(ev));
00122     ev.xfocus.display = qt_xdisplay();
00123     ev.xfocus.type = XFocusIn;
00124     ev.xfocus.window = winId();
00125     ev.xfocus.mode = NotifyNormal;
00126     ev.xfocus.detail = NotifyAncestor;
00127     Time time = qt_x_time;
00128     qt_x_time = 1;
00129     qApp->x11ProcessEvent( &ev );
00130     qt_x_time = time;
00131     }
00132 #endif
00133 }
00134 
00135 KPopupMenu* KSystemTray::contextMenu() const
00136 {
00137     return menu;
00138 }
00139 
00140 
00141 void KSystemTray::mousePressEvent( QMouseEvent *e )
00142 {
00143     if ( !rect().contains( e->pos() ) )
00144     return;
00145 
00146     switch ( e->button() ) {
00147     case LeftButton:
00148         toggleMinimizeRestore();
00149     break;
00150     case MidButton:
00151     // fall through
00152     case RightButton:
00153     if ( parentWidget() ) {
00154             KAction* action = d->actionCollection->action("minimizeRestore");
00155         if ( parentWidget()->isVisible() )
00156         action->setText( i18n("Minimize") );
00157         else
00158         action->setText( i18n("Restore") );
00159     }
00160     contextMenuAboutToShow( menu );
00161     menu->popup( e->globalPos() );
00162     break;
00163     default:
00164     // nothing
00165     break;
00166     }
00167 }
00168 
00169 
00170 
00171 void KSystemTray::mouseReleaseEvent( QMouseEvent * )
00172 {
00173 }
00174 
00175 
00176 void KSystemTray::contextMenuAboutToShow( KPopupMenu* )
00177 {
00178 }
00179 
00180 
00181 void KSystemTray::toggleMinimizeRestore()
00182 {
00183     QWidget *pw = parentWidget();
00184 
00185     if ( !pw )
00186     return;
00187 
00188     KWin::Info info = KWin::info( pw->winId() );
00189     bool visible = (info.mappingState == NET::Visible);
00190     if ( visible && !pw->isActiveWindow() ) // may be obscured -> raise it
00191     {
00192         KWinModule mod;
00193         if ( mod.stackingOrder().last() != pw->winId() )
00194         {
00195             if ( info.desktop == KWin::currentDesktop() )
00196             {
00197                 KWin::setActiveWindow( pw->winId() );
00198                 return;
00199             }
00200             else
00201                 visible = false;
00202         }
00203     }
00204 
00205     if ( !visible )
00206     {
00207         if ( info.mappingState != NET::Iconic )
00208         {
00209             pw->hide(); // KWin::setOnDesktop( parentWidget()->winId(), KWin::currentDesktop() );
00210 #ifndef Q_WS_QWS //FIXME
00211             pw->move( info.geometry.topLeft() );
00212 #endif
00213             pw->show();
00214 #ifndef Q_WS_QWS //FIXME
00215         }
00216         KWin::deIconifyWindow( pw->winId() );
00217     KWin::setActiveWindow( pw->winId() );
00218 #endif
00219     } else {
00220     pw->hide();
00221     }
00222 }
00223 
00224 KActionCollection* KSystemTray::actionCollection()
00225 {
00226     return d->actionCollection;
00227 }
00228 
00229 void KSystemTray::virtual_hook( int, void* )
00230 { /*BASE::virtual_hook( id, data );*/ }
00231 
00232 #include "ksystemtray.moc"
00233 #include "kdockwindow.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:01 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001