~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/PangoUtil.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// PangoUtils.cs
 
3
//  
 
4
// Author:
 
5
//       Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Gtk;
 
29
using System.Runtime.InteropServices;
 
30
 
 
31
namespace Xwt.GtkBackend
 
32
{
 
33
        public static class PangoUtil
 
34
        {
 
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";
 
42
/*              
 
43
                /// <summary>
 
44
                /// This doesn't leak Pango layouts, unlike some other ways to create them in GTK# <= 2.12.11
 
45
                /// </summary>
 
46
                public static Pango.Layout CreateLayout (Widget widget)
 
47
                {
 
48
                        var ptr = gtk_widget_create_pango_layout (widget.Handle, IntPtr.Zero);
 
49
                        return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
 
50
                }
 
51
 
 
52
                public static Pango.Layout CreateLayout (Widget widget, string text)
 
53
                {
 
54
                        IntPtr textPtr = text == null? IntPtr.Zero : GLib.Marshaller.StringToPtrGStrdup (text);
 
55
                        
 
56
                        var ptr = gtk_widget_create_pango_layout (widget.Handle, textPtr);
 
57
                        
 
58
                        if (textPtr != IntPtr.Zero)
 
59
                                GLib.Marshaller.Free (textPtr);
 
60
                        
 
61
                        return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
 
62
                }
 
63
                
 
64
                public static Pango.Layout CreateLayout (PrintContext context)
 
65
                {
 
66
                        var ptr = gtk_print_context_create_pango_layout (context.Handle);
 
67
                        return ptr == IntPtr.Zero? null : new Pango.Layout (ptr);
 
68
                }
 
69
                
 
70
                [DllImport (LIBGTK, CallingConvention=CallingConvention.Cdecl)]
 
71
                static extern IntPtr gtk_widget_create_pango_layout (IntPtr widget, IntPtr text);
 
72
                
 
73
                [DllImport (LIBGTK, CallingConvention=CallingConvention.Cdecl)]
 
74
                static extern IntPtr gtk_print_context_create_pango_layout (IntPtr context);
 
75
        }
 
76
        
 
77
        /// <summary>
 
78
        /// This creates a Pango list and applies attributes to it with *much* less overhead than the GTK# version.
 
79
        /// </summary>
 
80
        class FastPangoAttrList : IDisposable
 
81
        {
 
82
                IntPtr list;
 
83
                
 
84
                public FastPangoAttrList ()
 
85
                {
 
86
                        list = pango_attr_list_new ();
 
87
                }
 
88
                
 
89
                public void AddStyleAttribute (Pango.Style style, uint start, uint end)
 
90
                {
 
91
                        Add (pango_attr_style_new (style), start, end);
 
92
                }
 
93
                
 
94
                public void AddWeightAttribute (Pango.Weight weight, uint start, uint end)
 
95
                {
 
96
                        Add (pango_attr_weight_new (weight), start, end);
 
97
                }
 
98
                
 
99
                public void AddForegroundAttribute (Gdk.Color color, uint start, uint end)
 
100
                {
 
101
                        Add (pango_attr_foreground_new (color.Red, color.Green, color.Blue), start, end);
 
102
                }
 
103
                
 
104
                public void AddBackgroundAttribute (Gdk.Color color, uint start, uint end)
 
105
                {
 
106
                        Add (pango_attr_background_new (color.Red, color.Green, color.Blue), start, end);
 
107
                }
 
108
                
 
109
                public void AddUnderlineAttribute (Pango.Underline underline, uint start, uint end)
 
110
                {
 
111
                        Add (pango_attr_underline_new (underline), start, end);
 
112
                }
 
113
                
 
114
                void Add (IntPtr attribute, uint start, uint end)
 
115
                {
 
116
                        unsafe {
 
117
                                PangoAttribute *attPtr = (PangoAttribute *) attribute;
 
118
                                attPtr->start_index = start;
 
119
                                attPtr->end_index = end;
 
120
                        }
 
121
                        pango_attr_list_insert (list, attribute);
 
122
                }
 
123
                
 
124
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
125
                static extern IntPtr pango_attr_style_new (Pango.Style style);
 
126
                
 
127
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
128
                static extern IntPtr pango_attr_stretch_new (Pango.Stretch stretch);
 
129
                
 
130
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
131
                static extern IntPtr pango_attr_weight_new (Pango.Weight weight);
 
132
                
 
133
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
134
                static extern IntPtr pango_attr_foreground_new (ushort red, ushort green, ushort blue);
 
135
                
 
136
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
137
                static extern IntPtr pango_attr_background_new (ushort red, ushort green, ushort blue);
 
138
                
 
139
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
140
                static extern IntPtr pango_attr_underline_new (Pango.Underline underline);
 
141
                
 
142
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
143
                static extern IntPtr pango_attr_list_new ();
 
144
 
 
145
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
146
                static extern void pango_attr_list_unref (IntPtr list);
 
147
                
 
148
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
149
                static extern void pango_attr_list_insert (IntPtr list, IntPtr attr);
 
150
                
 
151
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
152
                static extern void pango_layout_set_attributes (IntPtr layout, IntPtr attrList);
 
153
                
 
154
                [DllImport (PangoUtil.LIBPANGO, CallingConvention=CallingConvention.Cdecl)]
 
155
                static extern void pango_attr_list_splice (IntPtr attr_list, IntPtr other, Int32 pos, Int32 len);
 
156
                
 
157
                public void Splice (Pango.AttrList attrs, int pos, int len)
 
158
                {
 
159
                        pango_attr_list_splice (list, attrs.Handle, pos, len);
 
160
                }
 
161
                
 
162
                public void AssignTo (Pango.Layout layout)
 
163
                {
 
164
                        pango_layout_set_attributes (layout.Handle, list);
 
165
                }
 
166
                
 
167
                [StructLayout (LayoutKind.Sequential)]
 
168
                struct PangoAttribute
 
169
                {
 
170
                        public IntPtr klass;
 
171
                        public uint start_index;
 
172
                        public uint end_index;
 
173
                }
 
174
                
 
175
                public void Dispose ()
 
176
                {
 
177
                        if (list != IntPtr.Zero) {
 
178
                                GC.SuppressFinalize (this);
 
179
                                Destroy ();
 
180
                        }
 
181
                }
 
182
                
 
183
                //NOTE: the list destroys all its attributes when the ref count reaches zero
 
184
                void Destroy ()
 
185
                {
 
186
                        pango_attr_list_unref (list);
 
187
                        list = IntPtr.Zero;
 
188
                }
 
189
                
 
190
                ~FastPangoAttrList ()
 
191
                {
 
192
                        GLib.Idle.Add (delegate {
 
193
                                Destroy ();
 
194
                                return false;
 
195
                        });
 
196
                }*/
 
197
        }
 
198
}
 
 
b'\\ No newline at end of file'