/********************************************************************
* FulcrumWeb RAD Framework - Fulcrum of your business *
* Copyright (c) 2002-2009 FulcrumWeb, ALL RIGHTS RESERVED *
* *
* THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED *
* FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE *
* COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE *
* AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT *
* AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE *
* AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS. *
********************************************************************/
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace Framework.Silverlight.Client
{
/// <summary>
/// Provides base features for popup dialogs.
/// </summary>
public partial class CxDialog
{
private int m_sizePercent = 80;
private NxDialogResult m_dialogResult = NxDialogResult.Cancel;
private NxDialogButtons m_buttons = NxDialogButtons.OK;
private NxDialogDefaultButton m_defButton = NxDialogDefaultButton.Button1;
private List<Button> m_btns = new List<Button>();
//----------------------------------------------------------------------------
/// <summary>
/// Ocurs when DialogResult property had change.
/// </summary>
public event DxDialogResultChangedHandler DialogResultChanged;
//----------------------------------------------------------------------------
/// <summary>
/// Gets or sets value that defines how many place(in percents) the dialog will hold.
/// </summary>
public int HoldSizePercent
{
get { return m_sizePercent; }
set { m_sizePercent = value; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the CxDialog class.
/// </summary>
public CxDialog()
{
InitializeComponent();
}
//----------------------------------------------------------------------------
/// <summary>
/// Shows dialog.
/// </summary>
public void ShowDialog()
{
if (CxAppContext.Instance.RootDialogContainer.LoadDialog(this))
{
OnShowing();
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Performs tasks when the dialog is showing.
/// </summary>
protected virtual void OnShowing()
{
foreach (Button button in m_btns)
{
button.Click -= OnDialogButtonClick;
button.Loaded -= button_Loaded;
}
m_btnsStack.Children.Clear();
CreateButtons(m_buttons);
PutButtons(m_btns);
Visibility = Visibility.Visible;
}
//----------------------------------------------------------------------------
/// <summary>
/// Hides dialog.
/// </summary>
public void CloseDialog()
{
CxAppContext.Instance.RootDialogContainer.UnloadDialog();
OnClosing();
}
//----------------------------------------------------------------------------
/// <summary>
/// Performs tasks when the dialog is closing.
/// </summary>
protected virtual void OnClosing()
{
Visibility = Visibility.Collapsed;
}
//----------------------------------------------------------------------------
/// <summary>
/// Raises the DialogResultChanged event.
/// </summary>
/// <param name="e">An CxDialogResultChangedEvrntArgs that contains the event data.</param>
protected virtual void OnDialogResultChanged(CxDialogResultChangedEventArgs e)
{
if (DialogResultChanged != null)
{
DialogResultChanged(this, e);
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Gets the dialog result for the dialog.
/// </summary>
public NxDialogResult DialogResult
{
get { return m_dialogResult; }
protected set { m_dialogResult = value; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Gets or sets value of NxDialogDefaultButton that defines default dialog button.
/// </summary>
public NxDialogDefaultButton DefaultButton
{
get { return m_defButton; }
set { m_defButton = value; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Gets or sets value of NxDialogButtons that defines buttons combination on dialog.
/// </summary>
public NxDialogButtons Buttons
{
get { return m_buttons; }
set { m_buttons = value; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Puts dialog buttons into parent container.
/// </summary>
/// <param name="buttons">List of buttons to put.</param>
protected virtual void PutButtons(IEnumerable<Button> buttons)
{
m_btnsStack.Children.Clear();
foreach (Button button in buttons)
{
m_btnsStack.Children.Add(button);
}
switch (m_defButton)
{
case NxDialogDefaultButton.Button1:
if (m_btns.Count >= 1)
{
m_btns[0].Focus();
}
break;
case NxDialogDefaultButton.Button2:
if (m_btns.Count >= 2)
{
m_btns[1].Focus();
}
break;
case NxDialogDefaultButton.Button3:
if (m_btns.Count >= 3)
{
m_btns[2].Focus();
}
break;
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Creates dialog buttons using specified NxDialogButtons.
/// </summary>
/// <param name="dialogButtons">NxDialogButtons to buttons create.</param>
/// <returns>Created list of dialog buttons.</returns>
protected void CreateButtons(NxDialogButtons dialogButtons)
{
foreach (Button button in m_btns)
{
button.Click -= OnDialogButtonClick;
button.Loaded -= button_Loaded;
}
m_btns.Clear();
//TODO: add multilanguage here
switch (dialogButtons)
{
case NxDialogButtons.OK:
CxDialogButton btnOK = new CxDialogButton
{
DialogResult = NxDialogResult.OK,
Content = "OK",
TabIndex = 0
};
m_btns.Add(btnOK);
break;
case NxDialogButtons.Cancel:
CxDialogButton btnCancelOnly = new CxDialogButton
{
DialogResult = NxDialogResult.Cancel,
Content = "Cancel",
TabIndex = 0
};
m_btns.Add(btnCancelOnly);
break;
case NxDialogButtons.OKCancel:
CxDialogButton btnOK2 = new CxDialogButton
{
DialogResult = NxDialogResult.OK,
Content = "OK",
TabIndex = 0
};
m_btns.Add(btnOK2);
CxDialogButton btnCancel = new CxDialogButton
{
DialogResult = NxDialogResult.Cancel,
Content = "Cancel",
TabIndex = 1
};
m_btns.Add(btnCancel);
break;
case NxDialogButtons.YesNo:
CxDialogButton btnYes = new CxDialogButton
{
DialogResult = NxDialogResult.YES,
Content = "Yes",
TabIndex = 0
};
m_btns.Add(btnYes);
CxDialogButton btnNo = new CxDialogButton
{
DialogResult = NxDialogResult.NO,
Content = "No",
TabIndex = 1
};
m_btns.Add(btnNo);
break;
case NxDialogButtons.YesNoCancel:
CxDialogButton btnYes2 = new CxDialogButton
{
DialogResult = NxDialogResult.YES,
Content = "Yes",
TabIndex = 0
};
m_btns.Add(btnYes2);
CxDialogButton btnNo2 = new CxDialogButton
{
DialogResult = NxDialogResult.NO,
Content = "No",
TabIndex = 1
};
m_btns.Add(btnNo2);
CxDialogButton btnCancel2 = new CxDialogButton
{
DialogResult = NxDialogResult.Cancel,
Content = "Cancel",
TabIndex = 2
};
m_btns.Add(btnCancel2);
break;
default:
m_btns = CreateCustomButtons();
break;
}
foreach (Button button in m_btns)
{
button.Width = 85;
button.Margin = new Thickness(10, 10, 10, 10);
button.Click += OnDialogButtonClick;
button.Loaded += button_Loaded;
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Handles dialog buttond Loaded event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">Instance of container class that contains the event data.</param>
void button_Loaded(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn == null)
{
return;
}
//set focus to default button
if (m_defButton == NxDialogDefaultButton.Button1 && btn.TabIndex == 0)
{
btn.Focus();
}
if (m_defButton == NxDialogDefaultButton.Button2 && btn.TabIndex == 1)
{
btn.Focus();
}
if (m_defButton == NxDialogDefaultButton.Button3 && btn.TabIndex == 2)
{
btn.Focus();
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Handles dialog buttond Click event.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">Instance of container class that contains the event data.</param>
protected virtual void OnDialogButtonClick(object sender, RoutedEventArgs e)
{
CxDialogButton btn = sender as CxDialogButton;
if (btn != null)
{
m_dialogResult = btn.DialogResult;
CloseDialog();
OnDialogResultChanged(new CxDialogResultChangedEventArgs(m_dialogResult));
}
}
//----------------------------------------------------------------------------
/// <summary>
/// Creates custom buttons set.
/// Dialog calls to this method if DefaultButton property set as 'Custom'.
/// </summary>
/// <returns>Created list of dialog buttons.</returns>
protected virtual List<Button> CreateCustomButtons()
{
return new List<Button>();
}
//----------------------------------------------------------------------------
/// <summary>
/// Gets or sets dialog title.
/// </summary>
public string Title
{
get { return m_Title.Text; }
set { m_Title.Text = value; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Gets or sets value that defines dialog size behavior.
/// true - dialog always shows with fixed size; false - dialog size depends on parent window size
/// </summary>
public bool IsFixedSize { get; set; }
//----------------------------------------------------------------------------
/// <summary>
/// Defines if this dialog is handled by user.
/// </summary>
public bool IsHandled { get; set; }
}
//----------------------------------------------------------------------------
/// <summary>
/// Specifies identifiers to indicate the return value of a dialog box.
/// </summary>
public enum NxDialogResult
{
/// <summary>
/// The dialog box return value is Yes (usually sent from a button labeled Yes).
/// </summary>
YES,
/// <summary>
/// The dialog box return value is No (usually sent from a button labeled No).
/// </summary>
NO,
/// <summary>
/// The dialog box return value is Cancel (usually sent from a button labeled Cancel).
/// </summary>
Cancel,
/// <summary>
/// The dialog box return value is OK (usually sent from a button labeled OK).
/// </summary>
OK
}
//----------------------------------------------------------------------------
/// <summary>
/// Specifies constants defining which buttons to display on a Dialog.
/// </summary>
public enum NxDialogButtons
{
/// <summary>
/// The dialog contains an OK button.
/// </summary>
OK,
/// <summary>
/// The dialog contains Cancel button.
/// </summary>
Cancel,
/// <summary>
/// The dialog contains OK and Cancel buttons.
/// </summary>
OKCancel,
/// <summary>
/// The dialog contains Yes and No buttons.
/// </summary>
YesNo,
/// <summary>
/// The dialog contains Yes, No, and Cancel buttons.
/// </summary>
YesNoCancel,
/// <summary>
/// The dialog contains custom buttons set.
/// </summary>
Custom,
}
//----------------------------------------------------------------------------
/// <summary>
/// Specifies constants defining the default button on a Dialog.
/// </summary>
public enum NxDialogDefaultButton
{
/// <summary>
/// The first button on the dialog is the default button.
/// </summary>
Button1,
/// <summary>
/// The second button on the dialog is the default button.
/// </summary>
Button2,
/// <summary>
/// The third button on the dialog is the default button.
/// </summary>
Button3,
}
}