patch by Utensil Candel to improve wxMac autocapture code

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56264 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Francesco Montorsi
2008-10-13 08:32:32 +00:00
parent 010df3801c
commit 101adcd54c
2 changed files with 38 additions and 17 deletions

View File

@@ -21,23 +21,45 @@
#include <wx/filename.h> #include <wx/filename.h>
#include "autocapture.h" #include "autocapture.h"
#ifdef __WXMAC__
#include <cstring>
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// AutoCaptureMechanism // AutoCaptureMechanism
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* static */ /* static */
wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height) void AutoCaptureMechanism::Delay(int seconds)
{
using std::clock;
using std::clock_t;
// Wait for 3 seconds
clock_t start = clock();
while (clock() - start < CLOCKS_PER_SEC * seconds)
wxYieldIfNeeded();
}
/* static */
wxBitmap AutoCaptureMechanism::Capture(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__
// wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC); // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
system("screencapture -x /tmp/wx_screen_capture.png"); char captureCommand[80] =""; // a reasonable max size is 80
sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png");
system(captureCommand);
wxBitmap fullscreen; wxBitmap fullscreen;
if(delay) Delay(delay);
do do
{ {
fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG); fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG);
@@ -46,6 +68,9 @@ wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height)
wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height)); wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height));
// to prevent loading the old screenshot next time
system("rm /tmp/wx_screen_capture.png");
#else // Under other paltforms, take a real screenshot #else // Under other paltforms, take a real screenshot
// Create a DC for the whole screen area // Create a DC for the whole screen area
@@ -83,10 +108,10 @@ wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height)
} }
/* static */ /* static */
wxBitmap AutoCaptureMechanism::Capture(wxRect rect) wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay)
{ {
wxPoint origin = rect.GetPosition(); wxPoint origin = rect.GetPosition();
return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight()); return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay);
} }
void AutoCaptureMechanism::CaptureAll() void AutoCaptureMechanism::CaptureAll()
@@ -137,16 +162,10 @@ wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
ctrl.name, ctrl.name); ctrl.name, ctrl.name);
choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook); choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
if (choice == wxYES)
{
using std::clock;
using std::clock_t;
// Wait for 3 seconds #ifndef __WXMAC__ //not __WXMAC__
clock_t start = clock(); if (choice == wxYES) Delay(3);
while (clock() - start < CLOCKS_PER_SEC * 3) #endif
wxYieldIfNeeded();
}
} }
wxRect rect = GetRect(ctrl.ctrl, ctrl.flag); wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);

View File

@@ -65,11 +65,14 @@ public:
void CaptureAll(); void CaptureAll();
// take a screenshot only of the given rect // take a screenshot only of the given rect
static wxBitmap Capture(wxRect rect); // delay is only useful for Mac, for fixing a delay bug
static wxBitmap Capture(int x, int y, int width, int height); static wxBitmap Capture(wxRect rect, int delay = 0);
static wxBitmap Capture(int x, int y, int width, int height, int delay = 0);
static void Delay(int seconds);
protected: // internal utils private: // internal utils
struct Control struct Control
{ {
Control() {} Control() {}
@@ -95,7 +98,6 @@ protected: // internal utils
void Save(wxBitmap screenshot, wxString fileName); void Save(wxBitmap screenshot, wxString fileName);
private:
typedef std::vector<Control> ControlList; typedef std::vector<Control> ControlList;
ControlList m_controlList; ControlList m_controlList;