xref: /haiku/src/add-ons/screen_savers/glife/GLifeConfig.cpp (revision cb29eafe2586fdb2d7685afa69fdab5d88a8b576)
1 /*
2  * Copyright 2012, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 		Aaron Hill <serac@hillvisions.com>
7  *		Alexander von Gluck <kallisti5@unixzen.com>
8  */
9 
10 
11 #include "GLifeConfig.h"
12 
13 #include <Catalog.h>
14 #include <GroupLayoutBuilder.h>
15 #include <Slider.h>
16 #include <stdio.h>
17 #include <String.h>
18 #include <StringView.h>
19 #include <View.h>
20 
21 #include "GLifeState.h"
22 
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "GLife ScreenSaver"
25 
26 
27 // ------------------------------------------------------
28 //  GLifeConfig Class Constructor Definition
29 GLifeConfig::GLifeConfig(BRect frame, GLifeState* pglsState)
30 	:
31 	BView(frame, "", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
32 	m_pglsState(pglsState)
33 {
34 	SetLayout(new BGroupLayout(B_HORIZONTAL));
35 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
36 
37 	// Info text
38 	BStringView* name = new BStringView(frame, B_EMPTY_STRING,
39 		B_TRANSLATE("OpenGL \"Game of Life\""), B_FOLLOW_LEFT);
40 	BStringView* author = new BStringView(frame, B_EMPTY_STRING,
41 		B_TRANSLATE("by Aaron Hill"), B_FOLLOW_LEFT);
42 
43 	// Sliders
44 	fGridDelay = new BSlider(frame, "GridDelay",
45 		B_TRANSLATE("Grid life delay: "),
46 		new BMessage(kGridDelay),
47 		0, 4, B_BLOCK_THUMB);
48 
49 	fGridDelay->SetHashMarks(B_HASH_MARKS_BOTTOM);
50 	fGridDelay->SetLimitLabels(B_TRANSLATE("None"), B_TRANSLATE_COMMENT("4x",
51 			"This is a factor: the x represents 'times'"));
52 	fGridDelay->SetValue(pglsState->GridDelay());
53 	fGridDelay->SetHashMarkCount(5);
54 
55 	fGridBorder = new BSlider(frame, "GridBorder",
56 		B_TRANSLATE("Grid border: "),
57 		new BMessage(kGridBorder),
58 		0, 10, B_BLOCK_THUMB);
59 
60 	fGridBorder->SetHashMarks(B_HASH_MARKS_BOTTOM);
61 	fGridBorder->SetLimitLabels("0", "10");
62 	fGridBorder->SetValue(pglsState->GridBorder());
63 	fGridBorder->SetHashMarkCount(11);
64 
65 	fGridWidth = new BSlider(frame, "GridWidth",
66 		B_TRANSLATE("Grid width: "),
67 		new BMessage(kGridWidth),
68 		10, 100, B_BLOCK_THUMB);
69 
70 	fGridWidth->SetHashMarks(B_HASH_MARKS_BOTTOM);
71 	//fGridWidth->SetLimitLabels("10", "100");
72 	fGridWidth->SetValue(pglsState->GridWidth());
73 	fGridWidth->SetHashMarkCount(10);
74 
75 	fGridHeight = new BSlider(frame, "GridHeight",
76 		B_TRANSLATE("Grid height: "),
77 		new BMessage(kGridHeight),
78 		10, 100, B_BLOCK_THUMB);
79 
80 	fGridHeight->SetHashMarks(B_HASH_MARKS_BOTTOM);
81 	//fGridHeight->SetLimitLabels("10", "100");
82 	fGridHeight->SetValue(pglsState->GridHeight());
83 	fGridHeight->SetHashMarkCount(10);
84 
85 	AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_DEFAULT_SPACING)
86 		.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
87 			.Add(name)
88 			.Add(author)
89 		)
90 		.AddGlue()
91 		.Add(fGridDelay)
92 		.Add(fGridBorder)
93 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
94 			.Add(fGridWidth)
95 			.Add(fGridHeight)
96 		)
97 		.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
98 			B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
99 	);
100 
101 	// Do our first label update
102 	_UpdateLabels();
103 }
104 
105 
106 // ------------------------------------------------------
107 //  GLifeConfig Class AttachedToWindow Definition
108 void
109 GLifeConfig::AttachedToWindow(void)
110 {
111 	fGridWidth->SetTarget(this);
112 	fGridHeight->SetTarget(this);
113 	fGridBorder->SetTarget(this);
114 	fGridDelay->SetTarget(this);
115 
116 #ifdef _USE_ASYNCHRONOUS
117 
118 	m_uiWindowFlags = Window()->Flags();
119 	Window()->SetFlags(m_uiWindowFlags | B_ASYNCHRONOUS_CONTROLS);
120 
121 #endif
122 }
123 
124 
125 void
126 GLifeConfig::_UpdateLabels()
127 {
128 	BString newLabel;
129 	newLabel.SetToFormat(B_TRANSLATE("Grid width: %li"), fGridWidth->Value());
130 	fGridWidth->SetLabel(newLabel);
131 
132 	newLabel.SetToFormat(B_TRANSLATE("Grid height: %li"), fGridHeight->Value());
133 	fGridHeight->SetLabel(newLabel);
134 
135 	newLabel.SetToFormat(B_TRANSLATE("Grid border: %li"), fGridBorder->Value());
136 	fGridBorder->SetLabel(newLabel);
137 
138 	int32 delay = fGridDelay->Value();
139 	if (delay <= 0)
140 		newLabel = B_TRANSLATE("Grid life delay: none");
141 	else {
142 		newLabel.SetToFormat(B_TRANSLATE_CONTEXT("Grid life delay: "
143 			"%" B_PRId32 "x", "This is a factor: the x represents 'times', "
144 			"don't translate '%" B_PRId32"'"), delay);
145 	}
146 	fGridDelay->SetLabel(newLabel);
147 }
148 
149 
150 // ------------------------------------------------------
151 //  GLifeConfig Class MessageReceived Definition
152 void
153 GLifeConfig::MessageReceived(BMessage* pbmMessage)
154 {
155 	switch(pbmMessage->what) {
156 		case kGridWidth:
157 			m_pglsState->GridWidth() = fGridWidth->Value();
158 			break;
159 		case kGridHeight:
160 			m_pglsState->GridHeight() = fGridHeight->Value();
161 			break;
162 		case kGridBorder:
163 			m_pglsState->GridBorder() = fGridBorder->Value();
164 			break;
165 		case kGridDelay:
166 			m_pglsState->GridDelay() = fGridDelay->Value();
167 			break;
168 		default:
169 			BView::MessageReceived(pbmMessage);
170 			return;
171 	}
172 	_UpdateLabels();
173 }
174