5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc
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
27
using System.Collections.Generic;
30
namespace Xwt.Backends
32
public class BackendHost<T,B>: BackendHost where B:IBackend
39
get { return (T)base.Parent; }
40
set { base.Parent = value; }
43
public new B Backend {
44
get { return (B)base.Backend; }
48
public class BackendHost
51
bool usingCustomBackend;
53
HashSet<object> defaultEnabledEvents;
59
public void SetCustomBackend (IBackend backend)
61
this.backend = backend;
62
usingCustomBackend = true;
65
public object Parent { get; internal set; }
67
public IBackend Backend {
74
internal bool BackendCreated {
75
get { return backend != null; }
78
protected virtual void OnBackendCreated ()
80
foreach (var ev in DefaultEnabledEvents)
81
Backend.EnableEvent (ev);
84
protected virtual IBackend OnCreateBackend ()
86
Type t = Parent.GetType ();
87
while (t != typeof(object)) {
88
IBackend b = WidgetRegistry.CreateBackend<IBackend> (t);
96
public void EnsureBackendLoaded ()
102
protected void LoadBackend ()
104
if (usingCustomBackend) {
105
usingCustomBackend = false;
106
backend.InitializeBackend (Parent);
109
else if (backend == null) {
110
backend = OnCreateBackend ();
112
throw new InvalidOperationException ("No backend found for object: " + Parent.GetType ());
113
backend.InitializeBackend (Parent);
118
public void OnBeforeEventAdd (object eventId, Delegate eventDelegate)
120
if (eventDelegate == null && !DefaultEnabledEvents.Contains (eventId))
121
Backend.EnableEvent (eventId);
124
public void OnAfterEventRemove (object eventId, Delegate eventDelegate)
126
if (eventDelegate != null && !DefaultEnabledEvents.Contains (eventId))
127
Backend.DisableEvent (eventId);
130
internal HashSet<object> DefaultEnabledEvents {
132
if (defaultEnabledEvents == null)
133
defaultEnabledEvents = EventUtil.GetDefaultEnabledEvents (Parent.GetType ());
134
return defaultEnabledEvents;