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