Additional CLI parameters for Screeshot (Ticket #3816) and some cleanup (I hope), using Translator kit now instead of own PNG when using CLI. More changes will follow.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34140 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Fredrik Modeen
2009-11-19 20:47:17 +00:00
parent f0a0bca5bb
commit 689b99a74c
9 changed files with 249 additions and 393 deletions
+2 -3
View File
@@ -1,15 +1,14 @@
SubDir HAIKU_TOP src apps screenshot ; SubDir HAIKU_TOP src apps screenshot ;
UseLibraryHeaders png zlib ; UseLibraryHeaders zlib ;
UsePrivateHeaders interface ; UsePrivateHeaders interface ;
Application Screenshot : Application Screenshot :
main.cpp main.cpp
PNGDump.cpp
PreviewView.cpp PreviewView.cpp
Screenshot.cpp Screenshot.cpp
ScreenshotWindow.cpp ScreenshotWindow.cpp
: be tracker translation libpng.so libz.so $(TARGET_LIBSUPC++) : be tracker translation libz.so $(TARGET_LIBSUPC++)
: Screenshot.rdef : Screenshot.rdef
; ;
-199
View File
@@ -1,199 +0,0 @@
/*
* Copyright 2001-2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* DarkWyrm <[email protected]>
* Stephan Aßmus <[email protected]>
*/
/** Function for saving a generic framebuffer to a PNG file */
#include "PNGDump.h"
#include <InterfaceDefs.h>
#include <NodeInfo.h>
#include <Rect.h>
#include <png.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define TRACE_PNGDUMP
#ifdef TRACE_PNGDUMP
# define TRACE(x) printf x
#else
# define TRACE(x) ;
#endif
status_t
SaveToPNG(const char* filename, const BRect& bounds, color_space space,
const void* bits, int32 bitsLength, int32 bytesPerRow)
{
int32 width = bounds.IntegerWidth() + 1;
int32 height = bounds.IntegerHeight() + 1;
TRACE(("SaveToPNG: %s (%ldx%ld)\n", filename, width, height));
FILE *file = fopen(filename, "wb");
if (file == NULL) {
TRACE(("Couldn't open file: %s\n", strerror(errno)));
return errno;
}
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (png == NULL) {
TRACE(("Couldn't create write struct\n"));
fclose(file);
return B_NO_MEMORY;
}
png_infop info = png_create_info_struct(png);
if (info == NULL) {
TRACE(("Couldn't create info struct\n"));
png_destroy_write_struct(&png, NULL);
fclose(file);
return B_NO_MEMORY;
}
if (setjmp(png->jmpbuf)) {
png_destroy_write_struct(&png, NULL);
fclose(file);
return B_ERROR;
}
png_init_io(png, file);
png_set_bgr(png);
// TODO: support other color spaces if needed
switch (space) {
case B_RGB32:
case B_RGBA32:
{
// create file without alpha channel
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
// convert from 32 bit RGB to 24 bit RGB while saving
png_byte* src = (png_byte*)bits;
int srcRowBytes = width * 4;
int dstRowBytes = width * 3;
int srcRowOffset = bytesPerRow - srcRowBytes;
png_byte tempRow[dstRowBytes];
for (int row = 0; row < height; row++) {
for (int i = 0; i < dstRowBytes; i += 3, src += 4) {
tempRow[i] = src[0];
tempRow[i + 1] = src[1];
tempRow[i + 2] = src[2];
}
src += srcRowOffset;
png_write_row(png, tempRow);
}
break;
}
case B_RGB16:
{
// create file without alpha channel
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
// convert from 16 bit RGB to 24 bit RGB while saving
uint16* src = (uint16 *)bits;
int dstRowBytes = width * 3;
png_byte tempRow[dstRowBytes];
for (int row = 0; row < height; row++) {
for (int i = 0; i < dstRowBytes; i += 3, src++) {
tempRow[i + 2] = (*src & 0xf800) >> 8;
tempRow[i + 1] = (*src & 0x07e0) >> 3;
tempRow[i] = (*src & 0x001f) << 3;
}
src = (uint16 *)((uint8 *)bits + row * bytesPerRow);
png_write_row(png, tempRow);
}
break;
}
case B_RGB15:
{
// create file without alpha channel
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
// convert from 15 bit RGB to 24 bit RGB while saving
uint16* src = (uint16 *)bits;
int dstRowBytes = width * 3;
png_byte tempRow[dstRowBytes];
for (int row = 0; row < height; row++) {
for (int i = 0; i < dstRowBytes; i += 3, src++) {
tempRow[i + 2] = (*src & 0x7c00) >> 7;
tempRow[i + 1] = (*src & 0x03e0) >> 2;
tempRow[i] = (*src & 0x001f) << 3;
}
src = (uint16 *)((uint8 *)bits + row * bytesPerRow);
png_write_row(png, tempRow);
}
break;
}
case B_CMAP8:
{
// create file without alpha channel
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);
// convert from 8 bit CMAP to 24 bit RGB while saving
const color_map *colorMap = system_colors();
uint8* src = (uint8 *)bits;
int dstRowBytes = width * 3;
png_byte tempRow[dstRowBytes];
for (int row = 0; row < height; row++) {
for (int i = 0; i < dstRowBytes; i += 3, src++) {
tempRow[i + 2] = colorMap->color_list[*src].red;
tempRow[i + 1] = colorMap->color_list[*src].green;
tempRow[i] = colorMap->color_list[*src].blue;
}
src = (uint8 *)bits + row * bytesPerRow;
png_write_row(png, tempRow);
}
break;
}
default:
{
TRACE(("Unsupported color space %x\n", space));
png_destroy_write_struct(&png, NULL);
fclose(file);
return B_ERROR;
}
}
png_write_end(png, info);
png_destroy_write_struct(&png, NULL);
fclose(file);
// Set the file type manually, so that it doesn't have to be
// picked up by the registrar or Tracker, first
BNode node(filename);
BNodeInfo nodeInfo(&node);
if (nodeInfo.InitCheck() == B_OK)
nodeInfo.SetType("image/png");
return B_OK;
}
-20
View File
@@ -1,20 +0,0 @@
/*
* Copyright 2001-2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* DarkWyrm <[email protected]>
*/
#ifndef PNGDUMP_H
#define PNGDUMP_H
#include <GraphicsDefs.h>
class BRect;
status_t SaveToPNG(const char* filename, const BRect& bounds, color_space space,
const void* bits, int32 bitsLength, int32 bytesPerRow);
#endif
+4 -1
View File
@@ -4,11 +4,14 @@
*/ */
#include "PreviewView.h" #include "PreviewView.h"
#include <ControlLook.h> #include <ControlLook.h>
PreviewView::PreviewView() PreviewView::PreviewView()
: BView("preview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE) :
BView("preview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
{ {
} }
-1
View File
@@ -2,7 +2,6 @@
* Copyright 2009, Philippe Saint-Pierre, [email protected] * Copyright 2009, Philippe Saint-Pierre, [email protected]
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef PREVIEW_VIEW_H #ifndef PREVIEW_VIEW_H
#define PREVIEW_VIEW_H #define PREVIEW_VIEW_H
+70 -16
View File
@@ -1,10 +1,14 @@
/* /*
* Copyright Karsten Heimrich, [email protected]. All rights reserved. * Copyright Karsten Heimrich, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
* Authors:
* Karsten Heimrich
* Fredrik Modéen
*/ */
#include "Screenshot.h" #include "Screenshot.h"
#include "ScreenshotWindow.h"
#include <stdio.h> #include <stdio.h>
@@ -12,10 +16,19 @@
#include <string.h> #include <string.h>
#include <TranslatorFormats.h>
#include "ScreenshotWindow.h"
Screenshot::Screenshot() Screenshot::Screenshot()
: BApplication("application/x-vnd.Haiku-Screenshot"), :
BApplication("application/x-vnd.Haiku-Screenshot"),
fArgvReceived(false), fArgvReceived(false),
fRefsReceived(false) fRefsReceived(false),
fImageFileType(B_PNG_FORMAT),
fTranslator(8)
{ {
} }
@@ -28,7 +41,7 @@ Screenshot::~Screenshot()
void void
Screenshot::ReadyToRun() Screenshot::ReadyToRun()
{ {
if(!fArgvReceived && !fRefsReceived) if (!fArgvReceived && !fRefsReceived)
new ScreenshotWindow(); new ScreenshotWindow();
fArgvReceived = false; fArgvReceived = false;
@@ -78,17 +91,26 @@ Screenshot::ArgvReceived(int32 argc, char** argv)
for (int32 i = 0; i < argc; i++) { for (int32 i = 0; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
_ShowHelp(); _ShowHelp();
else if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--border") == 0) else if (strcmp(argv[i], "-b") == 0
|| strcmp(argv[i], "--border") == 0)
includeBorder = true; includeBorder = true;
else if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--mouse-pointer") == 0) else if (strcmp(argv[i], "-m") == 0
|| strcmp(argv[i], "--mouse-pointer") == 0)
includeMouse = true; includeMouse = true;
else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "--window") == 0) else if (strcmp(argv[i], "-w") == 0
|| strcmp(argv[i], "--window") == 0)
grabActiveWindow = true; grabActiveWindow = true;
else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--silent") == 0) else if (strcmp(argv[i], "-s") == 0
|| strcmp(argv[i], "--silent") == 0)
saveScreenshotSilent = true; saveScreenshotSilent = true;
else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--options") == 0) else if (strcmp(argv[i], "-o") == 0
|| strcmp(argv[i], "--options") == 0)
showConfigureWindow = true; showConfigureWindow = true;
else if (strcmp(argv[i], "-d") == 0 else if (strcmp(argv[i], "-f") == 0
|| strncmp(argv[i], "--format", 6) == 0
|| strncmp(argv[i], "--format=", 7) == 0) {
_SetImageTypeSilence(argv[i + 1]);
} else if (strcmp(argv[i], "-d") == 0
|| strncmp(argv[i], "--delay", 7) == 0 || strncmp(argv[i], "--delay", 7) == 0
|| strncmp(argv[i], "--delay=", 8) == 0) { || strncmp(argv[i], "--delay=", 8) == 0) {
int32 seconds = -1; int32 seconds = -1;
@@ -97,16 +119,19 @@ Screenshot::ArgvReceived(int32 argc, char** argv)
if (seconds >= 0) { if (seconds >= 0) {
delay = seconds * 1000000; delay = seconds * 1000000;
i++; i++;
} } else {
else { printf("Screenshot: option requires an argument -- %s\n"
printf("Screenshot: option requires an argument -- %s\n", argv[i]); , argv[i]);
exit(0); exit(0);
} }
} }
} }
fArgvReceived = true; fArgvReceived = true;
new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow, new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
showConfigureWindow, saveScreenshotSilent); showConfigureWindow, saveScreenshotSilent, fImageFileType,
fTranslator);
} }
@@ -121,9 +146,38 @@ Screenshot::_ShowHelp() const
printf(" -w, --window Capture the active window instead of the entire screen\n"); printf(" -w, --window Capture the active window instead of the entire screen\n");
printf(" -d, --delay=seconds Take screenshot after specified delay [in seconds]\n"); printf(" -d, --delay=seconds Take screenshot after specified delay [in seconds]\n");
printf(" -s, --silent Saves the screenshot without showing the app window\n"); printf(" -s, --silent Saves the screenshot without showing the app window\n");
printf(" overrides --options, saves to home folder as png\n"); printf(" overrides --options\n");
printf(" -f, --format=image Write the image format you like to save as\n");
printf(" [bmp], [gif], [jpg], [png], [ppm], [targa], [tiff]\n");
printf("\n"); printf("\n");
printf("Note: OPTION -b, --border takes only effect when used with -w, --window\n"); printf("Note: OPTION -b, --border takes only effect when used with -w, --window\n");
exit(0); exit(0);
} }
void
Screenshot::_SetImageTypeSilence(const char* name)
{
if (strcmp(name, "bmp") == 0) {
fImageFileType = B_BMP_FORMAT;
fTranslator = 1;
} else if (strcmp(name, "gif") == 0) {
fImageFileType = B_GIF_FORMAT;
fTranslator = 3;
} else if (strcmp(name, "jpg") == 0) {
fImageFileType = B_JPEG_FORMAT;
fTranslator = 6;
} else if (strcmp(name, "ppm") == 0) {
fImageFileType = B_PPM_FORMAT;
fTranslator = 9;
} else if (strcmp(name, "targa") == 0) {
fImageFileType = B_TGA_FORMAT;
fTranslator = 14;
} else if (strcmp(name, "tif") == 0) {
fImageFileType = B_TIFF_FORMAT;
fTranslator = 15;
} else { //png
fImageFileType = B_PNG_FORMAT;
fTranslator = 8;
}
}
+13 -1
View File
@@ -1,7 +1,15 @@
/* /*
* Copyright Karsten Heimrich, [email protected]. All rights reserved. * Copyright Karsten Heimrich, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
* Authors:
* Karsten Heimrich
* Fredrik Modéen
*/ */
#ifndef SCREENSHOT_H
#define SCREENSHOT_H
#include <Application.h> #include <Application.h>
@@ -16,8 +24,12 @@ public:
private: private:
void _ShowHelp() const; void _ShowHelp() const;
void _SetImageTypeSilence(const char* name);
private:
bool fArgvReceived; bool fArgvReceived;
bool fRefsReceived; bool fRefsReceived;
int32 fImageFileType;
int32 fTranslator;
}; };
#endif /* SCREENSHOT_H */
+124 -128
View File
@@ -1,11 +1,18 @@
/* /*
* Copyright Karsten Heimrich, [email protected]. All rights reserved. * Copyright Karsten Heimrich, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
* Authors:
* Karsten Heimrich
* Fredrik Modéen
*/ */
#include "ScreenshotWindow.h" #include "ScreenshotWindow.h"
#include "PNGDump.h"
#include <stdio.h>
#include <stdlib.h>
#include <Alert.h> #include <Alert.h>
#include <Application.h> #include <Application.h>
@@ -44,8 +51,7 @@
#include <WindowInfo.h> #include <WindowInfo.h>
#include <stdio.h> #include "PreviewView.h"
#include <stdlib.h>
enum { enum {
@@ -81,8 +87,9 @@ public:
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) bool saveScreenshotSilent, int32 imageFileType, int32 translator)
: 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), fDelayControl(NULL),
@@ -95,15 +102,19 @@ ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
fIncludeMouse(includeMouse), fIncludeMouse(includeMouse),
fGrabActiveWindow(grabActiveWindow), fGrabActiveWindow(grabActiveWindow),
fShowConfigWindow(showConfigWindow), fShowConfigWindow(showConfigWindow),
fExtension("") fSaveScreenshotSilent(saveScreenshotSilent),
fExtension(""),
fTranslator(translator),
fImageFileType(imageFileType)
{ {
if (saveScreenshotSilent) { if (fSaveScreenshotSilent) {
_TakeScreenshot(); _TakeScreenshot();
_SaveScreenshotSilent(); _SaveScreenshot();
be_app_messenger.SendMessage(B_QUIT_REQUESTED); be_app_messenger.SendMessage(B_QUIT_REQUESTED);
} else { } else {
_InitWindow(); _InitWindow();
_CenterAndShow(); CenterOnScreen();
Show();
} }
} }
@@ -121,53 +132,58 @@ ScreenshotWindow::~ScreenshotWindow()
void void
ScreenshotWindow::MessageReceived(BMessage* message) ScreenshotWindow::MessageReceived(BMessage* message)
{ {
// message->PrintToStream();
switch (message->what) { switch (message->what) {
case kScreenshotType: { case kScreenshotType:
fGrabActiveWindow = false; fGrabActiveWindow = false;
if (fActiveWindow->Value() == B_CONTROL_ON) if (fActiveWindow->Value() == B_CONTROL_ON)
fGrabActiveWindow = true; fGrabActiveWindow = true;
fWindowBorder->SetEnabled(fGrabActiveWindow); fWindowBorder->SetEnabled(fGrabActiveWindow);
} break; break;
case kIncludeBorder: { case kIncludeBorder:
fIncludeBorder = (fWindowBorder->Value() == B_CONTROL_ON); fIncludeBorder = (fWindowBorder->Value() == B_CONTROL_ON);
} break; break;
case kShowMouse: { case kShowMouse:
printf("kShowMouse\n");
fIncludeMouse = (fShowMouse->Value() == B_CONTROL_ON); fIncludeMouse = (fShowMouse->Value() == B_CONTROL_ON);
} break; break;
case kBackToSave: { case kBackToSave:
{
BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout()); BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout());
if (layout) if (layout)
layout->SetVisibleItem(1L); layout->SetVisibleItem(1L);
SetTitle("Save Screenshot"); SetTitle("Save Screenshot");
} break; break;
}
case kTakeScreenshot: { case kTakeScreenshot:
Hide(); Hide();
_TakeScreenshot(); _TakeScreenshot();
_UpdatePreviewPanel(); _UpdatePreviewPanel();
Show(); Show();
} break; break;
case kImageOutputFormat: { case kImageOutputFormat:
message->FindInt32("be:type", &fImageFileType); message->FindInt32("be:type", &fImageFileType);
message->FindInt32("be:translator", &fTranslator); message->FindInt32("be:translator", &fTranslator);
const char* text = fNameControl->Text(); fNameControl->SetText(_FindValidFileName(fNameControl->Text()));
fNameControl->SetText(_FindValidFileName(text).String()); break;
} break;
case kLocationChanged: { case kLocationChanged:
{
void* source = NULL; void* source = NULL;
if (message->FindPointer("source", &source) == B_OK) if (message->FindPointer("source", &source) == B_OK)
fLastSelectedPath = static_cast<BMenuItem*> (source); fLastSelectedPath = static_cast<BMenuItem*> (source);
const char* text = fNameControl->Text(); fNameControl->SetText(_FindValidFileName(fNameControl->Text()));
fNameControl->SetText(_FindValidFileName(text).String()); break;
} break; }
case kChooseLocation: { case kChooseLocation:
{
if (!fOutputPathPanel) { if (!fOutputPathPanel) {
BMessenger target(this); BMessenger target(this);
fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target,
@@ -176,25 +192,27 @@ ScreenshotWindow::MessageReceived(BMessage* message)
fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON, "Select"); fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON, "Select");
} }
fOutputPathPanel->Show(); fOutputPathPanel->Show();
} break; break;
}
case B_CANCEL: { case B_CANCEL:
fLastSelectedPath->SetMarked(true); fLastSelectedPath->SetMarked(true);
} break; break;
case B_REFS_RECEIVED: { case B_REFS_RECEIVED:
{
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); int32 index = _PathIndexInMenu(path);
if (index < 0) { if (index < 0)
_AddItemToPathMenu(path.String(), _AddItemToPathMenu(path.String(), path,
path, fOutputPathMenu->CountItems() - 2, true); fOutputPathMenu->CountItems() - 2, true);
} else { else
fOutputPathMenu->ItemAt(index)->SetMarked(true); fOutputPathMenu->ItemAt(index)->SetMarked(true);
}
} }
} break; break;
}
case kFinishScreenshot: case kFinishScreenshot:
_WriteSettings(); _WriteSettings();
@@ -204,21 +222,43 @@ ScreenshotWindow::MessageReceived(BMessage* message)
// fall through // fall through
case B_QUIT_REQUESTED: case B_QUIT_REQUESTED:
be_app_messenger.SendMessage(B_QUIT_REQUESTED); be_app_messenger.SendMessage(B_QUIT_REQUESTED);
break; break;
case kShowOptions: { case kShowOptions:
{
BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout()); BCardLayout* layout = dynamic_cast<BCardLayout*> (GetLayout());
if (layout) if (layout)
layout->SetVisibleItem(0L); layout->SetVisibleItem(0L);
SetTitle("Take Screenshot"); SetTitle("Take Screenshot");
fBackToSave->SetEnabled(true); fBackToSave->SetEnabled(true);
} break; break;
}
default: { default:
BWindow::MessageReceived(message); BWindow::MessageReceived(message);
} break; break;
}; }
}
BPath
ScreenshotWindow::_GetDirectory()
{
BPath path;
if (!fSaveScreenshotSilent) {
BMessage* message = fLastSelectedPath->Message();
const char* stringPath;
if (!message || message->FindString("path", &stringPath) != B_OK) {
fprintf(stderr, "failed to find path in message\n");
} else
path.SetTo(stringPath);
} else {
if (find_directory(B_USER_DIRECTORY, &path) != B_OK)
fprintf(stderr, "failed to find user home folder\n");
}
return path;
} }
@@ -235,9 +275,8 @@ ScreenshotWindow::_InitWindow()
_TakeScreenshot(); _TakeScreenshot();
_UpdatePreviewPanel(); _UpdatePreviewPanel();
layout->SetVisibleItem(1L); layout->SetVisibleItem(1L);
} else { } else
layout->SetVisibleItem(0L); layout->SetVisibleItem(0L);
}
} }
@@ -249,9 +288,9 @@ ScreenshotWindow::_SetupFirstLayoutItem(BCardLayout* layout)
stringView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); stringView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fActiveWindow = new BRadioButton("Capture active window", fActiveWindow = new BRadioButton("Capture active window",
new BMessage(kScreenshotType)); new BMessage(kScreenshotType));
fWholeDesktop = new BRadioButton("Capture entire screen", fWholeDesktop = new BRadioButton("Capture entire screen",
new BMessage(kScreenshotType)); new BMessage(kScreenshotType));
fWholeDesktop->SetValue(B_CONTROL_ON); fWholeDesktop->SetValue(B_CONTROL_ON);
BString delay; BString delay;
@@ -338,7 +377,7 @@ ScreenshotWindow::_SetupSecondLayoutItem(BCardLayout* layout)
_SetupOutputPathMenu(new BMenu("Please select"), settings); _SetupOutputPathMenu(new BMenu("Please select"), settings);
BMenuField* menuField2 = new BMenuField("Save in:", fOutputPathMenu); BMenuField* menuField2 = new BMenuField("Save in:", fOutputPathMenu);
fNameControl->SetText(_FindValidFileName("screenshot1").String()); fNameControl->SetText(_FindValidFileName("screenshot1"));
_SetupTranslatorMenu(new BMenu("Please select"), settings); _SetupTranslatorMenu(new BMenu("Please select"), settings);
BMenuField* menuField = new BMenuField("Save as:", fTranslatorMenu); BMenuField* menuField = new BMenuField("Save as:", fTranslatorMenu);
@@ -458,10 +497,9 @@ ScreenshotWindow::_SetupOutputPathMenu(BMenu* outputPathMenu,
if (settings.IsEmpty() || lastSelectedPath.Length() == 0) { if (settings.IsEmpty() || lastSelectedPath.Length() == 0) {
fOutputPathMenu->ItemAt(1)->SetMarked(true); fOutputPathMenu->ItemAt(1)->SetMarked(true);
fLastSelectedPath = fOutputPathMenu->ItemAt(1); fLastSelectedPath = fOutputPathMenu->ItemAt(1);
} else { } else
_AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3, _AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3,
true); true);
}
} }
fOutputPathMenu->AddItem(new BSeparatorItem()); fOutputPathMenu->AddItem(new BSeparatorItem());
@@ -489,15 +527,6 @@ ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label,
} }
void
ScreenshotWindow::_CenterAndShow()
{
CenterOnScreen();
Show();
}
void void
ScreenshotWindow::_UpdatePreviewPanel() ScreenshotWindow::_UpdatePreviewPanel()
{ {
@@ -528,27 +557,25 @@ ScreenshotWindow::_UpdatePreviewPanel()
} }
BString const char*
ScreenshotWindow::_FindValidFileName(const char* name) ScreenshotWindow::_FindValidFileName(const char* name)
{ {
BString baseName(name); BString baseName(name);
if (fExtension.Compare("")) { if (fExtension.Compare(""))
baseName.RemoveLast(fExtension); baseName.RemoveLast(fExtension);
}
if (!fLastSelectedPath) if (!fSaveScreenshotSilent && !fLastSelectedPath)
return baseName; return baseName;
const char* path; BPath orgPath(_GetDirectory());
BMessage* message = fLastSelectedPath->Message(); if (orgPath == NULL)
if (!message || message->FindString("path", &path) != B_OK)
return baseName; return baseName;
BTranslatorRoster* roster = BTranslatorRoster::Default(); BTranslatorRoster* roster = BTranslatorRoster::Default();
const translation_format* formats = NULL; const translation_format* formats = NULL;
int32 numFormats;
int32 numFormats;
if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) == B_OK) { if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) == B_OK) {
for (int32 i = 0; i < numFormats; ++i) { for (int32 i = 0; i < numFormats; ++i) {
if (formats[i].type == uint32(fImageFileType)) { if (formats[i].type == uint32(fImageFileType)) {
@@ -559,8 +586,7 @@ ScreenshotWindow::_FindValidFileName(const char* name)
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
fExtension.SetTo(""); fExtension.SetTo("");
} }
break; break;
@@ -568,7 +594,7 @@ ScreenshotWindow::_FindValidFileName(const char* name)
} }
} }
BPath outputPath(path); BPath outputPath = orgPath;
BString fileName; BString fileName;
fileName << baseName << fExtension; fileName << baseName << fExtension;
outputPath.Append(fileName); outputPath.Append(fileName);
@@ -585,12 +611,12 @@ ScreenshotWindow::_FindValidFileName(const char* name)
do { do {
sprintf(filename, "%s%ld%s", baseName.String(), index++, sprintf(filename, "%s%ld%s", baseName.String(), index++,
fExtension.String()); fExtension.String());
outputPath.SetTo(path); outputPath.SetTo(orgPath.Path());
outputPath.Append(filename); outputPath.Append(filename);
entry.SetTo(outputPath.Path()); entry.SetTo(outputPath.Path());
} while (entry.Exists()); } while (entry.Exists());
return BString(filename); return BString(filename).String();
} }
@@ -679,9 +705,8 @@ ScreenshotWindow::_TakeScreenshot()
BScreen(this).ReadBitmap(fScreenshot, fIncludeMouse, &frame); BScreen(this).ReadBitmap(fScreenshot, fIncludeMouse, &frame);
if (fIncludeBorder) if (fIncludeBorder)
_MakeTabSpaceTransparent(&frame); _MakeTabSpaceTransparent(&frame);
} else { } else
BScreen(this).GetBitmap(&fScreenshot, fIncludeMouse); BScreen(this).GetBitmap(&fScreenshot, fIncludeMouse);
}
} }
@@ -738,38 +763,40 @@ ScreenshotWindow::_GetActiveWindowFrame(BRect* frame)
status_t status_t
ScreenshotWindow::_SaveScreenshot() ScreenshotWindow::_SaveScreenshot()
{ {
if (!fScreenshot || !fLastSelectedPath) if (!fScreenshot || (!fSaveScreenshotSilent && !fLastSelectedPath))
return B_ERROR; return B_ERROR;
const char* _path; BPath path(_GetDirectory());
BMessage* message = fLastSelectedPath->Message();
if (!message || message->FindString("path", &_path) != B_OK) if (path == NULL)
return B_ERROR; return B_ERROR;
if (fSaveScreenshotSilent)
path.Append(_FindValidFileName("screenshot1"));
else
path.Append(fNameControl->Text());
BEntry entry; BEntry entry;
BPath path;
path = _path;
path.Append(fNameControl->Text());
entry.SetTo(path.Path()); entry.SetTo(path.Path());
if (entry.Exists()) { if (!fSaveScreenshotSilent) {
BAlert *overwriteAlert = new BAlert("overwrite", "This file already exists.\n" if (entry.Exists()) {
"Are you sure would you like to overwrite it?", BAlert *overwriteAlert = new BAlert("overwrite", "This file "
"Cancel", "Overwrite", NULL, "already exists.\n Are you sure would you like to overwrite "
B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_WARNING_ALERT); "it?", "Cancel", "Overwrite", NULL, B_WIDTH_AS_USUAL,
B_EVEN_SPACING, B_WARNING_ALERT);
overwriteAlert->SetShortcut(0, B_ESCAPE); overwriteAlert->SetShortcut(0, B_ESCAPE);
int32 buttonIndex = overwriteAlert->Go();
if (buttonIndex == 0) { if (overwriteAlert->Go() == 0)
return B_CANCELED; return B_CANCELED;
} }
} }
BFile file(&entry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY); BFile file(&entry, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (file.InitCheck() != B_OK) { if (file.InitCheck() != B_OK)
return B_ERROR; return B_ERROR;
}
BBitmapStream bitmapStream(fScreenshot); BBitmapStream bitmapStream(fScreenshot);
BTranslatorRoster* roster = BTranslatorRoster::Default(); BTranslatorRoster* roster = BTranslatorRoster::Default();
roster->Translate(&bitmapStream, NULL, NULL, &file, fImageFileType, roster->Translate(&bitmapStream, NULL, NULL, &file, fImageFileType,
@@ -795,36 +822,6 @@ ScreenshotWindow::_SaveScreenshot()
} }
void
ScreenshotWindow::_SaveScreenshotSilent() const
{
if (!fScreenshot)
return;
BPath homePath;
if (find_directory(B_USER_DIRECTORY, &homePath) != B_OK) {
fprintf(stderr, "failed to find user home folder\n");
return;
}
BPath path;
BEntry entry;
int32 index = 1;
do {
char filename[32];
sprintf(filename, "screenshot%ld.png", index++);
path = homePath;
path.Append(filename);
entry.SetTo(path.Path());
} while (entry.Exists());
// Dump to PNG
SaveToPNG(path.Path(), fScreenshot->Bounds(), fScreenshot->ColorSpace(),
fScreenshot->Bits(), fScreenshot->BitsLength(),
fScreenshot->BytesPerRow());
}
void void
ScreenshotWindow::_MakeTabSpaceTransparent(BRect* frame) ScreenshotWindow::_MakeTabSpaceTransparent(BRect* frame)
{ {
@@ -901,7 +898,7 @@ ScreenshotWindow::_MakeTabSpaceTransparent(BRect* frame)
BView view(fScreenshot->Bounds(), "bitmap", B_FOLLOW_ALL_SIDES, 0); BView view(fScreenshot->Bounds(), "bitmap", B_FOLLOW_ALL_SIDES, 0);
fScreenshot->AddChild(&view); fScreenshot->AddChild(&view);
if(view.Looper() && view.Looper()->Lock()) { if (view.Looper() && view.Looper()->Lock()) {
view.SetDrawingMode(B_OP_COPY); view.SetDrawingMode(B_OP_COPY);
view.SetHighColor(B_TRANSPARENT_32_BIT); view.SetHighColor(B_TRANSPARENT_32_BIT);
@@ -913,4 +910,3 @@ ScreenshotWindow::_MakeTabSpaceTransparent(BRect* frame)
} }
fScreenshot->RemoveChild(&view); fScreenshot->RemoveChild(&view);
} }
+19 -7
View File
@@ -1,14 +1,21 @@
/* /*
* Copyright Karsten Heimrich, host.haiku@gmx.de. All rights reserved. * Copyright Karsten Heimrich, host.haiku@gmx.de. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
* Authors:
* Karsten Heimrich
* Fredrik Modéen
*/ */
#ifndef SCREENSHOT_WINDOW_H
#define SCREENSHOT_WINDOW_H
#include <String.h> #include <String.h>
#include <Window.h> #include <Window.h>
#include <TranslatorFormats.h>
#include "PreviewView.h"
class BBitmap; class BBitmap;
class BBox;
class BButton; class BButton;
class BCardLayout; class BCardLayout;
class BCheckBox; class BCheckBox;
@@ -17,6 +24,8 @@ class BMenu;
class BRadioButton; class BRadioButton;
class BTextControl; class BTextControl;
class BTextView; class BTextView;
class BPath;
class PreviewView;
class ScreenshotWindow : public BWindow { class ScreenshotWindow : public BWindow {
@@ -26,13 +35,16 @@ public:
bool includeMouse = false, bool includeMouse = false,
bool grabActiveWindow = false, bool grabActiveWindow = false,
bool showConfigWindow = false, bool showConfigWindow = false,
bool saveScreenshotSilent = false); bool saveScreenshotSilent = false,
int32 imageFileType = B_PNG_FORMAT,
int32 translator = 8);
virtual ~ScreenshotWindow(); virtual ~ScreenshotWindow();
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
private: private:
void _InitWindow(); void _InitWindow();
BPath _GetDirectory();
void _SetupFirstLayoutItem(BCardLayout* layout); void _SetupFirstLayoutItem(BCardLayout* layout);
void _SetupSecondLayoutItem(BCardLayout* layout); void _SetupSecondLayoutItem(BCardLayout* layout);
void _DisallowChar(BTextView* textView); void _DisallowChar(BTextView* textView);
@@ -42,10 +54,9 @@ private:
const BMessage& settings); const BMessage& settings);
void _AddItemToPathMenu(const char* path, void _AddItemToPathMenu(const char* path,
BString& label, int32 index, bool markItem); BString& label, int32 index, bool markItem);
void _CenterAndShow();
void _UpdatePreviewPanel(); void _UpdatePreviewPanel();
BString _FindValidFileName(const char* name); const char* _FindValidFileName(const char* name);
int32 _PathIndexInMenu(const BString& path) const; int32 _PathIndexInMenu(const BString& path) const;
BMessage _ReadSettings() const; BMessage _ReadSettings() const;
@@ -56,9 +67,7 @@ private:
void _MakeTabSpaceTransparent(BRect* frame); void _MakeTabSpaceTransparent(BRect* frame);
status_t _SaveScreenshot(); status_t _SaveScreenshot();
void _SaveScreenshotSilent() const;
private:
PreviewView* fPreview; PreviewView* fPreview;
BRadioButton* fActiveWindow; BRadioButton* fActiveWindow;
BRadioButton* fWholeDesktop; BRadioButton* fWholeDesktop;
@@ -81,8 +90,11 @@ private:
bool fIncludeMouse; bool fIncludeMouse;
bool fGrabActiveWindow; bool fGrabActiveWindow;
bool fShowConfigWindow; bool fShowConfigWindow;
bool fSaveScreenshotSilent;
BString fExtension; BString fExtension;
int32 fTranslator; int32 fTranslator;
int32 fImageFileType; int32 fImageFileType;
}; };
#endif /* SCREENSHOT_WINDOW_H */