#include <wx/filename.h>
#include "autocapture.h"
+#ifdef __WXMAC__
+#include <cstring>
+#endif
+
// ----------------------------------------------------------------------------
// AutoCaptureMechanism
// ----------------------------------------------------------------------------
/* 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.
#ifdef __WXMAC__
// 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;
+ if(delay) Delay(delay);
+
do
{
fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG);
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
// Create a DC for the whole screen area
}
/* static */
-wxBitmap AutoCaptureMechanism::Capture(wxRect rect)
+wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay)
{
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()
ctrl.name, ctrl.name);
choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
- if (choice == wxYES)
- {
- using std::clock;
- using std::clock_t;
- // Wait for 3 seconds
- clock_t start = clock();
- while (clock() - start < CLOCKS_PER_SEC * 3)
- wxYieldIfNeeded();
- }
+ #ifndef __WXMAC__ //not __WXMAC__
+ if (choice == wxYES) Delay(3);
+ #endif
}
wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
void CaptureAll();
// take a screenshot only of the given rect
- static wxBitmap Capture(wxRect rect);
- static wxBitmap Capture(int x, int y, int width, int height);
+ // delay is only useful for Mac, for fixing a delay bug
+ 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
{
Control() {}
void Save(wxBitmap screenshot, wxString fileName);
-private:
typedef std::vector<Control> ControlList;
ControlList m_controlList;