]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/font.cpp
Fix nonsensical checked menu item label and behaviour
[wxWidgets.git] / src / os2 / font.cpp
index e4abc8de91e245c0e2f11010037875d690ab8c3d..18dcce67b7efba80464a14127747bcfec4522796 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        font.cpp
+// Name:        src/os2/font.cpp
 // Purpose:     wxFont class
 // Author:      David Webster
 // Modified by:
@@ -9,6 +9,9 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
 // ============================================================================
 // declarations
 // ============================================================================
 // headers
 // ----------------------------------------------------------------------------
 
- #include <malloc.h>
-// For compilers that support precompilation, includes "wx.h".
-#include "wx/wxprec.h"
-
 #ifndef WX_PRECOMP
     #include <stdio.h>
-    #include "wx/setup.h"
     #include "wx/list.h"
     #include "wx/utils.h"
     #include "wx/app.h"
     #include "wx/font.h"
+    #include "wx/log.h"
 #endif // WX_PRECOMP
 
 #include "wx/os2/private.h"
 
 #include "wx/fontutil.h"
 #include "wx/fontmap.h"
+#include "wx/encinfo.h"
 
 #include "wx/tokenzr.h"
 
+#include <malloc.h>
+
 IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
 
 // ----------------------------------------------------------------------------
@@ -49,7 +51,7 @@ public:
     wxFontRefData()
     {
         Init(-1, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, FALSE,
-             "", wxFONTENCODING_DEFAULT);
+             wxEmptyString, wxFONTENCODING_DEFAULT);
     }
 
     wxFontRefData( int             nSize
@@ -108,8 +110,13 @@ public:
     //
     inline int GetPointSize(void) const
     {
-        return m_bNativeFontInfoOk ? m_vNativeFontInfo.GetPointSize()
-                                   : m_nPointSize;
+        //
+        // We don't use the actual native font point size since it is
+        // the chosen physical font, which is usually only and approximation
+        // of the desired outline font.  The actual displayable point size
+        // is the one stored in the refData
+        //
+        return m_nPointSize;
     }
 
     inline int GetFamily(void) const
@@ -253,7 +260,7 @@ protected:
               ,WXHANDLE                hPS   = 0
              );
     //
-    // If TRUE, the pointer to the actual font is temporary and SHOULD NOT BE
+    // If true, the pointer to the actual font is temporary and SHOULD NOT BE
     // DELETED by destructor
     //
     bool                            m_bTemporary;
@@ -340,29 +347,35 @@ void wxFontRefData::Init(
     m_hFont = hFont;
     m_nFontId = (int)hFont;
 
-    m_bNativeFontInfoOk = TRUE;
+    m_bNativeFontInfoOk = true;
     m_vNativeFontInfo = rInfo;
 
-    if (m_hPS == NULLHANDLE)
+    if (hPS == NULLHANDLE)
     {
         m_hPS = ::WinGetPS(HWND_DESKTOP);
-        m_bInternalPS;
+        m_bInternalPS = true;
     }
     else
         m_hPS = (HPS)hPS;
-}
+
+    m_nFontId     = 0;
+    m_bTemporary  = FALSE;
+    m_pFM         = (PFONTMETRICS)NULL;
+    m_nNumFonts   = 0;
+} // end of wxFontRefData::Init
 
 wxFontRefData::~wxFontRefData()
 {
     Free();
 }
 
-bool wxFontRefData::Alloc(
-  wxFont*                           pFont
-)
+bool wxFontRefData::Alloc( wxFont* pFont )
 {
     wxString                        sFaceName;
     long                            flId = m_hFont;
+    long                            lRc;
+    ERRORID                         vError;
+    wxString                        sError;
 
     if (!m_bNativeFontInfoOk)
     {
@@ -374,25 +387,34 @@ bool wxFontRefData::Alloc(
                       ,sFaceName
                       ,pFont
                      );
-        m_bNativeFontInfoOk = TRUE;
+        m_bNativeFontInfoOk = true;
     }
-
-    if(::GpiCreateLogFont( m_hPS
-                          ,NULL
-                          ,flId
-                          ,&m_vNativeFontInfo.fa
-                         ) != GPI_ERROR)
+    else
+    {
+        if (flId == 0L)
+            flId = 1L;
+        else
+            flId++;
+        if (flId > 254)
+            flId = 1L;
+    }
+    if((lRc = ::GpiCreateLogFont( m_hPS
+                                 ,NULL
+                                 ,flId
+                                 ,&m_vNativeFontInfo.fa
+                                )) != GPI_ERROR)
+    {
        m_hFont = (WXHFONT)flId;
        m_nFontId = flId;
-
+    }
     if (!m_hFont)
     {
-        wxLogLastError("CreateFont");
+        vError = ::WinGetLastError(vHabmain);
+        sError = wxPMErrorToStr(vError);
+        wxLogLastError(wxT("CreateFont"));
     }
 
-    //
-    // Query for the actual metrics of the current font being used
-    //
+    ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
     ::GpiQueryFontMetrics(m_hPS, sizeof(FONTMETRICS), &m_vNativeFontInfo.fm);
 
     //
@@ -400,17 +422,43 @@ bool wxFontRefData::Alloc(
     //
     memcpy(&m_vFattrs, &m_vNativeFontInfo.fa, sizeof(m_vFattrs));
     memcpy(&m_vFname, &m_vNativeFontInfo.fn, sizeof(m_vFname));
-    m_nPointSize  = m_vNativeFontInfo.fm.lEmHeight;
-    if (strcmp(m_vNativeFontInfo.fa.szFacename, "Times New Roman") == 0)
+    //
+    // Going to leave the point size alone.  Mostly we use outline fonts
+    // that can be set to any point size inside of Presentation Parameters,
+    // regardless of whether or not the actual font is registered in the system.
+    // The GpiCreateLogFont will do enough by selecting the right family,
+    // and face name.
+    //
+    if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Times New Roman") == 0)
+        m_nFamily = wxROMAN;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Times New Roman MT 30") == 0)
+        m_nFamily = wxROMAN;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "@Times New Roman MT 30") == 0)
         m_nFamily = wxROMAN;
-    else if (strcmp(m_vNativeFontInfo.fa.szFacename, "WarpSans") == 0)
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Tms Rmn") == 0)
+        m_nFamily = wxROMAN;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "WarpSans") == 0)
+        m_nFamily = wxDECORATIVE;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Helvetica") == 0)
+        m_nFamily = wxSWISS;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Helv") == 0)
         m_nFamily = wxSWISS;
-    else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Script") == 0)
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Script") == 0)
         m_nFamily = wxSCRIPT;
-    else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier New") == 0)
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Courier New") == 0)
+        m_nFamily = wxTELETYPE;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Courier") == 0)
+        m_nFamily = wxTELETYPE;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System Monospaced") == 0)
+        m_nFamily = wxTELETYPE;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System VIO") == 0)
         m_nFamily = wxMODERN;
-    else if (strcmp(m_vNativeFontInfo.fa.szFacename, "Courier") == 0)
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "System Proportional") == 0)
         m_nFamily = wxMODERN;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Arial") == 0)
+        m_nFamily = wxSWISS;
+    else if (strcmp(m_vNativeFontInfo.fm.szFamilyname, "Swiss") == 0)
+        m_nFamily = wxSWISS;
     else
         m_nFamily = wxSWISS;
 
@@ -444,7 +492,7 @@ bool wxFontRefData::Alloc(
             m_nWeight = wxFONTWEIGHT_NORMAL;
     }
     m_bUnderlined = ((m_vNativeFontInfo.fa.fsSelection & FATTR_SEL_UNDERSCORE) != 0);
-    m_sFaceName = m_vNativeFontInfo.fa.szFacename;
+    m_sFaceName = (wxChar*)m_vNativeFontInfo.fa.szFacename;
     m_vEncoding = wxGetFontEncFromCharSet(m_vNativeFontInfo.fa.usCodePage);
 
     //
@@ -464,7 +512,7 @@ bool wxFontRefData::Alloc(
         // Select the font into the Presentation space
         //
         ::GpiSetCharSet(m_hPS, flId); // sets font for presentation space
-    return TRUE;
+    return true;
 } // end of wxFontRefData::Alloc
 
 void wxFontRefData::Free()
@@ -475,10 +523,6 @@ void wxFontRefData::Free()
 
     if ( m_hFont )
     {
-        if (!::GpiSetCharSet(m_hPS, LCID_DEFAULT))
-        {
-            wxLogLastError(wxT("DeleteObject(font)"));
-        }
         ::GpiDeleteSetId(m_hPS, 1L); /* delete the logical font          */
         m_nFontId = 0;
         m_hFont   = 0;
@@ -536,7 +580,7 @@ bool wxNativeFontInfo::GetUnderlined() const
 
 wxString wxNativeFontInfo::GetFaceName() const
 {
-    return fm.szFacename;
+    return (wxChar*)fm.szFacename;
 } // end of wxNativeFontInfo::GetFaceName
 
 wxFontFamily wxNativeFontInfo::GetFamily() const
@@ -546,14 +590,36 @@ wxFontFamily wxNativeFontInfo::GetFamily() const
     //
     // Extract family from facename
     //
-    if (strcmp(fa.szFacename, "Times New Roman") == 0)
+    if (strcmp(fm.szFamilyname, "Times New Roman") == 0)
+        nFamily = wxROMAN;
+    else if (strcmp(fm.szFamilyname, "Times New Roman MT 30") == 0)
+        nFamily = wxROMAN;
+    else if (strcmp(fm.szFamilyname, "@Times New Roman MT 30") == 0)
         nFamily = wxROMAN;
-    else if (strcmp(fa.szFacename, "WarpSans") == 0)
+    else if (strcmp(fm.szFamilyname, "Tms Rmn") == 0)
+        nFamily = wxROMAN;
+    else if (strcmp(fm.szFamilyname, "WarpSans") == 0)
+        nFamily = wxDECORATIVE;
+    else if (strcmp(fm.szFamilyname, "Helvetica") == 0)
+        nFamily = wxSWISS;
+    else if (strcmp(fm.szFamilyname, "Helv") == 0)
         nFamily = wxSWISS;
-    else if (strcmp(fa.szFacename, "Script") == 0)
+    else if (strcmp(fm.szFamilyname, "Script") == 0)
         nFamily = wxSCRIPT;
-    else if (strcmp(fa.szFacename, "Courier New") == 0)
+    else if (strcmp(fm.szFamilyname, "Courier New") == 0)
+        nFamily = wxTELETYPE;
+    else if (strcmp(fm.szFamilyname, "Courier") == 0)
+        nFamily = wxTELETYPE;
+    else if (strcmp(fm.szFamilyname, "System Monospaced") == 0)
+        nFamily = wxTELETYPE;
+    else if (strcmp(fm.szFamilyname, "System VIO") == 0)
         nFamily = wxMODERN;
+    else if (strcmp(fm.szFamilyname, "System Proportional") == 0)
+        nFamily = wxMODERN;
+    else if (strcmp(fm.szFamilyname, "Arial") == 0)
+        nFamily = wxSWISS;
+    else if (strcmp(fm.szFamilyname, "Swiss") == 0)
+        nFamily = wxSWISS;
     else
         nFamily = wxSWISS;
     return (wxFontFamily)nFamily;
@@ -624,10 +690,10 @@ void wxNativeFontInfo::SetUnderlined(
 } // end of wxNativeFontInfo::SetUnderlined
 
 void wxNativeFontInfo::SetFaceName(
-  wxString                          sFacename
+  const wxString&                   sFacename
 )
 {
-    wxStrncpy(fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
+    wxStrncpy((wxChar*)fa.szFacename, sFacename, WXSIZEOF(fa.szFacename));
 } // end of wxNativeFontInfo::SetFaceName
 
 void wxNativeFontInfo::SetFamily(
@@ -639,40 +705,41 @@ void wxNativeFontInfo::SetFamily(
     switch (eFamily)
     {
         case wxSCRIPT:
-            sFacename = _T("Script");
+            sFacename = wxT("Tms Rmn");
             break;
 
         case wxDECORATIVE:
-            sFacename = _T("Times New Roman");
+            sFacename = wxT("WarpSans");
             break;
 
         case wxROMAN:
-            sFacename = _T("Times New Roman");
+            sFacename = wxT("Tms Rmn");
             break;
 
         case wxTELETYPE:
+            sFacename = wxT("Courier") ;
+            break;
+
         case wxMODERN:
-            sFacename = _T("Courier New");
+            sFacename = wxT("System VIO") ;
             break;
 
         case wxSWISS:
-            sFacename = _T("WarpSans");
+            sFacename = wxT("Helv") ;
             break;
 
         case wxDEFAULT:
         default:
-            sFacename = _T("Helv");
+            sFacename = wxT("System VIO") ;
     }
 
-    if (!wxStrlen(fa.szFacename) )
+    if (!wxStrlen((wxChar*)fa.szFacename) )
     {
         SetFaceName(sFacename);
     }
 } // end of wxNativeFontInfo::SetFamily
 
-void wxNativeFontInfo::SetEncoding(
-  wxFontEncoding                    eEncoding
-)
+void wxNativeFontInfo::SetEncoding( wxFontEncoding eEncoding )
 {
     wxNativeEncodingInfo            vInfo;
 
@@ -680,8 +747,7 @@ void wxNativeFontInfo::SetEncoding(
                                   ,&vInfo
                                  ))
     {
-#if wxUSE_FONTMAP
-        if (wxTheFontMapper->GetAltForEncoding( eEncoding
+        if (wxFontMapper::Get()->GetAltForEncoding( eEncoding
                                                ,&vInfo
                                               ))
         {
@@ -696,18 +762,15 @@ void wxNativeFontInfo::SetEncoding(
             }
         }
         else
-#endif // wxUSE_FONTMAP
         {
             // unsupported encoding, replace with the default
             vInfo.charset = 850;
         }
     }
-    fa.usCodePage = vInfo.charset;
+    fa.usCodePage = (USHORT)vInfo.charset;
 } // end of wxNativeFontInfo::SetFaceName
 
-bool wxNativeFontInfo::FromString(
-  const wxString&                   rsStr
-)
+bool wxNativeFontInfo::FromString( const wxString& rsStr )
 {
     long                            lVal;
 
@@ -769,8 +832,8 @@ bool wxNativeFontInfo::FromString(
     sToken = vTokenizer.GetNextToken();
     if(!sToken)
         return FALSE;
-    wxStrcpy(fa.szFacename, sToken.c_str());
-    return TRUE;
+    wxStrcpy((wxChar*)fa.szFacename, sToken.c_str());
+    return true;
 } // end of wxNativeFontInfo::FromString
 
 wxString wxNativeFontInfo::ToString() const
@@ -801,17 +864,15 @@ void wxFont::Init()
 {
 } // end of wxFont::Init
 
-bool wxFont::Create(
-  const wxNativeFontInfo&           rInfo
-, WXHFONT                           hFont
-)
+bool wxFont::Create( const wxNativeFontInfo& rInfo,
+                     WXHFONT hFont )
 {
     UnRef();
     m_refData = new wxFontRefData( rInfo
                                   ,hFont
                                  );
     RealizeResource();
-    return TRUE;
+    return true;
 } // end of wxFont::Create
 
 wxFont::wxFont(
@@ -828,15 +889,13 @@ wxFont::wxFont(
 // Constructor for a font. Note that the real construction is done
 // in wxDC::SetFont, when information is available about scaling etc.
 // ----------------------------------------------------------------------------
-bool wxFont::Create(
-  int                               nPointSize
-, int                               nFamily
-, int                               nStyle
-, int                               nWeight
-, bool                              bUnderlined
-, const wxString&                   rsFaceName
-, wxFontEncoding                    vEncoding
-)
+bool wxFont::Create( int             nPointSize,
+                     int             nFamily,
+                     int             nStyle,
+                     int             nWeight,
+                     bool            bUnderlined,
+                     const wxString& rsFaceName,
+                     wxFontEncoding  vEncoding )
 {
     UnRef();
 
@@ -857,7 +916,7 @@ bool wxFont::Create(
                                   ,vEncoding
                                  );
     RealizeResource();
-    return TRUE;
+    return true;
 } // end of wxFont::Create
 
 wxFont::~wxFont()
@@ -875,21 +934,19 @@ bool wxFont::RealizeResource()
 {
     if ( GetResourceHandle() )
     {
-        return TRUE;
+        return true;
     }
     return M_FONTDATA->Alloc(this);
 } // end of wxFont::RealizeResource
 
-bool wxFont::FreeResource(
-  bool                              bForce
-)
+bool wxFont::FreeResource( bool WXUNUSED(bForce) )
 {
     if (GetResourceHandle())
     {
         M_FONTDATA->Free();
-        return TRUE;
+        return true;
     }
-    return FALSE;
+    return false;
 } // end of wxFont::FreeResource
 
 WXHANDLE wxFont::GetResourceHandle()
@@ -1003,7 +1060,7 @@ void wxFont::SetEncoding(
     RealizeResource();
 } // end of wxFont::SetEncoding
 
-void wxFont::SetNativeFontInfo(
+void wxFont::DoSetNativeFontInfo(
   const wxNativeFontInfo&           rInfo
 )
 {
@@ -1057,7 +1114,7 @@ bool wxFont::GetUnderlined() const
 
 wxString wxFont::GetFaceName() const
 {
-    wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
+    wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
 
     return M_FONTDATA->GetFaceName();
 } // end of wxFont::GetFaceName
@@ -1069,11 +1126,10 @@ wxFontEncoding wxFont::GetEncoding() const
     return M_FONTDATA->GetEncoding();
 } // end of wxFont::GetEncoding
 
-wxNativeFontInfo* wxFont::GetNativeFontInfo() const
+const wxNativeFontInfo* wxFont::GetNativeFontInfo() const
 {
-    if (M_FONTDATA->HasNativeFontInfo())
-        return new wxNativeFontInfo(M_FONTDATA->GetNativeFontInfo());
-    return 0;
+    return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo())
+                                           : NULL;
 } // end of wxFont::GetNativeFontInfo
 
 //
@@ -1098,5 +1154,4 @@ void wxFont::SetPS(
     M_FONTDATA->SetPS(hPS);
 
     RealizeResource();
-} // end of wxFont::SetUnderlined
-
+} // end of wxFont::SetPS