xref: /haiku/src/apps/processcontroller/PriorityMenu.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2 	ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 	Copyright (C) 2004 beunited.org
4 
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.
9 
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.
14 
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
18 */
19 
20 
21 #include "PriorityMenu.h"
22 #include "ProcessController.h"
23 
24 #include <Catalog.h>
25 #include <MenuItem.h>
26 #include <Window.h>
27 
28 #include <stdio.h>
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "ProcessController"
32 
33 PriorityMenu::PriorityMenu(thread_id thread, int32 priority)
34 	: BMenu(B_EMPTY_STRING),
35 	fThreadID(thread),
36 	fPriority(priority)
37 {
38 }
39 
40 
41 void
42 PriorityMenu::Update(int32 priority)
43 {
44 	if (priority != fPriority && CountItems() > 0)
45 		RemoveItems(0, CountItems(), true);
46 	if (CountItems() < 1)
47 		BuildMenu();
48 
49 	fPriority = priority;
50 }
51 
52 
53 typedef struct {
54 	const char*	name;
55 	long		priority;
56 } PriorityRec;
57 
58 static PriorityRec	priorities[] = {
59 	{ B_TRANSLATE("Idle priority"),	0 },
60 	{ B_TRANSLATE("Lowest active priority"), 1 },
61 	{ B_TRANSLATE("Low priority"), 5 },
62 	{ B_TRANSLATE("Normal priority"), 10 },
63 	{ B_TRANSLATE("Display priority"), 15 },
64 	{ B_TRANSLATE("Urgent display priority"), 20 },
65 	{ B_TRANSLATE("Real-time display priority"), 100 },
66 	{ B_TRANSLATE("Urgent priority"), 110 },
67 	{ B_TRANSLATE("Real-time priority"), 120 },
68 	{ "",	-1 }
69 };
70 
71 PriorityRec customPriority = { B_TRANSLATE("Custom priority"), 0 };
72 
73 
74 void
75 PriorityMenu::BuildMenu()
76 {
77 	BMenuItem* item;
78 	BMessage* message;
79 	long found = false;
80 
81 	for (long index = 0; ; index++) {
82 		PriorityRec	*priority = &priorities[index];
83 		if (priority->priority < 0)
84 			break;
85 		if (!found && fPriority < priority->priority) {
86 			priority = &customPriority;
87 			priority->priority = fPriority;
88 			index--;
89 		}
90 		message = new BMessage('PrTh');
91 		message->AddInt32("thread", fThreadID);
92 		message->AddInt32("priority", priority->priority);
93 		BString name;
94 		const size_t size = B_OS_NAME_LENGTH * 4;
95 		snprintf(name.LockBuffer(size), size,
96 			"%s [%d]", priority->name, (int)priority->priority);
97 		name.UnlockBuffer();
98 		item = new BMenuItem(name.String(), message);
99 		item->SetTarget(gPCView);
100 		if (fPriority == priority->priority)
101 			found = true, item->SetMarked(true);
102 		AddItem(item);
103 	}
104 }
105 
106