home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / draw.pak / DRAW.CPP next >
C/C++ Source or Header  |  1997-07-23  |  6KB  |  236 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <owl\framewin.h>
  7. #include <owl\dc.h>
  8. #include <owl\opensave.h>
  9. #include <owl\inputdia.h>
  10. #include <cstring.h>
  11. #include <dir.h>
  12. #include "draw.rh"
  13.  
  14. class TDrawWindow : public TFrameWindow {
  15.   public:
  16.     TDrawWindow(TWindow* parent, const char far* title);
  17.  
  18.     void CleanupWindow();
  19.  
  20.   protected:
  21.     void EvLButtonDown(UINT modKeys, TPoint& point);
  22.     void EvLButtonUp(UINT modKeys, TPoint& point);
  23.     void EvMouseMove(UINT modKeys, TPoint& point);
  24.     void EvRButtonDown(UINT modKeys, TPoint& point);
  25.     void CmRecord();
  26.     void CmStop();
  27.     void CmPlay();
  28.  
  29.   private:
  30.     TClientDC*    HoldDC;
  31.     TMetaFileDC*  MetaFileDC;
  32.     BOOL          Recording;
  33.     BOOL          ButtonDown;
  34.  
  35.     TOpenSaveDialog::TData  FileData;
  36.  
  37.     BOOL CloseMetaFile();
  38.     void DisplayMessage(const string& msg);
  39.  
  40.   DECLARE_RESPONSE_TABLE(TDrawWindow);
  41. };
  42.  
  43. DEFINE_RESPONSE_TABLE1(TDrawWindow, TFrameWindow)
  44.   EV_WM_LBUTTONDOWN,
  45.   EV_WM_LBUTTONUP,
  46.   EV_WM_MOUSEMOVE,
  47.   EV_WM_RBUTTONDOWN,
  48.   EV_COMMAND(CM_RECORD, CmRecord),
  49.   EV_COMMAND(CM_STOP, CmStop),
  50.   EV_COMMAND(CM_PLAY, CmPlay),
  51. END_RESPONSE_TABLE;
  52.  
  53.  
  54. TDrawWindow::TDrawWindow(TWindow* parent, const char far* title)
  55.   : TFrameWindow(parent, title)
  56. {
  57.   ButtonDown = FALSE;
  58.   HoldDC = 0;
  59.   MetaFileDC = 0;
  60.   Recording = FALSE;
  61.   AssignMenu("DRAW_MENU");
  62.  
  63.   //
  64.   // Initialize file open data.
  65.   //
  66.   FileData.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;
  67.   FileData.SetFilter("MetaFiles (*.WMF)|*.wmf|");
  68. }
  69.  
  70. //
  71. // CleanupWindow(). Close and destroy meta file (disk file remains);
  72. //
  73. void
  74. TDrawWindow::CleanupWindow()
  75. {
  76.   CmStop();
  77. }
  78.  
  79. //
  80. // Define a TDrawWindow's response to an incoming "left-button-down"
  81. // message.  In response, TDrawWindows prepare to draw a line,
  82. // setting the current position in client area, retrieving a display
  83. // context from MS-Windows, and capturing mouse input.  Write drawing to
  84. // metafile if recording.
  85. //
  86. void
  87. TDrawWindow::EvLButtonDown(UINT, TPoint& point)
  88. {
  89.   if (!ButtonDown) {
  90.     ButtonDown = TRUE;
  91.     SetCapture();    // Direct all subsequent mouse input to this window
  92.     HoldDC = new TClientDC(*this);
  93.     HoldDC->MoveTo(point);
  94.     if (Recording)
  95.       MetaFileDC->MoveTo(point);
  96.   }
  97. }
  98.  
  99. //
  100. // Define a TDrawWindow's response to an incoming "mouse-move"
  101. // message.  In response, TDrawWindows draw a line using the new
  102. // position of the mouse. Write drawing to metafile if recording
  103. //
  104. void
  105. TDrawWindow::EvMouseMove(UINT /*modKeys*/, TPoint& point)
  106. {
  107.   if (ButtonDown) {
  108.     HoldDC->LineTo(point);   // draw the line
  109.     if (Recording)
  110.       MetaFileDC->LineTo(point);
  111.   }
  112. }
  113.  
  114. //
  115. // Define a TDrawWindow's response to an incoming "left-button-up"
  116. // message.  In response, TDrawWindows "cleanup" required after a
  117. // line is drawn, releasing the display context to MS-Windows, and
  118. // releasing mouse input.
  119. //
  120. void
  121. TDrawWindow::EvLButtonUp(UINT, TPoint&)
  122. {
  123.   if (ButtonDown) {
  124.     ReleaseCapture();
  125.     delete HoldDC;
  126.     HoldDC = 0;
  127.     ButtonDown = FALSE;
  128.   }
  129. }
  130.  
  131. //
  132. // Define a TDrawWindow's response to an incoming
  133. // "right-button-down" message.  In response, TDrawWindows "clear"
  134. // their client area.
  135. // Invalidate the entire window--Windows will send WM_PAINT message to the
  136. // window.  If recording close metafile and display message.
  137. //
  138. void
  139. TDrawWindow::EvRButtonDown(UINT, TPoint&)
  140. {
  141.   Invalidate(TRUE);
  142.   CmStop();
  143. }
  144.  
  145. //
  146. // Ask user for a filename and create metafile.  Set flag indicating that
  147. // we are recording metafile.  If already recording then close it and display
  148. // message.
  149. //
  150. void
  151. TDrawWindow::CmRecord()
  152. {
  153.   *FileData.FileName = 0;
  154.   if (TFileSaveDialog(this, FileData, 0, "Record MetaFile").Execute() == IDOK) {
  155.     CmStop();
  156.     MetaFileDC = new TMetaFileDC(FileData.FileName);
  157.     Recording = TRUE;
  158.     Invalidate(TRUE);                       // clear the client area.
  159.   }
  160. }
  161.  
  162. //
  163. // Stop any current MetaFile recording.
  164. //
  165. void
  166. TDrawWindow::CmStop()
  167. {
  168.   if (CloseMetaFile())
  169.     DisplayMessage("Current MetaFile recording complete.");
  170. }
  171.  
  172. //
  173. // Ask user for a file to play.  Assumes file is a metafile (PlayOnto will
  174. // fail if it isn't).  If already recording then close it and display message.
  175. // Before playing metafile clear client area.
  176. //
  177. void
  178. TDrawWindow::CmPlay()
  179. {
  180.   *FileData.FileName = 0;
  181.   if (TFileOpenDialog(this, FileData, 0, "Open MetaFile").Execute() == IDOK) {
  182.     CmStop();
  183.     Invalidate(TRUE);                       // clear the client area.
  184.     UpdateWindow();
  185.     TMetaFilePict mf(FileData.FileName);
  186.     HoldDC = new TClientDC(*this);
  187.     mf.PlayOnto(*HoldDC, GetClientRect().Size());
  188.     delete HoldDC;
  189.     HoldDC = 0;
  190.   }
  191. }
  192.  
  193. //
  194. // Close and destroy metafile, disk file remains.  Return TRUE if we were
  195. // recording, else FALSE.
  196. //
  197. BOOL
  198. TDrawWindow::CloseMetaFile()
  199. {
  200.   if (Recording) {
  201.     delete MetaFileDC;
  202.     Recording = FALSE;
  203.     return TRUE;
  204.   }
  205.   return FALSE;
  206. }
  207.  
  208. //
  209. // Display a message to user.
  210. //
  211. void
  212. TDrawWindow::DisplayMessage(const string& msg)
  213. {
  214.   MessageBox(msg.c_str(), GetApplication()->GetName(), MB_OK);
  215. }
  216.  
  217. //----------------------------------------------------------------------------
  218.  
  219. class TDrawApp : public TApplication {
  220.   public:
  221.     TDrawApp() : TApplication("Draw") {}
  222.     void InitMainWindow();
  223. };
  224.  
  225. void
  226. TDrawApp::InitMainWindow()
  227. {
  228.   MainWindow = new TDrawWindow(0, "Draw Away!");
  229. }
  230.  
  231. int
  232. OwlMain(int /*argc*/, char* /*argv*/ [])
  233. {
  234.   return TDrawApp().Run();
  235. }
  236.