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