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