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