]>
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()
49 #include "wx/scopeguard.h"
52 // ----------------------------------------------------------------------------
53 // wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
54 // ----------------------------------------------------------------------------
56 class wxDumpPreviewDlg
: public wxDialog
59 wxDumpPreviewDlg ( wxWindow
* parent
,
60 const wxString
& title
,
61 const wxString
& text
);
67 wxDECLARE_NO_COPY_CLASS ( wxDumpPreviewDlg
);
70 wxDumpPreviewDlg :: wxDumpPreviewDlg ( wxWindow
* parent
,
71 const wxString
& title
,
73 : wxDialog ( parent
, wxID_ANY
, title
,
74 wxDefaultPosition
, wxDefaultSize
,
75 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
80 // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
81 // faster than with wxTE_RICH
82 m_text
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
83 wxPoint ( 0 , 0 ), wxDefaultSize
,
88 m_text
-> SetValue ( text
);
90 // use fixed-width font
91 m_text
-> SetFont ( wxFont ( 12 , wxFONTFAMILY_TELETYPE
,
92 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
94 wxButton
* btnClose
= new wxButton ( this , wxID_CANCEL
, _ ( "Close" ));
100 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
),
101 * sizerBtns
= new wxBoxSizer ( wxHORIZONTAL
);
103 sizerBtns
-> Add ( btnClose
, 0 , 0 , 1 );
105 sizerTop
-> Add ( m_text
, 1 , wxEXPAND
);
106 sizerTop
-> Add ( sizerBtns
, 0 , wxALIGN_RIGHT
| wxTOP
| wxBOTTOM
| wxRIGHT
, 1 );
111 // make the text window bigger to show more contents of the file
112 sizerTop
-> SetItemMinSize ( m_text
, 600 , 300 );
121 // ----------------------------------------------------------------------------
122 // wxDumpOpenExternalDlg: choose a command for opening the given file
123 // ----------------------------------------------------------------------------
125 class wxDumpOpenExternalDlg
: public wxDialog
128 wxDumpOpenExternalDlg ( wxWindow
* parent
, const wxFileName
& filename
);
130 // return the command chosed by user to open this file
131 const wxString
& GetCommand () const { return m_command
; }
138 void OnBrowse ( wxCommandEvent
& event
);
139 #endif // wxUSE_FILEDLG
141 DECLARE_EVENT_TABLE ()
142 wxDECLARE_NO_COPY_CLASS ( wxDumpOpenExternalDlg
);
145 BEGIN_EVENT_TABLE ( wxDumpOpenExternalDlg
, wxDialog
)
148 EVT_BUTTON ( wxID_MORE
, wxDumpOpenExternalDlg :: OnBrowse
)
154 wxDumpOpenExternalDlg :: wxDumpOpenExternalDlg ( wxWindow
* parent
,
155 const wxFileName
& filename
)
160 _ ( "Open file \" %s \" " ),
161 filename
. GetFullPath (). c_str ()
167 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
168 sizerTop
-> Add ( new wxStaticText ( this , wxID_ANY
,
171 _ ( "Enter command to open file \" %s \" :" ),
172 filename
. GetFullName (). c_str ()
174 wxSizerFlags (). Border ());
176 wxSizer
* sizerH
= new wxBoxSizer ( wxHORIZONTAL
);
178 wxTextCtrl
* command
= new wxTextCtrl
184 wxSize ( 250 , wxDefaultCoord
),
187 , wxTextValidator ( wxFILTER_NONE
, & m_command
)
191 wxSizerFlags ( 1 ). Align ( wxALIGN_CENTER_VERTICAL
));
195 wxButton
* browse
= new wxButton ( this , wxID_MORE
, wxT ( ">>" ),
196 wxDefaultPosition
, wxDefaultSize
,
199 wxSizerFlags ( 0 ). Align ( wxALIGN_CENTER_VERTICAL
). Border ( wxLEFT
));
201 #endif // wxUSE_FILEDLG
203 sizerTop
-> Add ( sizerH
, wxSizerFlags ( 0 ). Expand (). Border ());
205 sizerTop
-> Add ( new wxStaticLine ( this ), wxSizerFlags (). Expand (). Border ());
207 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
),
208 wxSizerFlags (). Align ( wxALIGN_RIGHT
). Border ());
223 void wxDumpOpenExternalDlg :: OnBrowse ( wxCommandEvent
& )
225 wxFileName
fname ( m_command
);
226 wxFileDialog
dlg ( this ,
227 wxFileSelectorPromptStr
,
228 fname
. GetPathWithSep (),
231 , _ ( "Executable files (*.exe)|*.exe|All files (*.*)|*.*||" )
234 if ( dlg
. ShowModal () == wxID_OK
)
236 m_command
= dlg
. GetPath ();
237 TransferDataToWindow ();
241 #endif // wxUSE_FILEDLG
243 // ----------------------------------------------------------------------------
244 // wxDebugReportDialog: class showing debug report to the user
245 // ----------------------------------------------------------------------------
247 class wxDebugReportDialog
: public wxDialog
250 wxDebugReportDialog ( wxDebugReport
& dbgrpt
);
252 virtual bool TransferDataToWindow ();
253 virtual bool TransferDataFromWindow ();
256 void OnView ( wxCommandEvent
& );
257 void OnViewUpdate ( wxUpdateUIEvent
& );
258 void OnOpen ( wxCommandEvent
& );
261 // small helper: add wxEXPAND and wxALL flags
262 static wxSizerFlags
SizerFlags ( int proportion
)
264 return wxSizerFlags ( proportion
). Expand (). Border ();
268 wxDebugReport
& m_dbgrpt
;
270 wxCheckListBox
* m_checklst
;
273 wxArrayString m_files
;
275 DECLARE_EVENT_TABLE ()
276 wxDECLARE_NO_COPY_CLASS ( wxDebugReportDialog
);
279 // ============================================================================
280 // wxDebugReportDialog implementation
281 // ============================================================================
283 BEGIN_EVENT_TABLE ( wxDebugReportDialog
, wxDialog
)
284 EVT_BUTTON ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnView
)
285 EVT_UPDATE_UI ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnViewUpdate
)
286 EVT_BUTTON ( wxID_OPEN
, wxDebugReportDialog :: OnOpen
)
287 EVT_UPDATE_UI ( wxID_OPEN
, wxDebugReportDialog :: OnViewUpdate
)
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 wxDebugReportDialog :: wxDebugReportDialog ( wxDebugReport
& dbgrpt
)
296 : wxDialog ( NULL
, wxID_ANY
,
297 wxString :: Format ( _ ( "Debug report \" %s \" " ),
298 dbgrpt
. GetReportName (). c_str ()),
301 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
),
304 // upper part of the dialog: explanatory message
306 wxString debugDir
= dbgrpt
. GetDirectory ();
308 // The temporary directory can be the short form on Windows;
309 // normalize it for the benefit of users.
311 wxFileName
debugDirFilename ( debugDir
, wxEmptyString
);
312 debugDirFilename
. Normalize ( wxPATH_NORM_LONG
);
313 debugDir
= debugDirFilename
. GetPath ();
315 msg
<< _ ( "A debug report has been generated in the directory \n " )
317 << wxT ( " \" " ) << debugDir
<< wxT ( " \"\n " )
319 << _ ( "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 " )
321 << _ ( "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 " )
323 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
324 << wxT ( " \n\n " ); // just some white space to separate from other stuff
326 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
327 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
328 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
330 wxSizer
* sizerPreview
=
331 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
332 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
334 // ... and the list of files in this debug report with buttons to view them
335 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
336 sizerFileBtns
-> AddStretchSpacer ( 1 );
337 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _ ( "&View..." )),
338 wxSizerFlags (). Border ( wxBOTTOM
));
339 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _ ( "&Open..." )),
340 wxSizerFlags (). Border ( wxTOP
));
341 sizerFileBtns
-> AddStretchSpacer ( 1 );
343 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
345 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
346 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
347 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
349 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
352 // lower part of the dialog: notes field
353 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
355 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
357 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
358 wxDefaultPosition
, wxDefaultSize
,
361 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
362 sizerNotes
-> Add ( m_notes
, flagsExpand
);
365 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
366 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
367 sizerTop
-> AddSpacer ( 5 );
368 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
369 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
371 SetSizerAndFit ( sizerTop
);
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 bool wxDebugReportDialog :: TransferDataToWindow ()
382 // all files are included in the report by default
383 const size_t count
= m_dbgrpt
. GetFilesCount ();
384 for ( size_t n
= 0 ; n
< count
; n
++ )
388 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
390 m_checklst
-> Append ( name
+ wxT ( " (" ) + desc
+ wxT ( ')' ));
391 m_checklst
-> Check ( n
);
400 bool wxDebugReportDialog :: TransferDataFromWindow ()
402 // any unchecked files should be removed from the report
403 const size_t count
= m_checklst
-> GetCount ();
404 for ( size_t n
= 0 ; n
< count
; n
++ )
406 if ( ! m_checklst
-> IsChecked ( n
) )
408 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
412 // if the user entered any notes, add them to the report
413 const wxString notes
= m_notes
-> GetValue ();
414 if ( ! notes
. empty () )
416 // for now filename fixed, could make it configurable in the future...
417 m_dbgrpt
. AddText ( wxT ( "notes.txt" ), notes
, wxT ( "user notes" ));
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
429 const int sel
= m_checklst
-> GetSelection ();
430 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnView()" ) );
432 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
435 wxFFile
file ( fn
. GetFullPath ());
436 if ( file
. IsOpened () && file
. ReadAll (& str
) )
438 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
443 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
445 const int sel
= m_checklst
-> GetSelection ();
446 wxCHECK_RET ( sel
!= wxNOT_FOUND
, wxT ( "invalid selection in OnOpen()" ) );
448 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
450 // try to get the command to open this kind of files ourselves
454 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
457 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
460 #endif // wxUSE_MIMETYPE
462 // if we couldn't, ask the user
463 if ( command
. empty () )
465 wxDumpOpenExternalDlg
dlg ( this , fn
);
466 if ( dlg
. ShowModal () == wxID_OK
)
468 // get the command chosen by the user and append file name to it
470 // if we don't have place marker for file name in the command...
471 wxString cmd
= dlg
. GetCommand ();
475 if ( cmd
. find ( wxT ( '%' )) != wxString :: npos
)
477 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
480 #endif // wxUSE_MIMETYPE
482 // append the file name to the end
483 command
<< cmd
<< wxT ( " \" " ) << fn
. GetFullPath () << wxT ( '"' );
489 if ( ! command
. empty () )
490 :: wxExecute ( command
);
493 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
495 int sel
= m_checklst
-> GetSelection ();
498 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
499 event
. Enable ( fn
. FileExists ());
506 // ============================================================================
507 // wxDebugReportPreviewStd implementation
508 // ============================================================================
510 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
512 if ( ! dbgrpt
. GetFilesCount () )
515 wxDebugReportDialog
dlg ( dbgrpt
);
518 // before entering the event loop (from ShowModal()), block the event
519 // handling for all other windows as this could result in more crashes
520 wxEventLoop :: SetCriticalWindow (& dlg
);
522 wxON_BLOCK_EXIT1 ( wxEventLoop :: SetCriticalWindow
,
523 static_cast < wxWindow
*>( NULL
) );
526 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
529 #endif // wxUSE_DEBUGREPORT && wxUSE_XML