]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/screenshotgen/src/autocapture.cpp
routing native events first to the wx class and only if skipped call native handler
[wxWidgets.git] / utils / screenshotgen / src / autocapture.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: autocapture.cpp
3// Purpose: Implement wxCtrlMaskOut class
4// Author: Utensil Candel (UtensilCandel@@gmail.com)
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9// For compilers that support precompilation, includes "wx/wx.h".
10#include "wx/wxprec.h"
11
12#ifdef __BORLANDC__
13#pragma hdrstop
14#endif
15
16// for all others, include the necessary headers wxWidgets headers)
17#ifndef WX_PRECOMP
18#include "wx/wx.h"
19#endif
20
21#include "wx/filename.h"
22
23#include "autocapture.h"
24
25#ifdef __WXMAC__
26#include <cstring>
27#endif
28
29
30// ----------------------------------------------------------------------------
31// AutoCaptureMechanism
32// ----------------------------------------------------------------------------
33
34/* static */
35void AutoCaptureMechanism::Delay(int seconds)
36{
37 // TODO: Switch this to use wxTimer.
38
39 // Wait for 3 seconds
40 clock_t start = clock();
41 while (clock() - start < CLOCKS_PER_SEC * seconds)
42 wxYieldIfNeeded();
43}
44
45/* static */
46wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int delay)
47{
48 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
49#ifdef __WXMAC__
50
51 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
52
53 char captureCommand[80] =""; // a reasonable max size is 80
54
55 sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png");
56
57 system(captureCommand);
58
59 wxBitmap fullscreen;
60
61 if(delay) Delay(delay);
62
63 do
64 {
65 fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG);
66 }
67 while(!fullscreen.IsOk());
68
69 wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height));
70
71 // to prevent loading the old screenshot next time
72 system("rm /tmp/wx_screen_capture.png");
73
74#else // Under other paltforms, take a real screenshot
75
76 // Create a DC for the whole screen area
77 wxScreenDC dcScreen;
78
79 // Create a Bitmap that will later on hold the screenshot image
80 // Note that the Bitmap must have a size big enough to hold the screenshot
81 // -1 means using the current default colour depth
82 wxBitmap screenshot(width, height, -1);
83
84 // Create a memory DC that will be used for actually taking the screenshot
85 wxMemoryDC memDC;
86
87 // Tell the memory DC to use our Bitmap
88 // all drawing action on the memory DC will go to the Bitmap now
89 memDC.SelectObject(screenshot);
90
91 // Blit (in this case copy) the actual screen on the memory DC
92 // and thus the Bitmap
93 memDC.Blit( 0, // Copy to this X coordinate
94 0, // Copy to this Y coordinate
95 width, // Copy this width
96 height, // Copy this height
97 &dcScreen, // From where do we copy?
98 x, // What's the X offset in the original DC?
99 y // What's the Y offset in the original DC?
100 );
101
102 // Select the Bitmap out of the memory DC by selecting a new
103 // uninitialized Bitmap
104 memDC.SelectObject(wxNullBitmap);
105#endif // #ifdef __WXMAC__
106
107 return screenshot;
108}
109
110/* static */
111wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay)
112{
113 wxPoint origin = rect.GetPosition();
114 return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay);
115}
116
117void AutoCaptureMechanism::CaptureAll()
118{
119 // start from the first page
120 m_notebook->SetSelection(0);
121 wxYield();
122
123 for (ControlList::iterator it = m_controlList.begin();
124 it != m_controlList.end();
125 ++it)
126 {
127 Control &ctrl = *it;
128
129 if (ctrl.flag == AJ_TurnPage) // Turn to next page
130 {
131 m_notebook->SetSelection(m_notebook->GetSelection() + 1);
132 wxYield();
133 continue;
134 }
135
136 // create the screenshot
137 wxBitmap screenshot = Capture(ctrl);
138 if (ctrl.flag & AJ_Union)
139 screenshot = Union(screenshot, Capture(*(++it)));
140
141 // and save it
142 Save(screenshot, ctrl.name);
143 }
144}
145
146wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
147{
148 if (ctrl.name == wxT("")) // no manual specification for the control name
149 {
150 // Get its name from wxRTTI
151 ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName();
152 }
153
154 int choice = wxNO;
155
156 // for drop-down controls we need the help of the user
157 if (ctrl.flag & AJ_Dropdown)
158 {
159 wxString caption = _("Drop-down screenshot...");
160 wxString msg =
161 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."),
162 ctrl.name, ctrl.name);
163
164 choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
165
166 #ifndef __WXMAC__ //not __WXMAC__
167 if (choice == wxYES) Delay(3);
168 #endif
169 }
170
171 wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
172
173 // Do some rect adjust so it can include the dropdown list;
174 // currently this only works well under MSW; not adjusted for Linux and Mac OS
175 if (ctrl.flag & AJ_Dropdown && choice == wxYES)
176 {
177// #ifdef __WXMSW__
178 int h = rect.GetHeight();
179 rect.SetHeight(h * 4);
180// #endif
181 }
182
183 // cut off "wx" and change the name into lowercase.
184 // e.g. wxButton will have a name of "button" at the end
185 ctrl.name.StartsWith(_T("wx"), &(ctrl.name));
186 ctrl.name.MakeLower();
187
188 // take the screenshot
189 wxBitmap screenshot = Capture(rect);
190
191 if (ctrl.flag & AJ_RegionAdjust)
192 PutBack(ctrl.ctrl);
193
194 return screenshot;
195}
196
197wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2)
198{
199 int w1, w2, h1, h2, w, h;
200 w1 = pic1.GetWidth();
201 w2 = pic2.GetWidth();
202 h1 = pic1.GetHeight();
203 h2 = pic2.GetHeight();
204
205 const int gap_between = 20;
206
207 w = (w1 >= w2) ? w1 : w2;
208 h = h1 + h2 + gap_between;
209
210 wxBitmap result(w, h, -1);
211
212 wxMemoryDC dstDC;
213 dstDC.SelectObject(result);
214
215 dstDC.DrawBitmap(pic1, 0, 0, false);
216 dstDC.DrawBitmap(pic2, 0, h1 + gap_between, false);
217
218 dstDC.SelectObject(wxNullBitmap);
219
220 wxMemoryDC maskDC;
221 wxBitmap mask(w, h, 1);
222 maskDC.SelectObject(mask);
223
224 maskDC.SetPen(*wxTRANSPARENT_PEN);
225 maskDC.SetBrush(*wxBLACK_BRUSH);
226 maskDC.DrawRectangle(0, 0, w + 1, h + 1);
227
228 maskDC.SetBrush(*wxWHITE_BRUSH);
229 maskDC.DrawRectangle(0, 0, w1, h1);
230 maskDC.DrawRectangle(0, h1 + gap_between, w2, h2);
231 maskDC.SelectObject(wxNullBitmap);
232
233 result.SetMask(new wxMask(mask));
234
235 return result;
236}
237
238void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
239{
240 // make sure m_dir exists
241 if (!wxDirExists(m_dir))
242 wxMkdir(m_dir);
243
244 wxFileName fullFileName(m_dir, fileName + ".png");
245
246 // do not overwrite already existing files with this name
247 while (fullFileName.FileExists())
248 fullFileName.SetName(fullFileName.GetName() + "_");
249
250 // save the screenshot as a PNG
251 screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
252}
253
254wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)
255{
256 if (flag & AJ_RegionAdjust)
257 {
258 wxWindow * parent = ctrl->GetParent();
259 wxSizer * sizer = parent->GetSizer();
260
261 if (sizer)
262 {
263 sizer->Detach(ctrl);
264
265 /*
266 +---------+-----------+---------+
267 | 0 | label | 1 |
268 +---------+-----------+---------+
269 | label | ctrl | label |
270 +---------+-----------+---------+
271 | 2 | label | 3 |
272 +---------+-----------+---------+
273 */
274
275 m_grid = new wxFlexGridSizer(3, 3, m_border, m_border);
276
277 wxStaticText* l[4];
278
279 for (int i = 0; i < 4; ++i)
280 l[i] = new wxStaticText(parent, wxID_ANY, wxT(" "));
281
282 m_grid->Add(l[0]);
283 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
284 m_grid->Add(l[1]);
285 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
286 m_grid->Add(ctrl);
287 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
288 m_grid->Add(l[2]);
289 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
290 m_grid->Add(l[3]);
291
292 sizer->Add(m_grid);
293 parent->SetSizer(sizer);
294 parent->Layout();
295
296 parent->Refresh();
297 wxYield();
298
299 return wxRect(l[0]->GetScreenRect().GetBottomRight(),
300 l[3]->GetScreenRect().GetTopLeft());
301
302 }
303 else // Actually it won't get here working with the current guiframe.h/guiframe.cpp
304 {
305 return ctrl->GetScreenRect().Inflate(m_border);
306 }
307 }
308 else
309 {
310 return ctrl->GetScreenRect().Inflate(m_border);
311 }
312}
313
314void AutoCaptureMechanism::PutBack(wxWindow * ctrl)
315{
316 m_grid->Detach(ctrl);
317
318 wxSizerItemList children = m_grid->GetChildren();
319
320 for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it)
321 {
322 wxSizerItem* item = *it;
323 if (item->IsWindow()) delete (*it)->GetWindow();
324 }
325
326 wxSizer * sizer = ctrl->GetParent()->GetSizer();
327 sizer->Detach(m_grid);
328 delete m_grid;
329 sizer->Add(ctrl);
330}
331