many fixes; now the application correctly starts up
[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
29 #include "screenshot_main.h"
30 #include "ctrlmaskout.h"
31 #include "autocapture.h"
32
33 /*
34 // Global helper functions
35 enum wxBuildInfoFormat
36 {
37 short_f,
38 long_f
39 };
40
41 wxString wxbuildinfo(wxBuildInfoFormat format)
42 {
43 wxString wxbuild(wxVERSION_STRING);
44
45 if (format == long_f )
46 {
47 #if defined(__WXMSW__)
48 wxbuild << _T("-Windows");
49 #elif defined(__WXMAC__)
50 wxbuild << _T("-Mac");
51 #elif defined(__UNIX__)
52 wxbuild << _T("-Linux");
53 #endif
54
55 #if wxUSE_UNICODE
56 wxbuild << _T("-Unicode build");
57 #else
58 wxbuild << _T("-ANSI build");
59 #endif // wxUSE_UNICODE
60 }
61
62 return wxbuild;
63 }*/
64
65
66 // ----------------------------------------------------------------------------
67 // ScreenshotFrame
68 // ----------------------------------------------------------------------------
69
70 ScreenshotFrame::ScreenshotFrame(wxFrame *frame)
71 #if SCREENSHOTGEN_USE_AUI
72 : AuiGUIFrame(frame)
73 #else
74 : GUIFrame(frame)
75 #endif
76 {
77 #if wxUSE_STATUSBAR
78 statusBar->SetStatusText(_("Hello wxWidgets user!"), 0);
79 // statusBar->SetStatusText(wxbuildinfo(short_f), 1);
80 #endif
81
82 // We will hold one during the whole life time of the main frame
83 m_maskout = new CtrlMaskOut();
84
85 // At the begining, we are not specifying the rect region
86 capturingRect = false;
87
88 // Do some further customization on some controls generated by wxFormBuilder
89 InitFBControls();
90 #if SCREENSHOTGEN_USE_AUI
91 // Somehow it will be very small after I move to Aui
92 SetSize(600, 600);
93 // Maximize(true);
94 #endif
95 }
96
97 ScreenshotFrame::~ScreenshotFrame()
98 {
99 delete m_maskout;
100 }
101
102 /*
103 Do some further customization on some controls generated by wxFormBuilder.
104
105 Some controls can only be generated by wxFormBuilder without further
106 customization, e.g. unable to load a richtext file in a wxRichtextCtrl
107 during initialization.
108
109 Those customizations will be done here.
110 */
111 void ScreenshotFrame::InitFBControls()
112 {
113 // Do the default selection for wxComboBox
114 m_comboBox1->Select(0);
115
116 // To look better under gtk
117 #ifdef __WXGTK__
118 m_comboBox1->Delete(4);
119 #endif
120
121 // Add a root and some nodes for wxTreeCtrl
122 wxTreeItemId root = m_treeCtrl1->AddRoot(_("wxTreeCtrl"));
123
124 m_treeCtrl1->AppendItem(root, _("Node1"));
125
126 wxTreeItemId node2 = m_treeCtrl1->AppendItem(root, _("Node2"));
127 m_treeCtrl1->AppendItem(node2, _("Node3"));
128
129 m_treeCtrl1->ExpandAll();
130
131 // Add items into wxListCtrl
132 for(long index = 0; index < 5; index++)
133 m_listCtrl1->InsertItem( index, wxString::Format(_("Item\n(0,%d)"),index));
134
135 // Check the first item in wxCheckListBox
136 m_checkList1->Check(0);
137
138 // Load richtext.xml into wxRichtextCtrl
139 m_richText1->LoadFile(_T("richtext.xml"));
140 m_richText1->ShowPosition(335);
141 }
142
143
144
145 // ----------------------------------------------------------------------------
146 // ScreenshotFrame - event handlers
147 // ----------------------------------------------------------------------------
148
149 void ScreenshotFrame::OnClose(wxCloseEvent& WXUNUSED(event))
150 {
151 Destroy();
152 }
153
154 void ScreenshotFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
155 {
156 Destroy();
157 }
158
159 void ScreenshotFrame::OnSeeScreenshots(wxCommandEvent& WXUNUSED(event))
160 {
161 wxString defaultDir = m_maskout->GetDefaultDirectory();
162
163 // Check if defaultDir already existed
164 if(!wxDirExists(defaultDir))
165 wxMkdir(defaultDir);
166
167 // Use the native file browser to open defaultDir
168 #if defined(__WXMSW__)
169 wxExecute(_T("explorer ") + defaultDir);
170 #elif defined(__UNIX__) // nautilus is the GNOME file browser but works also for KDE
171 wxExecute(_T("nautilus ") + defaultDir);
172 #elif defined(_WXMAC_)
173 wxExecute(_T("open ") + defaultDir);
174 #else
175 wxMessageBox(_("Sorry, not Implemeted for this platform yet! Please open subdirectory \"")
176 + defaultDir
177 + _("\" manually.") );
178 #endif
179 }
180
181 void ScreenshotFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
182 {
183 wxAboutDialogInfo info;
184 info.SetName(_("Automatic Screenshot Generator"));
185 info.SetVersion(_("1.0"));
186 info.SetDescription(_("This utility automatically creates screenshots of wxWidgets controls for ues in wxWidgets documentation."));
187 info.SetCopyright(_T("(C) 2008 Utensil Candel"));
188
189 wxAboutBox(info);
190 }
191
192 void ScreenshotFrame::OnCaptureFullScreen(wxCommandEvent& WXUNUSED(event))
193 {
194 // Create a DC for the whole screen area
195 wxScreenDC dcScreen;
196
197 // Get the size of the screenDC
198 wxCoord screenWidth, screenHeight;
199 dcScreen.GetSize(&screenWidth, &screenHeight);
200
201 m_maskout->Capture(0, 0, screenWidth, screenHeight, _T("fullscreen"));
202 }
203
204 void ScreenshotFrame::OnCaptureRect(wxCommandEvent& WXUNUSED(event))
205 {
206 capturingRect = true;
207 wxMenuBar * menubar = this->GetMenuBar();
208 menubar->FindItem(idMenuCapRect)->Enable(false);
209 menubar->FindItem(idMenuEndCapRect)->Enable(true);
210
211 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
212
213 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
214 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
215 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
216 }
217
218 void ScreenshotFrame::OnEndCaptureRect(wxCommandEvent& WXUNUSED(event))
219 {
220 capturingRect = false;
221 wxMenuBar * menubar = this->GetMenuBar();
222 menubar->FindItem(idMenuCapRect)->Enable(true);
223 menubar->FindItem(idMenuEndCapRect)->Enable(false);
224
225 wxWindow * thePage = m_notebook1->GetPage(m_notebook1->GetSelection());
226
227 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
228 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
229 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
230 }
231
232 void ScreenshotFrame::OnNotebookPageChanging(
233 #if SCREENSHOTGEN_USE_AUI
234 wxAuiNotebookEvent& event
235 #else
236 wxNotebookEvent& event
237 #endif
238 )
239 {
240 if (!capturingRect)
241 {
242 event.Skip();
243 return;
244 }
245
246 wxWindow * thePage = m_notebook1->GetPage(event.GetOldSelection());
247
248 thePage->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
249 thePage->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
250 thePage->Disconnect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
251
252 event.Skip();
253 }
254
255 void ScreenshotFrame::OnNotebookPageChanged(
256 #if SCREENSHOTGEN_USE_AUI
257 wxAuiNotebookEvent& event
258 #else
259 wxNotebookEvent& event
260 #endif
261 )
262 {
263 if(!capturingRect)
264 {
265 event.Skip();
266 return;
267 }
268
269 wxWindow *thePage = m_notebook1->GetPage(event.GetSelection());
270
271 thePage->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonDown ), NULL, m_maskout);
272 thePage->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( CtrlMaskOut::OnLeftButtonUp ), NULL, m_maskout);
273 thePage->Connect( wxEVT_MOTION, wxMouseEventHandler( CtrlMaskOut::OnMouseMoving ), NULL, m_maskout);
274
275 event.Skip();
276 }
277
278 void ScreenshotFrame::OnCaptureAllControls(wxCommandEvent& WXUNUSED(event))
279 {
280 wxString dir = wxT("screenshots");
281
282 if (wxFileName::DirExists(dir))
283 {
284 int choice = wxMessageBox(_("It seems that you have already generated some screenshots.\nClick YES to delete them all(recommended), NO to preserve them\nCANCEL to cancel this auto-capture(so you can save them elsewhere)."),
285 _("Do you want to delete the 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 int n = files.GetCount();
295 for (int i = 0; i < n; ++i)
296 wxRemoveFile(files[i]);
297 }
298 break;
299
300 case wxNO : break;
301 case wxCANCEL : return;
302 }
303 }
304
305 this->Maximize();
306
307 AutoCaptureMechanism auto_cap(m_notebook1);
308
309 auto_cap.RegisterControl(m_button1);
310 auto_cap.RegisterControl(m_staticText1);
311 auto_cap.RegisterControl(m_checkBox1, AJ_Union);
312 auto_cap.RegisterControl(m_checkBox2, AJ_UnionEnd);
313 auto_cap.RegisterControl(m_radioBtn1, AJ_Union);
314 auto_cap.RegisterControl(m_radioBtn2, AJ_UnionEnd);
315 auto_cap.RegisterControl(m_bpButton1);
316 auto_cap.RegisterControl(m_bitmap1);
317 auto_cap.RegisterControl(m_gauge1, wxT("wxGauge"));
318 auto_cap.RegisterControl(m_slider1);
319 auto_cap.RegisterControl(m_toggleBtn1, AJ_Union);
320 auto_cap.RegisterControl(m_toggleBtn2, AJ_UnionEnd);
321 auto_cap.RegisterControl(m_hyperlink1);
322 auto_cap.RegisterControl(m_spinCtrl1, AJ_RegionAdjust);
323 auto_cap.RegisterControl(m_spinBtn1);
324 auto_cap.RegisterControl(m_scrollBar1);
325
326 auto_cap.RegisterPageTurn();
327
328 auto_cap.RegisterControl(m_checkList1);
329 auto_cap.RegisterControl(m_listBox1);
330 auto_cap.RegisterControl(m_radioBox1);
331 auto_cap.RegisterControl(m_staticBox1);
332 auto_cap.RegisterControl(m_treeCtrl1);
333 auto_cap.RegisterControl(m_listCtrl1, wxT("wxListCtrl"));
334
335 auto_cap.RegisterControl(m_animationCtrl1);
336 auto_cap.RegisterControl(m_collPane1, wxT("wxCollapsiblePane"), AJ_Union);
337 auto_cap.RegisterControl(m_collPane2, AJ_UnionEnd);
338
339 auto_cap.RegisterPageTurn();
340
341 auto_cap.RegisterControl(m_textCtrl1, AJ_Union);
342 auto_cap.RegisterControl(m_textCtrl2, AJ_UnionEnd);
343 auto_cap.RegisterControl(m_richText1);
344
345 auto_cap.RegisterPageTurn();
346
347 auto_cap.RegisterControl(m_colourPicker1, wxT("wxColourPickerCtrl"));
348 auto_cap.RegisterControl(m_fontPicker1, wxT("wxFontPickerCtrl"));
349 auto_cap.RegisterControl(m_filePicker1, wxT("wxFilePickerCtrl"), AJ_RegionAdjust);
350 auto_cap.RegisterControl(m_calendar1, wxT("wxCalendarCtrl"), AJ_RegionAdjust);
351 auto_cap.RegisterControl(m_datePicker1, wxT("wxDatePickerCtrl"));
352 auto_cap.RegisterControl(m_genericDirCtrl1, wxT("wxGenericDirCtrl"));
353 auto_cap.RegisterControl(m_dirPicker1, wxT("wxDirPickerCtrl"), AJ_RegionAdjust);
354
355 auto_cap.RegisterPageTurn();
356
357 auto_cap.RegisterControl(m_choice1, AJ_Dropdown);
358 auto_cap.RegisterControl(m_comboBox1, AJ_Dropdown);
359 auto_cap.RegisterControl(m_bmpComboBox1, AJ_Dropdown);
360 auto_cap.RegisterControl(m_ownerDrawnComboBox1, AJ_Dropdown);
361 auto_cap.RegisterControl(m_comboCtrl1, AJ_Dropdown|AJ_Union);
362 auto_cap.RegisterControl(m_comboCtrl2, AJ_Dropdown|AJ_UnionEnd);
363
364 auto_cap.CaptureAll();
365
366 wxMessageBox(_("All screenshots are generated successfully.\nSelect \"File->See screenshots\" to see them."),
367 _("Success"), wxOK, this);
368 }