xref: /haiku/src/preferences/screen/RefreshWindow.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2001-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Rafael Romo
7  *		Stefano Ceccherini (burton666@libero.it)
8  *		Axel Dörfler, axeld@pinc-software.de
9  */
10 
11 
12 #include "RefreshWindow.h"
13 
14 #include "Constants.h"
15 #include "RefreshSlider.h"
16 
17 #include <Alert.h>
18 #include <Application.h>
19 #include <Button.h>
20 #include <Catalog.h>
21 #include <String.h>
22 #include <StringView.h>
23 #include <Window.h>
24 
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "Screen"
28 
29 
30 RefreshWindow::RefreshWindow(BPoint position, float current, float min, float max)
31 	: BWindow(BRect(0, 0, 300, 200), B_TRANSLATE("Refresh rate"), B_MODAL_WINDOW,
32 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS, B_ALL_WORKSPACES)
33 {
34 	min = ceilf(min);
35 	max = floorf(max);
36 
37 	BView* topView = new BView(Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW);
38 	topView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
39 	AddChild(topView);
40 
41 	BRect rect = Bounds().InsetByCopy(8, 8);
42 	BStringView* stringView = new BStringView(rect, "info",
43 		B_TRANSLATE("Type or use the left and right arrow keys."));
44 	stringView->ResizeToPreferred();
45 	topView->AddChild(stringView);
46 
47 	rect.top += stringView->Bounds().Height() + 14;
48 	fRefreshSlider = new RefreshSlider(rect, min, max, B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT);
49 	fRefreshSlider->SetValue((int32)rintf(current * 10));
50 	fRefreshSlider->SetModificationMessage(new BMessage(SLIDER_MODIFICATION_MSG));
51 	float width, height;
52 	fRefreshSlider->GetPreferredSize(&width, &height);
53 	fRefreshSlider->ResizeTo(rect.Width(), height);
54 	topView->AddChild(fRefreshSlider);
55 
56 	BButton* doneButton = new BButton(rect, "DoneButton", B_TRANSLATE("Done"),
57 		new BMessage(BUTTON_DONE_MSG), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
58 	doneButton->ResizeToPreferred();
59 	doneButton->MoveTo(Bounds().Width() - doneButton->Bounds().Width() - 8,
60 		Bounds().Height() - doneButton->Bounds().Height() - 8);
61 	topView->AddChild(doneButton);
62 
63 	BButton* button = new BButton(doneButton->Frame(), "CancelButton",
64 		B_TRANSLATE("Cancel"), new BMessage(B_QUIT_REQUESTED),
65 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
66 	button->ResizeToPreferred();
67 	button->MoveBy(-button->Bounds().Width() - 10, 0);
68 	topView->AddChild(button);
69 
70 	doneButton->MakeDefault(true);
71 
72 	width = stringView->Bounds().Width() + 100;
73 	if (width < Bounds().Width())
74 		width = Bounds().Width();
75 	height = fRefreshSlider->Frame().bottom + button->Bounds().Height() + 20.0f;
76 
77 	ResizeTo(width, height);
78 	MoveTo(position.x - width / 2.5f, position.y - height / 1.9f);
79 }
80 
81 
82 void
83 RefreshWindow::WindowActivated(bool active)
84 {
85 	fRefreshSlider->MakeFocus(active);
86 }
87 
88 
89 void
90 RefreshWindow::MessageReceived(BMessage* message)
91 {
92 	switch (message->what) {
93 		case BUTTON_DONE_MSG:
94 		{
95 			float value = (float)fRefreshSlider->Value() / 10;
96 
97 			BMessage message(SET_CUSTOM_REFRESH_MSG);
98 			message.AddFloat("refresh", value);
99 			be_app->PostMessage(&message);
100 
101 			PostMessage(B_QUIT_REQUESTED);
102 			break;
103 		}
104 
105 		case SLIDER_INVOKE_MSG:
106 			fRefreshSlider->MakeFocus(true);
107 			break;
108 
109 		default:
110 			BWindow::MessageReceived(message);
111 			break;
112 	}
113 }
114