]> git.saurik.com Git - wxWidgets.git/blob - utils/screenshotgen/src/screenshot_main.cpp
inside <wx-data> we need to use instead of /
[wxWidgets.git] / utils / screenshotgen / src / screenshot_main.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: screenshot_main.cpp
3 // Purpose: Implement the Application Frame
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/filename.h>
19 #include <wx/dcbuffer.h>
20 #include <wx/colordlg.h>
21 #include <wx/fontdlg.h>
22 #include <wx/filedlg.h>
23 #include <wx/dirdlg.h>
24 #endif
25
26 #include <wx/dir.h>
27 #include <wx/aboutdlg.h>
28 #include <wx/msgdlg.h>
29 #include <wx/dcscreen.h>
30 #include <wx/filesys.h>
31 #include <wx/utils.h>
32
33 #include "screenshot_main.h"
34 #include "ctrlmaskout.h"
35 #include "autocapture.h"
36
37
38 // ----------------------------------------------------------------------------
39 // ScreenshotFrame
40 // ----------------------------------------------------------------------------
41
42 ScreenshotFrame::ScreenshotFrame(wxFrame *frame)
43 #if SCREENSHOTGEN_USE_AUI
44 : AuiGUIFrame(frame)
45 #else
46 : GUIFrame(frame)
47 #endif
48 {
49 #if wxUSE_STATUSBAR
50 statusBar->SetStatusText(_("Welcome to the Automatic Screenshot Generator!"), 0);
51 #endif
52
53 // We will hold one ctrlmaskout during the whole life time of the main frame
54 m_maskout = new CtrlMaskOut();
55
56 // At the begining, we are not specifying the rect region
57 capturingRect = false;
58
59 // Do some further customization on some controls generated by wxFormBuilder
60 InitFBControls();
61 #if SCREENSHOTGEN_USE_AUI
62 // Somehow it will be very small after I move to Aui
63 SetSize(600, 600);
64 // Maximize(true);
65 #endif
66 }
67
68 ScreenshotFrame::~ScreenshotFrame()
69 {
70 delete m_maskout;
71 }
72
73 /*
74 Do some further customization on some controls generated by wxFormBuilder.
75
76 wxFormBuilder does not allow customizations on some controls;
77 e.g. you cannot load a richtext file in a wxRichtextCtrl during initialization.
78
79 Those customizations will be done here.
80 */
81 void ScreenshotFrame::InitFBControls()
82 {
83 // For some reason, wxFormBuilder does not set the scrollbar range
84 m_scrollBar1->SetScrollbar(50, 1, 100, 1);
85
86 // Do the default selection for wxComboBox
87 m_comboBox1->Select(0);
88
89 // To look better under gtk
90 #ifdef __WXGTK__
91 m_comboBox1->Delete(4);
92 #endif
93
94 // Add a root and some nodes for wxTreeCtrl
95 wxTreeItemId root = m_treeCtrl1->AddRoot(_("wxTreeCtrl"));
96 m_treeCtrl1->AppendItem(root, _("Node1"));
97 wxTreeItemId node2 = m_treeCtrl1->AppendItem(root, _("Node2"));
98 m_treeCtrl1->AppendItem(node2, _("Node3"));
99 m_treeCtrl1->ExpandAll();
100
101 // Add items into wxListCtrl
102 for(long index = 0; index < 5; index++)
103 m_listCtrl1->InsertItem( index, wxString::Format(_("Item\n(0,%d)"),index));
104
105 // Check the first item in wxCheckListBox
106 m_checkList1->Check(0);
107
108 // Load richtext.xml into wxRichtextCtrl
109 m_richText1->LoadFile(_T("richtext.xml"));
110 //m_richText1->ShowPosition(335);
111
112 // select first page in the main notebook ctrl
113 m_notebook1->ChangeSelection(0);
114
115 // set minimum size hints
116 GetSizer()->SetSizeHints(this);
117 }
118
119
120
121 // ----------------------------------------------------------------------------
122 // ScreenshotFrame - event handlers
123 // ----------------------------------------------------------------------------
124
125 void ScreenshotFrame::OnClose(wxCloseEvent& WXUNUSED(event))
126 {
127 Destroy();
128 }
129
130 void ScreenshotFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
131 {
132 Destroy();
133 }
134
135 void ScreenshotFrame::OnSeeScreenshots(wxCommandEvent& WXUNUSED(event))
136 {
137 wxString defaultDir = m_maskout->GetDefaultDirectory();
138
139 // Check if defaultDir already existed
140 if (!wxDirExists(defaultDir))
141 wxMkdir(defaultDir);
142
143 // Use the native file browser to open defaultDir
144 wxLaunchDefaultBrowser(wxFileSystem::FileNameToURL(defaultDir));
145 }
146
147 void ScreenshotFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
148 {
149 wxAboutDialogInfo info;
150 info.SetName(_("Automatic Screenshot Generator"));
151 info.SetVersion(_("1.0"));
152 info.SetDescription(_("This utility automatically creates screenshots of wxWidgets controls for ues in wxWidgets documentation."));
153 info.SetCopyright(_T("(C) 2008 Utensil Candel"));
154
155 wxAboutBox(info);
156 }
157
158 void ScreenshotFrame::OnCaptureFullScreen(wxCommandEvent& WXUNUSED(event))
159 {
160 // Create a DC for the whole screen area
161 wxScreenDC dcScreen;
162
163 // Get the size of the screenDC
164 wxCoord screenWidth, screenHeight;
165 dcScreen.GetSize(&screenWidth, &screenHeight);
166
167 m_maskout->Capture(0, 0, screenWidth, screenHeight, _T("fullscreen"));
168
169 // Inform the user
170 wxMessageBox(_("A screenshot of the entire screen was saved as:\n\n ") +
171 m_maskout->GetDefaultDirectoryAbsPath() + wxFileName::GetPathSeparator() + "fullscreen.png",
172 _("Full screen capture"), wxICON_INFORMATION|wxOK, this);
173 }
174
175 void ScreenshotFrame::OnCaptureRect(wxCommandEvent& WXUNUSED(event))
176 {
177 capturingRect = true;
178 wxMenuBar * menubar = this->GetMenuBar();
179 menubar->FindItem(idMenuCapRect)->Enable(false);
180 menubar->FindItem(idMenuEndCapRect)->Enable(true);
181
182 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
183
184 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
185 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
186 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
187 }
188
189 void ScreenshotFrame::OnEndCaptureRect(wxCommandEvent& WXUNUSED(event))
190 {
191 capturingRect = false;
192 wxMenuBar * menubar = this->GetMenuBar();
193 menubar->FindItem(idMenuCapRect)->Enable(true);
194 menubar->FindItem(idMenuEndCapRect)->Enable(false);
195
196 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
197
198 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
199 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
200 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
201 }
202
203 void ScreenshotFrame::OnNotebookPageChanging(
204 #if SCREENSHOTGEN_USE_AUI
205 wxAuiNotebookEvent& event
206 #else
207 wxNotebookEvent& event
208 #endif
209 )
210 {
211 if (!capturingRect)
212 {
213 event.Skip();
214 return;
215 }
216
217 wxWindow * thePage = m_notebook1->GetPage(event.GetOldSelection());
218
219 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
220 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
221 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
222
223 event.Skip();
224 }
225
226 void ScreenshotFrame::OnNotebookPageChanged(
227 #if SCREENSHOTGEN_USE_AUI
228 wxAuiNotebookEvent& event
229 #else
230 wxNotebookEvent& event
231 #endif
232 )
233 {
234 if (!capturingRect)
235 {
236 event.Skip();
237 return;
238 }
239
240 wxWindow *thePage = m_notebook1->GetPage(event.GetSelection());
241
242 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
243 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
244 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
245
246 event.Skip();
247 }
248
249 void ScreenshotFrame::OnCaptureAllControls(wxCommandEvent& WXUNUSED(event))
250 {
251 wxString dir = m_maskout->GetDefaultDirectoryAbsPath();
252
253 // check if there are other screenshots taken before
254 if (wxFileName::DirExists(dir))
255 {
256 int choice = wxMessageBox(_("It seems that you have already generated some screenshots.\n\nClick YES to delete them all (recommended) or NO to preserve them.\nClick CANCEL to cancel this auto-capture operation."),
257 _("Delete existing screenshots?"),
258 wxYES_NO|wxCANCEL|wxICON_QUESTION, this);
259 switch(choice)
260 {
261 case wxYES:
262 {
263 wxArrayString files;
264 wxDir::GetAllFiles(dir, &files, wxT("*.png"), wxDIR_FILES);
265
266 // remove all PNG files from the screenshots folder
267 int n = files.GetCount();
268 for (int i = 0; i < n; ++i)
269 wxRemoveFile(files[i]);
270 }
271 break;
272
273 case wxNO: break;
274 case wxCANCEL: return;
275 }
276 }
277
278 // proceed with the automatic screenshot capture
279
280 this->Maximize();
281
282 AutoCaptureMechanism auto_cap(m_notebook1);
283
284 auto_cap.RegisterControl(m_button1);
285 auto_cap.RegisterControl(m_staticText1);
286 auto_cap.RegisterControl(m_checkBox1, AJ_Union);
287 auto_cap.RegisterControl(m_checkBox2, AJ_UnionEnd);
288 auto_cap.RegisterControl(m_radioBtn1, AJ_Union);
289 auto_cap.RegisterControl(m_radioBtn2, AJ_UnionEnd);
290 auto_cap.RegisterControl(m_bpButton1);
291 auto_cap.RegisterControl(m_bitmap1);
292 auto_cap.RegisterControl(m_gauge1, wxT("wxGauge"));
293 auto_cap.RegisterControl(m_slider1);
294 auto_cap.RegisterControl(m_toggleBtn1, AJ_Union);
295 auto_cap.RegisterControl(m_toggleBtn2, AJ_UnionEnd);
296 auto_cap.RegisterControl(m_hyperlink1);
297 auto_cap.RegisterControl(m_spinCtrl1, AJ_RegionAdjust);
298 auto_cap.RegisterControl(m_spinBtn1);
299 auto_cap.RegisterControl(m_scrollBar1);
300
301 auto_cap.RegisterPageTurn();
302
303 auto_cap.RegisterControl(m_checkList1);
304 auto_cap.RegisterControl(m_listBox1);
305 auto_cap.RegisterControl(m_radioBox1);
306 auto_cap.RegisterControl(m_staticBox1);
307 auto_cap.RegisterControl(m_treeCtrl1);
308 auto_cap.RegisterControl(m_listCtrl1, wxT("wxListCtrl"));
309
310 auto_cap.RegisterControl(m_animationCtrl1);
311 auto_cap.RegisterControl(m_collPane1, wxT("wxCollapsiblePane"), AJ_Union);
312 auto_cap.RegisterControl(m_collPane2, AJ_UnionEnd);
313
314 auto_cap.RegisterPageTurn();
315
316 auto_cap.RegisterControl(m_textCtrl1, AJ_Union);
317 auto_cap.RegisterControl(m_textCtrl2, AJ_UnionEnd);
318 auto_cap.RegisterControl(m_richText1);
319
320 auto_cap.RegisterPageTurn();
321
322 auto_cap.RegisterControl(m_colourPicker1, wxT("wxColourPickerCtrl"));
323 auto_cap.RegisterControl(m_fontPicker1, wxT("wxFontPickerCtrl"));
324 auto_cap.RegisterControl(m_filePicker1, wxT("wxFilePickerCtrl"), AJ_RegionAdjust);
325 auto_cap.RegisterControl(m_calendar1, wxT("wxCalendarCtrl"), AJ_RegionAdjust);
326 auto_cap.RegisterControl(m_datePicker1, wxT("wxDatePickerCtrl"));
327 auto_cap.RegisterControl(m_genericDirCtrl1, wxT("wxGenericDirCtrl"));
328 auto_cap.RegisterControl(m_dirPicker1, wxT("wxDirPickerCtrl"), AJ_RegionAdjust);
329
330 auto_cap.RegisterPageTurn();
331
332 auto_cap.RegisterControl(m_choice1, AJ_Dropdown);
333 auto_cap.RegisterControl(m_comboBox1, AJ_Dropdown);
334 auto_cap.RegisterControl(m_bmpComboBox1, AJ_Dropdown);
335 auto_cap.RegisterControl(m_ownerDrawnComboBox1, AJ_Dropdown);
336 auto_cap.RegisterControl(m_comboCtrl1, AJ_Dropdown|AJ_Union);
337 auto_cap.RegisterControl(m_comboCtrl2, AJ_Dropdown|AJ_UnionEnd);
338
339 auto_cap.CaptureAll();
340
341 wxMessageBox(_("All screenshots were generated successfully in the folder:\n ") + dir,
342 _("Success"), wxOK|wxICON_INFORMATION, this);
343 }