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