using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using JRCookbookBusiness; namespace JRCookbookControls { public partial class JRCookbookEditProcedure : UserControl { private RecipeProcedure _recipeProcedure; private bool _DeleteAllowed = true; private bool _IsEmptyValue = true; public event EventHandler DeleteRequested; public event EventHandler IsEmpty_Changed; private static ImageList _allButtonImagesList = new ImageList(); private static ImageList _allHandleImagesList = new ImageList(); private static bool _ImagesAlreadyLoaded = false; private static Point _HandleLocationPoint = new Point(0, 0); private static bool _isHandleLocationSet = false; private const string HEADING_IMAGE = "Heading.bmp"; private const string NOT_HEADING_IMAGE = "NotHeading.bmp"; private const string DELETE_IMAGE = "Delete.bmp"; private const string HANDLE_IMAGE = "Handle.bmp"; public JRCookbookEditProcedure() { InitializeComponent(); LoadImages(); } public JRCookbookEditProcedure(RecipeProcedure procedureToEdit) { InitializeComponent(); LoadImages(); recipeProcedure = procedureToEdit; } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public RecipeProcedure recipeProcedure { get { return _recipeProcedure; } set { _recipeProcedure = value; LoadControlFromObject(); } } public bool IsEmpty { get { return _IsEmptyValue; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public bool DeleteAllowed { get { return _DeleteAllowed; } set { _DeleteAllowed = value; btnDelete.Enabled = _DeleteAllowed; } } private void btnDelete_Click(object sender, EventArgs e) { if (this.DeleteRequested != null) this.DeleteRequested(this, new EventArgs()); } private void LoadControlFromObject() { if (_recipeProcedure == null) { txtProcedureText.Text = string.Empty; txtProcedureText.Font = new Font(txtProcedureText.Font, FontStyle.Regular); btnHeading.ImageKey = NOT_HEADING_IMAGE; } else { txtProcedureText.Text = _recipeProcedure.procedureText; if (_recipeProcedure.isHeading) { txtProcedureText.Font = new Font(txtProcedureText.Font, FontStyle.Bold); btnHeading.ImageKey = HEADING_IMAGE; } else { txtProcedureText.Font = new Font(txtProcedureText.Font, FontStyle.Regular); btnHeading.ImageKey = NOT_HEADING_IMAGE; } } } private void btnHeading_Click(object sender, EventArgs e) { if (_recipeProcedure != null) { _recipeProcedure.isHeading = !_recipeProcedure.isHeading; } LoadControlFromObject(); } private void txtProcedureText_TextChanged(object sender, EventArgs e) { _recipeProcedure.procedureText = txtProcedureText.Text; CheckForData(); } private void CheckForData() { var blnNewIsEmpty = true; if (txtProcedureText.Text.Trim() != String.Empty) { blnNewIsEmpty = false; } if (_recipeProcedure.isHeading) { btnHeading.ImageKey = HEADING_IMAGE; } else { btnHeading.ImageKey = NOT_HEADING_IMAGE; } if (blnNewIsEmpty != _IsEmptyValue) { _IsEmptyValue = blnNewIsEmpty; if (blnNewIsEmpty) { btnDelete.Visible = false; } else { btnDelete.Visible = true; } if (this.IsEmpty_Changed != null) this.IsEmpty_Changed(this, new EventArgs()); } } public void ShowInsertionBar(JRCookbookControls.InsertLocation insertLocation) { modSharedRoutines.ShowInsertionBar(this, insertLocation); } public void HideInsertionBar() { modSharedRoutines.HideInsertionBar(this); } public void DragStarted() { modSharedRoutines.DragStarted(this); } public void DragEnded() { modSharedRoutines.DragEnded(this); } protected override void OnPaint(PaintEventArgs e) { // If there is an image and it has a location, // paint it when the Form is repainted. base.OnPaint(e); Image myBackgroundImage = _allHandleImagesList.Images[HANDLE_IMAGE]; if (!_isHandleLocationSet) { _isHandleLocationSet = true; int verticalMidpointImage = myBackgroundImage.Height / 2; int verticalMidpointButton = btnHeading.Height / 2; int newTop = verticalMidpointButton - verticalMidpointImage + btnHeading.Top; _HandleLocationPoint = new Point(0, newTop); } e.Graphics.DrawImage(myBackgroundImage, _HandleLocationPoint.X, _HandleLocationPoint.Y, new Rectangle(0, 0, myBackgroundImage.Width, myBackgroundImage.Height), GraphicsUnit.Pixel); } private void LoadImages() { if (!_ImagesAlreadyLoaded) { _allButtonImagesList = new ImageList(); _allButtonImagesList.ImageSize = btnDelete.ClientSize; _allButtonImagesList.Images.Clear(); _allButtonImagesList.Images.Add(Properties.Resources.Delete); _allButtonImagesList.Images.Add(Properties.Resources.Heading); _allButtonImagesList.Images.Add(Properties.Resources.NotHeading); _allButtonImagesList.Images.SetKeyName(0, DELETE_IMAGE); _allButtonImagesList.Images.SetKeyName(1, HEADING_IMAGE); _allButtonImagesList.Images.SetKeyName(2, NOT_HEADING_IMAGE); _allHandleImagesList = new ImageList(); double handleWidthToHeightRatio = (double)(Properties.Resources.Handle.Width) / (double)(Properties.Resources.Handle.Height); int handleWidth = (int)((double)(btnHeading.Left) * 0.75); int handleHeight = (int)((double)(handleWidth) / handleWidthToHeightRatio); _allHandleImagesList.ImageSize = new Size(handleWidth, handleHeight); _allHandleImagesList.Images.Clear(); _allHandleImagesList.Images.Add(Properties.Resources.Handle); _allHandleImagesList.Images.SetKeyName(0, HANDLE_IMAGE); _ImagesAlreadyLoaded = true; } this.btnDelete.ImageList = _allButtonImagesList; this.btnDelete.ImageKey = DELETE_IMAGE; this.btnHeading.ImageList = _allButtonImagesList; this.btnHeading.ImageKey = NOT_HEADING_IMAGE; } } }