xref: /haiku/src/apps/screenshot/ScreenshotWindow.cpp (revision 675ffabd70492a962f8c0288a32208c22ce5de18)
1 /*
2  * Copyright 2010 Wim van der Meer <WPJvanderMeer@gmail.com>
3  * Copyright Karsten Heimrich, host.haiku@gmx.de.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Karsten Heimrich
8  *		Fredrik Modéen
9  *		Christophe Huriaux
10  *		Wim van der Meer
11  */
12 
13 
14 #include "ScreenshotWindow.h"
15 
16 #include <stdlib.h>
17 
18 #include <Alert.h>
19 #include <Application.h>
20 #include <Bitmap.h>
21 #include <Box.h>
22 #include <Button.h>
23 #include <Catalog.h>
24 #include <CheckBox.h>
25 #include <File.h>
26 #include <FilePanel.h>
27 #include <FindDirectory.h>
28 #include <GridLayoutBuilder.h>
29 #include <GroupLayoutBuilder.h>
30 #include <Locale.h>
31 #include <Menu.h>
32 #include <MenuField.h>
33 #include <MenuItem.h>
34 #include <MessageFilter.h>
35 #include <Path.h>
36 #include <Roster.h>
37 #include <String.h>
38 #include <StringView.h>
39 #include <TextControl.h>
40 #include <TranslationUtils.h>
41 #include <TranslatorRoster.h>
42 
43 #include "PreviewView.h"
44 #include "Utility.h"
45 
46 
47 #undef B_TRANSLATE_CONTEXT
48 #define B_TRANSLATE_CONTEXT "ScreenshotWindow"
49 
50 
51 enum {
52 	kActiveWindow,
53 	kIncludeBorder,
54 	kIncludeCursor,
55 	kNewScreenshot,
56 	kImageFormat,
57 	kLocationChanged,
58 	kChooseLocation,
59 	kSaveScreenshot,
60 	kSettings,
61 	kCloseTranslatorSettings
62 };
63 
64 
65 // #pragma mark - QuitMessageFilter
66 
67 
68 class QuitMessageFilter : public BMessageFilter {
69 public:
70 	QuitMessageFilter(BWindow* window)
71 		:
72 		BMessageFilter((uint32)B_QUIT_REQUESTED),
73 		fWindow(window)
74 	{
75 	}
76 
77 	virtual filter_result Filter(BMessage* message, BHandler** target)
78 	{
79 		BMessenger(fWindow).SendMessage(kCloseTranslatorSettings);
80 		return B_SKIP_MESSAGE;
81 	}
82 
83 private:
84 	BWindow* fWindow;
85 };
86 
87 
88 // #pragma mark - DirectoryRefFilter
89 
90 
91 class DirectoryRefFilter : public BRefFilter {
92 public:
93 	virtual ~DirectoryRefFilter()
94 	{
95 	}
96 
97 	virtual bool Filter(const entry_ref* ref, BNode* node,
98 		struct stat_beos* stat, const char* filetype)
99 	{
100 		return node->IsDirectory();
101 	}
102 };
103 
104 
105 // #pragma mark - ScreenshotWindow
106 
107 
108 ScreenshotWindow::ScreenshotWindow(const Utility& utility, bool silent,
109 	bool clipboard)
110 	:
111 	BWindow(BRect(0, 0, 200.0, 100.0), B_TRANSLATE("Screenshot"),
112 		B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_AVOID_FRONT
113 		| B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS
114 		| B_CLOSE_ON_ESCAPE),
115 	fUtility(utility),
116 	fDelayControl(NULL),
117 	fScreenshot(NULL),
118 	fOutputPathPanel(NULL),
119 	fLastSelectedPath(NULL),
120 	fSettingsWindow(NULL),
121 	fDelay(0),
122 	fIncludeBorder(false),
123 	fIncludeCursor(false),
124 	fGrabActiveWindow(false),
125 	fOutputFilename(NULL),
126 	fExtension(""),
127 	fImageFileType(B_PNG_FORMAT)
128 {
129 	// _ReadSettings() needs a valid fOutputPathMenu
130 	fOutputPathMenu = new BMenu(B_TRANSLATE("Please select"));
131 	_ReadSettings();
132 
133 	// _NewScreenshot() needs a valid fNameControl
134 	BString name(B_TRANSLATE_NOCOLLECT(fUtility.sDefaultFileNameBase));
135 	name << 1;
136 	name = _FindValidFileName(name.String());
137 	fNameControl = new BTextControl("", B_TRANSLATE("Name:"), name, NULL);
138 
139 	// Check if fUtility contains valid data
140 	if (fUtility.wholeScreen == NULL) {
141 		_NewScreenshot(silent, clipboard);
142 		return;
143 	}
144 
145 	fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, fGrabActiveWindow,
146 		fIncludeBorder);
147 
148 	fActiveWindow = new BCheckBox(B_TRANSLATE("Capture active window"),
149 		new BMessage(kActiveWindow));
150 	if (fGrabActiveWindow)
151 		fActiveWindow->SetValue(B_CONTROL_ON);
152 
153 	fWindowBorder = new BCheckBox(B_TRANSLATE("Include window border"),
154 		new BMessage(kIncludeBorder));
155 	if (fIncludeBorder)
156 		fWindowBorder->SetValue(B_CONTROL_ON);
157 	if (!fGrabActiveWindow)
158 		fWindowBorder->SetEnabled(false);
159 
160 	fShowCursor = new BCheckBox(B_TRANSLATE("Include mouse pointer"),
161 		new BMessage(kIncludeCursor));
162 	if (fIncludeCursor)
163 		fShowCursor->SetValue(B_CONTROL_ON);
164 
165 	BString delay;
166 	delay << fDelay / 1000000;
167 	fDelayControl = new BTextControl("", B_TRANSLATE("Delay:"), delay.String(),
168 		NULL);
169 	_DisallowChar(fDelayControl->TextView());
170 	fDelayControl->TextView()->SetAlignment(B_ALIGN_RIGHT);
171 	BStringView* seconds = new BStringView("", B_TRANSLATE("seconds"));
172 	seconds->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
173 
174 	BMenuField* menuField2 = new BMenuField(B_TRANSLATE("Save in:"),
175 		fOutputPathMenu);
176 
177 	fTranslatorMenu = new BMenu(B_TRANSLATE("Please select"));
178 	_SetupTranslatorMenu();
179 	BMenuField* menuField = new BMenuField(B_TRANSLATE("Save as:"),
180 		fTranslatorMenu);
181 
182 	BBox* divider = new BBox(B_FANCY_BORDER, NULL);
183 	divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
184 
185 	BButton* saveScreenshot  = new BButton("", B_TRANSLATE("Save"),
186 		new BMessage(kSaveScreenshot));
187 
188 	fPreview = new PreviewView();
189 
190 	BGridLayout* gridLayout = BGridLayoutBuilder(0.0, 5.0)
191 		.Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
192 		.Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
193 		.Add(menuField->CreateLabelLayoutItem(), 0, 1)
194 		.Add(menuField->CreateMenuBarLayoutItem(), 1, 1)
195 		.Add(new BButton("", B_TRANSLATE("Settings"B_UTF8_ELLIPSIS),
196 			new BMessage(kSettings)), 2, 1)
197 		.Add(menuField2->CreateLabelLayoutItem(), 0, 2)
198 		.Add(menuField2->CreateMenuBarLayoutItem(), 1, 2);
199 	gridLayout->SetMinColumnWidth(1,
200 		menuField->StringWidth("SomethingLongHere"));
201 
202 	SetLayout(new BGroupLayout(B_HORIZONTAL, 0));
203 
204 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
205 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10.0)
206 			.Add(fPreview)
207 			.AddGroup(B_VERTICAL, 0)
208 				.Add(fActiveWindow)
209 				.Add(fWindowBorder)
210 				.Add(fShowCursor)
211 				.AddGroup(B_HORIZONTAL, 5.0)
212 					.Add(fDelayControl->CreateLabelLayoutItem())
213 					.Add(fDelayControl->CreateTextViewLayoutItem())
214 					.Add(seconds)
215 					.End()
216 				.AddStrut(10.0)
217 				.Add(gridLayout)
218 				.AddGlue()
219 				.End())
220 		.AddStrut(10)
221 		.Add(divider)
222 		.AddStrut(10)
223 		.AddGroup(B_HORIZONTAL, 10.0)
224 			.Add(new BButton("", B_TRANSLATE("Copy to clipboard"),
225 				new BMessage(B_COPY)))
226 			.Add(new BButton("", B_TRANSLATE("New screenshot"),
227 				new BMessage(kNewScreenshot)))
228 			.AddGlue()
229 			.Add(saveScreenshot)
230 			.End()
231 		.SetInsets(10.0, 10.0, 10.0, 10.0)
232 	);
233 
234 	saveScreenshot->MakeDefault(true);
235 
236 	_UpdatePreviewPanel();
237 	_UpdateFilenameSelection();
238 
239 	CenterOnScreen();
240 	Show();
241 }
242 
243 
244 ScreenshotWindow::~ScreenshotWindow()
245 {
246 	if (fOutputPathPanel)
247 		delete fOutputPathPanel->RefFilter();
248 
249 	delete fOutputPathPanel;
250 	delete fScreenshot;
251 }
252 
253 
254 void
255 ScreenshotWindow::MessageReceived(BMessage* message)
256 {
257 	switch (message->what) {
258 		case kActiveWindow:
259 			fGrabActiveWindow = false;
260 			if (fActiveWindow->Value() == B_CONTROL_ON)
261 				fGrabActiveWindow = true;
262 
263 			fWindowBorder->SetEnabled(fGrabActiveWindow);
264 
265 			delete fScreenshot;
266 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
267 				fGrabActiveWindow, fIncludeBorder);
268 			_UpdatePreviewPanel();
269 			break;
270 
271 		case kIncludeBorder:
272 			fIncludeBorder = (fWindowBorder->Value() == B_CONTROL_ON);
273 			delete fScreenshot;
274 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
275 				fGrabActiveWindow, fIncludeBorder);
276 			_UpdatePreviewPanel();
277 			break;
278 
279 		case kIncludeCursor:
280 			fIncludeCursor = (fShowCursor->Value() == B_CONTROL_ON);
281 			delete fScreenshot;
282 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
283 				fGrabActiveWindow, fIncludeBorder);
284 			_UpdatePreviewPanel();
285 			break;
286 
287 		case kNewScreenshot:
288 			fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
289 			_NewScreenshot();
290 			break;
291 
292 		case kImageFormat:
293 			message->FindInt32("be:type", &fImageFileType);
294 			fNameControl->SetText(_FindValidFileName(
295 				fNameControl->Text()).String());
296 			_UpdateFilenameSelection();
297 			_ShowSettings(false);
298 			break;
299 
300 		case kLocationChanged:
301 		{
302 			void* source = NULL;
303 			if (message->FindPointer("source", &source) == B_OK)
304 				fLastSelectedPath = static_cast<BMenuItem*> (source);
305 
306 			fNameControl->SetText(_FindValidFileName(
307 				fNameControl->Text()).String());
308 
309 			_UpdateFilenameSelection();
310 			break;
311 		}
312 
313 		case kChooseLocation:
314 		{
315 			if (!fOutputPathPanel) {
316 				BMessenger target(this);
317 				fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, NULL,
318 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter());
319 				fOutputPathPanel->Window()->SetTitle(
320 					B_TRANSLATE("Choose folder"));
321 				fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON,
322 					B_TRANSLATE("Select"));
323 				fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON,
324 					B_TRANSLATE("Cancel"));
325 			}
326 			fOutputPathPanel->Show();
327 			break;
328 		}
329 
330 		case B_CANCEL:
331 			fLastSelectedPath->SetMarked(true);
332 			break;
333 
334 		case kSaveScreenshot:
335 			if (_SaveScreenshot() == B_OK)
336 				be_app->PostMessage(B_QUIT_REQUESTED);
337 			break;
338 
339 		case B_COPY:
340 			fUtility.CopyToClipboard(fScreenshot);
341 			break;
342 
343 		case kSettings:
344 			_ShowSettings(true);
345 			break;
346 
347 		case kCloseTranslatorSettings:
348 			fSettingsWindow->Lock();
349 			fSettingsWindow->Quit();
350 			fSettingsWindow = NULL;
351 			break;
352 
353 		default:
354 			BWindow::MessageReceived(message);
355 			break;
356 	}
357 }
358 
359 
360 void
361 ScreenshotWindow::Quit()
362 {
363 	if (fUtility.wholeScreen != NULL)
364 		_WriteSettings();
365 	BWindow::Quit();
366 }
367 
368 
369 void
370 ScreenshotWindow::_NewScreenshot(bool silent, bool clipboard)
371 {
372 	BMessage message(B_ARGV_RECEIVED);
373 	int32 argc = 3;
374 	BString delay;
375 	delay << fDelay / 1000000;
376 	message.AddString("argv", "screenshot");
377 	message.AddString("argv", "--delay");
378 	message.AddString("argv", delay);
379 
380 	if (silent || clipboard) {
381 		if (silent) {
382 			argc++;
383 			message.AddString("argv", "--silent");
384 		}
385 		if (clipboard) {
386 			argc++;
387 			message.AddString("argv", "--clipboard");
388 		}
389 		if (fIncludeBorder) {
390 			argc++;
391 			message.AddString("argv", "--border");
392 		}
393 		if (fIncludeCursor) {
394 			argc++;
395 			message.AddString("argv", "--mouse-pointer");
396 		}
397 		if (fGrabActiveWindow) {
398 			argc++;
399 			message.AddString("argv", "--window");
400 		}
401 		if (fLastSelectedPath) {
402 			BPath path(_GetDirectory());
403 			if (path != NULL) {
404 				path.Append(fNameControl->Text());
405 				argc++;
406 				message.AddString("argv", path.Path());
407 			}
408 		}
409 	}
410 	message.AddInt32("argc", argc);
411 
412 	be_roster->Launch("application/x-vnd.haiku-screenshot-cli", &message);
413 	be_app->PostMessage(B_QUIT_REQUESTED);
414 }
415 
416 
417 void
418 ScreenshotWindow::_UpdatePreviewPanel()
419 {
420 	float height = 150.0f;
421 
422 	float width = (fScreenshot->Bounds().Width()
423 		/ fScreenshot->Bounds().Height()) * height;
424 
425 	// to prevent a preview way too wide
426 	if (width > 400.0f) {
427 		width = 400.0f;
428 		height = (fScreenshot->Bounds().Height()
429 			/ fScreenshot->Bounds().Width()) * width;
430 	}
431 
432 	fPreview->SetExplicitMinSize(BSize(width, height));
433 
434 	fPreview->ClearViewBitmap();
435 	fPreview->SetViewBitmap(fScreenshot, fScreenshot->Bounds(),
436 		fPreview->Bounds(), B_FOLLOW_ALL, B_FILTER_BITMAP_BILINEAR);
437 }
438 
439 
440 void
441 ScreenshotWindow::_DisallowChar(BTextView* textView)
442 {
443 	for (uint32 i = 0; i < '0'; ++i)
444 		textView->DisallowChar(i);
445 
446 	for (uint32 i = '9' + 1; i < 255; ++i)
447 		textView->DisallowChar(i);
448 }
449 
450 
451 void
452 ScreenshotWindow::_SetupOutputPathMenu(const BMessage& settings)
453 {
454 	fOutputPathMenu->SetLabelFromMarked(true);
455 
456 	BString lastSelectedPath;
457 	settings.FindString("lastSelectedPath", &lastSelectedPath);
458 
459 	BPath path;
460 	find_directory(B_USER_DIRECTORY, &path);
461 
462 	BString label(B_TRANSLATE("Home folder"));
463 	_AddItemToPathMenu(path.Path(), label, 0,
464 		(path.Path() == lastSelectedPath));
465 
466 	path.Append("Desktop");
467 	label.SetTo(B_TRANSLATE("Desktop"));
468 	_AddItemToPathMenu(path.Path(), label, 0, (
469 		path.Path() == lastSelectedPath));
470 
471 	find_directory(B_BEOS_ETC_DIRECTORY, &path);
472 	path.Append("artwork");
473 
474 	label.SetTo(B_TRANSLATE("Artwork folder"));
475 	_AddItemToPathMenu(path.Path(), label, 2,
476 		(path.Path() == lastSelectedPath));
477 
478 	int32 i = 0;
479 	BString userPath;
480 	while (settings.FindString("path", ++i, &userPath) == B_OK) {
481 		_AddItemToPathMenu(userPath.String(), userPath, 3,
482 			(userPath == lastSelectedPath));
483 	}
484 
485 	if (!fLastSelectedPath) {
486 		if (settings.IsEmpty() || lastSelectedPath.Length() == 0) {
487 			fOutputPathMenu->ItemAt(1)->SetMarked(true);
488 			fLastSelectedPath = fOutputPathMenu->ItemAt(1);
489 		} else
490 			_AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3,
491 				true);
492 	}
493 
494 	fOutputPathMenu->AddItem(new BSeparatorItem());
495 	fOutputPathMenu->AddItem(new BMenuItem(B_TRANSLATE("Choose folder..."),
496 		new BMessage(kChooseLocation)));
497 }
498 
499 
500 void
501 ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label,
502 	int32 index, bool markItem)
503 {
504 	BMessage* message = new BMessage(kLocationChanged);
505 	message->AddString("path", path);
506 
507 	fOutputPathMenu->TruncateString(&label, B_TRUNCATE_MIDDLE,
508 		fOutputPathMenu->StringWidth("SomethingLongHere"));
509 
510 	fOutputPathMenu->AddItem(new BMenuItem(label.String(), message), index);
511 
512 	if (markItem) {
513 		fOutputPathMenu->ItemAt(index)->SetMarked(true);
514 		fLastSelectedPath = fOutputPathMenu->ItemAt(index);
515 	}
516 }
517 
518 
519 void
520 ScreenshotWindow::_UpdateFilenameSelection()
521 {
522 	fNameControl->MakeFocus(true);
523 	fNameControl->TextView()->Select(0,	fNameControl->TextView()->TextLength()
524 		- fExtension.Length());
525 
526 	fNameControl->TextView()->ScrollToSelection();
527 }
528 
529 
530 void
531 ScreenshotWindow::_SetupTranslatorMenu()
532 {
533 	BMessage message(kImageFormat);
534 	fTranslatorMenu = new BMenu("Please select");
535 	BTranslationUtils::AddTranslationItems(fTranslatorMenu, B_TRANSLATOR_BITMAP,
536 		&message, NULL, NULL, NULL);
537 
538 	fTranslatorMenu->SetLabelFromMarked(true);
539 
540 	if (fTranslatorMenu->ItemAt(0))
541 		fTranslatorMenu->ItemAt(0)->SetMarked(true);
542 
543 	int32 imageFileType;
544 	for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) {
545 		BMenuItem* item = fTranslatorMenu->ItemAt(i);
546 		if (item && item->Message()) {
547 			item->Message()->FindInt32("be:type", &imageFileType);
548 			if (fImageFileType == imageFileType) {
549 				item->SetMarked(true);
550 				MessageReceived(item->Message());
551 				break;
552 			}
553 		}
554 	}
555 }
556 
557 
558 status_t
559 ScreenshotWindow::_SaveScreenshot()
560 {
561 	if (!fScreenshot || !fLastSelectedPath)
562 		return B_ERROR;
563 
564 	BPath path(_GetDirectory());
565 
566 	if (path == NULL)
567 		return B_ERROR;
568 
569 	path.Append(fNameControl->Text());
570 
571 	BEntry entry;
572 	entry.SetTo(path.Path());
573 
574 	if (entry.Exists()) {
575 		BAlert* overwriteAlert = new BAlert(
576 			B_TRANSLATE("overwrite"),
577 			B_TRANSLATE("This file already exists.\n Are you sure would "
578 				"you like to overwrite it?"),
579 			B_TRANSLATE("Cancel"),
580 			B_TRANSLATE("Overwrite"),
581 			NULL, B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_WARNING_ALERT);
582 
583 			overwriteAlert->SetShortcut(0, B_ESCAPE);
584 
585 			if (overwriteAlert->Go() == 0)
586 				return B_CANCELED;
587 	}
588 
589 	return fUtility.Save(&fScreenshot, path.Path(), fImageFileType);
590 }
591 
592 
593 void
594 ScreenshotWindow::_ShowSettings(bool activate)
595 {
596 	if (!fSettingsWindow && !activate)
597 		return;
598 
599 	// Find a translator
600 	translator_id translator = 0;
601 	BTranslatorRoster *roster = BTranslatorRoster::Default();
602 	translator_id* translators = NULL;
603 	int32 numTranslators = 0;
604 	if (roster->GetAllTranslators(&translators, &numTranslators) != B_OK)
605 		return;
606 	bool foundTranslator = false;
607 	for (int32 x = 0; x < numTranslators; x++) {
608 		const translation_format* formats = NULL;
609 		int32 numFormats;
610 		if (roster->GetOutputFormats(translators[x], &formats,
611 			&numFormats) == B_OK) {
612 			for (int32 i = 0; i < numFormats; ++i) {
613 				if (formats[i].type == static_cast<uint32>(fImageFileType)) {
614 					translator = translators[x];
615 					foundTranslator = true;
616 					break;
617 				}
618 			}
619 		}
620 		if (foundTranslator)
621 			break;
622 	}
623 	delete [] translators;
624 	if (!foundTranslator)
625 		return;
626 
627 	// Create a window with a configuration view
628 	BView *view;
629 	BRect rect(0, 0, 239, 239);
630 
631 	status_t err = roster->MakeConfigurationView(translator, NULL, &view,
632 		&rect);
633 	if (err < B_OK || view == NULL) {
634 		BAlert *alert = new BAlert(NULL, strerror(err), "OK");
635 		alert->Go();
636 	} else {
637 		if (fSettingsWindow) {
638 			fSettingsWindow->RemoveChild(fSettingsWindow->ChildAt(0));
639 			float width, height;
640 			view->GetPreferredSize(&width, &height);
641 			fSettingsWindow->ResizeTo(width, height);
642 			fSettingsWindow->AddChild(view);
643 			if (activate)
644 				fSettingsWindow->Activate();
645 		} else {
646 			fSettingsWindow = new BWindow(rect,
647 				B_TRANSLATE("Translator Settings"),
648 				B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
649 				B_NOT_ZOOMABLE | B_NOT_RESIZABLE);
650 			fSettingsWindow->AddFilter(new QuitMessageFilter(this));
651 			fSettingsWindow->AddChild(view);
652 			fSettingsWindow->CenterOnScreen();
653 			fSettingsWindow->Show();
654 		}
655 	}
656 }
657 
658 
659 BString
660 ScreenshotWindow::_FindValidFileName(const char* name)
661 {
662 	BString baseName(name);
663 
664 	if (fExtension.Compare(""))
665 		baseName.RemoveLast(fExtension);
666 
667 	if (!fLastSelectedPath)
668 		return baseName;
669 
670 	BPath orgPath(_GetDirectory());
671 	if (orgPath == NULL)
672 		return baseName;
673 
674 	fExtension = BString(fUtility.GetFileNameExtension(fImageFileType));
675 
676 	BPath outputPath = orgPath;
677 	BString fileName;
678 	fileName << baseName << fExtension;
679 	outputPath.Append(fileName);
680 
681 	if (!BEntry(outputPath.Path()).Exists())
682 		return fileName;
683 
684 	if (baseName.FindFirst(B_TRANSLATE_NOCOLLECT(
685 			fUtility.sDefaultFileNameBase)) == 0)
686 		baseName.SetTo(fUtility.sDefaultFileNameBase);
687 
688 	BEntry entry;
689 	int32 index = 1;
690 
691 	do {
692 		fileName = "";
693 		fileName << baseName << index++ << fExtension;
694 		outputPath.SetTo(orgPath.Path());
695 		outputPath.Append(fileName);
696 		entry.SetTo(outputPath.Path());
697 	} while (entry.Exists());
698 
699 	return fileName;
700 }
701 
702 
703 BPath
704 ScreenshotWindow::_GetDirectory()
705 {
706 	BPath path;
707 
708 	BMessage* message = fLastSelectedPath->Message();
709 	const char* stringPath;
710 	if (message && message->FindString("path", &stringPath) == B_OK)
711 		path.SetTo(stringPath);
712 
713 	return path;
714 }
715 
716 
717 void
718 ScreenshotWindow::_ReadSettings()
719 {
720 	BMessage settings;
721 
722 	BPath settingsPath;
723 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
724 		return;
725 
726 	settingsPath.Append("Screenshot_settings");
727 
728 	BFile file(settingsPath.Path(), B_READ_ONLY);
729 	if (file.InitCheck() == B_OK)
730 		settings.Unflatten(&file);
731 
732 	if (settings.FindInt32("type", &fImageFileType) != B_OK)
733 		fImageFileType = B_PNG_FORMAT;
734 	settings.FindBool("includeBorder", &fIncludeBorder);
735 	settings.FindBool("includeCursor", &fIncludeCursor);
736 	settings.FindBool("grabActiveWindow", &fGrabActiveWindow);
737 	settings.FindInt64("delay", &fDelay);
738 	settings.FindString("outputFilename", &fOutputFilename);
739 
740 	_SetupOutputPathMenu(settings);
741 }
742 
743 
744 void
745 ScreenshotWindow::_WriteSettings()
746 {
747 	if (fDelayControl)
748 		fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
749 
750 	BMessage settings;
751 
752 	settings.AddInt32("type", fImageFileType);
753 	settings.AddBool("includeBorder", fIncludeBorder);
754 	settings.AddBool("includeCursor", fIncludeCursor);
755 	settings.AddBool("grabActiveWindow", fGrabActiveWindow);
756 	settings.AddInt64("delay", fDelay);
757 	settings.AddString("outputFilename", fOutputFilename);
758 
759 	BString path;
760 	int32 count = fOutputPathMenu->CountItems();
761 	if (count > 5) {
762 		for (int32 i = count - 3; i > count - 8 && i > 2; --i) {
763 			BMenuItem* item = fOutputPathMenu->ItemAt(i);
764 			if (item) {
765 				BMessage* msg = item->Message();
766 				if (msg && msg->FindString("path", &path) == B_OK)
767 					settings.AddString("path", path.String());
768 			}
769 		}
770 	}
771 
772 	if (fLastSelectedPath) {
773 		BMessage* msg = fLastSelectedPath->Message();
774 		if (msg && msg->FindString("path", &path) == B_OK)
775 			settings.AddString("lastSelectedPath", path.String());
776 	}
777 
778 	BPath settingsPath;
779 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
780 		return;
781 	settingsPath.Append("Screenshot_settings");
782 
783 	BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE
784 		| B_WRITE_ONLY);
785 	if (file.InitCheck() == B_OK) {
786 		ssize_t size;
787 		settings.Flatten(&file, &size);
788 	}
789 }
790