xref: /haiku/src/apps/pulse/ConfigView.cpp (revision 4afae676ad98b8f1e219f704dfc9c8507bce106e)
1 //****************************************************************************************
2 //
3 //	File:		ConfigView.cpp
4 //
5 //	Written by:	Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
10 
11 #include "ConfigView.h"
12 #include "Common.h"
13 #include "PulseApp.h"
14 #include "PrefsWindow.h"
15 #include <interface/Box.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 
19 RTColorControl::RTColorControl(BPoint point, BMessage *message)
20 	: BColorControl(point, B_CELLS_32x8, 6, "ColorControl", message, false) {
21 
22 }
23 
24 // Send a message every time the color changes, not just
25 // when the mouse button is released
26 void RTColorControl::SetValue(int32 color) {
27 	BColorControl::SetValue(color);
28 	Invoke();
29 }
30 
31 // A single class for all three prefs views, needs to be
32 // customized below to give each control the right message
33 ConfigView::ConfigView(BRect rect, const char *name, int mode, Prefs *prefs) :
34 	BView(rect, name, B_FOLLOW_NONE, B_WILL_DRAW) {
35 
36 	this->mode = mode;
37 	first_time_attached = true;
38 	fadecolors = NULL;
39 	active = idle = frame = NULL;
40 	iconwidth = NULL;
41 
42 	BRect r(6, 5, 296, 115);
43 	BBox *bbox = new BBox(r, "BBox");
44 	bbox->SetLabel("Bar Colors");
45 	AddChild(bbox);
46 
47 	if (mode == NORMAL_WINDOW_MODE) {
48 		colorcontrol = new RTColorControl(BPoint(10, 20),
49 			new BMessage(PRV_NORMAL_CHANGE_COLOR));
50 	} else if (mode == MINI_WINDOW_MODE) {
51 		colorcontrol = new RTColorControl(BPoint(10, 20),
52 			new BMessage(PRV_MINI_CHANGE_COLOR));
53 	} else {
54 		colorcontrol = new RTColorControl(BPoint(10, 20),
55 			new BMessage(PRV_DESKBAR_CHANGE_COLOR));
56 	}
57 
58 	bbox->AddChild(colorcontrol);
59 	r = colorcontrol->Frame();
60 	r.top = r.bottom + 10;
61 	r.bottom = r.top + 15;
62 
63 	if (mode == NORMAL_WINDOW_MODE) {
64 		r.right = r.left + be_plain_font->StringWidth("Fade colors") + 20;
65 		fadecolors = new BCheckBox(r, "FadeColors", "Fade colors",
66 			new BMessage(PRV_NORMAL_FADE_COLORS));
67 		bbox->AddChild(fadecolors);
68 
69 		colorcontrol->SetValue(prefs->normal_bar_color);
70 		fadecolors->SetValue(prefs->normal_fade_colors);
71 	} else if (mode == MINI_WINDOW_MODE) {
72 		r.right = r.left + be_plain_font->StringWidth("Active color") + 20;
73 		active = new BRadioButton(r, "ActiveColor", "Active color",
74 			new BMessage(PRV_MINI_ACTIVE));
75 		bbox->AddChild(active);
76 		active->SetValue(B_CONTROL_ON);
77 
78 		r.left = r.right + 5;
79 		r.right = r.left + be_plain_font->StringWidth("Idle color") + 20;
80 		idle = new BRadioButton(r, "IdleColor", "Idle color",
81 			new BMessage(PRV_MINI_IDLE));
82 		bbox->AddChild(idle);
83 
84 		r.left = r.right + 5;
85 		r.right = r.left + be_plain_font->StringWidth("Frame color") + 20;
86 		frame = new BRadioButton(r, "FrameColor", "Frame color",
87 			new BMessage(PRV_MINI_FRAME));
88 		bbox->AddChild(frame);
89 
90 		colorcontrol->SetValue(prefs->mini_active_color);
91 	} else {
92 		bbox->ResizeBy(0, 20);
93 
94 		r.right = r.left + be_plain_font->StringWidth("Active color") + 20;
95 		active = new BRadioButton(r, "ActiveColor", "Active color",
96 			new BMessage(PRV_DESKBAR_ACTIVE));
97 		bbox->AddChild(active);
98 		active->SetValue(B_CONTROL_ON);
99 
100 		r.left = r.right + 5;
101 		r.right = r.left + be_plain_font->StringWidth("Idle color") + 20;
102 		idle = new BRadioButton(r, "IdleColor", "Idle color",
103 			new BMessage(PRV_DESKBAR_IDLE));
104 		bbox->AddChild(idle);
105 
106 		r.left = r.right + 5;
107 		r.right = r.left + be_plain_font->StringWidth("Frame color") + 20;
108 		frame = new BRadioButton(r, "FrameColor", "Frame color",
109 			new BMessage(PRV_DESKBAR_FRAME));
110 		bbox->AddChild(frame);
111 
112 		r.top = active->Frame().bottom + 1;
113 		r.bottom = r.top + 15;
114 		r.left = 10;
115 		r.right = r.left + be_plain_font->StringWidth("Width of icon:") + 5 + 30;
116 		char temp[10];
117 		sprintf(temp, "%d", prefs->deskbar_icon_width);
118 		iconwidth = new BTextControl(r, "Width", "Width of icon:", temp,
119 			new BMessage(PRV_DESKBAR_ICON_WIDTH));
120 		bbox->AddChild(iconwidth);
121 		iconwidth->SetDivider(be_plain_font->StringWidth("Width of icon:") + 5);
122 		//iconwidth->SetModificationMessage(new BMessage(PRV_DESKBAR_ICON_WIDTH));
123 
124 		for (int x = 0; x < 256; x++) {
125 			if (x < '0' || x > '9') iconwidth->TextView()->DisallowChar(x);
126 		}
127 		iconwidth->TextView()->SetMaxBytes(2);
128 
129 		colorcontrol->SetValue(prefs->deskbar_active_color);
130 	}
131 }
132 
133 void ConfigView::AttachedToWindow() {
134 	BView::AttachedToWindow();
135 
136 	// AttachedToWindow() gets called every time this tab is brought
137 	// to the front, but we only want this initialization to happen once
138 	if (first_time_attached) {
139 		BMessenger messenger(this);
140 		colorcontrol->SetTarget(messenger);
141 		if (fadecolors != NULL) fadecolors->SetTarget(messenger);
142 		if (active != NULL) active->SetTarget(messenger);
143 		if (idle != NULL) idle->SetTarget(messenger);
144 		if (frame != NULL) frame->SetTarget(messenger);
145 		if (iconwidth != NULL) iconwidth->SetTarget(messenger);
146 
147 		first_time_attached = false;
148 	}
149 }
150 
151 void ConfigView::MessageReceived(BMessage *message) {
152 	PrefsWindow *prefswindow = (PrefsWindow *)Window();
153 	if (prefswindow == NULL) return;
154 	Prefs *prefs = prefswindow->prefs;
155 	BMessenger *messenger = prefswindow->messenger;
156 
157 	switch (message->what) {
158 		// These two send the color and the status of the fade checkbox together
159 		case PRV_NORMAL_FADE_COLORS:
160 		case PRV_NORMAL_CHANGE_COLOR: {
161 			bool fade_colors = (bool)fadecolors->Value();
162 			int32 bar_color = colorcontrol->Value();
163 			message->AddInt32("color", bar_color);
164 			message->AddBool("fade", fade_colors);
165 			prefs->normal_fade_colors = fade_colors;
166 			prefs->normal_bar_color = bar_color;
167 			messenger->SendMessage(message);
168 			break;
169 		}
170 		// Share the single color control among three values
171 		case PRV_MINI_ACTIVE:
172 			colorcontrol->SetValue(prefs->mini_active_color);
173 			break;
174 		case PRV_MINI_IDLE:
175 			colorcontrol->SetValue(prefs->mini_idle_color);
176 			break;
177 		case PRV_MINI_FRAME:
178 			colorcontrol->SetValue(prefs->mini_frame_color);
179 			break;
180 		case PRV_MINI_CHANGE_COLOR: {
181 			int32 color = colorcontrol->Value();
182 			if (active->Value()) {
183 				prefs->mini_active_color = color;
184 			} else if (idle->Value()) {
185 				prefs->mini_idle_color = color;
186 			} else {
187 				prefs->mini_frame_color = color;
188 			}
189 			message->AddInt32("active_color", prefs->mini_active_color);
190 			message->AddInt32("idle_color", prefs->mini_idle_color);
191 			message->AddInt32("frame_color", prefs->mini_frame_color);
192 			messenger->SendMessage(message);
193 			break;
194 		}
195 		case PRV_DESKBAR_ACTIVE:
196 			colorcontrol->SetValue(prefs->deskbar_active_color);
197 			break;
198 		case PRV_DESKBAR_IDLE:
199 			colorcontrol->SetValue(prefs->deskbar_idle_color);
200 			break;
201 		case PRV_DESKBAR_FRAME:
202 			colorcontrol->SetValue(prefs->deskbar_frame_color);
203 			break;
204 		case PRV_DESKBAR_ICON_WIDTH:
205 			UpdateDeskbarIconWidth();
206 			break;
207 		case PRV_DESKBAR_CHANGE_COLOR: {
208 			int32 color = colorcontrol->Value();
209 			if (active->Value()) {
210 				prefs->deskbar_active_color = color;
211 			} else if (idle->Value()) {
212 				prefs->deskbar_idle_color = color;
213 			} else {
214 				prefs->deskbar_frame_color = color;
215 			}
216 			message->AddInt32("active_color", prefs->deskbar_active_color);
217 			message->AddInt32("idle_color", prefs->deskbar_idle_color);
218 			message->AddInt32("frame_color", prefs->deskbar_frame_color);
219 			messenger->SendMessage(message);
220 			break;
221 		}
222 		case PRV_BOTTOM_DEFAULTS:
223 			ResetDefaults();
224 			break;
225 		default:
226 			BView::MessageReceived(message);
227 			break;
228 	}
229 }
230 
231 void ConfigView::UpdateDeskbarIconWidth() {
232 	PrefsWindow *prefswindow = (PrefsWindow *)Window();
233 	if (prefswindow == NULL) return;
234 	Prefs *prefs = prefswindow->prefs;
235 	BMessenger *messenger = prefswindow->messenger;
236 
237 	// Make sure the width shows at least one pixel per CPU and
238 	// that it will fit in the tray in any Deskbar orientation
239 	int width = atoi(iconwidth->Text());
240 	int min_width = GetMinimumViewWidth();
241 	if (width < min_width || width > 50) {
242 		char temp[10];
243 		if (width < min_width) {
244 			sprintf(temp, "%d", min_width);
245 			width = min_width;
246 		} else {
247 			strcpy(temp, "50");
248 			width = 50;
249 		}
250 		iconwidth->SetText(temp);
251 	}
252 
253 	BMessage *message = new BMessage(PRV_DESKBAR_ICON_WIDTH);
254 	message->AddInt32("width", width);
255 	prefs->deskbar_icon_width = width;
256 	messenger->SendMessage(message);
257 	delete message;
258 }
259 
260 // Only reset our own controls to default
261 void ConfigView::ResetDefaults() {
262 	PrefsWindow *prefswindow = (PrefsWindow *)Window();
263 	if (prefswindow == NULL) return;
264 	Prefs *prefs = prefswindow->prefs;
265 	BMessenger *messenger = prefswindow->messenger;
266 
267 	if (mode == NORMAL_WINDOW_MODE) {
268 		colorcontrol->SetValue(DEFAULT_NORMAL_BAR_COLOR);
269 		fadecolors->SetValue(DEFAULT_NORMAL_FADE_COLORS);
270 	} else if (mode == MINI_WINDOW_MODE) {
271 		prefs->mini_active_color = DEFAULT_MINI_ACTIVE_COLOR;
272 		prefs->mini_idle_color = DEFAULT_MINI_IDLE_COLOR;
273 		prefs->mini_frame_color = DEFAULT_MINI_FRAME_COLOR;
274 		if (active->Value()) {
275 			colorcontrol->SetValue(DEFAULT_MINI_ACTIVE_COLOR);
276 		} else if (idle->Value()) {
277 			colorcontrol->SetValue(DEFAULT_MINI_IDLE_COLOR);
278 		} else {
279 			colorcontrol->SetValue(DEFAULT_MINI_FRAME_COLOR);
280 		}
281 		BMessage *message = new BMessage(PRV_MINI_CHANGE_COLOR);
282 		message->AddInt32("active_color", DEFAULT_MINI_ACTIVE_COLOR);
283 		message->AddInt32("idle_color", DEFAULT_MINI_IDLE_COLOR);
284 		message->AddInt32("frame_color", DEFAULT_MINI_FRAME_COLOR);
285 		messenger->SendMessage(message);
286 	} else {
287 		prefs->deskbar_active_color = DEFAULT_DESKBAR_ACTIVE_COLOR;
288 		prefs->deskbar_idle_color = DEFAULT_DESKBAR_IDLE_COLOR;
289 		prefs->deskbar_frame_color = DEFAULT_DESKBAR_FRAME_COLOR;
290 		if (active->Value()) {
291 			colorcontrol->SetValue(DEFAULT_DESKBAR_ACTIVE_COLOR);
292 		} else if (idle->Value()) {
293 			colorcontrol->SetValue(DEFAULT_DESKBAR_IDLE_COLOR);
294 		} else {
295 			colorcontrol->SetValue(DEFAULT_DESKBAR_FRAME_COLOR);
296 		}
297 		BMessage *message = new BMessage(PRV_DESKBAR_CHANGE_COLOR);
298 		message->AddInt32("active_color", DEFAULT_DESKBAR_ACTIVE_COLOR);
299 		message->AddInt32("idle_color", DEFAULT_DESKBAR_IDLE_COLOR);
300 		message->AddInt32("frame_color", DEFAULT_DESKBAR_FRAME_COLOR);
301 		messenger->SendMessage(message);
302 
303 		char temp[10];
304 		sprintf(temp, "%d", DEFAULT_DESKBAR_ICON_WIDTH);
305 		iconwidth->SetText(temp);
306 		// Need to force the model message to be sent
307 		iconwidth->Invoke();
308 	}
309 }
310