/* * * Copyright (c) 1996 * Knowledge Science Institute, University of Calgary * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. The Knowledge Science Institute makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */ #include //#include int trackPopupMenu(MENU& menu, POINT& pt, char* menuName, int len) { HWND hwnd = GetActiveWindow(); HWND hwnd2 = GetTopWindow(hwnd); DEVICE dev; dev = ::GetDC(hwnd2); ::LPtoDP(dev,&pt,1); ::ReleaseDC(hwnd2,dev); ::ClientToScreen(hwnd2,&pt); int ret = ::TrackPopupMenu(menu,TPM_RIGHTBUTTON|TPM_RETURNCMD,pt.x,pt.y,0,hwnd2,NULL); if (menuName) { ::GetMenuString(menu,(UINT)ret,menuName,len,MF_BYCOMMAND); } return ret; } RECTANGLE trackRectangle(RECTANGLE r, COMPASEPOINTS p, WINDOWHANDLE hwnd) { if (hwnd==0) hwnd = GetTopWindow(GetActiveWindow()); DEVICE windDev = ::GetDC(hwnd); DEVICE dev = ::GetDC(0); //convert r to system coordinates ::LPtoDP(windDev,(POINT*)&r,2); ::ClientToScreen(hwnd,(POINT*)&r); ::ClientToScreen(hwnd,(POINT*)&r.right); //set up the burshes and pens HBRUSH oldBrush = (HBRUSH)SelectObject(dev,GetStockObject(HOLLOW_BRUSH)); int oldROP = SetROP2(dev,R2_NOT); HPEN newPen = CreatePen(PS_SOLID,3,0); HPEN oldPen = (HPEN)SelectObject(dev,newPen); //set up the cursor position POINT cur; cur.x = p&W?r.left:r.right; cur.y = p&N?r.top:r.bottom; SetCursorPos(cur.x,cur.y); //loop, drawing the rectangle attached to the mouse MRectangle(dev,r); int pushed = 0; while (1) { if (GetAsyncKeyState(VK_LBUTTON)<0) pushed = 1; //if mouse button down else if (pushed) break; POINT cur2; GetCursorPos(&cur2); if (!(cur.x==cur2.x && cur.y==cur2.y)) { MRectangle(dev,r); if (p&W) r.left = cur2.x; if (p&N) r.top = cur2.y; if (p&E) r.right = cur2.x; if (p&S) r.bottom = cur2.y; MRectangle(dev,r); cur.x = cur2.x; cur.y = cur2.y; } } //cleanup by undrawing the rectangle and releaseing resources MRectangle(dev,r); SelectObject(dev,oldPen); DeleteObject(newPen); SetROP2(dev,oldROP); SelectObject(dev,oldBrush); //convert the final rectangle back to logical coordinates ::ScreenToClient(hwnd,(POINT*)&r); ::ScreenToClient(hwnd,(POINT*)&r.right); ::DPtoLP(windDev,(POINT*)&r,2); //normalize the rectangle if (r.rightr.right) r.right = r.left+4; if (r.top+4>r.bottom) r.bottom = r.top+4; //release the device handles ::ReleaseDC(0,windDev); ::ReleaseDC(hwnd,dev); return r; } POINT trackElasticLine(POINT anchor, POINT movePoint, WINDOWHANDLE hwnd) { if (hwnd==0) hwnd = GetTopWindow(GetActiveWindow()); DEVICE windDev = ::GetDC(hwnd); DEVICE dev = ::GetDC(0); //convert r to system coordinates ::LPtoDP(windDev,&anchor,1); ::LPtoDP(windDev,&movePoint,1); ::ClientToScreen(hwnd,&anchor); ::ClientToScreen(hwnd,&movePoint); //set up the burshes and pens HBRUSH oldBrush = (HBRUSH)SelectObject(dev,GetStockObject(HOLLOW_BRUSH)); int oldROP = SetROP2(dev,R2_NOT); HPEN newPen = CreatePen(PS_SOLID,3,0); HPEN oldPen = (HPEN)SelectObject(dev,newPen); //set up the cursor position SetCursorPos(movePoint.x,movePoint.y); //loop, drawing the line attached to the mouse MDrawLine(dev,anchor,movePoint); int pushed = 0; while (1) { if (GetAsyncKeyState(VK_LBUTTON)<0) pushed = 1; //if mouse button down else if (pushed) break; POINT cur2; GetCursorPos(&cur2); if (!(movePoint.x==cur2.x && movePoint.y==cur2.y)) { MDrawLine(dev,anchor,movePoint); movePoint.x = cur2.x; movePoint.y = cur2.y; MDrawLine(dev,anchor,movePoint); } } //cleanup by undrawing the line and releaseing resources MDrawLine(dev,anchor,movePoint); SelectObject(dev,oldPen); DeleteObject(newPen); SetROP2(dev,oldROP); SelectObject(dev,oldBrush); //convert the final point back to logical coordinates ::ScreenToClient(hwnd,&movePoint); ::DPtoLP(windDev,&movePoint,1); //release the device handles ::ReleaseDC(0,windDev); ::ReleaseDC(hwnd,dev); return movePoint; } int askUser_string(const char* title, const char* prompt, char* buf, unsigned int siz) { TInputDialog dia(0,title,prompt,buf,siz); int ret = -1; int stat; switch (stat=(dia.Execute())) { #pragma warn .pia case IDOK: ret = 0; break; case IDCANCEL: ret = 1; break; default: //error(0,'E',"getNonNegInt(): Cannot open input dialog box, status=%d",stat); ret = -1; } return ret; } #define SETFILL(fill) \ HBRUSH oldHB=NULL, hB; \ if (fill) { \ hB = CreateSolidBrush(*fill); \ if (hB) oldHB = SelectObject(device,hB); \ } #define RESETFILL(fill) \ if (fill) { \ if (oldHB) SelectObject(device,oldHB); \ if (hB) DeleteObject(hB); \ } #define SETLINE(line,width,style) \ HPEN oldHP=NULL, hP; \ if (line) { \ hP = CreatePen(style,width,*line); \ if (hP) oldHP = SelectObject(device,hP); \ } #define RESETLINE(line) \ if (line) { \ if (oldHP) SelectObject(device,oldHP); \ if (hP) DeleteObject(hP); \ } int MRectangle(DEVICE device, RECTANGLE rect, COLOR* fill, COLOR* border, int width, unsigned long style) { SETFILL(fill); SETLINE(border,width,style); int ret = Rectangle(device,rect.left,rect.top,rect.right,rect.bottom); RESETFILL(fill); RESETLINE(border); return !ret; } int MEllipse(DEVICE device, RECTANGLE rect, COLOR* fill, COLOR* border, int width, unsigned long style) { SETFILL(fill); SETLINE(border,width,style); int ret = Ellipse(device,rect.left,rect.top,rect.right,rect.bottom); RESETFILL(fill); RESETLINE(border); return !ret; } int MRoundRect(DEVICE device, RECTANGLE rect, COORDINATE_INDICI w, COORDINATE_INDICI h, COLOR* fill, COLOR* border, int width, unsigned long style) { SETFILL(fill); SETLINE(border,width,style); int ret = RoundRect(device,rect.left,rect.top,rect.right,rect.bottom,w,h); RESETFILL(fill); RESETLINE(border); return !ret; } int MPolygon(DEVICE device, POINT* pVector, int count, COLOR* fill, COLOR* border, int width, unsigned long style) { SETFILL(fill); SETLINE(border,width,style); int ret = Polygon(device,pVector,count); RESETFILL(fill); RESETLINE(border); return !ret; } int MDrawLine(DEVICE device, POINT p1, POINT p2, COLOR* color, int width, unsigned long style) { SETLINE(color,width,style); MoveToEx(device,p1.x,p1.y,NULL); int ret = LineTo(device,p2.x,p2.y); RESETLINE(color); return !ret; } bool MIsPointInRectangle(POINT p, RECTANGLE r) { return p.x>=r.left && p.x<=r.right && p.y>=r.top && p.y<=r.bottom; } bool MIsPointInEllipse(POINT p, RECTANGLE r) { HRGN hrgn = CreateEllipticRgn(r.left,r.top,r.right,r.bottom); return PtInRegion(hrgn,p.x,p.y); } bool MIsPointInRoundRect(POINT p, RECTANGLE r, COORDINATE_INDICI w, COORDINATE_INDICI h) { HRGN hrgn = CreateRoundRectRgn(r.left,r.top,r.right,r.bottom,w,h); return PtInRegion(hrgn,p.x,p.y); } bool MIsPointInPolygon(POINT p, POINT* pVector, int count) { HRGN hrgn = CreatePolygonRgn(pVector,count,WINDING); return PtInRegion(hrgn,p.x,p.y); }