home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / METAFD.PRG < prev    next >
Text File  |  1994-05-27  |  11KB  |  396 lines

  1. ////////////////////////////
  2. //
  3. //  Clip-4-Win metafile demo
  4. //
  5. //  Copyright (C) 1992 Skelton Software, Kendal Cottage, Hillam, Leeds, UK.
  6. //  All Rights Reserved.
  7. //
  8. //
  9. //  Compile:    rmake metafd
  10. //
  11. ////////////////////////////
  12.  
  13.  
  14. #include "windows.ch"
  15.  
  16. // handle Clipper 10-char limit on external names
  17.  
  18. #xtranslate   GetMetaFileBits   =>      GetMFBits
  19. #xtranslate   SetMetaFileBitsBetter   =>      SetMFBBett
  20.  
  21. static  cAppName := "Clip-4-Win"
  22. static  hWnd, hInst, hPrevInst, nCmdShow
  23. static  cText := ""
  24. static  aWnd := {}, aAction := {}   // for event handlers
  25.  
  26. static hMetaFile := 0  //handle of metafile
  27. static cMetaFile := "UNTITLED"
  28.  
  29. static nOriginX :=0, nOriginY:=0  //for DoPlay() 
  30.  
  31. function main()
  32. local   hMenu, nEvent
  33.  
  34. hWnd = WinSetup(cAppName, "Clip-4-Win metafile demo")
  35. hMenu = MenuSetup()
  36. HideCaret(hWnd)
  37. AddHandler(hWnd, {|nEvent| MainEvent(nEvent)})
  38.  
  39. do while .t.
  40.     do while (nEvent := ChkEvent()) == EVENT_NONE
  41.         // some "background" processing could go here
  42.     enddo
  43.     HandleEvent(nEvent)
  44.     do case
  45.     case nEvent == EVENT_QUIT
  46.         DoExit()
  47.     endcase
  48. enddo
  49.  
  50. return 0
  51.  
  52.  
  53. procedure MainEvent(nEvent)
  54. local   hOldWnd ,hDC
  55. do case
  56. case nEvent == EVENT_REDRAW
  57.  
  58.     
  59.     hOldWnd = SelectWindow(hWnd)
  60.     @ 0,0 clear to maxrow(),maxcol() 
  61.     SelectWindow(hOldWnd) 
  62.     
  63.     if hMetaFile != 0
  64.        hDC = GetDC(hWnd)
  65.        PlayMetaFile(hDC, hMetaFile)
  66.        ReleaseDC(hWnd, hDC)
  67.     endif      
  68.  
  69.     nOriginX = 0    //initialize variables for DoPlay()
  70.     nOriginY = 0
  71.  
  72.     
  73. endcase
  74. return
  75.  
  76.  
  77. procedure DoAbout()
  78. MessageBox( , "Clip-4-Win Metafile Demo", "Info", MB_OK)
  79. return
  80.  
  81.  
  82. procedure DoExit()
  83. MessageBox(0, "Thanks for running this Clip-4-Win demo", "Clip-4-Win Demo Exiting", MB_OK)
  84. quit
  85. return
  86.  
  87.  
  88.  
  89. /*
  90.    Function: DoNew()
  91. */
  92. static function DoNew()
  93. local hMenu, hOldWnd
  94.        
  95.     hOldWnd = SelectWindow(hWnd)
  96.     @ 0,0 clear to maxrow(),maxcol() 
  97.     SelectWindow(hOldWnd) 
  98.     
  99.     //───── disable the "play", "save" and "saveas" menu items 
  100.     hMenu := GetMenu(hWnd)       // retrieve reference to main menu
  101.     EnableMenuItem(hMenu, "play", MF_DISABLED+MF_GRAYED)
  102.     EnableMenuItem(hMenu, "save", MF_DISABLED+MF_GRAYED)
  103.     EnableMenuItem(hMenu, "saveas", MF_DISABLED+MF_GRAYED)  
  104.     DrawMenuBar(hWnd)
  105.     if hMetaFile != 0
  106.      DeleteMetaFile(hMetaFile)
  107.      hMetaFile = 0
  108.      cMetaFile = "UNTITLED"   
  109.     endif     
  110.       
  111. return nil
  112.  
  113. /*
  114.    Function: DoOpen()
  115. */
  116. static function DoOpen()
  117. local cFile := GetOpenFileName(, "*.wmf", "Select a metafile")
  118. local hMenu
  119. local hTempMetaFile
  120. if cFile <> NIL
  121.   if (hTempMetaFile := GetMetaFile(cFile)) != 0
  122.      
  123. //───── enable  the "play", "save" and "saveas" menu items  
  124.       hMenu := GetMenu(hWnd)       // retrieve reference to main menu
  125.       EnableMenuItem(hMenu, "play", MF_ENABLED)
  126.       EnableMenuItem(hMenu, "save", MF_ENABLED)
  127.       EnableMenuItem(hMenu, "saveas", MF_ENABLED)
  128.       DrawMenuBar(hWnd)
  129.       if hMetaFile != 0
  130.        DeleteMetaFile(hMetaFile)
  131.       endif 
  132.       hMetaFile = hTempMetaFile    
  133.       cMetaFile = cFile  
  134.    else
  135.      MessageBox(hWnd, cFile + " is not a valid metafile", "Error", ;
  136.               MB_ICONEXCLAMATION + MB_OK)
  137.    endif 
  138. endif
  139. return nil
  140.  
  141. /*
  142.    Function: DoSave()
  143. */
  144. static function DoSave()
  145. local hTempMetaFile
  146. local cFile := IIF(cMetaFile == "UNTITLED";
  147.        ,GetSaveFileName(, "*.wmf", "Enter a metafile name");
  148.        ,cMetaFile) 
  149.  
  150. if cFile <> NIL
  151.   if (hTempMetaFile := CopyMetaFile(hMetaFile, cFile)) != 0
  152.      
  153.       DeleteMetaFile(hMetaFile)   // free memory
  154.       
  155.       hMetaFile = hTempMetaFile   //use the handle of new metafile
  156.    else
  157.      MessageBox(hWnd, cFile + " is not a valid filename", "Error", ;
  158.               MB_ICONEXCLAMATION + MB_OK)
  159.    endif 
  160. endif
  161. return nil
  162.  
  163.  
  164.  
  165. /*
  166.    Function: DoSaveAs()
  167. */
  168. static function DoSaveAs()
  169. local cFile := GetSaveFileName(, "*.wmf", "Select a metafile")
  170. local hMenu, hTempMetaFile
  171. if cFile <> NIL
  172.   if (hTempMetaFile := CopyMetaFile(hMetaFile, cFile)) != 0
  173.      
  174.       DeleteMetaFile(hMetaFile)   // free memory
  175.       
  176.       hMetaFile = hTempMetaFile   //use the handle of new metafile
  177.       cMetaFile = cFile  
  178.    else
  179.      MessageBox(hWnd, cFile + " is not a valid filename", "Error", ;
  180.               MB_ICONEXCLAMATION + MB_OK)
  181.    endif 
  182. endif
  183. return nil
  184.  
  185. /*
  186.    Function: DoCreate()
  187. */
  188. function DoCreate()
  189. local  hDCMeta, hDC
  190. local  nX1:=40, nY1:=150, nX2:=100, nY2:=40, i   
  191. local hMenu
  192.  
  193.   
  194.   hDCMeta = CreateMetaFile()   //creates a device context to record
  195.  
  196.                                 // a metafile      
  197.   for i= 1 to 12
  198.    MoveTo(hDCMeta, nX1 := nX1 + i*5, nY1 := nY1 + i*3)
  199.    LineTo(hDCMeta, nX2 := nX2 - i*8, nY2 := nY2 + i*2)  
  200.   next 
  201.   if hMetaFile != 0
  202.    DeleteMetaFile(hMetaFile)
  203.   endif
  204.   
  205.   hMetaFile = CloseMetaFile( hDCMeta )
  206.   hDC = GetDC(hWnd)
  207.   PlayMetaFile(hDC, hMetaFile)
  208.   ReleaseDC(hWnd, hDC)
  209.   
  210. //───── enable  the "play", "save" and  "saveas"  menu items  
  211.       hMenu := GetMenu(hWnd)       // retrieve reference to main menu
  212.       EnableMenuItem(hMenu, "play", MF_ENABLED)
  213.       EnableMenuItem(hMenu, "save", MF_ENABLED)
  214.       EnableMenuItem(hMenu, "saveas", MF_ENABLED)
  215.    DrawMenuBar(hWnd) 
  216.   return nil
  217.  
  218.  
  219. /*
  220.    Function: DoPlay()
  221. */
  222. static function DoPlay()
  223.  
  224. local  hDC   
  225.  
  226.  if ((hDC := GetDC(hWnd)) > 0) 
  227.   SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)    
  228.   
  229.    PlayMetaFile(hDC,hMetaFile)    
  230.    ReleaseDC(hWnd, hDC)
  231.  else
  232.    MessageBox(hWnd, " Error while creating device context", "Error", ;
  233.               MB_ICONEXCLAMATION + MB_OK)
  234.  endif 
  235.  
  236. return nil
  237.  
  238.  
  239. /*
  240.    Function: DoGetSet()
  241. */
  242. static function DoGetSet()
  243.  
  244. local  hDC   
  245. local  hGlobal
  246.  
  247.  if ((hDC := GetDC(hWnd)) > 0) 
  248.    SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)    
  249.   
  250.    hGlobal = GetMetaFileBits(hMetaFile) 
  251.    
  252.    PlayMetaFile(hDC,hGlobal)    // play global memory object 
  253.                                 // that is containing the metafile
  254.    
  255.      
  256.    ReleaseDC(hWnd, hDC)
  257.  
  258.    hMetaFile = SetMetaFileBits( hGlobal)  //return to usual metafile handle
  259.  else
  260.      MessageBox(hWnd, " Error while creating device context", "Error", ;
  261.               MB_ICONEXCLAMATION + MB_OK)
  262.  endif 
  263. return nil
  264.  
  265. /*
  266.    Function: DoGetSetBetter()
  267. */
  268. static function DoGetSetBetter()
  269.  
  270. local  hDC   
  271. local  hGlobal
  272.  
  273.  if ((hDC := GetDC(hWnd)) > 0) 
  274.    SetWindowOrigin(hDC, nOriginX :=nOriginX + 10, nOriginY :=nOriginY+10)    
  275.   
  276.    hGlobal = GetMetaFileBits(hMetaFile) 
  277.    
  278.    PlayMetaFile(hDC,hGlobal)    // play global memory object 
  279.                                 // that is containing the metafile
  280.    ReleaseDC(hWnd, hDC)
  281.    hMetaFile = SetMetaFileBitsBetter( hGlobal)  // get metafile handle
  282.                                  //that will be owned by GDI so it
  283.                                  //could be used by multiple applications              
  284.  
  285.    //return to usual the metafile handle
  286.    
  287.    hGlobal = GetMetaFileBits(hMetaFile) 
  288.    hMetaFile = SetMetaFileBits( hGlobal)  //return to usual metafile handle
  289.  else
  290.      MessageBox(hWnd, " Error while creating device context", "Error", ;
  291.               MB_ICONEXCLAMATION + MB_OK)
  292.  endif 
  293. return nil
  294.  
  295.  
  296.  
  297. function MenuSetup()
  298. local   hWnd := SelectWindow(), hMenu, hPopupMenu
  299.  
  300. if (hMenu := GetMenu(hWnd)) != nil
  301.     DestroyMenu(hMenu)
  302. endif
  303.  
  304. // do new one (forget old value)
  305. hMenu = CreateMenu()
  306. hPopupMenu = CreatePopupMenu()
  307. AppendMenu(hMenu, "file", MF_ENABLED + MF_POPUP, "&File", hPopupMenu)
  308. AppendMenu(hPopupMenu, "new", MF_ENABLED + MF_STRING, "&New", {|| DoNew()})
  309. AppendMenu(hPopupMenu, "open", MF_ENABLED + MF_STRING, "&Open", {|| DoOpen()})
  310. AppendMenu(hPopupMenu, "save", MF_GRAYED + MF_STRING, "&Save", {|| DoSave()})
  311. AppendMenu(hPopupMenu, "saveas", MF_GRAYED + MF_STRING, "Save &As", {|| DoSaveAs()})
  312. AppendMenu(hPopupMenu, "", MF_SEPARATOR)
  313. AppendMenu(hPopupMenu, "exit", MF_ENABLED + MF_STRING, "E&xit", {|| DoExit()})
  314.  
  315.  
  316. AppendMenu(hMenu, "play", MF_GRAYED + MF_STRING, "&Play", {|| DoPlay()})
  317. hPopupMenu = CreatePopupMenu()
  318. AppendMenu(hMenu, "create", MF_ENABLED + MF_POPUP, "&Create", hPopupMenu)
  319. AppendMenu(hPopupMenu, "createmetafile \t ALT+C", MF_ENABLED + MF_STRING, "&Create metafile", {|| DoCreate()})
  320.  
  321. hPopupMenu = CreatePopupMenu()
  322. AppendMenu(hMenu, "test", MF_ENABLED + MF_POPUP, "&Test", hPopupMenu)
  323. AppendMenu(hPopupMenu, "get/set", MF_ENABLED + MF_STRING, "Get/set metafile bits", {|| DoGetSet()})
  324. AppendMenu(hPopupMenu, "get/setbetter", MF_ENABLED + MF_STRING, "Get/set(better) metafile bits", {|| DoGetSetBetter()})
  325.  
  326. hPopupMenu = CreatePopupMenu()
  327. AppendMenu(hMenu, "help", MF_ENABLED + MF_POPUP, "&Help", hPopupMenu)
  328. AppendMenu(hPopupMenu, "about", MF_ENABLED + MF_STRING, "&About", {|| DoAbout()})
  329. SetMenu(hWnd, hMenu)
  330.  
  331. return hMenu
  332.  
  333.  
  334. function AddHandler(hWnd, bAction)  // --> nId  (for use with DelHandler)
  335. aadd(aWnd, hWnd)
  336. aadd(aAction, bAction)
  337. return len(aWnd)
  338.  
  339.  
  340. procedure DelHandler(nId)
  341. adel(aWnd, nId)
  342. asize(aWnd, len(aWnd) - 1)
  343. adel(aAction, nId)
  344. asize(aAction, len(aAction) - 1)
  345. return
  346.  
  347.  
  348. procedure HandleEvent(nEvent)
  349. local   hWnd := _LasthWnd(), i := 0
  350. do while (i := ascan(aWnd, hWnd, ++i)) != 0
  351.     eval(aAction[i], nEvent)
  352. enddo
  353. if nEvent == EVENT_DESTROY
  354.     // clean up, so the event handler needn't bother
  355.     do while (i := ascan(aWnd, hWnd)) != 0
  356.         DelHandler(i)
  357.     enddo
  358. endif
  359. return
  360.  
  361.  
  362. function WinNew(cAppName, cTitle, nX, nY, nWidth, nHeight)
  363. local   hWin, hInst, nCmdShow
  364.  
  365. hInst = _GetInstance()
  366. nCmdShow = _GetnCmdShow()
  367.  
  368. hWin = CreateWindow(cAppName,       ;   // window class
  369.             cTitle,         ;   // caption for title bar
  370.             WS_OVERLAPPEDWINDOW,;   // window style
  371.             nX,         ;   // x co-ordinate
  372.             nY,         ;   // y co-ordinate
  373.             nWidth,     ;   // width
  374.             nHeight,        ;   // height
  375.             hWnd,       ;   // hWnd of parent
  376.             0,          ;   // hMenu of menu (none yet)
  377.             hInst)          // our own app instance
  378.  
  379. if hWin == 0
  380.     // probably out of resources
  381.     MessageBox( , "Can't create window", "Error", MB_OK)
  382.     return nil
  383. endif
  384.  
  385. HideCaret(hWin)
  386.  
  387. // make sure it's displayed ...
  388. ShowWindow(hWin, nCmdShow)
  389.  
  390. // ... and up to date
  391. UpdateWindow(hWin)
  392.  
  393. return hWin
  394.  
  395.  
  396.