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