kdeui Library API Documentation

kmenubar.cpp

00001 /*
00002 
00003     Copyright (C) 1997, 1998, 1999, 2000  Sven Radej (radej@kde.org)
00004     Copyright (C) 1997, 1998, 1999, 2000 Matthias Ettrich (ettrich@kde.org)
00005     Copyright (C) 1999, 2000 Daniel "Mosfet" Duley (mosfet@kde.org)
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021     */
00022 
00023 
00024 #ifndef INCLUDE_MENUITEM_DEF
00025 #define INCLUDE_MENUITEM_DEF
00026 #endif
00027 
00028 #include <qevent.h>
00029 #include <qobjectlist.h>
00030 #include <qaccel.h>
00031 
00032 #include <kconfig.h>
00033 #include <kglobalsettings.h>
00034 #include <kmenubar.h>
00035 #include <kapplication.h>
00036 #include <kwin.h>
00037 #include <kwinmodule.h>
00038 #include <kglobal.h>
00039 
00040 #ifndef Q_WS_QWS
00041 #include <X11/Xlib.h>
00042 #include <X11/Xutil.h>
00043 #include <X11/Xatom.h>
00044 #endif
00045 
00046 
00047 class KMenuBar::KMenuBarPrivate
00048 {
00049 public:
00050     KMenuBarPrivate()
00051     {
00052       topLevel = false;
00053       forcedTopLevel = false;
00054       wasTopLevel = false;
00055     }
00056     bool forcedTopLevel;
00057     bool topLevel;
00058     int frameStyle;
00059     bool wasTopLevel; // when TLW is fullscreen, remember state
00060 };
00061 
00062 KMenuBar::KMenuBar(QWidget *parent, const char *name)
00063   : QMenuBar(parent, name)
00064 {
00065     d = new KMenuBarPrivate;
00066     d->frameStyle = frameStyle();
00067 
00068     if ( kapp )
00069         // toolbarAppearanceChanged(int) is sent when changing macstyle
00070         connect( kapp, SIGNAL(toolbarAppearanceChanged(int)),
00071             this, SLOT(slotReadConfig()));
00072 
00073     connect( qApp->desktop(), SIGNAL( resized( int )), SLOT( updateKMenubarSize()));
00074     slotReadConfig();
00075 }
00076 
00077 KMenuBar::~KMenuBar()
00078 {
00079   delete d;
00080 }
00081 
00082 void KMenuBar::setTopLevelMenu(bool top_level)
00083 {
00084   d->forcedTopLevel = top_level;
00085   setTopLevelMenuInternal( top_level );
00086 }
00087 
00088 #if QT_VERSION < 0x030100
00089 namespace
00090 {
00091 class QWidgetHack
00092     : public QWidget
00093     {
00094     public:
00095         bool isFullScreen() { return isTopLevel() && topData()->fullscreen; }
00096     };
00097 }
00098 #endif
00099 
00100 void KMenuBar::setTopLevelMenuInternal(bool top_level)
00101 {
00102   if (d->forcedTopLevel)
00103     top_level = true;
00104 
00105   if( parentWidget()
00106 #if QT_VERSION >= 0x030100
00107       && parentWidget()->topLevelWidget()->isFullScreen()) {
00108 #else
00109       && static_cast<QWidgetHack*>(parentWidget()->topLevelWidget())->isFullScreen()) {
00110 #endif
00111     d->wasTopLevel = top_level;
00112     top_level = false;
00113   }
00114 
00115   if ( isTopLevelMenu() == top_level )
00116     return;
00117   d->topLevel = top_level;
00118   if ( isTopLevelMenu() ) {
00119       bool wasVisible = !isHidden();
00120       d->frameStyle = frameStyle();
00121       removeEventFilter( topLevelWidget() );
00122       reparent( parentWidget(), WType_TopLevel | WType_Dialog | WStyle_NoBorder, QPoint(0,0), false );
00123 #ifndef Q_WS_QWS //FIXME
00124       KWin::setType( winId(), NET::TopMenu );
00125 #endif
00126       setFrameStyle( MenuBarPanel );
00127       installEventFilter( parentWidget()->topLevelWidget() );
00128       if ( wasVisible )
00129           show();
00130   } else {
00131       if ( parentWidget() ) {
00132           reparent( parentWidget(), QPoint(0,0), !isHidden());
00133           setBackgroundMode( PaletteButton );
00134           installEventFilter( topLevelWidget() );
00135           setFrameStyle( d->frameStyle );
00136       }
00137   }
00138 }
00139 
00140 bool KMenuBar::isTopLevelMenu() const
00141 {
00142   return d->topLevel;
00143 }
00144 
00145 void KMenuBar::show()
00146 {
00147     // work around a Qt bug
00148     // why is this still needed? (Simon)
00149     if ( d->topLevel && isVisible() )
00150     return;
00151 
00152     QMenuBar::show();
00153 }
00154 
00155 void KMenuBar::slotReadConfig()
00156 {
00157   KConfig *config = KGlobal::config();
00158   KConfigGroupSaver saver( config, "KDE" );
00159   setTopLevelMenuInternal( config->readBoolEntry( "macStyle", false ) );
00160 }
00161 
00162 bool KMenuBar::eventFilter(QObject *obj, QEvent *ev)
00163 {
00164 
00165     if ( d->topLevel ) {
00166     if ( ev->type() == QEvent::Resize )
00167         return FALSE; // hinder QMenubar to adjust its size
00168     if ( parentWidget() && obj == parentWidget()->topLevelWidget()  ) {
00169 
00170         if ( ev->type() == QEvent::Accel || ev->type() == QEvent::AccelAvailable ) {
00171         if ( QApplication::sendEvent( topLevelWidget(), ev ) )
00172             return TRUE;
00173         }
00174 
00175 //      if ( ev->type() == QEvent::Show && isHidden())
00176 //      show();  doesn't seem to do anything besides breaking things
00177 //      else
00178             if ( ev->type() == QEvent::WindowActivate )
00179         raise();
00180             else if(ev->type() == QEvent::ShowFullScreen )
00181                 // will update the state properly
00182                 setTopLevelMenuInternal( d->topLevel );
00183     }
00184     } else {
00185         if( topLevelWidget() && obj == topLevelWidget()) {
00186             if( ev->type() == QEvent::ShowNormal )
00187                 setTopLevelMenuInternal( d->wasTopLevel );
00188         }
00189     }
00190     return QMenuBar::eventFilter( obj, ev );
00191 }
00192 
00193 void KMenuBar::showEvent( QShowEvent *e )
00194 {
00195     updateKMenubarSize();
00196     QMenuBar::showEvent(e);
00197 }
00198 
00199 void KMenuBar::resizeEvent( QResizeEvent *e )
00200 {
00201     updateKMenubarSize();
00202     QMenuBar::resizeEvent(e);
00203 }
00204 
00205 void KMenuBar::updateKMenubarSize()
00206 {
00207     if ( d->topLevel ) {
00208         KConfigGroup xineramaConfig(KGlobal::config(),"Xinerama");
00209         int screen = xineramaConfig.readNumEntry("MenubarScreen",
00210             QApplication::desktop()->screenNumber(QPoint(0,0)) );
00211         QRect area = QApplication::desktop()->screenGeometry(screen);
00212         QMenuBar::setGeometry(area.left(), area.top()-frameWidth()-2, area.width(), heightForWidth( area.width() ) );
00213 #ifndef Q_WS_QWS //FIXME
00214         int strut_height = height()-frameWidth()-2;
00215         if( strut_height < 0 )
00216             strut_height = 0;
00217         KWin::setStrut( winId(), 0, 0, strut_height, 0 );
00218 #endif
00219     }
00220 }
00221 
00222 void KMenuBar::setGeometry( int x, int y, int w, int h )
00223 {
00224    // With the toolbar in toplevel-mode it sometimes has the tendency to cuddle up in
00225    // the topleft corner due to a misguided attempt from the layout manager (?) to
00226    // size us. The follow line filters out any resize attempt while in toplevel-mode.
00227    if ( !d->topLevel )
00228        QMenuBar::setGeometry(x,y,w,h);
00229 }
00230 
00231 void KMenuBar::virtual_hook( int, void* )
00232 { /*BASE::virtual_hook( id, data );*/ }
00233 
00234 
00235 
00236 
00237 #include "kmenubar.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:04 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001