~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/Util.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 03:53:12 UTC
  • Revision ID: ootz0rz@gmail.com-20090623035312-it8tb5wkha6nf31p
Added Do.Interface.Windows, and a bunch of cleanup with old MonoDevelop files elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Util.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.Collections.Generic;
 
23
 
 
24
using Gdk;
 
25
using Cairo;
 
26
 
 
27
using Do.Universe;
 
28
 
 
29
namespace Do.Interface
 
30
{
 
31
 
 
32
        public delegate void DoEventKeyDelegate (Gdk.EventKey key);
 
33
        public delegate void NullEventHandler ();
 
34
        public delegate void SearchStartedEventHandler (bool upstream_search);
 
35
        public delegate void SearchFinishedEventHandler (object controller, SearchFinishState state);
 
36
        
 
37
        public static class Util
 
38
        {
 
39
                public static string FormatCommonSubstrings (string main, string other, string format)
 
40
                {
 
41
                        int pos, len, match_pos, last_main_cut;
 
42
                        string lower_main, result;
 
43
                        string skipped, matched, remainder;
 
44
                        bool matchedTermination;
 
45
 
 
46
                        result = "";
 
47
                        match_pos = last_main_cut = 0;
 
48
                        lower_main = main.ToLower ();
 
49
                        other = other.ToLower ();
 
50
 
 
51
                        for (pos = 0; pos < other.Length; ++pos) {
 
52
                                matchedTermination = false;
 
53
                                for (len = 1; len <= other.Length - pos; ++len) {
 
54
                                        int tmp_match_pos = lower_main.IndexOf (other.Substring (pos, len));
 
55
                                        if (tmp_match_pos < 0) {
 
56
                                                len--;
 
57
                                                matchedTermination = false;
 
58
                                                break;
 
59
                                        } else {
 
60
                                                matchedTermination = true;
 
61
                                                match_pos = tmp_match_pos;
 
62
                                        }
 
63
                                }
 
64
                                if (matchedTermination) {
 
65
                                        len--;
 
66
                                }
 
67
                                if (0 < len) {
 
68
                                         //Theres a match starting at match_pos with positive length
 
69
                                        skipped = main.Substring (last_main_cut, match_pos - last_main_cut);
 
70
                                        matched = main.Substring (match_pos, len);
 
71
                                        if ( skipped.Length + matched.Length < main.Length) {
 
72
                                                remainder = FormatCommonSubstrings ( main.Substring (match_pos + len), other.Substring (pos + len), format);
 
73
                                        }
 
74
                                        else {
 
75
                                                remainder = "";
 
76
                                        }
 
77
                                        result = string.Format ("{0}{1}{2}", skipped, string.Format(format, matched), remainder);
 
78
                                        break;
 
79
                                }
 
80
                        }
 
81
                        if (result == "") {
 
82
                                // no matches
 
83
                                result = main;
 
84
                        }
 
85
                        return result;
 
86
                }
 
87
                
 
88
                public static class Appearance
 
89
                {
 
90
 
 
91
                        public static string MarkupSafeString (string s)
 
92
                        {
 
93
                                if (s == null) throw new ArgumentNullException ("s");
 
94
                                
 
95
                                return GLib.Markup.EscapeText (s);
 
96
                        }
 
97
                        
 
98
                        public static void SetColormap (Gtk.Widget widget)
 
99
                        {
 
100
                                Gdk.Colormap  colormap;
 
101
                                
 
102
                                colormap = widget.Screen.RgbaColormap;
 
103
                                if (colormap == null) {
 
104
                                        colormap = widget.Screen.RgbColormap;
 
105
                                        Console.Error.WriteLine ("No alpha support.");
 
106
                                }
 
107
                                
 
108
                                widget.Colormap = colormap;
 
109
                                colormap.Dispose ();
 
110
                        }                               
 
111
                        
 
112
                        public static void RGBToHSV (byte r, byte g, byte b, 
 
113
                                                     out double hue, out double sat, out double val)
 
114
                        {
 
115
                                // Ported from Murrine Engine.
 
116
                                double red, green, blue;
 
117
                                double max, min;
 
118
                                double delta;
 
119
                                
 
120
                                red = (double) r;
 
121
                                green = (double) g;
 
122
                                blue = (double) b;
 
123
                                
 
124
                                hue = 0;
 
125
                                
 
126
                                max = Math.Max (red, Math.Max (blue, green));
 
127
                                min = Math.Min (red, Math.Min (blue, green));
 
128
                                delta = max - min;
 
129
                                val = max / 255.0 * 100.0;
 
130
                                
 
131
                                if (Math.Abs (delta) < 0.0001) {
 
132
                                        sat = 0;
 
133
                                } else {
 
134
                                        sat = (delta / max) * 100;
 
135
                                        
 
136
                                        if (red == max)   hue = (green - blue) / delta;
 
137
                                        if (green == max) hue = 2 + (blue - red) / delta;
 
138
                                        if (blue == max)  hue = 4 + (red - green) / delta;
 
139
                                        
 
140
                                        hue *= 60;
 
141
                                        if (hue < 0) hue += 360;
 
142
                                }
 
143
                        }
 
144
                        
 
145
                        public static void HSVToRGB (double hue, double sat, double val,
 
146
                                                     out byte red, out byte green, out byte blue)
 
147
                        {
 
148
                                double h, s, v;
 
149
                                double r = 0, g = 0, b = 0;
 
150
 
 
151
                                h = hue;
 
152
                                s = sat / 100;
 
153
                                v = val / 100;
 
154
 
 
155
                                if (s == 0) {
 
156
                                        r = v;
 
157
                                        g = v;
 
158
                                        b = v;
 
159
                                } else {
 
160
                                        int secNum;
 
161
                                        double fracSec;
 
162
                                        double p, q, t;
 
163
                                        
 
164
                                        secNum = (int) Math.Floor(h / 60);
 
165
                                        fracSec = h/60 - secNum;
 
166
 
 
167
                                        p = v * (1 - s);
 
168
                                        q = v * (1 - s*fracSec);
 
169
                                        t = v * (1 - s*(1 - fracSec));
 
170
                                        
 
171
                                        switch (secNum) {
 
172
                                                case 0:
 
173
                                                        r = v;
 
174
                                                        g = t;
 
175
                                                        b = p;
 
176
                                                        break;
 
177
                                                case 1:
 
178
                                                        r = q;
 
179
                                                        g = v;
 
180
                                                        b = p;
 
181
                                                        break;
 
182
                                                case 2:
 
183
                                                        r = p;
 
184
                                                        g = v;
 
185
                                                        b = t;
 
186
                                                        break;
 
187
                                                case 3:
 
188
                                                        r = p;
 
189
                                                        g = q;
 
190
                                                        b = v;
 
191
                                                        break;
 
192
                                                case 4:
 
193
                                                        r = t;
 
194
                                                        g = p;
 
195
                                                        b = v;
 
196
                                                        break;
 
197
                                                case 5:
 
198
                                                        r = v;
 
199
                                                        g = p;
 
200
                                                        b = q;
 
201
                                                        break;
 
202
                                        }
 
203
                                }
 
204
                                red   = Convert.ToByte(r*255);
 
205
                                green = Convert.ToByte(g*255);
 
206
                                blue  = Convert.ToByte(b*255);
 
207
                        }
 
208
                        
 
209
                        static void GetFrame (Cairo.Context cairo, double x, double y, double width, double height, double radius)
 
210
                        {
 
211
                                if (radius == 0)
 
212
                                {
 
213
                                        cairo.MoveTo (x, y);
 
214
                                        cairo.Rectangle (x, y, width, height);
 
215
                                } else {
 
216
                                        cairo.MoveTo (x+radius, y);
 
217
                                        cairo.Arc (x+width-radius, y+radius, radius, (Math.PI*1.5), (Math.PI*2));
 
218
                                        cairo.Arc (x+width-radius, y+height-radius, radius, 0, (Math.PI*0.5));
 
219
                                        cairo.Arc (x+radius, y+height-radius, radius, (Math.PI*0.5), Math.PI);
 
220
                                        cairo.Arc (x+radius, y+radius, radius, Math.PI, (Math.PI*1.5));
 
221
                                }
 
222
                        }
 
223
 
 
224
                        static void GetShadowPattern (Cairo.Gradient shadow, ShadowParameters shadowParams)
 
225
                        {
 
226
                                double denLog = Math.Log(1.0f/shadowParams.shadowRadius);
 
227
                                
 
228
                                shadow.AddColorStop (0.0, new Cairo.Color (0, 0, 0, shadowParams.shadowAlpha));
 
229
 
 
230
                                for (int i=2; i<=shadowParams.shadowRadius; i++)
 
231
                                {
 
232
                                        double step = i/shadowParams.shadowRadius;
 
233
                                        shadow.AddColorStop (step, new Cairo.Color (0, 0, 0, shadowParams.shadowAlpha*(Math.Log(step)/denLog)));
 
234
                                }
 
235
                        }
 
236
                        
 
237
                        static void FillShadowPattern (Cairo.Context cairo, Cairo.Gradient shadow, ShadowParameters shadowParams)
 
238
                        {
 
239
                                GetShadowPattern (shadow, shadowParams);
 
240
                                cairo.Pattern = shadow;
 
241
                                cairo.Fill ();
 
242
                        }
 
243
 
 
244
                        public static void DrawShadow (Cairo.Context cr, double x, double y, double width, 
 
245
                                                          double height, double radius, ShadowParameters shadowParams)
 
246
                        {
 
247
                                Surface sr = cr.Target.CreateSimilar (cr.Target.Content, (int)width + (int)(2*shadowParams.shadowRadius) + (int)x, 
 
248
                                                                      (int)height + (int)(2*shadowParams.shadowRadius) + (int)y);
 
249
                                Context cairo = new Context (sr);
 
250
                                
 
251
                                radius++;
 
252
                                y++;
 
253
                                height--;
 
254
                                Cairo.Gradient shadow;
 
255
                                /* Top Left */
 
256
                                shadow = new Cairo.RadialGradient (x+radius, y+radius, radius,
 
257
                                                                   x+radius, y+radius, radius+shadowParams.shadowRadius);
 
258
                                cairo.Rectangle (x-shadowParams.shadowRadius, y-shadowParams.shadowRadius,
 
259
                                                 radius+shadowParams.shadowRadius, radius+shadowParams.shadowRadius);
 
260
                                FillShadowPattern (cairo, shadow, shadowParams);
 
261
                                shadow.Destroy ();
 
262
                                
 
263
                                /* Top */
 
264
                                shadow = new Cairo.LinearGradient (0.0, y,
 
265
                                                                   0.0, y-shadowParams.shadowRadius);
 
266
                                cairo.Rectangle (x+radius, y-shadowParams.shadowRadius,
 
267
                                                 width-radius*2, shadowParams.shadowRadius);
 
268
                                FillShadowPattern (cairo, shadow, shadowParams);
 
269
                                shadow.Destroy ();
 
270
                                
 
271
                                /* Top Right */
 
272
                                shadow = new Cairo.RadialGradient (width+x-radius, y+radius, radius,
 
273
                                                                   width+x-radius, y+radius, radius+shadowParams.shadowRadius);
 
274
                                cairo.Rectangle (width+x-radius, y-shadowParams.shadowRadius,
 
275
                                                 radius+shadowParams.shadowRadius, radius+shadowParams.shadowRadius);
 
276
                                FillShadowPattern (cairo, shadow, shadowParams);
 
277
                                shadow.Destroy ();
 
278
                                
 
279
                                /* Right */
 
280
                                shadow = new Cairo.LinearGradient (width+x, 0.0,
 
281
                                                                   width+x+shadowParams.shadowRadius, 0.0);
 
282
                                cairo.Rectangle (width+x, y+radius, shadowParams.shadowRadius, height-radius*2);
 
283
                                FillShadowPattern (cairo, shadow, shadowParams);
 
284
                                shadow.Destroy ();
 
285
                                
 
286
                                /* Bottom Right */
 
287
                                shadow = new Cairo.RadialGradient (width+x-radius, height+y-radius, radius,
 
288
                                                                   width+x-radius, height+y-radius, radius+shadowParams.shadowRadius);
 
289
                                cairo.Rectangle (width+x-radius, height+y-radius,
 
290
                                                radius+shadowParams.shadowRadius, radius+shadowParams.shadowRadius);
 
291
                                FillShadowPattern (cairo, shadow, shadowParams);
 
292
                                shadow.Destroy ();
 
293
                                
 
294
                                /* Bottom */
 
295
                                shadow = new Cairo.LinearGradient (0.0, height+y, 
 
296
                                                                   0.0, height+y+shadowParams.shadowRadius);
 
297
                                cairo.Rectangle (x+radius, height+y,
 
298
                                                 width-radius*2, shadowParams.shadowRadius);
 
299
                                FillShadowPattern (cairo, shadow, shadowParams);
 
300
                                shadow.Destroy ();
 
301
                                
 
302
                                /* Bottom Left */
 
303
                                shadow = new Cairo.RadialGradient (x+radius, height+y-radius, radius, 
 
304
                                                                   x+radius, height+y-radius, radius+shadowParams.shadowRadius);
 
305
                                cairo.Rectangle (x-shadowParams.shadowRadius, height+y-radius,
 
306
                                                 radius+shadowParams.shadowRadius, radius+shadowParams.shadowRadius);
 
307
                                FillShadowPattern (cairo, shadow, shadowParams);
 
308
                                shadow.Destroy ();
 
309
                                
 
310
                                /* Left */
 
311
                                shadow = new Cairo.LinearGradient (x, 0.0, 
 
312
                                                                   x-shadowParams.shadowRadius, 0.0);
 
313
                                cairo.Rectangle (x-shadowParams.shadowRadius, y+radius, 
 
314
                                                 radius+shadowParams.shadowRadius, height-radius*2);
 
315
                                FillShadowPattern (cairo, shadow, shadowParams);
 
316
                                shadow.Destroy ();
 
317
                                
 
318
                                y--;
 
319
                                height++;
 
320
                                /* Clear inner rectangle */
 
321
                                GetFrame (cairo, x, y, width, height, radius);
 
322
                                cairo.Operator = Cairo.Operator.Clear;
 
323
                                cairo.Fill();
 
324
                                
 
325
                                cr.SetSource (sr);
 
326
                                cr.Paint ();
 
327
                                
 
328
                                (cairo as IDisposable).Dispose ();
 
329
                                sr.Destroy ();
 
330
                        }
 
331
                }
 
332
                
 
333
                public class ShadowParameters
 
334
                {
 
335
                        public double shadowAlpha = 0.325;
 
336
                        public double shadowRadius = 15f;
 
337
                        
 
338
                        public ShadowParameters (double shadowAlpha, double shadowRadius)
 
339
                        {
 
340
                                this.shadowAlpha = shadowAlpha;
 
341
                                this.shadowRadius = shadowRadius;
 
342
                        }
 
343
                }
 
344
        }
 
345
}