2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // $Id: FriendshipOfferNotification.cs 118 2009-07-20 00:39:00Z latifer $
32 using System.Collections.Generic;
33 #if (COGBOT_LIBOMV || USE_STHREADS)
35 using Thread = ThreadPoolUtil.Thread;
36 using ThreadPool = ThreadPoolUtil.ThreadPool;
37 using Monitor = ThreadPoolUtil.Monitor;
41 using System.Windows.Forms;
47 /// Base class for all notificatiosn (blue dialogs)
49 public class Notification : UserControl
54 public NotificationType Type;
57 /// Callback when blue dialog notification is displayed or closed
59 /// <param name="sender">Notification dialog</param>
60 /// <param name="e">Notification parameters</param>
61 public delegate void NotificationCallback(object sender, NotificationEventArgs e);
64 /// Callback when blue dialog notification button is clicked
66 /// <param name="sender">Notification dialog</param>
67 /// <param name="e">Notification parameters</param>
68 public delegate void NotificationClickedCallback(object sender, EventArgs e, NotificationEventArgs notice);
71 /// Fired when a notification is displayed
73 public static event NotificationCallback OnNotificationDisplayed;
77 Type = NotificationType.Generic;
80 public Notification(NotificationType type)
85 protected void FireNotificationCallback(NotificationEventArgs e)
87 if (OnNotificationDisplayed == null) return;
91 WorkPool.QueueUserWorkItem((object o) => Notificaton_Displayed(this, e));
95 Console.WriteLine("" + ex);
96 OpenMetaverse.Logger.Log("Error executing notification callback", OpenMetaverse.Helpers.LogLevel.Warning, ex);
100 private void Notificaton_Displayed(Notification notification, NotificationEventArgs e)
104 e.HookNotification(this);
105 if (OnNotificationDisplayed != null)
106 OnNotificationDisplayed(notification, e);
110 Console.WriteLine("" + ex);
111 OpenMetaverse.Logger.Log("Error executing notification displayed", OpenMetaverse.Helpers.LogLevel.Warning, ex);
116 /// What kind of notification this is (blue dialog)
118 public enum NotificationType
134 /// Fired when blue dialog notification is displayed
136 public class NotificationEventArgs : EventArgs, IDisposable
138 public event Notification.NotificationCallback OnNotificationClosed;
140 public event Notification.NotificationClickedCallback OnNotificationClicked;
143 /// The Notfication form itself
145 public Notification Notice;
148 /// Type of notfication
150 public NotificationType Type;
153 /// Instance of Radegast where the event occured
155 public RadegastInstance Instance;
158 /// Notification text
160 public string Text = string.Empty;
163 /// Buttons displayed on the notification window
165 public List<Button> Buttons = new List<Button>();
168 /// When set true the Dialog Can send the Close Event
170 public bool CanClose = false;
173 /// The button has been pushed once
175 public bool ButtonSelected = false;
178 /// Create new event args object
180 /// <param name="instance">Instance of Radegast notification is coming from</param>
181 public NotificationEventArgs(RadegastInstance instance)
186 private void Notification_Closing(object sender, EventArgs e)
188 Notification_Close();
192 /// Triggers the OnNotificationClosing event.
194 internal void Notification_Close()
200 if (OnNotificationClosed != null)
201 OnNotificationClosed(this, this);
205 OpenMetaverse.Logger.Log("Error executing OnNotificationClosed " + Text,
206 OpenMetaverse.Helpers.LogLevel.Warning, ex);
208 if (!CanClose) Dispose();
214 /// Triggers the OnNotificationClicked event.
216 internal void Notification_Click(object sender, EventArgs e)
218 if (ButtonSelected) throw new InvalidOperationException("Clicked twice " + Text + " item: " + sender);
219 ButtonSelected = true;
222 if (OnNotificationClicked != null)
223 OnNotificationClicked(sender, e, this);
227 OpenMetaverse.Logger.Log("Error executing OnNotificationClicked", OpenMetaverse.Helpers.LogLevel.Warning, ex);
231 Notification_Close();
235 public void HookNotification(Notification notification)
237 Notice = notification;
241 notification.HandleDestroyed += Notification_Closing;
243 foreach (var button in Buttons)
245 button.Click += Notification_Click;
250 throw new InvalidOperationException("No buttons found on Dialog " + Text);
254 public void Dispose()
259 ButtonSelected = true;
260 Notification_Close();
264 Notice.HandleDestroyed -= Notification_Closing;
266 foreach (var button in Buttons)
267 button.Click -= Notification_Click;