btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / processcontroller / QuitMenu.cpp
blob1ed69b0a7db5572742cf1ca8fe6dd3a1f6edc5ee
1 /*
2 ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 Copyright (C) 2004 beunited.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "QuitMenu.h"
22 #include "IconMenuItem.h"
23 #include "ProcessController.h"
25 #include <Roster.h>
26 #include <Window.h>
27 #include <stdio.h>
30 class QuitMenuItem : public IconMenuItem {
31 public:
32 QuitMenuItem(team_id team, BBitmap* icon, const char* title,
33 BMessage* m, bool purge = false);
34 team_id Team() { return fTeam; }
36 private:
37 team_id fTeam;
41 QuitMenuItem::QuitMenuItem(team_id team, BBitmap* icon, const char* title,
42 BMessage* m, bool purge)
44 IconMenuItem(icon, title, m, true, purge), fTeam(team)
49 // #pragma mark -
52 QuitMenu::QuitMenu(const char* title, info_pack* infos, int infosCount)
53 : BMenu(title),
54 fInfos(infos),
55 fInfosCount(infosCount),
56 fMe(NULL)
58 SetTargetForItems(gPCView);
62 void
63 QuitMenu::AttachedToWindow()
65 if (!fMe)
66 fMe = new BMessenger(this);
68 be_roster->StartWatching(*fMe, B_REQUEST_LAUNCHED | B_REQUEST_QUIT);
69 BList apps;
70 team_id tmid;
71 be_roster->GetAppList(&apps);
72 for (int t = CountItems() - 1; t >= 0; t--) {
73 QuitMenuItem* item = (QuitMenuItem*)ItemAt(t);
74 bool found = false;
75 for (int a = 0; !found && (tmid = (team_id)(addr_t)apps.ItemAt(a)) != 0; a++)
76 if (item->Team() == tmid)
77 found = true;
78 if (!found)
79 RemoveItem(t);
81 for (int a = 0; (tmid = (team_id)(addr_t) apps.ItemAt(a)) != 0; a++) {
82 AddTeam(tmid);
85 BMenu::AttachedToWindow();
89 void
90 QuitMenu::DetachedFromWindow()
92 BMenu::DetachedFromWindow();
93 be_roster->StopWatching(*fMe);
94 delete fMe;
95 fMe = NULL;
99 void
100 QuitMenu::AddTeam(team_id tmid)
102 int t = 0;
103 QuitMenuItem* item;
104 while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) {
105 if (item->Team() == tmid)
106 return;
109 t = 0;
110 while (t < fInfosCount && tmid != fInfos[t].team_info.team) {
111 t++;
114 BMessage* message = new BMessage ('QtTm');
115 message->AddInt32 ("team", tmid);
116 item = NULL;
117 if (t < fInfosCount)
118 item = new QuitMenuItem(tmid, fInfos[t].team_icon, fInfos[t].team_name,
119 message);
120 else {
121 info_pack infos;
122 if (get_team_info(tmid, &infos.team_info) == B_OK
123 && get_team_name_and_icon(infos, true))
124 item = new QuitMenuItem(tmid, infos.team_icon, infos.team_name,
125 message, true);
127 if (item) {
128 item->SetTarget(gPCView);
129 AddItem(item);
130 } else
131 delete message;
135 void
136 QuitMenu::MessageReceived(BMessage *msg)
138 switch (msg->what) {
139 case B_SOME_APP_LAUNCHED:
141 int32 tmid;
142 if (msg->FindInt32("be:team", &tmid) == B_OK)
143 AddTeam(tmid);
144 break;
146 case B_SOME_APP_QUIT:
148 int32 tmid;
149 if (msg->FindInt32("be:team", &tmid) == B_OK) {
150 QuitMenuItem* item;
151 int t = 0;
152 while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) {
153 if (item->Team() == tmid) {
154 delete RemoveItem(--t);
155 return;
159 break;
162 default:
163 BMenu::MessageReceived(msg);