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