23 #include <tqguardedptr.h>
24 #include <tqpopupmenu.h>
29 #include "tdeaccelbase.h"
30 #include <tdeapplication.h>
33 #include <tdeshortcut.h>
35 #include "tdeaccelprivate.h"
38 # include <X11/Xlib.h>
41 const int XKeyPress = KeyPress;
81 bool kde_g_bKillAccelOverride =
false;
83 class TDEAccelEventHandler :
public TQWidget
86 static TDEAccelEventHandler*
self()
89 g_pSelf =
new TDEAccelEventHandler;
93 static void accelActivated(
bool b ) { g_bAccelActivated = b; }
96 TDEAccelEventHandler();
99 bool x11Event( XEvent* pEvent );
102 static TDEAccelEventHandler* g_pSelf;
103 static bool g_bAccelActivated;
106 TDEAccelEventHandler* TDEAccelEventHandler::g_pSelf = 0;
107 bool TDEAccelEventHandler::g_bAccelActivated =
false;
109 TDEAccelEventHandler::TDEAccelEventHandler()
110 : TQWidget( 0,
"TDEAccelEventHandler" )
114 kapp->installX11EventFilter(
this );
119 bool tqt_try_modal( TQWidget *, XEvent * );
121 bool TDEAccelEventHandler::x11Event( XEvent* pEvent )
123 if( TQWidget::keyboardGrabber() || !kapp->focusWidget() )
126 if ( !tqt_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 TDEAccelPrivate::TDEAccelPrivate(
TDEAccel* pParent, TQWidget* pWatch )
167 : TDEAccelBase( TDEAccelBase::QT_KEYS )
172 m_bAutoUpdate =
true;
173 connect( (TQAccel*)m_pAccel, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(slotKeyPressed(
int)) );
177 m_pWatch->installEventFilter(
this );
179 TDEAccelEventHandler::self();
182 void TDEAccelPrivate::setEnabled(
bool bEnabled )
184 m_bEnabled = bEnabled;
185 ((TQAccel*)m_pAccel)->setEnabled( bEnabled );
188 bool TDEAccelPrivate::setEnabled(
const TQString& sAction,
bool bEnable )
190 kdDebug(125) <<
"TDEAccelPrivate::setEnabled( \"" << sAction <<
"\", " << bEnable <<
" ): this = " <<
this <<
endl;
191 TDEAccelAction* pAction = actionPtr( sAction );
194 if( pAction->isEnabled() == bEnable )
197 pAction->setEnabled( bEnable );
199 TQMap<int, TDEAccelAction*>::const_iterator it = m_mapIDToAction.begin();
200 for( ; it != m_mapIDToAction.end(); ++it ) {
202 ((TQAccel*)m_pAccel)->setItemEnabled( it.key(), bEnable );
207 bool TDEAccelPrivate::removeAction(
const TQString& sAction )
212 TDEAccelAction* pAction = actions().actionPtr( sAction );
214 int nID = pAction->getID();
216 bool b = TDEAccelBase::remove( sAction );
217 ((TQAccel*)m_pAccel)->removeItem( nID );
223 bool TDEAccelPrivate::emitSignal( TDEAccelBase::Signal signal )
225 if( signal == TDEAccelBase::KEYCODE_CHANGED ) {
226 m_pAccel->emitKeycodeChanged();
232 bool TDEAccelPrivate::connectKey( TDEAccelAction& action,
const KKeyServer::Key& key )
234 uint keyQt =
key.keyCodeQt();
235 int nID = ((TQAccel*)m_pAccel)->insertItem( keyQt );
236 m_mapIDToAction[nID] = &
action;
237 m_mapIDToKey[nID] = keyQt;
241 ((TQAccel*)m_pAccel)->connectItem( nID,
action.objSlotPtr(),
action.methodSlotPtr() );
243 ((TQAccel*)m_pAccel)->connectItem( nID,
this, TQ_SLOT(slotKeyPressed(
int)));
246 ((TQAccel*)m_pAccel)->setItemEnabled( nID,
false );
249 kdDebug(125) <<
"TDEAccelPrivate::connectKey( \"" <<
action.name() <<
"\", " <<
key.key().toStringInternal() <<
" = 0x" << TQString::number(keyQt,16) <<
" ): id = " << nID <<
" m_pObjSlot = " <<
action.objSlotPtr() <<
endl;
256 uint keyQt =
key.keyCodeQt();
257 int nID = ((TQAccel*)m_pAccel)->insertItem( keyQt );
259 m_mapIDToKey[nID] = keyQt;
261 kdDebug(125) <<
"TDEAccelPrivate::connectKey( " <<
key.key().toStringInternal() <<
" = 0x" << TQString::number(keyQt,16) <<
" ): id = " << nID <<
endl;
265 bool TDEAccelPrivate::disconnectKey( TDEAccelAction& action,
const KKeyServer::Key& key )
267 int keyQt =
key.keyCodeQt();
268 TQMap<int, int>::iterator it = m_mapIDToKey.begin();
269 for( ; it != m_mapIDToKey.end(); ++it ) {
273 kdDebug(125) <<
"TDEAccelPrivate::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;
287 int keyQt =
key.keyCodeQt();
288 kdDebug(125) <<
"TDEAccelPrivate::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 TDEAccelPrivate::slotKeyPressed(
int id )
304 kdDebug(125) <<
"TDEAccelPrivate::slotKeyPressed( " <<
id <<
" )" <<
endl;
306 if( m_mapIDToKey.contains(
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, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(slotMenuActivated(
int)) );
322 pMenu->exec( m_pWatch->mapToGlobal( TQPoint( 0, 0 ) ) );
323 disconnect( pMenu, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(slotMenuActivated(
int)) );
329 void TDEAccelPrivate::slotShowMenu()
333 void TDEAccelPrivate::slotMenuActivated(
int iAction )
335 kdDebug(125) <<
"TDEAccelPrivate::slotMenuActivated( " << iAction <<
" )" <<
endl;
336 TDEAccelAction* pAction = actions().actionPtr( iAction );
339 connect(
this, TQ_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
340 emit menuItemActivated();
341 disconnect(
this, TQ_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
344 emitActivatedSignal( pAction );
348 bool TDEAccelPrivate::eventFilter( TQObject* , TQEvent* pEvent )
350 if( pEvent->type() == TQEvent::AccelOverride && m_bEnabled ) {
351 TQKeyEvent* pKeyEvent = (TQKeyEvent*) pEvent;
353 kdDebug(125) <<
"TDEAccelPrivate::eventFilter( AccelOverride ): this = " <<
this <<
", key = " <<
key.toStringInternal() <<
endl;
354 int keyCodeQt =
key.keyCodeQt();
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 TDEAccelAction* pAction = m_mapIDToAction[nID];
363 if( !pAction->isEnabled() )
366 TQGuardedPtr<TDEAccelPrivate> me =
this;
367 connect(
this, TQ_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
368 emit menuItemActivated();
370 disconnect( me, TQ_SIGNAL(menuItemActivated()), pAction->objSlotPtr(), pAction->methodSlotPtr() );
373 emitActivatedSignal( pAction );
376 slotKeyPressed( nID );
379 TDEAccelEventHandler::accelActivated(
true );
388 void TDEAccelPrivate::emitActivatedSignal( TDEAccelAction* pAction )
391 TQGuardedPtr<TDEAccelPrivate> me =
this;
392 TQRegExp reg(
"([ ]*TDEAccelAction.*)" );
393 if( reg.search( pAction->methodSlotPtr()) >= 0 ) {
394 connect(
this, TQ_SIGNAL(menuItemActivated(TDEAccelAction*)),
395 pAction->objSlotPtr(), pAction->methodSlotPtr() );
396 emit menuItemActivated( pAction );
398 disconnect( me, TQ_SIGNAL(menuItemActivated(TDEAccelAction*)),
399 pAction->objSlotPtr(), pAction->methodSlotPtr() );
401 connect(
this, TQ_SIGNAL(menuItemActivated()),
402 pAction->objSlotPtr(), pAction->methodSlotPtr() );
403 emit menuItemActivated();
405 disconnect( me, TQ_SIGNAL(menuItemActivated()),
406 pAction->objSlotPtr(), pAction->methodSlotPtr() );
418 : TQAccel( pParent, (psName) ? psName :
"TDEAccel-TQAccel" )
420 kdDebug(125) <<
"TDEAccel( pParent = " << pParent <<
", psName = " << psName <<
" ): this = " <<
this <<
endl;
421 d =
new TDEAccelPrivate(
this, pParent );
425 : TQAccel( watch, pParent, (psName) ? psName :
"TDEAccel-TQAccel" )
427 kdDebug(125) <<
"TDEAccel( watch = " << watch <<
", pParent = " << pParent <<
", psName = " << psName <<
" ): this = " <<
this <<
endl;
429 kdDebug(125) << kdBacktrace() <<
endl;
430 d =
new TDEAccelPrivate(
this, watch );
433 TDEAccel::~TDEAccel()
435 kdDebug(125) <<
"~TDEAccel(): this = " <<
this <<
endl;
439 TDEAccelActions& TDEAccel::actions() {
return d->actions(); }
440 const TDEAccelActions& TDEAccel::actions()
const {
return d->actions(); }
445 TDEAccelAction*
TDEAccel::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 TDEAccelAction*
TDEAccel::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 TDEAccelAction* pAction = actions().actionPtr( sAction );
506 bool TDEAccel::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) <<
"TDEAccel::setShortcut( \"" << sAction <<
"\", " << cut.toStringInternal() <<
" )" <<
endl;
515 TDEAccelAction* 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) <<
"TDEAccel::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 TDEAccelAction* pAction = d->insert( sAction, sLabel, TQString::null,
578 TDEAccelAction* pAction = d->insert(
TDEStdAccel::name(
id ), sLabel, TQString::null,
587 bool TDEAccel::connectItem(
const TQString& sAction,
const TQObject* pObjSlot,
const char* psMethodSlot,
bool bActivate )
589 kdDebug(125) <<
"TDEAccel::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 TDEAccelAction* 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 TDEAccelAction* pAction = d->actionPtr( sAction );
645 return pAction->shortcut().keyCodeQt();
651 TDEAccelAction* pAction = d->actionPtr(
KKey(key) );
653 return pAction->name();
655 return TQString::null;
659 void TDEAccel::virtual_hook(
int,
void* )
662 #include "tdeaccel.moc"
663 #include "tdeaccelprivate.moc"
Representation of a key in the format native of the windowing system (eg.
A KKeySequence object holds a sequence of up to 4 keys.
A KKey object represents a single key with possible modifiers (Shift, Ctrl, Alt, Win).
int keyCodeQt() const
Returns the qt key code.
void keycodeChanged()
Emitted when one of the key codes has changed.
bool removeItem(const TQString &sAction) TDE_DEPRECATED
const TQString & configGroup() const
Returns the configuration group of the settings.
bool insertItem(const TQString &sLabel, const TQString &sAction, const char *psKey, int nIDMenu=0, TQPopupMenu *pMenu=0, bool bConfigurable=true) TDE_DEPRECATED
bool connectItem(const TQString &sAction, const TQObject *pObjSlot, const char *psMethodSlot, bool bActivate=true) TDE_DEPRECATED
bool insertStdItem(TDEStdAccel::StdAccel id, const TQString &descr=TQString::null) TDE_DEPRECATED
bool readSettings(TDEConfigBase *pConfig=0)
Read all shortcuts from pConfig, or (if pConfig is zero) from the application's configuration file TD...
void emitKeycodeChanged()
Emits the keycodeChanged() signal.
bool updateConnections()
Updates the connections of the accelerations after changing them.
bool setAutoUpdate(bool bAuto)
Enable auto-update of connections.
bool writeSettings(TDEConfigBase *pConfig=0) const
Write the current shortcuts to pConfig, or (if pConfig is zero) to the application's configuration fi...
bool setSlot(const TQString &sAction, const TQObject *pObjSlot, const char *psMethodSlot)
Set the slot to be called when the shortcut of the action named by sAction is pressed.
void setEnabled(bool bEnabled)
Enables or disables the TDEAccel.
static int stringToKey(const TQString &) TDE_DEPRECATED
TQString findKey(int key) const TDE_DEPRECATED
bool remove(const TQString &sAction)
bool setItemEnabled(const TQString &sAction, bool bEnable) TDE_DEPRECATED
const TDEShortcut & shortcut(const TQString &sAction) const
Return the shortcut associated with the action named by sAction.
TDEAccel(TQWidget *pParent, const char *psName=0)
Creates a new TDEAccel that watches pParent, which is also the TQObject's parent.
TDEAccelAction * insert(const TQString &sAction, const TQString &sLabel, const TQString &sWhatsThis, const TDEShortcut &cutDef, const TQObject *pObjSlot, const char *psMethodSlot, bool bConfigurable=true, bool bEnabled=true)
Create an accelerator action.
bool setShortcut(const TQString &sAction, const TDEShortcut &shortcut)
Set the shortcut to be associated with the action named by sAction.
int currentKey(const TQString &action) const TDE_DEPRECATED
bool isEnabled()
Checks whether the TDEAccel is active.
void setConfigGroup(const TQString &name)
Returns the configuration group of the settings.
void changeMenuAccel(TQPopupMenu *menu, int id, const TQString &action) TDE_DEPRECATED
virtual bool isEnabled() const
KDE Configuration Management abstract base class.
The TDEShortcut class is used to represent a keyboard shortcut to an action.
static TDEShortcut & null()
Returns a null shortcut.
kndbgstream & endl(kndbgstream &s)
Does nothing.
kdbgstream kdWarning(int area=0)
Returns a warning stream.
kdbgstream kdDebug(int area=0)
Returns a debug stream.
kdbgstream & endl(kdbgstream &s)
Prints an "\n".
TDEAction * action(StdAction act_enum, const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0L)
StdAccel
Defines the identifier of all standard accelerators.
TDEShortcut shortcutDefault3(StdAccel id)
Returns the hardcoded default 3 modifier shortcut for id.
TDEShortcut shortcutDefault4(StdAccel id)
Returns the hardcoded default 4 modifier shortcut for id.
TQString name(StdAccel id)
Returns a unique name for the given accel.
TQString whatsThis(StdAccel)
Returns an extended WhatsThis description for the given accelerator.
TQString label(StdAccel id)
Returns a localized label for user-visible display.
const TDEShortcut & shortcut(StdAccel id)
Returns the keybinding for accel.