public void lnkShare_Click(object sender, EventArgs e) { if (m_Style.IsShared) { // The link is un-sharing. need to detect if any buttons except this one use this style m_Button.SetStyleObject(null); DatumList list = new DatumList(); Globals.Root.CurrentDocument.IterateEx(obj => obj.AddRequiredReferences(list.Add, Mapping.Ignore), false, false); m_Button.SetStyleObject(m_OriginalStyle); // either way we put the object back in the button itself in case the user cancels this screen if (list.Contains(m_Style) || m_Style.IsUserDefault) { // this style is referenced elsewhere. Need to make a copy ButtonStyle newStyle = (ButtonStyle)m_Style.Clone(Mapping.Ignore); newStyle.Name = ""; newStyle.IsShared = false; m_Style = newStyle; } else { // this style is not used elsewhere; we can just make it shared m_Style.IsShared = false; Globals.Root.CurrentDocument.RemoveButtonStyle(m_Style); // can keep the name } } else // otherwise we are sharing a previously custom style { string name = frmButtonStyleName.Display(m_Style); // Name will only be set if this was previously shared if (string.IsNullOrEmpty(name)) { return; } m_Style.IsShared = true; m_Style.Name = name; m_Transaction.Edit(Globals.Root.CurrentDocument); Globals.Root.CurrentDocument.AddButtonStyle(m_Style); } FillStylesList(); ReflectCustomState(); ctrStyle.DisplayStyle(m_Style); }
/// <summary>The style to use for a newly created button. The page can be specified which will use the most prevalent style on the page in preference</summary> internal static ButtonStyle GetStyleForNewButton(Page page) { ButtonStyle mostFrequent = (from styleInfo in (from shape in page where shape.ShapeCode == Shape.Shapes.Button group shape by((ButtonShape)shape).BackgroundStyleObject into g select new { Style = g.Key, Appearances = g.Count() }) orderby styleInfo.Appearances descending select styleInfo.Style).FirstOrDefault(); if (mostFrequent != null && mostFrequent.IsShared) // If not shared it should be only one occurrence anyway! { return(mostFrequent); } // Uses the last one which was accepted (cloned if necessary): ButtonStyle style; if (m_LastUsedStyle == null) { style = ButtonStyle.UserDefaultActionInstance; } else { if (m_LastUsedStyle.IsShared && m_LastUsedDocument == Globals.Root.CurrentDocument) { // can reuse the last one; it is shared style = m_LastUsedStyle; } else { // want to use the last one, but need to make a copy of it style = (ButtonStyle)m_LastUsedStyle.Clone(Mapping.Ignore); Debug.Assert(style.IsShared == false); } } return(style); }