]> git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/autocapture.cpp
remove misleaing mentions of wxEXEC_ASYNC from wxExecute() overloads always executing...
[wxWidgets.git] / utils / screenshotgen / src / autocapture.cpp
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 <ctime>
22 #include <wx/notebook.h>
23
24 #include "autocapture.h"
25
26 #ifdef __WXMAC__
27 #include <cstring>
28 #endif
29
30
31 // ----------------------------------------------------------------------------
32 // AutoCaptureMechanism
33 // ----------------------------------------------------------------------------
34
35 /* static */
36 wxString AutoCaptureMechanism::default_dir = _T("screenshots");
37
38 /* static */
39 wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath()
40 {
41 wxFileName output = wxFileName::DirName(GetDefaultDirectory());
42 output.MakeAbsolute();
43 return output.GetFullPath();
44 }
45
46 /* static */
47 void AutoCaptureMechanism::Delay(int seconds)
48 {
49 // TODO: Switch this to use wxTimer.
50
51 // Wait for 3 seconds
52 clock_t start = clock();
53 while (clock() - start < CLOCKS_PER_SEC * seconds)
54 wxYieldIfNeeded();
55 }
56
57 /* static */
58 wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height, int delay)
59 {
60 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
61 #ifdef __WXMAC__
62
63 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
64
65 char captureCommand[80] =""; // a reasonable max size is 80
66
67 sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png");
68
69 system(captureCommand);
70
71 wxBitmap fullscreen;
72
73 if(delay) Delay(delay);
74
75 do
76 {
77 fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG);
78 }
79 while(!fullscreen.IsOk());
80
81 wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height));
82
83 // to prevent loading the old screenshot next time
84 system("rm /tmp/wx_screen_capture.png");
85
86 #else // Under other paltforms, take a real screenshot
87
88 if(delay) Delay(delay);
89
90 // Create a DC for the whole screen area
91 wxScreenDC dcScreen;
92
93 // Create a Bitmap that will later on hold the screenshot image
94 // Note that the Bitmap must have a size big enough to hold the screenshot
95 // -1 means using the current default colour depth
96 wxBitmap screenshot(width, height, -1);
97
98 // Create a memory DC that will be used for actually taking the screenshot
99 wxMemoryDC memDC;
100
101 // Tell the memory DC to use our Bitmap
102 // all drawing action on the memory DC will go to the Bitmap now
103 memDC.SelectObject(screenshot);
104
105 // Blit (in this case copy) the actual screen on the memory DC
106 // and thus the Bitmap
107 memDC.Blit( 0, // Copy to this X coordinate
108 0, // Copy to this Y coordinate
109 width, // Copy this width
110 height, // Copy this height
111 &dcScreen, // From where do we copy?
112 x, // What's the X offset in the original DC?
113 y // What's the Y offset in the original DC?
114 );
115
116 // Select the Bitmap out of the memory DC by selecting a new
117 // uninitialized Bitmap
118 memDC.SelectObject(wxNullBitmap);
119 #endif // #ifdef __WXMAC__
120
121 return screenshot;
122 }
123
124 /* static */
125 wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay)
126 {
127 wxPoint origin = rect.GetPosition();
128 return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay);
129 }
130
131 /* static */
132 void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
133 {
134 // make sure default_dir exists
135 if (!wxDirExists(default_dir))
136 wxMkdir(default_dir);
137
138 wxFileName fullFileName(default_dir, fileName + ".png");
139
140 // do not overwrite already existing files with this name
141 while (fullFileName.FileExists())
142 fullFileName.SetName(fullFileName.GetName() + "_");
143
144 // save the screenshot as a PNG
145 screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
146 }
147
148 void AutoCaptureMechanism::CaptureAll()
149 {
150 // start from the first page
151 m_notebook->SetSelection(0);
152 wxYield();
153
154 for (ControlList::iterator it = m_controlList.begin();
155 it != m_controlList.end();
156 ++it)
157 {
158 Control &ctrl = *it;
159
160 if (ctrl.flag == AJ_TurnPage) // Turn to next page
161 {
162 m_notebook->SetSelection(m_notebook->GetSelection() + 1);
163 wxYield();
164 continue;
165 }
166
167 // create the screenshot
168 wxBitmap screenshot = Capture(ctrl);
169
170 if(ctrl.flag & AJ_Union)
171 {
172 // union screenshots until AJ_UnionEnd
173 do
174 {
175 ++it;
176 it->name = ctrl.name; //preserving the name
177 screenshot = Union(screenshot, Capture(*it));
178 }
179 while(!(it->flag & AJ_UnionEnd));
180 }
181
182 // and save it
183 Save(screenshot, ctrl.name);
184 }
185 }
186
187 wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
188 {
189 // no manual specification for the control name
190 // or name adjustment is disabled globally
191 if (ctrl.name == _T("") || m_flag & AJ_DisableNameAdjust)
192 {
193 // Get its name from wxRTTI
194 ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName();
195 }
196
197 int choice = wxNO;
198
199 wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
200
201 if (ctrl.flag & AJ_Dropdown && !(m_flag & AJ_DisableDropdown))
202 {
203 // for drop-down controls we need the help of the user
204 wxString caption = _("Drop-down screenshot...");
205 wxString msg =
206 wxString::Format(_("Do you wish to capture the drop-down list of '%s' ?\n\n If YES, please drop down the list of '%s' in 5 seconds after closing this message box.\n If NO, the screenshot for this control won't contain its drop-down list."),
207 ctrl.name, ctrl.name);
208
209 choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
210
211 if (choice == wxYES)
212 {
213 //A little hint
214 ctrl.ctrl->SetCursor(wxCursor(wxCURSOR_HAND));
215
216 // Do some rect adjust so it can include the dropdown list
217 // This adjust isn't pretty, but it works fine on all three paltforms.
218 // Looking forward to a better solution
219 int h = rect.GetHeight();
220 rect.SetHeight(h * 4);
221 }
222 }
223
224 // cut off "wx" and change the name into lowercase.
225 // e.g. wxButton will have a name of "button" at the end
226 ctrl.name.StartsWith(_T("wx"), &(ctrl.name));
227 ctrl.name.MakeLower();
228
229 // take the screenshot
230 wxBitmap screenshot = Capture(rect, (choice == wxYES)?5:0);
231
232 if (choice == wxYES) ctrl.ctrl->SetCursor(wxNullCursor);
233
234 if (ctrl.flag & AJ_RegionAdjust)
235 PutBack(ctrl.ctrl);
236
237 return screenshot;
238 }
239
240 wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2)
241 {
242 int w1, w2, h1, h2, w, h;
243 w1 = pic1.GetWidth();
244 w2 = pic2.GetWidth();
245 h1 = pic1.GetHeight();
246 h2 = pic2.GetHeight();
247
248 const int gap_between = 20;
249
250 w = (w1 >= w2) ? w1 : w2;
251 h = h1 + h2 + gap_between;
252
253 wxBitmap result(w, h, -1);
254
255 wxMemoryDC dstDC;
256 dstDC.SelectObject(result);
257
258 dstDC.SetPen(*wxTRANSPARENT_PEN);
259 dstDC.SetBrush(*wxWHITE_BRUSH);
260 dstDC.DrawRectangle(-1, -1, w + 1, h + 1);
261 dstDC.DrawBitmap(pic1, 0, 0, false);
262 dstDC.DrawBitmap(pic2, 0, h1 + gap_between, false);
263
264 dstDC.SelectObject(wxNullBitmap);
265
266 return result;
267 }
268
269 wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)
270 {
271 if( !(m_flag & AJ_DisableRegionAdjust) && (flag & AJ_RegionAdjust)
272 || (m_flag & AJ_AlwaysRegionAdjust) )
273 {
274 wxWindow * parent = ctrl->GetParent();
275 wxSizer * sizer = parent->GetSizer();
276
277 //The assertion won't fail if controls are still managed by wxSizer, and it's unlikely to
278 //change in the future.
279 wxASSERT_MSG(sizer,
280 "The GUI that AutoCaptureMechanism working with doesn't manage controls with wxSizer");
281
282 sizer->Detach(ctrl);
283
284 /*
285 +---------+-----------+---------+
286 | 0 | label | 1 |
287 +---------+-----------+---------+
288 | label | ctrl | label |
289 +---------+-----------+---------+
290 | 2 | label | 3 |
291 +---------+-----------+---------+
292 */
293
294 m_grid = new wxFlexGridSizer(3, 3, m_margin, m_margin);
295
296 wxStaticText* l[4];
297
298 for (int i = 0; i < 4; ++i)
299 l[i] = new wxStaticText(parent, wxID_ANY, _T(" "));
300
301 m_grid->Add(l[0]);
302 m_grid->Add(new wxStaticText(parent, wxID_ANY, _T(" ")));
303 m_grid->Add(l[1]);
304 m_grid->Add(new wxStaticText(parent, wxID_ANY, _T(" ")));
305 m_grid->Add(ctrl, 1, wxEXPAND);
306 m_grid->Add(new wxStaticText(parent, wxID_ANY, _T(" ")));
307 m_grid->Add(l[2]);
308 m_grid->Add(new wxStaticText(parent, wxID_ANY, _T(" ")));
309 m_grid->Add(l[3]);
310
311 sizer->Add(m_grid);
312 parent->SetSizer(sizer);
313 parent->Layout();
314
315 parent->Refresh();
316 wxYield();
317
318 return wxRect(l[0]->GetScreenRect().GetBottomRight(),
319 l[3]->GetScreenRect().GetTopLeft());
320 }
321 else
322 {
323 return ctrl->GetScreenRect().Inflate(m_margin);
324 }
325 }
326
327 void AutoCaptureMechanism::PutBack(wxWindow * ctrl)
328 {
329 if(!m_grid) return;
330
331 m_grid->Detach(ctrl);
332
333 wxSizerItemList children = m_grid->GetChildren();
334
335 for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it)
336 {
337 wxSizerItem* item = *it;
338 if (item->IsWindow()) delete (*it)->GetWindow();
339 }
340
341 wxSizer * sizer = ctrl->GetParent()->GetSizer();
342
343 //The assertion won't fail if controls are still managed by wxSizer, and it's unlikely to
344 //change in the future.
345 wxASSERT_MSG(sizer,
346 "The GUI that AutoCaptureMechanism working with doesn't manage controls with wxSizer");
347
348 sizer->Detach(m_grid);
349 delete m_grid;
350 m_grid = NULL;
351
352 sizer->Add(ctrl);
353 }
354