Draw text
DrawText.cpp
// Copyright 2011 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 "DrawText.h"

// Motif Widgets
Widget g_topLevel;                // application
Widget g_mainWindow;
Widget g_mainForm;
Widget g_mapWidget;
Widget g_topFormPanel;
Widget g_topRightFormPanel;
Widget g_fullExtentButton;
Widget g_refreshButton;
Widget g_textBox;
Widget g_textBoxLabel;
Widget g_howToLabel1;
Widget g_howToLabel2;

// Control Interfaces
IMapControl3Ptr g_ipMapControl;

IGeoFeatureLayerPtr g_ipGeoFeatureLayer;
IPolylinePtr g_ipPolyline;
IPointCollectionPtr g_ipPointCollection;
MapControlEvents* g_mapEvents;
IEventListenerHelperPtr g_ipMapControlEvent2Helper;

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, "Draw Text Example",
                NULL);

  // Set the application size by resizing the created widget
  XtResizeWidget(g_topLevel, 600, 480, 1);

  g_mainWindow = XtVaCreateWidget("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 the g_textBox
  g_textBox = XtVaCreateWidget("g_textBox",
                               xmTextFieldWidgetClass,   g_mainForm,
                               XmNleftAttachment,        XmATTACH_FORM,
                               XmNrightAttachment,       XmATTACH_FORM,
                               XmNbottomAttachment,      XmATTACH_FORM,
                               XmNheight,                35,
                               NULL);
  XmTextFieldSetString(g_textBox, "Put a map in your app...");

  // Create and place the g_textBox label
  XmString label = XmStringCreateLocalized("Enter text:");
  g_textBoxLabel = XtVaCreateWidget("g_textBoxLabel",
                                    xmLabelWidgetClass,  g_mainForm,
                                    XmNlabelString,      label,
                                    XmNalignment,        XmALIGNMENT_BEGINNING,
                                    XmNleftAttachment,   XmATTACH_FORM,
                                    XmNrightAttachment,  XmATTACH_FORM,
                                    XmNbottomAttachment, XmATTACH_WIDGET,
                                    XmNbottomWidget,     g_textBox,
                                    XmNheight,           25,
                                    NULL);
  XmStringFree(label);

  // Create subform for MapControl and Buttons subform
  g_topFormPanel = XtVaCreateWidget("g_topFormPanel",
                                    xmFormWidgetClass,    g_mainForm,
                                    XmNtopAttachment,     XmATTACH_FORM,
                                    XmNrightAttachment,   XmATTACH_FORM,
                                    XmNleftAttachment,    XmATTACH_FORM,
                                    XmNbottomAttachment,  XmATTACH_WIDGET,
                                    XmNbottomWidget,      g_textBoxLabel,
                                    NULL);

  // Create subform for buttons
  g_topRightFormPanel = XtVaCreateWidget("g_topRightFormPanel",
                                         xmFormWidgetClass,    g_topFormPanel,
                                         XmNtopAttachment,     XmATTACH_FORM,
                                         XmNrightAttachment,   XmATTACH_FORM,
                                         XmNbottomAttachment,  XmATTACH_FORM,
                                         XmNwidth,             125,
                                         NULL);

  // Create and place the full extent button
  label = XmStringCreateLocalized("Full Extent");
  g_fullExtentButton = XtVaCreateWidget("g_fullExtentButton",
                                        xmPushButtonWidgetClass, g_topRightFormPanel,
                                        XmNlabelString,          label,
                                        XmNleftAttachment,       XmATTACH_FORM,
                                        XmNrightAttachment,      XmATTACH_FORM,
                                        XmNbottomAttachment,     XmATTACH_FORM,
                                        XmNheight,               35,
                                        NULL);
  XmStringFree(label);
  XtAddCallback(g_fullExtentButton, XmNactivateCallback, FullExtClick, NULL);

  // Create and place the refresh button
  label = XmStringCreateLocalized("Clear Text");
  g_refreshButton = XtVaCreateWidget("g_refreshButton",
                                     xmPushButtonWidgetClass, g_topRightFormPanel,
                                     XmNlabelString,          label,
                                     XmNleftAttachment,       XmATTACH_FORM,
                                     XmNrightAttachment,      XmATTACH_FORM,
                                     XmNbottomAttachment,     XmATTACH_WIDGET,
                                     XmNbottomWidget,         g_fullExtentButton,
                                     XmNheight,               35,
                                     NULL);
  XmStringFree(label);
  XtAddCallback(g_refreshButton, XmNactivateCallback, RefreshClick, NULL);

  // Create and place the howto label1
  label = XmStringCreateLocalized(" Left mouse \n button to trace a \n line to draw text \n along.");
  g_howToLabel1 = XtVaCreateWidget("g_howToLabel1",
                                   xmLabelWidgetClass, g_topRightFormPanel,
                                   XmNlabelString,     label,
                                   XmNalignment,       XmALIGNMENT_BEGINNING,
                                   XmNleftAttachment,  XmATTACH_FORM,
                                   XmNrightAttachment, XmATTACH_FORM,
                                   XmNtopAttachment,   XmATTACH_FORM,
                                   XmNheight,          100,
                                   NULL);
  XmStringFree(label);

  // Create and place the howto label2
  label = XmStringCreateLocalized(" Right mouse \n button to drag a \n rectangle to \n zoom in.");
  g_howToLabel2 = XtVaCreateWidget("g_howToLabel2",
                                   xmLabelWidgetClass, g_topRightFormPanel,
                                   XmNlabelString,     label,
                                   XmNalignment,       XmALIGNMENT_BEGINNING,
                                   XmNleftAttachment,  XmATTACH_FORM,
                                   XmNrightAttachment, XmATTACH_FORM,
                                   XmNtopAttachment,   XmATTACH_WIDGET,
                                   XmNtopWidget,       g_howToLabel1,
                                   XmNheight,          100,
                                   NULL);
  XmStringFree(label);

  // Create and place the MapControl
  g_mapWidget = XtVaCreateWidget("g_mapWidget",
                                 mwCtlWidgetClass,        g_topFormPanel,
                                 XmNtopAttachment,        XmATTACH_FORM,
                                 XmNleftAttachment,       XmATTACH_FORM,
                                 XmNrightAttachment,      XmATTACH_WIDGET,
                                 XmNrightWidget,          g_topRightFormPanel,
                                 XmNbottomAttachment,     XmATTACH_FORM,
                                 MwNprogID,               AoPROGID_MapControl,
                                 NULL);
  MwCtlGetInterface(g_mapWidget, (IUnknown**)&g_ipMapControl);

  // Manage the non-parent widgets
  XtManageChild(g_mainWindow);
  XtManageChild(g_mainForm);
  XtManageChild(g_textBox);
  XtManageChild(g_textBoxLabel);
  XtManageChild(g_topFormPanel);
  XtManageChild(g_topRightFormPanel);
  XtManageChild(g_fullExtentButton);
  XtManageChild(g_refreshButton);
  XtManageChild(g_howToLabel1);
  XtManageChild(g_howToLabel2);
  XtManageChild(g_mapWidget);

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

  LoadData();

  // Listen for MapControlEvents
  g_mapEvents = new MapControlEvents();
  g_ipMapControlEvent2Helper.CreateInstance(CLSID_MapControlEvents2Listener);
  g_ipMapControlEvent2Helper->Startup(
    static_cast<IMapControlEvents2Helper*> (g_mapEvents));
  g_ipMapControlEvent2Helper->AdviseEvents(g_ipMapControl, NULL);

  // Grab hold of the IGeoFeatureLayer interface on the later in the
  //    MapControl in order to symbolize the data
  ILayerPtr ipLayer;
  g_ipMapControl->get_Layer(0, &ipLayer);
  g_ipGeoFeatureLayer = ipLayer;

  SymbolSetup();

  // 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_ipGeoFeatureLayer = 0;
  g_ipPolyline = 0;
  g_ipPointCollection = 0;
  g_ipMapControl = 0;

  g_ipMapControlEvent2Helper->UnadviseEvents();
  g_ipMapControlEvent2Helper->Shutdown();
  g_ipMapControlEvent2Helper = 0;
  delete g_mapEvents;

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

  AoExit(0);
}

void LoadData()
{
  char datapath[1024];
  sprintf(datapath, "%s/Samples/data/defensesolutions/Continents", getenv("AGSDEVKITJAVA"));
  CComBSTR SHAPE_PATH(datapath);
  HRESULT hr = g_ipMapControl->AddShapeFile(SHAPE_PATH, CComBSTR(L"Continents"));
  if (FAILED(hr))
  {
    std::cerr << "You must edit DrawText.cpp to provide a valid data source, "
              << "then recompile and try running the sample again."
              << std::endl;
    AoExit(0);
  }
}

void SymbolSetup()
{
  // Assign line symbol and fill symbol properties
  ISimpleLineSymbolPtr ipLineSymbol(CLSID_SimpleLineSymbol);
  ipLineSymbol->put_Width(0.1);
  IRgbColorPtr ipColor(CLSID_RgbColor);
  ipColor->put_Red(255);
  ipColor->put_Green(0);
  ipColor->put_Blue(0);
  ipLineSymbol->put_Color(ipColor);

  ISimpleFillSymbolPtr ipFillSymbol(CLSID_SimpleFillSymbol);
  ipFillSymbol->put_Outline(ipLineSymbol);
  ipColor->put_Red(0);
  ipColor->put_Green(0);
  ipColor->put_Blue(255);
  ipFillSymbol->put_Color(ipColor);

  // Set the symbol property of the renderer
  ISymbolPtr ipSymbol = ipFillSymbol;
  ISimpleRendererPtr ipSimpleRenderer(CLSID_SimpleRenderer);
  ipSimpleRenderer->putref_Symbol(ipSymbol);
  IFeatureRendererPtr ipRender = ipSimpleRenderer;
  g_ipGeoFeatureLayer->putref_Renderer(ipRender);
}

void FullExtClick(Widget w, XtPointer client_data, XtPointer call_data)
{
  IEnvelopePtr ipFullExt;
  g_ipMapControl->get_FullExtent(&ipFullExt);
  g_ipMapControl->put_Extent(ipFullExt);
}

void RefreshClick(Widget w, XtPointer client_data, XtPointer call_data)
{
  g_ipPolyline = NULL;
  g_ipPointCollection = NULL;

  g_ipMapControl->Refresh(esriViewForeground);
}