23 #include <tqguardedptr.h>
24 #include <tqpopupmenu.h>
29 #include "kaccelbase.h"
30 #include <kapplication.h>
33 #include <kshortcut.h>
35 #include "kaccelprivate.h"
38 # include <X11/Xlib.h>
39 # ifdef KeyPress // needed for --enable-final
41 const int XKeyPress = KeyPress;
81 bool kde_g_bKillAccelOverride =
false;
83 class KAccelEventHandler :
public TQWidget
86 static KAccelEventHandler*
self()
89 g_pSelf =
new KAccelEventHandler;
93 static void accelActivated(
bool b ) { g_bAccelActivated = b; }
99 bool x11Event( XEvent* pEvent );
102 static KAccelEventHandler* g_pSelf;
103 static bool g_bAccelActivated;
106 KAccelEventHandler* KAccelEventHandler::g_pSelf = 0;
107 bool KAccelEventHandler::g_bAccelActivated =
false;
109 KAccelEventHandler::KAccelEventHandler()
110 : TQWidget( 0,
"KAccelEventHandler" )
114 kapp->installX11EventFilter( TQT_TQWIDGET(
this) );
119 bool qt_try_modal( TQWidget *, XEvent * );
121 bool KAccelEventHandler::x11Event( XEvent* pEvent )
123 if( TQWidget::keyboardGrabber() || !kapp->focusWidget() )
126 if ( !qt_try_modal(kapp->focusWidget(), pEvent) )
129 if( pEvent->type == XKeyPress ) {
130 unsigned int tmp = pEvent->xkey.state;
131 pEvent->xkey.state &= ~0x2000;
133 pEvent->xkey.state = tmp;
136 int keyCodeQt =
key.keyCodeQt();
138 if(
key.modFlags() & KKey::SHIFT ) state |= TQt::ShiftButton;
139 if(
key.modFlags() & KKey::CTRL ) state |= TQt::ControlButton;
140 if(
key.modFlags() & KKey::ALT ) state |= TQt::AltButton;
141 if(
key.modFlags() & KKey::WIN ) state |= TQt::MetaButton;
143 TQKeyEvent ke( TQEvent::AccelOverride, keyCodeQt, 0, state );
146 g_bAccelActivated =
false;
147 kapp->sendEvent( kapp->focusWidget(), &ke );
151 if( ke.isAccepted() && !g_bAccelActivated )
152 kde_g_bKillAccelOverride =
true;
155 return g_bAccelActivated;
166 KAccelPrivate::KAccelPrivate(
KAccel* pParent, TQWidget* pWatch )
167 : KAccelBase( KAccelBase::QT_KEYS )
172 m_bAutoUpdate =
true;
173 connect( (TQAccel*)m_pAccel, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(slotKeyPressed(
int)) );
175 #ifdef Q_WS_X11 //only makes sense if KAccelEventHandler is working
177 m_pWatch->installEventFilter(
this );
179 KAccelEventHandler::self();
182 void KAccelPrivate::setEnabled(
bool bEnabled )
184 m_bEnabled = bEnabled;
185 ((TQAccel*)m_pAccel)->setEnabled( bEnabled );
188 bool KAccelPrivate::setEnabled(
const TQString& sAction,
bool bEnable )
190 kdDebug(125) <<
"KAccelPrivate::setEnabled( \"" << sAction <<
"\", " << bEnable <<
" ): this = " <<
this <<
endl;
191 KAccelAction* pAction = actionPtr( sAction );
194 if( pAction->isEnabled() == bEnable )
197 pAction->setEnabled( bEnable );
199 TQMap<int, KAccelAction*>::const_iterator it = m_mapIDToAction.begin();
200 for( ; it != m_mapIDToAction.end(); ++it ) {
202 ((TQAccel*)m_pAccel)->setItemEnabled( it.key(), bEnable );
207 bool KAccelPrivate::removeAction(
const TQString& sAction )
212 KAccelAction* pAction = actions().actionPtr( sAction );
214 int nID = pAction->getID();
216 bool b = KAccelBase::remove( sAction );
217 ((TQAccel*)m_pAccel)->removeItem( nID );
223 bool KAccelPrivate::emitSignal( KAccelBase::Signal signal )
225 if( signal == KAccelBase::KEYCODE_CHANGED ) {
226 m_pAccel->emitKeycodeChanged();
232 bool KAccelPrivate::connectKey( KAccelAction& action,
const KKeyServer::Key& key )
235 int nID = ((TQAccel*)m_pAccel)->insertItem( keyQt );
236 m_mapIDToAction[nID] = &action;
237 m_mapIDToKey[nID] = keyQt;
239 if( action.objSlotPtr() && action.methodSlotPtr() ) {
241 ((TQAccel*)m_pAccel)->connectItem( nID, action.objSlotPtr(), action.methodSlotPtr() );
243 ((TQAccel*)m_pAccel)->connectItem( nID,
this, TQT_SLOT(slotKeyPressed(
int)));
245 if( !action.isEnabled() )
246 ((TQAccel*)m_pAccel)->setItemEnabled( nID,
false );
249 kdDebug(125) <<
"KAccelPrivate::connectKey( \"" << action.name() <<
"\", " << key.
key().
toStringInternal() <<
" = 0x" << TQString::number(keyQt,16) <<
" ): id = " << nID <<
" m_pObjSlot = " << action.objSlotPtr() <<
endl;
257 int nID = ((TQAccel*)m_pAccel)->insertItem( keyQt );
259 m_mapIDToKey[nID] = keyQt;
261 kdDebug(125) <<
"KAccelPrivate::connectKey( " << key.
key().
toStringInternal() <<
" = 0x" << TQString::number(keyQt,16) <<
" ): id = " << nID <<
endl;
265 bool KAccelPrivate::disconnectKey( KAccelAction& action,
const KKeyServer::Key& key )
268 TQMap<int, int>::iterator it = m_mapIDToKey.begin();
269 for( ; it != m_mapIDToKey.end(); ++it ) {
273 kdDebug(125) <<
"KAccelPrivate::disconnectKey( \"" << action.name() <<
"\", 0x" << TQString::number(keyQt,16) <<
" ) : id = " << nID <<
" m_pObjSlot = " << action.objSlotPtr() <<
endl;
274 ((TQAccel*)m_pAccel)->removeItem( nID );
275 m_mapIDToAction.remove( nID );
276 m_mapIDToKey.remove( it );
281 kdWarning(125) <<
"Didn't find key in m_mapIDToKey." <<
endl;
288 kdDebug(125) <<
"KAccelPrivate::disconnectKey( 0x" << TQString::number(keyQt,16) <<
" )" <<
endl;
289 TQMap<int, int>::iterator it = m_mapIDToKey.begin();
290 for( ; it != m_mapIDToKey.end(); ++it ) {
292 ((TQAccel*)m_pAccel)->removeItem( it.key() );
293 m_mapIDToKey.remove( it );
298 kdWarning(125) <<
"Didn't find key in m_mapIDTokey." <<
endl;
302 void KAccelPrivate::slotKeyPressed(
int id )
304 kdDebug(125) <<
"KAccelPrivate::slotKeyPressed( " <<
id <<
" )" <<
endl;
306 if( m_mapIDToKey.contains(
id ) ) {
307 KKey key = m_mapIDToKey[id];
309 TQPopupMenu* pMenu = createPopupMenu( m_pWatch, seq );
317 if( pMenu->count() == 2 && pMenu->accel(1).isEmpty() ) {
318 int iAction = pMenu->idAt(1);
319 slotMenuActivated( iAction );
321 connect( pMenu, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(slotMenuActivated(
int)) );
322 pMenu->exec( m_pWatch->mapToGlobal( TQPoint( 0, 0 ) ) );
323 disconnect( pMenu, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(slotMenuActivated(
int)) );
329 void KAccelPrivate::slotShowMenu()
333 void KAccelPrivate::slotMenuActivated(
int iAction )
335 kdDebug(125) <<
"KAccelPrivate::slotMenuActivated( " << iAction <<
" )" <<
endl;
336 KAccelAction* pAction = actions().actionPtr( iAction );
339 connect(
this, TQT_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
340 emit menuItemActivated();
341 disconnect(
this, TQT_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
344 emitActivatedSignal( pAction );
348 bool KAccelPrivate::eventFilter( TQObject* , TQEvent* pEvent )
350 if( pEvent->type() == TQEvent::AccelOverride && m_bEnabled ) {
351 TQKeyEvent* pKeyEvent = (TQKeyEvent*) pEvent;
352 KKey key( pKeyEvent );
355 TQMap<int, int>::iterator it = m_mapIDToKey.begin();
356 for( ; it != m_mapIDToKey.end(); ++it ) {
357 if( (*it) == keyCodeQt ) {
360 if( m_mapIDToAction.contains( nID ) ) {
362 KAccelAction* pAction = m_mapIDToAction[nID];
363 if( !pAction->isEnabled() )
366 TQGuardedPtr<KAccelPrivate> me = this;
367 connect(
this, TQT_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
368 emit menuItemActivated();
370 disconnect( me, TQT_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
373 emitActivatedSignal( pAction );
376 slotKeyPressed( nID );
379 KAccelEventHandler::accelActivated(
true );
388 void KAccelPrivate::emitActivatedSignal( KAccelAction* pAction )
391 TQGuardedPtr<KAccelPrivate> me =
this;
392 TQRegExp reg(
"([ ]*KAccelAction.*)" );
393 if( reg.search( pAction->methodSlotPtr()) >= 0 ) {
394 connect(
this, TQT_SIGNAL(menuItemActivated(KAccelAction*)),
395 pAction->objSlotPtr(), pAction->methodSlotPtr() );
396 emit menuItemActivated( pAction );
398 disconnect( me, TQT_SIGNAL(menuItemActivated(KAccelAction*)),
399 pAction->objSlotPtr(), pAction->methodSlotPtr() );
401 connect(
this, TQT_SIGNAL(menuItemActivated()),
402 pAction->objSlotPtr(), pAction->methodSlotPtr() );
403 emit menuItemActivated();
405 disconnect( me, TQT_SIGNAL(menuItemActivated()),
406 pAction->objSlotPtr(), pAction->methodSlotPtr() );
418 : TQAccel( pParent, (psName) ? psName :
"KAccel-TQAccel" )
420 kdDebug(125) <<
"KAccel( pParent = " << pParent <<
", psName = " << psName <<
" ): this = " <<
this <<
endl;
421 d =
new KAccelPrivate(
this, pParent );
425 : TQAccel( watch, pParent, (psName) ? psName :
"KAccel-TQAccel" )
427 kdDebug(125) <<
"KAccel( watch = " << watch <<
", pParent = " << pParent <<
", psName = " << psName <<
" ): this = " <<
this <<
endl;
429 kdDebug(125) << kdBacktrace() <<
endl;
430 d =
new KAccelPrivate(
this, watch );
435 kdDebug(125) <<
"~KAccel(): this = " <<
this <<
endl;
439 KAccelActions& KAccel::actions() {
return d->actions(); }
440 const KAccelActions& KAccel::actions()
const {
return d->actions(); }
445 KAccelAction*
KAccel::insert(
const TQString& sAction,
const TQString& sLabel,
const TQString& sWhatsThis,
447 const TQObject* pObjSlot,
const char* psMethodSlot,
448 bool bConfigurable,
bool bEnabled )
450 return d->insert( sAction, sLabel, sWhatsThis,
452 pObjSlot, psMethodSlot,
453 bConfigurable, bEnabled );
456 KAccelAction*
KAccel::insert(
const TQString& sAction,
const TQString& sLabel,
const TQString& sWhatsThis,
458 const TQObject* pObjSlot,
const char* psMethodSlot,
459 bool bConfigurable,
bool bEnabled )
461 return d->insert( sAction, sLabel, sWhatsThis,
463 pObjSlot, psMethodSlot,
464 bConfigurable, bEnabled );
468 const TQObject* pObjSlot,
const char* psMethodSlot,
469 bool bConfigurable,
bool bEnabled )
471 return d->insert( psAction, i18n(psAction), TQString::null,
473 pObjSlot, psMethodSlot,
474 bConfigurable, bEnabled );
478 const TQObject* pObjSlot,
const char* psMethodSlot,
479 bool bConfigurable,
bool bEnabled )
482 if( sAction.isEmpty() )
487 pObjSlot, psMethodSlot,
488 bConfigurable, bEnabled );
496 {
return d->removeAction( sAction ); }
498 {
return d->updateConnections(); }
502 const KAccelAction* pAction = actions().actionPtr( sAction );
506 bool KAccel::setSlot(
const TQString& sAction,
const TQObject* pObjSlot,
const char* psMethodSlot )
507 {
return d->setActionSlot( sAction, pObjSlot, psMethodSlot ); }
510 {
return d->setEnabled( sAction, bEnable ); }
514 kdDebug(125) <<
"KAccel::setShortcut( \"" << sAction <<
"\", " << cut.toStringInternal() <<
" )" <<
endl;
515 KAccelAction* pAction = actions().actionPtr( sAction );
517 if( pAction->shortcut() != cut )
518 return d->setShortcut( sAction, cut );
525 {
return d->configGroup(); }
528 { d->setConfigGroup( s ); }
532 d->readSettings( pConfig );
537 { d->writeSettings( pConfig );
return true; }
541 kdDebug(125) <<
"KAccel::emitKeycodeChanged()" <<
endl;
545 #ifndef KDE_NO_COMPAT
552 int , TQPopupMenu *,
bool bConfigurable )
555 bool b = d->insert( sAction, sLabel, TQString::null,
558 bConfigurable ) != 0;
564 int , TQPopupMenu*,
bool bConfigurable )
567 cut.
init( TQKeySequence(key) );
568 KAccelAction* pAction = d->insert( sAction, sLabel, TQString::null,
578 KAccelAction* pAction = d->insert(
KStdAccel::name(
id ), sLabel, TQString::null,
587 bool KAccel::connectItem(
const TQString& sAction,
const TQObject* pObjSlot,
const char* psMethodSlot,
bool bActivate )
589 kdDebug(125) <<
"KAccel::connectItem( " << sAction <<
", " << pObjSlot <<
", " << psMethodSlot <<
" )" <<
endl;
590 if( bActivate ==
false )
591 d->setActionEnabled( sAction,
false );
592 bool b =
setSlot( sAction, pObjSlot, psMethodSlot );
593 if( bActivate ==
true )
594 d->setActionEnabled( sAction,
true );
599 {
return d->removeAction( sAction ); }
606 KAccelAction* pAction = actions().actionPtr( action );
607 TQString s = menu->text(
id );
608 if( !pAction || s.isEmpty() )
611 int i = s.find(
'\t' );
613 TQString k = pAction->shortcut().seq(0).toString();
618 s.replace( i+1, s.length()-i, k );
624 TQPixmap *pp = menu->pixmap(
id);
625 if( pp && !pp->isNull() )
626 menu->changeItem( *pp, s,
id );
628 menu->changeItem( s,
id );
643 KAccelAction* pAction = d->actionPtr( sAction );
645 return pAction->shortcut().keyCodeQt();
651 KAccelAction* pAction = d->actionPtr(
KKey(key) );
653 return pAction->name();
655 return TQString::null;
657 #endif // !KDE_NO_COMPAT
659 void KAccel::virtual_hook(
int,
void* )
662 #include "kaccel.moc"
663 #include "kaccelprivate.moc"