xref: /haiku/src/apps/screenshot/ScreenshotWindow.cpp (revision 5dab45be766adf3716d356b8c7fa5858548f6d90)
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 	Layout(false);
243 	_UpdatePreviewPanel();
244 	_UpdateFilenameSelection();
245 
246 	CenterOnScreen();
247 	Show();
248 }
249 
250 
251 ScreenshotWindow::~ScreenshotWindow()
252 {
253 	if (fOutputPathPanel)
254 		delete fOutputPathPanel->RefFilter();
255 
256 	delete fOutputPathPanel;
257 	delete fScreenshot;
258 }
259 
260 
261 void
262 ScreenshotWindow::MessageReceived(BMessage* message)
263 {
264 	switch (message->what) {
265 		case kActiveWindow:
266 			fGrabActiveWindow = false;
267 			if (fActiveWindow->Value() == B_CONTROL_ON)
268 				fGrabActiveWindow = true;
269 
270 			fWindowBorder->SetEnabled(fGrabActiveWindow);
271 
272 			delete fScreenshot;
273 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
274 				fGrabActiveWindow, fIncludeBorder);
275 			_UpdatePreviewPanel();
276 			break;
277 
278 		case kIncludeBorder:
279 			fIncludeBorder = (fWindowBorder->Value() == B_CONTROL_ON);
280 			delete fScreenshot;
281 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
282 				fGrabActiveWindow, fIncludeBorder);
283 			_UpdatePreviewPanel();
284 			break;
285 
286 		case kIncludeCursor:
287 			fIncludeCursor = (fShowCursor->Value() == B_CONTROL_ON);
288 			delete fScreenshot;
289 			fScreenshot = fUtility.MakeScreenshot(fIncludeCursor,
290 				fGrabActiveWindow, fIncludeBorder);
291 			_UpdatePreviewPanel();
292 			break;
293 
294 		case kNewScreenshot:
295 			fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
296 			_NewScreenshot();
297 			break;
298 
299 		case kImageFormat:
300 			message->FindInt32("be:type", &fImageFileType);
301 			fNameControl->SetText(_FindValidFileName(
302 				fNameControl->Text()).String());
303 			_UpdateFilenameSelection();
304 			_ShowSettings(false);
305 			break;
306 
307 		case kLocationChanged:
308 		{
309 			void* source = NULL;
310 			if (message->FindPointer("source", &source) == B_OK)
311 				fLastSelectedPath = static_cast<BMenuItem*> (source);
312 
313 			fNameControl->SetText(_FindValidFileName(
314 				fNameControl->Text()).String());
315 
316 			_UpdateFilenameSelection();
317 			break;
318 		}
319 
320 		case kChooseLocation:
321 		{
322 			if (!fOutputPathPanel) {
323 				BMessenger target(this);
324 				fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, NULL,
325 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter());
326 				fOutputPathPanel->Window()->SetTitle(
327 					B_TRANSLATE("Choose folder"));
328 				fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON,
329 					B_TRANSLATE("Select"));
330 				fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON,
331 					B_TRANSLATE("Cancel"));
332 			}
333 			fOutputPathPanel->Show();
334 			break;
335 		}
336 
337 		case B_REFS_RECEIVED:
338 		{
339 			entry_ref ref;
340 			if (message->FindRef("refs", &ref) == B_OK) {
341 				BEntry entry(&ref, true);
342 				if (entry.InitCheck() == B_OK) {
343 					BPath path;
344 					if (entry.GetPath(&path) == B_OK) {
345 						BString label(path.Path());
346 						_AddItemToPathMenu(path.Path(), label, 3, true);
347 					}
348 				}
349 			}
350 			break;
351 		}
352 
353 		case B_CANCEL:
354 			fLastSelectedPath->SetMarked(true);
355 			break;
356 
357 		case kSaveScreenshot:
358 			if (_SaveScreenshot() == B_OK)
359 				be_app->PostMessage(B_QUIT_REQUESTED);
360 			break;
361 
362 		case B_COPY:
363 			fUtility.CopyToClipboard(fScreenshot);
364 			break;
365 
366 		case kSettings:
367 			_ShowSettings(true);
368 			break;
369 
370 		case kCloseTranslatorSettings:
371 			fSettingsWindow->Lock();
372 			fSettingsWindow->Quit();
373 			fSettingsWindow = NULL;
374 			break;
375 
376 		default:
377 			BWindow::MessageReceived(message);
378 			break;
379 	}
380 }
381 
382 
383 void
384 ScreenshotWindow::Quit()
385 {
386 	if (fUtility.wholeScreen != NULL)
387 		_WriteSettings();
388 	BWindow::Quit();
389 }
390 
391 
392 void
393 ScreenshotWindow::_NewScreenshot(bool silent, bool clipboard)
394 {
395 	BMessage message(B_ARGV_RECEIVED);
396 	int32 argc = 3;
397 	BString delay;
398 	delay << fDelay / 1000000;
399 	message.AddString("argv", "screenshot");
400 	message.AddString("argv", "--delay");
401 	message.AddString("argv", delay);
402 
403 	if (silent || clipboard) {
404 		if (silent) {
405 			argc++;
406 			message.AddString("argv", "--silent");
407 		}
408 		if (clipboard) {
409 			argc++;
410 			message.AddString("argv", "--clipboard");
411 		}
412 		if (fIncludeBorder) {
413 			argc++;
414 			message.AddString("argv", "--border");
415 		}
416 		if (fIncludeCursor) {
417 			argc++;
418 			message.AddString("argv", "--mouse-pointer");
419 		}
420 		if (fGrabActiveWindow) {
421 			argc++;
422 			message.AddString("argv", "--window");
423 		}
424 		if (fLastSelectedPath) {
425 			BPath path(_GetDirectory());
426 			if (path != NULL) {
427 				path.Append(fNameControl->Text());
428 				argc++;
429 				message.AddString("argv", path.Path());
430 			}
431 		}
432 	}
433 	message.AddInt32("argc", argc);
434 
435 	be_roster->Launch("application/x-vnd.haiku-screenshot-cli", &message);
436 	be_app->PostMessage(B_QUIT_REQUESTED);
437 }
438 
439 
440 void
441 ScreenshotWindow::_UpdatePreviewPanel()
442 {
443 	float height = fPreview->Bounds().Height();
444 	float width = (fScreenshot->Bounds().Width()
445 		/ fScreenshot->Bounds().Height()) * height;
446 
447 	// to prevent a preview way too wide
448 	if (width > 400.0f) {
449 		width = 400.0f;
450 		height = (fScreenshot->Bounds().Height()
451 			/ fScreenshot->Bounds().Width()) * width;
452 	}
453 
454 	fPreview->SetExplicitMinSize(BSize(width, height));
455 	fPreview->SetExplicitMaxSize(BSize(width, height));
456 
457 	fPreview->ClearViewBitmap();
458 	fPreview->SetViewBitmap(fScreenshot, fScreenshot->Bounds(),
459 		fPreview->Bounds(), B_FOLLOW_ALL, B_FILTER_BITMAP_BILINEAR);
460 }
461 
462 
463 void
464 ScreenshotWindow::_DisallowChar(BTextView* textView)
465 {
466 	for (uint32 i = 0; i < '0'; ++i)
467 		textView->DisallowChar(i);
468 
469 	for (uint32 i = '9' + 1; i < 255; ++i)
470 		textView->DisallowChar(i);
471 }
472 
473 
474 void
475 ScreenshotWindow::_SetupOutputPathMenu(const BMessage& settings)
476 {
477 	fOutputPathMenu->SetLabelFromMarked(true);
478 
479 	BString lastSelectedPath;
480 	settings.FindString("lastSelectedPath", &lastSelectedPath);
481 
482 	BPath path;
483 	find_directory(B_USER_DIRECTORY, &path);
484 
485 	BString label(B_TRANSLATE("Home folder"));
486 	_AddItemToPathMenu(path.Path(), label, 0,
487 		path.Path() == lastSelectedPath, 'H');
488 
489 	path.Append("Desktop");
490 	label.SetTo(B_TRANSLATE("Desktop"));
491 	_AddItemToPathMenu(path.Path(), label, 0,
492 		path.Path() == lastSelectedPath, 'D');
493 
494 	find_directory(B_USER_NONPACKAGED_DATA_DIRECTORY, &path);
495 	path.Append("artwork");
496 
497 	label.SetTo(B_TRANSLATE("Artwork folder"));
498 	_AddItemToPathMenu(path.Path(), label, 2,
499 		path.Path() == lastSelectedPath, 'A');
500 
501 	int32 i = 0;
502 	BString userPath;
503 	while (settings.FindString("path", ++i, &userPath) == B_OK) {
504 		_AddItemToPathMenu(userPath.String(), userPath, 3,
505 			userPath == lastSelectedPath);
506 	}
507 
508 	if (!fLastSelectedPath) {
509 		if (settings.IsEmpty() || lastSelectedPath.Length() == 0) {
510 			fOutputPathMenu->ItemAt(1)->SetMarked(true);
511 			fLastSelectedPath = fOutputPathMenu->ItemAt(1);
512 		} else {
513 			_AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3,
514 				true);
515 		}
516 	}
517 
518 	fOutputPathMenu->AddItem(new BSeparatorItem());
519 	fOutputPathMenu->AddItem(new BMenuItem(
520 		B_TRANSLATE("Choose folder" B_UTF8_ELLIPSIS),
521 		new BMessage(kChooseLocation), 'F'));
522 }
523 
524 
525 void
526 ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label,
527 	int32 index, bool markItem, uint32 shortcutKey)
528 {
529 	// Make sure that item won't be a duplicate of an existing one
530 	for (int32 i = fOutputPathMenu->CountItems() - 1; i >= 0; --i) {
531 		BMenuItem* menuItem = fOutputPathMenu->ItemAt(i);
532 		BMessage* message = menuItem->Message();
533 		const char* pathFromItem;
534 		if (message != NULL && message->what == kLocationChanged
535 			&& message->FindString("path", &pathFromItem) == B_OK
536 			&& !strcmp(path, pathFromItem)) {
537 
538 			if (markItem) {
539 				fOutputPathMenu->ItemAt(i)->SetMarked(true);
540 				fLastSelectedPath = fOutputPathMenu->ItemAt(i);
541 			}
542 			return;
543 		}
544 	}
545 
546 	BMessage* message = new BMessage(kLocationChanged);
547 	message->AddString("path", path);
548 
549 	fOutputPathMenu->TruncateString(&label, B_TRUNCATE_MIDDLE,
550 		fOutputPathMenu->StringWidth("SomethingLongHere"));
551 
552 	fOutputPathMenu->AddItem(new BMenuItem(label.String(), message,
553 		shortcutKey), index);
554 
555 	if (markItem) {
556 		fOutputPathMenu->ItemAt(index)->SetMarked(true);
557 		fLastSelectedPath = fOutputPathMenu->ItemAt(index);
558 	}
559 }
560 
561 
562 void
563 ScreenshotWindow::_UpdateFilenameSelection()
564 {
565 	fNameControl->MakeFocus(true);
566 	fNameControl->TextView()->Select(0,	fNameControl->TextView()->TextLength()
567 		- fExtension.Length());
568 
569 	fNameControl->TextView()->ScrollToSelection();
570 }
571 
572 
573 void
574 ScreenshotWindow::_SetupTranslatorMenu()
575 {
576 	BMessage message(kImageFormat);
577 	fTranslatorMenu = new BMenu("Please select");
578 	BTranslationUtils::AddTranslationItems(fTranslatorMenu, B_TRANSLATOR_BITMAP,
579 		&message, NULL, NULL, NULL);
580 
581 	fTranslatorMenu->SetLabelFromMarked(true);
582 
583 	if (fTranslatorMenu->ItemAt(0))
584 		fTranslatorMenu->ItemAt(0)->SetMarked(true);
585 
586 	int32 imageFileType;
587 	for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) {
588 		BMenuItem* item = fTranslatorMenu->ItemAt(i);
589 		if (item != NULL && item->Message()) {
590 			item->Message()->FindInt32("be:type", &imageFileType);
591 			if (fImageFileType == imageFileType) {
592 				item->SetMarked(true);
593 				MessageReceived(item->Message());
594 				break;
595 			}
596 		}
597 	}
598 }
599 
600 
601 status_t
602 ScreenshotWindow::_SaveScreenshot()
603 {
604 	if (!fScreenshot || !fLastSelectedPath)
605 		return B_ERROR;
606 
607 	BPath path(_GetDirectory());
608 
609 	if (path == NULL)
610 		return B_ERROR;
611 
612 	path.Append(fNameControl->Text());
613 
614 	BEntry entry;
615 	entry.SetTo(path.Path());
616 
617 	if (entry.Exists()) {
618 		BAlert* overwriteAlert = new BAlert(
619 			B_TRANSLATE("overwrite"),
620 			B_TRANSLATE("This file already exists.\n Are you sure you would "
621 				"like to overwrite it?"),
622 			B_TRANSLATE("Cancel"),
623 			B_TRANSLATE("Overwrite"),
624 			NULL, B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_WARNING_ALERT);
625 
626 		overwriteAlert->SetShortcut(0, B_ESCAPE);
627 
628 		if (overwriteAlert->Go() == 0)
629 			return B_CANCELED;
630 	}
631 
632 	return fUtility.Save(fScreenshot, path.Path(), fImageFileType);
633 }
634 
635 
636 void
637 ScreenshotWindow::_ShowSettings(bool activate)
638 {
639 	if (!fSettingsWindow && !activate)
640 		return;
641 
642 	// Find a translator
643 	translator_id translator;
644 	if (fUtility.FindTranslator(fImageFileType, translator) != B_OK)
645 		return;
646 
647 	// Create a window with a configuration view
648 	BView* view;
649 	BRect rect(0, 0, 239, 239);
650 
651 	status_t status = BTranslatorRoster::Default()->MakeConfigurationView(
652 		translator, NULL, &view, &rect);
653 	if (status != B_OK || view == NULL) {
654 		// TODO: proper translation, better error dialog
655 		BAlert* alert = new BAlert(NULL, strerror(status), "OK");
656 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
657 		alert->Go();
658 	} else if (fSettingsWindow != NULL) {
659 		fSettingsWindow->RemoveChild(fSettingsWindow->ChildAt(0));
660 		float width, height;
661 		view->GetPreferredSize(&width, &height);
662 		fSettingsWindow->ResizeTo(width, height);
663 		fSettingsWindow->AddChild(view);
664 		if (activate)
665 			fSettingsWindow->Activate();
666 	} else {
667 		fSettingsWindow = new BWindow(rect,
668 			B_TRANSLATE("Translator Settings"),
669 			B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
670 			B_NOT_ZOOMABLE | B_NOT_RESIZABLE);
671 		fSettingsWindow->AddFilter(new QuitMessageFilter(this));
672 		fSettingsWindow->AddChild(view);
673 		fSettingsWindow->CenterOnScreen();
674 		fSettingsWindow->Show();
675 	}
676 }
677 
678 
679 BString
680 ScreenshotWindow::_FindValidFileName(const char* name)
681 {
682 	BString baseName(name);
683 
684 	if (!fExtension.IsEmpty())
685 		baseName.RemoveLast(fExtension);
686 
687 	if (!fLastSelectedPath)
688 		return baseName;
689 
690 	BPath orgPath(_GetDirectory());
691 	if (orgPath == NULL)
692 		return baseName;
693 
694 	fExtension = fUtility.FileNameExtension(fImageFileType);
695 
696 	BPath outputPath = orgPath;
697 	BString fileName;
698 	fileName << baseName << fExtension;
699 	outputPath.Append(fileName);
700 
701 	if (!BEntry(outputPath.Path()).Exists())
702 		return fileName;
703 
704 	if (baseName.FindFirst(B_TRANSLATE_NOCOLLECT(
705 			fUtility.sDefaultFileNameBase)) == 0)
706 		baseName.SetTo(fUtility.sDefaultFileNameBase);
707 
708 	BEntry entry;
709 	int32 index = 1;
710 
711 	do {
712 		fileName = "";
713 		fileName << baseName << index++ << fExtension;
714 		outputPath.SetTo(orgPath.Path());
715 		outputPath.Append(fileName);
716 		entry.SetTo(outputPath.Path());
717 	} while (entry.Exists());
718 
719 	return fileName;
720 }
721 
722 
723 BPath
724 ScreenshotWindow::_GetDirectory()
725 {
726 	BPath path;
727 
728 	BMessage* message = fLastSelectedPath->Message();
729 	const char* stringPath;
730 	if (message && message->FindString("path", &stringPath) == B_OK)
731 		path.SetTo(stringPath);
732 
733 	return path;
734 }
735 
736 
737 void
738 ScreenshotWindow::_ReadSettings()
739 {
740 	BMessage settings;
741 
742 	BPath settingsPath;
743 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
744 		return;
745 
746 	settingsPath.Append("Screenshot_settings");
747 
748 	BFile file(settingsPath.Path(), B_READ_ONLY);
749 	if (file.InitCheck() == B_OK)
750 		settings.Unflatten(&file);
751 
752 	if (settings.FindInt32("type", &fImageFileType) != B_OK)
753 		fImageFileType = B_PNG_FORMAT;
754 	settings.FindBool("includeBorder", &fIncludeBorder);
755 	settings.FindBool("includeCursor", &fIncludeCursor);
756 	settings.FindBool("grabActiveWindow", &fGrabActiveWindow);
757 	settings.FindInt64("delay", &fDelay);
758 	settings.FindString("outputFilename", &fOutputFilename);
759 
760 	_SetupOutputPathMenu(settings);
761 }
762 
763 
764 void
765 ScreenshotWindow::_WriteSettings()
766 {
767 	if (fDelayControl)
768 		fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
769 
770 	BMessage settings;
771 
772 	settings.AddInt32("type", fImageFileType);
773 	settings.AddBool("includeBorder", fIncludeBorder);
774 	settings.AddBool("includeCursor", fIncludeCursor);
775 	settings.AddBool("grabActiveWindow", fGrabActiveWindow);
776 	settings.AddInt64("delay", fDelay);
777 	settings.AddString("outputFilename", fOutputFilename);
778 
779 	BString path;
780 	int32 count = fOutputPathMenu->CountItems();
781 	if (count > 5) {
782 		for (int32 i = count - 3; i > count - 8 && i > 2; --i) {
783 			BMenuItem* item = fOutputPathMenu->ItemAt(i);
784 			if (item) {
785 				BMessage* msg = item->Message();
786 				if (msg && msg->FindString("path", &path) == B_OK)
787 					settings.AddString("path", path.String());
788 			}
789 		}
790 	}
791 
792 	if (fLastSelectedPath) {
793 		BMessage* msg = fLastSelectedPath->Message();
794 		if (msg && msg->FindString("path", &path) == B_OK)
795 			settings.AddString("lastSelectedPath", path.String());
796 	}
797 
798 	BPath settingsPath;
799 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
800 		return;
801 	settingsPath.Append("Screenshot_settings");
802 
803 	BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE
804 		| B_WRITE_ONLY);
805 	if (file.InitCheck() == B_OK) {
806 		ssize_t size;
807 		settings.Flatten(&file, &size);
808 	}
809 }
810