/// <summary>
 /// Called to draw an item in our plugins list box. We have to do this by
 /// hand to be able to support disabled items.
 /// </summary>
 /// <param name="sender">Event sender.</param>
 /// <param name="e">Event arguments.</param>
 private void PipelinePluginsLst_DrawItem(object sender, DrawItemEventArgs e)
 {
     // The code to do this has been ripped off MSDN and adapted.
     // Open help and look at the code example for the DrawItemEventArgs class.
     if (listItemRegularBrush != null)
     {
         PluginToImportDisplayInfo pluginDisplayInfo = (PluginToImportDisplayInfo)PipelinePluginsLst.Items[e.Index];
         Brush foreBrush = listItemRegularBrush;
         if (PipelinePluginsLst.SelectedIndices.Contains(e.Index))
         {
             foreBrush = listItemSelectedBrush;
         }
         else if (!pluginDisplayInfo.Importable)
         {
             foreBrush = listItemDisabledBrush;
         }
         e.DrawBackground();
         e.Graphics.DrawString(PipelinePluginsLst.Items[e.Index].ToString(), e.Font, foreBrush,
                               e.Bounds, StringFormat.GenericDefault);
         e.DrawFocusRectangle();
     }
 }
        /// <summary>
        /// Shows the form as a modal dialog, asking the user which plugins among
        /// the given list he/she wants to import.
        /// </summary>
        /// <param name="owner">Owner window of the dialog. Can be <c>null</c>.</param>
        /// <param name="pluginCollection">List of plugins available for importing.
        /// Upon exit, if the method returns <c>true</c>, the list will be modified
        /// to only contain those plugins selected by the user.</param>
        /// <param name="pluginOverwrites">Container of information about plugins
        /// overwriting existing ones. Upon exit, if the method returns <c>true</c>,
        /// the list will be modified to only contain plugins that are also in
        /// <paramref name="pluginCollection"/>.</param>
        /// <returns><c>true</c> if the user pressed OK to import selected
        /// plugins.</returns>
        public bool SelectPlugins(IWin32Window owner, ref PipelinePluginCollection pluginCollection,
                                  ref ImportedPipelinePluginOverwrites pluginOverwrites)
        {
            Debug.Assert(pluginCollection != null);
            Debug.Assert(pluginOverwrites != null);

            PipelinePluginsLst.BeginUpdate();
            try {
                // Populate the list with given plugins.
                foreach (PipelinePluginInfo pluginInfo in pluginCollection.Plugins)
                {
                    PipelinePluginsLst.Items.Add(new PluginToImportDisplayInfo {
                        PluginInfo = pluginInfo,
                        Importable = true,
                    });
                }

                // Disable any plugin that would overwrite a global one set by the administrator.
                for (int i = 0; i < PipelinePluginsLst.Items.Count; ++i)
                {
                    PluginToImportDisplayInfo newPlugin = (PluginToImportDisplayInfo)PipelinePluginsLst.Items[i];
                    ImportedPipelinePluginOverwrites.OverwriteInfo overwriteInfo;
                    if (pluginOverwrites.OverwriteInfos.TryGetValue(newPlugin.PluginInfo, out overwriteInfo) && overwriteInfo.OldInfo.Global)
                    {
                        newPlugin.Importable = false;
                    }
                }

                // Default is to want to select them all importable, compatible plugins.
                reselecting = true;
                try {
                    for (int i = 0; i < PipelinePluginsLst.Items.Count; ++i)
                    {
                        PluginToImportDisplayInfo newPlugin = (PluginToImportDisplayInfo)PipelinePluginsLst.Items[i];
                        if (newPlugin.Importable && newPlugin.Compatible)
                        {
                            PipelinePluginsLst.SetSelected(i, true);
                        }
                    }
                } finally {
                    reselecting = false;
                }

                // Update buttons to match the initial selection state.
                UpdateButtons();
            } finally {
                PipelinePluginsLst.EndUpdate();
            }

            // Show dialog to allow user to select plugins.
            bool res = ShowDialog(owner) == DialogResult.OK;

            if (res)
            {
                // Rebuild list of plugins in the collection.
                pluginCollection.Plugins.Clear();
                foreach (var item in PipelinePluginsLst.SelectedItems)
                {
                    pluginCollection.Plugins.Add(((PluginToImportDisplayInfo)item).PluginInfo);
                }

                // Also rebuild the list of overwrites to only include those we included in the collection.
                ImportedPipelinePluginOverwrites newOverwrites = new ImportedPipelinePluginOverwrites();
                foreach (var pluginInfo in pluginCollection.Plugins)
                {
                    ImportedPipelinePluginOverwrites.OverwriteInfo overwriteInfo;
                    if (pluginOverwrites.OverwriteInfos.TryGetValue(pluginInfo, out overwriteInfo))
                    {
                        newOverwrites.OverwriteInfos.Add(pluginInfo, overwriteInfo);
                    }
                }
                pluginOverwrites = newOverwrites;
            }
            return(res);
        }