1 //**************************************************************************************** 2 // 3 // File: DeskbarPulseView.cpp 4 // 5 // Written by: Daniel Switkin 6 // 7 // Copyright 1999, Be Incorporated 8 // 9 //**************************************************************************************** 10 11 12 #include "DeskbarPulseView.h" 13 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include <Alert.h> 19 #include <Application.h> 20 #include <Catalog.h> 21 #include <Deskbar.h> 22 #include <Roster.h> 23 24 #include "Common.h" 25 #include "Prefs.h" 26 #include "PulseApp.h" 27 28 #undef B_TRANSLATION_CONTEXT 29 #define B_TRANSLATION_CONTEXT "DeskbarPulseView" 30 31 32 DeskbarPulseView::DeskbarPulseView(BRect rect) : MiniPulseView(rect, "DeskbarPulseView") { 33 messagerunner = NULL; 34 prefs = NULL; 35 prefswindow = NULL; 36 } 37 38 DeskbarPulseView::DeskbarPulseView(BMessage *message) : MiniPulseView(message) { 39 mode1->SetLabel(B_TRANSLATE("Normal mode")); 40 mode1->SetMessage(new BMessage(PV_NORMAL_MODE)); 41 mode2->SetLabel(B_TRANSLATE("Mini mode")); 42 mode2->SetMessage(new BMessage(PV_MINI_MODE)); 43 quit = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(PV_QUIT), 0, 0); 44 popupmenu->AddSeparatorItem(); 45 popupmenu->AddItem(quit); 46 47 SetViewColor(B_TRANSPARENT_COLOR); 48 49 prefs = new Prefs(); 50 active_color.red = (prefs->deskbar_active_color & 0xff000000) >> 24; 51 active_color.green = (prefs->deskbar_active_color & 0x00ff0000) >> 16; 52 active_color.blue = (prefs->deskbar_active_color & 0x0000ff00) >> 8; 53 54 idle_color.red = (prefs->deskbar_idle_color & 0xff000000) >> 24; 55 idle_color.green = (prefs->deskbar_idle_color & 0x00ff0000) >> 16; 56 idle_color.blue = (prefs->deskbar_idle_color & 0x0000ff00) >> 8; 57 58 frame_color.red = (prefs->deskbar_frame_color & 0xff000000) >> 24; 59 frame_color.green = (prefs->deskbar_frame_color & 0x00ff0000) >> 16; 60 frame_color.blue = (prefs->deskbar_frame_color & 0x0000ff00) >> 8; 61 SetViewColor(idle_color); 62 63 messagerunner = NULL; 64 prefswindow = NULL; 65 } 66 67 void DeskbarPulseView::AttachedToWindow() { 68 BMessenger messenger(this); 69 mode1->SetTarget(messenger); 70 mode2->SetTarget(messenger); 71 preferences->SetTarget(messenger); 72 about->SetTarget(messenger); 73 quit->SetTarget(messenger); 74 75 system_info sys_info; 76 get_system_info(&sys_info); 77 if (sys_info.cpu_count >= 2) { 78 for (unsigned int x = 0; x < sys_info.cpu_count; x++) 79 cpu_menu_items[x]->SetTarget(messenger); 80 } 81 82 // Use a BMessageRunner to deliver periodic messsages instead 83 // of Pulse() events from the Deskbar - this is to avoid changing 84 // the current pulse rate and affecting other replicants 85 messagerunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE), 86 200000, -1); 87 } 88 89 void DeskbarPulseView::MouseDown(BPoint point) { 90 BPoint cursor; 91 uint32 buttons; 92 MakeFocus(true); 93 GetMouse(&cursor, &buttons, true); 94 95 if (buttons & B_PRIMARY_MOUSE_BUTTON) { 96 BMessage *message = Window()->CurrentMessage(); 97 int32 clicks = message->FindInt32("clicks"); 98 if (clicks >= 2) { 99 BMessenger messenger(this); 100 BMessage *m = new BMessage(PV_NORMAL_MODE); 101 messenger.SendMessage(m); 102 } 103 } else MiniPulseView::MouseDown(point); 104 } 105 106 void DeskbarPulseView::Pulse() { 107 // Override and do nothing here 108 } 109 110 void DeskbarPulseView::MessageReceived(BMessage *message) { 111 switch (message->what) { 112 case PV_NORMAL_MODE: 113 SetMode(true); 114 Remove(); 115 break; 116 case PV_MINI_MODE: 117 SetMode(false); 118 Remove(); 119 break; 120 case PV_PREFERENCES: 121 if (prefswindow != NULL) { 122 prefswindow->Activate(true); 123 break; 124 } 125 prefswindow = new PrefsWindow(prefs->prefs_window_rect, 126 "Pulse settings", new BMessenger(this), prefs); 127 prefswindow->Show(); 128 break; 129 case PV_ABOUT: { 130 PulseApp::ShowAbout(false); 131 break; 132 } 133 case PV_QUIT: 134 Remove(); 135 break; 136 case PRV_DESKBAR_CHANGE_COLOR: 137 UpdateColors(message); 138 break; 139 case PRV_DESKBAR_ICON_WIDTH: { 140 int width = message->FindInt32("width"); 141 ResizeTo(width - 1, 15); 142 Draw(Bounds()); 143 break; 144 } 145 case PV_REPLICANT_PULSE: 146 Update(); 147 Draw(Bounds()); 148 break; 149 case PRV_QUIT: 150 prefswindow = NULL; 151 break; 152 case PV_CPU_MENU_ITEM: 153 ChangeCPUState(message); 154 break; 155 default: 156 BView::MessageReceived(message); 157 break; 158 } 159 } 160 161 DeskbarPulseView *DeskbarPulseView::Instantiate(BMessage *data) { 162 if (!validate_instantiation(data, "DeskbarPulseView")) return NULL; 163 return new DeskbarPulseView(data); 164 } 165 166 status_t DeskbarPulseView::Archive(BMessage *data, bool deep) const { 167 PulseView::Archive(data, deep); 168 data->AddString("add_on", APP_SIGNATURE); 169 data->AddString("class", "DeskbarPulseView"); 170 return B_OK; 171 } 172 173 void DeskbarPulseView::Remove() { 174 // Remove ourselves from the deskbar by name 175 BDeskbar *deskbar = new BDeskbar(); 176 status_t err = deskbar->RemoveItem("DeskbarPulseView"); 177 if (err != B_OK) { 178 BString str; 179 snprintf(str.LockBuffer(512), 512, 180 B_TRANSLATE("Removing from Deskbar failed.\n%s"), strerror(err)); 181 str.UnlockBuffer(); 182 BAlert *alert = new BAlert(B_TRANSLATE("Info"), str.String(), 183 B_TRANSLATE("OK")); 184 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 185 alert->Go(NULL); 186 } 187 delete deskbar; 188 } 189 190 void DeskbarPulseView::SetMode(bool normal) { 191 if (normal) prefs->window_mode = NORMAL_WINDOW_MODE; 192 else prefs->window_mode = MINI_WINDOW_MODE; 193 prefs->Save(); 194 be_roster->Launch(APP_SIGNATURE); 195 } 196 197 DeskbarPulseView::~DeskbarPulseView() { 198 if (messagerunner != NULL) delete messagerunner; 199 if (prefswindow != NULL && prefswindow->Lock()) prefswindow->Quit(); 200 if (prefs != NULL) delete prefs; 201 } 202