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