xref: /haiku/src/apps/screenshot/ScreenshotWindow.cpp (revision e4eecdb5c047873433d6b1b76458210285c84b3b)
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 	// Set the height of fPreview to what the layout suggests
443 	fPreview->SetExplicitMinSize(BSize());
444 	fPreview->SetExplicitMaxSize(BSize());
445 	Layout(false);
446 
447 	float height = fPreview->Bounds().Height();
448 	float width = (fScreenshot->Bounds().Width()
449 		/ fScreenshot->Bounds().Height()) * height;
450 
451 	// to prevent a preview way too wide
452 	if (width > 400.0f) {
453 		width = 400.0f;
454 		height = (fScreenshot->Bounds().Height()
455 			/ fScreenshot->Bounds().Width()) * width;
456 	}
457 
458 	fPreview->SetExplicitMinSize(BSize(width, height));
459 	fPreview->SetExplicitMaxSize(BSize(width, height));
460 
461 	fPreview->ClearViewBitmap();
462 	fPreview->SetViewBitmap(fScreenshot, fScreenshot->Bounds(),
463 		fPreview->Bounds(), B_FOLLOW_ALL, B_FILTER_BITMAP_BILINEAR);
464 }
465 
466 
467 void
468 ScreenshotWindow::_DisallowChar(BTextView* textView)
469 {
470 	for (uint32 i = 0; i < '0'; ++i)
471 		textView->DisallowChar(i);
472 
473 	for (uint32 i = '9' + 1; i < 255; ++i)
474 		textView->DisallowChar(i);
475 }
476 
477 
478 void
479 ScreenshotWindow::_SetupOutputPathMenu(const BMessage& settings)
480 {
481 	fOutputPathMenu->SetLabelFromMarked(true);
482 
483 	BString lastSelectedPath;
484 	settings.FindString("lastSelectedPath", &lastSelectedPath);
485 
486 	BPath path;
487 	find_directory(B_USER_DIRECTORY, &path);
488 
489 	BString label(B_TRANSLATE("Home folder"));
490 	_AddItemToPathMenu(path.Path(), label, 0,
491 		path.Path() == lastSelectedPath, 'H');
492 
493 	path.Append("Desktop");
494 	label.SetTo(B_TRANSLATE("Desktop"));
495 	_AddItemToPathMenu(path.Path(), label, 0,
496 		path.Path() == lastSelectedPath, 'D');
497 
498 	find_directory(B_USER_NONPACKAGED_DATA_DIRECTORY, &path);
499 	path.Append("artwork");
500 
501 	label.SetTo(B_TRANSLATE("Artwork folder"));
502 	_AddItemToPathMenu(path.Path(), label, 2,
503 		path.Path() == lastSelectedPath, 'A');
504 
505 	int32 i = 0;
506 	BString userPath;
507 	while (settings.FindString("path", ++i, &userPath) == B_OK) {
508 		_AddItemToPathMenu(userPath.String(), userPath, 3,
509 			userPath == lastSelectedPath);
510 	}
511 
512 	if (!fLastSelectedPath) {
513 		if (settings.IsEmpty() || lastSelectedPath.Length() == 0) {
514 			fOutputPathMenu->ItemAt(1)->SetMarked(true);
515 			fLastSelectedPath = fOutputPathMenu->ItemAt(1);
516 		} else {
517 			_AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3,
518 				true);
519 		}
520 	}
521 
522 	fOutputPathMenu->AddItem(new BSeparatorItem());
523 	fOutputPathMenu->AddItem(new BMenuItem(
524 		B_TRANSLATE("Choose folder" B_UTF8_ELLIPSIS),
525 		new BMessage(kChooseLocation), 'F'));
526 }
527 
528 
529 void
530 ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label,
531 	int32 index, bool markItem, uint32 shortcutKey)
532 {
533 	// Make sure that item won't be a duplicate of an existing one
534 	for (int32 i = fOutputPathMenu->CountItems() - 1; i >= 0; --i) {
535 		BMenuItem* menuItem = fOutputPathMenu->ItemAt(i);
536 		BMessage* message = menuItem->Message();
537 		const char* pathFromItem;
538 		if (message != NULL && message->what == kLocationChanged
539 			&& message->FindString("path", &pathFromItem) == B_OK
540 			&& !strcmp(path, pathFromItem)) {
541 
542 			if (markItem) {
543 				fOutputPathMenu->ItemAt(i)->SetMarked(true);
544 				fLastSelectedPath = fOutputPathMenu->ItemAt(i);
545 			}
546 			return;
547 		}
548 	}
549 
550 	BMessage* message = new BMessage(kLocationChanged);
551 	message->AddString("path", path);
552 
553 	fOutputPathMenu->TruncateString(&label, B_TRUNCATE_MIDDLE,
554 		fOutputPathMenu->StringWidth("SomethingLongHere"));
555 
556 	fOutputPathMenu->AddItem(new BMenuItem(label.String(), message,
557 		shortcutKey), index);
558 
559 	if (markItem) {
560 		fOutputPathMenu->ItemAt(index)->SetMarked(true);
561 		fLastSelectedPath = fOutputPathMenu->ItemAt(index);
562 	}
563 }
564 
565 
566 void
567 ScreenshotWindow::_UpdateFilenameSelection()
568 {
569 	fNameControl->MakeFocus(true);
570 	fNameControl->TextView()->Select(0,	fNameControl->TextView()->TextLength()
571 		- fExtension.Length());
572 
573 	fNameControl->TextView()->ScrollToSelection();
574 }
575 
576 
577 void
578 ScreenshotWindow::_SetupTranslatorMenu()
579 {
580 	BMessage message(kImageFormat);
581 	fTranslatorMenu = new BMenu("Please select");
582 	BTranslationUtils::AddTranslationItems(fTranslatorMenu, B_TRANSLATOR_BITMAP,
583 		&message, NULL, NULL, NULL);
584 
585 	fTranslatorMenu->SetLabelFromMarked(true);
586 
587 	if (fTranslatorMenu->ItemAt(0))
588 		fTranslatorMenu->ItemAt(0)->SetMarked(true);
589 
590 	int32 imageFileType;
591 	for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) {
592 		BMenuItem* item = fTranslatorMenu->ItemAt(i);
593 		if (item != NULL && item->Message()) {
594 			item->Message()->FindInt32("be:type", &imageFileType);
595 			if (fImageFileType == imageFileType) {
596 				item->SetMarked(true);
597 				MessageReceived(item->Message());
598 				break;
599 			}
600 		}
601 	}
602 }
603 
604 
605 status_t
606 ScreenshotWindow::_SaveScreenshot()
607 {
608 	if (!fScreenshot || !fLastSelectedPath)
609 		return B_ERROR;
610 
611 	BPath path(_GetDirectory());
612 
613 	if (path == NULL)
614 		return B_ERROR;
615 
616 	path.Append(fNameControl->Text());
617 
618 	BEntry entry;
619 	entry.SetTo(path.Path());
620 
621 	if (entry.Exists()) {
622 		BAlert* overwriteAlert = new BAlert(
623 			B_TRANSLATE("overwrite"),
624 			B_TRANSLATE("This file already exists.\n Are you sure you would "
625 				"like to overwrite it?"),
626 			B_TRANSLATE("Cancel"),
627 			B_TRANSLATE("Overwrite"),
628 			NULL, B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_WARNING_ALERT);
629 
630 		overwriteAlert->SetShortcut(0, B_ESCAPE);
631 
632 		if (overwriteAlert->Go() == 0)
633 			return B_CANCELED;
634 	}
635 
636 	return fUtility.Save(fScreenshot, path.Path(), fImageFileType);
637 }
638 
639 
640 void
641 ScreenshotWindow::_ShowSettings(bool activate)
642 {
643 	if (!fSettingsWindow && !activate)
644 		return;
645 
646 	// Find a translator
647 	translator_id translator;
648 	if (fUtility.FindTranslator(fImageFileType, translator) != B_OK)
649 		return;
650 
651 	// Create a window with a configuration view
652 	BView* view;
653 	BRect rect(0, 0, 239, 239);
654 
655 	status_t status = BTranslatorRoster::Default()->MakeConfigurationView(
656 		translator, NULL, &view, &rect);
657 	if (status != B_OK || view == NULL) {
658 		// TODO: proper translation, better error dialog
659 		BAlert* alert = new BAlert(NULL, strerror(status), "OK");
660 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
661 		alert->Go();
662 	} else if (fSettingsWindow != NULL) {
663 		fSettingsWindow->RemoveChild(fSettingsWindow->ChildAt(0));
664 		float width, height;
665 		view->GetPreferredSize(&width, &height);
666 		fSettingsWindow->ResizeTo(width, height);
667 		fSettingsWindow->AddChild(view);
668 		if (activate)
669 			fSettingsWindow->Activate();
670 	} else {
671 		fSettingsWindow = new BWindow(rect,
672 			B_TRANSLATE("Translator Settings"),
673 			B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
674 			B_NOT_ZOOMABLE | B_NOT_RESIZABLE);
675 		fSettingsWindow->AddFilter(new QuitMessageFilter(this));
676 		fSettingsWindow->AddChild(view);
677 		fSettingsWindow->CenterOnScreen();
678 		fSettingsWindow->Show();
679 	}
680 }
681 
682 
683 BString
684 ScreenshotWindow::_FindValidFileName(const char* name)
685 {
686 	BString baseName(name);
687 
688 	if (!fExtension.IsEmpty())
689 		baseName.RemoveLast(fExtension);
690 
691 	if (!fLastSelectedPath)
692 		return baseName;
693 
694 	BPath orgPath(_GetDirectory());
695 	if (orgPath == NULL)
696 		return baseName;
697 
698 	fExtension = fUtility.FileNameExtension(fImageFileType);
699 
700 	BPath outputPath = orgPath;
701 	BString fileName;
702 	fileName << baseName << fExtension;
703 	outputPath.Append(fileName);
704 
705 	if (!BEntry(outputPath.Path()).Exists())
706 		return fileName;
707 
708 	if (baseName.FindFirst(B_TRANSLATE_NOCOLLECT(
709 			fUtility.sDefaultFileNameBase)) == 0)
710 		baseName.SetTo(fUtility.sDefaultFileNameBase);
711 
712 	BEntry entry;
713 	int32 index = 1;
714 
715 	do {
716 		fileName = "";
717 		fileName << baseName << index++ << fExtension;
718 		outputPath.SetTo(orgPath.Path());
719 		outputPath.Append(fileName);
720 		entry.SetTo(outputPath.Path());
721 	} while (entry.Exists());
722 
723 	return fileName;
724 }
725 
726 
727 BPath
728 ScreenshotWindow::_GetDirectory()
729 {
730 	BPath path;
731 
732 	BMessage* message = fLastSelectedPath->Message();
733 	const char* stringPath;
734 	if (message && message->FindString("path", &stringPath) == B_OK)
735 		path.SetTo(stringPath);
736 
737 	return path;
738 }
739 
740 
741 void
742 ScreenshotWindow::_ReadSettings()
743 {
744 	BMessage settings;
745 
746 	BPath settingsPath;
747 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
748 		return;
749 
750 	settingsPath.Append("Screenshot_settings");
751 
752 	BFile file(settingsPath.Path(), B_READ_ONLY);
753 	if (file.InitCheck() == B_OK)
754 		settings.Unflatten(&file);
755 
756 	if (settings.FindInt32("type", &fImageFileType) != B_OK)
757 		fImageFileType = B_PNG_FORMAT;
758 	settings.FindBool("includeBorder", &fIncludeBorder);
759 	settings.FindBool("includeCursor", &fIncludeCursor);
760 	settings.FindBool("grabActiveWindow", &fGrabActiveWindow);
761 	settings.FindInt64("delay", &fDelay);
762 	settings.FindString("outputFilename", &fOutputFilename);
763 
764 	_SetupOutputPathMenu(settings);
765 }
766 
767 
768 void
769 ScreenshotWindow::_WriteSettings()
770 {
771 	if (fDelayControl)
772 		fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
773 
774 	BMessage settings;
775 
776 	settings.AddInt32("type", fImageFileType);
777 	settings.AddBool("includeBorder", fIncludeBorder);
778 	settings.AddBool("includeCursor", fIncludeCursor);
779 	settings.AddBool("grabActiveWindow", fGrabActiveWindow);
780 	settings.AddInt64("delay", fDelay);
781 	settings.AddString("outputFilename", fOutputFilename);
782 
783 	BString path;
784 	int32 count = fOutputPathMenu->CountItems();
785 	if (count > 5) {
786 		for (int32 i = count - 3; i > count - 8 && i > 2; --i) {
787 			BMenuItem* item = fOutputPathMenu->ItemAt(i);
788 			if (item) {
789 				BMessage* msg = item->Message();
790 				if (msg && msg->FindString("path", &path) == B_OK)
791 					settings.AddString("path", path.String());
792 			}
793 		}
794 	}
795 
796 	if (fLastSelectedPath) {
797 		BMessage* msg = fLastSelectedPath->Message();
798 		if (msg && msg->FindString("path", &path) == B_OK)
799 			settings.AddString("lastSelectedPath", path.String());
800 	}
801 
802 	BPath settingsPath;
803 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
804 		return;
805 	settingsPath.Append("Screenshot_settings");
806 
807 	BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE
808 		| B_WRITE_ONLY);
809 	if (file.InitCheck() == B_OK) {
810 		ssize_t size;
811 		settings.Flatten(&file, &size);
812 	}
813 }
814