smartmsgdlg.cpp

00001 
00002 // Name:        smartmsgdlg.cpp
00003 // Purpose:     wxSmartMessageDialog
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     10/7/2006
00007 // RCS-ID:      $Id: smartmsgdlg.cpp,v 1.1.1.1 2006/12/12 09:39:39 frm Exp $
00008 // Copyright:   (c) Francesco Montorsi
00009 // Licence:     wxWindows licence
00011 
00012 // ============================================================================
00013 // declarations
00014 // ============================================================================
00015 
00016 // ----------------------------------------------------------------------------
00017 // headers
00018 // ----------------------------------------------------------------------------
00019 
00020 #include "wx/wxprec.h"
00021 
00022 #ifdef __BORLANDC__
00023     #pragma hdrstop
00024 #endif
00025 
00026 #ifndef WX_PRECOMP
00027     #include "wx/checkbox.h"
00028     #include "wx/wx.h"
00029 #endif
00030 
00031 #include "guipm/smartmsgdlg.h"
00032 #include "wx/artprov.h"
00033 #include "wx/statline.h"
00034 #include "wx/module.h"
00035 
00036 // statics
00037 wxConfigBase *wxSmartMessageDialog::m_pCfg = NULL;
00038 wxString wxSmartMessageDialog::m_strPath = wxT("/Messages");
00039 
00040 wxStringHashMap wxSmartMessageDialog::s_hashDesc;
00041 
00042 
00043 
00044 // ============================================================================
00045 // implementation
00046 // ============================================================================
00047 
00048 // ----------------------------------------------------------------------------
00049 // wxSmartMessageDialog
00050 // ----------------------------------------------------------------------------
00051 
00052 BEGIN_EVENT_TABLE(wxSmartMessageDialog, wxDialog)
00053         EVT_BUTTON(wxID_YES, wxSmartMessageDialog::OnYes)
00054         EVT_BUTTON(wxID_NO, wxSmartMessageDialog::OnNo)
00055         EVT_BUTTON(wxID_CANCEL, wxSmartMessageDialog::OnCancel)
00056 END_EVENT_TABLE()
00057 
00058 IMPLEMENT_CLASS(wxSmartMessageDialog, wxDialog)
00059 
00060 wxSmartMessageDialog::wxSmartMessageDialog(const wxString &keyname)
00061 {
00062     m_bShowAgain=true;
00063     m_pDoNotShowAgain=NULL;
00064     m_strKeyName=keyname;
00065 
00066     wxASSERT_MSG(s_hashDesc.count(keyname) == 1,
00067                  wxT("This message is not registered in wxSmartMessageDialogModule!!"));
00068     Load();
00069 }
00070 
00071 bool wxSmartMessageDialog::Create( wxWindow *parent,
00072                                     const wxString& message,
00073                                     const wxString& caption,
00074                                     long style,
00075                                     const wxPoint& pos)
00076 {
00077     if (!wxDialog::Create( parent, wxID_ANY, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE ))
00078         return false;
00079 
00080     SetMessageDialogStyle(style);
00081 
00082     bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
00083 
00084     wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
00085 
00086     wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
00087 
00088 #if wxUSE_STATBMP
00089     // 1) icon
00090     if (style & wxICON_MASK)
00091     {
00092         wxBitmap bitmap;
00093         switch ( style & wxICON_MASK )
00094         {
00095             default:
00096                 wxFAIL_MSG(_T("incorrect log style"));
00097                 // fall through
00098 
00099             case wxICON_ERROR:
00100                 bitmap = wxArtProvider::GetIcon(wxART_ERROR, wxART_MESSAGE_BOX);
00101                 break;
00102 
00103             case wxICON_INFORMATION:
00104                 bitmap = wxArtProvider::GetIcon(wxART_INFORMATION, wxART_MESSAGE_BOX);
00105                 break;
00106 
00107             case wxICON_WARNING:
00108                 bitmap = wxArtProvider::GetIcon(wxART_WARNING, wxART_MESSAGE_BOX);
00109                 break;
00110 
00111             case wxICON_QUESTION:
00112                 bitmap = wxArtProvider::GetIcon(wxART_QUESTION, wxART_MESSAGE_BOX);
00113                 break;
00114         }
00115         wxStaticBitmap *icon = new wxStaticBitmap(this, wxID_ANY, bitmap);
00116         if (is_pda)
00117             topsizer->Add( icon, 0, wxTOP|wxLEFT|wxRIGHT | wxALIGN_LEFT, 10 );
00118         else
00119             icon_text->Add( icon, 0, wxCENTER );
00120     }
00121 #endif // wxUSE_STATBMP
00122 
00123 #if wxUSE_STATTEXT
00124     // 2) text
00125     icon_text->Add( CreateTextSizer( message ), 0, wxALIGN_CENTER | wxLEFT, 10 );
00126 
00127     topsizer->Add( icon_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
00128 #endif // wxUSE_STATTEXT
00129 
00130     // My modification
00131     // ===================================================
00132 
00133     topsizer->AddSpacer(10);
00134 
00135     wxString label;
00136     if (style & wxYES_NO)
00137         label = _("Do not ask next time - assume 'Yes' as reply.");
00138     else
00139         label = _("Do not show this message next time.");
00140 
00141     m_pDoNotShowAgain = new wxCheckBox(this, wxID_ANY, label);
00142     m_pDoNotShowAgain->SetValue(!m_bShowAgain);
00143     topsizer->Add(m_pDoNotShowAgain, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 10);
00144 
00145     // End of my mod
00146     // ================
00147 
00148 
00149 #if wxUSE_STATLINE
00150     // 3) static line
00151     topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
00152 #endif // wxUSE_STATLINE
00153 
00154     // 4) buttons
00155     int center_flag = wxEXPAND;
00156     if (style & wxYES_NO) center_flag = wxALIGN_CENTRE;
00157     topsizer->Add( CreateButtonSizer( style & (wxOK|wxCANCEL|wxYES_NO|wxYES_DEFAULT|wxNO_DEFAULT) ),
00158                    0, center_flag | wxALL, 10 );
00159 
00160     SetAutoLayout( true );
00161     SetSizer( topsizer );
00162 
00163     topsizer->SetSizeHints( this );
00164     topsizer->Fit( this );
00165     wxSize size( GetSize() );
00166     if (size.x < size.y*3/2)
00167     {
00168         size.x = size.y*3/2;
00169         SetSize( size );
00170     }
00171 
00172     Centre( wxBOTH | wxCENTER_FRAME);
00173 
00174     return true;
00175 }
00176 
00177 void wxSmartMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
00178 {
00179     EndModal( wxID_YES );
00180 }
00181 
00182 void wxSmartMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
00183 {
00184     EndModal( wxID_NO );
00185 }
00186 
00187 void wxSmartMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
00188 {
00189     // Allow cancellation via ESC/Close button except if
00190     // only YES and NO are specified.
00191     const long style = GetMessageDialogStyle();
00192     if ( (style & wxYES_NO) != wxYES_NO || (style & wxCANCEL) )
00193     {
00194         EndModal( wxID_CANCEL );
00195     }
00196 }
00197 
00198 
00199 
00200 // ----------------------------------------------------------------------------
00201 // wxSmartMessageBox
00202 // ----------------------------------------------------------------------------
00203 
00204 int wxSmartMessageBox(const wxString &keyname,
00205                         const wxString& message,
00206                         const wxString& caption,
00207                         long style,
00208                         wxWindow *parent)
00209 {
00210     wxSmartMessageDialog dlg(keyname);
00211 
00212     if (dlg.ShowAgain())
00213     {
00214         dlg.Create(parent, message, caption, style);
00215         return dlg.ShowModal();
00216     }
00217     else
00218     {
00219         // when not showing a smart message box which has the wxYES_NO
00220         // style, it is always a wxYES to be implied
00221         if (style & wxYES_NO)
00222             return wxID_YES;
00223         return wxID_OK;
00224     }
00225 }
00226 
00227 
00228 
00229 // ----------------------------------------------------------------------------
00230 // wxSmartLog* family
00231 // ----------------------------------------------------------------------------
00232 
00233 #define LOG_BUFFER_SIZE   (4096)
00234 static wxChar s_szSmartBufStatic[LOG_BUFFER_SIZE];
00235 
00236 // wrapper for wxVsnprintf(s_szBuf) which always NULL-terminates it
00237 static inline void PrintfInLogBuf(const wxChar *szFormat, va_list argptr)
00238 {
00239     if ( wxVsnprintf(s_szSmartBufStatic, LOG_BUFFER_SIZE, szFormat, argptr) < 0 )
00240     {
00241         // must NUL-terminate it manually
00242         s_szSmartBufStatic[LOG_BUFFER_SIZE - 1] = _T('\0');
00243     }
00244     //else: NUL-terminated by vsnprintf()
00245 }
00246 
00247 inline int ShowMessageBox(const wxChar *keyname, const wxChar *msg)
00248 {
00249     return wxSmartMessageBox(keyname, msg,
00250                       _("Message"), wxOK|wxICON_INFORMATION);
00251 }
00252 
00253 inline int ShowWarningBox(const wxChar *keyname, const wxChar *msg)
00254 {
00255     return wxSmartMessageBox(keyname, msg,
00256                       _("Warning"), wxOK|wxICON_WARNING);
00257 }
00258 
00259 inline int ShowErrorBox(const wxChar *keyname, const wxChar *msg)
00260 {
00261     return wxSmartMessageBox(keyname, msg,
00262                       _("Error"), wxOK|wxICON_ERROR);
00263 }
00264 
00265 inline int ShowYesNoQuestionBox(const wxChar *keyname, const wxChar *msg)
00266 {
00267     return wxSmartMessageBox(keyname, msg,
00268                       _("Question"), wxYES_NO|wxICON_QUESTION);
00269 }
00270 
00271 #define IMPLEMENT_SMARTLOG_FUNCTION(level)                          \
00272   int wxSmartVLog##level(const wxChar *keyname,                     \
00273                           const wxChar *szFormat, va_list argptr)   \
00274   {                                                                 \
00275     PrintfInLogBuf(szFormat, argptr);                               \
00276     return Show##level##Box(keyname, s_szSmartBufStatic);           \
00277   }                                                                 \
00278                                                                     \
00279   int wxSmartLog##level(const wxChar *keyname,                      \
00280                          const wxChar *szFormat, ...)               \
00281   {                                                                 \
00282     va_list argptr;                                                 \
00283     va_start(argptr, szFormat);                                     \
00284     int ret = wxSmartVLog##level(keyname, szFormat, argptr);        \
00285     va_end(argptr);                                                 \
00286     return ret;                                                     \
00287   }
00288 
00289 IMPLEMENT_SMARTLOG_FUNCTION(Message)
00290 IMPLEMENT_SMARTLOG_FUNCTION(Warning)
00291 IMPLEMENT_SMARTLOG_FUNCTION(Error)
00292 IMPLEMENT_SMARTLOG_FUNCTION(YesNoQuestion)
00293 
00294 
00295 
00296 // ----------------------------------------------------------------------------
00297 // wxSmartMessageDialogModule
00298 // ----------------------------------------------------------------------------
00299 
00300 class wxSmartMessageDialogModule : public wxModule
00301 {
00302     DECLARE_DYNAMIC_CLASS(wxSmartMessageDialogModule)
00303 
00304 public:
00305     wxSmartMessageDialogModule() { }
00306 
00307     virtual void OnExit() {}
00308 
00309     virtual bool OnInit()
00310     {
00311         wxStringHashMap &h = wxSmartMessageDialog::s_hashDesc;
00312 
00313         h[wxT("updatecheck")] = _("Ask for the download of updated packages");
00314         h[wxT("depconflict")] = _("Ask for ignoring conflicting dependencies");
00315         h[wxT("ignoredep")] = _("Ask for ignoring conflicting dependencies");
00316         h[wxT("nowxbuilds")] = _("Warn about no wxWidgets builds defined");
00317         h[wxT("deletepresetopt")] = _("Warn when deleting an option which is part of a preset");
00318         h[wxT("editpresetopt")] = _("Warn when editing an option which is part of a preset");
00319         h[wxT("changewxbuild")] = _("Warn about options automatically changed when selecting a wxWidgets build");
00320         h[wxT("internetconn")] = _("Warn about Internet connection down");
00321 
00322         return true;
00323     }
00324 };
00325 
00326 IMPLEMENT_DYNAMIC_CLASS(wxSmartMessageDialogModule, wxModule)

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1