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

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Paint demo                     }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit LineBar;
  9.  
  10. { This unit defines a line thickness selection window for the paint program.
  11.   The line bar is responsible for displaying the available and current line
  12.   widths and provides the interface to select the current line width.
  13. }
  14.  
  15. interface
  16.  
  17. uses PaintDef, WinTypes, WinProcs, OWindows;
  18.  
  19. const
  20.   LineCount = 8;    { Number of line widths available }
  21.   LineBarWidth = LineCount * 4 + 6 + (1 + 2 + 3 + 4 + 5 + 7 + 9 + 12);
  22.               { Total width of window }
  23.  
  24. type
  25.  
  26.   PLineBar = ^TLineBar;
  27.   TLineBar = object(TWindow)
  28.     State: PState;
  29.     
  30.     { Creation }
  31.     constructor Init(AParent: PWindowsObject; AState: PState);
  32.  
  33.     { Display }
  34.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  35.  
  36.     { Window manager interface }
  37.     procedure WMLButtonDown(var Msg: TMessage);
  38.       virtual wm_First + wm_LButtonDown;
  39.   end;
  40.  
  41.  
  42. implementation
  43.  
  44. const
  45.   LineWidth: array[0..LineCount - 1] of Integer = (
  46.     1, 2, 3, 4, 5, 7, 9, 12);         { The available line widths }
  47.  
  48. { Create a line bar.
  49. }
  50. constructor TLineBar.Init(AParent: PWindowsObject; AState: PState);
  51. begin
  52.   TWindow.Init(AParent, nil);
  53.   Attr.Style := ws_Border or ws_Child or ws_Visible;
  54.   State := AState;
  55. end;
  56.  
  57. { Draw the line bar. A sample line of each availble is drawn vertically
  58.   (samples arrayed horizontally).
  59.   Each sample line is drawn by filling in the rectangle it occupies rather
  60.   than as a true line for ease of computation of position.
  61. }
  62. procedure TLineBar.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  63. var
  64.   I, X, W: Integer;  { Sample number; X position of sample; Sample width }
  65.   R: TRect;         { Rect that sample will occupy }
  66.  
  67. { Draw triangular notch marks to indicate the currently selected width.
  68. }
  69. procedure Notch(Y, DY: Integer);
  70. var
  71.   L: Integer;
  72. begin
  73.   for L := 3 downto 0 do
  74.   begin
  75.     MoveTo(PaintDC, X + W div 2 - L, Y);
  76.     LineTo(PaintDC, X + W div 2 + L + 1, Y);
  77.     Inc(Y, DY);
  78.   end;
  79. end;
  80.  
  81. begin
  82.   X := 4;
  83.   for I := 0 to LineCount - 1 do
  84.   begin
  85.     { Draw the line sample }
  86.     W := LineWidth[I];
  87.     SetRect(R, X, 5, X + W, 25);
  88.     FillRect(PaintDC, R, GetStockObject(black_Brush));
  89.  
  90.     { Mark the currently selected width }
  91.     if W = State^.PenSize then
  92.     begin
  93.       Notch(0, 1);
  94.       Notch(29, -1);
  95.     end;
  96.     Inc(X, W + 4);
  97.   end;
  98. end;
  99.  
  100. { Set the currently selected line widht to be that whose sample line was
  101.   pressed and update the display.
  102. }
  103. procedure TLineBar.WMLButtonDown(var Msg: TMessage);
  104. var
  105.   I, X, W: Integer;
  106. begin
  107.   X := 2;
  108.   for I := 0 to LineCount - 1 do
  109.   begin
  110.     W := LineWidth[I];
  111.     if (Msg.LParamLo >= X) and (Msg.LParamLo < X + W + 4) then
  112.     begin
  113.       State^.PenSize := W;
  114.       InvalidateRect(HWindow, nil, True);
  115.       Exit;
  116.     end;
  117.     Inc(X, W + 4);
  118.   end;
  119. end;
  120.  
  121. end.
  122.