// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net)
// All rights reserved.
//
// This file is part of Regionerate.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
using System.Collections.Generic;
using System.Reflection;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using stdole;
namespace Rauchy.Regionerate.Presentation.Addins.VisualStudio2005
{
public class ButtonManager : IDisposable
{
#region Fields (2)
private readonly IList _allocatedButtons;
private readonly IList _allocatedEvents;
#endregion Fields
#region Constructors (1)
///
/// Initializes a new instance of the class.
///
/// The application object.
public ButtonManager()
{
_allocatedButtons = new List();
_allocatedEvents = new List();
}
#endregion Constructors
#region Delegates and Events (6)
// Events (6)
// [rgn] Events (6)
private event EventHandler _onCodeWindowButtonClick;
private event EventHandler _onProjectButtonClick;
private event EventHandler _onSolutionButtonClick;
public event EventHandler OnCodeWindowButtonClick
{
add
{
_onCodeWindowButtonClick += value;
}
remove
{
_onCodeWindowButtonClick -= value;
}
}
public event EventHandler OnProjectButtonClick
{
add
{
_onProjectButtonClick += value;
}
remove
{
_onProjectButtonClick -= value;
}
}
public event EventHandler OnSolutionButtonClick
{
add
{
_onSolutionButtonClick += value;
}
remove
{
_onSolutionButtonClick -= value;
}
}
#endregion Delegates and Events
#region Methods (10)
// Public Methods (2)
// [rgn] Public Methods (2)
///
/// Creates the code window, solution & folder buttons.
///
public void CreateButtons( DTE2 applicationObject )
{
CreateCodeWindowButton( applicationObject );
CreateSolutionButton( applicationObject );
CreateProjectButton( applicationObject );
}
///
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
///2
public void Dispose()
{
foreach ( CommandBarButton button in _allocatedButtons )
{
button.Delete( false );
}
}
// Private Methods (8)
// [rgn] Private Methods (8)
private void codeWindow_Click( object CommandBarControl, ref bool Handled, ref bool CancelDefault )
{
if ( _onCodeWindowButtonClick != null )
{
Handled = true;
_onCodeWindowButtonClick( CommandBarControl, null );
}
}
private void CreateCodeWindowButton( DTE2 applicationObject )
{
CommandBarButton button = CreateCommandBarButton( applicationObject, "Code Window" );
_allocatedButtons.Add( button );
RegisterClickEvent( applicationObject, button, codeWindow_Click );
}
///
/// Creates and adds a to a specific .
///
/// The application object.
/// The name of the targe to add the button to.
///
private static CommandBarButton CreateCommandBarButton( DTE2 applicationObject, string target )
{
CommandBars commandBars = ( CommandBars )applicationObject.CommandBars;
CommandBar commandBar = commandBars[ target ];
// Add a command button to the Code Window command bar
CommandBarButton button =
( CommandBarButton )
commandBar.Controls.Add( MsoControlType.msoControlButton, Missing.Value, Missing.Value, 1, true );
button.Caption = "Regionerate this";
//using ( ImageToPictureDispConverter converter = new ImageToPictureDispConverter() )
//{
//button.Picture = ( StdPicture )converter.GetIPictureDispFromImage( Icons.Regionerate );
//button.Mask = ( StdPicture )converter.GetIPictureDispFromImage( Icons.RegionerateMask );
//}
// Attach to it's Click event
//_commandBarEvents =
// (CommandBarEvents)_applicationObject.Events.get_CommandBarEvents(_regionerateControl);
//_commandBarEvents.Click += new _dispCommandBarControlEvents_ClickEventHandler(regionerate_OnClick);
return button;
}
private void CreateProjectButton( DTE2 applicationObject )
{
CommandBarButton button = CreateCommandBarButton( applicationObject, "Project" );
_allocatedButtons.Add( button );
RegisterClickEvent( applicationObject, button, project_Click );
}
private void CreateSolutionButton( DTE2 applicationObject )
{
CommandBarButton button = CreateCommandBarButton( applicationObject, "Solution" );
_allocatedButtons.Add( button );
RegisterClickEvent( applicationObject, button, solution_Click );
}
private void project_Click( object CommandBarControl, ref bool Handled, ref bool CancelDefault )
{
if ( _onProjectButtonClick != null )
{
Handled = true;
_onProjectButtonClick( CommandBarControl, null );
}
}
private void RegisterClickEvent( DTE2 applicationObject,
CommandBarButton button,
_dispCommandBarControlEvents_ClickEventHandler eventHandler )
{
CommandBarEvents events = ( CommandBarEvents )applicationObject.Events.get_CommandBarEvents( button );
events.Click += eventHandler;
_allocatedEvents.Add( events );
}
private void solution_Click( object CommandBarControl, ref bool Handled, ref bool CancelDefault )
{
if ( _onSolutionButtonClick != null )
{
Handled = true;
_onSolutionButtonClick( CommandBarControl, null );
}
}
#endregion Methods
}
}