From dbc65e2760f79e0296c7e8b49ec6791d62070d3e Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 12 May 2002 19:35:33 +0000
Subject: [PATCH] fixed wxFontDialog API: accept const ref instead of (well, in
 addition to) a possibly NULL pointer

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15534 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/fontdlg.tex   | 37 ++++++++++++++-----
 include/wx/fontdlg.h        | 72 +++++++++++++++++++++++++++++++++----
 include/wx/gtk/fontdlg.h    | 36 +++++++++----------
 include/wx/gtk1/fontdlg.h   | 36 +++++++++----------
 include/wx/msw/fontdlg.h    | 19 +++++-----
 samples/dialogs/dialogs.cpp | 24 ++++++++-----
 src/common/cmndata.cpp      | 28 ++++++++++-----
 src/gtk/fontdlg.cpp         | 20 +++++++----
 src/gtk1/fontdlg.cpp        | 20 +++++++----
 src/msw/fontdlg.cpp         | 21 -----------
 10 files changed, 198 insertions(+), 115 deletions(-)

diff --git a/docs/latex/wx/fontdlg.tex b/docs/latex/wx/fontdlg.tex
index 6af8c9c6d0..0c77c38639 100644
--- a/docs/latex/wx/fontdlg.tex
+++ b/docs/latex/wx/fontdlg.tex
@@ -167,10 +167,29 @@ This class represents the font chooser dialog.
 
 \membersection{wxFontDialog::wxFontDialog}
 
-\func{}{wxFontDialog}{\param{wxWindow* }{parent}, \param{wxFontData* }{data = NULL}}
+\func{}{wxFontDialog}{\void}
 
-Constructor. Pass a parent window, and optionally a pointer to a block of font
-data, which will be copied to the font dialog's font data.
+\func{}{wxFontDialog}{\param{wxWindow* }{parent}}
+
+\func{}{wxFontDialog}{\param{wxWindow* }{parent}, \param{const wxFontData\& }{data}}
+
+Constructor. Pass a parent window, and optionally the 
+\helpref{font data}{wxfontdata} object to be used to initialize the dialog
+controls. If the default constructor is used, 
+\helpref{Create()}{wxfontdialogcreate} must be called before the dialog can be
+shown.
+
+\membersection{wxFontDialog::Create}\label{wxfontdialogcreate}
+
+\func{bool}{Create}{\void}
+
+\func{bool}{Create}{\param{wxWindow* }{parent}}
+
+\func{bool}{Create}{\param{wxWindow* }{parent}, \param{const wxFontData\& }{data}}
+
+Creates the dialog if it the wxFontDialog object had been initialized using the
+default constructor. Returns {\tt TRUE} on success and {\tt FALSE} if an error
+occured.
 
 \membersection{wxFontDialog::\destruct{wxFontDialog}}
 
@@ -180,6 +199,8 @@ Destructor.
 
 \membersection{wxFontDialog::GetFontData}
 
+\constfunc{const wxFontData\&}{GetFontData}{\void}
+
 \func{wxFontData\&}{GetFontData}{\void}
 
 Returns the \helpref{font data}{wxfontdata} associated with the font dialog.
@@ -188,10 +209,10 @@ Returns the \helpref{font data}{wxfontdata} associated with the font dialog.
 
 \func{int}{ShowModal}{\void}
 
-Shows the dialog, returning wxID\_OK if the user pressed Ok, and wxID\_CANCEL
-otherwise.
+Shows the dialog, returning {\tt wxID\_OK} if the user pressed Ok, and 
+{\tt wxID\_CANCEL} otherwise.
 
-If the user cancels the dialog (ShowModal returns wxID\_CANCEL), no font will be
-created. If the user presses OK (ShowModal returns wxID\_OK), a new wxFont will
-be created and stored in the font dialog's wxFontData structure.
+If the user cancels the dialog (ShowModal returns {\tt wxID\_CANCEL}), no font
+will be created. If the user presses OK, a new wxFont will be created and
+stored in the font dialog's wxFontData structure.
 
diff --git a/include/wx/fontdlg.h b/include/wx/fontdlg.h
index 1396becad1..f61331e1e5 100644
--- a/include/wx/fontdlg.h
+++ b/include/wx/fontdlg.h
@@ -1,20 +1,80 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/fontdlg.h
+// Purpose:     common interface for different wxFontDialog classes
+// Author:      Vadim Zeitlin
+// Modified by:
+// Created:     12.05.02
+// RCS-ID:      $Id$
+// Copyright:   (c) 1997-2002 wxWindows team
+// Licence:     wxWindows license
+///////////////////////////////////////////////////////////////////////////////
+
 #ifndef _WX_FONTDLG_H_BASE_
 #define _WX_FONTDLG_H_BASE_
 
+#include "wx/defs.h"            // for wxUSE_FONTDLG
+
 #if wxUSE_FONTDLG
 
+#include "wx/dialog.h"          // the base class
+#include "wx/cmndata.h"         // wxFontData
+
+// ----------------------------------------------------------------------------
+// wxFontDialog interface
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxFontDialogBase : public wxDialog
+{
+public:
+    // create the font dialog
+    wxFontDialogBase() { }
+    wxFontDialogBase(wxWindow *parent) { }
+    wxFontDialogBase(wxWindow *parent, const wxFontData& data) { }
+
+    bool Create(wxWindow *parent)
+        { return DoCreate(parent); }
+    bool Create(wxWindow *parent, const wxFontData& data)
+        { m_fontData = data; return Create(parent); }
+
+    virtual ~wxFontDialogBase();
+
+    // retrieve the font data
+    const wxFontData& GetFontData() const { return m_fontData; }
+    wxFontData& GetFontData() { return m_fontData; }
+
+    // deprecated interface, for compatibility only, don't use
+    wxFontDialogBase(wxWindow *parent, const wxFontData *data)
+        { Init(); Create(parent, data); }
+
+    bool Create(wxWindow *parent, const wxFontData *data)
+        { if ( data ) m_fontData = *data; return Create(parent); }
+
+protected:
+    virtual bool DoCreate(wxWindow *parent) { m_parent = parent; return TRUE; }
+
+    wxFontData m_fontData;
+};
+
+// ----------------------------------------------------------------------------
+// platform-specific wxFontDialog implementation
+// ----------------------------------------------------------------------------
+
 #if defined(__WXUNIVERSAL__) || defined(__WXMOTIF__) || defined(__WXMAC__)
-#   include "wx/generic/fontdlgg.h"
-#   define wxFontDialog wxGenericFontDialog
-#   define sm_classwxFontDialog sm_classwxGenericFontDialog
+    #include "wx/generic/fontdlgg.h"
+    #define wxFontDialog wxGenericFontDialog
+    #define sm_classwxFontDialog sm_classwxGenericFontDialog
 #elif defined(__WXMSW__)
-#   include "wx/msw/fontdlg.h"
+    #include "wx/msw/fontdlg.h"
 #elif defined(__WXGTK__)
-#   include "wx/gtk/fontdlg.h"
+    #include "wx/gtk/fontdlg.h"
 #elif defined(__WXPM__)
-#   include "wx/os2/fontdlg.h"
+    #include "wx/os2/fontdlg.h"
 #endif
 
+// ----------------------------------------------------------------------------
+// global public functions
+// ----------------------------------------------------------------------------
+
 // get the font from user and return it, returns wxNullFont if the dialog was
 // cancelled
 wxFont WXDLLEXPORT
diff --git a/include/wx/gtk/fontdlg.h b/include/wx/gtk/fontdlg.h
index d6f157bc71..20a92375e8 100644
--- a/include/wx/gtk/fontdlg.h
+++ b/include/wx/gtk/fontdlg.h
@@ -15,33 +15,31 @@
 #pragma interface "fontdlg.h"
 #endif
 
-#include "wx/setup.h"
-#include "wx/gdicmn.h"
-#include "wx/font.h"
-#include "wx/dialog.h"
-#include "wx/cmndata.h"
-
-//-----------------------------------------------------------------------------
-// classes
-//-----------------------------------------------------------------------------
-
-class wxFontDialog;
-
 //-----------------------------------------------------------------------------
 // wxFontDialog
 //-----------------------------------------------------------------------------
 
-class wxFontDialog: public wxDialog
+class wxFontDialog : public wxFontDialogBase
 {
 public:
-    wxFontDialog() {}
-    wxFontDialog( wxWindow *parent, wxFontData *data = (wxFontData *) NULL );
-    ~wxFontDialog();
+    wxFontDialog() : wxFontDialogBase() { /* must be Create()d later */ }
+    wxFontDialog(wxWindow *parent)
+        : wxFontDialogBase(parent) { Create(parent); }
+    wxFontDialog(wxWindow *parent, const wxFontData& data)
+        : wxFontDialogBase(parent, data) { Create(parent, data); }
+
+    virtual ~wxFontDialog();
+
+    // implementation only
+    void SetChosenFont(const char *name);
 
-    wxFontData& GetFontData() { return m_fontData; }
+    // deprecated interface, don't use
+    wxFontDialog(wxWindow *parent, const wxFontData *data)
+        : wxFontDialogBase(parent, data) { Create(parent, data); }
 
-//protected:
-    wxFontData m_fontData;
+protected:
+    // create the GTK dialog
+    virtual bool DoCreate(wxWindow *parent);
 
 private:
     DECLARE_DYNAMIC_CLASS(wxFontDialog)
diff --git a/include/wx/gtk1/fontdlg.h b/include/wx/gtk1/fontdlg.h
index d6f157bc71..20a92375e8 100644
--- a/include/wx/gtk1/fontdlg.h
+++ b/include/wx/gtk1/fontdlg.h
@@ -15,33 +15,31 @@
 #pragma interface "fontdlg.h"
 #endif
 
-#include "wx/setup.h"
-#include "wx/gdicmn.h"
-#include "wx/font.h"
-#include "wx/dialog.h"
-#include "wx/cmndata.h"
-
-//-----------------------------------------------------------------------------
-// classes
-//-----------------------------------------------------------------------------
-
-class wxFontDialog;
-
 //-----------------------------------------------------------------------------
 // wxFontDialog
 //-----------------------------------------------------------------------------
 
-class wxFontDialog: public wxDialog
+class wxFontDialog : public wxFontDialogBase
 {
 public:
-    wxFontDialog() {}
-    wxFontDialog( wxWindow *parent, wxFontData *data = (wxFontData *) NULL );
-    ~wxFontDialog();
+    wxFontDialog() : wxFontDialogBase() { /* must be Create()d later */ }
+    wxFontDialog(wxWindow *parent)
+        : wxFontDialogBase(parent) { Create(parent); }
+    wxFontDialog(wxWindow *parent, const wxFontData& data)
+        : wxFontDialogBase(parent, data) { Create(parent, data); }
+
+    virtual ~wxFontDialog();
+
+    // implementation only
+    void SetChosenFont(const char *name);
 
-    wxFontData& GetFontData() { return m_fontData; }
+    // deprecated interface, don't use
+    wxFontDialog(wxWindow *parent, const wxFontData *data)
+        : wxFontDialogBase(parent, data) { Create(parent, data); }
 
-//protected:
-    wxFontData m_fontData;
+protected:
+    // create the GTK dialog
+    virtual bool DoCreate(wxWindow *parent);
 
 private:
     DECLARE_DYNAMIC_CLASS(wxFontDialog)
diff --git a/include/wx/msw/fontdlg.h b/include/wx/msw/fontdlg.h
index f404b7e065..2114a3f55a 100644
--- a/include/wx/msw/fontdlg.h
+++ b/include/wx/msw/fontdlg.h
@@ -16,28 +16,25 @@
     #pragma interface "fontdlg.h"
 #endif
 
-#include "wx/dialog.h"
-#include "wx/cmndata.h"
-
 // ----------------------------------------------------------------------------
 // wxFontDialog
 // ----------------------------------------------------------------------------
 
-class WXDLLEXPORT wxFontDialog : public wxDialog
+class WXDLLEXPORT wxFontDialog : public wxFontDialogBase
 {
 public:
-    wxFontDialog();
-    wxFontDialog(wxWindow *parent, wxFontData *data = NULL);
-
-    bool Create(wxWindow *parent, wxFontData *data = NULL);
+    wxFontDialog() : wxFontDialogBase() { }
+    wxFontDialog(wxWindow *parent) : wxFontDialogBase(parent) { }
+    wxFontDialog(wxWindow *parent, const wxFontData& data)
+        : wxFontDialogBase(parent, data) { }
 
     virtual int ShowModal();
 
-    wxFontData& GetFontData() { return m_fontData; }
+    // deprecated
+    wxFontDialog(wxWindow *parent, wxFontData *data)
+        : wxFontDialogBase(parent, data) { }
 
 protected:
-    wxFontData m_fontData;
-
     DECLARE_DYNAMIC_CLASS(wxFontDialog)
 };
 
diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp
index e2fd90b69a..e8df89afe6 100644
--- a/samples/dialogs/dialogs.cpp
+++ b/samples/dialogs/dialogs.cpp
@@ -239,19 +239,25 @@ void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
 
 void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
 {
-      wxFontData data;
-      data.SetInitialFont(wxGetApp().m_canvasFont);
-      data.SetColour(wxGetApp().m_canvasTextColour);
+    wxFontData data;
+    data.SetInitialFont(wxGetApp().m_canvasFont);
+    data.SetColour(wxGetApp().m_canvasTextColour);
 
-      wxFontDialog *dialog = new wxFontDialog(this, &data);
-      if (dialog->ShowModal() == wxID_OK)
-      {
-        wxFontData retData = dialog->GetFontData();
+    // you might also do this:
+    //
+    //  wxFontDialog dialog;
+    //  if ( !dialog.Create(this, data) { ... error ... }
+    //
+    wxFontDialog dialog(this, data);
+
+    if (dialog.ShowModal() == wxID_OK)
+    {
+        wxFontData retData = dialog.GetFontData();
         wxGetApp().m_canvasFont = retData.GetChosenFont();
         wxGetApp().m_canvasTextColour = retData.GetColour();
         myCanvas->Refresh();
-      }
-      dialog->Destroy();
+    }
+    //else: cancelled by the user, don't change the font
 }
 
 #if defined(__WXMSW__) && wxTEST_GENERIC_DIALOGS_IN_MSW
diff --git a/src/common/cmndata.cpp b/src/common/cmndata.cpp
index e09196f2fe..9080a155e9 100644
--- a/src/common/cmndata.cpp
+++ b/src/common/cmndata.cpp
@@ -39,6 +39,10 @@
 #include "wx/cmndata.h"
 #include "wx/log.h"
 
+#if wxUSE_FONTDLG
+    #include "wx/fontdlg.h"
+#endif // wxUSE_FONTDLG
+
 // For compatibility
 #if (defined(__WXMOTIF__) || defined(__WXGTK__) || defined(__WXX11__)|| defined(__WXPM__) || defined(__WXMAC__)) && wxUSE_POSTSCRIPT
     #define wxCOMPATIBILITY_WITH_PRINTSETUPDATA 1
@@ -178,6 +182,14 @@ wxFontData::~wxFontData()
 {
 }
 
+#if wxUSE_FONTDLG
+
+wxFontDialogBase::~wxFontDialogBase()
+{
+}
+
+#endif // wxUSE_FONTDLG
+
 #if wxUSE_PRINTING_ARCHITECTURE
 // ----------------------------------------------------------------------------
 // Print data
@@ -192,16 +204,16 @@ wxPrintData::wxPrintData()
 #if TARGET_CARBON
     m_macPageFormat = kPMNoPageFormat;
     m_macPrintSettings = kPMNoPrintSettings;
-    
+
   #if PM_USE_SESSION_APIS
     PMPrintSession macPrintSession = kPMNoReference;
     OSStatus       err;
-    
+
     err = ::UMAPrOpen(&macPrintSession) ;
     if ( err == noErr )
-    {  
+    {
         err = PMCreatePageFormat((PMPageFormat *)&m_macPageFormat);
-        
+
         //  Note that PMPageFormat is not session-specific, but calling
         //  PMSessionDefaultPageFormat assigns values specific to the printer
         //  associated with the current printing session.
@@ -210,9 +222,9 @@ wxPrintData::wxPrintData()
             err = PMSessionDefaultPageFormat((PMPrintSession)macPrintSession,
                                              (PMPageFormat)m_macPageFormat);
         }
-        
+
         err = PMCreatePrintSettings((PMPrintSettings *)&m_macPrintSettings);
-        
+
         //  Note that PMPrintSettings is not session-specific, but calling
         //  PMSessionDefaultPrintSettings assigns values specific to the printer
         //  associated with the current printing session.
@@ -311,7 +323,7 @@ wxPrintData::~wxPrintData()
   #endif
         m_macPrintSettings = kPMNoPrintSettings;
     }
-    
+
 #else
     wxASSERT( m_macPrintSettings );
     // we should perhaps delete
@@ -861,7 +873,7 @@ bool wxPrintData::Ok() const
     return (m_devMode != NULL) ;
 #else
     return TRUE;
-#endif    
+#endif
 }
 
 // ----------------------------------------------------------------------------
diff --git a/src/gtk/fontdlg.cpp b/src/gtk/fontdlg.cpp
index fb404d8899..6206f9934d 100644
--- a/src/gtk/fontdlg.cpp
+++ b/src/gtk/fontdlg.cpp
@@ -76,7 +76,7 @@ void gtk_fontdialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFontDialog *dial
 
     gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
 
-    dialog->m_fontData.SetChosenFont(wxFont(fontname));
+    dialog->SetChosenFont(fontname);
 
     g_free( fontname );
 
@@ -105,10 +105,9 @@ void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialo
 // wxFontDialog
 //-----------------------------------------------------------------------------
 
-IMPLEMENT_DYNAMIC_CLASS(wxFontDialog,wxDialog)
+IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
 
-wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
-            : m_fontData(*fontdata)
+bool wxFontDialog::DoCreate(wxWindow *parent)
 {
     m_needParent = FALSE;
 
@@ -116,10 +115,10 @@ wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
         !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
                      wxDefaultValidator, wxT("fontdialog") ))
     {
-        wxFAIL_MSG( wxT("wxXX creation failed") );
-        return;
+        wxFAIL_MSG( wxT("wxFontDialog creation failed") );
+        return FALSE;
     }
-    
+
     wxString m_message( _("Choose font") );
     m_widget = gtk_font_selection_dialog_new( m_message.mbc_str() );
 
@@ -166,11 +165,18 @@ wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
             wxFAIL_MSG(_T("font is ok but no native font info?"));
         }
     }
+
+    return TRUE;
 }
 
 wxFontDialog::~wxFontDialog()
 {
 }
 
+void wxFontDialog::SetChosenFont(const char *fontname)
+{
+    m_fontData.SetChosenFont(wxFont(fontname));
+}
+
 #endif // wxUSE_FONTDLG
 
diff --git a/src/gtk1/fontdlg.cpp b/src/gtk1/fontdlg.cpp
index fb404d8899..6206f9934d 100644
--- a/src/gtk1/fontdlg.cpp
+++ b/src/gtk1/fontdlg.cpp
@@ -76,7 +76,7 @@ void gtk_fontdialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFontDialog *dial
 
     gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
 
-    dialog->m_fontData.SetChosenFont(wxFont(fontname));
+    dialog->SetChosenFont(fontname);
 
     g_free( fontname );
 
@@ -105,10 +105,9 @@ void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialo
 // wxFontDialog
 //-----------------------------------------------------------------------------
 
-IMPLEMENT_DYNAMIC_CLASS(wxFontDialog,wxDialog)
+IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
 
-wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
-            : m_fontData(*fontdata)
+bool wxFontDialog::DoCreate(wxWindow *parent)
 {
     m_needParent = FALSE;
 
@@ -116,10 +115,10 @@ wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
         !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
                      wxDefaultValidator, wxT("fontdialog") ))
     {
-        wxFAIL_MSG( wxT("wxXX creation failed") );
-        return;
+        wxFAIL_MSG( wxT("wxFontDialog creation failed") );
+        return FALSE;
     }
-    
+
     wxString m_message( _("Choose font") );
     m_widget = gtk_font_selection_dialog_new( m_message.mbc_str() );
 
@@ -166,11 +165,18 @@ wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
             wxFAIL_MSG(_T("font is ok but no native font info?"));
         }
     }
+
+    return TRUE;
 }
 
 wxFontDialog::~wxFontDialog()
 {
 }
 
+void wxFontDialog::SetChosenFont(const char *fontname)
+{
+    m_fontData.SetChosenFont(wxFont(fontname));
+}
+
 #endif // wxUSE_FONTDLG
 
diff --git a/src/msw/fontdlg.cpp b/src/msw/fontdlg.cpp
index b15e0adcbd..40faa9b30d 100644
--- a/src/msw/fontdlg.cpp
+++ b/src/msw/fontdlg.cpp
@@ -64,27 +64,6 @@ IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
 // wxFontDialog
 // ----------------------------------------------------------------------------
 
-wxFontDialog::wxFontDialog()
-{
-    m_parent = NULL;
-}
-
-wxFontDialog::wxFontDialog(wxWindow *parent, wxFontData *data)
-{
-    Create(parent, data);
-}
-
-bool wxFontDialog::Create(wxWindow *parent, wxFontData *data)
-{
-    m_parent = parent;
-
-    wxCHECK_MSG( data, FALSE, _T("no font data in wxFontDialog") );
-
-    m_fontData = *data;
-
-    return TRUE;
-}
-
 int wxFontDialog::ShowModal()
 {
     DWORD flags = CF_SCREENFONTS | CF_NOSIMULATIONS;
-- 
2.50.0

