home *** CD-ROM | disk | FTP | other *** search
/ Supergames for Windows 3 / wingames3.zip / wingames3 / U-Z / YALIFE / SHAPEWND.CPP < prev    next >
C/C++ Source or Header  |  1993-09-08  |  7KB  |  195 lines

  1. // (c) Jean MICHEL June 1993 -- any modifications must be signaled to the
  2. // author
  3.  
  4. // class TShapeWindow is for the 'Shapes' window
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <owl.h>
  9. #include <commdlg.h>
  10. #include <dir.h>
  11. #include <io.h>
  12. #include <string.h>
  13. #include <inputdia.h>
  14. #include <dos.h>
  15. #include <listbox.h>
  16. #include <button.h>
  17. #include <static.h>
  18. #include "cell.h"
  19. #include "shapewnd.h"
  20. #include "life.h"
  21. #include "efns.h"
  22.  
  23. cellpop selected_shape("o");
  24. static cellpop local_shape("o");
  25. int listbox_index;
  26. const RECT r={20,195,220,395};
  27.  
  28. TShapeWindow::TShapeWindow(PTWindowsObject AParent) :
  29.    TWindow(AParent, "Select a shape")
  30. { DisableAutoCreate();
  31.   Attr.Style |= WS_POPUPWINDOW | WS_CAPTION;
  32.   Attr.X = 0;
  33.   Attr.Y = 0;
  34.   Attr.W = 240;
  35.   Attr.H = 480;
  36.   new TButton(this, ID_BUTTON7, "Load new library",   20, 15, 200, 30, FALSE);
  37.   ListBox = new TListBox(this, ID_LISTBOX, 20, 60, 200, 75);
  38.   ListBox->Attr.Style^=LBS_SORT;
  39.   new TButton(this, ID_BUTTON3, "Load from file", 20, 140, 140, 30, FALSE);
  40.   new TButton(this, ID_BUTTON4, "Line",  180, 140, 40, 30, FALSE);
  41.   PreviewMsg=new TStatic(this, -1,         "Preview:",      20, 175, 160, 19, 0);
  42.   new TButton(this, ID_BUTTON5, "Mirror",  20, 410, 65, 30, FALSE);
  43.   new TButton(this, ID_BUTTON6, "Rotate",  105, 410, 65, 30, FALSE);
  44.   new TButton(this, ID_BUTTON1, "Ok",      190, 410, 30, 30, TRUE);
  45. }
  46.  
  47. void TShapeWindow::SetupList()
  48. { char *n;
  49.   ListBox->ClearList();
  50.   for(int j=0;n=getshapename(j);j++)ListBox->AddString(n);
  51.   ListBox->SetSelIndex(listbox_index);
  52. }
  53.  
  54. void TShapeWindow::SetupWindow()
  55. { TWindow::SetupWindow();
  56.   SetupList();
  57.   InvalidateRect(HWindow,&r,TRUE);
  58. };
  59.  
  60. void TShapeWindow::HandleListBoxMsg(RTMessage Msg)
  61. { cellpop temp=cellpop(getshape(listbox_index=ListBox->GetSelIndex()));
  62.   if(temp.error())return;
  63.   local_shape=temp;
  64.   selected_shape=local_shape;
  65.   InvalidateRect(HWindow,&r,TRUE);
  66.   if(Msg.LP.Hi==LBN_DBLCLK)HandleButton1Msg(Msg);
  67. }
  68.  
  69. void TShapeWindow::HandleButton1Msg(RTMessage)
  70. { selected_shape=local_shape;}
  71.  
  72. void TShapeWindow::Paint(HDC DC, PAINTSTRUCT&)
  73. { int bx=local_shape.firstx().cellx(),by=local_shape.firsty().celly(),
  74.       ex=local_shape.lastx().cellx(),ey=local_shape.lasty().celly();
  75.   int magnification= (r.right+1-r.left)/(ex+1-bx);
  76.   if(magnification> (r.bottom+1-r.top)/(ey+1-by))
  77.      magnification=(r.bottom+1-r.top)/(ey+1-by);
  78.   if(magnification==0)magnification=1;
  79.   if(magnification>25)magnification=25;
  80.   char prevmsg[20];
  81.   sprintf(prevmsg,"Preview:      Mag:%d",magnification);
  82.   PreviewMsg->SetText(prevmsg);
  83.   int xoffset=((r.right+1+r.left)-magnification*(ex+1-bx))/2;
  84.   if(xoffset<r.left)xoffset=r.left;
  85.   int yoffset=((r.bottom+1+r.top)-magnification*(ey+1-by))/2;
  86.   if(yoffset<r.top)yoffset=r.top;
  87.   int border=magnification>3;
  88.   MoveTo(DC,r.left-1,r.top-1);
  89.   LineTo(DC,r.left-1,r.bottom+1);
  90.   LineTo(DC,r.right+1,r.bottom+1);
  91.   LineTo(DC,r.right+1,r.top-1);
  92.   LineTo(DC,r.left-1,r.top-1);
  93.   SelectObject(DC,Pens[SET]);
  94.   SelectObject(DC,Brushes[SET]);
  95.   for(int iw=0;iw<local_shape.pop;iw++)
  96.   { int x=xoffset+magnification*(local_shape.v[iw].cellx()-bx);
  97.     int y=yoffset+magnification*(local_shape.v[iw].celly()-by);
  98.     if(magnification>1)
  99.       Rectangle(DC,x,y,x+magnification-border,y+magnification-border);
  100.     else SetPixel(DC,x,y,Colors[SET]);
  101.   }
  102.   HPEN pen=CreatePen(PS_DOT,1,RGB(100,100,100));
  103.   SelectObject(DC,pen);
  104.   xoffset+=magnification/2-1-bx*magnification;
  105.   yoffset+=magnification/2-1-by*magnification;
  106.   MoveTo(DC,xoffset,r.top-1);  LineTo(DC,xoffset,r.bottom+1);
  107.   MoveTo(DC,r.left-1,yoffset);  LineTo(DC,r.right-1,yoffset);
  108.   DeleteObject(pen);
  109. }
  110.  
  111. char *win_getfilename(HWND HWindow,char *szTemp,char *title,char *defext)
  112. { OPENFILENAME ofnTemp;
  113.   static char FileName[MAXPATH];
  114. /*
  115. Some Windows structures require the size of themselves in an effort to
  116. provide backward compatibility with future versions of Windows.  If the
  117. lStructSize member is not set the call to GetOpenFileName() will fail.
  118. */
  119.   ofnTemp.lStructSize = sizeof( OPENFILENAME );
  120.   ofnTemp.hwndOwner = HWindow;        // An invalid hWnd causes non-modality
  121.   ofnTemp.hInstance = 0;
  122.   ofnTemp.lpstrFilter = (LPSTR)szTemp;    // See previous note concerning string
  123.   ofnTemp.lpstrCustomFilter = NULL;
  124.   ofnTemp.nMaxCustFilter = 0;
  125.   ofnTemp.nFilterIndex = 2;
  126.   ofnTemp.lpstrFile = FileName;    // Stores the result in this variable
  127.   FileName[0]=0;
  128.   ofnTemp.nMaxFile = sizeof( FileName );
  129.   ofnTemp.lpstrFileTitle = NULL;
  130.   ofnTemp.nMaxFileTitle = 0;
  131.   ofnTemp.lpstrInitialDir = NULL;
  132.   ofnTemp.lpstrTitle = title;                // Title for dialog
  133.   ofnTemp.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
  134.   ofnTemp.nFileOffset = 0;
  135.   ofnTemp.nFileExtension = 0;
  136.   ofnTemp.lpstrDefExt = defext;
  137.   ofnTemp.lCustData = NULL;
  138.   ofnTemp.lpfnHook = NULL;
  139.   ofnTemp.lpTemplateName = NULL;
  140. /*
  141. If the call to GetOpenFileName() fails you can call CommDlgExtendedError()
  142. to retrieve the type of error that occured.
  143. */
  144.   if(GetOpenFileName( &ofnTemp ) != TRUE)
  145.   { DWORD Errval=CommDlgExtendedError();
  146.     if(Errval!=0)   // 0 value means user selected Cancel
  147.       efns(WARNING_,"GetOpenFileName returned Error %ld",Errval);
  148.     return NULL;
  149.   }
  150.   return FileName;
  151. }
  152.  
  153. void TShapeWindow::HandleButton3Msg(RTMessage)
  154. { char *FileName=win_getfilename(HWindow,"All Files (*.*)\0*.*\0Life Files (*.lfe)\0*.lfe\0",
  155.   "Load Shape","lfe");
  156.   if(FileName)
  157.   {  cellpop temp=readfile(FileName);
  158.      if(temp.error())return;
  159.      local_shape=temp;
  160.      selected_shape=local_shape;
  161.      InvalidateRect(HWindow,&r,TRUE);
  162.   }
  163. }
  164.  
  165. void TShapeWindow::HandleButton4Msg(RTMessage)
  166. { char InputText[6];static int lg=10;
  167.   sprintf(InputText, "%d", lg);
  168.   if ( GetApplication()->ExecDialog(
  169.   new TInputDialog(this, "Input Size of Line of Cells:","", InputText, sizeof InputText)
  170.   ) == IDOK )lg = atoi(InputText);
  171.   local_shape=cellpop(lg);
  172.   selected_shape=local_shape;
  173.   InvalidateRect(HWindow,&r,TRUE);
  174. }
  175.  
  176. void TShapeWindow::HandleButton5Msg(RTMessage)
  177. { local_shape.transform(1);
  178.   selected_shape=local_shape;
  179.   InvalidateRect(HWindow,&r,TRUE);
  180. }
  181.  
  182. void TShapeWindow::HandleButton6Msg(RTMessage)
  183. { local_shape.transform(5);
  184.   selected_shape=local_shape;
  185.   InvalidateRect(HWindow,&r,TRUE);
  186. }
  187.  
  188. void TShapeWindow::HandleButton7Msg(RTMessage)
  189. { char *FileName=win_getfilename(HWindow,"All Files (*.*)\0*.*\0Life Libraries (*.lib)\0*.lib\0",
  190.   "Load Shape Library","lib");
  191.   if(FileName)readshapelib(FileName);
  192.   SetupList();
  193. }
  194.  
  195.