home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
valset.zip
/
VALUESET.C
< prev
next >
Wrap
Text File
|
1992-01-04
|
19KB
|
589 lines
/*
a Value Set example,
Copyright TASP Group, Inc., 1992
All Rights Reserved
By Paul Montgomery, TASP Group, Inc.
4776 Blackberry Drive
Melbourne, FL. 32904
CIS: 71500,3525
You may use this example as a learning tool, feel free to make copies and
distribute them. You may not sell them. This header text must accompany
this example when copied or distributed.
*/
#define INCL_WIN
#define INCL_WINSTDVALSET
#include <os2.h>
HAB hab;
HSTR vlstype;
HPOINTER barney;
HPOINTER fred;
HPOINTER wilma;
HPOINTER bart;
HPOINTER bear;
HPOINTER cat;
HPOINTER andy;
HPOINTER donatelo;
HPOINTER face;
HPOINTER frame;
LONG iconx; // system values for icon dimensions
LONG icony;
HWND hwVs1, hwVs2;
// function prototypes
#pragma linkage ( WinProc, system )
MRESULT WinProc ( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 );
MRESULT InitDrag ( HWND hwnd, HWND hcont, MPARAM mp1, MPARAM mp2 );
MRESULT DragOver ( HWND hwnd, HWND hcont, MPARAM mp1, MPARAM mp2 );
MRESULT DragDrop ( HWND hwnd, HWND hcont, MPARAM mp1, MPARAM mp2 );
/* **************************************************************
* Function Name: main
*
* Description:
*
* Parameter Description
* --------------------------------------------------------------
*
* Returns:
*
* Comments:
*
***************************************************************/
int main ( int argc, char *argv[] )
{
QMSG qmsg;
HMQ hmq;
ULONG fs;
HWND hframe;
HWND hclient;
fs = FCF_TITLEBAR |
FCF_SYSMENU |
FCF_ICON |
FCF_TASKLIST |
FCF_MINBUTTON |
FCF_MAXBUTTON |
FCF_SIZEBORDER
;
if((hab = WinInitialize(0)) == 0)
{
return(FALSE);
}
if((hmq = WinCreateMsgQueue(hab, 0)) == 0)
{
WinTerminate(hab);
return(FALSE);
}
WinRegisterClass(hab,
"VALUETEST",
WinProc,
CS_SIZEREDRAW | CS_SYNCPAINT,
0);
iconx = WinQuerySysValue ( HWND_DESKTOP, SV_CXICON );
icony = WinQuerySysValue ( HWND_DESKTOP, SV_CYICON );
hframe = WinCreateStdWindow(HWND_DESKTOP,
0,
&fs,
"VALUETEST",
"Value Set Example",
0L,
0L,
1,
&hclient);
WinSetWindowPos ( hframe, HWND_TOP, 640/3, 480/3,
(iconx * 10) + 20 , (icony * 5 ) + 20,
SWP_SHOW | SWP_MOVE | SWP_ZORDER | SWP_SIZE );
while(WinGetMsg(hab, &qmsg, 0, 0, 0))
{
WinDispatchMsg(hab, &qmsg);
}
WinDestroyWindow(hframe);
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
return (0);
}
/* **************************************************************
* Function Name: WinProc
*
* Description:
*
* Parameter Description
* --------------------------------------------------------------
*
* Returns:
*
* Comments:
*
***************************************************************/
MRESULT WinProc ( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 )
{
VSCDATA vscdata;
SWP swp[2];
HPS hps;
LONG cx, cy, clientx, clienty;
HWND hother;
HPOINTER hicon;
LONG itemid;
HWND hvs;
switch (msg)
{
case WM_CREATE:
// here we need to create the 2 value sets
// the first we will fill with icons,
// the second we want so that we can show drag and drop between
// them.
// You must create and fill out one of these structs and
// pass it in on the CreateWindow call or else the value set will
// not be created.
vscdata.cbSize = sizeof(vscdata);
vscdata.usRowCount = 3;
vscdata.usColumnCount = 3;
cx = 3 * iconx;
cy = 3 * icony;
hwVs1 = WinCreateWindow ( hwnd,
WC_VALUESET,
"",
WS_VISIBLE | VS_BORDER | VS_ICON,
1,1,
cx, cy,
hwnd,
HWND_TOP,
1,
&vscdata,
0 );
hwVs2 = WinCreateWindow ( hwnd,
WC_VALUESET,
"",
WS_VISIBLE | VS_BORDER | VS_ICON,
cx + 20,1,
cx, cy,
hwnd,
HWND_TOP,
2,
&vscdata,
0 );
// load the icons for the valuesets from the RC file or
// whereever you are getting them from.
// remember that they could be bitmaps, text, colors, ownerdraw,
// or any combination there of. I am using icons for simplicity
barney = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 1);
wilma = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 2);
fred = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 3);
andy = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 4);
bart = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 5);
bear = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 6);
cat = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 7);
face = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 8);
donatelo = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 9);
frame = WinLoadPointer ( HWND_DESKTOP, (HMODULE)NULL, 10);
// place the icons into the first value set
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 1, 1 ), (MPARAM)barney );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 1, 2 ), (MPARAM)wilma );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 1, 3 ), (MPARAM)fred );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 2, 1 ), (MPARAM)andy );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 2, 2 ), (MPARAM)bart );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 2, 3 ), (MPARAM)bear );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 3, 1 ), (MPARAM)cat );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 3, 2 ), (MPARAM)face );
WinSendMsg ( hwVs1, VM_SETITEM,
MPFROM2SHORT ( 3, 3 ), (MPARAM)donatelo);
// set the attributes for the first value set I dont see a way
// of doing this globally for anything other than "TYPE". This could
// be a loop instead of individual calls, but hey.
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs1, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
// set the attributes for the second values set
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 1, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 2, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 1 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 2 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
WinSendMsg ( hwVs2, VM_SETITEMATTR,
MPFROM2SHORT ( 3, 3 ),
MPFROM2SHORT(VIA_ICON | VIA_DRAGGABLE | VIA_DROPONABLE, TRUE) );
// make vlstype known to the system for the drag and drop operations
vlstype = DrgAddStrHandle ("VALUESETTEST_DATA");
return (MRESULT) FALSE;
break;
case WM_SIZE:
WinQueryWindowPos ( hwVs1, &swp[0] );
WinQueryWindowPos ( hwVs2, &swp[1] );
// divide the client area in two and place one on the left
// and one on the right
clientx = SHORT1FROMMP(mp2);
clienty = SHORT2FROMMP(mp2);
swp[0].cx = (clientx / 2) - 10;
swp[0].cy = clienty;
swp[0].fl = SWP_MOVE | SWP_SHOW | SWP_SIZE;
swp[1].x = swp[0].cx + swp[0].x + 20;
swp[1].y = 1;
swp[1].cx = swp[0].cx;
swp[1].cy = swp[0].cy;
swp[0].fl = SWP_MOVE | SWP_SHOW | SWP_SIZE;
WinSetMultWindowPos ( hab, &swp[0], 2 );
return (MRESULT) TRUE;
break;
case WM_CONTROL:
switch (SHORT2FROMMP(mp1))
{
case VN_SELECT:
// Just beep to show that the selected item msg was
// received.
WinAlarm ( HWND_DESKTOP, WA_NOTE );
return (MRESULT) TRUE;
break;
case VN_ENTER:
// switch the icon to the other value set
// this is done by querying the selected item in the
// value set that sent the message.
itemid = (LONG)WinSendMsg ( (HWND)mp2, VM_QUERYSELECTEDITEM,
(MPARAM)0,(MPARAM)0 );
// then get the icon associated with that item
hicon = (HPOINTER)WinSendMsg ( (HWND)mp2, VM_QUERYITEM,
(MPARAM)itemid,NULL );
// figure out the handle for the other value set
hother = ((HWND)mp2 == hwVs1) ? hwVs2 : hwVs1;
// if the item is not occupied in the other value set
if (WinSendMsg ( hother, VM_QUERYITEM, (MPARAM)itemid, NULL) == NULL)
{
// set the item to the icon
WinSendMsg ( hother, VM_SETITEM,(MPARAM)itemid, (MPARAM)hicon );
// delete the icon in current window
WinSendMsg ( (HWND)mp2, VM_SETITEM, (MPARAM)itemid, (MPARAM)NULL);
}
return (MRESULT) TRUE;
break;
case VN_INITDRAG:
// the user has initiated a drag from the value set
// we need to fill out a drag init structure and then initiate
// the drag operation if it is valid.
hvs = WinWindowFromID ( hwnd, SHORT1FROMMP(mp1));
return InitDrag ( hwnd, hvs, mp1, mp2 );
break;
case VN_DRAGOVER:
hvs = WinWindowFromID ( hwnd, SHORT1FROMMP(mp1));
return DragOver ( hwnd, hvs, mp1, mp2 );
break;
case VN_DROP:
hvs = WinWindowFromID ( hwnd, SHORT1FROMMP(mp1));
return DragDrop ( hwnd, hvs, mp1, mp2 );
break;
}
break;
case WM_CONTROLPOINTER:
return (MRESULT) frame;
break;
case WM_PAINT:
hps = WinBeginPaint ( hwnd, 0, 0 );
GpiErase ( hps );
WinEndPaint ( hps );
return (MRESULT) TRUE;
break;
}
return(WinDefWindowProc(hwnd, msg, mp1, mp2));
}
/* **************************************************************
* Function Name: InitDrag
*
* Description:
*
* Parameter Description
* --------------------------------------------------------------
*
* Returns:
*
* Comments:
*
***************************************************************/
MRESULT InitDrag ( HWND hwnd, HWND hvs, MPARAM mp1, MPARAM mp2 )
{
PDRAGINFO pdrg;
PDRAGITEM pdrgitem;
PVSDRAGINIT pvsd;
DRAGIMAGE drgimg;
HPOINTER hicon;
// make sure that the drag operation is over a record
pvsd = (PVSDRAGINIT)mp2;
hicon = (HPOINTER)WinSendMsg ( hvs, VM_QUERYITEM,
MPFROM2SHORT(pvsd->usRow, pvsd->usColumn),
NULL );
if (hicon == NULL)
{
// there is no icon under the mouse.
// there is nothing to drag. We could query the selected item and
// drag that, but we will leave that for an excercise for the reader
// <GRIN>
return (MRESULT)FALSE;
}
// for lots of info on drag and drop, see the 1.3 docs.
pdrg = DrgAllocDraginfo (1);
pdrg->hwndSource = hwnd;
pdrgitem = DrgQueryDragitemPtr ( pdrg, 0 );
if (pdrgitem != NULL)
{
pdrgitem->hwndItem = hvs;
pdrgitem->ulItemID = (ULONG)MPFROM2SHORT(pvsd->usRow, pvsd->usColumn);
pdrgitem->hstrType = vlstype;
pdrgitem->hstrRMF = DrgAddStrHandle ("<DRM_PRINT,DRF_TEXT>");
pdrgitem->hstrContainerName = NULL;
pdrgitem->hstrSourceName = DrgAddStrHandle ("???");
pdrgitem->hstrTargetName = DrgAddStrHandle ("???");
pdrgitem->fsControl = DC_OPEN;
pdrgitem->fsSupportedOps = DO_MOVEABLE;
}
drgimg.cb = sizeof(DRAGIMAGE);
drgimg.hImage = hicon;
drgimg.fl = DRG_ICON;
drgimg.cxOffset = 0;
drgimg.cyOffset = 0;
DrgDrag ( hwnd, pdrg, &drgimg, 1, VK_BUTTON2, NULL );
// note: at this point the drag operation is complete. Be very carefull,
// all of the drag operations and messages ocurred reentrantly in this
// window proc.
DrgFreeDraginfo ( pdrg );
return (MRESULT) TRUE;
}
/* **************************************************************
* Function Name: DragOver
*
* Description:
*
* Parameter Description
* --------------------------------------------------------------
*
* Returns:
*
* Comments:
*
***************************************************************/
MRESULT DragOver ( HWND hwnd, HWND hvs, MPARAM mp1, MPARAM mp2 )
{
PDRAGITEM pdrgitem;
PVSDRAGINFO pvsd;
PDRAGINFO prdi;
MPARAM itemid;
pvsd = (PVSDRAGINFO) mp2;
prdi = pvsd->pDragInfo;
if ( DrgAccessDraginfo ( prdi ) )
{
pdrgitem = DrgQueryDragitemPtr(prdi, 0);
// is the drag item a type of item that we understand?
if (pdrgitem->hstrType == vlstype)
{
DrgFreeDraginfo ( prdi );
// check to see if we can drop over the current item
// the item must be empty in this example
itemid = MPFROM2SHORT(pvsd->usRow, pvsd->usColumn );
if (WinSendMsg ( hvs, VM_QUERYITEM, itemid, NULL) == NULL)
{
// yes we can accept it.
return ( MPFROM2SHORT ( DOR_DROP, DO_MOVE ) );
}
// its only droppable if the user moves over an empty slot
return ( MPFROM2SHORT ( DOR_NODROP, DO_MOVE ) );
}
DrgFreeDraginfo ( prdi );
}
// we could never accept it even over an empty slot
return ( MPFROM2SHORT ( DOR_NEVERDROP, DO_MOVE ) );
}
/* **************************************************************
* Function Name: DragDrop
*
* Description:
*
* Parameter Description
* --------------------------------------------------------------
*
* Returns:
*
* Comments:
*
***************************************************************/
MRESULT DragDrop ( HWND hwnd, HWND hvs, MPARAM mp1, MPARAM mp2 )
{
PDRAGITEM pdrgitem;
PVSDRAGINFO pvsd;
PDRAGINFO prdi;
HPOINTER hicon;
HWND hwsource;
MPARAM itemid;
// look at the CN_DROP message parms for details of a VSDRAGINFO struct
pvsd = (PVSDRAGINFO) mp2;
prdi = pvsd->pDragInfo;
if ( DrgAccessDraginfo ( prdi ))
{
pdrgitem = DrgQueryDragitemPtr(prdi, 0);
// we need to take the icon out of the source window
// and insert it into the designated value set at the
// mouse location. Notice that source and dest could be the same
// also notice that we are sure that the spot it is over is a valid
// spot to drop it and that the dragged item is a valid type. This
// was all checked in the dragover message.
// dest location
itemid = MPFROM2SHORT(pvsd->usRow, pvsd->usColumn);
// source value set
hwsource = pdrgitem->hwndItem;
// get the icon from the source window
hicon = (HPOINTER)WinSendMsg ( (HWND)hwsource, VM_QUERYITEM,
(MPARAM)pdrgitem->ulItemID, NULL );
// set the item to the icon in the dest value set
WinSendMsg ( hvs, VM_SETITEM,(MPARAM)itemid, (MPARAM)hicon );
// delete the icon in source window
WinSendMsg ( (HWND)hwsource, VM_SETITEM, (MPARAM)pdrgitem->ulItemID,
(MPARAM)NULL);
// clean up.
DrgDeleteDraginfoStrHandles ( prdi );
DrgFreeDraginfo ( prdi );
}
return((MPARAM)NULL);
}