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