]>
git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/autocapture.cpp
026a797cce5bd1e9760e9beba01ae6a40354382d
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: autocapture.cpp
3 // Purpose: Implement wxCtrlMaskOut class
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx/wx.h".
10 #include "wx/wxprec.h"
16 // for all others, include the necessary headers
21 #include <wx/filename.h>
22 #include "autocapture.h"
25 // ----------------------------------------------------------------------------
26 // AutoCaptureMechanism
27 // ----------------------------------------------------------------------------
30 wxBitmap
AutoCaptureMechanism :: Capture ( int x
, int y
, int width
, int height
)
32 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
35 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
37 system ( "screencapture -x /tmp/wx_screen_capture.png" );
43 fullscreen
= wxBitmap ( _T ( "/tmp/wx_screen_capture.png" ), wxBITMAP_TYPE_PNG
);
45 while (! fullscreen
. IsOk ());
47 wxBitmap screenshot
= fullscreen
. GetSubBitmap ( wxRect ( x
, y
, width
, height
));
49 #else // Under other paltforms, take a real screenshot
51 // Create a DC for the whole screen area
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 );
59 // Create a memory DC that will be used for actually taking the screenshot
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
);
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?
77 // Select the Bitmap out of the memory DC by selecting a new
78 // uninitialized Bitmap
79 memDC
. SelectObject ( wxNullBitmap
);
80 #endif // #ifdef __WXMAC__
86 wxBitmap
AutoCaptureMechanism :: Capture ( wxRect rect
)
88 wxPoint origin
= rect
. GetPosition ();
89 return Capture ( origin
. x
, origin
. y
, rect
. GetWidth (), rect
. GetHeight ());
92 void AutoCaptureMechanism :: CaptureAll ()
94 // start from the first page
95 m_notebook
-> SetSelection ( 0 );
98 for ( ControlList :: iterator it
= m_controlList
. begin ();
99 it
!= m_controlList
. end ();
104 if ( ctrl
. flag
== AJ_TurnPage
) // Turn to next page
106 m_notebook
-> SetSelection ( m_notebook
-> GetSelection () + 1 );
111 // create the screenshot
112 wxBitmap screenshot
= Capture ( ctrl
);
113 if ( ctrl
. flag
& AJ_Union
)
114 screenshot
= Union ( screenshot
, Capture (*(++ it
)));
117 Save ( screenshot
, ctrl
. name
);
121 wxBitmap
AutoCaptureMechanism :: Capture ( Control
& ctrl
)
123 if ( ctrl
. name
== wxT ( "" )) // no manual specification for the control name
125 // Get its name from wxRTTI
126 ctrl
. name
= ctrl
. ctrl
-> GetClassInfo ()-> GetClassName ();
131 // for drop-down controls we need the help of the user
132 if ( ctrl
. flag
& AJ_Dropdown
)
134 wxString caption
= _ ( "Drop-down screenshot..." );
136 wxString :: Format ( _ ( "Do you wish to capture the drop-down list of ' %s ' ? \n\n If you click YES you must drop-down the list of ' %s ' in 3 seconds after closing this message box. \n If you click NO the screenshot for this control won't contain its drop-down list." ),
137 ctrl
. name
, ctrl
. name
);
139 choice
= wxMessageBox ( msg
, caption
, wxYES_NO
, m_notebook
);
145 // Wait for 3 seconds
146 clock_t start
= clock ();
147 while ( clock () - start
< CLOCKS_PER_SEC
* 3 )
152 wxRect rect
= GetRect ( ctrl
. ctrl
, ctrl
. flag
);
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
)
159 int h
= rect
. GetHeight ();
160 rect
. SetHeight ( h
* 4 );
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 ();
169 // take the screenshot
170 wxBitmap screenshot
= Capture ( rect
);
172 if ( ctrl
. flag
& AJ_RegionAdjust
)
178 wxBitmap
AutoCaptureMechanism :: Union ( wxBitmap pic1
, wxBitmap pic2
)
180 int w1
, w2
, h1
, h2
, w
, h
;
181 w1
= pic1
. GetWidth ();
182 w2
= pic2
. GetWidth ();
183 h1
= pic1
. GetHeight ();
184 h2
= pic2
. GetHeight ();
186 const int gap_between
= 20 ;
188 w
= ( w1
>= w2
) ? w1
: w2
;
189 h
= h1
+ h2
+ gap_between
;
191 wxBitmap
result ( w
, h
, - 1 );
194 dstDC
. SelectObject ( result
);
196 dstDC
. DrawBitmap ( pic1
, 0 , 0 , false );
197 dstDC
. DrawBitmap ( pic2
, 0 , h1
+ gap_between
, false );
199 dstDC
. SelectObject ( wxNullBitmap
);
202 wxBitmap
mask ( w
, h
, 1 );
203 maskDC
. SelectObject ( mask
);
205 maskDC
. SetPen (* wxTRANSPARENT_PEN
);
206 maskDC
. SetBrush (* wxBLACK_BRUSH
);
207 maskDC
. DrawRectangle ( 0 , 0 , w
+ 1 , h
+ 1 );
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
);
214 result
. SetMask ( new wxMask ( mask
));
219 void AutoCaptureMechanism :: Save ( wxBitmap screenshot
, wxString fileName
)
221 // make sure m_dir exists
222 if (! wxDirExists ( m_dir
))
225 wxFileName
fullFileName ( m_dir
, fileName
+ ".png" );
227 // do not overwrite already existing files with this name
228 while ( fullFileName
. FileExists ())
229 fullFileName
. SetName ( fullFileName
. GetName () + "_" );
231 // save the screenshot as a PNG
232 screenshot
. SaveFile ( fullFileName
. GetFullPath (), wxBITMAP_TYPE_PNG
);
235 wxRect
AutoCaptureMechanism :: GetRect ( wxWindow
* ctrl
, int flag
)
237 if ( flag
& AJ_RegionAdjust
)
239 wxWindow
* parent
= ctrl
-> GetParent ();
240 wxSizer
* sizer
= parent
-> GetSizer ();
247 +---------+-----------+---------+
249 +---------+-----------+---------+
250 | label | ctrl | label |
251 +---------+-----------+---------+
253 +---------+-----------+---------+
256 m_grid
= new wxFlexGridSizer ( 3 , 3 , m_border
, m_border
);
260 for ( int i
= 0 ; i
< 4 ; ++ i
)
261 l
[ i
] = new wxStaticText ( parent
, wxID_ANY
, wxT ( " " ));
264 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
266 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
268 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
270 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
274 parent
-> SetSizer ( sizer
);
280 return wxRect ( l
[ 0 ]-> GetScreenRect (). GetBottomRight (),
281 l
[ 3 ]-> GetScreenRect (). GetTopLeft ());
284 else // Actually it won't get here working with the current guiframe.h/guiframe.cpp
286 return ctrl
-> GetScreenRect (). Inflate ( m_border
);
291 return ctrl
-> GetScreenRect (). Inflate ( m_border
);
295 void AutoCaptureMechanism :: PutBack ( wxWindow
* ctrl
)
297 m_grid
-> Detach ( ctrl
);
299 wxSizerItemList children
= m_grid
-> GetChildren ();
301 for ( wxSizerItemList :: iterator it
= children
. begin (); it
!= children
. end (); ++ it
)
303 wxSizerItem
* item
= * it
;
304 if ( item
-> IsWindow ()) delete (* it
)-> GetWindow ();
307 wxSizer
* sizer
= ctrl
-> GetParent ()-> GetSizer ();
308 sizer
-> Detach ( m_grid
);