#region CPL License /* Nuclex Framework Copyright (C) 2002-2019 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Windows.Forms; namespace Nuclex.Windows.Forms.Messages { /// Uses task dialogs to display message boxes public class StandardMessageBoxManager : IMessageService { #region class MessageScope /// Triggers the message displayed and acknowledged events private class MessageScope : IDisposable { /// /// Initializes a new message scope, triggering the message displayed event /// /// Message service the scope belongs to /// Image of the message being displayed /// Text contained in the message being displayed public MessageScope( StandardMessageBoxManager self, MessageBoxIcon image, MessageText text ) { EventHandler messageDisplayed = self.MessageDisplaying; if(messageDisplayed != null) { messageDisplayed(this, new MessageEventArgs(image, text)); } this.self = self; } /// Triggers the message acknowledged event public void Dispose() { EventHandler messageAcknowledged = self.MessageAcknowledged; if(messageAcknowledged != null) { messageAcknowledged(this, EventArgs.Empty); } } /// Message service the scope belongs to private StandardMessageBoxManager self; } #endregion // class MessageScope /// Delegate for the standard message box show function /// Window that will modally display the message box /// Text that will be presented to the user /// Contents of the message box' title bar /// Buttons available for the user to choose from /// Icon that will be displayed next to the text /// The choice made by the user if multiple buttons were provided private delegate DialogResult ShowMessageBoxDelegate( IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon ); /// Triggered when a message is displayed to the user public event EventHandler MessageDisplaying; /// Triggered when the user has acknowledged the current message public event EventHandler MessageAcknowledged; /// Initializes a new task dialog message service public StandardMessageBoxManager() : this(NullActiveWindowTracker.Default) { } /// Initializes a new task dialog message service /// /// Active window tracker used to obtain the parent window for message boxes /// public StandardMessageBoxManager(IActiveWindowTracker tracker) { this.tracker = tracker; this.showMessageDelegate = new ShowMessageBoxDelegate(MessageBox.Show); } /// Asks the user a question that can be answered via several buttons /// Image that will be shown on the message box /// Text that will be shown to the user /// Buttons available for the user to click on /// The button the user has clicked on public DialogResult ShowQuestion( MessageBoxIcon image, MessageText text, MessageBoxButtons buttons ) { using(var scope = new MessageScope(this, image, text)) { return showMessageBoxInActiveUiThread( text.Message, text.Caption, buttons, image ); } } /// Displays a notification to the user /// Image that will be shown on the message bx /// Text that will be shown to the user public void ShowNotification(MessageBoxIcon image, MessageText text) { using(var scope = new MessageScope(this, image, text)) { showMessageBoxInActiveUiThread( text.Message, text.Caption, MessageBoxButtons.OK, image ); } } /// Displays the message box in the active view's thread /// Text that will be presented to the user /// Contents of the message box' title bar /// Buttons available for the user to choose from /// Image that will be displayed next to the text /// private DialogResult showMessageBoxInActiveUiThread( string message, string caption, MessageBoxButtons buttons, MessageBoxIcon image ) { Form mainWindow = this.tracker.ActiveWindow; if(mainWindow != null) { return (DialogResult)mainWindow.Invoke( this.showMessageDelegate, (IWin32Window)mainWindow, message, caption, buttons, image ); } // No window tracker or unknown main window -- just show the message box return MessageBox.Show(message, caption, buttons, image); } /// Provides the currently active top-level window private IActiveWindowTracker tracker; /// Delegate for the MessageBox.Show() method private ShowMessageBoxDelegate showMessageDelegate; } } // namespace Nuclex.Windows.Forms.Messages