#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 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 GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion Disclaimer / License
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Maestro.Shared.UI
{
///
/// The base class of all view content. Provides the default implementation of
///
[ToolboxItem(false)]
public partial class ViewContentBase : UserControl, IViewContent
{
///
/// Initializes a new instance of the class.
///
public ViewContentBase()
{
InitializeComponent();
}
///
/// Gets whether this instance is a modal window
///
public virtual bool IsModalWindow => false;
///
/// Gets whether this instance can only be docked to the document region
///
public virtual bool IsExclusiveToDocumentRegion => false;
private string _title;
///
/// The title of the view
///
public string Title
{
get
{
return _title;
}
set
{
if (_title != value)
{
_title = value;
this.TitleChanged?.Invoke(this, EventArgs.Empty);
}
}
}
private Form _parent;
///
/// Sets the parent form for this instance
///
///
public void SetParentForm(Form form)
{
if (_parent != null)
throw new InvalidOperationException("Parent form already set");
_parent = form;
_parent.FormClosing += new FormClosingEventHandler(OnParentFormClosing);
_parent.FormClosing += new FormClosingEventHandler(OnParentFormClosed);
}
private void OnParentFormClosed(object sender, FormClosingEventArgs e) => this.ViewContentClosed?.Invoke(this, EventArgs.Empty);
private void OnParentFormClosing(object sender, FormClosingEventArgs e) => e.Cancel = CheckCancelEvents();
///
/// Fires when the title has been changed
///
public event EventHandler TitleChanged;
///
/// Detrmines if this view can be closed by the user, note that this does not affect the method
/// in any way. All view content can still be programmatically closed if they inherit from and
/// does not override the default implementation of
///
public virtual bool AllowUserClose => true;
internal bool CheckCancelEvents()
{
CancelEventArgs ce = new CancelEventArgs(false);
this.ViewContentClosing?.Invoke(this, ce);
return ce.Cancel;
}
///
/// Closes the view. This raises the event
///
public virtual void Close() => _parent?.Close();
///
/// Fired when the view has been closed internally
///
public event CancelEventHandler ViewContentClosing;
///
/// Displays an exception message
///
/// The exception object
public void ShowError(Exception ex) => ErrorDialog.Show(ex);
///
/// Displays an error message
///
/// The message
public virtual void ShowError(string message) => ErrorDialog.Show(message, message);
///
/// Displays an alert message
///
/// The title of this message
/// The message
public virtual void ShowMessage(string title, string message) => MessageBox.Show(message, title);
///
/// Make a request for confirmation
///
/// The title of the confirmation message
/// The message
///
/// true if confirmed, false otherwise
///
public virtual bool Confirm(string title, string message)
=> MessageBox.Show(message, title, MessageBoxButtons.YesNo) == DialogResult.Yes;
///
/// Make a request for confirmation
///
/// The title of the confirmation message
/// The message template
/// The template values
///
/// true if confirmed, false otherwise
///
public virtual bool ConfirmFormatted(string title, string format, params string[] args)
=> MessageBox.Show(string.Format(format, args), title, MessageBoxButtons.YesNo) == DialogResult.Yes;
///
/// The underlying control
///
public Control ContentControl
{
get { return this; }
}
private string _description;
///
/// The view's description, this is the ToolTip content
///
public string Description
{
get
{
return _description;
}
set
{
if (_description != value)
{
_description = value;
this.DescriptionChanged?.Invoke(this, EventArgs.Empty);
}
}
}
///
/// Raised when the description has changed
///
public event EventHandler DescriptionChanged;
///
/// Makes this content active
///
public void Activate() => this.ViewContentActivating?.Invoke(this, EventArgs.Empty);
///
/// Fired when the view is going to hide
///
public event EventHandler ViewContentHiding;
///
/// Fired when the view is activating
///
public event EventHandler ViewContentActivating;
///
/// Conceals the control from the user.
///
///
///
///
///
///
///
void IViewContent.Hide() => this.ViewContentHiding?.Invoke(this, EventArgs.Empty);
///
/// Indicates whether this view is attached to a workbench
///
public bool IsAttached
{
get;
internal set;
}
///
/// Indicates the default region this view content will be put in
///
public virtual ViewRegion DefaultRegion => ViewRegion.Document;
///
/// Gets the icon for this view
///
public virtual Icon ViewIcon => null;
///
/// Fired when the view has been closed internally
///
public event EventHandler ViewContentClosed;
///
/// Fired when the view, which was hidden is now being shown
///
public event EventHandler ViewContentShowing;
}
}