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