home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 June
/
PCWorld_1998-06_cd.bin
/
software
/
sharware
/
grafika
/
EROICA16
/
HOTSPOT.C_
/
HOTSPOT.C
Wrap
C/C++ Source or Header
|
1998-01-15
|
20KB
|
686 lines
/*-------------------------- Parallax Standard C_File ----------------------------
C_File: hotspot.c
Purpose: This file contains the source code for the main
window procedure for the Eroica hotspot application.
--------------------------------------------------------------------------------
Copyright (c)1997 Parallax Software , All rights reserved.
------------------------------------------------------------------------------*/
#pragma warning ( disable:4115 ) /* type name definition in parenthesis */
#pragma warning ( disable:4201 ) /* nonstandard extension used: nameless struct/union */
#pragma warning ( disable:4214 ) /* nonstandard extension used: bit field types other than int */
#pragma warning ( disable:4514 ) /* unreferenced inline/local function has been removed */
#include <windows.h>
#include <ddeml.h>
#include <stdlib.h>
#include <string.h>
#include <mmsystem.h>
#include <malloc.h>
#ifdef _WIN32
#include <shellapi.h>
#endif
#include "hotspot.h"
#define APPMUTEXNAME "EroicaHotspotSampleModule"
#ifdef _WIN32
#define STRNCAT( a,b,c ) strncat( (LPSTR)(a), (LPSTR)(b), c )
#define STRCAT( a,b ) strcat( (LPSTR)(a), (LPSTR)(b) )
#define STRRCHR(a,b) strrchr( (LPSTR)(a), (int)(b) )
#define STRLEN(a) strlen( (LPSTR)(a))
#define STRCPY(a,b) strcpy( (LPSTR)(a), (LPSTR)(b) )
#define STRNCPY(a,b,c) strncpy((LPSTR)(a), (LPSTR)(b), (size_t)(c))
#define STRNCMP(a,b,c) strncmp((LPSTR)(a), (LPSTR)(b), (size_t)(c))
#define STRICMP(a,b ) stricmp((LPSTR)(a), (LPSTR)(b))
#define STRSTR(a,b) strstr((LPSTR)(a), (LPSTR)(b))
#else
#define STRNCAT( a,b,c ) _fstrncat( (LPSTR)(a), (LPSTR)(b), c )
#define STRCAT( a,b ) _fstrcat( (LPSTR)(a), (LPSTR)(b) )
#define STRRCHR(a,b) _fstrrchr( (LPSTR)(a), (int)(b) )
#define STRLEN(a) _fstrlen( (LPSTR)(a))
#define STRCPY(a,b) _fstrcpy( (LPSTR)(a), (LPSTR)(b) )
#define STRNCPY(a,b,c) _fstrncpy((LPSTR)(a), (LPSTR)(b), (size_t)(c))
#define STRNCMP(a,b,c) _fstrncmp((LPSTR)(a), (LPSTR)(b), (size_t)(c))
#define STRICMP(a,b) _fstricmp((LPSTR)(a), (LPSTR)(b))
#define STRSTR(a,b) _fstrstr((LPSTR)(a), (LPSTR)(b))
#endif
/* global variables */
HANDLE ghInst = NULL; /* handle to this instance */
HWND ghWndMain = NULL; /* handle to main window */
HCONV ghConv = NULL; /* handle to DDE conversation */
DWORD idInst = 0L; /* instance identifier */
HSZ ghszTopic;
HSZ ghszServer;
HSZ hszServerBase = NULL;
HSZ hszServerInst;
LPSTR gCmdLine;
/* callback global variables */
int done = 0;
HDDEDATA hDataXact = NULL;
DWORD dwXactID;
int dataLen = MAX_DATA;
char *pStr = NULL;
/* Local messages */
#define WM_HOTSPOT_MSG (WM_USER + 1)
/* ========================================= Parallax C Function ==================
@Name: HandleHotspot
@Desc:
============================================================================== */
void HandleHotspot( LPSTR pData )
{
HCONV hCmdConv;
if ( pData == NULL ) {
return;
}
/* Parse the data into lines of DDE commands to be issued */
while ( *pData != 0 ) {
int len;
char c;
/* find length of current line */
len = 0;
c = pData[len];
while ( c != 0 && c != '\n' && c != '\r' ) {
c = pData[ ++len ];
}
if ( len != 0 ) {
pData[ len ] = 0; /* terminate the line */
if ( pData[0] == '#' ) {
/* echo text */
MessageBox( NULL, &pData[1], "Hotspot Message", MB_APPLMODAL | MB_ICONINFORMATION | MB_OK );
} else if ( pData[0] == '>' ) {
/* execute line */
WinExec( &pData[1], SW_SHOWNORMAL );
} else {
/* DDE command */
DdeClientTransaction( pData, (WORD)(len+1), ghConv, NULL, CF_TEXT, XTYP_EXECUTE, SYNCTIMEOUT, 0);
}
/* fixup the end of line */
pData[len] = c;
pData += len;
}
if ( c != 0 ) {
/* ignore end of line character */
pData++;
}
}
}
/* ========================================= Parallax C Function ==================
@Name: HandleHotspot0
@Desc:
============================================================================== */
void HandleHotspot0( LPSTR pData )
{
HCONV hCmdConv;
/* Connect conversation */
hCmdConv = DdeConnect( idInst, ghszServer, ghszTopic, (LPVOID)NULL);
if ( hCmdConv == NULL ) {
goto cleanup;
}
/* Locate data field */
if ( pData == NULL ) {
goto cleanup;
}
/* Parse the data into lines of hotspot commands to be issued */
while ( *pData != 0 ) {
LPSTR pArg, pArgEnd;
int len;
char c;
/* find length of current line */
len = 0;
c = pData[len];
while ( c != 0 && c != '\n' && c != '\r' ) {
c = pData[ ++len ];
}
pData[ len ] = 0; /* terminate the line */
pArg = pData;
while ( *pArg != 0 && *pArg != '(' ) {
pArg++;
}
if ( *pArg == 0 ) {
pArg = NULL;
}
if ( len > 0 ) {
pArgEnd = &pData[len-1];
while ( *pArgEnd != 0 && *pArgEnd != ')' ) {
pArgEnd--;
}
if ( *pArgEnd == 0 ) {
pArgEnd = NULL;
}
} else {
pArgEnd = NULL;
}
if ( pData[0] == '[' && pArg != NULL && pArgEnd != NULL ) {
*pArg = 0; /* terminate the command */
*pArgEnd = 0; /* terminate the args */
if ( STRNCMP( &pData[1], "echo", 4 ) == 0 ) {
/* echo text */
MessageBox( NULL, &pArg[1], "Hotspot Message", MB_APPLMODAL | MB_ICONINFORMATION | MB_OK );
} else if ( STRNCMP( &pData[1], "execute", 7 ) == 0 ) {
/* execute line */
WinExec( &pArg[1], SW_SHOWNORMAL );
} else if ( STRNCMP( &pData[1], "view", 4 ) == 0 ) {
/* DDE command */
char ddecmd[60];
DWORD result;
wsprintf( ddecmd, "[OpenDocWin(0,0,\"%s\",\"\",0,0,0)]", &pArg[1] );
DdeClientTransaction( ddecmd, (WORD)STRLEN(ddecmd)+1, hCmdConv, NULL, CF_TEXT, XTYP_EXECUTE, SYNCTIMEOUT, &result);
}
*pArg = '(';
*pArgEnd = ')';
}
/* fixup the end of line */
pData[ len ] = c;
pData += len;
if ( c != 0 ) {
/* ignore end of line character */
pData++;
}
}
cleanup:
if ( hCmdConv != NULL ) {
DdeDisconnect( hCmdConv );
}
}
#ifdef _WIN32
#define MYWM_NOTIFYICON (WM_USER + 2)
#define IDM_TOOLMENU_CLOSE (WM_USER + 3)
/*========================================= Parallax C function ==================
@Name : _CreateContextMenu
@Class : static
@Synopsis : Creates the popup menu
@Return : handle of the created menu or NULL if fails
==============================================================================*/
static HMENU _CreateContextMenu( void )
{
HMENU hm = CreatePopupMenu() ;
if ( hm ) {
AppendMenu( hm, MF_STRING, IDM_TOOLMENU_CLOSE, "&Close" ) ;
}
return ( hm ) ;
} /* _CreateContextMenu() */
/*========================================= Parallax C function ==================
@Name : On_MYWM_NOTIFYICON
@Class : static
@Synopsis : Handles messages from the taskbar icon
==============================================================================*/
static void On_MYWM_NOTIFYICON(WPARAM wParam, LPARAM lParam)
{
UINT uID = (UINT) wParam;
UINT uMouseMsg = (UINT)lParam;
HMENU hpopup = (HMENU)NULL ;
if (uMouseMsg == WM_RBUTTONDOWN) {
hpopup = _CreateContextMenu() ;
if ( hpopup ) {
RECT gMousePos;
SystemParametersInfo(SPI_GETWORKAREA, 0, &gMousePos, 0);
TrackPopupMenu( hpopup, TPM_RIGHTALIGN, gMousePos.right - 10, gMousePos.bottom - 30, 0, ghWndMain, NULL ) ;
DestroyMenu( hpopup ) ;
}
}
}
#endif
/* ========================================= Parallax C Function ==================
@Name: MainWndProc
@Desc:
============================================================================== */
long FAR PASCAL MainWndProc(HWND hWnd, unsigned msg, WORD wParam, LONG lParam)
{
static FARPROC lpfnDdeCallBack;
switch ( msg ) {
case WM_CREATE:
/* poll out once a second to see if Eroica is active */
SetTimer( hWnd, 1, 1000, NULL );
/* Ddeml initialization */
lpfnDdeCallBack = MakeProcInstance((FARPROC)DdeCallBack, ghInst);
if ( DdeInitialize( &idInst, (PFNCALLBACK)lpfnDdeCallBack, APPCMD_CLIENTONLY, 0L ) ) {
return(FALSE);
}
/* Topic Name.... */
ghszTopic = DdeCreateStringHandle(idInst, (LPSTR)"imaging", CP_WINANSI);
/* Server Name..... */
ghszServer = DdeCreateStringHandle(idInst, (LPSTR)"Eroica", CP_WINANSI);
pStr = (char*)malloc( dataLen );
#ifdef _WIN32
{
BOOL res;
NOTIFYICONDATA tnid;
HICON hicon = LoadIcon(ghInst, "MAINICON");
/* create the icon */
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = hWnd;
tnid.uID = 777; /* identifier */
tnid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnid.uCallbackMessage = MYWM_NOTIFYICON;
tnid.hIcon = hicon;
lstrcpyn(tnid.szTip,"Sample Hotspot Dispatcher",26);
res = Shell_NotifyIcon(NIM_ADD,&tnid);
if (hicon) {
DestroyIcon(hicon);
}
}
#endif
break;
case WM_TIMER:
if ( ghConv == NULL ) {
ghConv = DdeConnect( idInst, ghszServer, ghszTopic, (LPVOID)NULL);
if ( ghConv ) {
IMG_NotifyHotSpot( ghConv );
}
}
break;
case WM_SYSCOMMAND:
switch ( wParam & 0xFFF0 ) {
case SC_MAXIMIZE:
case SC_RESTORE:
return( 0 );
}
break;
case WM_DESTROY:
#ifdef _WIN32
{
BOOL res;
NOTIFYICONDATA tnid;
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = hWnd;
tnid.uID = 777; /* identifier */
res = Shell_NotifyIcon(NIM_DELETE,&tnid);
}
#endif
if ( pStr != NULL ) {
free( pStr );
pStr = NULL;
}
DdeFreeStringHandle( idInst, ghszTopic );
DdeFreeStringHandle( idInst, ghszServer );
DdeFreeStringHandle( idInst, hszServerBase );
/* terminate all DDE conversations */
DdeUninitialize(idInst);
FreeProcInstance(lpfnDdeCallBack);
PostQuitMessage(0);
break;
case WM_HOTSPOT_MSG:
{
char *szHotspotData = (char *)(UINT) lParam;
HandleHotspot( szHotspotData );
free( szHotspotData );
}
break;
#ifdef _WIN32
case MYWM_NOTIFYICON:
On_MYWM_NOTIFYICON(wParam, lParam);
break;
case WM_COMMAND:
/* IDM_TOOLMENU_CLOSE */
SendMessage( ghWndMain, WM_CLOSE, 0, 0 );
break;
#endif
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
/* ========================================= Parallax C Function ==================
@Name: WinMain
@Desc:
============================================================================== */
#ifdef _WIN32
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
#else
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
#endif
{
MSG msg;
static char szMainMenu[] = "MainMenu";
static char szMainClass[] = "HotspotClass";
BOOL fail = FALSE;
#if defined( _WIN32 )
HANDLE hMutex;
hMutex = CreateMutex( NULL, FALSE, APPMUTEXNAME );
if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
fail = TRUE;
}
#else
if ( hPrevInstance ) {
fail = TRUE;
}
#endif
if ( fail ) {
MessageBox( NULL, "Hotspot Sample is already running", "Hotspot Sample error", MB_ICONSTOP );
return( 0 );
}
gCmdLine = lpszCmdLine;
if (nCmdShow); /* swallow arg */
/* register class */
if ( !hPrevInstance ) {
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, "MAINICON");
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = szMainMenu;
wc.lpszClassName = szMainClass;
if (!RegisterClass(&wc)) {
return( FALSE );
}
}
ghInst = hInstance;
#ifdef _WIN32
ghWndMain = CreateWindow(szMainClass,
"HOTSPOT",
WS_EX_TOOLWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
#else
ghWndMain = CreateWindow(szMainClass,
"HOTSPOT",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
#endif
if (!ghWndMain ) {
return (FALSE);
}
#ifdef _WIN32
ShowWindow(ghWndMain, SW_SHOWMINNOACTIVE);
ShowWindow(ghWndMain, SW_HIDE);
#else
ShowWindow(ghWndMain, SW_SHOWMINNOACTIVE);
#endif
UpdateWindow(ghWndMain);
while ( GetMessage((LPMSG)&msg, NULL, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
/* ========================================= Parallax C Function ==================
@Name: DdeCallBack
@Desc:
============================================================================== */
HOTSPOT_EPOINT (HDDEDATA) DdeCallBack(WORD wType, WORD wFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2)
{
int nbytes;
/* unused formal parms */
dwData2;
wFmt;
switch ( wType ) {
case XTYP_ADVDATA:
nbytes = (int)DdeGetData( hData, NULL, 0, 0 );
if ( nbytes > dataLen ) {
char* pTmp = (char*)realloc( pStr, nbytes );
if ( pTmp != NULL ) {
pStr = pTmp;
dataLen = nbytes;
}
}
if (DdeGetData( hData, pStr, dataLen, 0 ) > 0) {
char item[80];
/* get the topic from the transaction record. */
if (DdeQueryString( idInst, hsz2, item, 80, CP_WINANSI ) != 0) {
if (STRICMP( item, "NotifyIM -- HSAActivated" ) == 0) {
/* if this is an HSA class activation, handle separately. */
if (pStr) {
HandleHotspot0( pStr );
}
} else if (STRICMP( item, "NotifyIM -- HotSpotActivated" ) == 0) {
/* if this is a regular hotspot activation, handle it. */
LPSTR pData = STRSTR( pStr, "(DATA)" );
if ( pData != NULL ) {
char *szHotspotData;
pData += strlen( "(DATA)" );
if ((szHotspotData = malloc( STRLEN( pData ) )) != NULL) {
STRCPY( szHotspotData, pData );
PostMessage( ghWndMain, WM_HOTSPOT_MSG, 0, (long)(UINT) szHotspotData );
}
}
}
}
}
break;
case XTYP_REGISTER:
DdeKeepStringHandle(idInst, hsz1);
DdeKeepStringHandle(idInst, hsz2);
hszServerBase = hsz1;
hszServerInst = hsz2;
return (HDDEDATA)NULL;
case XTYP_XACT_COMPLETE:
{
DWORD buf_size;
LPSTR img_buf;
PSTR pimg_buf;
if (dwXactID == dwData1) {
hDataXact = hData;
if (hDataXact) {
buf_size = DdeGetData( hDataXact, (void FAR *)NULL, 0, 0L);
buf_size++;
pimg_buf = (PSTR)malloc( (WORD)buf_size );
img_buf = (LPSTR)pimg_buf;
if (img_buf) {
DdeGetData( hDataXact, (LPSTR)img_buf, buf_size, 0L);
free( pimg_buf );
}
}
done = 1;
}
}
break;
case XTYP_UNREGISTER:
return (HDDEDATA)NULL;
case XTYP_DISCONNECT:
IMG_NotifyHotSpotStop( ghConv );
DdeDisconnect( ghConv );
ghConv = NULL;
if (!STRICMP( gCmdLine, "AUTOSTOP" )) {
PostMessage( ghWndMain, WM_CLOSE, 0, 0L );
}
return (HDDEDATA)NULL;
default:
return (HDDEDATA)NULL;
}
return (HDDEDATA)NULL;
}
/* ========================================= Parallax C Function ==================
@Name: IMG_NotifyHotSpot
@Desc:
============================================================================== */
int IMG_NotifyHotSpot( HCONV hConv )
{
DWORD result = 0;
HSZ hszString;
if ( hConv == NULL ) {
return( FALSE );
}
hszString = DdeCreateStringHandle(idInst, "NotifyIM -- HotSpotActivated", CP_WINANSI);
DdeClientTransaction( NULL, 0, hConv, hszString, CF_TEXT, XTYP_ADVSTART, SYNCTIMEOUT, (DWORD FAR *)&result );
DdeFreeStringHandle( idInst, hszString );
if ( result == DDE_FACK ) {
hszString = DdeCreateStringHandle(idInst, "NotifyIM -- HSAActivated", CP_WINANSI);
DdeClientTransaction( NULL, 0, hConv, hszString, CF_TEXT, XTYP_ADVSTART, SYNCTIMEOUT, (DWORD FAR *)&result );
DdeFreeStringHandle( idInst, hszString );
if ( result == DDE_FACK ) {
return( TRUE );
}
}
return( FALSE );
}
/* ========================================= Parallax C Function ==================
@Name: IMG_NotifyHotSpotStop
@Desc:
============================================================================== */
int IMG_NotifyHotSpotStop( HCONV hConv )
{
DWORD result = 0;
HSZ hszString;
if ( hConv == NULL ) {
return( FALSE );
}
hszString = DdeCreateStringHandle(idInst, "NotifyIM -- HotSpotActivated", CP_WINANSI);
DdeClientTransaction( NULL, 0, hConv, hszString, CF_TEXT, XTYP_ADVSTOP, SYNCTIMEOUT, (DWORD FAR *)&result );
DdeFreeStringHandle( idInst, hszString );
if ( result == DDE_FACK ) {
hszString = DdeCreateStringHandle(idInst, "NotifyIM -- HSAActivated", CP_WINANSI);
DdeClientTransaction( NULL, 0, ghConv, hszString, CF_TEXT, XTYP_ADVSTOP, SYNCTIMEOUT, (DWORD FAR *)&result );
DdeFreeStringHandle( idInst, hszString );
if ( result == DDE_FACK ) {
return( TRUE );
}
}
return( FALSE );
}
/* HOTSPOT.C */
/* end of file */