home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1999 January
/
pcwk_01_1999.iso
/
Wtestowe
/
Vistdstd
/
Install
/
Data.Z
/
Generic.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-09-27
|
9KB
|
345 lines
/* GENERIC.CPP - Source for Automation Demo Code in C++
* Copyright (C) 1991-1996 Visio Corporation. All rights reserved.
*
*
* You have a royalty-free right to use, modify, reproduce and distribute
* the Sample Application Files (and/or any modified version) in any way
* you find useful, provided that you agree that Visio has no warranty,
* obligations or liability for any Sample Application Files.
*
*/
#include "visiwrap.h" // Visio wrapper classes and utilities
#include "resource.h" // for our trivial 'About' dialog
//*****************************************************************************
//
// These declare the event handling procedures we're going to tell our event
// sinks to call when they receive notifications from Visio. The signature
// of ReceiveNotifyFromVisio must conform to VISEVENTPROC. See "addsink.h"
static long stc_nEventID= visEvtIDInval;
HRESULT STDMETHODCALLTYPE ReceiveNotifyFromVisio (
IUnknown FAR* ipSink,
WORD wEvent,
IUnknown FAR* ipSource,
DWORD nEventID,
DWORD dwEventSeq,
IUnknown FAR* ipSubject,
VARIANT eventExtra);
static long stc_nAnotherEventID= visEvtIDInval;
HRESULT STDMETHODCALLTYPE ReceiveAnotherNotifyFromVisio (
IUnknown FAR* ipSink,
WORD wEvent,
IUnknown FAR* ipSource,
DWORD nEventID,
DWORD dwEventSeq,
IUnknown FAR* ipSubject,
VARIANT eventExtra);
//*****************************************************************************
//
// Handy little "error handling" macro...
// Requires a "CU:" (clean-up) label somewhere in the function
#define check_valid(hr, obj) \
if ( !SUCCEEDED(hr) || !((obj).IsSet()) ) \
goto CU;
//*****************************************************************************
//
// RunDemo
//
// This function gets called when the user chooses this add-on from the Visio
// add-ons menu.
extern "C" int RunDemo(void)
{
// Two non-wrapped event sinks which we must
// 'Release' manually:
IUnknown FAR *pSink= NULL;
IUnknown FAR *pAnotherSink= NULL;
HRESULT hr= NOERROR;
static BOOL bFirstTime= TRUE; // only add event callbacks once...
BOOL bMadeIt= FALSE;
// The rest of these will be 'Release'd (if necessary) when
// the function exits and they go out of scope:
CVisioApplication app;
CVisioEventList eList;
CVisioEvent event;
CVisioEvent anotherEvent;
CVisioDocuments docs;
CVisioDocument doc;
CVisioPages pages;
CVisioPage page;
CVisioShape shape;
CVisioShape shape1;
CVisioMasters masters;
CVisioMaster master;
CVisioDocument stencil;
CVisioCell cell;
CVisioCell cell1;
if ( VAO_SUCCESS != vaoGetObjectWrap(app) )
goto CU;
// Add a 'Document Created' event notification and a 'Shape Added' notification
// to the Application's EventList:
if (bFirstTime && (SUCCEEDED(app.EventList(eList))))
{
bFirstTime= FALSE;
if (SUCCEEDED(CoCreateAddonSink(ReceiveNotifyFromVisio, &pSink)))
{
if (SUCCEEDED(eList.AddAdvise(visEvtCodeDocCreate, VVariant(pSink),
VBstr(""), VBstr(""), event)))
{
event.ID(&stc_nEventID);
}
pSink->Release();
pSink= NULL;
}
if (SUCCEEDED(CoCreateAddonSink(ReceiveAnotherNotifyFromVisio, &pAnotherSink)))
{
if (SUCCEEDED(eList.AddAdvise((short)(visEvtShape|visEvtAdd), VVariant(pAnotherSink),
VBstr(""), VBstr(""), anotherEvent)))
{
anotherEvent.ID(&stc_nAnotherEventID);
}
pAnotherSink->Release();
pAnotherSink= NULL;
}
}
// Add a new Document based on "sample.vst" and then drop two shapes
// and glue them together...
hr= app.Documents(docs);
check_valid(hr, docs);
hr= docs.Add(VBstr("sample.vst"), doc);
check_valid(hr, doc);
hr= doc.Pages(pages);
check_valid(hr, pages);
hr= pages.Item(VVariant(1L), page);
check_valid(hr, page);
hr= docs.Item(VVariant("sample.vss"), stencil);
check_valid(hr, stencil);
hr= stencil.Masters(masters);
check_valid(hr, masters);
hr= masters.Item(VVariant("Executive"), master);
check_valid(hr, master);
hr= page.Drop(master, 6.0, 6.0, shape);
check_valid(hr, shape);
// Re-using "master" without explicit 'Release' is OK --
// wrapper classes take care of it for you
hr= masters.Item(VVariant("Position"), master);
check_valid(hr, master);
hr= page.Drop(master, 3.0, 3.0, shape1);
check_valid(hr, shape1);
hr= shape.Cells(VBstr("Connections.X4"), cell);
check_valid(hr, cell);
hr= shape1.Cells(VBstr("Controls.X1"), cell1);
check_valid(hr, cell1);
hr= cell1.GlueTo(cell);
bMadeIt= TRUE;
CU:
if (!bMadeIt)
MessageBox(NULL, "Premature termination of generic. Is sample.vst/vss in the path?", "Generic", MB_OK);
return 0;
}
//*****************************************************************************
//
// ReceiveNotifyFromVisio
//
// This function gets called when Visio creates a document.
HRESULT STDMETHODCALLTYPE ReceiveNotifyFromVisio (
IUnknown FAR* ipSink, // i: calling sink [assert]
WORD wEvent, // i: code of firing event (visEvt*; visconst.h)
IUnknown FAR* ipSource, // i: object firing event [don't assert]
DWORD nEventID, // i: id of event in ipsource that's firing
DWORD dwEventSeq, // i: sequence number of event that's firing
IUnknown FAR* ipSubject, // i: object event is about [don't assert]
VARIANT eventExtra) // i: other info about event
{
BOOL bUnexpected= FALSE;
MessageBeep(MB_OK);
// QueryInterface for the Application interface on the IUnknown ipSource.
// The ipSource interface pointer, if it is non-NULL, points to
// the Application which owns the EventList which fired this event.
// To get the Event object, use the EventList::ItemFromID method.
if (ipSource!=NULL)
{
LPVISIOAPPLICATION pApp= NULL;
bUnexpected= TRUE; // assume until...
if (SUCCEEDED(ipSource->QueryInterface(IID_IVApplication, (LPVOID *) &pApp)))
{
CVisioApplication app(pApp);
CVisioEventList eList;
CVisioEvent evt;
if (SUCCEEDED(app.EventList(eList)) && eList.IsSet())
{
if (SUCCEEDED(eList.ItemFromID(stc_nEventID, evt)) && evt.IsSet())
{
short wEventProp;
if (SUCCEEDED(evt.getEvent(&wEventProp)))
{
if (wEventProp==wEvent)
bUnexpected= FALSE; // ...proven otherwise
}
}
}
pApp->Release();
}
}
// QueryInterface for the document interface on the IUnknown ipSubject.
// The ipSubject interface pointer, if it is non-NULL, should be able to give us
// back a Document since we signed up for a 'Document Created' event.
if (ipSubject!=NULL)
{
LPVISIODOCUMENT pDoc= NULL;
bUnexpected= TRUE; // assume until...
if (SUCCEEDED(ipSubject->QueryInterface(IID_IVDocument, (LPVOID *) &pDoc)))
{
CVisioDocument doc(pDoc);
short objType;
if ( doc.IsSet() && SUCCEEDED(doc.ObjectType(&objType)) && (objType==visObjTypeDoc) )
{
short sSaved;
if ( SUCCEEDED(doc.getSaved(&sSaved)) && sSaved )
bUnexpected= FALSE; // ...proven otherwise
}
pDoc->Release();
}
}
// Screech if unexpected condition was encountered.
if ( bUnexpected )
{
for ( int i = 0; i < 30; i++ )
MessageBeep((UINT)-1);
}
return NOERROR;
}
//*****************************************************************************
//
// ReceiveAnotherNotifyFromVisio
//
// This function gets called whenever Visio drops a new shape.
HRESULT STDMETHODCALLTYPE ReceiveAnotherNotifyFromVisio (
IUnknown FAR* ipSink, // i: calling sink [assert]
WORD wEvent, // i: code of firing event (visEvt*; visconst.h)
IUnknown FAR* ipSource, // i: object firing event [don't assert]
DWORD nEventID, // i: id of event in ipsource that's firing
DWORD dwEventSeq, // i: sequence number of event that's firing
IUnknown FAR* ipSubject, // i: object event is about [don't assert]
VARIANT eventExtra) // i: other info about event
{
return NOERROR;
}
/******************************************************************************
*+ AboutDlgProc
*
*/
#ifdef __BORLANDC__
#pragma argsused
#endif
#ifdef _DLL
BOOL __loadds CALLBACK AboutDlgProc(
#else
BOOL CALLBACK AboutDlgProc(
#endif
HWND hDlg,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (wParam)
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, TRUE);
return (TRUE);
}
}
return (FALSE);
}
/******************************************************************************
*+ ShowAboutDialog
*
* Displays the help about dialog.
*/
extern "C" void ShowAboutDialog(HINSTANCE hInstance)
{
FARPROC lpProcDlg= NULL;
lpProcDlg= MakeProcInstance((FARPROC)AboutDlgProc, hInstance);
DialogBox(hInstance, "ABOUTBOX", NULL, lpProcDlg);
FreeProcInstance(lpProcDlg);
}