Pan and zoom commands
UndoExt.cpp
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

  
#include "UndoExt.h"

UndoExtent::UndoExtent()
{
  m_ipHookHelper.CreateInstance(CLSID_HookHelper);

  // Load the bitmap
  IRasterPicturePtr ipRastPict(CLSID_BasicRasterPicture);
  IPicturePtr ipPict;
  HRESULT hr = ipRastPict->LoadPicture(CComBSTR(L"./Res/UnDoDraw.bmp"), &ipPict);
  if (SUCCEEDED(hr))
  {
    OLE_HANDLE hBitmap;
    hr = ipPict->get_Handle(&hBitmap);
    if (SUCCEEDED(hr))
      m_hBitmap = hBitmap;
  }
}

UndoExtent::~UndoExtent()
{
  m_ipHookHelper = 0;
  m_hBitmap = 0;

}

HRESULT UndoExtent::get_Enabled(VARIANT_BOOL* Enabled)
{
  if (!Enabled)
    return E_POINTER;

  IMapPtr ipMap;
  m_ipHookHelper->get_FocusMap(&ipMap);
  if (ipMap == 0)
    return S_OK;

  // Get the active view
  IActiveViewPtr ipActiveView(ipMap);
  // Get the extent stack
  IExtentStackPtr ipMapStack;
  ipActiveView->get_ExtentStack(&ipMapStack);

  // If the extent can be undo, enable
  VARIANT_BOOL vbUndoable;
  ipMapStack->CanUndo(&vbUndoable);
  if (vbUndoable)
    *Enabled = VARIANT_TRUE;
  else
    *Enabled = VARIANT_FALSE;

  return S_OK;
}

HRESULT UndoExtent::get_Checked(VARIANT_BOOL* Checked)
{
  if (!Checked)
    return E_POINTER;

  Checked = VARIANT_FALSE;
  return S_OK;
}

HRESULT UndoExtent::get_Name(BSTR* Name)
{
  if (!Name)
    return E_POINTER;
  
  *Name = ::AoAllocBSTR(L"Sample_Pan/Zoom_Undo Extent");
  return S_OK;
}

HRESULT UndoExtent::get_Caption(BSTR* Caption) 
{
  if (!Caption)
    return E_POINTER;
  
  *Caption = ::AoAllocBSTR(L"Undo Extent");
  return S_OK;
}

HRESULT UndoExtent::get_Tooltip(BSTR* Tooltip) 
{
  if (!Tooltip)
    return E_POINTER;
  
  *Tooltip = ::AoAllocBSTR(L"Draw Previous Extent");
  return S_OK;
}

HRESULT UndoExtent::get_Message(BSTR* Message) 
{
  if (!Message)
    return E_POINTER;
  
  *Message = ::AoAllocBSTR(L"Goto previous screen location");
  return S_OK;
}

HRESULT UndoExtent::get_Bitmap(OLE_HANDLE* bitmap) 
{
  if (!bitmap)
    return E_POINTER;
  
  if (m_hBitmap != 0)
  {
    *bitmap = m_hBitmap;
    return S_OK;
  }

  return E_FAIL;
}

HRESULT  UndoExtent::get_Category(BSTR* categoryName) 
{
  if (!categoryName)
    return E_POINTER;
  
  *categoryName = ::AoAllocBSTR(L"Sample_Pan/Zoom");
  return S_OK;
}

// Create the command and set who it will work with
HRESULT UndoExtent::OnCreate(IDispatch* hook) 
{
  if (!hook)
    return E_POINTER;
  
  m_ipHookHelper->putref_Hook(hook);
  return S_OK;
}

// When clicked, the button appears pressed
HRESULT UndoExtent::OnClick() 
{
  // Get the active view
  IMapPtr ipMap;
  m_ipHookHelper->get_FocusMap(&ipMap);
  IActiveViewPtr ipActiveView(ipMap);

  // Get the extent stack
  IExtentStackPtr ipMapExtent;
  ipActiveView->get_ExtentStack(&ipMapExtent);

  // Undo the extent
  VARIANT_BOOL vbUndoable;
  ipMapExtent->CanUndo(&vbUndoable);
  if (vbUndoable)
    ipMapExtent->Undo();

  return S_OK;
}