xref: /haiku/src/apps/pulse/DeskbarPulseView.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
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 (int x = 0; x < sys_info.cpu_count; x++) {
79 			cpu_menu_items[x]->SetTarget(messenger);
80 		}
81 	}
82 
83 	// Use a BMessageRunner to deliver periodic messsages instead
84 	// of Pulse() events from the Deskbar - this is to avoid changing
85 	// the current pulse rate and affecting other replicants
86 	messagerunner = new BMessageRunner(messenger, new BMessage(PV_REPLICANT_PULSE),
87 		200000, -1);
88 }
89 
90 void DeskbarPulseView::MouseDown(BPoint point) {
91 	BPoint cursor;
92 	uint32 buttons;
93 	MakeFocus(true);
94 	GetMouse(&cursor, &buttons, true);
95 
96 	if (buttons & B_PRIMARY_MOUSE_BUTTON) {
97 		BMessage *message = Window()->CurrentMessage();
98 		int32 clicks = message->FindInt32("clicks");
99 		if (clicks >= 2) {
100 			BMessenger messenger(this);
101 			BMessage *m = new BMessage(PV_NORMAL_MODE);
102 			messenger.SendMessage(m);
103 		}
104 	} else MiniPulseView::MouseDown(point);
105 }
106 
107 void DeskbarPulseView::Pulse() {
108 	// Override and do nothing here
109 }
110 
111 void DeskbarPulseView::MessageReceived(BMessage *message) {
112 	switch (message->what) {
113 		case PV_NORMAL_MODE:
114 			SetMode(true);
115 			Remove();
116 			break;
117 		case PV_MINI_MODE:
118 			SetMode(false);
119 			Remove();
120 			break;
121 		case PV_PREFERENCES:
122 			if (prefswindow != NULL) {
123 				prefswindow->Activate(true);
124 				break;
125 			}
126 			prefswindow = new PrefsWindow(prefs->prefs_window_rect,
127 				"Pulse settings", new BMessenger(this), prefs);
128 			prefswindow->Show();
129 			break;
130 		case PV_ABOUT: {
131 			PulseApp::ShowAbout(false);
132 			break;
133 		}
134 		case PV_QUIT:
135 			Remove();
136 			break;
137 		case PRV_DESKBAR_CHANGE_COLOR:
138 			UpdateColors(message);
139 			break;
140 		case PRV_DESKBAR_ICON_WIDTH: {
141 			int width = message->FindInt32("width");
142 			ResizeTo(width - 1, 15);
143 			Draw(Bounds());
144 			break;
145 		}
146 		case PV_REPLICANT_PULSE:
147 			Update();
148 			Draw(Bounds());
149 			break;
150 		case PRV_QUIT:
151 			prefswindow = NULL;
152 			break;
153 		case PV_CPU_MENU_ITEM:
154 			ChangeCPUState(message);
155 			break;
156 		default:
157 			BView::MessageReceived(message);
158 			break;
159 	}
160 }
161 
162 DeskbarPulseView *DeskbarPulseView::Instantiate(BMessage *data) {
163 	if (!validate_instantiation(data, "DeskbarPulseView")) return NULL;
164 	return new DeskbarPulseView(data);
165 }
166 
167 status_t DeskbarPulseView::Archive(BMessage *data, bool deep) const {
168 	PulseView::Archive(data, deep);
169 	data->AddString("add_on", APP_SIGNATURE);
170 	data->AddString("class", "DeskbarPulseView");
171 	return B_OK;
172 }
173 
174 void DeskbarPulseView::Remove() {
175 	// Remove ourselves from the deskbar by name
176 	BDeskbar *deskbar = new BDeskbar();
177 	status_t err = deskbar->RemoveItem("DeskbarPulseView");
178 	if (err != B_OK) {
179 		BString str;
180 		snprintf(str.LockBuffer(512), 512,
181 			B_TRANSLATE("Removing from Deskbar failed.\n%s"), strerror(err));
182 		str.UnlockBuffer();
183 		BAlert *alert = new BAlert(B_TRANSLATE("Info"), str.String(),
184 			B_TRANSLATE("OK"));
185 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
186 		alert->Go(NULL);
187 	}
188 	delete deskbar;
189 }
190 
191 void DeskbarPulseView::SetMode(bool normal) {
192 	if (normal) prefs->window_mode = NORMAL_WINDOW_MODE;
193 	else prefs->window_mode = MINI_WINDOW_MODE;
194 	prefs->Save();
195 	be_roster->Launch(APP_SIGNATURE);
196 }
197 
198 DeskbarPulseView::~DeskbarPulseView() {
199 	if (messagerunner != NULL) delete messagerunner;
200 	if (prefswindow != NULL && prefswindow->Lock()) prefswindow->Quit();
201 	if (prefs != NULL) delete prefs;
202 }
203