home *** CD-ROM | disk | FTP | other *** search
- /* DEMO.c -- TerraView Toolkit Demo Program */
-
- /**************************************************************************/
- /* Copyright (c) 1988, 1989 TerraLogics, Inc. */
- /* All Rights Reserved */
- /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF TERRALOGICS, INC. */
- /* The copyright notice above does not evidence any */
- /* actual or intended publication of such source code. */
- /**************************************************************************/
-
- /* Include files */
-
- #include <stdio.h>
- #include <math.h>
-
- #include "TvTypes.h"
- #include "Tv.h"
-
-
- /* Global variables */
-
- double zoom_factor = 2.0;
- int grid_up = 0;
-
- char txtbuf[132];
-
- #define MAX_OVERLAYS (8)
- int overlays = 0;
-
- static TvOverlayID overlay_user[MAX_OVERLAYS],
- overlay_tmp = 0,
- locator = 0,
- grid = 0,
- compass = 0,
- scale = 0;
-
- static double proj_data[16];
- static LONG proj_type = 0;
-
- #define CENTER(MAP,ROW,COLOR,STRING) \
- { \
- int len = strlen(STRING), center; \
- double min_row, min_col, max_row, max_col; \
- TvInquireVisible( MAP, TvRowCol, &min_row, &max_row, &min_col, &max_col ); \
- center = (max_col - min_col + 1) / 2 + 1; \
- TvText(MAP,TvRowCol,((double) ROW),((double) center - len/2),COLOR,STRING );\
- }
- int first_screen = 1;
-
-
- /* Button/function handling global variable */
-
- struct
- {
- int symbol;
- int button;
- } functions[20];
-
- /* The following examples of functions are included in this demo program */
-
- #define FUNCTION_ZOOM_IN (1)
- #define FUNCTION_ZOOM_OUT (2)
- #define FUNCTION_GRID (3)
- #define FUNCTION_IDENTIFY (4)
- #define FUNCTION_UP (5)
- #define FUNCTION_DOWN (6)
- #define FUNCTION_LEFT (7)
- #define FUNCTION_RIGHT (8)
- #define FUNCTION_QUIT (9)
- #define FUNCTION_QUAD_INSET (10)
- #define FUNCTION_ROTATE (11)
- #define FUNCTION_RECENTER (12)
- #define FUNCTION_ADDRESS_LOCATOR (13)
- #define FUNCTION_PRINT (14)
- #define FUNCTION_ADD_SYMBOL (15)
- #define FUNCTION_RESET (16)
- #define FUNCTION_HELP (17)
-
- #define BUTTON(SYM,FNC) functions[FNC].symbol = SYM; \
- functions[FNC].button = ++button;
-
-
- /* This function binds a button number to a symbol
- (graphic representation) and a function */
-
- setup_buttons( map )
- TvMapID map;
- {
- int button=0, i;
-
- /* SYMBOL FUNCTION BUTTON
- ------ -------- ------
- /* */
- BUTTON( 31, FUNCTION_ZOOM_IN ) /* 01 */
- BUTTON( 30, FUNCTION_ZOOM_OUT ) /* 02 */
- BUTTON( 19, FUNCTION_RECENTER ) /* 03 */
- BUTTON( 21, FUNCTION_ROTATE ) /* 04 */
- BUTTON( 24, FUNCTION_IDENTIFY ) /* 05 */
- BUTTON( 18, FUNCTION_ADDRESS_LOCATOR ) /* 06 */
- BUTTON( 23, FUNCTION_GRID ) /* 07 */
- BUTTON( 16, FUNCTION_ADD_SYMBOL ) /* 08 */
- BUTTON( 22, FUNCTION_QUAD_INSET ) /* 09 */
- BUTTON( 14, FUNCTION_RESET ) /* 10 */
- BUTTON( 15, FUNCTION_HELP ) /* 11 */
- BUTTON( 25, FUNCTION_QUIT ) /* 12 */
-
- for( i=1; i < 20; i++ )
- if ( functions[i].button )
- TvMapPanelButton( map, functions[i].button, functions[i].symbol, i );
- }
-
-
- /************************/
- /* Main Program */
- /************************/
-
- TvMain( argc, argv )
- int argc;
- char *argv[];
- {
- TvMapID map;
- int i;
-
- /* Create an empty map */
- if ( !(map = TvCreateMap( TvDefaultWindow, "Map Demo Application" )) )
- {
- printf( "Cannot create map!\n" );
- TvExit( 0 );
- }
-
- /* Enough command-line arguments? */
- if ( argc < 2 )
- {
- TvPrompt( map, "Usage: DEMO overlay-1.TV overlay-2.TV ...", txtbuf );
- TvExit( 0 );
- }
-
- /* Read overlays from command-line */
- while( (overlays < MAX_OVERLAYS) && (overlays < (argc-1)) )
- if ( (overlay_user[overlays] = TvReadOverlay( argv[overlays+1] )) != TvStatusError )
- overlays++;
- else
- {
- sprintf( txtbuf, "Cannot open overlay %s. Press ENTER to exit.", argv[overlays+1] );
- TvPrompt( map, txtbuf, txtbuf );
- TvExit( 0 );
- }
-
- /* Clear window, create map control panel, and load the buttons */
- TvClearWindow( TvInquireMapWindow( map ) );
- TvCreateMapPanel( map, TvPanelStyleRight, 12, 1 );
- setup_buttons( map );
-
- /* Now add the overlays to the map */
- for( i = 0; i < overlays; i++ ) TvAddOverlay( map, overlay_user[i] );
- proj_type = TvInqOverlayProjection( overlay_user[0], 16, proj_data );
-
- /* Generate a grid, compass, and scale overlays */
- grid = TvGenerateGrid( TvDefaultGridStyle );
- compass = TvGenerateCompass( 0 );
- TvAddOverlay( map, compass );
- scale = TvGenerateScale( 0 );
- TvAddOverlay( map, scale );
-
- /* Select the types of features to display */
- do_features( map );
-
- /* Set size of pick aperture as twice the default */
- TvSetMapPickAperture( map, 2 * TvDefaultPickAperture );
-
- /* Display the map on the screen */
- TvRefresh( map , TvAllOverlays );
-
- CENTER(map,2,TvColorGrid,"Welcome to the TerraView Toolkit(tm) Demonstration program")
- CENTER(map,6,TvColorGrid,"To proceed, use your mouse and click on a button")
- CENTER(map,8,TvColorGrid,"OR")
- CENTER(map,10,TvColorGrid,"Press F1 for more help")
- CENTER(map,14,TvColorGrid,"If you have any questions, please call us at (603) 889-1800")
- CENTER(map,16,TvColorGrid,"Monday to Friday between 9AM and 5PM Eastern Time")
-
- /****************************************/
- /* Process user input in a loop forever */
- /****************************************/
- while( 1 ) nxtfnc( );
- }
-
-
- /**********************************************/
- /* Function to get user input, and process it */
- /**********************************************/
-
- nxtfnc()
- {
- TvMapID cur;
- TvEventID event, event2;
- long int code, code2;
- char wild[255];
- static char line[132];
- static double latitude, longitude;
- double rescale = 0.0;
- LONG num, refresh = 0, button;
- static LONG prev_button = -1;
- static int hl_color = TvColorHighlight;
-
- /* Wait for the user to do something */
- event = TvGetEvent( TvAnyMap );
-
- /* Which map? What event? Change to upper case, if necessary. */
- cur = TvEventMap( event);
- button = TvEventData(event );
- code = TvEventCode(event);
- if ( (code >= 'a') && (code <= 'z') ) code -= 'a' - 'A';
-
- /* Skip mouse or button UP click */
- if ( !(code & KBD_MASK_DOWN) &&
- ( ((code & KBD_MASK_CLASS) == KBD_LOCATOR) ||
- ((code & KBD_MASK_CLASS) == KBD_SELECTOR) ) )
- {
- TvReplyEvent( event );
- return;
- }
-
- /* Default mouse click becomes same function as previous button */
- if ( (code & KBD_MASK_CLASS) == KBD_LOCATOR )
- switch( prev_button )
- {
- case FUNCTION_ZOOM_IN:
- case FUNCTION_ZOOM_OUT:
- case FUNCTION_RECENTER:
- case FUNCTION_IDENTIFY:
- case FUNCTION_ADD_SYMBOL:
- button = prev_button;
- code = KBD_SELECTOR;
- break;
-
- default:
- TvReplyEvent( event );
- return;
- }
-
- /* If the user clicked a button... */
- if ( (code & KBD_MASK_CLASS) == KBD_SELECTOR )
- {
- TvMapPanelArm( cur, functions[button].button, 0, TvColorGreen );
- TvMapPanelRefresh( cur );
-
- /* Process the button */
- prev_button = button;
- switch( button )
- {
-
- /* Note: In the following call, TvFWS specifies the coordinate mode
- "Fractional Window Size", which indicates a "fraction" of
- the current window */
-
- case FUNCTION_UP:
- /* Scroll up 1/4 of screen. */
- TvScrollMap( cur, TvFWS, 0.00, 0.25 );
- refresh++;
- break;
-
- case FUNCTION_DOWN:
- /* Scroll down 1/4 of screen. */
- TvScrollMap( cur, TvFWS, 0.00, -0.25 );
- refresh++;
- break;
-
- case FUNCTION_LEFT:
- /* Scroll left 1/4 of screen. */
- TvScrollMap( cur, TvFWS, -0.25, 0.00 );
- refresh++;
- break;
-
- case FUNCTION_RIGHT:
- /* Scroll right 1/4 of screen. */
- TvScrollMap( cur, TvFWS, 0.25, 0.00 );
- refresh++;
- break;
-
- case FUNCTION_ROTATE:
- {
- double angle;
- TvPrompt( cur, "Rotate map: Enter rotation angle from North (degrees): ",
- line );
- sscanf( line, "%lf", &angle );
- TvSetMapRotation( cur, angle );
- }
- refresh++;
- break;
-
- case FUNCTION_GRID:
- if ( !grid_up )
- {
- TvAddOverlay( cur, grid );
- if ( first_screen )
- {
- first_screen = 0;
- TvRefresh( cur, TvAllOverlays );
- }
- else
- TvRefresh( cur, grid );
- TvRemoveOverlay( cur, grid );
- grid_up = 1;
- }
- else
- {
- grid_up = 0;
- refresh++;
- }
- break;
-
- case FUNCTION_ZOOM_OUT:
- TvSetMapZoom( cur, (rescale = (1.0 / zoom_factor)) );
- refresh++;
- break;
-
- case FUNCTION_ZOOM_IN:
- case FUNCTION_RECENTER:
- if ( button == FUNCTION_ZOOM_IN )
- CENTER( cur, 4, TvColorHighlight,
- "Zoom in: Move mouse to desired center and click")
- else
- CENTER( cur, 4, TvColorHighlight,
- "Recenter: Move mouse to desired center and click")
-
- TvSetMouseTracking( cur, 1 );
- while( 1 )
- {
- event2 = TvGetEvent( cur );
- switch( code2 = TvEventCode( event2 ) )
- {
- case KBD_MOUSE1_UP:
- case KBD_MOUSE2_UP:
- break;
-
- case KBD_MOUSE_MOVED:
- TvInquireEventPosition( event2, TvLatLong, &latitude,
- &longitude );
- tell_position( cur, latitude, longitude );
- TvReplyEvent( event2 );
- continue;
-
- default:
- if ( ((code2 & KBD_MASK_CLASS) == KBD_SELECTOR) &&
- (button == FUNCTION_ZOOM_IN) &&
- (code2 & KBD_MASK_DOWN) )
- break;
- TvReplyEvent( event2 );
- continue;
- }
- break;
- }
- TvReplyEvent( event2 );
- TvSetMouseTracking( cur, 0 );
- if ( (code2 & KBD_MASK_CLASS) != KBD_SELECTOR )
- {
- TvInquireLastPosition(cur, TvLatLong, &latitude, &longitude);
- TvSetMapCenter( cur, TvLatLong, latitude, longitude );
- }
- if ( button == FUNCTION_ZOOM_IN )
- TvSetMapZoom( cur, (rescale = zoom_factor) );
- refresh++;
- break;
-
-
- case FUNCTION_ADD_SYMBOL:
- TvMessage(cur, "Add Symbol: Point to location and click");
- {
- TvOverlayID legend;
- double coord[2];
- char name[132];
-
- while( 1 )
- {
- event2 = TvGetEvent( cur );
- code2 = TvEventCode( event2 );
- if ( ((code2 & KBD_MASK_CLASS) == KBD_LOCATOR) )
- break;
- TvReplyEvent( event2 );
- }
-
- TvInquireEventPosition(event2, TvLatLong, &coord[0], &coord[1]);
- TvReplyEvent( event2 );
- TvPrompt(cur,"Add Symbol: Enter description of feature: ",name);
- if ( !strlen( name ) )
- {
- TvMessage( cur, TvClearMessage );
- break;
- }
- legend = TvGenerateLegend( "Some Symbols to Choose From:" );
- TvLegendFile( legend, "TvLegend.DEM", TvColorGreen, 0 );
- TvAddOverlay( cur, legend );
- TvPrompt(cur, "Add Symbol: Enter symbol number: ", line);
- TvRemoveOverlay( cur, legend );
- TvDeleteOverlay( legend );
- num = atoi( line );
- if ( (num < 0) || (num > 31) )
- {
- TvMessage(cur,"Unrecognized Symbol Code. Please retry.");
- break;
- }
- if ( overlay_tmp == 0 )
- {
- if ( !(overlay_tmp = TvCreateOverlay( 0 )) )
- TvExit( 0 );
- TvSetOverlayProjection(overlay_tmp,proj_type,16,proj_data);
- TvAddOverlay( cur, overlay_tmp );
- }
- TvBeginFeature(overlay_tmp,0,0,name,"");
- TvInsertSymbol(overlay_tmp, num, 1, coord );
- TvEndFeature( overlay_tmp );
- }
- break;
-
- case FUNCTION_QUAD_INSET:
- {
- TvWindowID window1, window2;
- TvMapID maps[4], pmap=cur;
- int i;
- TvDeviceID device;
- double x_base, y_base, x_size, y_size;
-
- for( i=0; i < 4; maps[i++] = TvStatusError );
-
- window1 = TvInquireMapWindow( cur );
- TvInquireWindow( window1, &device,
- &x_base, &y_base,
- &x_size, &y_size );
- if ( (x_size < 0.15) || ((y_size -= 0.1) < 0.15) ) break;
-
- TvClearWindow( window1 );
- TvMessage( cur, "Displaying current scale in a small window" );
- for( i=0; i < 4; pmap = maps[i++] )
- {
- double xo = (( !(i & 1) ) ? .04 : .52) * x_size;
- double yo = (( !(i & 2) ) ? 0.0 : 0.5) * y_size;
-
- if ( !(window2 = TvCreateWindow( TvDefaultDevice,
- xo + x_base, yo + y_base + 0.1, .44*x_size, .44*y_size )) )
- break;
-
- if ( !(maps[i] = TvDupMap( window2, pmap )) )
- {
- TvDeleteWindow( window2 );
- break;
- }
- TvSetMapWindowDelete( maps[i] );
- if ( i != 0 ) TvSetMapZoom( maps[i], 0.2 );
-
- do_features( maps[i] );
- TvRefresh( maps[i], TvAllOverlays );
- grid_up = 0;
-
- TvMessage( cur, TvClearMessage );
- TvMessage( cur, "Zooming out 5X" );
- }
-
- TvMessage( cur, "ANY KEY to resume" );
-
- while( 1 )
- {
- event2 = TvGetEvent( TvAnyMap );
- code2 = TvEventCode( event2 );
- TvReplyEvent( event2 );
- if ( ((code2 & KBD_MASK_CLASS) != KBD_SELECTOR) &&
- ((code2 & KBD_MASK_CLASS) != KBD_LOCATOR) )
- break;
- else if ( code2 & KBD_MASK_DOWN )
- break;
- }
-
- for( i=0; i < 4; pmap = maps[i++] )
- if ( maps[i] != TvStatusError )
- TvDeleteMap( maps[i] );
- }
- break;
-
- case FUNCTION_ADDRESS_LOCATOR:
- {
- LONG addr = -1, zipcode = -1, atol();
-
- if ( locator )
- {
- TvRemoveOverlay( cur, locator );
- TvDeleteOverlay( locator );
- locator = 0;
- }
- TvPrompt( cur, "Address Locator: Enter street NAME: ", line );
- if ( line[0] == '\0' ) break;
- strcpy( wild, line ); strcat( wild, "*" );
-
- TvPrompt( cur, "Address Locator: Enter street NUMBER: ", line );
- if ( line[0] ) addr = atol(line);
-
- TvPrompt( cur, "Address Locator: Enter ZIPCODE: ", line );
- if ( line[0] ) zipcode = atol(line);
-
- if ( zipcode == -1 )
- sprintf(line,"Searching for [ %ld %s ]...",
- addr, wild );
- else
- sprintf(line,"Searching for [ %05ld: %ld %s ]...",
- zipcode, addr, wild );
- TvMessage( cur, line );
-
- if ( (locator = TvStreetLocator( cur, TvAllOverlays,
- zipcode, wild, addr, 1 ))
- != TvStatusError )
- {
- TvAddOverlay( cur, locator );
- TvResetReference( cur );
- }
- else
- TvPrompt(cur,"Location not found! ENTER to continue.", line);
- }
- refresh++;
- break;
-
- case FUNCTION_PRINT:
- TvPrompt(cur,"Enter print file name (ENTER to abort):", line);
- if ( strlen(line) )
- {
- TvMessage( cur, "Please wait..." );
- TvScreenDump( cur , line );
- TvMessage( cur, TvClearMessage );
- }
- break;
-
- case FUNCTION_RESET:
- TvResetReference( cur );
- if ( overlay_tmp != 0 )
- {
- TvRemoveOverlay( cur, overlay_tmp );
- TvDeleteOverlay( overlay_tmp );
- overlay_tmp = 0;
- }
- if ( locator )
- {
- TvRemoveOverlay( cur, locator );
- TvDeleteOverlay( locator );
- locator = 0;
- }
- refresh++;
- break;
-
- case FUNCTION_HELP:
- tell_help( cur );
- refresh++;
- break;
-
-
- case FUNCTION_IDENTIFY:
- TvMessage(cur, "Identify: Point to unknown feature and click");
- while( 1 )
- {
- event2 = TvGetEvent( cur );
- code2 = TvEventCode( event2 );
- if ( (code2 & KBD_MASK_CLASS) == KBD_LOCATOR )
- break;
- TvReplyEvent( event2 );
- }
- TvInquireEventPosition(event2, TvLatLong, &latitude, &longitude);
- TvReplyEvent( event2 );
- TvPickByPosition( cur, TvLatLong, latitude, longitude );
- TvMessage( cur, "In progress..." );
- while ( (code2 != KBD_QUIT) && (code2 != KBD_WINDOW_EXIT) &&
- (code2 != 0177) && (TvPickNext(cur) == TvStatusOK) )
- {
- char name[80];
-
- TvDisplayPickedFeature( cur, hl_color );
- TvInquirePickedFeature( cur, 0, name, 0 );
- sprintf( line, "<%s> ANY KEY for next", name );
- TvMessage( cur, line );
- while( 1 )
- {
- event2 = TvGetEvent( TvAnyMap );
- code2 = TvEventCode( event2 );
- TvReplyEvent( event2 );
- if ( ((code2 & KBD_MASK_CLASS) != KBD_LOCATOR) &&
- ((code2 & KBD_MASK_CLASS) != KBD_SELECTOR) )
- break;
- else if ( (code2 & KBD_MASK_DOWN) )
- break;
- }
- TvMessage( cur, "In progress..." );
- TvHighlightPickedFeature( cur, 0 );
- }
- TvMessage( cur, TvClearMessage );
- break;
-
- case FUNCTION_QUIT:
- TvReplyEvent( event );
- TvClearWindow( TvInquireMapWindow( cur ) );
- CENTER( cur, 05, TvColorGrid,
- "Thanks for using the TerraView Demonstration program" )
- CENTER( cur, 10, TvColorGrid,
- "Comments and suggestions are greatly appreciated" )
- CENTER( cur, 15, TvColorGrid,
- "Please call us 9-5 EST M-F at (603) 889-1800" )
- CENTER( cur, 20, TvColorGrid,
- "We look forward to hearing from you!" )
- TvMessage( cur, "ANY key to exit" );
- event = TvGetEvent( TvAnyMap );
- TvReplyEvent( event );
- TvExit( 1 );
- break;
-
- } /* end switch( button ) */
-
- TvMapPanelArm( cur, functions[button].button, 0, TvColorWhite );
- } /* end if SELECTOR */
-
- else switch ( code )
- {
- case KBD_HELP:
- case KBD_F1:
- case 'H':
- tell_help( cur );
- refresh++;
- break;
-
- case KBD_QUIT:
- TvPrompt( cur , "Really Exit ? (Y/N) [N]: ", line );
- if ( (line[0] != 'Y') && (line[0] != 'y') ) break;
- case 'Q':
- case 'E':
- case 'X':
- TvReplyEvent( event );
- TvExit( 1 );
- break;
-
- case KBD_F2:
- TvSetMapZoom( cur, (rescale = zoom_factor) );
- refresh++;
- break;
-
- case KBD_F3:
- TvSetMapZoom( cur, (rescale = (1.0 / zoom_factor)) );
- refresh++;
- break;
-
- case KBD_UP:
- TvScrollMap( cur, TvFWS, 0.00, 0.25 );
- refresh++;
- break;
-
- case KBD_DOWN:
- TvScrollMap( cur, TvFWS, 0.00, -0.25 );
- refresh++;
- break;
-
- case KBD_LEFT:
- TvScrollMap( cur, TvFWS, -0.25, 0.00 );
- refresh++;
- break;
-
- case KBD_RIGHT:
- TvScrollMap( cur, TvFWS, 0.25, 0.00 );
- refresh++;
- break;
-
- case KBD_WINDOW_RESIZE:
- refresh++;
- break;
- } /* switch( code ) */
-
- if ( refresh )
- {
- do_features( cur );
- if ( !TvRefresh( cur, TvAllOverlays ) && (rescale != 0.0) )
- {
- /* No data displayed! Reset zoom and re-display */
- TvSetMapZoom( cur, (1.0 / rescale) );
- do_features( cur );
- TvRefresh( cur, TvAllOverlays );
- TvMessage( cur,"No Additional Data Available!");
- }
- rescale = 0.0;
- first_screen = grid_up = 0;
- }
- else
- TvMapPanelRefresh( cur );
-
- if ( event ) TvReplyEvent( event );
-
- return;
- }
-
- /***************************************/
- /* Function to tell the user the mouse */
- /* position in Latitude and Longitude */
- /***************************************/
- tell_position( cur, latitude, longitude )
- TvMapID cur;
- double latitude, longitude;
- {
- char msg[255];
- double lat_deg, lat_min, lat_sec;
- double long_deg, long_min, long_sec;
-
- TvDegDMS( latitude, &lat_deg, &lat_min, &lat_sec );
- TvDegDMS( longitude, &long_deg, &long_min, &long_sec );
- sprintf( msg, "%3.0lf %2.0lf'%5.2lf\" %3.0lf %2.0lf'%5.2lf\"",
- lat_deg, lat_min, lat_sec, long_deg, long_min, long_sec );
- TvMessage( cur, msg );
- }
-
-
- /*************************************/
- /* Function to display the help file */
- /*************************************/
- tell_help( cur )
- TvMapID cur;
- {
- char line[132];
- FILE *help;
- int line_number=4;
- TvEventID event2;
- LONG code2;
-
- if ( (help = fopen( "readme.txt", "r" )) == NULL ) return;
- TvClearWindow( TvInquireMapWindow( cur ) );
- while( fgets( line, 132, help ) != NULL )
- CENTER( cur, line_number++, TvColorHighlight, line )
-
- TvMessage( cur, "ANY KEY to resume" );
- while( 1 )
- {
- event2 = TvGetEvent( TvAnyMap );
- code2 = TvEventCode( event2 );
- TvReplyEvent( event2 );
- if ( ((code2 & KBD_MASK_CLASS) != KBD_SELECTOR) &&
- ((code2 & KBD_MASK_CLASS) != KBD_LOCATOR) )
- break;
- else if ( code2 & KBD_MASK_DOWN )
- break;
- }
- }
-
-
- /*****************************************************/
- /* Function to enable selected features in overlays */
- /*****************************************************/
- do_features( cur )
- TvMapID cur;
- {
- TvSelectFeature(cur, TvAllOverlays, "DYNAMAP", "A", 1);
- TvSelectFeature(cur, TvAllOverlays, "DYNAMAP", "0", 1);
- TvSelectFeature(cur, TvAllOverlays, "DYNAMAP", "4", 1);
- }
-