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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/HeaderBox.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
// HeaderBox.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 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
using System;
 
27
using Gtk;
 
28
using Xwt.Drawing;
 
29
using Xwt.CairoBackend;
 
30
 
 
31
namespace Xwt.GtkBackend
 
32
{
 
33
        class HeaderBox: Bin
 
34
        {
 
35
                Gtk.Widget child;
 
36
                int topMargin;
 
37
                int bottomMargin;
 
38
                int leftMargin;
 
39
                int rightMargin;
 
40
                
 
41
                int topPadding;
 
42
                int bottomPadding;
 
43
                int leftPadding;
 
44
                int rightPadding;
 
45
                
 
46
                Color? color;
 
47
                
 
48
                public HeaderBox ()
 
49
                {
 
50
                }
 
51
                
 
52
                public HeaderBox (IntPtr p): base (p)
 
53
                {
 
54
                }
 
55
                
 
56
                public HeaderBox (int topMargin, int bottomMargin, int leftMargin, int rightMargin)
 
57
                {
 
58
                        SetMargins (topMargin, bottomMargin, leftMargin, rightMargin);
 
59
                }
 
60
 
 
61
                public void SetBorderColor (Color color)
 
62
                {
 
63
                        this.color = color;
 
64
                        QueueDraw ();
 
65
                }
 
66
                
 
67
                public void SetMargins (int topMargin, int bottomMargin, int leftMargin, int rightMargin)
 
68
                {
 
69
                        this.topMargin = topMargin;
 
70
                        this.bottomMargin = bottomMargin;
 
71
                        this.leftMargin = leftMargin;
 
72
                        this.rightMargin = rightMargin;
 
73
                }
 
74
                
 
75
                public void SetPadding (int topPadding, int bottomPadding, int leftPadding, int rightPadding)
 
76
                {
 
77
                        this.topPadding = topPadding;
 
78
                        this.bottomPadding = bottomPadding;
 
79
                        this.leftPadding = leftPadding;
 
80
                        this.rightPadding = rightPadding;
 
81
                }
 
82
                
 
83
                public bool GradientBackround { get; set; }
 
84
                public bool DrawBackground { get; set; }
 
85
 
 
86
                protected override void OnAdded (Gtk.Widget widget)
 
87
                {
 
88
                        base.OnAdded (widget);
 
89
                        child = widget;
 
90
                }
 
91
 
 
92
                protected override void OnSizeRequested (ref Requisition requisition)
 
93
                {
 
94
                        if (child != null) {
 
95
                                requisition = child.SizeRequest ();
 
96
                                requisition.Width += leftMargin + rightMargin + leftPadding + rightPadding;
 
97
                                requisition.Height += topMargin + bottomMargin + topPadding + bottomPadding;
 
98
                        } else {
 
99
                                requisition.Width = 0;
 
100
                                requisition.Height = 0;
 
101
                        }
 
102
                }
 
103
 
 
104
                protected override void OnSizeAllocated (Gdk.Rectangle allocation)
 
105
                {
 
106
                        base.OnSizeAllocated (allocation);
 
107
                        if (allocation.Width > leftMargin + rightMargin + leftPadding + rightPadding) {
 
108
                                allocation.X += leftMargin + leftPadding;
 
109
                                allocation.Width -= leftMargin + rightMargin + leftPadding + rightPadding;
 
110
                        }
 
111
                        if (allocation.Height > topMargin + bottomMargin + topPadding + bottomPadding) {
 
112
                                allocation.Y += topMargin + topPadding;
 
113
                                allocation.Height -= topMargin + bottomMargin + topPadding + bottomPadding;
 
114
                        }
 
115
                        if (child != null)
 
116
                                child.SizeAllocate (allocation);
 
117
                }
 
118
 
 
119
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
120
                {
 
121
                        Gdk.Rectangle rect;
 
122
                        
 
123
                        if (DrawBackground) {
 
124
                                GdkWindow.DrawRectangle (Style.BackgroundGC (Gtk.StateType.Normal), true, Allocation.X, Allocation.Y, Allocation.Width - 1, Allocation.Height - 1);
 
125
                        }
 
126
                        
 
127
                        if (GradientBackround) {
 
128
                                rect = new Gdk.Rectangle (Allocation.X, Allocation.Y, Allocation.Width, Allocation.Height);
 
129
                                Color gcol = Util.ToXwtColor (Style.Background (Gtk.StateType.Normal));
 
130
                                
 
131
                                using (Cairo.Context cr = Gdk.CairoHelper.Create (GdkWindow)) {
 
132
                                        cr.NewPath ();
 
133
                                        cr.MoveTo (rect.X, rect.Y);
 
134
                                        cr.RelLineTo (rect.Width, 0);
 
135
                                        cr.RelLineTo (0, rect.Height);
 
136
                                        cr.RelLineTo (-rect.Width, 0);
 
137
                                        cr.RelLineTo (0, -rect.Height);
 
138
                                        cr.ClosePath ();
 
139
                                        Cairo.Gradient pat = new Cairo.LinearGradient (rect.X, rect.Y, rect.X, rect.Bottom);
 
140
                                        Cairo.Color color1 = gcol.ToCairoColor ();
 
141
                                        pat.AddColorStop (0, color1);
 
142
                                        gcol.Light -= 0.1;
 
143
                                        pat.AddColorStop (1, gcol.ToCairoColor ());
 
144
                                        cr.Pattern = pat;
 
145
                                        cr.FillPreserve ();
 
146
                                }
 
147
                        }
 
148
                        
 
149
                        bool res = base.OnExposeEvent (evnt);
 
150
                        
 
151
                        Gdk.GC borderColor;
 
152
                        if (color != null) {
 
153
                                borderColor = new Gdk.GC (GdkWindow);
 
154
                                borderColor.RgbFgColor = Util.ToGdkColor (color.Value);
 
155
                        }
 
156
                        else
 
157
                                borderColor = Style.DarkGC (Gtk.StateType.Normal);
 
158
                        
 
159
                        rect = Allocation;
 
160
                        for (int n=0; n<topMargin; n++)
 
161
                                GdkWindow.DrawLine (borderColor, rect.X, rect.Y + n, rect.Right, rect.Y + n);
 
162
                        
 
163
                        for (int n=0; n<bottomMargin; n++)
 
164
                                GdkWindow.DrawLine (borderColor, rect.X, rect.Bottom - n, rect.Right, rect.Bottom - n);
 
165
                        
 
166
                        for (int n=0; n<leftMargin; n++)
 
167
                                GdkWindow.DrawLine (borderColor, rect.X + n, rect.Y, rect.X + n, rect.Bottom);
 
168
                        
 
169
                        for (int n=0; n<rightMargin; n++)
 
170
                                GdkWindow.DrawLine (borderColor, rect.Right - n, rect.Y, rect.Right - n, rect.Bottom);
 
171
 
 
172
                        if (color != null)
 
173
                                borderColor.Dispose ();
 
174
                        
 
175
                        return res;
 
176
                }
 
177
        }
 
178
}
 
179