// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.Windows.Forms; namespace ICSharpCode.Core.WinForms { public class ToolBarCheckBox : ToolStripButton , IStatusUpdate { object caller; Codon codon; string description = String.Empty; ICheckableMenuCommand menuCommand = null; IEnumerable conditions; public ICheckableMenuCommand MenuCommand { get { return menuCommand; } } public object Caller { get { return caller; } } public string Description { get { return description; } set { description = value; } } public ToolBarCheckBox(string text) { this.RightToLeft = RightToLeft.Inherit; Text = text; } public ToolBarCheckBox(Codon codon, object caller, IEnumerable conditions) { this.RightToLeft = RightToLeft.Inherit; this.caller = caller; this.codon = codon; this.conditions = conditions; try { menuCommand = (ICheckableMenuCommand)codon.AddIn.CreateObject(codon.Properties["class"]); } catch (Exception) { } if (menuCommand == null) { MessageService.ShowError("Can't create toolbar checkbox : " + codon.Id); } menuCommand.Owner = this; if (codon.Properties.Contains("label")){ Text = StringParser.Parse(codon.Properties["label"]); } if (Image == null && codon.Properties.Contains("icon")) { Image = WinFormsResourceService.GetBitmap(StringParser.Parse(codon.Properties["icon"])); } UpdateText(); UpdateStatus(); } protected override void OnClick(EventArgs e) { base.OnClick(e); if (menuCommand != null) { menuCommand.Run(); Checked = menuCommand.IsChecked; } } public override bool Enabled { get { if (codon == null) { return base.Enabled; } ConditionFailedAction failedAction = Condition.GetFailedAction(conditions, caller); return failedAction != ConditionFailedAction.Disable; } } public virtual void UpdateStatus() { if (codon != null) { ConditionFailedAction failedAction = Condition.GetFailedAction(conditions, caller); bool isVisible = failedAction != ConditionFailedAction.Exclude; if (isVisible != Visible) Visible = isVisible; if (menuCommand != null) { bool isChecked = menuCommand.IsChecked; if (isChecked != Checked) Checked = isChecked; } if (this.Visible && codon.Properties.Contains("icon")) { Image = WinFormsResourceService.GetBitmap(StringParser.Parse(codon.Properties["icon"])); } } } public virtual void UpdateText() { if (codon != null) { if (codon.Properties.Contains("tooltip")) { ToolTipText = StringParser.Parse(codon.Properties["tooltip"]); } if (codon.Properties.Contains("label")){ Text = StringParser.Parse(codon.Properties["label"]); } } } } }