]>
Commit | Line | Data |
---|---|---|
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 | ||
8da17c86 FM |
21 | #include <ctime> |
22 | #include <wx/notebook.h> | |
f978831f | 23 | |
0d5eda9c FM |
24 | #include "autocapture.h" |
25 | ||
101adcd5 FM |
26 | #ifdef __WXMAC__ |
27 | #include <cstring> | |
28 | #endif | |
29 | ||
0d5eda9c | 30 | |
6cd1aa9d FM |
31 | // ---------------------------------------------------------------------------- |
32 | // AutoCaptureMechanism | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
17ad109b FM |
35 | /* static */ |
36 | wxString AutoCaptureMechanism::default_dir = _T("screenshots"); | |
37 | ||
8da17c86 FM |
38 | /* static */ |
39 | wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath() | |
40 | { | |
41 | wxFileName output = wxFileName::DirName(GetDefaultDirectory()); | |
42 | output.MakeAbsolute(); | |
43 | return output.GetFullPath(); | |
44 | } | |
45 | ||
6cd1aa9d | 46 | /* static */ |
101adcd5 FM |
47 | void AutoCaptureMechanism::Delay(int seconds) |
48 | { | |
f978831f | 49 | // TODO: Switch this to use wxTimer. |
101adcd5 FM |
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) | |
0d5eda9c | 59 | { |
6cd1aa9d FM |
60 | // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick. |
61 | #ifdef __WXMAC__ | |
0d5eda9c | 62 | |
6cd1aa9d | 63 | // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC); |
0d5eda9c | 64 | |
101adcd5 FM |
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); | |
0d5eda9c FM |
70 | |
71 | wxBitmap fullscreen; | |
72 | ||
101adcd5 FM |
73 | if(delay) Delay(delay); |
74 | ||
0d5eda9c FM |
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 | ||
101adcd5 FM |
83 | // to prevent loading the old screenshot next time |
84 | system("rm /tmp/wx_screen_capture.png"); | |
85 | ||
6cd1aa9d | 86 | #else // Under other paltforms, take a real screenshot |
0d5eda9c | 87 | |
8da17c86 | 88 | if(delay) Delay(delay); |
17ad109b | 89 | |
6cd1aa9d | 90 | // Create a DC for the whole screen area |
0d5eda9c FM |
91 | wxScreenDC dcScreen; |
92 | ||
6cd1aa9d FM |
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 | |
0d5eda9c FM |
96 | wxBitmap screenshot(width, height, -1); |
97 | ||
6cd1aa9d | 98 | // Create a memory DC that will be used for actually taking the screenshot |
0d5eda9c | 99 | wxMemoryDC memDC; |
6cd1aa9d FM |
100 | |
101 | // Tell the memory DC to use our Bitmap | |
102 | // all drawing action on the memory DC will go to the Bitmap now | |
0d5eda9c | 103 | memDC.SelectObject(screenshot); |
0d5eda9c | 104 | |
6cd1aa9d FM |
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__ | |
0d5eda9c FM |
120 | |
121 | return screenshot; | |
0d5eda9c FM |
122 | } |
123 | ||
6cd1aa9d | 124 | /* static */ |
101adcd5 | 125 | wxBitmap AutoCaptureMechanism::Capture(wxRect rect, int delay) |
0d5eda9c FM |
126 | { |
127 | wxPoint origin = rect.GetPosition(); | |
101adcd5 | 128 | return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay); |
0d5eda9c FM |
129 | } |
130 | ||
17ad109b FM |
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 | ||
60a2264d FM |
148 | void AutoCaptureMechanism::CaptureAll() |
149 | { | |
6cd1aa9d | 150 | // start from the first page |
60a2264d FM |
151 | m_notebook->SetSelection(0); |
152 | wxYield(); | |
153 | ||
6cd1aa9d FM |
154 | for (ControlList::iterator it = m_controlList.begin(); |
155 | it != m_controlList.end(); | |
156 | ++it) | |
60a2264d | 157 | { |
6cd1aa9d | 158 | Control &ctrl = *it; |
60a2264d | 159 | |
6cd1aa9d | 160 | if (ctrl.flag == AJ_TurnPage) // Turn to next page |
60a2264d FM |
161 | { |
162 | m_notebook->SetSelection(m_notebook->GetSelection() + 1); | |
163 | wxYield(); | |
164 | continue; | |
165 | } | |
166 | ||
6cd1aa9d | 167 | // create the screenshot |
60a2264d | 168 | wxBitmap screenshot = Capture(ctrl); |
17ad109b FM |
169 | |
170 | if(ctrl.flag & AJ_Union) | |
171 | { | |
8da17c86 | 172 | // union screenshots until AJ_UnionEnd |
17ad109b FM |
173 | do |
174 | { | |
8da17c86 FM |
175 | ++it; |
176 | it->name = ctrl.name; //preserving the name | |
177 | screenshot = Union(screenshot, Capture(*it)); | |
17ad109b | 178 | } |
8da17c86 | 179 | while(!(it->flag & AJ_UnionEnd)); |
17ad109b | 180 | } |
60a2264d | 181 | |
6cd1aa9d | 182 | // and save it |
60a2264d FM |
183 | Save(screenshot, ctrl.name); |
184 | } | |
185 | } | |
186 | ||
6cd1aa9d | 187 | wxBitmap AutoCaptureMechanism::Capture(Control& ctrl) |
4bae10bd | 188 | { |
8da17c86 FM |
189 | // no manual specification for the control name |
190 | // or name adjustment is disabled globally | |
191 | if (ctrl.name == _T("") || m_flag & AJ_DisableNameAdjust) | |
4bae10bd | 192 | { |
6cd1aa9d | 193 | // Get its name from wxRTTI |
4bae10bd FM |
194 | ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName(); |
195 | } | |
196 | ||
197 | int choice = wxNO; | |
198 | ||
8da17c86 FM |
199 | wxRect rect = GetRect(ctrl.ctrl, ctrl.flag); |
200 | ||
201 | if (ctrl.flag & AJ_Dropdown && !(m_flag & AJ_DisableDropdown)) | |
4bae10bd | 202 | { |
8da17c86 | 203 | // for drop-down controls we need the help of the user |
6cd1aa9d FM |
204 | wxString caption = _("Drop-down screenshot..."); |
205 | wxString msg = | |
8da17c86 | 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."), |
6cd1aa9d | 207 | ctrl.name, ctrl.name); |
4bae10bd | 208 | |
6cd1aa9d | 209 | choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook); |
4bae10bd | 210 | |
8da17c86 FM |
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 | } | |
4bae10bd FM |
222 | } |
223 | ||
6cd1aa9d | 224 | // cut off "wx" and change the name into lowercase. |
4bae10bd FM |
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 | ||
6cd1aa9d | 229 | // take the screenshot |
8da17c86 FM |
230 | wxBitmap screenshot = Capture(rect, (choice == wxYES)?5:0); |
231 | ||
232 | if (choice == wxYES) ctrl.ctrl->SetCursor(wxNullCursor); | |
4bae10bd | 233 | |
6cd1aa9d | 234 | if (ctrl.flag & AJ_RegionAdjust) |
4bae10bd | 235 | PutBack(ctrl.ctrl); |
4bae10bd FM |
236 | |
237 | return screenshot; | |
238 | } | |
239 | ||
4bae10bd FM |
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 | ||
17ad109b FM |
255 | wxMemoryDC dstDC; |
256 | dstDC.SelectObject(result); | |
0d5eda9c | 257 | |
17ad109b FM |
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); | |
0d5eda9c | 263 | |
17ad109b | 264 | dstDC.SelectObject(wxNullBitmap); |
0d5eda9c | 265 | |
17ad109b | 266 | return result; |
0d5eda9c FM |
267 | } |
268 | ||
269 | wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag) | |
270 | { | |
8da17c86 FM |
271 | if( !(m_flag & AJ_DisableRegionAdjust) && (flag & AJ_RegionAdjust) |
272 | || (m_flag & AJ_AlwaysRegionAdjust) ) | |
0d5eda9c FM |
273 | { |
274 | wxWindow * parent = ctrl->GetParent(); | |
275 | wxSizer * sizer = parent->GetSizer(); | |
276 | ||
8da17c86 FM |
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()); | |
0d5eda9c FM |
320 | } |
321 | else | |
322 | { | |
17ad109b | 323 | return ctrl->GetScreenRect().Inflate(m_margin); |
0d5eda9c FM |
324 | } |
325 | } | |
326 | ||
327 | void AutoCaptureMechanism::PutBack(wxWindow * ctrl) | |
328 | { | |
8da17c86 FM |
329 | if(!m_grid) return; |
330 | ||
0d5eda9c FM |
331 | m_grid->Detach(ctrl); |
332 | ||
333 | wxSizerItemList children = m_grid->GetChildren(); | |
334 | ||
6cd1aa9d | 335 | for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it) |
0d5eda9c FM |
336 | { |
337 | wxSizerItem* item = *it; | |
6cd1aa9d | 338 | if (item->IsWindow()) delete (*it)->GetWindow(); |
0d5eda9c FM |
339 | } |
340 | ||
341 | wxSizer * sizer = ctrl->GetParent()->GetSizer(); | |
8da17c86 FM |
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 | ||
0d5eda9c FM |
348 | sizer->Detach(m_grid); |
349 | delete m_grid; | |
8da17c86 FM |
350 | m_grid = NULL; |
351 | ||
0d5eda9c FM |
352 | sizer->Add(ctrl); |
353 | } | |
354 |