Patch by Wim van der Meer fixing ticket #3814 (saving settings of the screenshot

application). Thanks a bunch!


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36461 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2010-04-25 09:57:42 +00:00
parent 3a72ddf8ae
commit 08c7fa9db4
2 changed files with 116 additions and 62 deletions
+103 -51
View File
@@ -6,6 +6,7 @@
* Karsten Heimrich * Karsten Heimrich
* Fredrik Modéen * Fredrik Modéen
* Christophe Huriaux * Christophe Huriaux
* Wim van der Meer, [email protected]
*/ */
@@ -96,13 +97,49 @@ public:
#define TR_CONTEXT "ScreenshotWindow" #define TR_CONTEXT "ScreenshotWindow"
ScreenshotWindow::ScreenshotWindow()
:
BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE
| B_AVOID_FRONT | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
fDelayControl(NULL),
fScreenshot(NULL),
fOutputPathPanel(NULL),
fLastSelectedPath(NULL),
fDelay(0),
fTabHeight(0),
fIncludeBorder(false),
fIncludeMouse(false),
fGrabActiveWindow(false),
fShowConfigWindow(false),
fSaveScreenshotSilent(false),
fOutputFilename(NULL),
fExtension(""),
fImageFileType(B_PNG_FORMAT)
{
BMessage settings = _ReadSettings();
if (settings.FindInt32("type", &fImageFileType) != B_OK)
fImageFileType = B_PNG_FORMAT;
settings.FindBool("includeBorder", &fIncludeBorder);
settings.FindBool("includeMouse", &fIncludeMouse);
settings.FindBool("grabActiveWindow", &fGrabActiveWindow);
settings.FindInt64("delay", &fDelay);
settings.FindString("outputFilename", &fOutputFilename);
_InitWindow(settings);
CenterOnScreen();
Show();
}
ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder, ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
bool includeMouse, bool grabActiveWindow, bool showConfigWindow, bool includeMouse, bool grabActiveWindow, bool showConfigWindow,
bool saveScreenshotSilent, int32 imageFileType, const char* outputFilename) bool saveScreenshotSilent, int32 imageFileType, const char* outputFilename)
: :
BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW, BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake 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), fDelayControl(NULL),
fScreenshot(NULL), fScreenshot(NULL),
fOutputPathPanel(NULL), fOutputPathPanel(NULL),
@@ -119,11 +156,12 @@ ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
fImageFileType(imageFileType) fImageFileType(imageFileType)
{ {
if (fSaveScreenshotSilent) { if (fSaveScreenshotSilent) {
_TakeScreenshot(); _TakeScreenshot(fDelay);
_SaveScreenshot(); _SaveScreenshot();
be_app_messenger.SendMessage(B_QUIT_REQUESTED); be_app_messenger.SendMessage(B_QUIT_REQUESTED);
} else { } else {
_InitWindow(); BMessage settings = _ReadSettings();
_InitWindow(settings);
CenterOnScreen(); CenterOnScreen();
Show(); Show();
} }
@@ -157,7 +195,6 @@ ScreenshotWindow::MessageReceived(BMessage* message)
break; break;
case kShowMouse: case kShowMouse:
printf("kShowMouse\n");
fIncludeMouse = (fShowMouse->Value() == B_CONTROL_ON); fIncludeMouse = (fShowMouse->Value() == B_CONTROL_ON);
break; break;
@@ -174,8 +211,9 @@ ScreenshotWindow::MessageReceived(BMessage* message)
} }
case kTakeScreenshot: case kTakeScreenshot:
fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000;
Hide(); Hide();
_TakeScreenshot(); _TakeScreenshot(fDelay);
_UpdatePreviewPanel(); _UpdatePreviewPanel();
Show(); Show();
_UpdateFilenameSelection(); _UpdateFilenameSelection();
@@ -183,7 +221,8 @@ ScreenshotWindow::MessageReceived(BMessage* message)
case kImageOutputFormat: case kImageOutputFormat:
message->FindInt32("be:type", &fImageFileType); message->FindInt32("be:type", &fImageFileType);
fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String()); fNameControl->SetText(_FindValidFileName(
fNameControl->Text()).String());
_UpdateFilenameSelection(); _UpdateFilenameSelection();
break; break;
@@ -193,7 +232,8 @@ ScreenshotWindow::MessageReceived(BMessage* message)
if (message->FindPointer("source", &source) == B_OK) if (message->FindPointer("source", &source) == B_OK)
fLastSelectedPath = static_cast<BMenuItem*> (source); fLastSelectedPath = static_cast<BMenuItem*> (source);
fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String()); fNameControl->SetText(_FindValidFileName(
fNameControl->Text()).String());
_UpdateFilenameSelection(); _UpdateFilenameSelection();
break; break;
} }
@@ -202,10 +242,11 @@ ScreenshotWindow::MessageReceived(BMessage* message)
{ {
if (!fOutputPathPanel) { if (!fOutputPathPanel) {
BMessenger target(this); BMessenger target(this);
fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, NULL,
NULL, B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter()); B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter());
fOutputPathPanel->Window()->SetTitle(TR("Choose folder")); fOutputPathPanel->Window()->SetTitle(TR("Choose folder"));
fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON, TR("Select")); fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON,
TR("Select"));
fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON, TR("Cancel")); fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON, TR("Cancel"));
} }
fOutputPathPanel->Show(); fOutputPathPanel->Show();
@@ -238,6 +279,7 @@ ScreenshotWindow::MessageReceived(BMessage* message)
// fall through // fall through
case B_QUIT_REQUESTED: case B_QUIT_REQUESTED:
_WriteSettings();
be_app_messenger.SendMessage(B_QUIT_REQUESTED); be_app_messenger.SendMessage(B_QUIT_REQUESTED);
break; break;
@@ -281,16 +323,16 @@ ScreenshotWindow::_GetDirectory()
void void
ScreenshotWindow::_InitWindow() ScreenshotWindow::_InitWindow(const BMessage& settings)
{ {
BCardLayout* layout = new BCardLayout(); BCardLayout* layout = new BCardLayout();
SetLayout(layout); SetLayout(layout);
_SetupFirstLayoutItem(layout); _SetupFirstLayoutItem(layout);
_SetupSecondLayoutItem(layout); _SetupSecondLayoutItem(layout, settings);
if (!fShowConfigWindow) { if (!fShowConfigWindow) {
_TakeScreenshot(); _TakeScreenshot(0);
_UpdatePreviewPanel(); _UpdatePreviewPanel();
layout->SetVisibleItem(1L); layout->SetVisibleItem(1L);
fSaveScreenshot->MakeDefault(true); fSaveScreenshot->MakeDefault(true);
@@ -316,8 +358,8 @@ ScreenshotWindow::_SetupFirstLayoutItem(BCardLayout* layout)
BString delay; BString delay;
delay << fDelay / 1000000; delay << fDelay / 1000000;
fDelayControl = new BTextControl("", TR("Take screenshot after a delay of"), fDelayControl = new BTextControl("",
delay.String(), NULL); TR("Take screenshot after a delay of"), delay.String(), NULL);
_DisallowChar(fDelayControl->TextView()); _DisallowChar(fDelayControl->TextView());
fDelayControl->TextView()->SetAlignment(B_ALIGN_RIGHT); fDelayControl->TextView()->SetAlignment(B_ALIGN_RIGHT);
@@ -335,7 +377,8 @@ ScreenshotWindow::_SetupFirstLayoutItem(BCardLayout* layout)
BBox* divider = new BBox(B_FANCY_BORDER, NULL); BBox* divider = new BBox(B_FANCY_BORDER, NULL);
divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1)); divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
fBackToSave = new BButton("", TR("Back to saving"), new BMessage(kBackToSave)); fBackToSave = new BButton("", TR("Back to saving"),
new BMessage(kBackToSave));
fBackToSave->SetEnabled(false); fBackToSave->SetEnabled(false);
fTakeScreenshot = new BButton("", TR("Take screenshot"), fTakeScreenshot = new BButton("", TR("Take screenshot"),
@@ -387,22 +430,22 @@ ScreenshotWindow::_SetupFirstLayoutItem(BCardLayout* layout)
void void
ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout) ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout,
const BMessage& settings)
{ {
fPreview = new PreviewView(); fPreview = new PreviewView();
fNameControl = new BTextControl("", TR("Name:"), fNameControl = new BTextControl("", TR("Name:"),
TR_CMT("screenshot1", "!! Filename of first screenshot !!"), NULL); TR_CMT("screenshot1", "!! Filename of first screenshot !!"), NULL);
BMessage settings(_ReadSettings());
_SetupOutputPathMenu(new BMenu(TR("Please select")), settings); _SetupOutputPathMenu(new BMenu(TR("Please select")), settings);
BMenuField* menuField2 = new BMenuField(TR("Save in:"), fOutputPathMenu); BMenuField* menuField2 = new BMenuField(TR("Save in:"), fOutputPathMenu);
fNameControl->SetText(_FindValidFileName( fNameControl->SetText(_FindValidFileName(
TR_CMT("screenshot1", "!! Filename of first screenshot !!")).String()); TR_CMT("screenshot1", "!! Filename of first screenshot !!")).String());
_SetupTranslatorMenu(new BMenu(TR("Please select")), settings); _SetupTranslatorMenu(new BMenu(TR("Please select")));
BMenuField* menuField = new BMenuField(TR("Save as:"), fTranslatorMenu); BMenuField* menuField = new BMenuField(TR("Save as:"), fTranslatorMenu);
BBox* divider = new BBox(B_FANCY_BORDER, NULL); BBox* divider = new BBox(B_FANCY_BORDER, NULL);
@@ -454,8 +497,7 @@ ScreenshotWindow::_DisallowChar(BTextView* textView)
void void
ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu, ScreenshotWindow::_SetupTranslatorMenu(BMenu* translatorMenu)
const BMessage& settings)
{ {
fTranslatorMenu = translatorMenu; fTranslatorMenu = translatorMenu;
@@ -469,9 +511,6 @@ 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; 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);
@@ -501,17 +540,20 @@ ScreenshotWindow::_SetupOutputPathMenu(BMenu* outputPathMenu,
find_directory(B_USER_DIRECTORY, &path); find_directory(B_USER_DIRECTORY, &path);
BString label(TR("Home folder")); BString label(TR("Home folder"));
_AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath)); _AddItemToPathMenu(path.Path(), label, 0,
(path.Path() == lastSelectedPath));
path.Append("Desktop"); path.Append("Desktop");
label.SetTo(TR("Desktop")); label.SetTo(TR("Desktop"));
_AddItemToPathMenu(path.Path(), label, 0, (path.Path() == lastSelectedPath)); _AddItemToPathMenu(path.Path(), label, 0, (
path.Path() == lastSelectedPath));
find_directory(B_BEOS_ETC_DIRECTORY, &path); find_directory(B_BEOS_ETC_DIRECTORY, &path);
path.Append("artwork"); path.Append("artwork");
label.SetTo(TR("Artwork folder")); label.SetTo(TR("Artwork folder"));
_AddItemToPathMenu(path.Path(), label, 2, (path.Path() == lastSelectedPath)); _AddItemToPathMenu(path.Path(), label, 2,
(path.Path() == lastSelectedPath));
int32 i = 0; int32 i = 0;
BString userPath; BString userPath;
@@ -559,14 +601,14 @@ ScreenshotWindow::_UpdatePreviewPanel()
{ {
float height = 150.0f; float height = 150.0f;
float width = (fScreenshot->Bounds().Width() / float width = (fScreenshot->Bounds().Width()
fScreenshot->Bounds().Height()) * height; / fScreenshot->Bounds().Height()) * height;
// to prevent a preview way too wide // to prevent a preview way too wide
if (width > 400.0f) { if (width > 400.0f) {
width = 400.0f; width = 400.0f;
height = (fScreenshot->Bounds().Height() / height = (fScreenshot->Bounds().Height()
fScreenshot->Bounds().Width()) * width; / fScreenshot->Bounds().Width()) * width;
} }
fPreview->SetExplicitMinSize(BSize(width, height)); fPreview->SetExplicitMinSize(BSize(width, height));
@@ -591,8 +633,8 @@ ScreenshotWindow::_UpdateFilenameSelection()
{ {
fNameControl->MakeFocus(true); fNameControl->MakeFocus(true);
fNameControl->TextView()->Select(0, fNameControl->TextView()->Select(0,
fNameControl->TextView()->TextLength() - fNameControl->TextView()->TextLength()
fExtension.Length()); - fExtension.Length());
fNameControl->TextView()->ScrollToSelection(); fNameControl->TextView()->ScrollToSelection();
} }
@@ -627,7 +669,8 @@ ScreenshotWindow::_FindValidFileName(const char* name)
BMessage msgExtensions; BMessage msgExtensions;
if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) { if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
const char* extension; const char* extension;
if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) { if (msgExtensions.FindString("extensions", 0,
&extension) == B_OK) {
fExtension.SetTo(extension); fExtension.SetTo(extension);
fExtension.Prepend("."); fExtension.Prepend(".");
} else } else
@@ -647,8 +690,10 @@ ScreenshotWindow::_FindValidFileName(const char* name)
if (!BEntry(outputPath.Path()).Exists()) if (!BEntry(outputPath.Path()).Exists())
return fileName; return fileName;
if (baseName.FindFirst(TR_CMT("screenshot", "!! Basename of screenshot files. !!")) == 0) if (baseName.FindFirst(TR_CMT("screenshot",
baseName.SetTo(TR_CMT("screenshot", "!! Basename of screenshot files. !!" )); "!! Basename of screenshot files. !!")) == 0)
baseName.SetTo(TR_CMT("screenshot",
"!! Basename of screenshot files. !!"));
BEntry entry; BEntry entry;
int32 index = 1; int32 index = 1;
@@ -684,11 +729,12 @@ ScreenshotWindow::_PathIndexInMenu(const BString& path) const
BMessage BMessage
ScreenshotWindow::_ReadSettings() const ScreenshotWindow::_ReadSettings() const
{ {
BPath settingsPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
settingsPath.Append("screenshot");
BMessage settings; BMessage settings;
BPath settingsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
return settings;
settingsPath.Append("Screenshot_settings");
BFile file(settingsPath.Path(), B_READ_ONLY); BFile file(settingsPath.Path(), B_READ_ONLY);
if (file.InitCheck() == B_OK) if (file.InitCheck() == B_OK)
@@ -702,7 +748,13 @@ void
ScreenshotWindow::_WriteSettings() const ScreenshotWindow::_WriteSettings() const
{ {
BMessage settings; BMessage settings;
settings.AddInt32("be:type", fImageFileType);
settings.AddInt32("type", fImageFileType);
settings.AddBool("includeBorder", fIncludeBorder);
settings.AddBool("includeMouse", fIncludeMouse);
settings.AddBool("grabActiveWindow", fGrabActiveWindow);
settings.AddInt64("delay", fDelay);
settings.AddString("outputFilename", fOutputFilename);
BString path; BString path;
int32 count = fOutputPathMenu->CountItems(); int32 count = fOutputPathMenu->CountItems();
@@ -724,10 +776,12 @@ ScreenshotWindow::_WriteSettings() const
} }
BPath settingsPath; BPath settingsPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath); if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK)
settingsPath.Append("screenshot"); return;
settingsPath.Append("Screenshot_settings");
BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY); BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE
| B_WRITE_ONLY);
if (file.InitCheck() == B_OK) { if (file.InitCheck() == B_OK) {
ssize_t size; ssize_t size;
settings.Flatten(&file, &size); settings.Flatten(&file, &size);
@@ -736,12 +790,10 @@ ScreenshotWindow::_WriteSettings() const
void void
ScreenshotWindow::_TakeScreenshot() ScreenshotWindow::_TakeScreenshot(bigtime_t delay)
{ {
if (fDelayControl) if (delay > 0)
snooze((atoi(fDelayControl->Text()) * 1000000) + 50000); snooze(delay);
else if (fDelay > 0)
snooze(fDelay);
BRect frame; BRect frame;
delete fScreenshot; delete fScreenshot;
+13 -11
View File
@@ -5,6 +5,7 @@
* Authors: * Authors:
* Karsten Heimrich * Karsten Heimrich
* Fredrik Modéen * Fredrik Modéen
* Wim van der Meer, [email protected]
*/ */
#ifndef SCREENSHOT_WINDOW_H #ifndef SCREENSHOT_WINDOW_H
#define SCREENSHOT_WINDOW_H #define SCREENSHOT_WINDOW_H
@@ -31,12 +32,13 @@ class PreviewView;
class ScreenshotWindow : public BWindow { class ScreenshotWindow : public BWindow {
public: public:
ScreenshotWindow(bigtime_t delay = 0, ScreenshotWindow();
bool includeBorder = false, ScreenshotWindow(bigtime_t delay,
bool includeMouse = false, bool includeBorder,
bool grabActiveWindow = false, bool includeMouse,
bool showConfigWindow = false, bool grabActiveWindow,
bool saveScreenshotSilent = false, bool showConfigWindow,
bool saveScreenshotSilent,
int32 imageFileType = B_PNG_FORMAT, int32 imageFileType = B_PNG_FORMAT,
const char* outputFilename = NULL); const char* outputFilename = NULL);
virtual ~ScreenshotWindow(); virtual ~ScreenshotWindow();
@@ -44,13 +46,13 @@ public:
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
private: private:
void _InitWindow(); void _InitWindow(const BMessage& settings);
BPath _GetDirectory(); BPath _GetDirectory();
void _SetupFirstLayoutItem(BCardLayout* layout); void _SetupFirstLayoutItem(BCardLayout* layout);
void _SetupSecondLayoutItem(BCardLayout* layout); void _SetupSecondLayoutItem(BCardLayout* layout,
void _DisallowChar(BTextView* textView);
void _SetupTranslatorMenu(BMenu* translatorMenu,
const BMessage& settings); const BMessage& settings);
void _DisallowChar(BTextView* textView);
void _SetupTranslatorMenu(BMenu* translatorMenu);
void _SetupOutputPathMenu(BMenu* outputPathMenu, void _SetupOutputPathMenu(BMenu* outputPathMenu,
const BMessage& settings); const BMessage& settings);
void _AddItemToPathMenu(const char* path, void _AddItemToPathMenu(const char* path,
@@ -64,7 +66,7 @@ private:
BMessage _ReadSettings() const; BMessage _ReadSettings() const;
void _WriteSettings() const; void _WriteSettings() const;
void _TakeScreenshot(); void _TakeScreenshot(bigtime_t delay);
status_t _GetActiveWindowFrame(BRect* frame); status_t _GetActiveWindowFrame(BRect* frame);
void _MakeTabSpaceTransparent(BRect* frame); void _MakeTabSpaceTransparent(BRect* frame);