]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/dbgrptg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dbgrptg.cpp
3 // Purpose: implementation of wxDebugReportPreviewStd
4 // Author: Vadim Zeitlin, Andrej Putrin
8 // Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DEBUGREPORT && wxUSE_XML
28 #include "wx/debugrpt.h"
32 #include "wx/checklst.h"
33 #include "wx/textctrl.h"
35 #include "wx/stattext.h"
36 #include "wx/filedlg.h"
37 #include "wx/valtext.h"
38 #include "wx/button.h"
41 #include "wx/filename.h"
43 #include "wx/mimetype.h"
45 #include "wx/statline.h"
48 #include "wx/evtloop.h" // for SetCriticalWindow()
51 // ----------------------------------------------------------------------------
52 // wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
53 // ----------------------------------------------------------------------------
55 class wxDumpPreviewDlg
: public wxDialog
58 wxDumpPreviewDlg ( wxWindow
* parent
,
59 const wxString
& title
,
60 const wxString
& text
);
66 DECLARE_NO_COPY_CLASS ( wxDumpPreviewDlg
)
69 wxDumpPreviewDlg :: wxDumpPreviewDlg ( wxWindow
* parent
,
70 const wxString
& title
,
72 : wxDialog ( parent
, wxID_ANY
, title
,
73 wxDefaultPosition
, wxDefaultSize
,
74 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
79 // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
80 // faster than with wxTE_RICH
81 m_text
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
82 wxPoint ( 0 , 0 ), wxDefaultSize
,
87 m_text
-> SetValue ( text
);
89 // use fixed-width font
90 m_text
-> SetFont ( wxFont ( 12 , wxFONTFAMILY_TELETYPE
,
91 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
93 wxButton
* btnClose
= new wxButton ( this , wxID_CANCEL
, _ ( "Close" ));
99 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
),
100 * sizerBtns
= new wxBoxSizer ( wxHORIZONTAL
);
102 sizerBtns
-> Add ( btnClose
, 0 , 0 , 1 );
104 sizerTop
-> Add ( m_text
, 1 , wxEXPAND
);
105 sizerTop
-> Add ( sizerBtns
, 0 , wxALIGN_RIGHT
| wxTOP
| wxBOTTOM
| wxRIGHT
, 1 );
110 // make the text window bigger to show more contents of the file
111 sizerTop
-> SetItemMinSize ( m_text
, 600 , 300 );
120 // ----------------------------------------------------------------------------
121 // wxDumpOpenExternalDlg: choose a command for opening the given file
122 // ----------------------------------------------------------------------------
124 class wxDumpOpenExternalDlg
: public wxDialog
127 wxDumpOpenExternalDlg ( wxWindow
* parent
, const wxFileName
& filename
);
129 // return the command chosed by user to open this file
130 const wxString
& GetCommand () const { return m_command
; }
137 void OnBrowse ( wxCommandEvent
& event
);
138 #endif // wxUSE_FILEDLG
140 DECLARE_EVENT_TABLE ()
141 DECLARE_NO_COPY_CLASS ( wxDumpOpenExternalDlg
)
144 BEGIN_EVENT_TABLE ( wxDumpOpenExternalDlg
, wxDialog
)
147 EVT_BUTTON ( wxID_MORE
, wxDumpOpenExternalDlg :: OnBrowse
)
153 wxDumpOpenExternalDlg :: wxDumpOpenExternalDlg ( wxWindow
* parent
,
154 const wxFileName
& filename
)
159 _ ( "Open file \" %s \" " ),
160 filename
. GetFullPath (). c_str ()
166 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
167 sizerTop
-> Add ( new wxStaticText ( this , wxID_ANY
,
170 _ ( "Enter command to open file \" %s \" :" ),
171 filename
. GetFullName (). c_str ()
173 wxSizerFlags (). Border ());
175 wxSizer
* sizerH
= new wxBoxSizer ( wxHORIZONTAL
);
177 wxTextCtrl
* command
= new wxTextCtrl
183 wxSize ( 250 , wxDefaultCoord
),
186 , wxTextValidator ( wxFILTER_NONE
, & m_command
)
190 wxSizerFlags ( 1 ). Align ( wxALIGN_CENTER_VERTICAL
));
194 wxButton
* browse
= new wxButton ( this , wxID_MORE
, wxT ( ">>" ),
195 wxDefaultPosition
, wxDefaultSize
,
198 wxSizerFlags ( 0 ). Align ( wxALIGN_CENTER_VERTICAL
). Border ( wxLEFT
));
200 #endif // wxUSE_FILEDLG
202 sizerTop
-> Add ( sizerH
, wxSizerFlags ( 0 ). Expand (). Border ());
204 sizerTop
-> Add ( new wxStaticLine ( this ), wxSizerFlags (). Expand (). Border ());
206 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
),
207 wxSizerFlags (). Align ( wxALIGN_RIGHT
). Border ());
222 void wxDumpOpenExternalDlg :: OnBrowse ( wxCommandEvent
& )
224 wxFileName
fname ( m_command
);
225 wxFileDialog
dlg ( this ,
226 wxFileSelectorPromptStr
,
227 fname
. GetPathWithSep (),
230 , _ ( "Executable files (*.exe)|*.exe|All files (*.*)|*.*||" )
233 if ( dlg
. ShowModal () == wxID_OK
)
235 m_command
= dlg
. GetPath ();
236 TransferDataToWindow ();
240 #endif // wxUSE_FILEDLG
242 // ----------------------------------------------------------------------------
243 // wxDebugReportDialog: class showing debug report to the user
244 // ----------------------------------------------------------------------------
246 class wxDebugReportDialog
: public wxDialog
249 wxDebugReportDialog ( wxDebugReport
& dbgrpt
);
251 virtual bool TransferDataToWindow ();
252 virtual bool TransferDataFromWindow ();
255 void OnView ( wxCommandEvent
& );
256 void OnViewUpdate ( wxUpdateUIEvent
& );
257 void OnOpen ( wxCommandEvent
& );
260 // small helper: add wxEXPAND and wxALL flags
261 static wxSizerFlags
SizerFlags ( int proportion
)
263 return wxSizerFlags ( proportion
). Expand (). Border ();
267 wxDebugReport
& m_dbgrpt
;
269 wxCheckListBox
* m_checklst
;
272 wxArrayString m_files
;
274 DECLARE_EVENT_TABLE ()
275 DECLARE_NO_COPY_CLASS ( wxDebugReportDialog
)
278 // ============================================================================
279 // wxDebugReportDialog implementation
280 // ============================================================================
282 BEGIN_EVENT_TABLE ( wxDebugReportDialog
, wxDialog
)
283 EVT_BUTTON ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnView
)
284 EVT_UPDATE_UI ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnViewUpdate
)
285 EVT_BUTTON ( wxID_OPEN
, wxDebugReportDialog :: OnOpen
)
286 EVT_UPDATE_UI ( wxID_OPEN
, wxDebugReportDialog :: OnViewUpdate
)
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 wxDebugReportDialog :: wxDebugReportDialog ( wxDebugReport
& dbgrpt
)
295 : wxDialog ( NULL
, wxID_ANY
,
296 wxString :: Format ( _ ( "Debug report \" %s \" " ),
297 dbgrpt
. GetReportName (). c_str ()),
300 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
),
303 // upper part of the dialog: explanatory message
305 wxString debugDir
= dbgrpt
. GetDirectory ();
307 // The temporary directory can be the short form on Windows;
308 // normalize it for the benefit of users.
310 wxFileName
debugDirFilename ( debugDir
, wxEmptyString
);
311 debugDirFilename
. Normalize ( wxPATH_NORM_LONG
);
312 debugDir
= debugDirFilename
. GetPath ();
314 msg
<< _ ( "A debug report has been generated in the directory \n " )
316 << _T ( " \" " ) << debugDir
<< _T ( " \"\n " )
318 << _ ( "The report contains the files listed below. If any of these files contain private information, \n please uncheck them and they will be removed from the report. \n " )
320 << _ ( "If you wish to suppress this debug report completely, please choose the \" Cancel \" button, \n but be warned that it may hinder improving the program, so if \n at all possible please do continue with the report generation. \n " )
322 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
323 << _T ( " \n\n " ); // just some white space to separate from other stuff
325 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
326 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
327 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
329 wxSizer
* sizerPreview
=
330 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
331 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
333 // ... and the list of files in this debug report with buttons to view them
334 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
335 sizerFileBtns
-> AddStretchSpacer ( 1 );
336 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _T ( "&View..." )),
337 wxSizerFlags (). Border ( wxBOTTOM
));
338 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _T ( "&Open..." )),
339 wxSizerFlags (). Border ( wxTOP
));
340 sizerFileBtns
-> AddStretchSpacer ( 1 );
342 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
344 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
345 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
346 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
348 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
351 // lower part of the dialog: notes field
352 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
354 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
356 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
357 wxDefaultPosition
, wxDefaultSize
,
360 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
361 sizerNotes
-> Add ( m_notes
, flagsExpand
);
364 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
365 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
366 sizerTop
-> AddSpacer ( 5 );
367 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
368 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
370 SetSizerAndFit ( sizerTop
);
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 bool wxDebugReportDialog :: TransferDataToWindow ()
381 // all files are included in the report by default
382 const size_t count
= m_dbgrpt
. GetFilesCount ();
383 for ( size_t n
= 0 ; n
< count
; n
++ )
387 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
389 m_checklst
-> Append ( name
+ _T ( " (" ) + desc
+ _T ( ')' ));
390 m_checklst
-> Check ( n
);
399 bool wxDebugReportDialog :: TransferDataFromWindow ()
401 // any unchecked files should be removed from the report
402 const size_t count
= m_checklst
-> GetCount ();
403 for ( size_t n
= 0 ; n
< count
; n
++ )
405 if ( ! m_checklst
-> IsChecked ( n
) )
407 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
411 // if the user entered any notes, add them to the report
412 const wxString notes
= m_notes
-> GetValue ();
413 if ( ! notes
. empty () )
415 // for now filename fixed, could make it configurable in the future...
416 m_dbgrpt
. AddText ( _T ( "notes.txt" ), notes
, _T ( "user notes" ));
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
428 const int sel
= m_checklst
-> GetSelection ();
429 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnView()" ) );
431 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
434 wxFFile
file ( fn
. GetFullPath ());
435 if ( file
. IsOpened () && file
. ReadAll (& str
) )
437 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
442 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
444 const int sel
= m_checklst
-> GetSelection ();
445 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnOpen()" ) );
447 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
449 // try to get the command to open this kind of files ourselves
453 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
456 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
459 #endif // wxUSE_MIMETYPE
461 // if we couldn't, ask the user
462 if ( command
. empty () )
464 wxDumpOpenExternalDlg
dlg ( this , fn
);
465 if ( dlg
. ShowModal () == wxID_OK
)
467 // get the command chosen by the user and append file name to it
469 // if we don't have place marker for file name in the command...
470 wxString cmd
= dlg
. GetCommand ();
474 if ( cmd
. find ( _T ( '%' )) != wxString :: npos
)
476 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
479 #endif // wxUSE_MIMETYPE
481 // append the file name to the end
482 command
<< cmd
<< _T ( " \" " ) << fn
. GetFullPath () << _T ( '"' );
488 if ( ! command
. empty () )
489 :: wxExecute ( command
);
492 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
494 int sel
= m_checklst
-> GetSelection ();
497 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
498 event
. Enable ( fn
. FileExists ());
505 // ============================================================================
506 // wxDebugReportPreviewStd implementation
507 // ============================================================================
509 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
511 if ( ! dbgrpt
. GetFilesCount () )
514 wxDebugReportDialog
dlg ( dbgrpt
);
517 // before entering the event loop (from ShowModal()), block the event
518 // handling for all other windows as this could result in more crashes
519 wxEventLoop :: SetCriticalWindow (& dlg
);
522 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
525 #endif // wxUSE_DEBUGREPORT && wxUSE_XML