5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Runtime.InteropServices;
31
namespace Xwt.GtkBackend
33
public static class PangoUtil
35
internal const string LIBGTK = "libgtk-win32-2.0-0.dll";
36
internal const string LIBATK = "libatk-1.0-0.dll";
37
internal const string LIBGLIB = "libglib-2.0-0.dll";
38
internal const string LIBGDK = "libgdk-win32-2.0-0.dll";
39
internal const string LIBGOBJECT = "libgobject-2.0-0.dll";
40
internal const string LIBPANGO = "libpango-1.0-0.dll";
41
internal const string LIBPANGOCAIRO = "libpangocairo-1.0-0.dll";
44
/// This doesn't leak Pango layouts, unlike some other ways to create them in GTK# <= 2.12.11
46
public static Pango.Layout CreateLayout (Widget widget)
48
var ptr = gtk_widget_create_pango_layout (widget.Handle, IntPtr.Zero);
49
return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
52
public static Pango.Layout CreateLayout (Widget widget, string text)
54
IntPtr textPtr = text == null? IntPtr.Zero : GLib.Marshaller.StringToPtrGStrdup (text);
56
var ptr = gtk_widget_create_pango_layout (widget.Handle, textPtr);
58
if (textPtr != IntPtr.Zero)
59
GLib.Marshaller.Free (textPtr);
61
return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
64
public static Pango.Layout CreateLayout (PrintContext context)
66
var ptr = gtk_print_context_create_pango_layout (context.Handle);
67
return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
70
[DllImport (LIBGTK, CallingConvention=CallingConvention.Cdecl)]
71
static extern IntPtr gtk_widget_create_pango_layout (IntPtr widget, IntPtr text);
73
[DllImport (LIBGTK, CallingConvention=CallingConvention.Cdecl)]
74
static extern IntPtr gtk_print_context_create_pango_layout (IntPtr context);
78
/// This creates a Pango list and applies attributes to it with *much* less overhead than the GTK# version.
80
class FastPangoAttrList : IDisposable
84
public FastPangoAttrList ()
86
list = pango_attr_list_new ();
89
public void AddStyleAttribute (Pango.Style style, uint start, uint end)
91
Add (pango_attr_style_new (style), start, end);
94
public void AddWeightAttribute (Pango.Weight weight, uint start, uint end)
96
Add (pango_attr_weight_new (weight), start, end);
99
public void AddForegroundAttribute (Gdk.Color color, uint start, uint end)
101
Add (pango_attr_foreground_new (color.Red, color.Green, color.Blue), start, end);
104
public void AddBackgroundAttribute (Gdk.Color color, uint start, uint end)
106
Add (pango_attr_background_new (color.Red, color.Green, color.Blue), start, end);
109
public void AddUnderlineAttribute (Pango.Underline underline, uint start, uint end)
111
Add (pango_attr_underline_new (underline), start, end);
114
void Add (IntPtr attribute, uint start, uint end)
117
PangoAttribute *attPtr = (PangoAttribute *) attribute;
118
attPtr->start_index = start;
119
attPtr->end_index = end;
121
pango_attr_list_insert (list, attribute);
124
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
125
static extern IntPtr pango_attr_style_new (Pango.Style style);
127
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
128
static extern IntPtr pango_attr_stretch_new (Pango.Stretch stretch);
130
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
131
static extern IntPtr pango_attr_weight_new (Pango.Weight weight);
133
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
134
static extern IntPtr pango_attr_foreground_new (ushort red, ushort green, ushort blue);
136
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
137
static extern IntPtr pango_attr_background_new (ushort red, ushort green, ushort blue);
139
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
140
static extern IntPtr pango_attr_underline_new (Pango.Underline underline);
142
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
143
static extern IntPtr pango_attr_list_new ();
145
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
146
static extern void pango_attr_list_unref (IntPtr list);
148
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
149
static extern void pango_attr_list_insert (IntPtr list, IntPtr attr);
151
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
152
static extern void pango_layout_set_attributes (IntPtr layout, IntPtr attrList);
154
[DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
155
static extern void pango_attr_list_splice (IntPtr attr_list, IntPtr other, Int32 pos, Int32 len);
157
public void Splice (Pango.AttrList attrs, int pos, int len)
159
pango_attr_list_splice (list, attrs.Handle, pos, len);
162
public void AssignTo (Pango.Layout layout)
164
pango_layout_set_attributes (layout.Handle, list);
167
[StructLayout (LayoutKind.Sequential)]
168
struct PangoAttribute
171
public uint start_index;
172
public uint end_index;
175
public void Dispose ()
177
if (list != IntPtr.Zero) {
178
GC.SuppressFinalize (this);
183
//NOTE: the list destroys all its attributes when the ref count reaches zero
186
pango_attr_list_unref (list);
190
~FastPangoAttrList ()
192
GLib.Idle.Add (delegate {
b'\\ No newline at end of file'