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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/HPanedThin.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
// HPanedThin.cs
 
3
//
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc
 
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 MonoDevelop.Ide.Gui;
 
28
using System.Collections.Generic;
 
29
 
 
30
namespace MonoDevelop.Components
 
31
{
 
32
        public class HPanedThin: Gtk.HPaned
 
33
        {
 
34
                static HashSet<int> stylesParsed = new HashSet<int> ();
 
35
 
 
36
                CustomPanedHandle handle;
 
37
 
 
38
                public HPanedThin ()
 
39
                {
 
40
                        Mono.TextEditor.GtkWorkarounds.FixContainerLeak (this);
 
41
                        handle = new CustomPanedHandle (this);
 
42
                        handle.Parent = this;
 
43
                }
 
44
 
 
45
                public int GrabAreaSize {
 
46
                        get { return handle.GrabAreaSize; }
 
47
                        set {
 
48
                                handle.GrabAreaSize = value;
 
49
                                QueueResize ();
 
50
                        }
 
51
                }
 
52
 
 
53
                public Gtk.Widget HandleWidget {
 
54
                        get { return handle.HandleWidget; }
 
55
                        set { handle.HandleWidget = value; }
 
56
                }
 
57
 
 
58
                internal static void InitStyle (Gtk.Paned paned, int size)
 
59
                {
 
60
                        string id = "MonoDevelop.ThinPanedHandle.s" + size;
 
61
                        if (stylesParsed.Add (size)) {
 
62
                                Gtk.Rc.ParseString ("style \"" + id + "\" {\n GtkPaned::handle-size = " + size + "\n }\n");
 
63
                                Gtk.Rc.ParseString ("widget \"*." + id + "\" style  \"" + id + "\"\n");
 
64
                        }
 
65
                        paned.Name = id;
 
66
                }
 
67
 
 
68
                protected override void ForAll (bool include_internals, Gtk.Callback callback)
 
69
                {
 
70
                        base.ForAll (include_internals, callback);
 
71
                        if (handle != null)
 
72
                                callback (handle);
 
73
                }
 
74
 
 
75
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
76
                {
 
77
                        base.OnExposeEvent (evnt);
 
78
 
 
79
                        if (Child1 != null && Child1.Visible && Child2 != null && Child2.Visible) {
 
80
                                var gc = new Gdk.GC (evnt.Window);
 
81
                                gc.RgbFgColor = (HslColor) Styles.ThinSplitterColor;
 
82
                                var x = Child1.Allocation.X + Child1.Allocation.Width;
 
83
                                evnt.Window.DrawLine (gc, x, Allocation.Y, x, Allocation.Y + Allocation.Height);
 
84
                                gc.Dispose ();
 
85
                        }
 
86
 
 
87
                        return true;
 
88
                }
 
89
        }
 
90
 
 
91
        class CustomPanedHandle: Gtk.EventBox
 
92
        {
 
93
                static Gdk.Cursor resizeCursorW = new Gdk.Cursor (Gdk.CursorType.SbHDoubleArrow);
 
94
                static Gdk.Cursor resizeCursorH = new Gdk.Cursor (Gdk.CursorType.SbVDoubleArrow);
 
95
                internal const int HandleGrabWidth = 4;
 
96
 
 
97
                Gtk.Paned parent;
 
98
                bool horizontal;
 
99
                bool dragging;
 
100
                int initialPos;
 
101
                int initialPanedPos;
 
102
 
 
103
                public CustomPanedHandle (Gtk.Paned parent)
 
104
                {
 
105
                        this.parent = parent;
 
106
                        this.horizontal = parent is HPanedThin;
 
107
                        GrabAreaSize = HandleGrabWidth;
 
108
                        Events |= Gdk.EventMask.EnterNotifyMask | Gdk.EventMask.LeaveNotifyMask | Gdk.EventMask.PointerMotionMask | Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask;
 
109
 
 
110
                        parent.SizeRequested += delegate {
 
111
                                SizeRequest ();
 
112
                        };
 
113
                        parent.SizeAllocated += HandleSizeAllocated;
 
114
                        HandleWidget = null;
 
115
                }
 
116
 
 
117
                void HandleSizeAllocated (object o, Gtk.SizeAllocatedArgs args)
 
118
                {
 
119
                        if (parent.Child1 != null && parent.Child1.Visible && parent.Child2 != null && parent.Child2.Visible) {
 
120
                                Show ();
 
121
                                int centerSize = Child == null ? GrabAreaSize / 2 : 0;
 
122
                                if (horizontal)
 
123
                                        SizeAllocate (new Gdk.Rectangle (parent.Child1.Allocation.X + parent.Child1.Allocation.Width - centerSize, args.Allocation.Y, GrabAreaSize, args.Allocation.Height));
 
124
                                else
 
125
                                        SizeAllocate (new Gdk.Rectangle (args.Allocation.X, parent.Child1.Allocation.Y + parent.Child1.Allocation.Height - centerSize, args.Allocation.Width, GrabAreaSize));
 
126
                        } else
 
127
                                Hide ();
 
128
                }
 
129
 
 
130
                public int GrabAreaSize {
 
131
                        get {
 
132
                                if (horizontal)
 
133
                                        return SizeRequest ().Width;
 
134
                                else
 
135
                                        return SizeRequest ().Height;
 
136
                        }
 
137
                        set {
 
138
                                if (horizontal)
 
139
                                        WidthRequest = value;
 
140
                                else
 
141
                                        HeightRequest = value;
 
142
                        }
 
143
                }
 
144
 
 
145
                public Gtk.Widget HandleWidget {
 
146
                        get { return Child; }
 
147
                        set {
 
148
                                if (Child != null) {
 
149
                                        Remove (Child);
 
150
                                }
 
151
                                if (value != null) {
 
152
                                        Add (value);
 
153
                                        value.Show ();
 
154
                                        VisibleWindow = true;
 
155
                                        WidthRequest = HeightRequest = -1;
 
156
                                        HPanedThin.InitStyle (parent, GrabAreaSize);
 
157
                                } else {
 
158
                                        VisibleWindow = false;
 
159
                                        if (horizontal)
 
160
                                                WidthRequest = 1;
 
161
                                        else
 
162
                                                HeightRequest = 1;
 
163
                                        HPanedThin.InitStyle (parent, 1);
 
164
                                }
 
165
                        }
 
166
                }
 
167
 
 
168
                protected override bool OnEnterNotifyEvent (Gdk.EventCrossing evnt)
 
169
                {
 
170
                        if (horizontal)
 
171
                                GdkWindow.Cursor = resizeCursorW;
 
172
                        else
 
173
                                GdkWindow.Cursor = resizeCursorH;
 
174
                        return base.OnEnterNotifyEvent (evnt);
 
175
                }
 
176
 
 
177
                protected override bool OnLeaveNotifyEvent (Gdk.EventCrossing evnt)
 
178
                {
 
179
                        GdkWindow.Cursor = null;
 
180
                        return base.OnLeaveNotifyEvent (evnt);
 
181
                }
 
182
 
 
183
                protected override bool OnButtonPressEvent (Gdk.EventButton evnt)
 
184
                {
 
185
                        if (horizontal)
 
186
                                initialPos = (int) evnt.XRoot;
 
187
                        else
 
188
                                initialPos = (int) evnt.YRoot;
 
189
                        initialPanedPos = parent.Position;
 
190
                        dragging = true;
 
191
                        return true;
 
192
                }
 
193
 
 
194
                protected override bool OnButtonReleaseEvent (Gdk.EventButton evnt)
 
195
                {
 
196
                        dragging = false;
 
197
                        return true;
 
198
                }
 
199
 
 
200
                protected override bool OnMotionNotifyEvent (Gdk.EventMotion evnt)
 
201
                {
 
202
                        if (dragging) {
 
203
                                if (horizontal) {
 
204
                                        int newpos = initialPanedPos + ((int) evnt.XRoot - initialPos);
 
205
                                        parent.Position = newpos >= 10 ? newpos : 10;
 
206
                                }
 
207
                                else {
 
208
                                        int newpos = initialPanedPos + ((int) evnt.YRoot - initialPos);
 
209
                                        parent.Position = newpos >= 10 ? newpos : 10;
 
210
                                }
 
211
                        }
 
212
                        return base.OnMotionNotifyEvent (evnt);
 
213
                }
 
214
        }
 
215
}
 
216