karm

tdeaccelmenuwatch.cpp
1 /*
2 * tdeaccelmenuwatch.cpp -- Implementation of class TDEAccelMenuWatch.
3 * Author: Sirtaj Singh Kang
4 * Generated: Thu Jan 7 15:05:26 EST 1999
5 */
6 
7 #include <assert.h>
8 #include <tqpopupmenu.h>
9 
10 #include "tdeaccelmenuwatch.h"
11 
12 TDEAccelMenuWatch::TDEAccelMenuWatch( TDEAccel *accel, TQObject *parent )
13  : TQObject( parent ),
14  _accel( accel ),
15  _menu ( 0 )
16 {
17  _accList.setAutoDelete( true );
18  _menuList.setAutoDelete( false );
19 }
20 
21 void TDEAccelMenuWatch::setMenu( TQPopupMenu *menu )
22 {
23  assert( menu );
24 
25  // we use _menuList to ensure that the signal is
26  // connected only once per menu.
27 
28  if ( !_menuList.findRef( menu ) ) {
29  _menuList.append( menu );
30  connect( menu, TQ_SIGNAL(destroyed()), this, TQ_SLOT(removeDeadMenu()) );
31  }
32 
33  _menu = menu;
34 }
35 
36 void TDEAccelMenuWatch::connectAccel( int itemId, const char *action )
37 {
38  AccelItem *item = newAccelItem( _menu, itemId, StringAccel ) ;
39  item->action = TQString::fromLocal8Bit( action );
40 }
41 
42 void TDEAccelMenuWatch::connectAccel( int itemId, TDEStdAccel::StdAccel accel )
43 {
44  AccelItem *item = newAccelItem( _menu, itemId, StdAccel ) ;
45  item->stdAction = accel;
46 }
47 
49 {
50  assert( _accel != 0 );
51 
52  TQPtrListIterator<AccelItem> iter( _accList );
53  AccelItem *item;
54 
55  for( ; (item = iter.current()) ; ++iter ) {
56  // These setAccel calls were converted from all changeMenuAccel calls
57  // as descibed in KDE3PORTING.html
58  switch( item->type ) {
59  case StringAccel:
60  item->menu->setAccel( _accel->shortcut( item->action ).keyCodeQt(), item->itemId );
61  break;
62  case StdAccel:
63  item->menu->setAccel( TDEStdAccel::shortcut( item->stdAction ).keyCodeQt(), item->itemId );
64  break;
65  default:
66  break;
67  }
68  }
69 
70 }
71 
72 void TDEAccelMenuWatch::removeDeadMenu()
73 {
74  TQPopupMenu *sdr = (TQPopupMenu *) sender();
75  assert( sdr );
76 
77  if ( !_menuList.findRef( sdr ) )
78  return;
79 
80  // remove all accels
81 
82  AccelItem *accel;
83  for ( accel = _accList.first(); accel; accel = _accList.next() )
84  {
85 loop:
86  if( accel && accel->menu == sdr ) {
87  _accList.remove();
88  accel = _accList.current();
89  goto loop;
90  }
91  }
92 
93  // remove from menu list
94  _menuList.remove( sdr );
95 
96  return;
97 }
98 
99 TDEAccelMenuWatch::AccelItem *TDEAccelMenuWatch::newAccelItem( TQPopupMenu *,
100  int itemId, AccelType type )
101 {
102  AccelItem *item = new AccelItem;
103 
104  item->menu = _menu;
105  item->itemId = itemId;
106  item->type = type;
107 
108  _accList.append( item );
109 
110  return item;
111 }
112 
113 #include "tdeaccelmenuwatch.moc"
void connectAccel(int itemId, const char *action)
Connect the menu item identified to currentMenu()/id to the accelerator action.
TDEAccelMenuWatch(TDEAccel *accel, TQObject *parent=0)
TDEAccelMenuWatch Constructor.
void setMenu(TQPopupMenu *menu)
Set the menu on which connectAccel calls will operate.
void updateMenus()
Updates all menu accelerators.