X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4855a4773e9a49c6bb70f1adc3da7109e0b6ccf7..982f23fd00be7f4a44ba5595b6d817c76cd2fee0:/src/gtk/dcclient.cpp diff --git a/src/gtk/dcclient.cpp b/src/gtk/dcclient.cpp index 5af43d6f27..c673890cba 100644 --- a/src/gtk/dcclient.cpp +++ b/src/gtk/dcclient.cpp @@ -20,6 +20,7 @@ #include "wx/image.h" #include "wx/module.h" #include "wx/log.h" +#include "wx/fontutil.h" #include "wx/gtk/win_gtk.h" @@ -170,25 +171,56 @@ struct wxGC bool m_used; }; -static wxGC wxGCPool[GC_POOL_SIZE]; +#define GC_POOL_ALLOC_SIZE 100 + +static int wxGCPoolSize = 0; + +static wxGC *wxGCPool = NULL; static void wxInitGCPool() { - memset( wxGCPool, 0, GC_POOL_SIZE*sizeof(wxGC) ); + // This really could wait until the first call to + // wxGetPoolGC, but we will make the first allocation + // now when other initialization is being performed. + + // Set initial pool size. + wxGCPoolSize = GC_POOL_ALLOC_SIZE; + + // Allocate initial pool. + wxGCPool = (wxGC *)malloc(wxGCPoolSize * sizeof(wxGC)); + if (wxGCPool == NULL) + { + // If we cannot malloc, then fail with error + // when debug is enabled. If debug is not enabled, + // the problem will eventually get caught + // in wxGetPoolGC. + wxFAIL_MSG( wxT("Cannot allocate GC pool") ); + return; + } + + // Zero initial pool. + memset(wxGCPool, 0, wxGCPoolSize * sizeof(wxGC)); } static void wxCleanUpGCPool() { - for (int i = 0; i < GC_POOL_SIZE; i++) + for (int i = 0; i < wxGCPoolSize; i++) { if (wxGCPool[i].m_gc) gdk_gc_unref( wxGCPool[i].m_gc ); } + + free(wxGCPool); + wxGCPool = NULL; + wxGCPoolSize = 0; } static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type ) { - for (int i = 0; i < GC_POOL_SIZE; i++) + wxGC *pptr; + + // Look for an available GC. + for (int i = 0; i < wxGCPoolSize; i++) { if (!wxGCPool[i].m_gc) { @@ -204,6 +236,31 @@ static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type ) } } + // We did not find an available GC. + // We need to grow the GC pool. + pptr = (wxGC *)realloc(wxGCPool, + (wxGCPoolSize + GC_POOL_ALLOC_SIZE)*sizeof(wxGC)); + if (pptr != NULL) + { + // Initialize newly allocated pool. + wxGCPool = pptr; + memset(&wxGCPool[wxGCPoolSize], 0, + GC_POOL_ALLOC_SIZE*sizeof(wxGC)); + + // Initialize entry we will return. + wxGCPool[wxGCPoolSize].m_gc = gdk_gc_new( window ); + gdk_gc_set_exposures( wxGCPool[wxGCPoolSize].m_gc, FALSE ); + wxGCPool[wxGCPoolSize].m_type = type; + wxGCPool[wxGCPoolSize].m_used = TRUE; + + // Set new value of pool size. + wxGCPoolSize += GC_POOL_ALLOC_SIZE; + + // Return newly allocated entry. + return wxGCPool[wxGCPoolSize-GC_POOL_ALLOC_SIZE].m_gc; + } + + // The realloc failed. Fall through to error. wxFAIL_MSG( wxT("No GC available") ); return (GdkGC*) NULL; @@ -211,7 +268,7 @@ static GdkGC* wxGetPoolGC( GdkWindow *window, wxPoolGCType type ) static void wxFreePoolGC( GdkGC *gc ) { - for (int i = 0; i < GC_POOL_SIZE; i++) + for (int i = 0; i < wxGCPoolSize; i++) { if (wxGCPool[i].m_gc == gc) { @@ -274,6 +331,7 @@ wxWindowDC::wxWindowDC( wxWindow *window ) #ifdef __WXGTK20__ m_context = gtk_widget_get_pango_context( widget ); + m_fontdesc = widget->style->font_desc; #endif @@ -1369,8 +1427,8 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y ) PangoLayout *layout = pango_layout_new(m_context); pango_layout_set_font_description(layout, m_fontdesc); { - const wxWX2MBbuf data = text.mb_str(wxConvUTF8); - pango_layout_set_text(layout, data, strlen(data)); + const wxCharBuffer data = wxConvUTF8.cWC2MB( text ); + pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data )); } PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data; PangoRectangle rect; @@ -1389,7 +1447,6 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y ) gdk_gc_set_foreground( m_textGC, m_textForegroundColour.GetColor() ); } gdk_draw_string( m_window, font, m_textGC, x, y + font->ascent, text.mbc_str() ); -#endif // GTK+ 2.0/1.x /* CMB 17/7/98: simple underline: ignores scaling and underlying X font's XA_UNDERLINE_POSITION and XA_UNDERLINE_THICKNESS @@ -1400,6 +1457,7 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y ) if (font->descent > 0) ul_y++; gdk_draw_line( m_window, m_textGC, x, ul_y, x + width, ul_y); } +#endif // GTK+ 2.0/1.x #if defined(__WXGTK20__) && wxUSE_WCHAR_T g_object_unref( G_OBJECT( layout ) ); @@ -1499,7 +1557,7 @@ void wxWindowDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y, // don't use DrawPoint() because it uses the current pen // colour, and we don't need it here gdk_draw_point( m_window, m_textGC, - XLOG2DEV(x + dstX), YLOG2DEV(y + dstY) ); + XLOG2DEV(x) + dstX, YLOG2DEV(y) + dstY ); } } } @@ -1530,12 +1588,44 @@ void wxWindowDC::DoGetTextExtent(const wxString &string, { wxFont fontToUse = m_font; if (theFont) fontToUse = *theFont; - + if (string.IsEmpty()) + { + if (width) (*width) = 0; + if (height) (*height) = 0; + return; + } + +#ifdef __WXGTK20__ + PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description; + PangoLayout *layout = pango_layout_new(m_context); + pango_layout_set_font_description(layout, desc); + { + const wxCharBuffer data = wxConvUTF8.cWC2MB( string ); + pango_layout_set_text(layout, (const char*) data, strlen( (const char*) data )); + } + PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data; + + PangoRectangle rect; + pango_layout_line_get_extents(line, NULL, &rect); + + + if (width) (*width) = (wxCoord) rect.width; + if (height) (*height) = (wxCoord) rect.height; + if (descent) + { + // Do something about metrics here + (*descent) = 0; + } + if (externalLeading) (*externalLeading) = 0; // ?? + + g_object_unref( G_OBJECT( layout ) ); +#else GdkFont *font = fontToUse.GetInternalFont( m_scaleY ); if (width) (*width) = wxCoord(gdk_string_width( font, string.mbc_str() ) / m_scaleX); if (height) (*height) = wxCoord((font->ascent + font->descent) / m_scaleY); if (descent) (*descent) = wxCoord(font->descent / m_scaleY); if (externalLeading) (*externalLeading) = 0; // ?? +#endif } wxCoord wxWindowDC::GetCharWidth() const @@ -1600,7 +1690,7 @@ void wxWindowDC::SetFont( const wxFont &font ) m_font = font; #ifdef __WXGTK20__ - // fix fontdesc? + m_fontdesc = m_font.GetNativeFontInfo()->description; #endif }