home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / PAINT.ZIP / PAINTDEF.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  8.1 KB  |  301 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Paint demo                     }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit PaintDef;
  9.  
  10. { This unit supplies the basic definitions used by all modules of the
  11.   paint program, as well as a few utility routines dealing mainly
  12.   with common dialogs.
  13. }
  14.  
  15. interface
  16.  
  17. uses ResDef, OStdDlgs, WinTypes, WinProcs, Objects;
  18.  
  19. var 
  20.   DashedPen: HPen;
  21.  
  22. type
  23.  
  24.   PPaintTool = ^TPaintTool;
  25.  
  26.   { The state object is used for communication among modules of the
  27.     paint program. It records the state of drawing, i.e., what has
  28.     been drawn, what colors, pen, brush, etc. are selected, what the
  29.     current screen selection is, etc. Only one state object should
  30.     exist for each paint screen.
  31.   }
  32.  
  33.   PState = ^TState;
  34.   TState = record
  35.     PaintTool: PPaintTool;      { Tool currently in use }
  36.     MemDC: HDC;            { Display contex with the offscreen Bitmap }
  37.     Offset: TPoint;        { Offset of Bitmap origin relative to screen }
  38.     BitmapSize: TPoint;            { Size of current bitmap }
  39.     IsDirtyBitmap: Boolean;      { Records when the Bitmap has been modified }
  40.     Selection: TRect;           { Coordinates of current screen selection }
  41.     SelectionBM: HBitmap;       { Contents of current screen selection }
  42.     PenSize: Integer;           { Current line width selected }
  43.     PenColor: TColorRef;        { Current line color selected }
  44.     BrushColor: TColorRef;      { Current fill color selected }
  45.   end;
  46.  
  47.  
  48.   { Paint tools are the basic entities that cause painting to be done
  49.     in the paint program. PaintTool defines the interface required by
  50.     all PaintTools, but no implementation (save initialization).
  51.   }
  52.  
  53.   TPaintTool = object(TObject)
  54.     Icon: HIcon;                { Icon associated with tool }
  55.     Cursor: HCursor;            { Cursor to be displayed when tool is
  56.                                   in use }
  57.     State: PState;              { Current state of drawing }
  58.     Window: HWnd;               { Window tool is operating on }
  59.     DC: HDC;                    { Screen display context to be operated on }
  60.  
  61.     { Creation and activation }
  62.     constructor Init(AState: PState; IconName, CursorName: PChar);
  63.     procedure Select; virtual;
  64.     procedure Deselect; virtual;
  65.  
  66.     { Actions initiated by mouse action }
  67.     procedure MouseDown(AWindow: HWnd; X, Y: Integer;
  68.       AState: PState); virtual;
  69.     procedure MouseMove(X, Y: Integer); virtual;
  70.     procedure MouseUp; virtual;
  71.     procedure DrawBegin(X, Y: Integer); virtual;
  72.     procedure DrawTo(X, Y: Integer); virtual;
  73.     procedure DrawEnd; virtual;
  74.  
  75.     { Utility routines used by mouse actions }
  76.     procedure PickUpSelection(aDC: HDC; Left, Top, aWidth, aHeight: Integer);
  77.       virtual;
  78.     procedure ReleaseSelection; virtual;
  79.     procedure DropSelection; virtual;
  80.  
  81.     { Actions initiated by keyboard }
  82.     procedure Char(Key, Count, lParamHi: Word); virtual;
  83.   end;
  84.  
  85. { Utility routines }
  86.  
  87. { Dialog Interactions }
  88.  
  89. { Display a message in a dialog with certain standard buttons.
  90. }
  91. function Ask(Quest: PChar): Boolean;            { Yes/No }
  92. function AskCancel(Quest: PChar): Integer;      { Yes/No/Cancel }
  93. function Confirm(Msg: PChar): Boolean;          { Ok/Cancel }
  94. procedure Tell(Msg: PChar);                     { Ok }
  95.  
  96.   Display standard file dialogs. Path may contain a mask (e.g.,
  97.   '*.pas') and contains full path name on return. Function return
  98.   value is True if file was selected, False on Cancel. 
  99. }
  100. function FileOpenDialog(Path: PChar): Boolean;  { File open }
  101. function FileSaveDialog(Path: PChar): Boolean;  { Filename selection }
  102.  
  103. { Other }
  104. function CreateCompatibleDCW(HWindow: Hwnd): HDC;
  105.  
  106. implementation
  107.  
  108. uses OWindows;
  109.  
  110. { TPaintTool }
  111.  
  112. { Default initialization of a Paint Tool.
  113. }
  114. constructor TPaintTool.Init(AState: PState; IconName, CursorName: PChar);
  115. begin
  116.   TObject.Init;
  117.   State := AState;
  118.   Icon := LoadIcon(HInstance, IconName);
  119.   Cursor := LoadCursor(HInstance, CursorName);
  120. end;
  121.  
  122. { Set up the paint tool to be the currently used tool.
  123. }
  124. procedure TPaintTool.Select;
  125. begin
  126.   State^.PaintTool := @Self;
  127. end;
  128.  
  129. {
  130.   Prepare the paint tool to no longer be the currently used tool.
  131. }
  132. procedure TPaintTool.Deselect;
  133. begin
  134. end;
  135.  
  136. { Actions initiated by mouse actions. }
  137.  
  138. { Action to be taken when the mouse button is pressed down (and the
  139.   tool is the currently used tool).
  140. }
  141. procedure TPaintTool.MouseDown(AWindow: HWnd; X, Y: Integer;
  142.   AState: PState);
  143. begin
  144. end;
  145.  
  146. { Action to be taken when the mouse button is down and the mouse is moved.
  147. }
  148. procedure TPaintTool.MouseMove(X, Y: Integer);
  149. begin
  150. end;
  151.  
  152. { Action to be taken when the mouse button is released.
  153. }
  154. procedure TPaintTool.MouseUp;
  155. begin
  156. end;
  157.  
  158. { Prepare the tool to begin drawing. Used by tools whose actions are
  159.   in response to mouse clicks. Their drawing actions are divided into
  160.   three phases: 1) DrawBegin on mouse down, 2) DrawTo when the mouse
  161.   is moved, and 3) DrawEnd when the mouse is released.
  162. }
  163. procedure TPaintTool.DrawBegin(X, Y: Integer);
  164. begin
  165. end;
  166.  
  167. { Perform the tool drawing action.
  168. }
  169. procedure TPaintTool.DrawTo(X, Y: Integer);
  170. begin
  171. end;
  172.  
  173. { Prepare the tool to stop drawing.
  174. }
  175. procedure TPaintTool.DrawEnd;
  176. begin
  177. end;
  178.  
  179.  
  180. { Utility routines used by mouse action routines.
  181.  
  182. { Prepare the rectangle selected on the screen to be treated as
  183.   a distinct entity. E.g., for dragging or cutting.
  184. }
  185. procedure TPaintTool.PickUpSelection(aDC: HDC; Left, Top, 
  186.                                                aWidth, aHeight: Integer);
  187. begin
  188. end;
  189.  
  190. { Release the current selection without modifying the current Bitmap.
  191. }
  192. procedure TPaintTool.ReleaseSelection;
  193. begin
  194. end;
  195.  
  196. { Copy the current selection onto the current Bitmap and release the
  197.   selection. 
  198. }
  199. procedure TPaintTool.DropSelection;
  200. begin
  201. end;
  202.  
  203. { Action initiated by the keyboard.
  204.  
  205. { Action to be taken when a non-system key is pressed. That is, not an
  206.   "alt" or other specially interpreted key-stroke.
  207. }
  208. procedure TPaintTool.Char(Key, Count, lParamHi: Word);
  209. begin
  210. end;
  211.  
  212. { Utility routines }
  213.  
  214. { Display a message in a dialog box with certain common buttons.
  215. }
  216. { Yes/No }
  217. function Ask(Quest: PChar): Boolean;
  218. begin
  219.   Ask := MessageBox(0, Quest, '', mb_YesNo) = id_Yes;
  220. end;
  221.  
  222. { Yes/No/Cancel }
  223. function AskCancel(Quest: PChar): Integer;
  224. begin
  225.   AskCancel := MessageBox(0, Quest, '', mb_YesNoCancel);
  226. end;
  227.  
  228. { Ok/Cancel }
  229. function Confirm(Msg: PChar): Boolean;
  230. begin
  231.   Confirm := MessageBox(0, Msg, '', mb_OkCancel) = id_OK;
  232. end;
  233.  
  234. { Ok }
  235. procedure Tell(Msg: PChar);
  236. begin
  237.   MessageBox(0, Msg, '', mb_Ok);
  238. end;
  239.  
  240.  
  241. { File Dialogs }
  242.  
  243. { Display a standard file dialog. Path will be filled in with the
  244.   selected filename (full path). Which is a "sd_" constant specifying
  245.   which file dialog to display.
  246. }
  247. function FileDialog(var Path: PChar; Which: PChar): Boolean;
  248. begin
  249.   FileDialog := 
  250.   Application^.ExecDialog(new(PFileDialog, Init(Application^.MainWindow,
  251.     Which, Path))) = id_OK;
  252. end;
  253.  
  254. { Standard file open. (Select an existing file.) }
  255. function FileOpenDialog(Path: PChar): Boolean;
  256. begin
  257.   FileOpenDialog := FileDialog(Path, PChar(sd_FileOpen));
  258. end;
  259.  
  260. { Standard file save. (Select a new or existing file.) }
  261. function FileSaveDialog(Path: PChar): Boolean;
  262. begin
  263.   FileSaveDialog := FileDialog(Path, PChar(sd_FileSave));
  264. end;
  265.  
  266. { Other }
  267.  
  268. { Given a window, return a drawing context that is compatible with
  269.   that window.
  270. }
  271. function CreateCompatibleDCW(HWindow: Hwnd): HDC;
  272. var
  273.   DC: HDC;
  274. begin
  275.   DC := GetDC(HWindow);
  276.   CreateCompatibleDCW :=  CreateCompatibleDC(DC);
  277.   ReleaseDC(HWindow, DC);
  278. end;
  279.  
  280. { Deal with deinitialization of unit. }
  281. var
  282.   SaveExit: Pointer;
  283.  
  284. procedure PaintDefExit;
  285. far;
  286. begin
  287.   DeleteObject(DashedPen);
  288.   ExitProc := SaveExit;
  289. end;
  290.  
  291. { initialization }
  292. begin
  293.   { A pen that draws a dashed line. }
  294.   DashedPen := CreatePen(ps_Dot, 1, $000000);
  295.  
  296.   { Set up unit de-initialization }
  297.   SaveExit := ExitProc;
  298.   ExitProc := @PaintDefExit;
  299. end.
  300.