* save the last output location

* save the last used output format
* save the last 5 choosen locations
* don't do any window init in silent mode



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27785 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Karsten Heimrich
2008-09-29 22:05:26 +00:00
parent d99477440f
commit ae1cc08385
2 changed files with 190 additions and 63 deletions
+177 -59
View File
@@ -26,6 +26,7 @@
#include <Menu.h> #include <Menu.h>
#include <MenuField.h> #include <MenuField.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <Message.h>
#include <NodeInfo.h> #include <NodeInfo.h>
#include <Path.h> #include <Path.h>
#include <RadioButton.h> #include <RadioButton.h>
@@ -82,6 +83,7 @@ ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
: BWindow(BRect(0, 0, 200.0, 100.0), "Take Screenshot", B_TITLED_WINDOW, : BWindow(BRect(0, 0, 200.0, 100.0), "Take Screenshot", B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE | B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE |
B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE), B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
fDelayControl(NULL),
fScreenshot(NULL), fScreenshot(NULL),
fOutputPathPanel(NULL), fOutputPathPanel(NULL),
fLastSelectedPath(NULL), fLastSelectedPath(NULL),
@@ -89,11 +91,16 @@ ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
fIncludeBorder(includeBorder), fIncludeBorder(includeBorder),
fIncludeCursor(includeCursor), fIncludeCursor(includeCursor),
fGrabActiveWindow(grabActiveWindow), fGrabActiveWindow(grabActiveWindow),
fShowConfigWindow(showConfigWindow), fShowConfigWindow(showConfigWindow)
fSaveScreenshotSilent(saveScreenshotSilent)
{ {
_InitWindow(); if (saveScreenshotSilent) {
_CenterAndShow(); _TakeScreenshot();
_SaveScreenshotSilent();
be_app_messenger.SendMessage(B_QUIT_REQUESTED);
} else {
_InitWindow();
_CenterAndShow();
}
} }
@@ -136,6 +143,7 @@ ScreenshotWindow::MessageReceived(BMessage* message)
case kTakeScreenshot: { case kTakeScreenshot: {
Hide(); Hide();
_TakeScreenshot(); _TakeScreenshot();
_UpdatePreviewPanel();
Show(); Show();
} break; } break;
@@ -172,21 +180,18 @@ ScreenshotWindow::MessageReceived(BMessage* message)
entry_ref ref; entry_ref ref;
if (message->FindRef("refs", &ref) == B_OK) { if (message->FindRef("refs", &ref) == B_OK) {
BString path(BPath(&ref).Path()); BString path(BPath(&ref).Path());
int32 index = _PathIndexInMenu(path);
BMessage* message = new BMessage(kLocationChanged); if (index < 0) {
message->AddString("path", path.String()); _AddItemToPathMenu(path.String(),
path, fOutputPathMenu->CountItems() - 2, true);
fOutputPathMenu->TruncateString(&path, B_TRUNCATE_MIDDLE, } else {
fOutputPathMenu->StringWidth("SomethingLongHere")); fOutputPathMenu->ItemAt(index)->SetMarked(true);
fLastSelectedPath = new BMenuItem(path.String(), message); }
fLastSelectedPath->SetMarked(true);
fOutputPathMenu->AddItem(fLastSelectedPath,
fOutputPathMenu->CountItems() - 2);
} }
} break; } break;
case kFinishScreenshot: { case kFinishScreenshot: {
_WriteSettings();
_SaveScreenshot(); _SaveScreenshot();
be_app_messenger.SendMessage(B_QUIT_REQUESTED); be_app_messenger.SendMessage(B_QUIT_REQUESTED);
} break; } break;
@@ -218,6 +223,7 @@ ScreenshotWindow::_InitWindow()
if (!fShowConfigWindow) { if (!fShowConfigWindow) {
_TakeScreenshot(); _TakeScreenshot();
_UpdatePreviewPanel();
layout->SetVisibleItem(1L); layout->SetVisibleItem(1L);
} else { } else {
layout->SetVisibleItem(0L); layout->SetVisibleItem(0L);
@@ -319,10 +325,12 @@ ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout)
fNameControl = new BTextControl("", "Name:", "screenshot", NULL); fNameControl = new BTextControl("", "Name:", "screenshot", NULL);
_SetupTranslatorMenu(new BMenu("Please select")); BMessage settings(_ReadSettings());
_SetupTranslatorMenu(new BMenu("Please select"), settings);
BMenuField* menuField = new BMenuField("Save as:", fTranslatorMenu); BMenuField* menuField = new BMenuField("Save as:", fTranslatorMenu);
_SetupOutputPathMenu(new BMenu("Please select")); _SetupOutputPathMenu(new BMenu("Please select"), settings);
BMenuField* menuField2 = new BMenuField("Save in:", fOutputPathMenu); BMenuField* menuField2 = new BMenuField("Save in:", fOutputPathMenu);
fNameControl->SetText(_FindValidFileName("screenshot").String()); fNameControl->SetText(_FindValidFileName("screenshot").String());
@@ -372,7 +380,8 @@ ScreenshotWindow::_DisallowChar(BTextView* textView)
void void
ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu) ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu,
const BMessage& settings)
{ {
fTranslatorMenu = translatorMenu; fTranslatorMenu = translatorMenu;
@@ -386,12 +395,15 @@ ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu)
if (fTranslatorMenu->ItemAt(0)) if (fTranslatorMenu->ItemAt(0))
fTranslatorMenu->ItemAt(0)->SetMarked(true); fTranslatorMenu->ItemAt(0)->SetMarked(true);
if (settings.FindInt32("be:type", &fImageFileType) != B_OK)
fImageFileType = B_PNG_FORMAT;
int32 imageFileType;
for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) { for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) {
BMenuItem* item = fTranslatorMenu->ItemAt(i); BMenuItem* item = fTranslatorMenu->ItemAt(i);
if (item && item->Message()) { if (item && item->Message()) {
item->Message()->FindInt32("be:type", &fImageFileType); item->Message()->FindInt32("be:type", &imageFileType);
item->Message()->FindInt32("be:translator", &fTranslator); if (fImageFileType == imageFileType) {
if (fImageFileType == B_PNG_FORMAT) {
item->SetMarked(true); item->SetMarked(true);
break; break;
} }
@@ -401,37 +413,99 @@ ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu)
void void
ScreenshotWindow::_SetupOutputPathMenu(BMenu* outputPathMenu) ScreenshotWindow::_SetupOutputPathMenu(BMenu* outputPathMenu,
const BMessage& settings)
{ {
fOutputPathMenu = outputPathMenu; fOutputPathMenu = outputPathMenu;
fOutputPathMenu->SetLabelFromMarked(true);
BString lastSelectedPath;
settings.FindString("lastSelectedPath", &lastSelectedPath);
BPath path; BPath path;
find_directory(B_USER_DIRECTORY, &path); find_directory(B_USER_DIRECTORY, &path);
BMessage* message = new BMessage(kLocationChanged); BString label("Home directory");
message->AddString("path", path.Path()); _AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath));
fOutputPathMenu->AddItem(new BMenuItem("Home directory", message));
path.Append("Desktop"); path.Append("Desktop");
message = new BMessage(kLocationChanged); label.SetTo("Desktop");
message->AddString("path", path.Path()); _AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath));
fOutputPathMenu->AddItem(new BMenuItem("Desktop", message), 0);
find_directory(B_BEOS_ETC_DIRECTORY, &path); find_directory(B_BEOS_ETC_DIRECTORY, &path);
path.Append("artwork"); path.Append("artwork");
message = new BMessage(kLocationChanged); label.SetTo("Artwork directory");
message->AddString("path", path.Path()); _AddItemToPathMenu(path.Path(), label, 2, (path.Path() == lastSelectedPath));
fOutputPathMenu->AddItem(new BMenuItem("Artwork directory", message));
int32 i = 0;
BString userPath;
while (settings.FindString("path", i++, &userPath) == B_OK) {
_AddItemToPathMenu(userPath.String(), userPath, 3,
(userPath == lastSelectedPath));
}
if (!fLastSelectedPath) {
if (settings.IsEmpty() || lastSelectedPath.Length() == 0) {
fOutputPathMenu->ItemAt(1)->SetMarked(true);
fLastSelectedPath = fOutputPathMenu->ItemAt(1);
} else {
_AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3,
true);
}
}
fOutputPathMenu->AddItem(new BSeparatorItem()); fOutputPathMenu->AddItem(new BSeparatorItem());
fOutputPathMenu->AddItem(new BMenuItem("Choose directory...", fOutputPathMenu->AddItem(new BMenuItem("Choose directory...",
new BMessage(kChooseLocation))); new BMessage(kChooseLocation)));
}
fOutputPathMenu->SetLabelFromMarked(true);
fOutputPathMenu->ItemAt(1)->SetMarked(true); void
fLastSelectedPath = fOutputPathMenu->ItemAt(1); ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label,
int32 index, bool markItem)
{
BMessage* message = new BMessage(kLocationChanged);
message->AddString("path", path);
fOutputPathMenu->TruncateString(&label, B_TRUNCATE_MIDDLE,
fOutputPathMenu->StringWidth("SomethingLongHere"));
fOutputPathMenu->AddItem(new BMenuItem(label.String(), message), index);
if (markItem) {
fOutputPathMenu->ItemAt(index)->SetMarked(true);
fLastSelectedPath = fOutputPathMenu->ItemAt(index);
}
}
void
ScreenshotWindow::_CenterAndShow()
{
BSize size = GetLayout()->PreferredSize();
ResizeTo(size.Width(), size.Height());
BRect frame(BScreen(this).Frame());
MoveTo((frame.Width() - size.Width()) / 2.0,
(frame.Height() - size.Height()) / 2.0);
Show();
}
void
ScreenshotWindow::_UpdatePreviewPanel()
{
fPreviewBox->ClearViewBitmap();
fPreviewBox->SetViewBitmap(fScreenshot, fScreenshot->Bounds(),
fPreviewBox->Bounds(), B_FOLLOW_ALL, 0);
BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout());
if (layout)
layout->SetVisibleItem(1L);
SetTitle("Save Screenshot");
} }
@@ -452,11 +526,14 @@ ScreenshotWindow::_FindValidFileName(const char* name) const
if (!BEntry(outputPath.Path()).Exists()) if (!BEntry(outputPath.Path()).Exists())
return fileName; return fileName;
if (strncmp(name, "screenshot", strlen("screenshot")) == 0)
fileName.SetTo("screenshot");
BEntry entry; BEntry entry;
int32 index = 1; int32 index = 1;
char filename[32]; char filename[32];
do { do {
sprintf(filename, "%s%ld", name, index++); sprintf(filename, "%s%ld", fileName.String(), index++);
outputPath.SetTo(path); outputPath.SetTo(path);
outputPath.Append(filename); outputPath.Append(filename);
entry.SetTo(outputPath.Path()); entry.SetTo(outputPath.Path());
@@ -466,30 +543,81 @@ ScreenshotWindow::_FindValidFileName(const char* name) const
} }
void int32
ScreenshotWindow::_CenterAndShow() ScreenshotWindow::_PathIndexInMenu(const BString& path) const
{ {
if (fSaveScreenshotSilent) { BString userPath;
_SaveScreenshotSilent(); for (int32 i = 0; i < fOutputPathMenu->CountItems(); ++i) {
be_app_messenger.SendMessage(B_QUIT_REQUESTED); BMenuItem* item = fOutputPathMenu->ItemAt(i);
return; if (item && item->Message()
&& item->Message()->FindString("path", &userPath) == B_OK) {
if (userPath == path)
return i;
}
}
return -1;
}
BMessage
ScreenshotWindow::_ReadSettings() const
{
BPath settingsPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
settingsPath.Append("screenshot");
BMessage settings;
BFile file(settingsPath.Path(), B_READ_ONLY);
if (file.InitCheck() == B_OK)
settings.Unflatten(&file);
return settings;
}
void
ScreenshotWindow::_WriteSettings() const
{
BMessage settings;
settings.AddInt32("be:type", fImageFileType);
BString path;
int32 count = fOutputPathMenu->CountItems();
if (count > 5) {
for (int32 i = count - 3; i > count - 8 && i > 2; --i) {
BMenuItem* item = fOutputPathMenu->ItemAt(i);
if (item) {
BMessage* msg = item->Message();
if (msg && msg->FindString("path", &path) == B_OK)
settings.AddString("path", path.String());
}
}
} }
BSize size = GetLayout()->PreferredSize(); if (fLastSelectedPath) {
ResizeTo(size.Width(), size.Height()); BMessage* msg = fLastSelectedPath->Message();
if (msg && msg->FindString("path", &path) == B_OK)
settings.AddString("lastSelectedPath", path.String());
}
BRect frame(BScreen(this).Frame()); BPath settingsPath;
MoveTo((frame.Width() - size.Width()) / 2.0, find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
(frame.Height() - size.Height()) / 2.0); settingsPath.Append("screenshot");
Show(); BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (file.InitCheck() == B_OK) {
ssize_t size;
settings.Flatten(&file, &size);
}
} }
void void
ScreenshotWindow::_TakeScreenshot() ScreenshotWindow::_TakeScreenshot()
{ {
snooze((atoi(fDelayControl->Text()) * 1000000) + 50000); if (fDelayControl)
snooze((atoi(fDelayControl->Text()) * 1000000) + 50000);
BRect frame; BRect frame;
delete fScreenshot; delete fScreenshot;
@@ -499,16 +627,6 @@ ScreenshotWindow::_TakeScreenshot()
} else { } else {
BScreen(this).GetBitmap(&fScreenshot, fIncludeCursor); BScreen(this).GetBitmap(&fScreenshot, fIncludeCursor);
} }
fPreviewBox->ClearViewBitmap();
fPreviewBox->SetViewBitmap(fScreenshot, fScreenshot->Bounds(),
fPreviewBox->Bounds(), B_FOLLOW_ALL, 0);
BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout());
if (layout)
layout->SetVisibleItem(1L);
SetTitle("Save Screenshot");
} }
+13 -4
View File
@@ -35,11 +35,21 @@ private:
void _SetupFirstLayoutItem(BCardLayout* layout); void _SetupFirstLayoutItem(BCardLayout* layout);
void _SetupSecondLayoutItem(BCardLayout* layout); void _SetupSecondLayoutItem(BCardLayout* layout);
void _DisallowChar(BTextView* textView); void _DisallowChar(BTextView* textView);
void _SetupTranslatorMenu(BMenu* translatorMenu); void _SetupTranslatorMenu(BMenu* translatorMenu,
void _SetupOutputPathMenu(BMenu* outputPathMenu); const BMessage& settings);
BString _FindValidFileName(const char* name) const; void _SetupOutputPathMenu(BMenu* outputPathMenu,
const BMessage& settings);
void _AddItemToPathMenu(const char* path,
BString& label, int32 index, bool markItem);
void _CenterAndShow(); void _CenterAndShow();
void _UpdatePreviewPanel();
BString _FindValidFileName(const char* name) const;
int32 _PathIndexInMenu(const BString& path) const;
BMessage _ReadSettings() const;
void _WriteSettings() const;
void _TakeScreenshot(); void _TakeScreenshot();
status_t _GetActiveWindowFrame(BRect* frame); status_t _GetActiveWindowFrame(BRect* frame);
@@ -68,7 +78,6 @@ private:
bool fIncludeCursor; bool fIncludeCursor;
bool fGrabActiveWindow; bool fGrabActiveWindow;
bool fShowConfigWindow; bool fShowConfigWindow;
bool fSaveScreenshotSilent;
int32 fTranslator; int32 fTranslator;
int32 fImageFileType; int32 fImageFileType;