Scene, TOC, and toolbar
SceneTocToolbar.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 "SceneTocToolbar.h"

// Motif Widgets
Widget g_topLevel;                // application
Widget g_mainWindow;              
Widget g_mainForm;                
Widget g_sceneWidget;             
Widget g_tocWidget;               
Widget g_toolbarWidget;           

// Control Interfaces
ISceneControlPtr g_ipSceneControl;
IToolbarControlPtr g_ipToolbarControl;
ITOCControlPtr g_ipTOCControl;

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, "Motif Widget Scene Control",
                NULL);
  
  // Set the application size by resizing the created widget
  XtResizeWidget(g_topLevel, 800, 640, 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);
  
  // ToolbarControl setup 
  g_toolbarWidget = XtVaCreateWidget("g_toolbarWidget",
                                     mwCtlWidgetClass,    g_mainForm,
                                     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);
  
  // TOCControl setup
  g_tocWidget = XtVaCreateWidget("g_tocWidget",
                                 mwCtlWidgetClass,        g_mainForm,
                                 XmNtopAttachment,        XmATTACH_WIDGET,
                                 XmNtopWidget,            g_toolbarWidget,
                                 XmNleftAttachment,       XmATTACH_FORM,
                                 XmNbottomAttachment,     XmATTACH_FORM,
                                 MwNprogID,               AoPROGID_TOCControl,
                                 NULL);
  XtVaSetValues(g_tocWidget, XmNwidth, 200, NULL);
  MwCtlGetInterface(g_tocWidget, (IUnknown**)&g_ipTOCControl);
  
  // SceneControl setup
  g_sceneWidget = XtVaCreateWidget("g_sceneWidget",
                                   mwCtlWidgetClass,     g_mainForm,
                                   XmNtopAttachment,     XmATTACH_WIDGET,
                                   XmNtopWidget,         g_toolbarWidget,
                                   XmNleftAttachment,    XmATTACH_WIDGET,
                                   XmNleftWidget,        g_tocWidget,
                                   XmNrightAttachment,   XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   MwNprogID,            AoPROGID_SceneControl,
                                   NULL);
  MwCtlGetInterface(g_sceneWidget, (IUnknown**)&g_ipSceneControl);
  
  // Buddy the toolbar and TOC with the map
  g_ipTOCControl->SetBuddyControl(g_ipSceneControl);
  g_ipToolbarControl->SetBuddyControl(g_ipSceneControl);
  
  AddToolbarItems();
  
  // Manage the non-parent widgets
  XtManageChild(g_mainWindow);
  XtManageChild(g_mainForm);
  XtManageChild(g_toolbarWidget);
  XtManageChild(g_tocWidget);
  XtManageChild(g_sceneWidget);

  // 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);
}

// Function called when WM_DELETE_WINDOW protocol is passed
void CloseAppCallback(Widget w, XtPointer client_data, XtPointer call_data)
{
  g_ipSceneControl = 0;
  g_ipToolbarControl = 0;
  g_ipTOCControl = 0;

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

  AoExit(0);
}

void AddToolbarItems()
{
  long itemIndex;
  CComVariant varTool;

  varTool = L"esri3DAnalyst.ControlsSceneOpenDocCommand";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0,
                              esriCommandStyleIconOnly, &itemIndex);  
  varTool = L"esri3DAnalyst.ControlsSceneSceneToolbar";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0,
                              esriCommandStyleIconOnly, &itemIndex);
  varTool = L"esriControlCommands.ControlsSelectTool";
  g_ipToolbarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0,
                              esriCommandStyleIconOnly, &itemIndex);
}