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