5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
29
using MonoDevelop.Components;
31
using MonoDevelop.Ide;
32
using System.Reflection;
35
namespace MonoDevelop.Components.MainToolbar
42
public ImageSurface Img {
45
img = CairoExtensions.LoadImage (Assembly.GetCallingAssembly (), resourceName);
50
public LazyImage (string resourceName)
52
this.resourceName = resourceName;
55
public static implicit operator ImageSurface(LazyImage lazy)
62
class RoundButton : Gtk.EventBox
64
const int height = 32;
65
/* Cairo.Color borderColor;
67
Cairo.Color fill0Color;
68
Cairo.Color fill1Color;
69
Cairo.Color fill2Color;
70
Cairo.Color fill3Color;*/
72
LazyImage btnNormal/*, btnInactive, btnHover, btnPressed*/;
74
LazyImage iconRunNormal, iconRunDisabled;
75
LazyImage iconStopNormal, iconStopDisabled;
76
LazyImage iconBuildNormal, iconBuildDisabled;
78
public enum OperationIcon {
86
WidgetFlags |= Gtk.WidgetFlags.AppPaintable;
87
Events |= EventMask.ButtonPressMask | EventMask.ButtonReleaseMask | EventMask.LeaveNotifyMask | EventMask.PointerMotionMask;
88
VisibleWindow = false;
89
SetSizeRequest (height, height);
91
btnNormal = new LazyImage ("btExecuteBase-Normal.png");
92
// btnInactive = new LazyImage ("btExecuteBase-Disabled.png");
93
//btnPressed = new LazyImage ("btExecuteBase-Pressed.png");
94
// btnHover = new LazyImage ("btExecuteBase-Hover.png");
96
iconRunNormal = new LazyImage ("icoExecute-Normal.png");
97
iconRunDisabled = new LazyImage ("icoExecute-Disabled.png");
99
iconStopNormal = new LazyImage ("icoStop-Normal.png");
100
iconStopDisabled = new LazyImage ("icoStop-Disabled.png");
102
iconBuildNormal = new LazyImage ("icoBuild-Normal.png");
103
iconBuildDisabled = new LazyImage ("icoBuild-Disabled.png");
106
StateType hoverState = StateType.Normal;
108
protected override bool OnMotionNotifyEvent (EventMotion evnt)
111
State = IsInside (evnt.X, evnt.Y) ? hoverState : StateType.Normal;;
112
return base.OnMotionNotifyEvent (evnt);
116
protected override bool OnLeaveNotifyEvent (EventCrossing evnt)
118
State = StateType.Normal;
119
return base.OnLeaveNotifyEvent (evnt);
122
protected override bool OnButtonPressEvent (EventButton evnt)
124
if (evnt.Button == 1 && IsInside (evnt.X, evnt.Y)) {
125
hoverState = State = StateType.Selected;
130
protected override bool OnButtonReleaseEvent (EventButton evnt)
132
if (State == StateType.Selected)
133
OnClicked (EventArgs.Empty);
134
State = IsInside (evnt.X, evnt.Y) ? StateType.Prelight : StateType.Normal;;
135
hoverState = StateType.Prelight;
139
bool IsInside (double x, double y)
141
var xr = x - Allocation.Width / 2;
142
var yr = y - Allocation.Height / 2;
143
return Math.Sqrt (xr * xr + yr * yr) <= height / 2;
146
protected override void OnSizeRequested (ref Requisition requisition)
148
requisition.Width = btnNormal.Img.Width;
149
requisition.Height = btnNormal.Img.Height + 2;
150
base.OnSizeRequested (ref requisition);
153
ImageSurface GetIcon()
156
case OperationIcon.Stop:
157
return State == StateType.Insensitive ? iconStopDisabled : iconStopNormal;
158
case OperationIcon.Run:
159
return State == StateType.Insensitive ? iconRunDisabled : iconRunNormal;
160
case OperationIcon.Build:
161
return State == StateType.Insensitive ? iconBuildDisabled : iconBuildNormal;
163
throw new InvalidOperationException ();
167
public OperationIcon Icon {
177
protected override bool OnExposeEvent (EventExpose evnt)
179
using (var context = Gdk.CairoHelper.Create (evnt.Window)) {
180
DrawBackground (context, Allocation, 15, State);
181
var icon = GetIcon();
184
Allocation.X + (icon.Width - Allocation.Width) / 2,
185
Allocation.Y + (icon.Height - Allocation.Height) / 2
188
return base.OnExposeEvent (evnt);
191
void DrawBackground (Cairo.Context context, Gdk.Rectangle region, int radius, StateType state)
193
double rad = radius - 0.5;
194
int centerX = region.X + region.Width / 2;
195
int centerY = region.Y + region.Height / 2;
197
context.MoveTo (centerX + rad, centerY);
198
context.Arc (centerX, centerY, rad, 0, Math.PI * 2);
203
case StateType.Selected:
207
case StateType.Prelight:
211
case StateType.Insensitive:
220
using (var lg = new LinearGradient (0, centerY - rad, 0, centerY +rad)) {
221
lg.AddColorStop (0, new Cairo.Color (high, high, high));
222
lg.AddColorStop (1, new Cairo.Color (low, low, low));
223
context.Pattern = lg;
224
context.FillPreserve ();
227
context.Color = new Cairo.Color (0, 0, 0, 0.4);
228
context.LineWidth = 1;
232
public event EventHandler Clicked;
234
protected virtual void OnClicked (EventArgs e)
236
EventHandler handler = this.Clicked;