1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Defines the AutoCaptureMechanism class
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 #include <wx/notebook.h>
13 #include <wx/settings.h>
17 // Global helper functions. to take screenshot for a rect region
18 wxBitmap
Capture(wxRect rect
);
19 wxBitmap
Capture(int x
, int y
, int width
, int height
);
24 AJ_RegionAdjust
= 1 << 0,
31 class AutoCaptureMechanism
34 AutoCaptureMechanism(wxNotebook
* notebook
,
35 wxString directory
= wxT("screenshots"),
37 : m_notebook(notebook
), m_dir(directory
), m_border(border
) {}
38 ~AutoCaptureMechanism(){}
41 If wxRTTI can't get the name correctly, specify name;
42 If wxWindow::GetScreenRect doesn't get the rect correctly, set flag to AJ_RegionAdjust
44 void RegisterControl(wxWindow
* ctrl
, wxString name
= wxT(""),
47 m_controlList
.push_back(Control(ctrl
, name
, flag
));
50 void RegisterControl(wxWindow
* ctrl
, int flag
)
52 RegisterControl(ctrl
, wxT(""), flag
);
55 void RegisterPageTurn()
57 m_controlList
.push_back(Control(0, wxT(""), AJ_TurnPage
));
62 m_notebook
->SetSelection(0);
65 for(ControlList::iterator it
= m_controlList
.begin();
66 it
!= m_controlList
.end();
71 if(ctrl
.flag
== AJ_TurnPage
) // Turn to next page
73 m_notebook
->SetSelection(m_notebook
->GetSelection() + 1);
78 wxBitmap screenshot
= Capture(ctrl
);
80 if(ctrl
.flag
& AJ_Union
)
82 screenshot
= Union(screenshot
, Capture(*(++it
)));
85 Save(screenshot
, ctrl
.name
);
94 Control(wxWindow
* _ctrl
, wxString _name
, int _flag
)
95 : ctrl(_ctrl
), name(_name
), flag(_flag
) {}
102 typedef std::vector
<Control
> ControlList
;
103 ControlList m_controlList
;
105 // here we introduce the dependency on wxNotebook.
106 // The assumption of this whole class is that the gui has the following top-down structure
107 // wxNotebook wxPanel wxSizer wxControls
108 wxNotebook
* m_notebook
;
110 wxFlexGridSizer
* m_grid
;
117 wxBitmap
Capture(Control
& ctrl
)
119 if(ctrl
.name
== wxT("")) //no mannual specification for the control name
121 //Get name from wxRTTI
122 ctrl
.name
= ctrl
.ctrl
->GetClassInfo()->GetClassName();
127 if(ctrl
.flag
& AJ_Dropdown
)
129 wxString caption
= _("Do you wish to capture the dropdown list of ") + ctrl
.name
+ _("?");
130 wxString notice
= _("Click YES to capture it.\nAnd you MUST drop down the ") + ctrl
.name
+ _(" in 3 seconds after close me.\n");
131 notice
+= _("Click NO otherwise.");
133 choice
= wxMessageBox(notice
, caption
, wxYES_NO
, m_notebook
);
141 clock_t start
= clock();
142 while(clock() - start
< CLOCKS_PER_SEC
* 3)
149 wxRect rect
= GetRect(ctrl
.ctrl
, ctrl
.flag
);
151 //Do some rect adjust so it can include the dropdown list
152 //Currently it only works well under MSW, not adjusted for Linux and Mac OS
153 if(ctrl
.flag
& AJ_Dropdown
&& choice
== wxYES
)
156 int h
= rect
.GetHeight();
157 rect
.SetHeight(h
* 4);
161 //cut off "wx" and change them into lowercase.
162 // e.g. wxButton will have a name of "button" at the end
163 ctrl
.name
.StartsWith(_T("wx"), &(ctrl
.name
));
164 ctrl
.name
.MakeLower();
166 wxBitmap screenshot
= ::Capture(rect
);
168 if(ctrl
.flag
& AJ_RegionAdjust
)
176 //if AJ_RegionAdjust is specified, the following line will use the label trick to adjust
177 //the region position and size
178 wxRect
GetRect(wxWindow
* ctrl
, int flag
);
179 //put the control back after the label trick(Using reparent/resizer approach)
180 void PutBack(wxWindow
* ctrl
);
182 wxBitmap
Union(wxBitmap pic1
, wxBitmap pic2
)
184 int w1
, w2
, h1
, h2
, w
, h
;
185 w1
= pic1
.GetWidth();
186 w2
= pic2
.GetWidth();
187 h1
= pic1
.GetHeight();
188 h2
= pic2
.GetHeight();
190 const int gap_between
= 20;
192 w
= (w1
>= w2
) ? w1
: w2
;
193 h
= h1
+ h2
+ gap_between
;
195 wxBitmap
result(w
, h
, -1);
198 dstDC
.SelectObject(result
);
200 dstDC
.DrawBitmap(pic1
, 0, 0, false);
201 dstDC
.DrawBitmap(pic2
, 0, h1
+ gap_between
, false);
203 dstDC
.SelectObject(wxNullBitmap
);
206 wxBitmap
mask(w
, h
, 1);
207 maskDC
.SelectObject(mask
);
209 maskDC
.SetPen(*wxTRANSPARENT_PEN
);
210 maskDC
.SetBrush(*wxBLACK_BRUSH
);
211 maskDC
.DrawRectangle(0, 0, w
+ 1, h
+ 1);
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
);
218 result
.SetMask(new wxMask(mask
));
223 void Save(wxBitmap screenshot
, wxString fileName
);