]> git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/autocapture.cpp
fix the menu item bitmaps for wxMSW;
[wxWidgets.git] / utils / screenshotgen / src / autocapture.cpp
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>
22 #include "autocapture.h"
23
24
25 // ----------------------------------------------------------------------------
26 // AutoCaptureMechanism
27 // ----------------------------------------------------------------------------
28
29 /* static */
30 wxBitmap AutoCaptureMechanism::Capture(int x, int y, int width, int height)
31 {
32 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
33 #ifdef __WXMAC__
34
35 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
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
49 #else // Under other paltforms, take a real screenshot
50
51 // Create a DC for the whole screen area
52 wxScreenDC dcScreen;
53
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
57 wxBitmap screenshot(width, height, -1);
58
59 // Create a memory DC that will be used for actually taking the screenshot
60 wxMemoryDC memDC;
61
62 // Tell the memory DC to use our Bitmap
63 // all drawing action on the memory DC will go to the Bitmap now
64 memDC.SelectObject(screenshot);
65
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__
81
82 return screenshot;
83 }
84
85 /* static */
86 wxBitmap AutoCaptureMechanism::Capture(wxRect rect)
87 {
88 wxPoint origin = rect.GetPosition();
89 return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight());
90 }
91
92 void AutoCaptureMechanism::CaptureAll()
93 {
94 // start from the first page
95 m_notebook->SetSelection(0);
96 wxYield();
97
98 for (ControlList::iterator it = m_controlList.begin();
99 it != m_controlList.end();
100 ++it)
101 {
102 Control &ctrl = *it;
103
104 if (ctrl.flag == AJ_TurnPage) // Turn to next page
105 {
106 m_notebook->SetSelection(m_notebook->GetSelection() + 1);
107 wxYield();
108 continue;
109 }
110
111 // create the screenshot
112 wxBitmap screenshot = Capture(ctrl);
113 if (ctrl.flag & AJ_Union)
114 screenshot = Union(screenshot, Capture(*(++it)));
115
116 // and save it
117 Save(screenshot, ctrl.name);
118 }
119 }
120
121 wxBitmap AutoCaptureMechanism::Capture(Control& ctrl)
122 {
123 if (ctrl.name == wxT("")) // no manual specification for the control name
124 {
125 // Get its name from wxRTTI
126 ctrl.name = ctrl.ctrl->GetClassInfo()->GetClassName();
127 }
128
129 int choice = wxNO;
130
131 // for drop-down controls we need the help of the user
132 if (ctrl.flag & AJ_Dropdown)
133 {
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);
138
139 choice = wxMessageBox(msg, caption, wxYES_NO, m_notebook);
140 if (choice == wxYES)
141 {
142 using std::clock;
143 using std::clock_t;
144
145 // Wait for 3 seconds
146 clock_t start = clock();
147 while (clock() - start < CLOCKS_PER_SEC * 3)
148 wxYieldIfNeeded();
149 }
150 }
151
152 wxRect rect = GetRect(ctrl.ctrl, ctrl.flag);
153
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)
157 {
158 // #ifdef __WXMSW__
159 int h = rect.GetHeight();
160 rect.SetHeight(h * 4);
161 // #endif
162 }
163
164 // cut off "wx" and change the name into lowercase.
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
169 // AD-HOC FIX for wxHyperlink
170 if (ctrl.name == "generichyperlinkctrl")
171 ctrl.name = "hyperlinkctrl";
172
173 // take the screenshot
174 wxBitmap screenshot = Capture(rect);
175
176 if (ctrl.flag & AJ_RegionAdjust)
177 PutBack(ctrl.ctrl);
178
179 return screenshot;
180 }
181
182 wxBitmap AutoCaptureMechanism::Union(wxBitmap pic1, wxBitmap pic2)
183 {
184 int w1, w2, h1, h2, w, h;
185 w1 = pic1.GetWidth();
186 w2 = pic2.GetWidth();
187 h1 = pic1.GetHeight();
188 h2 = pic2.GetHeight();
189
190 const int gap_between = 20;
191
192 w = (w1 >= w2) ? w1 : w2;
193 h = h1 + h2 + gap_between;
194
195 wxBitmap result(w, h, -1);
196
197 wxMemoryDC dstDC;
198 dstDC.SelectObject(result);
199
200 dstDC.DrawBitmap(pic1, 0, 0, false);
201 dstDC.DrawBitmap(pic2, 0, h1 + gap_between, false);
202
203 dstDC.SelectObject(wxNullBitmap);
204
205 wxMemoryDC maskDC;
206 wxBitmap mask(w, h, 1);
207 maskDC.SelectObject(mask);
208
209 maskDC.SetPen(*wxTRANSPARENT_PEN);
210 maskDC.SetBrush(*wxBLACK_BRUSH);
211 maskDC.DrawRectangle(0, 0, w + 1, h + 1);
212
213 maskDC.SetBrush(*wxWHITE_BRUSH);
214 maskDC.DrawRectangle(0, 0, w1, h1);
215 maskDC.DrawRectangle(0, h1 + gap_between, w2, h2);
216 maskDC.SelectObject(wxNullBitmap);
217
218 result.SetMask(new wxMask(mask));
219
220 return result;
221 }
222
223 void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName)
224 {
225 // make sure m_dir exists
226 if (!wxDirExists(m_dir))
227 wxMkdir(m_dir);
228
229 wxFileName fullFileName(m_dir, fileName + ".png");
230
231 // do not overwrite already existing files with this name
232 while (fullFileName.FileExists())
233 fullFileName.SetName(fullFileName.GetName() + "_");
234
235 // save the screenshot as a PNG
236 screenshot.SaveFile(fullFileName.GetFullPath(), wxBITMAP_TYPE_PNG);
237 }
238
239 wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag)
240 {
241 if (flag & AJ_RegionAdjust)
242 {
243 wxWindow * parent = ctrl->GetParent();
244 wxSizer * sizer = parent->GetSizer();
245
246 if (sizer)
247 {
248 sizer->Detach(ctrl);
249
250 /*
251 +---------+-----------+---------+
252 | 0 | label | 1 |
253 +---------+-----------+---------+
254 | label | ctrl | label |
255 +---------+-----------+---------+
256 | 2 | label | 3 |
257 +---------+-----------+---------+
258 */
259
260 m_grid = new wxFlexGridSizer(3, 3, m_border, m_border);
261
262 wxStaticText* l[4];
263
264 for (int i = 0; i < 4; ++i)
265 l[i] = new wxStaticText(parent, wxID_ANY, wxT(" "));
266
267 m_grid->Add(l[0]);
268 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
269 m_grid->Add(l[1]);
270 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
271 m_grid->Add(ctrl);
272 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
273 m_grid->Add(l[2]);
274 m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" ")));
275 m_grid->Add(l[3]);
276
277 sizer->Add(m_grid);
278 parent->SetSizer(sizer);
279 parent->Layout();
280
281 parent->Refresh();
282 wxYield();
283
284 return wxRect(l[0]->GetScreenRect().GetBottomRight(),
285 l[3]->GetScreenRect().GetTopLeft());
286
287 }
288 else // Actually it won't get here working with the current guiframe.h/guiframe.cpp
289 {
290 return ctrl->GetScreenRect().Inflate(m_border);
291 }
292 }
293 else
294 {
295 return ctrl->GetScreenRect().Inflate(m_border);
296 }
297 }
298
299 void AutoCaptureMechanism::PutBack(wxWindow * ctrl)
300 {
301 m_grid->Detach(ctrl);
302
303 wxSizerItemList children = m_grid->GetChildren();
304
305 for (wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it)
306 {
307 wxSizerItem* item = *it;
308 if (item->IsWindow()) delete (*it)->GetWindow();
309 }
310
311 wxSizer * sizer = ctrl->GetParent()->GetSizer();
312 sizer->Detach(m_grid);
313 delete m_grid;
314 sizer->Add(ctrl);
315 }
316