#include <strings.h>
-#include <gdk/gdk.h>
+#include "wx/gtk/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
// do we have the native font info?
bool HasNativeFont() const
{
+#ifdef __WXGTK20__
+ // we always have a Pango font description
+ return TRUE;
+#else // GTK 1.x
+ // only use m_nativeFontInfo if it had been initialized
return !m_nativeFontInfo.IsDefault();
+#endif // GTK 2.0/1.x
}
// setters: all of them also take care to modify m_nativeFontInfo if we
void SetFaceName(const wxString& facename);
void SetEncoding(wxFontEncoding encoding);
+ // 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__) && !defined(__WXGTK20__)
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:
+#ifdef __WXGTK20__
+ void ClearGdkFonts() { }
+#else // GTK 1.x
+ // clear m_scaled_xfonts
+ void ClearGdkFonts();
+
// the map of font sizes to "GdkFont *"
wxScaledFontList m_scaled_xfonts;
+#endif // GTK 2.0/1.x
// the broken down font parameters
int m_pointSize;
m_weight;
bool m_underlined;
wxString m_faceName;
- wxFontEncoding m_encoding;
+ wxFontEncoding m_encoding; // Unused under GTK 2.0
- // 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;
// 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;
+
+#ifdef __WXGTK20__
+ // Create native font info
+ m_nativeFontInfo.description = pango_font_description_new();
+
+ // And set its values
+ switch (m_family)
+ {
+ case wxFONTFAMILY_MODERN:
+ case wxFONTFAMILY_TELETYPE:
+ pango_font_description_set_family( m_nativeFontInfo.description, "monospace" );
+ break;
+ case wxFONTFAMILY_SWISS:
+ pango_font_description_set_family( m_nativeFontInfo.description, "serif" );
+ break;
+ default:
+ pango_font_description_set_family( m_nativeFontInfo.description, "sans" );
+ break;
+ }
+ SetStyle( m_style );
+ SetPointSize( m_pointSize );
+ SetWeight( m_weight );
+#endif // GTK 2.0
}
-wxFontRefData::wxFontRefData( const wxFontRefData& data )
+void wxFontRefData::InitFromNative()
{
- m_pointSize = data.m_pointSize;
- m_family = data.m_family;
- m_style = data.m_style;
- m_weight = data.m_weight;
+#ifdef __WXGTK20__
+ // Get native info
+ PangoFontDescription *desc = m_nativeFontInfo.description;
- m_underlined = data.m_underlined;
+ // init fields
+ m_faceName = wxGTK_CONV_BACK( pango_font_description_get_family( desc ) );
- m_faceName = data.m_faceName;
- m_encoding = data.m_encoding;
+ m_pointSize = pango_font_description_get_size( desc ) / PANGO_SCALE;
- m_nativeFontInfo = data.m_nativeFontInfo;
-}
+ switch (pango_font_description_get_style( desc ))
+ {
+ case PANGO_STYLE_NORMAL:
+ m_style = wxFONTSTYLE_NORMAL;
+ break;
+ case PANGO_STYLE_ITALIC:
+ m_style = wxFONTSTYLE_ITALIC;
+ break;
+ case PANGO_STYLE_OBLIQUE:
+ m_style = wxFONTSTYLE_SLANT;
+ break;
+ }
-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);
-}
+ switch (pango_font_description_get_weight( desc ))
+ {
+ case PANGO_WEIGHT_ULTRALIGHT:
+ m_weight = wxFONTWEIGHT_LIGHT;
+ break;
+ case PANGO_WEIGHT_LIGHT:
+ m_weight = wxFONTWEIGHT_LIGHT;
+ break;
+ case PANGO_WEIGHT_NORMAL:
+ m_weight = wxFONTWEIGHT_NORMAL;
+ break;
+ case PANGO_WEIGHT_BOLD:
+ m_weight = wxFONTWEIGHT_BOLD;
+ break;
+ case PANGO_WEIGHT_ULTRABOLD:
+ m_weight = wxFONTWEIGHT_BOLD;
+ break;
+ case PANGO_WEIGHT_HEAVY:
+ m_weight = wxFONTWEIGHT_BOLD;
+ break;
+ }
-wxFontRefData::wxFontRefData(const wxString& fontname)
-{
- // remember the X font name
- m_nativeFontInfo.SetXFontName(fontname);
+ if (m_faceName == wxT("monospace"))
+ {
+ m_family = wxFONTFAMILY_TELETYPE;
+ }
+ else if (m_faceName == wxT("sans"))
+ {
+ m_family = wxFONTFAMILY_SWISS;
+ }
+ else
+ {
+ m_family = wxFONTFAMILY_UNKNOWN;
+ }
+
+ // Pango description are never underlined (?)
+ m_underlined = FALSE;
+ // Cannot we choose that
+ m_encoding = wxFONTENCODING_SYSTEM;
+#else // GTK 1.x
// 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;
}
}
else // unknown encoding
{
- // may be give a warning here?
+ // may be give a warning here? or use wxFontMapper?
m_encoding = wxFONTENCODING_SYSTEM;
}
+#endif // GTK 2.0/1.x
}
-wxFontRefData::~wxFontRefData()
+wxFontRefData::wxFontRefData( const wxFontRefData& data )
+ : wxObjectRefData()
+{
+ 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;
+}
+
+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)
+{
+ // VZ: FromString() should really work in both cases, doesn't it?
+#ifdef __WXGTK20__
+ m_nativeFontInfo.FromString( fontname );
+#else // GTK 1.x
+ m_nativeFontInfo.SetXFontName(fontname);
+#endif // GTK 2.0/1.x
+
+ InitFromNative();
+}
+
+#ifndef __WXGTK20__
+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();
+}
+#endif // GTK 1.x
+
+wxFontRefData::~wxFontRefData()
+{
+ ClearGdkFonts();
}
// ----------------------------------------------------------------------------
{
m_pointSize = pointSize;
+#ifdef __WXGTK20__
+ // Get native info
+ PangoFontDescription *desc = m_nativeFontInfo.description;
+
+ pango_font_description_set_size( desc, m_pointSize * PANGO_SCALE );
+#else
if ( HasNativeFont() )
{
wxString size;
m_nativeFontInfo.SetXFontComponent(wxXLFD_POINTSIZE, size);
}
+#endif
}
void wxFontRefData::SetFamily(int family)
{
m_style = style;
+#ifdef __WXGTK20__
+ // Get native info
+ PangoFontDescription *desc = m_nativeFontInfo.description;
+
+ switch ( style )
+ {
+ case wxFONTSTYLE_ITALIC:
+ pango_font_description_set_style( desc, PANGO_STYLE_ITALIC );
+ break;
+ case wxFONTSTYLE_SLANT:
+ pango_font_description_set_style( desc, PANGO_STYLE_OBLIQUE );
+ break;
+ default:
+ wxFAIL_MSG( _T("unknown font style") );
+ // fall through
+ case wxFONTSTYLE_NORMAL:
+ pango_font_description_set_style( desc, PANGO_STYLE_NORMAL );
+ break;
+ }
+#else
if ( HasNativeFont() )
{
wxString slant;
m_nativeFontInfo.SetXFontComponent(wxXLFD_SLANT, slant);
}
+#endif
}
void wxFontRefData::SetWeight(int weight)
{
m_weight = weight;
+#ifndef __WXGTK20__
if ( HasNativeFont() )
{
wxString boldness;
m_nativeFontInfo.SetXFontComponent(wxXLFD_WEIGHT, boldness);
}
+#endif
}
void wxFontRefData::SetUnderlined(bool underlined)
{
m_faceName = facename;
+#ifndef __WXGTK20__
if ( HasNativeFont() )
{
m_nativeFontInfo.SetXFontComponent(wxXLFD_FAMILY, facename);
}
+#endif
}
void wxFontRefData::SetEncoding(wxFontEncoding encoding)
{
m_encoding = encoding;
+#ifndef __WXGTK20__
if ( HasNativeFont() )
{
wxNativeEncodingInfo info;
m_nativeFontInfo.SetXFontComponent(wxXLFD_ENCODING, info.xencoding);
}
}
+#endif
+}
+
+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();
}
// ============================================================================
{
Init();
+#ifdef __WXGTK20__
+ Create( info.GetPointSize(),
+ info.GetFamily(),
+ info.GetStyle(),
+ info.GetWeight(),
+ info.GetUnderlined(),
+ info.GetFaceName(),
+ info.GetEncoding() );
+#else
Create(info.GetXFontName());
+#endif
}
bool wxFont::Create( int pointSize,
{
wxCHECK_MSG( Ok(), (wxNativeFontInfo *)NULL, wxT("invalid font") );
+#ifndef __WXGTK20__ // ???
if ( M_FONTDATA->m_nativeFontInfo.GetXFontName().empty() )
GetInternalFont();
+#endif
return new wxNativeFontInfo(M_FONTDATA->m_nativeFontInfo);
}
{
wxCHECK_MSG( Ok(), FALSE, wxT("invalid font") );
+#ifndef __WXGTK20__
if ( M_FONTDATA->HasNativeFont() )
{
// the monospace fonts are supposed to have "M" in the spacing field
return spacing.Upper() == _T('M');
}
+#endif
return wxFontBase::IsFixedWidth();
}
{
Unshare();
- M_FONTDATA->m_nativeFontInfo = info;
+ M_FONTDATA->SetNativeFontInfo(info);
}
// ----------------------------------------------------------------------------
GtkStyle *def = gtk_rc_get_style( widget );
if (def)
{
- g_systemDefaultGuiFont = gdk_font_ref( def->font );
+ g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
}
else
{
def = gtk_widget_get_default_style();
if (def)
- g_systemDefaultGuiFont = gdk_font_ref( def->font );
+ g_systemDefaultGuiFont = gdk_font_ref( GET_STYLE_FONT(def) );
}
gtk_widget_destroy( widget );
}
wxCHECK_MSG( Ok(), font, wxT("invalid font") )
- long int_scale = long(scale * 100.0 + 0.5); /* key for fontlist */
+#ifdef __WXGTK20__
+ if (*this == wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT))
+ {
+ font = GtkGetDefaultGuiFont();
+ }
+ else
+ {
+ PangoFontDescription *
+ font_description = GetNativeFontInfo()->description;
+
+ font = gdk_font_from_description( font_description );
+ }
+#else // GTK 1.x
+ 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 )
{
- M_FONTDATA->m_scaled_xfonts.Append( int_scale, (wxObject*)font );
+ list[int_scale] = font;
}
}
+#endif // GTK 2.0/1.x
// it's quite useless to make it a wxCHECK because we're going to crash
// anyhow...