home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / PENPAL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.1 KB  |  255 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit PenPal;
  9.  
  10. interface
  11.  
  12. uses WinTypes, Objects, OWindows, ODialogs, Pen;
  13.  
  14. {$R PENPAL.RES}
  15.  
  16. const
  17.   id_Add = 201;
  18.   id_Del = 202;
  19.   id_Lines = 6000;
  20.   MaxPens = 9;
  21.  
  22. type
  23.   PBitButton = ^TBitButton;
  24.   TBitButton = object(TButton)
  25.     procedure Disable;
  26.     procedure Enable;
  27.   end;
  28.  
  29.   PPenPic = ^TPenPic;
  30.   TPenPic = object(TWindow)
  31.     PenSet: PCollection;
  32.     SelectedPen: Integer;
  33.     UpPic, DownPic: HBitmap;
  34.     constructor Init(AParent: PWindowsObject);
  35.     destructor Done; virtual;
  36.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  37.     procedure AddPen(APen: PPen);
  38.     procedure DeletePen;
  39.     procedure SetupWindow; virtual;
  40.     procedure WMLButtonDown(var Msg: TMessage); virtual wm_First + wm_LButtonDown;
  41.   end;
  42.  
  43.   PPenPalette = ^TPenPalette;
  44.   TPenPalette = object(TWindow)
  45.     AddBtn, DelBtn: PBitButton;
  46.     Pens: PPenPic;
  47.     CurrentPen: PPen;
  48.     constructor Init(AParent: PWindowsObject; ATitle: PChar; APen: PPen);
  49.     destructor Done; virtual;
  50.     function CanClose: Boolean; virtual;
  51.     function GetClassName: PChar; virtual;
  52.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  53.     procedure Grow;
  54.     procedure SetupWindow; virtual;
  55.     procedure Shrink;
  56.     procedure IDAdd(var Msg: TMessage); virtual id_First + id_Add;
  57.     procedure IDDel(var Msg: TMessage); virtual id_First + id_Del;
  58.     procedure WMNCActivate(var Msg: TMessage); virtual wm_First + wm_NCActivate;
  59.   end;
  60.  
  61. implementation
  62.  
  63. uses WinProcs;
  64.  
  65.  
  66. procedure TBitButton.Disable;
  67. begin
  68.   if HWindow <> 0 then EnableWindow(HWindow, False);
  69. end;
  70.  
  71. procedure TBitButton.Enable;
  72. begin
  73.   if HWindow <> 0 then EnableWindow(HWindow, True);
  74. end;
  75.  
  76. constructor TPenPalette.Init(AParent: PWindowsObject; ATitle: PChar; APen: PPen);
  77. begin
  78.   TWindow.Init(AParent, ATitle);
  79.   with Attr do
  80.   begin
  81.     Style := Style or ws_PopupWindow or ws_Caption or ws_SysMenu;
  82.     x := 0;
  83.     Y := 0;
  84.     W := 132;
  85.     H := GetSystemMetrics(sm_CYCaption) + 42;
  86.   end;
  87.   AddBtn := New(PBitButton, Init(@Self, id_Add, '&Add pen', 0, 0, 65, 41, True));
  88.   DelBtn := New(PBitButton, Init(@Self, id_Del, '&Del pen', 65, 0, 65, 41, False));
  89.   CurrentPen := APen;
  90.   Pens := New(PPenPic, Init(@Self));
  91. end;
  92.  
  93. destructor TPenPalette.Done;
  94. begin
  95.   Dispose(Pens, Done);
  96.   TWindow.Done;
  97. end;
  98.  
  99. function TPenPalette.CanClose: Boolean;
  100. begin
  101.   Show(sw_Hide);
  102.   CanClose := False;
  103. end;
  104.  
  105. function TPenPalette.GetClassName: PChar;
  106. begin
  107.   GetClassName := 'PenPalette';
  108. end;
  109.  
  110. procedure TPenPalette.GetWindowClass(var AWndClass: TWndClass);
  111. begin
  112.   TWindow.GetWindowClass(AWndClass);
  113.   AWndClass.hbrBackground := GetStockObject(LtGray_Brush);
  114. end;
  115.  
  116. procedure TPenPalette.Grow;
  117. var
  118.   WindowRect: TRect;
  119. begin
  120.   GetWindowRect(HWindow, WindowRect);
  121.   with WindowRect do
  122.     MoveWindow(HWindow, left, top, right - left, bottom - top + 40, True);
  123. end;
  124.  
  125. procedure TPenPalette.SetupWindow;
  126. begin
  127.   TWindow.SetupWindow;
  128.   DelBtn^.Disable;
  129. end;
  130.  
  131. procedure TPenPalette.Shrink;
  132. var
  133.   WindowRect: TRect;
  134. begin
  135.   GetWindowRect(HWindow, WindowRect);
  136.   with WindowRect do
  137.     MoveWindow(HWindow, left, top, right - left, bottom - top - 40, True);
  138. end;
  139.  
  140. procedure TPenPalette.IDAdd(var Msg: TMessage);
  141. begin
  142.   Pens^.AddPen(CurrentPen);
  143. end;
  144.  
  145. procedure TPenPalette.IDDel(var Msg: TMessage);
  146. begin
  147.   Pens^.DeletePen;
  148. end;
  149.  
  150. procedure TPenPalette.WMNCActivate(var Msg: TMessage);
  151. begin
  152.   Msg.wParam := 1;
  153.   DefWndProc(Msg);
  154. end;
  155.  
  156. constructor TPenPic.Init(AParent: PWindowsObject);
  157. begin
  158.   TWindow.Init(AParent, nil);
  159.   Attr.Style := ws_Child or ws_Visible;
  160.   PenSet := New(PCollection, Init(MaxPens, 0));
  161.   SelectedPen := -1;
  162.   UpPic := LoadBitmap(HInstance, 'PAL_UP');
  163.   DownPic := LoadBitmap(HInstance, 'PAL_DOWN');
  164. end;
  165.  
  166. destructor TPenPic.Done;
  167. begin
  168.   DeleteObject(UpPic);
  169.   DeleteObject(DownPic);
  170.   Dispose(PenSet, Done);
  171.   TWindow.Done;
  172. end;
  173.  
  174. procedure TPenPic.AddPen(APen: PPen);
  175. begin
  176.   SelectedPen := PenSet^.Count;
  177.   PenSet^.Insert(New(PPen, InitLike(APen)));
  178.   with PPenPalette(Parent)^ do
  179.   begin
  180.     DelBtn^.Enable;
  181.     if PenSet^.Count >= MaxPens then
  182.       AddBtn^.Disable;
  183.     Grow;
  184.   end;
  185. end;
  186.  
  187. procedure TPenPic.DeletePen;
  188. begin
  189.   if SelectedPen > -1 then
  190.   begin
  191.     PenSet^.AtFree(SelectedPen);
  192.     PenSet^.Pack;
  193.     SelectedPen := -1;
  194.     with PPenPalette(Parent)^ do
  195.     begin
  196.       AddBtn^.Enable;
  197.       DelBtn^.Disable;
  198.       Shrink;
  199.     end;
  200.   end;
  201. end;
  202.  
  203. procedure TPenPic.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  204. var
  205.   PenCount: Integer;
  206.   MemDC: HDC;
  207.   TheBitmap, OrigBitmap: HBitmap;
  208.  
  209.   procedure ShowPen(P: PPen); far;
  210.   begin
  211.     Inc(PenCount);
  212.     if PenCount = SelectedPen then
  213.       TheBitmap := DownPic
  214.     else TheBitmap := UpPic;
  215.     SelectObject(MemDC, GetStockObject(LtGray_Brush));
  216.     SelectObject(MemDC, TheBitmap);
  217.     BitBlt(PaintDC, 0, PenCount * 40, 128, 40, MemDC, 0, 0, SrcCopy);
  218.  
  219.     P^.Select(PaintDC);
  220.     MoveTo(PaintDC, 15, PenCount * 40 + 20);
  221.     LineTo(PaintDC, 115, PenCount * 40 + 20);
  222.     P^.Delete;
  223.   end;
  224.  
  225. begin
  226.   MemDC := CreateMemoryDC;
  227.   OrigBitmap := SelectObject(MemDC, UpPic);
  228.   PenCount := -1;
  229.   PenSet^.ForEach(@ShowPen);
  230.   SelectObject(MemDC, OrigBitmap);
  231.   DeleteDC(MemDC);
  232. end;
  233.  
  234. procedure TPenPic.SetupWindow;
  235. var
  236.   ClientRect: TRect;
  237. begin
  238.   TWindow.SetupWindow;
  239.   GetClientRect(Parent^.HWindow, ClientRect);
  240.   with ClientRect do
  241.     MoveWindow(HWindow, 1, bottom - top + 1, 128,
  242.       40 * MaxPens, False);
  243. end;
  244.  
  245. procedure TPenPic.WMLButtonDown(var Msg: TMessage);
  246. begin
  247.   SelectedPen := Msg.LParamHi div 40;
  248.   with PPen(PenSet^.At(SelectedPen))^ do
  249.     PPenPalette(Parent)^.CurrentPen^.SetAttributes(Style, Width, Color);
  250.   PPenPalette(Parent)^.DelBtn^.Enable;
  251.   InvalidateRect(HWindow, nil, False);
  252. end;
  253.  
  254. end.
  255.