]>
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 // Licence: 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"
47 #include "wx/mimetype.h"
49 #include "wx/statline.h"
52 #include "wx/evtloop.h" // for SetCriticalWindow()
53 #include "wx/scopeguard.h"
56 // ----------------------------------------------------------------------------
57 // wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
58 // ----------------------------------------------------------------------------
60 class wxDumpPreviewDlg
: public wxDialog
63 wxDumpPreviewDlg ( wxWindow
* parent
,
64 const wxString
& title
,
65 const wxString
& text
);
71 wxDECLARE_NO_COPY_CLASS ( wxDumpPreviewDlg
);
74 wxDumpPreviewDlg :: wxDumpPreviewDlg ( wxWindow
* parent
,
75 const wxString
& title
,
77 : wxDialog ( parent
, wxID_ANY
, title
,
78 wxDefaultPosition
, wxDefaultSize
,
79 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
84 // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
85 // faster than with wxTE_RICH
86 m_text
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
87 wxPoint ( 0 , 0 ), wxDefaultSize
,
92 m_text
-> SetValue ( text
);
94 // use fixed-width font
95 m_text
-> SetFont ( wxFont ( 12 , wxFONTFAMILY_TELETYPE
,
96 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
98 wxButton
* btnClose
= new wxButton ( this , wxID_CANCEL
, _ ( "Close" ));
104 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
),
105 * sizerBtns
= new wxBoxSizer ( wxHORIZONTAL
);
107 sizerBtns
-> Add ( btnClose
, 0 , 0 , 1 );
109 sizerTop
-> Add ( m_text
, 1 , wxEXPAND
);
110 sizerTop
-> Add ( sizerBtns
, 0 , wxALIGN_RIGHT
| wxTOP
| wxBOTTOM
| wxRIGHT
, 1 );
115 // make the text window bigger to show more contents of the file
116 sizerTop
-> SetItemMinSize ( m_text
, 600 , 300 );
125 // ----------------------------------------------------------------------------
126 // wxDumpOpenExternalDlg: choose a command for opening the given file
127 // ----------------------------------------------------------------------------
129 class wxDumpOpenExternalDlg
: public wxDialog
132 wxDumpOpenExternalDlg ( wxWindow
* parent
, const wxFileName
& filename
);
134 // return the command chosed by user to open this file
135 const wxString
& GetCommand () const { return m_command
; }
142 void OnBrowse ( wxCommandEvent
& event
);
143 #endif // wxUSE_FILEDLG
145 DECLARE_EVENT_TABLE ()
146 wxDECLARE_NO_COPY_CLASS ( wxDumpOpenExternalDlg
);
149 BEGIN_EVENT_TABLE ( wxDumpOpenExternalDlg
, wxDialog
)
152 EVT_BUTTON ( wxID_MORE
, wxDumpOpenExternalDlg :: OnBrowse
)
158 wxDumpOpenExternalDlg :: wxDumpOpenExternalDlg ( wxWindow
* parent
,
159 const wxFileName
& filename
)
164 _ ( "Open file \" %s \" " ),
165 filename
. GetFullPath (). c_str ()
171 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
172 sizerTop
-> Add ( new wxStaticText ( this , wxID_ANY
,
175 _ ( "Enter command to open file \" %s \" :" ),
176 filename
. GetFullName (). c_str ()
178 wxSizerFlags (). Border ());
180 wxSizer
* sizerH
= new wxBoxSizer ( wxHORIZONTAL
);
182 wxTextCtrl
* command
= new wxTextCtrl
188 wxSize ( 250 , wxDefaultCoord
),
191 , wxTextValidator ( wxFILTER_NONE
, & m_command
)
195 wxSizerFlags ( 1 ). Align ( wxALIGN_CENTER_VERTICAL
));
199 wxButton
* browse
= new wxButton ( this , wxID_MORE
, wxT ( ">>" ),
200 wxDefaultPosition
, wxDefaultSize
,
203 wxSizerFlags ( 0 ). Align ( wxALIGN_CENTER_VERTICAL
). Border ( wxLEFT
));
205 #endif // wxUSE_FILEDLG
207 sizerTop
-> Add ( sizerH
, wxSizerFlags ( 0 ). Expand (). Border ());
209 sizerTop
-> Add ( new wxStaticLine ( this ), wxSizerFlags (). Expand (). Border ());
211 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
),
212 wxSizerFlags (). Align ( wxALIGN_RIGHT
). Border ());
227 void wxDumpOpenExternalDlg :: OnBrowse ( wxCommandEvent
& )
229 wxFileName
fname ( m_command
);
230 wxFileDialog
dlg ( this ,
231 wxFileSelectorPromptStr
,
232 fname
. GetPathWithSep (),
235 , _ ( "Executable files (*.exe)|*.exe|" ) + wxALL_FILES
238 if ( dlg
. ShowModal () == wxID_OK
)
240 m_command
= dlg
. GetPath ();
241 TransferDataToWindow ();
245 #endif // wxUSE_FILEDLG
247 // ----------------------------------------------------------------------------
248 // wxDebugReportDialog: class showing debug report to the user
249 // ----------------------------------------------------------------------------
251 class wxDebugReportDialog
: public wxDialog
254 wxDebugReportDialog ( wxDebugReport
& dbgrpt
);
256 virtual bool TransferDataToWindow ();
257 virtual bool TransferDataFromWindow ();
260 void OnView ( wxCommandEvent
& );
261 void OnViewUpdate ( wxUpdateUIEvent
& );
262 void OnOpen ( wxCommandEvent
& );
265 // small helper: add wxEXPAND and wxALL flags
266 static wxSizerFlags
SizerFlags ( int proportion
)
268 return wxSizerFlags ( proportion
). Expand (). Border ();
272 wxDebugReport
& m_dbgrpt
;
274 wxCheckListBox
* m_checklst
;
277 wxArrayString m_files
;
279 DECLARE_EVENT_TABLE ()
280 wxDECLARE_NO_COPY_CLASS ( wxDebugReportDialog
);
283 // ============================================================================
284 // wxDebugReportDialog implementation
285 // ============================================================================
287 BEGIN_EVENT_TABLE ( wxDebugReportDialog
, wxDialog
)
288 EVT_BUTTON ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnView
)
289 EVT_UPDATE_UI ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnViewUpdate
)
290 EVT_BUTTON ( wxID_OPEN
, wxDebugReportDialog :: OnOpen
)
291 EVT_UPDATE_UI ( wxID_OPEN
, wxDebugReportDialog :: OnViewUpdate
)
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 wxDebugReportDialog :: wxDebugReportDialog ( wxDebugReport
& dbgrpt
)
300 : wxDialog ( NULL
, wxID_ANY
,
301 wxString :: Format ( _ ( "Debug report \" %s \" " ),
302 dbgrpt
. GetReportName (). c_str ()),
305 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
),
308 // upper part of the dialog: explanatory message
310 wxString debugDir
= dbgrpt
. GetDirectory ();
312 // The temporary directory can be the short form on Windows;
313 // normalize it for the benefit of users.
315 wxFileName
debugDirFilename ( debugDir
, wxEmptyString
);
316 debugDirFilename
. Normalize ( wxPATH_NORM_LONG
);
317 debugDir
= debugDirFilename
. GetPath ();
319 msg
<< _ ( "A debug report has been generated in the directory \n " )
321 << wxT ( " \" " ) << debugDir
<< wxT ( " \"\n " )
323 << _ ( "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 " )
325 << _ ( "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 " )
327 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
328 << wxT ( " \n\n " ); // just some white space to separate from other stuff
330 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
331 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
332 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
334 wxSizer
* sizerPreview
=
335 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
336 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
338 // ... and the list of files in this debug report with buttons to view them
339 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
340 sizerFileBtns
-> AddStretchSpacer ( 1 );
341 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _ ( "&View..." )),
342 wxSizerFlags (). Border ( wxBOTTOM
));
343 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _ ( "&Open..." )),
344 wxSizerFlags (). Border ( wxTOP
));
345 sizerFileBtns
-> AddStretchSpacer ( 1 );
347 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
349 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
350 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
351 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
353 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
356 // lower part of the dialog: notes field
357 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
359 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
361 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
362 wxDefaultPosition
, wxDefaultSize
,
365 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
366 sizerNotes
-> Add ( m_notes
, flagsExpand
);
369 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
370 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
371 sizerTop
-> AddSpacer ( 5 );
372 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
373 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
375 SetSizerAndFit ( sizerTop
);
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 bool wxDebugReportDialog :: TransferDataToWindow ()
386 // all files are included in the report by default
387 const size_t count
= m_dbgrpt
. GetFilesCount ();
388 for ( size_t n
= 0 ; n
< count
; n
++ )
392 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
394 m_checklst
-> Append ( name
+ wxT ( " (" ) + desc
+ wxT ( ')' ));
395 m_checklst
-> Check ( n
);
404 bool wxDebugReportDialog :: TransferDataFromWindow ()
406 // any unchecked files should be removed from the report
407 const size_t count
= m_checklst
-> GetCount ();
408 for ( size_t n
= 0 ; n
< count
; n
++ )
410 if ( ! m_checklst
-> IsChecked ( n
) )
412 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
416 // if the user entered any notes, add them to the report
417 const wxString notes
= m_notes
-> GetValue ();
418 if ( ! notes
. empty () )
420 // for now filename fixed, could make it configurable in the future...
421 m_dbgrpt
. AddText ( wxT ( "notes.txt" ), notes
, wxT ( "user notes" ));
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
433 const int sel
= m_checklst
-> GetSelection ();
434 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnView()" ) );
436 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
439 const wxString
& fullPath
= fn
. GetFullPath ();
441 wxFFile
file ( fullPath
);
443 wxFile
file ( fullPath
);
445 if ( file
. IsOpened () && file
. ReadAll (& str
) )
447 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
452 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
454 const int sel
= m_checklst
-> GetSelection ();
455 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnOpen()" ) );
457 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
459 // try to get the command to open this kind of files ourselves
463 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
466 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
469 #endif // wxUSE_MIMETYPE
471 // if we couldn't, ask the user
472 if ( command
. empty () )
474 wxDumpOpenExternalDlg
dlg ( this , fn
);
475 if ( dlg
. ShowModal () == wxID_OK
)
477 // get the command chosen by the user and append file name to it
479 // if we don't have place marker for file name in the command...
480 wxString cmd
= dlg
. GetCommand ();
484 if ( cmd
. find ( wxT ( '%' )) != wxString :: npos
)
486 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
489 #endif // wxUSE_MIMETYPE
491 // append the file name to the end
492 command
<< cmd
<< wxT ( " \" " ) << fn
. GetFullPath () << wxT ( '"' );
498 if ( ! command
. empty () )
499 :: wxExecute ( command
);
502 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
504 int sel
= m_checklst
-> GetSelection ();
507 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
508 event
. Enable ( fn
. FileExists ());
515 // ============================================================================
516 // wxDebugReportPreviewStd implementation
517 // ============================================================================
519 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
521 if ( ! dbgrpt
. GetFilesCount () )
524 wxDebugReportDialog
dlg ( dbgrpt
);
527 // before entering the event loop (from ShowModal()), block the event
528 // handling for all other windows as this could result in more crashes
529 wxEventLoop :: SetCriticalWindow (& dlg
);
531 wxON_BLOCK_EXIT1 ( wxEventLoop :: SetCriticalWindow
,
532 static_cast < wxWindow
*>( NULL
) );
535 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
538 #endif // wxUSE_DEBUGREPORT && wxUSE_XML