3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
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.
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.
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/>.
22
using System.Collections.Generic;
29
namespace Do.Interface
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);
37
public static class Util
39
public static string FormatCommonSubstrings (string main, string other, string format)
41
int pos, len, match_pos, last_main_cut;
42
string lower_main, result;
43
string skipped, matched, remainder;
44
bool matchedTermination;
47
match_pos = last_main_cut = 0;
48
lower_main = main.ToLower ();
49
other = other.ToLower ();
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) {
57
matchedTermination = false;
60
matchedTermination = true;
61
match_pos = tmp_match_pos;
64
if (matchedTermination) {
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);
77
result = string.Format ("{0}{1}{2}", skipped, string.Format(format, matched), remainder);
88
public static class Appearance
91
public static string MarkupSafeString (string s)
93
if (s == null) throw new ArgumentNullException ("s");
95
return GLib.Markup.EscapeText (s);
98
public static void SetColormap (Gtk.Widget widget)
100
Gdk.Colormap colormap;
102
colormap = widget.Screen.RgbaColormap;
103
if (colormap == null) {
104
colormap = widget.Screen.RgbColormap;
105
Console.Error.WriteLine ("No alpha support.");
108
widget.Colormap = colormap;
112
public static void RGBToHSV (byte r, byte g, byte b,
113
out double hue, out double sat, out double val)
115
// Ported from Murrine Engine.
116
double red, green, blue;
126
max = Math.Max (red, Math.Max (blue, green));
127
min = Math.Min (red, Math.Min (blue, green));
129
val = max / 255.0 * 100.0;
131
if (Math.Abs (delta) < 0.0001) {
134
sat = (delta / max) * 100;
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;
141
if (hue < 0) hue += 360;
145
public static void HSVToRGB (double hue, double sat, double val,
146
out byte red, out byte green, out byte blue)
149
double r = 0, g = 0, b = 0;
164
secNum = (int) Math.Floor(h / 60);
165
fracSec = h/60 - secNum;
168
q = v * (1 - s*fracSec);
169
t = v * (1 - s*(1 - fracSec));
204
red = Convert.ToByte(r*255);
205
green = Convert.ToByte(g*255);
206
blue = Convert.ToByte(b*255);
209
static void GetFrame (Cairo.Context cairo, double x, double y, double width, double height, double radius)
214
cairo.Rectangle (x, y, width, height);
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));
224
static void GetShadowPattern (Cairo.Gradient shadow, ShadowParameters shadowParams)
226
double denLog = Math.Log(1.0f/shadowParams.shadowRadius);
228
shadow.AddColorStop (0.0, new Cairo.Color (0, 0, 0, shadowParams.shadowAlpha));
230
for (int i=2; i<=shadowParams.shadowRadius; i++)
232
double step = i/shadowParams.shadowRadius;
233
shadow.AddColorStop (step, new Cairo.Color (0, 0, 0, shadowParams.shadowAlpha*(Math.Log(step)/denLog)));
237
static void FillShadowPattern (Cairo.Context cairo, Cairo.Gradient shadow, ShadowParameters shadowParams)
239
GetShadowPattern (shadow, shadowParams);
240
cairo.Pattern = shadow;
244
public static void DrawShadow (Cairo.Context cr, double x, double y, double width,
245
double height, double radius, ShadowParameters shadowParams)
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);
254
Cairo.Gradient shadow;
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);
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);
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);
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);
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);
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);
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);
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);
320
/* Clear inner rectangle */
321
GetFrame (cairo, x, y, width, height, radius);
322
cairo.Operator = Cairo.Operator.Clear;
328
(cairo as IDisposable).Dispose ();
333
public class ShadowParameters
335
public double shadowAlpha = 0.325;
336
public double shadowRadius = 15f;
338
public ShadowParameters (double shadowAlpha, double shadowRadius)
340
this.shadowAlpha = shadowAlpha;
341
this.shadowRadius = shadowRadius;