]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/msgdlgg.cpp
remove duplicate deletion
[wxWidgets.git] / src / generic / msgdlgg.cpp
index 31e731998d8eb3c5647848834595778be6640772..18bd2ba2ba42323dbcf3a971575bcd9fa9888ba1 100644 (file)
@@ -1,12 +1,12 @@
 /////////////////////////////////////////////////////////////////////////////
 // Name:        msgdlgg.cpp
 // Purpose:     wxGenericMessageDialog
-// Author:      Julian Smart
+// Author:      Julian Smart, Robert Roebling
 // Modified by:
 // Created:     04/01/98
 // RCS-ID:      $Id$
-// Copyright:   (c) Julian Smart and Markus Holzem
-// Licence:    wxWindows license
+// Copyright:   (c) Julian Smart, Markus Holzem, Robert Roebling
+// Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
 #ifdef __GNUG__
 #endif
 
 #ifndef WX_PRECOMP
-#include "wx/utils.h"
-#include "wx/dialog.h"
-#include "wx/listbox.h"
-#include "wx/button.h"
-#include "wx/stattext.h"
-#include "wx/layout.h"
-#include "wx/intl.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/app.h"
 #endif
 
 #include <stdio.h>
 
 #include "wx/generic/msgdlgg.h"
 
-///////////////////////////////////////////////////////////////////
-// New dialog box implementations
+#if wxUSE_STATLINE
+  #include "wx/statline.h"
+#endif
 
-// Split message, using constraints to position controls
-void wxSplitMessage2(const wxChar *message, wxList *messageList, wxWindow *parent, wxRowColSizer *sizer)
-{
-  wxChar *copyMessage = copystring(message);
-  size_t i = 0;
-  size_t len = wxStrlen(copyMessage);
-  wxChar *currentMessage = copyMessage;
-
-//  wxWindow *lastWindow = parent;
-
-  while (i < len) {
-    while ((i < len) && (copyMessage[i] != _T('\n'))) i++;
-    if (i < len) copyMessage[i] = 0;
-    wxStaticText *mess = new wxStaticText(parent, -1, currentMessage);
-
-/*
-    wxLayoutConstraints *c = new wxLayoutConstraints;
-    c->left.SameAs             (parent, wxLeft, 10);
-    c->top.SameAs              (lastWindow, wxBottom, 5);
-    c->right.AsIs                      ();
-    c->height.AsIs                     ();
-
-    mess->SetConstraints(c);
-*/
-    sizer->AddSizerChild(mess);
-
-    messageList->Append(mess);
-
-    currentMessage = copyMessage + i + 1;
-  }
-  delete[] copyMessage;
-}
+// ----------------------------------------------------------------------------
+// icons
+// ----------------------------------------------------------------------------
 
 #if !USE_SHARED_LIBRARY
 BEGIN_EVENT_TABLE(wxGenericMessageDialog, wxDialog)
-       EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
-       EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
-       EVT_BUTTON(wxID_CANCEL, wxGenericMessageDialog::OnCancel)
+        EVT_BUTTON(wxID_YES, wxGenericMessageDialog::OnYes)
+        EVT_BUTTON(wxID_NO, wxGenericMessageDialog::OnNo)
+        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):
-       wxDialog(parent, -1, caption, pos,
-#ifdef __WXMOTIF_
-           wxSize(400, 300),
-#else
-           wxDefaultSize,
-#endif
-           wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
+wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
+                                                const wxString& message,
+                                                const wxString& caption,
+                                                long style,
+                                                const wxPoint& pos)
+                      : wxDialog( parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE )
 {
     m_dialogStyle = style;
 
     wxBeginBusyCursor();
 
-    wxSizer *topSizer = new wxSizer(this, wxSizerShrink);
-    topSizer->SetBorder(10, 10);
-
-    wxRowColSizer *messageSizer = new wxRowColSizer(topSizer, wxSIZER_COLS, 100);
-    messageSizer->SetName(_T("messageSizer"));
+    wxLayoutConstraints *c;
+    SetAutoLayout(TRUE);
 
-//    bool centre = ((style & wxCENTRE) == wxCENTRE);
+    wxStaticBitmap *icon = new wxStaticBitmap(this, -1,
+                                              wxTheApp->GetStdIcon(style & wxICON_MASK));
+    const int iconSize = icon->GetBitmap().GetWidth();
 
-    wxList messageList;
-    wxSplitMessage2(message, &messageList, this, messageSizer);
+    // split the message in lines
+    // --------------------------
 
-    // Insert a spacer
-    wxSpacingSizer *spacingSizer = new wxSpacingSizer(topSizer, wxBelow, messageSizer, 20);
+    wxArrayString lines;
+    wxSize sizeText = SplitTextMessage(message, &lines);
+    long widthTextMax = sizeText.GetWidth(),
+         heightTextMax = sizeText.GetHeight();
+    size_t nLineCount = lines.GetCount();
 
-    wxRowColSizer *buttonSizer = new wxRowColSizer(topSizer, wxSIZER_ROWS);
-    buttonSizer->SetName(_T("buttonSizer"));
-    buttonSizer->SetSpacing(12,0);
+    // 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;
+    }
 
-    // Specify constraints for the button sizer
-    wxLayoutConstraints *c = new wxLayoutConstraints;
-    c->width.AsIs              ();
-    c->height.AsIs             ();
-    c->top.Below               (spacingSizer);
-    c->centreX.SameAs  (spacingSizer, wxCentreX);
-    buttonSizer->SetConstraints(c);
+    if (style & wxOK) {
+        buttons[Btn_Ok] = new wxButton(this, wxID_OK, _("OK"));
 
-    wxButton *ok = (wxButton *) NULL;
-    wxButton *cancel = (wxButton *) NULL;
-    wxButton *yes = (wxButton *) NULL;
-    wxButton *no = (wxButton *) NULL;
+        if ( nDefaultBtn == -1 )
+            nDefaultBtn = Btn_Ok;
+    }
 
-    if (style & wxYES_NO) {
-       yes = new wxButton(this, wxID_YES, _("Yes"), wxDefaultPosition, wxSize(75,-1) );
-       no = new wxButton(this, wxID_NO, _("No"), wxDefaultPosition, wxSize(75,-1) );
+    if (style & wxCANCEL) {
+        buttons[Btn_Cancel] = new wxButton(this, wxID_CANCEL, _("Cancel"));
+    }
 
-       buttonSizer->AddSizerChild(yes);
-       buttonSizer->AddSizerChild(no);
+    // get the longest caption and also calc the number of buttons
+    size_t nBtn, nButtons = 0;
+    int width, widthBtnMax = 0;
+    for ( nBtn = 0; nBtn < Btn_Max; nBtn++ ) {
+        if ( buttons[nBtn] ) {
+            nButtons++;
+            GetTextExtent(buttons[nBtn]->GetLabel(), &width, NULL);
+            if ( width > widthBtnMax )
+                widthBtnMax = width;
+        }
     }
 
-    if (style & wxOK) {
-        ok = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxSize(75,-1) );
-        buttonSizer->AddSizerChild(ok);
+    // 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;
+
+    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);
+
+        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);
     }
 
-    if (style & wxCANCEL) {
-        cancel = new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxSize(75,-1) );
-        buttonSizer->AddSizerChild(cancel);
+    // 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];
+        }
     }
 
-    if (ok)
-    {
-      ok->SetDefault();
-      ok->SetFocus();
+    // set default button
+    // ------------------
+
+    if ( nDefaultBtn != -1 ) {
+        buttons[nDefaultBtn]->SetDefault();
+        buttons[nDefaultBtn]->SetFocus();
     }
-    else if (yes)
-    {
-      yes->SetDefault();
-      yes->SetFocus();
+    else {
+        wxFAIL_MSG( "can't find default button for this dialog." );
     }
 
+    // position the controls and the dialog itself
+    // -------------------------------------------
+
+    SetClientSize(widthDlg, heightDlg);
+
+    // 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());
+
     Layout();
-    Centre(wxBOTH);
+
+    Centre(wxCENTER_FRAME | wxBOTH);
 
     wxEndBusyCursor();
 }
 
 void wxGenericMessageDialog::OnYes(wxCommandEvent& WXUNUSED(event))
 {
-    EndModal(wxID_YES);
+    EndModal( wxID_YES );
 }
 
 void wxGenericMessageDialog::OnNo(wxCommandEvent& WXUNUSED(event))
 {
-    EndModal(wxID_NO);
+    EndModal( wxID_NO );
 }
 
 void wxGenericMessageDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
 {
-    // Allow cancellation via ESC/Close button except if
-    // only YES and NO are specified.
+    /* Allow cancellation via ESC/Close button except if
+       only YES and NO are specified. */
     if ( (m_dialogStyle & wxYES_NO) != wxYES_NO || (m_dialogStyle & wxCANCEL) )
-        EndModal(wxID_CANCEL);
+    {
+        EndModal( wxID_CANCEL );
+    }
 }