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
36BMessage *
37delay_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// ---------------------------------------------------------------
57SlideShowConfigView::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	SetViewUIColor(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" B_UTF8_ELLIPSIS, 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	delete pMsg;
140}
141
142// ---------------------------------------------------------------
143// Destructor
144//
145// Releases the translator settings
146//
147// Preconditions:
148//
149// Parameters:
150//
151// Postconditions:
152//
153// Returns:
154// ---------------------------------------------------------------
155SlideShowConfigView::~SlideShowConfigView()
156{
157	fSettings->Release();
158
159	delete fFilePanel;
160	fFilePanel = NULL;
161}
162
163// ---------------------------------------------------------------
164// AllAttached
165//
166//
167//
168// Preconditions:
169//
170// Parameters:
171//
172// Postconditions:
173//
174// Returns:
175// ---------------------------------------------------------------
176void
177SlideShowConfigView::AllAttached()
178{
179	BMessenger msgr(this);
180	fShowCaption->SetTarget(msgr);
181	fShowBorder->SetTarget(msgr);
182	fChooseFolder->SetTarget(msgr);
183	fFilePanel->SetTarget(msgr);
184
185	// Set target for menu items
186	for (int32 i = 0; i < fDelayMenu->CountItems(); i++) {
187		BMenuItem *item = fDelayMenu->ItemAt(i);
188		if (item)
189			item->SetTarget(msgr);
190	}
191}
192
193// ---------------------------------------------------------------
194// MessageReceived
195//
196// Handles state changes of the RLE setting checkbox
197//
198// Preconditions:
199//
200// Parameters: message	the actual BMessage that was received
201//
202// Postconditions:
203//
204// Returns:
205// ---------------------------------------------------------------
206void
207SlideShowConfigView::MessageReceived(BMessage *message)
208{
209	bool bNewVal;
210	switch (message->what) {
211		case CHANGE_CAPTION:
212			if (fShowCaption->Value())
213				bNewVal = true;
214			else
215				bNewVal = false;
216			fSettings->SetGetBool(SAVER_SETTING_CAPTION, &bNewVal);
217			fSettings->SaveSettings();
218			break;
219
220		case CHANGE_BORDER:
221			if (fShowBorder->Value())
222				bNewVal = true;
223			else
224				bNewVal = false;
225			fSettings->SetGetBool(SAVER_SETTING_BORDER, &bNewVal);
226			fSettings->SaveSettings();
227			break;
228
229		case CHOOSE_DIRECTORY:
230		{
231			BString strDirectory;
232			fSettings->GetString(SAVER_SETTING_DIRECTORY, strDirectory);
233			BEntry entry(strDirectory.String());
234			if (entry.InitCheck() != B_OK)
235				return;
236			entry_ref ref;
237			if (entry.GetRef(&ref) != B_OK)
238				return;
239			fFilePanel->SetPanelDirectory(&ref);
240
241			fFilePanel->Show();
242			break;
243		}
244
245		case CHANGE_DIRECTORY:
246		{
247			entry_ref ref;
248			if (message->FindRef("refs", &ref) != B_OK)
249				return;
250			BEntry entry(&ref, true);
251			if (entry.InitCheck() != B_OK)
252				return;
253			BPath path(&entry);
254			if (path.InitCheck() != B_OK)
255				return;
256			BString strDirectory = path.Path();
257
258			fSettings->SetString(SAVER_SETTING_DIRECTORY, strDirectory);
259			fSettings->SaveSettings();
260
261			Invalidate();
262			break;
263		}
264
265		case CHANGE_DELAY:
266		{
267			int32 newVal;
268			if (message->FindInt32("delay", &newVal) == B_OK) {
269				newVal *= 1000;
270				fSettings->SetGetInt32(SAVER_SETTING_DELAY, &newVal);
271				fSettings->SaveSettings();
272			}
273			break;
274		}
275
276		default:
277			BView::MessageReceived(message);
278			break;
279	}
280}
281
282// ---------------------------------------------------------------
283// Draw
284//
285// Draws information about the SlideShowConfigTranslator to this view.
286//
287// Preconditions:
288//
289// Parameters: area,	not used
290//
291// Postconditions:
292//
293// Returns:
294// ---------------------------------------------------------------
295void
296SlideShowConfigView::Draw(BRect area)
297{
298	SetFont(be_bold_font);
299	font_height fh;
300	GetFontHeight(&fh);
301	float xbold, ybold;
302	xbold = fh.descent + 1;
303	ybold = fh.ascent + fh.descent * 2 + fh.leading;
304
305	char title[] = "SlideShow Screen Saver";
306	DrawString(title, BPoint(xbold, ybold));
307
308	SetFont(be_plain_font);
309	font_height plainh;
310	GetFontHeight(&plainh);
311	float yplain;
312	yplain = plainh.ascent + plainh.descent * 2 + plainh.leading;
313
314	char writtenby[] = "Written by Michael Wilber";
315	DrawString(writtenby, BPoint(xbold, yplain * 1 + ybold));
316
317	// Draw current folder
318	BString strFolder;
319	fSettings->GetString(SAVER_SETTING_DIRECTORY, strFolder);
320	strFolder.Prepend("Image folder: ");
321	DrawString(strFolder.String(), BPoint(10, yplain * 9 + ybold));
322}
323