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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/ImageButton.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
// ImageButton.cs
 
3
//
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@xamarin.com>
 
6
//
 
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.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 MonoDevelop.Ide;
 
28
 
 
29
namespace MonoDevelop.Components
 
30
{
 
31
        public class ImageButton: Gtk.EventBox
 
32
        {
 
33
                Gdk.Pixbuf image;
 
34
                Gdk.Pixbuf inactiveImage;
 
35
                Gtk.Image imageWidget;
 
36
                bool hasInactiveImage;
 
37
                bool hover;
 
38
                bool pressed;
 
39
 
 
40
                public ImageButton ()
 
41
                {
 
42
                        Events |= Gdk.EventMask.EnterNotifyMask | Gdk.EventMask.LeaveNotifyMask | Gdk.EventMask.ButtonReleaseMask;
 
43
                        VisibleWindow = false;
 
44
                        imageWidget = new Gtk.Image ();
 
45
                        imageWidget.Show ();
 
46
                        Add (imageWidget);
 
47
                }
 
48
 
 
49
                public Gdk.Pixbuf Image {
 
50
                        get { return image; }
 
51
                        set {
 
52
                                image = value;
 
53
                                Gdk.Pixbuf oldInactive = null;
 
54
                                if (!hasInactiveImage) {
 
55
                                        oldInactive = inactiveImage;
 
56
                                        inactiveImage = image != null ? ImageService.MakeTransparent (image, 0.5) : null;
 
57
                                }
 
58
                                LoadImage ();
 
59
                                if (oldInactive != null)
 
60
                                        oldInactive.Dispose ();
 
61
                        }
 
62
                }
 
63
 
 
64
                public Gdk.Pixbuf InactiveImage {
 
65
                        get { return hasInactiveImage ? inactiveImage : null; }
 
66
                        set {
 
67
                                if (!hasInactiveImage && inactiveImage != null)
 
68
                                        inactiveImage.Dispose ();
 
69
                                hasInactiveImage = true;
 
70
                                inactiveImage = value;
 
71
                                LoadImage ();
 
72
                        }
 
73
                }
 
74
 
 
75
                protected override void OnDestroyed ()
 
76
                {
 
77
                        if (!hasInactiveImage && inactiveImage != null)
 
78
                                inactiveImage.Dispose ();
 
79
                        base.OnDestroyed ();
 
80
                }
 
81
 
 
82
                void LoadImage ()
 
83
                {
 
84
                        if (image != null) {
 
85
                                if (hover)
 
86
                                        imageWidget.Pixbuf = image;
 
87
                                else
 
88
                                        imageWidget.Pixbuf = inactiveImage;
 
89
                        } else {
 
90
                                imageWidget.Pixbuf = null;
 
91
                        }
 
92
                }
 
93
 
 
94
                protected override bool OnEnterNotifyEvent (Gdk.EventCrossing evnt)
 
95
                {
 
96
                        hover = true;
 
97
                        LoadImage ();
 
98
                        return base.OnEnterNotifyEvent (evnt);
 
99
                }
 
100
 
 
101
                protected override bool OnLeaveNotifyEvent (Gdk.EventCrossing evnt)
 
102
                {
 
103
                        hover = false;
 
104
                        LoadImage ();
 
105
                        return base.OnLeaveNotifyEvent (evnt);
 
106
                }
 
107
 
 
108
                protected override bool OnButtonPressEvent (Gdk.EventButton evnt)
 
109
                {
 
110
                        pressed = image != null;
 
111
                        return base.OnButtonPressEvent (evnt);
 
112
                }
 
113
 
 
114
                protected override bool OnButtonReleaseEvent (Gdk.EventButton evnt)
 
115
                {
 
116
                        if (pressed && evnt.Button == 1 && new Gdk.Rectangle (0, 0, Allocation.Width, Allocation.Height).Contains ((int)evnt.X, (int)evnt.Y)) {
 
117
                                hover = false;
 
118
                                LoadImage ();
 
119
                                if (Clicked != null)
 
120
                                        Clicked (this, EventArgs.Empty);
 
121
                                return true;
 
122
                        }
 
123
                        return base.OnButtonReleaseEvent (evnt);
 
124
                }
 
125
 
 
126
                public event EventHandler Clicked;
 
127
        }
 
128
}
 
129