]>
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
7 // Copyright: (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_DEBUGREPORT && wxUSE_XML
27 #include "wx/debugrpt.h"
31 #include "wx/checklst.h"
32 #include "wx/textctrl.h"
34 #include "wx/stattext.h"
35 #include "wx/filedlg.h"
36 #include "wx/valtext.h"
37 #include "wx/button.h"
40 #include "wx/filename.h"
46 #include "wx/mimetype.h"
48 #include "wx/statline.h"
51 #include "wx/evtloop.h" // for SetCriticalWindow()
52 #include "wx/scopeguard.h"
55 // ----------------------------------------------------------------------------
56 // wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
57 // ----------------------------------------------------------------------------
59 class wxDumpPreviewDlg
: public wxDialog
62 wxDumpPreviewDlg ( wxWindow
* parent
,
63 const wxString
& title
,
64 const wxString
& text
);
70 wxDECLARE_NO_COPY_CLASS ( wxDumpPreviewDlg
);
73 wxDumpPreviewDlg :: wxDumpPreviewDlg ( wxWindow
* parent
,
74 const wxString
& title
,
76 : wxDialog ( parent
, wxID_ANY
, title
,
77 wxDefaultPosition
, wxDefaultSize
,
78 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
83 // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
84 // faster than with wxTE_RICH
85 m_text
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
86 wxPoint ( 0 , 0 ), wxDefaultSize
,
91 m_text
-> SetValue ( text
);
93 // use fixed-width font
94 m_text
-> SetFont ( wxFont ( 12 , wxFONTFAMILY_TELETYPE
,
95 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
97 wxButton
* btnClose
= new wxButton ( this , wxID_CANCEL
, _ ( "Close" ));
103 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
),
104 * sizerBtns
= new wxBoxSizer ( wxHORIZONTAL
);
106 sizerBtns
-> Add ( btnClose
, 0 , 0 , 1 );
108 sizerTop
-> Add ( m_text
, 1 , wxEXPAND
);
109 sizerTop
-> Add ( sizerBtns
, 0 , wxALIGN_RIGHT
| wxTOP
| wxBOTTOM
| wxRIGHT
, 1 );
114 // make the text window bigger to show more contents of the file
115 sizerTop
-> SetItemMinSize ( m_text
, 600 , 300 );
124 // ----------------------------------------------------------------------------
125 // wxDumpOpenExternalDlg: choose a command for opening the given file
126 // ----------------------------------------------------------------------------
128 class wxDumpOpenExternalDlg
: public wxDialog
131 wxDumpOpenExternalDlg ( wxWindow
* parent
, const wxFileName
& filename
);
133 // return the command chosed by user to open this file
134 const wxString
& GetCommand () const { return m_command
; }
141 void OnBrowse ( wxCommandEvent
& event
);
142 #endif // wxUSE_FILEDLG
144 DECLARE_EVENT_TABLE ()
145 wxDECLARE_NO_COPY_CLASS ( wxDumpOpenExternalDlg
);
148 BEGIN_EVENT_TABLE ( wxDumpOpenExternalDlg
, wxDialog
)
151 EVT_BUTTON ( wxID_MORE
, wxDumpOpenExternalDlg :: OnBrowse
)
157 wxDumpOpenExternalDlg :: wxDumpOpenExternalDlg ( wxWindow
* parent
,
158 const wxFileName
& filename
)
163 _ ( "Open file \" %s \" " ),
164 filename
. GetFullPath (). c_str ()
170 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
171 sizerTop
-> Add ( new wxStaticText ( this , wxID_ANY
,
174 _ ( "Enter command to open file \" %s \" :" ),
175 filename
. GetFullName (). c_str ()
177 wxSizerFlags (). Border ());
179 wxSizer
* sizerH
= new wxBoxSizer ( wxHORIZONTAL
);
181 wxTextCtrl
* command
= new wxTextCtrl
187 wxSize ( 250 , wxDefaultCoord
),
190 , wxTextValidator ( wxFILTER_NONE
, & m_command
)
194 wxSizerFlags ( 1 ). Align ( wxALIGN_CENTER_VERTICAL
));
198 wxButton
* browse
= new wxButton ( this , wxID_MORE
, wxT ( ">>" ),
199 wxDefaultPosition
, wxDefaultSize
,
202 wxSizerFlags ( 0 ). Align ( wxALIGN_CENTER_VERTICAL
). Border ( wxLEFT
));
204 #endif // wxUSE_FILEDLG
206 sizerTop
-> Add ( sizerH
, wxSizerFlags ( 0 ). Expand (). Border ());
208 sizerTop
-> Add ( new wxStaticLine ( this ), wxSizerFlags (). Expand (). Border ());
210 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
),
211 wxSizerFlags (). Align ( wxALIGN_RIGHT
). Border ());
226 void wxDumpOpenExternalDlg :: OnBrowse ( wxCommandEvent
& )
228 wxFileName
fname ( m_command
);
229 wxFileDialog
dlg ( this ,
230 wxFileSelectorPromptStr
,
231 fname
. GetPathWithSep (),
234 , _ ( "Executable files (*.exe)|*.exe|" ) + wxALL_FILES
237 if ( dlg
. ShowModal () == wxID_OK
)
239 m_command
= dlg
. GetPath ();
240 TransferDataToWindow ();
244 #endif // wxUSE_FILEDLG
246 // ----------------------------------------------------------------------------
247 // wxDebugReportDialog: class showing debug report to the user
248 // ----------------------------------------------------------------------------
250 class wxDebugReportDialog
: public wxDialog
253 wxDebugReportDialog ( wxDebugReport
& dbgrpt
);
255 virtual bool TransferDataToWindow ();
256 virtual bool TransferDataFromWindow ();
259 void OnView ( wxCommandEvent
& );
260 void OnViewUpdate ( wxUpdateUIEvent
& );
261 void OnOpen ( wxCommandEvent
& );
264 // small helper: add wxEXPAND and wxALL flags
265 static wxSizerFlags
SizerFlags ( int proportion
)
267 return wxSizerFlags ( proportion
). Expand (). Border ();
271 wxDebugReport
& m_dbgrpt
;
273 wxCheckListBox
* m_checklst
;
276 wxArrayString m_files
;
278 DECLARE_EVENT_TABLE ()
279 wxDECLARE_NO_COPY_CLASS ( wxDebugReportDialog
);
282 // ============================================================================
283 // wxDebugReportDialog implementation
284 // ============================================================================
286 BEGIN_EVENT_TABLE ( wxDebugReportDialog
, wxDialog
)
287 EVT_BUTTON ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnView
)
288 EVT_UPDATE_UI ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnViewUpdate
)
289 EVT_BUTTON ( wxID_OPEN
, wxDebugReportDialog :: OnOpen
)
290 EVT_UPDATE_UI ( wxID_OPEN
, wxDebugReportDialog :: OnViewUpdate
)
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 wxDebugReportDialog :: wxDebugReportDialog ( wxDebugReport
& dbgrpt
)
299 : wxDialog ( NULL
, wxID_ANY
,
300 wxString :: Format ( _ ( "Debug report \" %s \" " ),
301 dbgrpt
. GetReportName (). c_str ()),
304 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
),
307 // upper part of the dialog: explanatory message
309 wxString debugDir
= dbgrpt
. GetDirectory ();
311 // The temporary directory can be the short form on Windows;
312 // normalize it for the benefit of users.
314 wxFileName
debugDirFilename ( debugDir
, wxEmptyString
);
315 debugDirFilename
. Normalize ( wxPATH_NORM_LONG
);
316 debugDir
= debugDirFilename
. GetPath ();
318 msg
<< _ ( "A debug report has been generated in the directory \n " )
320 << wxT ( " \" " ) << debugDir
<< wxT ( " \"\n " )
322 << _ ( "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 " )
324 << _ ( "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 " )
326 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
327 << wxT ( " \n\n " ); // just some white space to separate from other stuff
329 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
330 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
331 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
333 wxSizer
* sizerPreview
=
334 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
335 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
337 // ... and the list of files in this debug report with buttons to view them
338 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
339 sizerFileBtns
-> AddStretchSpacer ( 1 );
340 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _ ( "&View..." )),
341 wxSizerFlags (). Border ( wxBOTTOM
));
342 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _ ( "&Open..." )),
343 wxSizerFlags (). Border ( wxTOP
));
344 sizerFileBtns
-> AddStretchSpacer ( 1 );
346 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
348 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
349 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
350 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
352 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
355 // lower part of the dialog: notes field
356 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
358 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
360 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
361 wxDefaultPosition
, wxDefaultSize
,
364 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
365 sizerNotes
-> Add ( m_notes
, flagsExpand
);
368 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
369 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
370 sizerTop
-> AddSpacer ( 5 );
371 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
372 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
374 SetSizerAndFit ( sizerTop
);
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 bool wxDebugReportDialog :: TransferDataToWindow ()
385 // all files are included in the report by default
386 const size_t count
= m_dbgrpt
. GetFilesCount ();
387 for ( size_t n
= 0 ; n
< count
; n
++ )
391 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
393 m_checklst
-> Append ( name
+ wxT ( " (" ) + desc
+ wxT ( ')' ));
394 m_checklst
-> Check ( n
);
403 bool wxDebugReportDialog :: TransferDataFromWindow ()
405 // any unchecked files should be removed from the report
406 const size_t count
= m_checklst
-> GetCount ();
407 for ( size_t n
= 0 ; n
< count
; n
++ )
409 if ( ! m_checklst
-> IsChecked ( n
) )
411 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
415 // if the user entered any notes, add them to the report
416 const wxString notes
= m_notes
-> GetValue ();
417 if ( ! notes
. empty () )
419 // for now filename fixed, could make it configurable in the future...
420 m_dbgrpt
. AddText ( wxT ( "notes.txt" ), notes
, wxT ( "user notes" ));
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
432 const int sel
= m_checklst
-> GetSelection ();
433 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnView()" ) );
435 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
438 const wxString
& fullPath
= fn
. GetFullPath ();
440 wxFFile
file ( fullPath
);
442 wxFile
file ( fullPath
);
444 if ( file
. IsOpened () && file
. ReadAll (& str
) )
446 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
451 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
453 const int sel
= m_checklst
-> GetSelection ();
454 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnOpen()" ) );
456 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
458 // try to get the command to open this kind of files ourselves
462 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
465 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
468 #endif // wxUSE_MIMETYPE
470 // if we couldn't, ask the user
471 if ( command
. empty () )
473 wxDumpOpenExternalDlg
dlg ( this , fn
);
474 if ( dlg
. ShowModal () == wxID_OK
)
476 // get the command chosen by the user and append file name to it
478 // if we don't have place marker for file name in the command...
479 wxString cmd
= dlg
. GetCommand ();
483 if ( cmd
. find ( wxT ( '%' )) != wxString :: npos
)
485 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
488 #endif // wxUSE_MIMETYPE
490 // append the file name to the end
491 command
<< cmd
<< wxT ( " \" " ) << fn
. GetFullPath () << wxT ( '"' );
497 if ( ! command
. empty () )
498 :: wxExecute ( command
);
501 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
503 int sel
= m_checklst
-> GetSelection ();
506 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
507 event
. Enable ( fn
. FileExists ());
514 // ============================================================================
515 // wxDebugReportPreviewStd implementation
516 // ============================================================================
518 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
520 if ( ! dbgrpt
. GetFilesCount () )
523 wxDebugReportDialog
dlg ( dbgrpt
);
526 // before entering the event loop (from ShowModal()), block the event
527 // handling for all other windows as this could result in more crashes
528 wxEventLoop :: SetCriticalWindow (& dlg
);
530 wxON_BLOCK_EXIT1 ( wxEventLoop :: SetCriticalWindow
,
531 static_cast < wxWindow
*>( NULL
) );
534 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
537 #endif // wxUSE_DEBUGREPORT && wxUSE_XML