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 <MenuItem.h> 25 #include <Window.h> 26 27 #include <stdio.h> 28 29 30 PriorityMenu::PriorityMenu(thread_id thread, int32 priority) 31 : BMenu(B_EMPTY_STRING), 32 fThreadID(thread), 33 fPriority(priority) 34 { 35 } 36 37 38 void 39 PriorityMenu::Update(int32 priority) 40 { 41 if (priority != fPriority && CountItems() > 0) 42 RemoveItems(0, CountItems(), true); 43 if (CountItems() < 1) 44 BuildMenu(); 45 46 fPriority = priority; 47 } 48 49 50 typedef struct { 51 char name[32]; 52 long priority; 53 } PriorityRec; 54 55 static PriorityRec priorities[] = { 56 {"Idle", 0}, 57 {"Lowest Active", 1}, 58 {"Low", 5}, 59 {"Normal", 10}, 60 {"Display", 15}, 61 {"Urgent Display", 20}, 62 {"Real Time Display", 100}, 63 {"Urgent", 110}, 64 {"Real Time", 120}, 65 {"", -1} 66 }; 67 68 PriorityRec customPriority = { "Custom", 0 }; 69 70 71 void 72 PriorityMenu::BuildMenu() 73 { 74 BMenuItem* item; 75 BMessage* message; 76 char name[B_OS_NAME_LENGTH + 20]; 77 long found = false; 78 79 for (long index = 0; ; index++) { 80 PriorityRec *priority = &priorities[index]; 81 if (priority->priority < 0) 82 break; 83 if (!found && fPriority < priority->priority) { 84 priority = &customPriority; 85 priority->priority = fPriority; 86 index--; 87 } 88 message = new BMessage('PrTh'); 89 message->AddInt32("thread", fThreadID); 90 message->AddInt32("priority", priority->priority); 91 sprintf(name, "%s Priority [%d]", priority->name, (int)priority->priority); 92 item = new BMenuItem(name, message); 93 item->SetTarget(gPCView); 94 if (fPriority == priority->priority) 95 found = true, item->SetMarked(true); 96 AddItem(item); 97 } 98 } 99