/////////////////////////////////////////////////////////////////////////////
-// Name: gtk/font.cpp
+// Name: src/gtk1/font.cpp
// Purpose:
// Author: Robert Roebling
// Id: $Id$
-// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
+// Copyright: (c) 1998 Robert Roebling and Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// headers
// ----------------------------------------------------------------------------
-#ifdef __GNUG__
- #pragma implementation "font.h"
-#endif
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
#include "wx/font.h"
+
+#ifndef WX_PRECOMP
+ #include "wx/log.h"
+ #include "wx/settings.h"
+ #include "wx/cmndata.h"
+ #include "wx/gdicmn.h"
+#endif
+
#include "wx/fontutil.h"
-#include "wx/cmndata.h"
#include "wx/utils.h"
-#include "wx/log.h"
-#include "wx/gdicmn.h"
#include "wx/tokenzr.h"
-#include "wx/settings.h"
#include <strings.h>
-#include <gdk/gdk.h>
+#include "wx/gtk1/private.h"
#include <gdk/gdkprivate.h>
-#include <gtk/gtk.h>
// ----------------------------------------------------------------------------
// constants
static const int wxDEFAULT_FONT_SIZE = 12;
// ----------------------------------------------------------------------------
-// wxScaledFontList
+// wxScaledFontList: maps the font sizes to the GDK fonts for the given font
// ----------------------------------------------------------------------------
-// TODO: replace this with a type safe list or hash!!
-class wxScaledFontList : public wxList
-{
-public:
- wxScaledFontList() : wxList(wxKEY_INTEGER) { }
-};
+WX_DECLARE_HASH_MAP(int, GdkFont *, wxIntegerHash, wxIntegerEqual,
+ wxScaledFontList);
// ----------------------------------------------------------------------------
// wxFontRefData
// ----------------------------------------------------------------------------
-class wxFontRefData : public wxObjectRefData
+class wxFontRefData : public wxGDIRefData
{
public:
// from broken down font parameters, also default ctor
int family = wxFONTFAMILY_DEFAULT,
int style = wxFONTSTYLE_NORMAL,
int weight = wxFONTWEIGHT_NORMAL,
- bool underlined = FALSE,
+ bool underlined = false,
const wxString& faceName = wxEmptyString,
wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
// do we have the native font info?
bool HasNativeFont() const
{
+ // only use m_nativeFontInfo if it had been initialized
return !m_nativeFontInfo.IsDefault();
}
void SetStyle(int style);
void SetWeight(int weight);
void SetUnderlined(bool underlined);
- void SetFaceName(const wxString& facename);
+ bool SetFaceName(const wxString& facename);
void SetEncoding(wxFontEncoding encoding);
+ void SetNoAntiAliasing( bool no = true ) { m_noAA = no; }
+ bool GetNoAntiAliasing() const { return m_noAA; }
+
+ // and this one also modifies all the other font data fields
+ void SetNativeFontInfo(const wxNativeFontInfo& info);
+
// debugger helper: shows what the font really is
//
// VZ: I need this as my gdb either shows wildly wrong values or crashes
// when I ask it to "p fontRefData" :-(
-#ifdef __WXDEBUG__
+#if defined(__WXDEBUG__)
void Dump() const
{
wxPrintf(_T("%s-%s-%s-%d-%d\n"),
const wxString& faceName,
wxFontEncoding encoding);
+ // set all fields from (already initialized and valid) m_nativeFontInfo
+ void InitFromNative();
+
private:
+ // clear m_scaled_xfonts if any
+ void ClearGdkFonts();
+
// the map of font sizes to "GdkFont *"
wxScaledFontList m_scaled_xfonts;
- // the broken down font parameters
int m_pointSize;
int m_family,
m_style,
m_weight;
bool m_underlined;
wxString m_faceName;
- wxFontEncoding m_encoding;
+ wxFontEncoding m_encoding; // Unused under GTK 2.0
+ bool m_noAA; // No anti-aliasing
- // the native font info, basicly an XFLD
+ // The native font info, basicly an XFLD under GTK 1.2 and
+ // the pango font description under GTK 2.0.
wxNativeFontInfo m_nativeFontInfo;
friend class wxFont;
};
-// ============================================================================
-// wxFontRefData implementation
-// ============================================================================
+#define M_FONTDATA ((wxFontRefData*)m_refData)
// ----------------------------------------------------------------------------
-// wxFontRefData creation
+// wxFontRefData
// ----------------------------------------------------------------------------
void wxFontRefData::Init(int pointSize,
// and here, do we really want to forbid creation of the font of the size
// 90 (the value of wxDEFAULT)??
- m_pointSize = pointSize == wxDEFAULT ||
- pointSize == -1 ? wxDEFAULT_FONT_SIZE : pointSize;
+ m_pointSize = pointSize == wxDEFAULT || pointSize == -1
+ ? wxDEFAULT_FONT_SIZE
+ : pointSize;
m_underlined = underlined;
m_encoding = encoding;
-}
-
-wxFontRefData::wxFontRefData( const wxFontRefData& data )
-{
- m_pointSize = data.m_pointSize;
- m_family = data.m_family;
- m_style = data.m_style;
- m_weight = data.m_weight;
-
- m_underlined = data.m_underlined;
-
- m_faceName = data.m_faceName;
- m_encoding = data.m_encoding;
- m_nativeFontInfo = data.m_nativeFontInfo;
+ m_noAA = false;
}
-wxFontRefData::wxFontRefData(int size, int family, int style,
- int weight, bool underlined,
- const wxString& faceName,
- wxFontEncoding encoding)
+void wxFontRefData::InitFromNative()
{
- Init(size, family, style, weight, underlined, faceName, encoding);
-}
-
-wxFontRefData::wxFontRefData(const wxString& fontname)
-{
- // remember the X font name
- m_nativeFontInfo.SetXFontName(fontname);
+ m_noAA = false;
// get the font parameters from the XLFD
// -------------------------------------
{
// the test below catches all of BOLD, EXTRABOLD, DEMIBOLD, ULTRABOLD
// and BLACK
- if ( ((w[0u] == _T('B') && (!strcmp(w.c_str() + 1, _T("OLD")) ||
- !strcmp(w.c_str() + 1, _T("LACK"))))) ||
- strstr(w.c_str() + 1, _T("BOLD")) )
+ if ( ((w[0u] == _T('B') && (!wxStrcmp(w.c_str() + 1, wxT("OLD")) ||
+ !wxStrcmp(w.c_str() + 1, wxT("LACK"))))) ||
+ wxStrstr(w.c_str() + 1, _T("BOLD")) )
{
m_weight = wxFONTWEIGHT_BOLD;
}
}
}
- switch ( wxToupper(*m_nativeFontInfo.
- GetXFontComponent(wxXLFD_SLANT).c_str()) )
+ switch ( wxToupper(m_nativeFontInfo.
+ GetXFontComponent(wxXLFD_SLANT)[0u]).GetValue() )
{
case _T('I'): // italique
m_style = wxFONTSTYLE_ITALIC;
}
// X fonts are never underlined...
- m_underlined = FALSE;
+ m_underlined = false;
// deal with font encoding
wxString
}
else // unknown encoding
{
- // may be give a warning here?
+ // may be give a warning here? or use wxFontMapper?
m_encoding = wxFONTENCODING_SYSTEM;
}
}
-wxFontRefData::~wxFontRefData()
+wxFontRefData::wxFontRefData( const wxFontRefData& data )
+ : wxGDIRefData()
+{
+ m_pointSize = data.m_pointSize;
+ m_family = data.m_family;
+ m_style = data.m_style;
+ m_weight = data.m_weight;
+
+ m_underlined = data.m_underlined;
+
+ m_faceName = data.m_faceName;
+ m_encoding = data.m_encoding;
+
+ m_noAA = data.m_noAA;
+
+ // Forces a copy of the internal data. wxNativeFontInfo should probably
+ // have a copy ctor and assignment operator to fix this properly but that
+ // would break binary compatibility...
+ m_nativeFontInfo.FromString(data.m_nativeFontInfo.ToString());
+}
+
+wxFontRefData::wxFontRefData(int size, int family, int style,
+ int weight, bool underlined,
+ const wxString& faceName,
+ wxFontEncoding encoding)
+{
+ Init(size, family, style, weight, underlined, faceName, encoding);
+}
+
+wxFontRefData::wxFontRefData(const wxString& fontname)
+{
+ // FromString() should really work in GTK1 too, doesn't it?
+ m_nativeFontInfo.SetXFontName(fontname);
+
+ InitFromNative();
+}
+
+void wxFontRefData::ClearGdkFonts()
{
- wxNode *node = m_scaled_xfonts.First();
- while (node)
+ for ( wxScaledFontList::iterator i = m_scaled_xfonts.begin();
+ i != m_scaled_xfonts.end();
+ ++i )
{
- GdkFont *font = (GdkFont*)node->Data();
- wxNode *next = node->Next();
+ GdkFont *font = i->second;
gdk_font_unref( font );
- node = next;
}
+
+ m_scaled_xfonts.clear();
+}
+
+wxFontRefData::~wxFontRefData()
+{
+ ClearGdkFonts();
}
// ----------------------------------------------------------------------------
// the XLFD doesn't have "underlined" field anyhow
}
-void wxFontRefData::SetFaceName(const wxString& facename)
+bool wxFontRefData::SetFaceName(const wxString& facename)
{
m_faceName = facename;
{
m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
}
+
+ return true;
}
void wxFontRefData::SetEncoding(wxFontEncoding encoding)
}
}
-// ============================================================================
-// wxFont implementation
-// ============================================================================
+void wxFontRefData::SetNativeFontInfo(const wxNativeFontInfo& info)
+{
+ // previously cached fonts shouldn't be used
+ ClearGdkFonts();
+
+ m_nativeFontInfo = info;
+
+ // set all the other font parameters from the native font info
+ InitFromNative();
+}
// ----------------------------------------------------------------------------
// wxFont creation
IMPLEMENT_DYNAMIC_CLASS(wxFont, wxGDIObject)
-void wxFont::Init()
-{
-}
-
wxFont::wxFont(const wxNativeFontInfo& info)
{
- Init();
-
- Create(info.GetXFontName());
+ (void) Create(info.GetXFontName());
}
bool wxFont::Create( int pointSize,
const wxString& face,
wxFontEncoding encoding)
{
+ UnRef();
+
m_refData = new wxFontRefData(pointSize, family, style, weight,
underlined, face, encoding);
- return TRUE;
+ return true;
}
bool wxFont::Create(const wxString& fontname)
{
*this = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
- return TRUE;
+ return true;
}
m_refData = new wxFontRefData(fontname);
- return TRUE;
+ return true;
}
void wxFont::Unshare()
{
}
+wxGDIRefData *wxFont::CreateGDIRefData() const
+{
+ return new wxFontRefData;
+}
+
+wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
+{
+ return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data));
+}
+
// ----------------------------------------------------------------------------
// accessors
// ----------------------------------------------------------------------------
-// all accessors are just forwarded to wxFontRefData which has everything we
-// need
-
int wxFont::GetPointSize() const
{
wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
wxString wxFont::GetFaceName() const
{
- wxCHECK_MSG( Ok(), wxT(""), wxT("invalid font") );
+ wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") );
return M_FONTDATA->m_faceName;
}
bool wxFont::GetUnderlined() const
{
- wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
+ wxCHECK_MSG( Ok(), false, wxT("invalid font") );
return M_FONTDATA->m_underlined;
}
{
wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
+ // m_encoding is unused in wxGTK2, return encoding that the user set.
return M_FONTDATA->m_encoding;
}
-wxNativeFontInfo *wxFont::GetNativeFontInfo() const
+bool wxFont::GetNoAntiAliasing() const
+{
+ wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
+
+ return M_FONTDATA->m_noAA;
+}
+
+const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
{
wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
- if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
+ if ( !M_FONTDATA->HasNativeFont() )
+ {
+ // NB: this call has important side-effect: it not only finds
+ // GdkFont representation, it also initializes m_nativeFontInfo
+ // by calling its SetXFontName method
GetInternalFont();
+ }
- return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
+ return &(M_FONTDATA->m_nativeFontInfo);
}
bool wxFont::IsFixedWidth() const
{
- wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
+ wxCHECK_MSG( Ok(), false, wxT("invalid font") );
if ( M_FONTDATA->HasNativeFont() )
{
M_FONTDATA->SetWeight(weight);
}
-void wxFont::SetFaceName(const wxString& faceName)
+bool wxFont::SetFaceName(const wxString& faceName)
{
Unshare();
- M_FONTDATA->SetFaceName(faceName);
+ return M_FONTDATA->SetFaceName(faceName) &&
+ wxFontBase::SetFaceName(faceName);
}
void wxFont::SetUnderlined(bool underlined)
M_FONTDATA->SetEncoding(encoding);
}
-void wxFont::SetNativeFontInfo(const wxNativeFontInfo& info)
+void wxFont::DoSetNativeFontInfo( const wxNativeFontInfo& info )
{
Unshare();
- M_FONTDATA->m_nativeFontInfo = info;
+ M_FONTDATA->SetNativeFontInfo( info );
+}
+
+void wxFont::SetNoAntiAliasing( bool no )
+{
+ Unshare();
+
+ M_FONTDATA->SetNoAntiAliasing( no );
}
// ----------------------------------------------------------------------------
{
GdkFont *font = (GdkFont *) NULL;
- wxCHECK_MSG( Ok(), font, wxT("invalid font") )
+ wxCHECK_MSG( Ok(), font, wxT("invalid font") );
- long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
+ long int_scale = long(scale * 100.0 + 0.5); // key for fontlist
int point_scale = (int)((M_FONTDATA->m_pointSize * 10 * int_scale) / 100);
- wxNode *node = M_FONTDATA->m_scaled_xfonts.Find(int_scale);
- if (node)
+ wxScaledFontList& list = M_FONTDATA->m_scaled_xfonts;
+ wxScaledFontList::iterator i = list.find(int_scale);
+ if ( i != list.end() )
{
- font = (GdkFont*)node->Data();
+ font = i->second;
}
else // we don't have this font in this size yet
{
if ( !font )
{
// do we have the XLFD?
- if ( M_FONTDATA->HasNativeFont() )
+ if ( int_scale == 100 && M_FONTDATA->HasNativeFont() )
{
font = wxLoadFont(M_FONTDATA->m_nativeFontInfo.GetXFontName());
}
M_FONTDATA->m_faceName,
M_FONTDATA->m_encoding,
&xfontname);
- if ( font )
- {
+ // NB: wxFont::GetNativeFontInfo relies on this
+ // side-effect of GetInternalFont
+ if ( int_scale == 100 )
M_FONTDATA->m_nativeFontInfo.SetXFontName(xfontname);
- }
}
}
if ( font )
{
- M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
+ list[int_scale] = font;
}
}
return font;
}
-