#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
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