init the file and dir pickers with dummy files/folders
[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 // Init file and dir pickers
118 wxString file, dir;
119 #if defined(__WXMSW__)
120 file = "C:\\Windows\\explorer.exe";
121 dir = "C:\\Windows";
122 #else
123 file = "/bin/bash";
124 dir = "/home";
125 #endif
126 m_filePicker1->SetPath(file);
127 m_dirPicker1->SetPath(dir);
128
129 // Check the first item in wxCheckListBox
130 m_checkList1->Check(0);
131
132 // Load richtext.xml into wxRichtextCtrl
133 m_richText1->LoadFile(_T("richtext.xml"));
134 //m_richText1->ShowPosition(335);
135
136 // select first page in the main notebook ctrl
137 m_notebook1->ChangeSelection(0);
138
139 // set minimum size hints
140 GetSizer()->SetSizeHints(this);
141
142 // add bitmaps to the menus
143 m_menuCapRect->SetBitmaps( wxIcon(play_xpm) );
144 m_menuEndCapRect->SetBitmaps( wxIcon(stop_xpm) );
145 }
146
147
148
149 // ----------------------------------------------------------------------------
150 // ScreenshotFrame - event handlers
151 // ----------------------------------------------------------------------------
152
153 void ScreenshotFrame::OnClose(wxCloseEvent& WXUNUSED(event))
154 {
155 Destroy();
156 }
157
158 void ScreenshotFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
159 {
160 Destroy();
161 }
162
163 void ScreenshotFrame::OnSeeScreenshots(wxCommandEvent& WXUNUSED(event))
164 {
165 wxString defaultDir = m_maskout->GetDefaultDirectory();
166
167 // Check if defaultDir already existed
168 if (!wxDirExists(defaultDir))
169 wxMkdir(defaultDir);
170
171 // Use the native file browser to open defaultDir
172 wxLaunchDefaultBrowser(wxFileSystem::FileNameToURL(defaultDir));
173 }
174
175 void ScreenshotFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
176 {
177 wxAboutDialogInfo info;
178 info.SetName(_("Automatic Screenshot Generator"));
179 info.SetVersion(_("1.0"));
180 info.SetDescription(_("This utility automatically creates screenshots of wxWidgets controls for ues in wxWidgets documentation."));
181 info.SetCopyright(_T("(C) 2008 Utensil Candel"));
182
183 wxAboutBox(info);
184 }
185
186 void ScreenshotFrame::OnCaptureFullScreen(wxCommandEvent& WXUNUSED(event))
187 {
188 // Create a DC for the whole screen area
189 wxScreenDC dcScreen;
190
191 // Get the size of the screenDC
192 wxCoord screenWidth, screenHeight;
193 dcScreen.GetSize(&screenWidth, &screenHeight);
194
195 m_maskout->Capture(0, 0, screenWidth, screenHeight, _T("fullscreen"));
196
197 // Inform the user
198 wxMessageBox(_("A screenshot of the entire screen was saved as:\n\n ") +
199 m_maskout->GetDefaultDirectoryAbsPath() + wxFileName::GetPathSeparator() + "fullscreen.png",
200 _("Full screen capture"), wxICON_INFORMATION|wxOK, this);
201 }
202
203 void ScreenshotFrame::OnCaptureRect(wxCommandEvent& WXUNUSED(event))
204 {
205 capturingRect = true;
206 wxMenuBar * menubar = this->GetMenuBar();
207 menubar->FindItem(idMenuCapRect)->Enable(false);
208 menubar->FindItem(idMenuEndCapRect)->Enable(true);
209
210 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
211
212 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
213 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
214 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
215 }
216
217 void ScreenshotFrame::OnEndCaptureRect(wxCommandEvent& WXUNUSED(event))
218 {
219 capturingRect = false;
220 wxMenuBar * menubar = this->GetMenuBar();
221 menubar->FindItem(idMenuCapRect)->Enable(true);
222 menubar->FindItem(idMenuEndCapRect)->Enable(false);
223
224 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
225
226 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
227 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
228 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
229 }
230
231 void ScreenshotFrame::OnNotebookPageChanging(
232 #if SCREENSHOTGEN_USE_AUI
233 wxAuiNotebookEvent& event
234 #else
235 wxNotebookEvent& event
236 #endif
237 )
238 {
239 if (!capturingRect)
240 {
241 event.Skip();
242 return;
243 }
244
245 wxWindow * thePage = m_notebook1->GetPage(event.GetOldSelection());
246
247 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
248 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
249 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
250
251 event.Skip();
252 }
253
254 void ScreenshotFrame::OnNotebookPageChanged(
255 #if SCREENSHOTGEN_USE_AUI
256 wxAuiNotebookEvent& event
257 #else
258 wxNotebookEvent& event
259 #endif
260 )
261 {
262 if (!capturingRect)
263 {
264 event.Skip();
265 return;
266 }
267
268 wxWindow *thePage = m_notebook1->GetPage(event.GetSelection());
269
270 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
271 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
272 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
273
274 event.Skip();
275 }
276
277 void ScreenshotFrame::OnCaptureAllControls(wxCommandEvent& WXUNUSED(event))
278 {
279 wxString dir = m_maskout->GetDefaultDirectoryAbsPath();
280
281 // check if there are other screenshots taken before
282 if (wxFileName::DirExists(dir))
283 {
284 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."),
285 _("Delete existing screenshots?"),
286 wxYES_NO|wxCANCEL|wxICON_QUESTION, this);
287 switch(choice)
288 {
289 case wxYES:
290 {
291 wxArrayString files;
292 wxDir::GetAllFiles(dir, &files, wxT("*.png"), wxDIR_FILES);
293
294 // remove all PNG files from the screenshots folder
295 int n = files.GetCount();
296 for (int i = 0; i < n; ++i)
297 wxRemoveFile(files[i]);
298 }
299 break;
300
301 case wxNO: break;
302 case wxCANCEL: return;
303 }
304 }
305
306 // proceed with the automatic screenshot capture
307
308 this->Maximize();
309
310 AutoCaptureMechanism auto_cap(m_notebook1);
311
312 auto_cap.RegisterControl(m_button1);
313 auto_cap.RegisterControl(m_staticText1);
314 auto_cap.RegisterControl(m_checkBox1, AJ_Union);
315 auto_cap.RegisterControl(m_checkBox2, AJ_UnionEnd);
316 auto_cap.RegisterControl(m_radioBtn1, AJ_Union);
317 auto_cap.RegisterControl(m_radioBtn2, AJ_UnionEnd);
318 auto_cap.RegisterControl(m_bpButton1);
319 auto_cap.RegisterControl(m_bitmap1);
320 auto_cap.RegisterControl(m_gauge1, wxT("wxGauge"));
321 auto_cap.RegisterControl(m_slider1);
322 auto_cap.RegisterControl(m_toggleBtn1, AJ_Union);
323 auto_cap.RegisterControl(m_toggleBtn2, AJ_UnionEnd);
324 auto_cap.RegisterControl(m_hyperlink1);
325 auto_cap.RegisterControl(m_spinCtrl1, AJ_RegionAdjust);
326 auto_cap.RegisterControl(m_spinBtn1);
327 auto_cap.RegisterControl(m_scrollBar1);
328
329 auto_cap.RegisterPageTurn();
330
331 auto_cap.RegisterControl(m_checkList1);
332 auto_cap.RegisterControl(m_listBox1);
333 auto_cap.RegisterControl(m_radioBox1);
334 auto_cap.RegisterControl(m_staticBox1);
335 auto_cap.RegisterControl(m_treeCtrl1);
336 auto_cap.RegisterControl(m_listCtrl1, wxT("wxListCtrl"));
337
338 auto_cap.RegisterControl(m_animationCtrl1);
339 auto_cap.RegisterControl(m_collPane1, wxT("wxCollapsiblePane"), AJ_Union);
340 auto_cap.RegisterControl(m_collPane2, AJ_UnionEnd);
341
342 auto_cap.RegisterPageTurn();
343
344 auto_cap.RegisterControl(m_textCtrl1, AJ_Union);
345 auto_cap.RegisterControl(m_textCtrl2, AJ_UnionEnd);
346 auto_cap.RegisterControl(m_richText1);
347
348 auto_cap.RegisterPageTurn();
349
350 auto_cap.RegisterControl(m_colourPicker1, wxT("wxColourPickerCtrl"));
351 auto_cap.RegisterControl(m_fontPicker1, wxT("wxFontPickerCtrl"));
352 auto_cap.RegisterControl(m_filePicker1, wxT("wxFilePickerCtrl"), AJ_RegionAdjust);
353 auto_cap.RegisterControl(m_calendar1, wxT("wxCalendarCtrl"), AJ_RegionAdjust);
354 auto_cap.RegisterControl(m_datePicker1, wxT("wxDatePickerCtrl"));
355 auto_cap.RegisterControl(m_genericDirCtrl1, wxT("wxGenericDirCtrl"));
356 auto_cap.RegisterControl(m_dirPicker1, wxT("wxDirPickerCtrl"), AJ_RegionAdjust);
357
358 auto_cap.RegisterPageTurn();
359
360 auto_cap.RegisterControl(m_choice1, AJ_Dropdown);
361 auto_cap.RegisterControl(m_comboBox1, AJ_Dropdown);
362 auto_cap.RegisterControl(m_bmpComboBox1, AJ_Dropdown);
363 auto_cap.RegisterControl(m_ownerDrawnComboBox1, AJ_Dropdown);
364 auto_cap.RegisterControl(m_comboCtrl1, AJ_Dropdown|AJ_Union);
365 auto_cap.RegisterControl(m_comboCtrl2, AJ_Dropdown|AJ_UnionEnd);
366
367 auto_cap.CaptureAll();
368
369 wxMessageBox(_("All screenshots were generated successfully in the folder:\n ") + dir,
370 _("Success"), wxOK|wxICON_INFORMATION, this);
371 }