]>
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 msg
<< _ ( "A debug report has been generated in the directory \n " )
307 << _T ( " \" " ) << dbgrpt
. GetDirectory () << _T ( " \"\n " )
309 << _ ( "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 " )
311 << _ ( "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 " )
313 << _ ( " Thank you and we're sorry for the inconvenience! \n " )
314 << _T ( " \n\n " ); // just some white space to separate from other stuff
316 const wxSizerFlags
flagsFixed ( SizerFlags ( 0 ));
317 const wxSizerFlags
flagsExpand ( SizerFlags ( 1 ));
318 const wxSizerFlags
flagsExpand2 ( SizerFlags ( 2 ));
320 wxSizer
* sizerPreview
=
321 new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Debug report preview:" ));
322 sizerPreview
-> Add ( CreateTextSizer ( msg
), SizerFlags ( 0 ). Centre ());
324 // ... and the list of files in this debug report with buttons to view them
325 wxSizer
* sizerFileBtns
= new wxBoxSizer ( wxVERTICAL
);
326 sizerFileBtns
-> AddStretchSpacer ( 1 );
327 sizerFileBtns
-> Add ( new wxButton ( this , wxID_VIEW_DETAILS
, _T ( "&View..." )),
328 wxSizerFlags (). Border ( wxBOTTOM
));
329 sizerFileBtns
-> Add ( new wxButton ( this , wxID_OPEN
, _T ( "&Open..." )),
330 wxSizerFlags (). Border ( wxTOP
));
331 sizerFileBtns
-> AddStretchSpacer ( 1 );
333 m_checklst
= new wxCheckListBox ( this , wxID_ANY
);
335 wxSizer
* sizerFiles
= new wxBoxSizer ( wxHORIZONTAL
);
336 sizerFiles
-> Add ( m_checklst
, flagsExpand
);
337 sizerFiles
-> Add ( sizerFileBtns
, flagsFixed
);
339 sizerPreview
-> Add ( sizerFiles
, flagsExpand2
);
342 // lower part of the dialog: notes field
343 wxSizer
* sizerNotes
= new wxStaticBoxSizer ( wxVERTICAL
, this , _ ( "&Notes:" ));
345 msg
= _ ( "If you have any additional information pertaining to this bug \n report, please enter it here and it will be joined to it:" );
347 m_notes
= new wxTextCtrl ( this , wxID_ANY
, wxEmptyString
,
348 wxDefaultPosition
, wxDefaultSize
,
351 sizerNotes
-> Add ( CreateTextSizer ( msg
), flagsFixed
);
352 sizerNotes
-> Add ( m_notes
, flagsExpand
);
355 wxSizer
* sizerTop
= new wxBoxSizer ( wxVERTICAL
);
356 sizerTop
-> Add ( sizerPreview
, flagsExpand2
);
357 sizerTop
-> AddSpacer ( 5 );
358 sizerTop
-> Add ( sizerNotes
, flagsExpand
);
359 sizerTop
-> Add ( CreateStdDialogButtonSizer ( wxOK
| wxCANCEL
), flagsFixed
);
361 SetSizerAndFit ( sizerTop
);
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 bool wxDebugReportDialog :: TransferDataToWindow ()
372 // all files are included in the report by default
373 const size_t count
= m_dbgrpt
. GetFilesCount ();
374 for ( size_t n
= 0 ; n
< count
; n
++ )
378 if ( m_dbgrpt
. GetFile ( n
, & name
, & desc
) )
380 m_checklst
-> Append ( name
+ _T ( " (" ) + desc
+ _T ( ')' ));
381 m_checklst
-> Check ( n
);
390 bool wxDebugReportDialog :: TransferDataFromWindow ()
392 // any unchecked files should be removed from the report
393 const size_t count
= m_checklst
-> GetCount ();
394 for ( size_t n
= 0 ; n
< count
; n
++ )
396 if ( ! m_checklst
-> IsChecked ( n
) )
398 m_dbgrpt
. RemoveFile ( m_files
[ n
]);
402 // if the user entered any notes, add them to the report
403 const wxString notes
= m_notes
-> GetValue ();
404 if ( ! notes
. empty () )
406 // for now filename fixed, could make it configurable in the future...
407 m_dbgrpt
. AddText ( _T ( "notes.txt" ), notes
, _T ( "user notes" ));
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 void wxDebugReportDialog :: OnView ( wxCommandEvent
& )
419 const int sel
= m_checklst
-> GetSelection ();
420 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnView()" ) );
422 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
425 wxFFile
file ( fn
. GetFullPath ());
426 if ( file
. IsOpened () && file
. ReadAll (& str
) )
428 wxDumpPreviewDlg
dlg ( this , m_files
[ sel
], str
);
433 void wxDebugReportDialog :: OnOpen ( wxCommandEvent
& )
435 const int sel
= m_checklst
-> GetSelection ();
436 wxCHECK_RET ( sel
!= wxNOT_FOUND
, _T ( "invalid selection in OnOpen()" ) );
438 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
440 // try to get the command to open this kind of files ourselves
444 ft
= wxTheMimeTypesManager
-> GetFileTypeFromExtension ( fn
. GetExt ());
447 command
= ft
-> GetOpenCommand ( fn
. GetFullPath ());
450 #endif // wxUSE_MIMETYPE
452 // if we couldn't, ask the user
453 if ( command
. empty () )
455 wxDumpOpenExternalDlg
dlg ( this , fn
);
456 if ( dlg
. ShowModal () == wxID_OK
)
458 // get the command chosen by the user and append file name to it
460 // if we don't have place marker for file name in the command...
461 wxString cmd
= dlg
. GetCommand ();
465 if ( cmd
. find ( _T ( '%' )) != wxString :: npos
)
467 command
= wxFileType :: ExpandCommand ( cmd
, fn
. GetFullPath ());
470 #endif // wxUSE_MIMETYPE
472 // append the file name to the end
473 command
<< cmd
<< _T ( " \" " ) << fn
. GetFullPath () << _T ( '"' );
479 if ( ! command
. empty () )
480 :: wxExecute ( command
);
483 void wxDebugReportDialog :: OnViewUpdate ( wxUpdateUIEvent
& event
)
485 int sel
= m_checklst
-> GetSelection ();
488 wxFileName
fn ( m_dbgrpt
. GetDirectory (), m_files
[ sel
]);
489 event
. Enable ( fn
. FileExists ());
496 // ============================================================================
497 // wxDebugReportPreviewStd implementation
498 // ============================================================================
500 bool wxDebugReportPreviewStd :: Show ( wxDebugReport
& dbgrpt
) const
502 if ( ! dbgrpt
. GetFilesCount () )
505 wxDebugReportDialog
dlg ( dbgrpt
);
508 // before entering the event loop (from ShowModal()), block the event
509 // handling for all other windows as this could result in more crashes
510 wxEventLoop :: SetCriticalWindow (& dlg
);
513 return dlg
. ShowModal () == wxID_OK
&& dbgrpt
. GetFilesCount () != 0 ;
516 #endif // wxUSE_DEBUGREPORT && wxUSE_XML