~docky-core/docky/gio-cleanup

« back to all changes in this revision

Viewing changes to Docky/Docky/DockPlacementWidget.cs

  • Committer: Rico Tzschichholz
  • Date: 2010-09-23 09:00:01 UTC
  • mfrom: (1453.1.189 docky)
  • Revision ID: ricotz@t-online.de-20100923090001-u2zyf16hqffnnmnk
mergeĀ trunkĀ 1642

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  
2
 
//  Copyright (C) 2009 Jason Smith
3
 
// 
4
 
//  This program is free software: you can redistribute it and/or modify
5
 
//  it under the terms of the GNU General Public License as published by
6
 
//  the Free Software Foundation, either version 3 of the License, or
7
 
//  (at your option) any later version.
8
 
// 
9
 
//  This program is distributed in the hope that it will be useful,
10
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
//  GNU General Public License for more details.
13
 
// 
14
 
//  You should have received a copy of the GNU General Public License
15
 
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
// 
17
 
 
18
 
using System;
19
 
using System.Collections.Generic;
20
 
using System.Collections.ObjectModel;
21
 
using System.Linq;
22
 
using System.Text;
23
 
 
24
 
using Cairo;
25
 
using Gdk;
26
 
using Gtk;
27
 
 
28
 
using Docky.Interface;
29
 
 
30
 
namespace Docky
31
 
{
32
 
 
33
 
 
34
 
        public class DockPlacementWidget : Gtk.DrawingArea
35
 
        {
36
 
                const int Width = 260;
37
 
                const int Height = 175;
38
 
                
39
 
                const int DockSize = 30;
40
 
 
41
 
                Gdk.Rectangle allocation;
42
 
                IEnumerable<Dock> docks;
43
 
                
44
 
                public event EventHandler ActiveDockChanged;
45
 
                
46
 
                int X {
47
 
                        get { return (allocation.Width - Width) / 2; }
48
 
                }
49
 
                
50
 
                int Y {
51
 
                        get { return (allocation.Height - Height) / 2; }
52
 
                }
53
 
                
54
 
                Dock activeDock;
55
 
                public Dock ActiveDock {
56
 
                        get { return activeDock; } 
57
 
                        set { 
58
 
                                if (!docks.Contains (value))
59
 
                                        return;
60
 
                                activeDock = value; 
61
 
                                QueueDraw ();
62
 
                                OnActiveDockChanged ();
63
 
                        }
64
 
                }
65
 
                
66
 
                public DockPlacementWidget (IEnumerable<Dock> docks)
67
 
                {
68
 
                        this.docks = docks;
69
 
                        RegisterDocks ();
70
 
                        ActiveDock = docks.First ();
71
 
                        SetSizeRequest (Width, Height);
72
 
                        
73
 
                        AddEvents ((int) Gdk.EventMask.AllEventsMask);
74
 
                }
75
 
                
76
 
                public void SetDocks (IEnumerable<Dock> docks)
77
 
                {
78
 
                        UnregisterDocks ();
79
 
                        this.docks = docks;
80
 
                        RegisterDocks ();
81
 
                        
82
 
                        ActiveDock = docks.First ();
83
 
                }
84
 
                
85
 
                void RegisterDocks ()
86
 
                {
87
 
                        foreach (Dock dock in docks) {
88
 
                                dock.Preferences.PositionChanged += DockPreferencesPositionChanged;
89
 
                        }
90
 
                }
91
 
 
92
 
                void DockPreferencesPositionChanged (object sender, EventArgs e)
93
 
                {
94
 
                        QueueDraw ();
95
 
                }
96
 
                
97
 
                void UnregisterDocks ()
98
 
                {
99
 
                        foreach (Dock dock in docks) {
100
 
                                dock.Preferences.PositionChanged -= DockPreferencesPositionChanged;
101
 
                        }
102
 
                }
103
 
                
104
 
                // Is there really not an easier way to get this?
105
 
                protected override void OnSizeAllocated (Gdk.Rectangle allocation)
106
 
                {
107
 
                        this.allocation = allocation;
108
 
                        
109
 
                        base.OnSizeAllocated (allocation);
110
 
                }
111
 
                
112
 
                protected override bool OnButtonReleaseEvent (EventButton evnt)
113
 
                {
114
 
                        foreach (Dock dock in docks) {
115
 
                                Gdk.Rectangle area = DockRenderArea (dock.Preferences.Position);
116
 
                                if (area.Contains ((int) evnt.X, (int) evnt.Y)) {
117
 
                                        ActiveDock = dock;
118
 
                                        OnActiveDockChanged ();
119
 
                                        break;
120
 
                                }
121
 
                        }
122
 
                        
123
 
                        return base.OnButtonReleaseEvent (evnt);
124
 
                }
125
 
 
126
 
                
127
 
                Gdk.Rectangle DockRenderArea (DockPosition position)
128
 
                {
129
 
                        int x = X;
130
 
                        int y = Y;
131
 
                        
132
 
                        switch (position) {
133
 
                        case DockPosition.Top:
134
 
                                return new Gdk.Rectangle (x + (DockSize + 5), y + 1, Width - 2 * (DockSize + 5), DockSize);
135
 
                        case DockPosition.Left:
136
 
                                return new Gdk.Rectangle (x + 1, y + (DockSize + 5), DockSize, Height - 2 * (DockSize + 5));
137
 
                        case DockPosition.Right:
138
 
                                return new Gdk.Rectangle (x + Width - DockSize - 1, y + (DockSize + 5), DockSize, Height - 2 * (DockSize + 5)); 
139
 
                        default:
140
 
                        case DockPosition.Bottom:
141
 
                                return new Gdk.Rectangle (x + (DockSize + 5), y + Height - DockSize - 1, Width - 2 * (DockSize + 5), DockSize);
142
 
                        }
143
 
                }
144
 
                
145
 
                protected override bool OnExposeEvent (EventExpose evnt)
146
 
                {
147
 
                        if (!IsRealized)
148
 
                                return true;
149
 
                        
150
 
                        bool result = base.OnExposeEvent (evnt);
151
 
                        
152
 
                        int x = X;
153
 
                        int y = Y;
154
 
                        
155
 
                        using (Cairo.Context cr = Gdk.CairoHelper.Create (evnt.Window)) {
156
 
                                cr.RoundedRectangle (x + .5, 
157
 
                                                     y + .5, 
158
 
                                                     Width - 1, 
159
 
                                                     Height - 1,
160
 
                                                     5);
161
 
                                
162
 
                                LinearGradient lg = new LinearGradient (x, y, x + Width, y + Width);
163
 
                                lg.AddColorStop (0, new Cairo.Color (0.6, 0.9, 1.0, 0.7));
164
 
                                lg.AddColorStop (1, new Cairo.Color (0.3, 0.6, 0.9, 0.7));
165
 
                                cr.Pattern = lg;
166
 
                                cr.FillPreserve ();
167
 
                                
168
 
                                lg.Destroy ();
169
 
                                
170
 
                                cr.Color = new Cairo.Color (0, 0, 0);
171
 
                                cr.LineWidth = 1;
172
 
                                cr.Stroke ();
173
 
                                
174
 
                                foreach (Dock dock in docks) {
175
 
                                        Gdk.Rectangle area = DockRenderArea (dock.Preferences.Position);
176
 
                                        cr.Rectangle (area.X, area.Y, area.Width, area.Height);
177
 
                                        
178
 
                                        if (ActiveDock == dock)
179
 
                                                cr.Color = new Cairo.Color (1, 1, 1, .9);
180
 
                                        else
181
 
                                                cr.Color = new Cairo.Color (1, 1, 1, .35);
182
 
                                        
183
 
                                        cr.Fill ();
184
 
                                }
185
 
                        }
186
 
                        
187
 
                        return result;
188
 
                }
189
 
 
190
 
                void OnActiveDockChanged ()
191
 
                {
192
 
                        QueueDraw ();
193
 
                        if (ActiveDockChanged != null)
194
 
                                ActiveDockChanged (this, EventArgs.Empty);
195
 
                }
196
 
        }
197
 
}