summaryrefslogtreecommitdiffstats
path: root/karm/tdeaccelmenuwatch.cpp
blob: 5cb223664478818313ac4adb320bea02e97f34ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* tdeaccelmenuwatch.cpp -- Implementation of class TDEAccelMenuWatch.
* Author:    Sirtaj Singh Kang
* Generated: Thu Jan  7 15:05:26 EST 1999
*/

#include <assert.h>
#include <tqpopupmenu.h>

#include "tdeaccelmenuwatch.h"

TDEAccelMenuWatch::TDEAccelMenuWatch( TDEAccel *accel, TQObject *parent )
  : TQObject( parent ),
  _accel( accel ),
  _menu ( 0 )
{
  _accList.setAutoDelete( true );
  _menuList.setAutoDelete( false );
}

void TDEAccelMenuWatch::setMenu( TQPopupMenu *menu )
{
  assert( menu );

  // we use  _menuList to ensure that the signal is
  // connected only once per menu.

  if ( !_menuList.findRef( menu ) ) {
    _menuList.append( menu );
    connect( menu, TQT_SIGNAL(destroyed()), this, TQT_SLOT(removeDeadMenu()) );
  }

  _menu = menu;
}

void TDEAccelMenuWatch::connectAccel( int itemId, const char *action )
{
  AccelItem *item = newAccelItem( _menu, itemId, StringAccel ) ;
  item->action  = TQString::fromLocal8Bit( action );
}

void TDEAccelMenuWatch::connectAccel( int itemId, TDEStdAccel::StdAccel accel )
{
  AccelItem *item = newAccelItem( _menu, itemId, StdAccel ) ;
  item->stdAction  = accel;
}

void TDEAccelMenuWatch::updateMenus()
{
  assert( _accel != 0 );

  TQPtrListIterator<AccelItem> iter( _accList );
  AccelItem *item;

  for( ; (item = iter.current()) ; ++iter ) {
    // These setAccel calls were converted from all changeMenuAccel calls
    // as descibed in KDE3PORTING.html
    switch( item->type ) {
      case StringAccel:
        item->menu->setAccel( _accel->shortcut( item->action ).keyCodeQt(), item->itemId );
        break;
      case StdAccel:
        item->menu->setAccel( TDEStdAccel::shortcut( item->stdAction ).keyCodeQt(), item->itemId );
        break;
      default:
        break;
    }
  }

}

void TDEAccelMenuWatch::removeDeadMenu()
{
  TQPopupMenu *sdr = (TQPopupMenu *) sender();
  assert( sdr );

  if ( !_menuList.findRef( sdr ) )
    return;

  // remove all accels

  AccelItem *accel;
  for ( accel = _accList.first(); accel; accel = _accList.next() )
  {
loop:
    if( accel && accel->menu == sdr ) {
      _accList.remove();
      accel = _accList.current();
      goto loop;
    }
  }

  // remove from menu list
  _menuList.remove( sdr );

  return;
}

TDEAccelMenuWatch::AccelItem *TDEAccelMenuWatch::newAccelItem( TQPopupMenu *,
    int itemId, AccelType type )
{
  AccelItem *item = new AccelItem;

  item->menu  = _menu;
  item->itemId  = itemId;
  item->type  = type;

  _accList.append( item );

  return item;
}

#include "tdeaccelmenuwatch.moc"