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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt.Backends/BackendHost.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
// BackendHost.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 System.Collections.Generic;
 
28
using Xwt.Engine;
 
29
 
 
30
namespace Xwt.Backends
 
31
{
 
32
        public class BackendHost<T,B>: BackendHost where B:IBackend
 
33
        {
 
34
                public BackendHost ()
 
35
                {
 
36
                }
 
37
                
 
38
                public new T Parent {
 
39
                        get { return (T)base.Parent; }
 
40
                        set { base.Parent = value; }
 
41
                }
 
42
                
 
43
                public new B Backend {
 
44
                        get { return (B)base.Backend; }
 
45
                }
 
46
        }
 
47
        
 
48
        public class BackendHost
 
49
        {
 
50
                IBackend backend;
 
51
                bool usingCustomBackend;
 
52
                
 
53
                HashSet<object> defaultEnabledEvents;
 
54
                
 
55
                public BackendHost ()
 
56
                {
 
57
                }
 
58
                
 
59
                public void SetCustomBackend (IBackend backend)
 
60
                {
 
61
                        this.backend = backend;
 
62
                        usingCustomBackend = true;
 
63
                }
 
64
                
 
65
                public object Parent { get; internal set; }
 
66
                
 
67
                public IBackend Backend {
 
68
                        get {
 
69
                                LoadBackend ();
 
70
                                return backend;
 
71
                        }
 
72
                }
 
73
                
 
74
                internal bool BackendCreated {
 
75
                        get { return backend != null; }
 
76
                }
 
77
                
 
78
                protected virtual void OnBackendCreated ()
 
79
                {
 
80
                        foreach (var ev in DefaultEnabledEvents)
 
81
                                Backend.EnableEvent (ev);
 
82
                }
 
83
                
 
84
                protected virtual IBackend OnCreateBackend ()
 
85
                {
 
86
                        Type t = Parent.GetType ();
 
87
                        while (t != typeof(object)) {
 
88
                                IBackend b = WidgetRegistry.CreateBackend<IBackend> (t);
 
89
                                if (b != null)
 
90
                                        return b;
 
91
                                t = t.BaseType;
 
92
                        }
 
93
                        return null;
 
94
                }
 
95
                
 
96
                public void EnsureBackendLoaded ()
 
97
                {
 
98
                        if (backend == null)
 
99
                                LoadBackend ();
 
100
                }
 
101
                
 
102
                protected void LoadBackend ()
 
103
                {
 
104
                        if (usingCustomBackend) {
 
105
                                usingCustomBackend = false;
 
106
                                backend.InitializeBackend (Parent);
 
107
                                OnBackendCreated ();
 
108
                        }
 
109
                        else if (backend == null) {
 
110
                                backend = OnCreateBackend ();
 
111
                                if (backend == null)
 
112
                                        throw new InvalidOperationException ("No backend found for object: " + Parent.GetType ());
 
113
                                backend.InitializeBackend (Parent);
 
114
                                OnBackendCreated ();
 
115
                        }
 
116
                }
 
117
                
 
118
                public void OnBeforeEventAdd (object eventId, Delegate eventDelegate)
 
119
                {
 
120
                        if (eventDelegate == null && !DefaultEnabledEvents.Contains (eventId))
 
121
                                Backend.EnableEvent (eventId);
 
122
                }
 
123
                
 
124
                public void OnAfterEventRemove (object eventId, Delegate eventDelegate)
 
125
                {
 
126
                        if (eventDelegate != null && !DefaultEnabledEvents.Contains (eventId))
 
127
                                Backend.DisableEvent (eventId);
 
128
                }
 
129
                
 
130
                internal HashSet<object> DefaultEnabledEvents {
 
131
                        get {
 
132
                                if (defaultEnabledEvents == null)
 
133
                                        defaultEnabledEvents = EventUtil.GetDefaultEnabledEvents (Parent.GetType ());
 
134
                                return defaultEnabledEvents;
 
135
                        }
 
136
                }       
 
137
        }
 
138
}
 
139