many fixes; now the application correctly starts up
[wxWidgets.git] / utils / screenshotgen / src / autocapture.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: autocapture.h
3 // Purpose: Defines the AutoCaptureMechanism class
4 // Author: Utensil Candel (UtensilCandel@@gmail.com)
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifndef AUTOCAP_H
10 #define AUTOCAP_H
11
12 #include <wx/notebook.h>
13 #include <wx/settings.h>
14 #include <vector>
15 #include <ctime>
16
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);
20
21 enum AdjustFlags
22 {
23 AJ_Normal = 0,
24 AJ_RegionAdjust = 1 << 0,
25 AJ_Dropdown = 1 << 1,
26 AJ_TurnPage = 1 << 2,
27 AJ_Union = 1 << 3,
28 AJ_UnionEnd = 1 << 4
29 };
30
31
32 // ----------------------------------------------------------------------------
33 // class AutoCaptureMechanism
34 // ----------------------------------------------------------------------------
35
36 class AutoCaptureMechanism
37 {
38 public:
39 AutoCaptureMechanism(wxNotebook * notebook,
40 wxString directory = wxT("screenshots"),
41 int border = 5)
42 : m_notebook(notebook), m_dir(directory), m_border(border) {}
43 ~AutoCaptureMechanism(){}
44
45 /*
46 If wxRTTI can't get the name correctly, specify name;
47 If wxWindow::GetScreenRect doesn't get the rect correctly, set flag to AJ_RegionAdjust
48 */
49 void RegisterControl(wxWindow * ctrl, wxString name = wxT(""),
50 int flag = AJ_Normal)
51 {
52 m_controlList.push_back(Control(ctrl, name, flag));
53 }
54
55 void RegisterControl(wxWindow * ctrl, int flag)
56 {
57 RegisterControl(ctrl, wxT(""), flag);
58 }
59
60 void RegisterPageTurn()
61 {
62 m_controlList.push_back(Control(0, wxT(""), AJ_TurnPage));
63 }
64
65 void CaptureAll()
66 {
67 m_notebook->SetSelection(0);
68 wxYield();
69
70 for(ControlList::iterator it = m_controlList.begin();
71 it != m_controlList.end();
72 ++it)
73 {
74 Control & ctrl = *it;
75
76 if(ctrl.flag == AJ_TurnPage) // Turn to next page
77 {
78 m_notebook->SetSelection(m_notebook->GetSelection() + 1);
79 wxYield();
80 continue;
81 }
82
83 wxBitmap screenshot = Capture(ctrl);
84
85 if(ctrl.flag & AJ_Union)
86 {
87 screenshot = Union(screenshot, Capture(*(++it)));
88 }
89
90 Save(screenshot, ctrl.name);
91 }
92 }
93
94 protected: // internal utils
95 struct Control
96 {
97 Control() {}
98
99 Control(wxWindow * _ctrl, wxString _name, int _flag)
100 : ctrl(_ctrl), name(_name), flag(_flag) {}
101
102 wxWindow * ctrl;
103 wxString name;
104 int flag;
105 };
106
107 wxBitmap Capture(Control & ctrl);
108
109 //if AJ_RegionAdjust is specified, the following line will use the label trick to adjust
110 //the region position and size
111 wxRect GetRect(wxWindow* ctrl, int flag);
112
113 //put the control back after the label trick(Using reparent/resizer approach)
114 void PutBack(wxWindow * ctrl);
115
116 wxBitmap Union(wxBitmap pic1, wxBitmap pic2);
117
118 void Save(wxBitmap screenshot, wxString fileName);
119
120 private:
121 typedef std::vector<Control> ControlList;
122 ControlList m_controlList;
123
124 // here we introduce the dependency on wxNotebook.
125 // The assumption of this whole class is that the gui has the following top-down structure
126 // wxNotebook wxPanel wxSizer wxControls
127 wxNotebook* m_notebook;
128
129 wxFlexGridSizer* m_grid;
130
131 wxString m_dir;
132 int m_border;
133 };
134
135 #endif // AUTOCAP_H
136
137