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