summaryrefslogtreecommitdiffstats
path: root/kicker/kicker/core/menumanager.cpp
blob: 776056647f6e61a9e629853c40013e85a5684df5 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************

Copyright (c) 1996-2000 the kicker authors. See file AUTHORS.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#include <tqcursor.h>
#include <tqpixmap.h>
#include <tqtimer.h>

#include <kapplication.h>
#include <dcopclient.h>

#include "client_mnu.h"
#include "container_extension.h"
#include "global.h"
#include "k_new_mnu.h"
#include "k_mnu.h"
#include "k_mnu_stub.h"
#include "kicker.h"
#include "panelbutton.h"
#include "kickerSettings.h"

#include "menumanager.h"
#include "menumanager.moc"

// Why MenuManager doesn't use KStaticDeleter
// MenuManager gets created before the ExtensionManager
// So using KStaticDeleter results in MenuManager getting
// deleted before ExtensionManager, which means also the panels
// which means also the K Menu buttons. K Menu buttons call
// MenuManager in their dtor, so if MenuManager is already gone
// then every KButton will cause it to be reconstructed.
// So we rely on Kicker to delete MenuManager on the way out
// ensuring it's the last thing to go.
MenuManager* MenuManager::m_self = 0;

MenuManager* MenuManager::the()
{
    if (!m_self)
    {
        m_self = new MenuManager();
    }

    return m_self;
}

MenuManager::MenuManager(TQObject *parent)
    : TQObject(parent, "MenuManager"), DCOPObject("MenuManager")
{
    if (KickerSettings::legacyKMenu()) 
	m_kmenu = new KMenuStub(new PanelKMenu);
    else
	m_kmenu = new KMenuStub(new KMenu);

    kapp->dcopClient()->setNotifications(true);
    connect(kapp->dcopClient(), TQT_SIGNAL(applicationRemoved(const TQCString&)),
            this, TQT_SLOT(applicationRemoved(const TQCString&)));
}

MenuManager::~MenuManager()
{
    if (this == m_self)
    {
        m_self = 0;
    }

    delete m_kmenu;
}

void MenuManager::slotSetKMenuItemActive()
{
    m_kmenu->selectFirstItem();
}

void MenuManager::popupKMenu(const TQPoint &p)
{
    if (m_kmenu->isVisible())
    {
        m_kmenu->hide();
    }
    else if (p.isNull())
    {
        m_kmenu->popup(TQCursor::pos());
    }
    else
    {
        m_kmenu->popup( p );
    }
}

void MenuManager::registerKButton(PanelPopupButton *button)
{
    if (!button)
    {
        return;
    }

    m_kbuttons.append(button);
}

void MenuManager::unregisterKButton(PanelPopupButton *button)
{
    m_kbuttons.remove(button);
}

PanelPopupButton* MenuManager::findKButtonFor(TQWidget* menu)
{
    KButtonList::const_iterator itEnd = m_kbuttons.constEnd();
    for (KButtonList::const_iterator it = m_kbuttons.constBegin(); it != itEnd; ++it)
    {
        if ((*it)->popup() == menu)
        {
            return *it;
        }
    }

    return 0;
}

void MenuManager::kmenuAccelActivated()
{
    if (m_kmenu->isVisible())
    {
        m_kmenu->hide();
        return;
    }

    m_kmenu->initialize();

    if (m_kbuttons.isEmpty())
    {
        // no button to use, make it behave like a desktop menu
        TQPoint p;
        // Popup the K-menu at the center of the screen.
        TQDesktopWidget* desktop = KApplication::desktop();
        TQRect r;
        if (desktop->numScreens() < 2)
            r = desktop->geometry();
        else
            r = desktop->screenGeometry(desktop->screenNumber(TQCursor::pos()));
        // kMenu->rect() is not valid before showing, use tqsizeHint()
        p = r.center() - TQRect( TQPoint( 0, 0 ), m_kmenu->tqsizeHint()).center();
        m_kmenu->popup(p);

        // when the cursor is in the area where the menu pops up,
        // the item under the cursor gets selected. The single shot
        // avoids this from happening by allowing the item to be selected
        // when the event loop is enterred, and then resetting it.
        TQTimer::singleShot(0, this, TQT_SLOT(slotSetKMenuItemActive()));
    }
    else
    {
        // We need the kmenu's size to place it at the right position.
        // We cannot rely on the popup menu's current size(), if it wasn't
        // shown before, so we resize it here according to its tqsizeHint().
        const TQSize size = m_kmenu->tqsizeHint();
        m_kmenu->resize(size.width(),size.height());

        PanelPopupButton* button = findKButtonFor(m_kmenu->widget());

        // let's unhide the panel while we're at it. traverse the widget
        // hierarchy until we find the panel, if any
        TQObject* menuParent = button->tqparent();
        while (menuParent)
        {
            ExtensionContainer* ext = dynamic_cast<ExtensionContainer*>(menuParent);

            if (ext)
            {
                ext->unhideIfHidden();
                // make sure it's unhidden before we use it to figure out
                // where to popup
                tqApp->processEvents();
                break;
            }

            menuParent = menuParent->tqparent();
        }
        button->showMenu();
    }
}

TQCString MenuManager::createMenu(TQPixmap icon, TQString text)
{
    static int menucount = 0;
    menucount++;
    TQCString name;
    name.sprintf("kickerclientmenu-%d", menucount );
    KickerClientMenu* p = new KickerClientMenu( 0, name );
    clientmenus.append(p);
    m_kmenu->initialize();
    p->text = text;
    p->icon = icon;
    p->idInParentMenu = m_kmenu->insertClientMenu( p );
    p->createdBy = kapp->dcopClient()->senderId();
    m_kmenu->adjustSize();
    return name;
}

void MenuManager::removeMenu(TQCString menu)
{
    bool iterate = true, need_adjustSize = false;
    ClientMenuList::iterator it = clientmenus.begin();
    for (; it != clientmenus.end(); iterate ? ++it : it)
    {
        iterate = true;
        KickerClientMenu* m = *it;
        if (m->objId() == menu)
        {
            m_kmenu->removeClientMenu(m->idInParentMenu);
            it = clientmenus.erase(it);
            iterate = false;
            need_adjustSize = true;
        }
    }
    if (need_adjustSize)
        m_kmenu->adjustSize();
}


void MenuManager::applicationRemoved(const TQCString& appRemoved)
{
    bool iterate = true, need_adjustSize = false;
    ClientMenuList::iterator it = clientmenus.begin();
    for (; it != clientmenus.end(); iterate ? ++it : it)
    {
        iterate = true;
        KickerClientMenu* m = *it;
        if (m->createdBy == appRemoved)
        {
            m_kmenu->removeClientMenu(m->idInParentMenu);
            it = clientmenus.erase(it);
            iterate = false;
            need_adjustSize = true;
        }
    }
    if (need_adjustSize)
        m_kmenu->adjustSize();
}

bool MenuManager::process(const TQCString &fun, const TQByteArray &data,
				TQCString &replyType, TQByteArray &replyData)
{
    if ( fun == "createMenu(TQPixmap,TQString)" ) {
	TQDataStream dataStream( data, IO_ReadOnly );
	TQPixmap icon;
	TQString text;
	dataStream >> icon >> text;
	TQDataStream reply( replyData, IO_WriteOnly );
	reply << createMenu( icon, text );
	replyType = "TQCString";
	return true;
    } else if ( fun == "removeMenu(TQCString)" ) {
	TQDataStream dataStream( data, IO_ReadOnly );
	TQCString menu;
	dataStream >> menu;
	removeMenu( menu );
	replyType = "void";
	return true;
    }
    return false;
}