xref: /haiku/src/preferences/screen/ScreenWindow.cpp (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
1 /*
2  * Copyright 2001-2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus, superstippi@gmx.de
7  *		Andrew Bachmann
8  *		Stefano Ceccherini, burton666@libero.it
9  *		Alexandre Deckner, alex@zappotek.com
10  *		Axel Dörfler, axeld@pinc-software.de
11  *		Rene Gollent, rene@gollent.com
12  *		Thomas Kurschel
13  *		Rafael Romo
14  *		John Scipione, jscipione@gmail.com
15  */
16 
17 
18 #include "ScreenWindow.h"
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <strings.h>
23 
24 #include <Alert.h>
25 #include <Application.h>
26 #include <Box.h>
27 #include <Button.h>
28 #include <Catalog.h>
29 #include <ControlLook.h>
30 #include <Directory.h>
31 #include <File.h>
32 #include <FindDirectory.h>
33 #include <InterfaceDefs.h>
34 #include <LayoutBuilder.h>
35 #include <MenuBar.h>
36 #include <MenuItem.h>
37 #include <MenuField.h>
38 #include <Messenger.h>
39 #include <Path.h>
40 #include <PopUpMenu.h>
41 #include <Screen.h>
42 #include <SpaceLayoutItem.h>
43 #include <Spinner.h>
44 #include <String.h>
45 #include <StringView.h>
46 #include <Roster.h>
47 #include <Window.h>
48 
49 #include <InterfacePrivate.h>
50 
51 #include "AlertWindow.h"
52 #include "Constants.h"
53 #include "RefreshWindow.h"
54 #include "MonitorView.h"
55 #include "ScreenSettings.h"
56 #include "Utility.h"
57 
58 /* Note, this headers defines a *private* interface to the Radeon accelerant.
59  * It's a solution that works with the current BeOS interface that Haiku
60  * adopted.
61  * However, it's not a nice and clean solution. Don't use this header in any
62  * application if you can avoid it. No other driver is using this, or should
63  * be using this.
64  * It will be replaced as soon as we introduce an updated accelerant interface
65  * which may even happen before R1 hits the streets.
66  */
67 #include "multimon.h"	// the usual: DANGER WILL, ROBINSON!
68 
69 
70 #undef B_TRANSLATION_CONTEXT
71 #define B_TRANSLATION_CONTEXT "Screen"
72 
73 
74 const char* kBackgroundsSignature = "application/x-vnd.Haiku-Backgrounds";
75 
76 // list of officially supported colour spaces
77 static const struct {
78 	color_space	space;
79 	int32		bits_per_pixel;
80 	const char*	label;
81 } kColorSpaces[] = {
82 	{ B_CMAP8, 8, B_TRANSLATE("8 bits/pixel, 256 colors") },
83 	{ B_RGB15, 15, B_TRANSLATE("15 bits/pixel, 32768 colors") },
84 	{ B_RGB16, 16, B_TRANSLATE("16 bits/pixel, 65536 colors") },
85 	{ B_RGB24, 24, B_TRANSLATE("24 bits/pixel, 16 Million colors") },
86 	{ B_RGB32, 32, B_TRANSLATE("32 bits/pixel, 16 Million colors") }
87 };
88 static const int32 kColorSpaceCount
89 	= sizeof(kColorSpaces) / sizeof(kColorSpaces[0]);
90 
91 // list of standard refresh rates
92 static const int32 kRefreshRates[] = { 60, 70, 72, 75, 80, 85, 95, 100 };
93 static const int32 kRefreshRateCount
94 	= sizeof(kRefreshRates) / sizeof(kRefreshRates[0]);
95 
96 // list of combine modes
97 static const struct {
98 	combine_mode	mode;
99 	const char		*name;
100 } kCombineModes[] = {
101 	{ kCombineDisable, B_TRANSLATE("disable") },
102 	{ kCombineHorizontally, B_TRANSLATE("horizontally") },
103 	{ kCombineVertically, B_TRANSLATE("vertically") }
104 };
105 static const int32 kCombineModeCount
106 	= sizeof(kCombineModes) / sizeof(kCombineModes[0]);
107 
108 
109 static BString
110 tv_standard_to_string(uint32 mode)
111 {
112 	switch (mode) {
113 		case 0:		return "disabled";
114 		case 1:		return "NTSC";
115 		case 2:		return "NTSC Japan";
116 		case 3:		return "PAL BDGHI";
117 		case 4:		return "PAL M";
118 		case 5:		return "PAL N";
119 		case 6:		return "SECAM";
120 		case 101:	return "NTSC 443";
121 		case 102:	return "PAL 60";
122 		case 103:	return "PAL NC";
123 		default:
124 		{
125 			BString name;
126 			name << "??? (" << mode << ")";
127 
128 			return name;
129 		}
130 	}
131 }
132 
133 
134 static void
135 resolution_to_string(screen_mode& mode, BString &string)
136 {
137 	string << mode.width << " x " << mode.height;
138 }
139 
140 
141 static void
142 refresh_rate_to_string(float refresh, BString &string,
143 	bool appendUnit = true, bool alwaysWithFraction = false)
144 {
145 	snprintf(string.LockBuffer(32), 32, "%.*g", refresh >= 100.0 ? 4 : 3,
146 		refresh);
147 	string.UnlockBuffer();
148 
149 	if (appendUnit)
150 		string << " " << B_TRANSLATE("Hz");
151 }
152 
153 
154 static const char*
155 screen_errors(status_t status)
156 {
157 	switch (status) {
158 		case B_ENTRY_NOT_FOUND:
159 			return B_TRANSLATE("Unknown mode");
160 		// TODO: add more?
161 
162 		default:
163 			return strerror(status);
164 	}
165 }
166 
167 
168 //	#pragma mark - ScreenWindow
169 
170 
171 ScreenWindow::ScreenWindow(ScreenSettings* settings)
172 	:
173 	BWindow(settings->WindowFrame(), B_TRANSLATE_SYSTEM_NAME("Screen"),
174 		B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE
175 			| B_AUTO_UPDATE_SIZE_LIMITS, B_ALL_WORKSPACES),
176 	fIsVesa(false),
177 	fBootWorkspaceApplied(false),
178 	fOtherRefresh(NULL),
179 	fScreenMode(this),
180 	fUndoScreenMode(this),
181 	fModified(false)
182 {
183 	BScreen screen(this);
184 
185 	accelerant_device_info info;
186 	if (screen.GetDeviceInfo(&info) == B_OK
187 		&& !strcasecmp(info.chipset, "VESA"))
188 		fIsVesa = true;
189 
190 	_UpdateOriginal();
191 	_BuildSupportedColorSpaces();
192 	fActive = fSelected = fOriginal;
193 
194 	fSettings = settings;
195 
196 	// we need the "Current Workspace" first to get its height
197 
198 	BPopUpMenu* popUpMenu = new BPopUpMenu(B_TRANSLATE("Current workspace"),
199 		true, true);
200 	fAllWorkspacesItem = new BMenuItem(B_TRANSLATE("All workspaces"),
201 		new BMessage(WORKSPACE_CHECK_MSG));
202 	popUpMenu->AddItem(fAllWorkspacesItem);
203 	BMenuItem *item = new BMenuItem(B_TRANSLATE("Current workspace"),
204 		new BMessage(WORKSPACE_CHECK_MSG));
205 
206 	popUpMenu->AddItem(item);
207 	fAllWorkspacesItem->SetMarked(true);
208 
209 	BMenuField* workspaceMenuField = new BMenuField("WorkspaceMenu", NULL,
210 		popUpMenu);
211 	workspaceMenuField->ResizeToPreferred();
212 
213 	// box on the left with workspace count and monitor view
214 
215 	BBox* screenBox = new BBox("screen box");
216 	BGroupLayout* layout = new BGroupLayout(B_VERTICAL, B_USE_SMALL_SPACING);
217 	layout->SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
218 		B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING);
219 	screenBox->SetLayout(layout);
220 
221 	fMonitorInfo = new BStringView("monitor info", "");
222 	fMonitorInfo->SetAlignment(B_ALIGN_CENTER);
223 	screenBox->AddChild(fMonitorInfo);
224 
225 	fDeviceInfo = new BStringView("monitor info", "");
226 	fDeviceInfo->SetAlignment(B_ALIGN_CENTER);
227 	screenBox->AddChild(fDeviceInfo);
228 
229 	float scaling = std::max(1.0f, be_plain_font->Size() / 12.0f);
230 	fMonitorView = new MonitorView(BRect(0.0, 0.0, 80.0 * scaling,
231 			80.0 * scaling), "monitor", screen.Frame().IntegerWidth() + 1,
232 		screen.Frame().IntegerHeight() + 1);
233 	screenBox->AddChild(fMonitorView);
234 
235 	BStringView* workspaces = new BStringView("workspaces",
236 		B_TRANSLATE("Workspaces"));
237 	workspaces->SetAlignment(B_ALIGN_CENTER);
238 
239 	fColumnsControl = new BSpinner("columns", B_TRANSLATE("Columns:"),
240 		new BMessage(kMsgWorkspaceColumnsChanged));
241 	fColumnsControl->SetAlignment(B_ALIGN_RIGHT);
242 	fColumnsControl->SetRange(1, 32);
243 
244 	fRowsControl = new BSpinner("rows", B_TRANSLATE("Rows:"),
245 		new BMessage(kMsgWorkspaceRowsChanged));
246 	fRowsControl->SetAlignment(B_ALIGN_RIGHT);
247 	fRowsControl->SetRange(1, 32);
248 
249 	uint32 columns;
250 	uint32 rows;
251 	BPrivate::get_workspaces_layout(&columns, &rows);
252 	fColumnsControl->SetValue(columns);
253 	fRowsControl->SetValue(rows);
254 
255 	screenBox->AddChild(BLayoutBuilder::Group<>()
256 		.AddGroup(B_VERTICAL, B_USE_SMALL_SPACING)
257 			.Add(workspaces)
258 			.AddGrid(B_USE_DEFAULT_SPACING, B_USE_SMALL_SPACING)
259 				// columns
260 				.Add(fColumnsControl->CreateLabelLayoutItem(), 0, 0)
261 				.Add(fColumnsControl->CreateTextViewLayoutItem(), 1, 0)
262 				// rows
263 				.Add(fRowsControl->CreateLabelLayoutItem(), 0, 1)
264 				.Add(fRowsControl->CreateTextViewLayoutItem(), 1, 1)
265 				.End()
266 			.End()
267 		.View());
268 
269 	fBackgroundsButton = new BButton("BackgroundsButton",
270 		B_TRANSLATE("Set background" B_UTF8_ELLIPSIS),
271 		new BMessage(BUTTON_LAUNCH_BACKGROUNDS_MSG));
272 	fBackgroundsButton->SetFontSize(be_plain_font->Size() * 0.9);
273 	screenBox->AddChild(fBackgroundsButton);
274 
275 	// box on the right with screen resolution, etc.
276 
277 	BBox* controlsBox = new BBox("controls box");
278 	controlsBox->SetLabel(workspaceMenuField);
279 	BGroupView* outerControlsView = new BGroupView(B_VERTICAL);
280 	outerControlsView->GroupLayout()->SetInsets(B_USE_DEFAULT_SPACING,
281 		B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING);
282 	controlsBox->AddChild(outerControlsView);
283 
284 	fResolutionMenu = new BPopUpMenu("resolution", true, true);
285 
286 	uint16 maxWidth = 0;
287 	uint16 maxHeight = 0;
288 	uint16 previousWidth = 0;
289 	uint16 previousHeight = 0;
290 	for (int32 i = 0; i < fScreenMode.CountModes(); i++) {
291 		screen_mode mode = fScreenMode.ModeAt(i);
292 
293 		if (mode.width == previousWidth && mode.height == previousHeight)
294 			continue;
295 
296 		previousWidth = mode.width;
297 		previousHeight = mode.height;
298 		if (maxWidth < mode.width)
299 			maxWidth = mode.width;
300 		if (maxHeight < mode.height)
301 			maxHeight = mode.height;
302 
303 		BMessage* message = new BMessage(POP_RESOLUTION_MSG);
304 		message->AddInt32("width", mode.width);
305 		message->AddInt32("height", mode.height);
306 
307 		BString name;
308 		name << mode.width << " x " << mode.height;
309 
310 		fResolutionMenu->AddItem(new BMenuItem(name.String(), message));
311 	}
312 
313 	fMonitorView->SetMaxResolution(maxWidth, maxHeight);
314 
315 	fResolutionField = new BMenuField("ResolutionMenu",
316 		B_TRANSLATE("Resolution:"), fResolutionMenu);
317 	fResolutionField->SetAlignment(B_ALIGN_RIGHT);
318 
319 	fColorsMenu = new BPopUpMenu("colors", true, false);
320 
321 	for (int32 i = 0; i < kColorSpaceCount; i++) {
322 		if ((fSupportedColorSpaces & (1 << i)) == 0)
323 			continue;
324 
325 		BMessage* message = new BMessage(POP_COLORS_MSG);
326 		message->AddInt32("bits_per_pixel", kColorSpaces[i].bits_per_pixel);
327 		message->AddInt32("space", kColorSpaces[i].space);
328 
329 		BMenuItem* item = new BMenuItem(kColorSpaces[i].label, message);
330 		if (kColorSpaces[i].space == screen.ColorSpace())
331 			fUserSelectedColorSpace = item;
332 
333 		fColorsMenu->AddItem(item);
334 	}
335 
336 	fColorsField = new BMenuField("ColorsMenu", B_TRANSLATE("Colors:"),
337 		fColorsMenu);
338 	fColorsField->SetAlignment(B_ALIGN_RIGHT);
339 
340 	fRefreshMenu = new BPopUpMenu("refresh rate", true, true);
341 
342 	float min, max;
343 	if (fScreenMode.GetRefreshLimits(fActive, min, max) != B_OK) {
344 		// if we couldn't obtain the refresh limits, reset to the default
345 		// range. Constraints from detected monitors will fine-tune this
346 		// later.
347 		min = kRefreshRates[0];
348 		max = kRefreshRates[kRefreshRateCount - 1];
349 	}
350 
351 	if (min == max) {
352 		// This is a special case for drivers that only support a single
353 		// frequency, like the VESA driver
354 		BString name;
355 		refresh_rate_to_string(min, name);
356 		BMessage *message = new BMessage(POP_REFRESH_MSG);
357 		message->AddFloat("refresh", min);
358 		BMenuItem *item = new BMenuItem(name.String(), message);
359 		fRefreshMenu->AddItem(item);
360 		item->SetEnabled(false);
361 	} else {
362 		monitor_info info;
363 		if (fScreenMode.GetMonitorInfo(info) == B_OK) {
364 			min = max_c(info.min_vertical_frequency, min);
365 			max = min_c(info.max_vertical_frequency, max);
366 		}
367 
368 		for (int32 i = 0; i < kRefreshRateCount; ++i) {
369 			if (kRefreshRates[i] < min || kRefreshRates[i] > max)
370 				continue;
371 
372 			BString name;
373 			name << kRefreshRates[i] << " " << B_TRANSLATE("Hz");
374 
375 			BMessage *message = new BMessage(POP_REFRESH_MSG);
376 			message->AddFloat("refresh", kRefreshRates[i]);
377 
378 			fRefreshMenu->AddItem(new BMenuItem(name.String(), message));
379 		}
380 
381 		fOtherRefresh = new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS),
382 			new BMessage(POP_OTHER_REFRESH_MSG));
383 		fRefreshMenu->AddItem(fOtherRefresh);
384 	}
385 
386 	fRefreshField = new BMenuField("RefreshMenu", B_TRANSLATE("Refresh rate:"),
387 		fRefreshMenu);
388 	fRefreshField->SetAlignment(B_ALIGN_RIGHT);
389 
390 	if (_IsVesa())
391 		fRefreshField->Hide();
392 
393 	// enlarged area for multi-monitor settings
394 	{
395 		bool dummy;
396 		uint32 dummy32;
397 		bool multiMonSupport;
398 		bool useLaptopPanelSupport;
399 		bool tvStandardSupport;
400 
401 		multiMonSupport = TestMultiMonSupport(&screen) == B_OK;
402 		useLaptopPanelSupport = GetUseLaptopPanel(&screen, &dummy) == B_OK;
403 		tvStandardSupport = GetTVStandard(&screen, &dummy32) == B_OK;
404 
405 		// even if there is no support, we still create all controls
406 		// to make sure we don't access NULL pointers later on
407 
408 		fCombineMenu = new BPopUpMenu("CombineDisplays",
409 			true, true);
410 
411 		for (int32 i = 0; i < kCombineModeCount; i++) {
412 			BMessage *message = new BMessage(POP_COMBINE_DISPLAYS_MSG);
413 			message->AddInt32("mode", kCombineModes[i].mode);
414 
415 			fCombineMenu->AddItem(new BMenuItem(kCombineModes[i].name,
416 				message));
417 		}
418 
419 		fCombineField = new BMenuField("CombineMenu",
420 			B_TRANSLATE("Combine displays:"), fCombineMenu);
421 		fCombineField->SetAlignment(B_ALIGN_RIGHT);
422 
423 		if (!multiMonSupport)
424 			fCombineField->Hide();
425 
426 		fSwapDisplaysMenu = new BPopUpMenu("SwapDisplays",
427 			true, true);
428 
429 		// !order is important - we rely that boolean value == idx
430 		BMessage *message = new BMessage(POP_SWAP_DISPLAYS_MSG);
431 		message->AddBool("swap", false);
432 		fSwapDisplaysMenu->AddItem(new BMenuItem(B_TRANSLATE("no"), message));
433 
434 		message = new BMessage(POP_SWAP_DISPLAYS_MSG);
435 		message->AddBool("swap", true);
436 		fSwapDisplaysMenu->AddItem(new BMenuItem(B_TRANSLATE("yes"), message));
437 
438 		fSwapDisplaysField = new BMenuField("SwapMenu",
439 			B_TRANSLATE("Swap displays:"), fSwapDisplaysMenu);
440 		fSwapDisplaysField->SetAlignment(B_ALIGN_RIGHT);
441 
442 		if (!multiMonSupport)
443 			fSwapDisplaysField->Hide();
444 
445 		fUseLaptopPanelMenu = new BPopUpMenu("UseLaptopPanel",
446 			true, true);
447 
448 		// !order is important - we rely that boolean value == idx
449 		message = new BMessage(POP_USE_LAPTOP_PANEL_MSG);
450 		message->AddBool("use", false);
451 		fUseLaptopPanelMenu->AddItem(new BMenuItem(B_TRANSLATE("if needed"),
452 			message));
453 
454 		message = new BMessage(POP_USE_LAPTOP_PANEL_MSG);
455 		message->AddBool("use", true);
456 		fUseLaptopPanelMenu->AddItem(new BMenuItem(B_TRANSLATE("always"),
457 			message));
458 
459 		fUseLaptopPanelField = new BMenuField("UseLaptopPanel",
460 			B_TRANSLATE("Use laptop panel:"), fUseLaptopPanelMenu);
461 		fUseLaptopPanelField->SetAlignment(B_ALIGN_RIGHT);
462 
463 		if (!useLaptopPanelSupport)
464 			fUseLaptopPanelField->Hide();
465 
466 		fTVStandardMenu = new BPopUpMenu("TVStandard", true, true);
467 
468 		// arbitrary limit
469 		uint32 i;
470 		for (i = 0; i < 100; ++i) {
471 			uint32 mode;
472 			if (GetNthSupportedTVStandard(&screen, i, &mode) != B_OK)
473 				break;
474 
475 			BString name = tv_standard_to_string(mode);
476 
477 			message = new BMessage(POP_TV_STANDARD_MSG);
478 			message->AddInt32("tv_standard", mode);
479 
480 			fTVStandardMenu->AddItem(new BMenuItem(name.String(), message));
481 		}
482 
483 		fTVStandardField = new BMenuField("tv standard",
484 			B_TRANSLATE("Video format:"), fTVStandardMenu);
485 		fTVStandardField->SetAlignment(B_ALIGN_RIGHT);
486 
487 		if (!tvStandardSupport || i == 0)
488 			fTVStandardField->Hide();
489 	}
490 
491 	BLayoutBuilder::Group<>(outerControlsView)
492 		.AddGrid(B_USE_DEFAULT_SPACING, B_USE_SMALL_SPACING)
493 			.Add(fResolutionField->CreateLabelLayoutItem(), 0, 0)
494 			.Add(fResolutionField->CreateMenuBarLayoutItem(), 1, 0)
495 			.Add(fColorsField->CreateLabelLayoutItem(), 0, 1)
496 			.Add(fColorsField->CreateMenuBarLayoutItem(), 1, 1)
497 			.Add(fRefreshField->CreateLabelLayoutItem(), 0, 2)
498 			.Add(fRefreshField->CreateMenuBarLayoutItem(), 1, 2)
499 			.Add(fCombineField->CreateLabelLayoutItem(), 0, 3)
500 			.Add(fCombineField->CreateMenuBarLayoutItem(), 1, 3)
501 			.Add(fSwapDisplaysField->CreateLabelLayoutItem(), 0, 4)
502 			.Add(fSwapDisplaysField->CreateMenuBarLayoutItem(), 1, 4)
503 			.Add(fUseLaptopPanelField->CreateLabelLayoutItem(), 0, 5)
504 			.Add(fUseLaptopPanelField->CreateMenuBarLayoutItem(), 1, 5)
505 			.Add(fTVStandardField->CreateLabelLayoutItem(), 0, 6)
506 			.Add(fTVStandardField->CreateMenuBarLayoutItem(), 1, 6)
507 		.End();
508 
509 	// TODO: we don't support getting the screen's preferred settings
510 	/* fDefaultsButton = new BButton(buttonRect, "DefaultsButton", "Defaults",
511 		new BMessage(BUTTON_DEFAULTS_MSG));*/
512 
513 	fApplyButton = new BButton("ApplyButton", B_TRANSLATE("Apply"),
514 		new BMessage(BUTTON_APPLY_MSG));
515 	fApplyButton->SetEnabled(false);
516 	BLayoutBuilder::Group<>(outerControlsView)
517 		.AddGlue()
518 		.AddGroup(B_HORIZONTAL)
519 			.AddGlue()
520 			.Add(fApplyButton);
521 
522 	fRevertButton = new BButton("RevertButton", B_TRANSLATE("Revert"),
523 		new BMessage(BUTTON_REVERT_MSG));
524 	fRevertButton->SetEnabled(false);
525 
526 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
527 		.AddGroup(B_HORIZONTAL)
528 			.AddGroup(B_VERTICAL, 0, 1)
529 				.AddStrut(floorf(controlsBox->TopBorderOffset()) - 1)
530 				.Add(screenBox)
531 				.End()
532 			.Add(controlsBox, 2)
533 			.End()
534 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
535 			.Add(fRevertButton)
536 			.AddGlue()
537 			.End()
538 		.SetInsets(B_USE_WINDOW_SPACING);
539 
540 	_UpdateControls();
541 	_UpdateMonitor();
542 
543 	MoveOnScreen();
544 }
545 
546 
547 ScreenWindow::~ScreenWindow()
548 {
549 	delete fSettings;
550 }
551 
552 
553 bool
554 ScreenWindow::QuitRequested()
555 {
556 	fSettings->SetWindowFrame(Frame());
557 
558 	// Write mode of workspace 0 (the boot workspace) to the vesa settings file
559 	screen_mode vesaMode;
560 	if (fBootWorkspaceApplied && fScreenMode.Get(vesaMode, 0) == B_OK) {
561 		status_t status = _WriteVesaModeFile(vesaMode);
562 		if (status < B_OK) {
563 			BString warning = B_TRANSLATE("Could not write VESA mode settings"
564 				" file:\n\t");
565 			warning << strerror(status);
566 			BAlert* alert = new BAlert(B_TRANSLATE("Warning"),
567 				warning.String(), B_TRANSLATE("OK"), NULL,
568 				NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
569 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
570 			alert->Go();
571 		}
572 	}
573 
574 	be_app->PostMessage(B_QUIT_REQUESTED);
575 
576 	return BWindow::QuitRequested();
577 }
578 
579 
580 /*!	Update resolution list according to combine mode
581 	(some resolutions may not be combinable due to memory restrictions).
582 */
583 void
584 ScreenWindow::_CheckResolutionMenu()
585 {
586 	for (int32 i = 0; i < fResolutionMenu->CountItems(); i++)
587 		fResolutionMenu->ItemAt(i)->SetEnabled(false);
588 
589 	for (int32 i = 0; i < fScreenMode.CountModes(); i++) {
590 		screen_mode mode = fScreenMode.ModeAt(i);
591 		if (mode.combine != fSelected.combine)
592 			continue;
593 
594 		BString name;
595 		name << mode.width << " x " << mode.height;
596 
597 		BMenuItem *item = fResolutionMenu->FindItem(name.String());
598 		if (item != NULL)
599 			item->SetEnabled(true);
600 	}
601 }
602 
603 
604 /*!	Update color and refresh options according to current mode
605 	(a color space is made active if there is any mode with
606 	given resolution and this colour space; same applies for
607 	refresh rate, though "Other…" is always possible)
608 */
609 void
610 ScreenWindow::_CheckColorMenu()
611 {
612 	int32 supportsAnything = false;
613 	int32 index = 0;
614 
615 	for (int32 i = 0; i < kColorSpaceCount; i++) {
616 		if ((fSupportedColorSpaces & (1 << i)) == 0)
617 			continue;
618 
619 		bool supported = false;
620 
621 		for (int32 j = 0; j < fScreenMode.CountModes(); j++) {
622 			screen_mode mode = fScreenMode.ModeAt(j);
623 
624 			if (fSelected.width == mode.width
625 				&& fSelected.height == mode.height
626 				&& kColorSpaces[i].space == mode.space
627 				&& fSelected.combine == mode.combine) {
628 				supportsAnything = true;
629 				supported = true;
630 				break;
631 			}
632 		}
633 
634 		BMenuItem* item = fColorsMenu->ItemAt(index++);
635 		if (item)
636 			item->SetEnabled(supported);
637 	}
638 
639 	fColorsField->SetEnabled(supportsAnything);
640 
641 	if (!supportsAnything)
642 		return;
643 
644 	// Make sure a valid item is selected
645 
646 	BMenuItem* item = fColorsMenu->FindMarked();
647 	bool changed = false;
648 
649 	if (item != fUserSelectedColorSpace) {
650 		if (fUserSelectedColorSpace != NULL
651 			&& fUserSelectedColorSpace->IsEnabled()) {
652 			fUserSelectedColorSpace->SetMarked(true);
653 			item = fUserSelectedColorSpace;
654 			changed = true;
655 		}
656 	}
657 	if (item != NULL && !item->IsEnabled()) {
658 		// find the next best item
659 		int32 index = fColorsMenu->IndexOf(item);
660 		bool found = false;
661 
662 		for (int32 i = index + 1; i < fColorsMenu->CountItems(); i++) {
663 			item = fColorsMenu->ItemAt(i);
664 			if (item->IsEnabled()) {
665 				found = true;
666 				break;
667 			}
668 		}
669 		if (!found) {
670 			// search backwards as well
671 			for (int32 i = index - 1; i >= 0; i--) {
672 				item = fColorsMenu->ItemAt(i);
673 				if (item->IsEnabled())
674 					break;
675 			}
676 		}
677 
678 		item->SetMarked(true);
679 		changed = true;
680 	}
681 
682 	if (changed) {
683 		// Update selected space
684 
685 		BMessage* message = item->Message();
686 		int32 space;
687 		if (message->FindInt32("space", &space) == B_OK) {
688 			fSelected.space = (color_space)space;
689 			_UpdateColorLabel();
690 		}
691 	}
692 }
693 
694 
695 /*!	Enable/disable refresh options according to current mode. */
696 void
697 ScreenWindow::_CheckRefreshMenu()
698 {
699 	float min, max;
700 	if (fScreenMode.GetRefreshLimits(fSelected, min, max) != B_OK || min == max)
701 		return;
702 
703 	for (int32 i = fRefreshMenu->CountItems(); i-- > 0;) {
704 		BMenuItem* item = fRefreshMenu->ItemAt(i);
705 		BMessage* message = item->Message();
706 		float refresh;
707 		if (message != NULL && message->FindFloat("refresh", &refresh) == B_OK)
708 			item->SetEnabled(refresh >= min && refresh <= max);
709 	}
710 }
711 
712 
713 /*!	Activate appropriate menu item according to selected refresh rate */
714 void
715 ScreenWindow::_UpdateRefreshControl()
716 {
717 	for (int32 i = 0; i < fRefreshMenu->CountItems(); i++) {
718 		BMenuItem* item = fRefreshMenu->ItemAt(i);
719 		if (item->Message()->FindFloat("refresh") == fSelected.refresh) {
720 			item->SetMarked(true);
721 			// "Other" items only contains a refresh rate when active
722 			fOtherRefresh->SetLabel(B_TRANSLATE("Other" B_UTF8_ELLIPSIS));
723 			return;
724 		}
725 	}
726 
727 	// this is a non-standard refresh rate
728 	if (fOtherRefresh != NULL) {
729 		fOtherRefresh->Message()->ReplaceFloat("refresh", fSelected.refresh);
730 		fOtherRefresh->SetMarked(true);
731 
732 		BString string;
733 		refresh_rate_to_string(fSelected.refresh, string);
734 		fRefreshMenu->Superitem()->SetLabel(string.String());
735 
736 		string.Append(B_TRANSLATE("/other" B_UTF8_ELLIPSIS));
737 		fOtherRefresh->SetLabel(string.String());
738 	}
739 }
740 
741 
742 void
743 ScreenWindow::_UpdateMonitorView()
744 {
745 	BMessage updateMessage(UPDATE_DESKTOP_MSG);
746 	updateMessage.AddInt32("width", fSelected.width);
747 	updateMessage.AddInt32("height", fSelected.height);
748 
749 	PostMessage(&updateMessage, fMonitorView);
750 }
751 
752 
753 void
754 ScreenWindow::_UpdateControls()
755 {
756 	_UpdateWorkspaceButtons();
757 
758 	BMenuItem* item = fSwapDisplaysMenu->ItemAt((int32)fSelected.swap_displays);
759 	if (item != NULL && !item->IsMarked())
760 		item->SetMarked(true);
761 
762 	item = fUseLaptopPanelMenu->ItemAt((int32)fSelected.use_laptop_panel);
763 	if (item != NULL && !item->IsMarked())
764 		item->SetMarked(true);
765 
766 	for (int32 i = 0; i < fTVStandardMenu->CountItems(); i++) {
767 		item = fTVStandardMenu->ItemAt(i);
768 
769 		uint32 tvStandard;
770 		item->Message()->FindInt32("tv_standard", (int32 *)&tvStandard);
771 		if (tvStandard == fSelected.tv_standard) {
772 			if (!item->IsMarked())
773 				item->SetMarked(true);
774 			break;
775 		}
776 	}
777 
778 	_CheckResolutionMenu();
779 	_CheckColorMenu();
780 	_CheckRefreshMenu();
781 
782 	BString string;
783 	resolution_to_string(fSelected, string);
784 	item = fResolutionMenu->FindItem(string.String());
785 
786 	if (item != NULL) {
787 		if (!item->IsMarked())
788 			item->SetMarked(true);
789 	} else {
790 		// this is bad luck - if mode has been set via screen references,
791 		// this case cannot occur; there are three possible solutions:
792 		// 1. add a new resolution to list
793 		//    - we had to remove it as soon as a "valid" one is selected
794 		//    - we don't know which frequencies/bit depths are supported
795 		//    - as long as we haven't the GMT formula to create
796 		//      parameters for any resolution given, we cannot
797 		//      really set current mode - it's just not in the list
798 		// 2. choose nearest resolution
799 		//    - probably a good idea, but implies coding and testing
800 		// 3. choose lowest resolution
801 		//    - do you really think we are so lazy? yes, we are
802 		item = fResolutionMenu->ItemAt(0);
803 		if (item)
804 			item->SetMarked(true);
805 
806 		// okay - at least we set menu label to active resolution
807 		fResolutionMenu->Superitem()->SetLabel(string.String());
808 	}
809 
810 	// mark active combine mode
811 	for (int32 i = 0; i < kCombineModeCount; i++) {
812 		if (kCombineModes[i].mode == fSelected.combine) {
813 			item = fCombineMenu->ItemAt(i);
814 			if (item != NULL && !item->IsMarked())
815 				item->SetMarked(true);
816 			break;
817 		}
818 	}
819 
820 	item = fColorsMenu->ItemAt(0);
821 
822 	for (int32 i = 0, index = 0; i <  kColorSpaceCount; i++) {
823 		if ((fSupportedColorSpaces & (1 << i)) == 0)
824 			continue;
825 
826 		if (kColorSpaces[i].space == fSelected.space) {
827 			item = fColorsMenu->ItemAt(index);
828 			break;
829 		}
830 
831 		index++;
832 	}
833 
834 	if (item != NULL && !item->IsMarked())
835 		item->SetMarked(true);
836 
837 	_UpdateColorLabel();
838 	_UpdateMonitorView();
839 	_UpdateRefreshControl();
840 
841 	_CheckApplyEnabled();
842 }
843 
844 
845 /*! Reflect active mode in chosen settings */
846 void
847 ScreenWindow::_UpdateActiveMode()
848 {
849 	_UpdateActiveMode(current_workspace());
850 }
851 
852 
853 void
854 ScreenWindow::_UpdateActiveMode(int32 workspace)
855 {
856 	// Usually, this function gets called after a mode
857 	// has been set manually; still, as the graphics driver
858 	// is free to fiddle with mode passed, we better ask
859 	// what kind of mode we actually got
860 	if (fScreenMode.Get(fActive, workspace) == B_OK) {
861 		fSelected = fActive;
862 
863 		_UpdateMonitor();
864 		_BuildSupportedColorSpaces();
865 		_UpdateControls();
866 	}
867 }
868 
869 
870 void
871 ScreenWindow::_UpdateWorkspaceButtons()
872 {
873 	uint32 columns;
874 	uint32 rows;
875 	BPrivate::get_workspaces_layout(&columns, &rows);
876 
877 	// Set the max values enabling/disabling the up/down arrows
878 
879 	if (rows == 1)
880 		fColumnsControl->SetMaxValue(32);
881 	else if (rows == 2)
882 		fColumnsControl->SetMaxValue(16);
883 	else if (rows <= 4)
884 		fColumnsControl->SetMaxValue(8);
885 	else if (rows <= 8)
886 		fColumnsControl->SetMaxValue(4);
887 	else if (rows <= 16)
888 		fColumnsControl->SetMaxValue(2);
889 	else if (rows <= 32)
890 		fColumnsControl->SetMaxValue(1);
891 
892 	if (columns == 1)
893 		fRowsControl->SetMaxValue(32);
894 	else if (columns == 2)
895 		fRowsControl->SetMaxValue(16);
896 	else if (columns <= 4)
897 		fRowsControl->SetMaxValue(8);
898 	else if (columns <= 8)
899 		fRowsControl->SetMaxValue(4);
900 	else if (columns <= 16)
901 		fRowsControl->SetMaxValue(2);
902 	else if (columns <= 32)
903 		fRowsControl->SetMaxValue(1);
904 }
905 
906 
907 void
908 ScreenWindow::ScreenChanged(BRect frame, color_space mode)
909 {
910 	// move window on screen, if necessary
911 	if (frame.right <= Frame().right
912 		&& frame.bottom <= Frame().bottom) {
913 		MoveTo((frame.Width() - Frame().Width()) / 2,
914 			(frame.Height() - Frame().Height()) / 2);
915 	}
916 }
917 
918 
919 void
920 ScreenWindow::WorkspaceActivated(int32 workspace, bool state)
921 {
922 	if (fScreenMode.GetOriginalMode(fOriginal, workspace) == B_OK) {
923 		_UpdateActiveMode(workspace);
924 
925 		BMessage message(UPDATE_DESKTOP_COLOR_MSG);
926 		PostMessage(&message, fMonitorView);
927 	}
928 }
929 
930 
931 void
932 ScreenWindow::MessageReceived(BMessage* message)
933 {
934 	switch (message->what) {
935 		case WORKSPACE_CHECK_MSG:
936 			_CheckApplyEnabled();
937 			break;
938 
939 		case kMsgWorkspaceColumnsChanged:
940 		{
941 			uint32 newColumns = (uint32)fColumnsControl->Value();
942 
943 			uint32 rows;
944 			BPrivate::get_workspaces_layout(NULL, &rows);
945 			BPrivate::set_workspaces_layout(newColumns, rows);
946 
947 			_UpdateWorkspaceButtons();
948 			fRowsControl->SetValue(rows);
949 				// enables/disables up/down arrows
950 			_CheckApplyEnabled();
951 
952 			break;
953 		}
954 
955 		case kMsgWorkspaceRowsChanged:
956 		{
957 			uint32 newRows = (uint32)fRowsControl->Value();
958 
959 			uint32 columns;
960 			BPrivate::get_workspaces_layout(&columns, NULL);
961 			BPrivate::set_workspaces_layout(columns, newRows);
962 
963 			_UpdateWorkspaceButtons();
964 			fColumnsControl->SetValue(columns);
965 				// enables/disables up/down arrows
966 			_CheckApplyEnabled();
967 			break;
968 		}
969 
970 		case POP_RESOLUTION_MSG:
971 		{
972 			message->FindInt32("width", &fSelected.width);
973 			message->FindInt32("height", &fSelected.height);
974 
975 			_CheckColorMenu();
976 			_CheckRefreshMenu();
977 
978 			_UpdateMonitorView();
979 			_UpdateRefreshControl();
980 
981 			_CheckApplyEnabled();
982 			break;
983 		}
984 
985 		case POP_COLORS_MSG:
986 		{
987 			int32 space;
988 			if (message->FindInt32("space", &space) != B_OK)
989 				break;
990 
991 			int32 index;
992 			if (message->FindInt32("index", &index) == B_OK
993 				&& fColorsMenu->ItemAt(index) != NULL)
994 				fUserSelectedColorSpace = fColorsMenu->ItemAt(index);
995 
996 			fSelected.space = (color_space)space;
997 			_UpdateColorLabel();
998 
999 			_CheckApplyEnabled();
1000 			break;
1001 		}
1002 
1003 		case POP_REFRESH_MSG:
1004 		{
1005 			message->FindFloat("refresh", &fSelected.refresh);
1006 			fOtherRefresh->SetLabel(B_TRANSLATE("Other" B_UTF8_ELLIPSIS));
1007 				// revert "Other…" label - it might have a refresh rate prefix
1008 
1009 			_CheckApplyEnabled();
1010 			break;
1011 		}
1012 
1013 		case POP_OTHER_REFRESH_MSG:
1014 		{
1015 			// make sure menu shows something useful
1016 			_UpdateRefreshControl();
1017 
1018 			float min = 0, max = 999;
1019 			fScreenMode.GetRefreshLimits(fSelected, min, max);
1020 			if (min < gMinRefresh)
1021 				min = gMinRefresh;
1022 			if (max > gMaxRefresh)
1023 				max = gMaxRefresh;
1024 
1025 			monitor_info info;
1026 			if (fScreenMode.GetMonitorInfo(info) == B_OK) {
1027 				min = max_c(info.min_vertical_frequency, min);
1028 				max = min_c(info.max_vertical_frequency, max);
1029 			}
1030 
1031 			RefreshWindow *fRefreshWindow = new RefreshWindow(
1032 				fRefreshField->ConvertToScreen(B_ORIGIN), fSelected.refresh,
1033 				min, max);
1034 			fRefreshWindow->Show();
1035 			break;
1036 		}
1037 
1038 		case SET_CUSTOM_REFRESH_MSG:
1039 		{
1040 			// user pressed "done" in "Other…" refresh dialog;
1041 			// select the refresh rate chosen
1042 			message->FindFloat("refresh", &fSelected.refresh);
1043 
1044 			_UpdateRefreshControl();
1045 			_CheckApplyEnabled();
1046 			break;
1047 		}
1048 
1049 		case POP_COMBINE_DISPLAYS_MSG:
1050 		{
1051 			// new combine mode has bee chosen
1052 			int32 mode;
1053 			if (message->FindInt32("mode", &mode) == B_OK)
1054 				fSelected.combine = (combine_mode)mode;
1055 
1056 			_CheckResolutionMenu();
1057 			_CheckApplyEnabled();
1058 			break;
1059 		}
1060 
1061 		case POP_SWAP_DISPLAYS_MSG:
1062 			message->FindBool("swap", &fSelected.swap_displays);
1063 			_CheckApplyEnabled();
1064 			break;
1065 
1066 		case POP_USE_LAPTOP_PANEL_MSG:
1067 			message->FindBool("use", &fSelected.use_laptop_panel);
1068 			_CheckApplyEnabled();
1069 			break;
1070 
1071 		case POP_TV_STANDARD_MSG:
1072 			message->FindInt32("tv_standard", (int32 *)&fSelected.tv_standard);
1073 			_CheckApplyEnabled();
1074 			break;
1075 
1076 		case BUTTON_LAUNCH_BACKGROUNDS_MSG:
1077 			if (be_roster->Launch(kBackgroundsSignature) == B_ALREADY_RUNNING) {
1078 				app_info info;
1079 				be_roster->GetAppInfo(kBackgroundsSignature, &info);
1080 				be_roster->ActivateApp(info.team);
1081 			}
1082 			break;
1083 
1084 		case BUTTON_DEFAULTS_MSG:
1085 		{
1086 			// TODO: get preferred settings of screen
1087 			fSelected.width = 640;
1088 			fSelected.height = 480;
1089 			fSelected.space = B_CMAP8;
1090 			fSelected.refresh = 60.0;
1091 			fSelected.combine = kCombineDisable;
1092 			fSelected.swap_displays = false;
1093 			fSelected.use_laptop_panel = false;
1094 			fSelected.tv_standard = 0;
1095 
1096 			// TODO: workspace defaults
1097 
1098 			_UpdateControls();
1099 			break;
1100 		}
1101 
1102 		case BUTTON_UNDO_MSG:
1103 			fUndoScreenMode.Revert();
1104 			_UpdateActiveMode();
1105 			break;
1106 
1107 		case BUTTON_REVERT_MSG:
1108 		{
1109 			fModified = false;
1110 			fBootWorkspaceApplied = false;
1111 
1112 			// ScreenMode::Revert() assumes that we first set the correct
1113 			// number of workspaces
1114 
1115 			BPrivate::set_workspaces_layout(fOriginalWorkspacesColumns,
1116 				fOriginalWorkspacesRows);
1117 			_UpdateWorkspaceButtons();
1118 
1119 			fScreenMode.Revert();
1120 			_UpdateActiveMode();
1121 			break;
1122 		}
1123 
1124 		case BUTTON_APPLY_MSG:
1125 			_Apply();
1126 			break;
1127 
1128 		case MAKE_INITIAL_MSG:
1129 			// user pressed "keep" in confirmation dialog
1130 			fModified = true;
1131 			_UpdateActiveMode();
1132 			break;
1133 
1134 		case UPDATE_DESKTOP_COLOR_MSG:
1135 			PostMessage(message, fMonitorView);
1136 			break;
1137 
1138 		default:
1139 			BWindow::MessageReceived(message);
1140 	}
1141 }
1142 
1143 
1144 status_t
1145 ScreenWindow::_WriteVesaModeFile(const screen_mode& mode) const
1146 {
1147 	BPath path;
1148 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path, true);
1149 	if (status < B_OK)
1150 		return status;
1151 
1152 	path.Append("kernel/drivers");
1153 	status = create_directory(path.Path(), 0755);
1154 	if (status < B_OK)
1155 		return status;
1156 
1157 	path.Append("vesa");
1158 	BFile file;
1159 	status = file.SetTo(path.Path(), B_CREATE_FILE | B_WRITE_ONLY | B_ERASE_FILE);
1160 	if (status < B_OK)
1161 		return status;
1162 
1163 	char buffer[256];
1164 	snprintf(buffer, sizeof(buffer), "mode %" B_PRId32 " %" B_PRId32 " %"
1165 		B_PRId32 "\n", mode.width, mode.height, mode.BitsPerPixel());
1166 
1167 	ssize_t bytesWritten = file.Write(buffer, strlen(buffer));
1168 	if (bytesWritten < B_OK)
1169 		return bytesWritten;
1170 
1171 	return B_OK;
1172 }
1173 
1174 
1175 void
1176 ScreenWindow::_BuildSupportedColorSpaces()
1177 {
1178 	fSupportedColorSpaces = 0;
1179 
1180 	for (int32 i = 0; i < kColorSpaceCount; i++) {
1181 		for (int32 j = 0; j < fScreenMode.CountModes(); j++) {
1182 			if (fScreenMode.ModeAt(j).space == kColorSpaces[i].space) {
1183 				fSupportedColorSpaces |= 1 << i;
1184 				break;
1185 			}
1186 		}
1187 	}
1188 }
1189 
1190 
1191 void
1192 ScreenWindow::_CheckApplyEnabled()
1193 {
1194 	bool applyEnabled = true;
1195 
1196 	if (fSelected == fActive) {
1197 		applyEnabled = false;
1198 		if (fAllWorkspacesItem->IsMarked()) {
1199 			screen_mode screenMode;
1200 			const int32 workspaceCount = count_workspaces();
1201 			for (int32 i = 0; i < workspaceCount; i++) {
1202 				fScreenMode.Get(screenMode, i);
1203 				if (screenMode != fSelected) {
1204 					applyEnabled = true;
1205 					break;
1206 				}
1207 			}
1208 		}
1209 	}
1210 
1211 	fApplyButton->SetEnabled(applyEnabled);
1212 
1213 	uint32 columns;
1214 	uint32 rows;
1215 	BPrivate::get_workspaces_layout(&columns, &rows);
1216 
1217 	fRevertButton->SetEnabled(columns != fOriginalWorkspacesColumns
1218 		|| rows != fOriginalWorkspacesRows
1219 		|| fSelected != fOriginal);
1220 }
1221 
1222 
1223 void
1224 ScreenWindow::_UpdateOriginal()
1225 {
1226 	BPrivate::get_workspaces_layout(&fOriginalWorkspacesColumns,
1227 		&fOriginalWorkspacesRows);
1228 
1229 	fScreenMode.Get(fOriginal);
1230 	fScreenMode.UpdateOriginalModes();
1231 }
1232 
1233 
1234 void
1235 ScreenWindow::_UpdateMonitor()
1236 {
1237 	monitor_info info;
1238 	float diagonalInches;
1239 	status_t status = fScreenMode.GetMonitorInfo(info, &diagonalInches);
1240 	if (status == B_OK) {
1241 		char text[512];
1242 		snprintf(text, sizeof(text), "%s%s%s %g\"", info.vendor,
1243 			info.name[0] ? " " : "", info.name, diagonalInches);
1244 
1245 		fMonitorInfo->SetText(text);
1246 
1247 		if (fMonitorInfo->IsHidden(fMonitorInfo))
1248 			fMonitorInfo->Show();
1249 	} else {
1250 		if (!fMonitorInfo->IsHidden(fMonitorInfo))
1251 			fMonitorInfo->Hide();
1252 	}
1253 
1254 	// Add info about the graphics device
1255 
1256 	accelerant_device_info deviceInfo;
1257 
1258 	if (fScreenMode.GetDeviceInfo(deviceInfo) == B_OK) {
1259 		BString deviceString;
1260 
1261 		if (deviceInfo.name[0] && deviceInfo.chipset[0]) {
1262 			deviceString.SetToFormat("%s (%s)", deviceInfo.name,
1263 				deviceInfo.chipset);
1264 		} else if (deviceInfo.name[0] || deviceInfo.chipset[0]) {
1265 			deviceString
1266 				= deviceInfo.name[0] ? deviceInfo.name : deviceInfo.chipset;
1267 		}
1268 
1269 		fDeviceInfo->SetText(deviceString);
1270 	}
1271 
1272 
1273 	char text[512];
1274 	size_t length = 0;
1275 	text[0] = 0;
1276 
1277 	if (status == B_OK) {
1278 		if (info.min_horizontal_frequency != 0
1279 			&& info.min_vertical_frequency != 0
1280 			&& info.max_pixel_clock != 0) {
1281 			length = snprintf(text, sizeof(text),
1282 				B_TRANSLATE("Horizonal frequency:\t%lu - %lu kHz\n"
1283 				"Vertical frequency:\t%lu - %lu Hz\n\n"
1284 				"Maximum pixel clock:\t%g MHz"),
1285 				info.min_horizontal_frequency, info.max_horizontal_frequency,
1286 				info.min_vertical_frequency, info.max_vertical_frequency,
1287 				info.max_pixel_clock / 1000.0);
1288 		}
1289 		if (info.serial_number[0] && length < sizeof(text)) {
1290 			length += snprintf(text + length, sizeof(text) - length,
1291 				B_TRANSLATE("%sSerial no.: %s"), length ? "\n\n" : "",
1292 				info.serial_number);
1293 			if (info.produced.week != 0 && info.produced.year != 0
1294 				&& length < sizeof(text)) {
1295 				length += snprintf(text + length, sizeof(text) - length,
1296 					" (%u/%u)", info.produced.week, info.produced.year);
1297 			}
1298 		}
1299 	}
1300 
1301 	if (text[0])
1302 		fMonitorView->SetToolTip(text);
1303 }
1304 
1305 
1306 void
1307 ScreenWindow::_UpdateColorLabel()
1308 {
1309 	BString string;
1310 	string << fSelected.BitsPerPixel() << " " << B_TRANSLATE("bits/pixel");
1311 	fColorsMenu->Superitem()->SetLabel(string.String());
1312 }
1313 
1314 
1315 void
1316 ScreenWindow::_Apply()
1317 {
1318 	// make checkpoint, so we can undo these changes
1319 	fUndoScreenMode.UpdateOriginalModes();
1320 
1321 	status_t status = fScreenMode.Set(fSelected);
1322 	if (status == B_OK) {
1323 		// use the mode that has eventually been set and
1324 		// thus we know to be working; it can differ from
1325 		// the mode selected by user due to hardware limitation
1326 		display_mode newMode;
1327 		BScreen screen(this);
1328 		screen.GetMode(&newMode);
1329 
1330 		if (fAllWorkspacesItem->IsMarked()) {
1331 			int32 originatingWorkspace = current_workspace();
1332 			const int32 workspaceCount = count_workspaces();
1333 			for (int32 i = 0; i < workspaceCount; i++) {
1334 				if (i != originatingWorkspace)
1335 					screen.SetMode(i, &newMode, true);
1336 			}
1337 			fBootWorkspaceApplied = true;
1338 		} else {
1339 			if (current_workspace() == 0)
1340 				fBootWorkspaceApplied = true;
1341 		}
1342 
1343 		fActive = fSelected;
1344 
1345 		// TODO: only show alert when this is an unknown mode
1346 		BAlert* window = new AlertWindow(this);
1347 		window->Go(NULL);
1348 	} else {
1349 		char message[256];
1350 		snprintf(message, sizeof(message),
1351 			B_TRANSLATE("The screen mode could not be set:\n\t%s\n"),
1352 			screen_errors(status));
1353 		BAlert* alert = new BAlert(B_TRANSLATE("Warning"), message,
1354 			B_TRANSLATE("OK"), NULL, NULL,
1355 			B_WIDTH_AS_USUAL, B_WARNING_ALERT);
1356 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
1357 		alert->Go();
1358 	}
1359 }
1360