]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/textmeasure.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/textmeasure.cpp
3 // Purpose: wxTextMeasure implementation for wxGTK
4 // Author: Manuel Martin
7 // Copyright: (c) 1997-2012 wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
23 #include "wx/window.h"
27 #include "wx/private/textmeasure.h"
29 #include "wx/fontutil.h"
30 #include "wx/gtk/private.h"
31 #include "wx/gtk/dc.h"
34 #include "wx/gtk/dcclient.h"
37 // ============================================================================
38 // wxTextMeasure implementation
39 // ============================================================================
41 void wxTextMeasure::Init()
51 wxClassInfo
* const ci
= m_dc
->GetImpl()->GetClassInfo();
53 // Currently the code here only works with wxWindowDCImpl and only in
54 // wxGTK2 as wxGTK3 uses Cairo and not Pango for all its DCs.
55 if ( ci
->IsKindOf(wxCLASSINFO(wxWindowDCImpl
)))
63 // Get Gtk needed elements, if we have not them yet.
64 void wxTextMeasure::BeginMeasuring()
69 m_wdc
= wxDynamicCast(m_dc
->GetImpl(), wxWindowDCImpl
);
72 m_context
= m_wdc
->m_context
;
73 m_layout
= m_wdc
->m_layout
;
79 m_context
= gtk_widget_get_pango_context( m_win
->GetHandle() );
81 m_layout
= pango_layout_new(m_context
);
84 // set the font to use
87 pango_layout_set_font_description(m_layout
,
88 GetFont().GetNativeFontInfo()->description
);
92 void wxTextMeasure::EndMeasuring()
100 // Reset dc own font description
101 pango_layout_set_font_description( m_wdc
->m_layout
, m_wdc
->m_fontdesc
);
106 g_object_unref (m_layout
);
110 // Notice we don't check here the font. It is supposed to be OK before the call.
111 void wxTextMeasure::DoGetTextExtent(const wxString
& string
,
115 wxCoord
*externalLeading
)
128 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(string
, GetFont());
129 if ( !dataUTF8
&& !string
.empty() )
131 // hardly ideal, but what else can we do if conversion failed?
132 wxLogLastError(wxT("GetTextExtent"));
135 pango_layout_set_text(m_layout
, dataUTF8
, -1);
140 pango_layout_get_pixel_size(m_layout
, width
, height
);
144 // the logical rect bounds the ink rect
146 pango_layout_get_extents(m_layout
, NULL
, &rect
);
147 *width
= PANGO_PIXELS(rect
.width
);
148 *height
= PANGO_PIXELS(rect
.height
);
153 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
154 int baseline
= pango_layout_iter_get_baseline(iter
);
155 pango_layout_iter_free(iter
);
156 *descent
= *height
- PANGO_PIXELS(baseline
);
161 // No support for MSW-like "external leading" in Pango.
162 *externalLeading
= 0;
166 bool wxTextMeasure::DoGetPartialTextExtents(const wxString
& text
,
171 return wxTextMeasureBase::DoGetPartialTextExtents(text
, widths
, scaleX
);
174 const wxCharBuffer dataUTF8
= wxGTK_CONV_FONT(text
, GetFont());
177 // hardly ideal, but what else can we do if conversion failed?
178 wxLogLastError(wxT("GetPartialTextExtents"));
182 pango_layout_set_text(m_layout
, dataUTF8
, -1);
184 // Calculate the position of each character based on the widths of
185 // the previous characters
187 // Code borrowed from Scintilla's PlatGTK
188 PangoLayoutIter
*iter
= pango_layout_get_iter(m_layout
);
190 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
192 while (pango_layout_iter_next_cluster(iter
))
194 pango_layout_iter_get_cluster_extents(iter
, NULL
, &pos
);
195 int position
= PANGO_PIXELS(pos
.x
);
196 widths
[i
++] = position
;
199 const size_t len
= text
.length();
201 widths
[i
++] = PANGO_PIXELS(pos
.x
+ pos
.width
);
202 pango_layout_iter_free(iter
);