Files
JRCookbook/JRCookbookControls/JRCookbookEditTip.cs
2026-03-07 19:22:22 -06:00

248 lines
7.8 KiB
C#

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 JRCookbookEditTip : UserControl
{
private RecipeTip _recipeTip;
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 JRCookbookEditTip()
{
InitializeComponent();
LoadImages();
}
public JRCookbookEditTip(RecipeTip TipToEdit)
{
InitializeComponent();
LoadImages();
recipeTip = TipToEdit;
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public RecipeTip recipeTip
{
get
{
return _recipeTip;
}
set
{
_recipeTip = 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 (_recipeTip == null)
{
txtTipText.Text = string.Empty;
txtTipText.Font = new Font(txtTipText.Font, FontStyle.Regular);
btnHeading.ImageKey = NOT_HEADING_IMAGE;
}
else
{
txtTipText.Text = _recipeTip.tipText;
if (_recipeTip.isHeading)
{
txtTipText.Font = new Font(txtTipText.Font, FontStyle.Bold);
btnHeading.ImageKey = HEADING_IMAGE;
}
else
{
txtTipText.Font = new Font(txtTipText.Font, FontStyle.Regular);
btnHeading.ImageKey = NOT_HEADING_IMAGE;
}
}
}
private void btnHeading_Click(object sender, EventArgs e)
{
if(_recipeTip != null)
{
_recipeTip.isHeading = !_recipeTip.isHeading;
}
LoadControlFromObject();
}
private void txtTipText_TextChanged(object sender, EventArgs e)
{
_recipeTip.tipText = txtTipText.Text;
CheckForData();
}
private void CheckForData()
{
var blnNewIsEmpty = true;
if (txtTipText.Text.Trim() != String.Empty)
{
blnNewIsEmpty = false;
}
if (_recipeTip.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;
}
}
}