xref: /haiku/src/add-ons/screen_savers/slideshowsaver/SlideShowConfigView.cpp (revision ed6250c95736c0b55da79d6e9dd01369532260c0)
1 /*****************************************************************************/
2 // SlideShowConfigView
3 // Written by Michael Wilber
4 //
5 // SlideShowConfigView.cpp
6 //
7 // This BView based object displays the SlideShowSaver settings options
8 //
9 //
10 // Copyright (C) Haiku
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a
13 // copy of this software and associated documentation files (the "Software"),
14 // to deal in the Software without restriction, including without limitation
15 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 // and/or sell copies of the Software, and to permit persons to whom the
17 // Software is furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be included
20 // in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
29 /*****************************************************************************/
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <FilePanel.h>
34 #include "SlideShowConfigView.h"
35 
36 BMessage *
37 delay_msg(int32 option)
38 {
39 	BMessage *pMsg = new BMessage(CHANGE_DELAY);
40 	pMsg->AddInt32("delay", option);
41 	return pMsg;
42 }
43 
44 // ---------------------------------------------------------------
45 // Constructor
46 //
47 // Sets up the view settings
48 //
49 // Preconditions:
50 //
51 // Parameters:
52 //
53 // Postconditions:
54 //
55 // Returns:
56 // ---------------------------------------------------------------
57 SlideShowConfigView::SlideShowConfigView(const BRect &frame, const char *name,
58 	uint32 resize, uint32 flags, LiveSettings *settings)
59 	:	BView(frame, name, resize, flags)
60 {
61 	fSettings = settings;
62 
63 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
64 
65 	BMessage *pMsg;
66 	int32 val;
67 
68 	// Show Caption checkbox
69 	pMsg = new BMessage(CHANGE_CAPTION);
70 	fShowCaption = new BCheckBox(BRect(10, 45, 180, 62),
71 		"Show Caption", "Show Caption", pMsg);
72 	val = (fSettings->SetGetBool(SAVER_SETTING_CAPTION)) ? 1 : 0;
73 	fShowCaption->SetValue(val);
74 	fShowCaption->SetViewColor(ViewColor());
75 	AddChild(fShowCaption);
76 
77 	// Change Border checkbox
78 	pMsg = new BMessage(CHANGE_BORDER);
79 	fShowBorder = new BCheckBox(BRect(10, 70, 180, 87),
80 		"Show Border", "Show Border", pMsg);
81 	val = (fSettings->SetGetBool(SAVER_SETTING_BORDER)) ? 1 : 0;
82 	fShowBorder->SetValue(val);
83 	fShowBorder->SetViewColor(ViewColor());
84 	AddChild(fShowBorder);
85 
86 	// Delay Menu
87 	// setup PNG interlace options menu
88 	int32 currentDelay = fSettings->SetGetInt32(SAVER_SETTING_DELAY) / 1000;
89 	fDelayMenu = new BPopUpMenu("Delay Menu");
90 	struct DelayItem {
91 		const char *name;
92 		int32 delay;
93 	};
94 	DelayItem items[] = {
95 		{"No Delay",	0},
96 		{"1 second",	1},
97 		{"2 seconds",	2},
98 		{"3 seconds",	3},
99 		{"4 seconds",	4},
100 		{"5 seconds",	5},
101 		{"6 seconds",	6},
102 		{"7 seconds",	7},
103 		{"8 seconds",	8},
104 		{"9 seconds",	9},
105 		{"10 seconds",	10},
106 		{"15 seconds",	15},
107 		{"20 seconds",	20},
108 		{"30 seconds",	30},
109 		{"1 minute",	1 * 60},
110 		{"2 minutes",	2 * 60},
111 		{"5 minutes",	5 * 60},
112 		{"10 minutes",	10 * 60},
113 		{"15 minutes",	15 * 60}
114 	};
115 	for (uint32 i = 0; i < sizeof(items) / sizeof(DelayItem); i++) {
116 		BMenuItem *menuItem =
117 			new BMenuItem(items[i].name, delay_msg(items[i].delay));
118 		fDelayMenu->AddItem(menuItem);
119 		if (items[i].delay == currentDelay)
120 			menuItem->SetMarked(true);
121 	}
122 	fDelayMenuField = new BMenuField(BRect(10, 100, 180, 120),
123 		"Delay Menu Field", "Delay:", fDelayMenu);
124 	fDelayMenuField->SetViewColor(ViewColor());
125 	fDelayMenuField->SetDivider(40);
126 	AddChild(fDelayMenuField);
127 
128 	// Choose Image Folder button
129 	pMsg = new BMessage(CHOOSE_DIRECTORY);
130 	fChooseFolder = new BButton(BRect(50, 160, 180, 180),
131 		"Choose Folder", "Choose Image Folder...", pMsg);
132 	AddChild(fChooseFolder);
133 
134 	// Setup choose folder file panel
135 	pMsg = new BMessage(CHANGE_DIRECTORY);
136 	fFilePanel = new BFilePanel(B_OPEN_PANEL, NULL, (const entry_ref *) NULL,
137 		B_DIRECTORY_NODE, false, pMsg, NULL, true, true);
138 	fFilePanel->SetButtonLabel(B_DEFAULT_BUTTON, "Select");
139 }
140 
141 // ---------------------------------------------------------------
142 // Destructor
143 //
144 // Releases the translator settings
145 //
146 // Preconditions:
147 //
148 // Parameters:
149 //
150 // Postconditions:
151 //
152 // Returns:
153 // ---------------------------------------------------------------
154 SlideShowConfigView::~SlideShowConfigView()
155 {
156 	fSettings->Release();
157 
158 	delete fFilePanel;
159 	fFilePanel = NULL;
160 }
161 
162 // ---------------------------------------------------------------
163 // AllAttached
164 //
165 //
166 //
167 // Preconditions:
168 //
169 // Parameters:
170 //
171 // Postconditions:
172 //
173 // Returns:
174 // ---------------------------------------------------------------
175 void
176 SlideShowConfigView::AllAttached()
177 {
178 	BMessenger msgr(this);
179 	fShowCaption->SetTarget(msgr);
180 	fShowBorder->SetTarget(msgr);
181 	fChooseFolder->SetTarget(msgr);
182 	fFilePanel->SetTarget(msgr);
183 
184 	// Set target for menu items
185 	for (int32 i = 0; i < fDelayMenu->CountItems(); i++) {
186 		BMenuItem *item = fDelayMenu->ItemAt(i);
187 		if (item)
188 			item->SetTarget(msgr);
189 	}
190 }
191 
192 // ---------------------------------------------------------------
193 // MessageReceived
194 //
195 // Handles state changes of the RLE setting checkbox
196 //
197 // Preconditions:
198 //
199 // Parameters: message	the actual BMessage that was received
200 //
201 // Postconditions:
202 //
203 // Returns:
204 // ---------------------------------------------------------------
205 void
206 SlideShowConfigView::MessageReceived(BMessage *message)
207 {
208 	bool bNewVal;
209 	switch (message->what) {
210 		case CHANGE_CAPTION:
211 			if (fShowCaption->Value())
212 				bNewVal = true;
213 			else
214 				bNewVal = false;
215 			fSettings->SetGetBool(SAVER_SETTING_CAPTION, &bNewVal);
216 			fSettings->SaveSettings();
217 			break;
218 
219 		case CHANGE_BORDER:
220 			if (fShowBorder->Value())
221 				bNewVal = true;
222 			else
223 				bNewVal = false;
224 			fSettings->SetGetBool(SAVER_SETTING_BORDER, &bNewVal);
225 			fSettings->SaveSettings();
226 			break;
227 
228 		case CHOOSE_DIRECTORY:
229 		{
230 			BString strDirectory;
231 			fSettings->GetString(SAVER_SETTING_DIRECTORY, strDirectory);
232 			BEntry entry(strDirectory.String());
233 			if (entry.InitCheck() != B_OK)
234 				return;
235 			entry_ref ref;
236 			if (entry.GetRef(&ref) != B_OK)
237 				return;
238 			fFilePanel->SetPanelDirectory(&ref);
239 
240 			fFilePanel->Show();
241 			break;
242 		}
243 
244 		case CHANGE_DIRECTORY:
245 		{
246 			entry_ref ref;
247 			if (message->FindRef("refs", &ref) != B_OK)
248 				return;
249 			BEntry entry(&ref, true);
250 			if (entry.InitCheck() != B_OK)
251 				return;
252 			BPath path(&entry);
253 			if (path.InitCheck() != B_OK)
254 				return;
255 			BString strDirectory = path.Path();
256 
257 			fSettings->SetString(SAVER_SETTING_DIRECTORY, strDirectory);
258 			fSettings->SaveSettings();
259 
260 			Invalidate();
261 			break;
262 		}
263 
264 		case CHANGE_DELAY:
265 		{
266 			int32 newVal;
267 			if (message->FindInt32("delay", &newVal) == B_OK) {
268 				newVal *= 1000;
269 				fSettings->SetGetInt32(SAVER_SETTING_DELAY, &newVal);
270 				fSettings->SaveSettings();
271 			}
272 			break;
273 		}
274 
275 		default:
276 			BView::MessageReceived(message);
277 			break;
278 	}
279 }
280 
281 // ---------------------------------------------------------------
282 // Draw
283 //
284 // Draws information about the SlideShowConfigTranslator to this view.
285 //
286 // Preconditions:
287 //
288 // Parameters: area,	not used
289 //
290 // Postconditions:
291 //
292 // Returns:
293 // ---------------------------------------------------------------
294 void
295 SlideShowConfigView::Draw(BRect area)
296 {
297 	SetFont(be_bold_font);
298 	font_height fh;
299 	GetFontHeight(&fh);
300 	float xbold, ybold;
301 	xbold = fh.descent + 1;
302 	ybold = fh.ascent + fh.descent * 2 + fh.leading;
303 
304 	char title[] = "SlideShow Screen Saver";
305 	DrawString(title, BPoint(xbold, ybold));
306 
307 	SetFont(be_plain_font);
308 	font_height plainh;
309 	GetFontHeight(&plainh);
310 	float yplain;
311 	yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
312 
313 	char writtenby[] = "Written by Michael Wilber";
314 	DrawString(writtenby, BPoint(xbold, yplain * 1 + ybold));
315 
316 	// Draw current folder
317 	BString strFolder;
318 	fSettings->GetString(SAVER_SETTING_DIRECTORY, strFolder);
319 	strFolder.Prepend("Image Folder: ");
320 	DrawString(strFolder.String(), BPoint(10, yplain * 9 + ybold));
321 }
322