]>
Commit | Line | Data |
---|---|---|
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 | #include "autocapture.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/bitmap.h" | |
23 | #include "wx/filename.h" | |
24 | #include "wx/notebook.h" | |
25 | ||
26 | #include <ctime> | |
27 | ||
28 | #ifdef __WXMAC__ | |
29 | #include <cstring> | |
30 | #endif | |
31 | ||
32 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // AutoCaptureMechanism | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | AutoCaptureMechanism::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 | ||
46 | /* static */ | |
47 | wxString AutoCaptureMechanism::default_dir = wxT("screenshots"); | |
48 | ||
49 | /* static */ | |
50 | wxString AutoCaptureMechanism::GetDefaultDirectoryAbsPath() | |
51 | { | |
52 | wxFileName output = wxFileName::DirName(GetDefaultDirectory()); | |
53 | output.MakeAbsolute(); | |
54 | return output.GetFullPath(); | |
55 | } | |
56 | ||
57 | /* static */ | |
58 | void AutoCaptureMechanism::Delay(int seconds) | |
59 | { | |
60 | // TODO: Switch this to use wxTimer. | |
61 | ||
62 | // Wait for 3 seconds | |
63 | clock_t start = clock(); | |
64 | while ( clock() - start < (clock_t)CLOCKS_PER_SEC * seconds) | |
65 | wxYieldIfNeeded(); | |
66 | } | |
67 | ||
68 | /* static */ | |
69 | bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, int x, int y, | |
70 | int width, int height, int delay) | |
71 | { | |
72 | // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick. | |
73 | #ifdef __WXMAC__ | |
74 | ||
75 | // wxExecute(wxT("screencapture -x ") + tempfile, wxEXEC_SYNC); | |
76 | ||
77 | char captureCommand[80] =""; // a reasonable max size is 80 | |
78 | sprintf(captureCommand, "sleep %d;%s", delay, "screencapture -x /tmp/wx_screen_capture.png"); | |
79 | system(captureCommand); | |
80 | ||
81 | if(delay) Delay(delay); | |
82 | ||
83 | wxBitmap fullscreen; | |
84 | do | |
85 | { | |
86 | fullscreen = wxBitmap(wxT("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG); | |
87 | } | |
88 | while(!fullscreen.IsOk()); | |
89 | ||
90 | *bitmap = fullscreen.GetSubBitmap(wxRect(x, y, width, height)); | |
91 | ||
92 | // to prevent loading the old screenshot next time | |
93 | system("rm /tmp/wx_screen_capture.png"); | |
94 | ||
95 | return true; | |
96 | ||
97 | #else // Under other paltforms, take a real screenshot | |
98 | ||
99 | if(delay) Delay(delay); | |
100 | ||
101 | // Create a DC for the whole screen area | |
102 | wxScreenDC dcScreen; | |
103 | ||
104 | bitmap->Create(width, height); | |
105 | ||
106 | // Create a memory DC that will be used for actually taking the screenshot | |
107 | wxMemoryDC memDC; | |
108 | memDC.SelectObject((*bitmap)); | |
109 | memDC.Clear(); | |
110 | ||
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__ | |
126 | ||
127 | return true; | |
128 | } | |
129 | ||
130 | /* static */ | |
131 | bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, wxRect rect, int delay) | |
132 | { | |
133 | wxPoint origin = rect.GetPosition(); | |
134 | return Capture(bitmap, origin.x, origin.y, rect.GetWidth(), rect.GetHeight(), delay); | |
135 | } | |
136 | ||
137 | /* static */ | |
138 | void AutoCaptureMechanism::Save(wxBitmap* screenshot, const wxString& fileName) | |
139 | { | |
140 | // make sure default_dir exists | |
141 | if (!wxDirExists(default_dir)) | |
142 | wxMkdir(default_dir); | |
143 | ||
144 | wxFileName fullFileName(default_dir, fileName + ".png"); | |
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 | |
151 | screenshot->SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG); | |
152 | } | |
153 | ||
154 | void AutoCaptureMechanism::CaptureAll() | |
155 | { | |
156 | // start from the first page | |
157 | m_notebook->SetSelection(0); | |
158 | wxYield(); | |
159 | ||
160 | for (ControlList::iterator it = m_controlList.begin(); | |
161 | it != m_controlList.end(); | |
162 | ++it) | |
163 | { | |
164 | Control &ctrl = *it; | |
165 | ||
166 | if (ctrl.flag == AJ_TurnPage) // Turn to next page | |
167 | { | |
168 | m_notebook->SetSelection(m_notebook->GetSelection() + 1); | |
169 | wxYield(); | |
170 | continue; | |
171 | } | |
172 | ||
173 | // create the screenshot | |
174 | wxBitmap screenshot(1, 1); | |
175 | Capture(&screenshot, ctrl); | |
176 | ||
177 | if(ctrl.flag & AJ_Union) | |
178 | { | |
179 | // union screenshots until AJ_UnionEnd | |
180 | do | |
181 | { | |
182 | ++it; | |
183 | it->name = ctrl.name; //preserving the name | |
184 | wxBitmap screenshot2(1, 1); | |
185 | Capture(&screenshot2, *it); | |
186 | wxBitmap combined(1, 1); | |
187 | Union(&screenshot, &screenshot2, &combined); | |
188 | screenshot = combined; | |
189 | } | |
190 | while(!(it->flag & AJ_UnionEnd)); | |
191 | } | |
192 | ||
193 | // and save it | |
194 | Save(&screenshot, ctrl.name); | |
195 | } | |
196 | } | |
197 | ||
198 | bool AutoCaptureMechanism::Capture(wxBitmap* bitmap, Control& ctrl) | |
199 | { | |
200 | // no manual specification for the control name | |
201 | // or name adjustment is disabled globally | |
202 | if (ctrl.name == wxT("") || m_flag & AJ_DisableNameAdjust) | |
203 | { | |
204 | // Get its name from wxRTTI | |
205 | ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName(); | |
206 | } | |
207 | ||
208 | int choice = wxNO; | |
209 | ||
210 | wxRect rect = GetRect(ctrl.ctrl, ctrl.flag); | |
211 | ||
212 | if (ctrl.flag & AJ_Dropdown && !(m_flag & AJ_DisableDropdown)) | |
213 | { | |
214 | // for drop-down controls we need the help of the user | |
215 | wxString caption = _("Drop-down screenshot..."); | |
216 | wxString msg = | |
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."), | |
218 | ctrl.name, ctrl.name); | |
219 | ||
220 | choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook); | |
221 | ||
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 | } | |
233 | } | |
234 | ||
235 | // cut off "wx" and change the name into lowercase. | |
236 | // e.g. wxButton will have a name of "button" at the end | |
237 | ctrl.name.StartsWith(wxT("wx"), &(ctrl.name)); | |
238 | ctrl.name.MakeLower(); | |
239 | ||
240 | // take the screenshot | |
241 | Capture(bitmap, rect, (choice == wxYES)?5:0); | |
242 | ||
243 | if (choice == wxYES) ctrl.ctrl->SetCursor(wxNullCursor); | |
244 | ||
245 | if (ctrl.flag & AJ_RegionAdjust) | |
246 | PutBack(ctrl.ctrl); | |
247 | ||
248 | return true; | |
249 | } | |
250 | ||
251 | /* static */ | |
252 | bool AutoCaptureMechanism::Union(wxBitmap* top, wxBitmap* bottom, wxBitmap* result) | |
253 | { | |
254 | int w1, w2, h1, h2, w, h; | |
255 | w1 = top->GetWidth(); | |
256 | w2 = bottom->GetWidth(); | |
257 | h1 = top->GetHeight(); | |
258 | h2 = bottom->GetHeight(); | |
259 | ||
260 | const int gap_between = 20; | |
261 | ||
262 | w = (w1 >= w2) ? w1 : w2; | |
263 | h = h1 + h2 + gap_between; | |
264 | ||
265 | result->Create(w, h); | |
266 | ||
267 | wxMemoryDC dstDC; | |
268 | dstDC.SelectObject((*result)); | |
269 | ||
270 | dstDC.SetBrush(*wxWHITE_BRUSH); | |
271 | dstDC.Clear(); | |
272 | dstDC.DrawBitmap((*top), 0, 0); | |
273 | dstDC.DrawBitmap((*bottom), 0, h1 + gap_between); | |
274 | ||
275 | dstDC.SelectObject(wxNullBitmap); | |
276 | ||
277 | return true; | |
278 | } | |
279 | ||
280 | wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag) | |
281 | { | |
282 | if( !(m_flag & AJ_DisableRegionAdjust) && (flag & AJ_RegionAdjust) | |
283 | || (m_flag & AJ_AlwaysRegionAdjust) ) | |
284 | { | |
285 | wxWindow * parent = ctrl->GetParent(); | |
286 | wxSizer * sizer = parent->GetSizer(); | |
287 | ||
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) | |
310 | l[i] = new wxStaticText(parent, wxID_ANY, wxT(" ")); | |
311 | ||
312 | m_grid->Add(l[0]); | |
313 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
314 | m_grid->Add(l[1]); | |
315 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
316 | m_grid->Add(ctrl, 1, wxEXPAND); | |
317 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
318 | m_grid->Add(l[2]); | |
319 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
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()); | |
331 | } | |
332 | else | |
333 | { | |
334 | return ctrl->GetScreenRect().Inflate(m_margin); | |
335 | } | |
336 | } | |
337 | ||
338 | void AutoCaptureMechanism::PutBack(wxWindow * ctrl) | |
339 | { | |
340 | if(!m_grid) return; | |
341 | ||
342 | m_grid->Detach(ctrl); | |
343 | ||
344 | wxSizerItemList children = m_grid->GetChildren(); | |
345 | ||
346 | for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it) | |
347 | { | |
348 | wxSizerItem* item = *it; | |
349 | if (item->IsWindow()) delete (*it)->GetWindow(); | |
350 | } | |
351 | ||
352 | wxSizer * sizer = ctrl->GetParent()->GetSizer(); | |
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 | ||
359 | sizer->Detach(m_grid); | |
360 | delete m_grid; | |
361 | m_grid = NULL; | |
362 | ||
363 | sizer->Add(ctrl); | |
364 | } | |
365 |