]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | #include "autocapture.h" | |
24 | ||
25 | ||
26 | wxBitmap Capture(int x, int y, int width, int height) | |
27 | { | |
28 | //Somehow wxScreenDC.Blit() doesn't work under Mac for now. Here is a trick. | |
29 | #ifdef __WXMAC__ | |
30 | ||
31 | //wxExecute(_T("screencapture -x ") + tempfile, wxEXEC_SYNC); | |
32 | ||
33 | system("screencapture -x /tmp/wx_screen_capture.png"); | |
34 | ||
35 | wxBitmap fullscreen; | |
36 | ||
37 | do | |
38 | { | |
39 | fullscreen = wxBitmap(_T("/tmp/wx_screen_capture.png"), wxBITMAP_TYPE_PNG); | |
40 | } | |
41 | while(!fullscreen.IsOk()); | |
42 | ||
43 | wxBitmap screenshot = fullscreen.GetSubBitmap(wxRect(x,y,width,height)); | |
44 | ||
45 | #else //Under other paltforms, take a real screenshot | |
46 | ||
47 | //Create a DC for the whole screen area | |
48 | wxScreenDC dcScreen; | |
49 | ||
50 | //Create a Bitmap that will later on hold the screenshot image | |
51 | //Note that the Bitmap must have a size big enough to hold the screenshot | |
52 | //-1 means using the current default colour depth | |
53 | wxBitmap screenshot(width, height, -1); | |
54 | ||
55 | //Create a memory DC that will be used for actually taking the screenshot | |
56 | wxMemoryDC memDC; | |
57 | //Tell the memory DC to use our Bitmap | |
58 | //all drawing action on the memory DC will go to the Bitmap now | |
59 | memDC.SelectObject(screenshot); | |
60 | //Blit (in this case copy) the actual screen on the memory DC | |
61 | //and thus the Bitmap | |
62 | memDC.Blit( 0, //Copy to this X coordinate | |
63 | 0, //Copy to this Y coordinate | |
64 | width, //Copy this width | |
65 | height, //Copy this height | |
66 | &dcScreen, //From where do we copy? | |
67 | x, //What's the X offset in the original DC? | |
68 | y //What's the Y offset in the original DC? | |
69 | ); | |
70 | //Select the Bitmap out of the memory DC by selecting a new | |
71 | //uninitialized Bitmap | |
72 | memDC.SelectObject(wxNullBitmap); | |
73 | #endif //#ifdef __WXMAC__ | |
74 | ||
75 | // wxMessageBox(_(""),_("")); | |
76 | ||
77 | return screenshot; | |
78 | ||
79 | } | |
80 | ||
81 | wxBitmap Capture(wxRect rect) | |
82 | { | |
83 | wxPoint origin = rect.GetPosition(); | |
84 | return Capture(origin.x, origin.y, rect.GetWidth(), rect.GetHeight()); | |
85 | } | |
86 | ||
87 | void AutoCaptureMechanism::Save(wxBitmap screenshot, wxString fileName) | |
88 | { | |
89 | //Check if m_defaultDir already existed | |
90 | if(!wxDirExists(m_dir)) | |
91 | wxMkdir(m_dir); | |
92 | ||
93 | wxString fullFileName = m_dir + wxFileName::GetPathSeparator() + fileName; | |
94 | ||
95 | //to prvent overwritten | |
96 | while(wxFileName::FileExists(fullFileName + _T(".png"))) fullFileName += _T("_"); | |
97 | ||
98 | //Our Bitmap now has the screenshot, so let's save it as an png | |
99 | //The filename itself is without extension. | |
100 | screenshot.SaveFile(fullFileName + _T(".png"), wxBITMAP_TYPE_PNG); | |
101 | } | |
102 | ||
103 | wxRect AutoCaptureMechanism::GetRect(wxWindow* ctrl, int flag) | |
104 | { | |
105 | if(flag & AJ_RegionAdjust) | |
106 | { | |
107 | wxWindow * parent = ctrl->GetParent(); | |
108 | wxSizer * sizer = parent->GetSizer(); | |
109 | ||
110 | if(sizer) | |
111 | { | |
112 | sizer->Detach(ctrl); | |
113 | ||
114 | /* | |
115 | +---------+-----------+---------+ | |
116 | | 0 | label | 1 | | |
117 | +---------+-----------+---------+ | |
118 | | label | ctrl | label | | |
119 | +---------+-----------+---------+ | |
120 | | 2 | label | 3 | | |
121 | +---------+-----------+---------+ | |
122 | */ | |
123 | ||
124 | m_grid = new wxFlexGridSizer(3, 3, m_border, m_border); | |
125 | ||
126 | wxStaticText* l[4]; | |
127 | ||
128 | for(int i = 0; i < 4; ++i) | |
129 | l[i] = new wxStaticText(parent, wxID_ANY, wxT(" ")); | |
130 | ||
131 | m_grid->Add(l[0]); | |
132 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
133 | m_grid->Add(l[1]); | |
134 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
135 | m_grid->Add(ctrl); | |
136 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
137 | m_grid->Add(l[2]); | |
138 | m_grid->Add(new wxStaticText(parent, wxID_ANY, wxT(" "))); | |
139 | m_grid->Add(l[3]); | |
140 | ||
141 | sizer->Add(m_grid); | |
142 | parent->SetSizer(sizer); | |
143 | parent->Layout(); | |
144 | ||
145 | parent->Refresh(); | |
146 | wxYield(); | |
147 | ||
148 | return wxRect(l[0]->GetScreenRect().GetBottomRight(), | |
149 | l[3]->GetScreenRect().GetTopLeft()); | |
150 | ||
151 | } | |
152 | else //Actually it won't get here working with the current guiframe.h/guiframe.cpp | |
153 | { | |
154 | return ctrl->GetScreenRect().Inflate(m_border); | |
155 | } | |
156 | } | |
157 | else | |
158 | { | |
159 | return ctrl->GetScreenRect().Inflate(m_border); | |
160 | } | |
161 | } | |
162 | ||
163 | void AutoCaptureMechanism::PutBack(wxWindow * ctrl) | |
164 | { | |
165 | m_grid->Detach(ctrl); | |
166 | ||
167 | wxSizerItemList children = m_grid->GetChildren(); | |
168 | ||
169 | for(wxSizerItemList::iterator it = children.begin(); it != children.end(); ++it) | |
170 | { | |
171 | wxSizerItem* item = *it; | |
172 | if(item->IsWindow()) delete (*it)->GetWindow(); | |
173 | } | |
174 | ||
175 | wxSizer * sizer = ctrl->GetParent()->GetSizer(); | |
176 | sizer->Detach(m_grid); | |
177 | delete m_grid; | |
178 | sizer->Add(ctrl); | |
179 | } | |
180 |