]> git.saurik.com Git - wxWidgets.git/blame - 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
0d5eda9c
FM
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__
f978831f 13#pragma hdrstop
0d5eda9c
FM
14#endif
15
f978831f 16// for all others, include the necessary headers wxWidgets headers)
0d5eda9c 17#ifndef WX_PRECOMP
f978831f 18#include "wx/wx.h"
0d5eda9c
FM
19#endif
20
f978831f
BP
21#include "wx/filename.h"
22
0d5eda9c
FM
23#include "autocapture.h"
24
101adcd5
FM
25#ifdef __WXMAC__
26#include <cstring>
27#endif
28
0d5eda9c 29
6cd1aa9d
FM
30// ----------------------------------------------------------------------------
31// AutoCaptureMechanism
32// ----------------------------------------------------------------------------
33
34/* static */
101adcd5
FM
35void AutoCaptureMechanism::Delay(int seconds)
36{
f978831f 37 // TODO: Switch this to use wxTimer.
101adcd5
FM
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)
0d5eda9c 47{
6cd1aa9d
FM
48 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
49#ifdef __WXMAC__
0d5eda9c 50
6cd1aa9d 51 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
0d5eda9c 52
101adcd5
FM
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);
0d5eda9c
FM
58
59 wxBitmap fullscreen;
60
101adcd5
FM
61 if(delay) Delay(delay);
62
0d5eda9c
FM
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
101adcd5
FM
71 // to prevent loading the old screenshot next time
72 system("rm /tmp/wx_screen_capture.png");
73
6cd1aa9d 74#else // Under other paltforms, take a real screenshot
0d5eda9c 75
6cd1aa9d 76 // Create a DC for the whole screen area
0d5eda9c
FM
77 wxScreenDC dcScreen;
78
6cd1aa9d
FM
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
0d5eda9c
FM
82 wxBitmap screenshot(width, height, -1);
83
6cd1aa9d 84 // Create a memory DC that will be used for actually taking the screenshot
0d5eda9c 85 wxMemoryDC memDC;
6cd1aa9d
FM
86
87 // Tell the memory DC to use our Bitmap
88 // all drawing action on the memory DC will go to the Bitmap now
0d5eda9c 89 memDC.SelectObject(screenshot);
0d5eda9c 90
6cd1aa9d
FM
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__
0d5eda9c
FM
106
107 return screenshot;
0d5eda9c
FM
108}
109
6cd1aa9d 110/* static */
101adcd5 111wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay)
0d5eda9c
FM
112{
113 wxPoint origin = rect.GetPosition();
101adcd5 114 return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay);
0d5eda9c
FM
115}
116
60a2264d
FM
117void AutoCaptureMechanism::CaptureAll()
118{
6cd1aa9d 119 // start from the first page
60a2264d
FM
120 m_notebook->SetSelection(0);
121 wxYield();
122
6cd1aa9d
FM
123 for (ControlList::iterator it = m_controlList.begin();
124 it != m_controlList.end();
125 ++it)
60a2264d 126 {
6cd1aa9d 127 Control &ctrl = *it;
60a2264d 128
6cd1aa9d 129 if (ctrl.flag == AJ_TurnPage) // Turn to next page
60a2264d
FM
130 {
131 m_notebook->SetSelection(m_notebook->GetSelection() + 1);
132 wxYield();
133 continue;
134 }
135
6cd1aa9d 136 // create the screenshot
60a2264d 137 wxBitmap screenshot = Capture(ctrl);
6cd1aa9d 138 if (ctrl.flag & AJ_Union)
60a2264d 139 screenshot = Union(screenshot, Capture(*(++it)));
60a2264d 140
6cd1aa9d 141 // and save it
60a2264d
FM
142 Save(screenshot, ctrl.name);
143 }
144}
145
6cd1aa9d 146wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
4bae10bd 147{
6cd1aa9d 148 if (ctrl.name == wxT("")) // no manual specification for the control name
4bae10bd 149 {
6cd1aa9d 150 // Get its name from wxRTTI
4bae10bd
FM
151 ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName();
152 }
153
154 int choice = wxNO;
155
6cd1aa9d
FM
156 // for drop-down controls we need the help of the user
157 if (ctrl.flag & AJ_Dropdown)
4bae10bd 158 {
6cd1aa9d
FM
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);
4bae10bd 163
6cd1aa9d 164 choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
4bae10bd 165
101adcd5
FM
166 #ifndef __WXMAC__ //not __WXMAC__
167 if (choice == wxYES) Delay(3);
168 #endif
4bae10bd
FM
169 }
170
171 wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
172
6cd1aa9d
FM
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)
4bae10bd
FM
176 {
177// #ifdef __WXMSW__
178 int h = rect.GetHeight();
179 rect.SetHeight(h * 4);
180// #endif
181 }
182
6cd1aa9d 183 // cut off "wx" and change the name into lowercase.
4bae10bd
FM
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
6cd1aa9d
FM
188 // take the screenshot
189 wxBitmap screenshot = Capture(rect);
4bae10bd 190
6cd1aa9d 191 if (ctrl.flag & AJ_RegionAdjust)
4bae10bd 192 PutBack(ctrl.ctrl);
4bae10bd
FM
193
194 return screenshot;
195}
196
4bae10bd
FM
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
0d5eda9c
FM
238void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
239{
6cd1aa9d
FM
240 // make sure m_dir exists
241 if (!wxDirExists(m_dir))
0d5eda9c
FM
242 wxMkdir(m_dir);
243
6cd1aa9d 244 wxFileName fullFileName(m_dir, fileName + ".png");
0d5eda9c 245
6cd1aa9d
FM
246 // do not overwrite already existing files with this name
247 while (fullFileName.FileExists())
248 fullFileName.SetName(fullFileName.GetName() + "_");
0d5eda9c 249
6cd1aa9d
FM
250 // save the screenshot as a PNG
251 screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
0d5eda9c
FM
252}
253
254wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)
255{
6cd1aa9d 256 if (flag & AJ_RegionAdjust)
0d5eda9c
FM
257 {
258 wxWindow * parent = ctrl->GetParent();
259 wxSizer * sizer = parent->GetSizer();
260
6cd1aa9d 261 if (sizer)
0d5eda9c
FM
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
6cd1aa9d 279 for (int i = 0; i < 4; ++i)
0d5eda9c
FM
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 }
6cd1aa9d 303 else // Actually it won't get here working with the current guiframe.h/guiframe.cpp
0d5eda9c
FM
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
6cd1aa9d 320 for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it)
0d5eda9c
FM
321 {
322 wxSizerItem* item = *it;
6cd1aa9d 323 if (item->IsWindow()) delete (*it)->GetWindow();
0d5eda9c
FM
324 }
325
326 wxSizer * sizer = ctrl->GetParent()->GetSizer();
327 sizer->Detach(m_grid);
328 delete m_grid;
329 sizer->Add(ctrl);
330}
331