TNTmips

HOME

FREE PRODUCTS
  TNTlite
  TNTatlas
  TNTsim3D

DOWNLOADS
  Release Version
  Development Version
  FTP
  Language Kits
  Sample Geodata
  Reseller Resources
  Promotional

DOCUMENTATION
  Tutorials
  Technical Guides
  Quick Guides

SITE MAP

 
15 August 2008

page update: 5 Feb 07


objview.c


/**
 * Demonstrate use of TNTsdk object display functions
 *
 * This program allows the user to view one or more spatial data layers.
 *
 * The user can select a text file to read x/y coordinates
 * of symbols to be plotted over the selected objects.  The map projection for
 * these symbols may also be set via the File / Projection option.
 *
 * If the points are to be read from a file generated by a GPS device the
 * function to read the file could be altered to match a specific format and
 * the projection option could be removed if the file format contains this
 * information or it is always a specific projection.
**/

#include <mgui/appmain.h>
#include <mi32/gre.h>
#include <mi32/coordop.h>

#include <Xm/Form.h>
#include "toolskel.h"

static int PrivIndex=-1;

#define	SYMBOLRADIUS	4


//=====================================================================================================================
//		APP_OBJVIEW - Begin


class APP_OBJVIEW : public MGUI::APP_BASE {
	public:
		APP_OBJVIEW ();
	private:

		Widget m_toplevel;
		GRE_GROUP *m_group;
		GRE_VIEW *m_view;
		MXDHANDLE m_layermgr;
		SIMPLE_ARRAY<DPOINT2D> m_SymbolPoint;
		SPATREF::COORDREFSYS m_CoordRefSys;
		FILEPATH m_SymbolFilePath;

		static void CB_MenuFileGetCRS (Widget,void*,void*);
		static void CB_MenuFileReadPoints (Widget,void*,void*);
		static void CB_OpenLayerMgr (Widget,void*,void*);
		void CreateMainWindow ();
		static void GRECB_View (const GRE_CALLBACK_MSG *msg, void *vpApp);
		void HandleMsg (const GRE_CALLBACK_MSG *msg);

		// Overrides from APP_BASE.
		virtual void v_ExitInstance ();
		virtual const char* v_GetProcessName () const;
		virtual bool v_InitInstance ();
	};


APP_OBJVIEW::APP_OBJVIEW (
	) :
	m_toplevel(0),
	m_group(0),
	m_view(0),
	m_layermgr(0)
	{
	}


// Callback for File / Projection... menu option
void APP_OBJVIEW::CB_MenuFileGetCRS (
	Widget w,
	void *voption,
	void *
	) {
	GRE_VIEW *view = static_cast<GRE_VIEW*>(MxGetUserDataPtr(w));
	APP_OBJVIEW* pApp = static_cast<APP_OBJVIEW*>(view->GetPrivPtr(PrivIndex));
	SPATREF::DlgGetCoordRefSys(pApp->m_toplevel,pApp->m_CoordRefSys);
	if (pApp->m_SymbolPoint.GetNumItems() > 0) pApp->m_view->Restore();
	return;
	}


// Callback for File / Projection... menu option
void APP_OBJVIEW::CB_MenuFileReadPoints (
	Widget w,
	void *voption,
	void *cbs
	) {
	GRE_VIEW *view = static_cast<GRE_VIEW*>(MxGetUserDataPtr(w));
	APP_OBJVIEW* pApp = static_cast<APP_OBJVIEW*>(view->GetPrivPtr(PrivIndex));
	ERRVALUE err;

	char string[256];
	FILE *fp=NULL;
	DPOINT2D pt;

	if ((err = MdlgGetFile(pApp->m_toplevel,pApp->m_SymbolFilePath,NULL,"SelectFile")) < 0) goto Error;
	if ((err = MioOpen(pApp->m_SymbolFilePath.GetMioPath(),"r",&fp,0)) < 0) goto Error;
	pApp->m_SymbolPoint.Clear();

	while ((err = MioGetStr(string,255,fp)) >= 0) {
		if (string[0] == '#') continue;		// Used to indicate a comment
		if (sscanf(string,"%lf %lf",&pt.x,&pt.y) == 2) {
			if ((err = pApp->m_SymbolPoint.Append(pt)) < 0) goto Error;
			}
		}
	pApp->m_view->Restore();

Error:
	if (fp != NULL) fclose(fp);
	if (err < -1) MdlgShowErrorCode(NULL,err);
	return;
	}


// Open layer manager dialog
void APP_OBJVIEW::CB_OpenLayerMgr (
	Widget w,
	void *vpApp,
	void *cbs
	) {
	APP_OBJVIEW* pApp = static_cast<APP_OBJVIEW*>(vpApp);

	MdispxdLayerManagerOpen(pApp->m_layermgr);
	return;
	}


void APP_OBJVIEW::CreateMainWindow (
	) {
	static MENUITEM MenuItemFile[] = {
		{ "read_points_", 0, CB_MenuFileReadPoints },
		{ "projection_",  0, CB_MenuFileGetCRS    },
		{ "-" },
		{ "exit",         0, MxcbSetExit },
		{ NULL }
		};
	static MENUBARITEM MenuBar[] = {
		{ "file", MenuItemFile },
		{ "view" },
		{ "tool" },
		{ "options" },
		{ NULL }
		};
	static BUTTONITEM IconItemExtra[] = {
		{ "layer_controls_", 0, CB_OpenLayerMgr, NULL, NULL, NULL, NULL, "layer_controls" },
		{ NULL }
		};

	Arg arg[20];
	int n;

	n = 0;
	XtSetArgI(arg,n,XmNresizePolicy,XmRESIZE_GROW);
	Widget mainform = XmCreateForm(m_toplevel,"mainform",arg,n);

	n = 0;
	MdispViewCreate(m_group,mainform,arg,n,0,0,"MainView",&m_view,0);
	m_view->SetPrivPtr(PrivIndex,this);
	m_view->SetViewFlags(m_view->GetViewFlags() | VIEWFLAG_NoDestroyShell);

	GRE_ToolAddExample(m_view);

	m_view->AddToolIcons(false);

	for (BUTTONITEM *bi = IconItemExtra; bi->label!=NULL; ++bi) {
		bi->callback_data = (void*) this;
		}
	m_view->AddIconRowItems(IconItemExtra);
	m_view->CreateMenuBar(mainform,MenuBar);

	n = 0;
	XtSetArgI(arg,n,XmNminHeight,100);
	XtSetArgI(arg,n,XmNminWidth,100);
	XtSetValues(XtParent(mainform),arg,n);

	XtManageChild(mainform);
	XtRealizeWidget(m_toplevel);

	m_view->CallbackAdd(GRECB_View,this,GRE_OBJTYPE_View);

	return;
	}


// [static] Handle GRE messages.
void APP_OBJVIEW::GRECB_View (
	const GRE_CALLBACK_MSG *msg,
	void *vpApp
	) {
	static_cast<APP_OBJVIEW*>(vpApp)->HandleMsg(msg);
	}


// Handle GRE messages.
void APP_OBJVIEW::HandleMsg (
	const GRE_CALLBACK_MSG *msg
	) {
	ERRVALUE err = 0;
	COLOR color(COLOR::RED);

	if (m_SymbolPoint.GetNumItems() == 0) return;		// No symbols selected yet
	switch (msg->GetAction()) {
		case GRE_CALLBACK_MSG::ACTION_DrawEnd:
		case GRE_CALLBACK_MSG::ACTION_Expose:
		case GRE_CALLBACK_MSG::ACTION_RestoreEnd: {
			if (m_view->GetDrawingContextScreen() == NULL) return;
			SPATREF::COORDOP_LIST TransToGroup;
			if ((err = TransToGroup.Create(m_CoordRefSys,m_group->GetUsedCoordRefSys())) < 0) break;
			m_view->GetDrawingContextScreen()->SetColor(color);
			DPOINT2D dpoint;
			for (int i = 0; (i < m_SymbolPoint.GetNumItems()); ++i) {
				if ((err = TransToGroup.ConvertForward(&m_SymbolPoint[i],&dpoint,1)) < 0) break;
				if ((err = m_view->TransGroupToDisp(m_group,&dpoint,&dpoint,1)) < 0) break;
				INT32 x = ROUND(dpoint.x);
				INT32 y = ROUND(dpoint.y);
				m_view->GetDrawingContextScreen()->FillCircle(x,y,SYMBOLRADIUS);
				}
			break;
			}
		}
	
	if (err < 0) MxDisplayErrorCode(NULL,err);
	return;
	}


void APP_OBJVIEW::v_ExitInstance (
	) {
	m_group->DestroyAllLayers();
	m_view->Destroy();
	return;
	}


const char* APP_OBJVIEW::v_GetProcessName (
	) const {
	return ("objview");
	}


bool APP_OBJVIEW::v_InitInstance (
	) {
	m_toplevel = MxInitLegacy(LEGACYINIT_NoRealize);

	GRE_SYSTEM::InitX();
	PrivIndex = GRE_SYSTEM::AllocPrivIndex();

	m_group = new GRE_GROUP;
	m_group->SetPrivPtr(PrivIndex,this);

	CreateMainWindow();

	MdispxdLayerManagerCreate(m_toplevel,&m_layermgr,NULL,-1,m_group,0);

	return (true);
	}


//		APP_OBJVIEW - End
//=====================================================================================================================
//		MAIN application instance class

class MAIN : public MGUI::APP_MAIN {
	public:
		MAIN () : m_pApp(new APP_OBJVIEW) {}
		~MAIN () { }
	private:
		APP_OBJVIEW *m_pApp;
	};

static MAIN MainApp;

//=====================================================================================================================


Back Home ©MicroImages, Inc. 2008 Published in the United States of America
11th Floor - Sharp Tower, 206 South 13th Street, Lincoln NE 68508-2010   USA
Business & Sales: (402)477-9554  Support: (402)477-9562  Fax: (402)477-9559
Business info@microimages.com  Support support@microimages.com  Web webmaster@microimages.com