public static bool Merge(ToolStrip sourceToolStrip, ToolStrip targetToolStrip)
 {
     if (sourceToolStrip == null)
     {
         throw new ArgumentNullException("sourceToolStrip");
     }
     if (targetToolStrip == null)
     {
         throw new ArgumentNullException("targetToolStrip");
     }
     if (targetToolStrip == sourceToolStrip)
     {
         throw new ArgumentException(System.Windows.Forms.SR.GetString("ToolStripMergeImpossibleIdentical"));
     }
     bool flag = IsSpecialMDIStrip(sourceToolStrip) || ((sourceToolStrip.AllowMerge && targetToolStrip.AllowMerge) && (sourceToolStrip.GetType().IsAssignableFrom(targetToolStrip.GetType()) || targetToolStrip.GetType().IsAssignableFrom(sourceToolStrip.GetType())));
     MergeHistory history = null;
     if (flag)
     {
         history = new MergeHistory(sourceToolStrip);
         int count = sourceToolStrip.Items.Count;
         if (count > 0)
         {
             sourceToolStrip.SuspendLayout();
             targetToolStrip.SuspendLayout();
             try
             {
                 int num2 = count;
                 int num3 = 0;
                 int num4 = 0;
                 while (num3 < count)
                 {
                     ToolStripItem source = sourceToolStrip.Items[num4];
                     MergeRecursive(source, targetToolStrip.Items, history.MergeHistoryItemsStack);
                     int num5 = num2 - sourceToolStrip.Items.Count;
                     num4 = (num5 > 0) ? num4 : (num4 + 1);
                     num2 = sourceToolStrip.Items.Count;
                     num3++;
                 }
             }
             finally
             {
                 sourceToolStrip.ResumeLayout();
                 targetToolStrip.ResumeLayout();
             }
             if (history.MergeHistoryItemsStack.Count > 0)
             {
                 targetToolStrip.MergeHistoryStack.Push(history);
             }
         }
     }
     bool flag2 = false;
     if ((history != null) && (history.MergeHistoryItemsStack.Count > 0))
     {
         flag2 = true;
     }
     return flag2;
 }
예제 #2
0
        /// <include file='doc\ToolStripManager.uex' path='docs/doc[@for="ToolStripManager.Merge"]/*' />
        /// <devdoc> 
        /// merge two toolstrips
        /// </devdoc>
        public static bool Merge(ToolStrip sourceToolStrip, ToolStrip targetToolStrip) {
            // check arguments
            if (sourceToolStrip == null) {
                throw new ArgumentNullException("sourceToolStrip");
            }
            if (targetToolStrip == null) {
                throw new ArgumentNullException("targetToolStrip");
            }
            if (targetToolStrip == sourceToolStrip) {
                throw new ArgumentException(SR.GetString(SR.ToolStripMergeImpossibleIdentical));
            }

            // we only do this if the source and target toolstrips are the same
            bool canMerge = IsSpecialMDIStrip(sourceToolStrip);
            canMerge = (canMerge || (sourceToolStrip.AllowMerge &&
                                      targetToolStrip.AllowMerge &&
                                      (sourceToolStrip.GetType().IsAssignableFrom(targetToolStrip.GetType()) || targetToolStrip.GetType().IsAssignableFrom(sourceToolStrip.GetType()))
                                    )
                        );
            MergeHistory mergeHistory = null;
            if (canMerge) {
                //Debug.WriteLine("Begin merge between src: " + sourceToolStrip.Name + " and target: " + targetToolStrip.Name);
                Debug.Indent();
                mergeHistory = new MergeHistory(sourceToolStrip);


                int originalCount = sourceToolStrip.Items.Count;

                if (originalCount > 0) {
                    sourceToolStrip.SuspendLayout();
                    targetToolStrip.SuspendLayout();
                    try {
                        int lastCount = originalCount;

                        // 2. do the actual merging logic
                        for (int i = 0, itemToLookAt = 0; i < originalCount; i++) {
                            ToolStripItem item = sourceToolStrip.Items[itemToLookAt];
                            //Debug.WriteLine("doing the recursive merge for item " + item.Text);
                            MergeRecursive(item, targetToolStrip.Items, mergeHistory.MergeHistoryItemsStack);

                            int numberOfItemsMerged = lastCount - sourceToolStrip.Items.Count;
                            itemToLookAt = (numberOfItemsMerged > 0) ? itemToLookAt : itemToLookAt + 1;
                            lastCount = sourceToolStrip.Items.Count;
                        }
                    }
                    finally {
                        Debug.Unindent();
                        sourceToolStrip.ResumeLayout();
                        targetToolStrip.ResumeLayout();
                    }
                    //Debug.WriteLine("pusing mergehistory for toolstrip " + sourceToolStrip.Name + " in target toolstrip MergeHistoryStack property");
                    if (mergeHistory.MergeHistoryItemsStack.Count > 0) {
                        // only push this on the stack if we actually did something
                        targetToolStrip.MergeHistoryStack.Push(mergeHistory);
                    }
                }
            }
            bool result = false;
            if (mergeHistory != null && mergeHistory.MergeHistoryItemsStack.Count > 0) {
                result = true; // we did merge something
            }
            return result;
        }