~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to class/System.Windows/Mono/EventHandlerList.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// System.ComponentModel.EventHandlerList.cs
 
3
//
 
4
// Contact:
 
5
//   Moonlight List (moonlight-list@lists.ximian.com)
 
6
//
 
7
// (C) Ximian, Inc.  http://www.ximian.com
 
8
//
 
9
 
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining
 
12
// a copy of this software and associated documentation files (the
 
13
// "Software"), to deal in the Software without restriction, including
 
14
// without limitation the rights to use, copy, modify, merge, publish,
 
15
// distribute, sublicense, and/or sell copies of the Software, and to
 
16
// permit persons to whom the Software is furnished to do so, subject to
 
17
// the following conditions:
 
18
// 
 
19
// The above copyright notice and this permission notice shall be
 
20
// included in all copies or substantial portions of the Software.
 
21
// 
 
22
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
23
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
24
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
25
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
26
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
27
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
28
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
29
//
 
30
 
 
31
using System;
 
32
using System.Collections;
 
33
 
 
34
namespace Mono {
 
35
 
 
36
        // <summary>
 
37
        //   List of Event delegates.
 
38
        // </summary>
 
39
        //
 
40
        // <remarks>
 
41
        //   Longer description
 
42
        // </remarks>
 
43
        sealed class EventHandlerList : IDisposable {
 
44
                public EventHandlerList ()
 
45
                {
 
46
                        head = null;
 
47
                }
 
48
 
 
49
                public Delegate this [object key] {
 
50
                        get {
 
51
                                ListNode entry = FindEntry (key);
 
52
                                return entry == null ? null : entry.value;
 
53
                        }
 
54
 
 
55
                        set {
 
56
                                AddHandler (key, value);
 
57
                        }
 
58
                }
 
59
 
 
60
                public void AddHandler (object key, Delegate value)
 
61
                {
 
62
                        ListNode entry = FindEntry (key);
 
63
                        if (entry == null) {
 
64
                                head = new ListNode (key, value, head);
 
65
                                return;
 
66
                        }
 
67
                        entry.value = Delegate.Combine (entry.value, value);
 
68
                }
 
69
 
 
70
#if NET_2_0
 
71
                public void AddHandlers (EventHandlerList listToAddFrom)
 
72
                {
 
73
                        if (listToAddFrom == null) {
 
74
                                return;
 
75
                        }
 
76
 
 
77
                        for (ListNode entry = listToAddFrom.head; entry != null; entry = entry.next) {
 
78
                                AddHandler (entry.key, entry.value);
 
79
                        }
 
80
                }
 
81
#endif
 
82
 
 
83
                public void RemoveHandler (object key, Delegate value)
 
84
                {
 
85
                        ListNode entry = FindEntry (key);
 
86
                        if (entry == null)
 
87
                                return;
 
88
 
 
89
                        entry.value = Delegate.Remove (entry.value, value);
 
90
                }
 
91
 
 
92
                public void Dispose ()
 
93
                {
 
94
                        head = null;
 
95
                }
 
96
                private ListNode FindEntry (object key)
 
97
                {
 
98
                        for (ListNode entry = head; entry != null; entry = entry.next)
 
99
                                if (key == entry.key)
 
100
                                        return entry;
 
101
                        return null;
 
102
                }
 
103
 
 
104
                private class ListNode
 
105
                {
 
106
                        public object key;
 
107
                        public Delegate value;
 
108
                        public ListNode next;
 
109
                        public ListNode (object key, Delegate value, ListNode next)
 
110
                        {
 
111
                                this.key = key;
 
112
                                this.value = value;
 
113
                                this.next = next;
 
114
                        }
 
115
                }
 
116
 
 
117
                private ListNode head;
 
118
 
 
119
        }
 
120
        
 
121
}