xref: /haiku/src/apps/pulse/ConfigView.cpp (revision 60c26cd332a044bb9003091b9196cc404ebe5482)
1 /*
2  * Copyright 2002-2009, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Copyright 1999, Be Incorporated. All Rights Reserved.
6  * This file may be used under the terms of the Be Sample Code License.
7  *
8  * Written by:	Daniel Switkin
9  */
10 
11 
12 #include "ConfigView.h"
13 #include "Common.h"
14 #include "PulseApp.h"
15 #include "PrefsWindow.h"
16 
17 #include <Catalog.h>
18 #include <CheckBox.h>
19 #include <Locale.h>
20 #include <RadioButton.h>
21 #include <TextControl.h>
22 
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #undef B_TRANSLATE_CONTEXT
29 #define B_TRANSLATE_CONTEXT "ConfigView"
30 
31 
32 RTColorControl::RTColorControl(BPoint point, BMessage *message)
33 	: BColorControl(point, B_CELLS_32x8, 6, "ColorControl", message, false)
34 {
35 }
36 
37 
38 /*!
39 	Send a message every time the color changes, not just
40 	when the mouse button is released
41 */
42 void
43 RTColorControl::SetValue(int32 color)
44 {
45 	BColorControl::SetValue(color);
46 	Invoke();
47 }
48 
49 
50 //	#pragma mark -
51 
52 
53 /*!
54 	A single class for all three prefs views, needs to be
55 	customized below to give each control the right message
56 */
57 ConfigView::ConfigView(BRect rect, const char *name, uint32 mode, BMessenger& target,
58 		Prefs *prefs)
59 	: BBox(rect, name, B_FOLLOW_NONE, B_WILL_DRAW),
60 	fMode(mode),
61 	fTarget(target),
62 	fPrefs(prefs),
63 	fFirstTimeAttached(true)
64 {
65 	fFadeCheckBox = NULL;
66 	fActiveButton = fIdleButton = fFrameButton = NULL;
67 	fIconWidthControl = NULL;
68 
69 	SetLabel(B_TRANSLATE("Bar colors"));
70 
71 	font_height fontHeight;
72 	be_bold_font->GetHeight(&fontHeight);
73 
74 	fColorControl = new RTColorControl(BPoint(10, 5.0f + fontHeight.ascent
75 		+ fontHeight.descent), new BMessage(fMode));
76 	fColorControl->ResizeToPreferred();
77 	AddChild(fColorControl);
78 
79 	rect = fColorControl->Frame();
80 	rect.top = rect.bottom + 10.0f;
81 	rect.bottom = rect.top + 15.0f;
82 
83 	if (mode == PRV_NORMAL_CHANGE_COLOR) {
84 		// normal mode
85 
86 		fFadeCheckBox = new BCheckBox(rect, "FadeColors",
87 			B_TRANSLATE("Fade colors"), new BMessage(PRV_NORMAL_FADE_COLORS));
88 		fFadeCheckBox->ResizeToPreferred();
89 		AddChild(fFadeCheckBox);
90 
91 		fColorControl->SetValue(fPrefs->normal_bar_color);
92 		fFadeCheckBox->SetValue(fPrefs->normal_fade_colors);
93 	} else if (mode == PRV_MINI_CHANGE_COLOR) {
94 		// mini mode
95 
96 		fActiveButton = new BRadioButton(rect, "ActiveColor",
97 			B_TRANSLATE("Active color"), new BMessage(PRV_MINI_ACTIVE));
98 		fActiveButton->ResizeToPreferred();
99 		fActiveButton->SetValue(B_CONTROL_ON);
100 		AddChild(fActiveButton);
101 
102 		rect.left = fActiveButton->Frame().right + 5.0f;
103 		fIdleButton = new BRadioButton(rect, "IdleColor",
104 			B_TRANSLATE("Idle color"), new BMessage(PRV_MINI_IDLE));
105 		fIdleButton->ResizeToPreferred();
106 		AddChild(fIdleButton);
107 
108 		rect.left = fIdleButton->Frame().right + 5.0f;
109 		fFrameButton = new BRadioButton(rect, "FrameColor",
110 			B_TRANSLATE("Frame color"),	new BMessage(PRV_MINI_FRAME));
111 		fFrameButton->ResizeToPreferred();
112 		AddChild(fFrameButton);
113 
114 		fColorControl->SetValue(fPrefs->mini_active_color);
115 	} else {
116 		// deskbar mode
117 		fActiveButton = new BRadioButton(rect, "ActiveColor",
118 			B_TRANSLATE("Active color"), new BMessage(PRV_DESKBAR_ACTIVE));
119 		fActiveButton->ResizeToPreferred();
120 		fActiveButton->SetValue(B_CONTROL_ON);
121 		AddChild(fActiveButton);
122 
123 		rect.left = fActiveButton->Frame().right + 5.0f;
124 		fIdleButton = new BRadioButton(rect, "IdleColor",
125 			B_TRANSLATE("Idle color"), new BMessage(PRV_DESKBAR_IDLE));
126 		fIdleButton->ResizeToPreferred();
127 		AddChild(fIdleButton);
128 
129 		rect.left = fIdleButton->Frame().right + 5.0f;
130 		fFrameButton = new BRadioButton(rect, "FrameColor",
131 			B_TRANSLATE("Frame color"),	new BMessage(PRV_DESKBAR_FRAME));
132 		fFrameButton->ResizeToPreferred();
133 		AddChild(fFrameButton);
134 
135 		rect.left = fColorControl->Frame().left;
136 		rect.top = fActiveButton->Frame().bottom + 5.0f;
137 
138 		char temp[10];
139 		snprintf(temp, sizeof(temp), "%d", fPrefs->deskbar_icon_width);
140 		fIconWidthControl = new BTextControl(rect, "Width",
141 			B_TRANSLATE("Width of icon:"), temp,
142 			new BMessage(PRV_DESKBAR_ICON_WIDTH));
143 		AddChild(fIconWidthControl);
144 		fIconWidthControl->SetDivider(be_plain_font->StringWidth(
145 			fIconWidthControl->Label()) + 5.0f);
146 
147 		for (int c = 0; c < 256; c++) {
148 			if (!isdigit(c))
149 				fIconWidthControl->TextView()->DisallowChar(c);
150 		}
151 		fIconWidthControl->TextView()->SetMaxBytes(2);
152 
153 		float width, height;
154 		fIconWidthControl->GetPreferredSize(&width, &height);
155 		fIconWidthControl->ResizeTo(fIconWidthControl->Divider() + 32.0f
156 			+ fIconWidthControl->StringWidth("999"), height);
157 
158 		fColorControl->SetValue(fPrefs->deskbar_active_color);
159 	}
160 }
161 
162 
163 void
164 ConfigView::GetPreferredSize(float* _width, float* _height)
165 {
166 	float right, bottom;
167 
168 	if (fMode == PRV_NORMAL_CHANGE_COLOR) {
169 		// normal mode
170 		bottom = fFadeCheckBox->Frame().bottom;
171 		right = fFadeCheckBox->Frame().right;
172 	} else if (fMode == PRV_MINI_CHANGE_COLOR) {
173 		// mini mode
174 		bottom = fIdleButton->Frame().bottom;
175 		right = fFrameButton->Frame().right;
176 	} else {
177 		// deskbar mode
178 		bottom = fIconWidthControl->Frame().bottom;
179 		right = fFrameButton->Frame().right;
180 	}
181 
182 	if (right < fColorControl->Frame().right)
183 		right = fColorControl->Frame().right;
184 	if (right < 300)
185 		right = 300;
186 
187 	if (_width)
188 		*_width = right + 10.0f;
189 	if (_height)
190 		*_height = bottom + 8.0f;
191 }
192 
193 
194 void
195 ConfigView::AttachedToWindow()
196 {
197 	BView::AttachedToWindow();
198 
199 	// AttachedToWindow() gets called every time this tab is brought
200 	// to the front, but we only want this initialization to happen once
201 	if (fFirstTimeAttached) {
202 		if (Parent() != NULL)
203 			SetViewColor(Parent()->ViewColor());
204 		else
205 			SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
206 
207 		BMessenger messenger(this);
208 		fColorControl->SetTarget(messenger);
209 		if (fFadeCheckBox != NULL)
210 			fFadeCheckBox->SetTarget(messenger);
211 		if (fActiveButton != NULL)
212 			fActiveButton->SetTarget(messenger);
213 		if (fIdleButton != NULL)
214 			fIdleButton->SetTarget(messenger);
215 		if (fFrameButton != NULL)
216 			fFrameButton->SetTarget(messenger);
217 		if (fIconWidthControl != NULL)
218 			fIconWidthControl->SetTarget(messenger);
219 
220 		fFirstTimeAttached = false;
221 	}
222 }
223 
224 
225 void
226 ConfigView::MessageReceived(BMessage *message)
227 {
228 	switch (message->what) {
229 		// These two send the color and the status of the fade checkbox together
230 		case PRV_NORMAL_FADE_COLORS:
231 		case PRV_NORMAL_CHANGE_COLOR:
232 		{
233 			bool fade_colors = (bool)fFadeCheckBox->Value();
234 			int32 bar_color = fColorControl->Value();
235 			message->AddInt32("color", bar_color);
236 			message->AddBool("fade", fade_colors);
237 			fPrefs->normal_fade_colors = fade_colors;
238 			fPrefs->normal_bar_color = bar_color;
239 
240 			fTarget.SendMessage(message);
241 			break;
242 		}
243 		// Share the single color control among three values
244 		case PRV_MINI_ACTIVE:
245 			fColorControl->SetValue(fPrefs->mini_active_color);
246 			break;
247 		case PRV_MINI_IDLE:
248 			fColorControl->SetValue(fPrefs->mini_idle_color);
249 			break;
250 		case PRV_MINI_FRAME:
251 			fColorControl->SetValue(fPrefs->mini_frame_color);
252 			break;
253 		case PRV_MINI_CHANGE_COLOR: {
254 			int32 color = fColorControl->Value();
255 			if (fActiveButton->Value())
256 				fPrefs->mini_active_color = color;
257 			else if (fIdleButton->Value())
258 				fPrefs->mini_idle_color = color;
259 			else
260 				fPrefs->mini_frame_color = color;
261 
262 			message->AddInt32("active_color", fPrefs->mini_active_color);
263 			message->AddInt32("idle_color", fPrefs->mini_idle_color);
264 			message->AddInt32("frame_color", fPrefs->mini_frame_color);
265 			fTarget.SendMessage(message);
266 			break;
267 		}
268 		case PRV_DESKBAR_ACTIVE:
269 			fColorControl->SetValue(fPrefs->deskbar_active_color);
270 			break;
271 		case PRV_DESKBAR_IDLE:
272 			fColorControl->SetValue(fPrefs->deskbar_idle_color);
273 			break;
274 		case PRV_DESKBAR_FRAME:
275 			fColorControl->SetValue(fPrefs->deskbar_frame_color);
276 			break;
277 		case PRV_DESKBAR_ICON_WIDTH:
278 			UpdateDeskbarIconWidth();
279 			break;
280 		case PRV_DESKBAR_CHANGE_COLOR: {
281 			int32 color = fColorControl->Value();
282 			if (fActiveButton->Value())
283 				fPrefs->deskbar_active_color = color;
284 			else if (fIdleButton->Value())
285 				fPrefs->deskbar_idle_color = color;
286 			else
287 				fPrefs->deskbar_frame_color = color;
288 
289 			message->AddInt32("active_color", fPrefs->deskbar_active_color);
290 			message->AddInt32("idle_color", fPrefs->deskbar_idle_color);
291 			message->AddInt32("frame_color", fPrefs->deskbar_frame_color);
292 			fTarget.SendMessage(message);
293 			break;
294 		}
295 		case PRV_BOTTOM_DEFAULTS:
296 			_ResetDefaults();
297 			break;
298 		default:
299 			BView::MessageReceived(message);
300 			break;
301 	}
302 }
303 
304 
305 void
306 ConfigView::UpdateDeskbarIconWidth()
307 {
308 	// Make sure the width shows at least one pixel per CPU and
309 	// that it will fit in the tray in any Deskbar orientation
310 	int width = atoi(fIconWidthControl->Text());
311 	int min_width = GetMinimumViewWidth();
312 	if (width < min_width || width > 50) {
313 		char temp[10];
314 		if (width < min_width) {
315 			sprintf(temp, "%d", min_width);
316 			width = min_width;
317 		} else {
318 			strcpy(temp, "50");
319 			width = 50;
320 		}
321 		fIconWidthControl->SetText(temp);
322 	}
323 
324 	fPrefs->deskbar_icon_width = width;
325 
326 	BMessage message(PRV_DESKBAR_ICON_WIDTH);
327 	message.AddInt32("width", width);
328 	fTarget.SendMessage(&message);
329 }
330 
331 
332 void
333 ConfigView::_ResetDefaults()
334 {
335 	if (fMode == PRV_NORMAL_CHANGE_COLOR) {
336 		fColorControl->SetValue(DEFAULT_NORMAL_BAR_COLOR);
337 		fFadeCheckBox->SetValue(DEFAULT_NORMAL_FADE_COLORS);
338 	} else if (fMode == PRV_MINI_CHANGE_COLOR) {
339 		fPrefs->mini_active_color = DEFAULT_MINI_ACTIVE_COLOR;
340 		fPrefs->mini_idle_color = DEFAULT_MINI_IDLE_COLOR;
341 		fPrefs->mini_frame_color = DEFAULT_MINI_FRAME_COLOR;
342 		if (fActiveButton->Value())
343 			fColorControl->SetValue(DEFAULT_MINI_ACTIVE_COLOR);
344 		else if (fIdleButton->Value())
345 			fColorControl->SetValue(DEFAULT_MINI_IDLE_COLOR);
346 		else
347 			fColorControl->SetValue(DEFAULT_MINI_FRAME_COLOR);
348 
349 		BMessage *message = new BMessage(PRV_MINI_CHANGE_COLOR);
350 		message->AddInt32("active_color", DEFAULT_MINI_ACTIVE_COLOR);
351 		message->AddInt32("idle_color", DEFAULT_MINI_IDLE_COLOR);
352 		message->AddInt32("frame_color", DEFAULT_MINI_FRAME_COLOR);
353 		fTarget.SendMessage(message);
354 	} else {
355 		fPrefs->deskbar_active_color = DEFAULT_DESKBAR_ACTIVE_COLOR;
356 		fPrefs->deskbar_idle_color = DEFAULT_DESKBAR_IDLE_COLOR;
357 		fPrefs->deskbar_frame_color = DEFAULT_DESKBAR_FRAME_COLOR;
358 		if (fActiveButton->Value())
359 			fColorControl->SetValue(DEFAULT_DESKBAR_ACTIVE_COLOR);
360 		else if (fIdleButton->Value())
361 			fColorControl->SetValue(DEFAULT_DESKBAR_IDLE_COLOR);
362 		else
363 			fColorControl->SetValue(DEFAULT_DESKBAR_FRAME_COLOR);
364 
365 		BMessage *message = new BMessage(PRV_DESKBAR_CHANGE_COLOR);
366 		message->AddInt32("active_color", DEFAULT_DESKBAR_ACTIVE_COLOR);
367 		message->AddInt32("idle_color", DEFAULT_DESKBAR_IDLE_COLOR);
368 		message->AddInt32("frame_color", DEFAULT_DESKBAR_FRAME_COLOR);
369 		fTarget.SendMessage(message);
370 
371 		char temp[10];
372 		sprintf(temp, "%d", DEFAULT_DESKBAR_ICON_WIDTH);
373 		fIconWidthControl->SetText(temp);
374 		// Need to force the model message to be sent
375 		fIconWidthControl->Invoke();
376 	}
377 }
378