]>
git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/autocapture.cpp
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 wxWidgets headers)
21 #include "wx/filename.h"
23 #include "autocapture.h"
30 // ----------------------------------------------------------------------------
31 // AutoCaptureMechanism
32 // ----------------------------------------------------------------------------
35 wxString
AutoCaptureMechanism :: default_dir
= _T ( "screenshots" );
38 void AutoCaptureMechanism :: Delay ( int seconds
)
40 // TODO: Switch this to use wxTimer.
43 clock_t start
= clock ();
44 while ( clock () - start
< CLOCKS_PER_SEC
* seconds
)
49 wxBitmap
AutoCaptureMechanism :: Capture ( int x
, int y
, int width
, int height
, int delay
)
51 // Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick.
54 // wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC);
56 char captureCommand
[ 80 ] = "" ; // a reasonable max size is 80
58 sprintf ( captureCommand
, "sleep %d ; %s " , delay
, "screencapture -x /tmp/wx_screen_capture.png" );
60 system ( captureCommand
);
64 if ( delay
) Delay ( delay
);
68 fullscreen
= wxBitmap ( _T ( "/tmp/wx_screen_capture.png" ), wxBITMAP_TYPE_PNG
);
70 while (! fullscreen
. IsOk ());
72 wxBitmap screenshot
= fullscreen
. GetSubBitmap ( wxRect ( x
, y
, width
, height
));
74 // to prevent loading the old screenshot next time
75 system ( "rm /tmp/wx_screen_capture.png" );
77 #else // Under other paltforms, take a real screenshot
81 // Create a DC for the whole screen area
84 // Create a Bitmap that will later on hold the screenshot image
85 // Note that the Bitmap must have a size big enough to hold the screenshot
86 // -1 means using the current default colour depth
87 wxBitmap
screenshot ( width
, height
, - 1 );
89 // Create a memory DC that will be used for actually taking the screenshot
92 // Tell the memory DC to use our Bitmap
93 // all drawing action on the memory DC will go to the Bitmap now
94 memDC
. SelectObject ( screenshot
);
96 // Blit (in this case copy) the actual screen on the memory DC
97 // and thus the Bitmap
98 memDC
. Blit ( 0 , // Copy to this X coordinate
99 0 , // Copy to this Y coordinate
100 width
, // Copy this width
101 height
, // Copy this height
102 & dcScreen
, // From where do we copy?
103 x
, // What's the X offset in the original DC?
104 y
// What's the Y offset in the original DC?
107 // Select the Bitmap out of the memory DC by selecting a new
108 // uninitialized Bitmap
109 memDC
. SelectObject ( wxNullBitmap
);
110 #endif // #ifdef __WXMAC__
116 wxBitmap
AutoCaptureMechanism :: Capture ( wxRect rect
, int delay
)
118 wxPoint origin
= rect
. GetPosition ();
119 return Capture ( origin
. x
, origin
. y
, rect
. GetWidth (), rect
. GetHeight (), delay
);
123 void AutoCaptureMechanism :: Save ( wxBitmap screenshot
, wxString fileName
)
125 // make sure default_dir exists
126 if (! wxDirExists ( default_dir
))
127 wxMkdir ( default_dir
);
129 wxFileName
fullFileName ( default_dir
, fileName
+ ".png" );
131 // do not overwrite already existing files with this name
132 while ( fullFileName
. FileExists ())
133 fullFileName
. SetName ( fullFileName
. GetName () + "_" );
135 // save the screenshot as a PNG
136 screenshot
. SaveFile ( fullFileName
. GetFullPath (), wxBITMAP_TYPE_PNG
);
139 void AutoCaptureMechanism :: CaptureAll ()
141 // start from the first page
142 m_notebook
-> SetSelection ( 0 );
145 for ( ControlList :: iterator it
= m_controlList
. begin ();
146 it
!= m_controlList
. end ();
151 if ( ctrl
. flag
== AJ_TurnPage
) // Turn to next page
153 m_notebook
-> SetSelection ( m_notebook
-> GetSelection () + 1 );
158 // // create the screenshot
159 // wxBitmap screenshot = Capture(ctrl);
160 // if (ctrl.flag & AJ_Union)
161 // screenshot = Union(screenshot, Capture(*(++it)));
164 // Save(screenshot, ctrl.name);
165 // create the screenshot
166 wxBitmap screenshot
= Capture ( ctrl
);
168 if ( ctrl
. flag
& AJ_Union
)
173 screenshot
= Union ( screenshot
, Capture ( ctrl
));
175 while (!( ctrl
. flag
& AJ_UnionEnd
));
179 Save ( screenshot
, ctrl
. name
);
183 wxBitmap
AutoCaptureMechanism :: Capture ( Control
& ctrl
)
185 if ( ctrl
. name
== wxT ( "" )) // no manual specification for the control name
187 // Get its name from wxRTTI
188 ctrl
. name
= ctrl
. ctrl
-> GetClassInfo ()-> GetClassName ();
193 // for drop-down controls we need the help of the user
194 if ( ctrl
. flag
& AJ_Dropdown
)
196 wxString caption
= _ ( "Drop-down screenshot..." );
198 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." ),
199 ctrl
. name
, ctrl
. name
);
201 choice
= wxMessageBox ( msg
, caption
, wxYES_NO
, m_notebook
);
203 #ifndef __WXMAC__ //not __WXMAC__
204 if ( choice
== wxYES
) Delay ( 3 );
208 wxRect rect
= GetRect ( ctrl
. ctrl
, ctrl
. flag
);
210 // Do some rect adjust so it can include the dropdown list;
211 // currently this only works well under MSW; not adjusted for Linux and Mac OS
212 if ( ctrl
. flag
& AJ_Dropdown
&& choice
== wxYES
)
215 int h
= rect
. GetHeight ();
216 rect
. SetHeight ( h
* 4 );
220 // cut off "wx" and change the name into lowercase.
221 // e.g. wxButton will have a name of "button" at the end
222 ctrl
. name
. StartsWith ( _T ( "wx" ), &( ctrl
. name
));
223 ctrl
. name
. MakeLower ();
225 // take the screenshot
226 wxBitmap screenshot
= Capture ( rect
);
228 if ( ctrl
. flag
& AJ_RegionAdjust
)
234 wxBitmap
AutoCaptureMechanism :: Union ( wxBitmap pic1
, wxBitmap pic2
)
236 int w1
, w2
, h1
, h2
, w
, h
;
237 w1
= pic1
. GetWidth ();
238 w2
= pic2
. GetWidth ();
239 h1
= pic1
. GetHeight ();
240 h2
= pic2
. GetHeight ();
242 const int gap_between
= 20 ;
244 w
= ( w1
>= w2
) ? w1
: w2
;
245 h
= h1
+ h2
+ gap_between
;
247 wxBitmap
result ( w
, h
, - 1 );
250 //Mask the bitmap "result"
252 wxBitmap
mask ( w
, h
, 1 );
253 maskDC
. SelectObject ( mask
);
255 maskDC
. SetPen (* wxTRANSPARENT_PEN
);
256 maskDC
. SetBrush (* wxBLACK_BRUSH
);
257 maskDC
. DrawRectangle ( 0 , 0 , w
+ 1 , h
+ 1 );
259 maskDC
. SetBrush (* wxWHITE_BRUSH
);
260 maskDC
. DrawRectangle ( 0 , 0 , w1
, h1
);
261 maskDC
. DrawRectangle ( 0 , h1
+ gap_between
, w2
, h2
);
262 maskDC
. SelectObject ( wxNullBitmap
);
264 result
. SetMask ( new wxMask ( mask
));
268 dstDC
. SelectObject ( result
);
270 dstDC
. SetPen (* wxTRANSPARENT_PEN
);
271 dstDC
. SetBrush (* wxWHITE_BRUSH
);
272 dstDC
. DrawRectangle (- 1 , - 1 , w
+ 1 , h
+ 1 );
273 dstDC
. DrawBitmap ( pic1
, 0 , 0 , false );
274 dstDC
. DrawBitmap ( pic2
, 0 , h1
+ gap_between
, false );
276 dstDC
. SelectObject ( wxNullBitmap
);
281 wxRect
AutoCaptureMechanism :: GetRect ( wxWindow
* ctrl
, int flag
)
283 if ( flag
& AJ_RegionAdjust
)
285 wxWindow
* parent
= ctrl
-> GetParent ();
286 wxSizer
* sizer
= parent
-> GetSizer ();
293 +---------+-----------+---------+
295 +---------+-----------+---------+
296 | label | ctrl | label |
297 +---------+-----------+---------+
299 +---------+-----------+---------+
302 m_grid
= new wxFlexGridSizer ( 3 , 3 , m_margin
, m_margin
);
306 for ( int i
= 0 ; i
< 4 ; ++ i
)
307 l
[ i
] = new wxStaticText ( parent
, wxID_ANY
, wxT ( " " ));
310 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
312 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
314 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
316 m_grid
-> Add ( new wxStaticText ( parent
, wxID_ANY
, wxT ( " " )));
320 parent
-> SetSizer ( sizer
);
326 return wxRect ( l
[ 0 ]-> GetScreenRect (). GetBottomRight (),
327 l
[ 3 ]-> GetScreenRect (). GetTopLeft ());
330 else // Actually it won't get here working with the current guiframe.h/guiframe.cpp
332 return ctrl
-> GetScreenRect (). Inflate ( m_margin
);
337 return ctrl
-> GetScreenRect (). Inflate ( m_margin
);
341 void AutoCaptureMechanism :: PutBack ( wxWindow
* ctrl
)
343 m_grid
-> Detach ( ctrl
);
345 wxSizerItemList children
= m_grid
-> GetChildren ();
347 for ( wxSizerItemList :: iterator it
= children
. begin (); it
!= children
. end (); ++ it
)
349 wxSizerItem
* item
= * it
;
350 if ( item
-> IsWindow ()) delete (* it
)-> GetWindow ();
353 wxSizer
* sizer
= ctrl
-> GetParent ()-> GetSizer ();
354 sizer
-> Detach ( m_grid
);