From 0c9f5a026f4ccc18b4e8e51b8d907818e3cafa3b Mon Sep 17 00:00:00 2001 From: Wim van der Meer Date: Fri, 25 Jun 2010 13:12:15 +0000 Subject: [PATCH] * Modified PtrScr key behaviour: - No modifiers: take a screenshot with zero delay and launch the GUI - Shift-PrtScr: Silent screenshot using the saved GUI settings - Ctrl-PrtScr: Take a screenshot using the saved GUI settings and copy the screenshot to the system clipboard * A few locale related changes (I am not sure how this works when a class is shared between two applications, I hope I got everything right) git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37252 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/screenshot/Jamfile | 6 ++ src/apps/screenshot/Screenshot.cpp | 54 ++++++++-------- src/apps/screenshot/Screenshot.h | 2 +- src/apps/screenshot/ScreenshotApp.cpp | 21 ++++++- src/apps/screenshot/ScreenshotApp.h | 3 + src/apps/screenshot/ScreenshotWindow.cpp | 78 ++++++++++++++++-------- src/apps/screenshot/ScreenshotWindow.h | 6 +- src/apps/screenshot/Utility.cpp | 5 +- src/kits/interface/Window.cpp | 31 +++++++--- 9 files changed, 139 insertions(+), 67 deletions(-) diff --git a/src/apps/screenshot/Jamfile b/src/apps/screenshot/Jamfile index 94b052893c..e2022ffd72 100644 --- a/src/apps/screenshot/Jamfile +++ b/src/apps/screenshot/Jamfile @@ -24,3 +24,9 @@ Application screenshot : : be locale translation $(TARGET_LIBSUPC++) : Screenshot.rdef ; + +DoCatalogs screenshot : + x-vnd.haiku-screenshot-cli + : + Utility.cpp +; diff --git a/src/apps/screenshot/Screenshot.cpp b/src/apps/screenshot/Screenshot.cpp index 8a65811eb8..0e2f829dae 100644 --- a/src/apps/screenshot/Screenshot.cpp +++ b/src/apps/screenshot/Screenshot.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -29,6 +30,10 @@ #include "Utility.h" +#undef B_TRANSLATE_CONTEXT +#define B_TRANSLATE_CONTEXT "Screenshot" + + Screenshot::Screenshot() : BApplication("application/x-vnd.haiku-screenshot-cli"), @@ -56,7 +61,6 @@ Screenshot::ArgvReceived(int32 argc, char** argv) bool saveScreenshotSilent = false; bool copyToClipboard = false; uint32 imageFileType = B_PNG_FORMAT; - for (int32 i = 0; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) _ShowHelp(); @@ -98,8 +102,7 @@ Screenshot::ArgvReceived(int32 argc, char** argv) outputFilename = argv[i]; } - if (argc > 1) - _New(delay); + _New(delay); if (copyToClipboard || saveScreenshotSilent) { fLaunchGui = false; @@ -125,35 +128,32 @@ void Screenshot::ReadyToRun() { if (fLaunchGui) { - // Launch the GUI application - if (fUtility->wholeScreen == NULL) { - // No command line parameters were given - be_roster->Launch("application/x-vnd.haiku-screenshot"); - } else { - // Send the utility data and the command line settings to the GUI - BMessage message; - message.what = SS_UTILITY_DATA; + // Get a screenshot if we don't have one + if (fUtility->wholeScreen == NULL) + _New(0); - BMessage* bitmap = new BMessage(); - fUtility->wholeScreen->Archive(bitmap); - message.AddMessage("wholeScreen", bitmap); + // Send the screenshot data to the GUI + BMessage message; + message.what = SS_UTILITY_DATA; - bitmap = new BMessage(); - fUtility->cursorBitmap->Archive(bitmap); - message.AddMessage("cursorBitmap", bitmap); + BMessage* bitmap = new BMessage(); + fUtility->wholeScreen->Archive(bitmap); + message.AddMessage("wholeScreen", bitmap); - bitmap = new BMessage(); - fUtility->cursorAreaBitmap->Archive(bitmap); - message.AddMessage("cursorAreaBitmap", bitmap); + bitmap = new BMessage(); + fUtility->cursorBitmap->Archive(bitmap); + message.AddMessage("cursorBitmap", bitmap); - message.AddPoint("cursorPosition", fUtility->cursorPosition); - message.AddRect("activeWindowFrame", fUtility->activeWindowFrame); - message.AddRect("tabFrame", fUtility->tabFrame); - message.AddFloat("borderSize", fUtility->borderSize); + bitmap = new BMessage(); + fUtility->cursorAreaBitmap->Archive(bitmap); + message.AddMessage("cursorAreaBitmap", bitmap); - be_roster->Launch("application/x-vnd.haiku-screenshot", - &message); - } + message.AddPoint("cursorPosition", fUtility->cursorPosition); + message.AddRect("activeWindowFrame", fUtility->activeWindowFrame); + message.AddRect("tabFrame", fUtility->tabFrame); + message.AddFloat("borderSize", fUtility->borderSize); + + be_roster->Launch("application/x-vnd.haiku-screenshot", &message); } be_app->PostMessage(B_QUIT_REQUESTED); diff --git a/src/apps/screenshot/Screenshot.h b/src/apps/screenshot/Screenshot.h index 3350ad7748..94a71f46f6 100644 --- a/src/apps/screenshot/Screenshot.h +++ b/src/apps/screenshot/Screenshot.h @@ -27,7 +27,7 @@ private: void _New(bigtime_t delay); status_t _GetActiveWindowFrame(); int32 _GetImageType(const char* name) const; - + Utility* fUtility; BCatalog fCatalog; diff --git a/src/apps/screenshot/ScreenshotApp.cpp b/src/apps/screenshot/ScreenshotApp.cpp index 2e608fe873..e56e53e75d 100644 --- a/src/apps/screenshot/ScreenshotApp.cpp +++ b/src/apps/screenshot/ScreenshotApp.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -22,7 +23,9 @@ ScreenshotApp::ScreenshotApp() : BApplication("application/x-vnd.haiku-screenshot"), - fUtility(new Utility) + fUtility(new Utility), + fSilent(false), + fClipboard(false) { be_locale->GetAppCatalog(&fCatalog); } @@ -91,10 +94,24 @@ ScreenshotApp::MessageReceived(BMessage* message) } +void +ScreenshotApp::ArgvReceived(int32 argc, char** argv) +{ + for (int32 i = 0; i < argc; i++) { + if (strcmp(argv[i], "-s") == 0 + || strcmp(argv[i], "--silent") == 0) + fSilent = true; + else if (strcmp(argv[i], "-c") == 0 + || strcmp(argv[i], "--clipboard") == 0) + fClipboard = true; + } +} + + void ScreenshotApp::ReadyToRun() { - new ScreenshotWindow(*fUtility); + new ScreenshotWindow(*fUtility, fSilent, fClipboard); } diff --git a/src/apps/screenshot/ScreenshotApp.h b/src/apps/screenshot/ScreenshotApp.h index f310f6ee63..32038857f4 100644 --- a/src/apps/screenshot/ScreenshotApp.h +++ b/src/apps/screenshot/ScreenshotApp.h @@ -22,11 +22,14 @@ public: ~ScreenshotApp(); void MessageReceived(BMessage* message); + void ArgvReceived(int32 argc, char** argv); void ReadyToRun(); private: BCatalog fCatalog; Utility* fUtility; + bool fSilent; + bool fClipboard; }; diff --git a/src/apps/screenshot/ScreenshotWindow.cpp b/src/apps/screenshot/ScreenshotWindow.cpp index 59f472b8ba..cbe91c5a30 100644 --- a/src/apps/screenshot/ScreenshotWindow.cpp +++ b/src/apps/screenshot/ScreenshotWindow.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -71,7 +72,8 @@ public: #define B_TRANSLATE_CONTEXT "ScreenshotWindow" -ScreenshotWindow::ScreenshotWindow(const Utility& utility) +ScreenshotWindow::ScreenshotWindow(const Utility& utility, bool silent, + bool clipboard) : BWindow(BRect(0, 0, 200.0, 100.0), B_TRANSLATE("Screenshot"), B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_AVOID_FRONT @@ -90,17 +92,22 @@ ScreenshotWindow::ScreenshotWindow(const Utility& utility) fExtension(""), fImageFileType(B_PNG_FORMAT) { - // Check if fUtility contains valid data - if (fUtility.wholeScreen == NULL) { - // New screenshot using zero delay - _NewScreenshot(); - return; - } - // _ReadSettings() needs a valid fOutputPathMenu fOutputPathMenu = new BMenu(B_TRANSLATE("Please select")); _ReadSettings(); + // _NewScreenshot() needs a valid fNameControl + BString name(B_TRANSLATE(fUtility.sDefaultFileNameBase)); + name << 1; + name = _FindValidFileName(name.String()); + fNameControl = new BTextControl("", B_TRANSLATE("Name:"), name, NULL); + + // Check if fUtility contains valid data + if (fUtility.wholeScreen == NULL) { + _NewScreenshot(silent, clipboard); + return; + } + fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, fGrabActiveWindow, fIncludeBorder); @@ -130,11 +137,6 @@ ScreenshotWindow::ScreenshotWindow(const Utility& utility) BStringView* seconds = new BStringView("", B_TRANSLATE("seconds")); seconds->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); - BString name(fUtility.sDefaultFileNameBase); - name << 1; - name = _FindValidFileName(name.String()); - fNameControl = new BTextControl("", B_TRANSLATE("Name:"), name, NULL); - BMenuField* menuField2 = new BMenuField(B_TRANSLATE("Save in:"), fOutputPathMenu); @@ -318,21 +320,49 @@ ScreenshotWindow::Quit() void -ScreenshotWindow::_NewScreenshot() +ScreenshotWindow::_NewScreenshot(bool silent, bool clipboard) { + BMessage message(B_ARGV_RECEIVED); int32 argc = 3; - char* argv[3]; - argv[0] = "Screenshot"; - argv[1] = "--delay"; - BString delay; delay << fDelay / 1000000; - int32 charCount = delay.Length(); - argv[2] = (char*)malloc(charCount + 1); - delay.CopyInto(argv[2], 0, charCount); - argv[2][charCount] = '\0'; + message.AddString("argv", "screenshot"); + message.AddString("argv", "--delay"); + message.AddString("argv", delay); + + if (silent || clipboard) { + if (silent) { + argc++; + message.AddString("argv", "--silent"); + } + if (clipboard) { + argc++; + message.AddString("argv", "--clipboard"); + } + if (fIncludeBorder) { + argc++; + message.AddString("argv", "--border"); + } + if (fIncludeCursor) { + argc++; + message.AddString("argv", "--mouse-pointer"); + } + if (fGrabActiveWindow) { + argc++; + message.AddString("argv", "--window"); + } + if (fLastSelectedPath) { + BPath path(_GetDirectory()); + if (path != NULL) { + path.Append(fNameControl->Text()); + argc++; + message.AddString("argv", path.Path()); + } + } + } + message.AddInt32("argc", argc); - be_roster->Launch("application/x-vnd.haiku-screenshot-cli", argc, argv); + be_roster->Launch("application/x-vnd.haiku-screenshot-cli", &message); be_app->PostMessage(B_QUIT_REQUESTED); } @@ -538,7 +568,7 @@ ScreenshotWindow::_FindValidFileName(const char* name) if (!BEntry(outputPath.Path()).Exists()) return fileName; - if (baseName.FindFirst(fUtility.sDefaultFileNameBase) == 0) + if (baseName.FindFirst(B_TRANSLATE(fUtility.sDefaultFileNameBase)) == 0) baseName.SetTo(fUtility.sDefaultFileNameBase); BEntry entry; diff --git a/src/apps/screenshot/ScreenshotWindow.h b/src/apps/screenshot/ScreenshotWindow.h index 333588a9d0..8e739c3060 100644 --- a/src/apps/screenshot/ScreenshotWindow.h +++ b/src/apps/screenshot/ScreenshotWindow.h @@ -32,14 +32,16 @@ class Utility; class ScreenshotWindow : public BWindow { public: - ScreenshotWindow(const Utility& utility); + ScreenshotWindow(const Utility& utility, + bool silent, bool clipboard); ~ScreenshotWindow(); void MessageReceived(BMessage* message); void Quit(); private: - void _NewScreenshot(); + void _NewScreenshot(bool silent = false, + bool clipboard = false); void _UpdatePreviewPanel(); void _DisallowChar(BTextView* textView); void _SetupOutputPathMenu(const BMessage& settings); diff --git a/src/apps/screenshot/Utility.cpp b/src/apps/screenshot/Utility.cpp index 6160980dd9..13c2386a84 100644 --- a/src/apps/screenshot/Utility.cpp +++ b/src/apps/screenshot/Utility.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -96,7 +97,7 @@ Utility::Save(BBitmap** screenshot, const char* fileName, uint32 imageType) BString extension = GetFileNameExtension(imageType); do { fileNameString.SetTo(homePath.Path()); - fileNameString << "/" << sDefaultFileNameBase << index++ + fileNameString << "/" << B_TRANSLATE(sDefaultFileNameBase) << index++ << extension; entry.SetTo(fileNameString.String()); } while (entry.Exists()); @@ -204,7 +205,7 @@ Utility::GetFileNameExtension(uint32 imageType) const const char* Utility::_GetMimeString(uint32 imageType) const { - char dummy[] = ""; + const char *dummy = ""; translator_id* translators = NULL; int32 numTranslators = 0; BTranslatorRoster* roster = BTranslatorRoster::Default(); diff --git a/src/kits/interface/Window.cpp b/src/kits/interface/Window.cpp index ab7c4d1673..d52cbde801 100644 --- a/src/kits/interface/Window.cpp +++ b/src/kits/interface/Window.cpp @@ -3608,17 +3608,30 @@ BWindow::_HandleKeyDown(BMessage* event) return true; } + // PrtScr key takes a screenshot if (key == B_FUNCTION_KEY && rawKey == B_PRINT_KEY) { - BMessage message(B_REFS_RECEIVED); - message.AddBool("silent", true); + // With no modifier keys the best way to get a screenshot is by + // calling the screenshot CLI + if (modifiers == 0) { + be_roster->Launch("application/x-vnd.haiku-screenshot-cli"); + return true; + } - if ((modifiers & B_CONTROL_KEY) != 0) - message.AddBool("window", true); - - if ((modifiers & B_SHIFT_KEY) != 0 || (modifiers & B_OPTION_KEY) != 0) - message.ReplaceBool("silent", false); - - be_roster->Launch("application/x-vnd.haiku-screenshot-cli", &message); + // Prepare a message based on the modifier keys pressed and launch the + // screenshot GUI + BMessage message(B_ARGV_RECEIVED); + int32 argc = 1; + message.AddString("argv", "Screenshot"); + if ((modifiers & B_CONTROL_KEY) != 0) { + argc++; + message.AddString("argv", "--clipboard"); + } + if ((modifiers & B_SHIFT_KEY) != 0) { + argc++; + message.AddString("argv", "--silent"); + } + message.AddInt32("argc", argc); + be_roster->Launch("application/x-vnd.haiku-screenshot", &message); return true; }