kcmenumngr.cpp
00001 /* 00002 * This file is part of the KDE Libraries 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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 #include <tqwidget.h> 00022 #include <tqpopupmenu.h> 00023 #include "kcmenumngr.h" 00024 #include "tdeglobal.h" 00025 #include "tdeconfig.h" 00026 #include "tdeshortcut.h" 00027 00028 #undef KeyPress 00029 #undef None 00030 00031 template class TQPtrDict<TQPopupMenu>; 00032 00033 KContextMenuManager* KContextMenuManager::manager = 0; 00034 00035 KContextMenuManager::KContextMenuManager( TQObject* parent, const char* name ) 00036 : TQObject( parent, name) 00037 { 00038 TDEConfigGroupSaver saver ( TDEGlobal::config(), TQString::fromLatin1("Shortcuts") ) ; 00039 menuKey = TDEShortcut( saver.config()->readEntry(TQString::fromLatin1("PopupContextMenu"), TQString::fromLatin1("Menu") ) ).keyCodeQt(); 00040 saver.config()->setGroup( TQString::fromLatin1("ContextMenus") ) ; 00041 showOnPress = saver.config()->readBoolEntry(TQString::fromLatin1("ShowOnPress"), true ); 00042 } 00043 00044 KContextMenuManager::~KContextMenuManager() 00045 { 00046 } 00047 00048 00049 bool KContextMenuManager::showOnButtonPress( void ) 00050 { 00051 if ( !manager ) 00052 manager = new KContextMenuManager; 00053 return manager->showOnPress; 00054 } 00055 00056 00057 void KContextMenuManager::insert( TQWidget* widget, TQPopupMenu* popup ) 00058 { 00059 if ( !manager ) 00060 manager = new KContextMenuManager; 00061 00062 manager->connect( widget, TQT_SIGNAL( destroyed() ), manager, TQT_SLOT( widgetDestroyed() ) ); 00063 manager->menus.insert( widget, popup ); 00064 widget->installEventFilter( manager ); 00065 } 00066 00067 bool KContextMenuManager::eventFilter( TQObject *o, TQEvent * e) 00068 { 00069 TQPopupMenu* popup = 0; 00070 TQPoint pos; 00071 switch ( e->type() ) { 00072 case TQEvent::MouseButtonPress: 00073 if (((TQMouseEvent*) e )->button() != Qt::RightButton ) 00074 break; 00075 if ( !showOnPress ) 00076 return true; // eat event for safety 00077 popup = menus[o]; 00078 pos = ((TQMouseEvent*) e )->globalPos(); 00079 break; 00080 case TQEvent::MouseButtonRelease: 00081 if ( showOnPress || ((TQMouseEvent*) e )->button() != Qt::RightButton ) 00082 break; 00083 popup = menus[o]; 00084 pos = ((TQMouseEvent*) e )->globalPos(); 00085 break; 00086 case TQEvent::KeyPress: 00087 { 00088 if ( !o->isWidgetType() ) 00089 break; 00090 TQKeyEvent *k = (TQKeyEvent *)e; 00091 int key = k->key(); 00092 if ( k->state() & ShiftButton ) 00093 key |= SHIFT; 00094 if ( k->state() & ControlButton ) 00095 key |= CTRL; 00096 if ( k->state() & AltButton ) 00097 key |= ALT; 00098 if ( key != menuKey ) 00099 break; 00100 popup = menus[o]; 00101 if ( popup ) { 00102 TQWidget* w = (TQWidget*) o ; 00103 00104 // ### workaround 00105 pos = w->mapToGlobal( w->rect().center() ); 00106 // with later Qt snapshot 00107 // pos = w->mapToGlobal( w->microFocusHint().center() ); 00108 } 00109 } 00110 break; 00111 default: 00112 break; 00113 } 00114 00115 if ( popup ) { 00116 popup->popup( pos ); 00117 return true; 00118 } 00119 00120 return false; 00121 } 00122 00123 void KContextMenuManager::widgetDestroyed() 00124 { 00125 if ( menus.find( (TQObject*)sender() ) ) 00126 menus.remove( (TQObject*)sender() ); 00127 } 00128 00129 #include "kcmenumngr.moc"