Customization
Customization.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 "Customization.h"

// Motif Widgets 
Widget g_topLevel;                   // application
Widget g_mainWindow;
Widget g_mainForm;
Widget g_topFormPanel;
Widget g_toolbarWidget;
Widget g_bottomFormPanel;
Widget g_rightBottomFormPanel;
Widget g_mapWidget;
Widget g_customizeToggle;
Widget g_howToLabel;

// Control
IMapControl3Ptr g_ipMapControl;
IToolbarControlPtr g_ipToolbarControl;

int main(int argc, char* argv[])
{
  // Initialize the Engine
  ::AoInitialize(NULL);
  {
    IAoInitializePtr ipInit(CLSID_AoInitialize);
    esriLicenseStatus status;
    ipInit->Initialize(esriLicenseProductCodeEngine, &status);
    if (status != esriLicenseCheckedOut)
    {
      std::cerr << "Invalid Licensing." << std::endl;
      AoExit(0);  
    }
  }

  XtSetLanguageProc(NULL, NULL, NULL);
  XtAppContext app_context;
  
  // Initialize the Motif toolkit and create the parent widget
  g_topLevel = XtVaAppInitialize(&app_context, "XApplication", 
                                 NULL, 0, &argc, argv, NULL, NULL);
  XtVaSetValues(g_topLevel, 
                XmNtitle, "Customization Sample",
                NULL);
  
  // Set the application size by resizing the created widget
  XtResizeWidget(g_topLevel, 700, 475, 1);
  
  g_mainWindow = XtVaCreateWidget("g_mainWindow", 
                                  xmMainWindowWidgetClass, g_topLevel,
                                  NULL);
  
  g_mainForm = XtVaCreateWidget("g_mainForm",
                                xmFormWidgetClass,        g_mainWindow,
                                XmNtopAttachment,         XmATTACH_WIDGET,
                                XmNtopWidget,             g_mainWindow,
                                XmNbottomAttachment,      XmATTACH_WIDGET,
                                XmNbottomWidget,          g_mainWindow,
                                XmNleftAttachment,        XmATTACH_WIDGET,
                                XmNleftWidget,            g_mainWindow,
                                XmNrightAttachment,       XmATTACH_WIDGET,
                                XmNrightWidget,           g_mainWindow,
                                XmNfractionBase,          100,
                                NULL);
  
  // Create and place subform for ToolbarControl
  g_topFormPanel = XtVaCreateWidget("g_topFormPanel",
                                    xmFormWidgetClass,   g_mainForm,
                                    XmNtopAttachment,    XmATTACH_FORM,
                                    XmNrightAttachment,  XmATTACH_FORM,
                                    XmNleftAttachment,   XmATTACH_FORM,
                                    XmNheight,           50,
                                    NULL);
  
  // Create and place the ToolbarControl
  g_toolbarWidget = XtVaCreateWidget("g_toolbarWidget",
                                     mwCtlWidgetClass,   g_topFormPanel,
                                     XmNtopAttachment,   XmATTACH_FORM,
                                     XmNleftAttachment,  XmATTACH_FORM,
                                     XmNrightAttachment, XmATTACH_FORM,
                                     MwNprogID,          AoPROGID_ToolbarControl,
                                     NULL);
  XtVaSetValues(g_toolbarWidget, XmNheight, 25, NULL);
  MwCtlGetInterface(g_toolbarWidget, (IUnknown**)&g_ipToolbarControl);
  
  // Create and place subform for MapControl, g_rightBottomFormPanel
  g_bottomFormPanel = XtVaCreateWidget("g_bottomFormPanel",
                                       xmFormWidgetClass,   g_mainForm,
                                       XmNtopAttachment,    XmATTACH_WIDGET,
                                       XmNtopWidget,        g_topFormPanel,
                                       XmNrightAttachment,  XmATTACH_FORM,
                                       XmNbottomAttachment, XmATTACH_FORM,
                                       XmNleftAttachment,   XmATTACH_FORM,
                                       NULL);
  
  // Create and place subform for Customize toggle and directions
  g_rightBottomFormPanel = XtVaCreateWidget("g_rightBottomFormPanel",
                                            xmFormWidgetClass,   g_bottomFormPanel,
                                            XmNtopAttachment,    XmATTACH_FORM,
                                            XmNbottomAttachment, XmATTACH_FORM,
                                            XmNrightAttachment,  XmATTACH_FORM,
                                            XmNwidth,            275,
                                            NULL);
  
  // Create and place MapControl
  g_mapWidget = XtVaCreateWidget("g_mapWidget",
                                 mwCtlWidgetClass,    g_bottomFormPanel,
                                 XmNtopAttachment,    XmATTACH_FORM,
                                 XmNbottomAttachment, XmATTACH_FORM,
                                 XmNleftAttachment,   XmATTACH_FORM,
                                 XmNrightAttachment,  XmATTACH_WIDGET,
                                 XmNrightWidget,      g_rightBottomFormPanel,
                                 MwNprogID,           AoPROGID_MapControl,
                                 NULL);
  MwCtlGetInterface(g_mapWidget, (IUnknown**)&g_ipMapControl);
  
  // Create and place customize toggle
  XmString label = XmStringCreateLocalized("Customize");
  g_customizeToggle = XtVaCreateWidget("g_customizeToggle",
                                       xmToggleButtonWidgetClass,   g_rightBottomFormPanel,
                                       XmNlabelString,      label,
                                       XmNtopAttachment,    XmATTACH_FORM,
                                       XmNrightAttachment,  XmATTACH_FORM,
                                       XmNleftAttachment,   XmATTACH_FORM, 
                                       XmNheight,           25,
                                       NULL);
  XtAddCallback(g_customizeToggle, XmNvalueChangedCallback, ToggleCallback, 
                NULL);
  XmStringFree(label);
  
  // Create and place howto label
  label = XmStringCreateLocalized("\n \n To move an item, select it with the left \n mouse button and drag and drop it to \n the location indicated by the black \n verticle bar. \n \n To delete an item, either select it with \n the left mouse button and drag it off the \n ToolbarControl or select it with the right \n mouse button and choose delete from \n the customize menu. \n \n To change the group, group spacing or \n style of an item, select it with the right \n mouse button to display the customize \n menu."); 
  g_howToLabel = XtVaCreateWidget("g_howToLabel",
                                  xmLabelWidgetClass,    g_rightBottomFormPanel,
                                  XmNlabelString,        label,
                                  XmNalignment,          XmALIGNMENT_BEGINNING,
                                  XmNleftAttachment,     XmATTACH_FORM,
                                  XmNrightAttachment,    XmATTACH_FORM,
                                  XmNtopAttachment,      XmATTACH_WIDGET,
                                  XmNtopWidget,          g_customizeToggle,
                                  NULL);
  XmStringFree(label);
  
  // Manage the non-parent widgets
  XtManageChild(g_mainWindow);
  XtManageChild(g_mainForm);
  XtManageChild(g_topFormPanel);
  XtManageChild(g_toolbarWidget);
  XtManageChild(g_bottomFormPanel);
  XtManageChild(g_rightBottomFormPanel);
  XtManageChild(g_mapWidget);
  XtManageChild(g_customizeToggle);
  XtManageChild(g_howToLabel);
  
  g_ipToolbarControl->SetBuddyControl(g_ipMapControl);
  AddToolbarItems();

  // Handle the window manager message that the window is about to be closed 
  Atom wm_delete_window = XmInternAtom(XtDisplay(g_topLevel), 
                                       "WM_DELETE_WINDOW", 
                                       FALSE);
  XmAddWMProtocolCallback(g_topLevel, wm_delete_window, 
                          CloseAppCallback, NULL);
  
  // Start the application running 
  XtRealizeWidget(g_topLevel);
  MwCtlAppMainLoop(app_context);
}

void CloseAppCallback(Widget w, XtPointer client_data, XtPointer call_data)
{
  g_ipMapControl = 0;
  g_ipToolbarControl = 0;

  // Uninitialize the engine
  {
    IAoInitializePtr ipInit(CLSID_AoInitialize);
    ipInit->Shutdown();
  }
  ::AoUninitialize();
  
  AoExit(0);
}

void ToggleCallback(Widget w, XtPointer client_data, XtPointer call_data)
{
  XmToggleButtonCallbackStruct *customize = 
    (XmToggleButtonCallbackStruct *) call_data;
  
  if (customize->set)
    g_ipToolbarControl->put_Customize(VARIANT_TRUE);
  else
    g_ipToolbarControl->put_Customize(VARIANT_FALSE);
}

HRESULT AddToolbarItems()
{
  long itemIndex;
  CComVariant varTool;
  
  varTool = L"esriControlCommands.ControlsOpenDocCommand";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, 
                              esriCommandStyleIconOnly, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapZoomInTool";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_TRUE, 0, 
                              esriCommandStyleIconAndText, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapZoomOutTool";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, 
                              esriCommandStyleIconAndText, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapPanTool";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, 
                              esriCommandStyleIconAndText, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapFullExtentCommand";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, 
                              esriCommandStyleIconAndText, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapZoomToLastExtentBackCommand";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_TRUE, 0, 
                              esriCommandStyleTextOnly, &itemIndex);
  
  varTool = L"esriControlCommands.ControlsMapZoomToLastExtentForwardCommand";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, 
                              esriCommandStyleTextOnly, &itemIndex);
  
  return S_OK;
}