]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/msgdlgg.cpp
new file added
[wxWidgets.git] / src / generic / msgdlgg.cpp
index 0b8eea5424732d3f738c5b19cd235d719c3052fc..c3dd7a8067f28e0ec8b74adbc634c28870bc3af1 100644 (file)
@@ -1,18 +1,14 @@
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
-// Name:        msgdlgg.cpp
+// Name:        src/generic/msgdlgg.cpp
 // Purpose:     wxGenericMessageDialog
 // Author:      Julian Smart, Robert Roebling
 // Modified by:
 // Created:     04/01/98
 // RCS-ID:      $Id$
 // Purpose:     wxGenericMessageDialog
 // Author:      Julian Smart, Robert Roebling
 // Modified by:
 // Created:     04/01/98
 // RCS-ID:      $Id$
-// Copyright:   (c) Julian Smart, Markus Holzem, Robert Roebling
-// Licence:           wxWindows license
+// Copyright:   (c) Julian Smart and Robert Roebling
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation "msgdlgg.h"
-#endif
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 #pragma hdrstop
 #endif
 
 #pragma hdrstop
 #endif
 
+#if wxUSE_MSGDLG
+
 #ifndef WX_PRECOMP
 #ifndef WX_PRECOMP
-  #include "wx/utils.h"
-  #include "wx/dialog.h"
-  #include "wx/button.h"
-  #include "wx/stattext.h"
-  #include "wx/statbmp.h"
-  #include  "wx/layout.h"
-  #include "wx/intl.h"
-  #include "wx/dcclient.h"
-  #include "wx/settings.h"
+    #include "wx/utils.h"
+    #include "wx/dialog.h"
+    #include "wx/button.h"
+    #include "wx/stattext.h"
+    #include "wx/statbmp.h"
+    #include "wx/layout.h"
+    #include "wx/intl.h"
+    #include "wx/icon.h"
+    #include "wx/sizer.h"
+    #include "wx/app.h"
+    #include "wx/settings.h"
 #endif
 
 #include <stdio.h>
 #include <string.h>
 
 #endif
 
 #include <stdio.h>
 #include <string.h>
 
-#include "wx/generic/msgdlgg.h"
+#define __WX_COMPILING_MSGDLGG_CPP__ 1
+#include "wx/msgdlg.h"
+#include "wx/artprov.h"
+#include "wx/textwrapper.h"
 
 #if wxUSE_STATLINE
 
 #if wxUSE_STATLINE
-  #include "wx/statline.h"
+    #include "wx/statline.h"
 #endif
 
 // ----------------------------------------------------------------------------
 #endif
 
 // ----------------------------------------------------------------------------
-// icons
+// wxTitleTextWrapper: simple class to create wrapped text in "title font"
 // ----------------------------------------------------------------------------
 
 // ----------------------------------------------------------------------------
 
-// MSW icons are in the ressources, for all other platforms - in XPM files
-#ifndef __WXMSW__
-    #include "wx/generic/info.xpm"
-    #include "wx/generic/question.xpm"
-    #include "wx/generic/warning.xpm"
-    #include "wx/generic/error.xpm"
-#endif // __WXMSW__
+class wxTitleTextWrapper : public wxTextSizerWrapper
+{
+public:
+    wxTitleTextWrapper(wxWindow *win)
+        : wxTextSizerWrapper(win)
+    {
+    }
+
+protected:
+    virtual wxWindow *OnCreateLine(const wxString& s)
+    {
+        wxWindow * const win = wxTextSizerWrapper::OnCreateLine(s);
+
+        win->SetFont(win->GetFont().Larger().MakeBold());
 
 
+        return win;
+    }
+};
+
+// ----------------------------------------------------------------------------
+// icons
+// ----------------------------------------------------------------------------
 
 
-#if !USE_SHARED_LIBRARY
 BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
         EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
         EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
 BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
         EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
         EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
+        EVT_BUTTON(wxID_HELP, wxGenericMessageDialog::OnHelp)
         EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
 END_EVENT_TABLE()
 
 IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
         EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
 END_EVENT_TABLE()
 
 IMPLEMENT_CLASS(wxGenericMessageDialog, wxDialog)
-#endif
 
 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
                                                 const wxString& message,
                                                 const wxString& caption,
                                                 long style,
                                                 const wxPoint& pos)
 
 wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
                                                 const wxString& message,
                                                 const wxString& caption,
                                                 long style,
                                                 const wxPoint& pos)
-                      : wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE )
+                      : wxMessageDialogBase(GetParentForModalDialog(parent, style),
+                                            message,
+                                            caption,
+                                            style),
+                        m_pos(pos)
 {
 {
-    static const int LAYOUT_X_MARGIN = 5;
-    static const int LAYOUT_Y_MARGIN = 5;
+    m_created = false;
+}
 
 
-    m_dialogStyle = style;
+wxSizer *wxGenericMessageDialog::CreateMsgDlgButtonSizer()
+{
+#ifndef __SMARTPHONE__
+    if ( HasCustomLabels() )
+    {
+        wxStdDialogButtonSizer * const sizerStd = new wxStdDialogButtonSizer;
 
 
-    wxBeginBusyCursor();
+        wxButton *btnDef = NULL;
 
 
-    wxLayoutConstraints *c;
-    SetAutoLayout(TRUE);
+        if ( m_dialogStyle & wxOK )
+        {
+            btnDef = new wxButton(this, wxID_OK, GetCustomOKLabel());
+            sizerStd->AddButton(btnDef);
+        }
 
 
-    // create an icon
-    enum
-    {
-        Icon_Information,
-        Icon_Question,
-        Icon_Warning,
-        Icon_Error
-    } which;
-
-#ifdef __WXMSW__
-    static char *icons[] =
-    {
-        "wxICON_INFO",
-        "wxICON_QUESTION",
-        "wxICON_WARNING",
-        "wxICON_ERROR",
-    };
-#else // XPM icons
-    static char **icons[] =
-    {
-        info,
-        question,
-        warning,
-        error,
-    };
-#endif // !XPM/XPM
-
-    if ( style & wxICON_EXCLAMATION )
-        which = Icon_Warning;
-    else if ( style & wxICON_HAND )
-        which = Icon_Error;
-    else if ( style & wxICON_QUESTION )
-        which = Icon_Question;
-    else
-        which = Icon_Information;
-
-    wxStaticBitmap *icon = new wxStaticBitmap(this, -1, wxIcon(icons[which]));
-    const int iconSize = icon->GetBitmap().GetWidth();
-
-    // split the message in lines
-    // --------------------------
-    wxClientDC dc(this);
-    dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
-
-    wxArrayString lines;
-    wxString curLine;
-    long height, width, heightTextMax = 0, widthTextMax = 0;
-    for ( const char *pc = message; ; pc++ ) {
-        if ( *pc == '\n' || *pc == '\0' ) {
-            dc.GetTextExtent(curLine, &width, &height);
-            if ( width > widthTextMax )
-                widthTextMax = width;
-            if ( height > heightTextMax )
-                heightTextMax = height;
-
-            lines.Add(curLine);
-
-            if ( *pc == '\n' ) {
-               curLine.Empty();
-            }
-            else {
-               // the end of string
-               break;
-            }
+        if ( m_dialogStyle & wxCANCEL )
+        {
+            wxButton * const
+                cancel = new wxButton(this, wxID_CANCEL, GetCustomCancelLabel());
+            sizerStd->AddButton(cancel);
+
+            if ( m_dialogStyle & wxCANCEL_DEFAULT )
+                btnDef = cancel;
         }
         }
-        else {
-            curLine += *pc;
+
+        if ( m_dialogStyle & wxYES_NO )
+        {
+            wxButton * const
+                yes = new wxButton(this, wxID_YES, GetCustomYesLabel());
+            sizerStd->AddButton(yes);
+
+            wxButton * const
+                no = new wxButton(this, wxID_NO, GetCustomNoLabel());
+            sizerStd->AddButton(no);
+            if ( m_dialogStyle & wxNO_DEFAULT )
+                btnDef = no;
+            else if ( !btnDef )
+                btnDef = yes;
         }
         }
-    }
 
 
-    // calculate the total dialog size
-    enum
-    {
-        Btn_Ok,
-        Btn_Yes,
-        Btn_No,
-        Btn_Cancel,
-        Btn_Max
-    };
-    wxButton *buttons[Btn_Max] = { NULL, NULL, NULL, NULL };
-    int nDefaultBtn = -1;
-
-    // some checks are in order...
-    wxASSERT_MSG( !(style & wxOK) || !(style & wxYES_NO),
-                  "don't create dialog with both Yes/No and Ok buttons!" );
-
-    wxASSERT_MSG( (style & wxOK ) || (style & wxYES_NO),
-                  "don't create dialog with only the Cancel button!" );
-
-    if ( style & wxYES_NO ) {
-       buttons[Btn_Yes] = new wxButton(this, wxID_YES, _("Yes"));
-       buttons[Btn_No] = new wxButton(this, wxID_NO, _("No"));
-
-
-       if(style & wxNO_DEFAULT)
-          nDefaultBtn = Btn_No;
-       else
-          nDefaultBtn = Btn_Yes;
-    }
+        if ( m_dialogStyle & wxHELP )
+        {
+            wxButton * const
+                help = new wxButton(this, wxID_HELP, GetCustomHelpLabel());
+            sizerStd->AddButton(help);
+        }
 
 
-    if (style & wxOK) {
-        buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
+        if ( btnDef )
+        {
+            btnDef->SetDefault();
+            btnDef->SetFocus();
+        }
 
 
-        if ( nDefaultBtn == -1 )
-            nDefaultBtn = Btn_Ok;
-    }
+        sizerStd->Realize();
 
 
-    if (style & wxCANCEL) {
-        buttons[Btn_Cancel] = new wxButton(this, wxID_CANCEL, _("Cancel"));
+        return CreateSeparatedSizer(sizerStd);
     }
     }
+#endif // !__SMARTPHONE__
+
+    // Use standard labels for all buttons
+    return CreateSeparatedButtonSizer
+           (
+                m_dialogStyle & (wxOK | wxCANCEL | wxHELP | wxYES_NO |
+                                 wxNO_DEFAULT | wxCANCEL_DEFAULT)
+           );
+}
 
 
-    // get the longest caption and also calc the number of buttons
-    size_t nBtn, nButtons = 0;
-    long widthBtnMax = 0;
-    for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
-        if ( buttons[nBtn] ) {
-            nButtons++;
-            dc.GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
-            if ( width > widthBtnMax )
-                widthBtnMax = width;
-        }
-    }
+void wxGenericMessageDialog::DoCreateMsgdialog()
+{
+    wxDialog::Create(m_parent, wxID_ANY, m_caption, m_pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
 
 
-    // now we can place the buttons
-    if ( widthBtnMax < 75 )
-        widthBtnMax = 75;
-    else
-        widthBtnMax += 10;
-    long heightButton = widthBtnMax*23/75;
-
-    // *1.2 baselineskip
-    heightTextMax *= 12;
-    heightTextMax /= 10;
-
-    size_t nLineCount = lines.Count();
-
-    long widthButtonsTotal = nButtons * (widthBtnMax + LAYOUT_X_MARGIN) -
-                             LAYOUT_X_MARGIN;
-
-    // the size of the dialog
-    long widthDlg = wxMax(widthTextMax + iconSize + 4*LAYOUT_X_MARGIN,
-                        wxMax(widthButtonsTotal, width)) +
-                    2*LAYOUT_X_MARGIN,
-         heightDlg = 8*LAYOUT_Y_MARGIN + heightButton +
-                     heightTextMax*(nLineCount + 1);
-
-    // create the controls
-    // -------------------
-
-    // the icon first
-    c = new wxLayoutConstraints;
-    c->width.Absolute(iconSize);
-    c->height.Absolute(iconSize);
-    c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN);
-    c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
-    icon->SetConstraints(c);
-
-    wxStaticText *text = NULL;
-    for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
-        c = new wxLayoutConstraints;
-        if ( text == NULL )
-            c->top.SameAs(this, wxTop, 3*LAYOUT_Y_MARGIN);
-        else
-            c->top.Below(text);
+    bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
 
 
-        c->left.RightOf(icon, 2*LAYOUT_X_MARGIN);
-        c->width.Absolute(widthTextMax);
-        c->height.Absolute(heightTextMax);
-        text = new wxStaticText(this, -1, lines[nLine]);
-        text->SetConstraints(c);
-    }
+    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
 
 
-    // create the buttons
-    wxButton *btnPrevious = (wxButton *)NULL;
-    for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
-        if ( buttons[nBtn] ) {
-            c = new wxLayoutConstraints;
-
-            if ( btnPrevious ) {
-                c->left.RightOf(btnPrevious, LAYOUT_X_MARGIN);
-            }
-            else {
-                c->left.SameAs(this, wxLeft,
-                               (widthDlg - widthButtonsTotal) / 2);
-            }
-
-            c->width.Absolute(widthBtnMax);
-            c->top.Below(text, 4*LAYOUT_Y_MARGIN);
-            c->height.Absolute(heightButton);
-            buttons[nBtn]->SetConstraints(c);
-
-            btnPrevious = buttons[nBtn];
-        }
+    wxBoxSizer *icon_text = new wxBoxSizer( wxHORIZONTAL );
+
+#if wxUSE_STATBMP
+    // 1) icon
+    if (m_dialogStyle & wxICON_MASK)
+    {
+        wxStaticBitmap *icon = new wxStaticBitmap
+                                   (
+                                    this,
+                                    wxID_ANY,
+                                    wxArtProvider::GetMessageBoxIcon(m_dialogStyle)
+                                   );
+        if (is_pda)
+            topsizer->Add( icon, 0, wxTOP|wxLEFT|wxRIGHT | wxALIGN_LEFT, 10 );
+        else
+            icon_text->Add(icon, wxSizerFlags().Top().Border(wxRIGHT, 20));
     }
     }
+#endif // wxUSE_STATBMP
+
+#if wxUSE_STATTEXT
+    // 2) text
 
 
-    // set default button
-    // ------------------
+    wxBoxSizer * const textsizer = new wxBoxSizer(wxVERTICAL);
 
 
-    if ( nDefaultBtn != -1 ) {
-        buttons[nDefaultBtn]->SetDefault();
-        buttons[nDefaultBtn]->SetFocus();
+    // We want to show the main message in a different font to make it stand
+    // out if the extended message is used as well. This looks better and is
+    // more consistent with the native dialogs under MSW and GTK.
+    wxString lowerMessage;
+    if ( !m_extendedMessage.empty() )
+    {
+        wxTitleTextWrapper titleWrapper(this);
+        textsizer->Add(CreateTextSizer(GetMessage(), titleWrapper),
+                       wxSizerFlags().Border(wxBOTTOM, 20));
+
+        lowerMessage = GetExtendedMessage();
     }
     }
-    else {
-        wxFAIL_MSG( "can't find default button for this dialog." );
+    else // no extended message
+    {
+        lowerMessage = GetMessage();
     }
 
     }
 
-    // position the controls and the dialog itself
-    // -------------------------------------------
+    textsizer->Add(CreateTextSizer(lowerMessage));
 
 
-    SetClientSize(widthDlg, heightDlg);
+    icon_text->Add(textsizer, 0, wxALIGN_CENTER, 10);
+    topsizer->Add( icon_text, 1, wxLEFT|wxRIGHT|wxTOP, 10 );
+#endif // wxUSE_STATTEXT
 
 
-    // SetSizeHints() wants the size of the whole dialog, not just client size
-    wxSize sizeTotal = GetSize(),
-           sizeClient = GetClientSize();
-    SetSizeHints(widthDlg + sizeTotal.GetWidth() - sizeClient.GetWidth(),
-                 heightDlg + sizeTotal.GetHeight() - sizeClient.GetHeight());
+    // 3) optional checkbox and detailed text
+    AddMessageDialogCheckBox( topsizer );
+    AddMessageDialogDetails( topsizer );
 
 
-    Layout();
+    // 4) buttons
+    wxSizer *sizerBtn = CreateMsgDlgButtonSizer();
+    if ( sizerBtn )
+        topsizer->Add(sizerBtn, 0, wxEXPAND | wxALL, 10 );
 
 
-    Centre(wxCENTER_FRAME | wxBOTH);
+    SetAutoLayout( true );
+    SetSizer( topsizer );
 
 
-    wxEndBusyCursor();
+    topsizer->SetSizeHints( this );
+    topsizer->Fit( this );
+    wxSize size( GetSize() );
+    if (size.x < size.y*3/2)
+    {
+        size.x = size.y*3/2;
+        SetSize( size );
+    }
+
+    Centre( wxBOTH | wxCENTER_FRAME);
 }
 
 void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
 }
 
 void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
@@ -315,14 +250,31 @@ void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
     EndModal( wxID_NO );
 }
 
     EndModal( wxID_NO );
 }
 
+void wxGenericMessageDialog::OnHelp(wxCommandEvent& WXUNUSED(event))
+{
+    EndModal( wxID_HELP );
+}
+
 void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
 {
 void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
 {
-    /* Allow cancellation via ESC/Close button except if
-       only YES and NO are specified. */
-    if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
+    // Allow cancellation via ESC/Close button except if
+    // only YES and NO are specified.
+    const long style = GetMessageDialogStyle();
+    if ( (style & wxYES_NO) != wxYES_NO || (style & wxCANCEL) )
     {
         EndModal( wxID_CANCEL );
     }
 }
 
     {
         EndModal( wxID_CANCEL );
     }
 }
 
+int wxGenericMessageDialog::ShowModal()
+{
+    if ( !m_created )
+    {
+        m_created = true;
+        DoCreateMsgdialog();
+    }
+
+    return wxMessageDialogBase::ShowModal();
+}
 
 
+#endif // wxUSE_MSGDLG