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