]>
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"
40 #include "wx/filename.h"
42 #include "wx/mimetype.h"
44 #include "wx/statline.h"
47 #include "wx/evtloop.h" // for SetCriticalWindow()
50 // ----------------------------------------------------------------------------
51 // wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
52 // ----------------------------------------------------------------------------
54 class wxDumpPreviewDlg
: public wxDialog
57 wxDumpPreviewDlg ( wxWindow
* parent
,
58 const wxString
& title
,
59 const wxString
& text
);
65 DECLARE_NO_COPY_CLASS ( wxDumpPreviewDlg
)
68 wxDumpPreviewDlg :: wxDumpPreviewDlg ( wxWindow
* parent
,
69 const wxString
& title
,
71 : wxDialog ( parent
, wxID_ANY
, title
,
72 wxDefaultPosition
, wxDefaultSize
,
73 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
78 // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
79 // faster than with wxTE_RICH
80 m_text
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
81 wxPoint ( 0 , 0 ), wxDefaultSize
,
86 m_text
-> SetValue ( text
);
88 // use fixed-width font
89 m_text
-> SetFont ( wxFont ( 12 , wxFONTFAMILY_TELETYPE
,
90 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
92 wxButton
* btnClose
= new wxButton ( this , wxID_CANCEL
, _ ( "Close" ));
98 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
),
99 * sizerBtns
= new wxBoxSizer ( wxHORIZONTAL
);
101 sizerBtns
-> Add ( btnClose
, 0 , 0 , 1 );
103 sizerTop
-> Add ( m_text
, 1 , wxEXPAND
);
104 sizerTop
-> Add ( sizerBtns
, 0 , wxALIGN_RIGHT
| wxTOP
| wxBOTTOM
| wxRIGHT
, 1 );
109 // make the text window bigger to show more contents of the file
110 sizerTop
-> SetItemMinSize ( m_text
, 600 , 300 );
119 // ----------------------------------------------------------------------------
120 // wxDumpOpenExternalDlg: choose a command for opening the given file
121 // ----------------------------------------------------------------------------
123 class wxDumpOpenExternalDlg
: public wxDialog
126 wxDumpOpenExternalDlg ( wxWindow
* parent
, const wxFileName
& filename
);
128 // return the command chosed by user to open this file
129 const wxString
& GetCommand () const { return m_command
; }
136 void OnBrowse ( wxCommandEvent
& event
);
137 #endif // wxUSE_FILEDLG
139 DECLARE_EVENT_TABLE ()
140 DECLARE_NO_COPY_CLASS ( wxDumpOpenExternalDlg
)
143 BEGIN_EVENT_TABLE ( wxDumpOpenExternalDlg
, wxDialog
)
146 EVT_BUTTON ( wxID_MORE
, wxDumpOpenExternalDlg :: OnBrowse
)
152 wxDumpOpenExternalDlg :: wxDumpOpenExternalDlg ( wxWindow
* parent
,
153 const wxFileName
& filename
)
158 _ ( "Open file \" %s \" " ),
159 filename
. GetFullPath (). c_str ()
165 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
166 sizerTop
-> Add ( new wxStaticText ( this , wxID_ANY
,
169 _ ( "Enter command to open file \" %s \" :" ),
170 filename
. GetFullName (). c_str ()
172 wxSizerFlags (). Border ());
174 wxSizer
* sizerH
= new wxBoxSizer ( wxHORIZONTAL
);
176 wxTextCtrl
* command
= new wxTextCtrl
182 wxSize ( 250 , wxDefaultCoord
),
185 , wxTextValidator ( wxFILTER_NONE
, & m_command
)
189 wxSizerFlags ( 1 ). Align ( wxALIGN_CENTER_VERTICAL
));
193 wxButton
* browse
= new wxButton ( this , wxID_MORE
, wxT ( ">>" ),
194 wxDefaultPosition
, wxDefaultSize
,
197 wxSizerFlags ( 0 ). Align ( wxALIGN_CENTER_VERTICAL
). Border ( wxLEFT
));
199 #endif // wxUSE_FILEDLG
201 sizerTop
-> Add ( sizerH
, wxSizerFlags ( 0 ). Expand (). Border ());
203 sizerTop
-> Add ( new wxStaticLine ( this ), wxSizerFlags (). Expand (). Border ());
205 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
),
206 wxSizerFlags (). Align ( wxALIGN_RIGHT
). Border ());
221 void wxDumpOpenExternalDlg :: OnBrowse ( wxCommandEvent
& )
223 wxFileName
fname ( m_command
);
224 wxFileDialog
dlg ( this ,
225 wxFileSelectorPromptStr
,
226 fname
. GetPathWithSep (),
229 , _ ( "Executable files (*.exe)|*.exe|All files (*.*)|*.*||" )
232 if ( dlg
. ShowModal () == wxID_OK
)
234 m_command
= dlg
. GetPath ();
235 TransferDataToWindow ();
239 #endif // wxUSE_FILEDLG
241 // ----------------------------------------------------------------------------
242 // wxDebugReportDialog: class showing debug report to the user
243 // ----------------------------------------------------------------------------
245 class wxDebugReportDialog
: public wxDialog
248 wxDebugReportDialog ( wxDebugReport
& dbgrpt
);
250 virtual bool TransferDataToWindow ();
251 virtual bool TransferDataFromWindow ();
254 void OnView ( wxCommandEvent
& );
255 void OnViewUpdate ( wxUpdateUIEvent
& );
256 void OnOpen ( wxCommandEvent
& );
259 // small helper: add wxEXPAND and wxALL flags
260 static wxSizerFlags
SizerFlags ( int proportion
)
262 return wxSizerFlags ( proportion
). Expand (). Border ();
266 wxDebugReport
& m_dbgrpt
;
268 wxCheckListBox
* m_checklst
;
271 wxArrayString m_files
;
273 DECLARE_EVENT_TABLE ()
274 DECLARE_NO_COPY_CLASS ( wxDebugReportDialog
)
277 // ============================================================================
278 // wxDebugReportDialog implementation
279 // ============================================================================
281 BEGIN_EVENT_TABLE ( wxDebugReportDialog
, wxDialog
)
282 EVT_BUTTON ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnView
)
283 EVT_UPDATE_UI ( wxID_VIEW_DETAILS
, wxDebugReportDialog :: OnViewUpdate
)
284 EVT_BUTTON ( wxID_OPEN
, wxDebugReportDialog :: OnOpen
)
285 EVT_UPDATE_UI ( wxID_OPEN
, wxDebugReportDialog :: OnViewUpdate
)
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 wxDebugReportDialog :: wxDebugReportDialog ( wxDebugReport
& dbgrpt
)
294 : wxDialog ( NULL
, wxID_ANY
,
295 wxString :: Format ( _ ( "Debug report \" %s \" " ),
296 dbgrpt
. GetReportName (). c_str ()),
299 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
),
302 // upper part of the dialog: explanatory message
304 msg
<< _ ( "A debug report has been generated in the directory \n " )
306 << _T ( " \" " ) << dbgrpt
. GetDirectory () << _T ( " \"\n " )
308 << _ ( "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 " )
310 << _ ( "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 " )
312 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
313 << _T ( " \n\n " ); // just some white space to separate from other stuff
315 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
316 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
317 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
319 wxSizer
* sizerPreview
=
320 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
321 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
323 // ... and the list of files in this debug report with buttons to view them
324 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
325 sizerFileBtns
-> AddStretchSpacer ( 1 );
326 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _T ( "&View..." )),
327 wxSizerFlags (). Border ( wxBOTTOM
));
328 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _T ( "&Open..." )),
329 wxSizerFlags (). Border ( wxTOP
));
330 sizerFileBtns
-> AddStretchSpacer ( 1 );
332 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
334 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
335 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
336 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
338 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
341 // lower part of the dialog: notes field
342 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
344 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
346 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
347 wxDefaultPosition
, wxDefaultSize
,
350 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
351 sizerNotes
-> Add ( m_notes
, flagsExpand
);
354 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
355 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
356 sizerTop
-> AddSpacer ( 5 );
357 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
358 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
360 SetSizerAndFit ( sizerTop
);
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 bool wxDebugReportDialog :: TransferDataToWindow ()
371 // all files are included in the report by default
372 const size_t count
= m_dbgrpt
. GetFilesCount ();
373 for ( size_t n
= 0 ; n
< count
; n
++ )
377 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
379 m_checklst
-> Append ( name
+ _T ( " (" ) + desc
+ _T ( ')' ));
380 m_checklst
-> Check ( n
);
389 bool wxDebugReportDialog :: TransferDataFromWindow ()
391 // any unchecked files should be removed from the report
392 const size_t count
= m_checklst
-> GetCount ();
393 for ( size_t n
= 0 ; n
< count
; n
++ )
395 if ( ! m_checklst
-> IsChecked ( n
) )
397 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
401 // if the user entered any notes, add them to the report
402 const wxString notes
= m_notes
-> GetValue ();
403 if ( ! notes
. empty () )
405 // for now filename fixed, could make it configurable in the future...
406 m_dbgrpt
. AddText ( _T ( "notes.txt" ), notes
, _T ( "user notes" ));
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
418 const int sel
= m_checklst
-> GetSelection ();
419 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnView()" ) );
421 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
424 wxFFile
file ( fn
. GetFullPath ());
425 if ( file
. IsOpened () && file
. ReadAll (& str
) )
427 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
432 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
434 const int sel
= m_checklst
-> GetSelection ();
435 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnOpen()" ) );
437 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
439 // try to get the command to open this kind of files ourselves
443 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
446 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
449 #endif // wxUSE_MIMETYPE
451 // if we couldn't, ask the user
452 if ( command
. empty () )
454 wxDumpOpenExternalDlg
dlg ( this , fn
);
455 if ( dlg
. ShowModal () == wxID_OK
)
457 // get the command chosen by the user and append file name to it
459 // if we don't have place marker for file name in the command...
460 wxString cmd
= dlg
. GetCommand ();
464 if ( cmd
. find ( _T ( '%' )) != wxString :: npos
)
466 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
469 #endif // wxUSE_MIMETYPE
471 // append the file name to the end
472 command
<< cmd
<< _T ( " \" " ) << fn
. GetFullPath () << _T ( '"' );
478 if ( ! command
. empty () )
479 :: wxExecute ( command
);
482 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
484 int sel
= m_checklst
-> GetSelection ();
487 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
488 event
. Enable ( fn
. FileExists ());
495 // ============================================================================
496 // wxDebugReportPreviewStd implementation
497 // ============================================================================
499 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
501 if ( ! dbgrpt
. GetFilesCount () )
504 wxDebugReportDialog
dlg ( dbgrpt
);
507 // before entering the event loop (from ShowModal()), block the event
508 // handling for all other windows as this could result in more crashes
509 wxEventLoop :: SetCriticalWindow (& dlg
);
512 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
515 #endif // wxUSE_DEBUGREPORT && wxUSE_XML