other cleanup; adjusted english in the UI
[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 // Do the default selection for wxComboBox
84 m_comboBox1->Select(0);
85
86 // To look better under gtk
87 #ifdef __WXGTK__
88 m_comboBox1->Delete(4);
89 #endif
90
91 // Add a root and some nodes for wxTreeCtrl
92 wxTreeItemId root = m_treeCtrl1->AddRoot(_("wxTreeCtrl"));
93 m_treeCtrl1->AppendItem(root, _("Node1"));
94 wxTreeItemId node2 = m_treeCtrl1->AppendItem(root, _("Node2"));
95 m_treeCtrl1->AppendItem(node2, _("Node3"));
96 m_treeCtrl1->ExpandAll();
97
98 // Add items into wxListCtrl
99 for(long index = 0; index < 5; index++)
100 m_listCtrl1->InsertItem( index, wxString::Format(_("Item\n(0,%d)"),index));
101
102 // Check the first item in wxCheckListBox
103 m_checkList1->Check(0);
104
105 // Load richtext.xml into wxRichtextCtrl
106 m_richText1->LoadFile(_T("richtext.xml"));
107 //m_richText1->ShowPosition(335);
108
109 // select first page in the main notebook ctrl
110 m_notebook1->ChangeSelection(0);
111
112 // set minimum size hints
113 GetSizer()->SetSizeHints(this);
114 }
115
116
117
118 // ----------------------------------------------------------------------------
119 // ScreenshotFrame - event handlers
120 // ----------------------------------------------------------------------------
121
122 void ScreenshotFrame::OnClose(wxCloseEvent& WXUNUSED(event))
123 {
124 Destroy();
125 }
126
127 void ScreenshotFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
128 {
129 Destroy();
130 }
131
132 void ScreenshotFrame::OnSeeScreenshots(wxCommandEvent& WXUNUSED(event))
133 {
134 wxString defaultDir = m_maskout->GetDefaultDirectory();
135
136 // Check if defaultDir already existed
137 if (!wxDirExists(defaultDir))
138 wxMkdir(defaultDir);
139
140 // Use the native file browser to open defaultDir
141 wxLaunchDefaultBrowser(wxFileSystem::FileNameToURL(defaultDir));
142 }
143
144 void ScreenshotFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
145 {
146 wxAboutDialogInfo info;
147 info.SetName(_("Automatic Screenshot Generator"));
148 info.SetVersion(_("1.0"));
149 info.SetDescription(_("This utility automatically creates screenshots of wxWidgets controls for ues in wxWidgets documentation."));
150 info.SetCopyright(_T("(C) 2008 Utensil Candel"));
151
152 wxAboutBox(info);
153 }
154
155 void ScreenshotFrame::OnCaptureFullScreen(wxCommandEvent& WXUNUSED(event))
156 {
157 // Create a DC for the whole screen area
158 wxScreenDC dcScreen;
159
160 // Get the size of the screenDC
161 wxCoord screenWidth, screenHeight;
162 dcScreen.GetSize(&screenWidth, &screenHeight);
163
164 m_maskout->Capture(0, 0, screenWidth, screenHeight, _T("fullscreen"));
165
166 // Inform the user
167 wxMessageBox(_("A screenshot of the entire screen was saved as:\n\n ") +
168 m_maskout->GetDefaultDirectoryAbsPath() + wxFileName::GetPathSeparator() + "fullscreen.png",
169 _("Full screen capture"), wxICON_INFORMATION|wxOK, this);
170 }
171
172 void ScreenshotFrame::OnCaptureRect(wxCommandEvent& WXUNUSED(event))
173 {
174 capturingRect = true;
175 wxMenuBar * menubar = this->GetMenuBar();
176 menubar->FindItem(idMenuCapRect)->Enable(false);
177 menubar->FindItem(idMenuEndCapRect)->Enable(true);
178
179 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
180
181 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
182 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
183 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
184 }
185
186 void ScreenshotFrame::OnEndCaptureRect(wxCommandEvent& WXUNUSED(event))
187 {
188 capturingRect = false;
189 wxMenuBar * menubar = this->GetMenuBar();
190 menubar->FindItem(idMenuCapRect)->Enable(true);
191 menubar->FindItem(idMenuEndCapRect)->Enable(false);
192
193 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
194
195 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
196 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
197 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
198 }
199
200 void ScreenshotFrame::OnNotebookPageChanging(
201 #if SCREENSHOTGEN_USE_AUI
202 wxAuiNotebookEvent& event
203 #else
204 wxNotebookEvent& event
205 #endif
206 )
207 {
208 if (!capturingRect)
209 {
210 event.Skip();
211 return;
212 }
213
214 wxWindow * thePage = m_notebook1->GetPage(event.GetOldSelection());
215
216 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
217 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
218 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
219
220 event.Skip();
221 }
222
223 void ScreenshotFrame::OnNotebookPageChanged(
224 #if SCREENSHOTGEN_USE_AUI
225 wxAuiNotebookEvent& event
226 #else
227 wxNotebookEvent& event
228 #endif
229 )
230 {
231 if (!capturingRect)
232 {
233 event.Skip();
234 return;
235 }
236
237 wxWindow *thePage = m_notebook1->GetPage(event.GetSelection());
238
239 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
240 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
241 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
242
243 event.Skip();
244 }
245
246 void ScreenshotFrame::OnCaptureAllControls(wxCommandEvent& WXUNUSED(event))
247 {
248 wxString dir = m_maskout->GetDefaultDirectoryAbsPath();
249
250 // check if there are other screenshots taken before
251 if (wxFileName::DirExists(dir))
252 {
253 int choice = wxMessageBox(_("It seems that you have already generated some screenshots.\nClick YES to delete them all (recommended) or NO to preserve them.\nClick CANCEL to cancel this auto-capture operation."),
254 _("Delete existing screenshots?"),
255 wxYES_NO|wxCANCEL|wxICON_QUESTION, this);
256 switch(choice)
257 {
258 case wxYES:
259 {
260 wxArrayString files;
261 wxDir::GetAllFiles(dir, &files, wxT("*.png"), wxDIR_FILES);
262
263 // remove all PNG files from the screenshots folder
264 int n = files.GetCount();
265 for (int i = 0; i < n; ++i)
266 wxRemoveFile(files[i]);
267 }
268 break;
269
270 case wxNO: break;
271 case wxCANCEL: return;
272 }
273 }
274
275 // proceed with the automatic screenshot capture
276
277 this->Maximize();
278
279 AutoCaptureMechanism auto_cap(m_notebook1);
280
281 auto_cap.RegisterControl(m_button1);
282 auto_cap.RegisterControl(m_staticText1);
283 auto_cap.RegisterControl(m_checkBox1, AJ_Union);
284 auto_cap.RegisterControl(m_checkBox2, AJ_UnionEnd);
285 auto_cap.RegisterControl(m_radioBtn1, AJ_Union);
286 auto_cap.RegisterControl(m_radioBtn2, AJ_UnionEnd);
287 auto_cap.RegisterControl(m_bpButton1);
288 auto_cap.RegisterControl(m_bitmap1);
289 auto_cap.RegisterControl(m_gauge1, wxT("wxGauge"));
290 auto_cap.RegisterControl(m_slider1);
291 auto_cap.RegisterControl(m_toggleBtn1, AJ_Union);
292 auto_cap.RegisterControl(m_toggleBtn2, AJ_UnionEnd);
293 auto_cap.RegisterControl(m_hyperlink1);
294 auto_cap.RegisterControl(m_spinCtrl1, AJ_RegionAdjust);
295 auto_cap.RegisterControl(m_spinBtn1);
296 auto_cap.RegisterControl(m_scrollBar1);
297
298 auto_cap.RegisterPageTurn();
299
300 auto_cap.RegisterControl(m_checkList1);
301 auto_cap.RegisterControl(m_listBox1);
302 auto_cap.RegisterControl(m_radioBox1);
303 auto_cap.RegisterControl(m_staticBox1);
304 auto_cap.RegisterControl(m_treeCtrl1);
305 auto_cap.RegisterControl(m_listCtrl1, wxT("wxListCtrl"));
306
307 auto_cap.RegisterControl(m_animationCtrl1);
308 auto_cap.RegisterControl(m_collPane1, wxT("wxCollapsiblePane"), AJ_Union);
309 auto_cap.RegisterControl(m_collPane2, AJ_UnionEnd);
310
311 auto_cap.RegisterPageTurn();
312
313 auto_cap.RegisterControl(m_textCtrl1, AJ_Union);
314 auto_cap.RegisterControl(m_textCtrl2, AJ_UnionEnd);
315 auto_cap.RegisterControl(m_richText1);
316
317 auto_cap.RegisterPageTurn();
318
319 auto_cap.RegisterControl(m_colourPicker1, wxT("wxColourPickerCtrl"));
320 auto_cap.RegisterControl(m_fontPicker1, wxT("wxFontPickerCtrl"));
321 auto_cap.RegisterControl(m_filePicker1, wxT("wxFilePickerCtrl"), AJ_RegionAdjust);
322 auto_cap.RegisterControl(m_calendar1, wxT("wxCalendarCtrl"), AJ_RegionAdjust);
323 auto_cap.RegisterControl(m_datePicker1, wxT("wxDatePickerCtrl"));
324 auto_cap.RegisterControl(m_genericDirCtrl1, wxT("wxGenericDirCtrl"));
325 auto_cap.RegisterControl(m_dirPicker1, wxT("wxDirPickerCtrl"), AJ_RegionAdjust);
326
327 auto_cap.RegisterPageTurn();
328
329 auto_cap.RegisterControl(m_choice1, AJ_Dropdown);
330 auto_cap.RegisterControl(m_comboBox1, AJ_Dropdown);
331 auto_cap.RegisterControl(m_bmpComboBox1, AJ_Dropdown);
332 auto_cap.RegisterControl(m_ownerDrawnComboBox1, AJ_Dropdown);
333 auto_cap.RegisterControl(m_comboCtrl1, AJ_Dropdown|AJ_Union);
334 auto_cap.RegisterControl(m_comboCtrl2, AJ_Dropdown|AJ_UnionEnd);
335
336 auto_cap.CaptureAll();
337
338 wxMessageBox(_("All screenshots were generated successfully in the folder:\n ") + dir,
339 _("Success"), wxOK|wxICON_INFORMATION, this);
340 }