make Capture() functions static members of AutoCaptureMechanism; adjusted slightly question boxes to fit the default wxGTK width of wxMessageBox

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56123 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Francesco Montorsi
2008-10-06 14:48:48 +00:00
parent 60a2264d5d
commit 6cd1aa9d38
5 changed files with 104 additions and 105 deletions

View File

@@ -19,11 +19,15 @@
#endif #endif
#include <wx/filename.h> #include <wx/filename.h>
#include "autocapture.h" #include "autocapture.h"
wxBitmap Capture(int x, int y, int width, int height) // ----------------------------------------------------------------------------
// AutoCaptureMechanism
// ----------------------------------------------------------------------------
/* static */
wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height)
{ {
// 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__
@@ -54,9 +58,11 @@ wxBitmap Capture(int x, int y, int width, int height)
// 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;
// Tell the memory DC to use our Bitmap // Tell the memory DC to use our Bitmap
// all drawing action on the memory DC will go to the Bitmap now // all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(screenshot); 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
memDC.Blit( 0, // Copy to this X coordinate memDC.Blit( 0, // Copy to this X coordinate
@@ -67,30 +73,25 @@ wxBitmap Capture(int x, int y, int width, int height)
x, // What's the X offset in the original DC? x, // What's the X offset in the original DC?
y // What's the Y offset in the original DC? y // What's the Y offset in the original DC?
); );
// Select the Bitmap out of the memory DC by selecting a new // Select the Bitmap out of the memory DC by selecting a new
// uninitialized Bitmap // uninitialized Bitmap
memDC.SelectObject(wxNullBitmap); memDC.SelectObject(wxNullBitmap);
#endif // #ifdef __WXMAC__ #endif // #ifdef __WXMAC__
// wxMessageBox(_(""),_(""));
return screenshot; return screenshot;
} }
wxBitmap Capture(wxRect rect) /* static */
wxBitmap AutoCaptureMechanism::Capture(wxRect rect)
{ {
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());
} }
// ----------------------------------------------------------------------------
// AutoCaptureMechanism
// ----------------------------------------------------------------------------
void AutoCaptureMechanism::CaptureAll() void AutoCaptureMechanism::CaptureAll()
{ {
// start from the first page
m_notebook->SetSelection(0); m_notebook->SetSelection(0);
wxYield(); wxYield();
@@ -107,53 +108,51 @@ void AutoCaptureMechanism::CaptureAll()
continue; continue;
} }
// create the screenshot
wxBitmap screenshot = Capture(ctrl); wxBitmap screenshot = Capture(ctrl);
if (ctrl.flag & AJ_Union) if (ctrl.flag & AJ_Union)
{
screenshot = Union(screenshot, Capture(*(++it))); screenshot = Union(screenshot, Capture(*(++it)));
}
// and save it
Save(screenshot, ctrl.name); Save(screenshot, ctrl.name);
} }
} }
wxBitmap AutoCaptureMechanism::Capture(Control& ctrl) wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
{ {
if(ctrl.name == wxT("")) //no mannual specification for the control name if (ctrl.name == wxT("")) // no manual specification for the control name
{ {
//Get name from wxRTTI // Get its name from wxRTTI
ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName(); ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName();
} }
int choice = wxNO; int choice = wxNO;
// for drop-down controls we need the help of the user
if (ctrl.flag & AJ_Dropdown) if (ctrl.flag & AJ_Dropdown)
{ {
wxString caption = _("Do you wish to capture the dropdown list of ") + ctrl.name + _("?"); wxString caption = _("Drop-down screenshot...");
wxString notice = _("Click YES to capture it.\nAnd you MUST drop down the ") + ctrl.name + _(" in 3 seconds after close me.\n"); wxString msg =
notice += _("Click NO otherwise."); wxString::Format(_("Do you wish to capture the drop-down list of '%s' ?\n\nIf you click YES you must drop-down the list of '%s' in 3 seconds after closing this message box.\nIf you click NO the screenshot for this control won't contain its drop-down list."),
ctrl.name, ctrl.name);
choice = wxMessageBox(notice, caption, wxYES_NO, m_notebook);
choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
if (choice == wxYES) if (choice == wxYES)
{ {
//Wait for 3 seconds
using std::clock; using std::clock;
using std::clock_t; using std::clock_t;
// Wait for 3 seconds
clock_t start = clock(); clock_t start = clock();
while (clock() - start < CLOCKS_PER_SEC * 3) while (clock() - start < CLOCKS_PER_SEC * 3)
{
wxYieldIfNeeded(); wxYieldIfNeeded();
} }
} }
}
wxRect rect = GetRect(ctrl.ctrl, ctrl.flag); wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
//Do some rect adjust so it can include the dropdown list // Do some rect adjust so it can include the dropdown list;
//Currently it only works well under MSW, not adjusted for Linux and Mac OS // currently this only works well under MSW; not adjusted for Linux and Mac OS
if (ctrl.flag & AJ_Dropdown && choice == wxYES) if (ctrl.flag & AJ_Dropdown && choice == wxYES)
{ {
// #ifdef __WXMSW__ // #ifdef __WXMSW__
@@ -162,27 +161,20 @@ wxBitmap AutoCaptureMechanism::Capture(Control & ctrl)
// #endif // #endif
} }
//cut off "wx" and change them into lowercase. // cut off "wx" and change the name into lowercase.
// e.g. wxButton will have a name of "button" at the end // e.g. wxButton will have a name of "button" at the end
ctrl.name.StartsWith(_T("wx"), &(ctrl.name)); ctrl.name.StartsWith(_T("wx"), &(ctrl.name));
ctrl.name.MakeLower(); ctrl.name.MakeLower();
wxBitmap screenshot = ::Capture(rect); // take the screenshot
wxBitmap screenshot = Capture(rect);
if (ctrl.flag & AJ_RegionAdjust) if (ctrl.flag & AJ_RegionAdjust)
{
PutBack(ctrl.ctrl); PutBack(ctrl.ctrl);
}
return screenshot; return screenshot;
} }
//if AJ_RegionAdjust is specified, the following line will use the label trick to adjust
//the region position and size
wxRect GetRect(wxWindow* ctrl, int flag);
//put the control back after the label trick(Using reparent/resizer approach)
void PutBack(wxWindow * ctrl);
wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2) wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2)
{ {
int w1, w2, h1, h2, w, h; int w1, w2, h1, h2, w, h;
@@ -226,18 +218,18 @@ wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2)
void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName) void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
{ {
//Check if m_defaultDir already existed // make sure m_dir exists
if (!wxDirExists(m_dir)) if (!wxDirExists(m_dir))
wxMkdir(m_dir); wxMkdir(m_dir);
wxString fullFileName = m_dir + wxFileName::GetPathSeparator() + fileName; wxFileName fullFileName(m_dir, fileName + ".png");
//to prvent overwritten // do not overwrite already existing files with this name
while(wxFileName::FileExists(fullFileName + _T(".png"))) fullFileName += _T("_"); while (fullFileName.FileExists())
fullFileName.SetName(fullFileName.GetName() + "_");
//Our Bitmap now has the screenshot, so let's save it as an png // save the screenshot as a PNG
//The filename itself is without extension. screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
screenshot.SaveFile(fullFileName + _T(".png"), wxBITMAP_TYPE_PNG);
} }
wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag) wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)

View File

@@ -14,10 +14,8 @@
#include <vector> #include <vector>
#include <ctime> #include <ctime>
// Global helper functions. to take screenshot for a rect region
wxBitmap Capture(wxRect rect);
wxBitmap Capture(int x, int y, int width, int height);
// TODO: document what these flags mean
enum AdjustFlags enum AdjustFlags
{ {
AJ_Normal = 0, AJ_Normal = 0,
@@ -63,8 +61,14 @@ public:
m_controlList.push_back(Control(0, wxT(""), AJ_TurnPage)); m_controlList.push_back(Control(0, wxT(""), AJ_TurnPage));
} }
// capture all controls of the associated notebook
void CaptureAll(); 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);
protected: // internal utils protected: // internal utils
struct Control struct Control
{ {
@@ -80,8 +84,8 @@ protected: // internal utils
wxBitmap Capture(Control & ctrl); wxBitmap Capture(Control & ctrl);
//if AJ_RegionAdjust is specified, the following line will use the label trick to adjust // if AJ_RegionAdjust is specified, the following line will use the label
//the region position and size // trick to adjust the region position and size
wxRect GetRect(wxWindow* ctrl, int flag); wxRect GetRect(wxWindow* ctrl, int flag);
// put the control back after the label trick(Using reparent/resizer approach) // put the control back after the label trick(Using reparent/resizer approach)

View File

@@ -238,7 +238,7 @@
</object> </object>
</object> </object>
</object> </object>
<object class="wxBoxSizer" expanded="0"> <object class="wxBoxSizer" expanded="1">
<property name="minimum_size"></property> <property name="minimum_size"></property>
<property name="name">bSizer0</property> <property name="name">bSizer0</property>
<property name="orient">wxVERTICAL</property> <property name="orient">wxVERTICAL</property>
@@ -1348,7 +1348,7 @@
<object class="notebookpage" expanded="1"> <object class="notebookpage" expanded="1">
<property name="bitmap"></property> <property name="bitmap"></property>
<property name="label">Multiple choice Controls</property> <property name="label">Multiple choice Controls</property>
<property name="select">0</property> <property name="select">1</property>
<object class="wxPanel" expanded="1"> <object class="wxPanel" expanded="1">
<property name="bg"></property> <property name="bg"></property>
<property name="context_help"></property> <property name="context_help"></property>
@@ -1638,7 +1638,7 @@
<property name="tooltip"></property> <property name="tooltip"></property>
<property name="window_extra_style"></property> <property name="window_extra_style"></property>
<property name="window_name"></property> <property name="window_name"></property>
<property name="window_style"></property> <property name="window_style">wxSUNKEN_BORDER</property>
<event name="OnChar"></event> <event name="OnChar"></event>
<event name="OnEnterWindow"></event> <event name="OnEnterWindow"></event>
<event name="OnEraseBackground"></event> <event name="OnEraseBackground"></event>
@@ -1708,7 +1708,7 @@
<property name="tooltip">wxListCtrl</property> <property name="tooltip">wxListCtrl</property>
<property name="window_extra_style"></property> <property name="window_extra_style"></property>
<property name="window_name"></property> <property name="window_name"></property>
<property name="window_style"></property> <property name="window_style">wxSUNKEN_BORDER</property>
<event name="OnChar"></event> <event name="OnChar"></event>
<event name="OnEnterWindow"></event> <event name="OnEnterWindow"></event>
<event name="OnEraseBackground"></event> <event name="OnEraseBackground"></event>
@@ -2155,7 +2155,7 @@
<object class="notebookpage" expanded="1"> <object class="notebookpage" expanded="1">
<property name="bitmap"></property> <property name="bitmap"></property>
<property name="label">Picker Controls</property> <property name="label">Picker Controls</property>
<property name="select">1</property> <property name="select">0</property>
<object class="wxPanel" expanded="1"> <object class="wxPanel" expanded="1">
<property name="bg"></property> <property name="bg"></property>
<property name="context_help"></property> <property name="context_help"></property>

View File

@@ -227,10 +227,10 @@ GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString& title, cons
fgSizer2->Add( m_staticBox1, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 20 ); fgSizer2->Add( m_staticBox1, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 20 );
m_treeCtrl1 = new wxTreeCtrl( m_panel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_DEFAULT_STYLE ); m_treeCtrl1 = new wxTreeCtrl( m_panel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_DEFAULT_STYLE|wxSUNKEN_BORDER );
fgSizer2->Add( m_treeCtrl1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 20 ); fgSizer2->Add( m_treeCtrl1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 20 );
m_listCtrl1 = new wxListCtrl( m_panel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ); m_listCtrl1 = new wxListCtrl( m_panel2, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0|wxSUNKEN_BORDER );
m_listCtrl1->SetToolTip( _("wxListCtrl") ); m_listCtrl1->SetToolTip( _("wxListCtrl") );
fgSizer2->Add( m_listCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 20 ); fgSizer2->Add( m_listCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 20 );
@@ -274,7 +274,7 @@ GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString& title, cons
m_panel2->SetSizer( fgSizer2 ); m_panel2->SetSizer( fgSizer2 );
m_panel2->Layout(); m_panel2->Layout();
fgSizer2->Fit( m_panel2 ); fgSizer2->Fit( m_panel2 );
m_notebook1->AddPage( m_panel2, _("Multiple choice Controls"), false ); m_notebook1->AddPage( m_panel2, _("Multiple choice Controls"), true );
m_panel3 = new wxPanel( m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL ); m_panel3 = new wxPanel( m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
wxBoxSizer* bSizer2; wxBoxSizer* bSizer2;
bSizer2 = new wxBoxSizer( wxVERTICAL ); bSizer2 = new wxBoxSizer( wxVERTICAL );
@@ -353,7 +353,7 @@ GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString& title, cons
m_panel4->SetSizer( fgSizer5 ); m_panel4->SetSizer( fgSizer5 );
m_panel4->Layout(); m_panel4->Layout();
fgSizer5->Fit( m_panel4 ); fgSizer5->Fit( m_panel4 );
m_notebook1->AddPage( m_panel4, _("Picker Controls"), true ); m_notebook1->AddPage( m_panel4, _("Picker Controls"), false );
m_panel5 = new wxPanel( m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL ); m_panel5 = new wxPanel( m_notebook1, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
wxFlexGridSizer* fgSizer4; wxFlexGridSizer* fgSizer4;
fgSizer4 = new wxFlexGridSizer( 2, 2, 0, 0 ); fgSizer4 = new wxFlexGridSizer( 2, 2, 0, 0 );

View File

@@ -80,6 +80,9 @@ ScreenshotFrame::~ScreenshotFrame()
*/ */
void ScreenshotFrame::InitFBControls() void ScreenshotFrame::InitFBControls()
{ {
// For some reason, wxFormBuilder does not set the scrollbar range
m_scrollBar1->SetScrollbar(50, 1, 100, 1);
// Do the default selection for wxComboBox // Do the default selection for wxComboBox
m_comboBox1->Select(0); m_comboBox1->Select(0);
@@ -250,7 +253,7 @@ void ScreenshotFrame::OnCaptureAllControls(wxCommandEvent& WXUNUSED(event))
// check if there are other screenshots taken before // check if there are other screenshots taken before
if (wxFileName::DirExists(dir)) if (wxFileName::DirExists(dir))
{ {
int choice = wxMessageBox(_("It seems that you have already generated some screenshots.\nClick YES to delete them all (recommended) or NO to preserve them.\nClick CANCEL to cancel this auto-capture operation."), int choice = wxMessageBox(_("It seems that you have already generated some screenshots.\n\nClick YES to delete them all (recommended) or NO to preserve them.\nClick CANCEL to cancel this auto-capture operation."),
_("Delete existing screenshots?"), _("Delete existing screenshots?"),
wxYES_NO|wxCANCEL|wxICON_QUESTION, this); wxYES_NO|wxCANCEL|wxICON_QUESTION, this);
switch(choice) switch(choice)