Removed dependancy on wx/bitmap.h from autocapture.h.

Fixed issue with Union() improperly generating a combined bitmap from two source bitmaps (only shown on Mac).
Replaced old wxWidgets logo with the new one.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60620 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Bryan Petty
2009-05-13 18:30:06 +00:00
parent b5d9d763f7
commit 5f1b5e83c4
4 changed files with 77 additions and 60 deletions

View File

@@ -13,15 +13,17 @@
#pragma hdrstop #pragma hdrstop
#endif #endif
// for all others, include the necessary headers wxWidgets headers) #include "autocapture.h"
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include "wx/wx.h"
#endif #endif
#include <ctime> #include "wx/bitmap.h"
#include <wx/notebook.h> #include "wx/filename.h"
#include "wx/notebook.h"
#include "autocapture.h" #include <ctime>
#ifdef __WXMAC__ #ifdef __WXMAC__
#include <cstring> #include <cstring>
@@ -32,6 +34,15 @@
// AutoCaptureMechanism // AutoCaptureMechanism
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
AutoCaptureMechanism::AutoCaptureMechanism(wxNotebook *notebook,
int flag, int margin)
: m_notebook(notebook),
m_flag(flag),
m_margin(margin),
m_grid(NULL)
{
}
/* static */ /* static */
wxString AutoCaptureMechanism::default_dir = _T("screenshots"); wxString AutoCaptureMechanism::default_dir = _T("screenshots");
@@ -46,7 +57,7 @@ wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath()
/* static */ /* static */
void AutoCaptureMechanism::Delay(int seconds) void AutoCaptureMechanism::Delay(int seconds)
{ {
// TODO: Switch this to use wxTimer. // TODO: Switch this to use wxTimer.
// Wait for 3 seconds // Wait for 3 seconds
clock_t start = clock(); clock_t start = clock();
@@ -55,7 +66,8 @@ void AutoCaptureMechanism::Delay(int seconds)
} }
/* static */ /* static */
wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int delay) bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, int x, int y,
int width, int height, int delay)
{ {
// Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick. // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
#ifdef __WXMAC__ #ifdef __WXMAC__
@@ -63,26 +75,25 @@ wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int
// wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC); // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
char captureCommand[80] =""; // a reasonable max size is 80 char captureCommand[80] =""; // a reasonable max size is 80
sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png"); sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png");
system(captureCommand); system(captureCommand);
wxBitmap fullscreen;
if(delay) Delay(delay); if(delay) Delay(delay);
wxBitmap fullscreen;
do do
{ {
fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG); fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG);
} }
while(!fullscreen.IsOk()); while(!fullscreen.IsOk());
wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height)); bitmap = fullscreen.GetSubBitmap(wxRect(x,y,width,height));
// to prevent loading the old screenshot next time // to prevent loading the old screenshot next time
system("rm /tmp/wx_screen_capture.png"); system("rm /tmp/wx_screen_capture.png");
return true;
#else // Under other paltforms, take a real screenshot #else // Under other paltforms, take a real screenshot
if(delay) Delay(delay); if(delay) Delay(delay);
@@ -90,17 +101,12 @@ wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int
// Create a DC for the whole screen area // Create a DC for the whole screen area
wxScreenDC dcScreen; wxScreenDC dcScreen;
// Create a Bitmap that will later on hold the screenshot image bitmap->Create(width, height);
// Note that the Bitmap must have a size big enough to hold the screenshot
// -1 means using the current default colour depth
wxBitmap screenshot(width, height, -1);
// Create a memory DC that will be used for actually taking the screenshot // Create a memory DC that will be used for actually taking the screenshot
wxMemoryDC memDC; wxMemoryDC memDC;
memDC.SelectObject((*bitmap));
// Tell the memory DC to use our Bitmap memDC.Clear();
// all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(screenshot);
// Blit (in this case copy) the actual screen on the memory DC // Blit (in this case copy) the actual screen on the memory DC
// and thus the Bitmap // and thus the Bitmap
@@ -118,18 +124,18 @@ wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int
memDC.SelectObject(wxNullBitmap); memDC.SelectObject(wxNullBitmap);
#endif // #ifdef __WXMAC__ #endif // #ifdef __WXMAC__
return screenshot; return true;
} }
/* static */ /* static */
wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay) bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, wxRect rect, int delay)
{ {
wxPoint origin = rect.GetPosition(); wxPoint origin = rect.GetPosition();
return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay); return Capture(bitmap, origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay);
} }
/* static */ /* static */
void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName) void AutoCaptureMechanism::Save(wxBitmap* screenshot, const wxString& fileName)
{ {
// make sure default_dir exists // make sure default_dir exists
if (!wxDirExists(default_dir)) if (!wxDirExists(default_dir))
@@ -142,7 +148,7 @@ void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
fullFileName.SetName(fullFileName.GetName() + "_"); fullFileName.SetName(fullFileName.GetName() + "_");
// save the screenshot as a PNG // save the screenshot as a PNG
screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG); screenshot->SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
} }
void AutoCaptureMechanism::CaptureAll() void AutoCaptureMechanism::CaptureAll()
@@ -165,7 +171,8 @@ void AutoCaptureMechanism::CaptureAll()
} }
// create the screenshot // create the screenshot
wxBitmap screenshot = Capture(ctrl); wxBitmap screenshot(0, 0);
Capture(&screenshot, ctrl);
if(ctrl.flag & AJ_Union) if(ctrl.flag & AJ_Union)
{ {
@@ -174,17 +181,21 @@ void AutoCaptureMechanism::CaptureAll()
{ {
++it; ++it;
it->name = ctrl.name; //preserving the name it->name = ctrl.name; //preserving the name
screenshot = Union(screenshot, Capture(*it)); wxBitmap screenshot2(0, 0);
Capture(&screenshot2, *it);
wxBitmap combined(0, 0);
Union(&screenshot, &screenshot2, &combined);
screenshot = combined;
} }
while(!(it->flag & AJ_UnionEnd)); while(!(it->flag & AJ_UnionEnd));
} }
// and save it // and save it
Save(screenshot, ctrl.name); Save(&screenshot, ctrl.name);
} }
} }
wxBitmap AutoCaptureMechanism::Capture(Control& ctrl) bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, Control& ctrl)
{ {
// no manual specification for the control name // no manual specification for the control name
// or name adjustment is disabled globally // or name adjustment is disabled globally
@@ -227,43 +238,43 @@ wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
ctrl.name.MakeLower(); ctrl.name.MakeLower();
// take the screenshot // take the screenshot
wxBitmap screenshot = Capture(rect, (choice == wxYES)?5:0); Capture(bitmap, rect, (choice == wxYES)?5:0);
if (choice == wxYES) ctrl.ctrl->SetCursor(wxNullCursor); if (choice == wxYES) ctrl.ctrl->SetCursor(wxNullCursor);
if (ctrl.flag & AJ_RegionAdjust) if (ctrl.flag & AJ_RegionAdjust)
PutBack(ctrl.ctrl); PutBack(ctrl.ctrl);
return screenshot; return true;
} }
wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2) /* static */
bool AutoCaptureMechanism::Union(wxBitmap* top, wxBitmap* bottom, wxBitmap* result)
{ {
int w1, w2, h1, h2, w, h; int w1, w2, h1, h2, w, h;
w1 = pic1.GetWidth(); w1 = top->GetWidth();
w2 = pic2.GetWidth(); w2 = bottom->GetWidth();
h1 = pic1.GetHeight(); h1 = top->GetHeight();
h2 = pic2.GetHeight(); h2 = bottom->GetHeight();
const int gap_between = 20; const int gap_between = 20;
w = (w1 >= w2) ? w1 : w2; w = (w1 >= w2) ? w1 : w2;
h = h1 + h2 + gap_between; h = h1 + h2 + gap_between;
wxBitmap result(w, h, -1); result->Create(w, h);
wxMemoryDC dstDC; wxMemoryDC dstDC;
dstDC.SelectObject(result); dstDC.SelectObject((*result));
dstDC.SetPen(*wxTRANSPARENT_PEN);
dstDC.SetBrush(*wxWHITE_BRUSH); dstDC.SetBrush(*wxWHITE_BRUSH);
dstDC.DrawRectangle(-1, -1, w + 1, h + 1); dstDC.Clear();
dstDC.DrawBitmap(pic1, 0, 0, false); dstDC.DrawBitmap((*top), 0, 0);
dstDC.DrawBitmap(pic2, 0, h1 + gap_between, false); dstDC.DrawBitmap((*bottom), 0, h1 + gap_between);
dstDC.SelectObject(wxNullBitmap); dstDC.SelectObject(wxNullBitmap);
return result; return true;
} }
wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag) wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)

View File

@@ -10,8 +10,12 @@
#define _AUTOCAPTURE_H_ #define _AUTOCAPTURE_H_
#include <vector> #include <vector>
#include "wx/filename.h"
#include "wx/gdicmn.h"
class wxBitmap;
class wxFlexGridSizer;
class wxWindow;
class wxNotebook; class wxNotebook;
/** /**
@@ -210,11 +214,9 @@ public:
*/ */
AutoCaptureMechanism(wxNotebook *notebook, AutoCaptureMechanism(wxNotebook *notebook,
int flag = AJ_NormalAll, int flag = AJ_NormalAll,
int margin = 5) int margin = 5);
: m_notebook(notebook), m_flag(flag),
m_margin(margin), m_grid(NULL) {}
~AutoCaptureMechanism(){} ~AutoCaptureMechanism() { }
/** /**
Register a control and perform specifid auto adjustments. Register a control and perform specifid auto adjustments.
@@ -277,31 +279,34 @@ public:
/** /**
Take a screenshot for the given region. Take a screenshot for the given region.
@param rect is the given rectangular region. @param bitmap
Bitmap to save the screenshot to.
@param delay is only useful for Mac, for fixing a delay bug. It seems that it didn't @param rect
fix the bug, so it might be removed soon. Given rectangular region.
@param delay
Only useful for Mac, for fixing a delay bug. It seems that it
didn't fix the bug, so it might be removed soon.
*/ */
static wxBitmap Capture(wxRect rect, int delay = 0); static bool Capture(wxBitmap* bitmap, wxRect rect, int delay = 0);
/** /**
Take a screenshot for the given region. Take a screenshot for the given region.
@see Capture(wxRect rect, int delay) @see Capture(wxBitmap*,wxRect,int)
*/ */
static wxBitmap Capture(int x, int y, int width, int height, int delay = 0); static bool Capture(wxBitmap* bitmap, int x, int y, int width, int height, int delay = 0);
/** /**
Save the screenshot as the name of @a fileName in the default directory. Save the screenshot as the name of @a fileName in the default directory.
@a fileName should be without ".png". @a fileName should be without ".png".
*/ */
static void Save(wxBitmap screenshot, wxString fileName); static void Save(wxBitmap* screenshot, const wxString& fileName);
/** /**
Set the default directory where the screenshots will be generated. Set the default directory where the screenshots will be generated.
*/ */
static void SetDefaultDirectory(wxString dir) { default_dir = dir; } static void SetDefaultDirectory(const wxString& dir) { default_dir = dir; }
/** /**
Get the default directory where the screenshots will be generated. Get the default directory where the screenshots will be generated.
@@ -344,7 +349,7 @@ private:
/* /*
Capture and auto adjust the control. Used by CaptureAll(). Capture and auto adjust the control. Used by CaptureAll().
*/ */
wxBitmap Capture(Control & ctrl); bool Capture(wxBitmap* bitmap, Control& ctrl);
/* /*
Get the correct rectangular region that the control occupies. Used by Get the correct rectangular region that the control occupies. Used by
@@ -384,7 +389,7 @@ private:
The gap is 20 pixels by default. Currently it isn't configurable. The gap is 20 pixels by default. Currently it isn't configurable.
*/ */
static wxBitmap Union(wxBitmap pic1, wxBitmap pic2); static bool Union(wxBitmap* top, wxBitmap* bottom, wxBitmap* result);
/* /*
Delay a few seconds without blocking GUI. Delay a few seconds without blocking GUI.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -82,9 +82,10 @@ void ScreenshotFrame::OnCaptureFullScreen(wxCommandEvent& WXUNUSED(event))
wxCoord screenWidth, screenHeight; wxCoord screenWidth, screenHeight;
dcScreen.GetSize(&screenWidth, &screenHeight); dcScreen.GetSize(&screenWidth, &screenHeight);
wxBitmap fullscreen = AutoCaptureMechanism::Capture(0, 0, screenWidth, screenHeight); wxBitmap fullscreen(0, 0);
AutoCaptureMechanism::Capture(&fullscreen, 0, 0, screenWidth, screenHeight);
AutoCaptureMechanism::Save(fullscreen, _T("fullscreen")); AutoCaptureMechanism::Save(&fullscreen, _T("fullscreen"));
wxMessageBox(_("A screenshot of the entire screen was saved as:\n\n ") wxMessageBox(_("A screenshot of the entire screen was saved as:\n\n ")
+ AutoCaptureMechanism::GetDefaultDirectoryAbsPath() + _T("fullscreen.png"), + AutoCaptureMechanism::GetDefaultDirectoryAbsPath() + _T("fullscreen.png"),