xref: /haiku/src/preferences/screen/RefreshSlider.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
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 "RefreshSlider.h"
13 #include "Constants.h"
14 
15 #include <Catalog.h>
16 #include <String.h>
17 #include <Window.h>
18 
19 #include <new>
20 #include <stdio.h>
21 
22 
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "Screen"
25 
26 
27 RefreshSlider::RefreshSlider(BRect frame, float min, float max, uint32 resizingMode)
28 	: BSlider(frame, B_TRANSLATE("Screen"), B_TRANSLATE("Refresh rate:"),
29 		new BMessage(SLIDER_INVOKE_MSG), (int32)rintf(min * 10), (int32)rintf(max * 10),
30 		B_BLOCK_THUMB, resizingMode),
31 	fStatus(new (std::nothrow) char[32])
32 {
33 	BString minRefresh;
34 	minRefresh << (uint32)min;
35 	BString maxRefresh;
36 	maxRefresh << (uint32)max;
37 	SetLimitLabels(minRefresh.String(), maxRefresh.String());
38 
39 	SetHashMarks(B_HASH_MARKS_BOTTOM);
40 	SetHashMarkCount(uint32(max - min) / 5 + 1);
41 
42 	SetKeyIncrementValue(1);
43 }
44 
45 
46 RefreshSlider::~RefreshSlider()
47 {
48 	delete[] fStatus;
49 }
50 
51 
52 void
53 RefreshSlider::DrawFocusMark()
54 {
55 	if (IsFocus()) {
56 		rgb_color blue = { 0, 0, 229, 255 };
57 
58 		BRect rect(ThumbFrame());
59 		BView *view = OffscreenView();
60 
61 		rect.InsetBy(2.0, 2.0);
62 		rect.right--;
63 		rect.bottom--;
64 
65 		view->SetHighColor(blue);
66 		view->StrokeRect(rect);
67 	}
68 }
69 
70 
71 void
72 RefreshSlider::KeyDown(const char *bytes, int32 numBytes)
73 {
74 	switch (*bytes) {
75 		case B_LEFT_ARROW:
76 		{
77 			SetValue(Value() - 1);
78 			Invoke();
79 			break;
80 		}
81 
82 		case B_RIGHT_ARROW:
83 		{
84 			SetValue(Value() + 1);
85 			Invoke();
86 			break;
87 		}
88 
89 		default:
90 			break;
91 	}
92 }
93 
94 
95 const char*
96 RefreshSlider::UpdateText() const
97 {
98 	if (fStatus != NULL)
99 		snprintf(fStatus, 32, B_TRANSLATE("%.1f Hz"), (float)Value() / 10);
100 
101 	return fStatus;
102 }
103