~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Theatrics/SmartScrolledWindow.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-07-05 13:00:05 UTC
  • mfrom: (1.2.8 upstream) (1.3.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100705130005-d6hp4k5gcn1xkj8c
Tags: 2.4+dfsg-1ubuntu1
* debian/patches/remove_support_for_moonlight.patch,
  debian/patches/dont_add_moonlight_to_core_addins.patch,
  debian/control:
  + Enable support for Moonlight
* debian/rules:
  + Ensure Moonlight addin isn't shipped in main MonoDevelop package by
    mistake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Linq;
 
3
using Gtk;
 
4
using Gdk;
 
5
using System.Collections.Generic;
 
6
 
 
7
namespace Mono.TextEditor.Theatrics
 
8
{
 
9
        /// <summary>
 
10
        /// A scrolled window with the ability to put widgets beside the scrollbars.
 
11
        /// </summary>
 
12
        public class SmartScrolledWindow : Bin
 
13
        {
 
14
                Adjustment vAdjustment;
 
15
                Gtk.VScrollbar vScrollBar;
 
16
                
 
17
                Adjustment hAdjustment;
 
18
                Gtk.HScrollbar hScrollBar;
 
19
                
 
20
                List<SmartScrolledWindowContainerChild> children = new List<SmartScrolledWindowContainerChild> ();
 
21
                public enum ChildPosition {
 
22
                        Top,
 
23
                        Bottom,
 
24
                        Left,
 
25
                        Right
 
26
                }
 
27
                
 
28
                public Adjustment Vadjustment {
 
29
                        get { return this.vAdjustment; }
 
30
                }
 
31
 
 
32
                public Adjustment Hadjustment {
 
33
                        get { return this.hAdjustment; }
 
34
                }
 
35
                
 
36
                public override ContainerChild this [Widget w] {
 
37
                        get {
 
38
                                foreach (ContainerChild info in children.ToArray ()) {
 
39
                                        if (info.Child == w)
 
40
                                                return info;
 
41
                                }
 
42
                                return null;
 
43
                        }
 
44
                }
 
45
                
 
46
                public class SmartScrolledWindowContainerChild : Container.ContainerChild
 
47
                {
 
48
                        public ChildPosition ChildPosition {
 
49
                                get;
 
50
                                set;
 
51
                        }
 
52
                        
 
53
                        public SmartScrolledWindowContainerChild (Container parent, Widget child) : base (parent, child)
 
54
                        {
 
55
                        }
 
56
                }
 
57
                
 
58
                protected SmartScrolledWindow (IntPtr ptr) : base (ptr)
 
59
                {
 
60
                }
 
61
                
 
62
                public SmartScrolledWindow ()
 
63
                {
 
64
                        vAdjustment = new Adjustment (0, 0, 0, 0, 0, 0);
 
65
                        vAdjustment.Changed += HandleAdjustmentChanged;
 
66
                        
 
67
                        vScrollBar = new VScrollbar (vAdjustment);
 
68
                        vScrollBar.Parent = this;
 
69
                        vScrollBar.Show ();
 
70
                        
 
71
                        hAdjustment = new Adjustment (0, 0, 0, 0, 0, 0);
 
72
                        hAdjustment.Changed += HandleAdjustmentChanged;
 
73
                        
 
74
                        hScrollBar = new HScrollbar (hAdjustment);
 
75
                        hScrollBar.Parent = this;
 
76
                        hScrollBar.Show ();
 
77
                }
 
78
                
 
79
                void HandleAdjustmentChanged (object sender, EventArgs e)
 
80
                {
 
81
                        Adjustment adjustment = (Adjustment)sender;
 
82
                        Scrollbar scrollbar = adjustment == vAdjustment ? (Scrollbar)vScrollBar : hScrollBar;
 
83
                        bool newVisible = adjustment.Upper - adjustment.Lower > adjustment.PageSize;
 
84
                        if (scrollbar.Visible != newVisible) {
 
85
                                scrollbar.Visible = newVisible;
 
86
                                QueueResize ();
 
87
                        }
 
88
                }
 
89
                
 
90
                public override GLib.GType ChildType ()
 
91
                {
 
92
                        return Gtk.Widget.GType;
 
93
                }
 
94
                
 
95
                protected override void ForAll (bool include_internals, Gtk.Callback callback)
 
96
                {
 
97
                        base.ForAll (include_internals, callback);
 
98
                        
 
99
                        if (include_internals) {
 
100
                                callback (vScrollBar);
 
101
                                callback (hScrollBar);
 
102
                                children.ForEach (child => callback (child.Child));
 
103
                        }
 
104
                }
 
105
                
 
106
                public void AddChild (Gtk.Widget child, ChildPosition position)
 
107
                {
 
108
                        child.Parent = this;
 
109
                        child.Show ();
 
110
                        children.Add (new SmartScrolledWindowContainerChild (this, child) { ChildPosition = position });
 
111
                }
 
112
                
 
113
                protected override void OnAdded (Widget widget)
 
114
                {
 
115
                        base.OnAdded (widget);
 
116
                        if (widget == Child)
 
117
                                widget.SetScrollAdjustments (hAdjustment, vAdjustment);
 
118
                }
 
119
                
 
120
                protected override void OnRemoved (Widget widget)
 
121
                {
 
122
                        widget.Unparent ();
 
123
                        foreach (var info in children.ToArray ()) {
 
124
                                if (info.Child == widget) {
 
125
                                        children.Remove (info);
 
126
                                        break;
 
127
                                }
 
128
                        }
 
129
                }
 
130
                
 
131
                protected override void OnDestroyed ()
 
132
                {
 
133
                        base.OnDestroyed (); // child gets destroyed here
 
134
                        vScrollBar.Destroy ();
 
135
                        hScrollBar.Destroy ();
 
136
                        vAdjustment.Destroy ();
 
137
                        hAdjustment.Destroy (); 
 
138
                }
 
139
                 
 
140
                protected override void OnSizeAllocated (Rectangle allocation)
 
141
                {
 
142
                        base.OnSizeAllocated (allocation);
 
143
                        
 
144
                        int vwidth = vScrollBar.Visible ? vScrollBar.Requisition.Width : 1;
 
145
                        int hheight = hScrollBar.Visible ? hScrollBar.Requisition.Height : 1; 
 
146
                        Rectangle childRectangle = new Rectangle (allocation.X + 1, allocation.Y + 1, allocation.Width - vwidth, allocation.Height - hheight);
 
147
                        if (Child != null) 
 
148
                                Child.SizeAllocate (childRectangle);
 
149
                        
 
150
                        if (vScrollBar.Visible) {
 
151
                                int right = childRectangle.Right;
 
152
                                int vChildTopHeight = -1;
 
153
                                foreach (var child in children.Where (child => child.ChildPosition == ChildPosition.Top)) {
 
154
                                        child.Child.SizeAllocate (new Rectangle (right, childRectangle.Y + vChildTopHeight, allocation.Width - vwidth, child.Child.Requisition.Height));
 
155
                                        vChildTopHeight += child.Child.Requisition.Height;
 
156
                                }
 
157
                                int v = hScrollBar.Visible ? hScrollBar.Requisition.Height : 0;
 
158
                                vScrollBar.SizeAllocate (new Rectangle (right, childRectangle.Y + vChildTopHeight, vwidth, Allocation.Height - v - vChildTopHeight));
 
159
                                vScrollBar.Value = System.Math.Max (System.Math.Min (vAdjustment.Upper - vAdjustment.PageSize, vScrollBar.Value), vAdjustment.Lower);
 
160
                        }
 
161
                        
 
162
                        if (hScrollBar.Visible) {
 
163
                                int v = vScrollBar.Visible ? vScrollBar.Requisition.Width : 0;
 
164
                                hScrollBar.SizeAllocate (new Rectangle (allocation.X, childRectangle.Bottom, Allocation.Width - v, hheight));
 
165
                                hScrollBar.Value = System.Math.Max (System.Math.Min (hAdjustment.Upper - hAdjustment.PageSize, hScrollBar.Value), hAdjustment.Lower);
 
166
                        }
 
167
                }
 
168
                
 
169
                static double GetWheelDelta (Scrollbar scrollbar, ScrollDirection direction)
 
170
                {
 
171
                        double delta = System.Math.Pow (scrollbar.Adjustment.PageSize, 2.0 / 3.0);
 
172
                        if (direction == ScrollDirection.Up || direction == ScrollDirection.Left)
 
173
                                delta = -delta;
 
174
                        if (scrollbar.Inverted)
 
175
                                delta = -delta;
 
176
                        return delta;
 
177
                }
 
178
                
 
179
                protected override bool OnScrollEvent (EventScroll evnt)
 
180
                {
 
181
                        Scrollbar scrollWidget = (evnt.Direction == ScrollDirection.Up || evnt.Direction == ScrollDirection.Down) ? (Scrollbar)vScrollBar : hScrollBar;
 
182
                        
 
183
                        if (scrollWidget.Visible) {
 
184
                                double newValue = scrollWidget.Adjustment.Value + GetWheelDelta (scrollWidget, evnt.Direction);
 
185
                                newValue = System.Math.Max (System.Math.Min (scrollWidget.Adjustment.Upper  - scrollWidget.Adjustment.PageSize, newValue), scrollWidget.Adjustment.Lower);
 
186
                                scrollWidget.Adjustment.Value = newValue;
 
187
                        }
 
188
                        return base.OnScrollEvent (evnt);
 
189
                }
 
190
                
 
191
                protected override void OnSizeRequested (ref Gtk.Requisition requisition)
 
192
                {
 
193
                        base.OnSizeRequested (ref requisition);
 
194
                        if (Child != null)
 
195
                                Child.SizeRequest ();
 
196
                        vScrollBar.SizeRequest ();
 
197
                        hScrollBar.SizeRequest ();
 
198
                        children.ForEach (child => child.Child.SizeRequest ());
 
199
                }
 
200
                
 
201
                protected override bool OnExposeEvent (EventExpose evnt)
 
202
                {
 
203
                        Gdk.GC gc = Style.DarkGC (State);
 
204
                        evnt.Window.DrawLine (gc, Allocation.X, Allocation.Top, Allocation.X, Allocation.Bottom);
 
205
                        if (vScrollBar.Visible && hScrollBar.Visible) {
 
206
                                evnt.Window.DrawLine (gc, Allocation.Right, Allocation.Top, Allocation.Right, Allocation.Y + Allocation.Height / 2);
 
207
                        } else {
 
208
                                evnt.Window.DrawLine (gc, Allocation.Right, Allocation.Top, Allocation.Right, Allocation.Bottom);
 
209
                        }
 
210
                        evnt.Window.DrawLine (gc, Allocation.Left, Allocation.Y, Allocation.Right, Allocation.Y);
 
211
                        if (vScrollBar.Visible && hScrollBar.Visible) {
 
212
                                evnt.Window.DrawLine (gc, Allocation.Left, Allocation.Bottom, Allocation.Left + Allocation.Width / 2 , Allocation.Bottom);
 
213
                        } else {
 
214
                                evnt.Window.DrawLine (gc, Allocation.Left, Allocation.Bottom, Allocation.Right, Allocation.Bottom);
 
215
                        }
 
216
                        
 
217
/*                      if (vScrollBar.Visible && hScrollBar.Visible) {
 
218
                                int vwidth = vScrollBar.Requisition.Width;
 
219
                                int hheight = hScrollBar.Requisition.Height; 
 
220
 
 
221
                                evnt.Window.DrawRectangle (Style.BackgroundGC (State), true, 
 
222
                                                           Allocation.Right - vwidth, 
 
223
                                                           Allocation.Bottom - hheight, 
 
224
                                                           vwidth, hheight);
 
225
                        }*/
 
226
                        return base.OnExposeEvent (evnt);
 
227
                }
 
228
                
 
229
        }
 
230
}
 
231